finddev.c revision 31dbecd482405e0d3a67eb58e1a1c8cb9f2ad83e
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 34#if EXT2_FLAT_INCLUDES 35#include "ext2_fs.h" 36#else 37#include <linux/ext2_fs.h> 38#endif 39 40#include "ext2fs.h" 41 42struct dir_list { 43 char *name; 44 struct dir_list *next; 45}; 46 47/* 48 * This function adds an entry to the directory list 49 */ 50static void add_to_dirlist(const char *name, struct dir_list **list) 51{ 52 struct dir_list *dp; 53 54 dp = malloc(sizeof(struct dir_list)); 55 if (!dp) 56 return; 57 dp->name = malloc(strlen(name)+1); 58 if (!dp->name) { 59 free(dp); 60 return; 61 } 62 strcpy(dp->name, name); 63 dp->next = *list; 64 *list = dp; 65} 66 67/* 68 * This function frees a directory list 69 */ 70static void free_dirlist(struct dir_list **list) 71{ 72 struct dir_list *dp, *next; 73 74 for (dp = *list; dp; dp = next) { 75 next = dp->next; 76 free(dp->name); 77 free(dp); 78 } 79 *list = 0; 80} 81 82static int scan_dir(char *dirname, dev_t device, struct dir_list **list, 83 char **ret_path) 84{ 85 DIR *dir; 86 struct dirent *dp; 87 char path[1024], *cp; 88 int dirlen; 89 struct stat st; 90 91 dirlen = strlen(dirname); 92 if ((dir = opendir(dirname)) == NULL) 93 return errno; 94 dp = readdir(dir); 95 while (dp) { 96 if (dirlen + strlen(dp->d_name) + 2 >= sizeof(path)) 97 goto skip_to_next; 98 if (dp->d_name[0] == '.' && 99 ((dp->d_name[1] == 0) || 100 ((dp->d_name[1] == '.') && (dp->d_name[2] == 0)))) 101 goto skip_to_next; 102 sprintf(path, "%s/%s", dirname, dp->d_name); 103 if (stat(path, &st) < 0) 104 goto skip_to_next; 105 if (S_ISDIR(st.st_mode)) 106 add_to_dirlist(path, list); 107 if (S_ISBLK(st.st_mode) && st.st_rdev == device) { 108 cp = malloc(strlen(path)+1); 109 if (!cp) 110 return ENOMEM; 111 strcpy(cp, path); 112 *ret_path = cp; 113 return 0; 114 } 115 skip_to_next: 116 dp = readdir(dir); 117 } 118 closedir(dir); 119 return 0; 120} 121 122/* 123 * This function finds the pathname to a block device with a given 124 * device number. It returns a pointer to allocated memory to the 125 * pathname on success, and NULL on failure. 126 */ 127char *ext2fs_find_block_device(dev_t device) 128{ 129 struct dir_list *list = 0, *new_list = 0; 130 struct dir_list *current; 131 char *ret_path = 0; 132 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); 139 140 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; 158 } 159 } 160 free_dirlist(&list); 161 free_dirlist(&new_list); 162 return ret_path; 163} 164 165 166#ifdef DEBUG 167int main(int argc, char** argv) 168{ 169 char *devname, *tmp; 170 int major, minor; 171 dev_t device; 172 const char *errmsg = "Couldn't parse %s: %s\n"; 173 174 if ((argc != 2) && (argc != 3)) { 175 fprintf(stderr, "Usage: %s device_number\n", argv[0]); 176 fprintf(stderr, "\t: %s major minor\n", argv[0]); 177 exit(1); 178 } 179 if (argc == 2) { 180 device = strtoul(argv[1], &tmp, 0); 181 if (*tmp) { 182 fprintf(stderr, errmsg, "device number", argv[1]); 183 exit(1); 184 } 185 } else { 186 major = strtoul(argv[1], &tmp, 0); 187 if (*tmp) { 188 fprintf(stderr, errmsg, "major number", argv[1]); 189 exit(1); 190 } 191 minor = strtoul(argv[2], &tmp, 0); 192 if (*tmp) { 193 fprintf(stderr, errmsg, "minor number", argv[2]); 194 exit(1); 195 } 196 device = makedev(major, minor); 197 printf("Looking for device 0x%04x (%d:%d)\n", device, 198 major, minor); 199 } 200 devname = ext2fs_find_block_device(device); 201 if (devname) { 202 printf("Found device! %s\n", devname); 203 free(devname); 204 } else { 205 printf("Couldn't find device.\n"); 206 } 207 return 0; 208} 209 210#endif 211