Skip to content

Commit

Permalink
[refactor] Make aeternum usable programmatically.
Browse files Browse the repository at this point in the history
  • Loading branch information
AvianFlu committed Jan 22, 2012
1 parent 8e89d94 commit 2b547b3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 36 deletions.
72 changes: 36 additions & 36 deletions aeternum.c
@@ -1,64 +1,64 @@
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#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);
}
16 changes: 16 additions & 0 deletions 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

0 comments on commit 2b547b3

Please sign in to comment.