netsnmp: handle libnl dependency properly
[buildroot-gz.git] / package / makedevs / makedevs.c
blob7092b1475ec4dc847b95308bf8cb05fde80c0a63
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.
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.
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.
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <fcntl.h>
23 #include <getopt.h>
24 #include <time.h>
25 #include <pwd.h>
26 #include <grp.h>
27 #include <unistd.h>
28 #include <ctype.h>
29 #include <errno.h>
30 #include <libgen.h>
31 #include <stdarg.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #ifndef __APPLE__
35 #include <sys/sysmacros.h> /* major() and minor() */
36 #endif
37 #include <ftw.h>
38 #ifdef EXTENDED_ATTRIBUTES
39 #include <sys/capability.h>
40 #endif /* EXTENDED_ATTRIBUTES */
42 const char *bb_applet_name;
43 uid_t recursive_uid;
44 gid_t recursive_gid;
45 unsigned int recursive_mode;
46 #define PASSWD_PATH "etc/passwd" /* MUST be relative */
47 #define GROUP_PATH "etc/group" /* MUST be relative */
49 void bb_verror_msg(const char *s, va_list p)
51 fflush(stdout);
52 fprintf(stderr, "%s: ", bb_applet_name);
53 vfprintf(stderr, s, p);
56 void bb_error_msg(const char *s, ...)
58 va_list p;
60 va_start(p, s);
61 bb_verror_msg(s, p);
62 va_end(p);
63 putc('\n', stderr);
66 void bb_error_msg_and_die(const char *s, ...)
68 va_list p;
70 va_start(p, s);
71 bb_verror_msg(s, p);
72 va_end(p);
73 putc('\n', stderr);
74 exit(1);
77 void bb_vperror_msg(const char *s, va_list p)
79 int err=errno;
80 if(s == 0) s = "";
81 bb_verror_msg(s, p);
82 if (*s) s = ": ";
83 fprintf(stderr, "%s%s\n", s, strerror(err));
86 void bb_perror_msg(const char *s, ...)
88 va_list p;
90 va_start(p, s);
91 bb_vperror_msg(s, p);
92 va_end(p);
95 void bb_perror_msg_and_die(const char *s, ...)
97 va_list p;
99 va_start(p, s);
100 bb_vperror_msg(s, p);
101 va_end(p);
102 exit(1);
105 FILE *bb_xfopen(const char *path, const char *mode)
107 FILE *fp;
108 if ((fp = fopen(path, mode)) == NULL)
109 bb_perror_msg_and_die("%s", path);
110 return fp;
113 enum {
114 FILEUTILS_PRESERVE_STATUS = 1,
115 FILEUTILS_DEREFERENCE = 2,
116 FILEUTILS_RECUR = 4,
117 FILEUTILS_FORCE = 8,
118 FILEUTILS_INTERACTIVE = 16
120 int bb_make_directory (char *path, long mode, int flags)
122 mode_t mask;
123 const char *fail_msg;
124 char *s = path;
125 char c;
126 struct stat st;
128 mask = umask(0);
129 if (mode == -1) {
130 umask(mask);
131 mode = (S_IXUSR | S_IXGRP | S_IXOTH |
132 S_IWUSR | S_IWGRP | S_IWOTH |
133 S_IRUSR | S_IRGRP | S_IROTH) & ~mask;
134 } else {
135 umask(mask & ~0300);
138 do {
139 c = 0;
141 if (flags & FILEUTILS_RECUR) { /* Get the parent. */
142 /* Bypass leading non-'/'s and then subsequent '/'s. */
143 while (*s) {
144 if (*s == '/') {
145 do {
146 ++s;
147 } while (*s == '/');
148 c = *s; /* Save the current char */
149 *s = 0; /* and replace it with nul. */
150 break;
152 ++s;
156 if (mkdir(path, 0777) < 0) {
157 /* If we failed for any other reason than the directory
158 * already exists, output a diagnostic and return -1.*/
159 if ((errno != EEXIST && errno != EISDIR)
160 || !(flags & FILEUTILS_RECUR)
161 || (stat(path, &st) < 0 || !S_ISDIR(st.st_mode))) {
162 fail_msg = "create";
163 umask(mask);
164 break;
166 /* Since the directory exists, don't attempt to change
167 * permissions if it was the full target. Note that
168 * this is not an error conditon. */
169 if (!c) {
170 umask(mask);
171 return 0;
175 if (!c) {
176 /* Done. If necessary, updated perms on the newly
177 * created directory. Failure to update here _is_
178 * an error.*/
179 umask(mask);
180 if ((mode != -1) && (chmod(path, mode) < 0)){
181 fail_msg = "set permissions of";
182 break;
184 return 0;
187 /* Remove any inserted nul from the path (recursive mode). */
188 *s = c;
190 } while (1);
192 bb_perror_msg ("Cannot %s directory `%s'", fail_msg, path);
193 return -1;
196 const char * const bb_msg_memory_exhausted = "memory exhausted";
198 void *xmalloc(size_t size)
200 void *ptr = malloc(size);
201 if (ptr == NULL && size != 0)
202 bb_error_msg_and_die(bb_msg_memory_exhausted);
203 return ptr;
206 void *xcalloc(size_t nmemb, size_t size)
208 void *ptr = calloc(nmemb, size);
209 if (ptr == NULL && nmemb != 0 && size != 0)
210 bb_error_msg_and_die(bb_msg_memory_exhausted);
211 return ptr;
214 void *xrealloc(void *ptr, size_t size)
216 ptr = realloc(ptr, size);
217 if (ptr == NULL && size != 0)
218 bb_error_msg_and_die(bb_msg_memory_exhausted);
219 return ptr;
222 char *private_get_line_from_file(FILE *file, int c)
224 #define GROWBY (80) /* how large we will grow strings by */
226 int ch;
227 int idx = 0;
228 char *linebuf = NULL;
229 int linebufsz = 0;
231 while ((ch = getc(file)) != EOF) {
232 /* grow the line buffer as necessary */
233 if (idx > linebufsz - 2) {
234 linebuf = xrealloc(linebuf, linebufsz += GROWBY);
236 linebuf[idx++] = (char)ch;
237 if (!ch) return linebuf;
238 if (c<2 && ch == '\n') {
239 if (c) {
240 --idx;
242 break;
245 if (linebuf) {
246 if (ferror(file)) {
247 free(linebuf);
248 return NULL;
250 linebuf[idx] = 0;
252 return linebuf;
255 char *bb_get_chomped_line_from_file(FILE *file)
257 return private_get_line_from_file(file, 1);
260 long my_getpwnam(const char *name)
262 struct passwd *myuser;
263 FILE *stream;
265 stream = bb_xfopen(PASSWD_PATH, "r");
266 while(1) {
267 errno = 0;
268 myuser = fgetpwent(stream);
269 if (myuser == NULL)
270 bb_error_msg_and_die("unknown user name: %s", name);
271 if (errno)
272 bb_perror_msg_and_die("fgetpwent");
273 if (!strcmp(name, myuser->pw_name))
274 break;
276 fclose(stream);
278 return myuser->pw_uid;
281 long my_getgrnam(const char *name)
283 struct group *mygroup;
284 FILE *stream;
286 stream = bb_xfopen(GROUP_PATH, "r");
287 while(1) {
288 errno = 0;
289 mygroup = fgetgrent(stream);
290 if (mygroup == NULL)
291 bb_error_msg_and_die("unknown group name: %s", name);
292 if (errno)
293 bb_perror_msg_and_die("fgetgrent");
294 if (!strcmp(name, mygroup->gr_name))
295 break;
297 fclose(stream);
299 return mygroup->gr_gid;
302 unsigned long get_ug_id(const char *s, long (*my_getxxnam)(const char *))
304 unsigned long r;
305 char *p;
307 r = strtoul(s, &p, 10);
308 if (*p || (s == p)) {
309 r = my_getxxnam(s);
312 return r;
315 char * last_char_is(const char *s, int c)
317 char *sret = (char *)s;
318 if (sret) {
319 sret = strrchr(sret, c);
320 if(sret != NULL && *(sret+1) != 0)
321 sret = NULL;
323 return sret;
326 void bb_xasprintf(char **string_ptr, const char *format, ...)
328 va_list p;
329 int r;
331 va_start(p, format);
332 r = vasprintf(string_ptr, format, p);
333 va_end(p);
335 if (r < 0) {
336 bb_perror_msg_and_die("bb_xasprintf");
340 char *concat_path_file(const char *path, const char *filename)
342 char *outbuf;
343 char *lc;
345 if (!path)
346 path = "";
347 lc = last_char_is(path, '/');
348 while (*filename == '/')
349 filename++;
350 bb_xasprintf(&outbuf, "%s%s%s", path, (lc==NULL ? "/" : ""), filename);
352 return outbuf;
355 #ifdef EXTENDED_ATTRIBUTES
356 int bb_set_xattr(const char *fpath, const char *xattr)
358 cap_t cap, cap_file, cap_new;
359 char *cap_file_text, *cap_new_text;
360 ssize_t length;
362 cap = cap_from_text(xattr);
363 if (cap == NULL)
364 bb_perror_msg_and_die("cap_from_text failed for %s", xattr);
366 cap_file = cap_get_file(fpath);
367 if (cap_file == NULL) {
368 /* if no capability was set before, we initialize cap_file */
369 if (errno != ENODATA)
370 bb_perror_msg_and_die("cap_get_file failed on %s", fpath);
372 cap_file = cap_init();
373 if (!cap_file)
374 bb_perror_msg_and_die("cap_init failed");
377 if ((cap_file_text = cap_to_text(cap_file, &length)) == NULL)
378 bb_perror_msg_and_die("cap_to_name failed on %s", fpath);
380 bb_xasprintf(&cap_new_text, "%s %s", cap_file_text, xattr);
382 if ((cap_new = cap_from_text(cap_new_text)) == NULL)
383 bb_perror_msg_and_die("cap_from_text failed on %s", cap_new_text);
385 if (cap_set_file(fpath, cap_new) == -1)
386 bb_perror_msg_and_die("cap_set_file failed for %s (xattr = %s)", fpath, xattr);
388 cap_free(cap);
389 cap_free(cap_file);
390 cap_free(cap_file_text);
391 cap_free(cap_new);
392 cap_free(cap_new_text);
394 return 0;
396 #endif /* EXTENDED_ATTRIBUTES */
398 void bb_show_usage(void)
400 fprintf(stderr, "%s: [-d device_table] rootdir\n\n", bb_applet_name);
401 fprintf(stderr, "Creates a batch of special files as specified in a device table.\n");
402 fprintf(stderr, "Device table entries take the form of:\n");
403 fprintf(stderr, "name type mode user group major minor start increment count\n\n");
404 fprintf(stderr, "Where name is the file name, type can be one of:\n");
405 fprintf(stderr, " f A regular file\n");
406 fprintf(stderr, " d Directory\n");
407 fprintf(stderr, " r Directory recursively\n");
408 fprintf(stderr, " c Character special device file\n");
409 fprintf(stderr, " b Block special device file\n");
410 fprintf(stderr, " p Fifo (named pipe)\n");
411 fprintf(stderr, "uid is the user id for the target file, gid is the group id for the\n");
412 fprintf(stderr, "target file. The rest of the entries (major, minor, etc) apply to\n");
413 fprintf(stderr, "to device special files. A '-' may be used for blank entries.\n\n");
414 fprintf(stderr, "For example:\n");
415 fprintf(stderr, "<name> <type> <mode> <uid> <gid> <major> <minor> <start> <inc> <count>\n");
416 fprintf(stderr, "/dev d 755 0 0 - - - - -\n");
417 fprintf(stderr, "/dev/console c 666 0 0 5 1 - - -\n");
418 fprintf(stderr, "/dev/null c 666 0 0 1 3 0 0 -\n");
419 fprintf(stderr, "/dev/zero c 666 0 0 1 5 0 0 -\n");
420 fprintf(stderr, "/dev/hda b 640 0 0 3 0 0 0 -\n");
421 fprintf(stderr, "/dev/hda b 640 0 0 3 1 1 1 15\n");
422 fprintf(stderr, "/dev/rtp b 640 0 0 250 0 0 1 5\n");
423 fprintf(stderr, "/dev/gps b 640 0 0 251 0 1 1 5\n");
424 fprintf(stderr, "/dev/uio b 640 0 0 252 0 1 2 5\n");
425 fprintf(stderr, "/dev/uio b 640 0 0 252 1 6 2 5\n\n");
426 fprintf(stderr, "Will Produce:\n");
427 fprintf(stderr, "/dev\n");
428 fprintf(stderr, "/dev/console\n");
429 fprintf(stderr, "/dev/null\n");
430 fprintf(stderr, "/dev/zero\n");
431 fprintf(stderr, "/dev/hda\n");
432 fprintf(stderr, "/dev/hda[1-15] with minor numbers [1-15]\n");
433 fprintf(stderr, "/dev/rtp[0-4] with minor numbers [0-4]\n");
434 fprintf(stderr, "/dev/gps[1-5] with minor numbers [0-4]\n");
435 fprintf(stderr, "/dev/uio[1-5] with minor numbers 0,2,4,6,8\n");
436 fprintf(stderr, "/dev/uio[6-10] with minor numbers 1,3,5,7,9\n");
437 exit(1);
440 int bb_recursive(const char *fpath, const struct stat *sb,
441 int tflag, struct FTW *ftwbuf){
443 if (chown(fpath, recursive_uid, recursive_gid) == -1) {
444 bb_perror_msg("chown failed for %s", fpath);
445 return -1;
447 if (recursive_mode != -1) {
448 if (chmod(fpath, recursive_mode) < 0) {
449 bb_perror_msg("chmod failed for %s", fpath);
450 return -1;
454 return 0;
457 int main(int argc, char **argv)
459 int opt;
460 FILE *table = stdin;
461 char *rootdir = NULL;
462 char *full_name = NULL;
463 char *line = NULL;
464 int linenum = 0;
465 int ret = EXIT_SUCCESS;
467 bb_applet_name = basename(argv[0]);
469 while ((opt = getopt(argc, argv, "d:")) != -1) {
470 switch(opt) {
471 case 'd':
472 table = bb_xfopen((line=optarg), "r");
473 break;
474 default:
475 bb_show_usage();
479 if (optind >= argc || (rootdir=argv[optind])==NULL) {
480 bb_error_msg_and_die("root directory not speficied");
483 if (chdir(rootdir) != 0) {
484 bb_perror_msg_and_die("Couldnt chdir to %s", rootdir);
487 umask(0);
489 printf("rootdir=%s\n", rootdir);
490 if (line) {
491 printf("table='%s'\n", line);
492 } else {
493 printf("table=<stdin>\n");
496 while ((line = bb_get_chomped_line_from_file(table))) {
497 char type;
498 unsigned int mode = 0755;
499 unsigned int major = 0;
500 unsigned int minor = 0;
501 unsigned int count = 0;
502 unsigned int increment = 0;
503 unsigned int start = 0;
504 char xattr[255];
505 char name[4096];
506 char user[41];
507 char group[41];
508 uid_t uid;
509 gid_t gid;
511 linenum++;
513 if (1 == sscanf(line, "|xattr %254s", xattr)) {
514 #ifdef EXTENDED_ATTRIBUTES
515 if (!full_name)
516 bb_error_msg_and_die("line %d should be after a file\n", linenum);
518 if (bb_set_xattr(full_name, xattr) < 0)
519 bb_error_msg_and_die("can't set cap %s on file %s\n", xattr, full_name);
520 #else
521 bb_error_msg_and_die("line %d not supported: '%s'\nDid you forget to enable "
522 "BR2_ROOTFS_DEVICE_TABLE_SUPPORTS_EXTENDED_ATTRIBUTES?\n",
523 linenum, line);
524 #endif /* EXTENDED_ATTRIBUTES */
525 continue;
528 if ((2 > sscanf(line, "%4095s %c %o %40s %40s %u %u %u %u %u", name,
529 &type, &mode, user, group, &major,
530 &minor, &start, &increment, &count)) ||
531 ((major | minor | start | count | increment) > 0xfffff))
533 if (*line=='\0' || *line=='#' || isspace(*line))
534 continue;
535 bb_error_msg("line %d invalid: '%s'\n", linenum, line);
536 ret = EXIT_FAILURE;
537 continue;
539 if (name[0] == '#') {
540 continue;
542 if (*group) {
543 gid = get_ug_id(group, my_getgrnam);
544 } else {
545 gid = getgid();
547 if (*user) {
548 uid = get_ug_id(user, my_getpwnam);
549 } else {
550 uid = getuid();
554 * free previous full name
555 * we don't de-allocate full_name at the end of the parsing,
556 * because we may need it if the next line is an xattr.
558 free(full_name);
559 full_name = concat_path_file(rootdir, name);
561 if (type == 'd') {
562 bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR);
563 if (chown(full_name, uid, gid) == -1) {
564 bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
565 ret = EXIT_FAILURE;
566 goto loop;
568 if ((mode != -1) && (chmod(full_name, mode) < 0)){
569 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
570 ret = EXIT_FAILURE;
571 goto loop;
573 } else if (type == 'f') {
574 struct stat st;
575 if ((stat(full_name, &st) < 0 || !S_ISREG(st.st_mode))) {
576 bb_perror_msg("line %d: regular file '%s' does not exist", linenum, full_name);
577 ret = EXIT_FAILURE;
578 goto loop;
580 if (chown(full_name, uid, gid) == -1) {
581 bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
582 ret = EXIT_FAILURE;
583 goto loop;
585 if ((mode != -1) && (chmod(full_name, mode) < 0)){
586 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
587 ret = EXIT_FAILURE;
588 goto loop;
590 } else if (type == 'r') {
591 recursive_uid = uid;
592 recursive_gid = gid;
593 recursive_mode = mode;
594 if (nftw(full_name, bb_recursive, 20, FTW_MOUNT | FTW_PHYS) < 0) {
595 bb_perror_msg("line %d: recursive failed for %s", linenum, full_name);
596 ret = EXIT_FAILURE;
597 goto loop;
599 } else
601 dev_t rdev;
602 unsigned i;
603 char *full_name_inc;
605 if (type == 'p') {
606 mode |= S_IFIFO;
608 else if (type == 'c') {
609 mode |= S_IFCHR;
611 else if (type == 'b') {
612 mode |= S_IFBLK;
613 } else {
614 bb_error_msg("line %d: Unsupported file type %c", linenum, type);
615 ret = EXIT_FAILURE;
616 goto loop;
619 full_name_inc = xmalloc(strlen(full_name) + sizeof(int)*3 + 2);
620 if (count)
621 count--;
622 for (i = start; i <= start + count; i++) {
623 sprintf(full_name_inc, count ? "%s%u" : "%s", full_name, i);
624 rdev = makedev(major, minor + (i - start) * increment);
625 if (mknod(full_name_inc, mode, rdev) < 0) {
626 bb_perror_msg("line %d: can't create node %s", linenum, full_name_inc);
627 ret = EXIT_FAILURE;
628 } else if (chown(full_name_inc, uid, gid) < 0) {
629 bb_perror_msg("line %d: can't chown %s", linenum, full_name_inc);
630 ret = EXIT_FAILURE;
631 } else if (chmod(full_name_inc, mode) < 0) {
632 bb_perror_msg("line %d: can't chmod %s", linenum, full_name_inc);
633 ret = EXIT_FAILURE;
636 free(full_name_inc);
638 loop:
639 free(line);
641 fclose(table);
643 return ret;