chiark / gitweb /
volume_id: add volume_id_get_* functions
[elogind.git] / extras / volume_id / vol_id.c
index 96ccaf3637511c92db7b86bbffe61b44e5c66300..f6967603fa1c3f90d1e7d2c4bd2cec1382bf4a31 100644 (file)
 #include <unistd.h>
 #include <string.h>
 #include <ctype.h>
+#include <errno.h>
+#include <pwd.h>
 #include <grp.h>
+#include <getopt.h>
 #include <sys/ioctl.h>
 
 #include "../../udev.h"
@@ -109,30 +112,33 @@ static void set_str(char *to, const char *from, size_t count)
 
 int main(int argc, char *argv[])
 {
-       const char help[] = "Usage: vol_id [options] <device>\n"
-                           " --export        export key/value pairs\n"
-                           "  -t             filesystem type\n"
-                           "  -l             filesystem label\n"
-                           "  -u             filesystem uuid\n"
-                           " --skip-raid     don't probe for raid\n"
-                           " --probe-all     find possibly conflicting signatures\n"
-                           " --help\n"
-                           "\n";
+       static const struct option options[] = {
+               { "label", 0, NULL, 'l' },
+               { "label-raw", 0, NULL, 'L' },
+               { "uuid", 0, NULL, 'u' },
+               { "type", 0, NULL, 't' },
+               { "export", 0, NULL, 'x' },
+               { "skip-raid", 0, NULL, 's' },
+               { "probe-all", 0, NULL, 'a' },
+               { "help", 0, NULL, 'h' },
+               {}
+       };
+
        enum print_type {
                PRINT_EXPORT,
                PRINT_TYPE,
                PRINT_LABEL,
                PRINT_UUID,
+               PRINT_LABEL_RAW,
        } print = PRINT_EXPORT;
+
        struct volume_id *vid = NULL;
        static char name[VOLUME_ID_LABEL_SIZE];
-       int i;
        uint64_t size;
        int skip_raid = 0;
        int probe_all = 0;
-       const char *node = NULL;
-       uid_t nobody_uid;
-       gid_t nobody_gid;
+       const char *node;
+       struct passwd *pw;
        int retval;
        int rc = 0;
 
@@ -141,30 +147,56 @@ int main(int argc, char *argv[])
        /* hook in our debug into libvolume_id */
        volume_id_log_fn = vid_log;
 
-       for (i = 1 ; i < argc; i++) {
-               char *arg = argv[i];
+       while (1) {
+               int option;
 
-               if (strcmp(arg, "--export") == 0) {
-                       print = PRINT_EXPORT;
-               } else if (strcmp(arg, "-t") == 0) {
-                       print = PRINT_TYPE;
-               } else if (strcmp(arg, "-l") == 0) {
+               option = getopt_long(argc, argv, "lLutxsah", options, NULL);
+               if (option == -1)
+                       break;
+
+               switch (option) {
+               case 'l':
                        print = PRINT_LABEL;
-               } else if (strcmp(arg, "-u") == 0) {
+                       break;
+               case 'L':
+                       print = PRINT_LABEL_RAW;
+                       break;
+               case 'u':
                        print = PRINT_UUID;
-               } else if (strcmp(arg, "--skip-raid") == 0) {
+                       break;
+               case 't':
+                       print = PRINT_TYPE;
+                       break;
+               case 'x':
+                       print = PRINT_EXPORT;
+                       break;
+               case 's':
                        skip_raid = 1;
-               } else if (strcmp(arg, "--probe-all") == 0) {
+                       break;
+               case 'a':
                        probe_all = 1;
-               } else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
-                       printf(help);
+                       break;
+               case 'h':
+                       printf("Usage: vol_id [options] <device>\n"
+                           " --export        export key/value pairs\n"
+                           " --type          filesystem type\n"
+                           " --label         filesystem label\n"
+                           " --label-raw     raw label\n"
+                           " --uuid          filesystem uuid\n"
+                           " --skip-raid     don't probe for raid\n"
+                           " --probe-all     find possibly conflicting signatures\n"
+                           " --help\n\n");
                        goto exit;
-               } else
-                       node = arg;
+               default:
+                       retval = 1;
+                       goto exit;
+               }
        }
+
+       node = argv[optind];
        if (!node) {
-               err("no node specified");
-               fprintf(stderr, help);
+               err("no device");
+               fprintf(stderr, "no device\n");
                rc = 1;
                goto exit;
        }
@@ -178,15 +210,17 @@ int main(int argc, char *argv[])
 
        if (ioctl(vid->fd, BLKGETSIZE64, &size) != 0)
                size = 0;
-       dbg("BLKGETSIZE64=%llu", size);
+       dbg("BLKGETSIZE64=%llu", (unsigned long long)size);
 
-       /* drop all privileges */
-       nobody_uid = lookup_user("nobody");
-       nobody_gid = lookup_group("nogroup");
-       if (nobody_uid > 0 && nobody_gid > 0) {
+       /* try to drop all privileges before reading disk content */
+       pw = getpwnam ("nobody");
+       if (pw != NULL && pw->pw_uid > 0 && pw->pw_gid > 0) {
+               dbg("dropping privileges to %u:%u",
+                   (unsigned int)pw->pw_uid, (unsigned int)pw->pw_gid);
                if (setgroups(0, NULL) != 0 ||
-                   setgid(nobody_gid) != 0 ||
-                   setuid(nobody_uid) != 0) {
+                   setgid(pw->pw_gid) != 0 ||
+                   setuid(pw->pw_uid) != 0) {
+                       fprintf(stderr, "error dropping privileges: %s\n", strerror(errno));
                        rc = 3;
                        goto exit;
                }
@@ -291,20 +325,26 @@ int main(int argc, char *argv[])
                printf("%s\n", vid->type);
                break;
        case PRINT_LABEL:
-               if (name[0] == '\0' ||
-                   (vid->usage_id != VOLUME_ID_FILESYSTEM && vid->usage_id != VOLUME_ID_DISKLABEL)) {
+               if (name[0] == '\0' || vid->usage_id == VOLUME_ID_RAID) {
                        rc = 3;
                        goto exit;
                }
                printf("%s\n", name);
                break;
        case PRINT_UUID:
-               if (vid->uuid[0] == '\0' || vid->usage_id != VOLUME_ID_FILESYSTEM) {
+               if (vid->uuid[0] == '\0' || vid->usage_id == VOLUME_ID_RAID) {
                        rc = 4;
                        goto exit;
                }
                printf("%s\n", vid->uuid);
                break;
+       case PRINT_LABEL_RAW:
+               if (vid->label[0] == '\0' || vid->usage_id == VOLUME_ID_RAID) {
+                       rc = 3;
+                       goto exit;
+               }
+               printf("%s\n", vid->label);
+               break;
        }
 
 exit: