Skip to content

Commit

Permalink
knet: make internal functions static
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
  • Loading branch information
fabbione committed Nov 27, 2010
1 parent cc17b13 commit bff2d31
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 140 deletions.
272 changes: 134 additions & 138 deletions knet.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,138 @@ STATIC int knet_sockfd = 0;
STATIC pthread_mutex_t knet_eth_mutex = PTHREAD_MUTEX_INITIALIZER;

/* forward declarations */
STATIC int knet_read_pipe(int fd, char **file, size_t *length);
STATIC char *knet_get_v4_broadcast(const char *ip_addr, const char *prefix);
STATIC int knet_set_ip(struct knet_eth *knet_eth, const char *command,
const char *ip_addr, const char *prefix);
STATIC void knet_close_unsafe(struct knet_eth *knet_eth);
STATIC int knet_execute_shell(const char *command);

static int knet_read_pipe(int fd, char **file, size_t *length)
{
char buf[4096];
int n;
int done = 0;

*file = NULL;
*length = 0;

memset(buf, 0, sizeof(buf));

while (!done) {

n = read(fd, buf, sizeof(buf));

if (n < 0) {
if (errno == EINTR)
continue;

if (*file)
free(*file);

return n;
}

if (n == 0 && (!*length))
return 0;

if (n == 0)
done = 1;

if (*file)
*file = realloc(*file, (*length) + n + done);
else
*file = malloc(n + done);

if (!*file)
return -1;

memcpy((*file) + (*length), buf, n);
*length += (done + n);
}

/* Null terminator */
(*file)[(*length) - 1] = 0;

return 0;
}

STATIC int knet_execute_shell(const char *command)
{
pid_t pid;
int status, err = 0;
int fd[2];
char *data = NULL;
size_t size = 0;

if (command == NULL) {
errno = EINVAL;
return -1;
}

err = pipe(fd);
if (err)
goto out_clean;

pid = fork();
if (pid < 0) {
err = pid;
goto out_clean;
}

if (pid) { /* parent */

close(fd[1]);
err = knet_read_pipe(fd[0], &data, &size);
if (err)
goto out_clean;

waitpid(pid, &status, 0);
if (!WIFEXITED(status)) {
log_error("shell: child did not terminate normally");
err = -1;
goto out_clean;
}
if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
log_error("shell: child returned %d", WEXITSTATUS(status));
err = -1;
goto out_clean;
}
} else { /* child */
close(0);
close(1);
close(2);

close(fd[0]);
if(dup2(fd[1], 1) < 0)
log_error("Unable to redirect stdout");
if(dup2(fd[1], 2) < 0)
log_error("Unable to redirect stderr");
close(fd[1]);

execlp("/bin/sh", "/bin/sh", "-c", command, NULL);
exit(EXIT_FAILURE);
}

out_clean:
close(fd[0]);
close(fd[1]);

if ((size) && (err)) {
log_error("%s", data);
free(data);
}

return err;
}

static void knet_close_unsafe(struct knet_eth *knet_eth)
{
if (!knet_eth)
return;

if (knet_eth->knet_etherfd)
close(knet_eth->knet_etherfd);

free(knet_eth);

return;
}

struct knet_eth *knet_open(char *dev, size_t dev_size)
{
Expand Down Expand Up @@ -88,19 +215,6 @@ struct knet_eth *knet_open(char *dev, size_t dev_size)
return NULL;
}

STATIC void knet_close_unsafe(struct knet_eth *knet_eth)
{
if (!knet_eth)
return;

if (knet_eth->knet_etherfd)
close(knet_eth->knet_etherfd);

free(knet_eth);

return;
}

void knet_close(struct knet_eth *knet_eth)
{
pthread_mutex_lock(&knet_eth_mutex);
Expand Down Expand Up @@ -268,7 +382,7 @@ int knet_set_down(struct knet_eth *knet_eth)
return err;
}

STATIC char *knet_get_v4_broadcast(const char *ip_addr, const char *prefix)
static char *knet_get_v4_broadcast(const char *ip_addr, const char *prefix)
{
int prefix_len;
struct in_addr mask;
Expand All @@ -291,7 +405,7 @@ STATIC char *knet_get_v4_broadcast(const char *ip_addr, const char *prefix)
return strdup(inet_ntoa(broadcast));
}

STATIC int knet_set_ip(struct knet_eth *knet_eth, const char *command,
static int knet_set_ip(struct knet_eth *knet_eth, const char *command,
const char *ip_addr, const char *prefix)
{
char *broadcast = NULL;
Expand Down Expand Up @@ -349,121 +463,3 @@ int knet_del_ip(struct knet_eth *knet_eth, const char *ip_addr, const char *pref

return err;
}

STATIC int knet_read_pipe(int fd, char **file, size_t *length)
{
char buf[4096];
int n;
int done = 0;

*file = NULL;
*length = 0;

memset(buf, 0, sizeof(buf));

while (!done) {

n = read(fd, buf, sizeof(buf));

if (n < 0) {
if (errno == EINTR)
continue;

if (*file)
free(*file);

return n;
}

if (n == 0 && (!*length))
return 0;

if (n == 0)
done = 1;

if (*file)
*file = realloc(*file, (*length) + n + done);
else
*file = malloc(n + done);

if (!*file)
return -1;

memcpy((*file) + (*length), buf, n);
*length += (done + n);
}

/* Null terminator */
(*file)[(*length) - 1] = 0;

return 0;
}

int knet_execute_shell(const char *command)
{
pid_t pid;
int status, err = 0;
int fd[2];
char *data = NULL;
size_t size = 0;

if (command == NULL) {
errno = EINVAL;
return -1;
}

err = pipe(fd);
if (err)
goto out_clean;

pid = fork();
if (pid < 0) {
err = pid;
goto out_clean;
}

if (pid) { /* parent */

close(fd[1]);
err = knet_read_pipe(fd[0], &data, &size);
if (err)
goto out_clean;

waitpid(pid, &status, 0);
if (!WIFEXITED(status)) {
log_error("shell: child did not terminate normally");
err = -1;
goto out_clean;
}
if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
log_error("shell: child returned %d", WEXITSTATUS(status));
err = -1;
goto out_clean;
}
} else { /* child */
close(0);
close(1);
close(2);

close(fd[0]);
if(dup2(fd[1], 1) < 0)
log_error("Unable to redirect stdout");
if(dup2(fd[1], 2) < 0)
log_error("Unable to redirect stderr");
close(fd[1]);

execlp("/bin/sh", "/bin/sh", "-c", command, NULL);
exit(EXIT_FAILURE);
}

out_clean:
close(fd[0]);
close(fd[1]);

if ((size) && (err)) {
log_error("%s", data);
free(data);
}

return err;
}
2 changes: 0 additions & 2 deletions knet.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,4 @@ int knet_add_ip(struct knet_eth *knet_eth, const char *ip_addr,
int knet_del_ip(struct knet_eth *knet_eth, const char *ip_addr,
const char *prefix);

int knet_execute_shell(const char *command);

#endif
2 changes: 2 additions & 0 deletions tests/knet_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

extern int knet_sockfd;

extern int knet_execute_shell(const char *command);

static int is_if_in_system(char *name)
{
struct ifaddrs *ifap = NULL;
Expand Down

0 comments on commit bff2d31

Please sign in to comment.