From c2ec6a2fb53fd58c7cb048c5d05881dffac9aa64 Mon Sep 17 00:00:00 2001 From: Derek G Foster Date: Mon, 2 Oct 2023 14:21:49 -0700 Subject: [PATCH] Canonicalize infrap4d source files (#302) - 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 --- infrap4d/daemon/daemon-unix.c | 274 ++++++++++++++++----------------- infrap4d/daemon/daemon.c | 57 +++---- infrap4d/daemon/fatal-signal.c | 43 +++--- infrap4d/infrap4d_lite.cc | 12 +- infrap4d/infrap4d_main.cc | 16 +- 5 files changed, 188 insertions(+), 214 deletions(-) diff --git a/infrap4d/daemon/daemon-unix.c b/infrap4d/daemon/daemon-unix.c index 528039b6..175b4e1b 100644 --- a/infrap4d/daemon/daemon-unix.c +++ b/infrap4d/daemon/daemon-unix.c @@ -14,17 +14,17 @@ * limitations under the License. */ -#include "daemon.h" -#include - -#include -#include #include #include #include +#include #include #include +#include +#include #include + +#include "daemon.h" #include "fatal-signal.h" #ifdef __linux__ @@ -34,11 +34,11 @@ #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; @@ -46,46 +46,43 @@ 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 @@ -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: @@ -122,65 +116,61 @@ 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 @@ -188,25 +178,23 @@ fork_notify_startup(int fd) * 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 @@ -214,20 +202,18 @@ daemonize_start(bool access_datapath) * 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 @@ -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(); + } } diff --git a/infrap4d/daemon/daemon.c b/infrap4d/daemon/daemon.c index 239ef533..76a89556 100644 --- a/infrap4d/daemon/daemon.c +++ b/infrap4d/daemon/daemon.c @@ -14,11 +14,12 @@ * limitations under the License. */ #include "daemon.h" + +#include #include #include -#include -#include #include +#include /* For each of the standard file descriptors, whether to replace it by * /dev/null (if false) or keep it for the daemon to use (if true). */ @@ -31,49 +32,41 @@ static bool infrap4d_save_fds[3]; * redirect stdout or stderr to a file, in which case it is desirable to keep * these file descriptors. This function, therefore, disables replacing 'fd' * by /dev/null when the daemon detaches. */ -void -daemon_save_fd(int fd) -{ - assert(fd == STDIN_FILENO || - fd == STDOUT_FILENO || - fd == STDERR_FILENO); - infrap4d_save_fds[fd] = true; +void daemon_save_fd(int fd) { + assert(fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO); + infrap4d_save_fds[fd] = true; } /* Returns a readable and writable fd for /dev/null, if successful, otherwise * a negative errno value. The caller must not close the returned fd (because * the same fd will be handed out to subsequent callers). */ -static int -get_null_fd(void) -{ - static int null_fd; - char *device = "/dev/null"; +static int get_null_fd(void) { + static int null_fd; + char* device = "/dev/null"; - if (!null_fd) { - null_fd = open(device, O_RDWR); - if (null_fd < 0) { - int error = errno; - null_fd = -error; - } + if (!null_fd) { + null_fd = open(device, O_RDWR); + if (null_fd < 0) { + int error = errno; + null_fd = -error; } + } - return null_fd; + return null_fd; } /* Close standard file descriptors (except any that the client has requested we * leave open by calling daemon_save_fd()). If we're started from e.g. an SSH * session, then this keeps us from holding that session open artificially. */ -void -daemon_close_standard_fds(void) -{ - int null_fd = get_null_fd(); - if (null_fd >= 0) { - int fd; +void daemon_close_standard_fds(void) { + int null_fd = get_null_fd(); + if (null_fd >= 0) { + int fd; - for (fd = 0; fd < 3; fd++) { - if (!infrap4d_save_fds[fd]) { - dup2(null_fd, fd); - } - } + for (fd = 0; fd < 3; fd++) { + if (!infrap4d_save_fds[fd]) { + dup2(null_fd, fd); + } } + } } diff --git a/infrap4d/daemon/fatal-signal.c b/infrap4d/daemon/fatal-signal.c index a0e61ec7..922db18f 100644 --- a/infrap4d/daemon/fatal-signal.c +++ b/infrap4d/daemon/fatal-signal.c @@ -15,30 +15,29 @@ */ #include "fatal-signal.h" + #include #include #include -#include #include +#include #include #include #include -#include #ifndef SIG_ATOMIC_MAX #define SIG_ATOMIC_MAX TYPE_MAXIMUM(sig_atomic_t) #endif /* Signals to catch. */ -static const int fatal_signals[] = { SIGTERM, SIGINT, SIGHUP, SIGALRM, - SIGSEGV }; +static const int fatal_signals[] = {SIGTERM, SIGINT, SIGHUP, SIGALRM, SIGSEGV}; /* Hooks to call upon catching a signal */ struct infrap4d_hook { - void (*hook_cb)(void *aux); - void (*cancel_cb)(void *aux); - void *aux; - bool run_at_exit; + void (*hook_cb)(void* aux); + void (*cancel_cb)(void* aux); + void* aux; + bool run_at_exit; }; #define MAX_HOOKS 32 static struct infrap4d_hook infrap4d_hooks[MAX_HOOKS]; @@ -55,22 +54,20 @@ static volatile sig_atomic_t infrap4d_stored_sig_nr = SIG_ATOMIC_MAX; * allow it to terminate without calling the hooks registered before calling * this function. New hooks registered after calling this function will take * effect normally. */ -void -daemon_fatal_signal_fork(void) -{ - size_t i; +void daemon_fatal_signal_fork(void) { + size_t i; - for (i = 0; i < infrap4d_n_hooks; i++) { - struct infrap4d_hook *h = &infrap4d_hooks[i]; - if (h->cancel_cb) { - h->cancel_cb(h->aux); - } + for (i = 0; i < infrap4d_n_hooks; i++) { + struct infrap4d_hook* h = &infrap4d_hooks[i]; + if (h->cancel_cb) { + h->cancel_cb(h->aux); } - infrap4d_n_hooks = 0; + } + infrap4d_n_hooks = 0; - /* Raise any signals that we have already received with the default - * handler. */ - if (infrap4d_stored_sig_nr != SIG_ATOMIC_MAX) { - raise(infrap4d_stored_sig_nr); - } + /* Raise any signals that we have already received with the default + * handler. */ + if (infrap4d_stored_sig_nr != SIG_ATOMIC_MAX) { + raise(infrap4d_stored_sig_nr); + } } diff --git a/infrap4d/infrap4d_lite.cc b/infrap4d/infrap4d_lite.cc index b25893b8..aac4132e 100644 --- a/infrap4d/infrap4d_lite.cc +++ b/infrap4d/infrap4d_lite.cc @@ -7,7 +7,7 @@ #include "stratum/glue/status/status.h" #include "stratum/hal/bin/tdi/main.h" -extern "C" { +extern "C" { #include "daemon/daemon.h" } @@ -18,15 +18,15 @@ int main(int argc, char* argv[]) { stratum::hal::tdi::ParseCommandLine(argc, argv, true); if (FLAGS_detach) { - daemonize_start(false); - daemonize_complete(); + daemonize_start(false); + daemonize_complete(); } auto status = stratum::hal::tdi::Main(); if (!status.ok()) { - // TODO: Figure out logging for infrap4d - return status.error_code(); - } + // TODO: Figure out logging for infrap4d + return status.error_code(); + } return 0; } diff --git a/infrap4d/infrap4d_main.cc b/infrap4d/infrap4d_main.cc index c3bc140d..81ddae37 100644 --- a/infrap4d/infrap4d_main.cc +++ b/infrap4d/infrap4d_main.cc @@ -7,7 +7,7 @@ #include "stratum/glue/status/status.h" #include "stratum/hal/bin/tdi/main.h" -extern "C" { +extern "C" { #include "daemon/daemon.h" } @@ -19,8 +19,8 @@ int main(int argc, char* argv[]) { stratum::hal::tdi::ParseCommandLine(argc, argv, true); if (FLAGS_detach) { - daemonize_start(false); - daemonize_complete(); + daemonize_start(false); + daemonize_complete(); } absl::Notification ready_sync; @@ -36,15 +36,15 @@ int main(int argc, char* argv[]) { * sequence, just disables krnlmon logic. */ if (!FLAGS_disable_krnlmon) { - krnlmon_create_main_thread(&ready_sync); - krnlmon_create_shutdown_thread(&done_sync); + krnlmon_create_main_thread(&ready_sync); + krnlmon_create_shutdown_thread(&done_sync); } auto status = stratum::hal::tdi::Main(&ready_sync, &done_sync); if (!status.ok()) { - // TODO: Figure out logging for infrap4d - return status.error_code(); - } + // TODO: Figure out logging for infrap4d + return status.error_code(); + } return 0; }