busybox/util-linux/volume_id/get_devname.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * Support functions for mounting devices by label/uuid
   4 *
   5 * Copyright (C) 2006 by Jason Schoon <floydpink@gmail.com>
   6 * Some portions cribbed from e2fsprogs, util-linux, dosfstools
   7 *
   8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
   9 */
  10#include <sys/mount.h> /* BLKGETSIZE64 */
  11#if !defined(BLKGETSIZE64)
  12# define BLKGETSIZE64 _IOR(0x12,114,size_t)
  13#endif
  14#include "volume_id_internal.h"
  15
  16static struct uuidCache_s {
  17        struct uuidCache_s *next;
  18//      int major, minor;
  19        char *device;
  20        char *label;
  21        char *uc_uuid; /* prefix makes it easier to grep for */
  22        IF_FEATURE_BLKID_TYPE(const char *type;)
  23} *uuidCache;
  24
  25#if !ENABLE_FEATURE_BLKID_TYPE
  26#define get_label_uuid(fd, label, uuid, type) \
  27        get_label_uuid(fd, label, uuid)
  28#define uuidcache_addentry(device, label, uuid, type) \
  29        uuidcache_addentry(device, label, uuid)
  30#endif
  31
  32/* Returns !0 on error.
  33 * Otherwise, returns malloc'ed strings for label and uuid
  34 * (and they can't be NULL, although they can be "").
  35 * NB: closes fd. */
  36static int
  37get_label_uuid(int fd, char **label, char **uuid, const char **type)
  38{
  39        int rv = 1;
  40        uint64_t size;
  41        struct volume_id *vid;
  42
  43        /* fd is owned by vid now */
  44        vid = volume_id_open_node(fd);
  45
  46        if (ioctl(/*vid->*/fd, BLKGETSIZE64, &size) != 0)
  47                size = 0;
  48
  49        if (volume_id_probe_all(vid, /*0,*/ size) != 0)
  50                goto ret;
  51
  52        if (vid->label[0] != '\0' || vid->uuid[0] != '\0'
  53#if ENABLE_FEATURE_BLKID_TYPE
  54         || vid->type != NULL
  55#endif
  56        ) {
  57                *label = xstrndup(vid->label, sizeof(vid->label));
  58                *uuid  = xstrndup(vid->uuid, sizeof(vid->uuid));
  59#if ENABLE_FEATURE_BLKID_TYPE
  60                *type = vid->type;
  61                dbg("found label '%s', uuid '%s', type '%s'", *label, *uuid, *type);
  62#else
  63                dbg("found label '%s', uuid '%s'", *label, *uuid);
  64#endif
  65                rv = 0;
  66        }
  67 ret:
  68        free_volume_id(vid); /* also closes fd */
  69        return rv;
  70}
  71
  72/* NB: we take ownership of (malloc'ed) label and uuid */
  73static void
  74uuidcache_addentry(char *device, /*int major, int minor,*/ char *label, char *uuid, const char *type)
  75{
  76        struct uuidCache_s *last;
  77
  78        if (!uuidCache) {
  79                last = uuidCache = xzalloc(sizeof(*uuidCache));
  80        } else {
  81                for (last = uuidCache; last->next; last = last->next)
  82                        continue;
  83                last->next = xzalloc(sizeof(*uuidCache));
  84                last = last->next;
  85        }
  86        /*last->next = NULL; - xzalloc did it*/
  87//      last->major = major;
  88//      last->minor = minor;
  89        last->device = device;
  90        last->label = label;
  91        last->uc_uuid = uuid;
  92        IF_FEATURE_BLKID_TYPE(last->type = type;)
  93}
  94
  95/* If get_label_uuid() on device_name returns success,
  96 * add a cache entry for this device.
  97 * If device node does not exist, it will be temporarily created. */
  98static int FAST_FUNC
  99uuidcache_check_device(const char *device,
 100                struct stat *statbuf,
 101                void *userData UNUSED_PARAM,
 102                int depth UNUSED_PARAM)
 103{
 104        /* note: this check rejects links to devices, among other nodes */
 105        if (!S_ISBLK(statbuf->st_mode))
 106                return TRUE;
 107
 108        /* Users report that mucking with floppies (especially non-present
 109         * ones) is significant PITA. This is a horribly dirty hack,
 110         * but it is very useful in real world.
 111         * If this will ever need to be enabled, consider using O_NONBLOCK.
 112         */
 113        if (major(statbuf->st_rdev) == 2)
 114                return TRUE;
 115
 116        add_to_uuid_cache(device);
 117
 118        return TRUE;
 119}
 120
 121static struct uuidCache_s*
 122uuidcache_init(int scan_devices)
 123{
 124        dbg("DBG: uuidCache=%x, uuidCache");
 125        if (uuidCache)
 126                return uuidCache;
 127
 128        /* We were scanning /proc/partitions
 129         * and /proc/sys/dev/cdrom/info here.
 130         * Missed volume managers. I see that "standard" blkid uses these:
 131         * /dev/mapper/control
 132         * /proc/devices
 133         * /proc/evms/volumes
 134         * /proc/lvm/VGs
 135         * This is unacceptably complex. Let's just scan /dev.
 136         * (Maybe add scanning of /sys/block/XXX/dev for devices
 137         * somehow not having their /dev/XXX entries created?) */
 138        if (scan_devices)
 139                recursive_action("/dev", ACTION_RECURSE,
 140                        uuidcache_check_device, /* file_action */
 141                        NULL, /* dir_action */
 142                        NULL, /* userData */
 143                        0 /* depth */);
 144
 145        return uuidCache;
 146}
 147
 148#define UUID   1
 149#define VOL    2
 150
 151#ifdef UNUSED
 152static char *
 153get_spec_by_x(int n, const char *t, int *majorPtr, int *minorPtr)
 154{
 155        struct uuidCache_s *uc;
 156
 157        uc = uuidcache_init(/*scan_devices:*/ 1);
 158        while (uc) {
 159                switch (n) {
 160                case UUID:
 161                        if (strcmp(t, uc->uc_uuid) == 0) {
 162                                *majorPtr = uc->major;
 163                                *minorPtr = uc->minor;
 164                                return uc->device;
 165                        }
 166                        break;
 167                case VOL:
 168                        if (strcmp(t, uc->label) == 0) {
 169                                *majorPtr = uc->major;
 170                                *minorPtr = uc->minor;
 171                                return uc->device;
 172                        }
 173                        break;
 174                }
 175                uc = uc->next;
 176        }
 177        return NULL;
 178}
 179
 180static unsigned char
 181fromhex(char c)
 182{
 183        if (isdigit(c))
 184                return (c - '0');
 185        return ((c|0x20) - 'a' + 10);
 186}
 187
 188static char *
 189get_spec_by_uuid(const char *s, int *major, int *minor)
 190{
 191        unsigned char uuid[16];
 192        int i;
 193
 194        if (strlen(s) != 36 || s[8] != '-' || s[13] != '-'
 195         || s[18] != '-' || s[23] != '-'
 196        ) {
 197                goto bad_uuid;
 198        }
 199        for (i = 0; i < 16; i++) {
 200                if (*s == '-')
 201                        s++;
 202                if (!isxdigit(s[0]) || !isxdigit(s[1]))
 203                        goto bad_uuid;
 204                uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
 205                s += 2;
 206        }
 207        return get_spec_by_x(UUID, (char *)uuid, major, minor);
 208
 209 bad_uuid:
 210        fprintf(stderr, _("mount: bad UUID"));
 211        return 0;
 212}
 213
 214static char *
 215get_spec_by_volume_label(const char *s, int *major, int *minor)
 216{
 217        return get_spec_by_x(VOL, s, major, minor);
 218}
 219#endif // UNUSED
 220
 221/* Used by blkid */
 222void display_uuid_cache(int scan_devices)
 223{
 224        struct uuidCache_s *uc;
 225
 226        uc = uuidcache_init(scan_devices);
 227        while (uc) {
 228                printf("%s:", uc->device);
 229                if (uc->label[0])
 230                        printf(" LABEL=\"%s\"", uc->label);
 231                if (uc->uc_uuid[0])
 232                        printf(" UUID=\"%s\"", uc->uc_uuid);
 233#if ENABLE_FEATURE_BLKID_TYPE
 234        if (uc->type)
 235                printf(" TYPE=\"%s\"", uc->type);
 236#endif
 237                bb_putchar('\n');
 238                uc = uc->next;
 239        }
 240}
 241
 242int add_to_uuid_cache(const char *device)
 243{
 244        char *uuid = uuid; /* for compiler */
 245        char *label = label;
 246#if ENABLE_FEATURE_BLKID_TYPE
 247        const char *type = type;
 248#endif
 249        int fd;
 250
 251        fd = open(device, O_RDONLY);
 252        if (fd < 0)
 253                return 0;
 254
 255        /* get_label_uuid() closes fd in all cases (success & failure) */
 256        if (get_label_uuid(fd, &label, &uuid, &type) == 0) {
 257                /* uuidcache_addentry() takes ownership of all four params */
 258                uuidcache_addentry(xstrdup(device), /*ma, mi,*/ label, uuid, type);
 259                return 1;
 260        }
 261        return 0;
 262}
 263
 264
 265/* Used by mount and findfs */
 266
 267char *get_devname_from_label(const char *spec)
 268{
 269        struct uuidCache_s *uc;
 270
 271        uc = uuidcache_init(/*scan_devices:*/ 1);
 272        while (uc) {
 273                if (uc->label[0] && strcmp(spec, uc->label) == 0) {
 274                        return xstrdup(uc->device);
 275                }
 276                uc = uc->next;
 277        }
 278        return NULL;
 279}
 280
 281char *get_devname_from_uuid(const char *spec)
 282{
 283        struct uuidCache_s *uc;
 284
 285        uc = uuidcache_init(/*scan_devices:*/ 1);
 286        while (uc) {
 287                /* case of hex numbers doesn't matter */
 288                if (strcasecmp(spec, uc->uc_uuid) == 0) {
 289                        return xstrdup(uc->device);
 290                }
 291                uc = uc->next;
 292        }
 293        return NULL;
 294}
 295
 296int resolve_mount_spec(char **fsname)
 297{
 298        char *tmp = *fsname;
 299
 300        if (strncmp(*fsname, "UUID=", 5) == 0)
 301                tmp = get_devname_from_uuid(*fsname + 5);
 302        else if (strncmp(*fsname, "LABEL=", 6) == 0)
 303                tmp = get_devname_from_label(*fsname + 6);
 304
 305        if (tmp == *fsname)
 306                return 0; /* no UUID= or LABEL= prefix found */
 307
 308        if (tmp)
 309                *fsname = tmp;
 310        return 1;
 311}
 312