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