blob: 57eb29957079c5ae1bb977b2118712d10ca3a70f [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 Hughes8d82ea02015-02-06 20:15:18 -080017#include <errno.h>
Colin Cross6310a822010-04-20 14:29:05 -070018#include <stdio.h>
19#include <stdlib.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <stdarg.h>
23#include <string.h>
24#include <stddef.h>
25#include <ctype.h>
26
27#include "init.h"
28#include "parser.h"
29#include "init_parser.h"
30#include "log.h"
Colin Cross6310a822010-04-20 14:29:05 -070031#include "property_service.h"
32#include "util.h"
33
34#include <cutils/iosched_policy.h>
Dima Zavinda04c522011-09-01 17:09:44 -070035#include <cutils/list.h>
Colin Cross6310a822010-04-20 14:29:05 -070036
Colin Cross6310a822010-04-20 14:29:05 -070037static list_declare(service_list);
38static list_declare(action_list);
39static list_declare(action_queue);
40
Dima Zavin78a1b1f2011-12-20 12:53:48 -080041struct import {
42 struct listnode list;
43 const char *filename;
44};
45
Colin Cross6310a822010-04-20 14:29:05 -070046static void *parse_service(struct parse_state *state, int nargs, char **args);
47static void parse_line_service(struct parse_state *state, int nargs, char **args);
48
49static void *parse_action(struct parse_state *state, int nargs, char **args);
50static void parse_line_action(struct parse_state *state, int nargs, char **args);
51
52#define SECTION 0x01
53#define COMMAND 0x02
54#define OPTION 0x04
55
56#include "keywords.h"
57
58#define KEYWORD(symbol, flags, nargs, func) \
59 [ K_##symbol ] = { #symbol, func, nargs + 1, flags, },
60
Greg Hackmannd68db712013-11-18 09:44:20 -080061static struct {
Colin Cross6310a822010-04-20 14:29:05 -070062 const char *name;
63 int (*func)(int nargs, char **args);
64 unsigned char nargs;
65 unsigned char flags;
66} keyword_info[KEYWORD_COUNT] = {
67 [ K_UNKNOWN ] = { "unknown", 0, 0, 0 },
68#include "keywords.h"
69};
70#undef KEYWORD
71
72#define kw_is(kw, type) (keyword_info[kw].flags & (type))
73#define kw_name(kw) (keyword_info[kw].name)
74#define kw_func(kw) (keyword_info[kw].func)
75#define kw_nargs(kw) (keyword_info[kw].nargs)
76
Elliott Hughesc0e919c2015-02-04 14:46:36 -080077void dump_parser_state() {
78 if (false) {
79 struct listnode* node;
80 list_for_each(node, &service_list) {
81 service* svc = node_to_item(node, struct service, slist);
82 INFO("service %s\n", svc->name);
83 INFO(" class '%s'\n", svc->classname);
84 INFO(" exec");
85 for (int n = 0; n < svc->nargs; n++) {
86 INFO(" '%s'", svc->args[n]);
87 }
88 INFO("\n");
89 for (socketinfo* si = svc->sockets; si; si = si->next) {
90 INFO(" socket %s %s 0%o\n", si->name, si->type, si->perm);
91 }
92 }
93
94 list_for_each(node, &action_list) {
95 action* act = node_to_item(node, struct action, alist);
96 INFO("on ");
97 char name_str[256] = "";
98 build_triggers_string(name_str, sizeof(name_str), act);
99 INFO("%s", name_str);
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 Hackmannd68db712013-11-18 09:44:20 -0800116static int lookup_keyword(const char *s)
Colin Cross6310a822010-04-20 14:29:05 -0700117{
118 switch (*s++) {
Yongqin Liua197ff12014-12-05 13:45:02 +0800119 case 'b':
120 if (!strcmp(s, "ootchart_init")) return K_bootchart_init;
Mark Salyzyn7a3d66c2015-03-24 07:29:22 -0700121 break;
Colin Cross6310a822010-04-20 14:29:05 -0700122 case 'c':
Elliott Hughesf682b472015-02-06 12:19:48 -0800123 if (!strcmp(s, "opy")) return K_copy;
Colin Cross6310a822010-04-20 14:29:05 -0700124 if (!strcmp(s, "apability")) return K_capability;
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;
San Mehat429721c2014-09-23 07:48:47 -0700141 if (!strcmp(s, "xeconce")) return K_execonce;
Colin Cross6310a822010-04-20 14:29:05 -0700142 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;
155 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;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700162 if (!strcmp(s, "oad_all_props")) return K_load_all_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;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500187 if (!strcmp(s, "etcon")) return K_setcon;
Colin Cross6310a822010-04-20 14:29:05 -0700188 if (!strcmp(s, "etenv")) return K_setenv;
Colin Cross6310a822010-04-20 14:29:05 -0700189 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 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;
210 if (!strcmp(s, "ait")) return K_wait;
211 break;
212 }
213 return K_UNKNOWN;
214}
215
Elliott Hughesf682b472015-02-06 12:19:48 -0800216static void parse_line_no_op(struct parse_state*, int, char**) {
Colin Cross6310a822010-04-20 14:29:05 -0700217}
218
Dima Zavina6235ea2011-12-16 14:20:12 -0800219static int push_chars(char **dst, int *len, const char *chars, int cnt)
220{
221 if (cnt > *len)
222 return -1;
223
224 memcpy(*dst, chars, cnt);
225 *dst += cnt;
226 *len -= cnt;
227
228 return 0;
229}
230
Dima Zavin84bf9af2011-12-20 13:44:41 -0800231int expand_props(char *dst, const char *src, int dst_size)
Dima Zavina6235ea2011-12-16 14:20:12 -0800232{
Dima Zavina6235ea2011-12-16 14:20:12 -0800233 char *dst_ptr = dst;
234 const char *src_ptr = src;
Dima Zavina6235ea2011-12-16 14:20:12 -0800235 int ret = 0;
236 int left = dst_size - 1;
237
238 if (!src || !dst || dst_size == 0)
239 return -1;
240
Dima Zavina6235ea2011-12-16 14:20:12 -0800241 /* - variables can either be $x.y or ${x.y}, in case they are only part
242 * of the string.
243 * - will accept $$ as a literal $.
244 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
245 * bad things will happen
246 */
247 while (*src_ptr && left > 0) {
248 char *c;
249 char prop[PROP_NAME_MAX + 1];
Colin Cross2deedfe2013-01-28 17:13:35 -0800250 char prop_val[PROP_VALUE_MAX];
Dima Zavina6235ea2011-12-16 14:20:12 -0800251 int prop_len = 0;
Colin Cross2deedfe2013-01-28 17:13:35 -0800252 int prop_val_len;
Dima Zavina6235ea2011-12-16 14:20:12 -0800253
254 c = strchr(src_ptr, '$');
255 if (!c) {
256 while (left-- > 0 && *src_ptr)
257 *(dst_ptr++) = *(src_ptr++);
258 break;
259 }
260
261 memset(prop, 0, sizeof(prop));
262
263 ret = push_chars(&dst_ptr, &left, src_ptr, c - src_ptr);
264 if (ret < 0)
265 goto err_nospace;
266 c++;
267
268 if (*c == '$') {
269 *(dst_ptr++) = *(c++);
270 src_ptr = c;
271 left--;
272 continue;
273 } else if (*c == '\0') {
274 break;
275 }
276
277 if (*c == '{') {
278 c++;
279 while (*c && *c != '}' && prop_len < PROP_NAME_MAX)
280 prop[prop_len++] = *(c++);
281 if (*c != '}') {
282 /* failed to find closing brace, abort. */
283 if (prop_len == PROP_NAME_MAX)
284 ERROR("prop name too long during expansion of '%s'\n",
285 src);
286 else if (*c == '\0')
287 ERROR("unexpected end of string in '%s', looking for }\n",
288 src);
289 goto err;
290 }
291 prop[prop_len] = '\0';
292 c++;
293 } else if (*c) {
294 while (*c && prop_len < PROP_NAME_MAX)
295 prop[prop_len++] = *(c++);
296 if (prop_len == PROP_NAME_MAX && *c != '\0') {
297 ERROR("prop name too long in '%s'\n", src);
298 goto err;
299 }
300 prop[prop_len] = '\0';
301 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
302 prop);
303 }
304
305 if (prop_len == 0) {
306 ERROR("invalid zero-length prop name in '%s'\n", src);
307 goto err;
308 }
309
Colin Cross2deedfe2013-01-28 17:13:35 -0800310 prop_val_len = property_get(prop, prop_val);
311 if (!prop_val_len) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800312 ERROR("property '%s' doesn't exist while expanding '%s'\n",
313 prop, src);
314 goto err;
315 }
316
Colin Cross2deedfe2013-01-28 17:13:35 -0800317 ret = push_chars(&dst_ptr, &left, prop_val, prop_val_len);
Dima Zavina6235ea2011-12-16 14:20:12 -0800318 if (ret < 0)
319 goto err_nospace;
320 src_ptr = c;
321 continue;
322 }
323
324 *dst_ptr = '\0';
325 return 0;
326
327err_nospace:
328 ERROR("destination buffer overflow while expanding '%s'\n", src);
329err:
330 return -1;
331}
332
Greg Hackmannd68db712013-11-18 09:44:20 -0800333static void parse_import(struct parse_state *state, int nargs, char **args)
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800334{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800335 struct listnode *import_list = (listnode*) state->priv;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800336 char conf_file[PATH_MAX];
337 int ret;
338
339 if (nargs != 2) {
340 ERROR("single argument needed for import\n");
341 return;
342 }
343
344 ret = expand_props(conf_file, args[1], sizeof(conf_file));
345 if (ret) {
346 ERROR("error while handling import on line '%d' in '%s'\n",
347 state->line, state->filename);
348 return;
349 }
350
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800351 struct import* import = (struct import*) calloc(1, sizeof(struct import));
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800352 import->filename = strdup(conf_file);
353 list_add_tail(import_list, &import->list);
354 INFO("found import '%s', adding to import list", import->filename);
355}
356
Greg Hackmannd68db712013-11-18 09:44:20 -0800357static void parse_new_section(struct parse_state *state, int kw,
Colin Cross6310a822010-04-20 14:29:05 -0700358 int nargs, char **args)
359{
360 printf("[ %s %s ]\n", args[0],
361 nargs > 1 ? args[1] : "");
362 switch(kw) {
363 case K_service:
364 state->context = parse_service(state, nargs, args);
365 if (state->context) {
366 state->parse_line = parse_line_service;
367 return;
368 }
369 break;
370 case K_on:
371 state->context = parse_action(state, nargs, args);
372 if (state->context) {
373 state->parse_line = parse_line_action;
374 return;
375 }
376 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700377 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800378 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800379 break;
Colin Cross6310a822010-04-20 14:29:05 -0700380 }
381 state->parse_line = parse_line_no_op;
382}
383
Elliott Hughesf682b472015-02-06 12:19:48 -0800384static void parse_config(const char *fn, const std::string& data)
Colin Cross6310a822010-04-20 14:29:05 -0700385{
386 struct parse_state state;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800387 struct listnode import_list;
388 struct listnode *node;
Colin Cross6310a822010-04-20 14:29:05 -0700389 char *args[INIT_PARSER_MAXARGS];
390 int nargs;
391
392 nargs = 0;
393 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800394 state.line = 0;
Elliott Hughesf682b472015-02-06 12:19:48 -0800395 state.ptr = strdup(data.c_str()); // TODO: fix this code!
Colin Cross6310a822010-04-20 14:29:05 -0700396 state.nexttoken = 0;
397 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800398
399 list_init(&import_list);
400 state.priv = &import_list;
401
Colin Cross6310a822010-04-20 14:29:05 -0700402 for (;;) {
403 switch (next_token(&state)) {
404 case T_EOF:
405 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800406 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700407 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800408 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700409 if (nargs) {
410 int kw = lookup_keyword(args[0]);
411 if (kw_is(kw, SECTION)) {
412 state.parse_line(&state, 0, 0);
413 parse_new_section(&state, kw, nargs, args);
414 } else {
415 state.parse_line(&state, nargs, args);
416 }
417 nargs = 0;
418 }
419 break;
420 case T_TEXT:
421 if (nargs < INIT_PARSER_MAXARGS) {
422 args[nargs++] = state.text;
423 }
424 break;
425 }
426 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800427
428parser_done:
429 list_for_each(node, &import_list) {
430 struct import *import = node_to_item(node, struct import, list);
431 int ret;
432
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800433 ret = init_parse_config_file(import->filename);
434 if (ret)
435 ERROR("could not import file '%s' from '%s'\n",
436 import->filename, fn);
437 }
Colin Cross6310a822010-04-20 14:29:05 -0700438}
439
Elliott Hughesf682b472015-02-06 12:19:48 -0800440int init_parse_config_file(const char* path) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800441 INFO("Parsing %s...\n", path);
Elliott Hughesf682b472015-02-06 12:19:48 -0800442 std::string data;
443 if (!read_file(path, &data)) {
444 return -1;
445 }
Colin Cross6310a822010-04-20 14:29:05 -0700446
Elliott Hughesf682b472015-02-06 12:19:48 -0800447 parse_config(path, data);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800448 dump_parser_state();
Colin Cross6310a822010-04-20 14:29:05 -0700449 return 0;
450}
451
452static int valid_name(const char *name)
453{
454 if (strlen(name) > 16) {
455 return 0;
456 }
457 while (*name) {
458 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
459 return 0;
460 }
461 name++;
462 }
463 return 1;
464}
465
466struct service *service_find_by_name(const char *name)
467{
468 struct listnode *node;
469 struct service *svc;
470 list_for_each(node, &service_list) {
471 svc = node_to_item(node, struct service, slist);
472 if (!strcmp(svc->name, name)) {
473 return svc;
474 }
475 }
476 return 0;
477}
478
479struct service *service_find_by_pid(pid_t pid)
480{
481 struct listnode *node;
482 struct service *svc;
483 list_for_each(node, &service_list) {
484 svc = node_to_item(node, struct service, slist);
485 if (svc->pid == pid) {
486 return svc;
487 }
488 }
489 return 0;
490}
491
492struct service *service_find_by_keychord(int keychord_id)
493{
494 struct listnode *node;
495 struct service *svc;
496 list_for_each(node, &service_list) {
497 svc = node_to_item(node, struct service, slist);
498 if (svc->keychord_id == keychord_id) {
499 return svc;
500 }
501 }
502 return 0;
503}
504
505void service_for_each(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 func(svc);
512 }
513}
514
515void service_for_each_class(const char *classname,
516 void (*func)(struct service *svc))
517{
518 struct listnode *node;
519 struct service *svc;
520 list_for_each(node, &service_list) {
521 svc = node_to_item(node, struct service, slist);
522 if (!strcmp(svc->classname, classname)) {
523 func(svc);
524 }
525 }
526}
527
528void service_for_each_flags(unsigned matchflags,
529 void (*func)(struct service *svc))
530{
531 struct listnode *node;
532 struct service *svc;
533 list_for_each(node, &service_list) {
534 svc = node_to_item(node, struct service, slist);
535 if (svc->flags & matchflags) {
536 func(svc);
537 }
538 }
539}
540
541void action_for_each_trigger(const char *trigger,
542 void (*func)(struct action *act))
543{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700544 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700545 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700546 struct trigger *cur_trigger;
547
Colin Cross6310a822010-04-20 14:29:05 -0700548 list_for_each(node, &action_list) {
549 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700550 list_for_each(node2, &act->triggers) {
551 cur_trigger = node_to_item(node2, struct trigger, nlist);
552 if (!strcmp(cur_trigger->name, trigger)) {
553 func(act);
554 }
Colin Cross6310a822010-04-20 14:29:05 -0700555 }
556 }
557}
558
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700559
Colin Cross6310a822010-04-20 14:29:05 -0700560void queue_property_triggers(const char *name, const char *value)
561{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700562 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700563 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700564 struct trigger *cur_trigger;
565 bool match;
566 int name_length;
567
Colin Cross6310a822010-04-20 14:29:05 -0700568 list_for_each(node, &action_list) {
569 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700570 match = !name;
571 list_for_each(node2, &act->triggers) {
572 cur_trigger = node_to_item(node2, struct trigger, nlist);
573 if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) {
574 const char *test = cur_trigger->name + strlen("property:");
575 if (!match) {
576 name_length = strlen(name);
577 if (!strncmp(name, test, name_length) &&
578 test[name_length] == '=' &&
579 (!strcmp(test + name_length + 1, value) ||
580 !strcmp(test + name_length + 1, "*"))) {
581 match = true;
582 continue;
583 }
584 } else {
585 const char* equals = strchr(test, '=');
586 if (equals) {
587 char prop_name[PROP_NAME_MAX + 1];
588 char value[PROP_VALUE_MAX];
589 int length = equals - test;
590 if (length <= PROP_NAME_MAX) {
591 int ret;
592 memcpy(prop_name, test, length);
593 prop_name[length] = 0;
Colin Cross6310a822010-04-20 14:29:05 -0700594
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700595 /* does the property exist, and match the trigger value? */
596 ret = property_get(prop_name, value);
597 if (ret > 0 && (!strcmp(equals + 1, value) ||
598 !strcmp(equals + 1, "*"))) {
599 continue;
600 }
601 }
602 }
603 }
604 }
605 match = false;
606 break;
607 }
608 if (match) {
609 action_add_queue_tail(act);
Colin Cross6310a822010-04-20 14:29:05 -0700610 }
611 }
612}
613
614void queue_all_property_triggers()
615{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700616 queue_property_triggers(NULL, NULL);
Colin Cross6310a822010-04-20 14:29:05 -0700617}
618
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800619void queue_builtin_action(int (*func)(int nargs, char **args), const char *name)
Colin Cross6310a822010-04-20 14:29:05 -0700620{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800621 action* act = (action*) calloc(1, sizeof(*act));
622 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700623 cur_trigger->name = name;
624 list_init(&act->triggers);
625 list_add_tail(&act->triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700626 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800627 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700628
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800629 command* cmd = (command*) calloc(1, sizeof(*cmd));
Colin Cross6310a822010-04-20 14:29:05 -0700630 cmd->func = func;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800631 cmd->args[0] = const_cast<char*>(name);
Riley Andrews24a3b782014-06-26 13:56:01 -0700632 cmd->nargs = 1;
Colin Cross6310a822010-04-20 14:29:05 -0700633 list_add_tail(&act->commands, &cmd->clist);
634
635 list_add_tail(&action_list, &act->alist);
636 action_add_queue_tail(act);
637}
638
639void action_add_queue_tail(struct action *act)
640{
Colin Crossa5064622013-03-07 13:42:29 -0800641 if (list_empty(&act->qlist)) {
642 list_add_tail(&action_queue, &act->qlist);
643 }
Colin Cross6310a822010-04-20 14:29:05 -0700644}
645
646struct action *action_remove_queue_head(void)
647{
648 if (list_empty(&action_queue)) {
649 return 0;
650 } else {
651 struct listnode *node = list_head(&action_queue);
652 struct action *act = node_to_item(node, struct action, qlist);
653 list_remove(node);
Colin Crossa5064622013-03-07 13:42:29 -0800654 list_init(node);
Colin Cross6310a822010-04-20 14:29:05 -0700655 return act;
656 }
657}
658
659int action_queue_empty()
660{
661 return list_empty(&action_queue);
662}
663
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800664service* make_exec_oneshot_service(int nargs, char** args) {
665 // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS...
666 int command_arg = 1;
667 for (int i = 1; i < nargs; ++i) {
668 if (strcmp(args[i], "--") == 0) {
669 command_arg = i + 1;
670 break;
671 }
672 }
673 if (command_arg > 4 + NR_SVC_SUPP_GIDS) {
674 ERROR("exec called with too many supplementary group ids\n");
675 return NULL;
676 }
677
678 int argc = nargs - command_arg;
679 char** argv = (args + command_arg);
680 if (argc < 1) {
681 ERROR("exec called without command\n");
682 return NULL;
683 }
684
685 service* svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * argc);
686 if (svc == NULL) {
687 ERROR("Couldn't allocate service for exec of '%s': %s", argv[0], strerror(errno));
688 return NULL;
689 }
690
691 if (command_arg > 2) {
692 svc->seclabel = args[1];
693 }
694 if (command_arg > 3) {
695 svc->uid = decode_uid(args[2]);
696 }
697 if (command_arg > 4) {
698 svc->gid = decode_uid(args[3]);
699 svc->nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */;
700 for (size_t i = 0; i < svc->nr_supp_gids; ++i) {
701 svc->supp_gids[i] = decode_uid(args[4 + i]);
702 }
703 }
704
705 static int exec_count; // Every service needs a unique name.
706 char* name = NULL;
707 asprintf(&name, "exec %d (%s)", exec_count++, argv[0]);
708 if (name == NULL) {
709 ERROR("Couldn't allocate name for exec service '%s'\n", argv[0]);
710 free(svc);
711 return NULL;
712 }
713 svc->name = name;
714 svc->classname = "default";
715 svc->flags = SVC_EXEC | SVC_ONESHOT;
716 svc->nargs = argc;
717 memcpy(svc->args, argv, sizeof(char*) * svc->nargs);
718 svc->args[argc] = NULL;
719 list_add_tail(&service_list, &svc->slist);
720 return svc;
721}
722
Colin Cross6310a822010-04-20 14:29:05 -0700723static void *parse_service(struct parse_state *state, int nargs, char **args)
724{
Colin Cross6310a822010-04-20 14:29:05 -0700725 if (nargs < 3) {
726 parse_error(state, "services must have a name and a program\n");
727 return 0;
728 }
729 if (!valid_name(args[1])) {
730 parse_error(state, "invalid service name '%s'\n", args[1]);
731 return 0;
732 }
733
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800734 service* svc = (service*) service_find_by_name(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700735 if (svc) {
736 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
737 return 0;
738 }
739
740 nargs -= 2;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800741 svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700742 if (!svc) {
743 parse_error(state, "out of memory\n");
744 return 0;
745 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800746 svc->name = strdup(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700747 svc->classname = "default";
748 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800749 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Colin Cross6310a822010-04-20 14:29:05 -0700750 svc->args[nargs] = 0;
751 svc->nargs = nargs;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700752 list_init(&svc->onrestart.triggers);
753 cur_trigger->name = "onrestart";
754 list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700755 list_init(&svc->onrestart.commands);
756 list_add_tail(&service_list, &svc->slist);
757 return svc;
758}
759
760static void parse_line_service(struct parse_state *state, int nargs, char **args)
761{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800762 struct service *svc = (service*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700763 struct command *cmd;
764 int i, kw, kw_nargs;
765
766 if (nargs == 0) {
767 return;
768 }
769
770 svc->ioprio_class = IoSchedClass_NONE;
771
772 kw = lookup_keyword(args[0]);
773 switch (kw) {
774 case K_capability:
775 break;
776 case K_class:
777 if (nargs != 2) {
778 parse_error(state, "class option requires a classname\n");
779 } else {
780 svc->classname = args[1];
781 }
782 break;
783 case K_console:
784 svc->flags |= SVC_CONSOLE;
785 break;
786 case K_disabled:
787 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700788 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700789 break;
790 case K_ioprio:
791 if (nargs != 3) {
792 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
793 } else {
794 svc->ioprio_pri = strtoul(args[2], 0, 8);
795
796 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
797 parse_error(state, "priority value must be range 0 - 7\n");
798 break;
799 }
800
801 if (!strcmp(args[1], "rt")) {
802 svc->ioprio_class = IoSchedClass_RT;
803 } else if (!strcmp(args[1], "be")) {
804 svc->ioprio_class = IoSchedClass_BE;
805 } else if (!strcmp(args[1], "idle")) {
806 svc->ioprio_class = IoSchedClass_IDLE;
807 } else {
808 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
809 }
810 }
811 break;
812 case K_group:
813 if (nargs < 2) {
814 parse_error(state, "group option requires a group id\n");
815 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
816 parse_error(state, "group option accepts at most %d supp. groups\n",
817 NR_SVC_SUPP_GIDS);
818 } else {
819 int n;
820 svc->gid = decode_uid(args[1]);
821 for (n = 2; n < nargs; n++) {
822 svc->supp_gids[n-2] = decode_uid(args[n]);
823 }
824 svc->nr_supp_gids = n - 2;
825 }
826 break;
827 case K_keycodes:
828 if (nargs < 2) {
829 parse_error(state, "keycodes option requires atleast one keycode\n");
830 } else {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800831 svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
Colin Cross6310a822010-04-20 14:29:05 -0700832 if (!svc->keycodes) {
833 parse_error(state, "could not allocate keycodes\n");
834 } else {
835 svc->nkeycodes = nargs - 1;
836 for (i = 1; i < nargs; i++) {
837 svc->keycodes[i - 1] = atoi(args[i]);
838 }
839 }
840 }
841 break;
842 case K_oneshot:
843 svc->flags |= SVC_ONESHOT;
844 break;
845 case K_onrestart:
846 nargs--;
847 args++;
848 kw = lookup_keyword(args[0]);
849 if (!kw_is(kw, COMMAND)) {
850 parse_error(state, "invalid command '%s'\n", args[0]);
851 break;
852 }
853 kw_nargs = kw_nargs(kw);
854 if (nargs < kw_nargs) {
855 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
856 kw_nargs > 2 ? "arguments" : "argument");
857 break;
858 }
859
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800860 cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700861 cmd->func = kw_func(kw);
862 cmd->nargs = nargs;
863 memcpy(cmd->args, args, sizeof(char*) * nargs);
864 list_add_tail(&svc->onrestart.commands, &cmd->clist);
865 break;
866 case K_critical:
867 svc->flags |= SVC_CRITICAL;
868 break;
869 case K_setenv: { /* name value */
Gavin.Changc3a46762013-09-28 00:22:11 +0800870 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700871 parse_error(state, "setenv option requires name and value arguments\n");
872 break;
873 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800874 svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
Colin Cross6310a822010-04-20 14:29:05 -0700875 if (!ei) {
876 parse_error(state, "out of memory\n");
877 break;
878 }
879 ei->name = args[1];
880 ei->value = args[2];
881 ei->next = svc->envvars;
882 svc->envvars = ei;
883 break;
884 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400885 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700886 if (nargs < 4) {
887 parse_error(state, "socket option requires name, type, perm arguments\n");
888 break;
889 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400890 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
891 && strcmp(args[2],"seqpacket")) {
892 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700893 break;
894 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800895 socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
Colin Cross6310a822010-04-20 14:29:05 -0700896 if (!si) {
897 parse_error(state, "out of memory\n");
898 break;
899 }
900 si->name = args[1];
901 si->type = args[2];
902 si->perm = strtoul(args[3], 0, 8);
903 if (nargs > 4)
904 si->uid = decode_uid(args[4]);
905 if (nargs > 5)
906 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400907 if (nargs > 6)
908 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700909 si->next = svc->sockets;
910 svc->sockets = si;
911 break;
912 }
913 case K_user:
914 if (nargs != 2) {
915 parse_error(state, "user option requires a user id\n");
916 } else {
917 svc->uid = decode_uid(args[1]);
918 }
919 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500920 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500921 if (nargs != 2) {
922 parse_error(state, "seclabel option requires a label string\n");
923 } else {
924 svc->seclabel = args[1];
925 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500926 break;
927
Colin Cross6310a822010-04-20 14:29:05 -0700928 default:
929 parse_error(state, "invalid option '%s'\n", args[0]);
930 }
931}
932
933static void *parse_action(struct parse_state *state, int nargs, char **args)
934{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700935 struct trigger *cur_trigger;
936 int i;
Colin Cross6310a822010-04-20 14:29:05 -0700937 if (nargs < 2) {
938 parse_error(state, "actions must have a trigger\n");
939 return 0;
940 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700941
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800942 action* act = (action*) calloc(1, sizeof(*act));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700943 list_init(&act->triggers);
944
945 for (i = 1; i < nargs; i++) {
946 if (!(i % 2)) {
947 if (strcmp(args[i], "&&")) {
948 parse_error(state, "& is the only symbol allowed to concatenate actions\n");
949 return 0;
950 } else
951 continue;
952 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800953 cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700954 cur_trigger->name = args[i];
955 list_add_tail(&act->triggers, &cur_trigger->nlist);
956 }
957
Colin Cross6310a822010-04-20 14:29:05 -0700958 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800959 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700960 list_add_tail(&action_list, &act->alist);
961 /* XXX add to hash */
962 return act;
963}
964
965static void parse_line_action(struct parse_state* state, int nargs, char **args)
966{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800967 struct action *act = (action*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700968 int kw, n;
969
970 if (nargs == 0) {
971 return;
972 }
973
974 kw = lookup_keyword(args[0]);
975 if (!kw_is(kw, COMMAND)) {
976 parse_error(state, "invalid command '%s'\n", args[0]);
977 return;
978 }
979
980 n = kw_nargs(kw);
981 if (nargs < n) {
982 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
983 n > 2 ? "arguments" : "argument");
984 return;
985 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800986 command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700987 cmd->func = kw_func(kw);
Riley Andrews24a3b782014-06-26 13:56:01 -0700988 cmd->line = state->line;
989 cmd->filename = state->filename;
Colin Cross6310a822010-04-20 14:29:05 -0700990 cmd->nargs = nargs;
991 memcpy(cmd->args, args, sizeof(char*) * nargs);
992 list_add_tail(&act->commands, &cmd->clist);
993}