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 "config.h"
     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 #ifdef HAVE_SYS_SYSMACROS_H
     35 #include <sys/sysmacros.h>
     36 #endif
     37 
     38 #include "ext2_fs.h"
     39 #include "ext2fs.h"
     40 #include "ext2fsP.h"
     41 
     42 struct dir_list {
     43 	char	*name;
     44 	struct dir_list *next;
     45 };
     46 
     47 /*
     48  * This function adds an entry to the directory list
     49  */
     50 static 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  */
     70 static 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 
     82 static 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 				closedir(dir);
    111 				return ENOMEM;
    112 			}
    113 			strcpy(cp, path);
    114 			*ret_path = cp;
    115 			goto success;
    116 		}
    117 	skip_to_next:
    118 		dp = readdir(dir);
    119 	}
    120 success:
    121 	closedir(dir);
    122 	return 0;
    123 }
    124 
    125 /*
    126  * This function finds the pathname to a block device with a given
    127  * device number.  It returns a pointer to allocated memory to the
    128  * pathname on success, and NULL on failure.
    129  */
    130 char *ext2fs_find_block_device(dev_t device)
    131 {
    132 	struct dir_list *list = 0, *new_list = 0;
    133 	struct dir_list *current;
    134 	char	*ret_path = 0;
    135 	int    level = 0;
    136 
    137 	/*
    138 	 * Add the starting directories to search...
    139 	 */
    140 	add_to_dirlist("/devices", &list);
    141 	add_to_dirlist("/devfs", &list);
    142 	add_to_dirlist("/dev", &list);
    143 
    144 	while (list) {
    145 		current = list;
    146 		list = list->next;
    147 #ifdef DEBUG
    148 		printf("Scanning directory %s\n", current->name);
    149 #endif
    150 		scan_dir(current->name, device, &new_list, &ret_path);
    151 		free(current->name);
    152 		free(current);
    153 		if (ret_path)
    154 			break;
    155 		/*
    156 		 * If we're done checking at this level, descend to
    157 		 * the next level of subdirectories. (breadth-first)
    158 		 */
    159 		if (list == 0) {
    160 			list = new_list;
    161 			new_list = 0;
    162 			/* Avoid infinite loop */
    163 			if (++level >= EXT2FS_MAX_NESTED_LINKS)
    164 				break;
    165 		}
    166 	}
    167 	free_dirlist(&list);
    168 	free_dirlist(&new_list);
    169 	return ret_path;
    170 }
    171 
    172 
    173 #ifdef DEBUG
    174 int main(int argc, char** argv)
    175 {
    176 	char	*devname, *tmp;
    177 	int	major, minor;
    178 	dev_t	device;
    179 	const char *errmsg = "Couldn't parse %s: %s\n";
    180 
    181 	if ((argc != 2) && (argc != 3)) {
    182 		fprintf(stderr, "Usage: %s device_number\n", argv[0]);
    183 		fprintf(stderr, "\t: %s major minor\n", argv[0]);
    184 		exit(1);
    185 	}
    186 	if (argc == 2) {
    187 		device = strtoul(argv[1], &tmp, 0);
    188 		if (*tmp) {
    189 			fprintf(stderr, errmsg, "device number", argv[1]);
    190 			exit(1);
    191 		}
    192 	} else {
    193 		major = strtoul(argv[1], &tmp, 0);
    194 		if (*tmp) {
    195 			fprintf(stderr, errmsg, "major number", argv[1]);
    196 			exit(1);
    197 		}
    198 		minor = strtoul(argv[2], &tmp, 0);
    199 		if (*tmp) {
    200 			fprintf(stderr, errmsg, "minor number", argv[2]);
    201 			exit(1);
    202 		}
    203 		device = makedev(major, minor);
    204 		printf("Looking for device 0x%04x (%d:%d)\n", device,
    205 		       major, minor);
    206 	}
    207 	devname = ext2fs_find_block_device(device);
    208 	if (devname) {
    209 		printf("Found device!  %s\n", devname);
    210 		free(devname);
    211 	} else {
    212 		printf("Couldn't find device.\n");
    213 	}
    214 	return 0;
    215 }
    216 
    217 #endif
    218