pit

Owner: IIIlllIIIllI URL: git@github.com:nyangkosense/pit.git

fix function panic_close

Commit 62ba0c32431f6aaf2855e5ee115f6a164bad95f6 by IIIlllIIIllI <seb.michalk@gmail.com> on 2024-12-09 10:34:00 +0100
diff --git a/pit.c b/pit.c
index fdcec7f..4826539 100644
--- a/pit.c
+++ b/pit.c
@@ -940,31 +940,6 @@ find_mounted_pits(char ***paths, int *count)
     return 0;
 }
 
-static int
-close_mapper_device(const char *name)
-{
-    struct crypt_device *cd;
-    int r;
-
-    r = crypt_init_by_name(&cd, name);
-    if (r < 0) {
-        fprintf(stderr, "pit: cannot init device %s: %s\n", 
-                name, strerror(-r));
-        return -1;
-    }
-
-    r = crypt_deactivate(cd, name);
-    if (r < 0) {
-        fprintf(stderr, "pit: cannot deactivate %s: %s\n", 
-                name, strerror(-r));
-        crypt_free(cd);
-        return -1;
-    }
-
-    crypt_free(cd);
-    return 0;
-}
-
 /* panic_close forcefully, without any save attempt, closes the mounts by pit */
 static int
 panic_close(void)
@@ -972,17 +947,21 @@ panic_close(void)
     DIR *dir;
     struct dirent *dp;
     int ret = 0;
+    char **mounted = NULL;
+    int count = 0;
 
     if (geteuid() != 0) {
         return run_privileged("%s panic", program_name);
     }
 
+    /* first find our mounted pits */
     if (find_mounted_pits(&mounted, &count) == 0 && count > 0) {
         printf("pit: force closing %d containers...\n", count);
         
         for (int i = 0; i < count; i++) {
             if (!mounted[i]) continue;
 
+            /* check /proc for processes using this mount */
             DIR *proc_dir = opendir("/proc");
             if (proc_dir) {
                 struct dirent *pid_dir;
@@ -1004,17 +983,10 @@ panic_close(void)
                     }
                 }
                 closedir(proc_dir);
-                usleep(100000);
-    /* first kill everything in /proc that touches the mounts */
-    DIR *proc_dir = opendir("/proc");
-    if (proc_dir) {
-        struct dirent *pid_dir;
-        while ((pid_dir = readdir(proc_dir)) != NULL) {
-            if (isdigit(pid_dir->d_name[0])) {
-                /* just kill every process that might touch the paths */
-                kill(atoi(pid_dir->d_name), SIGKILL);
+                usleep(100000); /* small delay for processes to die */
             }
 
+            /* Force unmount */
             printf("pit: force unmounting %s\n", mounted[i]);
             if (umount2(mounted[i], MNT_FORCE | MNT_DETACH) < 0) {
                 fprintf(stderr, "pit: cannot unmount %s: %s\n", 
@@ -1023,12 +995,10 @@ panic_close(void)
             rmdir(mounted[i]);
             free(mounted[i]);
         }
-        closedir(proc_dir);
-        /* give processes no chance to save anything */
-        sync();
+        free(mounted);
     }
 
-    /* immediately force close all device mappers without proper unmounting */
+    /* force close all pit mappers */
     dir = opendir("/dev/mapper");
     if (!dir) {
         fprintf(stderr, "pit: cannot open /dev/mapper: %s\n", strerror(errno));
@@ -1040,10 +1010,6 @@ panic_close(void)
             printf("pit: force closing device %s\n", dp->d_name);
             struct crypt_device *cd;
             if (crypt_init_by_name(&cd, dp->d_name) == 0) {
-            printf("pit: force closing %s\n", dp->d_name);
-            struct crypt_device *cd;
-            if (crypt_init_by_name(&cd, dp->d_name) == 0) {
-                /* force close even if device is busy */
                 crypt_deactivate_by_name(cd, dp->d_name, CRYPT_DEACTIVATE_FORCE);
                 crypt_free(cd);
             }