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

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