makedevs.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * public domain -- Dave 'Kill a Cop' Cinege <dcinege@psychosis.com>
  4. *
  5. * makedevs
  6. * Make ranges of device files quickly.
  7. * known bugs: can't deal with alpha ranges
  8. */
  9. //usage:#if ENABLE_FEATURE_MAKEDEVS_LEAF
  10. //usage:#define makedevs_trivial_usage
  11. //usage: "NAME TYPE MAJOR MINOR FIRST LAST [s]"
  12. //usage:#define makedevs_full_usage "\n\n"
  13. //usage: "Create a range of block or character special files"
  14. //usage: "\n"
  15. //usage: "\nTYPE is:"
  16. //usage: "\n b Block device"
  17. //usage: "\n c Character device"
  18. //usage: "\n f FIFO, MAJOR and MINOR are ignored"
  19. //usage: "\n"
  20. //usage: "\nFIRST..LAST specify numbers appended to NAME."
  21. //usage: "\nIf 's' is the last argument, the base device is created as well."
  22. //usage: "\n"
  23. //usage: "\nExamples:"
  24. //usage: "\n makedevs /dev/ttyS c 4 66 2 63 -> ttyS2-ttyS63"
  25. //usage: "\n makedevs /dev/hda b 3 0 0 8 s -> hda,hda1-hda8"
  26. //usage:
  27. //usage:#define makedevs_example_usage
  28. //usage: "# makedevs /dev/ttyS c 4 66 2 63\n"
  29. //usage: "[creates ttyS2-ttyS63]\n"
  30. //usage: "# makedevs /dev/hda b 3 0 0 8 s\n"
  31. //usage: "[creates hda,hda1-hda8]\n"
  32. //usage:#endif
  33. //usage:
  34. //usage:#if ENABLE_FEATURE_MAKEDEVS_TABLE
  35. //usage:#define makedevs_trivial_usage
  36. //usage: "[-d device_table] rootdir"
  37. //usage:#define makedevs_full_usage "\n\n"
  38. //usage: "Create a range of special files as specified in a device table.\n"
  39. //usage: "Device table entries take the form of:\n"
  40. //usage: "<name> <type> <mode> <uid> <gid> <major> <minor> <start> <inc> <count>\n"
  41. //usage: "Where name is the file name, type can be one of:\n"
  42. //usage: " f Regular file\n"
  43. //usage: " d Directory\n"
  44. //usage: " c Character device\n"
  45. //usage: " b Block device\n"
  46. //usage: " p Fifo (named pipe)\n"
  47. //usage: "uid is the user id for the target file, gid is the group id for the\n"
  48. //usage: "target file. The rest of the entries (major, minor, etc) apply to\n"
  49. //usage: "to device special files. A '-' may be used for blank entries."
  50. //usage:
  51. //usage:#define makedevs_example_usage
  52. //usage: "For example:\n"
  53. //usage: "<name> <type> <mode><uid><gid><major><minor><start><inc><count>\n"
  54. //usage: "/dev d 755 0 0 - - - - -\n"
  55. //usage: "/dev/console c 666 0 0 5 1 - - -\n"
  56. //usage: "/dev/null c 666 0 0 1 3 0 0 -\n"
  57. //usage: "/dev/zero c 666 0 0 1 5 0 0 -\n"
  58. //usage: "/dev/hda b 640 0 0 3 0 0 0 -\n"
  59. //usage: "/dev/hda b 640 0 0 3 1 1 1 15\n\n"
  60. //usage: "Will Produce:\n"
  61. //usage: "/dev\n"
  62. //usage: "/dev/console\n"
  63. //usage: "/dev/null\n"
  64. //usage: "/dev/zero\n"
  65. //usage: "/dev/hda\n"
  66. //usage: "/dev/hda[0-15]\n"
  67. //usage:#endif
  68. #include "libbb.h"
  69. #if ENABLE_FEATURE_MAKEDEVS_LEAF
  70. /*
  71. makedevs NAME TYPE MAJOR MINOR FIRST LAST [s]
  72. TYPEs:
  73. b Block device
  74. c Character device
  75. f FIFO
  76. FIRST..LAST specify numbers appended to NAME.
  77. If 's' is the last argument, the base device is created as well.
  78. Examples:
  79. makedevs /dev/ttyS c 4 66 2 63 -> ttyS2-ttyS63
  80. makedevs /dev/hda b 3 0 0 8 s -> hda,hda1-hda8
  81. */
  82. int makedevs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  83. int makedevs_main(int argc, char **argv)
  84. {
  85. mode_t mode;
  86. char *basedev, *type, *nodname, *buf;
  87. int Smajor, Sminor, S, E;
  88. if (argc < 7 || argv[1][0] == '-')
  89. bb_show_usage();
  90. basedev = argv[1];
  91. buf = xasprintf("%s%u", argv[1], (unsigned)-1);
  92. type = argv[2];
  93. Smajor = xatoi_positive(argv[3]);
  94. Sminor = xatoi_positive(argv[4]);
  95. S = xatoi_positive(argv[5]);
  96. E = xatoi_positive(argv[6]);
  97. nodname = argv[7] ? basedev : buf;
  98. mode = 0660;
  99. switch (type[0]) {
  100. case 'c':
  101. mode |= S_IFCHR;
  102. break;
  103. case 'b':
  104. mode |= S_IFBLK;
  105. break;
  106. case 'f':
  107. mode |= S_IFIFO;
  108. break;
  109. default:
  110. bb_show_usage();
  111. }
  112. while (S <= E) {
  113. sprintf(buf, "%s%u", basedev, S);
  114. /* if mode != S_IFCHR and != S_IFBLK,
  115. * third param in mknod() ignored */
  116. if (mknod(nodname, mode, makedev(Smajor, Sminor)))
  117. bb_perror_msg("can't create '%s'", nodname);
  118. /*if (nodname == basedev)*/ /* ex. /dev/hda - to /dev/hda1 ... */
  119. nodname = buf;
  120. S++;
  121. Sminor++;
  122. }
  123. return 0;
  124. }
  125. #elif ENABLE_FEATURE_MAKEDEVS_TABLE
  126. /* Licensed under GPLv2 or later, see file LICENSE in this source tree. */
  127. int makedevs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  128. int makedevs_main(int argc UNUSED_PARAM, char **argv)
  129. {
  130. parser_t *parser;
  131. char *line = (char *)"-";
  132. int ret = EXIT_SUCCESS;
  133. opt_complementary = "=1"; /* exactly one param */
  134. getopt32(argv, "d:", &line);
  135. argv += optind;
  136. xchdir(*argv); /* ensure root dir exists */
  137. umask(0);
  138. printf("rootdir=%s\ntable=", *argv);
  139. if (NOT_LONE_DASH(line)) {
  140. printf("'%s'\n", line);
  141. } else {
  142. puts("<stdin>");
  143. }
  144. parser = config_open(line);
  145. while (config_read(parser, &line, 1, 1, "# \t", PARSE_NORMAL)) {
  146. int linenum;
  147. char type;
  148. unsigned mode = 0755;
  149. unsigned major = 0;
  150. unsigned minor = 0;
  151. unsigned count = 0;
  152. unsigned increment = 0;
  153. unsigned start = 0;
  154. char name[41];
  155. char user[41];
  156. char group[41];
  157. char *full_name = name;
  158. uid_t uid;
  159. gid_t gid;
  160. linenum = parser->lineno;
  161. if ((2 > sscanf(line, "%40s %c %o %40s %40s %u %u %u %u %u",
  162. name, &type, &mode, user, group,
  163. &major, &minor, &start, &increment, &count))
  164. || ((unsigned)(major | minor | start | count | increment) > 255)
  165. ) {
  166. bb_error_msg("invalid line %d: '%s'", linenum, line);
  167. ret = EXIT_FAILURE;
  168. continue;
  169. }
  170. gid = (*group) ? get_ug_id(group, xgroup2gid) : getgid();
  171. uid = (*user) ? get_ug_id(user, xuname2uid) : getuid();
  172. /* We are already in the right root dir,
  173. * so make absolute paths relative */
  174. if ('/' == *full_name)
  175. full_name++;
  176. if (type == 'd') {
  177. bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR);
  178. if (chown(full_name, uid, gid) == -1) {
  179. chown_fail:
  180. bb_perror_msg("line %d: can't chown %s", linenum, full_name);
  181. ret = EXIT_FAILURE;
  182. continue;
  183. }
  184. if (chmod(full_name, mode) < 0) {
  185. chmod_fail:
  186. bb_perror_msg("line %d: can't chmod %s", linenum, full_name);
  187. ret = EXIT_FAILURE;
  188. continue;
  189. }
  190. } else if (type == 'f') {
  191. struct stat st;
  192. if ((stat(full_name, &st) < 0 || !S_ISREG(st.st_mode))) {
  193. bb_perror_msg("line %d: regular file '%s' does not exist", linenum, full_name);
  194. ret = EXIT_FAILURE;
  195. continue;
  196. }
  197. if (chown(full_name, uid, gid) < 0)
  198. goto chown_fail;
  199. if (chmod(full_name, mode) < 0)
  200. goto chmod_fail;
  201. } else {
  202. dev_t rdev;
  203. unsigned i;
  204. char *full_name_inc;
  205. if (type == 'p') {
  206. mode |= S_IFIFO;
  207. } else if (type == 'c') {
  208. mode |= S_IFCHR;
  209. } else if (type == 'b') {
  210. mode |= S_IFBLK;
  211. } else {
  212. bb_error_msg("line %d: unsupported file type %c", linenum, type);
  213. ret = EXIT_FAILURE;
  214. continue;
  215. }
  216. full_name_inc = xmalloc(strlen(full_name) + sizeof(int)*3 + 2);
  217. if (count)
  218. count--;
  219. for (i = start; i <= start + count; i++) {
  220. sprintf(full_name_inc, count ? "%s%u" : "%s", full_name, i);
  221. rdev = makedev(major, minor + (i - start) * increment);
  222. if (mknod(full_name_inc, mode, rdev) < 0) {
  223. bb_perror_msg("line %d: can't create node %s", linenum, full_name_inc);
  224. ret = EXIT_FAILURE;
  225. } else if (chown(full_name_inc, uid, gid) < 0) {
  226. bb_perror_msg("line %d: can't chown %s", linenum, full_name_inc);
  227. ret = EXIT_FAILURE;
  228. } else if (chmod(full_name_inc, mode) < 0) {
  229. bb_perror_msg("line %d: can't chmod %s", linenum, full_name_inc);
  230. ret = EXIT_FAILURE;
  231. }
  232. }
  233. free(full_name_inc);
  234. }
  235. }
  236. if (ENABLE_FEATURE_CLEAN_UP)
  237. config_close(parser);
  238. return ret;
  239. }
  240. #else
  241. # error makedevs configuration error, either leaf or table must be selected
  242. #endif