Add 'do not fork' option
authorAlex Bligh <alex@alex.org.uk>
Tue, 17 May 2011 12:04:59 +0000 (13:04 +0100)
committerAlex Bligh <alex@alex.org.uk>
Tue, 17 May 2011 12:04:59 +0000 (13:04 +0100)
man/nbd-server.1.in.sgml
nbd-server.c

index 2695086..047fcb6 100644 (file)
@@ -67,6 +67,7 @@ manpage.1: manpage.sgml
       <arg><option>-o <replaceable>section name</replaceable></option></arg>
       <arg><option>-C <replaceable>config file</replaceable></option></arg>
       <arg><option>-M <replaceable>max connections</replaceable></option></arg>
       <arg><option>-o <replaceable>section name</replaceable></option></arg>
       <arg><option>-C <replaceable>config file</replaceable></option></arg>
       <arg><option>-M <replaceable>max connections</replaceable></option></arg>
+      <arg><option>-d</option></arg>
     </cmdsynopsis>
   </refsynopsisdiv>
   <refsect1>
     </cmdsynopsis>
   </refsynopsisdiv>
   <refsect1>
@@ -216,6 +217,12 @@ manpage.1: manpage.sgml
        </listitem>
       </varlistentry>
       <varlistentry>
        </listitem>
       </varlistentry>
       <varlistentry>
+        <term><option>-d</option></term>
+       <listitem>
+         <para>Do not fork. Useful for debugging.</para>
+       </listitem>
+      </varlistentry>
+      <varlistentry>
        <term><option>host list filename</option></term>
        <listitem>
          <para>This argument should contain a list of IP-addresses
        <term><option>host list filename</option></term>
        <listitem>
          <para>This argument should contain a list of IP-addresses
index 5c7cbc0..cd584f0 100644 (file)
@@ -116,6 +116,9 @@ gchar* rungroup=NULL;
 /** whether to export using the old negotiation protocol (port-based) */
 gboolean do_oldstyle=FALSE;
 
 /** whether to export using the old negotiation protocol (port-based) */
 gboolean do_oldstyle=FALSE;
 
+/* Whether we should avoid forking */
+int dontfork = 0;
+
 /** Logging macros, now nothing goes to syslog unless you say ISSERVER */
 #ifdef ISSERVER
 #define msg2(a,b) syslog(a,b)
 /** Logging macros, now nothing goes to syslog unless you say ISSERVER */
 #ifdef ISSERVER
 #define msg2(a,b) syslog(a,b)
@@ -413,6 +416,7 @@ SERVER* cmdline(int argc, char *argv[]) {
                {"read-only", no_argument, NULL, 'r'},
                {"multi-file", no_argument, NULL, 'm'},
                {"copy-on-write", no_argument, NULL, 'c'},
                {"read-only", no_argument, NULL, 'r'},
                {"multi-file", no_argument, NULL, 'm'},
                {"copy-on-write", no_argument, NULL, 'c'},
+               {"dont-fork", no_argument, NULL, 'd'},
                {"authorize-file", required_argument, NULL, 'l'},
                {"config-file", required_argument, NULL, 'C'},
                {"pid-file", required_argument, NULL, 'p'},
                {"authorize-file", required_argument, NULL, 'l'},
                {"config-file", required_argument, NULL, 'C'},
                {"pid-file", required_argument, NULL, 'p'},
@@ -434,7 +438,7 @@ SERVER* cmdline(int argc, char *argv[]) {
        serve=g_new0(SERVER, 1);
        serve->authname = g_strdup(default_authname);
        serve->virtstyle=VIRT_IPLIT;
        serve=g_new0(SERVER, 1);
        serve->authname = g_strdup(default_authname);
        serve->virtstyle=VIRT_IPLIT;
-       while((c=getopt_long(argc, argv, "-C:cl:mo:rp:M:", long_options, &i))>=0) {
+       while((c=getopt_long(argc, argv, "-C:cdl:mo:rp:M:", long_options, &i))>=0) {
                switch (c) {
                case 1:
                        /* non-option argument */
                switch (c) {
                case 1:
                        /* non-option argument */
@@ -503,6 +507,9 @@ SERVER* cmdline(int argc, char *argv[]) {
                case 'c': 
                        serve->flags |=F_COPYONWRITE;
                        break;
                case 'c': 
                        serve->flags |=F_COPYONWRITE;
                        break;
+               case 'd': 
+                       dontfork = 1;
+                       break;
                case 'C':
                        g_free(config_file_pos);
                        config_file_pos=g_strdup(optarg);
                case 'C':
                        g_free(config_file_pos);
                        config_file_pos=g_strdup(optarg);
@@ -1787,31 +1794,33 @@ int serveloop(GArray* servers) {
                                }
                                msg2(LOG_INFO,"Authorized client") ;
                                pid=g_malloc(sizeof(pid_t));
                                }
                                msg2(LOG_INFO,"Authorized client") ;
                                pid=g_malloc(sizeof(pid_t));
-#ifndef NOFORK
-                               if ((*pid=fork())<0) {
-                                       msg3(LOG_INFO,"Could not fork (%s)",strerror(errno)) ;
-                                       close(net);
-                                       continue;
-                               }
-                               if (*pid>0) { /* parent */
-                                       close(net);
-                                       g_hash_table_insert(children, pid, pid);
-                                       continue;
-                               }
-                               /* child */
-                               g_hash_table_destroy(children);
-                               for(i=0;i<servers->len;i++) {
-                                       serve=&g_array_index(servers, SERVER, i);
-                                       close(serve->socket);
+
+                               if (!dontfork) {
+                                       if ((*pid=fork())<0) {
+                                               msg3(LOG_INFO,"Could not fork (%s)",strerror(errno)) ;
+                                               close(net);
+                                               continue;
+                                       }
+                                       if (*pid>0) { /* parent */
+                                               close(net);
+                                               g_hash_table_insert(children, pid, pid);
+                                               continue;
+                                       }
+                                       /* child */
+                                       g_hash_table_destroy(children);
+                                       for(i=0;i<servers->len;i++) {
+                                               serve=&g_array_index(servers, SERVER, i);
+                                               close(serve->socket);
+                                       }
+                                       /* FALSE does not free the
+                                          actual data. This is required,
+                                          because the client has a
+                                          direct reference into that
+                                          data, and otherwise we get a
+                                          segfault... */
+                                       g_array_free(servers, FALSE);
                                }
                                }
-                               /* FALSE does not free the
-                               actual data. This is required,
-                               because the client has a
-                               direct reference into that
-                               data, and otherwise we get a
-                               segfault... */
-                               g_array_free(servers, FALSE);
-#endif // NOFORK
+
                                msg2(LOG_INFO,"Starting to serve");
                                serveconnection(client);
                                exit(EXIT_SUCCESS);
                                msg2(LOG_INFO,"Starting to serve");
                                serveconnection(client);
                                exit(EXIT_SUCCESS);
@@ -1978,7 +1987,7 @@ void setup_servers(GArray* servers) {
  *     is only used to create a PID file of the form
  *     /var/run/nbd-server.&lt;port&gt;.pid; it's not modified in any way.
  **/
  *     is only used to create a PID file of the form
  *     /var/run/nbd-server.&lt;port&gt;.pid; it's not modified in any way.
  **/
-#if !defined(NODAEMON) && !defined(NOFORK)
+#if !defined(NODAEMON)
 void daemonize(SERVER* serve) {
        FILE*pidf;
 
 void daemonize(SERVER* serve) {
        FILE*pidf;
 
@@ -2007,7 +2016,7 @@ void daemonize(SERVER* serve) {
 }
 #else
 #define daemonize(serve)
 }
 #else
 #define daemonize(serve)
-#endif /* !defined(NODAEMON) && !defined(NOFORK) */
+#endif /* !defined(NODAEMON) */
 
 /*
  * Everything beyond this point (in the file) is run in non-daemon mode.
 
 /*
  * Everything beyond this point (in the file) is run in non-daemon mode.
@@ -2146,7 +2155,8 @@ int main(int argc, char *argv[]) {
                g_message("No configured exports; quitting.");
                exit(EXIT_FAILURE);
        }
                g_message("No configured exports; quitting.");
                exit(EXIT_FAILURE);
        }
-       daemonize(serve);
+       if (!dontfork)
+               daemonize(serve);
        setup_servers(servers);
        dousers();
        serveloop(servers);
        setup_servers(servers);
        dousers();
        serveloop(servers);