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