blob: 39323573cac1f1e12420405967e660b385a05060 [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
17#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <fcntl.h>
21#include <stdarg.h>
22#include <string.h>
23#include <stddef.h>
24#include <ctype.h>
25
26#include "init.h"
27#include "parser.h"
28#include "init_parser.h"
29#include "log.h"
Colin Cross6310a822010-04-20 14:29:05 -070030#include "property_service.h"
31#include "util.h"
32
33#include <cutils/iosched_policy.h>
Dima Zavinda04c522011-09-01 17:09:44 -070034#include <cutils/list.h>
Colin Cross6310a822010-04-20 14:29:05 -070035
Colin Cross6310a822010-04-20 14:29:05 -070036static list_declare(service_list);
37static list_declare(action_list);
38static list_declare(action_queue);
39
Dima Zavin78a1b1f2011-12-20 12:53:48 -080040struct import {
41 struct listnode list;
42 const char *filename;
43};
44
Colin Cross6310a822010-04-20 14:29:05 -070045static void *parse_service(struct parse_state *state, int nargs, char **args);
46static void parse_line_service(struct parse_state *state, int nargs, char **args);
47
48static void *parse_action(struct parse_state *state, int nargs, char **args);
49static void parse_line_action(struct parse_state *state, int nargs, char **args);
50
51#define SECTION 0x01
52#define COMMAND 0x02
53#define OPTION 0x04
54
55#include "keywords.h"
56
57#define KEYWORD(symbol, flags, nargs, func) \
58 [ K_##symbol ] = { #symbol, func, nargs + 1, flags, },
59
Greg Hackmannd68db712013-11-18 09:44:20 -080060static struct {
Colin Cross6310a822010-04-20 14:29:05 -070061 const char *name;
62 int (*func)(int nargs, char **args);
63 unsigned char nargs;
64 unsigned char flags;
65} keyword_info[KEYWORD_COUNT] = {
66 [ K_UNKNOWN ] = { "unknown", 0, 0, 0 },
67#include "keywords.h"
68};
69#undef KEYWORD
70
71#define kw_is(kw, type) (keyword_info[kw].flags & (type))
72#define kw_name(kw) (keyword_info[kw].name)
73#define kw_func(kw) (keyword_info[kw].func)
74#define kw_nargs(kw) (keyword_info[kw].nargs)
75
Elliott Hughesc0e919c2015-02-04 14:46:36 -080076void dump_parser_state() {
77 if (false) {
78 struct listnode* node;
79 list_for_each(node, &service_list) {
80 service* svc = node_to_item(node, struct service, slist);
81 INFO("service %s\n", svc->name);
82 INFO(" class '%s'\n", svc->classname);
83 INFO(" exec");
84 for (int n = 0; n < svc->nargs; n++) {
85 INFO(" '%s'", svc->args[n]);
86 }
87 INFO("\n");
88 for (socketinfo* si = svc->sockets; si; si = si->next) {
89 INFO(" socket %s %s 0%o\n", si->name, si->type, si->perm);
90 }
91 }
92
93 list_for_each(node, &action_list) {
94 action* act = node_to_item(node, struct action, alist);
95 INFO("on ");
96 char name_str[256] = "";
97 build_triggers_string(name_str, sizeof(name_str), act);
98 INFO("%s", name_str);
99 INFO("\n");
100
101 struct listnode* node2;
102 list_for_each(node2, &act->commands) {
103 command* cmd = node_to_item(node2, struct command, clist);
104 INFO(" %p", cmd->func);
105 for (int n = 0; n < cmd->nargs; n++) {
106 INFO(" %s", cmd->args[n]);
107 }
108 INFO("\n");
109 }
110 INFO("\n");
111 }
112 }
113}
114
Greg Hackmannd68db712013-11-18 09:44:20 -0800115static int lookup_keyword(const char *s)
Colin Cross6310a822010-04-20 14:29:05 -0700116{
117 switch (*s++) {
Yongqin Liua197ff12014-12-05 13:45:02 +0800118 case 'b':
119 if (!strcmp(s, "ootchart_init")) return K_bootchart_init;
Colin Cross6310a822010-04-20 14:29:05 -0700120 case 'c':
Elliott Hughesf682b472015-02-06 12:19:48 -0800121 if (!strcmp(s, "opy")) return K_copy;
Colin Cross6310a822010-04-20 14:29:05 -0700122 if (!strcmp(s, "apability")) return K_capability;
123 if (!strcmp(s, "hdir")) return K_chdir;
124 if (!strcmp(s, "hroot")) return K_chroot;
125 if (!strcmp(s, "lass")) return K_class;
126 if (!strcmp(s, "lass_start")) return K_class_start;
127 if (!strcmp(s, "lass_stop")) return K_class_stop;
Ken Sumrall752923c2010-12-03 16:33:31 -0800128 if (!strcmp(s, "lass_reset")) return K_class_reset;
Colin Cross6310a822010-04-20 14:29:05 -0700129 if (!strcmp(s, "onsole")) return K_console;
130 if (!strcmp(s, "hown")) return K_chown;
131 if (!strcmp(s, "hmod")) return K_chmod;
132 if (!strcmp(s, "ritical")) return K_critical;
133 break;
134 case 'd':
135 if (!strcmp(s, "isabled")) return K_disabled;
136 if (!strcmp(s, "omainname")) return K_domainname;
137 break;
138 case 'e':
JP Abgrall3beec7e2014-05-02 21:14:29 -0700139 if (!strcmp(s, "nable")) return K_enable;
Colin Cross6310a822010-04-20 14:29:05 -0700140 if (!strcmp(s, "xec")) return K_exec;
San Mehat429721c2014-09-23 07:48:47 -0700141 if (!strcmp(s, "xeconce")) return K_execonce;
Colin Cross6310a822010-04-20 14:29:05 -0700142 if (!strcmp(s, "xport")) return K_export;
143 break;
144 case 'g':
145 if (!strcmp(s, "roup")) return K_group;
146 break;
147 case 'h':
148 if (!strcmp(s, "ostname")) return K_hostname;
149 break;
150 case 'i':
151 if (!strcmp(s, "oprio")) return K_ioprio;
152 if (!strcmp(s, "fup")) return K_ifup;
153 if (!strcmp(s, "nsmod")) return K_insmod;
154 if (!strcmp(s, "mport")) return K_import;
155 break;
156 case 'k':
157 if (!strcmp(s, "eycodes")) return K_keycodes;
158 break;
159 case 'l':
160 if (!strcmp(s, "oglevel")) return K_loglevel;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800161 if (!strcmp(s, "oad_persist_props")) return K_load_persist_props;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700162 if (!strcmp(s, "oad_all_props")) return K_load_all_props;
Colin Cross6310a822010-04-20 14:29:05 -0700163 break;
164 case 'm':
165 if (!strcmp(s, "kdir")) return K_mkdir;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700166 if (!strcmp(s, "ount_all")) return K_mount_all;
Colin Cross6310a822010-04-20 14:29:05 -0700167 if (!strcmp(s, "ount")) return K_mount;
168 break;
169 case 'o':
170 if (!strcmp(s, "n")) return K_on;
171 if (!strcmp(s, "neshot")) return K_oneshot;
172 if (!strcmp(s, "nrestart")) return K_onrestart;
173 break;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700174 case 'p':
175 if (!strcmp(s, "owerctl")) return K_powerctl;
Elliott Hughesf682b472015-02-06 12:19:48 -0800176 break;
Colin Cross6310a822010-04-20 14:29:05 -0700177 case 'r':
178 if (!strcmp(s, "estart")) return K_restart;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500179 if (!strcmp(s, "estorecon")) return K_restorecon;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400180 if (!strcmp(s, "estorecon_recursive")) return K_restorecon_recursive;
Ken Sumrall203bad52011-01-18 17:37:41 -0800181 if (!strcmp(s, "mdir")) return K_rmdir;
182 if (!strcmp(s, "m")) return K_rm;
Colin Cross6310a822010-04-20 14:29:05 -0700183 break;
184 case 's':
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500185 if (!strcmp(s, "eclabel")) return K_seclabel;
Colin Cross6310a822010-04-20 14:29:05 -0700186 if (!strcmp(s, "ervice")) return K_service;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500187 if (!strcmp(s, "etcon")) return K_setcon;
188 if (!strcmp(s, "etenforce")) return K_setenforce;
Colin Cross6310a822010-04-20 14:29:05 -0700189 if (!strcmp(s, "etenv")) return K_setenv;
190 if (!strcmp(s, "etkey")) return K_setkey;
191 if (!strcmp(s, "etprop")) return K_setprop;
192 if (!strcmp(s, "etrlimit")) return K_setrlimit;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500193 if (!strcmp(s, "etsebool")) return K_setsebool;
Colin Cross6310a822010-04-20 14:29:05 -0700194 if (!strcmp(s, "ocket")) return K_socket;
195 if (!strcmp(s, "tart")) return K_start;
196 if (!strcmp(s, "top")) return K_stop;
Ken Sumralla76baaa2013-07-09 18:42:09 -0700197 if (!strcmp(s, "wapon_all")) return K_swapon_all;
Colin Cross6310a822010-04-20 14:29:05 -0700198 if (!strcmp(s, "ymlink")) return K_symlink;
199 if (!strcmp(s, "ysclktz")) return K_sysclktz;
200 break;
201 case 't':
202 if (!strcmp(s, "rigger")) return K_trigger;
203 break;
204 case 'u':
205 if (!strcmp(s, "ser")) return K_user;
206 break;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000207 case 'v':
208 if (!strcmp(s, "erity_load_state")) return K_verity_load_state;
209 break;
Colin Cross6310a822010-04-20 14:29:05 -0700210 case 'w':
211 if (!strcmp(s, "rite")) return K_write;
212 if (!strcmp(s, "ait")) return K_wait;
213 break;
214 }
215 return K_UNKNOWN;
216}
217
Elliott Hughesf682b472015-02-06 12:19:48 -0800218static void parse_line_no_op(struct parse_state*, int, char**) {
Colin Cross6310a822010-04-20 14:29:05 -0700219}
220
Dima Zavina6235ea2011-12-16 14:20:12 -0800221static int push_chars(char **dst, int *len, const char *chars, int cnt)
222{
223 if (cnt > *len)
224 return -1;
225
226 memcpy(*dst, chars, cnt);
227 *dst += cnt;
228 *len -= cnt;
229
230 return 0;
231}
232
Dima Zavin84bf9af2011-12-20 13:44:41 -0800233int expand_props(char *dst, const char *src, int dst_size)
Dima Zavina6235ea2011-12-16 14:20:12 -0800234{
Dima Zavina6235ea2011-12-16 14:20:12 -0800235 char *dst_ptr = dst;
236 const char *src_ptr = src;
Dima Zavina6235ea2011-12-16 14:20:12 -0800237 int ret = 0;
238 int left = dst_size - 1;
239
240 if (!src || !dst || dst_size == 0)
241 return -1;
242
Dima Zavina6235ea2011-12-16 14:20:12 -0800243 /* - variables can either be $x.y or ${x.y}, in case they are only part
244 * of the string.
245 * - will accept $$ as a literal $.
246 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
247 * bad things will happen
248 */
249 while (*src_ptr && left > 0) {
250 char *c;
251 char prop[PROP_NAME_MAX + 1];
Colin Cross2deedfe2013-01-28 17:13:35 -0800252 char prop_val[PROP_VALUE_MAX];
Dima Zavina6235ea2011-12-16 14:20:12 -0800253 int prop_len = 0;
Colin Cross2deedfe2013-01-28 17:13:35 -0800254 int prop_val_len;
Dima Zavina6235ea2011-12-16 14:20:12 -0800255
256 c = strchr(src_ptr, '$');
257 if (!c) {
258 while (left-- > 0 && *src_ptr)
259 *(dst_ptr++) = *(src_ptr++);
260 break;
261 }
262
263 memset(prop, 0, sizeof(prop));
264
265 ret = push_chars(&dst_ptr, &left, src_ptr, c - src_ptr);
266 if (ret < 0)
267 goto err_nospace;
268 c++;
269
270 if (*c == '$') {
271 *(dst_ptr++) = *(c++);
272 src_ptr = c;
273 left--;
274 continue;
275 } else if (*c == '\0') {
276 break;
277 }
278
279 if (*c == '{') {
280 c++;
281 while (*c && *c != '}' && prop_len < PROP_NAME_MAX)
282 prop[prop_len++] = *(c++);
283 if (*c != '}') {
284 /* failed to find closing brace, abort. */
285 if (prop_len == PROP_NAME_MAX)
286 ERROR("prop name too long during expansion of '%s'\n",
287 src);
288 else if (*c == '\0')
289 ERROR("unexpected end of string in '%s', looking for }\n",
290 src);
291 goto err;
292 }
293 prop[prop_len] = '\0';
294 c++;
295 } else if (*c) {
296 while (*c && prop_len < PROP_NAME_MAX)
297 prop[prop_len++] = *(c++);
298 if (prop_len == PROP_NAME_MAX && *c != '\0') {
299 ERROR("prop name too long in '%s'\n", src);
300 goto err;
301 }
302 prop[prop_len] = '\0';
303 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
304 prop);
305 }
306
307 if (prop_len == 0) {
308 ERROR("invalid zero-length prop name in '%s'\n", src);
309 goto err;
310 }
311
Colin Cross2deedfe2013-01-28 17:13:35 -0800312 prop_val_len = property_get(prop, prop_val);
313 if (!prop_val_len) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800314 ERROR("property '%s' doesn't exist while expanding '%s'\n",
315 prop, src);
316 goto err;
317 }
318
Colin Cross2deedfe2013-01-28 17:13:35 -0800319 ret = push_chars(&dst_ptr, &left, prop_val, prop_val_len);
Dima Zavina6235ea2011-12-16 14:20:12 -0800320 if (ret < 0)
321 goto err_nospace;
322 src_ptr = c;
323 continue;
324 }
325
326 *dst_ptr = '\0';
327 return 0;
328
329err_nospace:
330 ERROR("destination buffer overflow while expanding '%s'\n", src);
331err:
332 return -1;
333}
334
Greg Hackmannd68db712013-11-18 09:44:20 -0800335static void parse_import(struct parse_state *state, int nargs, char **args)
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800336{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800337 struct listnode *import_list = (listnode*) state->priv;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800338 char conf_file[PATH_MAX];
339 int ret;
340
341 if (nargs != 2) {
342 ERROR("single argument needed for import\n");
343 return;
344 }
345
346 ret = expand_props(conf_file, args[1], sizeof(conf_file));
347 if (ret) {
348 ERROR("error while handling import on line '%d' in '%s'\n",
349 state->line, state->filename);
350 return;
351 }
352
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800353 struct import* import = (struct import*) calloc(1, sizeof(struct import));
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800354 import->filename = strdup(conf_file);
355 list_add_tail(import_list, &import->list);
356 INFO("found import '%s', adding to import list", import->filename);
357}
358
Greg Hackmannd68db712013-11-18 09:44:20 -0800359static void parse_new_section(struct parse_state *state, int kw,
Colin Cross6310a822010-04-20 14:29:05 -0700360 int nargs, char **args)
361{
362 printf("[ %s %s ]\n", args[0],
363 nargs > 1 ? args[1] : "");
364 switch(kw) {
365 case K_service:
366 state->context = parse_service(state, nargs, args);
367 if (state->context) {
368 state->parse_line = parse_line_service;
369 return;
370 }
371 break;
372 case K_on:
373 state->context = parse_action(state, nargs, args);
374 if (state->context) {
375 state->parse_line = parse_line_action;
376 return;
377 }
378 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700379 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800380 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800381 break;
Colin Cross6310a822010-04-20 14:29:05 -0700382 }
383 state->parse_line = parse_line_no_op;
384}
385
Elliott Hughesf682b472015-02-06 12:19:48 -0800386static void parse_config(const char *fn, const std::string& data)
Colin Cross6310a822010-04-20 14:29:05 -0700387{
388 struct parse_state state;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800389 struct listnode import_list;
390 struct listnode *node;
Colin Cross6310a822010-04-20 14:29:05 -0700391 char *args[INIT_PARSER_MAXARGS];
392 int nargs;
393
394 nargs = 0;
395 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800396 state.line = 0;
Elliott Hughesf682b472015-02-06 12:19:48 -0800397 state.ptr = strdup(data.c_str()); // TODO: fix this code!
Colin Cross6310a822010-04-20 14:29:05 -0700398 state.nexttoken = 0;
399 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800400
401 list_init(&import_list);
402 state.priv = &import_list;
403
Colin Cross6310a822010-04-20 14:29:05 -0700404 for (;;) {
405 switch (next_token(&state)) {
406 case T_EOF:
407 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800408 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700409 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800410 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700411 if (nargs) {
412 int kw = lookup_keyword(args[0]);
413 if (kw_is(kw, SECTION)) {
414 state.parse_line(&state, 0, 0);
415 parse_new_section(&state, kw, nargs, args);
416 } else {
417 state.parse_line(&state, nargs, args);
418 }
419 nargs = 0;
420 }
421 break;
422 case T_TEXT:
423 if (nargs < INIT_PARSER_MAXARGS) {
424 args[nargs++] = state.text;
425 }
426 break;
427 }
428 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800429
430parser_done:
431 list_for_each(node, &import_list) {
432 struct import *import = node_to_item(node, struct import, list);
433 int ret;
434
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800435 ret = init_parse_config_file(import->filename);
436 if (ret)
437 ERROR("could not import file '%s' from '%s'\n",
438 import->filename, fn);
439 }
Colin Cross6310a822010-04-20 14:29:05 -0700440}
441
Elliott Hughesf682b472015-02-06 12:19:48 -0800442int init_parse_config_file(const char* path) {
443 INFO("Parsing %s...", path);
444 std::string data;
445 if (!read_file(path, &data)) {
446 return -1;
447 }
Colin Cross6310a822010-04-20 14:29:05 -0700448
Elliott Hughesf682b472015-02-06 12:19:48 -0800449 parse_config(path, data);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800450 dump_parser_state();
Colin Cross6310a822010-04-20 14:29:05 -0700451 return 0;
452}
453
454static int valid_name(const char *name)
455{
456 if (strlen(name) > 16) {
457 return 0;
458 }
459 while (*name) {
460 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
461 return 0;
462 }
463 name++;
464 }
465 return 1;
466}
467
468struct service *service_find_by_name(const char *name)
469{
470 struct listnode *node;
471 struct service *svc;
472 list_for_each(node, &service_list) {
473 svc = node_to_item(node, struct service, slist);
474 if (!strcmp(svc->name, name)) {
475 return svc;
476 }
477 }
478 return 0;
479}
480
481struct service *service_find_by_pid(pid_t pid)
482{
483 struct listnode *node;
484 struct service *svc;
485 list_for_each(node, &service_list) {
486 svc = node_to_item(node, struct service, slist);
487 if (svc->pid == pid) {
488 return svc;
489 }
490 }
491 return 0;
492}
493
494struct service *service_find_by_keychord(int keychord_id)
495{
496 struct listnode *node;
497 struct service *svc;
498 list_for_each(node, &service_list) {
499 svc = node_to_item(node, struct service, slist);
500 if (svc->keychord_id == keychord_id) {
501 return svc;
502 }
503 }
504 return 0;
505}
506
507void service_for_each(void (*func)(struct service *svc))
508{
509 struct listnode *node;
510 struct service *svc;
511 list_for_each(node, &service_list) {
512 svc = node_to_item(node, struct service, slist);
513 func(svc);
514 }
515}
516
517void service_for_each_class(const char *classname,
518 void (*func)(struct service *svc))
519{
520 struct listnode *node;
521 struct service *svc;
522 list_for_each(node, &service_list) {
523 svc = node_to_item(node, struct service, slist);
524 if (!strcmp(svc->classname, classname)) {
525 func(svc);
526 }
527 }
528}
529
530void service_for_each_flags(unsigned matchflags,
531 void (*func)(struct service *svc))
532{
533 struct listnode *node;
534 struct service *svc;
535 list_for_each(node, &service_list) {
536 svc = node_to_item(node, struct service, slist);
537 if (svc->flags & matchflags) {
538 func(svc);
539 }
540 }
541}
542
543void action_for_each_trigger(const char *trigger,
544 void (*func)(struct action *act))
545{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700546 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700547 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700548 struct trigger *cur_trigger;
549
Colin Cross6310a822010-04-20 14:29:05 -0700550 list_for_each(node, &action_list) {
551 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700552 list_for_each(node2, &act->triggers) {
553 cur_trigger = node_to_item(node2, struct trigger, nlist);
554 if (!strcmp(cur_trigger->name, trigger)) {
555 func(act);
556 }
Colin Cross6310a822010-04-20 14:29:05 -0700557 }
558 }
559}
560
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700561
Colin Cross6310a822010-04-20 14:29:05 -0700562void queue_property_triggers(const char *name, const char *value)
563{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700564 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700565 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700566 struct trigger *cur_trigger;
567 bool match;
568 int name_length;
569
Colin Cross6310a822010-04-20 14:29:05 -0700570 list_for_each(node, &action_list) {
571 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700572 match = !name;
573 list_for_each(node2, &act->triggers) {
574 cur_trigger = node_to_item(node2, struct trigger, nlist);
575 if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) {
576 const char *test = cur_trigger->name + strlen("property:");
577 if (!match) {
578 name_length = strlen(name);
579 if (!strncmp(name, test, name_length) &&
580 test[name_length] == '=' &&
581 (!strcmp(test + name_length + 1, value) ||
582 !strcmp(test + name_length + 1, "*"))) {
583 match = true;
584 continue;
585 }
586 } else {
587 const char* equals = strchr(test, '=');
588 if (equals) {
589 char prop_name[PROP_NAME_MAX + 1];
590 char value[PROP_VALUE_MAX];
591 int length = equals - test;
592 if (length <= PROP_NAME_MAX) {
593 int ret;
594 memcpy(prop_name, test, length);
595 prop_name[length] = 0;
Colin Cross6310a822010-04-20 14:29:05 -0700596
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700597 /* does the property exist, and match the trigger value? */
598 ret = property_get(prop_name, value);
599 if (ret > 0 && (!strcmp(equals + 1, value) ||
600 !strcmp(equals + 1, "*"))) {
601 continue;
602 }
603 }
604 }
605 }
606 }
607 match = false;
608 break;
609 }
610 if (match) {
611 action_add_queue_tail(act);
Colin Cross6310a822010-04-20 14:29:05 -0700612 }
613 }
614}
615
616void queue_all_property_triggers()
617{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700618 queue_property_triggers(NULL, NULL);
Colin Cross6310a822010-04-20 14:29:05 -0700619}
620
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800621void queue_builtin_action(int (*func)(int nargs, char **args), const char *name)
Colin Cross6310a822010-04-20 14:29:05 -0700622{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800623 action* act = (action*) calloc(1, sizeof(*act));
624 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700625 cur_trigger->name = name;
626 list_init(&act->triggers);
627 list_add_tail(&act->triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700628 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800629 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700630
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800631 command* cmd = (command*) calloc(1, sizeof(*cmd));
Colin Cross6310a822010-04-20 14:29:05 -0700632 cmd->func = func;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800633 cmd->args[0] = const_cast<char*>(name);
Riley Andrews24a3b782014-06-26 13:56:01 -0700634 cmd->nargs = 1;
Colin Cross6310a822010-04-20 14:29:05 -0700635 list_add_tail(&act->commands, &cmd->clist);
636
637 list_add_tail(&action_list, &act->alist);
638 action_add_queue_tail(act);
639}
640
641void action_add_queue_tail(struct action *act)
642{
Colin Crossa5064622013-03-07 13:42:29 -0800643 if (list_empty(&act->qlist)) {
644 list_add_tail(&action_queue, &act->qlist);
645 }
Colin Cross6310a822010-04-20 14:29:05 -0700646}
647
648struct action *action_remove_queue_head(void)
649{
650 if (list_empty(&action_queue)) {
651 return 0;
652 } else {
653 struct listnode *node = list_head(&action_queue);
654 struct action *act = node_to_item(node, struct action, qlist);
655 list_remove(node);
Colin Crossa5064622013-03-07 13:42:29 -0800656 list_init(node);
Colin Cross6310a822010-04-20 14:29:05 -0700657 return act;
658 }
659}
660
661int action_queue_empty()
662{
663 return list_empty(&action_queue);
664}
665
666static void *parse_service(struct parse_state *state, int nargs, char **args)
667{
Colin Cross6310a822010-04-20 14:29:05 -0700668 if (nargs < 3) {
669 parse_error(state, "services must have a name and a program\n");
670 return 0;
671 }
672 if (!valid_name(args[1])) {
673 parse_error(state, "invalid service name '%s'\n", args[1]);
674 return 0;
675 }
676
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800677 service* svc = (service*) service_find_by_name(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700678 if (svc) {
679 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
680 return 0;
681 }
682
683 nargs -= 2;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800684 svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700685 if (!svc) {
686 parse_error(state, "out of memory\n");
687 return 0;
688 }
689 svc->name = args[1];
690 svc->classname = "default";
691 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800692 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Colin Cross6310a822010-04-20 14:29:05 -0700693 svc->args[nargs] = 0;
694 svc->nargs = nargs;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700695 list_init(&svc->onrestart.triggers);
696 cur_trigger->name = "onrestart";
697 list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700698 list_init(&svc->onrestart.commands);
699 list_add_tail(&service_list, &svc->slist);
700 return svc;
701}
702
703static void parse_line_service(struct parse_state *state, int nargs, char **args)
704{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800705 struct service *svc = (service*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700706 struct command *cmd;
707 int i, kw, kw_nargs;
708
709 if (nargs == 0) {
710 return;
711 }
712
713 svc->ioprio_class = IoSchedClass_NONE;
714
715 kw = lookup_keyword(args[0]);
716 switch (kw) {
717 case K_capability:
718 break;
719 case K_class:
720 if (nargs != 2) {
721 parse_error(state, "class option requires a classname\n");
722 } else {
723 svc->classname = args[1];
724 }
725 break;
726 case K_console:
727 svc->flags |= SVC_CONSOLE;
728 break;
729 case K_disabled:
730 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700731 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700732 break;
733 case K_ioprio:
734 if (nargs != 3) {
735 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
736 } else {
737 svc->ioprio_pri = strtoul(args[2], 0, 8);
738
739 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
740 parse_error(state, "priority value must be range 0 - 7\n");
741 break;
742 }
743
744 if (!strcmp(args[1], "rt")) {
745 svc->ioprio_class = IoSchedClass_RT;
746 } else if (!strcmp(args[1], "be")) {
747 svc->ioprio_class = IoSchedClass_BE;
748 } else if (!strcmp(args[1], "idle")) {
749 svc->ioprio_class = IoSchedClass_IDLE;
750 } else {
751 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
752 }
753 }
754 break;
755 case K_group:
756 if (nargs < 2) {
757 parse_error(state, "group option requires a group id\n");
758 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
759 parse_error(state, "group option accepts at most %d supp. groups\n",
760 NR_SVC_SUPP_GIDS);
761 } else {
762 int n;
763 svc->gid = decode_uid(args[1]);
764 for (n = 2; n < nargs; n++) {
765 svc->supp_gids[n-2] = decode_uid(args[n]);
766 }
767 svc->nr_supp_gids = n - 2;
768 }
769 break;
770 case K_keycodes:
771 if (nargs < 2) {
772 parse_error(state, "keycodes option requires atleast one keycode\n");
773 } else {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800774 svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
Colin Cross6310a822010-04-20 14:29:05 -0700775 if (!svc->keycodes) {
776 parse_error(state, "could not allocate keycodes\n");
777 } else {
778 svc->nkeycodes = nargs - 1;
779 for (i = 1; i < nargs; i++) {
780 svc->keycodes[i - 1] = atoi(args[i]);
781 }
782 }
783 }
784 break;
785 case K_oneshot:
786 svc->flags |= SVC_ONESHOT;
787 break;
788 case K_onrestart:
789 nargs--;
790 args++;
791 kw = lookup_keyword(args[0]);
792 if (!kw_is(kw, COMMAND)) {
793 parse_error(state, "invalid command '%s'\n", args[0]);
794 break;
795 }
796 kw_nargs = kw_nargs(kw);
797 if (nargs < kw_nargs) {
798 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
799 kw_nargs > 2 ? "arguments" : "argument");
800 break;
801 }
802
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800803 cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700804 cmd->func = kw_func(kw);
805 cmd->nargs = nargs;
806 memcpy(cmd->args, args, sizeof(char*) * nargs);
807 list_add_tail(&svc->onrestart.commands, &cmd->clist);
808 break;
809 case K_critical:
810 svc->flags |= SVC_CRITICAL;
811 break;
812 case K_setenv: { /* name value */
Gavin.Changc3a46762013-09-28 00:22:11 +0800813 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700814 parse_error(state, "setenv option requires name and value arguments\n");
815 break;
816 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800817 svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
Colin Cross6310a822010-04-20 14:29:05 -0700818 if (!ei) {
819 parse_error(state, "out of memory\n");
820 break;
821 }
822 ei->name = args[1];
823 ei->value = args[2];
824 ei->next = svc->envvars;
825 svc->envvars = ei;
826 break;
827 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400828 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700829 if (nargs < 4) {
830 parse_error(state, "socket option requires name, type, perm arguments\n");
831 break;
832 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400833 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
834 && strcmp(args[2],"seqpacket")) {
835 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700836 break;
837 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800838 socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
Colin Cross6310a822010-04-20 14:29:05 -0700839 if (!si) {
840 parse_error(state, "out of memory\n");
841 break;
842 }
843 si->name = args[1];
844 si->type = args[2];
845 si->perm = strtoul(args[3], 0, 8);
846 if (nargs > 4)
847 si->uid = decode_uid(args[4]);
848 if (nargs > 5)
849 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400850 if (nargs > 6)
851 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700852 si->next = svc->sockets;
853 svc->sockets = si;
854 break;
855 }
856 case K_user:
857 if (nargs != 2) {
858 parse_error(state, "user option requires a user id\n");
859 } else {
860 svc->uid = decode_uid(args[1]);
861 }
862 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500863 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500864 if (nargs != 2) {
865 parse_error(state, "seclabel option requires a label string\n");
866 } else {
867 svc->seclabel = args[1];
868 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500869 break;
870
Colin Cross6310a822010-04-20 14:29:05 -0700871 default:
872 parse_error(state, "invalid option '%s'\n", args[0]);
873 }
874}
875
876static void *parse_action(struct parse_state *state, int nargs, char **args)
877{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700878 struct trigger *cur_trigger;
879 int i;
Colin Cross6310a822010-04-20 14:29:05 -0700880 if (nargs < 2) {
881 parse_error(state, "actions must have a trigger\n");
882 return 0;
883 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700884
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800885 action* act = (action*) calloc(1, sizeof(*act));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700886 list_init(&act->triggers);
887
888 for (i = 1; i < nargs; i++) {
889 if (!(i % 2)) {
890 if (strcmp(args[i], "&&")) {
891 parse_error(state, "& is the only symbol allowed to concatenate actions\n");
892 return 0;
893 } else
894 continue;
895 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800896 cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700897 cur_trigger->name = args[i];
898 list_add_tail(&act->triggers, &cur_trigger->nlist);
899 }
900
Colin Cross6310a822010-04-20 14:29:05 -0700901 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800902 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700903 list_add_tail(&action_list, &act->alist);
904 /* XXX add to hash */
905 return act;
906}
907
908static void parse_line_action(struct parse_state* state, int nargs, char **args)
909{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800910 struct action *act = (action*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700911 int kw, n;
912
913 if (nargs == 0) {
914 return;
915 }
916
917 kw = lookup_keyword(args[0]);
918 if (!kw_is(kw, COMMAND)) {
919 parse_error(state, "invalid command '%s'\n", args[0]);
920 return;
921 }
922
923 n = kw_nargs(kw);
924 if (nargs < n) {
925 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
926 n > 2 ? "arguments" : "argument");
927 return;
928 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800929 command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700930 cmd->func = kw_func(kw);
Riley Andrews24a3b782014-06-26 13:56:01 -0700931 cmd->line = state->line;
932 cmd->filename = state->filename;
Colin Cross6310a822010-04-20 14:29:05 -0700933 cmd->nargs = nargs;
934 memcpy(cmd->args, args, sizeof(char*) * nargs);
935 list_add_tail(&act->commands, &cmd->clist);
936}