blob: 3bc1559c78b511ff6704281a0fc4fcbbabe7cad8 [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
Bernhard Reutner-Fischerc89982d2006-06-03 19:49:21 +000010#include "busybox.h"
Eric Andersen3d925622005-06-09 10:16:02 +000011
12#ifdef CONFIG_FEATURE_MAKEDEVS_LEAF
Erik Andersene49d5ec2000-02-08 19:58:47 +000013int makedevs_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000014{
Eric Andersen36006022002-06-08 12:44:17 +000015 mode_t mode;
16 char *basedev, *type, *nodname, buf[255];
Eric Andersen4f807a82004-07-26 09:11:12 +000017 int Smajor, Sminor, S, E;
Eric Andersencc8ed391999-10-05 16:24:54 +000018
Erik Andersen5e1189e2000-04-15 16:34:54 +000019 if (argc < 7 || *argv[1]=='-')
Manuel Novoa III cad53642003-03-19 09:13:01 +000020 bb_show_usage();
Erik Andersen5e1189e2000-04-15 16:34:54 +000021
Eric Andersen19732c62002-04-13 14:26:44 +000022 basedev = argv[1];
23 type = argv[2];
Denis Vlasenko13858992006-10-08 12:49:22 +000024 Smajor = xatoi_u(argv[3]);
25 Sminor = xatoi_u(argv[4]);
26 S = xatoi_u(argv[5]);
27 E = xatoi_u(argv[6]);
Eric Andersen36006022002-06-08 12:44:17 +000028 nodname = argc == 8 ? basedev : buf;
29
30 mode = 0660;
Eric Andersen19732c62002-04-13 14:26:44 +000031
Eric Andersencc8ed391999-10-05 16:24:54 +000032 switch (type[0]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000033 case 'c':
Eric Andersen36006022002-06-08 12:44:17 +000034 mode |= S_IFCHR;
Erik Andersene49d5ec2000-02-08 19:58:47 +000035 break;
36 case 'b':
Eric Andersen36006022002-06-08 12:44:17 +000037 mode |= S_IFBLK;
Erik Andersene49d5ec2000-02-08 19:58:47 +000038 break;
39 case 'f':
Eric Andersen36006022002-06-08 12:44:17 +000040 mode |= S_IFIFO;
Erik Andersene49d5ec2000-02-08 19:58:47 +000041 break;
42 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +000043 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000044 }
Eric Andersencc8ed391999-10-05 16:24:54 +000045
Erik Andersene49d5ec2000-02-08 19:58:47 +000046 while (S <= E) {
Eric Andersen36006022002-06-08 12:44:17 +000047 int sz;
Erik Andersene49d5ec2000-02-08 19:58:47 +000048
Eric Andersen36006022002-06-08 12:44:17 +000049 sz = snprintf(buf, sizeof(buf), "%s%d", basedev, S);
50 if(sz<0 || sz>=sizeof(buf)) /* libc different */
Manuel Novoa III cad53642003-03-19 09:13:01 +000051 bb_error_msg_and_die("%s too large", basedev);
Erik Andersene49d5ec2000-02-08 19:58:47 +000052
Eric Andersen36006022002-06-08 12:44:17 +000053 /* if mode != S_IFCHR and != S_IFBLK third param in mknod() ignored */
Erik Andersene49d5ec2000-02-08 19:58:47 +000054
Eric Andersene71e7602004-07-30 14:36:37 +000055 if (mknod(nodname, mode, makedev(Smajor, Sminor)))
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000056 bb_error_msg("failed to create: %s", nodname);
Erik Andersene49d5ec2000-02-08 19:58:47 +000057
Eric Andersen36006022002-06-08 12:44:17 +000058 if (nodname == basedev) /* ex. /dev/hda - to /dev/hda1 ... */
59 nodname = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +000060 S++;
61 Sminor++;
Eric Andersencc8ed391999-10-05 16:24:54 +000062 }
63
Erik Andersene49d5ec2000-02-08 19:58:47 +000064 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +000065}
66
Eric Andersen3d925622005-06-09 10:16:02 +000067#elif defined CONFIG_FEATURE_MAKEDEVS_TABLE
68
Rob Landley1b751c82005-10-28 09:24:33 +000069/* Licensed under the GPL v2 or later, see the file LICENSE in this tarball. */
Eric Andersencc8ed391999-10-05 16:24:54 +000070
Rob Landleydfba7412006-03-06 20:47:33 +000071int makedevs_main(int argc, char **argv)
Eric Andersen3d925622005-06-09 10:16:02 +000072{
Eric Andersene8614db2005-07-18 09:28:36 +000073 FILE *table = stdin;
74 char *rootdir = NULL;
75 char *line = NULL;
76 int linenum = 0;
Eric Andersen3d925622005-06-09 10:16:02 +000077 int ret = EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +000078
Denis Vlasenko67b23e62006-10-03 21:00:06 +000079 getopt32(argc, argv, "d:", &line);
Rob Landley1b751c82005-10-28 09:24:33 +000080 if (line)
Rob Landleyd921b2e2006-08-03 15:41:12 +000081 table = xfopen(line, "r");
Eric Andersen3d925622005-06-09 10:16:02 +000082
Eric Andersene8614db2005-07-18 09:28:36 +000083 if (optind >= argc || (rootdir=argv[optind])==NULL) {
Eric Andersen2ada89b2005-07-18 09:45:35 +000084 bb_error_msg_and_die("root directory not specified");
Eric Andersene8614db2005-07-18 09:28:36 +000085 }
86
Rob Landleyd921b2e2006-08-03 15:41:12 +000087 xchdir(rootdir);
Eric Andersen3d925622005-06-09 10:16:02 +000088
89 umask(0);
90
Eric Andersene8614db2005-07-18 09:28:36 +000091 printf("rootdir=%s\n", rootdir);
92 if (line) {
93 printf("table='%s'\n", line);
94 } else {
95 printf("table=<stdin>\n");
96 }
97
Denis Vlasenko2d5ca602006-10-12 22:43:20 +000098 while ((line = xmalloc_getline(table))) {
Eric Andersen3d925622005-06-09 10:16:02 +000099 char type;
100 unsigned int mode = 0755;
101 unsigned int major = 0;
102 unsigned int minor = 0;
103 unsigned int count = 0;
104 unsigned int increment = 0;
105 unsigned int start = 0;
106 char name[41];
107 char user[41];
108 char group[41];
109 char *full_name;
110 uid_t uid;
111 gid_t gid;
112
Eric Andersene8614db2005-07-18 09:28:36 +0000113 linenum++;
114
Eric Andersen3d925622005-06-09 10:16:02 +0000115 if ((2 > sscanf(line, "%40s %c %o %40s %40s %u %u %u %u %u", name,
Eric Andersene8614db2005-07-18 09:28:36 +0000116 &type, &mode, user, group, &major,
117 &minor, &start, &increment, &count)) ||
118 ((major | minor | start | count | increment) > 255))
119 {
120 if (*line=='\0' || *line=='#' || isspace(*line))
121 continue;
Denis Vlasenko6d655be2006-09-06 19:02:46 +0000122 bb_error_msg("line %d invalid: '%s'", linenum, line);
Eric Andersen3d925622005-06-09 10:16:02 +0000123 ret = EXIT_FAILURE;
124 continue;
125 }
126 if (name[0] == '#') {
127 continue;
128 }
Bernhard Reutner-Fischer17329742005-12-21 15:07:30 +0000129
130 gid = (*group) ? get_ug_id(group, bb_xgetgrnam) : getgid();
131 uid = (*user) ? get_ug_id(user, bb_xgetpwnam) : getuid();
Eric Andersen3d925622005-06-09 10:16:02 +0000132 full_name = concat_path_file(rootdir, name);
133
134 if (type == 'd') {
Eric Andersene8614db2005-07-18 09:28:36 +0000135 bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR);
Eric Andersen3d925622005-06-09 10:16:02 +0000136 if (chown(full_name, uid, gid) == -1) {
Eric Andersene8614db2005-07-18 09:28:36 +0000137 bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
Eric Andersen3d925622005-06-09 10:16:02 +0000138 ret = EXIT_FAILURE;
139 goto loop;
140 }
Eric Andersen90161c92005-07-18 22:40:59 +0000141 if ((mode != -1) && (chmod(full_name, mode) < 0)){
142 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
143 ret = EXIT_FAILURE;
144 goto loop;
145 }
146 } else if (type == 'f') {
147 struct stat st;
148 if ((stat(full_name, &st) < 0 || !S_ISREG(st.st_mode))) {
149 bb_perror_msg("line %d: regular file '%s' does not exist", linenum, full_name);
150 ret = EXIT_FAILURE;
151 goto loop;
152 }
153 if (chown(full_name, uid, gid) == -1) {
154 bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
155 ret = EXIT_FAILURE;
156 goto loop;
157 }
158 if ((mode != -1) && (chmod(full_name, mode) < 0)){
159 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
160 ret = EXIT_FAILURE;
161 goto loop;
162 }
163 } else
164 {
Eric Andersen3d925622005-06-09 10:16:02 +0000165 dev_t rdev;
166
167 if (type == 'p') {
168 mode |= S_IFIFO;
169 }
170 else if (type == 'c') {
171 mode |= S_IFCHR;
172 }
173 else if (type == 'b') {
174 mode |= S_IFBLK;
175 } else {
Eric Andersen2ada89b2005-07-18 09:45:35 +0000176 bb_error_msg("line %d: unsupported file type %c", linenum, type);
Eric Andersen3d925622005-06-09 10:16:02 +0000177 ret = EXIT_FAILURE;
178 goto loop;
179 }
180
181 if (count > 0) {
182 int i;
183 char *full_name_inc;
184
185 full_name_inc = xmalloc(strlen(full_name) + 4);
186 for (i = start; i < count; i++) {
187 sprintf(full_name_inc, "%s%d", full_name, i);
188 rdev = (major << 8) + minor + (i * increment - start);
189 if (mknod(full_name_inc, mode, rdev) == -1) {
Denis Vlasenkoea620772006-10-14 02:23:43 +0000190 bb_perror_msg("line %d: cannot create node %s", linenum, full_name_inc);
Eric Andersen3d925622005-06-09 10:16:02 +0000191 ret = EXIT_FAILURE;
192 }
193 else if (chown(full_name_inc, uid, gid) == -1) {
Eric Andersene8614db2005-07-18 09:28:36 +0000194 bb_perror_msg("line %d: chown failed for %s", linenum, full_name_inc);
Eric Andersen3d925622005-06-09 10:16:02 +0000195 ret = EXIT_FAILURE;
196 }
Eric Andersen90161c92005-07-18 22:40:59 +0000197 if ((mode != -1) && (chmod(full_name_inc, mode) < 0)){
198 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name_inc);
199 ret = EXIT_FAILURE;
200 }
Eric Andersen3d925622005-06-09 10:16:02 +0000201 }
202 free(full_name_inc);
203 } else {
204 rdev = (major << 8) + minor;
205 if (mknod(full_name, mode, rdev) == -1) {
Denis Vlasenkoea620772006-10-14 02:23:43 +0000206 bb_perror_msg("line %d: cannot create node %s", linenum, full_name);
Eric Andersen3d925622005-06-09 10:16:02 +0000207 ret = EXIT_FAILURE;
208 }
209 else if (chown(full_name, uid, gid) == -1) {
Eric Andersene8614db2005-07-18 09:28:36 +0000210 bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
Eric Andersen3d925622005-06-09 10:16:02 +0000211 ret = EXIT_FAILURE;
212 }
Eric Andersen90161c92005-07-18 22:40:59 +0000213 if ((mode != -1) && (chmod(full_name, mode) < 0)){
214 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
215 ret = EXIT_FAILURE;
216 }
Eric Andersen3d925622005-06-09 10:16:02 +0000217 }
218 }
219loop:
220 free(line);
221 free(full_name);
222 }
223 fclose(table);
224
Bernhard Reutner-Fischer56fbd212006-04-24 17:41:29 +0000225 return ret;
Eric Andersencc8ed391999-10-05 16:24:54 +0000226}
Eric Andersene8614db2005-07-18 09:28:36 +0000227
Eric Andersen3d925622005-06-09 10:16:02 +0000228#else
Bernhard Reutner-Fischer771b1862006-03-24 14:30:05 +0000229# error makedevs configuration error, either leaf or table must be selected
Eric Andersen3d925622005-06-09 10:16:02 +0000230#endif