Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Bernhard Reutner-Fischer | d9cf7ac | 2006-04-12 18:39:58 +0000 | [diff] [blame] | 2 | /* |
| 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 Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 9 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 11 | |
Denis Vlasenko | e324184 | 2007-08-13 10:36:25 +0000 | [diff] [blame] | 12 | #if ENABLE_FEATURE_MAKEDEVS_LEAF |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 13 | /* |
| 14 | makedevs NAME TYPE MAJOR MINOR FIRST LAST [s] |
| 15 | TYPEs: |
| 16 | b Block device |
| 17 | c Character device |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 18 | f FIFO |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 19 | |
| 20 | FIRST..LAST specify numbers appended to NAME. |
| 21 | If 's' is the last argument, the base device is created as well. |
| 22 | Examples: |
| 23 | makedevs /dev/ttyS c 4 66 2 63 -> ttyS2-ttyS63 |
| 24 | makedevs /dev/hda b 3 0 0 8 s -> hda,hda1-hda8 |
| 25 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 26 | int makedevs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 27 | int makedevs_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 28 | { |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 29 | mode_t mode; |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 30 | char *basedev, *type, *nodname, *buf; |
Eric Andersen | 4f807a8 | 2004-07-26 09:11:12 +0000 | [diff] [blame] | 31 | int Smajor, Sminor, S, E; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 33 | if (argc < 7 || argv[1][0] == '-') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 34 | bb_show_usage(); |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 35 | |
Eric Andersen | 19732c6 | 2002-04-13 14:26:44 +0000 | [diff] [blame] | 36 | basedev = argv[1]; |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 37 | buf = xasprintf("%s%u", argv[1], (unsigned)-1); |
Eric Andersen | 19732c6 | 2002-04-13 14:26:44 +0000 | [diff] [blame] | 38 | type = argv[2]; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 39 | Smajor = xatoi_u(argv[3]); |
| 40 | Sminor = xatoi_u(argv[4]); |
| 41 | S = xatoi_u(argv[5]); |
| 42 | E = xatoi_u(argv[6]); |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 43 | nodname = argv[7] ? basedev : buf; |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 44 | |
| 45 | mode = 0660; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 46 | switch (type[0]) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 47 | case 'c': |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 48 | mode |= S_IFCHR; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 49 | break; |
| 50 | case 'b': |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 51 | mode |= S_IFBLK; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 52 | break; |
| 53 | case 'f': |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 54 | mode |= S_IFIFO; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 55 | break; |
| 56 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 57 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 58 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 59 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 60 | while (S <= E) { |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 61 | sprintf(buf, "%s%u", basedev, S); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 62 | |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 63 | /* if mode != S_IFCHR and != S_IFBLK, |
| 64 | * third param in mknod() ignored */ |
Eric Andersen | e71e760 | 2004-07-30 14:36:37 +0000 | [diff] [blame] | 65 | if (mknod(nodname, mode, makedev(Smajor, Sminor))) |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 66 | bb_perror_msg("can't create '%s'", nodname); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 67 | |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 68 | /*if (nodname == basedev)*/ /* ex. /dev/hda - to /dev/hda1 ... */ |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 69 | nodname = buf; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 70 | S++; |
| 71 | Sminor++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | return 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Denis Vlasenko | e324184 | 2007-08-13 10:36:25 +0000 | [diff] [blame] | 77 | #elif ENABLE_FEATURE_MAKEDEVS_TABLE |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 78 | |
Rob Landley | 1b751c8 | 2005-10-28 09:24:33 +0000 | [diff] [blame] | 79 | /* Licensed under the GPL v2 or later, see the file LICENSE in this tarball. */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 80 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 81 | int makedevs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 82 | int makedevs_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 83 | { |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 84 | parser_t *parser; |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 85 | char *line = (char *)"-"; |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 86 | int ret = EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 87 | |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 88 | opt_complementary = "=1"; /* exactly one param */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 89 | getopt32(argv, "d:", &line); |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 90 | argv += optind; |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 91 | |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 92 | xchdir(*argv); /* ensure root dir exists */ |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 93 | |
| 94 | umask(0); |
| 95 | |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 96 | printf("rootdir=%s\ntable=", *argv); |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 97 | if (NOT_LONE_DASH(line)) { |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 98 | printf("'%s'\n", line); |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 99 | } else { |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 100 | puts("<stdin>"); |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 103 | parser = config_open(line); |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 104 | while (config_read(parser, &line, 1, 1, "# \t", PARSE_NORMAL)) { |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 105 | int linenum; |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 106 | char type; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 107 | unsigned mode = 0755; |
| 108 | unsigned major = 0; |
| 109 | unsigned minor = 0; |
| 110 | unsigned count = 0; |
| 111 | unsigned increment = 0; |
| 112 | unsigned start = 0; |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 113 | char name[41]; |
| 114 | char user[41]; |
| 115 | char group[41]; |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 116 | char *full_name = name; |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 117 | uid_t uid; |
| 118 | gid_t gid; |
| 119 | |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 120 | linenum = parser->lineno; |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 121 | |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 122 | if ((2 > sscanf(line, "%40s %c %o %40s %40s %u %u %u %u %u", |
| 123 | name, &type, &mode, user, group, |
| 124 | &major, &minor, &start, &increment, &count)) |
| 125 | || ((unsigned)(major | minor | start | count | increment) > 255) |
| 126 | ) { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 127 | bb_error_msg("invalid line %d: '%s'", linenum, line); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 128 | ret = EXIT_FAILURE; |
| 129 | continue; |
| 130 | } |
Bernhard Reutner-Fischer | 1732974 | 2005-12-21 15:07:30 +0000 | [diff] [blame] | 131 | |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 132 | gid = (*group) ? get_ug_id(group, xgroup2gid) : getgid(); |
| 133 | uid = (*user) ? get_ug_id(user, xuname2uid) : getuid(); |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 134 | /* We are already in the right root dir, |
| 135 | * so make absolute paths relative */ |
| 136 | if ('/' == *full_name) |
| 137 | full_name++; |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 138 | |
| 139 | if (type == 'd') { |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 140 | bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 141 | if (chown(full_name, uid, gid) == -1) { |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 142 | chown_fail: |
| 143 | bb_perror_msg("line %d: can't chown %s", linenum, full_name); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 144 | ret = EXIT_FAILURE; |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 145 | continue; |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 146 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 147 | if (chmod(full_name, mode) < 0) { |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 148 | chmod_fail: |
| 149 | bb_perror_msg("line %d: can't chmod %s", linenum, full_name); |
Eric Andersen | 90161c9 | 2005-07-18 22:40:59 +0000 | [diff] [blame] | 150 | ret = EXIT_FAILURE; |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 151 | continue; |
Eric Andersen | 90161c9 | 2005-07-18 22:40:59 +0000 | [diff] [blame] | 152 | } |
| 153 | } else if (type == 'f') { |
| 154 | struct stat st; |
| 155 | if ((stat(full_name, &st) < 0 || !S_ISREG(st.st_mode))) { |
| 156 | bb_perror_msg("line %d: regular file '%s' does not exist", linenum, full_name); |
| 157 | ret = EXIT_FAILURE; |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 158 | continue; |
Eric Andersen | 90161c9 | 2005-07-18 22:40:59 +0000 | [diff] [blame] | 159 | } |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 160 | if (chown(full_name, uid, gid) < 0) |
| 161 | goto chown_fail; |
| 162 | if (chmod(full_name, mode) < 0) |
| 163 | goto chmod_fail; |
Denis Vlasenko | e324184 | 2007-08-13 10:36:25 +0000 | [diff] [blame] | 164 | } else { |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 165 | dev_t rdev; |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 166 | unsigned i; |
| 167 | char *full_name_inc; |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 168 | |
| 169 | if (type == 'p') { |
| 170 | mode |= S_IFIFO; |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 171 | } else if (type == 'c') { |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 172 | mode |= S_IFCHR; |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 173 | } else if (type == 'b') { |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 174 | mode |= S_IFBLK; |
| 175 | } else { |
Eric Andersen | 2ada89b | 2005-07-18 09:45:35 +0000 | [diff] [blame] | 176 | bb_error_msg("line %d: unsupported file type %c", linenum, type); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 177 | ret = EXIT_FAILURE; |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 178 | continue; |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 181 | full_name_inc = xmalloc(strlen(full_name) + sizeof(int)*3 + 2); |
| 182 | if (count) |
| 183 | count--; |
| 184 | for (i = start; i <= start + count; i++) { |
| 185 | sprintf(full_name_inc, count ? "%s%u" : "%s", full_name, i); |
| 186 | rdev = makedev(major, minor + (i - start) * increment); |
| 187 | if (mknod(full_name_inc, mode, rdev) < 0) { |
| 188 | bb_perror_msg("line %d: can't create node %s", linenum, full_name_inc); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 189 | ret = EXIT_FAILURE; |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 190 | } else if (chown(full_name_inc, uid, gid) < 0) { |
| 191 | bb_perror_msg("line %d: can't chown %s", linenum, full_name_inc); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 192 | ret = EXIT_FAILURE; |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 193 | } else if (chmod(full_name_inc, mode) < 0) { |
| 194 | bb_perror_msg("line %d: can't chmod %s", linenum, full_name_inc); |
Eric Andersen | 90161c9 | 2005-07-18 22:40:59 +0000 | [diff] [blame] | 195 | ret = EXIT_FAILURE; |
| 196 | } |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 197 | } |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 198 | free(full_name_inc); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 199 | } |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 200 | } |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 201 | if (ENABLE_FEATURE_CLEAN_UP) |
| 202 | config_close(parser); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 203 | |
Bernhard Reutner-Fischer | 56fbd21 | 2006-04-24 17:41:29 +0000 | [diff] [blame] | 204 | return ret; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 205 | } |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 206 | |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 207 | #else |
Bernhard Reutner-Fischer | 771b186 | 2006-03-24 14:30:05 +0000 | [diff] [blame] | 208 | # error makedevs configuration error, either leaf or table must be selected |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 209 | #endif |