blob: 4093da42edefd3dc106fcad06a23623cf40a725e [file] [log] [blame]
Colin Cross6310a822010-04-20 14:29:05 -07001/*
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 Hughesda40c002015-03-27 23:20:44 -070017#include <ctype.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080018#include <errno.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070019#include <fcntl.h>
20#include <inttypes.h>
21#include <stdarg.h>
22#include <stddef.h>
Colin Cross6310a822010-04-20 14:29:05 -070023#include <stdio.h>
24#include <stdlib.h>
Colin Cross6310a822010-04-20 14:29:05 -070025#include <string.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070026#include <unistd.h>
Colin Cross6310a822010-04-20 14:29:05 -070027
28#include "init.h"
29#include "parser.h"
30#include "init_parser.h"
31#include "log.h"
Colin Cross6310a822010-04-20 14:29:05 -070032#include "property_service.h"
33#include "util.h"
34
35#include <cutils/iosched_policy.h>
Dima Zavinda04c522011-09-01 17:09:44 -070036#include <cutils/list.h>
Colin Cross6310a822010-04-20 14:29:05 -070037
Colin Cross6310a822010-04-20 14:29:05 -070038static list_declare(service_list);
39static list_declare(action_list);
40static list_declare(action_queue);
41
Dima Zavin78a1b1f2011-12-20 12:53:48 -080042struct import {
43 struct listnode list;
44 const char *filename;
45};
46
Colin Cross6310a822010-04-20 14:29:05 -070047static void *parse_service(struct parse_state *state, int nargs, char **args);
48static void parse_line_service(struct parse_state *state, int nargs, char **args);
49
50static void *parse_action(struct parse_state *state, int nargs, char **args);
51static 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 Hackmannd68db712013-11-18 09:44:20 -080062static struct {
Colin Cross6310a822010-04-20 14:29:05 -070063 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 Hughesc0e919c2015-02-04 14:46:36 -080078void 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 ");
98 char name_str[256] = "";
99 build_triggers_string(name_str, sizeof(name_str), act);
100 INFO("%s", name_str);
101 INFO("\n");
102
103 struct listnode* node2;
104 list_for_each(node2, &act->commands) {
105 command* cmd = node_to_item(node2, struct command, clist);
106 INFO(" %p", cmd->func);
107 for (int n = 0; n < cmd->nargs; n++) {
108 INFO(" %s", cmd->args[n]);
109 }
110 INFO("\n");
111 }
112 INFO("\n");
113 }
114 }
115}
116
Greg Hackmannd68db712013-11-18 09:44:20 -0800117static int lookup_keyword(const char *s)
Colin Cross6310a822010-04-20 14:29:05 -0700118{
119 switch (*s++) {
Yongqin Liua197ff12014-12-05 13:45:02 +0800120 case 'b':
121 if (!strcmp(s, "ootchart_init")) return K_bootchart_init;
Mark Salyzyn7a3d66c2015-03-24 07:29:22 -0700122 break;
Colin Cross6310a822010-04-20 14:29:05 -0700123 case 'c':
Elliott Hughesf682b472015-02-06 12:19:48 -0800124 if (!strcmp(s, "opy")) return K_copy;
Colin Cross6310a822010-04-20 14:29:05 -0700125 if (!strcmp(s, "lass")) return K_class;
126 if (!strcmp(s, "lass_start")) return K_class_start;
127 if (!strcmp(s, "lass_stop")) return K_class_stop;
Ken Sumrall752923c2010-12-03 16:33:31 -0800128 if (!strcmp(s, "lass_reset")) return K_class_reset;
Colin Cross6310a822010-04-20 14:29:05 -0700129 if (!strcmp(s, "onsole")) return K_console;
130 if (!strcmp(s, "hown")) return K_chown;
131 if (!strcmp(s, "hmod")) return K_chmod;
132 if (!strcmp(s, "ritical")) return K_critical;
133 break;
134 case 'd':
135 if (!strcmp(s, "isabled")) return K_disabled;
136 if (!strcmp(s, "omainname")) return K_domainname;
137 break;
138 case 'e':
JP Abgrall3beec7e2014-05-02 21:14:29 -0700139 if (!strcmp(s, "nable")) return K_enable;
Colin Cross6310a822010-04-20 14:29:05 -0700140 if (!strcmp(s, "xec")) return K_exec;
141 if (!strcmp(s, "xport")) return K_export;
142 break;
143 case 'g':
144 if (!strcmp(s, "roup")) return K_group;
145 break;
146 case 'h':
147 if (!strcmp(s, "ostname")) return K_hostname;
148 break;
149 case 'i':
150 if (!strcmp(s, "oprio")) return K_ioprio;
151 if (!strcmp(s, "fup")) return K_ifup;
152 if (!strcmp(s, "nsmod")) return K_insmod;
153 if (!strcmp(s, "mport")) return K_import;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000154 if (!strcmp(s, "nstallkey")) return K_installkey;
Colin Cross6310a822010-04-20 14:29:05 -0700155 break;
156 case 'k':
157 if (!strcmp(s, "eycodes")) return K_keycodes;
158 break;
159 case 'l':
160 if (!strcmp(s, "oglevel")) return K_loglevel;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800161 if (!strcmp(s, "oad_persist_props")) return K_load_persist_props;
Paul Lawrence948410a2015-07-01 14:40:56 -0700162 if (!strcmp(s, "oad_system_props")) return K_load_system_props;
Colin Cross6310a822010-04-20 14:29:05 -0700163 break;
164 case 'm':
165 if (!strcmp(s, "kdir")) return K_mkdir;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700166 if (!strcmp(s, "ount_all")) return K_mount_all;
Colin Cross6310a822010-04-20 14:29:05 -0700167 if (!strcmp(s, "ount")) return K_mount;
168 break;
169 case 'o':
170 if (!strcmp(s, "n")) return K_on;
171 if (!strcmp(s, "neshot")) return K_oneshot;
172 if (!strcmp(s, "nrestart")) return K_onrestart;
173 break;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700174 case 'p':
175 if (!strcmp(s, "owerctl")) return K_powerctl;
Elliott Hughesf682b472015-02-06 12:19:48 -0800176 break;
Colin Cross6310a822010-04-20 14:29:05 -0700177 case 'r':
178 if (!strcmp(s, "estart")) return K_restart;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500179 if (!strcmp(s, "estorecon")) return K_restorecon;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400180 if (!strcmp(s, "estorecon_recursive")) return K_restorecon_recursive;
Ken Sumrall203bad52011-01-18 17:37:41 -0800181 if (!strcmp(s, "mdir")) return K_rmdir;
182 if (!strcmp(s, "m")) return K_rm;
Colin Cross6310a822010-04-20 14:29:05 -0700183 break;
184 case 's':
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500185 if (!strcmp(s, "eclabel")) return K_seclabel;
Colin Cross6310a822010-04-20 14:29:05 -0700186 if (!strcmp(s, "ervice")) return K_service;
187 if (!strcmp(s, "etenv")) return K_setenv;
Colin Cross6310a822010-04-20 14:29:05 -0700188 if (!strcmp(s, "etprop")) return K_setprop;
189 if (!strcmp(s, "etrlimit")) return K_setrlimit;
Paul Crowley749af8c2015-05-28 17:35:06 +0100190 if (!strcmp(s, "etusercryptopolicies")) return K_setusercryptopolicies;
Colin Cross6310a822010-04-20 14:29:05 -0700191 if (!strcmp(s, "ocket")) return K_socket;
192 if (!strcmp(s, "tart")) return K_start;
193 if (!strcmp(s, "top")) return K_stop;
Ken Sumralla76baaa2013-07-09 18:42:09 -0700194 if (!strcmp(s, "wapon_all")) return K_swapon_all;
Colin Cross6310a822010-04-20 14:29:05 -0700195 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 Tolvanen8ff01902015-02-16 11:03:34 +0000204 case 'v':
205 if (!strcmp(s, "erity_load_state")) return K_verity_load_state;
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000206 if (!strcmp(s, "erity_update_state")) return K_verity_update_state;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000207 break;
Colin Cross6310a822010-04-20 14:29:05 -0700208 case 'w':
209 if (!strcmp(s, "rite")) return K_write;
Elliott Hughesd62f0602015-06-12 18:02:20 -0700210 if (!strcmp(s, "ritepid")) return K_writepid;
Colin Cross6310a822010-04-20 14:29:05 -0700211 if (!strcmp(s, "ait")) return K_wait;
212 break;
213 }
214 return K_UNKNOWN;
215}
216
Elliott Hughesf682b472015-02-06 12:19:48 -0800217static void parse_line_no_op(struct parse_state*, int, char**) {
Colin Cross6310a822010-04-20 14:29:05 -0700218}
219
Dima Zavina6235ea2011-12-16 14:20:12 -0800220static int push_chars(char **dst, int *len, const char *chars, int cnt)
221{
222 if (cnt > *len)
223 return -1;
224
225 memcpy(*dst, chars, cnt);
226 *dst += cnt;
227 *len -= cnt;
228
229 return 0;
230}
231
Dima Zavin84bf9af2011-12-20 13:44:41 -0800232int expand_props(char *dst, const char *src, int dst_size)
Dima Zavina6235ea2011-12-16 14:20:12 -0800233{
Dima Zavina6235ea2011-12-16 14:20:12 -0800234 char *dst_ptr = dst;
235 const char *src_ptr = src;
Dima Zavina6235ea2011-12-16 14:20:12 -0800236 int ret = 0;
237 int left = dst_size - 1;
238
239 if (!src || !dst || dst_size == 0)
240 return -1;
241
Dima Zavina6235ea2011-12-16 14:20:12 -0800242 /* - variables can either be $x.y or ${x.y}, in case they are only part
243 * of the string.
244 * - will accept $$ as a literal $.
245 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
246 * bad things will happen
247 */
248 while (*src_ptr && left > 0) {
249 char *c;
250 char prop[PROP_NAME_MAX + 1];
Dima Zavina6235ea2011-12-16 14:20:12 -0800251 int prop_len = 0;
252
253 c = strchr(src_ptr, '$');
254 if (!c) {
255 while (left-- > 0 && *src_ptr)
256 *(dst_ptr++) = *(src_ptr++);
257 break;
258 }
259
260 memset(prop, 0, sizeof(prop));
261
262 ret = push_chars(&dst_ptr, &left, src_ptr, c - src_ptr);
263 if (ret < 0)
264 goto err_nospace;
265 c++;
266
267 if (*c == '$') {
268 *(dst_ptr++) = *(c++);
269 src_ptr = c;
270 left--;
271 continue;
272 } else if (*c == '\0') {
273 break;
274 }
275
276 if (*c == '{') {
277 c++;
278 while (*c && *c != '}' && prop_len < PROP_NAME_MAX)
279 prop[prop_len++] = *(c++);
280 if (*c != '}') {
281 /* failed to find closing brace, abort. */
282 if (prop_len == PROP_NAME_MAX)
283 ERROR("prop name too long during expansion of '%s'\n",
284 src);
285 else if (*c == '\0')
286 ERROR("unexpected end of string in '%s', looking for }\n",
287 src);
288 goto err;
289 }
290 prop[prop_len] = '\0';
291 c++;
292 } else if (*c) {
293 while (*c && prop_len < PROP_NAME_MAX)
294 prop[prop_len++] = *(c++);
295 if (prop_len == PROP_NAME_MAX && *c != '\0') {
296 ERROR("prop name too long in '%s'\n", src);
297 goto err;
298 }
299 prop[prop_len] = '\0';
300 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
301 prop);
302 }
303
304 if (prop_len == 0) {
305 ERROR("invalid zero-length prop name in '%s'\n", src);
306 goto err;
307 }
308
Yabin Cui74edcea2015-07-24 10:11:05 -0700309 std::string prop_val = property_get(prop);
310 if (prop_val.empty()) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800311 ERROR("property '%s' doesn't exist while expanding '%s'\n",
312 prop, src);
313 goto err;
314 }
315
Yabin Cui74edcea2015-07-24 10:11:05 -0700316 ret = push_chars(&dst_ptr, &left, prop_val.c_str(), prop_val.size());
Dima Zavina6235ea2011-12-16 14:20:12 -0800317 if (ret < 0)
318 goto err_nospace;
319 src_ptr = c;
320 continue;
321 }
322
323 *dst_ptr = '\0';
324 return 0;
325
326err_nospace:
327 ERROR("destination buffer overflow while expanding '%s'\n", src);
328err:
329 return -1;
330}
331
Greg Hackmannd68db712013-11-18 09:44:20 -0800332static void parse_import(struct parse_state *state, int nargs, char **args)
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800333{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800334 struct listnode *import_list = (listnode*) state->priv;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800335 char conf_file[PATH_MAX];
336 int ret;
337
338 if (nargs != 2) {
339 ERROR("single argument needed for import\n");
340 return;
341 }
342
343 ret = expand_props(conf_file, args[1], sizeof(conf_file));
344 if (ret) {
345 ERROR("error while handling import on line '%d' in '%s'\n",
346 state->line, state->filename);
347 return;
348 }
349
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800350 struct import* import = (struct import*) calloc(1, sizeof(struct import));
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800351 import->filename = strdup(conf_file);
352 list_add_tail(import_list, &import->list);
Elliott Hughesda40c002015-03-27 23:20:44 -0700353 INFO("Added '%s' to import list\n", import->filename);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800354}
355
Greg Hackmannd68db712013-11-18 09:44:20 -0800356static void parse_new_section(struct parse_state *state, int kw,
Colin Cross6310a822010-04-20 14:29:05 -0700357 int nargs, char **args)
358{
359 printf("[ %s %s ]\n", args[0],
360 nargs > 1 ? args[1] : "");
361 switch(kw) {
362 case K_service:
363 state->context = parse_service(state, nargs, args);
364 if (state->context) {
365 state->parse_line = parse_line_service;
366 return;
367 }
368 break;
369 case K_on:
370 state->context = parse_action(state, nargs, args);
371 if (state->context) {
372 state->parse_line = parse_line_action;
373 return;
374 }
375 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700376 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800377 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800378 break;
Colin Cross6310a822010-04-20 14:29:05 -0700379 }
380 state->parse_line = parse_line_no_op;
381}
382
Elliott Hughesf682b472015-02-06 12:19:48 -0800383static void parse_config(const char *fn, const std::string& data)
Colin Cross6310a822010-04-20 14:29:05 -0700384{
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800385 struct listnode import_list;
386 struct listnode *node;
Colin Cross6310a822010-04-20 14:29:05 -0700387 char *args[INIT_PARSER_MAXARGS];
Colin Cross6310a822010-04-20 14:29:05 -0700388
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700389 int nargs = 0;
390
391 parse_state state;
Colin Cross6310a822010-04-20 14:29:05 -0700392 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800393 state.line = 0;
Elliott Hughesf682b472015-02-06 12:19:48 -0800394 state.ptr = strdup(data.c_str()); // TODO: fix this code!
Colin Cross6310a822010-04-20 14:29:05 -0700395 state.nexttoken = 0;
396 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800397
398 list_init(&import_list);
399 state.priv = &import_list;
400
Colin Cross6310a822010-04-20 14:29:05 -0700401 for (;;) {
402 switch (next_token(&state)) {
403 case T_EOF:
404 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800405 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700406 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800407 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700408 if (nargs) {
409 int kw = lookup_keyword(args[0]);
410 if (kw_is(kw, SECTION)) {
411 state.parse_line(&state, 0, 0);
412 parse_new_section(&state, kw, nargs, args);
413 } else {
414 state.parse_line(&state, nargs, args);
415 }
416 nargs = 0;
417 }
418 break;
419 case T_TEXT:
420 if (nargs < INIT_PARSER_MAXARGS) {
421 args[nargs++] = state.text;
422 }
423 break;
424 }
425 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800426
427parser_done:
428 list_for_each(node, &import_list) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700429 struct import* import = node_to_item(node, struct import, list);
430 if (!init_parse_config_file(import->filename)) {
431 ERROR("could not import file '%s' from '%s': %s\n",
432 import->filename, fn, strerror(errno));
433 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800434 }
Colin Cross6310a822010-04-20 14:29:05 -0700435}
436
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700437bool init_parse_config_file(const char* path) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800438 INFO("Parsing %s...\n", path);
Elliott Hughesda40c002015-03-27 23:20:44 -0700439 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800440 std::string data;
441 if (!read_file(path, &data)) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700442 return false;
Elliott Hughesf682b472015-02-06 12:19:48 -0800443 }
Colin Cross6310a822010-04-20 14:29:05 -0700444
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700445 data.push_back('\n'); // TODO: fix parse_config.
Elliott Hughesf682b472015-02-06 12:19:48 -0800446 parse_config(path, data);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800447 dump_parser_state();
Elliott Hughesda40c002015-03-27 23:20:44 -0700448
449 NOTICE("(Parsing %s took %.2fs.)\n", path, t.duration());
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700450 return true;
Colin Cross6310a822010-04-20 14:29:05 -0700451}
452
453static int valid_name(const char *name)
454{
455 if (strlen(name) > 16) {
456 return 0;
457 }
458 while (*name) {
459 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
460 return 0;
461 }
462 name++;
463 }
464 return 1;
465}
466
467struct service *service_find_by_name(const char *name)
468{
469 struct listnode *node;
470 struct service *svc;
471 list_for_each(node, &service_list) {
472 svc = node_to_item(node, struct service, slist);
473 if (!strcmp(svc->name, name)) {
474 return svc;
475 }
476 }
477 return 0;
478}
479
480struct service *service_find_by_pid(pid_t pid)
481{
482 struct listnode *node;
483 struct service *svc;
484 list_for_each(node, &service_list) {
485 svc = node_to_item(node, struct service, slist);
486 if (svc->pid == pid) {
487 return svc;
488 }
489 }
490 return 0;
491}
492
493struct service *service_find_by_keychord(int keychord_id)
494{
495 struct listnode *node;
496 struct service *svc;
497 list_for_each(node, &service_list) {
498 svc = node_to_item(node, struct service, slist);
499 if (svc->keychord_id == keychord_id) {
500 return svc;
501 }
502 }
503 return 0;
504}
505
506void service_for_each(void (*func)(struct service *svc))
507{
508 struct listnode *node;
509 struct service *svc;
510 list_for_each(node, &service_list) {
511 svc = node_to_item(node, struct service, slist);
512 func(svc);
513 }
514}
515
516void service_for_each_class(const char *classname,
517 void (*func)(struct service *svc))
518{
519 struct listnode *node;
520 struct service *svc;
521 list_for_each(node, &service_list) {
522 svc = node_to_item(node, struct service, slist);
523 if (!strcmp(svc->classname, classname)) {
524 func(svc);
525 }
526 }
527}
528
529void service_for_each_flags(unsigned matchflags,
530 void (*func)(struct service *svc))
531{
532 struct listnode *node;
533 struct service *svc;
534 list_for_each(node, &service_list) {
535 svc = node_to_item(node, struct service, slist);
536 if (svc->flags & matchflags) {
537 func(svc);
538 }
539 }
540}
541
542void action_for_each_trigger(const char *trigger,
543 void (*func)(struct action *act))
544{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700545 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700546 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700547 struct trigger *cur_trigger;
548
Colin Cross6310a822010-04-20 14:29:05 -0700549 list_for_each(node, &action_list) {
550 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700551 list_for_each(node2, &act->triggers) {
552 cur_trigger = node_to_item(node2, struct trigger, nlist);
553 if (!strcmp(cur_trigger->name, trigger)) {
554 func(act);
555 }
Colin Cross6310a822010-04-20 14:29:05 -0700556 }
557 }
558}
559
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700560
Colin Cross6310a822010-04-20 14:29:05 -0700561void queue_property_triggers(const char *name, const char *value)
562{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700563 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700564 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700565 struct trigger *cur_trigger;
566 bool match;
567 int name_length;
568
Colin Cross6310a822010-04-20 14:29:05 -0700569 list_for_each(node, &action_list) {
570 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700571 match = !name;
572 list_for_each(node2, &act->triggers) {
573 cur_trigger = node_to_item(node2, struct trigger, nlist);
574 if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) {
575 const char *test = cur_trigger->name + strlen("property:");
576 if (!match) {
577 name_length = strlen(name);
578 if (!strncmp(name, test, name_length) &&
579 test[name_length] == '=' &&
580 (!strcmp(test + name_length + 1, value) ||
581 !strcmp(test + name_length + 1, "*"))) {
582 match = true;
583 continue;
584 }
585 } else {
586 const char* equals = strchr(test, '=');
587 if (equals) {
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700588 int length = equals - test;
589 if (length <= PROP_NAME_MAX) {
Yabin Cui74edcea2015-07-24 10:11:05 -0700590 std::string prop_name(test, length);
591 std::string value = property_get(prop_name.c_str());
Colin Cross6310a822010-04-20 14:29:05 -0700592
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700593 /* does the property exist, and match the trigger value? */
Yabin Cui74edcea2015-07-24 10:11:05 -0700594 if (!value.empty() && (!strcmp(equals + 1, value.c_str()) ||
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700595 !strcmp(equals + 1, "*"))) {
596 continue;
597 }
598 }
599 }
600 }
601 }
602 match = false;
603 break;
604 }
605 if (match) {
606 action_add_queue_tail(act);
Colin Cross6310a822010-04-20 14:29:05 -0700607 }
608 }
609}
610
611void queue_all_property_triggers()
612{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700613 queue_property_triggers(NULL, NULL);
Colin Cross6310a822010-04-20 14:29:05 -0700614}
615
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800616void queue_builtin_action(int (*func)(int nargs, char **args), const char *name)
Colin Cross6310a822010-04-20 14:29:05 -0700617{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800618 action* act = (action*) calloc(1, sizeof(*act));
619 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700620 cur_trigger->name = name;
621 list_init(&act->triggers);
622 list_add_tail(&act->triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700623 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800624 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700625
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800626 command* cmd = (command*) calloc(1, sizeof(*cmd));
Colin Cross6310a822010-04-20 14:29:05 -0700627 cmd->func = func;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800628 cmd->args[0] = const_cast<char*>(name);
Riley Andrews24a3b782014-06-26 13:56:01 -0700629 cmd->nargs = 1;
Colin Cross6310a822010-04-20 14:29:05 -0700630 list_add_tail(&act->commands, &cmd->clist);
631
632 list_add_tail(&action_list, &act->alist);
633 action_add_queue_tail(act);
634}
635
636void action_add_queue_tail(struct action *act)
637{
Colin Crossa5064622013-03-07 13:42:29 -0800638 if (list_empty(&act->qlist)) {
639 list_add_tail(&action_queue, &act->qlist);
640 }
Colin Cross6310a822010-04-20 14:29:05 -0700641}
642
643struct action *action_remove_queue_head(void)
644{
645 if (list_empty(&action_queue)) {
646 return 0;
647 } else {
648 struct listnode *node = list_head(&action_queue);
649 struct action *act = node_to_item(node, struct action, qlist);
650 list_remove(node);
Colin Crossa5064622013-03-07 13:42:29 -0800651 list_init(node);
Colin Cross6310a822010-04-20 14:29:05 -0700652 return act;
653 }
654}
655
656int action_queue_empty()
657{
658 return list_empty(&action_queue);
659}
660
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800661service* make_exec_oneshot_service(int nargs, char** args) {
662 // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS...
Mark Salyzyn17fff892015-06-02 11:11:02 -0700663 // SECLABEL can be a - to denote default
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800664 int command_arg = 1;
665 for (int i = 1; i < nargs; ++i) {
666 if (strcmp(args[i], "--") == 0) {
667 command_arg = i + 1;
668 break;
669 }
670 }
671 if (command_arg > 4 + NR_SVC_SUPP_GIDS) {
672 ERROR("exec called with too many supplementary group ids\n");
673 return NULL;
674 }
675
676 int argc = nargs - command_arg;
677 char** argv = (args + command_arg);
678 if (argc < 1) {
679 ERROR("exec called without command\n");
680 return NULL;
681 }
682
683 service* svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * argc);
684 if (svc == NULL) {
685 ERROR("Couldn't allocate service for exec of '%s': %s", argv[0], strerror(errno));
686 return NULL;
687 }
688
Mark Salyzyn17fff892015-06-02 11:11:02 -0700689 if ((command_arg > 2) && strcmp(args[1], "-")) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800690 svc->seclabel = args[1];
691 }
692 if (command_arg > 3) {
693 svc->uid = decode_uid(args[2]);
694 }
695 if (command_arg > 4) {
696 svc->gid = decode_uid(args[3]);
697 svc->nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */;
698 for (size_t i = 0; i < svc->nr_supp_gids; ++i) {
699 svc->supp_gids[i] = decode_uid(args[4 + i]);
700 }
701 }
702
703 static int exec_count; // Every service needs a unique name.
704 char* name = NULL;
705 asprintf(&name, "exec %d (%s)", exec_count++, argv[0]);
706 if (name == NULL) {
707 ERROR("Couldn't allocate name for exec service '%s'\n", argv[0]);
708 free(svc);
709 return NULL;
710 }
711 svc->name = name;
712 svc->classname = "default";
713 svc->flags = SVC_EXEC | SVC_ONESHOT;
714 svc->nargs = argc;
715 memcpy(svc->args, argv, sizeof(char*) * svc->nargs);
716 svc->args[argc] = NULL;
717 list_add_tail(&service_list, &svc->slist);
718 return svc;
719}
720
Colin Cross6310a822010-04-20 14:29:05 -0700721static void *parse_service(struct parse_state *state, int nargs, char **args)
722{
Colin Cross6310a822010-04-20 14:29:05 -0700723 if (nargs < 3) {
724 parse_error(state, "services must have a name and a program\n");
725 return 0;
726 }
727 if (!valid_name(args[1])) {
728 parse_error(state, "invalid service name '%s'\n", args[1]);
729 return 0;
730 }
731
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800732 service* svc = (service*) service_find_by_name(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700733 if (svc) {
734 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
735 return 0;
736 }
737
738 nargs -= 2;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800739 svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700740 if (!svc) {
741 parse_error(state, "out of memory\n");
742 return 0;
743 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800744 svc->name = strdup(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700745 svc->classname = "default";
746 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800747 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Colin Cross6310a822010-04-20 14:29:05 -0700748 svc->args[nargs] = 0;
749 svc->nargs = nargs;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700750 list_init(&svc->onrestart.triggers);
751 cur_trigger->name = "onrestart";
752 list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700753 list_init(&svc->onrestart.commands);
754 list_add_tail(&service_list, &svc->slist);
755 return svc;
756}
757
758static void parse_line_service(struct parse_state *state, int nargs, char **args)
759{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800760 struct service *svc = (service*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700761 struct command *cmd;
762 int i, kw, kw_nargs;
763
764 if (nargs == 0) {
765 return;
766 }
767
768 svc->ioprio_class = IoSchedClass_NONE;
769
770 kw = lookup_keyword(args[0]);
771 switch (kw) {
Colin Cross6310a822010-04-20 14:29:05 -0700772 case K_class:
773 if (nargs != 2) {
774 parse_error(state, "class option requires a classname\n");
775 } else {
776 svc->classname = args[1];
777 }
778 break;
779 case K_console:
780 svc->flags |= SVC_CONSOLE;
781 break;
782 case K_disabled:
783 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700784 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700785 break;
786 case K_ioprio:
787 if (nargs != 3) {
788 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
789 } else {
790 svc->ioprio_pri = strtoul(args[2], 0, 8);
791
792 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
793 parse_error(state, "priority value must be range 0 - 7\n");
794 break;
795 }
796
797 if (!strcmp(args[1], "rt")) {
798 svc->ioprio_class = IoSchedClass_RT;
799 } else if (!strcmp(args[1], "be")) {
800 svc->ioprio_class = IoSchedClass_BE;
801 } else if (!strcmp(args[1], "idle")) {
802 svc->ioprio_class = IoSchedClass_IDLE;
803 } else {
804 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
805 }
806 }
807 break;
808 case K_group:
809 if (nargs < 2) {
810 parse_error(state, "group option requires a group id\n");
811 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
812 parse_error(state, "group option accepts at most %d supp. groups\n",
813 NR_SVC_SUPP_GIDS);
814 } else {
815 int n;
816 svc->gid = decode_uid(args[1]);
817 for (n = 2; n < nargs; n++) {
818 svc->supp_gids[n-2] = decode_uid(args[n]);
819 }
820 svc->nr_supp_gids = n - 2;
821 }
822 break;
823 case K_keycodes:
824 if (nargs < 2) {
825 parse_error(state, "keycodes option requires atleast one keycode\n");
826 } else {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800827 svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
Colin Cross6310a822010-04-20 14:29:05 -0700828 if (!svc->keycodes) {
829 parse_error(state, "could not allocate keycodes\n");
830 } else {
831 svc->nkeycodes = nargs - 1;
832 for (i = 1; i < nargs; i++) {
833 svc->keycodes[i - 1] = atoi(args[i]);
834 }
835 }
836 }
837 break;
838 case K_oneshot:
839 svc->flags |= SVC_ONESHOT;
840 break;
841 case K_onrestart:
842 nargs--;
843 args++;
844 kw = lookup_keyword(args[0]);
845 if (!kw_is(kw, COMMAND)) {
846 parse_error(state, "invalid command '%s'\n", args[0]);
847 break;
848 }
849 kw_nargs = kw_nargs(kw);
850 if (nargs < kw_nargs) {
851 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
852 kw_nargs > 2 ? "arguments" : "argument");
853 break;
854 }
855
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800856 cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700857 cmd->func = kw_func(kw);
858 cmd->nargs = nargs;
859 memcpy(cmd->args, args, sizeof(char*) * nargs);
860 list_add_tail(&svc->onrestart.commands, &cmd->clist);
861 break;
862 case K_critical:
863 svc->flags |= SVC_CRITICAL;
864 break;
865 case K_setenv: { /* name value */
Gavin.Changc3a46762013-09-28 00:22:11 +0800866 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700867 parse_error(state, "setenv option requires name and value arguments\n");
868 break;
869 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800870 svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
Colin Cross6310a822010-04-20 14:29:05 -0700871 if (!ei) {
872 parse_error(state, "out of memory\n");
873 break;
874 }
875 ei->name = args[1];
876 ei->value = args[2];
877 ei->next = svc->envvars;
878 svc->envvars = ei;
879 break;
880 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400881 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700882 if (nargs < 4) {
883 parse_error(state, "socket option requires name, type, perm arguments\n");
884 break;
885 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400886 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
887 && strcmp(args[2],"seqpacket")) {
888 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700889 break;
890 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800891 socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
Colin Cross6310a822010-04-20 14:29:05 -0700892 if (!si) {
893 parse_error(state, "out of memory\n");
894 break;
895 }
896 si->name = args[1];
897 si->type = args[2];
898 si->perm = strtoul(args[3], 0, 8);
899 if (nargs > 4)
900 si->uid = decode_uid(args[4]);
901 if (nargs > 5)
902 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400903 if (nargs > 6)
904 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700905 si->next = svc->sockets;
906 svc->sockets = si;
907 break;
908 }
909 case K_user:
910 if (nargs != 2) {
911 parse_error(state, "user option requires a user id\n");
912 } else {
913 svc->uid = decode_uid(args[1]);
914 }
915 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500916 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500917 if (nargs != 2) {
918 parse_error(state, "seclabel option requires a label string\n");
919 } else {
920 svc->seclabel = args[1];
921 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500922 break;
Elliott Hughesd62f0602015-06-12 18:02:20 -0700923 case K_writepid:
924 if (nargs < 2) {
925 parse_error(state, "writepid option requires at least one filename\n");
926 break;
927 }
928 svc->writepid_files_ = new std::vector<std::string>;
929 for (int i = 1; i < nargs; ++i) {
930 svc->writepid_files_->push_back(args[i]);
931 }
932 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500933
Colin Cross6310a822010-04-20 14:29:05 -0700934 default:
935 parse_error(state, "invalid option '%s'\n", args[0]);
936 }
937}
938
939static void *parse_action(struct parse_state *state, int nargs, char **args)
940{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700941 struct trigger *cur_trigger;
942 int i;
Colin Cross6310a822010-04-20 14:29:05 -0700943 if (nargs < 2) {
944 parse_error(state, "actions must have a trigger\n");
945 return 0;
946 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700947
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800948 action* act = (action*) calloc(1, sizeof(*act));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700949 list_init(&act->triggers);
950
951 for (i = 1; i < nargs; i++) {
952 if (!(i % 2)) {
953 if (strcmp(args[i], "&&")) {
Tom Cherryae392cf2015-04-13 13:07:04 -0700954 struct listnode *node;
955 struct listnode *node2;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700956 parse_error(state, "& is the only symbol allowed to concatenate actions\n");
Tom Cherryae392cf2015-04-13 13:07:04 -0700957 list_for_each_safe(node, node2, &act->triggers) {
958 struct trigger *trigger = node_to_item(node, struct trigger, nlist);
959 free(trigger);
960 }
961 free(act);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700962 return 0;
963 } else
964 continue;
965 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800966 cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700967 cur_trigger->name = args[i];
968 list_add_tail(&act->triggers, &cur_trigger->nlist);
969 }
970
Colin Cross6310a822010-04-20 14:29:05 -0700971 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800972 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700973 list_add_tail(&action_list, &act->alist);
974 /* XXX add to hash */
975 return act;
976}
977
978static void parse_line_action(struct parse_state* state, int nargs, char **args)
979{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800980 struct action *act = (action*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700981 int kw, n;
982
983 if (nargs == 0) {
984 return;
985 }
986
987 kw = lookup_keyword(args[0]);
988 if (!kw_is(kw, COMMAND)) {
989 parse_error(state, "invalid command '%s'\n", args[0]);
990 return;
991 }
992
993 n = kw_nargs(kw);
994 if (nargs < n) {
995 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
996 n > 2 ? "arguments" : "argument");
997 return;
998 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800999 command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -07001000 cmd->func = kw_func(kw);
Riley Andrews24a3b782014-06-26 13:56:01 -07001001 cmd->line = state->line;
1002 cmd->filename = state->filename;
Colin Cross6310a822010-04-20 14:29:05 -07001003 cmd->nargs = nargs;
1004 memcpy(cmd->args, args, sizeof(char*) * nargs);
1005 list_add_tail(&act->commands, &cmd->clist);
1006}