blob: 906c91fcd6767b20c3a5f2130522068ccade27b5 [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
2 * devno.c - find a particular device by its device number (major/minor)
3 *
4 * Copyright (C) 2000, 2001, 2003 Theodore Ts'o
5 * Copyright (C) 2001 Andreas Dilger
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the
9 * GNU Lesser General Public License.
10 * %End-Header%
11 */
12
13#include <stdio.h>
14#include <string.h>
15#ifdef HAVE_UNISTD_H
16#include <unistd.h>
17#endif
18#include <stdlib.h>
19#ifdef HAVE_SYS_TYPES_H
20#include <sys/types.h>
21#endif
22#ifdef HAVE_SYS_STAT_H
23#include <sys/stat.h>
24#endif
25#include <dirent.h>
26#ifdef HAVE_ERRNO_H
27#include <errno.h>
28#endif
29#ifdef HAVE_SYS_MKDEV_H
30#include <sys/mkdev.h>
31#endif
32#include <fcntl.h>
33#include <inttypes.h>
34
35#include "blkidP.h"
36#include "pathnames.h"
37#include "at.h"
38#include "sysfs.h"
39
40static char *blkid_strconcat(const char *a, const char *b, const char *c)
41{
42 char *res, *p;
43 size_t len, al, bl, cl;
44
45 al = a ? strlen(a) : 0;
46 bl = b ? strlen(b) : 0;
47 cl = c ? strlen(c) : 0;
48
49 len = al + bl + cl;
50 if (!len)
51 return NULL;
52 p = res = malloc(len + 1);
53 if (!res)
54 return NULL;
55 if (al) {
56 memcpy(p, a, al);
57 p += al;
58 }
59 if (bl) {
60 memcpy(p, b, bl);
61 p += bl;
62 }
63 if (cl) {
64 memcpy(p, c, cl);
65 p += cl;
66 }
67 *p = '\0';
68 return res;
69}
70
71/*
72 * This function adds an entry to the directory list
73 */
74static void add_to_dirlist(const char *dir, const char *subdir,
75 struct dir_list **list)
76{
77 struct dir_list *dp;
78
79 dp = malloc(sizeof(struct dir_list));
80 if (!dp)
81 return;
82 dp->name = subdir ? blkid_strconcat(dir, "/", subdir) :
83 dir ? strdup(dir) : NULL;
84
85 if (!dp->name) {
86 free(dp);
87 return;
88 }
89 dp->next = *list;
90 *list = dp;
91}
92
93/*
94 * This function frees a directory list
95 */
96static void free_dirlist(struct dir_list **list)
97{
98 struct dir_list *dp, *next;
99
100 for (dp = *list; dp; dp = next) {
101 next = dp->next;
102 free(dp->name);
103 free(dp);
104 }
105 *list = NULL;
106}
107
108void blkid__scan_dir(char *dirname, dev_t devno, struct dir_list **list,
109 char **devname)
110{
111 DIR *dir;
112 struct dirent *dp;
113 struct stat st;
114
115 if ((dir = opendir(dirname)) == NULL)
116 return;
117
118 while ((dp = readdir(dir)) != NULL) {
119#ifdef _DIRENT_HAVE_D_TYPE
120 if (dp->d_type != DT_UNKNOWN && dp->d_type != DT_BLK &&
121 dp->d_type != DT_LNK && dp->d_type != DT_DIR)
122 continue;
123#endif
124 if (dp->d_name[0] == '.' &&
125 ((dp->d_name[1] == 0) ||
126 ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
127 continue;
128
129 if (fstat_at(dirfd(dir), dirname, dp->d_name, &st, 0))
130 continue;
131
132 if (S_ISBLK(st.st_mode) && st.st_rdev == devno) {
133 *devname = blkid_strconcat(dirname, "/", dp->d_name);
134 DBG(DEBUG_DEVNO,
135 printf("found 0x%llx at %s\n", (long long)devno,
136 *devname));
137 break;
138 }
139
140 if (!list || !S_ISDIR(st.st_mode))
141 continue;
142
143 /* add subdirectory (but not symlink) to the list */
144#ifdef _DIRENT_HAVE_D_TYPE
145 if (dp->d_type == DT_LNK)
146 continue;
147 if (dp->d_type == DT_UNKNOWN)
148#endif
149 {
150 if (fstat_at(dirfd(dir), dirname, dp->d_name, &st, 1) ||
151 !S_ISDIR(st.st_mode))
152 continue; /* symlink or lstat() failed */
153 }
154
155 if (*dp->d_name == '.' || (
156#ifdef _DIRENT_HAVE_D_TYPE
157 dp->d_type == DT_DIR &&
158#endif
159 strcmp(dp->d_name, "shm") == 0))
160 /* ignore /dev/.{udev,mount,mdadm} and /dev/shm */
161 continue;
162
163 add_to_dirlist(dirname, dp->d_name, list);
164 }
165 closedir(dir);
166 return;
167}
168
169/* Directories where we will try to search for device numbers */
170static const char *devdirs[] = { "/devices", "/devfs", "/dev", NULL };
171
172/**
173 * SECTION: misc
174 * @title: Miscellaneous utils
175 * @short_description: mix of various utils for low-level and high-level API
176 */
177
178
179
180static char *scandev_devno_to_devpath(dev_t devno)
181{
182 struct dir_list *list = NULL, *new_list = NULL;
183 char *devname = NULL;
184 const char **dir;
185
186 /*
187 * Add the starting directories to search in reverse order of
188 * importance, since we are using a stack...
189 */
190 for (dir = devdirs; *dir; dir++)
191 add_to_dirlist(*dir, NULL, &list);
192
193 while (list) {
194 struct dir_list *current = list;
195
196 list = list->next;
197 DBG(DEBUG_DEVNO, printf("directory %s\n", current->name));
198 blkid__scan_dir(current->name, devno, &new_list, &devname);
199 free(current->name);
200 free(current);
201 if (devname)
202 break;
203 /*
204 * If we're done checking at this level, descend to
205 * the next level of subdirectories. (breadth-first)
206 */
207 if (list == NULL) {
208 list = new_list;
209 new_list = NULL;
210 }
211 }
212 free_dirlist(&list);
213 free_dirlist(&new_list);
214
215 return devname;
216}
217
218/**
219 * blkid_devno_to_devname:
220 * @devno: device number
221 *
222 * This function finds the pathname to a block device with a given
223 * device number.
224 *
225 * Returns: a pointer to allocated memory to the pathname on success,
226 * and NULL on failure.
227 */
228char *blkid_devno_to_devname(dev_t devno)
229{
230 char *path = NULL;
231 char buf[PATH_MAX];
232
233 path = sysfs_devno_to_devpath(devno, buf, sizeof(buf));
234 if (path)
235 path = strdup(path);
236 if (!path)
237 path = scandev_devno_to_devpath(devno);
238
239 if (!path) {
240 DBG(DEBUG_DEVNO,
241 printf("blkid: couldn't find devno 0x%04lx\n",
242 (unsigned long) devno));
243 } else {
244 DBG(DEBUG_DEVNO,
245 printf("found devno 0x%04llx as %s\n", (long long)devno, path));
246 }
247
248 return path;
249}
250
251
252/**
253 * blkid_devno_to_wholedisk:
254 * @dev: device number
255 * @diskname: buffer to return diskname (or NULL)
256 * @len: diskname buffer size (or 0)
257 * @diskdevno: pointer to returns devno of entire disk (or NULL)
258 *
259 * This function uses sysfs to convert the @devno device number to the *name*
260 * of the whole disk. The function DOES NOT return full device name. The @dev
261 * argument could be partition or whole disk -- both is converted.
262 *
263 * For example: sda1, 0x0801 --> sda, 0x0800
264 *
265 * For conversion to the full disk *path* use blkid_devno_to_devname(), for
266 * example:
267 *
268 * <informalexample>
269 * <programlisting>
270 *
271 * dev_t dev = 0x0801, disk; // sda1 = 8:1
272 * char *diskpath, diskname[32];
273 *
274 * blkid_devno_to_wholedisk(dev, diskname, sizeof(diskname), &disk);
275 * diskpath = blkid_devno_to_devname(disk);
276 *
277 * // print "0x0801: sda, /dev/sda, 8:0
278 * printf("0x%x: %s, %s, %d:%d\n",
279 * dev, diskname, diskpath, major(disk), minor(disk));
280 *
281 * free(diskpath);
282 *
283 * </programlisting>
284 * </informalexample>
285 *
286 * Returns: 0 on success or -1 in case of error.
287 */
288int blkid_devno_to_wholedisk(dev_t dev, char *diskname,
289 size_t len, dev_t *diskdevno)
290{
291 return sysfs_devno_to_wholedisk( dev, diskname, len, diskdevno);
292}
293
294/*
295 * Returns 1 if the @major number is associated with @drvname.
296 */
297int blkid_driver_has_major(const char *drvname, int major)
298{
299 FILE *f;
300 char buf[128];
301 int match = 0;
302
303 f = fopen(_PATH_PROC_DEVICES, "r");
304 if (!f)
305 return 0;
306
307 while (fgets(buf, sizeof(buf), f)) { /* skip to block dev section */
308 if (strncmp("Block devices:\n", buf, sizeof(buf)) == 0)
309 break;
310 }
311
312 while (fgets(buf, sizeof(buf), f)) {
313 int maj;
314 char name[64 + 1];
315
316 if (sscanf(buf, "%d %64[^\n ]", &maj, name) != 2)
317 continue;
318
319 if (maj == major && strcmp(name, drvname) == 0) {
320 match = 1;
321 break;
322 }
323 }
324
325 fclose(f);
326
327 DBG(DEBUG_DEVNO, printf("major %d %s associated with '%s' driver\n",
328 major, match ? "is" : "is NOT", drvname));
329 return match;
330}
331
332
333#ifdef TEST_PROGRAM
334int main(int argc, char** argv)
335{
336 char *devname, *tmp;
337 char diskname[PATH_MAX];
338 int major, minor;
339 dev_t devno, disk_devno;
340 const char *errmsg = "Couldn't parse %s: %s\n";
341
342 blkid_init_debug(DEBUG_ALL);
343 if ((argc != 2) && (argc != 3)) {
344 fprintf(stderr, "Usage:\t%s device_number\n\t%s major minor\n"
345 "Resolve a device number to a device name\n",
346 argv[0], argv[0]);
347 exit(1);
348 }
349 if (argc == 2) {
350 devno = strtoul(argv[1], &tmp, 0);
351 if (*tmp) {
352 fprintf(stderr, errmsg, "device number", argv[1]);
353 exit(1);
354 }
355 } else {
356 major = strtoul(argv[1], &tmp, 0);
357 if (*tmp) {
358 fprintf(stderr, errmsg, "major number", argv[1]);
359 exit(1);
360 }
361 minor = strtoul(argv[2], &tmp, 0);
362 if (*tmp) {
363 fprintf(stderr, errmsg, "minor number", argv[2]);
364 exit(1);
365 }
366 devno = makedev(major, minor);
367 }
368 printf("Looking for device 0x%04llx\n", (long long)devno);
369 devname = blkid_devno_to_devname(devno);
370 free(devname);
371
372 printf("Looking for whole-device for 0x%04llx\n", (long long)devno);
373 if (blkid_devno_to_wholedisk(devno, diskname,
374 sizeof(diskname), &disk_devno) == 0)
375 printf("found devno 0x%04llx as /dev/%s\n", (long long) disk_devno, diskname);
376
377 return 0;
378}
379#endif