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