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