makedevs.c 14 KB

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