1
2
3
4
5
6
7
8
9
10#include <sys/mount.h>
11#if !defined(BLKGETSIZE64)
12# define BLKGETSIZE64 _IOR(0x12,114,size_t)
13#endif
14#include "volume_id_internal.h"
15
16static struct uuidCache_s {
17 struct uuidCache_s *next;
18
19 char *device;
20 char *label;
21 char *uc_uuid;
22 IF_FEATURE_BLKID_TYPE(const char *type;)
23} *uuidCache;
24
25#if !ENABLE_FEATURE_BLKID_TYPE
26#define get_label_uuid(fd, label, uuid, type) \
27 get_label_uuid(fd, label, uuid)
28#define uuidcache_addentry(device, label, uuid, type) \
29 uuidcache_addentry(device, label, uuid)
30#endif
31
32
33
34
35
36static int
37get_label_uuid(int fd, char **label, char **uuid, const char **type)
38{
39 int rv = 1;
40 uint64_t size;
41 struct volume_id *vid;
42
43
44 vid = volume_id_open_node(fd);
45
46 if (ioctl(fd, BLKGETSIZE64, &size) != 0)
47 size = 0;
48
49 if (volume_id_probe_all(vid, size) != 0)
50 goto ret;
51
52 if (vid->label[0] != '\0' || vid->uuid[0] != '\0'
53#if ENABLE_FEATURE_BLKID_TYPE
54 || vid->type != NULL
55#endif
56 ) {
57 *label = xstrndup(vid->label, sizeof(vid->label));
58 *uuid = xstrndup(vid->uuid, sizeof(vid->uuid));
59#if ENABLE_FEATURE_BLKID_TYPE
60 *type = vid->type;
61 dbg("found label '%s', uuid '%s', type '%s'", *label, *uuid, *type);
62#else
63 dbg("found label '%s', uuid '%s'", *label, *uuid);
64#endif
65 rv = 0;
66 }
67 ret:
68 free_volume_id(vid);
69 return rv;
70}
71
72
73static void
74uuidcache_addentry(char *device, char *label, char *uuid, const char *type)
75{
76 struct uuidCache_s *last;
77
78 if (!uuidCache) {
79 last = uuidCache = xzalloc(sizeof(*uuidCache));
80 } else {
81 for (last = uuidCache; last->next; last = last->next)
82 continue;
83 last->next = xzalloc(sizeof(*uuidCache));
84 last = last->next;
85 }
86
87
88
89 last->device = device;
90 last->label = label;
91 last->uc_uuid = uuid;
92 IF_FEATURE_BLKID_TYPE(last->type = type;)
93}
94
95
96
97
98static int FAST_FUNC
99uuidcache_check_device(const char *device,
100 struct stat *statbuf,
101 void *userData UNUSED_PARAM,
102 int depth UNUSED_PARAM)
103{
104
105 if (!S_ISBLK(statbuf->st_mode))
106 return TRUE;
107
108
109
110
111
112
113 if (major(statbuf->st_rdev) == 2)
114 return TRUE;
115
116 add_to_uuid_cache(device);
117
118 return TRUE;
119}
120
121static struct uuidCache_s*
122uuidcache_init(int scan_devices)
123{
124 dbg("DBG: uuidCache=%x, uuidCache");
125 if (uuidCache)
126 return uuidCache;
127
128
129
130
131
132
133
134
135
136
137
138 if (scan_devices)
139 recursive_action("/dev", ACTION_RECURSE,
140 uuidcache_check_device,
141 NULL,
142 NULL,
143 0 );
144
145 return uuidCache;
146}
147
148#define UUID 1
149#define VOL 2
150
151#ifdef UNUSED
152static char *
153get_spec_by_x(int n, const char *t, int *majorPtr, int *minorPtr)
154{
155 struct uuidCache_s *uc;
156
157 uc = uuidcache_init( 1);
158 while (uc) {
159 switch (n) {
160 case UUID:
161 if (strcmp(t, uc->uc_uuid) == 0) {
162 *majorPtr = uc->major;
163 *minorPtr = uc->minor;
164 return uc->device;
165 }
166 break;
167 case VOL:
168 if (strcmp(t, uc->label) == 0) {
169 *majorPtr = uc->major;
170 *minorPtr = uc->minor;
171 return uc->device;
172 }
173 break;
174 }
175 uc = uc->next;
176 }
177 return NULL;
178}
179
180static unsigned char
181fromhex(char c)
182{
183 if (isdigit(c))
184 return (c - '0');
185 return ((c|0x20) - 'a' + 10);
186}
187
188static char *
189get_spec_by_uuid(const char *s, int *major, int *minor)
190{
191 unsigned char uuid[16];
192 int i;
193
194 if (strlen(s) != 36 || s[8] != '-' || s[13] != '-'
195 || s[18] != '-' || s[23] != '-'
196 ) {
197 goto bad_uuid;
198 }
199 for (i = 0; i < 16; i++) {
200 if (*s == '-')
201 s++;
202 if (!isxdigit(s[0]) || !isxdigit(s[1]))
203 goto bad_uuid;
204 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
205 s += 2;
206 }
207 return get_spec_by_x(UUID, (char *)uuid, major, minor);
208
209 bad_uuid:
210 fprintf(stderr, _("mount: bad UUID"));
211 return 0;
212}
213
214static char *
215get_spec_by_volume_label(const char *s, int *major, int *minor)
216{
217 return get_spec_by_x(VOL, s, major, minor);
218}
219#endif
220
221
222void display_uuid_cache(int scan_devices)
223{
224 struct uuidCache_s *uc;
225
226 uc = uuidcache_init(scan_devices);
227 while (uc) {
228 printf("%s:", uc->device);
229 if (uc->label[0])
230 printf(" LABEL=\"%s\"", uc->label);
231 if (uc->uc_uuid[0])
232 printf(" UUID=\"%s\"", uc->uc_uuid);
233#if ENABLE_FEATURE_BLKID_TYPE
234 if (uc->type)
235 printf(" TYPE=\"%s\"", uc->type);
236#endif
237 bb_putchar('\n');
238 uc = uc->next;
239 }
240}
241
242int add_to_uuid_cache(const char *device)
243{
244 char *uuid = uuid;
245 char *label = label;
246#if ENABLE_FEATURE_BLKID_TYPE
247 const char *type = type;
248#endif
249 int fd;
250
251 fd = open(device, O_RDONLY);
252 if (fd < 0)
253 return 0;
254
255
256 if (get_label_uuid(fd, &label, &uuid, &type) == 0) {
257
258 uuidcache_addentry(xstrdup(device), label, uuid, type);
259 return 1;
260 }
261 return 0;
262}
263
264
265
266
267char *get_devname_from_label(const char *spec)
268{
269 struct uuidCache_s *uc;
270
271 uc = uuidcache_init( 1);
272 while (uc) {
273 if (uc->label[0] && strcmp(spec, uc->label) == 0) {
274 return xstrdup(uc->device);
275 }
276 uc = uc->next;
277 }
278 return NULL;
279}
280
281char *get_devname_from_uuid(const char *spec)
282{
283 struct uuidCache_s *uc;
284
285 uc = uuidcache_init( 1);
286 while (uc) {
287
288 if (strcasecmp(spec, uc->uc_uuid) == 0) {
289 return xstrdup(uc->device);
290 }
291 uc = uc->next;
292 }
293 return NULL;
294}
295
296int resolve_mount_spec(char **fsname)
297{
298 char *tmp = *fsname;
299
300 if (strncmp(*fsname, "UUID=", 5) == 0)
301 tmp = get_devname_from_uuid(*fsname + 5);
302 else if (strncmp(*fsname, "LABEL=", 6) == 0)
303 tmp = get_devname_from_label(*fsname + 6);
304
305 if (tmp == *fsname)
306 return 0;
307
308 if (tmp)
309 *fsname = tmp;
310 return 1;
311}
312