uuidd: implement --no-fork option

With this option, uuidd does not daemonize itself but stays
in the foreground.

Signed-off-by: Petr Uzel <petr.uzel@suse.cz>
This commit is contained in:
Petr Uzel 2012-05-03 21:01:52 +02:00 committed by Karel Zak
parent f6f3dc7811
commit e1cf3ebed5
2 changed files with 15 additions and 6 deletions

View File

@ -39,6 +39,9 @@ the pid file is written to /var/run/uuidd/uuidd.pid.
.BR \-P , " \-\-no-pid " .BR \-P , " \-\-no-pid "
Do not create pid file. Do not create pid file.
.TP .TP
.BR \-F , " \-\-no-fork "
Do not daemonize using double-fork.
.TP
.B \-q .B \-q
Suppress some failure messages. Suppress some failure messages.
.TP .TP

View File

@ -66,6 +66,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)
" -t, --time test time-based generation\n" " -t, --time test time-based generation\n"
" -n, --uuids <num> request number of uuids\n" " -n, --uuids <num> request number of uuids\n"
" -P, --no-pid do not create pid file\n" " -P, --no-pid do not create pid file\n"
" -F, --no-fork do not daemonize using double-fork\n"
" -d, --debug run in debugging mode\n" " -d, --debug run in debugging mode\n"
" -q, --quiet turn on quiet mode\n" " -q, --quiet turn on quiet mode\n"
" -V, --version output version information and exit\n" " -V, --version output version information and exit\n"
@ -249,7 +250,7 @@ static int create_pidfile(const char *pidfile_path, int quiet)
} }
static void server_loop(const char *socket_path, const char *pidfile_path, static void server_loop(const char *socket_path, const char *pidfile_path,
int debug, int timeout, int quiet) int debug, int timeout, int quiet, int no_fork)
{ {
struct sockaddr_un my_addr, from_addr; struct sockaddr_un my_addr, from_addr;
socklen_t fromlen; socklen_t fromlen;
@ -287,7 +288,7 @@ static void server_loop(const char *socket_path, const char *pidfile_path,
* Make sure the socket isn't using fd numbers 0-2 to avoid it * Make sure the socket isn't using fd numbers 0-2 to avoid it
* getting closed by create_daemon() * getting closed by create_daemon()
*/ */
while (!debug && s <= 2) { while ((!debug || no_fork) && s <= 2) {
s = dup(s); s = dup(s);
if (s < 0) if (s < 0)
err(EXIT_FAILURE, "dup"); err(EXIT_FAILURE, "dup");
@ -318,7 +319,7 @@ static void server_loop(const char *socket_path, const char *pidfile_path,
} }
cleanup_socket = socket_path; cleanup_socket = socket_path;
if (!debug) if (!debug && !no_fork)
create_daemon(); create_daemon();
signal(SIGHUP, terminate_intr); signal(SIGHUP, terminate_intr);
signal(SIGINT, terminate_intr); signal(SIGINT, terminate_intr);
@ -462,7 +463,7 @@ int main(int argc, char **argv)
int i, c, ret; int i, c, ret;
int debug = 0, do_type = 0, do_kill = 0, num = 0; int debug = 0, do_type = 0, do_kill = 0, num = 0;
int timeout = 0, quiet = 0, drop_privs = 0; int timeout = 0, quiet = 0, drop_privs = 0;
int no_pid = 0; int no_pid = 0, no_fork = 0;
static const struct option longopts[] = { static const struct option longopts[] = {
{"pid", required_argument, NULL, 'p'}, {"pid", required_argument, NULL, 'p'},
@ -473,6 +474,7 @@ int main(int argc, char **argv)
{"time", no_argument, NULL, 't'}, {"time", no_argument, NULL, 't'},
{"uuids", required_argument, NULL, 'n'}, {"uuids", required_argument, NULL, 'n'},
{"no-pid", no_argument, NULL, 'P'}, {"no-pid", no_argument, NULL, 'P'},
{"no-fork", no_argument, NULL, 'F'},
{"debug", no_argument, NULL, 'd'}, {"debug", no_argument, NULL, 'd'},
{"quiet", no_argument, NULL, 'q'}, {"quiet", no_argument, NULL, 'q'},
{"version", no_argument, NULL, 'V'}, {"version", no_argument, NULL, 'V'},
@ -486,7 +488,7 @@ int main(int argc, char **argv)
atexit(close_stdout); atexit(close_stdout);
while ((c = while ((c =
getopt_long(argc, argv, "p:s:T:krtn:PdqVh", longopts, getopt_long(argc, argv, "p:s:T:krtn:PFdqVh", longopts,
NULL)) != -1) { NULL)) != -1) {
switch (c) { switch (c) {
case 'd': case 'd':
@ -512,6 +514,10 @@ int main(int argc, char **argv)
no_pid = 1; no_pid = 1;
drop_privs = 1; drop_privs = 1;
break; break;
case 'F':
no_fork = 1;
drop_privs = 1;
break;
case 'q': case 'q':
quiet++; quiet++;
break; break;
@ -637,6 +643,6 @@ int main(int argc, char **argv)
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
server_loop(socket_path, pidfile_path, debug, timeout, quiet); server_loop(socket_path, pidfile_path, debug, timeout, quiet, no_fork);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }