[Nbd] [PATCH 4/5] Add 'do not fork' option
- To: nbd-general@lists.sourceforge.net
- Subject: [Nbd] [PATCH 4/5] Add 'do not fork' option
- From: Alex Bligh <alex@...872...>
- Date: Tue, 17 May 2011 13:34:54 +0100
- Message-id: <1305635695-24058-4-git-send-email-alex@...872...>
- In-reply-to: <1305635695-24058-1-git-send-email-alex@...872...>
- References: <1305635695-24058-1-git-send-email-alex@...872...>
Signed-off-by: Alex Bligh <alex@...872...>
---
man/nbd-server.1.in.sgml | 7 +++++
nbd-server.c | 66 ++++++++++++++++++++++++++-------------------
2 files changed, 45 insertions(+), 28 deletions(-)
diff --git a/man/nbd-server.1.in.sgml b/man/nbd-server.1.in.sgml
index 2695086..047fcb6 100644
--- a/man/nbd-server.1.in.sgml
+++ b/man/nbd-server.1.in.sgml
@@ -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>-d</option></arg>
</cmdsynopsis>
</refsynopsisdiv>
<refsect1>
@@ -216,6 +217,12 @@ manpage.1: manpage.sgml
</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
diff --git a/nbd-server.c b/nbd-server.c
index 5c7cbc0..cd584f0 100644
--- a/nbd-server.c
+++ b/nbd-server.c
@@ -116,6 +116,9 @@ gchar* rungroup=NULL;
/** 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)
@@ -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'},
+ {"dont-fork", no_argument, NULL, 'd'},
{"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;
- 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 */
@@ -503,6 +507,9 @@ SERVER* cmdline(int argc, char *argv[]) {
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);
@@ -1787,31 +1794,33 @@ int serveloop(GArray* servers) {
}
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);
@@ -1978,7 +1987,7 @@ void setup_servers(GArray* servers) {
* is only used to create a PID file of the form
* /var/run/nbd-server.<port>.pid; it's not modified in any way.
**/
-#if !defined(NODAEMON) && !defined(NOFORK)
+#if !defined(NODAEMON)
void daemonize(SERVER* serve) {
FILE*pidf;
@@ -2007,7 +2016,7 @@ void daemonize(SERVER* 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.
@@ -2146,7 +2155,8 @@ int main(int argc, char *argv[]) {
g_message("No configured exports; quitting.");
exit(EXIT_FAILURE);
}
- daemonize(serve);
+ if (!dontfork)
+ daemonize(serve);
setup_servers(servers);
dousers();
serveloop(servers);
--
1.7.1
Reply to: