Home | History | Annotate | Download | only in ext2fs
      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 Library
      9  * General Public License, version 2.
     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 #include "ext2_fs.h"
     35 #include "ext2fs.h"
     36 #include "ext2fsP.h"
     37 
     38 struct dir_list {
     39 	char	*name;
     40 	struct dir_list *next;
     41 };
     42 
     43 /*
     44  * This function adds an entry to the directory list
     45  */
     46 static void add_to_dirlist(const char *name, struct dir_list **list)
     47 {
     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  */
     66 static 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 
     78 static 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) ||
     96 		     ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
     97 			goto skip_to_next;
     98 		sprintf(path, "%s/%s", dirname, dp->d_name);
     99 		if (stat(path, &st) < 0)
    100 			goto skip_to_next;
    101 		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);
    105 			if (!cp) {
    106 				closedir(dir);
    107 				return ENOMEM;
    108 			}
    109 			strcpy(cp, path);
    110 			*ret_path = cp;
    111 			goto success;
    112 		}
    113 	skip_to_next:
    114 		dp = readdir(dir);
    115 	}
    116 success:
    117 	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  */
    126 char *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;
    131 	int    level = 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 			/* Avoid infinite loop */
    159 			if (++level >= EXT2FS_MAX_NESTED_LINKS)
    160 				break;
    161 		}
    162 	}
    163 	free_dirlist(&list);
    164 	free_dirlist(&new_list);
    165 	return ret_path;
    166 }
    167 
    168 
    169 #ifdef DEBUG
    170 int 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 }
    212 
    213 #endif
    214