blob: 1f88f34288437c1e5daf4cba2193cf308d886776 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Eric Andersen3d925622005-06-09 10:16:02 +000011
Denis Vlasenkoe3241842007-08-13 10:36:25 +000012#if ENABLE_FEATURE_MAKEDEVS_LEAF
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000013int makedevs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000014int makedevs_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000015{
Eric Andersen36006022002-06-08 12:44:17 +000016 mode_t mode;
17 char *basedev, *type, *nodname, buf[255];
Eric Andersen4f807a82004-07-26 09:11:12 +000018 int Smajor, Sminor, S, E;
Eric Andersencc8ed391999-10-05 16:24:54 +000019
Erik Andersen5e1189e2000-04-15 16:34:54 +000020 if (argc < 7 || *argv[1]=='-')
Manuel Novoa III cad53642003-03-19 09:13:01 +000021 bb_show_usage();
Erik Andersen5e1189e2000-04-15 16:34:54 +000022
Eric Andersen19732c62002-04-13 14:26:44 +000023 basedev = argv[1];
24 type = argv[2];
Denis Vlasenko13858992006-10-08 12:49:22 +000025 Smajor = xatoi_u(argv[3]);
26 Sminor = xatoi_u(argv[4]);
27 S = xatoi_u(argv[5]);
28 E = xatoi_u(argv[6]);
Eric Andersen36006022002-06-08 12:44:17 +000029 nodname = argc == 8 ? basedev : buf;
30
31 mode = 0660;
Eric Andersen19732c62002-04-13 14:26:44 +000032
Eric Andersencc8ed391999-10-05 16:24:54 +000033 switch (type[0]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000034 case 'c':
Eric Andersen36006022002-06-08 12:44:17 +000035 mode |= S_IFCHR;
Erik Andersene49d5ec2000-02-08 19:58:47 +000036 break;
37 case 'b':
Eric Andersen36006022002-06-08 12:44:17 +000038 mode |= S_IFBLK;
Erik Andersene49d5ec2000-02-08 19:58:47 +000039 break;
40 case 'f':
Eric Andersen36006022002-06-08 12:44:17 +000041 mode |= S_IFIFO;
Erik Andersene49d5ec2000-02-08 19:58:47 +000042 break;
43 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000045 }
Eric Andersencc8ed391999-10-05 16:24:54 +000046
Erik Andersene49d5ec2000-02-08 19:58:47 +000047 while (S <= E) {
Eric Andersen36006022002-06-08 12:44:17 +000048 int sz;
Erik Andersene49d5ec2000-02-08 19:58:47 +000049
Eric Andersen36006022002-06-08 12:44:17 +000050 sz = snprintf(buf, sizeof(buf), "%s%d", basedev, S);
Denis Vlasenko51742f42007-04-12 00:32:05 +000051 if (sz < 0 || sz >= sizeof(buf)) /* libc different */
Manuel Novoa III cad53642003-03-19 09:13:01 +000052 bb_error_msg_and_die("%s too large", basedev);
Erik Andersene49d5ec2000-02-08 19:58:47 +000053
Eric Andersen36006022002-06-08 12:44:17 +000054 /* if mode != S_IFCHR and != S_IFBLK third param in mknod() ignored */
Erik Andersene49d5ec2000-02-08 19:58:47 +000055
Eric Andersene71e7602004-07-30 14:36:37 +000056 if (mknod(nodname, mode, makedev(Smajor, Sminor)))
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000057 bb_error_msg("failed to create: %s", nodname);
Erik Andersene49d5ec2000-02-08 19:58:47 +000058
Eric Andersen36006022002-06-08 12:44:17 +000059 if (nodname == basedev) /* ex. /dev/hda - to /dev/hda1 ... */
60 nodname = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 S++;
62 Sminor++;
Eric Andersencc8ed391999-10-05 16:24:54 +000063 }
64
Erik Andersene49d5ec2000-02-08 19:58:47 +000065 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +000066}
67
Denis Vlasenkoe3241842007-08-13 10:36:25 +000068#elif ENABLE_FEATURE_MAKEDEVS_TABLE
Eric Andersen3d925622005-06-09 10:16:02 +000069
Rob Landley1b751c82005-10-28 09:24:33 +000070/* Licensed under the GPL v2 or later, see the file LICENSE in this tarball. */
Eric Andersencc8ed391999-10-05 16:24:54 +000071
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000072int makedevs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000073int makedevs_main(int argc, char **argv)
Eric Andersen3d925622005-06-09 10:16:02 +000074{
Eric Andersene8614db2005-07-18 09:28:36 +000075 FILE *table = stdin;
76 char *rootdir = NULL;
77 char *line = NULL;
78 int linenum = 0;
Eric Andersen3d925622005-06-09 10:16:02 +000079 int ret = EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +000080
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000081 getopt32(argv, "d:", &line);
Rob Landley1b751c82005-10-28 09:24:33 +000082 if (line)
Rob Landleyd921b2e2006-08-03 15:41:12 +000083 table = xfopen(line, "r");
Eric Andersen3d925622005-06-09 10:16:02 +000084
Eric Andersene8614db2005-07-18 09:28:36 +000085 if (optind >= argc || (rootdir=argv[optind])==NULL) {
Eric Andersen2ada89b2005-07-18 09:45:35 +000086 bb_error_msg_and_die("root directory not specified");
Eric Andersene8614db2005-07-18 09:28:36 +000087 }
88
Rob Landleyd921b2e2006-08-03 15:41:12 +000089 xchdir(rootdir);
Eric Andersen3d925622005-06-09 10:16:02 +000090
91 umask(0);
92
Eric Andersene8614db2005-07-18 09:28:36 +000093 printf("rootdir=%s\n", rootdir);
94 if (line) {
95 printf("table='%s'\n", line);
96 } else {
97 printf("table=<stdin>\n");
98 }
99
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000100 while ((line = xmalloc_getline(table))) {
Eric Andersen3d925622005-06-09 10:16:02 +0000101 char type;
102 unsigned int mode = 0755;
103 unsigned int major = 0;
104 unsigned int minor = 0;
105 unsigned int count = 0;
106 unsigned int increment = 0;
107 unsigned int start = 0;
108 char name[41];
109 char user[41];
110 char group[41];
111 char *full_name;
112 uid_t uid;
113 gid_t gid;
114
Eric Andersene8614db2005-07-18 09:28:36 +0000115 linenum++;
116
Eric Andersen3d925622005-06-09 10:16:02 +0000117 if ((2 > sscanf(line, "%40s %c %o %40s %40s %u %u %u %u %u", name,
Eric Andersene8614db2005-07-18 09:28:36 +0000118 &type, &mode, user, group, &major,
119 &minor, &start, &increment, &count)) ||
120 ((major | minor | start | count | increment) > 255))
121 {
122 if (*line=='\0' || *line=='#' || isspace(*line))
123 continue;
Denis Vlasenko6d655be2006-09-06 19:02:46 +0000124 bb_error_msg("line %d invalid: '%s'", linenum, line);
Eric Andersen3d925622005-06-09 10:16:02 +0000125 ret = EXIT_FAILURE;
126 continue;
127 }
128 if (name[0] == '#') {
129 continue;
130 }
Bernhard Reutner-Fischer17329742005-12-21 15:07:30 +0000131
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000132 gid = (*group) ? get_ug_id(group, xgroup2gid) : getgid();
133 uid = (*user) ? get_ug_id(user, xuname2uid) : getuid();
Eric Andersen3d925622005-06-09 10:16:02 +0000134 full_name = concat_path_file(rootdir, name);
135
136 if (type == 'd') {
Eric Andersene8614db2005-07-18 09:28:36 +0000137 bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR);
Eric Andersen3d925622005-06-09 10:16:02 +0000138 if (chown(full_name, uid, gid) == -1) {
Eric Andersene8614db2005-07-18 09:28:36 +0000139 bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
Eric Andersen3d925622005-06-09 10:16:02 +0000140 ret = EXIT_FAILURE;
141 goto loop;
142 }
Eric Andersen90161c92005-07-18 22:40:59 +0000143 if ((mode != -1) && (chmod(full_name, mode) < 0)){
144 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
145 ret = EXIT_FAILURE;
146 goto loop;
147 }
148 } else if (type == 'f') {
149 struct stat st;
150 if ((stat(full_name, &st) < 0 || !S_ISREG(st.st_mode))) {
151 bb_perror_msg("line %d: regular file '%s' does not exist", linenum, full_name);
152 ret = EXIT_FAILURE;
153 goto loop;
154 }
155 if (chown(full_name, uid, gid) == -1) {
156 bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
157 ret = EXIT_FAILURE;
158 goto loop;
159 }
160 if ((mode != -1) && (chmod(full_name, mode) < 0)){
161 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
162 ret = EXIT_FAILURE;
163 goto loop;
164 }
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000165 } else {
Eric Andersen3d925622005-06-09 10:16:02 +0000166 dev_t rdev;
167
168 if (type == 'p') {
169 mode |= S_IFIFO;
170 }
171 else if (type == 'c') {
172 mode |= S_IFCHR;
173 }
174 else if (type == 'b') {
175 mode |= S_IFBLK;
176 } else {
Eric Andersen2ada89b2005-07-18 09:45:35 +0000177 bb_error_msg("line %d: unsupported file type %c", linenum, type);
Eric Andersen3d925622005-06-09 10:16:02 +0000178 ret = EXIT_FAILURE;
179 goto loop;
180 }
181
182 if (count > 0) {
183 int i;
184 char *full_name_inc;
185
186 full_name_inc = xmalloc(strlen(full_name) + 4);
187 for (i = start; i < count; i++) {
188 sprintf(full_name_inc, "%s%d", full_name, i);
Bernhard Reutner-Fischer7b1f2362007-01-21 11:20:01 +0000189 rdev = makedev(major, minor + (i * increment - start));
Eric Andersen3d925622005-06-09 10:16:02 +0000190 if (mknod(full_name_inc, mode, rdev) == -1) {
Denis Vlasenkoea620772006-10-14 02:23:43 +0000191 bb_perror_msg("line %d: cannot create node %s", linenum, full_name_inc);
Eric Andersen3d925622005-06-09 10:16:02 +0000192 ret = EXIT_FAILURE;
193 }
194 else if (chown(full_name_inc, uid, gid) == -1) {
Eric Andersene8614db2005-07-18 09:28:36 +0000195 bb_perror_msg("line %d: chown failed for %s", linenum, full_name_inc);
Eric Andersen3d925622005-06-09 10:16:02 +0000196 ret = EXIT_FAILURE;
197 }
Eric Andersen90161c92005-07-18 22:40:59 +0000198 if ((mode != -1) && (chmod(full_name_inc, mode) < 0)){
199 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name_inc);
200 ret = EXIT_FAILURE;
201 }
Eric Andersen3d925622005-06-09 10:16:02 +0000202 }
203 free(full_name_inc);
204 } else {
Bernhard Reutner-Fischer7b1f2362007-01-21 11:20:01 +0000205 rdev = makedev(major, minor);
Eric Andersen3d925622005-06-09 10:16:02 +0000206 if (mknod(full_name, mode, rdev) == -1) {
Denis Vlasenkoea620772006-10-14 02:23:43 +0000207 bb_perror_msg("line %d: cannot create node %s", linenum, full_name);
Eric Andersen3d925622005-06-09 10:16:02 +0000208 ret = EXIT_FAILURE;
209 }
210 else if (chown(full_name, uid, gid) == -1) {
Eric Andersene8614db2005-07-18 09:28:36 +0000211 bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
Eric Andersen3d925622005-06-09 10:16:02 +0000212 ret = EXIT_FAILURE;
213 }
Eric Andersen90161c92005-07-18 22:40:59 +0000214 if ((mode != -1) && (chmod(full_name, mode) < 0)){
215 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
216 ret = EXIT_FAILURE;
217 }
Eric Andersen3d925622005-06-09 10:16:02 +0000218 }
219 }
220loop:
221 free(line);
222 free(full_name);
223 }
224 fclose(table);
225
Bernhard Reutner-Fischer56fbd212006-04-24 17:41:29 +0000226 return ret;
Eric Andersencc8ed391999-10-05 16:24:54 +0000227}
Eric Andersene8614db2005-07-18 09:28:36 +0000228
Eric Andersen3d925622005-06-09 10:16:02 +0000229#else
Bernhard Reutner-Fischer771b1862006-03-24 14:30:05 +0000230# error makedevs configuration error, either leaf or table must be selected
Eric Andersen3d925622005-06-09 10:16:02 +0000231#endif