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 | */ |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 9 | //config:config MAKEDEVS |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 10 | //config: bool "makedevs (9.3 kb)" |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: 'makedevs' is a utility used to create a batch of devices with |
| 14 | //config: one command. |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 15 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 16 | //config: There are two choices for command line behaviour, the interface |
| 17 | //config: as used by LEAF/Linux Router Project, or a device table file. |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 18 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 19 | //config: 'leaf' is traditionally what busybox follows, it allows multiple |
| 20 | //config: devices of a particluar type to be created per command. |
| 21 | //config: e.g. /dev/hda[0-9] |
| 22 | //config: Device properties are passed as command line arguments. |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 23 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 24 | //config: 'table' reads device properties from a file or stdin, allowing |
| 25 | //config: a batch of unrelated devices to be made with one command. |
| 26 | //config: User/group names are allowed as an alternative to uid/gid. |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 27 | //config: |
| 28 | //config:choice |
| 29 | //config: prompt "Choose makedevs behaviour" |
| 30 | //config: depends on MAKEDEVS |
| 31 | //config: default FEATURE_MAKEDEVS_TABLE |
| 32 | //config: |
| 33 | //config:config FEATURE_MAKEDEVS_LEAF |
| 34 | //config: bool "leaf" |
| 35 | //config: |
| 36 | //config:config FEATURE_MAKEDEVS_TABLE |
| 37 | //config: bool "table" |
| 38 | //config: |
| 39 | //config:endchoice |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 40 | |
Denys Vlasenko | 9536ef7 | 2017-08-06 21:47:07 +0200 | [diff] [blame] | 41 | //applet:IF_MAKEDEVS(APPLET_NOEXEC(makedevs, makedevs, BB_DIR_SBIN, BB_SUID_DROP, makedevs)) |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 42 | |
| 43 | //kbuild:lib-$(CONFIG_MAKEDEVS) += makedevs.o |
| 44 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 45 | //usage:#if ENABLE_FEATURE_MAKEDEVS_LEAF |
| 46 | //usage:#define makedevs_trivial_usage |
| 47 | //usage: "NAME TYPE MAJOR MINOR FIRST LAST [s]" |
| 48 | //usage:#define makedevs_full_usage "\n\n" |
| 49 | //usage: "Create a range of block or character special files" |
| 50 | //usage: "\n" |
| 51 | //usage: "\nTYPE is:" |
| 52 | //usage: "\n b Block device" |
| 53 | //usage: "\n c Character device" |
| 54 | //usage: "\n f FIFO, MAJOR and MINOR are ignored" |
| 55 | //usage: "\n" |
| 56 | //usage: "\nFIRST..LAST specify numbers appended to NAME." |
| 57 | //usage: "\nIf 's' is the last argument, the base device is created as well." |
| 58 | //usage: "\n" |
| 59 | //usage: "\nExamples:" |
| 60 | //usage: "\n makedevs /dev/ttyS c 4 66 2 63 -> ttyS2-ttyS63" |
| 61 | //usage: "\n makedevs /dev/hda b 3 0 0 8 s -> hda,hda1-hda8" |
| 62 | //usage: |
| 63 | //usage:#define makedevs_example_usage |
| 64 | //usage: "# makedevs /dev/ttyS c 4 66 2 63\n" |
| 65 | //usage: "[creates ttyS2-ttyS63]\n" |
| 66 | //usage: "# makedevs /dev/hda b 3 0 0 8 s\n" |
| 67 | //usage: "[creates hda,hda1-hda8]\n" |
| 68 | //usage:#endif |
| 69 | //usage: |
| 70 | //usage:#if ENABLE_FEATURE_MAKEDEVS_TABLE |
| 71 | //usage:#define makedevs_trivial_usage |
| 72 | //usage: "[-d device_table] rootdir" |
| 73 | //usage:#define makedevs_full_usage "\n\n" |
| 74 | //usage: "Create a range of special files as specified in a device table.\n" |
| 75 | //usage: "Device table entries take the form of:\n" |
Bernhard Reutner-Fischer | 14f5553 | 2011-05-30 23:00:47 +0200 | [diff] [blame] | 76 | //usage: "<name> <type> <mode> <uid> <gid> <major> <minor> <start> <inc> <count>\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 77 | //usage: "Where name is the file name, type can be one of:\n" |
| 78 | //usage: " f Regular file\n" |
| 79 | //usage: " d Directory\n" |
| 80 | //usage: " c Character device\n" |
| 81 | //usage: " b Block device\n" |
| 82 | //usage: " p Fifo (named pipe)\n" |
| 83 | //usage: "uid is the user id for the target file, gid is the group id for the\n" |
| 84 | //usage: "target file. The rest of the entries (major, minor, etc) apply to\n" |
| 85 | //usage: "to device special files. A '-' may be used for blank entries." |
| 86 | //usage: |
| 87 | //usage:#define makedevs_example_usage |
| 88 | //usage: "For example:\n" |
| 89 | //usage: "<name> <type> <mode><uid><gid><major><minor><start><inc><count>\n" |
| 90 | //usage: "/dev d 755 0 0 - - - - -\n" |
| 91 | //usage: "/dev/console c 666 0 0 5 1 - - -\n" |
| 92 | //usage: "/dev/null c 666 0 0 1 3 0 0 -\n" |
| 93 | //usage: "/dev/zero c 666 0 0 1 5 0 0 -\n" |
| 94 | //usage: "/dev/hda b 640 0 0 3 0 0 0 -\n" |
| 95 | //usage: "/dev/hda b 640 0 0 3 1 1 1 15\n\n" |
| 96 | //usage: "Will Produce:\n" |
| 97 | //usage: "/dev\n" |
| 98 | //usage: "/dev/console\n" |
| 99 | //usage: "/dev/null\n" |
| 100 | //usage: "/dev/zero\n" |
| 101 | //usage: "/dev/hda\n" |
| 102 | //usage: "/dev/hda[0-15]\n" |
| 103 | //usage:#endif |
| 104 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 105 | #include "libbb.h" |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 106 | |
Denis Vlasenko | e324184 | 2007-08-13 10:36:25 +0000 | [diff] [blame] | 107 | #if ENABLE_FEATURE_MAKEDEVS_LEAF |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 108 | /* |
| 109 | makedevs NAME TYPE MAJOR MINOR FIRST LAST [s] |
| 110 | TYPEs: |
| 111 | b Block device |
| 112 | c Character device |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 113 | f FIFO |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 114 | |
| 115 | FIRST..LAST specify numbers appended to NAME. |
| 116 | If 's' is the last argument, the base device is created as well. |
| 117 | Examples: |
| 118 | makedevs /dev/ttyS c 4 66 2 63 -> ttyS2-ttyS63 |
| 119 | makedevs /dev/hda b 3 0 0 8 s -> hda,hda1-hda8 |
| 120 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 121 | int makedevs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 122 | int makedevs_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 123 | { |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 124 | mode_t mode; |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 125 | char *basedev, *type, *nodname, *buf; |
Eric Andersen | 4f807a8 | 2004-07-26 09:11:12 +0000 | [diff] [blame] | 126 | int Smajor, Sminor, S, E; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 127 | |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 128 | if (argc < 7 || argv[1][0] == '-') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 129 | bb_show_usage(); |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 130 | |
Eric Andersen | 19732c6 | 2002-04-13 14:26:44 +0000 | [diff] [blame] | 131 | basedev = argv[1]; |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 132 | buf = xasprintf("%s%u", argv[1], (unsigned)-1); |
Eric Andersen | 19732c6 | 2002-04-13 14:26:44 +0000 | [diff] [blame] | 133 | type = argv[2]; |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 134 | Smajor = xatoi_positive(argv[3]); |
| 135 | Sminor = xatoi_positive(argv[4]); |
| 136 | S = xatoi_positive(argv[5]); |
| 137 | E = xatoi_positive(argv[6]); |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 138 | nodname = argv[7] ? basedev : buf; |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 139 | |
| 140 | mode = 0660; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 141 | switch (type[0]) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 142 | case 'c': |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 143 | mode |= S_IFCHR; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 144 | break; |
| 145 | case 'b': |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 146 | mode |= S_IFBLK; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 147 | break; |
| 148 | case 'f': |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 149 | mode |= S_IFIFO; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 150 | break; |
| 151 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 152 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 153 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 154 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 155 | while (S <= E) { |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 156 | sprintf(buf, "%s%u", basedev, S); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 157 | |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 158 | /* if mode != S_IFCHR and != S_IFBLK, |
| 159 | * third param in mknod() ignored */ |
Denys Vlasenko | 2c769c6 | 2016-11-27 23:27:54 +0100 | [diff] [blame] | 160 | if (mknod(nodname, mode, makedev(Smajor, Sminor)) != 0 |
| 161 | && errno != EEXIST |
| 162 | ) { |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 163 | bb_perror_msg("can't create '%s'", nodname); |
Denys Vlasenko | 2c769c6 | 2016-11-27 23:27:54 +0100 | [diff] [blame] | 164 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 165 | |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 166 | /*if (nodname == basedev)*/ /* ex. /dev/hda - to /dev/hda1 ... */ |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 167 | nodname = buf; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 168 | S++; |
| 169 | Sminor++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 172 | return 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Denis Vlasenko | e324184 | 2007-08-13 10:36:25 +0000 | [diff] [blame] | 175 | #elif ENABLE_FEATURE_MAKEDEVS_TABLE |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 176 | |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 177 | /* Licensed under GPLv2 or later, see file LICENSE in this source tree. */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 178 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 179 | int makedevs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 180 | int makedevs_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 181 | { |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 182 | parser_t *parser; |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 183 | char *line = (char *)"-"; |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 184 | int ret = EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 185 | |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 186 | getopt32(argv, "^" "d:" "\0" "=1", &line); |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 187 | argv += optind; |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 188 | |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 189 | xchdir(*argv); /* ensure root dir exists */ |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 190 | |
| 191 | umask(0); |
| 192 | |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 193 | printf("rootdir=%s\ntable=", *argv); |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 194 | if (NOT_LONE_DASH(line)) { |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 195 | printf("'%s'\n", line); |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 196 | } else { |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 197 | puts("<stdin>"); |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 200 | parser = config_open(line); |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 201 | while (config_read(parser, &line, 1, 1, "# \t", PARSE_NORMAL)) { |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 202 | int linenum; |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 203 | char type; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 204 | unsigned mode = 0755; |
| 205 | unsigned major = 0; |
| 206 | unsigned minor = 0; |
| 207 | unsigned count = 0; |
| 208 | unsigned increment = 0; |
| 209 | unsigned start = 0; |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 210 | char user[41]; |
| 211 | char group[41]; |
Denys Vlasenko | 3532e60 | 2017-07-06 02:04:32 +0200 | [diff] [blame] | 212 | char *full_name; |
| 213 | int name_len; |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 214 | uid_t uid; |
| 215 | gid_t gid; |
| 216 | |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 217 | linenum = parser->lineno; |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 218 | |
Denys Vlasenko | 3532e60 | 2017-07-06 02:04:32 +0200 | [diff] [blame] | 219 | if ((1 > sscanf(line, "%*s%n %c %o %40s %40s %u %u %u %u %u", |
| 220 | &name_len, &type, &mode, user, group, |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 221 | &major, &minor, &start, &increment, &count)) |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 222 | || ((unsigned)(major | minor | start | count | increment) > 255) |
| 223 | ) { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 224 | bb_error_msg("invalid line %d: '%s'", linenum, line); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 225 | ret = EXIT_FAILURE; |
| 226 | continue; |
| 227 | } |
Bernhard Reutner-Fischer | 1732974 | 2005-12-21 15:07:30 +0000 | [diff] [blame] | 228 | |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 229 | gid = (*group) ? get_ug_id(group, xgroup2gid) : getgid(); |
| 230 | uid = (*user) ? get_ug_id(user, xuname2uid) : getuid(); |
Denys Vlasenko | 3532e60 | 2017-07-06 02:04:32 +0200 | [diff] [blame] | 231 | line[name_len] = '\0'; |
| 232 | full_name = line; |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 233 | /* We are already in the right root dir, |
| 234 | * so make absolute paths relative */ |
Denys Vlasenko | 3532e60 | 2017-07-06 02:04:32 +0200 | [diff] [blame] | 235 | if ('/' == full_name[0]) |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 236 | full_name++; |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 237 | |
| 238 | if (type == 'd') { |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 239 | bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 240 | if (chown(full_name, uid, gid) == -1) { |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 241 | chown_fail: |
| 242 | bb_perror_msg("line %d: can't chown %s", linenum, full_name); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 243 | ret = EXIT_FAILURE; |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 244 | continue; |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 245 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 246 | if (chmod(full_name, mode) < 0) { |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 247 | chmod_fail: |
| 248 | bb_perror_msg("line %d: can't chmod %s", linenum, full_name); |
Eric Andersen | 90161c9 | 2005-07-18 22:40:59 +0000 | [diff] [blame] | 249 | ret = EXIT_FAILURE; |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 250 | continue; |
Eric Andersen | 90161c9 | 2005-07-18 22:40:59 +0000 | [diff] [blame] | 251 | } |
| 252 | } else if (type == 'f') { |
| 253 | struct stat st; |
| 254 | if ((stat(full_name, &st) < 0 || !S_ISREG(st.st_mode))) { |
| 255 | bb_perror_msg("line %d: regular file '%s' does not exist", linenum, full_name); |
| 256 | ret = EXIT_FAILURE; |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 257 | continue; |
Eric Andersen | 90161c9 | 2005-07-18 22:40:59 +0000 | [diff] [blame] | 258 | } |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 259 | if (chown(full_name, uid, gid) < 0) |
| 260 | goto chown_fail; |
| 261 | if (chmod(full_name, mode) < 0) |
| 262 | goto chmod_fail; |
Denis Vlasenko | e324184 | 2007-08-13 10:36:25 +0000 | [diff] [blame] | 263 | } else { |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 264 | unsigned i; |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 265 | |
| 266 | if (type == 'p') { |
| 267 | mode |= S_IFIFO; |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 268 | } else if (type == 'c') { |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 269 | mode |= S_IFCHR; |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 270 | } else if (type == 'b') { |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 271 | mode |= S_IFBLK; |
| 272 | } else { |
Eric Andersen | 2ada89b | 2005-07-18 09:45:35 +0000 | [diff] [blame] | 273 | bb_error_msg("line %d: unsupported file type %c", linenum, type); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 274 | ret = EXIT_FAILURE; |
Denis Vlasenko | b2dc913 | 2008-08-03 22:14:02 +0000 | [diff] [blame] | 275 | continue; |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Denys Vlasenko | 3c9688e | 2017-07-06 02:17:24 +0200 | [diff] [blame] | 278 | if (count != 0) |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 279 | count--; |
Denys Vlasenko | 3c9688e | 2017-07-06 02:17:24 +0200 | [diff] [blame] | 280 | for (i = 0; i <= count; i++) { |
| 281 | dev_t rdev; |
| 282 | char *nameN = full_name; |
| 283 | if (count != 0) |
| 284 | nameN = xasprintf("%s%u", full_name, start + i); |
| 285 | rdev = makedev(major, minor + i * increment); |
| 286 | if (mknod(nameN, mode, rdev) != 0 |
Denys Vlasenko | 1d3a04a | 2016-11-28 01:22:57 +0100 | [diff] [blame] | 287 | && errno != EEXIST |
Denys Vlasenko | 2c769c6 | 2016-11-27 23:27:54 +0100 | [diff] [blame] | 288 | ) { |
Denys Vlasenko | 3c9688e | 2017-07-06 02:17:24 +0200 | [diff] [blame] | 289 | bb_perror_msg("line %d: can't create node %s", linenum, nameN); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 290 | ret = EXIT_FAILURE; |
Denys Vlasenko | 3c9688e | 2017-07-06 02:17:24 +0200 | [diff] [blame] | 291 | } else if (chown(nameN, uid, gid) < 0) { |
| 292 | bb_perror_msg("line %d: can't chown %s", linenum, nameN); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 293 | ret = EXIT_FAILURE; |
Denys Vlasenko | 3c9688e | 2017-07-06 02:17:24 +0200 | [diff] [blame] | 294 | } else if (chmod(nameN, mode) < 0) { |
| 295 | bb_perror_msg("line %d: can't chmod %s", linenum, nameN); |
Eric Andersen | 90161c9 | 2005-07-18 22:40:59 +0000 | [diff] [blame] | 296 | ret = EXIT_FAILURE; |
| 297 | } |
Denys Vlasenko | 3c9688e | 2017-07-06 02:17:24 +0200 | [diff] [blame] | 298 | if (count != 0) |
| 299 | free(nameN); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 300 | } |
| 301 | } |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 302 | } |
Denis Vlasenko | e1fa817 | 2008-08-01 02:15:05 +0000 | [diff] [blame] | 303 | if (ENABLE_FEATURE_CLEAN_UP) |
| 304 | config_close(parser); |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 305 | |
Bernhard Reutner-Fischer | 56fbd21 | 2006-04-24 17:41:29 +0000 | [diff] [blame] | 306 | return ret; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 307 | } |
Eric Andersen | e8614db | 2005-07-18 09:28:36 +0000 | [diff] [blame] | 308 | |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 309 | #else |
Bernhard Reutner-Fischer | 771b186 | 2006-03-24 14:30:05 +0000 | [diff] [blame] | 310 | # error makedevs configuration error, either leaf or table must be selected |
Eric Andersen | 3d92562 | 2005-06-09 10:16:02 +0000 | [diff] [blame] | 311 | #endif |