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 | |
Bernhard Reutner-Fischer | c89982d | 2006-06-03 19:49:21 +0000 | [diff] [blame] | 10 | #include "busybox.h" |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 11 | |
| 12 | #ifdef CONFIG_FEATURE_MAKEDEVS_LEAF |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 13 | int makedevs_main(int argc, char **argv); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 14 | int makedevs_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 15 | { |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 16 | mode_t mode; |
| 17 | char *basedev, *type, *nodname, buf[255]; |
Eric Andersen | 4f807a8 | 2004-07-26 09:11:12 +0000 | [diff] [blame] | 18 | int Smajor, Sminor, S, E; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 19 | |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 20 | if (argc < 7 || *argv[1]=='-') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 21 | bb_show_usage(); |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 22 | |
Eric Andersen | 19732c6 | 2002-04-13 14:26:44 +0000 | [diff] [blame] | 23 | basedev = argv[1]; |
| 24 | type = argv[2]; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 25 | Smajor = xatoi_u(argv[3]); |
| 26 | Sminor = xatoi_u(argv[4]); |
| 27 | S = xatoi_u(argv[5]); |
| 28 | E = xatoi_u(argv[6]); |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 29 | nodname = argc == 8 ? basedev : buf; |
| 30 | |
| 31 | mode = 0660; |
Eric Andersen | 19732c6 | 2002-04-13 14:26:44 +0000 | [diff] [blame] | 32 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 33 | switch (type[0]) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 34 | case 'c': |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 35 | mode |= S_IFCHR; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 36 | break; |
| 37 | case 'b': |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 38 | mode |= S_IFBLK; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 39 | break; |
| 40 | case 'f': |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 41 | mode |= S_IFIFO; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 42 | break; |
| 43 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 44 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 45 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 46 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 47 | while (S <= E) { |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 48 | int sz; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 49 | |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 50 | sz = snprintf(buf, sizeof(buf), "%s%d", basedev, S); |
| 51 | if(sz<0 || sz>=sizeof(buf)) /* libc different */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 52 | bb_error_msg_and_die("%s too large", basedev); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 53 | |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 54 | /* if mode != S_IFCHR and != S_IFBLK third param in mknod() ignored */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 55 | |
Eric Andersen | e71e760 | 2004-07-30 14:36:37 +0000 | [diff] [blame] | 56 | if (mknod(nodname, mode, makedev(Smajor, Sminor))) |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 57 | bb_error_msg("failed to create: %s", nodname); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 58 | |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 59 | if (nodname == basedev) /* ex. /dev/hda - to /dev/hda1 ... */ |
| 60 | nodname = buf; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 61 | S++; |
| 62 | Sminor++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 65 | return 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 68 | #elif defined CONFIG_FEATURE_MAKEDEVS_TABLE |
| 69 | |
Rob Landley | 1b751c8 | 2005-10-28 09:24:33 +0000 | [diff] [blame] | 70 | /* 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] | 71 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 72 | int makedevs_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 73 | int makedevs_main(int argc, char **argv) |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 74 | { |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 75 | FILE *table = stdin; |
| 76 | char *rootdir = NULL; |
| 77 | char *line = NULL; |
| 78 | int linenum = 0; |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 79 | int ret = EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 80 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 81 | getopt32(argc, argv, "d:", &line); |
Rob Landley | 1b751c8 | 2005-10-28 09:24:33 +0000 | [diff] [blame] | 82 | if (line) |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 83 | table = xfopen(line, "r"); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 84 | |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 85 | if (optind >= argc || (rootdir=argv[optind])==NULL) { |
Eric Andersen | 2ada89b | 2005-07-18 09:45:35 +0000 | [diff] [blame] | 86 | bb_error_msg_and_die("root directory not specified"); |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 89 | xchdir(rootdir); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 90 | |
| 91 | umask(0); |
| 92 | |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 93 | printf("rootdir=%s\n", rootdir); |
| 94 | if (line) { |
| 95 | printf("table='%s'\n", line); |
| 96 | } else { |
| 97 | printf("table=<stdin>\n"); |
| 98 | } |
| 99 | |
Denis Vlasenko | 2d5ca60 | 2006-10-12 22:43:20 +0000 | [diff] [blame] | 100 | while ((line = xmalloc_getline(table))) { |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 101 | 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 Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 115 | linenum++; |
| 116 | |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 117 | 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] | 118 | &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 Vlasenko | 6d655be | 2006-09-06 19:02:46 +0000 | [diff] [blame] | 124 | bb_error_msg("line %d invalid: '%s'", linenum, line); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 125 | ret = EXIT_FAILURE; |
| 126 | continue; |
| 127 | } |
| 128 | if (name[0] == '#') { |
| 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(); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 134 | full_name = concat_path_file(rootdir, name); |
| 135 | |
| 136 | if (type == 'd') { |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 137 | bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 138 | if (chown(full_name, uid, gid) == -1) { |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 139 | bb_perror_msg("line %d: chown failed for %s", linenum, full_name); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 140 | ret = EXIT_FAILURE; |
| 141 | goto loop; |
| 142 | } |
Eric Andersen | 90161c9 | 2005-07-18 22:40:59 +0000 | [diff] [blame] | 143 | 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 | } |
| 165 | } else |
| 166 | { |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 167 | dev_t rdev; |
| 168 | |
| 169 | if (type == 'p') { |
| 170 | mode |= S_IFIFO; |
| 171 | } |
| 172 | else if (type == 'c') { |
| 173 | mode |= S_IFCHR; |
| 174 | } |
| 175 | else if (type == 'b') { |
| 176 | mode |= S_IFBLK; |
| 177 | } else { |
Eric Andersen | 2ada89b | 2005-07-18 09:45:35 +0000 | [diff] [blame] | 178 | bb_error_msg("line %d: unsupported file type %c", linenum, type); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 179 | ret = EXIT_FAILURE; |
| 180 | goto loop; |
| 181 | } |
| 182 | |
| 183 | if (count > 0) { |
| 184 | int i; |
| 185 | char *full_name_inc; |
| 186 | |
| 187 | full_name_inc = xmalloc(strlen(full_name) + 4); |
| 188 | for (i = start; i < count; i++) { |
| 189 | sprintf(full_name_inc, "%s%d", full_name, i); |
Bernhard Reutner-Fischer | 7b1f236 | 2007-01-21 11:20:01 +0000 | [diff] [blame] | 190 | rdev = makedev(major, minor + (i * increment - start)); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 191 | if (mknod(full_name_inc, mode, rdev) == -1) { |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 192 | bb_perror_msg("line %d: cannot create node %s", linenum, full_name_inc); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 193 | ret = EXIT_FAILURE; |
| 194 | } |
| 195 | else if (chown(full_name_inc, uid, gid) == -1) { |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 196 | 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] | 197 | ret = EXIT_FAILURE; |
| 198 | } |
Eric Andersen | 90161c9 | 2005-07-18 22:40:59 +0000 | [diff] [blame] | 199 | if ((mode != -1) && (chmod(full_name_inc, mode) < 0)){ |
| 200 | bb_perror_msg("line %d: chmod failed for %s", linenum, full_name_inc); |
| 201 | ret = EXIT_FAILURE; |
| 202 | } |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 203 | } |
| 204 | free(full_name_inc); |
| 205 | } else { |
Bernhard Reutner-Fischer | 7b1f236 | 2007-01-21 11:20:01 +0000 | [diff] [blame] | 206 | rdev = makedev(major, minor); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 207 | if (mknod(full_name, mode, rdev) == -1) { |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 208 | bb_perror_msg("line %d: cannot create node %s", linenum, full_name); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 209 | ret = EXIT_FAILURE; |
| 210 | } |
| 211 | else if (chown(full_name, uid, gid) == -1) { |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 212 | bb_perror_msg("line %d: chown failed for %s", linenum, full_name); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 213 | ret = EXIT_FAILURE; |
| 214 | } |
Eric Andersen | 90161c9 | 2005-07-18 22:40:59 +0000 | [diff] [blame] | 215 | if ((mode != -1) && (chmod(full_name, mode) < 0)){ |
| 216 | bb_perror_msg("line %d: chmod failed for %s", linenum, full_name); |
| 217 | ret = EXIT_FAILURE; |
| 218 | } |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | loop: |
| 222 | free(line); |
| 223 | free(full_name); |
| 224 | } |
| 225 | fclose(table); |
| 226 | |
Bernhard Reutner-Fischer | 56fbd21 | 2006-04-24 17:41:29 +0000 | [diff] [blame] | 227 | return ret; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 228 | } |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 229 | |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 230 | #else |
Bernhard Reutner-Fischer | 771b186 | 2006-03-24 14:30:05 +0000 | [diff] [blame] | 231 | # error makedevs configuration error, either leaf or table must be selected |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 232 | #endif |