Initial commit. It's far from finished.
This commit is contained in:
94
main.c
Normal file
94
main.c
Normal file
@@ -0,0 +1,94 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include "confparser.h"
|
||||
#include "log_trace.h"
|
||||
#include "rmps.h"
|
||||
|
||||
static void usage(char *argv)
|
||||
{
|
||||
fprintf( stderr,
|
||||
"Usage:\n%s start|stop|restart [--daemonize=yes|no]\n",
|
||||
argv );
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int fork_flag = 1, task;
|
||||
|
||||
if (argc < 2 || argc > 3) {
|
||||
usage(argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (!strcmp(argv[1], "start"))
|
||||
task = 1;
|
||||
else if (!strcmp(argv[1], "stop"))
|
||||
task = 2;
|
||||
else if (!strcmp(argv[1], "restart"))
|
||||
task = 3;
|
||||
else {
|
||||
usage(argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (argc == 3) {
|
||||
if (!strcmp("--daemonize=yes", argv[2]));
|
||||
else if (!strcmp("--daemonize=no", argv[2]))
|
||||
fork_flag = 0;
|
||||
else {
|
||||
usage(argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
if (confparse() != 0) {
|
||||
fprintf(stderr, "Failed to parse the conf!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
//confexport();
|
||||
|
||||
if (task == 2 || task == 3) {
|
||||
char buf[10];
|
||||
int pid;
|
||||
FILE *fd;
|
||||
if (task == 2)
|
||||
log_trace(VERBOSE, "We got a stop signal!");
|
||||
else if (task == 3)
|
||||
log_trace(VERBOSE, "We got a restart signal!");
|
||||
|
||||
fd = fopen(conf.rmps.pidfile, "r");
|
||||
|
||||
switch (errno) {
|
||||
case EEXIST:
|
||||
if (!fgets(buf, 10, fd)) {
|
||||
log_trace(ERROR, "Failed to read %s!", conf.rmps.pidfile);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
pid = strtol(buf, NULL, 10);
|
||||
kill(pid, SIGTERM);
|
||||
//waitpid(TODO);
|
||||
break;
|
||||
case EACCES:
|
||||
log_trace(ERROR, "Permission denied to read PID. Exiting!");
|
||||
exit(EXIT_FAILURE);
|
||||
case ENOENT:
|
||||
if (task == 2)
|
||||
exit(EXIT_FAILURE);
|
||||
break;
|
||||
default:
|
||||
log_trace( ERROR,
|
||||
"Unhandled errno while opening PID: %d. Exiting!",
|
||||
errno
|
||||
);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
if (task == 1 || task == 3) {
|
||||
launch_rmps(&conf, fork_flag);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user