blob: 76728042a8303e0a7c70ae44bd068ef9af5b0c09 [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;
124 if (!strcmp(s, "hdir")) return K_chdir;
125 if (!strcmp(s, "hroot")) return K_chroot;
126 if (!strcmp(s, "lass")) return K_class;
127 if (!strcmp(s, "lass_start")) return K_class_start;
128 if (!strcmp(s, "lass_stop")) return K_class_stop;
Ken Sumrall752923c2010-12-03 16:33:31 -0800129 if (!strcmp(s, "lass_reset")) return K_class_reset;
Colin Cross6310a822010-04-20 14:29:05 -0700130 if (!strcmp(s, "onsole")) return K_console;
131 if (!strcmp(s, "hown")) return K_chown;
132 if (!strcmp(s, "hmod")) return K_chmod;
133 if (!strcmp(s, "ritical")) return K_critical;
134 break;
135 case 'd':
136 if (!strcmp(s, "isabled")) return K_disabled;
137 if (!strcmp(s, "omainname")) return K_domainname;
138 break;
139 case 'e':
JP Abgrall3beec7e2014-05-02 21:14:29 -0700140 if (!strcmp(s, "nable")) return K_enable;
Colin Cross6310a822010-04-20 14:29:05 -0700141 if (!strcmp(s, "xec")) return K_exec;
San Mehat429721c2014-09-23 07:48:47 -0700142 if (!strcmp(s, "xeconce")) return K_execonce;
Colin Cross6310a822010-04-20 14:29:05 -0700143 if (!strcmp(s, "xport")) return K_export;
144 break;
145 case 'g':
146 if (!strcmp(s, "roup")) return K_group;
147 break;
148 case 'h':
149 if (!strcmp(s, "ostname")) return K_hostname;
150 break;
151 case 'i':
152 if (!strcmp(s, "oprio")) return K_ioprio;
153 if (!strcmp(s, "fup")) return K_ifup;
154 if (!strcmp(s, "nsmod")) return K_insmod;
155 if (!strcmp(s, "mport")) return K_import;
156 break;
157 case 'k':
158 if (!strcmp(s, "eycodes")) return K_keycodes;
159 break;
160 case 'l':
161 if (!strcmp(s, "oglevel")) return K_loglevel;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800162 if (!strcmp(s, "oad_persist_props")) return K_load_persist_props;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700163 if (!strcmp(s, "oad_all_props")) return K_load_all_props;
Colin Cross6310a822010-04-20 14:29:05 -0700164 break;
165 case 'm':
166 if (!strcmp(s, "kdir")) return K_mkdir;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700167 if (!strcmp(s, "ount_all")) return K_mount_all;
Colin Cross6310a822010-04-20 14:29:05 -0700168 if (!strcmp(s, "ount")) return K_mount;
169 break;
170 case 'o':
171 if (!strcmp(s, "n")) return K_on;
172 if (!strcmp(s, "neshot")) return K_oneshot;
173 if (!strcmp(s, "nrestart")) return K_onrestart;
174 break;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700175 case 'p':
176 if (!strcmp(s, "owerctl")) return K_powerctl;
Elliott Hughesf682b472015-02-06 12:19:48 -0800177 break;
Colin Cross6310a822010-04-20 14:29:05 -0700178 case 'r':
179 if (!strcmp(s, "estart")) return K_restart;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500180 if (!strcmp(s, "estorecon")) return K_restorecon;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400181 if (!strcmp(s, "estorecon_recursive")) return K_restorecon_recursive;
Ken Sumrall203bad52011-01-18 17:37:41 -0800182 if (!strcmp(s, "mdir")) return K_rmdir;
183 if (!strcmp(s, "m")) return K_rm;
Colin Cross6310a822010-04-20 14:29:05 -0700184 break;
185 case 's':
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500186 if (!strcmp(s, "eclabel")) return K_seclabel;
Colin Cross6310a822010-04-20 14:29:05 -0700187 if (!strcmp(s, "ervice")) return K_service;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500188 if (!strcmp(s, "etcon")) return K_setcon;
189 if (!strcmp(s, "etenforce")) return K_setenforce;
Colin Cross6310a822010-04-20 14:29:05 -0700190 if (!strcmp(s, "etenv")) return K_setenv;
191 if (!strcmp(s, "etkey")) return K_setkey;
192 if (!strcmp(s, "etprop")) return K_setprop;
193 if (!strcmp(s, "etrlimit")) return K_setrlimit;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500194 if (!strcmp(s, "etsebool")) return K_setsebool;
Colin Cross6310a822010-04-20 14:29:05 -0700195 if (!strcmp(s, "ocket")) return K_socket;
196 if (!strcmp(s, "tart")) return K_start;
197 if (!strcmp(s, "top")) return K_stop;
Ken Sumralla76baaa2013-07-09 18:42:09 -0700198 if (!strcmp(s, "wapon_all")) return K_swapon_all;
Colin Cross6310a822010-04-20 14:29:05 -0700199 if (!strcmp(s, "ymlink")) return K_symlink;
200 if (!strcmp(s, "ysclktz")) return K_sysclktz;
201 break;
202 case 't':
203 if (!strcmp(s, "rigger")) return K_trigger;
204 break;
205 case 'u':
206 if (!strcmp(s, "ser")) return K_user;
207 break;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000208 case 'v':
209 if (!strcmp(s, "erity_load_state")) return K_verity_load_state;
210 break;
Colin Cross6310a822010-04-20 14:29:05 -0700211 case 'w':
212 if (!strcmp(s, "rite")) return K_write;
213 if (!strcmp(s, "ait")) return K_wait;
214 break;
215 }
216 return K_UNKNOWN;
217}
218
Elliott Hughesf682b472015-02-06 12:19:48 -0800219static void parse_line_no_op(struct parse_state*, int, char**) {
Colin Cross6310a822010-04-20 14:29:05 -0700220}
221
Dima Zavina6235ea2011-12-16 14:20:12 -0800222static int push_chars(char **dst, int *len, const char *chars, int cnt)
223{
224 if (cnt > *len)
225 return -1;
226
227 memcpy(*dst, chars, cnt);
228 *dst += cnt;
229 *len -= cnt;
230
231 return 0;
232}
233
Dima Zavin84bf9af2011-12-20 13:44:41 -0800234int expand_props(char *dst, const char *src, int dst_size)
Dima Zavina6235ea2011-12-16 14:20:12 -0800235{
Dima Zavina6235ea2011-12-16 14:20:12 -0800236 char *dst_ptr = dst;
237 const char *src_ptr = src;
Dima Zavina6235ea2011-12-16 14:20:12 -0800238 int ret = 0;
239 int left = dst_size - 1;
240
241 if (!src || !dst || dst_size == 0)
242 return -1;
243
Dima Zavina6235ea2011-12-16 14:20:12 -0800244 /* - variables can either be $x.y or ${x.y}, in case they are only part
245 * of the string.
246 * - will accept $$ as a literal $.
247 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
248 * bad things will happen
249 */
250 while (*src_ptr && left > 0) {
251 char *c;
252 char prop[PROP_NAME_MAX + 1];
Colin Cross2deedfe2013-01-28 17:13:35 -0800253 char prop_val[PROP_VALUE_MAX];
Dima Zavina6235ea2011-12-16 14:20:12 -0800254 int prop_len = 0;
Colin Cross2deedfe2013-01-28 17:13:35 -0800255 int prop_val_len;
Dima Zavina6235ea2011-12-16 14:20:12 -0800256
257 c = strchr(src_ptr, '$');
258 if (!c) {
259 while (left-- > 0 && *src_ptr)
260 *(dst_ptr++) = *(src_ptr++);
261 break;
262 }
263
264 memset(prop, 0, sizeof(prop));
265
266 ret = push_chars(&dst_ptr, &left, src_ptr, c - src_ptr);
267 if (ret < 0)
268 goto err_nospace;
269 c++;
270
271 if (*c == '$') {
272 *(dst_ptr++) = *(c++);
273 src_ptr = c;
274 left--;
275 continue;
276 } else if (*c == '\0') {
277 break;
278 }
279
280 if (*c == '{') {
281 c++;
282 while (*c && *c != '}' && prop_len < PROP_NAME_MAX)
283 prop[prop_len++] = *(c++);
284 if (*c != '}') {
285 /* failed to find closing brace, abort. */
286 if (prop_len == PROP_NAME_MAX)
287 ERROR("prop name too long during expansion of '%s'\n",
288 src);
289 else if (*c == '\0')
290 ERROR("unexpected end of string in '%s', looking for }\n",
291 src);
292 goto err;
293 }
294 prop[prop_len] = '\0';
295 c++;
296 } else if (*c) {
297 while (*c && prop_len < PROP_NAME_MAX)
298 prop[prop_len++] = *(c++);
299 if (prop_len == PROP_NAME_MAX && *c != '\0') {
300 ERROR("prop name too long in '%s'\n", src);
301 goto err;
302 }
303 prop[prop_len] = '\0';
304 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
305 prop);
306 }
307
308 if (prop_len == 0) {
309 ERROR("invalid zero-length prop name in '%s'\n", src);
310 goto err;
311 }
312
Colin Cross2deedfe2013-01-28 17:13:35 -0800313 prop_val_len = property_get(prop, prop_val);
314 if (!prop_val_len) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800315 ERROR("property '%s' doesn't exist while expanding '%s'\n",
316 prop, src);
317 goto err;
318 }
319
Colin Cross2deedfe2013-01-28 17:13:35 -0800320 ret = push_chars(&dst_ptr, &left, prop_val, prop_val_len);
Dima Zavina6235ea2011-12-16 14:20:12 -0800321 if (ret < 0)
322 goto err_nospace;
323 src_ptr = c;
324 continue;
325 }
326
327 *dst_ptr = '\0';
328 return 0;
329
330err_nospace:
331 ERROR("destination buffer overflow while expanding '%s'\n", src);
332err:
333 return -1;
334}
335
Greg Hackmannd68db712013-11-18 09:44:20 -0800336static void parse_import(struct parse_state *state, int nargs, char **args)
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800337{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800338 struct listnode *import_list = (listnode*) state->priv;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800339 char conf_file[PATH_MAX];
340 int ret;
341
342 if (nargs != 2) {
343 ERROR("single argument needed for import\n");
344 return;
345 }
346
347 ret = expand_props(conf_file, args[1], sizeof(conf_file));
348 if (ret) {
349 ERROR("error while handling import on line '%d' in '%s'\n",
350 state->line, state->filename);
351 return;
352 }
353
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800354 struct import* import = (struct import*) calloc(1, sizeof(struct import));
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800355 import->filename = strdup(conf_file);
356 list_add_tail(import_list, &import->list);
357 INFO("found import '%s', adding to import list", import->filename);
358}
359
Greg Hackmannd68db712013-11-18 09:44:20 -0800360static void parse_new_section(struct parse_state *state, int kw,
Colin Cross6310a822010-04-20 14:29:05 -0700361 int nargs, char **args)
362{
363 printf("[ %s %s ]\n", args[0],
364 nargs > 1 ? args[1] : "");
365 switch(kw) {
366 case K_service:
367 state->context = parse_service(state, nargs, args);
368 if (state->context) {
369 state->parse_line = parse_line_service;
370 return;
371 }
372 break;
373 case K_on:
374 state->context = parse_action(state, nargs, args);
375 if (state->context) {
376 state->parse_line = parse_line_action;
377 return;
378 }
379 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700380 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800381 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800382 break;
Colin Cross6310a822010-04-20 14:29:05 -0700383 }
384 state->parse_line = parse_line_no_op;
385}
386
Elliott Hughesf682b472015-02-06 12:19:48 -0800387static void parse_config(const char *fn, const std::string& data)
Colin Cross6310a822010-04-20 14:29:05 -0700388{
389 struct parse_state state;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800390 struct listnode import_list;
391 struct listnode *node;
Colin Cross6310a822010-04-20 14:29:05 -0700392 char *args[INIT_PARSER_MAXARGS];
393 int nargs;
394
395 nargs = 0;
396 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800397 state.line = 0;
Elliott Hughesf682b472015-02-06 12:19:48 -0800398 state.ptr = strdup(data.c_str()); // TODO: fix this code!
Colin Cross6310a822010-04-20 14:29:05 -0700399 state.nexttoken = 0;
400 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800401
402 list_init(&import_list);
403 state.priv = &import_list;
404
Colin Cross6310a822010-04-20 14:29:05 -0700405 for (;;) {
406 switch (next_token(&state)) {
407 case T_EOF:
408 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800409 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700410 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800411 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700412 if (nargs) {
413 int kw = lookup_keyword(args[0]);
414 if (kw_is(kw, SECTION)) {
415 state.parse_line(&state, 0, 0);
416 parse_new_section(&state, kw, nargs, args);
417 } else {
418 state.parse_line(&state, nargs, args);
419 }
420 nargs = 0;
421 }
422 break;
423 case T_TEXT:
424 if (nargs < INIT_PARSER_MAXARGS) {
425 args[nargs++] = state.text;
426 }
427 break;
428 }
429 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800430
431parser_done:
432 list_for_each(node, &import_list) {
433 struct import *import = node_to_item(node, struct import, list);
434 int ret;
435
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800436 ret = init_parse_config_file(import->filename);
437 if (ret)
438 ERROR("could not import file '%s' from '%s'\n",
439 import->filename, fn);
440 }
Colin Cross6310a822010-04-20 14:29:05 -0700441}
442
Elliott Hughesf682b472015-02-06 12:19:48 -0800443int init_parse_config_file(const char* path) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800444 INFO("Parsing %s...\n", path);
Elliott Hughesf682b472015-02-06 12:19:48 -0800445 std::string data;
446 if (!read_file(path, &data)) {
447 return -1;
448 }
Colin Cross6310a822010-04-20 14:29:05 -0700449
Elliott Hughesf682b472015-02-06 12:19:48 -0800450 parse_config(path, data);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800451 dump_parser_state();
Colin Cross6310a822010-04-20 14:29:05 -0700452 return 0;
453}
454
455static int valid_name(const char *name)
456{
457 if (strlen(name) > 16) {
458 return 0;
459 }
460 while (*name) {
461 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
462 return 0;
463 }
464 name++;
465 }
466 return 1;
467}
468
469struct service *service_find_by_name(const char *name)
470{
471 struct listnode *node;
472 struct service *svc;
473 list_for_each(node, &service_list) {
474 svc = node_to_item(node, struct service, slist);
475 if (!strcmp(svc->name, name)) {
476 return svc;
477 }
478 }
479 return 0;
480}
481
482struct service *service_find_by_pid(pid_t pid)
483{
484 struct listnode *node;
485 struct service *svc;
486 list_for_each(node, &service_list) {
487 svc = node_to_item(node, struct service, slist);
488 if (svc->pid == pid) {
489 return svc;
490 }
491 }
492 return 0;
493}
494
495struct service *service_find_by_keychord(int keychord_id)
496{
497 struct listnode *node;
498 struct service *svc;
499 list_for_each(node, &service_list) {
500 svc = node_to_item(node, struct service, slist);
501 if (svc->keychord_id == keychord_id) {
502 return svc;
503 }
504 }
505 return 0;
506}
507
508void service_for_each(void (*func)(struct service *svc))
509{
510 struct listnode *node;
511 struct service *svc;
512 list_for_each(node, &service_list) {
513 svc = node_to_item(node, struct service, slist);
514 func(svc);
515 }
516}
517
518void service_for_each_class(const char *classname,
519 void (*func)(struct service *svc))
520{
521 struct listnode *node;
522 struct service *svc;
523 list_for_each(node, &service_list) {
524 svc = node_to_item(node, struct service, slist);
525 if (!strcmp(svc->classname, classname)) {
526 func(svc);
527 }
528 }
529}
530
531void service_for_each_flags(unsigned matchflags,
532 void (*func)(struct service *svc))
533{
534 struct listnode *node;
535 struct service *svc;
536 list_for_each(node, &service_list) {
537 svc = node_to_item(node, struct service, slist);
538 if (svc->flags & matchflags) {
539 func(svc);
540 }
541 }
542}
543
544void action_for_each_trigger(const char *trigger,
545 void (*func)(struct action *act))
546{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700547 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700548 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700549 struct trigger *cur_trigger;
550
Colin Cross6310a822010-04-20 14:29:05 -0700551 list_for_each(node, &action_list) {
552 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700553 list_for_each(node2, &act->triggers) {
554 cur_trigger = node_to_item(node2, struct trigger, nlist);
555 if (!strcmp(cur_trigger->name, trigger)) {
556 func(act);
557 }
Colin Cross6310a822010-04-20 14:29:05 -0700558 }
559 }
560}
561
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700562
Colin Cross6310a822010-04-20 14:29:05 -0700563void queue_property_triggers(const char *name, const char *value)
564{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700565 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700566 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700567 struct trigger *cur_trigger;
568 bool match;
569 int name_length;
570
Colin Cross6310a822010-04-20 14:29:05 -0700571 list_for_each(node, &action_list) {
572 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700573 match = !name;
574 list_for_each(node2, &act->triggers) {
575 cur_trigger = node_to_item(node2, struct trigger, nlist);
576 if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) {
577 const char *test = cur_trigger->name + strlen("property:");
578 if (!match) {
579 name_length = strlen(name);
580 if (!strncmp(name, test, name_length) &&
581 test[name_length] == '=' &&
582 (!strcmp(test + name_length + 1, value) ||
583 !strcmp(test + name_length + 1, "*"))) {
584 match = true;
585 continue;
586 }
587 } else {
588 const char* equals = strchr(test, '=');
589 if (equals) {
590 char prop_name[PROP_NAME_MAX + 1];
591 char value[PROP_VALUE_MAX];
592 int length = equals - test;
593 if (length <= PROP_NAME_MAX) {
594 int ret;
595 memcpy(prop_name, test, length);
596 prop_name[length] = 0;
Colin Cross6310a822010-04-20 14:29:05 -0700597
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700598 /* does the property exist, and match the trigger value? */
599 ret = property_get(prop_name, value);
600 if (ret > 0 && (!strcmp(equals + 1, value) ||
601 !strcmp(equals + 1, "*"))) {
602 continue;
603 }
604 }
605 }
606 }
607 }
608 match = false;
609 break;
610 }
611 if (match) {
612 action_add_queue_tail(act);
Colin Cross6310a822010-04-20 14:29:05 -0700613 }
614 }
615}
616
617void queue_all_property_triggers()
618{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700619 queue_property_triggers(NULL, NULL);
Colin Cross6310a822010-04-20 14:29:05 -0700620}
621
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800622void queue_builtin_action(int (*func)(int nargs, char **args), const char *name)
Colin Cross6310a822010-04-20 14:29:05 -0700623{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800624 action* act = (action*) calloc(1, sizeof(*act));
625 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700626 cur_trigger->name = name;
627 list_init(&act->triggers);
628 list_add_tail(&act->triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700629 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800630 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700631
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800632 command* cmd = (command*) calloc(1, sizeof(*cmd));
Colin Cross6310a822010-04-20 14:29:05 -0700633 cmd->func = func;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800634 cmd->args[0] = const_cast<char*>(name);
Riley Andrews24a3b782014-06-26 13:56:01 -0700635 cmd->nargs = 1;
Colin Cross6310a822010-04-20 14:29:05 -0700636 list_add_tail(&act->commands, &cmd->clist);
637
638 list_add_tail(&action_list, &act->alist);
639 action_add_queue_tail(act);
640}
641
642void action_add_queue_tail(struct action *act)
643{
Colin Crossa5064622013-03-07 13:42:29 -0800644 if (list_empty(&act->qlist)) {
645 list_add_tail(&action_queue, &act->qlist);
646 }
Colin Cross6310a822010-04-20 14:29:05 -0700647}
648
649struct action *action_remove_queue_head(void)
650{
651 if (list_empty(&action_queue)) {
652 return 0;
653 } else {
654 struct listnode *node = list_head(&action_queue);
655 struct action *act = node_to_item(node, struct action, qlist);
656 list_remove(node);
Colin Crossa5064622013-03-07 13:42:29 -0800657 list_init(node);
Colin Cross6310a822010-04-20 14:29:05 -0700658 return act;
659 }
660}
661
662int action_queue_empty()
663{
664 return list_empty(&action_queue);
665}
666
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800667service* make_exec_oneshot_service(int nargs, char** args) {
668 // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS...
669 int command_arg = 1;
670 for (int i = 1; i < nargs; ++i) {
671 if (strcmp(args[i], "--") == 0) {
672 command_arg = i + 1;
673 break;
674 }
675 }
676 if (command_arg > 4 + NR_SVC_SUPP_GIDS) {
677 ERROR("exec called with too many supplementary group ids\n");
678 return NULL;
679 }
680
681 int argc = nargs - command_arg;
682 char** argv = (args + command_arg);
683 if (argc < 1) {
684 ERROR("exec called without command\n");
685 return NULL;
686 }
687
688 service* svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * argc);
689 if (svc == NULL) {
690 ERROR("Couldn't allocate service for exec of '%s': %s", argv[0], strerror(errno));
691 return NULL;
692 }
693
694 if (command_arg > 2) {
695 svc->seclabel = args[1];
696 }
697 if (command_arg > 3) {
698 svc->uid = decode_uid(args[2]);
699 }
700 if (command_arg > 4) {
701 svc->gid = decode_uid(args[3]);
702 svc->nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */;
703 for (size_t i = 0; i < svc->nr_supp_gids; ++i) {
704 svc->supp_gids[i] = decode_uid(args[4 + i]);
705 }
706 }
707
708 static int exec_count; // Every service needs a unique name.
709 char* name = NULL;
710 asprintf(&name, "exec %d (%s)", exec_count++, argv[0]);
711 if (name == NULL) {
712 ERROR("Couldn't allocate name for exec service '%s'\n", argv[0]);
713 free(svc);
714 return NULL;
715 }
716 svc->name = name;
717 svc->classname = "default";
718 svc->flags = SVC_EXEC | SVC_ONESHOT;
719 svc->nargs = argc;
720 memcpy(svc->args, argv, sizeof(char*) * svc->nargs);
721 svc->args[argc] = NULL;
722 list_add_tail(&service_list, &svc->slist);
723 return svc;
724}
725
Colin Cross6310a822010-04-20 14:29:05 -0700726static void *parse_service(struct parse_state *state, int nargs, char **args)
727{
Colin Cross6310a822010-04-20 14:29:05 -0700728 if (nargs < 3) {
729 parse_error(state, "services must have a name and a program\n");
730 return 0;
731 }
732 if (!valid_name(args[1])) {
733 parse_error(state, "invalid service name '%s'\n", args[1]);
734 return 0;
735 }
736
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800737 service* svc = (service*) service_find_by_name(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700738 if (svc) {
739 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
740 return 0;
741 }
742
743 nargs -= 2;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800744 svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700745 if (!svc) {
746 parse_error(state, "out of memory\n");
747 return 0;
748 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800749 svc->name = strdup(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700750 svc->classname = "default";
751 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800752 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Colin Cross6310a822010-04-20 14:29:05 -0700753 svc->args[nargs] = 0;
754 svc->nargs = nargs;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700755 list_init(&svc->onrestart.triggers);
756 cur_trigger->name = "onrestart";
757 list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700758 list_init(&svc->onrestart.commands);
759 list_add_tail(&service_list, &svc->slist);
760 return svc;
761}
762
763static void parse_line_service(struct parse_state *state, int nargs, char **args)
764{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800765 struct service *svc = (service*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700766 struct command *cmd;
767 int i, kw, kw_nargs;
768
769 if (nargs == 0) {
770 return;
771 }
772
773 svc->ioprio_class = IoSchedClass_NONE;
774
775 kw = lookup_keyword(args[0]);
776 switch (kw) {
777 case K_capability:
778 break;
779 case K_class:
780 if (nargs != 2) {
781 parse_error(state, "class option requires a classname\n");
782 } else {
783 svc->classname = args[1];
784 }
785 break;
786 case K_console:
787 svc->flags |= SVC_CONSOLE;
788 break;
789 case K_disabled:
790 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700791 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700792 break;
793 case K_ioprio:
794 if (nargs != 3) {
795 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
796 } else {
797 svc->ioprio_pri = strtoul(args[2], 0, 8);
798
799 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
800 parse_error(state, "priority value must be range 0 - 7\n");
801 break;
802 }
803
804 if (!strcmp(args[1], "rt")) {
805 svc->ioprio_class = IoSchedClass_RT;
806 } else if (!strcmp(args[1], "be")) {
807 svc->ioprio_class = IoSchedClass_BE;
808 } else if (!strcmp(args[1], "idle")) {
809 svc->ioprio_class = IoSchedClass_IDLE;
810 } else {
811 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
812 }
813 }
814 break;
815 case K_group:
816 if (nargs < 2) {
817 parse_error(state, "group option requires a group id\n");
818 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
819 parse_error(state, "group option accepts at most %d supp. groups\n",
820 NR_SVC_SUPP_GIDS);
821 } else {
822 int n;
823 svc->gid = decode_uid(args[1]);
824 for (n = 2; n < nargs; n++) {
825 svc->supp_gids[n-2] = decode_uid(args[n]);
826 }
827 svc->nr_supp_gids = n - 2;
828 }
829 break;
830 case K_keycodes:
831 if (nargs < 2) {
832 parse_error(state, "keycodes option requires atleast one keycode\n");
833 } else {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800834 svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
Colin Cross6310a822010-04-20 14:29:05 -0700835 if (!svc->keycodes) {
836 parse_error(state, "could not allocate keycodes\n");
837 } else {
838 svc->nkeycodes = nargs - 1;
839 for (i = 1; i < nargs; i++) {
840 svc->keycodes[i - 1] = atoi(args[i]);
841 }
842 }
843 }
844 break;
845 case K_oneshot:
846 svc->flags |= SVC_ONESHOT;
847 break;
848 case K_onrestart:
849 nargs--;
850 args++;
851 kw = lookup_keyword(args[0]);
852 if (!kw_is(kw, COMMAND)) {
853 parse_error(state, "invalid command '%s'\n", args[0]);
854 break;
855 }
856 kw_nargs = kw_nargs(kw);
857 if (nargs < kw_nargs) {
858 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
859 kw_nargs > 2 ? "arguments" : "argument");
860 break;
861 }
862
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800863 cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700864 cmd->func = kw_func(kw);
865 cmd->nargs = nargs;
866 memcpy(cmd->args, args, sizeof(char*) * nargs);
867 list_add_tail(&svc->onrestart.commands, &cmd->clist);
868 break;
869 case K_critical:
870 svc->flags |= SVC_CRITICAL;
871 break;
872 case K_setenv: { /* name value */
Gavin.Changc3a46762013-09-28 00:22:11 +0800873 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700874 parse_error(state, "setenv option requires name and value arguments\n");
875 break;
876 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800877 svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
Colin Cross6310a822010-04-20 14:29:05 -0700878 if (!ei) {
879 parse_error(state, "out of memory\n");
880 break;
881 }
882 ei->name = args[1];
883 ei->value = args[2];
884 ei->next = svc->envvars;
885 svc->envvars = ei;
886 break;
887 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400888 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700889 if (nargs < 4) {
890 parse_error(state, "socket option requires name, type, perm arguments\n");
891 break;
892 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400893 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
894 && strcmp(args[2],"seqpacket")) {
895 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700896 break;
897 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800898 socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
Colin Cross6310a822010-04-20 14:29:05 -0700899 if (!si) {
900 parse_error(state, "out of memory\n");
901 break;
902 }
903 si->name = args[1];
904 si->type = args[2];
905 si->perm = strtoul(args[3], 0, 8);
906 if (nargs > 4)
907 si->uid = decode_uid(args[4]);
908 if (nargs > 5)
909 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400910 if (nargs > 6)
911 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700912 si->next = svc->sockets;
913 svc->sockets = si;
914 break;
915 }
916 case K_user:
917 if (nargs != 2) {
918 parse_error(state, "user option requires a user id\n");
919 } else {
920 svc->uid = decode_uid(args[1]);
921 }
922 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500923 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500924 if (nargs != 2) {
925 parse_error(state, "seclabel option requires a label string\n");
926 } else {
927 svc->seclabel = args[1];
928 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500929 break;
930
Colin Cross6310a822010-04-20 14:29:05 -0700931 default:
932 parse_error(state, "invalid option '%s'\n", args[0]);
933 }
934}
935
936static void *parse_action(struct parse_state *state, int nargs, char **args)
937{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700938 struct trigger *cur_trigger;
939 int i;
Colin Cross6310a822010-04-20 14:29:05 -0700940 if (nargs < 2) {
941 parse_error(state, "actions must have a trigger\n");
942 return 0;
943 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700944
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800945 action* act = (action*) calloc(1, sizeof(*act));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700946 list_init(&act->triggers);
947
948 for (i = 1; i < nargs; i++) {
949 if (!(i % 2)) {
950 if (strcmp(args[i], "&&")) {
951 parse_error(state, "& is the only symbol allowed to concatenate actions\n");
952 return 0;
953 } else
954 continue;
955 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800956 cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700957 cur_trigger->name = args[i];
958 list_add_tail(&act->triggers, &cur_trigger->nlist);
959 }
960
Colin Cross6310a822010-04-20 14:29:05 -0700961 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800962 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700963 list_add_tail(&action_list, &act->alist);
964 /* XXX add to hash */
965 return act;
966}
967
968static void parse_line_action(struct parse_state* state, int nargs, char **args)
969{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800970 struct action *act = (action*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700971 int kw, n;
972
973 if (nargs == 0) {
974 return;
975 }
976
977 kw = lookup_keyword(args[0]);
978 if (!kw_is(kw, COMMAND)) {
979 parse_error(state, "invalid command '%s'\n", args[0]);
980 return;
981 }
982
983 n = kw_nargs(kw);
984 if (nargs < n) {
985 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
986 n > 2 ? "arguments" : "argument");
987 return;
988 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800989 command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700990 cmd->func = kw_func(kw);
Riley Andrews24a3b782014-06-26 13:56:01 -0700991 cmd->line = state->line;
992 cmd->filename = state->filename;
Colin Cross6310a822010-04-20 14:29:05 -0700993 cmd->nargs = nargs;
994 memcpy(cmd->args, args, sizeof(char*) * nargs);
995 list_add_tail(&act->commands, &cmd->clist);
996}