1 /* vi: set sw=4 ts=4: */
3 * public domain -- Dave 'Kill a Cop' Cinege <dcinege@psychosis.com>
6 * Make ranges of device files quickly.
7 * known bugs: can't deal with alpha ranges
10 //usage:#if ENABLE_FEATURE_MAKEDEVS_LEAF
11 //usage:#define makedevs_trivial_usage
12 //usage: "NAME TYPE MAJOR MINOR FIRST LAST [s]"
13 //usage:#define makedevs_full_usage "\n\n"
14 //usage: "Create a range of block or character special files"
17 //usage: "\n b Block device"
18 //usage: "\n c Character device"
19 //usage: "\n f FIFO, MAJOR and MINOR are ignored"
21 //usage: "\nFIRST..LAST specify numbers appended to NAME."
22 //usage: "\nIf 's' is the last argument, the base device is created as well."
24 //usage: "\nExamples:"
25 //usage: "\n makedevs /dev/ttyS c 4 66 2 63 -> ttyS2-ttyS63"
26 //usage: "\n makedevs /dev/hda b 3 0 0 8 s -> hda,hda1-hda8"
28 //usage:#define makedevs_example_usage
29 //usage: "# makedevs /dev/ttyS c 4 66 2 63\n"
30 //usage: "[creates ttyS2-ttyS63]\n"
31 //usage: "# makedevs /dev/hda b 3 0 0 8 s\n"
32 //usage: "[creates hda,hda1-hda8]\n"
35 //usage:#if ENABLE_FEATURE_MAKEDEVS_TABLE
36 //usage:#define makedevs_trivial_usage
37 //usage: "[-d device_table] rootdir"
38 //usage:#define makedevs_full_usage "\n\n"
39 //usage: "Create a range of special files as specified in a device table.\n"
40 //usage: "Device table entries take the form of:\n"
41 //usage: "<name> <type> <mode> <uid> <gid> <major> <minor> <start> <inc> <count>\n"
42 //usage: "Where name is the file name, type can be one of:\n"
43 //usage: " f Regular file\n"
44 //usage: " d Directory\n"
45 //usage: " c Character device\n"
46 //usage: " b Block device\n"
47 //usage: " p Fifo (named pipe)\n"
48 //usage: "uid is the user id for the target file, gid is the group id for the\n"
49 //usage: "target file. The rest of the entries (major, minor, etc) apply to\n"
50 //usage: "to device special files. A '-' may be used for blank entries."
52 //usage:#define makedevs_example_usage
53 //usage: "For example:\n"
54 //usage: "<name> <type> <mode><uid><gid><major><minor><start><inc><count>\n"
55 //usage: "/dev d 755 0 0 - - - - -\n"
56 //usage: "/dev/console c 666 0 0 5 1 - - -\n"
57 //usage: "/dev/null c 666 0 0 1 3 0 0 -\n"
58 //usage: "/dev/zero c 666 0 0 1 5 0 0 -\n"
59 //usage: "/dev/hda b 640 0 0 3 0 0 0 -\n"
60 //usage: "/dev/hda b 640 0 0 3 1 1 1 15\n\n"
61 //usage: "Will Produce:\n"
63 //usage: "/dev/console\n"
64 //usage: "/dev/null\n"
65 //usage: "/dev/zero\n"
67 //usage: "/dev/hda[0-15]\n"
72 #if ENABLE_FEATURE_MAKEDEVS_LEAF
74 makedevs NAME TYPE MAJOR MINOR FIRST LAST [s]
80 FIRST..LAST specify numbers appended to NAME.
81 If 's' is the last argument, the base device is created as well.
83 makedevs /dev/ttyS c 4 66 2 63 -> ttyS2-ttyS63
84 makedevs /dev/hda b 3 0 0 8 s -> hda,hda1-hda8
86 int makedevs_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
87 int makedevs_main(int argc
, char **argv
)
90 char *basedev
, *type
, *nodname
, *buf
;
91 int Smajor
, Sminor
, S
, E
;
93 if (argc
< 7 || argv
[1][0] == '-')
97 buf
= xasprintf("%s%u", argv
[1], (unsigned)-1);
99 Smajor
= xatoi_positive(argv
[3]);
100 Sminor
= xatoi_positive(argv
[4]);
101 S
= xatoi_positive(argv
[5]);
102 E
= xatoi_positive(argv
[6]);
103 nodname
= argv
[7] ? basedev
: buf
;
121 sprintf(buf
, "%s%u", basedev
, S
);
123 /* if mode != S_IFCHR and != S_IFBLK,
124 * third param in mknod() ignored */
125 if (mknod(nodname
, mode
, makedev(Smajor
, Sminor
)))
126 bb_perror_msg("can't create '%s'", nodname
);
128 /*if (nodname == basedev)*/ /* ex. /dev/hda - to /dev/hda1 ... */
137 #elif ENABLE_FEATURE_MAKEDEVS_TABLE
139 /* Licensed under GPLv2 or later, see file LICENSE in this source tree. */
141 int makedevs_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
142 int makedevs_main(int argc UNUSED_PARAM
, char **argv
)
145 char *line
= (char *)"-";
146 int ret
= EXIT_SUCCESS
;
148 opt_complementary
= "=1"; /* exactly one param */
149 getopt32(argv
, "d:", &line
);
152 xchdir(*argv
); /* ensure root dir exists */
156 printf("rootdir=%s\ntable=", *argv
);
157 if (NOT_LONE_DASH(line
)) {
158 printf("'%s'\n", line
);
163 parser
= config_open(line
);
164 while (config_read(parser
, &line
, 1, 1, "# \t", PARSE_NORMAL
)) {
167 unsigned mode
= 0755;
171 unsigned increment
= 0;
176 char *full_name
= name
;
180 linenum
= parser
->lineno
;
182 if ((2 > sscanf(line
, "%40s %c %o %40s %40s %u %u %u %u %u",
183 name
, &type
, &mode
, user
, group
,
184 &major
, &minor
, &start
, &increment
, &count
))
185 || ((unsigned)(major
| minor
| start
| count
| increment
) > 255)
187 bb_error_msg("invalid line %d: '%s'", linenum
, line
);
192 gid
= (*group
) ? get_ug_id(group
, xgroup2gid
) : getgid();
193 uid
= (*user
) ? get_ug_id(user
, xuname2uid
) : getuid();
194 /* We are already in the right root dir,
195 * so make absolute paths relative */
196 if ('/' == *full_name
)
200 bb_make_directory(full_name
, mode
| S_IFDIR
, FILEUTILS_RECUR
);
201 if (chown(full_name
, uid
, gid
) == -1) {
203 bb_perror_msg("line %d: can't chown %s", linenum
, full_name
);
207 if (chmod(full_name
, mode
) < 0) {
209 bb_perror_msg("line %d: can't chmod %s", linenum
, full_name
);
213 } else if (type
== 'f') {
215 if ((stat(full_name
, &st
) < 0 || !S_ISREG(st
.st_mode
))) {
216 bb_perror_msg("line %d: regular file '%s' does not exist", linenum
, full_name
);
220 if (chown(full_name
, uid
, gid
) < 0)
222 if (chmod(full_name
, mode
) < 0)
231 } else if (type
== 'c') {
233 } else if (type
== 'b') {
236 bb_error_msg("line %d: unsupported file type %c", linenum
, type
);
241 full_name_inc
= xmalloc(strlen(full_name
) + sizeof(int)*3 + 2);
244 for (i
= start
; i
<= start
+ count
; i
++) {
245 sprintf(full_name_inc
, count
? "%s%u" : "%s", full_name
, i
);
246 rdev
= makedev(major
, minor
+ (i
- start
) * increment
);
247 if (mknod(full_name_inc
, mode
, rdev
) < 0) {
248 bb_perror_msg("line %d: can't create node %s", linenum
, full_name_inc
);
250 } else if (chown(full_name_inc
, uid
, gid
) < 0) {
251 bb_perror_msg("line %d: can't chown %s", linenum
, full_name_inc
);
253 } else if (chmod(full_name_inc
, mode
) < 0) {
254 bb_perror_msg("line %d: can't chmod %s", linenum
, full_name_inc
);
261 if (ENABLE_FEATURE_CLEAN_UP
)
262 config_close(parser
);
268 # error makedevs configuration error, either leaf or table must be selected