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