3
0

finddev.c 4.0 KB

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