[Open-FCoE] [PATCH 3/3] [fcoemon] Deamonize fcoemon by default

Robert Love robert.w.love at intel.com
Thu Jan 22 20:58:47 UTC 2009


The daemon will now start as a daemon by default. The -f
option was added to force the daemon to run in the foreground.

Depends on: [Open-FCoE] [PATCH] open-fcoe: Initial code for fcoemon and Open-FCoE service   script

Signed-off-by: Robert Love <robert.w.love at intel.com>
---

 usr/doc/fcoemon.8           |    5 +++++
 usr/etc/initd/initd.fedora  |    2 +-
 usr/tools/fcoemon/fcoemon.c |   39 ++++++++++++++++++++++++++++++++++-----
 3 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/usr/doc/fcoemon.8 b/usr/doc/fcoemon.8
index 2b0187a..bc7ae8c 100644
--- a/usr/doc/fcoemon.8
+++ b/usr/doc/fcoemon.8
@@ -7,6 +7,8 @@
 .P
 \fBfcoemon\fR [\fB\-v\fR | \fB\-\-version\fR]
 .P
+\fBfcoemon\fR [\fB\-f\fR | \fB\-\-foreground\fR]
+.P
 \fBfcoemon\fR [\fB\-d\fR | \fB\-\-debug\fR] [\fB\-e\fR | \fB\-\-exec\fR \fI<path\-to\-fcoeplumb\-script>\fR]
 .SH "DESCRIPTION"
 The \fBfcoemon\fR command is a FCoE management tool provided by the Open\-FCoE package.
@@ -31,6 +33,9 @@ Show the usage message of the \fBfcoeemon\fR command.
 \fB\-v | \-\-version\fR
 Show the version of the \fBfcoeemon\fR command.
 .TP
+\fB\-f | \-\-foreground\fR
+Run \fBfcoemon\fR in the foreground.
+.TP
 \fB\-d | \-\-debug\fR
 Enable debugging messages.
 .TP
diff --git a/usr/etc/initd/initd.fedora b/usr/etc/initd/initd.fedora
index 86cda6e..8ed9404 100755
--- a/usr/etc/initd/initd.fedora
+++ b/usr/etc/initd/initd.fedora
@@ -225,7 +225,7 @@ service_start()
 	# daemon to create the FCoE interfaces for these ports.
 	#
 	if [ "$HAS_DCB_IF" = "true" ]; then
-	    daemon --pidfile ${PID_FILE} ${FCOEMON} &
+	    daemon --pidfile ${PID_FILE} ${FCOEMON}
 	fi
 
 	return
diff --git a/usr/tools/fcoemon/fcoemon.c b/usr/tools/fcoemon/fcoemon.c
index adaa581..cceb94b 100644
--- a/usr/tools/fcoemon/fcoemon.c
+++ b/usr/tools/fcoemon/fcoemon.c
@@ -150,6 +150,7 @@ static int fcm_link_buf_check(size_t);
 static struct option fcm_options[] = {
 	{"debug", 0, NULL, 'd'},
 	{"exec", 1, NULL, 'e'},
+	{"foreground", 0, NULL, 'f'},
 	{"version", 0, NULL, 'v'},
 	{NULL, 0, NULL, 0}
 };
@@ -1878,10 +1879,11 @@ fcm_usage(void)
 {
 	printf("%s\n", fcoemon_version);
 	printf("Usage: %s\n"
-		"\t [-e|--exec <exec>]\n"
-		"\t [-d|--debug]\n"
-		"\t [-v|--version]\n"
-		"\t [-h|--help]\n\n", progname);
+	       "\t [-e|--exec <exec>]\n"
+	       "\t [-f|--foreground]\n"
+	       "\t [-d|--debug]\n"
+	       "\t [-v|--version]\n"
+	       "\t [-h|--help]\n\n", progname);
 	exit(1);
 }
 
@@ -1924,6 +1926,7 @@ fcm_pidfile_create(void)
 int main(int argc, char **argv)
 {
 	struct sigaction sig;
+	int fcm_fg = 0;
 	int rc;
 	int c;
 
@@ -1932,9 +1935,11 @@ int main(int argc, char **argv)
 	sa_log_flags = 0;
 	openlog(sa_log_prefix, LOG_CONS, LOG_DAEMON);
 
-	while ((c = getopt_long(argc, argv, "de:hv",
+	while ((c = getopt_long(argc, argv, "fde:hv",
 			fcm_options, NULL)) != -1) {
 		switch (c) {
+		case 'f':
+			fcm_fg = 1;
 		case 'd':
 			fcm_dcbd_debug = 1;
 			break;
@@ -1953,6 +1958,30 @@ int main(int argc, char **argv)
 	if (argc != optind)
 		fcm_usage();
 
+	if (!fcm_fg) {
+		pid_t pid, sid;
+
+		pid = fork();
+		if (pid < 0) {
+			SA_LOG("Starting daemon failed");
+			exit(EXIT_FAILURE);
+		} else if (pid) {
+			SA_LOG("fcoemon daemon started with pid=%d!", pid);
+			exit(EXIT_SUCCESS);
+		}
+
+		/* Create a new SID for the child process */
+		sid = setsid();
+		if (sid < 0)
+			exit(EXIT_FAILURE);
+	}
+
+	umask(0);
+
+	/* Change the current working directory */
+        if ((chdir("/")) < 0)
+                exit(EXIT_FAILURE);
+
 	/*
 	 * Set up for signals.
 	 */




More information about the devel mailing list