This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

introduce nscd --foreground, for systemd use


Systemd likes to run daemons in foreground, so it can easily tell when
they die/crash/etc.  nscd -d doesn't suit that purpose: it runs in
foreground, but it disables paranoid mode and syslogging, so I thought
of introducing an option to run without forking, but otherwise behave
like the daemon would.  Thoughts?

for  ChangeLog
2012-01-21  Alexandre Oliva  <aoliva@redhat.com>

	* nscd/nscd.c (go_background): Replaced with...
	(run_in_foreground): ... this.  Document it.
	(options): Add -F --foreground.
	(main): Implement it.
	(parse_opt): Parse it.

Index: nscd/nscd.c
===================================================================
--- nscd/nscd.c.orig	2012-01-21 01:26:33.069905151 -0200
+++ nscd/nscd.c	2012-01-21 01:29:07.527644868 -0200
@@ -72,7 +72,12 @@ thread_info_t thread_info;
 int do_shutdown;
 int disabled_passwd;
 int disabled_group;
-int go_background = 1;
+
+/* Default is to daemonize.  Set to 1 to run in foreground in
+   debugging mode, or negative to run in foreground but otherwise
+   behave like a daemon, i.e., detach from terminal and use
+   syslog.  */
+static int run_in_foreground = 0;
 
 static const char *conffile = _PATH_NSCDCONF;
 
@@ -104,6 +109,8 @@ static const struct argp_option options[
     N_("Read configuration data from NAME") },
   { "debug", 'd', NULL, 0,
     N_("Do not fork and display messages on the current tty") },
+  { "foreground", 'F', NULL, 0,
+    N_("Do not fork, but otherwise behave like a deamon") },
   { "nthreads", 't', N_("NUMBER"), 0, N_("Start NUMBER threads") },
   { "shutdown", 'K', NULL, 0, N_("Shut the server down") },
   { "statistics", 'g', NULL, 0, N_("Print current configuration statistics") },
@@ -174,16 +181,22 @@ main (int argc, char **argv)
   /* Determine page size.  */
   pagesize_m1 = getpagesize () - 1;
 
-  /* Behave like a daemon.  */
-  if (go_background)
+  if (run_in_foreground <= 0)
     {
       int i;
+      pid_t pid;
 
-      pid_t pid = fork ();
-      if (pid == -1)
-	error (EXIT_FAILURE, errno, _("cannot fork"));
-      if (pid != 0)
-	exit (0);
+      /* Behave like a daemon.  */
+      if (!run_in_foreground)
+	{
+	  pid = fork ();
+	  if (pid == -1)
+	    error (EXIT_FAILURE, errno, _("cannot fork"));
+	  if (pid != 0)
+	    exit (0);
+	}
+      else
+	fprintf (stderr, _("further output sent to syslog\n"));
 
       int nullfd = open (_PATH_DEVNULL, O_RDWR);
       if (nullfd != -1)
@@ -234,11 +247,14 @@ main (int argc, char **argv)
 	for (i = min_close_fd; i < getdtablesize (); i++)
 	  close (i);
 
-      pid = fork ();
-      if (pid == -1)
-	error (EXIT_FAILURE, errno, _("cannot fork"));
-      if (pid != 0)
-	exit (0);
+      if (!run_in_foreground)
+	{
+	  pid = fork ();
+	  if (pid == -1)
+	    error (EXIT_FAILURE, errno, _("cannot fork"));
+	  if (pid != 0)
+	    exit (0);
+	}
 
       setsid ();
 
@@ -260,7 +276,7 @@ main (int argc, char **argv)
       signal (SIGTSTP, SIG_IGN);
     }
   else
-    /* In foreground mode we are not paranoid.  */
+    /* In debug mode we are not paranoid.  */
     paranoia = 0;
 
   signal (SIGINT, termination_handler);
@@ -309,7 +325,11 @@ parse_opt (int key, char *arg, struct ar
     {
     case 'd':
       ++debug_level;
-      go_background = 0;
+      run_in_foreground = 1;
+      break;
+
+    case 'F':
+      run_in_foreground = -1;
       break;
 
     case 'f':
-- 
Alexandre Oliva, freedom fighter    http://FSFLA.org/~lxoliva/
You must be the change you wish to see in the world. -- Gandhi
Be Free! -- http://FSFLA.org/   FSF Latin America board member
Free Software Evangelist      Red Hat Brazil Compiler Engineer

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]