blob: 88eb5cee91efc0c6b052e3b3b79c5f513029f268 [file] [log] [blame]
Theodore Ts'ode23aa12000-08-19 17:00:47 +00001/*
2 * finddev.c -- this routine attempts to find a particular device in
3 * /dev
Theodore Ts'oefc6f622008-08-27 23:07:54 -04004 *
Theodore Ts'ode23aa12000-08-19 17:00:47 +00005 * Copyright (C) 2000 Theodore Ts'o.
6 *
7 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04008 * This file may be redistributed under the terms of the GNU Library
9 * General Public License, version 2.
Theodore Ts'ode23aa12000-08-19 17:00:47 +000010 * %End-Header%
11 */
12
13#include <stdio.h>
14#include <string.h>
15#if HAVE_UNISTD_H
16#include <unistd.h>
17#endif
18#include <stdlib.h>
19#include <string.h>
20#if HAVE_SYS_TYPES_H
21#include <sys/types.h>
22#endif
23#if HAVE_SYS_STAT_H
24#include <sys/stat.h>
25#endif
26#include <dirent.h>
27#if HAVE_ERRNO_H
28#include <errno.h>
29#endif
30#if HAVE_SYS_MKDEV_H
31#include <sys/mkdev.h>
32#endif
33
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000034#include "ext2_fs.h"
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000035#include "ext2fs.h"
JP Abgralle0ed7402014-03-19 19:08:39 -070036#include "ext2fsP.h"
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000037
Theodore Ts'ode23aa12000-08-19 17:00:47 +000038struct dir_list {
39 char *name;
40 struct dir_list *next;
41};
42
Theodore Ts'ode23aa12000-08-19 17:00:47 +000043/*
44 * This function adds an entry to the directory list
45 */
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000046static void add_to_dirlist(const char *name, struct dir_list **list)
Theodore Ts'ode23aa12000-08-19 17:00:47 +000047{
48 struct dir_list *dp;
49
50 dp = malloc(sizeof(struct dir_list));
51 if (!dp)
52 return;
53 dp->name = malloc(strlen(name)+1);
54 if (!dp->name) {
55 free(dp);
56 return;
57 }
58 strcpy(dp->name, name);
59 dp->next = *list;
60 *list = dp;
61}
62
63/*
64 * This function frees a directory list
65 */
66static void free_dirlist(struct dir_list **list)
67{
68 struct dir_list *dp, *next;
69
70 for (dp = *list; dp; dp = next) {
71 next = dp->next;
72 free(dp->name);
73 free(dp);
74 }
75 *list = 0;
76}
77
78static int scan_dir(char *dirname, dev_t device, struct dir_list **list,
79 char **ret_path)
80{
81 DIR *dir;
82 struct dirent *dp;
83 char path[1024], *cp;
84 int dirlen;
85 struct stat st;
86
87 dirlen = strlen(dirname);
88 if ((dir = opendir(dirname)) == NULL)
89 return errno;
90 dp = readdir(dir);
91 while (dp) {
92 if (dirlen + strlen(dp->d_name) + 2 >= sizeof(path))
93 goto skip_to_next;
94 if (dp->d_name[0] == '.' &&
95 ((dp->d_name[1] == 0) ||
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000096 ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
Theodore Ts'ode23aa12000-08-19 17:00:47 +000097 goto skip_to_next;
98 sprintf(path, "%s/%s", dirname, dp->d_name);
99 if (stat(path, &st) < 0)
Theodore Ts'of86fa6c2000-08-20 21:51:24 +0000100 goto skip_to_next;
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000101 if (S_ISDIR(st.st_mode))
102 add_to_dirlist(path, list);
103 if (S_ISBLK(st.st_mode) && st.st_rdev == device) {
104 cp = malloc(strlen(path)+1);
Theodore Ts'o37517212001-07-29 12:01:09 -0400105 if (!cp) {
106 closedir(dir);
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000107 return ENOMEM;
Theodore Ts'o37517212001-07-29 12:01:09 -0400108 }
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000109 strcpy(cp, path);
110 *ret_path = cp;
Theodore Ts'o37517212001-07-29 12:01:09 -0400111 goto success;
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000112 }
113 skip_to_next:
114 dp = readdir(dir);
115 }
Theodore Ts'o37517212001-07-29 12:01:09 -0400116success:
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000117 closedir(dir);
118 return 0;
119}
120
121/*
122 * This function finds the pathname to a block device with a given
123 * device number. It returns a pointer to allocated memory to the
124 * pathname on success, and NULL on failure.
125 */
126char *ext2fs_find_block_device(dev_t device)
127{
128 struct dir_list *list = 0, *new_list = 0;
129 struct dir_list *current;
130 char *ret_path = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -0700131 int level = 0;
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000132
133 /*
134 * Add the starting directories to search...
135 */
136 add_to_dirlist("/devices", &list);
137 add_to_dirlist("/devfs", &list);
138 add_to_dirlist("/dev", &list);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400139
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000140 while (list) {
141 current = list;
142 list = list->next;
143#ifdef DEBUG
144 printf("Scanning directory %s\n", current->name);
145#endif
146 scan_dir(current->name, device, &new_list, &ret_path);
147 free(current->name);
148 free(current);
149 if (ret_path)
150 break;
151 /*
152 * If we're done checking at this level, descend to
153 * the next level of subdirectories. (breadth-first)
154 */
155 if (list == 0) {
156 list = new_list;
157 new_list = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -0700158 /* Avoid infinite loop */
159 if (++level >= EXT2FS_MAX_NESTED_LINKS)
160 break;
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000161 }
162 }
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000163 free_dirlist(&list);
164 free_dirlist(&new_list);
165 return ret_path;
166}
167
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400168
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000169#ifdef DEBUG
170int main(int argc, char** argv)
171{
172 char *devname, *tmp;
173 int major, minor;
174 dev_t device;
175 const char *errmsg = "Couldn't parse %s: %s\n";
176
177 if ((argc != 2) && (argc != 3)) {
178 fprintf(stderr, "Usage: %s device_number\n", argv[0]);
179 fprintf(stderr, "\t: %s major minor\n", argv[0]);
180 exit(1);
181 }
182 if (argc == 2) {
183 device = strtoul(argv[1], &tmp, 0);
184 if (*tmp) {
185 fprintf(stderr, errmsg, "device number", argv[1]);
186 exit(1);
187 }
188 } else {
189 major = strtoul(argv[1], &tmp, 0);
190 if (*tmp) {
191 fprintf(stderr, errmsg, "major number", argv[1]);
192 exit(1);
193 }
194 minor = strtoul(argv[2], &tmp, 0);
195 if (*tmp) {
196 fprintf(stderr, errmsg, "minor number", argv[2]);
197 exit(1);
198 }
199 device = makedev(major, minor);
200 printf("Looking for device 0x%04x (%d:%d)\n", device,
201 major, minor);
202 }
203 devname = ext2fs_find_block_device(device);
204 if (devname) {
205 printf("Found device! %s\n", devname);
206 free(devname);
207 } else {
208 printf("Couldn't find device.\n");
209 }
210 return 0;
211}
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400212
Theodore Ts'ode23aa12000-08-19 17:00:47 +0000213#endif