makedevs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 as
  5. * published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU Library General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. *
  16. */
  17. #define _GNU_SOURCE
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <fcntl.h>
  22. #include <getopt.h>
  23. #include <time.h>
  24. #include <pwd.h>
  25. #include <grp.h>
  26. #include <unistd.h>
  27. #include <ctype.h>
  28. #include <errno.h>
  29. #include <libgen.h>
  30. #include <stdarg.h>
  31. #include <sys/stat.h>
  32. #include <sys/types.h>
  33. #ifndef __APPLE__
  34. #include <sys/sysmacros.h> /* major() and minor() */
  35. #endif
  36. const char *bb_applet_name;
  37. void bb_verror_msg(const char *s, va_list p)
  38. {
  39. fflush(stdout);
  40. fprintf(stderr, "%s: ", bb_applet_name);
  41. vfprintf(stderr, s, p);
  42. }
  43. void bb_error_msg(const char *s, ...)
  44. {
  45. va_list p;
  46. va_start(p, s);
  47. bb_verror_msg(s, p);
  48. va_end(p);
  49. putc('\n', stderr);
  50. }
  51. void bb_error_msg_and_die(const char *s, ...)
  52. {
  53. va_list p;
  54. va_start(p, s);
  55. bb_verror_msg(s, p);
  56. va_end(p);
  57. putc('\n', stderr);
  58. exit(1);
  59. }
  60. void bb_vperror_msg(const char *s, va_list p)
  61. {
  62. int err=errno;
  63. if(s == 0) s = "";
  64. bb_verror_msg(s, p);
  65. if (*s) s = ": ";
  66. fprintf(stderr, "%s%s\n", s, strerror(err));
  67. }
  68. void bb_perror_msg(const char *s, ...)
  69. {
  70. va_list p;
  71. va_start(p, s);
  72. bb_vperror_msg(s, p);
  73. va_end(p);
  74. }
  75. void bb_perror_msg_and_die(const char *s, ...)
  76. {
  77. va_list p;
  78. va_start(p, s);
  79. bb_vperror_msg(s, p);
  80. va_end(p);
  81. exit(1);
  82. }
  83. FILE *bb_xfopen(const char *path, const char *mode)
  84. {
  85. FILE *fp;
  86. if ((fp = fopen(path, mode)) == NULL)
  87. bb_perror_msg_and_die("%s", path);
  88. return fp;
  89. }
  90. enum {
  91. FILEUTILS_PRESERVE_STATUS = 1,
  92. FILEUTILS_DEREFERENCE = 2,
  93. FILEUTILS_RECUR = 4,
  94. FILEUTILS_FORCE = 8,
  95. FILEUTILS_INTERACTIVE = 16
  96. };
  97. int bb_make_directory (char *path, long mode, int flags)
  98. {
  99. mode_t mask;
  100. const char *fail_msg;
  101. char *s = path;
  102. char c;
  103. struct stat st;
  104. mask = umask(0);
  105. if (mode == -1) {
  106. umask(mask);
  107. mode = (S_IXUSR | S_IXGRP | S_IXOTH |
  108. S_IWUSR | S_IWGRP | S_IWOTH |
  109. S_IRUSR | S_IRGRP | S_IROTH) & ~mask;
  110. } else {
  111. umask(mask & ~0300);
  112. }
  113. do {
  114. c = 0;
  115. if (flags & FILEUTILS_RECUR) { /* Get the parent. */
  116. /* Bypass leading non-'/'s and then subsequent '/'s. */
  117. while (*s) {
  118. if (*s == '/') {
  119. do {
  120. ++s;
  121. } while (*s == '/');
  122. c = *s; /* Save the current char */
  123. *s = 0; /* and replace it with nul. */
  124. break;
  125. }
  126. ++s;
  127. }
  128. }
  129. if (mkdir(path, 0777) < 0) {
  130. /* If we failed for any other reason than the directory
  131. * already exists, output a diagnostic and return -1.*/
  132. if ((errno != EEXIST && errno != EISDIR)
  133. || !(flags & FILEUTILS_RECUR)
  134. || (stat(path, &st) < 0 || !S_ISDIR(st.st_mode))) {
  135. fail_msg = "create";
  136. umask(mask);
  137. break;
  138. }
  139. /* Since the directory exists, don't attempt to change
  140. * permissions if it was the full target. Note that
  141. * this is not an error conditon. */
  142. if (!c) {
  143. umask(mask);
  144. return 0;
  145. }
  146. }
  147. if (!c) {
  148. /* Done. If necessary, updated perms on the newly
  149. * created directory. Failure to update here _is_
  150. * an error.*/
  151. umask(mask);
  152. if ((mode != -1) && (chmod(path, mode) < 0)){
  153. fail_msg = "set permissions of";
  154. break;
  155. }
  156. return 0;
  157. }
  158. /* Remove any inserted nul from the path (recursive mode). */
  159. *s = c;
  160. } while (1);
  161. bb_perror_msg ("Cannot %s directory `%s'", fail_msg, path);
  162. return -1;
  163. }
  164. const char * const bb_msg_memory_exhausted = "memory exhausted";
  165. void *xmalloc(size_t size)
  166. {
  167. void *ptr = malloc(size);
  168. if (ptr == NULL && size != 0)
  169. bb_error_msg_and_die(bb_msg_memory_exhausted);
  170. return ptr;
  171. }
  172. void *xcalloc(size_t nmemb, size_t size)
  173. {
  174. void *ptr = calloc(nmemb, size);
  175. if (ptr == NULL && nmemb != 0 && size != 0)
  176. bb_error_msg_and_die(bb_msg_memory_exhausted);
  177. return ptr;
  178. }
  179. void *xrealloc(void *ptr, size_t size)
  180. {
  181. ptr = realloc(ptr, size);
  182. if (ptr == NULL && size != 0)
  183. bb_error_msg_and_die(bb_msg_memory_exhausted);
  184. return ptr;
  185. }
  186. char *private_get_line_from_file(FILE *file, int c)
  187. {
  188. #define GROWBY (80) /* how large we will grow strings by */
  189. int ch;
  190. int idx = 0;
  191. char *linebuf = NULL;
  192. int linebufsz = 0;
  193. while ((ch = getc(file)) != EOF) {
  194. /* grow the line buffer as necessary */
  195. if (idx > linebufsz - 2) {
  196. linebuf = xrealloc(linebuf, linebufsz += GROWBY);
  197. }
  198. linebuf[idx++] = (char)ch;
  199. if (!ch) return linebuf;
  200. if (c<2 && ch == '\n') {
  201. if (c) {
  202. --idx;
  203. }
  204. break;
  205. }
  206. }
  207. if (linebuf) {
  208. if (ferror(file)) {
  209. free(linebuf);
  210. return NULL;
  211. }
  212. linebuf[idx] = 0;
  213. }
  214. return linebuf;
  215. }
  216. char *bb_get_chomped_line_from_file(FILE *file)
  217. {
  218. return private_get_line_from_file(file, 1);
  219. }
  220. long my_getpwnam(const char *name)
  221. {
  222. struct passwd *myuser;
  223. myuser = getpwnam(name);
  224. if (myuser==NULL)
  225. bb_error_msg_and_die("unknown user name: %s", name);
  226. return myuser->pw_uid;
  227. }
  228. long my_getgrnam(const char *name)
  229. {
  230. struct group *mygroup;
  231. mygroup = getgrnam(name);
  232. if (mygroup==NULL)
  233. bb_error_msg_and_die("unknown group name: %s", name);
  234. return (mygroup->gr_gid);
  235. }
  236. unsigned long get_ug_id(const char *s, long (*my_getxxnam)(const char *))
  237. {
  238. unsigned long r;
  239. char *p;
  240. r = strtoul(s, &p, 10);
  241. if (*p || (s == p)) {
  242. r = my_getxxnam(s);
  243. }
  244. return r;
  245. }
  246. char * last_char_is(const char *s, int c)
  247. {
  248. char *sret = (char *)s;
  249. if (sret) {
  250. sret = strrchr(sret, c);
  251. if(sret != NULL && *(sret+1) != 0)
  252. sret = NULL;
  253. }
  254. return sret;
  255. }
  256. void bb_xasprintf(char **string_ptr, const char *format, ...)
  257. {
  258. va_list p;
  259. int r;
  260. va_start(p, format);
  261. r = vasprintf(string_ptr, format, p);
  262. va_end(p);
  263. if (r < 0) {
  264. bb_perror_msg_and_die("bb_xasprintf");
  265. }
  266. }
  267. char *concat_path_file(const char *path, const char *filename)
  268. {
  269. char *outbuf;
  270. char *lc;
  271. if (!path)
  272. path = "";
  273. lc = last_char_is(path, '/');
  274. while (*filename == '/')
  275. filename++;
  276. bb_xasprintf(&outbuf, "%s%s%s", path, (lc==NULL ? "/" : ""), filename);
  277. return outbuf;
  278. }
  279. void bb_show_usage(void)
  280. {
  281. fprintf(stderr, "%s: [-d device_table] rootdir\n\n", bb_applet_name);
  282. fprintf(stderr, "Creates a batch of special files as specified in a device table.\n");
  283. fprintf(stderr, "Device table entries take the form of:\n");
  284. fprintf(stderr, "name type mode user group major minor start increment count\n\n");
  285. fprintf(stderr, "Where name is the file name, type can be one of:\n");
  286. fprintf(stderr, " f A regular file\n");
  287. fprintf(stderr, " d Directory\n");
  288. fprintf(stderr, " c Character special device file\n");
  289. fprintf(stderr, " b Block special device file\n");
  290. fprintf(stderr, " p Fifo (named pipe)\n");
  291. fprintf(stderr, "uid is the user id for the target file, gid is the group id for the\n");
  292. fprintf(stderr, "target file. The rest of the entries (major, minor, etc) apply to\n");
  293. fprintf(stderr, "to device special files. A '-' may be used for blank entries.\n\n");
  294. fprintf(stderr, "For example:\n");
  295. fprintf(stderr, "<name> <type> <mode> <uid> <gid> <major> <minor> <start> <inc> <count>\n");
  296. fprintf(stderr, "/dev d 755 0 0 - - - - -\n");
  297. fprintf(stderr, "/dev/console c 666 0 0 5 1 - - -\n");
  298. fprintf(stderr, "/dev/null c 666 0 0 1 3 0 0 -\n");
  299. fprintf(stderr, "/dev/zero c 666 0 0 1 5 0 0 -\n");
  300. fprintf(stderr, "/dev/hda b 640 0 0 3 0 0 0 -\n");
  301. fprintf(stderr, "/dev/hda b 640 0 0 3 1 1 1 15\n");
  302. fprintf(stderr, "/dev/rtp b 640 0 0 250 0 0 1 5\n");
  303. fprintf(stderr, "/dev/gps b 640 0 0 251 0 1 1 5\n");
  304. fprintf(stderr, "/dev/uio b 640 0 0 252 0 1 2 5\n");
  305. fprintf(stderr, "/dev/uio b 640 0 0 252 1 6 2 5\n\n");
  306. fprintf(stderr, "Will Produce:\n");
  307. fprintf(stderr, "/dev\n");
  308. fprintf(stderr, "/dev/console\n");
  309. fprintf(stderr, "/dev/null\n");
  310. fprintf(stderr, "/dev/zero\n");
  311. fprintf(stderr, "/dev/hda\n");
  312. fprintf(stderr, "/dev/hda[1-15] with minor numbers [1-15]\n");
  313. fprintf(stderr, "/dev/rtp[0-4] with minor numbers [0-4]\n");
  314. fprintf(stderr, "/dev/gps[1-5] with minor numbers [0-4]\n");
  315. fprintf(stderr, "/dev/uio[1-5] with minor numbers 0,2,4,6,8\n");
  316. fprintf(stderr, "/dev/uio[6-10] with minor numbers 1,3,5,7,9\n");
  317. exit(1);
  318. }
  319. int main(int argc, char **argv)
  320. {
  321. int opt;
  322. FILE *table = stdin;
  323. char *rootdir = NULL;
  324. char *line = NULL;
  325. int linenum = 0;
  326. int ret = EXIT_SUCCESS;
  327. bb_applet_name = basename(argv[0]);
  328. while ((opt = getopt(argc, argv, "d:")) != -1) {
  329. switch(opt) {
  330. case 'd':
  331. table = bb_xfopen((line=optarg), "r");
  332. break;
  333. default:
  334. bb_show_usage();
  335. }
  336. }
  337. if (optind >= argc || (rootdir=argv[optind])==NULL) {
  338. bb_error_msg_and_die("root directory not speficied");
  339. }
  340. if (chdir(rootdir) != 0) {
  341. bb_perror_msg_and_die("Couldnt chdir to %s", rootdir);
  342. }
  343. umask(0);
  344. printf("rootdir=%s\n", rootdir);
  345. if (line) {
  346. printf("table='%s'\n", line);
  347. } else {
  348. printf("table=<stdin>\n");
  349. }
  350. while ((line = bb_get_chomped_line_from_file(table))) {
  351. char type;
  352. unsigned int mode = 0755;
  353. unsigned int major = 0;
  354. unsigned int minor = 0;
  355. unsigned int count = 0;
  356. unsigned int increment = 0;
  357. unsigned int start = 0;
  358. char name[41];
  359. char user[41];
  360. char group[41];
  361. char *full_name;
  362. uid_t uid;
  363. gid_t gid;
  364. linenum++;
  365. if ((2 > sscanf(line, "%40s %c %o %40s %40s %u %u %u %u %u", name,
  366. &type, &mode, user, group, &major,
  367. &minor, &start, &increment, &count)) ||
  368. ((major | minor | start | count | increment) > 0xfffff))
  369. {
  370. if (*line=='\0' || *line=='#' || isspace(*line))
  371. continue;
  372. bb_error_msg("line %d invalid: '%s'\n", linenum, line);
  373. ret = EXIT_FAILURE;
  374. continue;
  375. }
  376. if (name[0] == '#') {
  377. continue;
  378. }
  379. if (*group) {
  380. gid = get_ug_id(group, my_getgrnam);
  381. } else {
  382. gid = getgid();
  383. }
  384. if (*user) {
  385. uid = get_ug_id(user, my_getpwnam);
  386. } else {
  387. uid = getuid();
  388. }
  389. full_name = concat_path_file(rootdir, name);
  390. if (type == 'd') {
  391. bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR);
  392. if (chown(full_name, uid, gid) == -1) {
  393. bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
  394. ret = EXIT_FAILURE;
  395. goto loop;
  396. }
  397. if ((mode != -1) && (chmod(full_name, mode) < 0)){
  398. bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
  399. ret = EXIT_FAILURE;
  400. goto loop;
  401. }
  402. } else if (type == 'f') {
  403. struct stat st;
  404. if ((stat(full_name, &st) < 0 || !S_ISREG(st.st_mode))) {
  405. bb_perror_msg("line %d: regular file '%s' does not exist", linenum, full_name);
  406. ret = EXIT_FAILURE;
  407. goto loop;
  408. }
  409. if (chown(full_name, uid, gid) == -1) {
  410. bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
  411. ret = EXIT_FAILURE;
  412. goto loop;
  413. }
  414. if ((mode != -1) && (chmod(full_name, mode) < 0)){
  415. bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
  416. ret = EXIT_FAILURE;
  417. goto loop;
  418. }
  419. } else
  420. {
  421. dev_t rdev;
  422. if (type == 'p') {
  423. mode |= S_IFIFO;
  424. }
  425. else if (type == 'c') {
  426. mode |= S_IFCHR;
  427. }
  428. else if (type == 'b') {
  429. mode |= S_IFBLK;
  430. } else {
  431. bb_error_msg("line %d: Unsupported file type %c", linenum, type);
  432. ret = EXIT_FAILURE;
  433. goto loop;
  434. }
  435. if (count > 0) {
  436. int i;
  437. char *full_name_inc;
  438. full_name_inc = xmalloc(strlen(full_name) + 8);
  439. for (i = 0; i < count; i++) {
  440. sprintf(full_name_inc, "%s%d", full_name, start + i);
  441. rdev = makedev(major, minor + i * increment);
  442. if (mknod(full_name_inc, mode, rdev) == -1) {
  443. bb_perror_msg("line %d: Couldnt create node %s", linenum, full_name_inc);
  444. ret = EXIT_FAILURE;
  445. }
  446. else if (chown(full_name_inc, uid, gid) == -1) {
  447. bb_perror_msg("line %d: chown failed for %s", linenum, full_name_inc);
  448. ret = EXIT_FAILURE;
  449. }
  450. if ((mode != -1) && (chmod(full_name_inc, mode) < 0)){
  451. bb_perror_msg("line %d: chmod failed for %s", linenum, full_name_inc);
  452. ret = EXIT_FAILURE;
  453. }
  454. }
  455. free(full_name_inc);
  456. } else {
  457. rdev = makedev(major, minor);
  458. if (mknod(full_name, mode, rdev) == -1) {
  459. bb_perror_msg("line %d: Couldnt create node %s", linenum, full_name);
  460. ret = EXIT_FAILURE;
  461. }
  462. else if (chown(full_name, uid, gid) == -1) {
  463. bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
  464. ret = EXIT_FAILURE;
  465. }
  466. if ((mode != -1) && (chmod(full_name, mode) < 0)){
  467. bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
  468. ret = EXIT_FAILURE;
  469. }
  470. }
  471. }
  472. loop:
  473. free(line);
  474. free(full_name);
  475. }
  476. fclose(table);
  477. return 0;
  478. }