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 | |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <fcntl.h> |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 14 | #include <time.h> |
Eric Andersen | 54e57da | 2005-07-18 09:42:37 +0000 | [diff] [blame] | 15 | #include <ctype.h> |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 16 | #include <unistd.h> |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/sysmacros.h> /* major() and minor() */ |
| 19 | #include "busybox.h" |
| 20 | |
| 21 | #ifdef CONFIG_FEATURE_MAKEDEVS_LEAF |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 22 | int makedevs_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 23 | { |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 24 | mode_t mode; |
| 25 | char *basedev, *type, *nodname, buf[255]; |
Eric Andersen | 4f807a8 | 2004-07-26 09:11:12 +0000 | [diff] [blame] | 26 | int Smajor, Sminor, S, E; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 27 | |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 28 | if (argc < 7 || *argv[1]=='-') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 29 | bb_show_usage(); |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 30 | |
Eric Andersen | 19732c6 | 2002-04-13 14:26:44 +0000 | [diff] [blame] | 31 | basedev = argv[1]; |
| 32 | type = argv[2]; |
Eric Andersen | cbcdbc4 | 2004-07-30 17:48:21 +0000 | [diff] [blame] | 33 | Smajor = atoi(argv[3]); |
| 34 | Sminor = atoi(argv[4]); |
Eric Andersen | 19732c6 | 2002-04-13 14:26:44 +0000 | [diff] [blame] | 35 | S = atoi(argv[5]); |
| 36 | E = atoi(argv[6]); |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 37 | nodname = argc == 8 ? basedev : buf; |
| 38 | |
| 39 | mode = 0660; |
Eric Andersen | 19732c6 | 2002-04-13 14:26:44 +0000 | [diff] [blame] | 40 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 41 | switch (type[0]) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 42 | case 'c': |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 43 | mode |= S_IFCHR; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 44 | break; |
| 45 | case 'b': |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 46 | mode |= S_IFBLK; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 47 | break; |
| 48 | case 'f': |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 49 | mode |= S_IFIFO; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | break; |
| 51 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 52 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 53 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 54 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 55 | while (S <= E) { |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 56 | int sz; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 57 | |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 58 | sz = snprintf(buf, sizeof(buf), "%s%d", basedev, S); |
| 59 | if(sz<0 || sz>=sizeof(buf)) /* libc different */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 60 | bb_error_msg_and_die("%s too large", basedev); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 61 | |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 62 | /* if mode != S_IFCHR and != S_IFBLK third param in mknod() ignored */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 63 | |
Eric Andersen | e71e760 | 2004-07-30 14:36:37 +0000 | [diff] [blame] | 64 | if (mknod(nodname, mode, makedev(Smajor, Sminor))) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 65 | bb_error_msg("Failed to create: %s", nodname); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 66 | |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 67 | if (nodname == basedev) /* ex. /dev/hda - to /dev/hda1 ... */ |
| 68 | nodname = buf; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 69 | S++; |
| 70 | Sminor++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 73 | return 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 76 | #elif defined CONFIG_FEATURE_MAKEDEVS_TABLE |
| 77 | |
Rob Landley | 1b751c8 | 2005-10-28 09:24:33 +0000 | [diff] [blame] | 78 | /* 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] | 79 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 80 | int makedevs_main(int argc, char **argv) |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 81 | { |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 82 | FILE *table = stdin; |
| 83 | char *rootdir = NULL; |
| 84 | char *line = NULL; |
| 85 | int linenum = 0; |
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 | |
Rob Landley | 1b751c8 | 2005-10-28 09:24:33 +0000 | [diff] [blame] | 88 | unsigned long flags; |
Bernhard Reutner-Fischer | 1732974 | 2005-12-21 15:07:30 +0000 | [diff] [blame] | 89 | flags = bb_getopt_ulflags(argc, argv, "d:", &line); |
Rob Landley | 1b751c8 | 2005-10-28 09:24:33 +0000 | [diff] [blame] | 90 | if (line) |
| 91 | table = bb_xfopen(line, "r"); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 92 | |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 93 | if (optind >= argc || (rootdir=argv[optind])==NULL) { |
Eric Andersen | 2ada89b | 2005-07-18 09:45:35 +0000 | [diff] [blame] | 94 | bb_error_msg_and_die("root directory not specified"); |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Bernhard Reutner-Fischer | d9cf7ac | 2006-04-12 18:39:58 +0000 | [diff] [blame] | 97 | bb_xchdir(rootdir); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 98 | |
| 99 | umask(0); |
| 100 | |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 101 | printf("rootdir=%s\n", rootdir); |
| 102 | if (line) { |
| 103 | printf("table='%s'\n", line); |
| 104 | } else { |
| 105 | printf("table=<stdin>\n"); |
| 106 | } |
| 107 | |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 108 | while ((line = bb_get_chomped_line_from_file(table))) { |
| 109 | char type; |
| 110 | unsigned int mode = 0755; |
| 111 | unsigned int major = 0; |
| 112 | unsigned int minor = 0; |
| 113 | unsigned int count = 0; |
| 114 | unsigned int increment = 0; |
| 115 | unsigned int start = 0; |
| 116 | char name[41]; |
| 117 | char user[41]; |
| 118 | char group[41]; |
| 119 | char *full_name; |
| 120 | uid_t uid; |
| 121 | gid_t gid; |
| 122 | |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 123 | linenum++; |
| 124 | |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 125 | if ((2 > sscanf(line, "%40s %c %o %40s %40s %u %u %u %u %u", name, |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 126 | &type, &mode, user, group, &major, |
| 127 | &minor, &start, &increment, &count)) || |
| 128 | ((major | minor | start | count | increment) > 255)) |
| 129 | { |
| 130 | if (*line=='\0' || *line=='#' || isspace(*line)) |
| 131 | continue; |
| 132 | bb_error_msg("line %d invalid: '%s'\n", linenum, line); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 133 | ret = EXIT_FAILURE; |
| 134 | continue; |
| 135 | } |
| 136 | if (name[0] == '#') { |
| 137 | continue; |
| 138 | } |
Bernhard Reutner-Fischer | 1732974 | 2005-12-21 15:07:30 +0000 | [diff] [blame] | 139 | |
| 140 | gid = (*group) ? get_ug_id(group, bb_xgetgrnam) : getgid(); |
| 141 | uid = (*user) ? get_ug_id(user, bb_xgetpwnam) : getuid(); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 142 | full_name = concat_path_file(rootdir, name); |
| 143 | |
| 144 | if (type == 'd') { |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 145 | bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 146 | if (chown(full_name, uid, gid) == -1) { |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 147 | bb_perror_msg("line %d: chown failed for %s", linenum, full_name); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 148 | ret = EXIT_FAILURE; |
| 149 | goto loop; |
| 150 | } |
Eric Andersen | 90161c9 | 2005-07-18 22:40:59 +0000 | [diff] [blame] | 151 | if ((mode != -1) && (chmod(full_name, mode) < 0)){ |
| 152 | bb_perror_msg("line %d: chmod failed for %s", linenum, full_name); |
| 153 | ret = EXIT_FAILURE; |
| 154 | goto loop; |
| 155 | } |
| 156 | } else if (type == 'f') { |
| 157 | struct stat st; |
| 158 | if ((stat(full_name, &st) < 0 || !S_ISREG(st.st_mode))) { |
| 159 | bb_perror_msg("line %d: regular file '%s' does not exist", linenum, full_name); |
| 160 | ret = EXIT_FAILURE; |
| 161 | goto loop; |
| 162 | } |
| 163 | if (chown(full_name, uid, gid) == -1) { |
| 164 | bb_perror_msg("line %d: chown failed for %s", linenum, full_name); |
| 165 | ret = EXIT_FAILURE; |
| 166 | goto loop; |
| 167 | } |
| 168 | if ((mode != -1) && (chmod(full_name, mode) < 0)){ |
| 169 | bb_perror_msg("line %d: chmod failed for %s", linenum, full_name); |
| 170 | ret = EXIT_FAILURE; |
| 171 | goto loop; |
| 172 | } |
| 173 | } else |
| 174 | { |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 175 | dev_t rdev; |
| 176 | |
| 177 | if (type == 'p') { |
| 178 | mode |= S_IFIFO; |
| 179 | } |
| 180 | else if (type == 'c') { |
| 181 | mode |= S_IFCHR; |
| 182 | } |
| 183 | else if (type == 'b') { |
| 184 | mode |= S_IFBLK; |
| 185 | } else { |
Eric Andersen | 2ada89b | 2005-07-18 09:45:35 +0000 | [diff] [blame] | 186 | bb_error_msg("line %d: unsupported file type %c", linenum, type); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 187 | ret = EXIT_FAILURE; |
| 188 | goto loop; |
| 189 | } |
| 190 | |
| 191 | if (count > 0) { |
| 192 | int i; |
| 193 | char *full_name_inc; |
| 194 | |
| 195 | full_name_inc = xmalloc(strlen(full_name) + 4); |
| 196 | for (i = start; i < count; i++) { |
| 197 | sprintf(full_name_inc, "%s%d", full_name, i); |
| 198 | rdev = (major << 8) + minor + (i * increment - start); |
| 199 | if (mknod(full_name_inc, mode, rdev) == -1) { |
Eric Andersen | 2ada89b | 2005-07-18 09:45:35 +0000 | [diff] [blame] | 200 | bb_perror_msg("line %d: could not create node %s", linenum, full_name_inc); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 201 | ret = EXIT_FAILURE; |
| 202 | } |
| 203 | else if (chown(full_name_inc, uid, gid) == -1) { |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 204 | bb_perror_msg("line %d: chown failed for %s", linenum, full_name_inc); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 205 | ret = EXIT_FAILURE; |
| 206 | } |
Eric Andersen | 90161c9 | 2005-07-18 22:40:59 +0000 | [diff] [blame] | 207 | if ((mode != -1) && (chmod(full_name_inc, mode) < 0)){ |
| 208 | bb_perror_msg("line %d: chmod failed for %s", linenum, full_name_inc); |
| 209 | ret = EXIT_FAILURE; |
| 210 | } |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 211 | } |
| 212 | free(full_name_inc); |
| 213 | } else { |
| 214 | rdev = (major << 8) + minor; |
| 215 | if (mknod(full_name, mode, rdev) == -1) { |
Eric Andersen | 2ada89b | 2005-07-18 09:45:35 +0000 | [diff] [blame] | 216 | bb_perror_msg("line %d: could not create node %s", linenum, full_name); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 217 | ret = EXIT_FAILURE; |
| 218 | } |
| 219 | else if (chown(full_name, uid, gid) == -1) { |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 220 | bb_perror_msg("line %d: chown failed for %s", linenum, full_name); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 221 | ret = EXIT_FAILURE; |
| 222 | } |
Eric Andersen | 90161c9 | 2005-07-18 22:40:59 +0000 | [diff] [blame] | 223 | if ((mode != -1) && (chmod(full_name, mode) < 0)){ |
| 224 | bb_perror_msg("line %d: chmod failed for %s", linenum, full_name); |
| 225 | ret = EXIT_FAILURE; |
| 226 | } |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | loop: |
| 230 | free(line); |
| 231 | free(full_name); |
| 232 | } |
| 233 | fclose(table); |
| 234 | |
Bernhard Reutner-Fischer | 56fbd21 | 2006-04-24 17:41:29 +0000 | [diff] [blame] | 235 | return ret; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 236 | } |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 237 | |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 238 | #else |
Bernhard Reutner-Fischer | 771b186 | 2006-03-24 14:30:05 +0000 | [diff] [blame] | 239 | # error makedevs configuration error, either leaf or table must be selected |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 240 | #endif |