File ocki-3.8.2-pkcsslotd-adapt-pid-file-creation-on-demonize-for-sy.patch of Package openCryptoki.9565

From 25ee28d0484a232214d7a6b88d5084b607764c9e Mon Sep 17 00:00:00 2001
From: Harald Freudenberger <freude@linux.ibm.com>
Date: Thu, 22 Nov 2018 08:40:05 +0100
Subject: [PATCH] pkcsslotd: adapt pid file creation on demonize for systemd

The current implementation creates the pid file of the
pkcsslotd demon as one of the last steps of the slot
demons startup. It's done after the parent has spawned
away the child which will become the demon when this child
is nearly done with initialization. At this time the
parent has already terminated.

However, systemd expects to find the pid file when the
parent terminates. So here is now a patch which moves
the creation of the pid file into the parent code. Now
the pid file is created after the child has been forked
in the parent process short before it calls exit().

Signed-off-by: Harald Freudenberger <freude@linux.ibm.com>
---
 usr/sbin/pkcsslotd/slotmgr.c | 61 ++++++++++++++++++++++++------------
 1 file changed, 41 insertions(+), 20 deletions(-)

diff --git a/usr/sbin/pkcsslotd/slotmgr.c b/usr/sbin/pkcsslotd/slotmgr.c
index 5275b17d..394a2384 100644
--- a/usr/sbin/pkcsslotd/slotmgr.c
+++ b/usr/sbin/pkcsslotd/slotmgr.c
@@ -286,7 +286,26 @@ int chk_create_tokdir(Slot_Info_t_64* psinfo) {
 		}
 	}
 	umask(proc_umask);
-	return 0;
+    return 0;
+}
+
+static int create_pid_file(pid_t pid)
+{
+    FILE *pidfile;
+
+    pidfile = fopen(PID_FILE_PATH, "w");
+    if (!pidfile) {
+        fprintf(stderr, "Could not create pid file '%s' [errno=%d].\n",
+                PID_FILE_PATH, errno);
+        return -1;
+    }
+
+    fprintf(pidfile, "%d\n", (int) pid);
+    fflush(pidfile);
+    fclose(pidfile);
+    InfoLog("PID File created");
+
+    return 0;
 }
 
 /*****************************************
@@ -401,18 +420,23 @@ int main ( int argc, char *argv[], char *envp[]) {
 			DetachFromSharedMemory();
 			DestroySharedMemory();
 			return 7;
+		} else if (pid != 0) {
+			/*
+			 * This is the parent
+			 * Create the pid file for the client as systemd wants to
+			 * see the pid file a soon as the parent terminates.
+			 */
+			create_pid_file(pid);
+			/* now terminate the parent */
+			exit(0);
 		} else {
-			if ( pid != 0) {
-				exit(0); // Terminate the parent
-			} else {
-
-				setsid(); // Session leader
+			/* This is the child */
+			setsid();       // Session leader
 #ifndef DEV
-				fclose(stderr);
-				fclose(stdout);
-				fclose(stdin);
+			fclose(stderr);
+			fclose(stdout);
+			fclose(stdin);
 #endif
-			}
 		}
 	} else {
 #ifdef DEV
@@ -465,16 +489,13 @@ int main ( int argc, char *argv[], char *envp[]) {
 	}
 #endif
 
-	// We've fully become a daemon.  Now create the PID file
-	{
-		FILE *pidfile;
-
-		pidfile = fopen(PID_FILE_PATH,"w");
-		if (pidfile) {
-			fprintf(pidfile,"%d",getpid());
-			fclose(pidfile);
-		}
-	}
+	/*
+	 * We've fully become a daemon.
+	 * In not-daemon mode the pid file hasn't been created jet,
+	 * so let's do this now.
+	 */
+	if (!Daemon)
+		create_pid_file(getpid());
 
 	while (1) {
 #if !(THREADED) && !(NOGARBAGE)
-- 
2.17.1

openSUSE Build Service is sponsored by