File ocki-3.15.1-pkcstok_migrate-Fix-detection-if-pkcsslotd-is-still-.patch of Package openCryptoki.23571

From 2d2daa620611e37f4ae7201882d2828d0d6d7768 Mon Sep 17 00:00:00 2001
From: Ingo Franzki <ifranzki@linux.ibm.com>
Date: Tue, 13 Jul 2021 09:05:22 +0200
Subject: [PATCH] pkcstok_migrate: Fix detection if pkcsslotd is still running

Change the code to use the pid file that pkcsslotd creates, and check
if the process with the pid contained in the pid file still exists and
runs pkcsslotd.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
---
 usr/sbin/pkcstok_migrate/pkcstok_migrate.c | 71 +++++++++++-----------
 1 file changed, 35 insertions(+), 36 deletions(-)

diff --git a/usr/sbin/pkcstok_migrate/pkcstok_migrate.c b/usr/sbin/pkcstok_migrate/pkcstok_migrate.c
index 05081aff..a29dc8f7 100644
--- a/usr/sbin/pkcstok_migrate/pkcstok_migrate.c
+++ b/usr/sbin/pkcstok_migrate/pkcstok_migrate.c
@@ -2466,54 +2466,53 @@ static CK_RV backup_repository(const char *data_store)
  */
 static CK_BBOOL pkcsslotd_running(void)
 {
-    DIR *dir;
     FILE *fp;
-    struct dirent* ent;
     char* endptr;
-    char buf[PATH_MAX];
+    long lpid;
     char fname[PATH_MAX];
+    char buf[PATH_MAX];
+    char* first;
 
     TRACE_INFO("Checking if pkcsslotd is running ...\n");
-    if (!(dir = opendir("/proc"))) {
-        TRACE_WARN("Cannot open /proc, i.e. cannot check if pkcsslotd is running.\n");
-        return CK_TRUE;
+
+    fp = fopen(PID_FILE_PATH, "r");
+    if (fp == NULL) {
+        TRACE_INFO("Pid file '%s' not existent, pkcsslotd is not running\n",
+                   PID_FILE_PATH);
+        return CK_FALSE;
     }
 
-    while ((ent = readdir(dir)) != NULL) {
-        /* if endptr is not a null character, the directory is not
-         * entirely numeric, so ignore it */
-        long lpid = strtol(ent->d_name, &endptr, 10);
-        if (*endptr != '\0') {
-            continue;
-        }
+    if (fgets(buf, sizeof(buf), fp) == NULL) {
+        TRACE_WARN("Cannot read pid file '%s': %s\n", PID_FILE_PATH,
+                   strerror(errno));
+        fclose(fp);
+        return CK_FALSE;
+    }
+    fclose(fp);
 
-        /* try to open the cmdline file */
-        snprintf(fname, sizeof(fname), "/proc/%ld/cmdline", lpid);
-        fp = fopen(fname, "r");
-        if (!fp) {
-            warnx("fopen(%s) failed, errno=%s", fname, strerror(errno));
-            return CK_TRUE;
-        }
+    lpid = strtol(buf, &endptr, 10);
+    if (*endptr != '\0' && *endptr != '\n') {
+        TRACE_WARN("Failed to parse pid file '%s': %s\n", PID_FILE_PATH,
+                           buf);
+        return CK_FALSE;
+    }
 
-        /* check the first token in the file: the program pathname */
-        if (fgets(buf, sizeof(buf), fp) != NULL) {
-            char* first = strtok(buf, " ");
-            if (!first) {
-                TRACE_WARN("Cannot read program name from %s, i.e. cannot check if pkcsslotd is running.\n",
-                           fname);
-                return CK_TRUE;
-            }
-            if (strstr(first, "pkcsslotd") != NULL) {
-                fclose(fp);
-                closedir(dir);
-                return CK_TRUE;
-            }
-        }
+    snprintf(fname, sizeof(fname), "/proc/%ld/cmdline", lpid);
+    fp = fopen(fname, "r");
+    if (fp == NULL) {
+        TRACE_INFO("Stale pid file, pkcsslotd is not running\n");
+        return CK_FALSE;
+    }
+
+    if (fgets(buf, sizeof(buf), fp) == NULL) {
+        TRACE_INFO("Failed to read '%s'\n", fname);
         fclose(fp);
+        return CK_FALSE;
     }
+    fclose(fp);
 
-    closedir(dir);
-    return CK_FALSE;
+    first = strtok(buf, " ");
+    return (first != NULL && strstr(first, "pkcsslotd") != NULL);
 }
 
 /**
-- 
2.26.2

openSUSE Build Service is sponsored by