1 /*
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 "config.h"
14 #include <stdio.h>
15 #include <string.h>
16 #if HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19 #include <stdlib.h>
20 #include <string.h>
21 #if HAVE_SYS_TYPES_H
22 #include <sys/types.h>
23 #endif
24 #if HAVE_SYS_STAT_H
25 #include <sys/stat.h>
26 #endif
27 #include <dirent.h>
28 #if HAVE_ERRNO_H
29 #include <errno.h>
30 #endif
31 #if HAVE_SYS_MKDEV_H
32 #include <sys/mkdev.h>
33 #endif
34 #ifdef HAVE_SYS_SYSMACROS_H
35 #include <sys/sysmacros.h>
36 #endif
37
38 #include "blkidP.h"
39
40 #if defined(__GNUC__) && __GNUC__ >= 8
41 /* gcc incorrectly thinks the destination string is not being null-terminated */
42 #pragma GCC diagnostic push
43 #pragma GCC diagnostic ignored "-Wstringop-truncation"
44 #endif
45
blkid_strndup(const char * s,int length)46 char *blkid_strndup(const char *s, int length)
47 {
48 char *ret;
49
50 if (!s)
51 return NULL;
52
53 if (!length)
54 length = strlen(s);
55
56 ret = malloc(length + 1);
57 if (ret) {
58 strncpy(ret, s, length);
59 ret[length] = '\0';
60 }
61 return ret;
62 }
63
64 #if defined(__GNUC__) && __GNUC__ >= 8
65 #pragma GCC diagnostic pop
66 #endif
67
blkid_strdup(const char * s)68 char *blkid_strdup(const char *s)
69 {
70 return blkid_strndup(s, 0);
71 }
72
73 /*
74 * This function adds an entry to the directory list
75 */
add_to_dirlist(const char * name,struct dir_list ** list)76 static void add_to_dirlist(const char *name, struct dir_list **list)
77 {
78 struct dir_list *dp;
79
80 dp = malloc(sizeof(struct dir_list));
81 if (!dp)
82 return;
83 dp->name = blkid_strdup(name);
84 if (!dp->name) {
85 free(dp);
86 return;
87 }
88 dp->next = *list;
89 *list = dp;
90 }
91
92 /*
93 * This function frees a directory list
94 */
free_dirlist(struct dir_list ** list)95 static void free_dirlist(struct dir_list **list)
96 {
97 struct dir_list *dp, *next;
98
99 for (dp = *list; dp; dp = next) {
100 next = dp->next;
101 free(dp->name);
102 free(dp);
103 }
104 *list = NULL;
105 }
106
blkid__scan_dir(const char * dirname,dev_t devno,struct dir_list ** list,char ** devname)107 void blkid__scan_dir(const char *dirname, dev_t devno, struct dir_list **list,
108 char **devname)
109 {
110 DIR *dir;
111 struct dirent *dp;
112 char path[1024];
113 int dirlen;
114 struct stat st;
115
116 if ((dir = opendir(dirname)) == NULL)
117 return;
118 dirlen = strlen(dirname) + 2;
119 while ((dp = readdir(dir)) != 0) {
120 if (dirlen + strlen(dp->d_name) >= sizeof(path))
121 continue;
122
123 if (dp->d_name[0] == '.' &&
124 ((dp->d_name[1] == 0) ||
125 ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
126 continue;
127
128 sprintf(path, "%s/%s", dirname, dp->d_name);
129 if (stat(path, &st) < 0)
130 continue;
131
132 if (blkidP_is_disk_device(st.st_mode) && st.st_rdev == devno) {
133 *devname = blkid_strdup(path);
134 DBG(DEBUG_DEVNO,
135 printf("found 0x%llx at %s (%p)\n", (long long)devno,
136 path, *devname));
137 break;
138 }
139 if (list && S_ISDIR(st.st_mode) && !lstat(path, &st) &&
140 S_ISDIR(st.st_mode))
141 add_to_dirlist(path, list);
142 }
143 closedir(dir);
144 return;
145 }
146
147 /* Directories where we will try to search for device numbers */
148 static const char *devdirs[] = { "/devices", "/devfs", "/dev", NULL };
149
150 /*
151 * This function finds the pathname to a block device with a given
152 * device number. It returns a pointer to allocated memory to the
153 * pathname on success, and NULL on failure.
154 */
blkid_devno_to_devname(dev_t devno)155 char *blkid_devno_to_devname(dev_t devno)
156 {
157 struct dir_list *list = NULL, *new_list = NULL;
158 char *devname = NULL;
159 const char **dir;
160
161 /*
162 * Add the starting directories to search in reverse order of
163 * importance, since we are using a stack...
164 */
165 for (dir = devdirs; *dir; dir++)
166 add_to_dirlist(*dir, &list);
167
168 while (list) {
169 struct dir_list *current = list;
170
171 list = list->next;
172 DBG(DEBUG_DEVNO, printf("directory %s\n", current->name));
173 blkid__scan_dir(current->name, devno, &new_list, &devname);
174 free(current->name);
175 free(current);
176 if (devname)
177 break;
178 /*
179 * If we're done checking at this level, descend to
180 * the next level of subdirectories. (breadth-first)
181 */
182 if (list == NULL) {
183 list = new_list;
184 new_list = NULL;
185 }
186 }
187 free_dirlist(&list);
188 free_dirlist(&new_list);
189
190 if (!devname) {
191 DBG(DEBUG_DEVNO,
192 printf("blkid: couldn't find devno 0x%04lx\n",
193 (unsigned long) devno));
194 } else {
195 DBG(DEBUG_DEVNO,
196 printf("found devno 0x%04llx as %s\n", (long long)devno, devname));
197 }
198
199
200 return devname;
201 }
202
203 #ifdef TEST_PROGRAM
main(int argc,char ** argv)204 int main(int argc, char** argv)
205 {
206 char *devname, *tmp;
207 int major, minor;
208 dev_t devno;
209 const char *errmsg = "Couldn't parse %s: %s\n";
210
211 blkid_debug_mask = DEBUG_ALL;
212 if ((argc != 2) && (argc != 3)) {
213 fprintf(stderr, "Usage:\t%s device_number\n\t%s major minor\n"
214 "Resolve a device number to a device name\n",
215 argv[0], argv[0]);
216 exit(1);
217 }
218 if (argc == 2) {
219 devno = strtoul(argv[1], &tmp, 0);
220 if (*tmp) {
221 fprintf(stderr, errmsg, "device number", argv[1]);
222 exit(1);
223 }
224 } else {
225 major = strtoul(argv[1], &tmp, 0);
226 if (*tmp) {
227 fprintf(stderr, errmsg, "major number", argv[1]);
228 exit(1);
229 }
230 minor = strtoul(argv[2], &tmp, 0);
231 if (*tmp) {
232 fprintf(stderr, errmsg, "minor number", argv[2]);
233 exit(1);
234 }
235 devno = makedev(major, minor);
236 }
237 printf("Looking for device 0x%04llx\n", (long long)devno);
238 devname = blkid_devno_to_devname(devno);
239 free(devname);
240 return 0;
241 }
242 #endif
243