pit

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

panic should forcefully close all pits

Commit a40f1006efc4863ac6de4ea078da904371fb5b63 by seb.michalk@gmail.com <seb.michalk@gmail.com> on 2024-12-07 17:52:21 +0100
diff --git a/pit.c b/pit.c
index c9a7376..d625709 100644
--- a/pit.c
+++ b/pit.c
@@ -967,66 +967,53 @@ close_mapper_device(const char *name)
     return 0;
 }
 
+/* panic_close forcefully, without any save attempt, closes the mounts by pit */
 static int
 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);
     }
 
-    if (find_mounted_pits(&mounted, &count) == 0 && count > 0) {
-        printf("pit: unmounting %d containers...\n", count);
-        
-        for (int i = 0; i < count; i++) {
-            if (mounted[i]) {
-                if (unmount_filesystem(mounted[i]) < 0) {
-                    fprintf(stderr, "pit: forced unmount of %s\n", mounted[i]);
-                    if (umount2(mounted[i], MNT_FORCE) < 0) {
-                        fprintf(stderr, "pit: cannot unmount %s: %s\n", 
-                                mounted[i], strerror(errno));
-                        ret = -1;
-                    }
-                }
-                rmdir(mounted[i]);
-                free(mounted[i]);
+    /* 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);
             }
         }
-        free(mounted);
+        closedir(proc_dir);
+        /* give processes no chance to save anything */
+        sync();
     }
 
+    /* immediately force close all device mappers without proper unmounting */
     dir = opendir("/dev/mapper");
     if (!dir) {
         fprintf(stderr, "pit: cannot open /dev/mapper: %s\n", strerror(errno));
         return -1;
     }
 
-    printf("pit: closing encrypted devices...\n");
-    
     while ((dp = readdir(dir)) != NULL) {
-        /* Skip . and .. entries */
-        if (dp->d_name[0] == '.')
-            continue;
-            
-        /* Skip the control device */
-        if (strcmp(dp->d_name, "control") == 0)
-            continue;
-
-        /* Only handle our pit devices */
         if (strncmp(dp->d_name, MAPPER_PREFIX, strlen(MAPPER_PREFIX)) == 0) {
-            printf("pit: closing %s\n", dp->d_name);
-            if (close_mapper_device(dp->d_name) < 0) {
-                ret = -1;
+            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);
             }
         }
     }
-    
     closedir(dir);
+
     return ret;
 }