ace1440

Code Review

May 28th, 2026
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.42 KB | Source Code | 0 0
  1. #include <libudev.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <strings.h>
  7.  
  8. #include "detect.h"
  9.  
  10. /*
  11. * Match device labels on Tails or TailsData. These are the two partition
  12. * labels that are documented within the Tails documentation.
  13. * ref: https://tails.net/doc/persistent_storage/fsck/
  14. */
  15. static bool is_tails_device(struct udev_device *dev) {
  16.     const char *label = udev_device_get_property_value(dev, "ID_FS_LABEL");
  17.     if (label == NULL) return false;
  18.  
  19.     return strcasecmp(label, "tails") == 0 ||
  20.         strcasecmp(label, "tailsdata") == 0;
  21. }
  22.  
  23. static struct udev_device *get_parent_disk_from_tails_device(struct udev_device *dev) {
  24.     struct udev_device *curr = dev;
  25.     while (curr != NULL) {
  26.         const char *devtype = udev_device_get_devtype(curr);
  27.         if (devtype != NULL && strcmp(devtype, "disk") == 0) {
  28.             break;
  29.         }
  30.         curr = udev_device_get_parent(curr);
  31.     }
  32.     return curr;
  33. }
  34.  
  35. char *alloc_tails_device() {
  36.     printf("Scanning for tails device:\n");
  37.     struct udev *udev = udev_new();
  38.     if (udev == NULL) return NULL;
  39.  
  40.     struct udev_enumerate *enumerate = udev_enumerate_new(udev);
  41.     if (enumerate == NULL) {
  42.         udev_unref(udev);
  43.         return NULL;
  44.     }
  45.     udev_enumerate_add_match_subsystem(enumerate, "block");
  46.     int scan_return_code = udev_enumerate_scan_devices(enumerate);
  47.     if (scan_return_code < 0) {
  48.         fprintf(stderr, "An error occurred while scanning block devices. Error code: %d\n", scan_return_code);
  49.         udev_enumerate_unref(enumerate);
  50.         udev_unref(udev);
  51.         return NULL;
  52.     }
  53.     struct udev_list_entry *device_entry = udev_enumerate_get_list_entry(enumerate);
  54.  
  55.     while (device_entry) {
  56.         const char *path = udev_list_entry_get_name(device_entry);
  57.  
  58.         struct udev_device *dev = udev_device_new_from_syspath(udev, path);
  59.         if (dev == NULL) goto next_item;
  60.  
  61.         if (is_tails_device(dev)) {
  62.             char *return_path = NULL;
  63.  
  64.             // Parent-chain pointer derived from dev, not separately owned.
  65.             struct udev_device *tails_disk = get_parent_disk_from_tails_device(dev);
  66.             if (tails_disk == NULL) {
  67.                 fprintf(stderr, "Could not resolve the parent disk for the detected partition!\n");
  68.                 goto cleanup_and_return;
  69.             }
  70.  
  71.             const char *tails_disk_devnode = udev_device_get_devnode(tails_disk);
  72.             if (tails_disk_devnode == NULL) {
  73.                 fprintf(stderr, "Tails device identified, but devnode couldn't be resolved. Continuing...\n");
  74.                 goto next_item;
  75.             }
  76.  
  77.             printf("Tails device found: %s\n", tails_disk_devnode);
  78.             return_path = strdup(tails_disk_devnode);
  79.             if (return_path == NULL) {
  80.                 fprintf(stderr, "Out of memory error when preparing to return path to Tails device!\n");
  81.                 goto cleanup_and_return;
  82.             }
  83.  
  84.         cleanup_and_return:
  85.             udev_device_unref(dev);
  86.             udev_enumerate_unref(enumerate);
  87.             udev_unref(udev);
  88.  
  89.             return return_path;
  90.         }
  91.     next_item:
  92.         if (dev != NULL) udev_device_unref(dev);
  93.         device_entry = udev_list_entry_get_next(device_entry);
  94.     }
  95.  
  96.     printf("No device found!\n");
  97.  
  98.     udev_enumerate_unref(enumerate);
  99.     udev_unref(udev);
  100.  
  101.     return NULL;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment