File open-iscsi-option-no-pid-file of Package open-iscsi

From 5ebb5dbca96a0854bd0c3306a42fac7c4ebefe61 Mon Sep 17 00:00:00 2001
From: Hannes Reinecke <hare@suse.de>
Date: Wed, 12 Nov 2008 15:24:21 +0100
Subject: [PATCH] iscsid: Implement --no-pid-file

For root on iSCSI scenarios the /var directory might not exist.
And we don't need the pid file anyway as the daemon is synchronized
via the IPC connection.

Signed-off-by: Hannes Reinecke <hare@suse.de>
---
 doc/iscsid.8 |    3 +++
 usr/iscsid.c |   40 ++++++++++++++++++++++++----------------
 2 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/doc/iscsid.8 b/doc/iscsid.8
index 1dfa1e5..92b7f81 100644
--- a/doc/iscsid.8
+++ b/doc/iscsid.8
@@ -35,6 +35,9 @@ run under user ID \fIuid\fR (default is the current user ID)
 .BI [-g|--gid=]\fIgid\fP
 run under user group ID \fIgid\fR (default is the current user group ID).
 .TP
+.BI [-n|--no-pid-file]\fP
+do not write a process ID file.
+.TP
 .BI [-p|--pid=]\fIpid\-file\fP
 write process ID to \fIpid\-file\fR rather than the default
 \fI/var/run/iscsid.pid\fR
diff --git a/usr/iscsid.c b/usr/iscsid.c
index 578a206..3da235b 100644
--- a/usr/iscsid.c
+++ b/usr/iscsid.c
@@ -59,6 +59,7 @@ static struct option const long_options[] = {
 	{"debug", required_argument, NULL, 'd'},
 	{"uid", required_argument, NULL, 'u'},
 	{"gid", required_argument, NULL, 'g'},
+	{"no-pid-file", no_argument, NULL, 'n'},
 	{"pid", required_argument, NULL, 'p'},
 	{"help", no_argument, NULL, 'h'},
 	{"version", no_argument, NULL, 'v'},
@@ -80,6 +81,7 @@ Open-iSCSI initiator daemon.\n\
   -d, --debug debuglevel  print debugging information\n\
   -u, --uid=uid           run as uid, default is current user\n\
   -g, --gid=gid           run as gid, default is current user group\n\
+  -n, --no-pid-file       do not use a pid file\n\
   -p, --pid=pidfile       use pid file (default " PID_FILE ").\n\
   -h, --help              display this help and exit\n\
   -v, --version           display version and exit\n\
@@ -332,7 +334,7 @@ int main(int argc, char *argv[])
 	sigaction(SIGPIPE, &sa_new, &sa_old );
 	sigaction(SIGTERM, &sa_new, &sa_old );
 
-	while ((ch = getopt_long(argc, argv, "c:i:fd:u:g:p:vh", long_options,
+	while ((ch = getopt_long(argc, argv, "c:i:fd:nu:g:p:vh", long_options,
 				 &longindex)) >= 0) {
 		switch (ch) {
 		case 'c':
@@ -353,6 +355,9 @@ int main(int argc, char *argv[])
 		case 'g':
 			gid = strtoul(optarg, NULL, 10);
 			break;
+		case 'n':
+			pid_file = NULL;
+			break;
 		case 'p':
 			pid_file = optarg;
 			break;
@@ -404,13 +409,15 @@ int main(int argc, char *argv[])
 
 	if (log_daemon) {
 		char buf[64];
-		int fd;
-
-		fd = open(pid_file, O_WRONLY|O_CREAT, 0644);
-		if (fd < 0) {
-			log_error("Unable to create pid file");
-			log_close(log_pid);
-			exit(1);
+		int fd = -1;
+
+		if (pid_file) {
+			fd = open(pid_file, O_WRONLY|O_CREAT, 0644);
+			if (fd < 0) {
+				log_error("Unable to create pid file");
+				log_close(log_pid);
+				exit(1);
+			}
 		}
 		pid = fork();
 		if (pid < 0) {
@@ -428,15 +435,16 @@ int main(int argc, char *argv[])
 		}
 
 		chdir("/");
-		if (lockf(fd, F_TLOCK, 0) < 0) {
-			log_error("Unable to lock pid file");
-			log_close(log_pid);
-			exit(1);
+		if (fd > 0) {
+			if (lockf(fd, F_TLOCK, 0) < 0) {
+				log_error("Unable to lock pid file");
+				log_close(log_pid);
+				exit(1);
+			}
+			ftruncate(fd, 0);
+			sprintf(buf, "%d\n", getpid());
+			write(fd, buf, strlen(buf));
 		}
-		ftruncate(fd, 0);
-		sprintf(buf, "%d\n", getpid());
-		write(fd, buf, strlen(buf));
-
 		daemon_init();
 	} else {
 		if ((control_fd = ipc->ctldev_open()) < 0) {
-- 
1.6.0.2