1 /* vi: set sw=4 ts=4: */
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.
33 #include <sys/types.h>
35 #include <sys/sysmacros.h> /* major() and minor() */
39 const char *bb_applet_name;
42 unsigned int recursive_mode;
44 void bb_verror_msg(const char *s, va_list p)
47 fprintf(stderr, "%s: ", bb_applet_name);
48 vfprintf(stderr, s, p);
51 void bb_error_msg(const char *s, ...)
61 void bb_error_msg_and_die(const char *s, ...)
72 void bb_vperror_msg(const char *s, va_list p)
78 fprintf(stderr, "%s%s\n", s, strerror(err));
81 void bb_perror_msg(const char *s, ...)
90 void bb_perror_msg_and_die(const char *s, ...)
100 FILE *bb_xfopen(const char *path, const char *mode)
103 if ((fp = fopen(path, mode)) == NULL)
104 bb_perror_msg_and_die("%s", path);
109 FILEUTILS_PRESERVE_STATUS = 1,
110 FILEUTILS_DEREFERENCE = 2,
113 FILEUTILS_INTERACTIVE = 16
115 int bb_make_directory (char *path, long mode, int flags)
118 const char *fail_msg;
126 mode = (S_IXUSR | S_IXGRP | S_IXOTH |
127 S_IWUSR | S_IWGRP | S_IWOTH |
128 S_IRUSR | S_IRGRP | S_IROTH) & ~mask;
136 if (flags & FILEUTILS_RECUR) { /* Get the parent. */
137 /* Bypass leading non-'/'s and then subsequent '/'s. */
143 c = *s; /* Save the current char */
144 *s = 0; /* and replace it with nul. */
151 if (mkdir(path, 0777) < 0) {
152 /* If we failed for any other reason than the directory
153 * already exists, output a diagnostic and return -1.*/
154 if ((errno != EEXIST && errno != EISDIR)
155 || !(flags & FILEUTILS_RECUR)
156 || (stat(path, &st) < 0 || !S_ISDIR(st.st_mode))) {
161 /* Since the directory exists, don't attempt to change
162 * permissions if it was the full target. Note that
163 * this is not an error conditon. */
171 /* Done. If necessary, updated perms on the newly
172 * created directory. Failure to update here _is_
175 if ((mode != -1) && (chmod(path, mode) < 0)){
176 fail_msg = "set permissions of";
182 /* Remove any inserted nul from the path (recursive mode). */
187 bb_perror_msg ("Cannot %s directory `%s'", fail_msg, path);
191 const char * const bb_msg_memory_exhausted = "memory exhausted";
193 void *xmalloc(size_t size)
195 void *ptr = malloc(size);
196 if (ptr == NULL && size != 0)
197 bb_error_msg_and_die(bb_msg_memory_exhausted);
201 void *xcalloc(size_t nmemb, size_t size)
203 void *ptr = calloc(nmemb, size);
204 if (ptr == NULL && nmemb != 0 && size != 0)
205 bb_error_msg_and_die(bb_msg_memory_exhausted);
209 void *xrealloc(void *ptr, size_t size)
211 ptr = realloc(ptr, size);
212 if (ptr == NULL && size != 0)
213 bb_error_msg_and_die(bb_msg_memory_exhausted);
217 char *private_get_line_from_file(FILE *file, int c)
219 #define GROWBY (80) /* how large we will grow strings by */
223 char *linebuf = NULL;
226 while ((ch = getc(file)) != EOF) {
227 /* grow the line buffer as necessary */
228 if (idx > linebufsz - 2) {
229 linebuf = xrealloc(linebuf, linebufsz += GROWBY);
231 linebuf[idx++] = (char)ch;
232 if (!ch) return linebuf;
233 if (c<2 && ch == '\n') {
250 char *bb_get_chomped_line_from_file(FILE *file)
252 return private_get_line_from_file(file, 1);
255 long my_getpwnam(const char *name)
257 struct passwd *myuser;
259 myuser = getpwnam(name);
261 bb_error_msg_and_die("unknown user name: %s", name);
263 return myuser->pw_uid;
266 long my_getgrnam(const char *name)
268 struct group *mygroup;
270 mygroup = getgrnam(name);
272 bb_error_msg_and_die("unknown group name: %s", name);
274 return (mygroup->gr_gid);
277 unsigned long get_ug_id(const char *s, long (*my_getxxnam)(const char *))
282 r = strtoul(s, &p, 10);
283 if (*p || (s == p)) {
290 char * last_char_is(const char *s, int c)
292 char *sret = (char *)s;
294 sret = strrchr(sret, c);
295 if(sret != NULL && *(sret+1) != 0)
301 void bb_xasprintf(char **string_ptr, const char *format, ...)
307 r = vasprintf(string_ptr, format, p);
311 bb_perror_msg_and_die("bb_xasprintf");
315 char *concat_path_file(const char *path, const char *filename)
322 lc = last_char_is(path, '/');
323 while (*filename == '/')
325 bb_xasprintf(&outbuf, "%s%s%s", path, (lc==NULL ? "/" : ""), filename);
330 void bb_show_usage(void)
332 fprintf(stderr, "%s: [-d device_table] rootdir\n\n", bb_applet_name);
333 fprintf(stderr, "Creates a batch of special files as specified in a device table.\n");
334 fprintf(stderr, "Device table entries take the form of:\n");
335 fprintf(stderr, "name type mode user group major minor start increment count\n\n");
336 fprintf(stderr, "Where name is the file name, type can be one of:\n");
337 fprintf(stderr, " f A regular file\n");
338 fprintf(stderr, " d Directory\n");
339 fprintf(stderr, " r Directory recursively\n");
340 fprintf(stderr, " c Character special device file\n");
341 fprintf(stderr, " b Block special device file\n");
342 fprintf(stderr, " p Fifo (named pipe)\n");
343 fprintf(stderr, "uid is the user id for the target file, gid is the group id for the\n");
344 fprintf(stderr, "target file. The rest of the entries (major, minor, etc) apply to\n");
345 fprintf(stderr, "to device special files. A '-' may be used for blank entries.\n\n");
346 fprintf(stderr, "For example:\n");
347 fprintf(stderr, "<name> <type> <mode> <uid> <gid> <major> <minor> <start> <inc> <count>\n");
348 fprintf(stderr, "/dev d 755 0 0 - - - - -\n");
349 fprintf(stderr, "/dev/console c 666 0 0 5 1 - - -\n");
350 fprintf(stderr, "/dev/null c 666 0 0 1 3 0 0 -\n");
351 fprintf(stderr, "/dev/zero c 666 0 0 1 5 0 0 -\n");
352 fprintf(stderr, "/dev/hda b 640 0 0 3 0 0 0 -\n");
353 fprintf(stderr, "/dev/hda b 640 0 0 3 1 1 1 15\n");
354 fprintf(stderr, "/dev/rtp b 640 0 0 250 0 0 1 5\n");
355 fprintf(stderr, "/dev/gps b 640 0 0 251 0 1 1 5\n");
356 fprintf(stderr, "/dev/uio b 640 0 0 252 0 1 2 5\n");
357 fprintf(stderr, "/dev/uio b 640 0 0 252 1 6 2 5\n\n");
358 fprintf(stderr, "Will Produce:\n");
359 fprintf(stderr, "/dev\n");
360 fprintf(stderr, "/dev/console\n");
361 fprintf(stderr, "/dev/null\n");
362 fprintf(stderr, "/dev/zero\n");
363 fprintf(stderr, "/dev/hda\n");
364 fprintf(stderr, "/dev/hda[1-15] with minor numbers [1-15]\n");
365 fprintf(stderr, "/dev/rtp[0-4] with minor numbers [0-4]\n");
366 fprintf(stderr, "/dev/gps[1-5] with minor numbers [0-4]\n");
367 fprintf(stderr, "/dev/uio[1-5] with minor numbers 0,2,4,6,8\n");
368 fprintf(stderr, "/dev/uio[6-10] with minor numbers 1,3,5,7,9\n");
372 int bb_recursive(const char *fpath, const struct stat *sb,
373 int tflag, struct FTW *ftwbuf){
375 if (chown(fpath, recursive_uid, recursive_gid) == -1) {
376 bb_perror_msg("chown failed for %s", fpath);
379 if (recursive_mode != -1) {
380 if (chmod(fpath, recursive_mode) < 0) {
381 bb_perror_msg("chmod failed for %s", fpath);
389 int main(int argc, char **argv)
393 char *rootdir = NULL;
396 int ret = EXIT_SUCCESS;
398 bb_applet_name = basename(argv[0]);
400 while ((opt = getopt(argc, argv, "d:")) != -1) {
403 table = bb_xfopen((line=optarg), "r");
410 if (optind >= argc || (rootdir=argv[optind])==NULL) {
411 bb_error_msg_and_die("root directory not speficied");
414 if (chdir(rootdir) != 0) {
415 bb_perror_msg_and_die("Couldnt chdir to %s", rootdir);
420 printf("rootdir=%s\n", rootdir);
422 printf("table='%s'\n", line);
424 printf("table=<stdin>\n");
427 while ((line = bb_get_chomped_line_from_file(table))) {
429 unsigned int mode = 0755;
430 unsigned int major = 0;
431 unsigned int minor = 0;
432 unsigned int count = 0;
433 unsigned int increment = 0;
434 unsigned int start = 0;
444 if ((2 > sscanf(line, "%4095s %c %o %40s %40s %u %u %u %u %u", name,
445 &type, &mode, user, group, &major,
446 &minor, &start, &increment, &count)) ||
447 ((major | minor | start | count | increment) > 0xfffff))
449 if (*line=='\0' || *line=='#' || isspace(*line))
451 bb_error_msg("line %d invalid: '%s'\n", linenum, line);
455 if (name[0] == '#') {
459 gid = get_ug_id(group, my_getgrnam);
464 uid = get_ug_id(user, my_getpwnam);
468 full_name = concat_path_file(rootdir, name);
471 bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR);
472 if (chown(full_name, uid, gid) == -1) {
473 bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
477 if ((mode != -1) && (chmod(full_name, mode) < 0)){
478 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
482 } else if (type == 'f') {
484 if ((stat(full_name, &st) < 0 || !S_ISREG(st.st_mode))) {
485 bb_perror_msg("line %d: regular file '%s' does not exist", linenum, full_name);
489 if (chown(full_name, uid, gid) == -1) {
490 bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
494 if ((mode != -1) && (chmod(full_name, mode) < 0)){
495 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
499 } else if (type == 'r') {
502 recursive_mode = mode;
503 if (nftw(full_name, bb_recursive, 20, FTW_MOUNT | FTW_PHYS) < 0) {
504 bb_perror_msg("line %d: recursive failed for %s", linenum, full_name);
515 else if (type == 'c') {
518 else if (type == 'b') {
521 bb_error_msg("line %d: Unsupported file type %c", linenum, type);
530 full_name_inc = xmalloc(strlen(full_name) + 8);
531 for (i = 0; i < count; i++) {
532 sprintf(full_name_inc, "%s%d", full_name, start + i);
533 rdev = makedev(major, minor + i * increment);
534 if (mknod(full_name_inc, mode, rdev) == -1) {
535 bb_perror_msg("line %d: Couldnt create node %s", linenum, full_name_inc);
538 else if (chown(full_name_inc, uid, gid) == -1) {
539 bb_perror_msg("line %d: chown failed for %s", linenum, full_name_inc);
542 if ((mode != -1) && (chmod(full_name_inc, mode) < 0)){
543 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name_inc);
549 rdev = makedev(major, minor);
550 if (mknod(full_name, mode, rdev) == -1) {
551 bb_perror_msg("line %d: Couldnt create node %s", linenum, full_name);
554 else if (chown(full_name, uid, gid) == -1) {
555 bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
558 if ((mode != -1) && (chmod(full_name, mode) < 0)){
559 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);