diff options
-rw-r--r-- | src/havegecmd.c | 61 | ||||
-rw-r--r-- | src/haveged.c | 23 | ||||
-rw-r--r-- | src/haveged.h | 6 |
3 files changed, 51 insertions, 39 deletions
diff --git a/src/havegecmd.c b/src/havegecmd.c index c2acbc7..18ff322 100644 --- a/src/havegecmd.c +++ b/src/havegecmd.c @@ -51,7 +51,7 @@ struct ucred int socket_fd; -static int new_root( /* RETURN: status */ +static void new_root( /* RETURN: nothing */ const char *root, /* IN: path of the new root file system */ const volatile char *path, /* IN: path of the haveged executable */ char *const argv[], /* IN: arguments for the haveged process */ @@ -59,28 +59,35 @@ static int new_root( /* RETURN: status */ { int ret; - print_msg("%s: restart in new root: %s\n", params->daemon, root); + fprintf(stderr, "%s: restart in new root: %s\n", params->daemon, root); ret = chdir(root); if (ret < 0) { - print_msg("%s: can't change to working directory : %s\n", params->daemon, root); - return ret; + if (errno != ENOENT) + error_exit("can't change to working directory : %s", root); + else + fprintf(stderr, "%s: can't change to working directory : %s\n", params->daemon, root); } ret = chroot("."); if (ret < 0) { - print_msg("%s: can't change root directory\n", params->daemon); - return ret; + if (errno != ENOENT) + error_exit("can't change root directory"); + else + fprintf(stderr, "%s: can't change root directory\n", params->daemon); } ret = chdir("/"); if (ret < 0) { - print_msg("%s: can't change to working directory /\n", params->daemon); - return ret; + if (errno != ENOENT) + error_exit("can't change to working directory /"); + else + fprintf(stderr, "%s: can't change to working directory /\n", params->daemon); } ret = execv((const char *)path, argv); if (ret < 0) { - print_msg("%s: can't restart %s\n", params->daemon, path); - return ret; + if (errno != ENOENT) + error_exit("can't restart %s", path); + else + fprintf(stderr, "%s: can't restart %s\n", params->daemon, path); } - return 0; } /** @@ -222,7 +229,7 @@ int socket_handler( /* RETURN: closed file descriptor */ { struct ucred cred = {0}; unsigned char magic[2], *ptr; - int enqry; + char *enqry; char *optarg = NULL; socklen_t clen; int ret = -1, len; @@ -242,7 +249,7 @@ int socket_handler( /* RETURN: closed file descriptor */ optarg = calloc(alen, sizeof(char)); if (!optarg) - print_msg("can not allocate memory for message from UNIX socket"); + error_exit("can not allocate memory for message from UNIX socket"); ptr = (unsigned char*)optarg; len = alen; @@ -252,34 +259,36 @@ int socket_handler( /* RETURN: closed file descriptor */ clen = sizeof(struct ucred); ret = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &clen); if (ret < 0) { - print_msg("%s: can not get credentials from UNIX socket part1\n", params->daemon); + fprintf(stderr, "%s: can not get credentials from UNIX socket part1\n", params->daemon); goto out; } if (clen != sizeof(struct ucred)) { - print_msg("%s: can not get credentials from UNIX socket part2\n", params->daemon); + fprintf(stderr, "%s: can not get credentials from UNIX socket part2\n", params->daemon); goto out; } if (cred.uid != 0) { - enqry = -EPERM; + enqry = "\x15"; - ptr = (unsigned char *)&enqry; - len = sizeof(enqry); + ptr = (unsigned char *)enqry; + len = (int)strlen(enqry)+1; safeout(fd, ptr, len); } switch (magic[0]) { case MAGIC_CHROOT: + enqry = "\x6"; - enqry = new_root(optarg, path, argv, params); - ptr = (unsigned char *)&enqry; - len = sizeof(enqry); + ptr = (unsigned char *)enqry; + len = (int)strlen(enqry)+1; safeout(fd, ptr, len); + + new_root(optarg, path, argv, params); break; default: - enqry = -EINVAL; + enqry = "\x15"; - ptr = (unsigned char *)&enqry; - len = sizeof(enqry); + ptr = (unsigned char *)enqry; + len = (int)strlen(enqry)+1; safeout(fd, ptr, len); break; } @@ -323,7 +332,7 @@ ssize_t safein( /* RETURN: read bytes */ continue; if (errno == EAGAIN || errno == EWOULDBLOCK) break; - print_msg("Unable to read from socket: %d", socket_fd); + error_exit("Unable to read from socket: %d", socket_fd); } ptr = (char *) ptr + p; ret += p; @@ -352,7 +361,7 @@ void safeout( /* RETURN: nothing */ continue; if (errno == EPIPE || errno == EAGAIN || errno == EWOULDBLOCK) break; - print_msg("Unable to write to socket: %d", fd); + error_exit("Unable to write to socket: %d", fd); } ptr = (char *) ptr + p; len -= p; diff --git a/src/haveged.c b/src/haveged.c index d230c37..733d0da 100644 --- a/src/haveged.c +++ b/src/haveged.c @@ -118,7 +118,7 @@ static void set_watermark(int level); static void anchor_info(H_PTR h); static int get_runsize(unsigned int *bufct, unsigned int *bufrem, char *bp); static char *ppSize(char *buffer, double sz); -static void error_exit(const char *format, ...); +static void print_msg(const char *format, ...); static void run_app(H_PTR handle, H_UINT bufct, H_UINT bufres); static void show_meterInfo(H_UINT id, H_UINT event); @@ -338,9 +338,8 @@ int main(int argc, char **argv) } while (c!=-1); #ifndef NO_COMMAND_MODE if (params->setup & CMD_MODE) { - int ret = 0, len, answer; - char message[PATH_MAX+5], cmd[2]; - void * ptr; + int ret = 0, len; + char *ptr, message[PATH_MAX+5], answer[2], cmd[2]; fd_set read_fd; socket_fd = cmd_connect(params); @@ -373,7 +372,8 @@ int main(int argc, char **argv) ret = -1; break; } - ptr = &answer; + answer[0] = '\0'; + ptr = &answer[0]; len = sizeof(answer); FD_ZERO(&read_fd); @@ -392,7 +392,10 @@ int main(int argc, char **argv) close(socket_fd); if (ret < 0) goto err; - ret = answer; + if (answer[0] != '\x6') + ret = -1; + else + ret = 0; err: return ret; } @@ -403,10 +406,10 @@ int main(int argc, char **argv) else { if (socket_fd == -2) { fprintf(stderr, "%s: command socket already in use\n", params->daemon); - error_exit("%s: please check if there is another instance of haveged running\n", params->daemon); + fprintf(stderr, "%s: please check if there is another instance of haveged running\n", params->daemon); + fprintf(stderr, "%s: disabling command mode for this instance\n", params->daemon); } else { fprintf(stderr, "%s: can not initialize command socket: %s\n", params->daemon, strerror(errno)); - fprintf(stderr, "%s: disabling command mode for this instance\n", params->daemon); } } } @@ -713,7 +716,7 @@ static void anchor_info(H_PTR h) /** * Bail.... */ -static void error_exit( /* RETURN: nothing */ +void error_exit( /* RETURN: nothing */ const char *format, /* IN: msg format */ ...) /* IN: varadic args */ { @@ -831,7 +834,7 @@ static char *ppSize( /* RETURN: the formatted size */ /** * Execution notices - to stderr or syslog */ -void print_msg( /* RETURN: nothing */ +static void print_msg( /* RETURN: nothing */ const char *format, /* IN: format string */ ...) /* IN: args */ { diff --git a/src/haveged.h b/src/haveged.h index b3a91a0..9b1efaf 100644 --- a/src/haveged.h +++ b/src/haveged.h @@ -88,8 +88,8 @@ typedef struct { } H_METER; /** - * Execution notices - to stderr or syslog - */ -void print_msg(const char *, ...); + * Bail.... + */ +void error_exit(const char *, ...); #endif |