mdev.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 "libbb.h"
  12. #include "xregex.h"
  13. struct globals {
  14. int root_major, root_minor;
  15. };
  16. #define G (*(struct globals*)&bb_common_bufsiz1)
  17. #define root_major (G.root_major)
  18. #define root_minor (G.root_minor)
  19. #define MAX_SYSFS_DEPTH 3 /* prevent infinite loops in /sys symlinks */
  20. /* mknod in /dev based on a path like "/sys/block/hda/hda1" */
  21. static void make_device(char *path, int delete)
  22. {
  23. const char *device_name;
  24. int major, minor, type, len;
  25. int mode = 0660;
  26. uid_t uid = 0;
  27. gid_t gid = 0;
  28. char *temp = path + strlen(path);
  29. char *command = NULL;
  30. /* Try to read major/minor string. Note that the kernel puts \n after
  31. * the data, so we don't need to worry about null terminating the string
  32. * because sscanf() will stop at the first nondigit, which \n is. We
  33. * also depend on path having writeable space after it. */
  34. if (!delete) {
  35. strcat(path, "/dev");
  36. len = open_read_close(path, temp + 1, 64);
  37. *temp++ = 0;
  38. if (len < 1) return;
  39. }
  40. /* Determine device name, type, major and minor */
  41. device_name = bb_basename(path);
  42. type = path[5]=='c' ? S_IFCHR : S_IFBLK;
  43. /* If we have a config file, look up permissions for this device */
  44. if (ENABLE_FEATURE_MDEV_CONF) {
  45. char *conf, *pos, *end;
  46. int line, fd;
  47. /* mmap the config file */
  48. fd = open("/etc/mdev.conf", O_RDONLY);
  49. if (fd < 0)
  50. goto end_parse;
  51. len = xlseek(fd, 0, SEEK_END);
  52. conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
  53. close(fd);
  54. if (!conf)
  55. goto end_parse;
  56. line = 0;
  57. /* Loop through lines in mmaped file*/
  58. for (pos=conf; pos-conf<len;) {
  59. int field;
  60. char *end2;
  61. line++;
  62. /* find end of this line */
  63. for (end=pos; end-conf<len && *end!='\n'; end++)
  64. ;
  65. /* Three fields: regex, uid:gid, mode */
  66. for (field=0; field < (3 + ENABLE_FEATURE_MDEV_EXEC);
  67. field++)
  68. {
  69. /* Skip whitespace */
  70. while (pos<end && isspace(*pos)) pos++;
  71. if (pos==end || *pos=='#') break;
  72. for (end2=pos;
  73. end2<end && !isspace(*end2) && *end2!='#'; end2++)
  74. ;
  75. if (field == 0) {
  76. /* Regex to match this device */
  77. char *regex = xstrndup(pos, end2-pos);
  78. regex_t match;
  79. regmatch_t off;
  80. int result;
  81. /* Is this it? */
  82. xregcomp(&match,regex, REG_EXTENDED);
  83. result = regexec(&match, device_name, 1, &off, 0);
  84. regfree(&match);
  85. free(regex);
  86. /* If not this device, skip rest of line */
  87. if (result || off.rm_so
  88. || off.rm_eo != strlen(device_name))
  89. break;
  90. }
  91. if (field == 1) {
  92. /* uid:gid */
  93. char *s, *s2;
  94. /* Find : */
  95. for (s=pos; s<end2 && *s!=':'; s++)
  96. ;
  97. if (s == end2) break;
  98. /* Parse UID */
  99. uid = strtoul(pos, &s2, 10);
  100. if (s != s2) {
  101. struct passwd *pass;
  102. char *_unam = xstrndup(pos, s-pos);
  103. pass = getpwnam(_unam);
  104. free(_unam);
  105. if (!pass) break;
  106. uid = pass->pw_uid;
  107. }
  108. s++;
  109. /* parse GID */
  110. gid = strtoul(s, &s2, 10);
  111. if (end2 != s2) {
  112. struct group *grp;
  113. char *_grnam = xstrndup(s, end2-s);
  114. grp = getgrnam(_grnam);
  115. free(_grnam);
  116. if (!grp) break;
  117. gid = grp->gr_gid;
  118. }
  119. }
  120. if (field == 2) {
  121. /* mode */
  122. mode = strtoul(pos, &pos, 8);
  123. if (pos != end2) break;
  124. }
  125. if (ENABLE_FEATURE_MDEV_EXEC && field == 3) {
  126. // Command to run
  127. const char *s = "@$*";
  128. const char *s2;
  129. s2 = strchr(s, *pos++);
  130. if (!s2) {
  131. // Force error
  132. field = 1;
  133. break;
  134. }
  135. if ((s2-s+1) & (1<<delete))
  136. command = xstrndup(pos, end-pos);
  137. }
  138. pos = end2;
  139. }
  140. /* Did everything parse happily? */
  141. if (field > 2) break;
  142. if (field) bb_error_msg_and_die("bad line %d",line);
  143. /* Next line */
  144. pos = ++end;
  145. }
  146. munmap(conf, len);
  147. end_parse: /* nothing */ ;
  148. }
  149. umask(0);
  150. if (!delete) {
  151. if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
  152. if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
  153. bb_perror_msg_and_die("mknod %s", device_name);
  154. if (major == root_major && minor == root_minor)
  155. symlink(device_name, "root");
  156. if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid);
  157. }
  158. if (command) {
  159. /* setenv will leak memory, so use putenv */
  160. char *s = xasprintf("MDEV=%s", device_name);
  161. putenv(s);
  162. if (system(command) == -1)
  163. bb_perror_msg_and_die("cannot run %s", command);
  164. s[4] = '\0';
  165. unsetenv(s);
  166. free(s);
  167. free(command);
  168. }
  169. if (delete) unlink(device_name);
  170. }
  171. /* File callback for /sys/ traversal */
  172. static int fileAction(const char *fileName, struct stat *statbuf,
  173. void *userData, int depth)
  174. {
  175. size_t len = strlen(fileName) - 4;
  176. char *scratch = userData;
  177. if (strcmp(fileName + len, "/dev"))
  178. return FALSE;
  179. strcpy(scratch, fileName);
  180. scratch[len] = 0;
  181. make_device(scratch, 0);
  182. return TRUE;
  183. }
  184. /* Directory callback for /sys/ traversal */
  185. static int dirAction(const char *fileName, struct stat *statbuf,
  186. void *userData, int depth)
  187. {
  188. return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
  189. }
  190. /* For the full gory details, see linux/Documentation/firmware_class/README
  191. *
  192. * Firmware loading works like this:
  193. * - kernel sets FIRMWARE env var
  194. * - userspace checks /lib/firmware/$FIRMWARE
  195. * - userspace waits for /sys/$DEVPATH/loading to appear
  196. * - userspace writes "1" to /sys/$DEVPATH/loading
  197. * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
  198. * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
  199. * - kernel loads firmware into device
  200. */
  201. static void load_firmware(const char *const firmware, const char *const sysfs_path)
  202. {
  203. int cnt;
  204. int firmware_fd, loading_fd, data_fd;
  205. /* check for $FIRMWARE from kernel */
  206. /* XXX: dont bother: open(NULL) works same as open("no-such-file")
  207. * if (!firmware)
  208. * return;
  209. */
  210. /* check for /lib/firmware/$FIRMWARE */
  211. xchdir("/lib/firmware");
  212. firmware_fd = xopen(firmware, O_RDONLY);
  213. /* in case we goto out ... */
  214. data_fd = -1;
  215. /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
  216. xchdir(sysfs_path);
  217. for (cnt = 0; cnt < 30; ++cnt) {
  218. loading_fd = open("loading", O_WRONLY);
  219. if (loading_fd == -1)
  220. sleep(1);
  221. else
  222. break;
  223. }
  224. if (loading_fd == -1)
  225. goto out;
  226. /* tell kernel we're loading by `echo 1 > /sys/$DEVPATH/loading` */
  227. if (write(loading_fd, "1", 1) != 1)
  228. goto out;
  229. /* load firmware by `cat /lib/firmware/$FIRMWARE > /sys/$DEVPATH/data */
  230. data_fd = open("data", O_WRONLY);
  231. if (data_fd == -1)
  232. goto out;
  233. cnt = bb_copyfd_eof(firmware_fd, data_fd);
  234. /* tell kernel result by `echo [0|-1] > /sys/$DEVPATH/loading` */
  235. if (cnt > 0)
  236. write(loading_fd, "0", 1);
  237. else
  238. write(loading_fd, "-1", 2);
  239. out:
  240. if (ENABLE_FEATURE_CLEAN_UP) {
  241. close(firmware_fd);
  242. close(loading_fd);
  243. close(data_fd);
  244. }
  245. }
  246. int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  247. int mdev_main(int argc, char **argv)
  248. {
  249. char *action;
  250. char *env_path;
  251. RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
  252. xchdir("/dev");
  253. /* Scan */
  254. if (argc == 2 && !strcmp(argv[1],"-s")) {
  255. struct stat st;
  256. xstat("/", &st);
  257. root_major = major(st.st_dev);
  258. root_minor = minor(st.st_dev);
  259. recursive_action("/sys/block",
  260. ACTION_RECURSE | ACTION_FOLLOWLINKS,
  261. fileAction, dirAction, temp, 0);
  262. recursive_action("/sys/class",
  263. ACTION_RECURSE | ACTION_FOLLOWLINKS,
  264. fileAction, dirAction, temp, 0);
  265. /* Hotplug */
  266. } else {
  267. action = getenv("ACTION");
  268. env_path = getenv("DEVPATH");
  269. if (!action || !env_path)
  270. bb_show_usage();
  271. sprintf(temp, "/sys%s", env_path);
  272. if (!strcmp(action, "remove"))
  273. make_device(temp, 1);
  274. else if (!strcmp(action, "add")) {
  275. make_device(temp, 0);
  276. if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE)
  277. load_firmware(getenv("FIRMWARE"), temp);
  278. }
  279. }
  280. if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);
  281. return 0;
  282. }