blob: c9f5c92c28262296c06b542ea8cfdfcfa374f1c1 [file] [log] [blame]
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001/*
2 * devno.c - find a particular device by its device number (major/minor)
3 *
Theodore Ts'o50b380b2003-02-12 23:51:21 -05004 * Copyright (C) 2000, 2001, 2003 Theodore Ts'o
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05005 * 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#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'o7a603aa2003-01-26 01:54:39 -050034#include "blkidP.h"
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050035
Theodore Ts'o50b380b2003-02-12 23:51:21 -050036char *blkid_strndup(const char *s, int length)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050037{
38 char *ret;
39
40 if (!s)
41 return NULL;
42
Theodore Ts'o50b380b2003-02-12 23:51:21 -050043 if (!length)
44 length = strlen(s);
45
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050046 ret = malloc(length + 1);
47 if (ret) {
48 strncpy(ret, s, length);
49 ret[length] = '\0';
50 }
51 return ret;
52}
53
Theodore Ts'o50b380b2003-02-12 23:51:21 -050054char *blkid_strdup(const char *s)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050055{
Theodore Ts'o50b380b2003-02-12 23:51:21 -050056 return blkid_strndup(s, 0);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050057}
58
59/*
60 * This function adds an entry to the directory list
61 */
62static void add_to_dirlist(const char *name, struct dir_list **list)
63{
64 struct dir_list *dp;
65
66 dp = malloc(sizeof(struct dir_list));
67 if (!dp)
68 return;
Theodore Ts'o50b380b2003-02-12 23:51:21 -050069 dp->name = blkid_strdup(name);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050070 if (!dp->name) {
71 free(dp);
72 return;
73 }
74 dp->next = *list;
75 *list = dp;
76}
77
78/*
79 * This function frees a directory list
80 */
81static void free_dirlist(struct dir_list **list)
82{
83 struct dir_list *dp, *next;
84
85 for (dp = *list; dp; dp = next) {
86 next = dp->next;
Theodore Ts'o50b380b2003-02-12 23:51:21 -050087 free(dp->name);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050088 free(dp);
89 }
90 *list = NULL;
91}
92
Theodore Ts'of4e89bc2008-08-26 08:13:56 -040093void blkid__scan_dir(char *dirname, dev_t devno, struct dir_list **list,
94 char **devname)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050095{
96 DIR *dir;
97 struct dirent *dp;
98 char path[1024];
99 int dirlen;
100 struct stat st;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500101
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500102 if ((dir = opendir(dirname)) == NULL)
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500103 return;
104 dirlen = strlen(dirname) + 2;
105 while ((dp = readdir(dir)) != 0) {
106 if (dirlen + strlen(dp->d_name) >= sizeof(path))
107 continue;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500108
109 if (dp->d_name[0] == '.' &&
110 ((dp->d_name[1] == 0) ||
111 ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500112 continue;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500113
114 sprintf(path, "%s/%s", dirname, dp->d_name);
115 if (stat(path, &st) < 0)
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500116 continue;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500117
Theodore Ts'o5b984ab2006-09-24 22:33:38 -0400118 if (S_ISBLK(st.st_mode) && st.st_rdev == devno) {
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500119 *devname = blkid_strdup(path);
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500120 DBG(DEBUG_DEVNO,
Matthias Andree12a829d2006-05-30 01:48:51 +0200121 printf("found 0x%llx at %s (%p)\n", (long long)devno,
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500122 path, *devname));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500123 break;
124 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400125 if (list && S_ISDIR(st.st_mode) && !lstat(path, &st) &&
Theodore Ts'o5b984ab2006-09-24 22:33:38 -0400126 S_ISDIR(st.st_mode))
127 add_to_dirlist(path, list);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500128 }
129 closedir(dir);
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500130 return;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500131}
132
133/* Directories where we will try to search for device numbers */
Theodore Ts'o0bb74062008-08-27 00:00:44 -0400134static const char *devdirs[] = { "/devices", "/devfs", "/dev", NULL };
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500135
136/*
137 * This function finds the pathname to a block device with a given
138 * device number. It returns a pointer to allocated memory to the
139 * pathname on success, and NULL on failure.
140 */
141char *blkid_devno_to_devname(dev_t devno)
142{
143 struct dir_list *list = NULL, *new_list = NULL;
144 char *devname = NULL;
145 const char **dir;
146
147 /*
148 * Add the starting directories to search in reverse order of
149 * importance, since we are using a stack...
150 */
Theodore Ts'o0bb74062008-08-27 00:00:44 -0400151 for (dir = devdirs; *dir; dir++)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500152 add_to_dirlist(*dir, &list);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500153
154 while (list) {
155 struct dir_list *current = list;
156
157 list = list->next;
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500158 DBG(DEBUG_DEVNO, printf("directory %s\n", current->name));
Theodore Ts'of4e89bc2008-08-26 08:13:56 -0400159 blkid__scan_dir(current->name, devno, &new_list, &devname);
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500160 free(current->name);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500161 free(current);
162 if (devname)
163 break;
164 /*
165 * If we're done checking at this level, descend to
166 * the next level of subdirectories. (breadth-first)
167 */
168 if (list == NULL) {
169 list = new_list;
170 new_list = NULL;
171 }
172 }
173 free_dirlist(&list);
174 free_dirlist(&new_list);
175
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500176 if (!devname) {
177 DBG(DEBUG_DEVNO,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400178 printf("blkid: couldn't find devno 0x%04lx\n",
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500179 (unsigned long) devno));
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500180 } else {
181 DBG(DEBUG_DEVNO,
Matthias Andree12a829d2006-05-30 01:48:51 +0200182 printf("found devno 0x%04llx as %s\n", (long long)devno, devname));
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500183 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400184
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500185
186 return devname;
187}
188
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500189#ifdef TEST_PROGRAM
190int main(int argc, char** argv)
191{
192 char *devname, *tmp;
193 int major, minor;
194 dev_t devno;
195 const char *errmsg = "Couldn't parse %s: %s\n";
196
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500197 blkid_debug_mask = DEBUG_ALL;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500198 if ((argc != 2) && (argc != 3)) {
199 fprintf(stderr, "Usage:\t%s device_number\n\t%s major minor\n"
200 "Resolve a device number to a device name\n",
201 argv[0], argv[0]);
202 exit(1);
203 }
204 if (argc == 2) {
205 devno = strtoul(argv[1], &tmp, 0);
206 if (*tmp) {
207 fprintf(stderr, errmsg, "device number", argv[1]);
208 exit(1);
209 }
210 } else {
211 major = strtoul(argv[1], &tmp, 0);
212 if (*tmp) {
213 fprintf(stderr, errmsg, "major number", argv[1]);
214 exit(1);
215 }
216 minor = strtoul(argv[2], &tmp, 0);
217 if (*tmp) {
218 fprintf(stderr, errmsg, "minor number", argv[2]);
219 exit(1);
220 }
221 devno = makedev(major, minor);
222 }
Matthias Andree12a829d2006-05-30 01:48:51 +0200223 printf("Looking for device 0x%04llx\n", (long long)devno);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500224 devname = blkid_devno_to_devname(devno);
Jim Meyering45e338f2009-02-23 18:07:50 +0100225 free(devname);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500226 return 0;
227}
228#endif