blob: f3d34b2ed4451206742e20a72c2515768d5148a8 [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;
Colin Cross6310a822010-04-20 14:29:05 -0700121 case 'c':
Elliott Hughesf682b472015-02-06 12:19:48 -0800122 if (!strcmp(s, "opy")) return K_copy;
Colin Cross6310a822010-04-20 14:29:05 -0700123 if (!strcmp(s, "apability")) return K_capability;
Colin Cross6310a822010-04-20 14:29:05 -0700124 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 Sumrall752923c2010-12-03 16:33:31 -0800127 if (!strcmp(s, "lass_reset")) return K_class_reset;
Colin Cross6310a822010-04-20 14:29:05 -0700128 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 Abgrall3beec7e2014-05-02 21:14:29 -0700138 if (!strcmp(s, "nable")) return K_enable;
Colin Cross6310a822010-04-20 14:29:05 -0700139 if (!strcmp(s, "xec")) return K_exec;
San Mehat429721c2014-09-23 07:48:47 -0700140 if (!strcmp(s, "xeconce")) return K_execonce;
Colin Cross6310a822010-04-20 14:29:05 -0700141 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;
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 Sumrallc5c51032011-03-08 17:01:29 -0800160 if (!strcmp(s, "oad_persist_props")) return K_load_persist_props;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700161 if (!strcmp(s, "oad_all_props")) return K_load_all_props;
Colin Cross6310a822010-04-20 14:29:05 -0700162 break;
163 case 'm':
164 if (!strcmp(s, "kdir")) return K_mkdir;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700165 if (!strcmp(s, "ount_all")) return K_mount_all;
Colin Cross6310a822010-04-20 14:29:05 -0700166 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 Kralevichca8e66a2013-04-18 12:20:02 -0700173 case 'p':
174 if (!strcmp(s, "owerctl")) return K_powerctl;
Elliott Hughesf682b472015-02-06 12:19:48 -0800175 break;
Colin Cross6310a822010-04-20 14:29:05 -0700176 case 'r':
177 if (!strcmp(s, "estart")) return K_restart;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500178 if (!strcmp(s, "estorecon")) return K_restorecon;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400179 if (!strcmp(s, "estorecon_recursive")) return K_restorecon_recursive;
Ken Sumrall203bad52011-01-18 17:37:41 -0800180 if (!strcmp(s, "mdir")) return K_rmdir;
181 if (!strcmp(s, "m")) return K_rm;
Colin Cross6310a822010-04-20 14:29:05 -0700182 break;
183 case 's':
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500184 if (!strcmp(s, "eclabel")) return K_seclabel;
Colin Cross6310a822010-04-20 14:29:05 -0700185 if (!strcmp(s, "ervice")) return K_service;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500186 if (!strcmp(s, "etcon")) return K_setcon;
Colin Cross6310a822010-04-20 14:29:05 -0700187 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;
190 if (!strcmp(s, "ocket")) return K_socket;
191 if (!strcmp(s, "tart")) return K_start;
192 if (!strcmp(s, "top")) return K_stop;
Ken Sumralla76baaa2013-07-09 18:42:09 -0700193 if (!strcmp(s, "wapon_all")) return K_swapon_all;
Colin Cross6310a822010-04-20 14:29:05 -0700194 if (!strcmp(s, "ymlink")) return K_symlink;
195 if (!strcmp(s, "ysclktz")) return K_sysclktz;
196 break;
197 case 't':
198 if (!strcmp(s, "rigger")) return K_trigger;
199 break;
200 case 'u':
201 if (!strcmp(s, "ser")) return K_user;
202 break;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000203 case 'v':
204 if (!strcmp(s, "erity_load_state")) return K_verity_load_state;
205 break;
Colin Cross6310a822010-04-20 14:29:05 -0700206 case 'w':
207 if (!strcmp(s, "rite")) return K_write;
208 if (!strcmp(s, "ait")) return K_wait;
209 break;
210 }
211 return K_UNKNOWN;
212}
213
Elliott Hughesf682b472015-02-06 12:19:48 -0800214static void parse_line_no_op(struct parse_state*, int, char**) {
Colin Cross6310a822010-04-20 14:29:05 -0700215}
216
Dima Zavina6235ea2011-12-16 14:20:12 -0800217static int push_chars(char **dst, int *len, const char *chars, int cnt)
218{
219 if (cnt > *len)
220 return -1;
221
222 memcpy(*dst, chars, cnt);
223 *dst += cnt;
224 *len -= cnt;
225
226 return 0;
227}
228
Dima Zavin84bf9af2011-12-20 13:44:41 -0800229int expand_props(char *dst, const char *src, int dst_size)
Dima Zavina6235ea2011-12-16 14:20:12 -0800230{
Dima Zavina6235ea2011-12-16 14:20:12 -0800231 char *dst_ptr = dst;
232 const char *src_ptr = src;
Dima Zavina6235ea2011-12-16 14:20:12 -0800233 int ret = 0;
234 int left = dst_size - 1;
235
236 if (!src || !dst || dst_size == 0)
237 return -1;
238
Dima Zavina6235ea2011-12-16 14:20:12 -0800239 /* - variables can either be $x.y or ${x.y}, in case they are only part
240 * of the string.
241 * - will accept $$ as a literal $.
242 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
243 * bad things will happen
244 */
245 while (*src_ptr && left > 0) {
246 char *c;
247 char prop[PROP_NAME_MAX + 1];
Colin Cross2deedfe2013-01-28 17:13:35 -0800248 char prop_val[PROP_VALUE_MAX];
Dima Zavina6235ea2011-12-16 14:20:12 -0800249 int prop_len = 0;
Colin Cross2deedfe2013-01-28 17:13:35 -0800250 int prop_val_len;
Dima Zavina6235ea2011-12-16 14:20:12 -0800251
252 c = strchr(src_ptr, '$');
253 if (!c) {
254 while (left-- > 0 && *src_ptr)
255 *(dst_ptr++) = *(src_ptr++);
256 break;
257 }
258
259 memset(prop, 0, sizeof(prop));
260
261 ret = push_chars(&dst_ptr, &left, src_ptr, c - src_ptr);
262 if (ret < 0)
263 goto err_nospace;
264 c++;
265
266 if (*c == '$') {
267 *(dst_ptr++) = *(c++);
268 src_ptr = c;
269 left--;
270 continue;
271 } else if (*c == '\0') {
272 break;
273 }
274
275 if (*c == '{') {
276 c++;
277 while (*c && *c != '}' && prop_len < PROP_NAME_MAX)
278 prop[prop_len++] = *(c++);
279 if (*c != '}') {
280 /* failed to find closing brace, abort. */
281 if (prop_len == PROP_NAME_MAX)
282 ERROR("prop name too long during expansion of '%s'\n",
283 src);
284 else if (*c == '\0')
285 ERROR("unexpected end of string in '%s', looking for }\n",
286 src);
287 goto err;
288 }
289 prop[prop_len] = '\0';
290 c++;
291 } else if (*c) {
292 while (*c && prop_len < PROP_NAME_MAX)
293 prop[prop_len++] = *(c++);
294 if (prop_len == PROP_NAME_MAX && *c != '\0') {
295 ERROR("prop name too long in '%s'\n", src);
296 goto err;
297 }
298 prop[prop_len] = '\0';
299 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
300 prop);
301 }
302
303 if (prop_len == 0) {
304 ERROR("invalid zero-length prop name in '%s'\n", src);
305 goto err;
306 }
307
Colin Cross2deedfe2013-01-28 17:13:35 -0800308 prop_val_len = property_get(prop, prop_val);
309 if (!prop_val_len) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800310 ERROR("property '%s' doesn't exist while expanding '%s'\n",
311 prop, src);
312 goto err;
313 }
314
Colin Cross2deedfe2013-01-28 17:13:35 -0800315 ret = push_chars(&dst_ptr, &left, prop_val, prop_val_len);
Dima Zavina6235ea2011-12-16 14:20:12 -0800316 if (ret < 0)
317 goto err_nospace;
318 src_ptr = c;
319 continue;
320 }
321
322 *dst_ptr = '\0';
323 return 0;
324
325err_nospace:
326 ERROR("destination buffer overflow while expanding '%s'\n", src);
327err:
328 return -1;
329}
330
Greg Hackmannd68db712013-11-18 09:44:20 -0800331static void parse_import(struct parse_state *state, int nargs, char **args)
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800332{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800333 struct listnode *import_list = (listnode*) state->priv;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800334 char conf_file[PATH_MAX];
335 int ret;
336
337 if (nargs != 2) {
338 ERROR("single argument needed for import\n");
339 return;
340 }
341
342 ret = expand_props(conf_file, args[1], sizeof(conf_file));
343 if (ret) {
344 ERROR("error while handling import on line '%d' in '%s'\n",
345 state->line, state->filename);
346 return;
347 }
348
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800349 struct import* import = (struct import*) calloc(1, sizeof(struct import));
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800350 import->filename = strdup(conf_file);
351 list_add_tail(import_list, &import->list);
352 INFO("found import '%s', adding to import list", import->filename);
353}
354
Greg Hackmannd68db712013-11-18 09:44:20 -0800355static void parse_new_section(struct parse_state *state, int kw,
Colin Cross6310a822010-04-20 14:29:05 -0700356 int nargs, char **args)
357{
358 printf("[ %s %s ]\n", args[0],
359 nargs > 1 ? args[1] : "");
360 switch(kw) {
361 case K_service:
362 state->context = parse_service(state, nargs, args);
363 if (state->context) {
364 state->parse_line = parse_line_service;
365 return;
366 }
367 break;
368 case K_on:
369 state->context = parse_action(state, nargs, args);
370 if (state->context) {
371 state->parse_line = parse_line_action;
372 return;
373 }
374 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700375 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800376 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800377 break;
Colin Cross6310a822010-04-20 14:29:05 -0700378 }
379 state->parse_line = parse_line_no_op;
380}
381
Elliott Hughesf682b472015-02-06 12:19:48 -0800382static void parse_config(const char *fn, const std::string& data)
Colin Cross6310a822010-04-20 14:29:05 -0700383{
384 struct parse_state state;
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];
388 int nargs;
389
390 nargs = 0;
391 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800392 state.line = 0;
Elliott Hughesf682b472015-02-06 12:19:48 -0800393 state.ptr = strdup(data.c_str()); // TODO: fix this code!
Colin Cross6310a822010-04-20 14:29:05 -0700394 state.nexttoken = 0;
395 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800396
397 list_init(&import_list);
398 state.priv = &import_list;
399
Colin Cross6310a822010-04-20 14:29:05 -0700400 for (;;) {
401 switch (next_token(&state)) {
402 case T_EOF:
403 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800404 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700405 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800406 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700407 if (nargs) {
408 int kw = lookup_keyword(args[0]);
409 if (kw_is(kw, SECTION)) {
410 state.parse_line(&state, 0, 0);
411 parse_new_section(&state, kw, nargs, args);
412 } else {
413 state.parse_line(&state, nargs, args);
414 }
415 nargs = 0;
416 }
417 break;
418 case T_TEXT:
419 if (nargs < INIT_PARSER_MAXARGS) {
420 args[nargs++] = state.text;
421 }
422 break;
423 }
424 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800425
426parser_done:
427 list_for_each(node, &import_list) {
428 struct import *import = node_to_item(node, struct import, list);
429 int ret;
430
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800431 ret = init_parse_config_file(import->filename);
432 if (ret)
433 ERROR("could not import file '%s' from '%s'\n",
434 import->filename, fn);
435 }
Colin Cross6310a822010-04-20 14:29:05 -0700436}
437
Elliott Hughesf682b472015-02-06 12:19:48 -0800438int init_parse_config_file(const char* path) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800439 INFO("Parsing %s...\n", path);
Elliott Hughesf682b472015-02-06 12:19:48 -0800440 std::string data;
441 if (!read_file(path, &data)) {
442 return -1;
443 }
Colin Cross6310a822010-04-20 14:29:05 -0700444
Elliott Hughesf682b472015-02-06 12:19:48 -0800445 parse_config(path, data);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800446 dump_parser_state();
Colin Cross6310a822010-04-20 14:29:05 -0700447 return 0;
448}
449
450static int valid_name(const char *name)
451{
452 if (strlen(name) > 16) {
453 return 0;
454 }
455 while (*name) {
456 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
457 return 0;
458 }
459 name++;
460 }
461 return 1;
462}
463
464struct service *service_find_by_name(const char *name)
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 if (!strcmp(svc->name, name)) {
471 return svc;
472 }
473 }
474 return 0;
475}
476
477struct service *service_find_by_pid(pid_t pid)
478{
479 struct listnode *node;
480 struct service *svc;
481 list_for_each(node, &service_list) {
482 svc = node_to_item(node, struct service, slist);
483 if (svc->pid == pid) {
484 return svc;
485 }
486 }
487 return 0;
488}
489
490struct service *service_find_by_keychord(int keychord_id)
491{
492 struct listnode *node;
493 struct service *svc;
494 list_for_each(node, &service_list) {
495 svc = node_to_item(node, struct service, slist);
496 if (svc->keychord_id == keychord_id) {
497 return svc;
498 }
499 }
500 return 0;
501}
502
503void service_for_each(void (*func)(struct service *svc))
504{
505 struct listnode *node;
506 struct service *svc;
507 list_for_each(node, &service_list) {
508 svc = node_to_item(node, struct service, slist);
509 func(svc);
510 }
511}
512
513void service_for_each_class(const char *classname,
514 void (*func)(struct service *svc))
515{
516 struct listnode *node;
517 struct service *svc;
518 list_for_each(node, &service_list) {
519 svc = node_to_item(node, struct service, slist);
520 if (!strcmp(svc->classname, classname)) {
521 func(svc);
522 }
523 }
524}
525
526void service_for_each_flags(unsigned matchflags,
527 void (*func)(struct service *svc))
528{
529 struct listnode *node;
530 struct service *svc;
531 list_for_each(node, &service_list) {
532 svc = node_to_item(node, struct service, slist);
533 if (svc->flags & matchflags) {
534 func(svc);
535 }
536 }
537}
538
539void action_for_each_trigger(const char *trigger,
540 void (*func)(struct action *act))
541{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700542 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700543 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700544 struct trigger *cur_trigger;
545
Colin Cross6310a822010-04-20 14:29:05 -0700546 list_for_each(node, &action_list) {
547 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700548 list_for_each(node2, &act->triggers) {
549 cur_trigger = node_to_item(node2, struct trigger, nlist);
550 if (!strcmp(cur_trigger->name, trigger)) {
551 func(act);
552 }
Colin Cross6310a822010-04-20 14:29:05 -0700553 }
554 }
555}
556
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700557
Colin Cross6310a822010-04-20 14:29:05 -0700558void queue_property_triggers(const char *name, const char *value)
559{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700560 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700561 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700562 struct trigger *cur_trigger;
563 bool match;
564 int name_length;
565
Colin Cross6310a822010-04-20 14:29:05 -0700566 list_for_each(node, &action_list) {
567 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700568 match = !name;
569 list_for_each(node2, &act->triggers) {
570 cur_trigger = node_to_item(node2, struct trigger, nlist);
571 if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) {
572 const char *test = cur_trigger->name + strlen("property:");
573 if (!match) {
574 name_length = strlen(name);
575 if (!strncmp(name, test, name_length) &&
576 test[name_length] == '=' &&
577 (!strcmp(test + name_length + 1, value) ||
578 !strcmp(test + name_length + 1, "*"))) {
579 match = true;
580 continue;
581 }
582 } else {
583 const char* equals = strchr(test, '=');
584 if (equals) {
585 char prop_name[PROP_NAME_MAX + 1];
586 char value[PROP_VALUE_MAX];
587 int length = equals - test;
588 if (length <= PROP_NAME_MAX) {
589 int ret;
590 memcpy(prop_name, test, length);
591 prop_name[length] = 0;
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? */
594 ret = property_get(prop_name, value);
595 if (ret > 0 && (!strcmp(equals + 1, value) ||
596 !strcmp(equals + 1, "*"))) {
597 continue;
598 }
599 }
600 }
601 }
602 }
603 match = false;
604 break;
605 }
606 if (match) {
607 action_add_queue_tail(act);
Colin Cross6310a822010-04-20 14:29:05 -0700608 }
609 }
610}
611
612void queue_all_property_triggers()
613{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700614 queue_property_triggers(NULL, NULL);
Colin Cross6310a822010-04-20 14:29:05 -0700615}
616
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800617void queue_builtin_action(int (*func)(int nargs, char **args), const char *name)
Colin Cross6310a822010-04-20 14:29:05 -0700618{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800619 action* act = (action*) calloc(1, sizeof(*act));
620 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700621 cur_trigger->name = name;
622 list_init(&act->triggers);
623 list_add_tail(&act->triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700624 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800625 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700626
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800627 command* cmd = (command*) calloc(1, sizeof(*cmd));
Colin Cross6310a822010-04-20 14:29:05 -0700628 cmd->func = func;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800629 cmd->args[0] = const_cast<char*>(name);
Riley Andrews24a3b782014-06-26 13:56:01 -0700630 cmd->nargs = 1;
Colin Cross6310a822010-04-20 14:29:05 -0700631 list_add_tail(&act->commands, &cmd->clist);
632
633 list_add_tail(&action_list, &act->alist);
634 action_add_queue_tail(act);
635}
636
637void action_add_queue_tail(struct action *act)
638{
Colin Crossa5064622013-03-07 13:42:29 -0800639 if (list_empty(&act->qlist)) {
640 list_add_tail(&action_queue, &act->qlist);
641 }
Colin Cross6310a822010-04-20 14:29:05 -0700642}
643
644struct action *action_remove_queue_head(void)
645{
646 if (list_empty(&action_queue)) {
647 return 0;
648 } else {
649 struct listnode *node = list_head(&action_queue);
650 struct action *act = node_to_item(node, struct action, qlist);
651 list_remove(node);
Colin Crossa5064622013-03-07 13:42:29 -0800652 list_init(node);
Colin Cross6310a822010-04-20 14:29:05 -0700653 return act;
654 }
655}
656
657int action_queue_empty()
658{
659 return list_empty(&action_queue);
660}
661
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800662service* make_exec_oneshot_service(int nargs, char** args) {
663 // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS...
664 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
689 if (command_arg > 2) {
690 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) {
772 case K_capability:
773 break;
774 case K_class:
775 if (nargs != 2) {
776 parse_error(state, "class option requires a classname\n");
777 } else {
778 svc->classname = args[1];
779 }
780 break;
781 case K_console:
782 svc->flags |= SVC_CONSOLE;
783 break;
784 case K_disabled:
785 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700786 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700787 break;
788 case K_ioprio:
789 if (nargs != 3) {
790 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
791 } else {
792 svc->ioprio_pri = strtoul(args[2], 0, 8);
793
794 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
795 parse_error(state, "priority value must be range 0 - 7\n");
796 break;
797 }
798
799 if (!strcmp(args[1], "rt")) {
800 svc->ioprio_class = IoSchedClass_RT;
801 } else if (!strcmp(args[1], "be")) {
802 svc->ioprio_class = IoSchedClass_BE;
803 } else if (!strcmp(args[1], "idle")) {
804 svc->ioprio_class = IoSchedClass_IDLE;
805 } else {
806 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
807 }
808 }
809 break;
810 case K_group:
811 if (nargs < 2) {
812 parse_error(state, "group option requires a group id\n");
813 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
814 parse_error(state, "group option accepts at most %d supp. groups\n",
815 NR_SVC_SUPP_GIDS);
816 } else {
817 int n;
818 svc->gid = decode_uid(args[1]);
819 for (n = 2; n < nargs; n++) {
820 svc->supp_gids[n-2] = decode_uid(args[n]);
821 }
822 svc->nr_supp_gids = n - 2;
823 }
824 break;
825 case K_keycodes:
826 if (nargs < 2) {
827 parse_error(state, "keycodes option requires atleast one keycode\n");
828 } else {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800829 svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
Colin Cross6310a822010-04-20 14:29:05 -0700830 if (!svc->keycodes) {
831 parse_error(state, "could not allocate keycodes\n");
832 } else {
833 svc->nkeycodes = nargs - 1;
834 for (i = 1; i < nargs; i++) {
835 svc->keycodes[i - 1] = atoi(args[i]);
836 }
837 }
838 }
839 break;
840 case K_oneshot:
841 svc->flags |= SVC_ONESHOT;
842 break;
843 case K_onrestart:
844 nargs--;
845 args++;
846 kw = lookup_keyword(args[0]);
847 if (!kw_is(kw, COMMAND)) {
848 parse_error(state, "invalid command '%s'\n", args[0]);
849 break;
850 }
851 kw_nargs = kw_nargs(kw);
852 if (nargs < kw_nargs) {
853 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
854 kw_nargs > 2 ? "arguments" : "argument");
855 break;
856 }
857
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800858 cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700859 cmd->func = kw_func(kw);
860 cmd->nargs = nargs;
861 memcpy(cmd->args, args, sizeof(char*) * nargs);
862 list_add_tail(&svc->onrestart.commands, &cmd->clist);
863 break;
864 case K_critical:
865 svc->flags |= SVC_CRITICAL;
866 break;
867 case K_setenv: { /* name value */
Gavin.Changc3a46762013-09-28 00:22:11 +0800868 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700869 parse_error(state, "setenv option requires name and value arguments\n");
870 break;
871 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800872 svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
Colin Cross6310a822010-04-20 14:29:05 -0700873 if (!ei) {
874 parse_error(state, "out of memory\n");
875 break;
876 }
877 ei->name = args[1];
878 ei->value = args[2];
879 ei->next = svc->envvars;
880 svc->envvars = ei;
881 break;
882 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400883 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700884 if (nargs < 4) {
885 parse_error(state, "socket option requires name, type, perm arguments\n");
886 break;
887 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400888 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
889 && strcmp(args[2],"seqpacket")) {
890 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700891 break;
892 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800893 socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
Colin Cross6310a822010-04-20 14:29:05 -0700894 if (!si) {
895 parse_error(state, "out of memory\n");
896 break;
897 }
898 si->name = args[1];
899 si->type = args[2];
900 si->perm = strtoul(args[3], 0, 8);
901 if (nargs > 4)
902 si->uid = decode_uid(args[4]);
903 if (nargs > 5)
904 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400905 if (nargs > 6)
906 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700907 si->next = svc->sockets;
908 svc->sockets = si;
909 break;
910 }
911 case K_user:
912 if (nargs != 2) {
913 parse_error(state, "user option requires a user id\n");
914 } else {
915 svc->uid = decode_uid(args[1]);
916 }
917 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500918 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500919 if (nargs != 2) {
920 parse_error(state, "seclabel option requires a label string\n");
921 } else {
922 svc->seclabel = args[1];
923 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500924 break;
925
Colin Cross6310a822010-04-20 14:29:05 -0700926 default:
927 parse_error(state, "invalid option '%s'\n", args[0]);
928 }
929}
930
931static void *parse_action(struct parse_state *state, int nargs, char **args)
932{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700933 struct trigger *cur_trigger;
934 int i;
Colin Cross6310a822010-04-20 14:29:05 -0700935 if (nargs < 2) {
936 parse_error(state, "actions must have a trigger\n");
937 return 0;
938 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700939
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800940 action* act = (action*) calloc(1, sizeof(*act));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700941 list_init(&act->triggers);
942
943 for (i = 1; i < nargs; i++) {
944 if (!(i % 2)) {
945 if (strcmp(args[i], "&&")) {
946 parse_error(state, "& is the only symbol allowed to concatenate actions\n");
947 return 0;
948 } else
949 continue;
950 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800951 cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700952 cur_trigger->name = args[i];
953 list_add_tail(&act->triggers, &cur_trigger->nlist);
954 }
955
Colin Cross6310a822010-04-20 14:29:05 -0700956 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800957 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700958 list_add_tail(&action_list, &act->alist);
959 /* XXX add to hash */
960 return act;
961}
962
963static void parse_line_action(struct parse_state* state, int nargs, char **args)
964{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800965 struct action *act = (action*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700966 int kw, n;
967
968 if (nargs == 0) {
969 return;
970 }
971
972 kw = lookup_keyword(args[0]);
973 if (!kw_is(kw, COMMAND)) {
974 parse_error(state, "invalid command '%s'\n", args[0]);
975 return;
976 }
977
978 n = kw_nargs(kw);
979 if (nargs < n) {
980 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
981 n > 2 ? "arguments" : "argument");
982 return;
983 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800984 command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700985 cmd->func = kw_func(kw);
Riley Andrews24a3b782014-06-26 13:56:01 -0700986 cmd->line = state->line;
987 cmd->filename = state->filename;
Colin Cross6310a822010-04-20 14:29:05 -0700988 cmd->nargs = nargs;
989 memcpy(cmd->args, args, sizeof(char*) * nargs);
990 list_add_tail(&act->commands, &cmd->clist);
991}