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