From 087639eeaa9641beb7538ced3a3161d22dabab62 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Thu, 15 Feb 2018 18:03:33 +0100 Subject: [PATCH] Implemented non-daemonizing mode --- nwhod.c | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/nwhod.c b/nwhod.c index 4e32ea6..443f78a 100644 --- a/nwhod.c +++ b/nwhod.c @@ -1,13 +1,14 @@ /* * The Remote User Information Daemon * - * (c) 1997--2010 Martin Mares + * (c) 1997--2017 Martin Mares */ #include #include #include #include +#include #include #include #include @@ -30,6 +31,7 @@ struct hostrec { u32 addr; }; +static int do_not_daemonize; static int sock, port; static struct hostrec *first_host; static time_t now, last_local_scan; @@ -291,6 +293,9 @@ do_tick(void) static void daemonize(void) { + if (do_not_daemonize) + return; + pid_t pid = fork(); if (pid < 0) die("Fork failed: %m"); @@ -350,6 +355,13 @@ client(char *serv) } } +static void +usage(void) +{ + fprintf(stderr, "Usage: nwhod [-d] []\n"); + exit(1); +} + int main(int argc, char **argv) { @@ -359,14 +371,22 @@ main(int argc, char **argv) return 0; } - if (argc == 2) - client(argv[1]); - else if (argc == 1) + int opt; + while ((opt = getopt(argc, argv, "d")) >= 0) + switch (opt) + { + case 'd': + do_not_daemonize = 1; + break; + default: + usage(); + } + + if (optind == argc-1) + client(argv[optind]); + else if (optind == argc) server(); else - { - fprintf(stderr, "Usage: nwhod []\n"); - return 1; - } + usage(); return 0; } -- 2.39.2