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

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