pit

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

rewrite panic close, rm unused functions

Commit fd75c332043d6fd5fef33229f47ff3f824f727df by IIIlllIIIllI <seb.michalk@gmail.com> on 2024-12-09 10:26:38 +0100
diff --cc pit.c
index c12b096,d625709..7458d44
--- a/pit.c
+++ b/pit.c
@@@ -941,6 -943,32 +941,35 @@@ find_mounted_pits(char ***paths, int *c
  }
  
  static int
++<<<<<<< HEAD
++=======
+ 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
++>>>>>>> a40f1006efc4863ac6de4ea078da904371fb5b63
  panic_close(void)
  {
      DIR *dir;
@@@ -953,47 -979,22 +980,61 @@@
          return run_privileged("%s panic", program_name);
      }
  
++<<<<<<< HEAD
 +    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;
 +
 +            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])) 
 +                        continue;
 +
 +                    char path[PATH_MAX], link[PATH_MAX];
 +                    snprintf(path, sizeof(path), "/proc/%s/cwd", pid_dir->d_name);
 +                    
 +                    ssize_t len = readlink(path, link, sizeof(link) - 1);
 +                    if (len > 0) {
 +                        link[len] = '\0';
 +                        if (strstr(link, mounted[i])) {
 +                            pid_t pid = atoi(pid_dir->d_name);
 +                            printf("pit: killing process %d using %s\n", pid, mounted[i]);
 +                            kill(pid, SIGKILL);
 +                        }
 +                    }
 +                }
 +                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);
++>>>>>>> a40f1006efc4863ac6de4ea078da904371fb5b63
              }
 +
 +            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", 
 +                        mounted[i], strerror(errno));
 +            }
 +            rmdir(mounted[i]);
 +            free(mounted[i]);
          }
-         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));
@@@ -1002,9 -1003,10 +1043,16 @@@
  
      while ((dp = readdir(dir)) != NULL) {
          if (strncmp(dp->d_name, MAPPER_PREFIX, strlen(MAPPER_PREFIX)) == 0) {
++<<<<<<< HEAD
 +            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 */
++>>>>>>> a40f1006efc4863ac6de4ea078da904371fb5b63
                  crypt_deactivate_by_name(cd, dp->d_name, CRYPT_DEACTIVATE_FORCE);
                  crypt_free(cd);
              }