makedevs.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #include "libbb.h"
  10. #if ENABLE_FEATURE_MAKEDEVS_LEAF
  11. /*
  12. makedevs NAME TYPE MAJOR MINOR FIRST LAST [s]
  13. TYPEs:
  14. b Block device
  15. c Character device
  16. f FIFO
  17. FIRST..LAST specify numbers appended to NAME.
  18. If 's' is the last argument, the base device is created as well.
  19. Examples:
  20. makedevs /dev/ttyS c 4 66 2 63 -> ttyS2-ttyS63
  21. makedevs /dev/hda b 3 0 0 8 s -> hda,hda1-hda8
  22. */
  23. int makedevs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  24. int makedevs_main(int argc, char **argv)
  25. {
  26. mode_t mode;
  27. char *basedev, *type, *nodname, *buf;
  28. int Smajor, Sminor, S, E;
  29. if (argc < 7 || argv[1][0] == '-')
  30. bb_show_usage();
  31. basedev = argv[1];
  32. buf = xasprintf("%s%u", argv[1], (unsigned)-1);
  33. type = argv[2];
  34. Smajor = xatoi_positive(argv[3]);
  35. Sminor = xatoi_positive(argv[4]);
  36. S = xatoi_positive(argv[5]);
  37. E = xatoi_positive(argv[6]);
  38. nodname = argv[7] ? basedev : buf;
  39. mode = 0660;
  40. switch (type[0]) {
  41. case 'c':
  42. mode |= S_IFCHR;
  43. break;
  44. case 'b':
  45. mode |= S_IFBLK;
  46. break;
  47. case 'f':
  48. mode |= S_IFIFO;
  49. break;
  50. default:
  51. bb_show_usage();
  52. }
  53. while (S <= E) {
  54. sprintf(buf, "%s%u", basedev, S);
  55. /* if mode != S_IFCHR and != S_IFBLK,
  56. * third param in mknod() ignored */
  57. if (mknod(nodname, mode, makedev(Smajor, Sminor)))
  58. bb_perror_msg("can't create '%s'", nodname);
  59. /*if (nodname == basedev)*/ /* ex. /dev/hda - to /dev/hda1 ... */
  60. nodname = buf;
  61. S++;
  62. Sminor++;
  63. }
  64. return 0;
  65. }
  66. #elif ENABLE_FEATURE_MAKEDEVS_TABLE
  67. /* Licensed under GPLv2 or later, see file LICENSE in this source tree. */
  68. int makedevs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  69. int makedevs_main(int argc UNUSED_PARAM, char **argv)
  70. {
  71. parser_t *parser;
  72. char *line = (char *)"-";
  73. int ret = EXIT_SUCCESS;
  74. opt_complementary = "=1"; /* exactly one param */
  75. getopt32(argv, "d:", &line);
  76. argv += optind;
  77. xchdir(*argv); /* ensure root dir exists */
  78. umask(0);
  79. printf("rootdir=%s\ntable=", *argv);
  80. if (NOT_LONE_DASH(line)) {
  81. printf("'%s'\n", line);
  82. } else {
  83. puts("<stdin>");
  84. }
  85. parser = config_open(line);
  86. while (config_read(parser, &line, 1, 1, "# \t", PARSE_NORMAL)) {
  87. int linenum;
  88. char type;
  89. unsigned mode = 0755;
  90. unsigned major = 0;
  91. unsigned minor = 0;
  92. unsigned count = 0;
  93. unsigned increment = 0;
  94. unsigned start = 0;
  95. char name[41];
  96. char user[41];
  97. char group[41];
  98. char *full_name = name;
  99. uid_t uid;
  100. gid_t gid;
  101. linenum = parser->lineno;
  102. if ((2 > sscanf(line, "%40s %c %o %40s %40s %u %u %u %u %u",
  103. name, &type, &mode, user, group,
  104. &major, &minor, &start, &increment, &count))
  105. || ((unsigned)(major | minor | start | count | increment) > 255)
  106. ) {
  107. bb_error_msg("invalid line %d: '%s'", linenum, line);
  108. ret = EXIT_FAILURE;
  109. continue;
  110. }
  111. gid = (*group) ? get_ug_id(group, xgroup2gid) : getgid();
  112. uid = (*user) ? get_ug_id(user, xuname2uid) : getuid();
  113. /* We are already in the right root dir,
  114. * so make absolute paths relative */
  115. if ('/' == *full_name)
  116. full_name++;
  117. if (type == 'd') {
  118. bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR);
  119. if (chown(full_name, uid, gid) == -1) {
  120. chown_fail:
  121. bb_perror_msg("line %d: can't chown %s", linenum, full_name);
  122. ret = EXIT_FAILURE;
  123. continue;
  124. }
  125. if (chmod(full_name, mode) < 0) {
  126. chmod_fail:
  127. bb_perror_msg("line %d: can't chmod %s", linenum, full_name);
  128. ret = EXIT_FAILURE;
  129. continue;
  130. }
  131. } else if (type == 'f') {
  132. struct stat st;
  133. if ((stat(full_name, &st) < 0 || !S_ISREG(st.st_mode))) {
  134. bb_perror_msg("line %d: regular file '%s' does not exist", linenum, full_name);
  135. ret = EXIT_FAILURE;
  136. continue;
  137. }
  138. if (chown(full_name, uid, gid) < 0)
  139. goto chown_fail;
  140. if (chmod(full_name, mode) < 0)
  141. goto chmod_fail;
  142. } else {
  143. dev_t rdev;
  144. unsigned i;
  145. char *full_name_inc;
  146. if (type == 'p') {
  147. mode |= S_IFIFO;
  148. } else if (type == 'c') {
  149. mode |= S_IFCHR;
  150. } else if (type == 'b') {
  151. mode |= S_IFBLK;
  152. } else {
  153. bb_error_msg("line %d: unsupported file type %c", linenum, type);
  154. ret = EXIT_FAILURE;
  155. continue;
  156. }
  157. full_name_inc = xmalloc(strlen(full_name) + sizeof(int)*3 + 2);
  158. if (count)
  159. count--;
  160. for (i = start; i <= start + count; i++) {
  161. sprintf(full_name_inc, count ? "%s%u" : "%s", full_name, i);
  162. rdev = makedev(major, minor + (i - start) * increment);
  163. if (mknod(full_name_inc, mode, rdev) < 0) {
  164. bb_perror_msg("line %d: can't create node %s", linenum, full_name_inc);
  165. ret = EXIT_FAILURE;
  166. } else if (chown(full_name_inc, uid, gid) < 0) {
  167. bb_perror_msg("line %d: can't chown %s", linenum, full_name_inc);
  168. ret = EXIT_FAILURE;
  169. } else if (chmod(full_name_inc, mode) < 0) {
  170. bb_perror_msg("line %d: can't chmod %s", linenum, full_name_inc);
  171. ret = EXIT_FAILURE;
  172. }
  173. }
  174. free(full_name_inc);
  175. }
  176. }
  177. if (ENABLE_FEATURE_CLEAN_UP)
  178. config_close(parser);
  179. return ret;
  180. }
  181. #else
  182. # error makedevs configuration error, either leaf or table must be selected
  183. #endif