1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <sys/mount.h>
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
24 char *device;
25 char *label;
26 char *uc_uuid;
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
38
39
40
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
49 vid = volume_id_open_node(fd);
50
51 if (ioctl(fd, BLKGETSIZE64, &size) != 0)
52 size = 0;
53
54 if (volume_id_probe_all(vid, 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);
74 return rv;
75}
76
77
78static void
79uuidcache_addentry(char *device, 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
92
93
94 last->device = device;
95 last->label = label;
96 last->uc_uuid = uuid;
97 IF_FEATURE_BLKID_TYPE(last->type = type;)
98}
99
100
101
102
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
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
118
119
120
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
138
139
140
141
142
143
144
145
146
147 if (scan_devices)
148 recursive_action("/dev", ACTION_RECURSE,
149 uuidcache_check_device,
150 NULL,
151 NULL,
152 0 );
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( 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
229
230
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;
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
265 if (get_label_uuid(fd, &label, &uuid, &type) == 0) {
266
267 uuidcache_addentry(xstrdup(device), label, uuid, type);
268 return 1;
269 }
270 return 0;
271}
272
273
274
275
276char *get_devname_from_label(const char *spec)
277{
278 struct uuidCache_s *uc;
279
280 uc = uuidcache_init( 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( 1);
295 while (uc) {
296
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;
316
317 if (tmp)
318 *fsname = tmp;
319 return 1;
320}
321