blob: 593f0c56b816fcf26212e7343bc8ed205ca2b867 [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, "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;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000155 if (!strcmp(s, "nstallkey")) return K_installkey;
Colin Cross6310a822010-04-20 14:29:05 -0700156 break;
157 case 'k':
158 if (!strcmp(s, "eycodes")) return K_keycodes;
159 break;
160 case 'l':
161 if (!strcmp(s, "oglevel")) return K_loglevel;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800162 if (!strcmp(s, "oad_persist_props")) return K_load_persist_props;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700163 if (!strcmp(s, "oad_all_props")) return K_load_all_props;
Colin Cross6310a822010-04-20 14:29:05 -0700164 break;
165 case 'm':
166 if (!strcmp(s, "kdir")) return K_mkdir;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700167 if (!strcmp(s, "ount_all")) return K_mount_all;
Colin Cross6310a822010-04-20 14:29:05 -0700168 if (!strcmp(s, "ount")) return K_mount;
169 break;
170 case 'o':
171 if (!strcmp(s, "n")) return K_on;
172 if (!strcmp(s, "neshot")) return K_oneshot;
173 if (!strcmp(s, "nrestart")) return K_onrestart;
174 break;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700175 case 'p':
176 if (!strcmp(s, "owerctl")) return K_powerctl;
Elliott Hughesf682b472015-02-06 12:19:48 -0800177 break;
Colin Cross6310a822010-04-20 14:29:05 -0700178 case 'r':
179 if (!strcmp(s, "estart")) return K_restart;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500180 if (!strcmp(s, "estorecon")) return K_restorecon;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400181 if (!strcmp(s, "estorecon_recursive")) return K_restorecon_recursive;
Ken Sumrall203bad52011-01-18 17:37:41 -0800182 if (!strcmp(s, "mdir")) return K_rmdir;
183 if (!strcmp(s, "m")) return K_rm;
Colin Cross6310a822010-04-20 14:29:05 -0700184 break;
185 case 's':
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500186 if (!strcmp(s, "eclabel")) return K_seclabel;
Colin Cross6310a822010-04-20 14:29:05 -0700187 if (!strcmp(s, "ervice")) return K_service;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500188 if (!strcmp(s, "etcon")) return K_setcon;
Colin Cross6310a822010-04-20 14:29:05 -0700189 if (!strcmp(s, "etenv")) return K_setenv;
Colin Cross6310a822010-04-20 14:29:05 -0700190 if (!strcmp(s, "etprop")) return K_setprop;
191 if (!strcmp(s, "etrlimit")) return K_setrlimit;
192 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;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000205 case 'v':
206 if (!strcmp(s, "erity_load_state")) return K_verity_load_state;
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000207 if (!strcmp(s, "erity_update_state")) return K_verity_update_state;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000208 break;
Colin Cross6310a822010-04-20 14:29:05 -0700209 case 'w':
210 if (!strcmp(s, "rite")) return K_write;
211 if (!strcmp(s, "ait")) return K_wait;
212 break;
213 }
214 return K_UNKNOWN;
215}
216
Elliott Hughesf682b472015-02-06 12:19:48 -0800217static void parse_line_no_op(struct parse_state*, int, char**) {
Colin Cross6310a822010-04-20 14:29:05 -0700218}
219
Dima Zavina6235ea2011-12-16 14:20:12 -0800220static int push_chars(char **dst, int *len, const char *chars, int cnt)
221{
222 if (cnt > *len)
223 return -1;
224
225 memcpy(*dst, chars, cnt);
226 *dst += cnt;
227 *len -= cnt;
228
229 return 0;
230}
231
Dima Zavin84bf9af2011-12-20 13:44:41 -0800232int expand_props(char *dst, const char *src, int dst_size)
Dima Zavina6235ea2011-12-16 14:20:12 -0800233{
Dima Zavina6235ea2011-12-16 14:20:12 -0800234 char *dst_ptr = dst;
235 const char *src_ptr = src;
Dima Zavina6235ea2011-12-16 14:20:12 -0800236 int ret = 0;
237 int left = dst_size - 1;
238
239 if (!src || !dst || dst_size == 0)
240 return -1;
241
Dima Zavina6235ea2011-12-16 14:20:12 -0800242 /* - variables can either be $x.y or ${x.y}, in case they are only part
243 * of the string.
244 * - will accept $$ as a literal $.
245 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
246 * bad things will happen
247 */
248 while (*src_ptr && left > 0) {
249 char *c;
250 char prop[PROP_NAME_MAX + 1];
Colin Cross2deedfe2013-01-28 17:13:35 -0800251 char prop_val[PROP_VALUE_MAX];
Dima Zavina6235ea2011-12-16 14:20:12 -0800252 int prop_len = 0;
Colin Cross2deedfe2013-01-28 17:13:35 -0800253 int prop_val_len;
Dima Zavina6235ea2011-12-16 14:20:12 -0800254
255 c = strchr(src_ptr, '$');
256 if (!c) {
257 while (left-- > 0 && *src_ptr)
258 *(dst_ptr++) = *(src_ptr++);
259 break;
260 }
261
262 memset(prop, 0, sizeof(prop));
263
264 ret = push_chars(&dst_ptr, &left, src_ptr, c - src_ptr);
265 if (ret < 0)
266 goto err_nospace;
267 c++;
268
269 if (*c == '$') {
270 *(dst_ptr++) = *(c++);
271 src_ptr = c;
272 left--;
273 continue;
274 } else if (*c == '\0') {
275 break;
276 }
277
278 if (*c == '{') {
279 c++;
280 while (*c && *c != '}' && prop_len < PROP_NAME_MAX)
281 prop[prop_len++] = *(c++);
282 if (*c != '}') {
283 /* failed to find closing brace, abort. */
284 if (prop_len == PROP_NAME_MAX)
285 ERROR("prop name too long during expansion of '%s'\n",
286 src);
287 else if (*c == '\0')
288 ERROR("unexpected end of string in '%s', looking for }\n",
289 src);
290 goto err;
291 }
292 prop[prop_len] = '\0';
293 c++;
294 } else if (*c) {
295 while (*c && prop_len < PROP_NAME_MAX)
296 prop[prop_len++] = *(c++);
297 if (prop_len == PROP_NAME_MAX && *c != '\0') {
298 ERROR("prop name too long in '%s'\n", src);
299 goto err;
300 }
301 prop[prop_len] = '\0';
302 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
303 prop);
304 }
305
306 if (prop_len == 0) {
307 ERROR("invalid zero-length prop name in '%s'\n", src);
308 goto err;
309 }
310
Colin Cross2deedfe2013-01-28 17:13:35 -0800311 prop_val_len = property_get(prop, prop_val);
312 if (!prop_val_len) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800313 ERROR("property '%s' doesn't exist while expanding '%s'\n",
314 prop, src);
315 goto err;
316 }
317
Colin Cross2deedfe2013-01-28 17:13:35 -0800318 ret = push_chars(&dst_ptr, &left, prop_val, prop_val_len);
Dima Zavina6235ea2011-12-16 14:20:12 -0800319 if (ret < 0)
320 goto err_nospace;
321 src_ptr = c;
322 continue;
323 }
324
325 *dst_ptr = '\0';
326 return 0;
327
328err_nospace:
329 ERROR("destination buffer overflow while expanding '%s'\n", src);
330err:
331 return -1;
332}
333
Greg Hackmannd68db712013-11-18 09:44:20 -0800334static void parse_import(struct parse_state *state, int nargs, char **args)
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800335{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800336 struct listnode *import_list = (listnode*) state->priv;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800337 char conf_file[PATH_MAX];
338 int ret;
339
340 if (nargs != 2) {
341 ERROR("single argument needed for import\n");
342 return;
343 }
344
345 ret = expand_props(conf_file, args[1], sizeof(conf_file));
346 if (ret) {
347 ERROR("error while handling import on line '%d' in '%s'\n",
348 state->line, state->filename);
349 return;
350 }
351
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800352 struct import* import = (struct import*) calloc(1, sizeof(struct import));
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800353 import->filename = strdup(conf_file);
354 list_add_tail(import_list, &import->list);
Elliott Hughesda40c002015-03-27 23:20:44 -0700355 INFO("Added '%s' to import list\n", import->filename);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800356}
357
Greg Hackmannd68db712013-11-18 09:44:20 -0800358static void parse_new_section(struct parse_state *state, int kw,
Colin Cross6310a822010-04-20 14:29:05 -0700359 int nargs, char **args)
360{
361 printf("[ %s %s ]\n", args[0],
362 nargs > 1 ? args[1] : "");
363 switch(kw) {
364 case K_service:
365 state->context = parse_service(state, nargs, args);
366 if (state->context) {
367 state->parse_line = parse_line_service;
368 return;
369 }
370 break;
371 case K_on:
372 state->context = parse_action(state, nargs, args);
373 if (state->context) {
374 state->parse_line = parse_line_action;
375 return;
376 }
377 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700378 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800379 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800380 break;
Colin Cross6310a822010-04-20 14:29:05 -0700381 }
382 state->parse_line = parse_line_no_op;
383}
384
Elliott Hughesf682b472015-02-06 12:19:48 -0800385static void parse_config(const char *fn, const std::string& data)
Colin Cross6310a822010-04-20 14:29:05 -0700386{
387 struct parse_state state;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800388 struct listnode import_list;
389 struct listnode *node;
Colin Cross6310a822010-04-20 14:29:05 -0700390 char *args[INIT_PARSER_MAXARGS];
391 int nargs;
392
393 nargs = 0;
394 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800395 state.line = 0;
Elliott Hughesf682b472015-02-06 12:19:48 -0800396 state.ptr = strdup(data.c_str()); // TODO: fix this code!
Colin Cross6310a822010-04-20 14:29:05 -0700397 state.nexttoken = 0;
398 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800399
400 list_init(&import_list);
401 state.priv = &import_list;
402
Colin Cross6310a822010-04-20 14:29:05 -0700403 for (;;) {
404 switch (next_token(&state)) {
405 case T_EOF:
406 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800407 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700408 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800409 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700410 if (nargs) {
411 int kw = lookup_keyword(args[0]);
412 if (kw_is(kw, SECTION)) {
413 state.parse_line(&state, 0, 0);
414 parse_new_section(&state, kw, nargs, args);
415 } else {
416 state.parse_line(&state, nargs, args);
417 }
418 nargs = 0;
419 }
420 break;
421 case T_TEXT:
422 if (nargs < INIT_PARSER_MAXARGS) {
423 args[nargs++] = state.text;
424 }
425 break;
426 }
427 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800428
429parser_done:
430 list_for_each(node, &import_list) {
431 struct import *import = node_to_item(node, struct import, list);
432 int ret;
433
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800434 ret = init_parse_config_file(import->filename);
435 if (ret)
436 ERROR("could not import file '%s' from '%s'\n",
437 import->filename, fn);
438 }
Colin Cross6310a822010-04-20 14:29:05 -0700439}
440
Elliott Hughesf682b472015-02-06 12:19:48 -0800441int init_parse_config_file(const char* path) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800442 INFO("Parsing %s...\n", path);
Elliott Hughesda40c002015-03-27 23:20:44 -0700443 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800444 std::string data;
445 if (!read_file(path, &data)) {
446 return -1;
447 }
Colin Cross6310a822010-04-20 14:29:05 -0700448
Elliott Hughesf682b472015-02-06 12:19:48 -0800449 parse_config(path, data);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800450 dump_parser_state();
Elliott Hughesda40c002015-03-27 23:20:44 -0700451
452 NOTICE("(Parsing %s took %.2fs.)\n", path, t.duration());
Colin Cross6310a822010-04-20 14:29:05 -0700453 return 0;
454}
455
456static int valid_name(const char *name)
457{
458 if (strlen(name) > 16) {
459 return 0;
460 }
461 while (*name) {
462 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
463 return 0;
464 }
465 name++;
466 }
467 return 1;
468}
469
470struct service *service_find_by_name(const char *name)
471{
472 struct listnode *node;
473 struct service *svc;
474 list_for_each(node, &service_list) {
475 svc = node_to_item(node, struct service, slist);
476 if (!strcmp(svc->name, name)) {
477 return svc;
478 }
479 }
480 return 0;
481}
482
483struct service *service_find_by_pid(pid_t pid)
484{
485 struct listnode *node;
486 struct service *svc;
487 list_for_each(node, &service_list) {
488 svc = node_to_item(node, struct service, slist);
489 if (svc->pid == pid) {
490 return svc;
491 }
492 }
493 return 0;
494}
495
496struct service *service_find_by_keychord(int keychord_id)
497{
498 struct listnode *node;
499 struct service *svc;
500 list_for_each(node, &service_list) {
501 svc = node_to_item(node, struct service, slist);
502 if (svc->keychord_id == keychord_id) {
503 return svc;
504 }
505 }
506 return 0;
507}
508
509void service_for_each(void (*func)(struct service *svc))
510{
511 struct listnode *node;
512 struct service *svc;
513 list_for_each(node, &service_list) {
514 svc = node_to_item(node, struct service, slist);
515 func(svc);
516 }
517}
518
519void service_for_each_class(const char *classname,
520 void (*func)(struct service *svc))
521{
522 struct listnode *node;
523 struct service *svc;
524 list_for_each(node, &service_list) {
525 svc = node_to_item(node, struct service, slist);
526 if (!strcmp(svc->classname, classname)) {
527 func(svc);
528 }
529 }
530}
531
532void service_for_each_flags(unsigned matchflags,
533 void (*func)(struct service *svc))
534{
535 struct listnode *node;
536 struct service *svc;
537 list_for_each(node, &service_list) {
538 svc = node_to_item(node, struct service, slist);
539 if (svc->flags & matchflags) {
540 func(svc);
541 }
542 }
543}
544
545void action_for_each_trigger(const char *trigger,
546 void (*func)(struct action *act))
547{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700548 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700549 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700550 struct trigger *cur_trigger;
551
Colin Cross6310a822010-04-20 14:29:05 -0700552 list_for_each(node, &action_list) {
553 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700554 list_for_each(node2, &act->triggers) {
555 cur_trigger = node_to_item(node2, struct trigger, nlist);
556 if (!strcmp(cur_trigger->name, trigger)) {
557 func(act);
558 }
Colin Cross6310a822010-04-20 14:29:05 -0700559 }
560 }
561}
562
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700563
Colin Cross6310a822010-04-20 14:29:05 -0700564void queue_property_triggers(const char *name, const char *value)
565{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700566 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700567 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700568 struct trigger *cur_trigger;
569 bool match;
570 int name_length;
571
Colin Cross6310a822010-04-20 14:29:05 -0700572 list_for_each(node, &action_list) {
573 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700574 match = !name;
575 list_for_each(node2, &act->triggers) {
576 cur_trigger = node_to_item(node2, struct trigger, nlist);
577 if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) {
578 const char *test = cur_trigger->name + strlen("property:");
579 if (!match) {
580 name_length = strlen(name);
581 if (!strncmp(name, test, name_length) &&
582 test[name_length] == '=' &&
583 (!strcmp(test + name_length + 1, value) ||
584 !strcmp(test + name_length + 1, "*"))) {
585 match = true;
586 continue;
587 }
588 } else {
589 const char* equals = strchr(test, '=');
590 if (equals) {
591 char prop_name[PROP_NAME_MAX + 1];
592 char value[PROP_VALUE_MAX];
593 int length = equals - test;
594 if (length <= PROP_NAME_MAX) {
595 int ret;
596 memcpy(prop_name, test, length);
597 prop_name[length] = 0;
Colin Cross6310a822010-04-20 14:29:05 -0700598
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700599 /* does the property exist, and match the trigger value? */
600 ret = property_get(prop_name, value);
601 if (ret > 0 && (!strcmp(equals + 1, value) ||
602 !strcmp(equals + 1, "*"))) {
603 continue;
604 }
605 }
606 }
607 }
608 }
609 match = false;
610 break;
611 }
612 if (match) {
613 action_add_queue_tail(act);
Colin Cross6310a822010-04-20 14:29:05 -0700614 }
615 }
616}
617
618void queue_all_property_triggers()
619{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700620 queue_property_triggers(NULL, NULL);
Colin Cross6310a822010-04-20 14:29:05 -0700621}
622
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800623void queue_builtin_action(int (*func)(int nargs, char **args), const char *name)
Colin Cross6310a822010-04-20 14:29:05 -0700624{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800625 action* act = (action*) calloc(1, sizeof(*act));
626 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700627 cur_trigger->name = name;
628 list_init(&act->triggers);
629 list_add_tail(&act->triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700630 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800631 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700632
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800633 command* cmd = (command*) calloc(1, sizeof(*cmd));
Colin Cross6310a822010-04-20 14:29:05 -0700634 cmd->func = func;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800635 cmd->args[0] = const_cast<char*>(name);
Riley Andrews24a3b782014-06-26 13:56:01 -0700636 cmd->nargs = 1;
Colin Cross6310a822010-04-20 14:29:05 -0700637 list_add_tail(&act->commands, &cmd->clist);
638
639 list_add_tail(&action_list, &act->alist);
640 action_add_queue_tail(act);
641}
642
643void action_add_queue_tail(struct action *act)
644{
Colin Crossa5064622013-03-07 13:42:29 -0800645 if (list_empty(&act->qlist)) {
646 list_add_tail(&action_queue, &act->qlist);
647 }
Colin Cross6310a822010-04-20 14:29:05 -0700648}
649
650struct action *action_remove_queue_head(void)
651{
652 if (list_empty(&action_queue)) {
653 return 0;
654 } else {
655 struct listnode *node = list_head(&action_queue);
656 struct action *act = node_to_item(node, struct action, qlist);
657 list_remove(node);
Colin Crossa5064622013-03-07 13:42:29 -0800658 list_init(node);
Colin Cross6310a822010-04-20 14:29:05 -0700659 return act;
660 }
661}
662
663int action_queue_empty()
664{
665 return list_empty(&action_queue);
666}
667
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800668service* make_exec_oneshot_service(int nargs, char** args) {
669 // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS...
670 int command_arg = 1;
671 for (int i = 1; i < nargs; ++i) {
672 if (strcmp(args[i], "--") == 0) {
673 command_arg = i + 1;
674 break;
675 }
676 }
677 if (command_arg > 4 + NR_SVC_SUPP_GIDS) {
678 ERROR("exec called with too many supplementary group ids\n");
679 return NULL;
680 }
681
682 int argc = nargs - command_arg;
683 char** argv = (args + command_arg);
684 if (argc < 1) {
685 ERROR("exec called without command\n");
686 return NULL;
687 }
688
689 service* svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * argc);
690 if (svc == NULL) {
691 ERROR("Couldn't allocate service for exec of '%s': %s", argv[0], strerror(errno));
692 return NULL;
693 }
694
695 if (command_arg > 2) {
696 svc->seclabel = args[1];
697 }
698 if (command_arg > 3) {
699 svc->uid = decode_uid(args[2]);
700 }
701 if (command_arg > 4) {
702 svc->gid = decode_uid(args[3]);
703 svc->nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */;
704 for (size_t i = 0; i < svc->nr_supp_gids; ++i) {
705 svc->supp_gids[i] = decode_uid(args[4 + i]);
706 }
707 }
708
709 static int exec_count; // Every service needs a unique name.
710 char* name = NULL;
711 asprintf(&name, "exec %d (%s)", exec_count++, argv[0]);
712 if (name == NULL) {
713 ERROR("Couldn't allocate name for exec service '%s'\n", argv[0]);
714 free(svc);
715 return NULL;
716 }
717 svc->name = name;
718 svc->classname = "default";
719 svc->flags = SVC_EXEC | SVC_ONESHOT;
720 svc->nargs = argc;
721 memcpy(svc->args, argv, sizeof(char*) * svc->nargs);
722 svc->args[argc] = NULL;
723 list_add_tail(&service_list, &svc->slist);
724 return svc;
725}
726
Colin Cross6310a822010-04-20 14:29:05 -0700727static void *parse_service(struct parse_state *state, int nargs, char **args)
728{
Colin Cross6310a822010-04-20 14:29:05 -0700729 if (nargs < 3) {
730 parse_error(state, "services must have a name and a program\n");
731 return 0;
732 }
733 if (!valid_name(args[1])) {
734 parse_error(state, "invalid service name '%s'\n", args[1]);
735 return 0;
736 }
737
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800738 service* svc = (service*) service_find_by_name(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700739 if (svc) {
740 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
741 return 0;
742 }
743
744 nargs -= 2;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800745 svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700746 if (!svc) {
747 parse_error(state, "out of memory\n");
748 return 0;
749 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800750 svc->name = strdup(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700751 svc->classname = "default";
752 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800753 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Colin Cross6310a822010-04-20 14:29:05 -0700754 svc->args[nargs] = 0;
755 svc->nargs = nargs;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700756 list_init(&svc->onrestart.triggers);
757 cur_trigger->name = "onrestart";
758 list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700759 list_init(&svc->onrestart.commands);
760 list_add_tail(&service_list, &svc->slist);
761 return svc;
762}
763
764static void parse_line_service(struct parse_state *state, int nargs, char **args)
765{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800766 struct service *svc = (service*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700767 struct command *cmd;
768 int i, kw, kw_nargs;
769
770 if (nargs == 0) {
771 return;
772 }
773
774 svc->ioprio_class = IoSchedClass_NONE;
775
776 kw = lookup_keyword(args[0]);
777 switch (kw) {
Colin Cross6310a822010-04-20 14:29:05 -0700778 case K_class:
779 if (nargs != 2) {
780 parse_error(state, "class option requires a classname\n");
781 } else {
782 svc->classname = args[1];
783 }
784 break;
785 case K_console:
786 svc->flags |= SVC_CONSOLE;
787 break;
788 case K_disabled:
789 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700790 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700791 break;
792 case K_ioprio:
793 if (nargs != 3) {
794 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
795 } else {
796 svc->ioprio_pri = strtoul(args[2], 0, 8);
797
798 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
799 parse_error(state, "priority value must be range 0 - 7\n");
800 break;
801 }
802
803 if (!strcmp(args[1], "rt")) {
804 svc->ioprio_class = IoSchedClass_RT;
805 } else if (!strcmp(args[1], "be")) {
806 svc->ioprio_class = IoSchedClass_BE;
807 } else if (!strcmp(args[1], "idle")) {
808 svc->ioprio_class = IoSchedClass_IDLE;
809 } else {
810 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
811 }
812 }
813 break;
814 case K_group:
815 if (nargs < 2) {
816 parse_error(state, "group option requires a group id\n");
817 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
818 parse_error(state, "group option accepts at most %d supp. groups\n",
819 NR_SVC_SUPP_GIDS);
820 } else {
821 int n;
822 svc->gid = decode_uid(args[1]);
823 for (n = 2; n < nargs; n++) {
824 svc->supp_gids[n-2] = decode_uid(args[n]);
825 }
826 svc->nr_supp_gids = n - 2;
827 }
828 break;
829 case K_keycodes:
830 if (nargs < 2) {
831 parse_error(state, "keycodes option requires atleast one keycode\n");
832 } else {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800833 svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
Colin Cross6310a822010-04-20 14:29:05 -0700834 if (!svc->keycodes) {
835 parse_error(state, "could not allocate keycodes\n");
836 } else {
837 svc->nkeycodes = nargs - 1;
838 for (i = 1; i < nargs; i++) {
839 svc->keycodes[i - 1] = atoi(args[i]);
840 }
841 }
842 }
843 break;
844 case K_oneshot:
845 svc->flags |= SVC_ONESHOT;
846 break;
847 case K_onrestart:
848 nargs--;
849 args++;
850 kw = lookup_keyword(args[0]);
851 if (!kw_is(kw, COMMAND)) {
852 parse_error(state, "invalid command '%s'\n", args[0]);
853 break;
854 }
855 kw_nargs = kw_nargs(kw);
856 if (nargs < kw_nargs) {
857 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
858 kw_nargs > 2 ? "arguments" : "argument");
859 break;
860 }
861
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800862 cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700863 cmd->func = kw_func(kw);
864 cmd->nargs = nargs;
865 memcpy(cmd->args, args, sizeof(char*) * nargs);
866 list_add_tail(&svc->onrestart.commands, &cmd->clist);
867 break;
868 case K_critical:
869 svc->flags |= SVC_CRITICAL;
870 break;
871 case K_setenv: { /* name value */
Gavin.Changc3a46762013-09-28 00:22:11 +0800872 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700873 parse_error(state, "setenv option requires name and value arguments\n");
874 break;
875 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800876 svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
Colin Cross6310a822010-04-20 14:29:05 -0700877 if (!ei) {
878 parse_error(state, "out of memory\n");
879 break;
880 }
881 ei->name = args[1];
882 ei->value = args[2];
883 ei->next = svc->envvars;
884 svc->envvars = ei;
885 break;
886 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400887 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700888 if (nargs < 4) {
889 parse_error(state, "socket option requires name, type, perm arguments\n");
890 break;
891 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400892 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
893 && strcmp(args[2],"seqpacket")) {
894 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700895 break;
896 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800897 socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
Colin Cross6310a822010-04-20 14:29:05 -0700898 if (!si) {
899 parse_error(state, "out of memory\n");
900 break;
901 }
902 si->name = args[1];
903 si->type = args[2];
904 si->perm = strtoul(args[3], 0, 8);
905 if (nargs > 4)
906 si->uid = decode_uid(args[4]);
907 if (nargs > 5)
908 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400909 if (nargs > 6)
910 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700911 si->next = svc->sockets;
912 svc->sockets = si;
913 break;
914 }
915 case K_user:
916 if (nargs != 2) {
917 parse_error(state, "user option requires a user id\n");
918 } else {
919 svc->uid = decode_uid(args[1]);
920 }
921 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500922 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500923 if (nargs != 2) {
924 parse_error(state, "seclabel option requires a label string\n");
925 } else {
926 svc->seclabel = args[1];
927 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500928 break;
929
Colin Cross6310a822010-04-20 14:29:05 -0700930 default:
931 parse_error(state, "invalid option '%s'\n", args[0]);
932 }
933}
934
935static void *parse_action(struct parse_state *state, int nargs, char **args)
936{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700937 struct trigger *cur_trigger;
938 int i;
Colin Cross6310a822010-04-20 14:29:05 -0700939 if (nargs < 2) {
940 parse_error(state, "actions must have a trigger\n");
941 return 0;
942 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700943
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800944 action* act = (action*) calloc(1, sizeof(*act));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700945 list_init(&act->triggers);
946
947 for (i = 1; i < nargs; i++) {
948 if (!(i % 2)) {
949 if (strcmp(args[i], "&&")) {
950 parse_error(state, "& is the only symbol allowed to concatenate actions\n");
951 return 0;
952 } else
953 continue;
954 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800955 cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700956 cur_trigger->name = args[i];
957 list_add_tail(&act->triggers, &cur_trigger->nlist);
958 }
959
Colin Cross6310a822010-04-20 14:29:05 -0700960 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800961 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700962 list_add_tail(&action_list, &act->alist);
963 /* XXX add to hash */
964 return act;
965}
966
967static void parse_line_action(struct parse_state* state, int nargs, char **args)
968{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800969 struct action *act = (action*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700970 int kw, n;
971
972 if (nargs == 0) {
973 return;
974 }
975
976 kw = lookup_keyword(args[0]);
977 if (!kw_is(kw, COMMAND)) {
978 parse_error(state, "invalid command '%s'\n", args[0]);
979 return;
980 }
981
982 n = kw_nargs(kw);
983 if (nargs < n) {
984 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
985 n > 2 ? "arguments" : "argument");
986 return;
987 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800988 command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700989 cmd->func = kw_func(kw);
Riley Andrews24a3b782014-06-26 13:56:01 -0700990 cmd->line = state->line;
991 cmd->filename = state->filename;
Colin Cross6310a822010-04-20 14:29:05 -0700992 cmd->nargs = nargs;
993 memcpy(cmd->args, args, sizeof(char*) * nargs);
994 list_add_tail(&act->commands, &cmd->clist);
995}