1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <sys/mount.h>
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
25 char *device;
26 char *label;
27 char *uc_uuid;
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
39
40
41
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
50 vid = volume_id_open_node(fd);
51
52 if (ioctl(fd, BLKGETSIZE64, &size) != 0)
53 size = 0;
54
55 if (volume_id_probe_all(vid, 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);
75 return rv;
76}
77
78
79static void
80uuidcache_addentry(char *device, 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
93
94
95 last->device = device;
96 last->label = label;
97 last->uc_uuid = uuid;
98 IF_FEATURE_BLKID_TYPE(last->type = type;)
99}
100
101
102
103
104static int FAST_FUNC
105uuidcache_check_device(struct recursive_state *state UNUSED_PARAM,
106 const char *device,
107 struct stat *statbuf)
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 );
153 }
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( 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
230
231
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;
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
266 if (get_label_uuid(fd, &label, &uuid, &type) == 0) {
267
268 uuidcache_addentry(xstrdup(device), label, uuid, type);
269 return 1;
270 }
271 return 0;
272}
273
274
275
276
277char *get_devname_from_label(const char *spec)
278{
279 struct uuidCache_s *uc;
280
281 uc = uuidcache_init( 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( 1);
296 while (uc) {
297
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;
317
318 if (tmp)
319 *fsname = tmp;
320 return 1;
321}
322