blob: 4e18e20782eea290807dc9f4033fc9215153a555 [file] [log] [blame]
Colin Cross6310a822010-04-20 14:29:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughesda40c002015-03-27 23:20:44 -070017#include <ctype.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080018#include <errno.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070019#include <fcntl.h>
20#include <inttypes.h>
21#include <stdarg.h>
22#include <stddef.h>
Colin Cross6310a822010-04-20 14:29:05 -070023#include <stdio.h>
24#include <stdlib.h>
Colin Cross6310a822010-04-20 14:29:05 -070025#include <string.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070026#include <unistd.h>
Colin Cross6310a822010-04-20 14:29:05 -070027
28#include "init.h"
29#include "parser.h"
30#include "init_parser.h"
31#include "log.h"
Colin Cross6310a822010-04-20 14:29:05 -070032#include "property_service.h"
33#include "util.h"
34
35#include <cutils/iosched_policy.h>
Dima Zavinda04c522011-09-01 17:09:44 -070036#include <cutils/list.h>
Colin Cross6310a822010-04-20 14:29:05 -070037
Colin Cross6310a822010-04-20 14:29:05 -070038static list_declare(service_list);
39static list_declare(action_list);
40static list_declare(action_queue);
41
Dima Zavin78a1b1f2011-12-20 12:53:48 -080042struct import {
43 struct listnode list;
44 const char *filename;
45};
46
Colin Cross6310a822010-04-20 14:29:05 -070047static void *parse_service(struct parse_state *state, int nargs, char **args);
48static void parse_line_service(struct parse_state *state, int nargs, char **args);
49
50static void *parse_action(struct parse_state *state, int nargs, char **args);
51static void parse_line_action(struct parse_state *state, int nargs, char **args);
52
53#define SECTION 0x01
54#define COMMAND 0x02
55#define OPTION 0x04
56
57#include "keywords.h"
58
59#define KEYWORD(symbol, flags, nargs, func) \
60 [ K_##symbol ] = { #symbol, func, nargs + 1, flags, },
61
Greg Hackmannd68db712013-11-18 09:44:20 -080062static struct {
Colin Cross6310a822010-04-20 14:29:05 -070063 const char *name;
64 int (*func)(int nargs, char **args);
65 unsigned char nargs;
66 unsigned char flags;
67} keyword_info[KEYWORD_COUNT] = {
68 [ K_UNKNOWN ] = { "unknown", 0, 0, 0 },
69#include "keywords.h"
70};
71#undef KEYWORD
72
73#define kw_is(kw, type) (keyword_info[kw].flags & (type))
74#define kw_name(kw) (keyword_info[kw].name)
75#define kw_func(kw) (keyword_info[kw].func)
76#define kw_nargs(kw) (keyword_info[kw].nargs)
77
Elliott Hughesc0e919c2015-02-04 14:46:36 -080078void dump_parser_state() {
79 if (false) {
80 struct listnode* node;
81 list_for_each(node, &service_list) {
82 service* svc = node_to_item(node, struct service, slist);
83 INFO("service %s\n", svc->name);
84 INFO(" class '%s'\n", svc->classname);
85 INFO(" exec");
86 for (int n = 0; n < svc->nargs; n++) {
87 INFO(" '%s'", svc->args[n]);
88 }
89 INFO("\n");
90 for (socketinfo* si = svc->sockets; si; si = si->next) {
91 INFO(" socket %s %s 0%o\n", si->name, si->type, si->perm);
92 }
93 }
94
95 list_for_each(node, &action_list) {
96 action* act = node_to_item(node, struct action, alist);
97 INFO("on ");
98 char name_str[256] = "";
99 build_triggers_string(name_str, sizeof(name_str), act);
100 INFO("%s", name_str);
101 INFO("\n");
102
103 struct listnode* node2;
104 list_for_each(node2, &act->commands) {
105 command* cmd = node_to_item(node2, struct command, clist);
106 INFO(" %p", cmd->func);
107 for (int n = 0; n < cmd->nargs; n++) {
108 INFO(" %s", cmd->args[n]);
109 }
110 INFO("\n");
111 }
112 INFO("\n");
113 }
114 }
115}
116
Greg Hackmannd68db712013-11-18 09:44:20 -0800117static int lookup_keyword(const char *s)
Colin Cross6310a822010-04-20 14:29:05 -0700118{
119 switch (*s++) {
Yongqin Liua197ff12014-12-05 13:45:02 +0800120 case 'b':
121 if (!strcmp(s, "ootchart_init")) return K_bootchart_init;
Mark Salyzyn7a3d66c2015-03-24 07:29:22 -0700122 break;
Colin Cross6310a822010-04-20 14:29:05 -0700123 case 'c':
Elliott Hughesf682b472015-02-06 12:19:48 -0800124 if (!strcmp(s, "opy")) return K_copy;
Colin Cross6310a822010-04-20 14:29:05 -0700125 if (!strcmp(s, "apability")) return K_capability;
Colin Cross6310a822010-04-20 14:29:05 -0700126 if (!strcmp(s, "lass")) return K_class;
127 if (!strcmp(s, "lass_start")) return K_class_start;
128 if (!strcmp(s, "lass_stop")) return K_class_stop;
Ken Sumrall752923c2010-12-03 16:33:31 -0800129 if (!strcmp(s, "lass_reset")) return K_class_reset;
Colin Cross6310a822010-04-20 14:29:05 -0700130 if (!strcmp(s, "onsole")) return K_console;
131 if (!strcmp(s, "hown")) return K_chown;
132 if (!strcmp(s, "hmod")) return K_chmod;
133 if (!strcmp(s, "ritical")) return K_critical;
134 break;
135 case 'd':
136 if (!strcmp(s, "isabled")) return K_disabled;
137 if (!strcmp(s, "omainname")) return K_domainname;
138 break;
139 case 'e':
JP Abgrall3beec7e2014-05-02 21:14:29 -0700140 if (!strcmp(s, "nable")) return K_enable;
Colin Cross6310a822010-04-20 14:29:05 -0700141 if (!strcmp(s, "xec")) return K_exec;
San Mehat429721c2014-09-23 07:48:47 -0700142 if (!strcmp(s, "xeconce")) return K_execonce;
Colin Cross6310a822010-04-20 14:29:05 -0700143 if (!strcmp(s, "xport")) return K_export;
144 break;
145 case 'g':
146 if (!strcmp(s, "roup")) return K_group;
147 break;
148 case 'h':
149 if (!strcmp(s, "ostname")) return K_hostname;
150 break;
151 case 'i':
152 if (!strcmp(s, "oprio")) return K_ioprio;
153 if (!strcmp(s, "fup")) return K_ifup;
154 if (!strcmp(s, "nsmod")) return K_insmod;
155 if (!strcmp(s, "mport")) return K_import;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000156 if (!strcmp(s, "nstallkey")) return K_installkey;
Colin Cross6310a822010-04-20 14:29:05 -0700157 break;
158 case 'k':
159 if (!strcmp(s, "eycodes")) return K_keycodes;
160 break;
161 case 'l':
162 if (!strcmp(s, "oglevel")) return K_loglevel;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800163 if (!strcmp(s, "oad_persist_props")) return K_load_persist_props;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700164 if (!strcmp(s, "oad_all_props")) return K_load_all_props;
Colin Cross6310a822010-04-20 14:29:05 -0700165 break;
166 case 'm':
167 if (!strcmp(s, "kdir")) return K_mkdir;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700168 if (!strcmp(s, "ount_all")) return K_mount_all;
Colin Cross6310a822010-04-20 14:29:05 -0700169 if (!strcmp(s, "ount")) return K_mount;
170 break;
171 case 'o':
172 if (!strcmp(s, "n")) return K_on;
173 if (!strcmp(s, "neshot")) return K_oneshot;
174 if (!strcmp(s, "nrestart")) return K_onrestart;
175 break;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700176 case 'p':
177 if (!strcmp(s, "owerctl")) return K_powerctl;
Elliott Hughesf682b472015-02-06 12:19:48 -0800178 break;
Colin Cross6310a822010-04-20 14:29:05 -0700179 case 'r':
180 if (!strcmp(s, "estart")) return K_restart;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500181 if (!strcmp(s, "estorecon")) return K_restorecon;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400182 if (!strcmp(s, "estorecon_recursive")) return K_restorecon_recursive;
Ken Sumrall203bad52011-01-18 17:37:41 -0800183 if (!strcmp(s, "mdir")) return K_rmdir;
184 if (!strcmp(s, "m")) return K_rm;
Colin Cross6310a822010-04-20 14:29:05 -0700185 break;
186 case 's':
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500187 if (!strcmp(s, "eclabel")) return K_seclabel;
Colin Cross6310a822010-04-20 14:29:05 -0700188 if (!strcmp(s, "ervice")) return K_service;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500189 if (!strcmp(s, "etcon")) return K_setcon;
Colin Cross6310a822010-04-20 14:29:05 -0700190 if (!strcmp(s, "etenv")) return K_setenv;
Colin Cross6310a822010-04-20 14:29:05 -0700191 if (!strcmp(s, "etprop")) return K_setprop;
192 if (!strcmp(s, "etrlimit")) return K_setrlimit;
193 if (!strcmp(s, "ocket")) return K_socket;
194 if (!strcmp(s, "tart")) return K_start;
195 if (!strcmp(s, "top")) return K_stop;
Ken Sumralla76baaa2013-07-09 18:42:09 -0700196 if (!strcmp(s, "wapon_all")) return K_swapon_all;
Colin Cross6310a822010-04-20 14:29:05 -0700197 if (!strcmp(s, "ymlink")) return K_symlink;
198 if (!strcmp(s, "ysclktz")) return K_sysclktz;
199 break;
200 case 't':
201 if (!strcmp(s, "rigger")) return K_trigger;
202 break;
203 case 'u':
204 if (!strcmp(s, "ser")) return K_user;
205 break;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000206 case 'v':
207 if (!strcmp(s, "erity_load_state")) return K_verity_load_state;
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000208 if (!strcmp(s, "erity_update_state")) return K_verity_update_state;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000209 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);
Elliott Hughesda40c002015-03-27 23:20:44 -0700356 INFO("Added '%s' to import list\n", import->filename);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800357}
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) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800443 INFO("Parsing %s...\n", path);
Elliott Hughesda40c002015-03-27 23:20:44 -0700444 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800445 std::string data;
446 if (!read_file(path, &data)) {
447 return -1;
448 }
Colin Cross6310a822010-04-20 14:29:05 -0700449
Elliott Hughesf682b472015-02-06 12:19:48 -0800450 parse_config(path, data);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800451 dump_parser_state();
Elliott Hughesda40c002015-03-27 23:20:44 -0700452
453 NOTICE("(Parsing %s took %.2fs.)\n", path, t.duration());
Colin Cross6310a822010-04-20 14:29:05 -0700454 return 0;
455}
456
457static int valid_name(const char *name)
458{
459 if (strlen(name) > 16) {
460 return 0;
461 }
462 while (*name) {
463 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
464 return 0;
465 }
466 name++;
467 }
468 return 1;
469}
470
471struct service *service_find_by_name(const char *name)
472{
473 struct listnode *node;
474 struct service *svc;
475 list_for_each(node, &service_list) {
476 svc = node_to_item(node, struct service, slist);
477 if (!strcmp(svc->name, name)) {
478 return svc;
479 }
480 }
481 return 0;
482}
483
484struct service *service_find_by_pid(pid_t pid)
485{
486 struct listnode *node;
487 struct service *svc;
488 list_for_each(node, &service_list) {
489 svc = node_to_item(node, struct service, slist);
490 if (svc->pid == pid) {
491 return svc;
492 }
493 }
494 return 0;
495}
496
497struct service *service_find_by_keychord(int keychord_id)
498{
499 struct listnode *node;
500 struct service *svc;
501 list_for_each(node, &service_list) {
502 svc = node_to_item(node, struct service, slist);
503 if (svc->keychord_id == keychord_id) {
504 return svc;
505 }
506 }
507 return 0;
508}
509
510void service_for_each(void (*func)(struct service *svc))
511{
512 struct listnode *node;
513 struct service *svc;
514 list_for_each(node, &service_list) {
515 svc = node_to_item(node, struct service, slist);
516 func(svc);
517 }
518}
519
520void service_for_each_class(const char *classname,
521 void (*func)(struct service *svc))
522{
523 struct listnode *node;
524 struct service *svc;
525 list_for_each(node, &service_list) {
526 svc = node_to_item(node, struct service, slist);
527 if (!strcmp(svc->classname, classname)) {
528 func(svc);
529 }
530 }
531}
532
533void service_for_each_flags(unsigned matchflags,
534 void (*func)(struct service *svc))
535{
536 struct listnode *node;
537 struct service *svc;
538 list_for_each(node, &service_list) {
539 svc = node_to_item(node, struct service, slist);
540 if (svc->flags & matchflags) {
541 func(svc);
542 }
543 }
544}
545
546void action_for_each_trigger(const char *trigger,
547 void (*func)(struct action *act))
548{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700549 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700550 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700551 struct trigger *cur_trigger;
552
Colin Cross6310a822010-04-20 14:29:05 -0700553 list_for_each(node, &action_list) {
554 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700555 list_for_each(node2, &act->triggers) {
556 cur_trigger = node_to_item(node2, struct trigger, nlist);
557 if (!strcmp(cur_trigger->name, trigger)) {
558 func(act);
559 }
Colin Cross6310a822010-04-20 14:29:05 -0700560 }
561 }
562}
563
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700564
Colin Cross6310a822010-04-20 14:29:05 -0700565void queue_property_triggers(const char *name, const char *value)
566{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700567 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700568 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700569 struct trigger *cur_trigger;
570 bool match;
571 int name_length;
572
Colin Cross6310a822010-04-20 14:29:05 -0700573 list_for_each(node, &action_list) {
574 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700575 match = !name;
576 list_for_each(node2, &act->triggers) {
577 cur_trigger = node_to_item(node2, struct trigger, nlist);
578 if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) {
579 const char *test = cur_trigger->name + strlen("property:");
580 if (!match) {
581 name_length = strlen(name);
582 if (!strncmp(name, test, name_length) &&
583 test[name_length] == '=' &&
584 (!strcmp(test + name_length + 1, value) ||
585 !strcmp(test + name_length + 1, "*"))) {
586 match = true;
587 continue;
588 }
589 } else {
590 const char* equals = strchr(test, '=');
591 if (equals) {
592 char prop_name[PROP_NAME_MAX + 1];
593 char value[PROP_VALUE_MAX];
594 int length = equals - test;
595 if (length <= PROP_NAME_MAX) {
596 int ret;
597 memcpy(prop_name, test, length);
598 prop_name[length] = 0;
Colin Cross6310a822010-04-20 14:29:05 -0700599
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700600 /* does the property exist, and match the trigger value? */
601 ret = property_get(prop_name, value);
602 if (ret > 0 && (!strcmp(equals + 1, value) ||
603 !strcmp(equals + 1, "*"))) {
604 continue;
605 }
606 }
607 }
608 }
609 }
610 match = false;
611 break;
612 }
613 if (match) {
614 action_add_queue_tail(act);
Colin Cross6310a822010-04-20 14:29:05 -0700615 }
616 }
617}
618
619void queue_all_property_triggers()
620{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700621 queue_property_triggers(NULL, NULL);
Colin Cross6310a822010-04-20 14:29:05 -0700622}
623
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800624void queue_builtin_action(int (*func)(int nargs, char **args), const char *name)
Colin Cross6310a822010-04-20 14:29:05 -0700625{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800626 action* act = (action*) calloc(1, sizeof(*act));
627 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700628 cur_trigger->name = name;
629 list_init(&act->triggers);
630 list_add_tail(&act->triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700631 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800632 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700633
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800634 command* cmd = (command*) calloc(1, sizeof(*cmd));
Colin Cross6310a822010-04-20 14:29:05 -0700635 cmd->func = func;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800636 cmd->args[0] = const_cast<char*>(name);
Riley Andrews24a3b782014-06-26 13:56:01 -0700637 cmd->nargs = 1;
Colin Cross6310a822010-04-20 14:29:05 -0700638 list_add_tail(&act->commands, &cmd->clist);
639
640 list_add_tail(&action_list, &act->alist);
641 action_add_queue_tail(act);
642}
643
644void action_add_queue_tail(struct action *act)
645{
Colin Crossa5064622013-03-07 13:42:29 -0800646 if (list_empty(&act->qlist)) {
647 list_add_tail(&action_queue, &act->qlist);
648 }
Colin Cross6310a822010-04-20 14:29:05 -0700649}
650
651struct action *action_remove_queue_head(void)
652{
653 if (list_empty(&action_queue)) {
654 return 0;
655 } else {
656 struct listnode *node = list_head(&action_queue);
657 struct action *act = node_to_item(node, struct action, qlist);
658 list_remove(node);
Colin Crossa5064622013-03-07 13:42:29 -0800659 list_init(node);
Colin Cross6310a822010-04-20 14:29:05 -0700660 return act;
661 }
662}
663
664int action_queue_empty()
665{
666 return list_empty(&action_queue);
667}
668
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800669service* make_exec_oneshot_service(int nargs, char** args) {
670 // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS...
671 int command_arg = 1;
672 for (int i = 1; i < nargs; ++i) {
673 if (strcmp(args[i], "--") == 0) {
674 command_arg = i + 1;
675 break;
676 }
677 }
678 if (command_arg > 4 + NR_SVC_SUPP_GIDS) {
679 ERROR("exec called with too many supplementary group ids\n");
680 return NULL;
681 }
682
683 int argc = nargs - command_arg;
684 char** argv = (args + command_arg);
685 if (argc < 1) {
686 ERROR("exec called without command\n");
687 return NULL;
688 }
689
690 service* svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * argc);
691 if (svc == NULL) {
692 ERROR("Couldn't allocate service for exec of '%s': %s", argv[0], strerror(errno));
693 return NULL;
694 }
695
696 if (command_arg > 2) {
697 svc->seclabel = args[1];
698 }
699 if (command_arg > 3) {
700 svc->uid = decode_uid(args[2]);
701 }
702 if (command_arg > 4) {
703 svc->gid = decode_uid(args[3]);
704 svc->nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */;
705 for (size_t i = 0; i < svc->nr_supp_gids; ++i) {
706 svc->supp_gids[i] = decode_uid(args[4 + i]);
707 }
708 }
709
710 static int exec_count; // Every service needs a unique name.
711 char* name = NULL;
712 asprintf(&name, "exec %d (%s)", exec_count++, argv[0]);
713 if (name == NULL) {
714 ERROR("Couldn't allocate name for exec service '%s'\n", argv[0]);
715 free(svc);
716 return NULL;
717 }
718 svc->name = name;
719 svc->classname = "default";
720 svc->flags = SVC_EXEC | SVC_ONESHOT;
721 svc->nargs = argc;
722 memcpy(svc->args, argv, sizeof(char*) * svc->nargs);
723 svc->args[argc] = NULL;
724 list_add_tail(&service_list, &svc->slist);
725 return svc;
726}
727
Colin Cross6310a822010-04-20 14:29:05 -0700728static void *parse_service(struct parse_state *state, int nargs, char **args)
729{
Colin Cross6310a822010-04-20 14:29:05 -0700730 if (nargs < 3) {
731 parse_error(state, "services must have a name and a program\n");
732 return 0;
733 }
734 if (!valid_name(args[1])) {
735 parse_error(state, "invalid service name '%s'\n", args[1]);
736 return 0;
737 }
738
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800739 service* svc = (service*) service_find_by_name(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700740 if (svc) {
741 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
742 return 0;
743 }
744
745 nargs -= 2;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800746 svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700747 if (!svc) {
748 parse_error(state, "out of memory\n");
749 return 0;
750 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800751 svc->name = strdup(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700752 svc->classname = "default";
753 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800754 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Colin Cross6310a822010-04-20 14:29:05 -0700755 svc->args[nargs] = 0;
756 svc->nargs = nargs;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700757 list_init(&svc->onrestart.triggers);
758 cur_trigger->name = "onrestart";
759 list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700760 list_init(&svc->onrestart.commands);
761 list_add_tail(&service_list, &svc->slist);
762 return svc;
763}
764
765static void parse_line_service(struct parse_state *state, int nargs, char **args)
766{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800767 struct service *svc = (service*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700768 struct command *cmd;
769 int i, kw, kw_nargs;
770
771 if (nargs == 0) {
772 return;
773 }
774
775 svc->ioprio_class = IoSchedClass_NONE;
776
777 kw = lookup_keyword(args[0]);
778 switch (kw) {
779 case K_capability:
780 break;
781 case K_class:
782 if (nargs != 2) {
783 parse_error(state, "class option requires a classname\n");
784 } else {
785 svc->classname = args[1];
786 }
787 break;
788 case K_console:
789 svc->flags |= SVC_CONSOLE;
790 break;
791 case K_disabled:
792 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700793 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700794 break;
795 case K_ioprio:
796 if (nargs != 3) {
797 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
798 } else {
799 svc->ioprio_pri = strtoul(args[2], 0, 8);
800
801 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
802 parse_error(state, "priority value must be range 0 - 7\n");
803 break;
804 }
805
806 if (!strcmp(args[1], "rt")) {
807 svc->ioprio_class = IoSchedClass_RT;
808 } else if (!strcmp(args[1], "be")) {
809 svc->ioprio_class = IoSchedClass_BE;
810 } else if (!strcmp(args[1], "idle")) {
811 svc->ioprio_class = IoSchedClass_IDLE;
812 } else {
813 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
814 }
815 }
816 break;
817 case K_group:
818 if (nargs < 2) {
819 parse_error(state, "group option requires a group id\n");
820 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
821 parse_error(state, "group option accepts at most %d supp. groups\n",
822 NR_SVC_SUPP_GIDS);
823 } else {
824 int n;
825 svc->gid = decode_uid(args[1]);
826 for (n = 2; n < nargs; n++) {
827 svc->supp_gids[n-2] = decode_uid(args[n]);
828 }
829 svc->nr_supp_gids = n - 2;
830 }
831 break;
832 case K_keycodes:
833 if (nargs < 2) {
834 parse_error(state, "keycodes option requires atleast one keycode\n");
835 } else {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800836 svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
Colin Cross6310a822010-04-20 14:29:05 -0700837 if (!svc->keycodes) {
838 parse_error(state, "could not allocate keycodes\n");
839 } else {
840 svc->nkeycodes = nargs - 1;
841 for (i = 1; i < nargs; i++) {
842 svc->keycodes[i - 1] = atoi(args[i]);
843 }
844 }
845 }
846 break;
847 case K_oneshot:
848 svc->flags |= SVC_ONESHOT;
849 break;
850 case K_onrestart:
851 nargs--;
852 args++;
853 kw = lookup_keyword(args[0]);
854 if (!kw_is(kw, COMMAND)) {
855 parse_error(state, "invalid command '%s'\n", args[0]);
856 break;
857 }
858 kw_nargs = kw_nargs(kw);
859 if (nargs < kw_nargs) {
860 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
861 kw_nargs > 2 ? "arguments" : "argument");
862 break;
863 }
864
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800865 cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700866 cmd->func = kw_func(kw);
867 cmd->nargs = nargs;
868 memcpy(cmd->args, args, sizeof(char*) * nargs);
869 list_add_tail(&svc->onrestart.commands, &cmd->clist);
870 break;
871 case K_critical:
872 svc->flags |= SVC_CRITICAL;
873 break;
874 case K_setenv: { /* name value */
Gavin.Changc3a46762013-09-28 00:22:11 +0800875 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700876 parse_error(state, "setenv option requires name and value arguments\n");
877 break;
878 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800879 svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
Colin Cross6310a822010-04-20 14:29:05 -0700880 if (!ei) {
881 parse_error(state, "out of memory\n");
882 break;
883 }
884 ei->name = args[1];
885 ei->value = args[2];
886 ei->next = svc->envvars;
887 svc->envvars = ei;
888 break;
889 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400890 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700891 if (nargs < 4) {
892 parse_error(state, "socket option requires name, type, perm arguments\n");
893 break;
894 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400895 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
896 && strcmp(args[2],"seqpacket")) {
897 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700898 break;
899 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800900 socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
Colin Cross6310a822010-04-20 14:29:05 -0700901 if (!si) {
902 parse_error(state, "out of memory\n");
903 break;
904 }
905 si->name = args[1];
906 si->type = args[2];
907 si->perm = strtoul(args[3], 0, 8);
908 if (nargs > 4)
909 si->uid = decode_uid(args[4]);
910 if (nargs > 5)
911 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400912 if (nargs > 6)
913 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700914 si->next = svc->sockets;
915 svc->sockets = si;
916 break;
917 }
918 case K_user:
919 if (nargs != 2) {
920 parse_error(state, "user option requires a user id\n");
921 } else {
922 svc->uid = decode_uid(args[1]);
923 }
924 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500925 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500926 if (nargs != 2) {
927 parse_error(state, "seclabel option requires a label string\n");
928 } else {
929 svc->seclabel = args[1];
930 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500931 break;
932
Colin Cross6310a822010-04-20 14:29:05 -0700933 default:
934 parse_error(state, "invalid option '%s'\n", args[0]);
935 }
936}
937
938static void *parse_action(struct parse_state *state, int nargs, char **args)
939{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700940 struct trigger *cur_trigger;
941 int i;
Colin Cross6310a822010-04-20 14:29:05 -0700942 if (nargs < 2) {
943 parse_error(state, "actions must have a trigger\n");
944 return 0;
945 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700946
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800947 action* act = (action*) calloc(1, sizeof(*act));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700948 list_init(&act->triggers);
949
950 for (i = 1; i < nargs; i++) {
951 if (!(i % 2)) {
952 if (strcmp(args[i], "&&")) {
953 parse_error(state, "& is the only symbol allowed to concatenate actions\n");
954 return 0;
955 } else
956 continue;
957 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800958 cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700959 cur_trigger->name = args[i];
960 list_add_tail(&act->triggers, &cur_trigger->nlist);
961 }
962
Colin Cross6310a822010-04-20 14:29:05 -0700963 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800964 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700965 list_add_tail(&action_list, &act->alist);
966 /* XXX add to hash */
967 return act;
968}
969
970static void parse_line_action(struct parse_state* state, int nargs, char **args)
971{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800972 struct action *act = (action*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700973 int kw, n;
974
975 if (nargs == 0) {
976 return;
977 }
978
979 kw = lookup_keyword(args[0]);
980 if (!kw_is(kw, COMMAND)) {
981 parse_error(state, "invalid command '%s'\n", args[0]);
982 return;
983 }
984
985 n = kw_nargs(kw);
986 if (nargs < n) {
987 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
988 n > 2 ? "arguments" : "argument");
989 return;
990 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800991 command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700992 cmd->func = kw_func(kw);
Riley Andrews24a3b782014-06-26 13:56:01 -0700993 cmd->line = state->line;
994 cmd->filename = state->filename;
Colin Cross6310a822010-04-20 14:29:05 -0700995 cmd->nargs = nargs;
996 memcpy(cmd->args, args, sizeof(char*) * nargs);
997 list_add_tail(&act->commands, &cmd->clist);
998}