blob: df049edebe26b88c4c7a30fde4ae3a6514a0e50b [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;
141 if (!strcmp(s, "xport")) return K_export;
142 break;
143 case 'g':
144 if (!strcmp(s, "roup")) return K_group;
145 break;
146 case 'h':
147 if (!strcmp(s, "ostname")) return K_hostname;
148 break;
149 case 'i':
150 if (!strcmp(s, "oprio")) return K_ioprio;
151 if (!strcmp(s, "fup")) return K_ifup;
152 if (!strcmp(s, "nsmod")) return K_insmod;
153 if (!strcmp(s, "mport")) return K_import;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000154 if (!strcmp(s, "nstallkey")) return K_installkey;
Colin Cross6310a822010-04-20 14:29:05 -0700155 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;
187 if (!strcmp(s, "etenv")) return K_setenv;
Colin Cross6310a822010-04-20 14:29:05 -0700188 if (!strcmp(s, "etprop")) return K_setprop;
189 if (!strcmp(s, "etrlimit")) return K_setrlimit;
190 if (!strcmp(s, "ocket")) return K_socket;
191 if (!strcmp(s, "tart")) return K_start;
192 if (!strcmp(s, "top")) return K_stop;
Ken Sumralla76baaa2013-07-09 18:42:09 -0700193 if (!strcmp(s, "wapon_all")) return K_swapon_all;
Colin Cross6310a822010-04-20 14:29:05 -0700194 if (!strcmp(s, "ymlink")) return K_symlink;
195 if (!strcmp(s, "ysclktz")) return K_sysclktz;
196 break;
197 case 't':
198 if (!strcmp(s, "rigger")) return K_trigger;
199 break;
200 case 'u':
201 if (!strcmp(s, "ser")) return K_user;
202 break;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000203 case 'v':
204 if (!strcmp(s, "erity_load_state")) return K_verity_load_state;
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000205 if (!strcmp(s, "erity_update_state")) return K_verity_update_state;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000206 break;
Colin Cross6310a822010-04-20 14:29:05 -0700207 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);
Elliott Hughesda40c002015-03-27 23:20:44 -0700353 INFO("Added '%s' to import list\n", import->filename);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800354}
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{
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800385 struct listnode import_list;
386 struct listnode *node;
Colin Cross6310a822010-04-20 14:29:05 -0700387 char *args[INIT_PARSER_MAXARGS];
Colin Cross6310a822010-04-20 14:29:05 -0700388
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700389 int nargs = 0;
390
391 parse_state state;
Colin Cross6310a822010-04-20 14:29:05 -0700392 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) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700429 struct import* import = node_to_item(node, struct import, list);
430 if (!init_parse_config_file(import->filename)) {
431 ERROR("could not import file '%s' from '%s': %s\n",
432 import->filename, fn, strerror(errno));
433 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800434 }
Colin Cross6310a822010-04-20 14:29:05 -0700435}
436
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700437bool init_parse_config_file(const char* path) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800438 INFO("Parsing %s...\n", path);
Elliott Hughesda40c002015-03-27 23:20:44 -0700439 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800440 std::string data;
441 if (!read_file(path, &data)) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700442 return false;
Elliott Hughesf682b472015-02-06 12:19:48 -0800443 }
Colin Cross6310a822010-04-20 14:29:05 -0700444
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700445 data.push_back('\n'); // TODO: fix parse_config.
Elliott Hughesf682b472015-02-06 12:19:48 -0800446 parse_config(path, data);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800447 dump_parser_state();
Elliott Hughesda40c002015-03-27 23:20:44 -0700448
449 NOTICE("(Parsing %s took %.2fs.)\n", path, t.duration());
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700450 return true;
Colin Cross6310a822010-04-20 14:29:05 -0700451}
452
453static int valid_name(const char *name)
454{
455 if (strlen(name) > 16) {
456 return 0;
457 }
458 while (*name) {
459 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
460 return 0;
461 }
462 name++;
463 }
464 return 1;
465}
466
467struct service *service_find_by_name(const char *name)
468{
469 struct listnode *node;
470 struct service *svc;
471 list_for_each(node, &service_list) {
472 svc = node_to_item(node, struct service, slist);
473 if (!strcmp(svc->name, name)) {
474 return svc;
475 }
476 }
477 return 0;
478}
479
480struct service *service_find_by_pid(pid_t pid)
481{
482 struct listnode *node;
483 struct service *svc;
484 list_for_each(node, &service_list) {
485 svc = node_to_item(node, struct service, slist);
486 if (svc->pid == pid) {
487 return svc;
488 }
489 }
490 return 0;
491}
492
493struct service *service_find_by_keychord(int keychord_id)
494{
495 struct listnode *node;
496 struct service *svc;
497 list_for_each(node, &service_list) {
498 svc = node_to_item(node, struct service, slist);
499 if (svc->keychord_id == keychord_id) {
500 return svc;
501 }
502 }
503 return 0;
504}
505
506void service_for_each(void (*func)(struct service *svc))
507{
508 struct listnode *node;
509 struct service *svc;
510 list_for_each(node, &service_list) {
511 svc = node_to_item(node, struct service, slist);
512 func(svc);
513 }
514}
515
516void service_for_each_class(const char *classname,
517 void (*func)(struct service *svc))
518{
519 struct listnode *node;
520 struct service *svc;
521 list_for_each(node, &service_list) {
522 svc = node_to_item(node, struct service, slist);
523 if (!strcmp(svc->classname, classname)) {
524 func(svc);
525 }
526 }
527}
528
529void service_for_each_flags(unsigned matchflags,
530 void (*func)(struct service *svc))
531{
532 struct listnode *node;
533 struct service *svc;
534 list_for_each(node, &service_list) {
535 svc = node_to_item(node, struct service, slist);
536 if (svc->flags & matchflags) {
537 func(svc);
538 }
539 }
540}
541
542void action_for_each_trigger(const char *trigger,
543 void (*func)(struct action *act))
544{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700545 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700546 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700547 struct trigger *cur_trigger;
548
Colin Cross6310a822010-04-20 14:29:05 -0700549 list_for_each(node, &action_list) {
550 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700551 list_for_each(node2, &act->triggers) {
552 cur_trigger = node_to_item(node2, struct trigger, nlist);
553 if (!strcmp(cur_trigger->name, trigger)) {
554 func(act);
555 }
Colin Cross6310a822010-04-20 14:29:05 -0700556 }
557 }
558}
559
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700560
Colin Cross6310a822010-04-20 14:29:05 -0700561void queue_property_triggers(const char *name, const char *value)
562{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700563 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700564 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700565 struct trigger *cur_trigger;
566 bool match;
567 int name_length;
568
Colin Cross6310a822010-04-20 14:29:05 -0700569 list_for_each(node, &action_list) {
570 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700571 match = !name;
572 list_for_each(node2, &act->triggers) {
573 cur_trigger = node_to_item(node2, struct trigger, nlist);
574 if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) {
575 const char *test = cur_trigger->name + strlen("property:");
576 if (!match) {
577 name_length = strlen(name);
578 if (!strncmp(name, test, name_length) &&
579 test[name_length] == '=' &&
580 (!strcmp(test + name_length + 1, value) ||
581 !strcmp(test + name_length + 1, "*"))) {
582 match = true;
583 continue;
584 }
585 } else {
586 const char* equals = strchr(test, '=');
587 if (equals) {
588 char prop_name[PROP_NAME_MAX + 1];
589 char value[PROP_VALUE_MAX];
590 int length = equals - test;
591 if (length <= PROP_NAME_MAX) {
592 int ret;
593 memcpy(prop_name, test, length);
594 prop_name[length] = 0;
Colin Cross6310a822010-04-20 14:29:05 -0700595
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700596 /* does the property exist, and match the trigger value? */
597 ret = property_get(prop_name, value);
598 if (ret > 0 && (!strcmp(equals + 1, value) ||
599 !strcmp(equals + 1, "*"))) {
600 continue;
601 }
602 }
603 }
604 }
605 }
606 match = false;
607 break;
608 }
609 if (match) {
610 action_add_queue_tail(act);
Colin Cross6310a822010-04-20 14:29:05 -0700611 }
612 }
613}
614
615void queue_all_property_triggers()
616{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700617 queue_property_triggers(NULL, NULL);
Colin Cross6310a822010-04-20 14:29:05 -0700618}
619
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800620void queue_builtin_action(int (*func)(int nargs, char **args), const char *name)
Colin Cross6310a822010-04-20 14:29:05 -0700621{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800622 action* act = (action*) calloc(1, sizeof(*act));
623 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700624 cur_trigger->name = name;
625 list_init(&act->triggers);
626 list_add_tail(&act->triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700627 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800628 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700629
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800630 command* cmd = (command*) calloc(1, sizeof(*cmd));
Colin Cross6310a822010-04-20 14:29:05 -0700631 cmd->func = func;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800632 cmd->args[0] = const_cast<char*>(name);
Riley Andrews24a3b782014-06-26 13:56:01 -0700633 cmd->nargs = 1;
Colin Cross6310a822010-04-20 14:29:05 -0700634 list_add_tail(&act->commands, &cmd->clist);
635
636 list_add_tail(&action_list, &act->alist);
637 action_add_queue_tail(act);
638}
639
640void action_add_queue_tail(struct action *act)
641{
Colin Crossa5064622013-03-07 13:42:29 -0800642 if (list_empty(&act->qlist)) {
643 list_add_tail(&action_queue, &act->qlist);
644 }
Colin Cross6310a822010-04-20 14:29:05 -0700645}
646
647struct action *action_remove_queue_head(void)
648{
649 if (list_empty(&action_queue)) {
650 return 0;
651 } else {
652 struct listnode *node = list_head(&action_queue);
653 struct action *act = node_to_item(node, struct action, qlist);
654 list_remove(node);
Colin Crossa5064622013-03-07 13:42:29 -0800655 list_init(node);
Colin Cross6310a822010-04-20 14:29:05 -0700656 return act;
657 }
658}
659
660int action_queue_empty()
661{
662 return list_empty(&action_queue);
663}
664
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800665service* make_exec_oneshot_service(int nargs, char** args) {
666 // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS...
667 int command_arg = 1;
668 for (int i = 1; i < nargs; ++i) {
669 if (strcmp(args[i], "--") == 0) {
670 command_arg = i + 1;
671 break;
672 }
673 }
674 if (command_arg > 4 + NR_SVC_SUPP_GIDS) {
675 ERROR("exec called with too many supplementary group ids\n");
676 return NULL;
677 }
678
679 int argc = nargs - command_arg;
680 char** argv = (args + command_arg);
681 if (argc < 1) {
682 ERROR("exec called without command\n");
683 return NULL;
684 }
685
686 service* svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * argc);
687 if (svc == NULL) {
688 ERROR("Couldn't allocate service for exec of '%s': %s", argv[0], strerror(errno));
689 return NULL;
690 }
691
692 if (command_arg > 2) {
693 svc->seclabel = args[1];
694 }
695 if (command_arg > 3) {
696 svc->uid = decode_uid(args[2]);
697 }
698 if (command_arg > 4) {
699 svc->gid = decode_uid(args[3]);
700 svc->nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */;
701 for (size_t i = 0; i < svc->nr_supp_gids; ++i) {
702 svc->supp_gids[i] = decode_uid(args[4 + i]);
703 }
704 }
705
706 static int exec_count; // Every service needs a unique name.
707 char* name = NULL;
708 asprintf(&name, "exec %d (%s)", exec_count++, argv[0]);
709 if (name == NULL) {
710 ERROR("Couldn't allocate name for exec service '%s'\n", argv[0]);
711 free(svc);
712 return NULL;
713 }
714 svc->name = name;
715 svc->classname = "default";
716 svc->flags = SVC_EXEC | SVC_ONESHOT;
717 svc->nargs = argc;
718 memcpy(svc->args, argv, sizeof(char*) * svc->nargs);
719 svc->args[argc] = NULL;
720 list_add_tail(&service_list, &svc->slist);
721 return svc;
722}
723
Colin Cross6310a822010-04-20 14:29:05 -0700724static void *parse_service(struct parse_state *state, int nargs, char **args)
725{
Colin Cross6310a822010-04-20 14:29:05 -0700726 if (nargs < 3) {
727 parse_error(state, "services must have a name and a program\n");
728 return 0;
729 }
730 if (!valid_name(args[1])) {
731 parse_error(state, "invalid service name '%s'\n", args[1]);
732 return 0;
733 }
734
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800735 service* svc = (service*) service_find_by_name(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700736 if (svc) {
737 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
738 return 0;
739 }
740
741 nargs -= 2;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800742 svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700743 if (!svc) {
744 parse_error(state, "out of memory\n");
745 return 0;
746 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800747 svc->name = strdup(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700748 svc->classname = "default";
749 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800750 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Colin Cross6310a822010-04-20 14:29:05 -0700751 svc->args[nargs] = 0;
752 svc->nargs = nargs;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700753 list_init(&svc->onrestart.triggers);
754 cur_trigger->name = "onrestart";
755 list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700756 list_init(&svc->onrestart.commands);
757 list_add_tail(&service_list, &svc->slist);
758 return svc;
759}
760
761static void parse_line_service(struct parse_state *state, int nargs, char **args)
762{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800763 struct service *svc = (service*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700764 struct command *cmd;
765 int i, kw, kw_nargs;
766
767 if (nargs == 0) {
768 return;
769 }
770
771 svc->ioprio_class = IoSchedClass_NONE;
772
773 kw = lookup_keyword(args[0]);
774 switch (kw) {
Colin Cross6310a822010-04-20 14:29:05 -0700775 case K_class:
776 if (nargs != 2) {
777 parse_error(state, "class option requires a classname\n");
778 } else {
779 svc->classname = args[1];
780 }
781 break;
782 case K_console:
783 svc->flags |= SVC_CONSOLE;
784 break;
785 case K_disabled:
786 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700787 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700788 break;
789 case K_ioprio:
790 if (nargs != 3) {
791 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
792 } else {
793 svc->ioprio_pri = strtoul(args[2], 0, 8);
794
795 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
796 parse_error(state, "priority value must be range 0 - 7\n");
797 break;
798 }
799
800 if (!strcmp(args[1], "rt")) {
801 svc->ioprio_class = IoSchedClass_RT;
802 } else if (!strcmp(args[1], "be")) {
803 svc->ioprio_class = IoSchedClass_BE;
804 } else if (!strcmp(args[1], "idle")) {
805 svc->ioprio_class = IoSchedClass_IDLE;
806 } else {
807 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
808 }
809 }
810 break;
811 case K_group:
812 if (nargs < 2) {
813 parse_error(state, "group option requires a group id\n");
814 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
815 parse_error(state, "group option accepts at most %d supp. groups\n",
816 NR_SVC_SUPP_GIDS);
817 } else {
818 int n;
819 svc->gid = decode_uid(args[1]);
820 for (n = 2; n < nargs; n++) {
821 svc->supp_gids[n-2] = decode_uid(args[n]);
822 }
823 svc->nr_supp_gids = n - 2;
824 }
825 break;
826 case K_keycodes:
827 if (nargs < 2) {
828 parse_error(state, "keycodes option requires atleast one keycode\n");
829 } else {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800830 svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
Colin Cross6310a822010-04-20 14:29:05 -0700831 if (!svc->keycodes) {
832 parse_error(state, "could not allocate keycodes\n");
833 } else {
834 svc->nkeycodes = nargs - 1;
835 for (i = 1; i < nargs; i++) {
836 svc->keycodes[i - 1] = atoi(args[i]);
837 }
838 }
839 }
840 break;
841 case K_oneshot:
842 svc->flags |= SVC_ONESHOT;
843 break;
844 case K_onrestart:
845 nargs--;
846 args++;
847 kw = lookup_keyword(args[0]);
848 if (!kw_is(kw, COMMAND)) {
849 parse_error(state, "invalid command '%s'\n", args[0]);
850 break;
851 }
852 kw_nargs = kw_nargs(kw);
853 if (nargs < kw_nargs) {
854 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
855 kw_nargs > 2 ? "arguments" : "argument");
856 break;
857 }
858
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800859 cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700860 cmd->func = kw_func(kw);
861 cmd->nargs = nargs;
862 memcpy(cmd->args, args, sizeof(char*) * nargs);
863 list_add_tail(&svc->onrestart.commands, &cmd->clist);
864 break;
865 case K_critical:
866 svc->flags |= SVC_CRITICAL;
867 break;
868 case K_setenv: { /* name value */
Gavin.Changc3a46762013-09-28 00:22:11 +0800869 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700870 parse_error(state, "setenv option requires name and value arguments\n");
871 break;
872 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800873 svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
Colin Cross6310a822010-04-20 14:29:05 -0700874 if (!ei) {
875 parse_error(state, "out of memory\n");
876 break;
877 }
878 ei->name = args[1];
879 ei->value = args[2];
880 ei->next = svc->envvars;
881 svc->envvars = ei;
882 break;
883 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400884 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700885 if (nargs < 4) {
886 parse_error(state, "socket option requires name, type, perm arguments\n");
887 break;
888 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400889 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
890 && strcmp(args[2],"seqpacket")) {
891 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700892 break;
893 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800894 socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
Colin Cross6310a822010-04-20 14:29:05 -0700895 if (!si) {
896 parse_error(state, "out of memory\n");
897 break;
898 }
899 si->name = args[1];
900 si->type = args[2];
901 si->perm = strtoul(args[3], 0, 8);
902 if (nargs > 4)
903 si->uid = decode_uid(args[4]);
904 if (nargs > 5)
905 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400906 if (nargs > 6)
907 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700908 si->next = svc->sockets;
909 svc->sockets = si;
910 break;
911 }
912 case K_user:
913 if (nargs != 2) {
914 parse_error(state, "user option requires a user id\n");
915 } else {
916 svc->uid = decode_uid(args[1]);
917 }
918 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500919 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500920 if (nargs != 2) {
921 parse_error(state, "seclabel option requires a label string\n");
922 } else {
923 svc->seclabel = args[1];
924 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500925 break;
926
Colin Cross6310a822010-04-20 14:29:05 -0700927 default:
928 parse_error(state, "invalid option '%s'\n", args[0]);
929 }
930}
931
932static void *parse_action(struct parse_state *state, int nargs, char **args)
933{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700934 struct trigger *cur_trigger;
935 int i;
Colin Cross6310a822010-04-20 14:29:05 -0700936 if (nargs < 2) {
937 parse_error(state, "actions must have a trigger\n");
938 return 0;
939 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700940
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800941 action* act = (action*) calloc(1, sizeof(*act));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700942 list_init(&act->triggers);
943
944 for (i = 1; i < nargs; i++) {
945 if (!(i % 2)) {
946 if (strcmp(args[i], "&&")) {
Tom Cherryae392cf2015-04-13 13:07:04 -0700947 struct listnode *node;
948 struct listnode *node2;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700949 parse_error(state, "& is the only symbol allowed to concatenate actions\n");
Tom Cherryae392cf2015-04-13 13:07:04 -0700950 list_for_each_safe(node, node2, &act->triggers) {
951 struct trigger *trigger = node_to_item(node, struct trigger, nlist);
952 free(trigger);
953 }
954 free(act);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700955 return 0;
956 } else
957 continue;
958 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800959 cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700960 cur_trigger->name = args[i];
961 list_add_tail(&act->triggers, &cur_trigger->nlist);
962 }
963
Colin Cross6310a822010-04-20 14:29:05 -0700964 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800965 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700966 list_add_tail(&action_list, &act->alist);
967 /* XXX add to hash */
968 return act;
969}
970
971static void parse_line_action(struct parse_state* state, int nargs, char **args)
972{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800973 struct action *act = (action*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700974 int kw, n;
975
976 if (nargs == 0) {
977 return;
978 }
979
980 kw = lookup_keyword(args[0]);
981 if (!kw_is(kw, COMMAND)) {
982 parse_error(state, "invalid command '%s'\n", args[0]);
983 return;
984 }
985
986 n = kw_nargs(kw);
987 if (nargs < n) {
988 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
989 n > 2 ? "arguments" : "argument");
990 return;
991 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800992 command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700993 cmd->func = kw_func(kw);
Riley Andrews24a3b782014-06-26 13:56:01 -0700994 cmd->line = state->line;
995 cmd->filename = state->filename;
Colin Cross6310a822010-04-20 14:29:05 -0700996 cmd->nargs = nargs;
997 memcpy(cmd->args, args, sizeof(char*) * nargs);
998 list_add_tail(&act->commands, &cmd->clist);
999}