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