annotate toys/other/makedevs.c @ 1736:5892daac85ab draft

Switch nsenter to default y.
author Rob Landley <rob@landley.net>
date Thu, 12 Mar 2015 15:34:03 -0500
parents 9adb0ccb617e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1325
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
1 /* makedevs.c - Make ranges of device files.
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
2 *
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
3 * Copyright 2014 Bilal Qureshi <bilal.jmi@gmail.com>
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
4 * Copyright 2014 Kyungwan Han <asura321@gmail.com>
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
5 *
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
6 * No Standard
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
7
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
8 USE_MAKEDEVS(NEWTOY(makedevs, "<1>1d:", TOYFLAG_USR|TOYFLAG_BIN))
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
9
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
10 config MAKEDEVS
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
11 bool "makedevs"
1373
a3ee4f91462a Promote makedevs to other.
Rob Landley <rob@landley.net>
parents: 1372
diff changeset
12 default y
1325
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
13 help
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
14 usage: makedevs [-d device_table] rootdir
1362
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
15
1325
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
16 Create a range of special files as specified in a device table.
1362
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
17
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
18 -d file containing device table (default reads from stdin)
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
19
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
20 Each line of of the device table has the fields:
1325
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
21 <name> <type> <mode> <uid> <gid> <major> <minor> <start> <increment> <count>
1362
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
22 Where name is the file name, and type is one of the following:
1325
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
23
1362
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
24 b Block device
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
25 c Character device
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
26 d Directory
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
27 f Regular file
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
28 p Named pipe (fifo)
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
29
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
30 Other fields specify permissions, user and group id owning the file,
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
31 and additional fields for device special files. Use '-' for blank entries,
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
32 unspecified fields are treated as '-'.
1325
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
33 */
1362
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
34
1325
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
35 #define FOR_makedevs
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
36 #include "toys.h"
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
37
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
38 GLOBALS(
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
39 char *fname;
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
40 )
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
41
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
42 void makedevs_main()
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
43 {
1732
9adb0ccb617e Make find accept numeric uid/gid, and simplify makedevs using the new infrastructure.
Rob Landley <rob@landley.net>
parents: 1373
diff changeset
44 int fd = 0, line_no, i;
1325
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
45 char *line = NULL;
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
46
1362
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
47 // Open file and chdir, verbosely
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
48 xprintf("rootdir = %s\n", *toys.optargs);
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
49 if (toys.optflags & FLAG_d && strcmp(TT.fname, "-")) {
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
50 fd = xopen(TT.fname, O_RDONLY);
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
51 xprintf("table = %s\n", TT.fname);
1372
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
52 } else xprintf("table = <stdin>\n");
1362
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
53 xchdir(*toys.optargs);
1325
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
54
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
55 for (line_no = 0; (line = get_line(fd)); free(line)) {
1372
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
56 char type=0, user[64], group[64], *node, *ptr = line;
1325
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
57 unsigned int mode = 0755, major = 0, minor = 0, cnt = 0, incr = 0,
1362
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
58 st_val = 0;
1325
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
59 uid_t uid;
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
60 gid_t gid;
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
61 struct stat st;
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
62
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
63 line_no++;
1362
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
64 while (isspace(*ptr)) ptr++;
1325
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
65 if (!*ptr || *ptr == '#') continue;
1372
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
66 node = ptr;
1362
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
67
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
68 while (*ptr && !isspace(*ptr)) ptr++;
1372
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
69 if (*ptr) *(ptr++) = 0;
1362
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
70 *user = *group = 0;
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
71 sscanf(ptr, "%c %o %63s %63s %u %u %u %u %u", &type, &mode,
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
72 user, group, &major, &minor, &st_val, &incr, &cnt);
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
73
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
74 // type order here needs to line up with actions[] order.
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
75 i = stridx("pcbdf", type);
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
76 if (i == -1) {
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
77 error_msg("line %d: bad type %c", line_no, type);
1325
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
78 continue;
1362
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
79 } else mode |= (mode_t[]){S_IFIFO, S_IFCHR, S_IFBLK, 0, 0}[i];
1325
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
80
1732
9adb0ccb617e Make find accept numeric uid/gid, and simplify makedevs using the new infrastructure.
Rob Landley <rob@landley.net>
parents: 1373
diff changeset
81 uid = *user ? xgetpwnamid(user)->pw_uid : getuid();
9adb0ccb617e Make find accept numeric uid/gid, and simplify makedevs using the new infrastructure.
Rob Landley <rob@landley.net>
parents: 1373
diff changeset
82 gid = *group ? xgetgrnamid(group)->gr_gid : getgid();
1325
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
83
1362
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
84 while (*node == '/') node++; // using relative path
1372
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
85
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
86 for (i = 0; (!cnt && !i) || i < cnt; i++) {
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
87 if (cnt) {
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
88 snprintf(toybuf, sizeof(toybuf), "%s%u", node, st_val + i);
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
89 ptr = toybuf;
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
90 } else ptr = node;
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
91
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
92 if (type == 'd') {
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
93 if (mkpathat(AT_FDCWD, ptr, mode, 3)) {
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
94 perror_msg("can't create directory '%s'", ptr);
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
95 continue;
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
96 }
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
97 } else if (type == 'f') {
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
98 if (stat(ptr, &st) || !S_ISREG(st.st_mode)) {
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
99 perror_msg("line %d: file '%s' does not exist", line_no, ptr);
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
100 continue;
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
101 }
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
102 } else if (mknod(ptr, mode, makedev(major, minor + i*incr))) {
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
103 perror_msg("line %d: can't create node '%s'", line_no, ptr);
1362
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
104 continue;
b9605ebd3af4 Cleanup pass on makedevs. Need to test it before promoting.
Rob Landley <rob@landley.net>
parents: 1325
diff changeset
105 }
1372
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
106
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
107 if (chown(ptr, uid, gid) || chmod(ptr, mode))
d46baa9adb5f Bugfixes for cleaned up makedevs.
Rob Landley <rob@landley.net>
parents: 1362
diff changeset
108 perror_msg("line %d: can't chown/chmod '%s'", line_no, ptr);
1325
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
109 }
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
110 }
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
111 xclose(fd);
41fc44c76ade makedevs - making devices/nodes in a range. Supports reading the tabled entry from file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
112 }