blob: 86e9ce68fd06dfeff32d6561cc636b581c94991b [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 ");
Yabin Cui00ede7d2015-07-24 13:26:04 -070098 std::string trigger_name = build_triggers_string(act);
99 INFO("%s", trigger_name.c_str());
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800100 INFO("\n");
101
102 struct listnode* node2;
103 list_for_each(node2, &act->commands) {
104 command* cmd = node_to_item(node2, struct command, clist);
105 INFO(" %p", cmd->func);
106 for (int n = 0; n < cmd->nargs; n++) {
107 INFO(" %s", cmd->args[n]);
108 }
109 INFO("\n");
110 }
111 INFO("\n");
112 }
113 }
114}
115
Greg Hackmannd68db712013-11-18 09:44:20 -0800116static int lookup_keyword(const char *s)
Colin Cross6310a822010-04-20 14:29:05 -0700117{
118 switch (*s++) {
Yongqin Liua197ff12014-12-05 13:45:02 +0800119 case 'b':
120 if (!strcmp(s, "ootchart_init")) return K_bootchart_init;
Mark Salyzyn7a3d66c2015-03-24 07:29:22 -0700121 break;
Colin Cross6310a822010-04-20 14:29:05 -0700122 case 'c':
Elliott Hughesf682b472015-02-06 12:19:48 -0800123 if (!strcmp(s, "opy")) return K_copy;
Colin Cross6310a822010-04-20 14:29:05 -0700124 if (!strcmp(s, "lass")) return K_class;
125 if (!strcmp(s, "lass_start")) return K_class_start;
126 if (!strcmp(s, "lass_stop")) return K_class_stop;
Ken Sumrall752923c2010-12-03 16:33:31 -0800127 if (!strcmp(s, "lass_reset")) return K_class_reset;
Colin Cross6310a822010-04-20 14:29:05 -0700128 if (!strcmp(s, "onsole")) return K_console;
129 if (!strcmp(s, "hown")) return K_chown;
130 if (!strcmp(s, "hmod")) return K_chmod;
131 if (!strcmp(s, "ritical")) return K_critical;
132 break;
133 case 'd':
134 if (!strcmp(s, "isabled")) return K_disabled;
135 if (!strcmp(s, "omainname")) return K_domainname;
136 break;
137 case 'e':
JP Abgrall3beec7e2014-05-02 21:14:29 -0700138 if (!strcmp(s, "nable")) return K_enable;
Colin Cross6310a822010-04-20 14:29:05 -0700139 if (!strcmp(s, "xec")) return K_exec;
140 if (!strcmp(s, "xport")) return K_export;
141 break;
142 case 'g':
143 if (!strcmp(s, "roup")) return K_group;
144 break;
145 case 'h':
146 if (!strcmp(s, "ostname")) return K_hostname;
147 break;
148 case 'i':
149 if (!strcmp(s, "oprio")) return K_ioprio;
150 if (!strcmp(s, "fup")) return K_ifup;
151 if (!strcmp(s, "nsmod")) return K_insmod;
152 if (!strcmp(s, "mport")) return K_import;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000153 if (!strcmp(s, "nstallkey")) return K_installkey;
Colin Cross6310a822010-04-20 14:29:05 -0700154 break;
155 case 'k':
156 if (!strcmp(s, "eycodes")) return K_keycodes;
157 break;
158 case 'l':
159 if (!strcmp(s, "oglevel")) return K_loglevel;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800160 if (!strcmp(s, "oad_persist_props")) return K_load_persist_props;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700161 if (!strcmp(s, "oad_all_props")) return K_load_all_props;
Colin Cross6310a822010-04-20 14:29:05 -0700162 break;
163 case 'm':
164 if (!strcmp(s, "kdir")) return K_mkdir;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700165 if (!strcmp(s, "ount_all")) return K_mount_all;
Colin Cross6310a822010-04-20 14:29:05 -0700166 if (!strcmp(s, "ount")) return K_mount;
167 break;
168 case 'o':
169 if (!strcmp(s, "n")) return K_on;
170 if (!strcmp(s, "neshot")) return K_oneshot;
171 if (!strcmp(s, "nrestart")) return K_onrestart;
172 break;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700173 case 'p':
174 if (!strcmp(s, "owerctl")) return K_powerctl;
Elliott Hughesf682b472015-02-06 12:19:48 -0800175 break;
Colin Cross6310a822010-04-20 14:29:05 -0700176 case 'r':
177 if (!strcmp(s, "estart")) return K_restart;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500178 if (!strcmp(s, "estorecon")) return K_restorecon;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400179 if (!strcmp(s, "estorecon_recursive")) return K_restorecon_recursive;
Ken Sumrall203bad52011-01-18 17:37:41 -0800180 if (!strcmp(s, "mdir")) return K_rmdir;
181 if (!strcmp(s, "m")) return K_rm;
Colin Cross6310a822010-04-20 14:29:05 -0700182 break;
183 case 's':
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500184 if (!strcmp(s, "eclabel")) return K_seclabel;
Colin Cross6310a822010-04-20 14:29:05 -0700185 if (!strcmp(s, "ervice")) return K_service;
186 if (!strcmp(s, "etenv")) return K_setenv;
Colin Cross6310a822010-04-20 14:29:05 -0700187 if (!strcmp(s, "etprop")) return K_setprop;
188 if (!strcmp(s, "etrlimit")) return K_setrlimit;
189 if (!strcmp(s, "ocket")) return K_socket;
190 if (!strcmp(s, "tart")) return K_start;
191 if (!strcmp(s, "top")) return K_stop;
Ken Sumralla76baaa2013-07-09 18:42:09 -0700192 if (!strcmp(s, "wapon_all")) return K_swapon_all;
Colin Cross6310a822010-04-20 14:29:05 -0700193 if (!strcmp(s, "ymlink")) return K_symlink;
194 if (!strcmp(s, "ysclktz")) return K_sysclktz;
195 break;
196 case 't':
197 if (!strcmp(s, "rigger")) return K_trigger;
198 break;
199 case 'u':
200 if (!strcmp(s, "ser")) return K_user;
201 break;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000202 case 'v':
203 if (!strcmp(s, "erity_load_state")) return K_verity_load_state;
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000204 if (!strcmp(s, "erity_update_state")) return K_verity_update_state;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000205 break;
Colin Cross6310a822010-04-20 14:29:05 -0700206 case 'w':
207 if (!strcmp(s, "rite")) return K_write;
Elliott Hughesd62f0602015-06-12 18:02:20 -0700208 if (!strcmp(s, "ritepid")) return K_writepid;
Colin Cross6310a822010-04-20 14:29:05 -0700209 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
Yabin Cui00ede7d2015-07-24 13:26:04 -0700218int expand_props(const char *src, std::string *dst) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800219 const char *src_ptr = src;
Dima Zavina6235ea2011-12-16 14:20:12 -0800220
Yabin Cui00ede7d2015-07-24 13:26:04 -0700221 if (!src || !dst) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800222 return -1;
Yabin Cui00ede7d2015-07-24 13:26:04 -0700223 }
Dima Zavina6235ea2011-12-16 14:20:12 -0800224
Dima Zavina6235ea2011-12-16 14:20:12 -0800225 /* - variables can either be $x.y or ${x.y}, in case they are only part
226 * of the string.
227 * - will accept $$ as a literal $.
228 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
229 * bad things will happen
230 */
Yabin Cui00ede7d2015-07-24 13:26:04 -0700231 while (*src_ptr) {
232 const char *c;
Dima Zavina6235ea2011-12-16 14:20:12 -0800233
234 c = strchr(src_ptr, '$');
235 if (!c) {
Yabin Cui00ede7d2015-07-24 13:26:04 -0700236 dst->append(src_ptr);
Dima Zavina6235ea2011-12-16 14:20:12 -0800237 break;
238 }
239
Yabin Cui00ede7d2015-07-24 13:26:04 -0700240 dst->append(src_ptr, c);
Dima Zavina6235ea2011-12-16 14:20:12 -0800241 c++;
242
243 if (*c == '$') {
Yabin Cui00ede7d2015-07-24 13:26:04 -0700244 dst->push_back(*(c++));
Dima Zavina6235ea2011-12-16 14:20:12 -0800245 src_ptr = c;
Dima Zavina6235ea2011-12-16 14:20:12 -0800246 continue;
247 } else if (*c == '\0') {
248 break;
249 }
250
Yabin Cui00ede7d2015-07-24 13:26:04 -0700251 std::string prop_name;
Dima Zavina6235ea2011-12-16 14:20:12 -0800252 if (*c == '{') {
253 c++;
Yabin Cui00ede7d2015-07-24 13:26:04 -0700254 const char* end = strchr(c, '}');
255 if (!end) {
256 // failed to find closing brace, abort.
257 ERROR("unexpected end of string in '%s', looking for }\n", src);
Dima Zavina6235ea2011-12-16 14:20:12 -0800258 goto err;
259 }
Yabin Cui00ede7d2015-07-24 13:26:04 -0700260 prop_name = std::string(c, end);
261 c = end + 1;
262 } else {
263 prop_name = c;
Dima Zavina6235ea2011-12-16 14:20:12 -0800264 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
Yabin Cui00ede7d2015-07-24 13:26:04 -0700265 c);
266 c += prop_name.size();
Dima Zavina6235ea2011-12-16 14:20:12 -0800267 }
268
Yabin Cui00ede7d2015-07-24 13:26:04 -0700269 if (prop_name.empty()) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800270 ERROR("invalid zero-length prop name in '%s'\n", src);
271 goto err;
272 }
273
Yabin Cui00ede7d2015-07-24 13:26:04 -0700274 std::string prop_val = property_get(prop_name.c_str());
Yabin Cui74edcea2015-07-24 10:11:05 -0700275 if (prop_val.empty()) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800276 ERROR("property '%s' doesn't exist while expanding '%s'\n",
Yabin Cui00ede7d2015-07-24 13:26:04 -0700277 prop_name.c_str(), src);
Dima Zavina6235ea2011-12-16 14:20:12 -0800278 goto err;
279 }
280
Yabin Cui00ede7d2015-07-24 13:26:04 -0700281 dst->append(prop_val);
Dima Zavina6235ea2011-12-16 14:20:12 -0800282 src_ptr = c;
283 continue;
284 }
285
Dima Zavina6235ea2011-12-16 14:20:12 -0800286 return 0;
Dima Zavina6235ea2011-12-16 14:20:12 -0800287err:
288 return -1;
289}
290
Greg Hackmannd68db712013-11-18 09:44:20 -0800291static void parse_import(struct parse_state *state, int nargs, char **args)
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800292{
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800293 if (nargs != 2) {
294 ERROR("single argument needed for import\n");
295 return;
296 }
297
Yabin Cui00ede7d2015-07-24 13:26:04 -0700298 std::string conf_file;
299 int ret = expand_props(args[1], &conf_file);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800300 if (ret) {
301 ERROR("error while handling import on line '%d' in '%s'\n",
302 state->line, state->filename);
303 return;
304 }
305
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800306 struct import* import = (struct import*) calloc(1, sizeof(struct import));
Yabin Cui00ede7d2015-07-24 13:26:04 -0700307 import->filename = strdup(conf_file.c_str());
308
309 struct listnode *import_list = (listnode*) state->priv;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800310 list_add_tail(import_list, &import->list);
Elliott Hughesda40c002015-03-27 23:20:44 -0700311 INFO("Added '%s' to import list\n", import->filename);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800312}
313
Greg Hackmannd68db712013-11-18 09:44:20 -0800314static void parse_new_section(struct parse_state *state, int kw,
Colin Cross6310a822010-04-20 14:29:05 -0700315 int nargs, char **args)
316{
317 printf("[ %s %s ]\n", args[0],
318 nargs > 1 ? args[1] : "");
319 switch(kw) {
320 case K_service:
321 state->context = parse_service(state, nargs, args);
322 if (state->context) {
323 state->parse_line = parse_line_service;
324 return;
325 }
326 break;
327 case K_on:
328 state->context = parse_action(state, nargs, args);
329 if (state->context) {
330 state->parse_line = parse_line_action;
331 return;
332 }
333 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700334 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800335 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800336 break;
Colin Cross6310a822010-04-20 14:29:05 -0700337 }
338 state->parse_line = parse_line_no_op;
339}
340
Elliott Hughesf682b472015-02-06 12:19:48 -0800341static void parse_config(const char *fn, const std::string& data)
Colin Cross6310a822010-04-20 14:29:05 -0700342{
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800343 struct listnode import_list;
344 struct listnode *node;
Colin Cross6310a822010-04-20 14:29:05 -0700345 char *args[INIT_PARSER_MAXARGS];
Colin Cross6310a822010-04-20 14:29:05 -0700346
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700347 int nargs = 0;
348
349 parse_state state;
Colin Cross6310a822010-04-20 14:29:05 -0700350 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800351 state.line = 0;
Elliott Hughesf682b472015-02-06 12:19:48 -0800352 state.ptr = strdup(data.c_str()); // TODO: fix this code!
Colin Cross6310a822010-04-20 14:29:05 -0700353 state.nexttoken = 0;
354 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800355
356 list_init(&import_list);
357 state.priv = &import_list;
358
Colin Cross6310a822010-04-20 14:29:05 -0700359 for (;;) {
360 switch (next_token(&state)) {
361 case T_EOF:
362 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800363 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700364 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800365 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700366 if (nargs) {
367 int kw = lookup_keyword(args[0]);
368 if (kw_is(kw, SECTION)) {
369 state.parse_line(&state, 0, 0);
370 parse_new_section(&state, kw, nargs, args);
371 } else {
372 state.parse_line(&state, nargs, args);
373 }
374 nargs = 0;
375 }
376 break;
377 case T_TEXT:
378 if (nargs < INIT_PARSER_MAXARGS) {
379 args[nargs++] = state.text;
380 }
381 break;
382 }
383 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800384
385parser_done:
386 list_for_each(node, &import_list) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700387 struct import* import = node_to_item(node, struct import, list);
388 if (!init_parse_config_file(import->filename)) {
389 ERROR("could not import file '%s' from '%s': %s\n",
390 import->filename, fn, strerror(errno));
391 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800392 }
Colin Cross6310a822010-04-20 14:29:05 -0700393}
394
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700395bool init_parse_config_file(const char* path) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800396 INFO("Parsing %s...\n", path);
Elliott Hughesda40c002015-03-27 23:20:44 -0700397 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800398 std::string data;
399 if (!read_file(path, &data)) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700400 return false;
Elliott Hughesf682b472015-02-06 12:19:48 -0800401 }
Colin Cross6310a822010-04-20 14:29:05 -0700402
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700403 data.push_back('\n'); // TODO: fix parse_config.
Elliott Hughesf682b472015-02-06 12:19:48 -0800404 parse_config(path, data);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800405 dump_parser_state();
Elliott Hughesda40c002015-03-27 23:20:44 -0700406
407 NOTICE("(Parsing %s took %.2fs.)\n", path, t.duration());
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700408 return true;
Colin Cross6310a822010-04-20 14:29:05 -0700409}
410
411static int valid_name(const char *name)
412{
413 if (strlen(name) > 16) {
414 return 0;
415 }
416 while (*name) {
417 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
418 return 0;
419 }
420 name++;
421 }
422 return 1;
423}
424
425struct service *service_find_by_name(const char *name)
426{
427 struct listnode *node;
428 struct service *svc;
429 list_for_each(node, &service_list) {
430 svc = node_to_item(node, struct service, slist);
431 if (!strcmp(svc->name, name)) {
432 return svc;
433 }
434 }
435 return 0;
436}
437
438struct service *service_find_by_pid(pid_t pid)
439{
440 struct listnode *node;
441 struct service *svc;
442 list_for_each(node, &service_list) {
443 svc = node_to_item(node, struct service, slist);
444 if (svc->pid == pid) {
445 return svc;
446 }
447 }
448 return 0;
449}
450
451struct service *service_find_by_keychord(int keychord_id)
452{
453 struct listnode *node;
454 struct service *svc;
455 list_for_each(node, &service_list) {
456 svc = node_to_item(node, struct service, slist);
457 if (svc->keychord_id == keychord_id) {
458 return svc;
459 }
460 }
461 return 0;
462}
463
464void service_for_each(void (*func)(struct service *svc))
465{
466 struct listnode *node;
467 struct service *svc;
468 list_for_each(node, &service_list) {
469 svc = node_to_item(node, struct service, slist);
470 func(svc);
471 }
472}
473
474void service_for_each_class(const char *classname,
475 void (*func)(struct service *svc))
476{
477 struct listnode *node;
478 struct service *svc;
479 list_for_each(node, &service_list) {
480 svc = node_to_item(node, struct service, slist);
481 if (!strcmp(svc->classname, classname)) {
482 func(svc);
483 }
484 }
485}
486
487void service_for_each_flags(unsigned matchflags,
488 void (*func)(struct service *svc))
489{
490 struct listnode *node;
491 struct service *svc;
492 list_for_each(node, &service_list) {
493 svc = node_to_item(node, struct service, slist);
494 if (svc->flags & matchflags) {
495 func(svc);
496 }
497 }
498}
499
500void action_for_each_trigger(const char *trigger,
501 void (*func)(struct action *act))
502{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700503 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700504 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700505 struct trigger *cur_trigger;
506
Colin Cross6310a822010-04-20 14:29:05 -0700507 list_for_each(node, &action_list) {
508 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700509 list_for_each(node2, &act->triggers) {
510 cur_trigger = node_to_item(node2, struct trigger, nlist);
511 if (!strcmp(cur_trigger->name, trigger)) {
512 func(act);
513 }
Colin Cross6310a822010-04-20 14:29:05 -0700514 }
515 }
516}
517
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700518
Colin Cross6310a822010-04-20 14:29:05 -0700519void queue_property_triggers(const char *name, const char *value)
520{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700521 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700522 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700523 struct trigger *cur_trigger;
524 bool match;
525 int name_length;
526
Colin Cross6310a822010-04-20 14:29:05 -0700527 list_for_each(node, &action_list) {
528 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700529 match = !name;
530 list_for_each(node2, &act->triggers) {
531 cur_trigger = node_to_item(node2, struct trigger, nlist);
532 if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) {
533 const char *test = cur_trigger->name + strlen("property:");
534 if (!match) {
535 name_length = strlen(name);
536 if (!strncmp(name, test, name_length) &&
537 test[name_length] == '=' &&
538 (!strcmp(test + name_length + 1, value) ||
539 !strcmp(test + name_length + 1, "*"))) {
540 match = true;
541 continue;
542 }
543 } else {
544 const char* equals = strchr(test, '=');
545 if (equals) {
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700546 int length = equals - test;
547 if (length <= PROP_NAME_MAX) {
Yabin Cui74edcea2015-07-24 10:11:05 -0700548 std::string prop_name(test, length);
549 std::string value = property_get(prop_name.c_str());
Colin Cross6310a822010-04-20 14:29:05 -0700550
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700551 /* does the property exist, and match the trigger value? */
Yabin Cui74edcea2015-07-24 10:11:05 -0700552 if (!value.empty() && (!strcmp(equals + 1, value.c_str()) ||
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700553 !strcmp(equals + 1, "*"))) {
554 continue;
555 }
556 }
557 }
558 }
559 }
560 match = false;
561 break;
562 }
563 if (match) {
564 action_add_queue_tail(act);
Colin Cross6310a822010-04-20 14:29:05 -0700565 }
566 }
567}
568
569void queue_all_property_triggers()
570{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700571 queue_property_triggers(NULL, NULL);
Colin Cross6310a822010-04-20 14:29:05 -0700572}
573
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800574void queue_builtin_action(int (*func)(int nargs, char **args), const char *name)
Colin Cross6310a822010-04-20 14:29:05 -0700575{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800576 action* act = (action*) calloc(1, sizeof(*act));
577 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700578 cur_trigger->name = name;
579 list_init(&act->triggers);
580 list_add_tail(&act->triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700581 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800582 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700583
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800584 command* cmd = (command*) calloc(1, sizeof(*cmd));
Colin Cross6310a822010-04-20 14:29:05 -0700585 cmd->func = func;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800586 cmd->args[0] = const_cast<char*>(name);
Riley Andrews24a3b782014-06-26 13:56:01 -0700587 cmd->nargs = 1;
Colin Cross6310a822010-04-20 14:29:05 -0700588 list_add_tail(&act->commands, &cmd->clist);
589
590 list_add_tail(&action_list, &act->alist);
591 action_add_queue_tail(act);
592}
593
594void action_add_queue_tail(struct action *act)
595{
Colin Crossa5064622013-03-07 13:42:29 -0800596 if (list_empty(&act->qlist)) {
597 list_add_tail(&action_queue, &act->qlist);
598 }
Colin Cross6310a822010-04-20 14:29:05 -0700599}
600
601struct action *action_remove_queue_head(void)
602{
603 if (list_empty(&action_queue)) {
604 return 0;
605 } else {
606 struct listnode *node = list_head(&action_queue);
607 struct action *act = node_to_item(node, struct action, qlist);
608 list_remove(node);
Colin Crossa5064622013-03-07 13:42:29 -0800609 list_init(node);
Colin Cross6310a822010-04-20 14:29:05 -0700610 return act;
611 }
612}
613
614int action_queue_empty()
615{
616 return list_empty(&action_queue);
617}
618
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800619service* make_exec_oneshot_service(int nargs, char** args) {
620 // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS...
Mark Salyzyn17fff892015-06-02 11:11:02 -0700621 // SECLABEL can be a - to denote default
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800622 int command_arg = 1;
623 for (int i = 1; i < nargs; ++i) {
624 if (strcmp(args[i], "--") == 0) {
625 command_arg = i + 1;
626 break;
627 }
628 }
629 if (command_arg > 4 + NR_SVC_SUPP_GIDS) {
630 ERROR("exec called with too many supplementary group ids\n");
631 return NULL;
632 }
633
634 int argc = nargs - command_arg;
635 char** argv = (args + command_arg);
636 if (argc < 1) {
637 ERROR("exec called without command\n");
638 return NULL;
639 }
640
641 service* svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * argc);
642 if (svc == NULL) {
643 ERROR("Couldn't allocate service for exec of '%s': %s", argv[0], strerror(errno));
644 return NULL;
645 }
646
Mark Salyzyn17fff892015-06-02 11:11:02 -0700647 if ((command_arg > 2) && strcmp(args[1], "-")) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800648 svc->seclabel = args[1];
649 }
650 if (command_arg > 3) {
651 svc->uid = decode_uid(args[2]);
652 }
653 if (command_arg > 4) {
654 svc->gid = decode_uid(args[3]);
655 svc->nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */;
656 for (size_t i = 0; i < svc->nr_supp_gids; ++i) {
657 svc->supp_gids[i] = decode_uid(args[4 + i]);
658 }
659 }
660
661 static int exec_count; // Every service needs a unique name.
662 char* name = NULL;
663 asprintf(&name, "exec %d (%s)", exec_count++, argv[0]);
664 if (name == NULL) {
665 ERROR("Couldn't allocate name for exec service '%s'\n", argv[0]);
666 free(svc);
667 return NULL;
668 }
669 svc->name = name;
670 svc->classname = "default";
671 svc->flags = SVC_EXEC | SVC_ONESHOT;
672 svc->nargs = argc;
673 memcpy(svc->args, argv, sizeof(char*) * svc->nargs);
674 svc->args[argc] = NULL;
675 list_add_tail(&service_list, &svc->slist);
676 return svc;
677}
678
Colin Cross6310a822010-04-20 14:29:05 -0700679static void *parse_service(struct parse_state *state, int nargs, char **args)
680{
Colin Cross6310a822010-04-20 14:29:05 -0700681 if (nargs < 3) {
682 parse_error(state, "services must have a name and a program\n");
683 return 0;
684 }
685 if (!valid_name(args[1])) {
686 parse_error(state, "invalid service name '%s'\n", args[1]);
687 return 0;
688 }
689
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800690 service* svc = (service*) service_find_by_name(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700691 if (svc) {
692 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
693 return 0;
694 }
695
696 nargs -= 2;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800697 svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700698 if (!svc) {
699 parse_error(state, "out of memory\n");
700 return 0;
701 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800702 svc->name = strdup(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700703 svc->classname = "default";
704 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800705 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Colin Cross6310a822010-04-20 14:29:05 -0700706 svc->args[nargs] = 0;
707 svc->nargs = nargs;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700708 list_init(&svc->onrestart.triggers);
709 cur_trigger->name = "onrestart";
710 list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700711 list_init(&svc->onrestart.commands);
712 list_add_tail(&service_list, &svc->slist);
713 return svc;
714}
715
716static void parse_line_service(struct parse_state *state, int nargs, char **args)
717{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800718 struct service *svc = (service*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700719 struct command *cmd;
720 int i, kw, kw_nargs;
721
722 if (nargs == 0) {
723 return;
724 }
725
726 svc->ioprio_class = IoSchedClass_NONE;
727
728 kw = lookup_keyword(args[0]);
729 switch (kw) {
Colin Cross6310a822010-04-20 14:29:05 -0700730 case K_class:
731 if (nargs != 2) {
732 parse_error(state, "class option requires a classname\n");
733 } else {
734 svc->classname = args[1];
735 }
736 break;
737 case K_console:
738 svc->flags |= SVC_CONSOLE;
739 break;
740 case K_disabled:
741 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700742 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700743 break;
744 case K_ioprio:
745 if (nargs != 3) {
746 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
747 } else {
748 svc->ioprio_pri = strtoul(args[2], 0, 8);
749
750 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
751 parse_error(state, "priority value must be range 0 - 7\n");
752 break;
753 }
754
755 if (!strcmp(args[1], "rt")) {
756 svc->ioprio_class = IoSchedClass_RT;
757 } else if (!strcmp(args[1], "be")) {
758 svc->ioprio_class = IoSchedClass_BE;
759 } else if (!strcmp(args[1], "idle")) {
760 svc->ioprio_class = IoSchedClass_IDLE;
761 } else {
762 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
763 }
764 }
765 break;
766 case K_group:
767 if (nargs < 2) {
768 parse_error(state, "group option requires a group id\n");
769 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
770 parse_error(state, "group option accepts at most %d supp. groups\n",
771 NR_SVC_SUPP_GIDS);
772 } else {
773 int n;
774 svc->gid = decode_uid(args[1]);
775 for (n = 2; n < nargs; n++) {
776 svc->supp_gids[n-2] = decode_uid(args[n]);
777 }
778 svc->nr_supp_gids = n - 2;
779 }
780 break;
781 case K_keycodes:
782 if (nargs < 2) {
783 parse_error(state, "keycodes option requires atleast one keycode\n");
784 } else {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800785 svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
Colin Cross6310a822010-04-20 14:29:05 -0700786 if (!svc->keycodes) {
787 parse_error(state, "could not allocate keycodes\n");
788 } else {
789 svc->nkeycodes = nargs - 1;
790 for (i = 1; i < nargs; i++) {
791 svc->keycodes[i - 1] = atoi(args[i]);
792 }
793 }
794 }
795 break;
796 case K_oneshot:
797 svc->flags |= SVC_ONESHOT;
798 break;
799 case K_onrestart:
800 nargs--;
801 args++;
802 kw = lookup_keyword(args[0]);
803 if (!kw_is(kw, COMMAND)) {
804 parse_error(state, "invalid command '%s'\n", args[0]);
805 break;
806 }
807 kw_nargs = kw_nargs(kw);
808 if (nargs < kw_nargs) {
809 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
810 kw_nargs > 2 ? "arguments" : "argument");
811 break;
812 }
813
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800814 cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700815 cmd->func = kw_func(kw);
816 cmd->nargs = nargs;
817 memcpy(cmd->args, args, sizeof(char*) * nargs);
818 list_add_tail(&svc->onrestart.commands, &cmd->clist);
819 break;
820 case K_critical:
821 svc->flags |= SVC_CRITICAL;
822 break;
823 case K_setenv: { /* name value */
Gavin.Changc3a46762013-09-28 00:22:11 +0800824 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700825 parse_error(state, "setenv option requires name and value arguments\n");
826 break;
827 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800828 svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
Colin Cross6310a822010-04-20 14:29:05 -0700829 if (!ei) {
830 parse_error(state, "out of memory\n");
831 break;
832 }
833 ei->name = args[1];
834 ei->value = args[2];
835 ei->next = svc->envvars;
836 svc->envvars = ei;
837 break;
838 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400839 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700840 if (nargs < 4) {
841 parse_error(state, "socket option requires name, type, perm arguments\n");
842 break;
843 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400844 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
845 && strcmp(args[2],"seqpacket")) {
846 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700847 break;
848 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800849 socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
Colin Cross6310a822010-04-20 14:29:05 -0700850 if (!si) {
851 parse_error(state, "out of memory\n");
852 break;
853 }
854 si->name = args[1];
855 si->type = args[2];
856 si->perm = strtoul(args[3], 0, 8);
857 if (nargs > 4)
858 si->uid = decode_uid(args[4]);
859 if (nargs > 5)
860 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400861 if (nargs > 6)
862 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700863 si->next = svc->sockets;
864 svc->sockets = si;
865 break;
866 }
867 case K_user:
868 if (nargs != 2) {
869 parse_error(state, "user option requires a user id\n");
870 } else {
871 svc->uid = decode_uid(args[1]);
872 }
873 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500874 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500875 if (nargs != 2) {
876 parse_error(state, "seclabel option requires a label string\n");
877 } else {
878 svc->seclabel = args[1];
879 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500880 break;
Elliott Hughesd62f0602015-06-12 18:02:20 -0700881 case K_writepid:
882 if (nargs < 2) {
883 parse_error(state, "writepid option requires at least one filename\n");
884 break;
885 }
886 svc->writepid_files_ = new std::vector<std::string>;
887 for (int i = 1; i < nargs; ++i) {
888 svc->writepid_files_->push_back(args[i]);
889 }
890 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500891
Colin Cross6310a822010-04-20 14:29:05 -0700892 default:
893 parse_error(state, "invalid option '%s'\n", args[0]);
894 }
895}
896
897static void *parse_action(struct parse_state *state, int nargs, char **args)
898{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700899 struct trigger *cur_trigger;
900 int i;
Colin Cross6310a822010-04-20 14:29:05 -0700901 if (nargs < 2) {
902 parse_error(state, "actions must have a trigger\n");
903 return 0;
904 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700905
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800906 action* act = (action*) calloc(1, sizeof(*act));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700907 list_init(&act->triggers);
908
909 for (i = 1; i < nargs; i++) {
910 if (!(i % 2)) {
911 if (strcmp(args[i], "&&")) {
Tom Cherryae392cf2015-04-13 13:07:04 -0700912 struct listnode *node;
913 struct listnode *node2;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700914 parse_error(state, "& is the only symbol allowed to concatenate actions\n");
Tom Cherryae392cf2015-04-13 13:07:04 -0700915 list_for_each_safe(node, node2, &act->triggers) {
916 struct trigger *trigger = node_to_item(node, struct trigger, nlist);
917 free(trigger);
918 }
919 free(act);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700920 return 0;
921 } else
922 continue;
923 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800924 cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700925 cur_trigger->name = args[i];
926 list_add_tail(&act->triggers, &cur_trigger->nlist);
927 }
928
Colin Cross6310a822010-04-20 14:29:05 -0700929 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800930 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700931 list_add_tail(&action_list, &act->alist);
932 /* XXX add to hash */
933 return act;
934}
935
936static void parse_line_action(struct parse_state* state, int nargs, char **args)
937{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800938 struct action *act = (action*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700939 int kw, n;
940
941 if (nargs == 0) {
942 return;
943 }
944
945 kw = lookup_keyword(args[0]);
946 if (!kw_is(kw, COMMAND)) {
947 parse_error(state, "invalid command '%s'\n", args[0]);
948 return;
949 }
950
951 n = kw_nargs(kw);
952 if (nargs < n) {
953 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
954 n > 2 ? "arguments" : "argument");
955 return;
956 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800957 command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700958 cmd->func = kw_func(kw);
Riley Andrews24a3b782014-06-26 13:56:01 -0700959 cmd->line = state->line;
960 cmd->filename = state->filename;
Colin Cross6310a822010-04-20 14:29:05 -0700961 cmd->nargs = nargs;
962 memcpy(cmd->args, args, sizeof(char*) * nargs);
963 list_add_tail(&act->commands, &cmd->clist);
964}