Skip to content

Commit

Permalink
Merge pull request #74 from OttoHollmann/master
Browse files Browse the repository at this point in the history
Synchronize haveged instances during switch root
  • Loading branch information
jirka-h authored Nov 28, 2022
2 parents 20e69a2 + 55dd6b7 commit 1c5eb6b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
3 changes: 1 addition & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ AC_CONFIG_HEADER([config.h])
AM_INIT_AUTOMAKE([subdir-objects no-dependencies])
AC_CONFIG_SRCDIR([src/haveged.c])
AC_CHECK_TYPES([uint32_t, uint8_t])
HA_LDFLAGS=""
HA_LDFLAGS="-pthread"

##libtool_start##
LT_INIT
Expand Down Expand Up @@ -73,7 +73,6 @@ AC_ARG_ENABLE(threads,
, enable_threads="no")
if test "x$enable_threads" = "xyes"; then
AC_DEFINE(NUMBER_CORES, 4, [Define maxium number of collection threads])
HA_LDFLAGS="-pthread"
else
AC_DEFINE(NUMBER_CORES, 1, [Define to single collection thread])
fi
Expand Down
20 changes: 17 additions & 3 deletions src/havegecmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
#include <semaphore.h>

#ifndef HAVE_STRUCT_UCRED
struct ucred
Expand All @@ -54,6 +55,7 @@ struct ucred
int first_byte;
int socket_fd;
static char errmsg[1024];
extern sem_t *sem;

static int new_root( /* RETURN: status */
const char *root, /* IN: path of the new root file system */
Expand Down Expand Up @@ -95,6 +97,7 @@ static int new_root( /* RETURN: status */
strerror(errno));
goto err;
}
sem_close(sem);
ret = execv((const char *)path, argv);
if (ret < 0) {
snprintf(&errmsg[0], sizeof(errmsg)-1,
Expand Down Expand Up @@ -265,8 +268,14 @@ int socket_handler( /* RETURN: closed file descriptor */
}

if (magic[1] == '\002') { /* ASCII start of text: read argument provided */
uint32_t alen;

uint32_t alen = 0;

/*
* wait for the haveged -c instance to finish writting
* before continuing to read from the socket
*/
sem_wait(sem);
sem_post(sem);
ret = receive_uinteger(fd, &alen);
if (ret < 0) {
print_msg("%s: can not read from UNIX socket\n", params->daemon);
Expand All @@ -285,6 +294,11 @@ int socket_handler( /* RETURN: closed file descriptor */
print_msg("%s: can not read from UNIX socket\n", params->daemon);
goto out;
}
/*
* We no more need the semaphore unlink it
* Not sure if it is the best place to unlink here
*/
sem_unlink(SEM_NAME);
}

clen = sizeof(struct ucred);
Expand Down Expand Up @@ -444,7 +458,7 @@ int receive_uinteger( /* RETURN: status */
int fd, /* IN: file descriptor */
uint32_t *value) /* OUT: 32 bit unsigned integer */
{
uint8_t buffer[4];
uint8_t buffer[4] = {0};

if (safein(fd, buffer, 4 * sizeof(uint8_t)) < 0)
return -1;
Expand Down
2 changes: 2 additions & 0 deletions src/havegecmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ extern "C" {
#define SOCK_NONBLOCK 0
#endif

#define SEM_NAME "haveged_sem"

/**
* Open and listen on a UNIX socket to get command from there
*/
Expand Down
28 changes: 28 additions & 0 deletions src/haveged.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <semaphore.h>

#ifndef NO_DAEMON
#include <syslog.h>
Expand Down Expand Up @@ -131,6 +132,8 @@ static void usage(int db, int nopts, struct option *long_options, const char **c

static sigset_t mask, omask;

sem_t *sem = NULL;

#define ATOU(a) (unsigned int)atoi(a)
/**
* Entry point
Expand Down Expand Up @@ -360,6 +363,15 @@ int main(int argc, char **argv)
fd_set read_fd;
sigset_t block;

/* init semaphore */
sem = sem_open(SEM_NAME, 0);
if (sem == NULL) {
print_msg("sem_open() failed \n");
print_msg("Error : %s \n", strerror(errno));
ret = -1;
goto err;
}

socket_fd = cmd_connect(params);
if (socket_fd < 0) {
ret = -1;
Expand All @@ -377,9 +389,19 @@ int main(int argc, char **argv)
root = optarg;
size = (uint32_t)strlen(root)+1;
cmd[1] = '\002';
/*
* Synchronise haveged -c instance and daemon instance
* prevent daemon instance from readin messages
* from the socket until the -c instance finish writting
*/
sem_wait(sem);
safeout(socket_fd, &cmd[0], 2);
send_uinteger(socket_fd, size);
safeout(socket_fd, root, size);
/*
* unblock the daemon instance as we finished writting
*/
sem_post(sem);
break;
case MAGIC_CLOSE:
ptr = &cmd[0];
Expand Down Expand Up @@ -440,6 +462,7 @@ int main(int argc, char **argv)
}
err:
close(socket_fd);
sem_close(sem);
return ret;
}
else if (!(params->setup & RUN_AS_APP)){
Expand All @@ -455,6 +478,11 @@ int main(int argc, char **argv)
fprintf(stderr, "%s: disabling command mode for this instance\n", params->daemon);
}
}
/* Initilize named semaphore to synchronize command isntances */
sem = sem_open(SEM_NAME, O_CREAT, 0644, 1);
if (sem == NULL) {
error_exit("Couldn't create nammed semaphore " SEM_NAME" error: %s", strerror(errno));
}
}
#endif
if (params->tests_config == 0)
Expand Down

0 comments on commit 1c5eb6b

Please sign in to comment.