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
9 //config:config MAKEDEVS
10 //config: bool "makedevs"
13 //config: 'makedevs' is a utility used to create a batch of devices with
14 //config: one command.
16 //config: There are two choices for command line behaviour, the interface
17 //config: as used by LEAF/Linux Router Project, or a device table file.
19 //config: 'leaf' is traditionally what busybox follows, it allows multiple
20 //config: devices of a particluar type to be created per command.
21 //config: e.g. /dev/hda[0-9]
22 //config: Device properties are passed as command line arguments.
24 //config: 'table' reads device properties from a file or stdin, allowing
25 //config: a batch of unrelated devices to be made with one command.
26 //config: User/group names are allowed as an alternative to uid/gid.
29 //config: prompt "Choose makedevs behaviour"
30 //config: depends on MAKEDEVS
31 //config: default FEATURE_MAKEDEVS_TABLE
33 //config:config FEATURE_MAKEDEVS_LEAF
36 //config:config FEATURE_MAKEDEVS_TABLE
37 //config: bool "table"
41 //usage:#if ENABLE_FEATURE_MAKEDEVS_LEAF
42 //usage:#define makedevs_trivial_usage
43 //usage: "NAME TYPE MAJOR MINOR FIRST LAST [s]"
44 //usage:#define makedevs_full_usage "\n\n"
45 //usage: "Create a range of block or character special files"
48 //usage: "\n b Block device"
49 //usage: "\n c Character device"
50 //usage: "\n f FIFO, MAJOR and MINOR are ignored"
52 //usage: "\nFIRST..LAST specify numbers appended to NAME."
53 //usage: "\nIf 's' is the last argument, the base device is created as well."
55 //usage: "\nExamples:"
56 //usage: "\n makedevs /dev/ttyS c 4 66 2 63 -> ttyS2-ttyS63"
57 //usage: "\n makedevs /dev/hda b 3 0 0 8 s -> hda,hda1-hda8"
59 //usage:#define makedevs_example_usage
60 //usage: "# makedevs /dev/ttyS c 4 66 2 63\n"
61 //usage: "[creates ttyS2-ttyS63]\n"
62 //usage: "# makedevs /dev/hda b 3 0 0 8 s\n"
63 //usage: "[creates hda,hda1-hda8]\n"
66 //usage:#if ENABLE_FEATURE_MAKEDEVS_TABLE
67 //usage:#define makedevs_trivial_usage
68 //usage: "[-d device_table] rootdir"
69 //usage:#define makedevs_full_usage "\n\n"
70 //usage: "Create a range of special files as specified in a device table.\n"
71 //usage: "Device table entries take the form of:\n"
72 //usage: "<name> <type> <mode> <uid> <gid> <major> <minor> <start> <inc> <count>\n"
73 //usage: "Where name is the file name, type can be one of:\n"
74 //usage: " f Regular file\n"
75 //usage: " d Directory\n"
76 //usage: " c Character device\n"
77 //usage: " b Block device\n"
78 //usage: " p Fifo (named pipe)\n"
79 //usage: "uid is the user id for the target file, gid is the group id for the\n"
80 //usage: "target file. The rest of the entries (major, minor, etc) apply to\n"
81 //usage: "to device special files. A '-' may be used for blank entries."
83 //usage:#define makedevs_example_usage
84 //usage: "For example:\n"
85 //usage: "<name> <type> <mode><uid><gid><major><minor><start><inc><count>\n"
86 //usage: "/dev d 755 0 0 - - - - -\n"
87 //usage: "/dev/console c 666 0 0 5 1 - - -\n"
88 //usage: "/dev/null c 666 0 0 1 3 0 0 -\n"
89 //usage: "/dev/zero c 666 0 0 1 5 0 0 -\n"
90 //usage: "/dev/hda b 640 0 0 3 0 0 0 -\n"
91 //usage: "/dev/hda b 640 0 0 3 1 1 1 15\n\n"
92 //usage: "Will Produce:\n"
94 //usage: "/dev/console\n"
95 //usage: "/dev/null\n"
96 //usage: "/dev/zero\n"
98 //usage: "/dev/hda[0-15]\n"
103 #if ENABLE_FEATURE_MAKEDEVS_LEAF
105 makedevs NAME TYPE MAJOR MINOR FIRST LAST [s]
111 FIRST..LAST specify numbers appended to NAME.
112 If 's' is the last argument, the base device is created as well.
114 makedevs /dev/ttyS c 4 66 2 63 -> ttyS2-ttyS63
115 makedevs /dev/hda b 3 0 0 8 s -> hda,hda1-hda8
117 int makedevs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
118 int makedevs_main(int argc, char **argv)
121 char *basedev, *type, *nodname, *buf;
122 int Smajor, Sminor, S, E;
124 if (argc < 7 || argv[1][0] == '-')
128 buf = xasprintf("%s%u", argv[1], (unsigned)-1);
130 Smajor = xatoi_positive(argv[3]);
131 Sminor = xatoi_positive(argv[4]);
132 S = xatoi_positive(argv[5]);
133 E = xatoi_positive(argv[6]);
134 nodname = argv[7] ? basedev : buf;
152 sprintf(buf, "%s%u", basedev, S);
154 /* if mode != S_IFCHR and != S_IFBLK,
155 * third param in mknod() ignored */
156 if (mknod(nodname, mode, makedev(Smajor, Sminor)))
157 bb_perror_msg("can't create '%s'", nodname);
159 /*if (nodname == basedev)*/ /* ex. /dev/hda - to /dev/hda1 ... */
168 #elif ENABLE_FEATURE_MAKEDEVS_TABLE
170 /* Licensed under GPLv2 or later, see file LICENSE in this source tree. */
172 int makedevs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
173 int makedevs_main(int argc UNUSED_PARAM, char **argv)
176 char *line = (char *)"-";
177 int ret = EXIT_SUCCESS;
179 opt_complementary = "=1"; /* exactly one param */
180 getopt32(argv, "d:", &line);
183 xchdir(*argv); /* ensure root dir exists */
187 printf("rootdir=%s\ntable=", *argv);
188 if (NOT_LONE_DASH(line)) {
189 printf("'%s'\n", line);
194 parser = config_open(line);
195 while (config_read(parser, &line, 1, 1, "# \t", PARSE_NORMAL)) {
198 unsigned mode = 0755;
202 unsigned increment = 0;
207 char *full_name = name;
211 linenum = parser->lineno;
213 if ((2 > sscanf(line, "%40s %c %o %40s %40s %u %u %u %u %u",
214 name, &type, &mode, user, group,
215 &major, &minor, &start, &increment, &count))
216 || ((unsigned)(major | minor | start | count | increment) > 255)
218 bb_error_msg("invalid line %d: '%s'", linenum, line);
223 gid = (*group) ? get_ug_id(group, xgroup2gid) : getgid();
224 uid = (*user) ? get_ug_id(user, xuname2uid) : getuid();
225 /* We are already in the right root dir,
226 * so make absolute paths relative */
227 if ('/' == *full_name)
231 bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR);
232 if (chown(full_name, uid, gid) == -1) {
234 bb_perror_msg("line %d: can't chown %s", linenum, full_name);
238 if (chmod(full_name, mode) < 0) {
240 bb_perror_msg("line %d: can't chmod %s", linenum, full_name);
244 } else if (type == 'f') {
246 if ((stat(full_name, &st) < 0 || !S_ISREG(st.st_mode))) {
247 bb_perror_msg("line %d: regular file '%s' does not exist", linenum, full_name);
251 if (chown(full_name, uid, gid) < 0)
253 if (chmod(full_name, mode) < 0)
262 } else if (type == 'c') {
264 } else if (type == 'b') {
267 bb_error_msg("line %d: unsupported file type %c", linenum, type);
272 full_name_inc = xmalloc(strlen(full_name) + sizeof(int)*3 + 2);
275 for (i = start; i <= start + count; i++) {
276 sprintf(full_name_inc, count ? "%s%u" : "%s", full_name, i);
277 rdev = makedev(major, minor + (i - start) * increment);
278 if (mknod(full_name_inc, mode, rdev) < 0) {
279 bb_perror_msg("line %d: can't create node %s", linenum, full_name_inc);
281 } else if (chown(full_name_inc, uid, gid) < 0) {
282 bb_perror_msg("line %d: can't chown %s", linenum, full_name_inc);
284 } else if (chmod(full_name_inc, mode) < 0) {
285 bb_perror_msg("line %d: can't chmod %s", linenum, full_name_inc);
292 if (ENABLE_FEATURE_CLEAN_UP)
293 config_close(parser);
299 # error makedevs configuration error, either leaf or table must be selected