summaryrefslogtreecommitdiffstats
path: root/src/havegecmd.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/havegecmd.c')
-rw-r--r--src/havegecmd.c61
1 files changed, 26 insertions, 35 deletions
diff --git a/src/havegecmd.c b/src/havegecmd.c
index 18ff322..c2acbc7 100644
--- a/src/havegecmd.c
+++ b/src/havegecmd.c
@@ -51,7 +51,7 @@ struct ucred
int socket_fd;
-static void new_root( /* RETURN: nothing */
+static int new_root( /* RETURN: status */
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,35 +59,28 @@ static void new_root( /* RETURN: nothing */
{
int ret;
- fprintf(stderr, "%s: restart in new root: %s\n", params->daemon, root);
+ print_msg("%s: restart in new root: %s\n", params->daemon, root);
ret = chdir(root);
if (ret < 0) {
- 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);
+ print_msg("%s: can't change to working directory : %s\n", params->daemon, root);
+ return ret;
}
ret = chroot(".");
if (ret < 0) {
- if (errno != ENOENT)
- error_exit("can't change root directory");
- else
- fprintf(stderr, "%s: can't change root directory\n", params->daemon);
+ print_msg("%s: can't change root directory\n", params->daemon);
+ return ret;
}
ret = chdir("/");
if (ret < 0) {
- if (errno != ENOENT)
- error_exit("can't change to working directory /");
- else
- fprintf(stderr, "%s: can't change to working directory /\n", params->daemon);
+ print_msg("%s: can't change to working directory /\n", params->daemon);
+ return ret;
}
ret = execv((const char *)path, argv);
if (ret < 0) {
- if (errno != ENOENT)
- error_exit("can't restart %s", path);
- else
- fprintf(stderr, "%s: can't restart %s\n", params->daemon, path);
+ print_msg("%s: can't restart %s\n", params->daemon, path);
+ return ret;
}
+ return 0;
}
/**
@@ -229,7 +222,7 @@ int socket_handler( /* RETURN: closed file descriptor */
{
struct ucred cred = {0};
unsigned char magic[2], *ptr;
- char *enqry;
+ int enqry;
char *optarg = NULL;
socklen_t clen;
int ret = -1, len;
@@ -249,7 +242,7 @@ int socket_handler( /* RETURN: closed file descriptor */
optarg = calloc(alen, sizeof(char));
if (!optarg)
- error_exit("can not allocate memory for message from UNIX socket");
+ print_msg("can not allocate memory for message from UNIX socket");
ptr = (unsigned char*)optarg;
len = alen;
@@ -259,36 +252,34 @@ int socket_handler( /* RETURN: closed file descriptor */
clen = sizeof(struct ucred);
ret = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &clen);
if (ret < 0) {
- fprintf(stderr, "%s: can not get credentials from UNIX socket part1\n", params->daemon);
+ print_msg("%s: can not get credentials from UNIX socket part1\n", params->daemon);
goto out;
}
if (clen != sizeof(struct ucred)) {
- fprintf(stderr, "%s: can not get credentials from UNIX socket part2\n", params->daemon);
+ print_msg("%s: can not get credentials from UNIX socket part2\n", params->daemon);
goto out;
}
if (cred.uid != 0) {
- enqry = "\x15";
+ enqry = -EPERM;
- ptr = (unsigned char *)enqry;
- len = (int)strlen(enqry)+1;
+ ptr = (unsigned char *)&enqry;
+ len = sizeof(enqry);
safeout(fd, ptr, len);
}
switch (magic[0]) {
case MAGIC_CHROOT:
- enqry = "\x6";
- ptr = (unsigned char *)enqry;
- len = (int)strlen(enqry)+1;
+ enqry = new_root(optarg, path, argv, params);
+ ptr = (unsigned char *)&enqry;
+ len = sizeof(enqry);
safeout(fd, ptr, len);
-
- new_root(optarg, path, argv, params);
break;
default:
- enqry = "\x15";
+ enqry = -EINVAL;
- ptr = (unsigned char *)enqry;
- len = (int)strlen(enqry)+1;
+ ptr = (unsigned char *)&enqry;
+ len = sizeof(enqry);
safeout(fd, ptr, len);
break;
}
@@ -332,7 +323,7 @@ ssize_t safein( /* RETURN: read bytes */
continue;
if (errno == EAGAIN || errno == EWOULDBLOCK)
break;
- error_exit("Unable to read from socket: %d", socket_fd);
+ print_msg("Unable to read from socket: %d", socket_fd);
}
ptr = (char *) ptr + p;
ret += p;
@@ -361,7 +352,7 @@ void safeout( /* RETURN: nothing */
continue;
if (errno == EPIPE || errno == EAGAIN || errno == EWOULDBLOCK)
break;
- error_exit("Unable to write to socket: %d", fd);
+ print_msg("Unable to write to socket: %d", fd);
}
ptr = (char *) ptr + p;
len -= p;