From 2b547b38f0a41275407ab9d5a426cb1bff138d2b Mon Sep 17 00:00:00 2001 From: Charlie McConnell Date: Sat, 21 Jan 2012 16:22:37 -0800 Subject: [PATCH] [refactor] Make aeternum usable programmatically. --- aeternum.c | 72 +++++++++++++++++++++++++++--------------------------- aeternum.h | 16 ++++++++++++ 2 files changed, 52 insertions(+), 36 deletions(-) create mode 100644 aeternum.h diff --git a/aeternum.c b/aeternum.c index 3206134..6ca1cb6 100644 --- a/aeternum.c +++ b/aeternum.c @@ -1,64 +1,64 @@ #include #include #include +#include +#include #include #include -#include #include #include #include +#include "aeternum.h" +struct Options *aeternum_options(int argc, char *argv[]) { + Options *opts = Options_parse(argc, argv); + return opts; +} -void run_target(const char *name, char *args[]); +void aeternum_start(Options *opts) { + assert(opts != NULL); + assert(opts->outfile != NULL); + assert(opts->errfile != NULL); + assert(opts->child_args); + aeternum_fork(); + aeternum_redirect(opts->outfile, STDOUT_FILENO); + aeternum_redirect(opts->errfile, STDERR_FILENO); + aeternum_exec(opts->target, opts->child_args); +} -int main(int argc, char *argv[]) { - assert(argc > 1); - char **child_args = &argv[1]; - int i; +void aeternum_fork() { - // Fork the process. int pid = fork(); - - // fork() makes a clone of the current process - the same code will thus get - // executed in both the parent and the child. We must check for this. - // fork() returns 0 in the child process, the child's pid in the parent - // process, and -1 if an error occurs while forking. - + + signal(SIGCHLD, SIG_IGN); + signal(SIGHUP, SIG_IGN); + if (pid < 0) { - // An error has occurred - printf("An error has occurred while forking - errno %d. Exiting.\n", errno); + printf("An error occurred: %s", strerror(errno)); + // Should we find a graceful way to bail here? exit(pid); } else if (pid > 0) { - // This is the parent process printf("Child has been spawned and daemonized. PID: %d\n", pid); - // We want to daemonize, so the parent needs to exit at this point. exit(0); } - // Only child process can get here, parent process quits. - - // Detach from the terminal; start a new process group setsid(); - - // Set the umask umask(027); +} - // Redirect stdio to a file (only suitable if you want logging) - int out = open("out.log", O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR); - int err = open("err.log", O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR); - dup2(out, STDOUT_FILENO); - dup2(err, STDERR_FILENO); - - // Ignore a few signals that could otherwise ruin our day. - signal(SIGCHLD, SIG_IGN); - signal(SIGHUP, SIG_IGN); - - run_target(argv[1], child_args); +void aeternum_redirect(const char *dest, int fileno) { + int out = open(dest, + O_WRONLY | O_APPEND | O_CREAT, + S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR); + if (out == -1) printf("An error occurred: %s", strerror(errno)); + assert(out != -1); + int dup_res = dup2(out, fileno); + if (dup_res == -1) printf("An error occurred: %s", strerror(errno)); + assert(dup_res != -1); } -void run_target(const char *filename, char *args[]) { - int result; - result = execvp(filename, args); +void aeternum_exec(const char *filename, char *args[]) { + int result = execvp(filename, args); assert(result != -1); } diff --git a/aeternum.h b/aeternum.h new file mode 100644 index 0000000..259b090 --- /dev/null +++ b/aeternum.h @@ -0,0 +1,16 @@ +#ifndef _aeternum_h +#define _aeternum_h + +#include "options.h" + +struct Options *aeternum_options(int argc, char *argv[]); + +void aeternum_start(Options *opts); + +void aeternum_fork(); + +void aeternum_redirect(const char *filename, int fileno); + +void aeternum_exec(const char *filename, char *args[]); + +#endif