Skip to content

Commit

Permalink
Canonicalize infrap4d source files (#302)
Browse files Browse the repository at this point in the history
- Use clang-format to ensure that the C and C++ files in the
  infrap4d folder conform to the coding standard.

Signed-off-by: Derek G Foster <[email protected]>
  • Loading branch information
ffoulkes authored Oct 2, 2023
1 parent fe9fc27 commit c2ec6a2
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 214 deletions.
274 changes: 129 additions & 145 deletions infrap4d/daemon/daemon-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
* limitations under the License.
*/

#include "daemon.h"
#include <stdio.h>

#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include "daemon.h"
#include "fatal-signal.h"

#ifdef __linux__
Expand All @@ -34,58 +34,55 @@
#endif

/* --detach: Should we run in the background? */
bool infrap4d_detach = true; /* Was --detach specified? */
static bool infrap4d_detached; /* Have we already detached? */
bool infrap4d_detach = true; /* Was --detach specified? */
static bool infrap4d_detached; /* Have we already detached? */

/* --pidfile: Name of pidfile (null if none). */
char *infrap4d_pidfile;
char* infrap4d_pidfile;

/* File descriptor used by daemonize_start() and daemonize_complete(). */
int infrap4d_daemonize_fd = -1;

static pid_t fork_and_clean_up(void);
static void daemonize_post_detach(void);

static int
read_fully(int fd, void *p_, size_t size, size_t *bytes_read)
{
char *p = p_;

*bytes_read = 0;
while (size > 0) {
ssize_t retval = read(fd, p, size);
if (retval > 0) {
*bytes_read += retval;
size -= retval;
p += retval;
} else if (retval == 0) {
return EOF;
} else if (errno != EINTR) {
return errno;
}
static int read_fully(int fd, void* p_, size_t size, size_t* bytes_read) {
char* p = p_;

*bytes_read = 0;
while (size > 0) {
ssize_t retval = read(fd, p, size);
if (retval > 0) {
*bytes_read += retval;
size -= retval;
p += retval;
} else if (retval == 0) {
return EOF;
} else if (errno != EINTR) {
return errno;
}
return 0;
}
return 0;
}

static int
write_fully(int fd, const void *p_, size_t size, size_t *bytes_written)
{
const char *p = p_;

*bytes_written = 0;
while (size > 0) {
ssize_t retval = write(fd, p, size);
if (retval > 0) {
*bytes_written += retval;
size -= retval;
p += retval;
} else if (retval == 0) {
return EPROTO;
} else if (errno != EINTR) {
return errno;
}
static int write_fully(int fd, const void* p_, size_t size,
size_t* bytes_written) {
const char* p = p_;

*bytes_written = 0;
while (size > 0) {
ssize_t retval = write(fd, p, size);
if (retval > 0) {
*bytes_written += retval;
size -= retval;
p += retval;
} else if (retval == 0) {
return EPROTO;
} else if (errno != EINTR) {
return errno;
}
return 0;
}
return 0;
}

/* Calls fork() and on success returns its return value. On failure, logs an
Expand All @@ -94,18 +91,15 @@ write_fully(int fd, const void *p_, size_t size, size_t *bytes_written)
* Post-fork, but before returning, this function calls a few other functions
* that are generally useful if the child isn't planning to exec a new
* process. */
static pid_t
fork_and_clean_up(void)
{
pid_t pid = fork();
if (pid < 0) {
printf("fork failed\n");
}
else if (pid > 0) {
/* Running in parent process. */
daemon_fatal_signal_fork();
}
return pid;
static pid_t fork_and_clean_up(void) {
pid_t pid = fork();
if (pid < 0) {
printf("fork failed\n");
} else if (pid > 0) {
/* Running in parent process. */
daemon_fatal_signal_fork();
}
return pid;
}

/* Forks, then:
Expand All @@ -122,112 +116,104 @@ fork_and_clean_up(void)
* able to signal its readiness by calling fork_notify_startup(), then this
* function returns -1. However, even in case of failure it still sets child
* process id in '*child_pid'. */
static int
fork_and_wait_for_startup(int *fdp, pid_t *child_pid)
{
int fds[2];
pid_t pid;
int ret = 0;

if (pipe(fds)) {
printf("failed to create pipe \n");
}
pid = fork_and_clean_up();
if (pid > 0) {
/* Running in parent process. */
size_t bytes_read;
char c;

close(fds[1]);
if (read_fully(fds[0], &c, 1, &bytes_read) != 0) {
int retval;
int status;

do {
retval = waitpid(pid, &status, 0);
} while (retval == -1 && errno == EINTR);

if (retval == pid) {
if (WIFEXITED(status) && WEXITSTATUS(status)) {
/* Child exited with an error. Convey the same error
* to our parent process as a courtesy. */
exit(WEXITSTATUS(status));
} else {
ret = -1;
}
} else {
abort();
}
static int fork_and_wait_for_startup(int* fdp, pid_t* child_pid) {
int fds[2];
pid_t pid;
int ret = 0;

if (pipe(fds)) {
printf("failed to create pipe \n");
}
pid = fork_and_clean_up();
if (pid > 0) {
/* Running in parent process. */
size_t bytes_read;
char c;

close(fds[1]);
if (read_fully(fds[0], &c, 1, &bytes_read) != 0) {
int retval;
int status;

do {
retval = waitpid(pid, &status, 0);
} while (retval == -1 && errno == EINTR);

if (retval == pid) {
if (WIFEXITED(status) && WEXITSTATUS(status)) {
/* Child exited with an error. Convey the same error
* to our parent process as a courtesy. */
exit(WEXITSTATUS(status));
} else {
ret = -1;
}
*fdp = fds[0];
} else if (!pid) {
/* Running in child process. */
close(fds[0]);
*fdp = fds[1];
} else {
abort();
}
}
*child_pid = pid;
return ret;
*fdp = fds[0];
} else if (!pid) {
/* Running in child process. */
close(fds[0]);
*fdp = fds[1];
}
*child_pid = pid;
return ret;
}

static void
fork_notify_startup(int fd)
{
if (fd != -1) {
size_t bytes_written;
int error;
static void fork_notify_startup(int fd) {
if (fd != -1) {
size_t bytes_written;
int error;

error = write_fully(fd, "", 1, &bytes_written);
if (error) {
printf("pipe write failed \n");
}
error = write_fully(fd, "", 1, &bytes_written);
if (error) {
printf("pipe write failed \n");
}
}
}

/* If daemonization is configured, then starts daemonization, by forking and
* returning in the child process. The parent process hangs around until the
* child lets it know either that it completed startup successfully (by calling
* daemonize_complete()) or that it failed to start up (by exiting with a
* nonzero exit code). */
void
daemonize_start(bool access_datapath)
{
infrap4d_daemonize_fd = -1;
void daemonize_start(bool access_datapath) {
infrap4d_daemonize_fd = -1;

if (infrap4d_detach) {
pid_t pid;
if (infrap4d_detach) {
pid_t pid;

if (fork_and_wait_for_startup(&infrap4d_daemonize_fd, &pid)) {
printf("could not detach from foreground session \n");
}
if (pid > 0) {
/* Running in parent process. */
exit(0);
} else {
/* Running in daemon or monitor process. */
setsid();
}
if (fork_and_wait_for_startup(&infrap4d_daemonize_fd, &pid)) {
printf("could not detach from foreground session \n");
}
if (pid > 0) {
/* Running in parent process. */
exit(0);
} else {
/* Running in daemon or monitor process. */
setsid();
}
}
}

/* If daemonization is configured, then this function notifies the parent
* process that the child process has completed startup successfully. It also
* call daemonize_post_detach().
*
* Calling this function more than once has no additional effect. */
void
daemonize_complete(void)
{
if (infrap4d_pidfile) {
free(infrap4d_pidfile);
infrap4d_pidfile = NULL;
}

if (!infrap4d_detached) {
infrap4d_detached = true;

fork_notify_startup(infrap4d_daemonize_fd);
daemonize_post_detach();
}
void daemonize_complete(void) {
if (infrap4d_pidfile) {
free(infrap4d_pidfile);
infrap4d_pidfile = NULL;
}

if (!infrap4d_detached) {
infrap4d_detached = true;

fork_notify_startup(infrap4d_daemonize_fd);
daemonize_post_detach();
}
}

/* If daemonization is configured, then this function does traditional Unix
Expand All @@ -237,10 +223,8 @@ daemonize_complete(void)
* It only makes sense to call this function as part of an implementation of a
* special daemon subprocess. A normal daemon should just call
* daemonize_complete(). */
static void
daemonize_post_detach(void)
{
if (infrap4d_detach) {
daemon_close_standard_fds();
}
static void daemonize_post_detach(void) {
if (infrap4d_detach) {
daemon_close_standard_fds();
}
}
Loading

0 comments on commit c2ec6a2

Please sign in to comment.