finddev.c revision f86fa6c7483f6fee33fb128fced4644ec876a1bc
1/* 2 * finddev.c -- this routine attempts to find a particular device in 3 * /dev 4 * 5 * Copyright (C) 2000 Theodore Ts'o. 6 * 7 * %Begin-Header% 8 * This file may be redistributed under the terms of the GNU Public 9 * 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 34struct dir_list { 35 char *name; 36 struct dir_list *next; 37}; 38 39/* 40 * This function adds an entry to the directory list 41 */ 42static void add_to_dirlist(char *name, struct dir_list **list) 43{ 44 struct dir_list *dp; 45 46 dp = malloc(sizeof(struct dir_list)); 47 if (!dp) 48 return; 49 dp->name = malloc(strlen(name)+1); 50 if (!dp->name) { 51 free(dp); 52 return; 53 } 54 strcpy(dp->name, name); 55 dp->next = *list; 56 *list = dp; 57} 58 59/* 60 * This function frees a directory list 61 */ 62static void free_dirlist(struct dir_list **list) 63{ 64 struct dir_list *dp, *next; 65 66 for (dp = *list; dp; dp = next) { 67 next = dp->next; 68 free(dp->name); 69 free(dp); 70 } 71 *list = 0; 72} 73 74static int scan_dir(char *dirname, dev_t device, struct dir_list **list, 75 char **ret_path) 76{ 77 DIR *dir; 78 struct dirent *dp; 79 char path[1024], *cp; 80 int dirlen; 81 struct stat st; 82 83 dirlen = strlen(dirname); 84 if ((dir = opendir(dirname)) == NULL) 85 return errno; 86 dp = readdir(dir); 87 while (dp) { 88 if (dirlen + strlen(dp->d_name) + 2 >= sizeof(path)) 89 goto skip_to_next; 90 if (dp->d_name[0] == '.' && 91 ((dp->d_name[1] == 0) || 92 (dp->d_name[1] == '.') && (dp->d_name[2] == 0))) 93 goto skip_to_next; 94 sprintf(path, "%s/%s", dirname, dp->d_name); 95 if (stat(path, &st) < 0) 96 goto skip_to_next; 97 if (S_ISDIR(st.st_mode)) 98 add_to_dirlist(path, list); 99 if (S_ISBLK(st.st_mode) && st.st_rdev == device) { 100 cp = malloc(strlen(path)+1); 101 if (!cp) 102 return ENOMEM; 103 strcpy(cp, path); 104 *ret_path = cp; 105 return 0; 106 } 107 skip_to_next: 108 dp = readdir(dir); 109 } 110 closedir(dir); 111 return 0; 112} 113 114/* 115 * This function finds the pathname to a block device with a given 116 * device number. It returns a pointer to allocated memory to the 117 * pathname on success, and NULL on failure. 118 */ 119char *ext2fs_find_block_device(dev_t device) 120{ 121 struct dir_list *list = 0, *new_list = 0; 122 struct dir_list *current; 123 char *ret_path = 0; 124 125 /* 126 * Add the starting directories to search... 127 */ 128 add_to_dirlist("/devices", &list); 129 add_to_dirlist("/devfs", &list); 130 add_to_dirlist("/dev", &list); 131 132 while (list) { 133 current = list; 134 list = list->next; 135#ifdef DEBUG 136 printf("Scanning directory %s\n", current->name); 137#endif 138 scan_dir(current->name, device, &new_list, &ret_path); 139 free(current->name); 140 free(current); 141 if (ret_path) 142 break; 143 /* 144 * If we're done checking at this level, descend to 145 * the next level of subdirectories. (breadth-first) 146 */ 147 if (list == 0) { 148 list = new_list; 149 new_list = 0; 150 } 151 } 152found_it: 153 free_dirlist(&list); 154 free_dirlist(&new_list); 155 return ret_path; 156} 157 158 159#ifdef DEBUG 160int main(int argc, char** argv) 161{ 162 char *devname, *tmp; 163 int major, minor; 164 dev_t device; 165 const char *errmsg = "Couldn't parse %s: %s\n"; 166 167 if ((argc != 2) && (argc != 3)) { 168 fprintf(stderr, "Usage: %s device_number\n", argv[0]); 169 fprintf(stderr, "\t: %s major minor\n", argv[0]); 170 exit(1); 171 } 172 if (argc == 2) { 173 device = strtoul(argv[1], &tmp, 0); 174 if (*tmp) { 175 fprintf(stderr, errmsg, "device number", argv[1]); 176 exit(1); 177 } 178 } else { 179 major = strtoul(argv[1], &tmp, 0); 180 if (*tmp) { 181 fprintf(stderr, errmsg, "major number", argv[1]); 182 exit(1); 183 } 184 minor = strtoul(argv[2], &tmp, 0); 185 if (*tmp) { 186 fprintf(stderr, errmsg, "minor number", argv[2]); 187 exit(1); 188 } 189 device = makedev(major, minor); 190 printf("Looking for device 0x%04x (%d:%d)\n", device, 191 major, minor); 192 } 193 devname = ext2fs_find_block_device(device); 194 if (devname) { 195 printf("Found device! %s\n", devname); 196 free(devname); 197 } else { 198 printf("Couldn't find device.\n"); 199 } 200 return 0; 201} 202 203#endif 204