X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=main-postlicyd.c;h=ecf60f0c130cd1e6de3420045a5709c52174da42;hb=cf48fad99532b7590b8bfea416532a7a84910bdf;hp=fe4a8516ca180f60ad7850fc67d119dfd5920ac1;hpb=1c42f5f8ed1972f13424197e9493bbed8e401576;p=apps%2Fpfixtools.git diff --git a/main-postlicyd.c b/main-postlicyd.c index fe4a851..ecf60f0 100644 --- a/main-postlicyd.c +++ b/main-postlicyd.c @@ -38,6 +38,8 @@ #include "epoll.h" #include "threads.h" +#define DAEMON_NAME "postlicyd" + /* administrivia {{{ */ static int main_initialize(void) @@ -59,6 +61,15 @@ static void main_shutdown(void) module_init(main_initialize); module_exit(main_shutdown); +void usage(void) +{ + fputs("usage: "DAEMON_NAME" [options] config\n" + "\n" + "Options:\n" + " -p file to write our pid to\n" + , stderr); +} + /* }}} */ void *job_run(int fd, void *data) @@ -91,7 +102,6 @@ static int main_loop(void) int main(int argc, char *argv[]) { const char *pidfile = NULL; - FILE *f = NULL; int res; for (int c = 0; (c = getopt(argc, argv, "h" "p:")) >= 0; ) { @@ -100,18 +110,19 @@ int main(int argc, char *argv[]) pidfile = optarg; break; default: - //usage(); + usage(); return EXIT_FAILURE; } } - if (pidfile) { - f = fopen(pidfile, "w"); - if (!f) { - syslog(LOG_CRIT, "unable to write pidfile %s", pidfile); - } - fprintf(f, "%d\n", getpid()); - fflush(f); + if (argc - optind != 1) { + usage(); + return EXIT_FAILURE; + } + + if (pidfile_open(pidfile) < 0) { + syslog(LOG_CRIT, "unable to write pidfile %s", pidfile); + return EXIT_FAILURE; } if (daemon_detach() < 0) { @@ -119,19 +130,8 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - if (f) { - rewind(f); - ftruncate(fileno(f), 0); - fprintf(f, "%d\n", getpid()); - fflush(f); - } + pidfile_refresh(); res = main_loop(); - if (f) { - rewind(f); - ftruncate(fileno(f), 0); - fclose(f); - f = NULL; - } syslog(LOG_INFO, "Stopping..."); return res; }