makedevs.c 16 KB

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