blob: 956ed255a887e110c2929926fbbcb1b3ae55ceef [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;
Elliott Hughesd62f0602015-06-12 18:02:20 -0700209 if (!strcmp(s, "ritepid")) return K_writepid;
Colin Cross6310a822010-04-20 14:29:05 -0700210 if (!strcmp(s, "ait")) return K_wait;
211 break;
212 }
213 return K_UNKNOWN;
214}
215
Elliott Hughesf682b472015-02-06 12:19:48 -0800216static void parse_line_no_op(struct parse_state*, int, char**) {
Colin Cross6310a822010-04-20 14:29:05 -0700217}
218
Dima Zavina6235ea2011-12-16 14:20:12 -0800219static int push_chars(char **dst, int *len, const char *chars, int cnt)
220{
221 if (cnt > *len)
222 return -1;
223
224 memcpy(*dst, chars, cnt);
225 *dst += cnt;
226 *len -= cnt;
227
228 return 0;
229}
230
Dima Zavin84bf9af2011-12-20 13:44:41 -0800231int expand_props(char *dst, const char *src, int dst_size)
Dima Zavina6235ea2011-12-16 14:20:12 -0800232{
Dima Zavina6235ea2011-12-16 14:20:12 -0800233 char *dst_ptr = dst;
234 const char *src_ptr = src;
Dima Zavina6235ea2011-12-16 14:20:12 -0800235 int ret = 0;
236 int left = dst_size - 1;
237
238 if (!src || !dst || dst_size == 0)
239 return -1;
240
Dima Zavina6235ea2011-12-16 14:20:12 -0800241 /* - variables can either be $x.y or ${x.y}, in case they are only part
242 * of the string.
243 * - will accept $$ as a literal $.
244 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
245 * bad things will happen
246 */
247 while (*src_ptr && left > 0) {
248 char *c;
249 char prop[PROP_NAME_MAX + 1];
Colin Cross2deedfe2013-01-28 17:13:35 -0800250 char prop_val[PROP_VALUE_MAX];
Dima Zavina6235ea2011-12-16 14:20:12 -0800251 int prop_len = 0;
Colin Cross2deedfe2013-01-28 17:13:35 -0800252 int prop_val_len;
Dima Zavina6235ea2011-12-16 14:20:12 -0800253
254 c = strchr(src_ptr, '$');
255 if (!c) {
256 while (left-- > 0 && *src_ptr)
257 *(dst_ptr++) = *(src_ptr++);
258 break;
259 }
260
261 memset(prop, 0, sizeof(prop));
262
263 ret = push_chars(&dst_ptr, &left, src_ptr, c - src_ptr);
264 if (ret < 0)
265 goto err_nospace;
266 c++;
267
268 if (*c == '$') {
269 *(dst_ptr++) = *(c++);
270 src_ptr = c;
271 left--;
272 continue;
273 } else if (*c == '\0') {
274 break;
275 }
276
277 if (*c == '{') {
278 c++;
279 while (*c && *c != '}' && prop_len < PROP_NAME_MAX)
280 prop[prop_len++] = *(c++);
281 if (*c != '}') {
282 /* failed to find closing brace, abort. */
283 if (prop_len == PROP_NAME_MAX)
284 ERROR("prop name too long during expansion of '%s'\n",
285 src);
286 else if (*c == '\0')
287 ERROR("unexpected end of string in '%s', looking for }\n",
288 src);
289 goto err;
290 }
291 prop[prop_len] = '\0';
292 c++;
293 } else if (*c) {
294 while (*c && prop_len < PROP_NAME_MAX)
295 prop[prop_len++] = *(c++);
296 if (prop_len == PROP_NAME_MAX && *c != '\0') {
297 ERROR("prop name too long in '%s'\n", src);
298 goto err;
299 }
300 prop[prop_len] = '\0';
301 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
302 prop);
303 }
304
305 if (prop_len == 0) {
306 ERROR("invalid zero-length prop name in '%s'\n", src);
307 goto err;
308 }
309
Colin Cross2deedfe2013-01-28 17:13:35 -0800310 prop_val_len = property_get(prop, prop_val);
311 if (!prop_val_len) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800312 ERROR("property '%s' doesn't exist while expanding '%s'\n",
313 prop, src);
314 goto err;
315 }
316
Colin Cross2deedfe2013-01-28 17:13:35 -0800317 ret = push_chars(&dst_ptr, &left, prop_val, prop_val_len);
Dima Zavina6235ea2011-12-16 14:20:12 -0800318 if (ret < 0)
319 goto err_nospace;
320 src_ptr = c;
321 continue;
322 }
323
324 *dst_ptr = '\0';
325 return 0;
326
327err_nospace:
328 ERROR("destination buffer overflow while expanding '%s'\n", src);
329err:
330 return -1;
331}
332
Greg Hackmannd68db712013-11-18 09:44:20 -0800333static void parse_import(struct parse_state *state, int nargs, char **args)
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800334{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800335 struct listnode *import_list = (listnode*) state->priv;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800336 char conf_file[PATH_MAX];
337 int ret;
338
339 if (nargs != 2) {
340 ERROR("single argument needed for import\n");
341 return;
342 }
343
344 ret = expand_props(conf_file, args[1], sizeof(conf_file));
345 if (ret) {
346 ERROR("error while handling import on line '%d' in '%s'\n",
347 state->line, state->filename);
348 return;
349 }
350
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800351 struct import* import = (struct import*) calloc(1, sizeof(struct import));
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800352 import->filename = strdup(conf_file);
353 list_add_tail(import_list, &import->list);
Elliott Hughesda40c002015-03-27 23:20:44 -0700354 INFO("Added '%s' to import list\n", import->filename);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800355}
356
Greg Hackmannd68db712013-11-18 09:44:20 -0800357static void parse_new_section(struct parse_state *state, int kw,
Colin Cross6310a822010-04-20 14:29:05 -0700358 int nargs, char **args)
359{
360 printf("[ %s %s ]\n", args[0],
361 nargs > 1 ? args[1] : "");
362 switch(kw) {
363 case K_service:
364 state->context = parse_service(state, nargs, args);
365 if (state->context) {
366 state->parse_line = parse_line_service;
367 return;
368 }
369 break;
370 case K_on:
371 state->context = parse_action(state, nargs, args);
372 if (state->context) {
373 state->parse_line = parse_line_action;
374 return;
375 }
376 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700377 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800378 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800379 break;
Colin Cross6310a822010-04-20 14:29:05 -0700380 }
381 state->parse_line = parse_line_no_op;
382}
383
Elliott Hughesf682b472015-02-06 12:19:48 -0800384static void parse_config(const char *fn, const std::string& data)
Colin Cross6310a822010-04-20 14:29:05 -0700385{
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800386 struct listnode import_list;
387 struct listnode *node;
Colin Cross6310a822010-04-20 14:29:05 -0700388 char *args[INIT_PARSER_MAXARGS];
Colin Cross6310a822010-04-20 14:29:05 -0700389
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700390 int nargs = 0;
391
392 parse_state state;
Colin Cross6310a822010-04-20 14:29:05 -0700393 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800394 state.line = 0;
Elliott Hughesf682b472015-02-06 12:19:48 -0800395 state.ptr = strdup(data.c_str()); // TODO: fix this code!
Colin Cross6310a822010-04-20 14:29:05 -0700396 state.nexttoken = 0;
397 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800398
399 list_init(&import_list);
400 state.priv = &import_list;
401
Colin Cross6310a822010-04-20 14:29:05 -0700402 for (;;) {
403 switch (next_token(&state)) {
404 case T_EOF:
405 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800406 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700407 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800408 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700409 if (nargs) {
410 int kw = lookup_keyword(args[0]);
411 if (kw_is(kw, SECTION)) {
412 state.parse_line(&state, 0, 0);
413 parse_new_section(&state, kw, nargs, args);
414 } else {
415 state.parse_line(&state, nargs, args);
416 }
417 nargs = 0;
418 }
419 break;
420 case T_TEXT:
421 if (nargs < INIT_PARSER_MAXARGS) {
422 args[nargs++] = state.text;
423 }
424 break;
425 }
426 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800427
428parser_done:
429 list_for_each(node, &import_list) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700430 struct import* import = node_to_item(node, struct import, list);
431 if (!init_parse_config_file(import->filename)) {
432 ERROR("could not import file '%s' from '%s': %s\n",
433 import->filename, fn, strerror(errno));
434 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800435 }
Colin Cross6310a822010-04-20 14:29:05 -0700436}
437
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700438bool init_parse_config_file(const char* path) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800439 INFO("Parsing %s...\n", path);
Elliott Hughesda40c002015-03-27 23:20:44 -0700440 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800441 std::string data;
442 if (!read_file(path, &data)) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700443 return false;
Elliott Hughesf682b472015-02-06 12:19:48 -0800444 }
Colin Cross6310a822010-04-20 14:29:05 -0700445
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700446 data.push_back('\n'); // TODO: fix parse_config.
Elliott Hughesf682b472015-02-06 12:19:48 -0800447 parse_config(path, data);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800448 dump_parser_state();
Elliott Hughesda40c002015-03-27 23:20:44 -0700449
450 NOTICE("(Parsing %s took %.2fs.)\n", path, t.duration());
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700451 return true;
Colin Cross6310a822010-04-20 14:29:05 -0700452}
453
454static int valid_name(const char *name)
455{
456 if (strlen(name) > 16) {
457 return 0;
458 }
459 while (*name) {
460 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
461 return 0;
462 }
463 name++;
464 }
465 return 1;
466}
467
468struct service *service_find_by_name(const char *name)
469{
470 struct listnode *node;
471 struct service *svc;
472 list_for_each(node, &service_list) {
473 svc = node_to_item(node, struct service, slist);
474 if (!strcmp(svc->name, name)) {
475 return svc;
476 }
477 }
478 return 0;
479}
480
481struct service *service_find_by_pid(pid_t pid)
482{
483 struct listnode *node;
484 struct service *svc;
485 list_for_each(node, &service_list) {
486 svc = node_to_item(node, struct service, slist);
487 if (svc->pid == pid) {
488 return svc;
489 }
490 }
491 return 0;
492}
493
494struct service *service_find_by_keychord(int keychord_id)
495{
496 struct listnode *node;
497 struct service *svc;
498 list_for_each(node, &service_list) {
499 svc = node_to_item(node, struct service, slist);
500 if (svc->keychord_id == keychord_id) {
501 return svc;
502 }
503 }
504 return 0;
505}
506
507void service_for_each(void (*func)(struct service *svc))
508{
509 struct listnode *node;
510 struct service *svc;
511 list_for_each(node, &service_list) {
512 svc = node_to_item(node, struct service, slist);
513 func(svc);
514 }
515}
516
517void service_for_each_class(const char *classname,
518 void (*func)(struct service *svc))
519{
520 struct listnode *node;
521 struct service *svc;
522 list_for_each(node, &service_list) {
523 svc = node_to_item(node, struct service, slist);
524 if (!strcmp(svc->classname, classname)) {
525 func(svc);
526 }
527 }
528}
529
530void service_for_each_flags(unsigned matchflags,
531 void (*func)(struct service *svc))
532{
533 struct listnode *node;
534 struct service *svc;
535 list_for_each(node, &service_list) {
536 svc = node_to_item(node, struct service, slist);
537 if (svc->flags & matchflags) {
538 func(svc);
539 }
540 }
541}
542
543void action_for_each_trigger(const char *trigger,
544 void (*func)(struct action *act))
545{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700546 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700547 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700548 struct trigger *cur_trigger;
549
Colin Cross6310a822010-04-20 14:29:05 -0700550 list_for_each(node, &action_list) {
551 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700552 list_for_each(node2, &act->triggers) {
553 cur_trigger = node_to_item(node2, struct trigger, nlist);
554 if (!strcmp(cur_trigger->name, trigger)) {
555 func(act);
556 }
Colin Cross6310a822010-04-20 14:29:05 -0700557 }
558 }
559}
560
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700561
Colin Cross6310a822010-04-20 14:29:05 -0700562void queue_property_triggers(const char *name, const char *value)
563{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700564 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700565 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700566 struct trigger *cur_trigger;
567 bool match;
568 int name_length;
569
Colin Cross6310a822010-04-20 14:29:05 -0700570 list_for_each(node, &action_list) {
571 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700572 match = !name;
573 list_for_each(node2, &act->triggers) {
574 cur_trigger = node_to_item(node2, struct trigger, nlist);
575 if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) {
576 const char *test = cur_trigger->name + strlen("property:");
577 if (!match) {
578 name_length = strlen(name);
579 if (!strncmp(name, test, name_length) &&
580 test[name_length] == '=' &&
581 (!strcmp(test + name_length + 1, value) ||
582 !strcmp(test + name_length + 1, "*"))) {
583 match = true;
584 continue;
585 }
586 } else {
587 const char* equals = strchr(test, '=');
588 if (equals) {
589 char prop_name[PROP_NAME_MAX + 1];
590 char value[PROP_VALUE_MAX];
591 int length = equals - test;
592 if (length <= PROP_NAME_MAX) {
593 int ret;
594 memcpy(prop_name, test, length);
595 prop_name[length] = 0;
Colin Cross6310a822010-04-20 14:29:05 -0700596
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700597 /* does the property exist, and match the trigger value? */
598 ret = property_get(prop_name, value);
599 if (ret > 0 && (!strcmp(equals + 1, value) ||
600 !strcmp(equals + 1, "*"))) {
601 continue;
602 }
603 }
604 }
605 }
606 }
607 match = false;
608 break;
609 }
610 if (match) {
611 action_add_queue_tail(act);
Colin Cross6310a822010-04-20 14:29:05 -0700612 }
613 }
614}
615
616void queue_all_property_triggers()
617{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700618 queue_property_triggers(NULL, NULL);
Colin Cross6310a822010-04-20 14:29:05 -0700619}
620
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800621void queue_builtin_action(int (*func)(int nargs, char **args), const char *name)
Colin Cross6310a822010-04-20 14:29:05 -0700622{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800623 action* act = (action*) calloc(1, sizeof(*act));
624 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700625 cur_trigger->name = name;
626 list_init(&act->triggers);
627 list_add_tail(&act->triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700628 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800629 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700630
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800631 command* cmd = (command*) calloc(1, sizeof(*cmd));
Colin Cross6310a822010-04-20 14:29:05 -0700632 cmd->func = func;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800633 cmd->args[0] = const_cast<char*>(name);
Riley Andrews24a3b782014-06-26 13:56:01 -0700634 cmd->nargs = 1;
Colin Cross6310a822010-04-20 14:29:05 -0700635 list_add_tail(&act->commands, &cmd->clist);
636
637 list_add_tail(&action_list, &act->alist);
638 action_add_queue_tail(act);
639}
640
641void action_add_queue_tail(struct action *act)
642{
Colin Crossa5064622013-03-07 13:42:29 -0800643 if (list_empty(&act->qlist)) {
644 list_add_tail(&action_queue, &act->qlist);
645 }
Colin Cross6310a822010-04-20 14:29:05 -0700646}
647
648struct action *action_remove_queue_head(void)
649{
650 if (list_empty(&action_queue)) {
651 return 0;
652 } else {
653 struct listnode *node = list_head(&action_queue);
654 struct action *act = node_to_item(node, struct action, qlist);
655 list_remove(node);
Colin Crossa5064622013-03-07 13:42:29 -0800656 list_init(node);
Colin Cross6310a822010-04-20 14:29:05 -0700657 return act;
658 }
659}
660
661int action_queue_empty()
662{
663 return list_empty(&action_queue);
664}
665
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800666service* make_exec_oneshot_service(int nargs, char** args) {
667 // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS...
Mark Salyzyn17fff892015-06-02 11:11:02 -0700668 // SECLABEL can be a - to denote default
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800669 int command_arg = 1;
670 for (int i = 1; i < nargs; ++i) {
671 if (strcmp(args[i], "--") == 0) {
672 command_arg = i + 1;
673 break;
674 }
675 }
676 if (command_arg > 4 + NR_SVC_SUPP_GIDS) {
677 ERROR("exec called with too many supplementary group ids\n");
678 return NULL;
679 }
680
681 int argc = nargs - command_arg;
682 char** argv = (args + command_arg);
683 if (argc < 1) {
684 ERROR("exec called without command\n");
685 return NULL;
686 }
687
688 service* svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * argc);
689 if (svc == NULL) {
690 ERROR("Couldn't allocate service for exec of '%s': %s", argv[0], strerror(errno));
691 return NULL;
692 }
693
Mark Salyzyn17fff892015-06-02 11:11:02 -0700694 if ((command_arg > 2) && strcmp(args[1], "-")) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800695 svc->seclabel = args[1];
696 }
697 if (command_arg > 3) {
698 svc->uid = decode_uid(args[2]);
699 }
700 if (command_arg > 4) {
701 svc->gid = decode_uid(args[3]);
702 svc->nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */;
703 for (size_t i = 0; i < svc->nr_supp_gids; ++i) {
704 svc->supp_gids[i] = decode_uid(args[4 + i]);
705 }
706 }
707
708 static int exec_count; // Every service needs a unique name.
709 char* name = NULL;
710 asprintf(&name, "exec %d (%s)", exec_count++, argv[0]);
711 if (name == NULL) {
712 ERROR("Couldn't allocate name for exec service '%s'\n", argv[0]);
713 free(svc);
714 return NULL;
715 }
716 svc->name = name;
717 svc->classname = "default";
718 svc->flags = SVC_EXEC | SVC_ONESHOT;
719 svc->nargs = argc;
720 memcpy(svc->args, argv, sizeof(char*) * svc->nargs);
721 svc->args[argc] = NULL;
722 list_add_tail(&service_list, &svc->slist);
723 return svc;
724}
725
Colin Cross6310a822010-04-20 14:29:05 -0700726static void *parse_service(struct parse_state *state, int nargs, char **args)
727{
Colin Cross6310a822010-04-20 14:29:05 -0700728 if (nargs < 3) {
729 parse_error(state, "services must have a name and a program\n");
730 return 0;
731 }
732 if (!valid_name(args[1])) {
733 parse_error(state, "invalid service name '%s'\n", args[1]);
734 return 0;
735 }
736
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800737 service* svc = (service*) service_find_by_name(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700738 if (svc) {
739 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
740 return 0;
741 }
742
743 nargs -= 2;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800744 svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700745 if (!svc) {
746 parse_error(state, "out of memory\n");
747 return 0;
748 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800749 svc->name = strdup(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700750 svc->classname = "default";
751 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800752 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Colin Cross6310a822010-04-20 14:29:05 -0700753 svc->args[nargs] = 0;
754 svc->nargs = nargs;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700755 list_init(&svc->onrestart.triggers);
756 cur_trigger->name = "onrestart";
757 list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700758 list_init(&svc->onrestart.commands);
759 list_add_tail(&service_list, &svc->slist);
760 return svc;
761}
762
763static void parse_line_service(struct parse_state *state, int nargs, char **args)
764{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800765 struct service *svc = (service*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700766 struct command *cmd;
767 int i, kw, kw_nargs;
768
769 if (nargs == 0) {
770 return;
771 }
772
773 svc->ioprio_class = IoSchedClass_NONE;
774
775 kw = lookup_keyword(args[0]);
776 switch (kw) {
Colin Cross6310a822010-04-20 14:29:05 -0700777 case K_class:
778 if (nargs != 2) {
779 parse_error(state, "class option requires a classname\n");
780 } else {
781 svc->classname = args[1];
782 }
783 break;
784 case K_console:
785 svc->flags |= SVC_CONSOLE;
786 break;
787 case K_disabled:
788 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700789 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700790 break;
791 case K_ioprio:
792 if (nargs != 3) {
793 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
794 } else {
795 svc->ioprio_pri = strtoul(args[2], 0, 8);
796
797 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
798 parse_error(state, "priority value must be range 0 - 7\n");
799 break;
800 }
801
802 if (!strcmp(args[1], "rt")) {
803 svc->ioprio_class = IoSchedClass_RT;
804 } else if (!strcmp(args[1], "be")) {
805 svc->ioprio_class = IoSchedClass_BE;
806 } else if (!strcmp(args[1], "idle")) {
807 svc->ioprio_class = IoSchedClass_IDLE;
808 } else {
809 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
810 }
811 }
812 break;
813 case K_group:
814 if (nargs < 2) {
815 parse_error(state, "group option requires a group id\n");
816 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
817 parse_error(state, "group option accepts at most %d supp. groups\n",
818 NR_SVC_SUPP_GIDS);
819 } else {
820 int n;
821 svc->gid = decode_uid(args[1]);
822 for (n = 2; n < nargs; n++) {
823 svc->supp_gids[n-2] = decode_uid(args[n]);
824 }
825 svc->nr_supp_gids = n - 2;
826 }
827 break;
828 case K_keycodes:
829 if (nargs < 2) {
830 parse_error(state, "keycodes option requires atleast one keycode\n");
831 } else {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800832 svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
Colin Cross6310a822010-04-20 14:29:05 -0700833 if (!svc->keycodes) {
834 parse_error(state, "could not allocate keycodes\n");
835 } else {
836 svc->nkeycodes = nargs - 1;
837 for (i = 1; i < nargs; i++) {
838 svc->keycodes[i - 1] = atoi(args[i]);
839 }
840 }
841 }
842 break;
843 case K_oneshot:
844 svc->flags |= SVC_ONESHOT;
845 break;
846 case K_onrestart:
847 nargs--;
848 args++;
849 kw = lookup_keyword(args[0]);
850 if (!kw_is(kw, COMMAND)) {
851 parse_error(state, "invalid command '%s'\n", args[0]);
852 break;
853 }
854 kw_nargs = kw_nargs(kw);
855 if (nargs < kw_nargs) {
856 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
857 kw_nargs > 2 ? "arguments" : "argument");
858 break;
859 }
860
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800861 cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700862 cmd->func = kw_func(kw);
863 cmd->nargs = nargs;
864 memcpy(cmd->args, args, sizeof(char*) * nargs);
865 list_add_tail(&svc->onrestart.commands, &cmd->clist);
866 break;
867 case K_critical:
868 svc->flags |= SVC_CRITICAL;
869 break;
870 case K_setenv: { /* name value */
Gavin.Changc3a46762013-09-28 00:22:11 +0800871 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700872 parse_error(state, "setenv option requires name and value arguments\n");
873 break;
874 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800875 svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
Colin Cross6310a822010-04-20 14:29:05 -0700876 if (!ei) {
877 parse_error(state, "out of memory\n");
878 break;
879 }
880 ei->name = args[1];
881 ei->value = args[2];
882 ei->next = svc->envvars;
883 svc->envvars = ei;
884 break;
885 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400886 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700887 if (nargs < 4) {
888 parse_error(state, "socket option requires name, type, perm arguments\n");
889 break;
890 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400891 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
892 && strcmp(args[2],"seqpacket")) {
893 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700894 break;
895 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800896 socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
Colin Cross6310a822010-04-20 14:29:05 -0700897 if (!si) {
898 parse_error(state, "out of memory\n");
899 break;
900 }
901 si->name = args[1];
902 si->type = args[2];
903 si->perm = strtoul(args[3], 0, 8);
904 if (nargs > 4)
905 si->uid = decode_uid(args[4]);
906 if (nargs > 5)
907 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400908 if (nargs > 6)
909 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700910 si->next = svc->sockets;
911 svc->sockets = si;
912 break;
913 }
914 case K_user:
915 if (nargs != 2) {
916 parse_error(state, "user option requires a user id\n");
917 } else {
918 svc->uid = decode_uid(args[1]);
919 }
920 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500921 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500922 if (nargs != 2) {
923 parse_error(state, "seclabel option requires a label string\n");
924 } else {
925 svc->seclabel = args[1];
926 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500927 break;
Elliott Hughesd62f0602015-06-12 18:02:20 -0700928 case K_writepid:
929 if (nargs < 2) {
930 parse_error(state, "writepid option requires at least one filename\n");
931 break;
932 }
933 svc->writepid_files_ = new std::vector<std::string>;
934 for (int i = 1; i < nargs; ++i) {
935 svc->writepid_files_->push_back(args[i]);
936 }
937 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500938
Colin Cross6310a822010-04-20 14:29:05 -0700939 default:
940 parse_error(state, "invalid option '%s'\n", args[0]);
941 }
942}
943
944static void *parse_action(struct parse_state *state, int nargs, char **args)
945{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700946 struct trigger *cur_trigger;
947 int i;
Colin Cross6310a822010-04-20 14:29:05 -0700948 if (nargs < 2) {
949 parse_error(state, "actions must have a trigger\n");
950 return 0;
951 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700952
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800953 action* act = (action*) calloc(1, sizeof(*act));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700954 list_init(&act->triggers);
955
956 for (i = 1; i < nargs; i++) {
957 if (!(i % 2)) {
958 if (strcmp(args[i], "&&")) {
Tom Cherryae392cf2015-04-13 13:07:04 -0700959 struct listnode *node;
960 struct listnode *node2;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700961 parse_error(state, "& is the only symbol allowed to concatenate actions\n");
Tom Cherryae392cf2015-04-13 13:07:04 -0700962 list_for_each_safe(node, node2, &act->triggers) {
963 struct trigger *trigger = node_to_item(node, struct trigger, nlist);
964 free(trigger);
965 }
966 free(act);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700967 return 0;
968 } else
969 continue;
970 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800971 cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700972 cur_trigger->name = args[i];
973 list_add_tail(&act->triggers, &cur_trigger->nlist);
974 }
975
Colin Cross6310a822010-04-20 14:29:05 -0700976 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800977 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700978 list_add_tail(&action_list, &act->alist);
979 /* XXX add to hash */
980 return act;
981}
982
983static void parse_line_action(struct parse_state* state, int nargs, char **args)
984{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800985 struct action *act = (action*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700986 int kw, n;
987
988 if (nargs == 0) {
989 return;
990 }
991
992 kw = lookup_keyword(args[0]);
993 if (!kw_is(kw, COMMAND)) {
994 parse_error(state, "invalid command '%s'\n", args[0]);
995 return;
996 }
997
998 n = kw_nargs(kw);
999 if (nargs < n) {
1000 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
1001 n > 2 ? "arguments" : "argument");
1002 return;
1003 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -08001004 command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -07001005 cmd->func = kw_func(kw);
Riley Andrews24a3b782014-06-26 13:56:01 -07001006 cmd->line = state->line;
1007 cmd->filename = state->filename;
Colin Cross6310a822010-04-20 14:29:05 -07001008 cmd->nargs = nargs;
1009 memcpy(cmd->args, args, sizeof(char*) * nargs);
1010 list_add_tail(&act->commands, &cmd->clist);
1011}