blob: 65fc1a684502b94869ed831407913e04dac5faa5 [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++) {
118 case 'c':
Elliott Hughesf682b472015-02-06 12:19:48 -0800119 if (!strcmp(s, "opy")) return K_copy;
Colin Cross6310a822010-04-20 14:29:05 -0700120 if (!strcmp(s, "apability")) return K_capability;
121 if (!strcmp(s, "hdir")) return K_chdir;
122 if (!strcmp(s, "hroot")) return K_chroot;
123 if (!strcmp(s, "lass")) return K_class;
124 if (!strcmp(s, "lass_start")) return K_class_start;
125 if (!strcmp(s, "lass_stop")) return K_class_stop;
Ken Sumrall752923c2010-12-03 16:33:31 -0800126 if (!strcmp(s, "lass_reset")) return K_class_reset;
Colin Cross6310a822010-04-20 14:29:05 -0700127 if (!strcmp(s, "onsole")) return K_console;
128 if (!strcmp(s, "hown")) return K_chown;
129 if (!strcmp(s, "hmod")) return K_chmod;
130 if (!strcmp(s, "ritical")) return K_critical;
131 break;
132 case 'd':
133 if (!strcmp(s, "isabled")) return K_disabled;
134 if (!strcmp(s, "omainname")) return K_domainname;
135 break;
136 case 'e':
JP Abgrall3beec7e2014-05-02 21:14:29 -0700137 if (!strcmp(s, "nable")) return K_enable;
Colin Cross6310a822010-04-20 14:29:05 -0700138 if (!strcmp(s, "xec")) return K_exec;
San Mehat429721c2014-09-23 07:48:47 -0700139 if (!strcmp(s, "xeconce")) return K_execonce;
Colin Cross6310a822010-04-20 14:29:05 -0700140 if (!strcmp(s, "xport")) return K_export;
141 break;
142 case 'g':
143 if (!strcmp(s, "roup")) return K_group;
144 break;
145 case 'h':
146 if (!strcmp(s, "ostname")) return K_hostname;
147 break;
148 case 'i':
149 if (!strcmp(s, "oprio")) return K_ioprio;
150 if (!strcmp(s, "fup")) return K_ifup;
151 if (!strcmp(s, "nsmod")) return K_insmod;
152 if (!strcmp(s, "mport")) return K_import;
153 break;
154 case 'k':
155 if (!strcmp(s, "eycodes")) return K_keycodes;
156 break;
157 case 'l':
158 if (!strcmp(s, "oglevel")) return K_loglevel;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800159 if (!strcmp(s, "oad_persist_props")) return K_load_persist_props;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700160 if (!strcmp(s, "oad_all_props")) return K_load_all_props;
Colin Cross6310a822010-04-20 14:29:05 -0700161 break;
162 case 'm':
163 if (!strcmp(s, "kdir")) return K_mkdir;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700164 if (!strcmp(s, "ount_all")) return K_mount_all;
Colin Cross6310a822010-04-20 14:29:05 -0700165 if (!strcmp(s, "ount")) return K_mount;
166 break;
167 case 'o':
168 if (!strcmp(s, "n")) return K_on;
169 if (!strcmp(s, "neshot")) return K_oneshot;
170 if (!strcmp(s, "nrestart")) return K_onrestart;
171 break;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700172 case 'p':
173 if (!strcmp(s, "owerctl")) return K_powerctl;
Elliott Hughesf682b472015-02-06 12:19:48 -0800174 break;
Colin Cross6310a822010-04-20 14:29:05 -0700175 case 'r':
176 if (!strcmp(s, "estart")) return K_restart;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500177 if (!strcmp(s, "estorecon")) return K_restorecon;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400178 if (!strcmp(s, "estorecon_recursive")) return K_restorecon_recursive;
Ken Sumrall203bad52011-01-18 17:37:41 -0800179 if (!strcmp(s, "mdir")) return K_rmdir;
180 if (!strcmp(s, "m")) return K_rm;
Colin Cross6310a822010-04-20 14:29:05 -0700181 break;
182 case 's':
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500183 if (!strcmp(s, "eclabel")) return K_seclabel;
Colin Cross6310a822010-04-20 14:29:05 -0700184 if (!strcmp(s, "ervice")) return K_service;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500185 if (!strcmp(s, "etcon")) return K_setcon;
186 if (!strcmp(s, "etenforce")) return K_setenforce;
Colin Cross6310a822010-04-20 14:29:05 -0700187 if (!strcmp(s, "etenv")) return K_setenv;
188 if (!strcmp(s, "etkey")) return K_setkey;
189 if (!strcmp(s, "etprop")) return K_setprop;
190 if (!strcmp(s, "etrlimit")) return K_setrlimit;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500191 if (!strcmp(s, "etsebool")) return K_setsebool;
Colin Cross6310a822010-04-20 14:29:05 -0700192 if (!strcmp(s, "ocket")) return K_socket;
193 if (!strcmp(s, "tart")) return K_start;
194 if (!strcmp(s, "top")) return K_stop;
Ken Sumralla76baaa2013-07-09 18:42:09 -0700195 if (!strcmp(s, "wapon_all")) return K_swapon_all;
Colin Cross6310a822010-04-20 14:29:05 -0700196 if (!strcmp(s, "ymlink")) return K_symlink;
197 if (!strcmp(s, "ysclktz")) return K_sysclktz;
198 break;
199 case 't':
200 if (!strcmp(s, "rigger")) return K_trigger;
201 break;
202 case 'u':
203 if (!strcmp(s, "ser")) return K_user;
204 break;
205 case 'w':
206 if (!strcmp(s, "rite")) return K_write;
207 if (!strcmp(s, "ait")) return K_wait;
208 break;
209 }
210 return K_UNKNOWN;
211}
212
Elliott Hughesf682b472015-02-06 12:19:48 -0800213static void parse_line_no_op(struct parse_state*, int, char**) {
Colin Cross6310a822010-04-20 14:29:05 -0700214}
215
Dima Zavina6235ea2011-12-16 14:20:12 -0800216static int push_chars(char **dst, int *len, const char *chars, int cnt)
217{
218 if (cnt > *len)
219 return -1;
220
221 memcpy(*dst, chars, cnt);
222 *dst += cnt;
223 *len -= cnt;
224
225 return 0;
226}
227
Dima Zavin84bf9af2011-12-20 13:44:41 -0800228int expand_props(char *dst, const char *src, int dst_size)
Dima Zavina6235ea2011-12-16 14:20:12 -0800229{
Dima Zavina6235ea2011-12-16 14:20:12 -0800230 char *dst_ptr = dst;
231 const char *src_ptr = src;
Dima Zavina6235ea2011-12-16 14:20:12 -0800232 int ret = 0;
233 int left = dst_size - 1;
234
235 if (!src || !dst || dst_size == 0)
236 return -1;
237
Dima Zavina6235ea2011-12-16 14:20:12 -0800238 /* - variables can either be $x.y or ${x.y}, in case they are only part
239 * of the string.
240 * - will accept $$ as a literal $.
241 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
242 * bad things will happen
243 */
244 while (*src_ptr && left > 0) {
245 char *c;
246 char prop[PROP_NAME_MAX + 1];
Colin Cross2deedfe2013-01-28 17:13:35 -0800247 char prop_val[PROP_VALUE_MAX];
Dima Zavina6235ea2011-12-16 14:20:12 -0800248 int prop_len = 0;
Colin Cross2deedfe2013-01-28 17:13:35 -0800249 int prop_val_len;
Dima Zavina6235ea2011-12-16 14:20:12 -0800250
251 c = strchr(src_ptr, '$');
252 if (!c) {
253 while (left-- > 0 && *src_ptr)
254 *(dst_ptr++) = *(src_ptr++);
255 break;
256 }
257
258 memset(prop, 0, sizeof(prop));
259
260 ret = push_chars(&dst_ptr, &left, src_ptr, c - src_ptr);
261 if (ret < 0)
262 goto err_nospace;
263 c++;
264
265 if (*c == '$') {
266 *(dst_ptr++) = *(c++);
267 src_ptr = c;
268 left--;
269 continue;
270 } else if (*c == '\0') {
271 break;
272 }
273
274 if (*c == '{') {
275 c++;
276 while (*c && *c != '}' && prop_len < PROP_NAME_MAX)
277 prop[prop_len++] = *(c++);
278 if (*c != '}') {
279 /* failed to find closing brace, abort. */
280 if (prop_len == PROP_NAME_MAX)
281 ERROR("prop name too long during expansion of '%s'\n",
282 src);
283 else if (*c == '\0')
284 ERROR("unexpected end of string in '%s', looking for }\n",
285 src);
286 goto err;
287 }
288 prop[prop_len] = '\0';
289 c++;
290 } else if (*c) {
291 while (*c && prop_len < PROP_NAME_MAX)
292 prop[prop_len++] = *(c++);
293 if (prop_len == PROP_NAME_MAX && *c != '\0') {
294 ERROR("prop name too long in '%s'\n", src);
295 goto err;
296 }
297 prop[prop_len] = '\0';
298 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
299 prop);
300 }
301
302 if (prop_len == 0) {
303 ERROR("invalid zero-length prop name in '%s'\n", src);
304 goto err;
305 }
306
Colin Cross2deedfe2013-01-28 17:13:35 -0800307 prop_val_len = property_get(prop, prop_val);
308 if (!prop_val_len) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800309 ERROR("property '%s' doesn't exist while expanding '%s'\n",
310 prop, src);
311 goto err;
312 }
313
Colin Cross2deedfe2013-01-28 17:13:35 -0800314 ret = push_chars(&dst_ptr, &left, prop_val, prop_val_len);
Dima Zavina6235ea2011-12-16 14:20:12 -0800315 if (ret < 0)
316 goto err_nospace;
317 src_ptr = c;
318 continue;
319 }
320
321 *dst_ptr = '\0';
322 return 0;
323
324err_nospace:
325 ERROR("destination buffer overflow while expanding '%s'\n", src);
326err:
327 return -1;
328}
329
Greg Hackmannd68db712013-11-18 09:44:20 -0800330static void parse_import(struct parse_state *state, int nargs, char **args)
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800331{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800332 struct listnode *import_list = (listnode*) state->priv;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800333 char conf_file[PATH_MAX];
334 int ret;
335
336 if (nargs != 2) {
337 ERROR("single argument needed for import\n");
338 return;
339 }
340
341 ret = expand_props(conf_file, args[1], sizeof(conf_file));
342 if (ret) {
343 ERROR("error while handling import on line '%d' in '%s'\n",
344 state->line, state->filename);
345 return;
346 }
347
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800348 struct import* import = (struct import*) calloc(1, sizeof(struct import));
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800349 import->filename = strdup(conf_file);
350 list_add_tail(import_list, &import->list);
351 INFO("found import '%s', adding to import list", import->filename);
352}
353
Greg Hackmannd68db712013-11-18 09:44:20 -0800354static void parse_new_section(struct parse_state *state, int kw,
Colin Cross6310a822010-04-20 14:29:05 -0700355 int nargs, char **args)
356{
357 printf("[ %s %s ]\n", args[0],
358 nargs > 1 ? args[1] : "");
359 switch(kw) {
360 case K_service:
361 state->context = parse_service(state, nargs, args);
362 if (state->context) {
363 state->parse_line = parse_line_service;
364 return;
365 }
366 break;
367 case K_on:
368 state->context = parse_action(state, nargs, args);
369 if (state->context) {
370 state->parse_line = parse_line_action;
371 return;
372 }
373 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700374 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800375 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800376 break;
Colin Cross6310a822010-04-20 14:29:05 -0700377 }
378 state->parse_line = parse_line_no_op;
379}
380
Elliott Hughesf682b472015-02-06 12:19:48 -0800381static void parse_config(const char *fn, const std::string& data)
Colin Cross6310a822010-04-20 14:29:05 -0700382{
383 struct parse_state state;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800384 struct listnode import_list;
385 struct listnode *node;
Colin Cross6310a822010-04-20 14:29:05 -0700386 char *args[INIT_PARSER_MAXARGS];
387 int nargs;
388
389 nargs = 0;
390 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800391 state.line = 0;
Elliott Hughesf682b472015-02-06 12:19:48 -0800392 state.ptr = strdup(data.c_str()); // TODO: fix this code!
Colin Cross6310a822010-04-20 14:29:05 -0700393 state.nexttoken = 0;
394 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800395
396 list_init(&import_list);
397 state.priv = &import_list;
398
Colin Cross6310a822010-04-20 14:29:05 -0700399 for (;;) {
400 switch (next_token(&state)) {
401 case T_EOF:
402 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800403 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700404 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800405 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700406 if (nargs) {
407 int kw = lookup_keyword(args[0]);
408 if (kw_is(kw, SECTION)) {
409 state.parse_line(&state, 0, 0);
410 parse_new_section(&state, kw, nargs, args);
411 } else {
412 state.parse_line(&state, nargs, args);
413 }
414 nargs = 0;
415 }
416 break;
417 case T_TEXT:
418 if (nargs < INIT_PARSER_MAXARGS) {
419 args[nargs++] = state.text;
420 }
421 break;
422 }
423 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800424
425parser_done:
426 list_for_each(node, &import_list) {
427 struct import *import = node_to_item(node, struct import, list);
428 int ret;
429
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800430 ret = init_parse_config_file(import->filename);
431 if (ret)
432 ERROR("could not import file '%s' from '%s'\n",
433 import->filename, fn);
434 }
Colin Cross6310a822010-04-20 14:29:05 -0700435}
436
Elliott Hughesf682b472015-02-06 12:19:48 -0800437int init_parse_config_file(const char* path) {
438 INFO("Parsing %s...", path);
439 std::string data;
440 if (!read_file(path, &data)) {
441 return -1;
442 }
Colin Cross6310a822010-04-20 14:29:05 -0700443
Elliott Hughesf682b472015-02-06 12:19:48 -0800444 parse_config(path, data);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800445 dump_parser_state();
Colin Cross6310a822010-04-20 14:29:05 -0700446 return 0;
447}
448
449static int valid_name(const char *name)
450{
451 if (strlen(name) > 16) {
452 return 0;
453 }
454 while (*name) {
455 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
456 return 0;
457 }
458 name++;
459 }
460 return 1;
461}
462
463struct service *service_find_by_name(const char *name)
464{
465 struct listnode *node;
466 struct service *svc;
467 list_for_each(node, &service_list) {
468 svc = node_to_item(node, struct service, slist);
469 if (!strcmp(svc->name, name)) {
470 return svc;
471 }
472 }
473 return 0;
474}
475
476struct service *service_find_by_pid(pid_t pid)
477{
478 struct listnode *node;
479 struct service *svc;
480 list_for_each(node, &service_list) {
481 svc = node_to_item(node, struct service, slist);
482 if (svc->pid == pid) {
483 return svc;
484 }
485 }
486 return 0;
487}
488
489struct service *service_find_by_keychord(int keychord_id)
490{
491 struct listnode *node;
492 struct service *svc;
493 list_for_each(node, &service_list) {
494 svc = node_to_item(node, struct service, slist);
495 if (svc->keychord_id == keychord_id) {
496 return svc;
497 }
498 }
499 return 0;
500}
501
502void service_for_each(void (*func)(struct service *svc))
503{
504 struct listnode *node;
505 struct service *svc;
506 list_for_each(node, &service_list) {
507 svc = node_to_item(node, struct service, slist);
508 func(svc);
509 }
510}
511
512void service_for_each_class(const char *classname,
513 void (*func)(struct service *svc))
514{
515 struct listnode *node;
516 struct service *svc;
517 list_for_each(node, &service_list) {
518 svc = node_to_item(node, struct service, slist);
519 if (!strcmp(svc->classname, classname)) {
520 func(svc);
521 }
522 }
523}
524
525void service_for_each_flags(unsigned matchflags,
526 void (*func)(struct service *svc))
527{
528 struct listnode *node;
529 struct service *svc;
530 list_for_each(node, &service_list) {
531 svc = node_to_item(node, struct service, slist);
532 if (svc->flags & matchflags) {
533 func(svc);
534 }
535 }
536}
537
538void action_for_each_trigger(const char *trigger,
539 void (*func)(struct action *act))
540{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700541 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700542 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700543 struct trigger *cur_trigger;
544
Colin Cross6310a822010-04-20 14:29:05 -0700545 list_for_each(node, &action_list) {
546 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700547 list_for_each(node2, &act->triggers) {
548 cur_trigger = node_to_item(node2, struct trigger, nlist);
549 if (!strcmp(cur_trigger->name, trigger)) {
550 func(act);
551 }
Colin Cross6310a822010-04-20 14:29:05 -0700552 }
553 }
554}
555
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700556
Colin Cross6310a822010-04-20 14:29:05 -0700557void queue_property_triggers(const char *name, const char *value)
558{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700559 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700560 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700561 struct trigger *cur_trigger;
562 bool match;
563 int name_length;
564
Colin Cross6310a822010-04-20 14:29:05 -0700565 list_for_each(node, &action_list) {
566 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700567 match = !name;
568 list_for_each(node2, &act->triggers) {
569 cur_trigger = node_to_item(node2, struct trigger, nlist);
570 if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) {
571 const char *test = cur_trigger->name + strlen("property:");
572 if (!match) {
573 name_length = strlen(name);
574 if (!strncmp(name, test, name_length) &&
575 test[name_length] == '=' &&
576 (!strcmp(test + name_length + 1, value) ||
577 !strcmp(test + name_length + 1, "*"))) {
578 match = true;
579 continue;
580 }
581 } else {
582 const char* equals = strchr(test, '=');
583 if (equals) {
584 char prop_name[PROP_NAME_MAX + 1];
585 char value[PROP_VALUE_MAX];
586 int length = equals - test;
587 if (length <= PROP_NAME_MAX) {
588 int ret;
589 memcpy(prop_name, test, length);
590 prop_name[length] = 0;
Colin Cross6310a822010-04-20 14:29:05 -0700591
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700592 /* does the property exist, and match the trigger value? */
593 ret = property_get(prop_name, value);
594 if (ret > 0 && (!strcmp(equals + 1, value) ||
595 !strcmp(equals + 1, "*"))) {
596 continue;
597 }
598 }
599 }
600 }
601 }
602 match = false;
603 break;
604 }
605 if (match) {
606 action_add_queue_tail(act);
Colin Cross6310a822010-04-20 14:29:05 -0700607 }
608 }
609}
610
611void queue_all_property_triggers()
612{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700613 queue_property_triggers(NULL, NULL);
Colin Cross6310a822010-04-20 14:29:05 -0700614}
615
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800616void queue_builtin_action(int (*func)(int nargs, char **args), const char *name)
Colin Cross6310a822010-04-20 14:29:05 -0700617{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800618 action* act = (action*) calloc(1, sizeof(*act));
619 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700620 cur_trigger->name = name;
621 list_init(&act->triggers);
622 list_add_tail(&act->triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700623 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800624 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700625
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800626 command* cmd = (command*) calloc(1, sizeof(*cmd));
Colin Cross6310a822010-04-20 14:29:05 -0700627 cmd->func = func;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800628 cmd->args[0] = const_cast<char*>(name);
Riley Andrews24a3b782014-06-26 13:56:01 -0700629 cmd->nargs = 1;
Colin Cross6310a822010-04-20 14:29:05 -0700630 list_add_tail(&act->commands, &cmd->clist);
631
632 list_add_tail(&action_list, &act->alist);
633 action_add_queue_tail(act);
634}
635
636void action_add_queue_tail(struct action *act)
637{
Colin Crossa5064622013-03-07 13:42:29 -0800638 if (list_empty(&act->qlist)) {
639 list_add_tail(&action_queue, &act->qlist);
640 }
Colin Cross6310a822010-04-20 14:29:05 -0700641}
642
643struct action *action_remove_queue_head(void)
644{
645 if (list_empty(&action_queue)) {
646 return 0;
647 } else {
648 struct listnode *node = list_head(&action_queue);
649 struct action *act = node_to_item(node, struct action, qlist);
650 list_remove(node);
Colin Crossa5064622013-03-07 13:42:29 -0800651 list_init(node);
Colin Cross6310a822010-04-20 14:29:05 -0700652 return act;
653 }
654}
655
656int action_queue_empty()
657{
658 return list_empty(&action_queue);
659}
660
661static void *parse_service(struct parse_state *state, int nargs, char **args)
662{
Colin Cross6310a822010-04-20 14:29:05 -0700663 if (nargs < 3) {
664 parse_error(state, "services must have a name and a program\n");
665 return 0;
666 }
667 if (!valid_name(args[1])) {
668 parse_error(state, "invalid service name '%s'\n", args[1]);
669 return 0;
670 }
671
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800672 service* svc = (service*) service_find_by_name(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700673 if (svc) {
674 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
675 return 0;
676 }
677
678 nargs -= 2;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800679 svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700680 if (!svc) {
681 parse_error(state, "out of memory\n");
682 return 0;
683 }
684 svc->name = args[1];
685 svc->classname = "default";
686 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800687 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Colin Cross6310a822010-04-20 14:29:05 -0700688 svc->args[nargs] = 0;
689 svc->nargs = nargs;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700690 list_init(&svc->onrestart.triggers);
691 cur_trigger->name = "onrestart";
692 list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700693 list_init(&svc->onrestart.commands);
694 list_add_tail(&service_list, &svc->slist);
695 return svc;
696}
697
698static void parse_line_service(struct parse_state *state, int nargs, char **args)
699{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800700 struct service *svc = (service*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700701 struct command *cmd;
702 int i, kw, kw_nargs;
703
704 if (nargs == 0) {
705 return;
706 }
707
708 svc->ioprio_class = IoSchedClass_NONE;
709
710 kw = lookup_keyword(args[0]);
711 switch (kw) {
712 case K_capability:
713 break;
714 case K_class:
715 if (nargs != 2) {
716 parse_error(state, "class option requires a classname\n");
717 } else {
718 svc->classname = args[1];
719 }
720 break;
721 case K_console:
722 svc->flags |= SVC_CONSOLE;
723 break;
724 case K_disabled:
725 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700726 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700727 break;
728 case K_ioprio:
729 if (nargs != 3) {
730 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
731 } else {
732 svc->ioprio_pri = strtoul(args[2], 0, 8);
733
734 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
735 parse_error(state, "priority value must be range 0 - 7\n");
736 break;
737 }
738
739 if (!strcmp(args[1], "rt")) {
740 svc->ioprio_class = IoSchedClass_RT;
741 } else if (!strcmp(args[1], "be")) {
742 svc->ioprio_class = IoSchedClass_BE;
743 } else if (!strcmp(args[1], "idle")) {
744 svc->ioprio_class = IoSchedClass_IDLE;
745 } else {
746 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
747 }
748 }
749 break;
750 case K_group:
751 if (nargs < 2) {
752 parse_error(state, "group option requires a group id\n");
753 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
754 parse_error(state, "group option accepts at most %d supp. groups\n",
755 NR_SVC_SUPP_GIDS);
756 } else {
757 int n;
758 svc->gid = decode_uid(args[1]);
759 for (n = 2; n < nargs; n++) {
760 svc->supp_gids[n-2] = decode_uid(args[n]);
761 }
762 svc->nr_supp_gids = n - 2;
763 }
764 break;
765 case K_keycodes:
766 if (nargs < 2) {
767 parse_error(state, "keycodes option requires atleast one keycode\n");
768 } else {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800769 svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
Colin Cross6310a822010-04-20 14:29:05 -0700770 if (!svc->keycodes) {
771 parse_error(state, "could not allocate keycodes\n");
772 } else {
773 svc->nkeycodes = nargs - 1;
774 for (i = 1; i < nargs; i++) {
775 svc->keycodes[i - 1] = atoi(args[i]);
776 }
777 }
778 }
779 break;
780 case K_oneshot:
781 svc->flags |= SVC_ONESHOT;
782 break;
783 case K_onrestart:
784 nargs--;
785 args++;
786 kw = lookup_keyword(args[0]);
787 if (!kw_is(kw, COMMAND)) {
788 parse_error(state, "invalid command '%s'\n", args[0]);
789 break;
790 }
791 kw_nargs = kw_nargs(kw);
792 if (nargs < kw_nargs) {
793 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
794 kw_nargs > 2 ? "arguments" : "argument");
795 break;
796 }
797
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800798 cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700799 cmd->func = kw_func(kw);
800 cmd->nargs = nargs;
801 memcpy(cmd->args, args, sizeof(char*) * nargs);
802 list_add_tail(&svc->onrestart.commands, &cmd->clist);
803 break;
804 case K_critical:
805 svc->flags |= SVC_CRITICAL;
806 break;
807 case K_setenv: { /* name value */
Gavin.Changc3a46762013-09-28 00:22:11 +0800808 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700809 parse_error(state, "setenv option requires name and value arguments\n");
810 break;
811 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800812 svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
Colin Cross6310a822010-04-20 14:29:05 -0700813 if (!ei) {
814 parse_error(state, "out of memory\n");
815 break;
816 }
817 ei->name = args[1];
818 ei->value = args[2];
819 ei->next = svc->envvars;
820 svc->envvars = ei;
821 break;
822 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400823 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700824 if (nargs < 4) {
825 parse_error(state, "socket option requires name, type, perm arguments\n");
826 break;
827 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400828 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
829 && strcmp(args[2],"seqpacket")) {
830 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700831 break;
832 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800833 socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
Colin Cross6310a822010-04-20 14:29:05 -0700834 if (!si) {
835 parse_error(state, "out of memory\n");
836 break;
837 }
838 si->name = args[1];
839 si->type = args[2];
840 si->perm = strtoul(args[3], 0, 8);
841 if (nargs > 4)
842 si->uid = decode_uid(args[4]);
843 if (nargs > 5)
844 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400845 if (nargs > 6)
846 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700847 si->next = svc->sockets;
848 svc->sockets = si;
849 break;
850 }
851 case K_user:
852 if (nargs != 2) {
853 parse_error(state, "user option requires a user id\n");
854 } else {
855 svc->uid = decode_uid(args[1]);
856 }
857 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500858 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500859 if (nargs != 2) {
860 parse_error(state, "seclabel option requires a label string\n");
861 } else {
862 svc->seclabel = args[1];
863 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500864 break;
865
Colin Cross6310a822010-04-20 14:29:05 -0700866 default:
867 parse_error(state, "invalid option '%s'\n", args[0]);
868 }
869}
870
871static void *parse_action(struct parse_state *state, int nargs, char **args)
872{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700873 struct trigger *cur_trigger;
874 int i;
Colin Cross6310a822010-04-20 14:29:05 -0700875 if (nargs < 2) {
876 parse_error(state, "actions must have a trigger\n");
877 return 0;
878 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700879
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800880 action* act = (action*) calloc(1, sizeof(*act));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700881 list_init(&act->triggers);
882
883 for (i = 1; i < nargs; i++) {
884 if (!(i % 2)) {
885 if (strcmp(args[i], "&&")) {
886 parse_error(state, "& is the only symbol allowed to concatenate actions\n");
887 return 0;
888 } else
889 continue;
890 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800891 cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700892 cur_trigger->name = args[i];
893 list_add_tail(&act->triggers, &cur_trigger->nlist);
894 }
895
Colin Cross6310a822010-04-20 14:29:05 -0700896 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800897 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700898 list_add_tail(&action_list, &act->alist);
899 /* XXX add to hash */
900 return act;
901}
902
903static void parse_line_action(struct parse_state* state, int nargs, char **args)
904{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800905 struct action *act = (action*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700906 int kw, n;
907
908 if (nargs == 0) {
909 return;
910 }
911
912 kw = lookup_keyword(args[0]);
913 if (!kw_is(kw, COMMAND)) {
914 parse_error(state, "invalid command '%s'\n", args[0]);
915 return;
916 }
917
918 n = kw_nargs(kw);
919 if (nargs < n) {
920 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
921 n > 2 ? "arguments" : "argument");
922 return;
923 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800924 command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700925 cmd->func = kw_func(kw);
Riley Andrews24a3b782014-06-26 13:56:01 -0700926 cmd->line = state->line;
927 cmd->filename = state->filename;
Colin Cross6310a822010-04-20 14:29:05 -0700928 cmd->nargs = nargs;
929 memcpy(cmd->args, args, sizeof(char*) * nargs);
930 list_add_tail(&act->commands, &cmd->clist);
931}