From 2c99851181a652358aa3ca58ef38c57e46ae02e4 Mon Sep 17 00:00:00 2001 From: Bernhard Reutner-Fischer Date: Wed, 12 Apr 2006 18:09:26 +0000 Subject: [PATCH] - patch from Denis Vlasenko to add and use bb_xdaemon() --- debianutils/start_stop_daemon.c | 3 +-- include/libbb.h | 1 + libbb/Makefile.in | 2 +- libbb/bb_xdaemon.c | 17 +++++++++++++++++ miscutils/crond.c | 5 ++--- miscutils/watchdog.c | 3 +-- networking/dnsd.c | 5 ++--- networking/httpd.c | 4 ++-- networking/inetd.c | 3 ++- networking/telnetd.c | 6 ++---- sysklogd/klogd.c | 3 +-- sysklogd/syslogd.c | 3 +-- 12 files changed, 33 insertions(+), 22 deletions(-) create mode 100644 libbb/bb_xdaemon.c diff --git a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c index 5b689740e..b44a23b4b 100644 --- a/debianutils/start_stop_daemon.c +++ b/debianutils/start_stop_daemon.c @@ -275,8 +275,7 @@ start_stop_daemon_main(int argc, char **argv) } *--argv = startas; if (opt & SSD_OPT_BACKGROUND) { - if (daemon(0, 0) == -1) - bb_perror_msg_and_die ("unable to fork"); + bb_xdaemon(0, 0); setsid(); } if (opt & SSD_OPT_MAKEPID) { diff --git a/include/libbb.h b/include/libbb.h index c031f2841..650002b7b 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -146,6 +146,7 @@ extern void bb_fflush_stdout_and_exit(int retval) ATTRIBUTE_NORETURN; extern void xstat(const char *filename, struct stat *buf); extern int bb_xsocket(int domain, int type, int protocol); +extern void bb_xdaemon(int nochdir, int noclose); #define BB_GETOPT_ERROR 0x80000000UL extern const char *bb_opt_complementally; diff --git a/libbb/Makefile.in b/libbb/Makefile.in index c69978370..3fb945e31 100644 --- a/libbb/Makefile.in +++ b/libbb/Makefile.in @@ -30,7 +30,7 @@ LIBBB-y:= \ trim.c u_signal_names.c vdprintf.c verror_msg.c \ vherror_msg.c vperror_msg.c wfopen.c xconnect.c xgetcwd.c xstat.c \ xgethostbyname.c xgethostbyname2.c xreadlink.c xregcomp.c xgetlarg.c \ - bb_xsocket.c \ + bb_xsocket.c bb_xdaemon.c \ get_terminal_width_height.c fclose_nonstdin.c fflush_stdout_and_exit.c \ getopt_ulflags.c default_error_retval.c wfopen_input.c speed_table.c \ perror_nomsg_and_die.c perror_nomsg.c skip_whitespace.c bb_askpass.c \ diff --git a/libbb/bb_xdaemon.c b/libbb/bb_xdaemon.c new file mode 100644 index 000000000..a40f4f993 --- /dev/null +++ b/libbb/bb_xdaemon.c @@ -0,0 +1,17 @@ +/* vi: set sw=4 ts=4: */ +/* + * bb_xdaemon.c - a daemon() which dies on failure with error message + * + * Copyright (C) 2006 Denis Vlasenko + * + * Licensed under LGPL, see file docs/lesser.txt in this tarball for details. + */ +#include +#include "libbb.h" + +void bb_xdaemon(int nochdir, int noclose) +{ + if (daemon(nochdir, noclose)) + bb_perror_msg_and_die("daemon"); +} + diff --git a/miscutils/crond.c b/miscutils/crond.c index 7e50b61c7..06b8769fa 100644 --- a/miscutils/crond.c +++ b/miscutils/crond.c @@ -1,3 +1,4 @@ +/* vi: set sw=4 ts=4: */ /* * crond -d[#] -c -f -b * @@ -213,9 +214,7 @@ int crond_main(int ac, char **av) /* reexec for vfork() do continue parent */ vfork_daemon_rexec(1, 0, ac, av, "-f"); #else /* uClinux */ - if (daemon(1, 0) < 0) { - bb_perror_msg_and_die("daemon"); - } + bb_xdaemon(1, 0); #endif /* uClinux */ } diff --git a/miscutils/watchdog.c b/miscutils/watchdog.c index 55dd69857..58a606553 100644 --- a/miscutils/watchdog.c +++ b/miscutils/watchdog.c @@ -40,8 +40,7 @@ int watchdog_main(int argc, char **argv) if (optind < argc - 1 || argc == 1) bb_show_usage(); - if (daemon(0, 1) < 0) - bb_perror_msg_and_die("Failed forking watchdog daemon"); + bb_xdaemon(0, 1); signal(SIGHUP, watchdog_shutdown); signal(SIGINT, watchdog_shutdown); diff --git a/networking/dnsd.c b/networking/dnsd.c index 9c5193820..d78ea04c0 100644 --- a/networking/dnsd.c +++ b/networking/dnsd.c @@ -5,6 +5,7 @@ * Copyright (C) 2005 Roberto A. Foglietta (me@roberto.foglietta.name) * Copyright (C) 2005 Odd Arild Olsen (oao at fibula dot no) * Copyright (C) 2003 Paul Sheer + * * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. * * Odd Arild Olsen started out with the sheerdns [1] of Paul Sheer and rewrote @@ -408,9 +409,7 @@ int dnsd_main(int argc, char **argv) /* reexec for vfork() do continue parent */ vfork_daemon_rexec(1, 0, argc, argv, "-d"); #else /* uClinux */ - if (daemon(1, 0) < 0) { - bb_perror_msg_and_die("daemon"); - } + bb_xdaemon(1, 0); #endif /* uClinuvx */ dnsentryinit(is_verbose()); diff --git a/networking/httpd.c b/networking/httpd.c index fde8ae4bd..354c199e7 100644 --- a/networking/httpd.c +++ b/networking/httpd.c @@ -1,3 +1,4 @@ +/* vi: set sw=4 ts=4: */ /* * httpd implementation for busybox * @@ -2114,8 +2115,7 @@ int httpd_main(int argc, char *argv[]) #if !ENABLE_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY # if !DEBUG - if (daemon(1, 0) < 0) /* don`t change curent directory */ - bb_perror_msg_and_die("daemon"); + bb_xdaemon(1, 0); /* don`t change curent directory */ # endif return miniHttpd(server); #else diff --git a/networking/inetd.c b/networking/inetd.c index cc700832c..39a2b24cb 100644 --- a/networking/inetd.c +++ b/networking/inetd.c @@ -1314,7 +1314,7 @@ inetd_main (int argc, char *argv[]) /* reexec for vfork() do continue parent */ vfork_daemon_rexec (0, 0, argc, argv, "-f"); #else - daemon (0, 0); + daemon (0, 0); /* bb_xdaemon? */ #endif /* uClinux */ } else { setsid (); @@ -1802,3 +1802,4 @@ daytime_dg (int s, servtab_t *sep ATTRIBUTE_UNUSED) (void) sendto (s, buffer, strlen (buffer), 0, &sa, sizeof (sa)); } #endif /* CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME */ +/* vi: set sw=4 ts=4: */ diff --git a/networking/telnetd.c b/networking/telnetd.c index 3e4b42cfa..d53f56b14 100644 --- a/networking/telnetd.c +++ b/networking/telnetd.c @@ -1,4 +1,4 @@ -/* vi:set ts=4:*/ +/* vi: set sw=4 ts=4: */ /* * Simple telnet server * Bjorn Wesen, Axis Communications AB (bjornw@axis.com) @@ -469,9 +469,7 @@ telnetd_main(int argc, char **argv) bb_perror_msg_and_die("listen"); } - if (daemon(0, 0) < 0) - bb_perror_msg_and_die("daemon"); - + bb_xdaemon(0, 0); maxfd = master_fd; #endif /* CONFIG_FEATURE_TELNETD_INETD */ diff --git a/sysklogd/klogd.c b/sysklogd/klogd.c index 51b01430e..6dc5457af 100644 --- a/sysklogd/klogd.c +++ b/sysklogd/klogd.c @@ -121,8 +121,7 @@ int klogd_main(int argc, char **argv) #if defined(__uClinux__) vfork_daemon_rexec(0, 1, argc, argv, "-n"); #else /* __uClinux__ */ - if (daemon(0, 1) < 0) - bb_perror_msg_and_die("daemon"); + bb_xdaemon(0, 1); #endif /* __uClinux__ */ } doKlogd(console_log_level); diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c index b43c72d66..0fcbb48bc 100644 --- a/sysklogd/syslogd.c +++ b/sysklogd/syslogd.c @@ -673,8 +673,7 @@ int syslogd_main(int argc, char **argv) #if defined(__uClinux__) vfork_daemon_rexec(0, 1, argc, argv, "-n"); #else /* __uClinux__ */ - if(daemon(0, 1) < 0) - bb_perror_msg_and_die("daemon"); + bb_xdaemon(0, 1); #endif /* __uClinux__ */ } doSyslogd(); -- 2.25.1