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 
11 //kbuild:lib-$(CONFIG_BLKID) += get_devname.o
12 //kbuild:lib-$(CONFIG_FINDFS) += get_devname.o
13 //kbuild:lib-$(CONFIG_FEATURE_MOUNT_LABEL) += get_devname.o
14 //kbuild:lib-$(CONFIG_FEATURE_SWAPONOFF_LABEL) += get_devname.o
15 
16 #include <sys/mount.h> /* BLKGETSIZE64 */
17 #if !defined(BLKGETSIZE64)
18 # define BLKGETSIZE64 _IOR(0x12,114,size_t)
19 #endif
20 #include "volume_id_internal.h"
21 
22 static struct uuidCache_s {
23 	struct uuidCache_s *next;
24 //	int major, minor;
25 	char *device;
26 	char *label;
27 	char *uc_uuid; /* prefix makes it easier to grep for */
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 /* Returns !0 on error.
39  * Otherwise, returns malloc'ed strings for label and uuid
40  * (and they can't be NULL, although they can be "").
41  * NB: closes fd. */
42 static int
get_label_uuid(int fd,char ** label,char ** uuid,const char ** type)43 get_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 	/* fd is owned by vid now */
50 	vid = volume_id_open_node(fd);
51 
52 	if (ioctl(/*vid->*/fd, BLKGETSIZE64, &size) != 0)
53 		size = 0;
54 
55 	if (volume_id_probe_all(vid, /*0,*/ 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); /* also closes fd */
75 	return rv;
76 }
77 
78 /* NB: we take ownership of (malloc'ed) label and uuid */
79 static void
uuidcache_addentry(char * device,char * label,char * uuid,const char * type)80 uuidcache_addentry(char *device, /*int major, int minor,*/ 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 	/*last->next = NULL; - xzalloc did it*/
93 //	last->major = major;
94 //	last->minor = minor;
95 	last->device = device;
96 	last->label = label;
97 	last->uc_uuid = uuid;
98 	IF_FEATURE_BLKID_TYPE(last->type = type;)
99 }
100 
101 /* If get_label_uuid() on device_name returns success,
102  * add a cache entry for this device.
103  * If device node does not exist, it will be temporarily created. */
104 static int FAST_FUNC
uuidcache_check_device(struct recursive_state * state UNUSED_PARAM,const char * device,struct stat * statbuf)105 uuidcache_check_device(struct recursive_state *state UNUSED_PARAM,
106 		const char *device,
107 		struct stat *statbuf)
108 {
109 	/* note: this check rejects links to devices, among other nodes */
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 	/* Users report that mucking with floppies (especially non-present
118 	 * ones) is significant PITA. This is a horribly dirty hack,
119 	 * but it is very useful in real world.
120 	 * If this will ever need to be enabled, consider using O_NONBLOCK.
121 	 */
122 	if (major(statbuf->st_rdev) == 2)
123 		return TRUE;
124 
125 	add_to_uuid_cache(device);
126 
127 	return TRUE;
128 }
129 
130 static struct uuidCache_s*
uuidcache_init(int scan_devices)131 uuidcache_init(int scan_devices)
132 {
133 	dbg("DBG: uuidCache=%x, uuidCache");
134 	if (uuidCache)
135 		return uuidCache;
136 
137 	/* We were scanning /proc/partitions
138 	 * and /proc/sys/dev/cdrom/info here.
139 	 * Missed volume managers. I see that "standard" blkid uses these:
140 	 * /dev/mapper/control
141 	 * /proc/devices
142 	 * /proc/evms/volumes
143 	 * /proc/lvm/VGs
144 	 * This is unacceptably complex. Let's just scan /dev.
145 	 * (Maybe add scanning of /sys/block/XXX/dev for devices
146 	 * somehow not having their /dev/XXX entries created?) */
147 	if (scan_devices) {
148 		recursive_action("/dev", ACTION_RECURSE,
149 			uuidcache_check_device, /* file_action */
150 			NULL, /* dir_action */
151 			NULL /* userData */
152 		);
153 	}
154 
155 	return uuidCache;
156 }
157 
158 #define UUID   1
159 #define VOL    2
160 
161 #ifdef UNUSED
162 static char *
get_spec_by_x(int n,const char * t,int * majorPtr,int * minorPtr)163 get_spec_by_x(int n, const char *t, int *majorPtr, int *minorPtr)
164 {
165 	struct uuidCache_s *uc;
166 
167 	uc = uuidcache_init(/*scan_devices:*/ 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 
190 static unsigned char
fromhex(char c)191 fromhex(char c)
192 {
193 	if (isdigit(c))
194 		return (c - '0');
195 	return ((c|0x20) - 'a' + 10);
196 }
197 
198 static char *
get_spec_by_uuid(const char * s,int * major,int * minor)199 get_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 
224 static char *
get_spec_by_volume_label(const char * s,int * major,int * minor)225 get_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 // UNUSED
230 
231 /* Used by blkid */
display_uuid_cache(int scan_devices)232 void 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 
add_to_uuid_cache(const char * device)252 int add_to_uuid_cache(const char *device)
253 {
254 	char *uuid = uuid; /* for compiler */
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 	/* get_label_uuid() closes fd in all cases (success & failure) */
266 	if (get_label_uuid(fd, &label, &uuid, &type) == 0) {
267 		/* uuidcache_addentry() takes ownership of all four params */
268 		uuidcache_addentry(xstrdup(device), /*ma, mi,*/ label, uuid, type);
269 		return 1;
270 	}
271 	return 0;
272 }
273 
274 
275 /* Used by mount and findfs */
276 
get_devname_from_label(const char * spec)277 char *get_devname_from_label(const char *spec)
278 {
279 	struct uuidCache_s *uc;
280 
281 	uc = uuidcache_init(/*scan_devices:*/ 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 
get_devname_from_uuid(const char * spec)291 char *get_devname_from_uuid(const char *spec)
292 {
293 	struct uuidCache_s *uc;
294 
295 	uc = uuidcache_init(/*scan_devices:*/ 1);
296 	while (uc) {
297 		/* case of hex numbers doesn't matter */
298 		if (strcasecmp(spec, uc->uc_uuid) == 0) {
299 			return xstrdup(uc->device);
300 		}
301 		uc = uc->next;
302 	}
303 	return NULL;
304 }
305 
resolve_mount_spec(char ** fsname)306 int 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; /* no UUID= or LABEL= prefix found */
317 
318 	if (tmp)
319 		*fsname = tmp;
320 	return 1;
321 }
322