blob: 7db203f0f64bf24e8580e8da5668a13a63ec40e8 [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;
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000205 if (!strcmp(s, "erity_update_state")) return K_verity_update_state;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000206 break;
Colin Cross6310a822010-04-20 14:29:05 -0700207 case 'w':
208 if (!strcmp(s, "rite")) return K_write;
209 if (!strcmp(s, "ait")) return K_wait;
210 break;
211 }
212 return K_UNKNOWN;
213}
214
Elliott Hughesf682b472015-02-06 12:19:48 -0800215static void parse_line_no_op(struct parse_state*, int, char**) {
Colin Cross6310a822010-04-20 14:29:05 -0700216}
217
Dima Zavina6235ea2011-12-16 14:20:12 -0800218static int push_chars(char **dst, int *len, const char *chars, int cnt)
219{
220 if (cnt > *len)
221 return -1;
222
223 memcpy(*dst, chars, cnt);
224 *dst += cnt;
225 *len -= cnt;
226
227 return 0;
228}
229
Dima Zavin84bf9af2011-12-20 13:44:41 -0800230int expand_props(char *dst, const char *src, int dst_size)
Dima Zavina6235ea2011-12-16 14:20:12 -0800231{
Dima Zavina6235ea2011-12-16 14:20:12 -0800232 char *dst_ptr = dst;
233 const char *src_ptr = src;
Dima Zavina6235ea2011-12-16 14:20:12 -0800234 int ret = 0;
235 int left = dst_size - 1;
236
237 if (!src || !dst || dst_size == 0)
238 return -1;
239
Dima Zavina6235ea2011-12-16 14:20:12 -0800240 /* - variables can either be $x.y or ${x.y}, in case they are only part
241 * of the string.
242 * - will accept $$ as a literal $.
243 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
244 * bad things will happen
245 */
246 while (*src_ptr && left > 0) {
247 char *c;
248 char prop[PROP_NAME_MAX + 1];
Colin Cross2deedfe2013-01-28 17:13:35 -0800249 char prop_val[PROP_VALUE_MAX];
Dima Zavina6235ea2011-12-16 14:20:12 -0800250 int prop_len = 0;
Colin Cross2deedfe2013-01-28 17:13:35 -0800251 int prop_val_len;
Dima Zavina6235ea2011-12-16 14:20:12 -0800252
253 c = strchr(src_ptr, '$');
254 if (!c) {
255 while (left-- > 0 && *src_ptr)
256 *(dst_ptr++) = *(src_ptr++);
257 break;
258 }
259
260 memset(prop, 0, sizeof(prop));
261
262 ret = push_chars(&dst_ptr, &left, src_ptr, c - src_ptr);
263 if (ret < 0)
264 goto err_nospace;
265 c++;
266
267 if (*c == '$') {
268 *(dst_ptr++) = *(c++);
269 src_ptr = c;
270 left--;
271 continue;
272 } else if (*c == '\0') {
273 break;
274 }
275
276 if (*c == '{') {
277 c++;
278 while (*c && *c != '}' && prop_len < PROP_NAME_MAX)
279 prop[prop_len++] = *(c++);
280 if (*c != '}') {
281 /* failed to find closing brace, abort. */
282 if (prop_len == PROP_NAME_MAX)
283 ERROR("prop name too long during expansion of '%s'\n",
284 src);
285 else if (*c == '\0')
286 ERROR("unexpected end of string in '%s', looking for }\n",
287 src);
288 goto err;
289 }
290 prop[prop_len] = '\0';
291 c++;
292 } else if (*c) {
293 while (*c && prop_len < PROP_NAME_MAX)
294 prop[prop_len++] = *(c++);
295 if (prop_len == PROP_NAME_MAX && *c != '\0') {
296 ERROR("prop name too long in '%s'\n", src);
297 goto err;
298 }
299 prop[prop_len] = '\0';
300 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
301 prop);
302 }
303
304 if (prop_len == 0) {
305 ERROR("invalid zero-length prop name in '%s'\n", src);
306 goto err;
307 }
308
Colin Cross2deedfe2013-01-28 17:13:35 -0800309 prop_val_len = property_get(prop, prop_val);
310 if (!prop_val_len) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800311 ERROR("property '%s' doesn't exist while expanding '%s'\n",
312 prop, src);
313 goto err;
314 }
315
Colin Cross2deedfe2013-01-28 17:13:35 -0800316 ret = push_chars(&dst_ptr, &left, prop_val, prop_val_len);
Dima Zavina6235ea2011-12-16 14:20:12 -0800317 if (ret < 0)
318 goto err_nospace;
319 src_ptr = c;
320 continue;
321 }
322
323 *dst_ptr = '\0';
324 return 0;
325
326err_nospace:
327 ERROR("destination buffer overflow while expanding '%s'\n", src);
328err:
329 return -1;
330}
331
Greg Hackmannd68db712013-11-18 09:44:20 -0800332static void parse_import(struct parse_state *state, int nargs, char **args)
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800333{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800334 struct listnode *import_list = (listnode*) state->priv;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800335 char conf_file[PATH_MAX];
336 int ret;
337
338 if (nargs != 2) {
339 ERROR("single argument needed for import\n");
340 return;
341 }
342
343 ret = expand_props(conf_file, args[1], sizeof(conf_file));
344 if (ret) {
345 ERROR("error while handling import on line '%d' in '%s'\n",
346 state->line, state->filename);
347 return;
348 }
349
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800350 struct import* import = (struct import*) calloc(1, sizeof(struct import));
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800351 import->filename = strdup(conf_file);
352 list_add_tail(import_list, &import->list);
353 INFO("found import '%s', adding to import list", import->filename);
354}
355
Greg Hackmannd68db712013-11-18 09:44:20 -0800356static void parse_new_section(struct parse_state *state, int kw,
Colin Cross6310a822010-04-20 14:29:05 -0700357 int nargs, char **args)
358{
359 printf("[ %s %s ]\n", args[0],
360 nargs > 1 ? args[1] : "");
361 switch(kw) {
362 case K_service:
363 state->context = parse_service(state, nargs, args);
364 if (state->context) {
365 state->parse_line = parse_line_service;
366 return;
367 }
368 break;
369 case K_on:
370 state->context = parse_action(state, nargs, args);
371 if (state->context) {
372 state->parse_line = parse_line_action;
373 return;
374 }
375 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700376 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800377 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800378 break;
Colin Cross6310a822010-04-20 14:29:05 -0700379 }
380 state->parse_line = parse_line_no_op;
381}
382
Elliott Hughesf682b472015-02-06 12:19:48 -0800383static void parse_config(const char *fn, const std::string& data)
Colin Cross6310a822010-04-20 14:29:05 -0700384{
385 struct parse_state state;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800386 struct listnode import_list;
387 struct listnode *node;
Colin Cross6310a822010-04-20 14:29:05 -0700388 char *args[INIT_PARSER_MAXARGS];
389 int nargs;
390
391 nargs = 0;
392 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800393 state.line = 0;
Elliott Hughesf682b472015-02-06 12:19:48 -0800394 state.ptr = strdup(data.c_str()); // TODO: fix this code!
Colin Cross6310a822010-04-20 14:29:05 -0700395 state.nexttoken = 0;
396 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800397
398 list_init(&import_list);
399 state.priv = &import_list;
400
Colin Cross6310a822010-04-20 14:29:05 -0700401 for (;;) {
402 switch (next_token(&state)) {
403 case T_EOF:
404 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800405 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700406 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800407 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700408 if (nargs) {
409 int kw = lookup_keyword(args[0]);
410 if (kw_is(kw, SECTION)) {
411 state.parse_line(&state, 0, 0);
412 parse_new_section(&state, kw, nargs, args);
413 } else {
414 state.parse_line(&state, nargs, args);
415 }
416 nargs = 0;
417 }
418 break;
419 case T_TEXT:
420 if (nargs < INIT_PARSER_MAXARGS) {
421 args[nargs++] = state.text;
422 }
423 break;
424 }
425 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800426
427parser_done:
428 list_for_each(node, &import_list) {
429 struct import *import = node_to_item(node, struct import, list);
430 int ret;
431
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800432 ret = init_parse_config_file(import->filename);
433 if (ret)
434 ERROR("could not import file '%s' from '%s'\n",
435 import->filename, fn);
436 }
Colin Cross6310a822010-04-20 14:29:05 -0700437}
438
Elliott Hughesf682b472015-02-06 12:19:48 -0800439int init_parse_config_file(const char* path) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800440 INFO("Parsing %s...\n", path);
Elliott Hughesf682b472015-02-06 12:19:48 -0800441 std::string data;
442 if (!read_file(path, &data)) {
443 return -1;
444 }
Colin Cross6310a822010-04-20 14:29:05 -0700445
Elliott Hughesf682b472015-02-06 12:19:48 -0800446 parse_config(path, data);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800447 dump_parser_state();
Colin Cross6310a822010-04-20 14:29:05 -0700448 return 0;
449}
450
451static int valid_name(const char *name)
452{
453 if (strlen(name) > 16) {
454 return 0;
455 }
456 while (*name) {
457 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
458 return 0;
459 }
460 name++;
461 }
462 return 1;
463}
464
465struct service *service_find_by_name(const char *name)
466{
467 struct listnode *node;
468 struct service *svc;
469 list_for_each(node, &service_list) {
470 svc = node_to_item(node, struct service, slist);
471 if (!strcmp(svc->name, name)) {
472 return svc;
473 }
474 }
475 return 0;
476}
477
478struct service *service_find_by_pid(pid_t pid)
479{
480 struct listnode *node;
481 struct service *svc;
482 list_for_each(node, &service_list) {
483 svc = node_to_item(node, struct service, slist);
484 if (svc->pid == pid) {
485 return svc;
486 }
487 }
488 return 0;
489}
490
491struct service *service_find_by_keychord(int keychord_id)
492{
493 struct listnode *node;
494 struct service *svc;
495 list_for_each(node, &service_list) {
496 svc = node_to_item(node, struct service, slist);
497 if (svc->keychord_id == keychord_id) {
498 return svc;
499 }
500 }
501 return 0;
502}
503
504void service_for_each(void (*func)(struct service *svc))
505{
506 struct listnode *node;
507 struct service *svc;
508 list_for_each(node, &service_list) {
509 svc = node_to_item(node, struct service, slist);
510 func(svc);
511 }
512}
513
514void service_for_each_class(const char *classname,
515 void (*func)(struct service *svc))
516{
517 struct listnode *node;
518 struct service *svc;
519 list_for_each(node, &service_list) {
520 svc = node_to_item(node, struct service, slist);
521 if (!strcmp(svc->classname, classname)) {
522 func(svc);
523 }
524 }
525}
526
527void service_for_each_flags(unsigned matchflags,
528 void (*func)(struct service *svc))
529{
530 struct listnode *node;
531 struct service *svc;
532 list_for_each(node, &service_list) {
533 svc = node_to_item(node, struct service, slist);
534 if (svc->flags & matchflags) {
535 func(svc);
536 }
537 }
538}
539
540void action_for_each_trigger(const char *trigger,
541 void (*func)(struct action *act))
542{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700543 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700544 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700545 struct trigger *cur_trigger;
546
Colin Cross6310a822010-04-20 14:29:05 -0700547 list_for_each(node, &action_list) {
548 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700549 list_for_each(node2, &act->triggers) {
550 cur_trigger = node_to_item(node2, struct trigger, nlist);
551 if (!strcmp(cur_trigger->name, trigger)) {
552 func(act);
553 }
Colin Cross6310a822010-04-20 14:29:05 -0700554 }
555 }
556}
557
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700558
Colin Cross6310a822010-04-20 14:29:05 -0700559void queue_property_triggers(const char *name, const char *value)
560{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700561 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700562 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700563 struct trigger *cur_trigger;
564 bool match;
565 int name_length;
566
Colin Cross6310a822010-04-20 14:29:05 -0700567 list_for_each(node, &action_list) {
568 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700569 match = !name;
570 list_for_each(node2, &act->triggers) {
571 cur_trigger = node_to_item(node2, struct trigger, nlist);
572 if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) {
573 const char *test = cur_trigger->name + strlen("property:");
574 if (!match) {
575 name_length = strlen(name);
576 if (!strncmp(name, test, name_length) &&
577 test[name_length] == '=' &&
578 (!strcmp(test + name_length + 1, value) ||
579 !strcmp(test + name_length + 1, "*"))) {
580 match = true;
581 continue;
582 }
583 } else {
584 const char* equals = strchr(test, '=');
585 if (equals) {
586 char prop_name[PROP_NAME_MAX + 1];
587 char value[PROP_VALUE_MAX];
588 int length = equals - test;
589 if (length <= PROP_NAME_MAX) {
590 int ret;
591 memcpy(prop_name, test, length);
592 prop_name[length] = 0;
Colin Cross6310a822010-04-20 14:29:05 -0700593
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700594 /* does the property exist, and match the trigger value? */
595 ret = property_get(prop_name, value);
596 if (ret > 0 && (!strcmp(equals + 1, value) ||
597 !strcmp(equals + 1, "*"))) {
598 continue;
599 }
600 }
601 }
602 }
603 }
604 match = false;
605 break;
606 }
607 if (match) {
608 action_add_queue_tail(act);
Colin Cross6310a822010-04-20 14:29:05 -0700609 }
610 }
611}
612
613void queue_all_property_triggers()
614{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700615 queue_property_triggers(NULL, NULL);
Colin Cross6310a822010-04-20 14:29:05 -0700616}
617
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800618void queue_builtin_action(int (*func)(int nargs, char **args), const char *name)
Colin Cross6310a822010-04-20 14:29:05 -0700619{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800620 action* act = (action*) calloc(1, sizeof(*act));
621 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700622 cur_trigger->name = name;
623 list_init(&act->triggers);
624 list_add_tail(&act->triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700625 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800626 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700627
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800628 command* cmd = (command*) calloc(1, sizeof(*cmd));
Colin Cross6310a822010-04-20 14:29:05 -0700629 cmd->func = func;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800630 cmd->args[0] = const_cast<char*>(name);
Riley Andrews24a3b782014-06-26 13:56:01 -0700631 cmd->nargs = 1;
Colin Cross6310a822010-04-20 14:29:05 -0700632 list_add_tail(&act->commands, &cmd->clist);
633
634 list_add_tail(&action_list, &act->alist);
635 action_add_queue_tail(act);
636}
637
638void action_add_queue_tail(struct action *act)
639{
Colin Crossa5064622013-03-07 13:42:29 -0800640 if (list_empty(&act->qlist)) {
641 list_add_tail(&action_queue, &act->qlist);
642 }
Colin Cross6310a822010-04-20 14:29:05 -0700643}
644
645struct action *action_remove_queue_head(void)
646{
647 if (list_empty(&action_queue)) {
648 return 0;
649 } else {
650 struct listnode *node = list_head(&action_queue);
651 struct action *act = node_to_item(node, struct action, qlist);
652 list_remove(node);
Colin Crossa5064622013-03-07 13:42:29 -0800653 list_init(node);
Colin Cross6310a822010-04-20 14:29:05 -0700654 return act;
655 }
656}
657
658int action_queue_empty()
659{
660 return list_empty(&action_queue);
661}
662
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800663service* make_exec_oneshot_service(int nargs, char** args) {
664 // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS...
665 int command_arg = 1;
666 for (int i = 1; i < nargs; ++i) {
667 if (strcmp(args[i], "--") == 0) {
668 command_arg = i + 1;
669 break;
670 }
671 }
672 if (command_arg > 4 + NR_SVC_SUPP_GIDS) {
673 ERROR("exec called with too many supplementary group ids\n");
674 return NULL;
675 }
676
677 int argc = nargs - command_arg;
678 char** argv = (args + command_arg);
679 if (argc < 1) {
680 ERROR("exec called without command\n");
681 return NULL;
682 }
683
684 service* svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * argc);
685 if (svc == NULL) {
686 ERROR("Couldn't allocate service for exec of '%s': %s", argv[0], strerror(errno));
687 return NULL;
688 }
689
690 if (command_arg > 2) {
691 svc->seclabel = args[1];
692 }
693 if (command_arg > 3) {
694 svc->uid = decode_uid(args[2]);
695 }
696 if (command_arg > 4) {
697 svc->gid = decode_uid(args[3]);
698 svc->nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */;
699 for (size_t i = 0; i < svc->nr_supp_gids; ++i) {
700 svc->supp_gids[i] = decode_uid(args[4 + i]);
701 }
702 }
703
704 static int exec_count; // Every service needs a unique name.
705 char* name = NULL;
706 asprintf(&name, "exec %d (%s)", exec_count++, argv[0]);
707 if (name == NULL) {
708 ERROR("Couldn't allocate name for exec service '%s'\n", argv[0]);
709 free(svc);
710 return NULL;
711 }
712 svc->name = name;
713 svc->classname = "default";
714 svc->flags = SVC_EXEC | SVC_ONESHOT;
715 svc->nargs = argc;
716 memcpy(svc->args, argv, sizeof(char*) * svc->nargs);
717 svc->args[argc] = NULL;
718 list_add_tail(&service_list, &svc->slist);
719 return svc;
720}
721
Colin Cross6310a822010-04-20 14:29:05 -0700722static void *parse_service(struct parse_state *state, int nargs, char **args)
723{
Colin Cross6310a822010-04-20 14:29:05 -0700724 if (nargs < 3) {
725 parse_error(state, "services must have a name and a program\n");
726 return 0;
727 }
728 if (!valid_name(args[1])) {
729 parse_error(state, "invalid service name '%s'\n", args[1]);
730 return 0;
731 }
732
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800733 service* svc = (service*) service_find_by_name(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700734 if (svc) {
735 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
736 return 0;
737 }
738
739 nargs -= 2;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800740 svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700741 if (!svc) {
742 parse_error(state, "out of memory\n");
743 return 0;
744 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800745 svc->name = strdup(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700746 svc->classname = "default";
747 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800748 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Colin Cross6310a822010-04-20 14:29:05 -0700749 svc->args[nargs] = 0;
750 svc->nargs = nargs;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700751 list_init(&svc->onrestart.triggers);
752 cur_trigger->name = "onrestart";
753 list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700754 list_init(&svc->onrestart.commands);
755 list_add_tail(&service_list, &svc->slist);
756 return svc;
757}
758
759static void parse_line_service(struct parse_state *state, int nargs, char **args)
760{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800761 struct service *svc = (service*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700762 struct command *cmd;
763 int i, kw, kw_nargs;
764
765 if (nargs == 0) {
766 return;
767 }
768
769 svc->ioprio_class = IoSchedClass_NONE;
770
771 kw = lookup_keyword(args[0]);
772 switch (kw) {
773 case K_capability:
774 break;
775 case K_class:
776 if (nargs != 2) {
777 parse_error(state, "class option requires a classname\n");
778 } else {
779 svc->classname = args[1];
780 }
781 break;
782 case K_console:
783 svc->flags |= SVC_CONSOLE;
784 break;
785 case K_disabled:
786 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700787 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700788 break;
789 case K_ioprio:
790 if (nargs != 3) {
791 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
792 } else {
793 svc->ioprio_pri = strtoul(args[2], 0, 8);
794
795 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
796 parse_error(state, "priority value must be range 0 - 7\n");
797 break;
798 }
799
800 if (!strcmp(args[1], "rt")) {
801 svc->ioprio_class = IoSchedClass_RT;
802 } else if (!strcmp(args[1], "be")) {
803 svc->ioprio_class = IoSchedClass_BE;
804 } else if (!strcmp(args[1], "idle")) {
805 svc->ioprio_class = IoSchedClass_IDLE;
806 } else {
807 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
808 }
809 }
810 break;
811 case K_group:
812 if (nargs < 2) {
813 parse_error(state, "group option requires a group id\n");
814 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
815 parse_error(state, "group option accepts at most %d supp. groups\n",
816 NR_SVC_SUPP_GIDS);
817 } else {
818 int n;
819 svc->gid = decode_uid(args[1]);
820 for (n = 2; n < nargs; n++) {
821 svc->supp_gids[n-2] = decode_uid(args[n]);
822 }
823 svc->nr_supp_gids = n - 2;
824 }
825 break;
826 case K_keycodes:
827 if (nargs < 2) {
828 parse_error(state, "keycodes option requires atleast one keycode\n");
829 } else {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800830 svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
Colin Cross6310a822010-04-20 14:29:05 -0700831 if (!svc->keycodes) {
832 parse_error(state, "could not allocate keycodes\n");
833 } else {
834 svc->nkeycodes = nargs - 1;
835 for (i = 1; i < nargs; i++) {
836 svc->keycodes[i - 1] = atoi(args[i]);
837 }
838 }
839 }
840 break;
841 case K_oneshot:
842 svc->flags |= SVC_ONESHOT;
843 break;
844 case K_onrestart:
845 nargs--;
846 args++;
847 kw = lookup_keyword(args[0]);
848 if (!kw_is(kw, COMMAND)) {
849 parse_error(state, "invalid command '%s'\n", args[0]);
850 break;
851 }
852 kw_nargs = kw_nargs(kw);
853 if (nargs < kw_nargs) {
854 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
855 kw_nargs > 2 ? "arguments" : "argument");
856 break;
857 }
858
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800859 cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700860 cmd->func = kw_func(kw);
861 cmd->nargs = nargs;
862 memcpy(cmd->args, args, sizeof(char*) * nargs);
863 list_add_tail(&svc->onrestart.commands, &cmd->clist);
864 break;
865 case K_critical:
866 svc->flags |= SVC_CRITICAL;
867 break;
868 case K_setenv: { /* name value */
Gavin.Changc3a46762013-09-28 00:22:11 +0800869 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700870 parse_error(state, "setenv option requires name and value arguments\n");
871 break;
872 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800873 svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
Colin Cross6310a822010-04-20 14:29:05 -0700874 if (!ei) {
875 parse_error(state, "out of memory\n");
876 break;
877 }
878 ei->name = args[1];
879 ei->value = args[2];
880 ei->next = svc->envvars;
881 svc->envvars = ei;
882 break;
883 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400884 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700885 if (nargs < 4) {
886 parse_error(state, "socket option requires name, type, perm arguments\n");
887 break;
888 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400889 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
890 && strcmp(args[2],"seqpacket")) {
891 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700892 break;
893 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800894 socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
Colin Cross6310a822010-04-20 14:29:05 -0700895 if (!si) {
896 parse_error(state, "out of memory\n");
897 break;
898 }
899 si->name = args[1];
900 si->type = args[2];
901 si->perm = strtoul(args[3], 0, 8);
902 if (nargs > 4)
903 si->uid = decode_uid(args[4]);
904 if (nargs > 5)
905 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400906 if (nargs > 6)
907 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700908 si->next = svc->sockets;
909 svc->sockets = si;
910 break;
911 }
912 case K_user:
913 if (nargs != 2) {
914 parse_error(state, "user option requires a user id\n");
915 } else {
916 svc->uid = decode_uid(args[1]);
917 }
918 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500919 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500920 if (nargs != 2) {
921 parse_error(state, "seclabel option requires a label string\n");
922 } else {
923 svc->seclabel = args[1];
924 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500925 break;
926
Colin Cross6310a822010-04-20 14:29:05 -0700927 default:
928 parse_error(state, "invalid option '%s'\n", args[0]);
929 }
930}
931
932static void *parse_action(struct parse_state *state, int nargs, char **args)
933{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700934 struct trigger *cur_trigger;
935 int i;
Colin Cross6310a822010-04-20 14:29:05 -0700936 if (nargs < 2) {
937 parse_error(state, "actions must have a trigger\n");
938 return 0;
939 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700940
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800941 action* act = (action*) calloc(1, sizeof(*act));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700942 list_init(&act->triggers);
943
944 for (i = 1; i < nargs; i++) {
945 if (!(i % 2)) {
946 if (strcmp(args[i], "&&")) {
947 parse_error(state, "& is the only symbol allowed to concatenate actions\n");
948 return 0;
949 } else
950 continue;
951 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800952 cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700953 cur_trigger->name = args[i];
954 list_add_tail(&act->triggers, &cur_trigger->nlist);
955 }
956
Colin Cross6310a822010-04-20 14:29:05 -0700957 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800958 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700959 list_add_tail(&action_list, &act->alist);
960 /* XXX add to hash */
961 return act;
962}
963
964static void parse_line_action(struct parse_state* state, int nargs, char **args)
965{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800966 struct action *act = (action*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700967 int kw, n;
968
969 if (nargs == 0) {
970 return;
971 }
972
973 kw = lookup_keyword(args[0]);
974 if (!kw_is(kw, COMMAND)) {
975 parse_error(state, "invalid command '%s'\n", args[0]);
976 return;
977 }
978
979 n = kw_nargs(kw);
980 if (nargs < n) {
981 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
982 n > 2 ? "arguments" : "argument");
983 return;
984 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800985 command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700986 cmd->func = kw_func(kw);
Riley Andrews24a3b782014-06-26 13:56:01 -0700987 cmd->line = state->line;
988 cmd->filename = state->filename;
Colin Cross6310a822010-04-20 14:29:05 -0700989 cmd->nargs = nargs;
990 memcpy(cmd->args, args, sizeof(char*) * nargs);
991 list_add_tail(&act->commands, &cmd->clist);
992}