Blame SOURCES/opencryptoki-3.16.0-pkcstok_migrate-detection_if_pkcsslotd_is_still_running.patch

8520e9
commit 5951869263b556280da53498270cf4826f779c5b
8520e9
Author: Ingo Franzki <ifranzki@linux.ibm.com>
8520e9
Date:   Tue Jul 13 09:05:22 2021 +0200
8520e9
8520e9
    pkcstok_migrate: Fix detection if pkcsslotd is still running
8520e9
    
8520e9
    Change the code to use the pid file that pkcsslotd creates, and check
8520e9
    if the process with the pid contained in the pid file still exists and
8520e9
    runs pkcsslotd.
8520e9
    
8520e9
    Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
8520e9
8520e9
diff --git a/usr/sbin/pkcstok_migrate/pkcstok_migrate.c b/usr/sbin/pkcstok_migrate/pkcstok_migrate.c
8520e9
index 05081aff..a29dc8f7 100644
8520e9
--- a/usr/sbin/pkcstok_migrate/pkcstok_migrate.c
8520e9
+++ b/usr/sbin/pkcstok_migrate/pkcstok_migrate.c
8520e9
@@ -2474,54 +2474,53 @@ static CK_RV backup_repository(const char *data_store)
8520e9
  */
8520e9
 static CK_BBOOL pkcsslotd_running(void)
8520e9
 {
8520e9
-    DIR *dir;
8520e9
     FILE *fp;
8520e9
-    struct dirent* ent;
8520e9
     char* endptr;
8520e9
-    char buf[PATH_MAX];
8520e9
+    long lpid;
8520e9
     char fname[PATH_MAX];
8520e9
+    char buf[PATH_MAX];
8520e9
+    char* first;
8520e9
 
8520e9
     TRACE_INFO("Checking if pkcsslotd is running ...\n");
8520e9
-    if (!(dir = opendir("/proc"))) {
8520e9
-        TRACE_WARN("Cannot open /proc, i.e. cannot check if pkcsslotd is running.\n");
8520e9
-        return CK_TRUE;
8520e9
+
8520e9
+    fp = fopen(PID_FILE_PATH, "r");
8520e9
+    if (fp == NULL) {
8520e9
+        TRACE_INFO("Pid file '%s' not existent, pkcsslotd is not running\n",
8520e9
+                   PID_FILE_PATH);
8520e9
+        return CK_FALSE;
8520e9
     }
8520e9
 
8520e9
-    while ((ent = readdir(dir)) != NULL) {
8520e9
-        /* if endptr is not a null character, the directory is not
8520e9
-         * entirely numeric, so ignore it */
8520e9
-        long lpid = strtol(ent->d_name, &endptr, 10);
8520e9
-        if (*endptr != '\0') {
8520e9
-            continue;
8520e9
-        }
8520e9
+    if (fgets(buf, sizeof(buf), fp) == NULL) {
8520e9
+        TRACE_WARN("Cannot read pid file '%s': %s\n", PID_FILE_PATH,
8520e9
+                   strerror(errno));
8520e9
+        fclose(fp);
8520e9
+        return CK_FALSE;
8520e9
+    }
8520e9
+    fclose(fp);
8520e9
 
8520e9
-        /* try to open the cmdline file */
8520e9
-        snprintf(fname, sizeof(fname), "/proc/%ld/cmdline", lpid);
8520e9
-        fp = fopen(fname, "r");
8520e9
-        if (!fp) {
8520e9
-            warnx("fopen(%s) failed, errno=%s", fname, strerror(errno));
8520e9
-            return CK_TRUE;
8520e9
-        }
8520e9
+    lpid = strtol(buf, &endptr, 10);
8520e9
+    if (*endptr != '\0' && *endptr != '\n') {
8520e9
+        TRACE_WARN("Failed to parse pid file '%s': %s\n", PID_FILE_PATH,
8520e9
+                           buf);
8520e9
+        return CK_FALSE;
8520e9
+    }
8520e9
 
8520e9
-        /* check the first token in the file: the program pathname */
8520e9
-        if (fgets(buf, sizeof(buf), fp) != NULL) {
8520e9
-            char* first = strtok(buf, " ");
8520e9
-            if (!first) {
8520e9
-                TRACE_WARN("Cannot read program name from %s, i.e. cannot check if pkcsslotd is running.\n",
8520e9
-                           fname);
8520e9
-                return CK_TRUE;
8520e9
-            }
8520e9
-            if (strstr(first, "pkcsslotd") != NULL) {
8520e9
-                fclose(fp);
8520e9
-                closedir(dir);
8520e9
-                return CK_TRUE;
8520e9
-            }
8520e9
-        }
8520e9
+    snprintf(fname, sizeof(fname), "/proc/%ld/cmdline", lpid);
8520e9
+    fp = fopen(fname, "r");
8520e9
+    if (fp == NULL) {
8520e9
+        TRACE_INFO("Stale pid file, pkcsslotd is not running\n");
8520e9
+        return CK_FALSE;
8520e9
+    }
8520e9
+
8520e9
+    if (fgets(buf, sizeof(buf), fp) == NULL) {
8520e9
+        TRACE_INFO("Failed to read '%s'\n", fname);
8520e9
         fclose(fp);
8520e9
+        return CK_FALSE;
8520e9
     }
8520e9
+    fclose(fp);
8520e9
 
8520e9
-    closedir(dir);
8520e9
-    return CK_FALSE;
8520e9
+    first = strtok(buf, " ");
8520e9
+    return (first != NULL && strstr(first, "pkcsslotd") != NULL);
8520e9
 }
8520e9
 
8520e9
 /**