Blob Blame History Raw
commit 5951869263b556280da53498270cf4826f779c5b
Author: Ingo Franzki <ifranzki@linux.ibm.com>
Date:   Tue Jul 13 09:05:22 2021 +0200

    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>

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
@@ -2474,54 +2474,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);
 }
 
 /**