blob: 61a5e0a741279fd604dba9ed12fe4e4f4f3891bd [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;
207 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) {
440 INFO("Parsing %s...", path);
441 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
663static void *parse_service(struct parse_state *state, int nargs, char **args)
664{
Colin Cross6310a822010-04-20 14:29:05 -0700665 if (nargs < 3) {
666 parse_error(state, "services must have a name and a program\n");
667 return 0;
668 }
669 if (!valid_name(args[1])) {
670 parse_error(state, "invalid service name '%s'\n", args[1]);
671 return 0;
672 }
673
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800674 service* svc = (service*) service_find_by_name(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700675 if (svc) {
676 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
677 return 0;
678 }
679
680 nargs -= 2;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800681 svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700682 if (!svc) {
683 parse_error(state, "out of memory\n");
684 return 0;
685 }
686 svc->name = args[1];
687 svc->classname = "default";
688 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800689 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Colin Cross6310a822010-04-20 14:29:05 -0700690 svc->args[nargs] = 0;
691 svc->nargs = nargs;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700692 list_init(&svc->onrestart.triggers);
693 cur_trigger->name = "onrestart";
694 list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700695 list_init(&svc->onrestart.commands);
696 list_add_tail(&service_list, &svc->slist);
697 return svc;
698}
699
700static void parse_line_service(struct parse_state *state, int nargs, char **args)
701{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800702 struct service *svc = (service*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700703 struct command *cmd;
704 int i, kw, kw_nargs;
705
706 if (nargs == 0) {
707 return;
708 }
709
710 svc->ioprio_class = IoSchedClass_NONE;
711
712 kw = lookup_keyword(args[0]);
713 switch (kw) {
714 case K_capability:
715 break;
716 case K_class:
717 if (nargs != 2) {
718 parse_error(state, "class option requires a classname\n");
719 } else {
720 svc->classname = args[1];
721 }
722 break;
723 case K_console:
724 svc->flags |= SVC_CONSOLE;
725 break;
726 case K_disabled:
727 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700728 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700729 break;
730 case K_ioprio:
731 if (nargs != 3) {
732 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
733 } else {
734 svc->ioprio_pri = strtoul(args[2], 0, 8);
735
736 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
737 parse_error(state, "priority value must be range 0 - 7\n");
738 break;
739 }
740
741 if (!strcmp(args[1], "rt")) {
742 svc->ioprio_class = IoSchedClass_RT;
743 } else if (!strcmp(args[1], "be")) {
744 svc->ioprio_class = IoSchedClass_BE;
745 } else if (!strcmp(args[1], "idle")) {
746 svc->ioprio_class = IoSchedClass_IDLE;
747 } else {
748 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
749 }
750 }
751 break;
752 case K_group:
753 if (nargs < 2) {
754 parse_error(state, "group option requires a group id\n");
755 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
756 parse_error(state, "group option accepts at most %d supp. groups\n",
757 NR_SVC_SUPP_GIDS);
758 } else {
759 int n;
760 svc->gid = decode_uid(args[1]);
761 for (n = 2; n < nargs; n++) {
762 svc->supp_gids[n-2] = decode_uid(args[n]);
763 }
764 svc->nr_supp_gids = n - 2;
765 }
766 break;
767 case K_keycodes:
768 if (nargs < 2) {
769 parse_error(state, "keycodes option requires atleast one keycode\n");
770 } else {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800771 svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
Colin Cross6310a822010-04-20 14:29:05 -0700772 if (!svc->keycodes) {
773 parse_error(state, "could not allocate keycodes\n");
774 } else {
775 svc->nkeycodes = nargs - 1;
776 for (i = 1; i < nargs; i++) {
777 svc->keycodes[i - 1] = atoi(args[i]);
778 }
779 }
780 }
781 break;
782 case K_oneshot:
783 svc->flags |= SVC_ONESHOT;
784 break;
785 case K_onrestart:
786 nargs--;
787 args++;
788 kw = lookup_keyword(args[0]);
789 if (!kw_is(kw, COMMAND)) {
790 parse_error(state, "invalid command '%s'\n", args[0]);
791 break;
792 }
793 kw_nargs = kw_nargs(kw);
794 if (nargs < kw_nargs) {
795 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
796 kw_nargs > 2 ? "arguments" : "argument");
797 break;
798 }
799
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800800 cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700801 cmd->func = kw_func(kw);
802 cmd->nargs = nargs;
803 memcpy(cmd->args, args, sizeof(char*) * nargs);
804 list_add_tail(&svc->onrestart.commands, &cmd->clist);
805 break;
806 case K_critical:
807 svc->flags |= SVC_CRITICAL;
808 break;
809 case K_setenv: { /* name value */
Gavin.Changc3a46762013-09-28 00:22:11 +0800810 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700811 parse_error(state, "setenv option requires name and value arguments\n");
812 break;
813 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800814 svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
Colin Cross6310a822010-04-20 14:29:05 -0700815 if (!ei) {
816 parse_error(state, "out of memory\n");
817 break;
818 }
819 ei->name = args[1];
820 ei->value = args[2];
821 ei->next = svc->envvars;
822 svc->envvars = ei;
823 break;
824 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400825 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700826 if (nargs < 4) {
827 parse_error(state, "socket option requires name, type, perm arguments\n");
828 break;
829 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400830 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
831 && strcmp(args[2],"seqpacket")) {
832 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700833 break;
834 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800835 socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
Colin Cross6310a822010-04-20 14:29:05 -0700836 if (!si) {
837 parse_error(state, "out of memory\n");
838 break;
839 }
840 si->name = args[1];
841 si->type = args[2];
842 si->perm = strtoul(args[3], 0, 8);
843 if (nargs > 4)
844 si->uid = decode_uid(args[4]);
845 if (nargs > 5)
846 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400847 if (nargs > 6)
848 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700849 si->next = svc->sockets;
850 svc->sockets = si;
851 break;
852 }
853 case K_user:
854 if (nargs != 2) {
855 parse_error(state, "user option requires a user id\n");
856 } else {
857 svc->uid = decode_uid(args[1]);
858 }
859 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500860 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500861 if (nargs != 2) {
862 parse_error(state, "seclabel option requires a label string\n");
863 } else {
864 svc->seclabel = args[1];
865 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500866 break;
867
Colin Cross6310a822010-04-20 14:29:05 -0700868 default:
869 parse_error(state, "invalid option '%s'\n", args[0]);
870 }
871}
872
873static void *parse_action(struct parse_state *state, int nargs, char **args)
874{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700875 struct trigger *cur_trigger;
876 int i;
Colin Cross6310a822010-04-20 14:29:05 -0700877 if (nargs < 2) {
878 parse_error(state, "actions must have a trigger\n");
879 return 0;
880 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700881
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800882 action* act = (action*) calloc(1, sizeof(*act));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700883 list_init(&act->triggers);
884
885 for (i = 1; i < nargs; i++) {
886 if (!(i % 2)) {
887 if (strcmp(args[i], "&&")) {
888 parse_error(state, "& is the only symbol allowed to concatenate actions\n");
889 return 0;
890 } else
891 continue;
892 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800893 cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700894 cur_trigger->name = args[i];
895 list_add_tail(&act->triggers, &cur_trigger->nlist);
896 }
897
Colin Cross6310a822010-04-20 14:29:05 -0700898 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800899 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700900 list_add_tail(&action_list, &act->alist);
901 /* XXX add to hash */
902 return act;
903}
904
905static void parse_line_action(struct parse_state* state, int nargs, char **args)
906{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800907 struct action *act = (action*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700908 int kw, n;
909
910 if (nargs == 0) {
911 return;
912 }
913
914 kw = lookup_keyword(args[0]);
915 if (!kw_is(kw, COMMAND)) {
916 parse_error(state, "invalid command '%s'\n", args[0]);
917 return;
918 }
919
920 n = kw_nargs(kw);
921 if (nargs < n) {
922 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
923 n > 2 ? "arguments" : "argument");
924 return;
925 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800926 command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700927 cmd->func = kw_func(kw);
Riley Andrews24a3b782014-06-26 13:56:01 -0700928 cmd->line = state->line;
929 cmd->filename = state->filename;
Colin Cross6310a822010-04-20 14:29:05 -0700930 cmd->nargs = nargs;
931 memcpy(cmd->args, args, sizeof(char*) * nargs);
932 list_add_tail(&act->commands, &cmd->clist);
933}