Theodore Ts'o | de23aa1 | 2000-08-19 17:00:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | * finddev.c -- this routine attempts to find a particular device in |
| 3 | * /dev |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 4 | * |
Theodore Ts'o | de23aa1 | 2000-08-19 17:00:47 +0000 | [diff] [blame] | 5 | * Copyright (C) 2000 Theodore Ts'o. |
| 6 | * |
| 7 | * %Begin-Header% |
Theodore Ts'o | 543547a | 2010-05-17 21:31:56 -0400 | [diff] [blame] | 8 | * This file may be redistributed under the terms of the GNU Library |
| 9 | * General Public License, version 2. |
Theodore Ts'o | de23aa1 | 2000-08-19 17:00:47 +0000 | [diff] [blame] | 10 | * %End-Header% |
| 11 | */ |
| 12 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 13 | #include "config.h" |
Theodore Ts'o | de23aa1 | 2000-08-19 17:00:47 +0000 | [diff] [blame] | 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 | |
Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 35 | #include "ext2_fs.h" |
Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 36 | #include "ext2fs.h" |
Niu Yawei | 08ae93a | 2011-11-19 23:08:03 -0500 | [diff] [blame] | 37 | #include "ext2fsP.h" |
Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 38 | |
Theodore Ts'o | de23aa1 | 2000-08-19 17:00:47 +0000 | [diff] [blame] | 39 | struct dir_list { |
| 40 | char *name; |
| 41 | struct dir_list *next; |
| 42 | }; |
| 43 | |
Theodore Ts'o | de23aa1 | 2000-08-19 17:00:47 +0000 | [diff] [blame] | 44 | /* |
| 45 | * This function adds an entry to the directory list |
| 46 | */ |
Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 47 | static void add_to_dirlist(const char *name, struct dir_list **list) |
Theodore Ts'o | de23aa1 | 2000-08-19 17:00:47 +0000 | [diff] [blame] | 48 | { |
| 49 | struct dir_list *dp; |
| 50 | |
| 51 | dp = malloc(sizeof(struct dir_list)); |
| 52 | if (!dp) |
| 53 | return; |
| 54 | dp->name = malloc(strlen(name)+1); |
| 55 | if (!dp->name) { |
| 56 | free(dp); |
| 57 | return; |
| 58 | } |
| 59 | strcpy(dp->name, name); |
| 60 | dp->next = *list; |
| 61 | *list = dp; |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * This function frees a directory list |
| 66 | */ |
| 67 | static void free_dirlist(struct dir_list **list) |
| 68 | { |
| 69 | struct dir_list *dp, *next; |
| 70 | |
| 71 | for (dp = *list; dp; dp = next) { |
| 72 | next = dp->next; |
| 73 | free(dp->name); |
| 74 | free(dp); |
| 75 | } |
| 76 | *list = 0; |
| 77 | } |
| 78 | |
| 79 | static int scan_dir(char *dirname, dev_t device, struct dir_list **list, |
| 80 | char **ret_path) |
| 81 | { |
| 82 | DIR *dir; |
| 83 | struct dirent *dp; |
| 84 | char path[1024], *cp; |
| 85 | int dirlen; |
| 86 | struct stat st; |
| 87 | |
| 88 | dirlen = strlen(dirname); |
| 89 | if ((dir = opendir(dirname)) == NULL) |
| 90 | return errno; |
| 91 | dp = readdir(dir); |
| 92 | while (dp) { |
| 93 | if (dirlen + strlen(dp->d_name) + 2 >= sizeof(path)) |
| 94 | goto skip_to_next; |
| 95 | if (dp->d_name[0] == '.' && |
| 96 | ((dp->d_name[1] == 0) || |
Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 97 | ((dp->d_name[1] == '.') && (dp->d_name[2] == 0)))) |
Theodore Ts'o | de23aa1 | 2000-08-19 17:00:47 +0000 | [diff] [blame] | 98 | goto skip_to_next; |
| 99 | sprintf(path, "%s/%s", dirname, dp->d_name); |
| 100 | if (stat(path, &st) < 0) |
Theodore Ts'o | f86fa6c | 2000-08-20 21:51:24 +0000 | [diff] [blame] | 101 | goto skip_to_next; |
Theodore Ts'o | de23aa1 | 2000-08-19 17:00:47 +0000 | [diff] [blame] | 102 | if (S_ISDIR(st.st_mode)) |
| 103 | add_to_dirlist(path, list); |
| 104 | if (S_ISBLK(st.st_mode) && st.st_rdev == device) { |
| 105 | cp = malloc(strlen(path)+1); |
Theodore Ts'o | 3751721 | 2001-07-29 12:01:09 -0400 | [diff] [blame] | 106 | if (!cp) { |
| 107 | closedir(dir); |
Theodore Ts'o | de23aa1 | 2000-08-19 17:00:47 +0000 | [diff] [blame] | 108 | return ENOMEM; |
Theodore Ts'o | 3751721 | 2001-07-29 12:01:09 -0400 | [diff] [blame] | 109 | } |
Theodore Ts'o | de23aa1 | 2000-08-19 17:00:47 +0000 | [diff] [blame] | 110 | strcpy(cp, path); |
| 111 | *ret_path = cp; |
Theodore Ts'o | 3751721 | 2001-07-29 12:01:09 -0400 | [diff] [blame] | 112 | goto success; |
Theodore Ts'o | de23aa1 | 2000-08-19 17:00:47 +0000 | [diff] [blame] | 113 | } |
| 114 | skip_to_next: |
| 115 | dp = readdir(dir); |
| 116 | } |
Theodore Ts'o | 3751721 | 2001-07-29 12:01:09 -0400 | [diff] [blame] | 117 | success: |
Theodore Ts'o | de23aa1 | 2000-08-19 17:00:47 +0000 | [diff] [blame] | 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 | */ |
| 127 | char *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; |
Niu Yawei | 08ae93a | 2011-11-19 23:08:03 -0500 | [diff] [blame] | 132 | int level = 0; |
Theodore Ts'o | de23aa1 | 2000-08-19 17:00:47 +0000 | [diff] [blame] | 133 | |
| 134 | /* |
| 135 | * Add the starting directories to search... |
| 136 | */ |
| 137 | add_to_dirlist("/devices", &list); |
| 138 | add_to_dirlist("/devfs", &list); |
| 139 | add_to_dirlist("/dev", &list); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 140 | |
Theodore Ts'o | de23aa1 | 2000-08-19 17:00:47 +0000 | [diff] [blame] | 141 | while (list) { |
| 142 | current = list; |
| 143 | list = list->next; |
| 144 | #ifdef DEBUG |
| 145 | printf("Scanning directory %s\n", current->name); |
| 146 | #endif |
| 147 | scan_dir(current->name, device, &new_list, &ret_path); |
| 148 | free(current->name); |
| 149 | free(current); |
| 150 | if (ret_path) |
| 151 | break; |
| 152 | /* |
| 153 | * If we're done checking at this level, descend to |
| 154 | * the next level of subdirectories. (breadth-first) |
| 155 | */ |
| 156 | if (list == 0) { |
| 157 | list = new_list; |
| 158 | new_list = 0; |
Niu Yawei | 08ae93a | 2011-11-19 23:08:03 -0500 | [diff] [blame] | 159 | /* Avoid infinite loop */ |
| 160 | if (++level >= EXT2FS_MAX_NESTED_LINKS) |
| 161 | break; |
Theodore Ts'o | de23aa1 | 2000-08-19 17:00:47 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
Theodore Ts'o | de23aa1 | 2000-08-19 17:00:47 +0000 | [diff] [blame] | 164 | free_dirlist(&list); |
| 165 | free_dirlist(&new_list); |
| 166 | return ret_path; |
| 167 | } |
| 168 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 169 | |
Theodore Ts'o | de23aa1 | 2000-08-19 17:00:47 +0000 | [diff] [blame] | 170 | #ifdef DEBUG |
| 171 | int main(int argc, char** argv) |
| 172 | { |
| 173 | char *devname, *tmp; |
| 174 | int major, minor; |
| 175 | dev_t device; |
| 176 | const char *errmsg = "Couldn't parse %s: %s\n"; |
| 177 | |
| 178 | if ((argc != 2) && (argc != 3)) { |
| 179 | fprintf(stderr, "Usage: %s device_number\n", argv[0]); |
| 180 | fprintf(stderr, "\t: %s major minor\n", argv[0]); |
| 181 | exit(1); |
| 182 | } |
| 183 | if (argc == 2) { |
| 184 | device = strtoul(argv[1], &tmp, 0); |
| 185 | if (*tmp) { |
| 186 | fprintf(stderr, errmsg, "device number", argv[1]); |
| 187 | exit(1); |
| 188 | } |
| 189 | } else { |
| 190 | major = strtoul(argv[1], &tmp, 0); |
| 191 | if (*tmp) { |
| 192 | fprintf(stderr, errmsg, "major number", argv[1]); |
| 193 | exit(1); |
| 194 | } |
| 195 | minor = strtoul(argv[2], &tmp, 0); |
| 196 | if (*tmp) { |
| 197 | fprintf(stderr, errmsg, "minor number", argv[2]); |
| 198 | exit(1); |
| 199 | } |
| 200 | device = makedev(major, minor); |
| 201 | printf("Looking for device 0x%04x (%d:%d)\n", device, |
| 202 | major, minor); |
| 203 | } |
| 204 | devname = ext2fs_find_block_device(device); |
| 205 | if (devname) { |
| 206 | printf("Found device! %s\n", devname); |
| 207 | free(devname); |
| 208 | } else { |
| 209 | printf("Couldn't find device.\n"); |
| 210 | } |
| 211 | return 0; |
| 212 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 213 | |
Theodore Ts'o | de23aa1 | 2000-08-19 17:00:47 +0000 | [diff] [blame] | 214 | #endif |