blob: 8cca8388249cca5837b437cf9c1d9d0944a017aa [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Bernhard Reutner-Fischerd9cf7ac2006-04-12 18:39:58 +00002/*
3 * public domain -- Dave 'Kill a Cop' Cinege <dcinege@psychosis.com>
4 *
5 * makedevs
6 * Make ranges of device files quickly.
7 * known bugs: can't deal with alpha ranges
8 */
Eric Andersen3d925622005-06-09 10:16:02 +00009
Pere Orga5bc8c002011-04-11 03:29:49 +020010//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"
15//usage: "\n"
16//usage: "\nTYPE is:"
17//usage: "\n b Block device"
18//usage: "\n c Character device"
19//usage: "\n f FIFO, MAJOR and MINOR are ignored"
20//usage: "\n"
21//usage: "\nFIRST..LAST specify numbers appended to NAME."
22//usage: "\nIf 's' is the last argument, the base device is created as well."
23//usage: "\n"
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"
27//usage:
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"
33//usage:#endif
34//usage:
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: "<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."
51//usage:
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"
62//usage: "/dev\n"
63//usage: "/dev/console\n"
64//usage: "/dev/null\n"
65//usage: "/dev/zero\n"
66//usage: "/dev/hda\n"
67//usage: "/dev/hda[0-15]\n"
68//usage:#endif
69
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000070#include "libbb.h"
Eric Andersen3d925622005-06-09 10:16:02 +000071
Denis Vlasenkoe3241842007-08-13 10:36:25 +000072#if ENABLE_FEATURE_MAKEDEVS_LEAF
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +000073/*
74makedevs NAME TYPE MAJOR MINOR FIRST LAST [s]
75TYPEs:
76b Block device
77c Character device
Denis Vlasenkob2dc9132008-08-03 22:14:02 +000078f FIFO
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +000079
80FIRST..LAST specify numbers appended to NAME.
81If 's' is the last argument, the base device is created as well.
82Examples:
83 makedevs /dev/ttyS c 4 66 2 63 -> ttyS2-ttyS63
84 makedevs /dev/hda b 3 0 0 8 s -> hda,hda1-hda8
85*/
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000086int makedevs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000087int makedevs_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000088{
Eric Andersen36006022002-06-08 12:44:17 +000089 mode_t mode;
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +000090 char *basedev, *type, *nodname, *buf;
Eric Andersen4f807a82004-07-26 09:11:12 +000091 int Smajor, Sminor, S, E;
Eric Andersencc8ed391999-10-05 16:24:54 +000092
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +000093 if (argc < 7 || argv[1][0] == '-')
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 bb_show_usage();
Erik Andersen5e1189e2000-04-15 16:34:54 +000095
Eric Andersen19732c62002-04-13 14:26:44 +000096 basedev = argv[1];
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +000097 buf = xasprintf("%s%u", argv[1], (unsigned)-1);
Eric Andersen19732c62002-04-13 14:26:44 +000098 type = argv[2];
Denys Vlasenko77832482010-08-12 14:14:45 +020099 Smajor = xatoi_positive(argv[3]);
100 Sminor = xatoi_positive(argv[4]);
101 S = xatoi_positive(argv[5]);
102 E = xatoi_positive(argv[6]);
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000103 nodname = argv[7] ? basedev : buf;
Eric Andersen36006022002-06-08 12:44:17 +0000104
105 mode = 0660;
Eric Andersencc8ed391999-10-05 16:24:54 +0000106 switch (type[0]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000107 case 'c':
Eric Andersen36006022002-06-08 12:44:17 +0000108 mode |= S_IFCHR;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000109 break;
110 case 'b':
Eric Andersen36006022002-06-08 12:44:17 +0000111 mode |= S_IFBLK;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112 break;
113 case 'f':
Eric Andersen36006022002-06-08 12:44:17 +0000114 mode |= S_IFIFO;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000115 break;
116 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000117 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000118 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000119
Erik Andersene49d5ec2000-02-08 19:58:47 +0000120 while (S <= E) {
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000121 sprintf(buf, "%s%u", basedev, S);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000123 /* if mode != S_IFCHR and != S_IFBLK,
124 * third param in mknod() ignored */
Eric Andersene71e7602004-07-30 14:36:37 +0000125 if (mknod(nodname, mode, makedev(Smajor, Sminor)))
Denys Vlasenko651a2692010-03-23 16:25:17 +0100126 bb_perror_msg("can't create '%s'", nodname);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000128 /*if (nodname == basedev)*/ /* ex. /dev/hda - to /dev/hda1 ... */
Eric Andersen36006022002-06-08 12:44:17 +0000129 nodname = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000130 S++;
131 Sminor++;
Eric Andersencc8ed391999-10-05 16:24:54 +0000132 }
133
Erik Andersene49d5ec2000-02-08 19:58:47 +0000134 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000135}
136
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000137#elif ENABLE_FEATURE_MAKEDEVS_TABLE
Eric Andersen3d925622005-06-09 10:16:02 +0000138
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200139/* Licensed under GPLv2 or later, see file LICENSE in this source tree. */
Eric Andersencc8ed391999-10-05 16:24:54 +0000140
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000141int makedevs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000142int makedevs_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen3d925622005-06-09 10:16:02 +0000143{
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000144 parser_t *parser;
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000145 char *line = (char *)"-";
Eric Andersen3d925622005-06-09 10:16:02 +0000146 int ret = EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000147
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000148 opt_complementary = "=1"; /* exactly one param */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000149 getopt32(argv, "d:", &line);
Denis Vlasenkob2dc9132008-08-03 22:14:02 +0000150 argv += optind;
Eric Andersene8614db2005-07-18 09:28:36 +0000151
Denis Vlasenkob2dc9132008-08-03 22:14:02 +0000152 xchdir(*argv); /* ensure root dir exists */
Eric Andersen3d925622005-06-09 10:16:02 +0000153
154 umask(0);
155
Denis Vlasenkob2dc9132008-08-03 22:14:02 +0000156 printf("rootdir=%s\ntable=", *argv);
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000157 if (NOT_LONE_DASH(line)) {
Denis Vlasenkob2dc9132008-08-03 22:14:02 +0000158 printf("'%s'\n", line);
Eric Andersene8614db2005-07-18 09:28:36 +0000159 } else {
Denis Vlasenkob2dc9132008-08-03 22:14:02 +0000160 puts("<stdin>");
Eric Andersene8614db2005-07-18 09:28:36 +0000161 }
162
Denis Vlasenkob2dc9132008-08-03 22:14:02 +0000163 parser = config_open(line);
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000164 while (config_read(parser, &line, 1, 1, "# \t", PARSE_NORMAL)) {
Denis Vlasenkob2dc9132008-08-03 22:14:02 +0000165 int linenum;
Eric Andersen3d925622005-06-09 10:16:02 +0000166 char type;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000167 unsigned mode = 0755;
168 unsigned major = 0;
169 unsigned minor = 0;
170 unsigned count = 0;
171 unsigned increment = 0;
172 unsigned start = 0;
Eric Andersen3d925622005-06-09 10:16:02 +0000173 char name[41];
174 char user[41];
175 char group[41];
Denis Vlasenkob2dc9132008-08-03 22:14:02 +0000176 char *full_name = name;
Eric Andersen3d925622005-06-09 10:16:02 +0000177 uid_t uid;
178 gid_t gid;
179
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000180 linenum = parser->lineno;
Eric Andersene8614db2005-07-18 09:28:36 +0000181
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000182 if ((2 > sscanf(line, "%40s %c %o %40s %40s %u %u %u %u %u",
183 name, &type, &mode, user, group,
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200184 &major, &minor, &start, &increment, &count))
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000185 || ((unsigned)(major | minor | start | count | increment) > 255)
186 ) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000187 bb_error_msg("invalid line %d: '%s'", linenum, line);
Eric Andersen3d925622005-06-09 10:16:02 +0000188 ret = EXIT_FAILURE;
189 continue;
190 }
Bernhard Reutner-Fischer17329742005-12-21 15:07:30 +0000191
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000192 gid = (*group) ? get_ug_id(group, xgroup2gid) : getgid();
193 uid = (*user) ? get_ug_id(user, xuname2uid) : getuid();
Denis Vlasenkob2dc9132008-08-03 22:14:02 +0000194 /* We are already in the right root dir,
195 * so make absolute paths relative */
196 if ('/' == *full_name)
197 full_name++;
Eric Andersen3d925622005-06-09 10:16:02 +0000198
199 if (type == 'd') {
Eric Andersene8614db2005-07-18 09:28:36 +0000200 bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR);
Eric Andersen3d925622005-06-09 10:16:02 +0000201 if (chown(full_name, uid, gid) == -1) {
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000202 chown_fail:
203 bb_perror_msg("line %d: can't chown %s", linenum, full_name);
Eric Andersen3d925622005-06-09 10:16:02 +0000204 ret = EXIT_FAILURE;
Denis Vlasenkob2dc9132008-08-03 22:14:02 +0000205 continue;
Eric Andersen3d925622005-06-09 10:16:02 +0000206 }
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000207 if (chmod(full_name, mode) < 0) {
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000208 chmod_fail:
209 bb_perror_msg("line %d: can't chmod %s", linenum, full_name);
Eric Andersen90161c92005-07-18 22:40:59 +0000210 ret = EXIT_FAILURE;
Denis Vlasenkob2dc9132008-08-03 22:14:02 +0000211 continue;
Eric Andersen90161c92005-07-18 22:40:59 +0000212 }
213 } else if (type == 'f') {
214 struct stat st;
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);
217 ret = EXIT_FAILURE;
Denis Vlasenkob2dc9132008-08-03 22:14:02 +0000218 continue;
Eric Andersen90161c92005-07-18 22:40:59 +0000219 }
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000220 if (chown(full_name, uid, gid) < 0)
221 goto chown_fail;
222 if (chmod(full_name, mode) < 0)
223 goto chmod_fail;
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000224 } else {
Eric Andersen3d925622005-06-09 10:16:02 +0000225 dev_t rdev;
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000226 unsigned i;
227 char *full_name_inc;
Eric Andersen3d925622005-06-09 10:16:02 +0000228
229 if (type == 'p') {
230 mode |= S_IFIFO;
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000231 } else if (type == 'c') {
Eric Andersen3d925622005-06-09 10:16:02 +0000232 mode |= S_IFCHR;
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000233 } else if (type == 'b') {
Eric Andersen3d925622005-06-09 10:16:02 +0000234 mode |= S_IFBLK;
235 } else {
Eric Andersen2ada89b2005-07-18 09:45:35 +0000236 bb_error_msg("line %d: unsupported file type %c", linenum, type);
Eric Andersen3d925622005-06-09 10:16:02 +0000237 ret = EXIT_FAILURE;
Denis Vlasenkob2dc9132008-08-03 22:14:02 +0000238 continue;
Eric Andersen3d925622005-06-09 10:16:02 +0000239 }
240
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000241 full_name_inc = xmalloc(strlen(full_name) + sizeof(int)*3 + 2);
242 if (count)
243 count--;
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);
Eric Andersen3d925622005-06-09 10:16:02 +0000249 ret = EXIT_FAILURE;
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000250 } else if (chown(full_name_inc, uid, gid) < 0) {
251 bb_perror_msg("line %d: can't chown %s", linenum, full_name_inc);
Eric Andersen3d925622005-06-09 10:16:02 +0000252 ret = EXIT_FAILURE;
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000253 } else if (chmod(full_name_inc, mode) < 0) {
254 bb_perror_msg("line %d: can't chmod %s", linenum, full_name_inc);
Eric Andersen90161c92005-07-18 22:40:59 +0000255 ret = EXIT_FAILURE;
256 }
Eric Andersen3d925622005-06-09 10:16:02 +0000257 }
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000258 free(full_name_inc);
Eric Andersen3d925622005-06-09 10:16:02 +0000259 }
Eric Andersen3d925622005-06-09 10:16:02 +0000260 }
Denis Vlasenkoe1fa8172008-08-01 02:15:05 +0000261 if (ENABLE_FEATURE_CLEAN_UP)
262 config_close(parser);
Eric Andersen3d925622005-06-09 10:16:02 +0000263
Bernhard Reutner-Fischer56fbd212006-04-24 17:41:29 +0000264 return ret;
Eric Andersencc8ed391999-10-05 16:24:54 +0000265}
Eric Andersene8614db2005-07-18 09:28:36 +0000266
Eric Andersen3d925622005-06-09 10:16:02 +0000267#else
Bernhard Reutner-Fischer771b1862006-03-24 14:30:05 +0000268# error makedevs configuration error, either leaf or table must be selected
Eric Andersen3d925622005-06-09 10:16:02 +0000269#endif