Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 17 | #include <ctype.h> |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame^] | 18 | #include <dirent.h> |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 19 | #include <errno.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
| 21 | #include <inttypes.h> |
| 22 | #include <stdarg.h> |
| 23 | #include <stddef.h> |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 26 | #include <string.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 28 | |
| 29 | #include "init.h" |
| 30 | #include "parser.h" |
| 31 | #include "init_parser.h" |
| 32 | #include "log.h" |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 33 | #include "property_service.h" |
| 34 | #include "util.h" |
| 35 | |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame^] | 36 | #include <base/stringprintf.h> |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 37 | #include <cutils/iosched_policy.h> |
Dima Zavin | da04c52 | 2011-09-01 17:09:44 -0700 | [diff] [blame] | 38 | #include <cutils/list.h> |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 39 | |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 40 | static list_declare(service_list); |
| 41 | static list_declare(action_list); |
| 42 | static list_declare(action_queue); |
| 43 | |
Dima Zavin | 78a1b1f | 2011-12-20 12:53:48 -0800 | [diff] [blame] | 44 | struct import { |
| 45 | struct listnode list; |
| 46 | const char *filename; |
| 47 | }; |
| 48 | |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 49 | static void *parse_service(struct parse_state *state, int nargs, char **args); |
| 50 | static void parse_line_service(struct parse_state *state, int nargs, char **args); |
| 51 | |
| 52 | static void *parse_action(struct parse_state *state, int nargs, char **args); |
| 53 | static void parse_line_action(struct parse_state *state, int nargs, char **args); |
| 54 | |
| 55 | #define SECTION 0x01 |
| 56 | #define COMMAND 0x02 |
| 57 | #define OPTION 0x04 |
| 58 | |
| 59 | #include "keywords.h" |
| 60 | |
| 61 | #define KEYWORD(symbol, flags, nargs, func) \ |
| 62 | [ K_##symbol ] = { #symbol, func, nargs + 1, flags, }, |
| 63 | |
Greg Hackmann | d68db71 | 2013-11-18 09:44:20 -0800 | [diff] [blame] | 64 | static struct { |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 65 | const char *name; |
| 66 | int (*func)(int nargs, char **args); |
| 67 | unsigned char nargs; |
| 68 | unsigned char flags; |
| 69 | } keyword_info[KEYWORD_COUNT] = { |
| 70 | [ K_UNKNOWN ] = { "unknown", 0, 0, 0 }, |
| 71 | #include "keywords.h" |
| 72 | }; |
| 73 | #undef KEYWORD |
| 74 | |
| 75 | #define kw_is(kw, type) (keyword_info[kw].flags & (type)) |
| 76 | #define kw_name(kw) (keyword_info[kw].name) |
| 77 | #define kw_func(kw) (keyword_info[kw].func) |
| 78 | #define kw_nargs(kw) (keyword_info[kw].nargs) |
| 79 | |
Elliott Hughes | c0e919c | 2015-02-04 14:46:36 -0800 | [diff] [blame] | 80 | void dump_parser_state() { |
| 81 | if (false) { |
| 82 | struct listnode* node; |
| 83 | list_for_each(node, &service_list) { |
| 84 | service* svc = node_to_item(node, struct service, slist); |
| 85 | INFO("service %s\n", svc->name); |
| 86 | INFO(" class '%s'\n", svc->classname); |
| 87 | INFO(" exec"); |
| 88 | for (int n = 0; n < svc->nargs; n++) { |
| 89 | INFO(" '%s'", svc->args[n]); |
| 90 | } |
| 91 | INFO("\n"); |
| 92 | for (socketinfo* si = svc->sockets; si; si = si->next) { |
| 93 | INFO(" socket %s %s 0%o\n", si->name, si->type, si->perm); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | list_for_each(node, &action_list) { |
| 98 | action* act = node_to_item(node, struct action, alist); |
| 99 | INFO("on "); |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 100 | std::string trigger_name = build_triggers_string(act); |
| 101 | INFO("%s", trigger_name.c_str()); |
Elliott Hughes | c0e919c | 2015-02-04 14:46:36 -0800 | [diff] [blame] | 102 | INFO("\n"); |
| 103 | |
| 104 | struct listnode* node2; |
| 105 | list_for_each(node2, &act->commands) { |
| 106 | command* cmd = node_to_item(node2, struct command, clist); |
| 107 | INFO(" %p", cmd->func); |
| 108 | for (int n = 0; n < cmd->nargs; n++) { |
| 109 | INFO(" %s", cmd->args[n]); |
| 110 | } |
| 111 | INFO("\n"); |
| 112 | } |
| 113 | INFO("\n"); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
Greg Hackmann | d68db71 | 2013-11-18 09:44:20 -0800 | [diff] [blame] | 118 | static int lookup_keyword(const char *s) |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 119 | { |
| 120 | switch (*s++) { |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 121 | case 'b': |
| 122 | if (!strcmp(s, "ootchart_init")) return K_bootchart_init; |
Mark Salyzyn | 7a3d66c | 2015-03-24 07:29:22 -0700 | [diff] [blame] | 123 | break; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 124 | case 'c': |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 125 | if (!strcmp(s, "opy")) return K_copy; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 126 | if (!strcmp(s, "lass")) return K_class; |
| 127 | if (!strcmp(s, "lass_start")) return K_class_start; |
| 128 | if (!strcmp(s, "lass_stop")) return K_class_stop; |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 129 | if (!strcmp(s, "lass_reset")) return K_class_reset; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 130 | if (!strcmp(s, "onsole")) return K_console; |
| 131 | if (!strcmp(s, "hown")) return K_chown; |
| 132 | if (!strcmp(s, "hmod")) return K_chmod; |
| 133 | if (!strcmp(s, "ritical")) return K_critical; |
| 134 | break; |
| 135 | case 'd': |
| 136 | if (!strcmp(s, "isabled")) return K_disabled; |
| 137 | if (!strcmp(s, "omainname")) return K_domainname; |
| 138 | break; |
| 139 | case 'e': |
JP Abgrall | 3beec7e | 2014-05-02 21:14:29 -0700 | [diff] [blame] | 140 | if (!strcmp(s, "nable")) return K_enable; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 141 | if (!strcmp(s, "xec")) return K_exec; |
| 142 | if (!strcmp(s, "xport")) return K_export; |
| 143 | break; |
| 144 | case 'g': |
| 145 | if (!strcmp(s, "roup")) return K_group; |
| 146 | break; |
| 147 | case 'h': |
| 148 | if (!strcmp(s, "ostname")) return K_hostname; |
| 149 | break; |
| 150 | case 'i': |
| 151 | if (!strcmp(s, "oprio")) return K_ioprio; |
| 152 | if (!strcmp(s, "fup")) return K_ifup; |
| 153 | if (!strcmp(s, "nsmod")) return K_insmod; |
| 154 | if (!strcmp(s, "mport")) return K_import; |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 155 | if (!strcmp(s, "nstallkey")) return K_installkey; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 156 | break; |
| 157 | case 'k': |
| 158 | if (!strcmp(s, "eycodes")) return K_keycodes; |
| 159 | break; |
| 160 | case 'l': |
| 161 | if (!strcmp(s, "oglevel")) return K_loglevel; |
Ken Sumrall | c5c5103 | 2011-03-08 17:01:29 -0800 | [diff] [blame] | 162 | if (!strcmp(s, "oad_persist_props")) return K_load_persist_props; |
Riley Andrews | e4b7b29 | 2014-06-16 15:06:21 -0700 | [diff] [blame] | 163 | if (!strcmp(s, "oad_all_props")) return K_load_all_props; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 164 | break; |
| 165 | case 'm': |
| 166 | if (!strcmp(s, "kdir")) return K_mkdir; |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 167 | if (!strcmp(s, "ount_all")) return K_mount_all; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 168 | if (!strcmp(s, "ount")) return K_mount; |
| 169 | break; |
| 170 | case 'o': |
| 171 | if (!strcmp(s, "n")) return K_on; |
| 172 | if (!strcmp(s, "neshot")) return K_oneshot; |
| 173 | if (!strcmp(s, "nrestart")) return K_onrestart; |
| 174 | break; |
Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 175 | case 'p': |
| 176 | if (!strcmp(s, "owerctl")) return K_powerctl; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 177 | break; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 178 | case 'r': |
| 179 | if (!strcmp(s, "estart")) return K_restart; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 180 | if (!strcmp(s, "estorecon")) return K_restorecon; |
Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 181 | if (!strcmp(s, "estorecon_recursive")) return K_restorecon_recursive; |
Ken Sumrall | 203bad5 | 2011-01-18 17:37:41 -0800 | [diff] [blame] | 182 | if (!strcmp(s, "mdir")) return K_rmdir; |
| 183 | if (!strcmp(s, "m")) return K_rm; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 184 | break; |
| 185 | case 's': |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 186 | if (!strcmp(s, "eclabel")) return K_seclabel; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 187 | if (!strcmp(s, "ervice")) return K_service; |
| 188 | if (!strcmp(s, "etenv")) return K_setenv; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 189 | if (!strcmp(s, "etprop")) return K_setprop; |
| 190 | if (!strcmp(s, "etrlimit")) return K_setrlimit; |
| 191 | if (!strcmp(s, "ocket")) return K_socket; |
| 192 | if (!strcmp(s, "tart")) return K_start; |
| 193 | if (!strcmp(s, "top")) return K_stop; |
Ken Sumrall | a76baaa | 2013-07-09 18:42:09 -0700 | [diff] [blame] | 194 | if (!strcmp(s, "wapon_all")) return K_swapon_all; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 195 | if (!strcmp(s, "ymlink")) return K_symlink; |
| 196 | if (!strcmp(s, "ysclktz")) return K_sysclktz; |
| 197 | break; |
| 198 | case 't': |
| 199 | if (!strcmp(s, "rigger")) return K_trigger; |
| 200 | break; |
| 201 | case 'u': |
| 202 | if (!strcmp(s, "ser")) return K_user; |
| 203 | break; |
Sami Tolvanen | 8ff0190 | 2015-02-16 11:03:34 +0000 | [diff] [blame] | 204 | case 'v': |
| 205 | if (!strcmp(s, "erity_load_state")) return K_verity_load_state; |
Sami Tolvanen | acbf9be | 2015-03-19 10:00:34 +0000 | [diff] [blame] | 206 | if (!strcmp(s, "erity_update_state")) return K_verity_update_state; |
Sami Tolvanen | 8ff0190 | 2015-02-16 11:03:34 +0000 | [diff] [blame] | 207 | break; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 208 | case 'w': |
| 209 | if (!strcmp(s, "rite")) return K_write; |
Elliott Hughes | d62f060 | 2015-06-12 18:02:20 -0700 | [diff] [blame] | 210 | if (!strcmp(s, "ritepid")) return K_writepid; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 211 | if (!strcmp(s, "ait")) return K_wait; |
| 212 | break; |
| 213 | } |
| 214 | return K_UNKNOWN; |
| 215 | } |
| 216 | |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 217 | static void parse_line_no_op(struct parse_state*, int, char**) { |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 220 | int expand_props(const char *src, std::string *dst) { |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 221 | const char *src_ptr = src; |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 222 | |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 223 | if (!src || !dst) { |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 224 | return -1; |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 225 | } |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 226 | |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 227 | /* - variables can either be $x.y or ${x.y}, in case they are only part |
| 228 | * of the string. |
| 229 | * - will accept $$ as a literal $. |
| 230 | * - no nested property expansion, i.e. ${foo.${bar}} is not supported, |
| 231 | * bad things will happen |
| 232 | */ |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 233 | while (*src_ptr) { |
| 234 | const char *c; |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 235 | |
| 236 | c = strchr(src_ptr, '$'); |
| 237 | if (!c) { |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 238 | dst->append(src_ptr); |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 239 | break; |
| 240 | } |
| 241 | |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 242 | dst->append(src_ptr, c); |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 243 | c++; |
| 244 | |
| 245 | if (*c == '$') { |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 246 | dst->push_back(*(c++)); |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 247 | src_ptr = c; |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 248 | continue; |
| 249 | } else if (*c == '\0') { |
| 250 | break; |
| 251 | } |
| 252 | |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 253 | std::string prop_name; |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 254 | if (*c == '{') { |
| 255 | c++; |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 256 | const char* end = strchr(c, '}'); |
| 257 | if (!end) { |
| 258 | // failed to find closing brace, abort. |
| 259 | ERROR("unexpected end of string in '%s', looking for }\n", src); |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 260 | goto err; |
| 261 | } |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 262 | prop_name = std::string(c, end); |
| 263 | c = end + 1; |
| 264 | } else { |
| 265 | prop_name = c; |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 266 | ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n", |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 267 | c); |
| 268 | c += prop_name.size(); |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 271 | if (prop_name.empty()) { |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 272 | ERROR("invalid zero-length prop name in '%s'\n", src); |
| 273 | goto err; |
| 274 | } |
| 275 | |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 276 | std::string prop_val = property_get(prop_name.c_str()); |
Yabin Cui | 74edcea | 2015-07-24 10:11:05 -0700 | [diff] [blame] | 277 | if (prop_val.empty()) { |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 278 | ERROR("property '%s' doesn't exist while expanding '%s'\n", |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 279 | prop_name.c_str(), src); |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 280 | goto err; |
| 281 | } |
| 282 | |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 283 | dst->append(prop_val); |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 284 | src_ptr = c; |
| 285 | continue; |
| 286 | } |
| 287 | |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 288 | return 0; |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 289 | err: |
| 290 | return -1; |
| 291 | } |
| 292 | |
Greg Hackmann | d68db71 | 2013-11-18 09:44:20 -0800 | [diff] [blame] | 293 | static void parse_import(struct parse_state *state, int nargs, char **args) |
Dima Zavin | 78a1b1f | 2011-12-20 12:53:48 -0800 | [diff] [blame] | 294 | { |
Dima Zavin | 78a1b1f | 2011-12-20 12:53:48 -0800 | [diff] [blame] | 295 | if (nargs != 2) { |
| 296 | ERROR("single argument needed for import\n"); |
| 297 | return; |
| 298 | } |
| 299 | |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 300 | std::string conf_file; |
| 301 | int ret = expand_props(args[1], &conf_file); |
Dima Zavin | 78a1b1f | 2011-12-20 12:53:48 -0800 | [diff] [blame] | 302 | if (ret) { |
| 303 | ERROR("error while handling import on line '%d' in '%s'\n", |
| 304 | state->line, state->filename); |
| 305 | return; |
| 306 | } |
| 307 | |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 308 | struct import* import = (struct import*) calloc(1, sizeof(struct import)); |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 309 | import->filename = strdup(conf_file.c_str()); |
| 310 | |
| 311 | struct listnode *import_list = (listnode*) state->priv; |
Dima Zavin | 78a1b1f | 2011-12-20 12:53:48 -0800 | [diff] [blame] | 312 | list_add_tail(import_list, &import->list); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 313 | INFO("Added '%s' to import list\n", import->filename); |
Dima Zavin | 78a1b1f | 2011-12-20 12:53:48 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Greg Hackmann | d68db71 | 2013-11-18 09:44:20 -0800 | [diff] [blame] | 316 | static void parse_new_section(struct parse_state *state, int kw, |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 317 | int nargs, char **args) |
| 318 | { |
| 319 | printf("[ %s %s ]\n", args[0], |
| 320 | nargs > 1 ? args[1] : ""); |
| 321 | switch(kw) { |
| 322 | case K_service: |
| 323 | state->context = parse_service(state, nargs, args); |
| 324 | if (state->context) { |
| 325 | state->parse_line = parse_line_service; |
| 326 | return; |
| 327 | } |
| 328 | break; |
| 329 | case K_on: |
| 330 | state->context = parse_action(state, nargs, args); |
| 331 | if (state->context) { |
| 332 | state->parse_line = parse_line_action; |
| 333 | return; |
| 334 | } |
| 335 | break; |
Mike Lockwood | f5cb5b2 | 2011-06-07 20:08:40 -0700 | [diff] [blame] | 336 | case K_import: |
Dima Zavin | 78a1b1f | 2011-12-20 12:53:48 -0800 | [diff] [blame] | 337 | parse_import(state, nargs, args); |
Dima Zavin | a6235ea | 2011-12-16 14:20:12 -0800 | [diff] [blame] | 338 | break; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 339 | } |
| 340 | state->parse_line = parse_line_no_op; |
| 341 | } |
| 342 | |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 343 | static void parse_config(const char *fn, const std::string& data) |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 344 | { |
Dima Zavin | 78a1b1f | 2011-12-20 12:53:48 -0800 | [diff] [blame] | 345 | struct listnode import_list; |
| 346 | struct listnode *node; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 347 | char *args[INIT_PARSER_MAXARGS]; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 348 | |
Tom Cherry | eaa3b4e | 2015-05-12 13:54:41 -0700 | [diff] [blame] | 349 | int nargs = 0; |
| 350 | |
| 351 | parse_state state; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 352 | state.filename = fn; |
Bruce Beare | 1be6968 | 2010-12-26 09:55:10 -0800 | [diff] [blame] | 353 | state.line = 0; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 354 | state.ptr = strdup(data.c_str()); // TODO: fix this code! |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 355 | state.nexttoken = 0; |
| 356 | state.parse_line = parse_line_no_op; |
Dima Zavin | 78a1b1f | 2011-12-20 12:53:48 -0800 | [diff] [blame] | 357 | |
| 358 | list_init(&import_list); |
| 359 | state.priv = &import_list; |
| 360 | |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 361 | for (;;) { |
| 362 | switch (next_token(&state)) { |
| 363 | case T_EOF: |
| 364 | state.parse_line(&state, 0, 0); |
Dima Zavin | 78a1b1f | 2011-12-20 12:53:48 -0800 | [diff] [blame] | 365 | goto parser_done; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 366 | case T_NEWLINE: |
Bruce Beare | 1be6968 | 2010-12-26 09:55:10 -0800 | [diff] [blame] | 367 | state.line++; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 368 | if (nargs) { |
| 369 | int kw = lookup_keyword(args[0]); |
| 370 | if (kw_is(kw, SECTION)) { |
| 371 | state.parse_line(&state, 0, 0); |
| 372 | parse_new_section(&state, kw, nargs, args); |
| 373 | } else { |
| 374 | state.parse_line(&state, nargs, args); |
| 375 | } |
| 376 | nargs = 0; |
| 377 | } |
| 378 | break; |
| 379 | case T_TEXT: |
| 380 | if (nargs < INIT_PARSER_MAXARGS) { |
| 381 | args[nargs++] = state.text; |
| 382 | } |
| 383 | break; |
| 384 | } |
| 385 | } |
Dima Zavin | 78a1b1f | 2011-12-20 12:53:48 -0800 | [diff] [blame] | 386 | |
| 387 | parser_done: |
| 388 | list_for_each(node, &import_list) { |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 389 | struct import* import = node_to_item(node, struct import, list); |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame^] | 390 | if (!init_parse_config(import->filename)) { |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 391 | ERROR("could not import file '%s' from '%s': %s\n", |
| 392 | import->filename, fn, strerror(errno)); |
| 393 | } |
Dima Zavin | 78a1b1f | 2011-12-20 12:53:48 -0800 | [diff] [blame] | 394 | } |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame^] | 397 | static bool init_parse_config_file(const char* path) { |
| 398 | INFO("Parsing file %s...\n", path); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 399 | Timer t; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 400 | std::string data; |
| 401 | if (!read_file(path, &data)) { |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 402 | return false; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 403 | } |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 404 | |
Tom Cherry | eaa3b4e | 2015-05-12 13:54:41 -0700 | [diff] [blame] | 405 | data.push_back('\n'); // TODO: fix parse_config. |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 406 | parse_config(path, data); |
Elliott Hughes | c0e919c | 2015-02-04 14:46:36 -0800 | [diff] [blame] | 407 | dump_parser_state(); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 408 | |
| 409 | NOTICE("(Parsing %s took %.2fs.)\n", path, t.duration()); |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 410 | return true; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame^] | 413 | static bool init_parse_config_dir(const char* path) { |
| 414 | INFO("Parsing directory %s...\n", path); |
| 415 | std::unique_ptr<DIR, int(*)(DIR*)> config_dir(opendir(path), closedir); |
| 416 | if (!config_dir) { |
| 417 | ERROR("Could not import directory '%s'\n", path); |
| 418 | return false; |
| 419 | } |
| 420 | dirent* current_file; |
| 421 | while ((current_file = readdir(config_dir.get()))) { |
| 422 | std::string current_path = |
| 423 | android::base::StringPrintf("%s/%s", path, current_file->d_name); |
| 424 | // Ignore directories and only process regular files. |
| 425 | if (current_file->d_type == DT_REG) { |
| 426 | if (!init_parse_config_file(current_path.c_str())) { |
| 427 | ERROR("could not import file '%s'\n", current_path.c_str()); |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | return true; |
| 432 | } |
| 433 | |
| 434 | bool init_parse_config(const char* path) { |
| 435 | if (is_dir(path)) { |
| 436 | return init_parse_config_dir(path); |
| 437 | } |
| 438 | return init_parse_config_file(path); |
| 439 | } |
| 440 | |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 441 | static int valid_name(const char *name) |
| 442 | { |
| 443 | if (strlen(name) > 16) { |
| 444 | return 0; |
| 445 | } |
| 446 | while (*name) { |
| 447 | if (!isalnum(*name) && (*name != '_') && (*name != '-')) { |
| 448 | return 0; |
| 449 | } |
| 450 | name++; |
| 451 | } |
| 452 | return 1; |
| 453 | } |
| 454 | |
| 455 | struct service *service_find_by_name(const char *name) |
| 456 | { |
| 457 | struct listnode *node; |
| 458 | struct service *svc; |
| 459 | list_for_each(node, &service_list) { |
| 460 | svc = node_to_item(node, struct service, slist); |
| 461 | if (!strcmp(svc->name, name)) { |
| 462 | return svc; |
| 463 | } |
| 464 | } |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | struct service *service_find_by_pid(pid_t pid) |
| 469 | { |
| 470 | struct listnode *node; |
| 471 | struct service *svc; |
| 472 | list_for_each(node, &service_list) { |
| 473 | svc = node_to_item(node, struct service, slist); |
| 474 | if (svc->pid == pid) { |
| 475 | return svc; |
| 476 | } |
| 477 | } |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | struct service *service_find_by_keychord(int keychord_id) |
| 482 | { |
| 483 | struct listnode *node; |
| 484 | struct service *svc; |
| 485 | list_for_each(node, &service_list) { |
| 486 | svc = node_to_item(node, struct service, slist); |
| 487 | if (svc->keychord_id == keychord_id) { |
| 488 | return svc; |
| 489 | } |
| 490 | } |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | void service_for_each(void (*func)(struct service *svc)) |
| 495 | { |
| 496 | struct listnode *node; |
| 497 | struct service *svc; |
| 498 | list_for_each(node, &service_list) { |
| 499 | svc = node_to_item(node, struct service, slist); |
| 500 | func(svc); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | void service_for_each_class(const char *classname, |
| 505 | void (*func)(struct service *svc)) |
| 506 | { |
| 507 | struct listnode *node; |
| 508 | struct service *svc; |
| 509 | list_for_each(node, &service_list) { |
| 510 | svc = node_to_item(node, struct service, slist); |
| 511 | if (!strcmp(svc->classname, classname)) { |
| 512 | func(svc); |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | void service_for_each_flags(unsigned matchflags, |
| 518 | void (*func)(struct service *svc)) |
| 519 | { |
| 520 | struct listnode *node; |
| 521 | struct service *svc; |
| 522 | list_for_each(node, &service_list) { |
| 523 | svc = node_to_item(node, struct service, slist); |
| 524 | if (svc->flags & matchflags) { |
| 525 | func(svc); |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | void action_for_each_trigger(const char *trigger, |
| 531 | void (*func)(struct action *act)) |
| 532 | { |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 533 | struct listnode *node, *node2; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 534 | struct action *act; |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 535 | struct trigger *cur_trigger; |
| 536 | |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 537 | list_for_each(node, &action_list) { |
| 538 | act = node_to_item(node, struct action, alist); |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 539 | list_for_each(node2, &act->triggers) { |
| 540 | cur_trigger = node_to_item(node2, struct trigger, nlist); |
| 541 | if (!strcmp(cur_trigger->name, trigger)) { |
| 542 | func(act); |
| 543 | } |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | } |
| 547 | |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 548 | |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 549 | void queue_property_triggers(const char *name, const char *value) |
| 550 | { |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 551 | struct listnode *node, *node2; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 552 | struct action *act; |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 553 | struct trigger *cur_trigger; |
| 554 | bool match; |
| 555 | int name_length; |
| 556 | |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 557 | list_for_each(node, &action_list) { |
| 558 | act = node_to_item(node, struct action, alist); |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 559 | match = !name; |
| 560 | list_for_each(node2, &act->triggers) { |
| 561 | cur_trigger = node_to_item(node2, struct trigger, nlist); |
| 562 | if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) { |
| 563 | const char *test = cur_trigger->name + strlen("property:"); |
| 564 | if (!match) { |
| 565 | name_length = strlen(name); |
| 566 | if (!strncmp(name, test, name_length) && |
| 567 | test[name_length] == '=' && |
| 568 | (!strcmp(test + name_length + 1, value) || |
| 569 | !strcmp(test + name_length + 1, "*"))) { |
| 570 | match = true; |
| 571 | continue; |
| 572 | } |
| 573 | } else { |
| 574 | const char* equals = strchr(test, '='); |
| 575 | if (equals) { |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 576 | int length = equals - test; |
| 577 | if (length <= PROP_NAME_MAX) { |
Yabin Cui | 74edcea | 2015-07-24 10:11:05 -0700 | [diff] [blame] | 578 | std::string prop_name(test, length); |
| 579 | std::string value = property_get(prop_name.c_str()); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 580 | |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 581 | /* does the property exist, and match the trigger value? */ |
Yabin Cui | 74edcea | 2015-07-24 10:11:05 -0700 | [diff] [blame] | 582 | if (!value.empty() && (!strcmp(equals + 1, value.c_str()) || |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 583 | !strcmp(equals + 1, "*"))) { |
| 584 | continue; |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | match = false; |
| 591 | break; |
| 592 | } |
| 593 | if (match) { |
| 594 | action_add_queue_tail(act); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | void queue_all_property_triggers() |
| 600 | { |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 601 | queue_property_triggers(NULL, NULL); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 604 | void queue_builtin_action(int (*func)(int nargs, char **args), const char *name) |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 605 | { |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 606 | action* act = (action*) calloc(1, sizeof(*act)); |
| 607 | trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger)); |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 608 | cur_trigger->name = name; |
| 609 | list_init(&act->triggers); |
| 610 | list_add_tail(&act->triggers, &cur_trigger->nlist); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 611 | list_init(&act->commands); |
Colin Cross | a506462 | 2013-03-07 13:42:29 -0800 | [diff] [blame] | 612 | list_init(&act->qlist); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 613 | |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 614 | command* cmd = (command*) calloc(1, sizeof(*cmd)); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 615 | cmd->func = func; |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 616 | cmd->args[0] = const_cast<char*>(name); |
Riley Andrews | 24a3b78 | 2014-06-26 13:56:01 -0700 | [diff] [blame] | 617 | cmd->nargs = 1; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 618 | list_add_tail(&act->commands, &cmd->clist); |
| 619 | |
| 620 | list_add_tail(&action_list, &act->alist); |
| 621 | action_add_queue_tail(act); |
| 622 | } |
| 623 | |
| 624 | void action_add_queue_tail(struct action *act) |
| 625 | { |
Colin Cross | a506462 | 2013-03-07 13:42:29 -0800 | [diff] [blame] | 626 | if (list_empty(&act->qlist)) { |
| 627 | list_add_tail(&action_queue, &act->qlist); |
| 628 | } |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | struct action *action_remove_queue_head(void) |
| 632 | { |
| 633 | if (list_empty(&action_queue)) { |
| 634 | return 0; |
| 635 | } else { |
| 636 | struct listnode *node = list_head(&action_queue); |
| 637 | struct action *act = node_to_item(node, struct action, qlist); |
| 638 | list_remove(node); |
Colin Cross | a506462 | 2013-03-07 13:42:29 -0800 | [diff] [blame] | 639 | list_init(node); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 640 | return act; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | int action_queue_empty() |
| 645 | { |
| 646 | return list_empty(&action_queue); |
| 647 | } |
| 648 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 649 | service* make_exec_oneshot_service(int nargs, char** args) { |
| 650 | // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS... |
Mark Salyzyn | 17fff89 | 2015-06-02 11:11:02 -0700 | [diff] [blame] | 651 | // SECLABEL can be a - to denote default |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 652 | int command_arg = 1; |
| 653 | for (int i = 1; i < nargs; ++i) { |
| 654 | if (strcmp(args[i], "--") == 0) { |
| 655 | command_arg = i + 1; |
| 656 | break; |
| 657 | } |
| 658 | } |
| 659 | if (command_arg > 4 + NR_SVC_SUPP_GIDS) { |
| 660 | ERROR("exec called with too many supplementary group ids\n"); |
| 661 | return NULL; |
| 662 | } |
| 663 | |
| 664 | int argc = nargs - command_arg; |
| 665 | char** argv = (args + command_arg); |
| 666 | if (argc < 1) { |
| 667 | ERROR("exec called without command\n"); |
| 668 | return NULL; |
| 669 | } |
| 670 | |
| 671 | service* svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * argc); |
| 672 | if (svc == NULL) { |
| 673 | ERROR("Couldn't allocate service for exec of '%s': %s", argv[0], strerror(errno)); |
| 674 | return NULL; |
| 675 | } |
| 676 | |
Mark Salyzyn | 17fff89 | 2015-06-02 11:11:02 -0700 | [diff] [blame] | 677 | if ((command_arg > 2) && strcmp(args[1], "-")) { |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 678 | svc->seclabel = args[1]; |
| 679 | } |
| 680 | if (command_arg > 3) { |
| 681 | svc->uid = decode_uid(args[2]); |
| 682 | } |
| 683 | if (command_arg > 4) { |
| 684 | svc->gid = decode_uid(args[3]); |
| 685 | svc->nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */; |
| 686 | for (size_t i = 0; i < svc->nr_supp_gids; ++i) { |
| 687 | svc->supp_gids[i] = decode_uid(args[4 + i]); |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | static int exec_count; // Every service needs a unique name. |
| 692 | char* name = NULL; |
| 693 | asprintf(&name, "exec %d (%s)", exec_count++, argv[0]); |
| 694 | if (name == NULL) { |
| 695 | ERROR("Couldn't allocate name for exec service '%s'\n", argv[0]); |
| 696 | free(svc); |
| 697 | return NULL; |
| 698 | } |
| 699 | svc->name = name; |
| 700 | svc->classname = "default"; |
| 701 | svc->flags = SVC_EXEC | SVC_ONESHOT; |
| 702 | svc->nargs = argc; |
| 703 | memcpy(svc->args, argv, sizeof(char*) * svc->nargs); |
| 704 | svc->args[argc] = NULL; |
| 705 | list_add_tail(&service_list, &svc->slist); |
| 706 | return svc; |
| 707 | } |
| 708 | |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 709 | static void *parse_service(struct parse_state *state, int nargs, char **args) |
| 710 | { |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 711 | if (nargs < 3) { |
| 712 | parse_error(state, "services must have a name and a program\n"); |
| 713 | return 0; |
| 714 | } |
| 715 | if (!valid_name(args[1])) { |
| 716 | parse_error(state, "invalid service name '%s'\n", args[1]); |
| 717 | return 0; |
| 718 | } |
| 719 | |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 720 | service* svc = (service*) service_find_by_name(args[1]); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 721 | if (svc) { |
| 722 | parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]); |
| 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | nargs -= 2; |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 727 | svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 728 | if (!svc) { |
| 729 | parse_error(state, "out of memory\n"); |
| 730 | return 0; |
| 731 | } |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 732 | svc->name = strdup(args[1]); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 733 | svc->classname = "default"; |
| 734 | memcpy(svc->args, args + 2, sizeof(char*) * nargs); |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 735 | trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger)); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 736 | svc->args[nargs] = 0; |
| 737 | svc->nargs = nargs; |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 738 | list_init(&svc->onrestart.triggers); |
| 739 | cur_trigger->name = "onrestart"; |
| 740 | list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 741 | list_init(&svc->onrestart.commands); |
| 742 | list_add_tail(&service_list, &svc->slist); |
| 743 | return svc; |
| 744 | } |
| 745 | |
| 746 | static void parse_line_service(struct parse_state *state, int nargs, char **args) |
| 747 | { |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 748 | struct service *svc = (service*) state->context; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 749 | struct command *cmd; |
| 750 | int i, kw, kw_nargs; |
| 751 | |
| 752 | if (nargs == 0) { |
| 753 | return; |
| 754 | } |
| 755 | |
| 756 | svc->ioprio_class = IoSchedClass_NONE; |
| 757 | |
| 758 | kw = lookup_keyword(args[0]); |
| 759 | switch (kw) { |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 760 | case K_class: |
| 761 | if (nargs != 2) { |
| 762 | parse_error(state, "class option requires a classname\n"); |
| 763 | } else { |
| 764 | svc->classname = args[1]; |
| 765 | } |
| 766 | break; |
| 767 | case K_console: |
| 768 | svc->flags |= SVC_CONSOLE; |
| 769 | break; |
| 770 | case K_disabled: |
| 771 | svc->flags |= SVC_DISABLED; |
Ken Sumrall | a286480 | 2011-10-26 16:56:00 -0700 | [diff] [blame] | 772 | svc->flags |= SVC_RC_DISABLED; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 773 | break; |
| 774 | case K_ioprio: |
| 775 | if (nargs != 3) { |
| 776 | parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n"); |
| 777 | } else { |
| 778 | svc->ioprio_pri = strtoul(args[2], 0, 8); |
| 779 | |
| 780 | if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) { |
| 781 | parse_error(state, "priority value must be range 0 - 7\n"); |
| 782 | break; |
| 783 | } |
| 784 | |
| 785 | if (!strcmp(args[1], "rt")) { |
| 786 | svc->ioprio_class = IoSchedClass_RT; |
| 787 | } else if (!strcmp(args[1], "be")) { |
| 788 | svc->ioprio_class = IoSchedClass_BE; |
| 789 | } else if (!strcmp(args[1], "idle")) { |
| 790 | svc->ioprio_class = IoSchedClass_IDLE; |
| 791 | } else { |
| 792 | parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n"); |
| 793 | } |
| 794 | } |
| 795 | break; |
| 796 | case K_group: |
| 797 | if (nargs < 2) { |
| 798 | parse_error(state, "group option requires a group id\n"); |
| 799 | } else if (nargs > NR_SVC_SUPP_GIDS + 2) { |
| 800 | parse_error(state, "group option accepts at most %d supp. groups\n", |
| 801 | NR_SVC_SUPP_GIDS); |
| 802 | } else { |
| 803 | int n; |
| 804 | svc->gid = decode_uid(args[1]); |
| 805 | for (n = 2; n < nargs; n++) { |
| 806 | svc->supp_gids[n-2] = decode_uid(args[n]); |
| 807 | } |
| 808 | svc->nr_supp_gids = n - 2; |
| 809 | } |
| 810 | break; |
| 811 | case K_keycodes: |
| 812 | if (nargs < 2) { |
| 813 | parse_error(state, "keycodes option requires atleast one keycode\n"); |
| 814 | } else { |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 815 | svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0])); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 816 | if (!svc->keycodes) { |
| 817 | parse_error(state, "could not allocate keycodes\n"); |
| 818 | } else { |
| 819 | svc->nkeycodes = nargs - 1; |
| 820 | for (i = 1; i < nargs; i++) { |
| 821 | svc->keycodes[i - 1] = atoi(args[i]); |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | break; |
| 826 | case K_oneshot: |
| 827 | svc->flags |= SVC_ONESHOT; |
| 828 | break; |
| 829 | case K_onrestart: |
| 830 | nargs--; |
| 831 | args++; |
| 832 | kw = lookup_keyword(args[0]); |
| 833 | if (!kw_is(kw, COMMAND)) { |
| 834 | parse_error(state, "invalid command '%s'\n", args[0]); |
| 835 | break; |
| 836 | } |
| 837 | kw_nargs = kw_nargs(kw); |
| 838 | if (nargs < kw_nargs) { |
| 839 | parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1, |
| 840 | kw_nargs > 2 ? "arguments" : "argument"); |
| 841 | break; |
| 842 | } |
| 843 | |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 844 | cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 845 | cmd->func = kw_func(kw); |
| 846 | cmd->nargs = nargs; |
| 847 | memcpy(cmd->args, args, sizeof(char*) * nargs); |
| 848 | list_add_tail(&svc->onrestart.commands, &cmd->clist); |
| 849 | break; |
| 850 | case K_critical: |
| 851 | svc->flags |= SVC_CRITICAL; |
| 852 | break; |
| 853 | case K_setenv: { /* name value */ |
Gavin.Chang | c3a4676 | 2013-09-28 00:22:11 +0800 | [diff] [blame] | 854 | if (nargs < 3) { |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 855 | parse_error(state, "setenv option requires name and value arguments\n"); |
| 856 | break; |
| 857 | } |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 858 | svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei)); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 859 | if (!ei) { |
| 860 | parse_error(state, "out of memory\n"); |
| 861 | break; |
| 862 | } |
| 863 | ei->name = args[1]; |
| 864 | ei->value = args[2]; |
| 865 | ei->next = svc->envvars; |
| 866 | svc->envvars = ei; |
| 867 | break; |
| 868 | } |
Stephen Smalley | 8348d27 | 2013-05-13 12:37:04 -0400 | [diff] [blame] | 869 | case K_socket: {/* name type perm [ uid gid context ] */ |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 870 | if (nargs < 4) { |
| 871 | parse_error(state, "socket option requires name, type, perm arguments\n"); |
| 872 | break; |
| 873 | } |
Mike Lockwood | 912ff85 | 2010-10-01 08:20:36 -0400 | [diff] [blame] | 874 | if (strcmp(args[2],"dgram") && strcmp(args[2],"stream") |
| 875 | && strcmp(args[2],"seqpacket")) { |
| 876 | parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n"); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 877 | break; |
| 878 | } |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 879 | socketinfo* si = (socketinfo*) calloc(1, sizeof(*si)); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 880 | if (!si) { |
| 881 | parse_error(state, "out of memory\n"); |
| 882 | break; |
| 883 | } |
| 884 | si->name = args[1]; |
| 885 | si->type = args[2]; |
| 886 | si->perm = strtoul(args[3], 0, 8); |
| 887 | if (nargs > 4) |
| 888 | si->uid = decode_uid(args[4]); |
| 889 | if (nargs > 5) |
| 890 | si->gid = decode_uid(args[5]); |
Stephen Smalley | 8348d27 | 2013-05-13 12:37:04 -0400 | [diff] [blame] | 891 | if (nargs > 6) |
| 892 | si->socketcon = args[6]; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 893 | si->next = svc->sockets; |
| 894 | svc->sockets = si; |
| 895 | break; |
| 896 | } |
| 897 | case K_user: |
| 898 | if (nargs != 2) { |
| 899 | parse_error(state, "user option requires a user id\n"); |
| 900 | } else { |
| 901 | svc->uid = decode_uid(args[1]); |
| 902 | } |
| 903 | break; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 904 | case K_seclabel: |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 905 | if (nargs != 2) { |
| 906 | parse_error(state, "seclabel option requires a label string\n"); |
| 907 | } else { |
| 908 | svc->seclabel = args[1]; |
| 909 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 910 | break; |
Elliott Hughes | d62f060 | 2015-06-12 18:02:20 -0700 | [diff] [blame] | 911 | case K_writepid: |
| 912 | if (nargs < 2) { |
| 913 | parse_error(state, "writepid option requires at least one filename\n"); |
| 914 | break; |
| 915 | } |
| 916 | svc->writepid_files_ = new std::vector<std::string>; |
| 917 | for (int i = 1; i < nargs; ++i) { |
| 918 | svc->writepid_files_->push_back(args[i]); |
| 919 | } |
| 920 | break; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 921 | |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 922 | default: |
| 923 | parse_error(state, "invalid option '%s'\n", args[0]); |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | static void *parse_action(struct parse_state *state, int nargs, char **args) |
| 928 | { |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 929 | struct trigger *cur_trigger; |
| 930 | int i; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 931 | if (nargs < 2) { |
| 932 | parse_error(state, "actions must have a trigger\n"); |
| 933 | return 0; |
| 934 | } |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 935 | |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 936 | action* act = (action*) calloc(1, sizeof(*act)); |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 937 | list_init(&act->triggers); |
| 938 | |
| 939 | for (i = 1; i < nargs; i++) { |
| 940 | if (!(i % 2)) { |
| 941 | if (strcmp(args[i], "&&")) { |
Tom Cherry | ae392cf | 2015-04-13 13:07:04 -0700 | [diff] [blame] | 942 | struct listnode *node; |
| 943 | struct listnode *node2; |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 944 | parse_error(state, "& is the only symbol allowed to concatenate actions\n"); |
Tom Cherry | ae392cf | 2015-04-13 13:07:04 -0700 | [diff] [blame] | 945 | list_for_each_safe(node, node2, &act->triggers) { |
| 946 | struct trigger *trigger = node_to_item(node, struct trigger, nlist); |
| 947 | free(trigger); |
| 948 | } |
| 949 | free(act); |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 950 | return 0; |
| 951 | } else |
| 952 | continue; |
| 953 | } |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 954 | cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger)); |
Badhri Jagan Sridharan | 0b41512 | 2014-10-10 23:19:06 -0700 | [diff] [blame] | 955 | cur_trigger->name = args[i]; |
| 956 | list_add_tail(&act->triggers, &cur_trigger->nlist); |
| 957 | } |
| 958 | |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 959 | list_init(&act->commands); |
Colin Cross | a506462 | 2013-03-07 13:42:29 -0800 | [diff] [blame] | 960 | list_init(&act->qlist); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 961 | list_add_tail(&action_list, &act->alist); |
| 962 | /* XXX add to hash */ |
| 963 | return act; |
| 964 | } |
| 965 | |
| 966 | static void parse_line_action(struct parse_state* state, int nargs, char **args) |
| 967 | { |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 968 | struct action *act = (action*) state->context; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 969 | int kw, n; |
| 970 | |
| 971 | if (nargs == 0) { |
| 972 | return; |
| 973 | } |
| 974 | |
| 975 | kw = lookup_keyword(args[0]); |
| 976 | if (!kw_is(kw, COMMAND)) { |
| 977 | parse_error(state, "invalid command '%s'\n", args[0]); |
| 978 | return; |
| 979 | } |
| 980 | |
| 981 | n = kw_nargs(kw); |
| 982 | if (nargs < n) { |
| 983 | parse_error(state, "%s requires %d %s\n", args[0], n - 1, |
| 984 | n > 2 ? "arguments" : "argument"); |
| 985 | return; |
| 986 | } |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 987 | command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 988 | cmd->func = kw_func(kw); |
Riley Andrews | 24a3b78 | 2014-06-26 13:56:01 -0700 | [diff] [blame] | 989 | cmd->line = state->line; |
| 990 | cmd->filename = state->filename; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 991 | cmd->nargs = nargs; |
| 992 | memcpy(cmd->args, args, sizeof(char*) * nargs); |
| 993 | list_add_tail(&act->commands, &cmd->clist); |
| 994 | } |