Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[api] --min-uptime support
  • Loading branch information
mmalecki committed Aug 29, 2013
1 parent 594a863 commit 52a033f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions options.h
Expand Up @@ -9,6 +9,7 @@ struct options_s {
char *pidname;
int json;
char **child_args;
int min_uptime;
};

typedef struct options_s options_t;
Expand Down
32 changes: 28 additions & 4 deletions src/aeternum.c
Expand Up @@ -6,6 +6,7 @@
#include <sys/types.h>
#include <pwd.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <errno.h>

Expand All @@ -16,15 +17,14 @@

extern char **environ;

static char *outfile;
static char *errfile;

options_t opts;

uv_loop_t *loop;
uv_process_t child_req;
uv_process_options_t options;

static struct timeval start_tv;

void spawn_cb(uv_process_t*, int, int);
void configure_stdio();

Expand Down Expand Up @@ -78,6 +78,10 @@ void spawn_child(int detach) {
options.exit_cb = detach ? NULL : spawn_cb;
if (detach) options.flags = UV_PROCESS_DETACHED;

if (opts.min_uptime != 0) {
gettimeofday(&start_tv, NULL);
}

if (uv_spawn(loop, &child_req, options)) {
fprintf(stderr, "Error %s\n", uv_err_name(uv_last_error(loop)));
fprintf(stderr, "%s\n", uv_strerror(uv_last_error(loop)));
Expand All @@ -94,6 +98,20 @@ void spawn_child(int detach) {
}

void spawn_cb(uv_process_t *req, int exit_status, int signal_status) {
struct timeval tv;
int ms_up;

if (opts.min_uptime != 0) {
gettimeofday(&tv, NULL);
ms_up = ((start_tv.tv_sec * 1000) + (start_tv.tv_usec / 1000)) -
((tv.tv_sec * 1000) + (tv.tv_usec / 1000));

if (ms_up < opts.min_uptime) {
fprintf(stderr, "Child exited prematurely with %d, exiting.\n", exit_status);
exit(7);
}
}

if (signal_status) {
fprintf(stderr, "Child killed by signal %d, restarting.\n", signal_status);
}
Expand Down Expand Up @@ -166,6 +184,7 @@ int main(int argc, char *argv[]) {
int r;
saneopt_t* opt;
char** args;
char* min_uptime;

signal(SIGHUP, SIG_IGN);
signal(SIGINT, handle_signal);
Expand All @@ -189,12 +208,12 @@ int main(int argc, char *argv[]) {
opts.outfile = NULL;
opts.errfile = NULL;
opts.pidname = NULL;
opts.min_uptime = 0;
opts.target = argv[0];
opts.json = 0;
opts.child_args = argv;

spawn_child(1);
printf("%d\n", child_req.pid);
return 0;
}

Expand All @@ -206,6 +225,11 @@ int main(int argc, char *argv[]) {
opts.target = args[1];
opts.child_args = &args[1];

min_uptime = saneopt_get(opt, "min-uptime");
if (min_uptime != NULL) {
sscanf(min_uptime, "%d", &opts.min_uptime);
}

configure_stdio();

spawn_child(0);
Expand Down

0 comments on commit 52a033f

Please sign in to comment.