Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
| 6 | #include <fcntl.h> |
| 7 | #include <getopt.h> |
| 8 | #include <time.h> |
| 9 | #include <unistd.h> |
| 10 | #include <sys/types.h> |
| 11 | #include <sys/sysmacros.h> /* major() and minor() */ |
| 12 | #include "busybox.h" |
| 13 | |
| 14 | #ifdef CONFIG_FEATURE_MAKEDEVS_LEAF |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 15 | /* |
| 16 | * public domain -- Dave 'Kill a Cop' Cinege <dcinege@psychosis.com> |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 17 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 18 | * makedevs |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 19 | * Make ranges of device files quickly. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 20 | * known bugs: can't deal with alpha ranges |
| 21 | */ |
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 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 78 | /* |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 79 | * This program is free software; you can redistribute it and/or modify |
| 80 | * it under the terms of the GNU General Public License version 2 as |
| 81 | * published by the Free Software Foundation. |
| 82 | * |
| 83 | * This program is distributed in the hope that it will be useful, |
| 84 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 85 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 86 | * GNU Library General Public License for more details. |
| 87 | * |
| 88 | * You should have received a copy of the GNU General Public License |
| 89 | * along with this program; if not, write to the Free Software |
| 90 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 91 | * |
| 92 | */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 93 | |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 94 | extern int makedevs_main(int argc, char **argv) |
| 95 | { |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 96 | int opt; |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame^] | 97 | FILE *table = stdin; |
| 98 | char *rootdir = NULL; |
| 99 | char *line = NULL; |
| 100 | int linenum = 0; |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 101 | int ret = EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 102 | |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame^] | 103 | while ((opt = getopt(argc, argv, "d:")) != -1) { |
| 104 | switch(opt) { |
| 105 | case 'd': |
| 106 | table = bb_xfopen((line=optarg), "r"); |
| 107 | break; |
| 108 | default: |
| 109 | bb_show_usage(); |
| 110 | } |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame^] | 113 | if (optind >= argc || (rootdir=argv[optind])==NULL) { |
| 114 | bb_error_msg_and_die("root directory not speficied"); |
| 115 | } |
| 116 | |
| 117 | if (chdir(rootdir) != 0) { |
| 118 | bb_perror_msg_and_die("Couldnt chdir to %s", rootdir); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | umask(0); |
| 122 | |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame^] | 123 | printf("rootdir=%s\n", rootdir); |
| 124 | if (line) { |
| 125 | printf("table='%s'\n", line); |
| 126 | } else { |
| 127 | printf("table=<stdin>\n"); |
| 128 | } |
| 129 | |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 130 | while ((line = bb_get_chomped_line_from_file(table))) { |
| 131 | char type; |
| 132 | unsigned int mode = 0755; |
| 133 | unsigned int major = 0; |
| 134 | unsigned int minor = 0; |
| 135 | unsigned int count = 0; |
| 136 | unsigned int increment = 0; |
| 137 | unsigned int start = 0; |
| 138 | char name[41]; |
| 139 | char user[41]; |
| 140 | char group[41]; |
| 141 | char *full_name; |
| 142 | uid_t uid; |
| 143 | gid_t gid; |
| 144 | |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame^] | 145 | linenum++; |
| 146 | |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 147 | 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^] | 148 | &type, &mode, user, group, &major, |
| 149 | &minor, &start, &increment, &count)) || |
| 150 | ((major | minor | start | count | increment) > 255)) |
| 151 | { |
| 152 | if (*line=='\0' || *line=='#' || isspace(*line)) |
| 153 | continue; |
| 154 | bb_error_msg("line %d invalid: '%s'\n", linenum, line); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 155 | ret = EXIT_FAILURE; |
| 156 | continue; |
| 157 | } |
| 158 | if (name[0] == '#') { |
| 159 | continue; |
| 160 | } |
| 161 | if (group) { |
| 162 | gid = get_ug_id(group, my_getgrnam); |
| 163 | } else { |
| 164 | gid = getgid(); |
| 165 | } |
| 166 | if (user) { |
| 167 | uid = get_ug_id(user, my_getpwnam); |
| 168 | } else { |
| 169 | uid = getuid(); |
| 170 | } |
| 171 | full_name = concat_path_file(rootdir, name); |
| 172 | |
| 173 | if (type == 'd') { |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame^] | 174 | bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 175 | if (chown(full_name, uid, gid) == -1) { |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame^] | 176 | bb_perror_msg("line %d: chown failed for %s", linenum, full_name); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 177 | ret = EXIT_FAILURE; |
| 178 | goto loop; |
| 179 | } |
| 180 | } else { |
| 181 | dev_t rdev; |
| 182 | |
| 183 | if (type == 'p') { |
| 184 | mode |= S_IFIFO; |
| 185 | } |
| 186 | else if (type == 'c') { |
| 187 | mode |= S_IFCHR; |
| 188 | } |
| 189 | else if (type == 'b') { |
| 190 | mode |= S_IFBLK; |
| 191 | } else { |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame^] | 192 | bb_error_msg("line %d: Unsupported file type %c", linenum, type); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 193 | ret = EXIT_FAILURE; |
| 194 | goto loop; |
| 195 | } |
| 196 | |
| 197 | if (count > 0) { |
| 198 | int i; |
| 199 | char *full_name_inc; |
| 200 | |
| 201 | full_name_inc = xmalloc(strlen(full_name) + 4); |
| 202 | for (i = start; i < count; i++) { |
| 203 | sprintf(full_name_inc, "%s%d", full_name, i); |
| 204 | rdev = (major << 8) + minor + (i * increment - start); |
| 205 | if (mknod(full_name_inc, mode, rdev) == -1) { |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame^] | 206 | bb_perror_msg("line %d: Couldnt create node %s", linenum, full_name_inc); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 207 | ret = EXIT_FAILURE; |
| 208 | } |
| 209 | else if (chown(full_name_inc, uid, gid) == -1) { |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame^] | 210 | 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] | 211 | ret = EXIT_FAILURE; |
| 212 | } |
| 213 | } |
| 214 | free(full_name_inc); |
| 215 | } else { |
| 216 | rdev = (major << 8) + minor; |
| 217 | if (mknod(full_name, mode, rdev) == -1) { |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame^] | 218 | bb_perror_msg("line %d: Couldnt create node %s", linenum, full_name); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 219 | ret = EXIT_FAILURE; |
| 220 | } |
| 221 | else if (chown(full_name, uid, gid) == -1) { |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame^] | 222 | bb_perror_msg("line %d: chown failed for %s", linenum, full_name); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 223 | ret = EXIT_FAILURE; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | loop: |
| 228 | free(line); |
| 229 | free(full_name); |
| 230 | } |
| 231 | fclose(table); |
| 232 | |
| 233 | return 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 234 | } |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame^] | 235 | |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 236 | #else |
| 237 | # error makdedevs configuration error, either leaf or table must be selected |
| 238 | #endif |