mdev.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. *
  4. * mdev - Mini udev for busybox
  5. *
  6. * Copyright 2005 Rob Landley <rob@landley.net>
  7. * Copyright 2005 Frank Sorenson <frank@tuxrocks.com>
  8. *
  9. * Licensed under GPL version 2, see file LICENSE in this tarball for details.
  10. */
  11. #include "busybox.h"
  12. #include "xregex.h"
  13. #define DEV_PATH "/dev"
  14. struct mdev_globals
  15. {
  16. int root_major, root_minor;
  17. } mdev_globals;
  18. #define bbg mdev_globals
  19. /* mknod in /dev based on a path like "/sys/block/hda/hda1" */
  20. static void make_device(char *path, int delete)
  21. {
  22. char *device_name;
  23. int major, minor, type, len;
  24. int mode = 0660;
  25. uid_t uid = 0;
  26. gid_t gid = 0;
  27. char *temp = path + strlen(path);
  28. char *command = NULL;
  29. /* Try to read major/minor string. Note that the kernel puts \n after
  30. * the data, so we don't need to worry about null terminating the string
  31. * because sscanf() will stop at the first nondigit, which \n is. We
  32. * also depend on path having writeable space after it. */
  33. if (!delete) {
  34. strcat(path, "/dev");
  35. len = open_read_close(path, temp + 1, 64);
  36. *temp++ = 0;
  37. if (len < 1) return;
  38. }
  39. /* Determine device name, type, major and minor */
  40. device_name = strrchr(path, '/') + 1;
  41. type = path[5]=='c' ? S_IFCHR : S_IFBLK;
  42. /* If we have a config file, look up permissions for this device */
  43. if (ENABLE_FEATURE_MDEV_CONF) {
  44. char *conf, *pos, *end;
  45. int line, fd;
  46. /* mmap the config file */
  47. fd = open("/etc/mdev.conf", O_RDONLY);
  48. if (fd < 0)
  49. goto end_parse;
  50. len = xlseek(fd, 0, SEEK_END);
  51. conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
  52. close(fd);
  53. if (!conf)
  54. goto end_parse;
  55. line = 0;
  56. /* Loop through lines in mmaped file*/
  57. for (pos=conf; pos-conf<len;) {
  58. int field;
  59. char *end2;
  60. line++;
  61. /* find end of this line */
  62. for (end=pos; end-conf<len && *end!='\n'; end++)
  63. ;
  64. /* Three fields: regex, uid:gid, mode */
  65. for (field=0; field < (3 + ENABLE_FEATURE_MDEV_EXEC);
  66. field++)
  67. {
  68. /* Skip whitespace */
  69. while (pos<end && isspace(*pos)) pos++;
  70. if (pos==end || *pos=='#') break;
  71. for (end2=pos;
  72. end2<end && !isspace(*end2) && *end2!='#'; end2++)
  73. ;
  74. if (field == 0) {
  75. /* Regex to match this device */
  76. char *regex = strndupa(pos, end2-pos);
  77. regex_t match;
  78. regmatch_t off;
  79. int result;
  80. /* Is this it? */
  81. xregcomp(&match,regex, REG_EXTENDED);
  82. result = regexec(&match, device_name, 1, &off, 0);
  83. regfree(&match);
  84. /* If not this device, skip rest of line */
  85. if (result || off.rm_so
  86. || off.rm_eo != strlen(device_name))
  87. break;
  88. }
  89. if (field == 1) {
  90. /* uid:gid */
  91. char *s, *s2;
  92. /* Find : */
  93. for (s=pos; s<end2 && *s!=':'; s++)
  94. ;
  95. if (s == end2) break;
  96. /* Parse UID */
  97. uid = strtoul(pos, &s2, 10);
  98. if (s != s2) {
  99. struct passwd *pass;
  100. pass = getpwnam(strndupa(pos, s-pos));
  101. if (!pass) break;
  102. uid = pass->pw_uid;
  103. }
  104. s++;
  105. /* parse GID */
  106. gid = strtoul(s, &s2, 10);
  107. if (end2 != s2) {
  108. struct group *grp;
  109. grp = getgrnam(strndupa(s, end2-s));
  110. if (!grp) break;
  111. gid = grp->gr_gid;
  112. }
  113. }
  114. if (field == 2) {
  115. /* mode */
  116. mode = strtoul(pos, &pos, 8);
  117. if (pos != end2) break;
  118. }
  119. if (ENABLE_FEATURE_MDEV_EXEC && field == 3) {
  120. // Command to run
  121. char *s = "@$*", *s2;
  122. s2 = strchr(s, *pos++);
  123. if (!s2) {
  124. // Force error
  125. field = 1;
  126. break;
  127. }
  128. if ((s2-s+1) & (1<<delete))
  129. command = xstrndup(pos, end-pos);
  130. }
  131. pos = end2;
  132. }
  133. /* Did everything parse happily? */
  134. if (field > 2) break;
  135. if (field) bb_error_msg_and_die("bad line %d",line);
  136. /* Next line */
  137. pos = ++end;
  138. }
  139. munmap(conf, len);
  140. end_parse: /* nothing */ ;
  141. }
  142. umask(0);
  143. if (!delete) {
  144. if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
  145. if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
  146. bb_perror_msg_and_die("mknod %s", device_name);
  147. if (major == bbg.root_major && minor == bbg.root_minor)
  148. symlink(device_name, "root");
  149. if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid);
  150. }
  151. if (command) {
  152. int rc;
  153. char *s;
  154. s = xasprintf("MDEV=%s", device_name);
  155. putenv(s);
  156. rc = system(command);
  157. s[4] = 0;
  158. putenv(s);
  159. free(s);
  160. free(command);
  161. if (rc == -1) bb_perror_msg_and_die("cannot run %s", command);
  162. }
  163. if (delete) unlink(device_name);
  164. }
  165. /* Recursive search of /sys/block or /sys/class. path must be a writeable
  166. * buffer of size PATH_MAX containing the directory string to start at. */
  167. static void find_dev(char *path)
  168. {
  169. DIR *dir;
  170. size_t len = strlen(path);
  171. struct dirent *entry;
  172. dir = opendir(path);
  173. if (dir == NULL)
  174. return;
  175. while ((entry = readdir(dir)) != NULL) {
  176. struct stat st;
  177. /* Skip "." and ".." (also skips hidden files, which is ok) */
  178. if (entry->d_name[0] == '.')
  179. continue;
  180. // uClibc doesn't fill out entry->d_type reliably. so we use lstat().
  181. snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
  182. if (!lstat(path, &st) && S_ISDIR(st.st_mode)) find_dev(path);
  183. path[len] = 0;
  184. /* If there's a dev entry, mknod it */
  185. if (!strcmp(entry->d_name, "dev")) make_device(path, 0);
  186. }
  187. closedir(dir);
  188. }
  189. int mdev_main(int argc, char *argv[])
  190. {
  191. char *action;
  192. char *env_path;
  193. RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
  194. xchdir(DEV_PATH);
  195. /* Scan */
  196. if (argc == 2 && !strcmp(argv[1],"-s")) {
  197. struct stat st;
  198. xstat("/", &st);
  199. bbg.root_major = major(st.st_dev);
  200. bbg.root_minor = minor(st.st_dev);
  201. strcpy(temp,"/sys/block");
  202. find_dev(temp);
  203. strcpy(temp,"/sys/class");
  204. find_dev(temp);
  205. /* Hotplug */
  206. } else {
  207. action = getenv("ACTION");
  208. env_path = getenv("DEVPATH");
  209. if (!action || !env_path)
  210. bb_show_usage();
  211. sprintf(temp, "/sys%s", env_path);
  212. if (!strcmp(action, "add")) make_device(temp,0);
  213. else if (!strcmp(action, "remove")) make_device(temp,1);
  214. }
  215. if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);
  216. return 0;
  217. }