blob: 1af52c853b021b869612282eeb58e0af5ce13246 [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;
Paul Lawrence948410a2015-07-01 14:40:56 -0700161 if (!strcmp(s, "oad_system_props")) return K_load_system_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;
Paul Crowley749af8c2015-05-28 17:35:06 +0100189 if (!strcmp(s, "etusercryptopolicies")) return K_setusercryptopolicies;
Colin Cross6310a822010-04-20 14:29:05 -0700190 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
Yabin Cui00ede7d2015-07-24 13:26:04 -0700219int expand_props(const char *src, std::string *dst) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800220 const char *src_ptr = src;
Dima Zavina6235ea2011-12-16 14:20:12 -0800221
Yabin Cui00ede7d2015-07-24 13:26:04 -0700222 if (!src || !dst) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800223 return -1;
Yabin Cui00ede7d2015-07-24 13:26:04 -0700224 }
Dima Zavina6235ea2011-12-16 14:20:12 -0800225
Dima Zavina6235ea2011-12-16 14:20:12 -0800226 /* - variables can either be $x.y or ${x.y}, in case they are only part
227 * of the string.
228 * - will accept $$ as a literal $.
229 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
230 * bad things will happen
231 */
Yabin Cui00ede7d2015-07-24 13:26:04 -0700232 while (*src_ptr) {
233 const char *c;
Dima Zavina6235ea2011-12-16 14:20:12 -0800234
235 c = strchr(src_ptr, '$');
236 if (!c) {
Yabin Cui00ede7d2015-07-24 13:26:04 -0700237 dst->append(src_ptr);
Dima Zavina6235ea2011-12-16 14:20:12 -0800238 break;
239 }
240
Yabin Cui00ede7d2015-07-24 13:26:04 -0700241 dst->append(src_ptr, c);
Dima Zavina6235ea2011-12-16 14:20:12 -0800242 c++;
243
244 if (*c == '$') {
Yabin Cui00ede7d2015-07-24 13:26:04 -0700245 dst->push_back(*(c++));
Dima Zavina6235ea2011-12-16 14:20:12 -0800246 src_ptr = c;
Dima Zavina6235ea2011-12-16 14:20:12 -0800247 continue;
248 } else if (*c == '\0') {
249 break;
250 }
251
Yabin Cui00ede7d2015-07-24 13:26:04 -0700252 std::string prop_name;
Dima Zavina6235ea2011-12-16 14:20:12 -0800253 if (*c == '{') {
254 c++;
Yabin Cui00ede7d2015-07-24 13:26:04 -0700255 const char* end = strchr(c, '}');
256 if (!end) {
257 // failed to find closing brace, abort.
258 ERROR("unexpected end of string in '%s', looking for }\n", src);
Dima Zavina6235ea2011-12-16 14:20:12 -0800259 goto err;
260 }
Yabin Cui00ede7d2015-07-24 13:26:04 -0700261 prop_name = std::string(c, end);
262 c = end + 1;
263 } else {
264 prop_name = c;
Dima Zavina6235ea2011-12-16 14:20:12 -0800265 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
Yabin Cui00ede7d2015-07-24 13:26:04 -0700266 c);
267 c += prop_name.size();
Dima Zavina6235ea2011-12-16 14:20:12 -0800268 }
269
Yabin Cui00ede7d2015-07-24 13:26:04 -0700270 if (prop_name.empty()) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800271 ERROR("invalid zero-length prop name in '%s'\n", src);
272 goto err;
273 }
274
Yabin Cui00ede7d2015-07-24 13:26:04 -0700275 std::string prop_val = property_get(prop_name.c_str());
Yabin Cui74edcea2015-07-24 10:11:05 -0700276 if (prop_val.empty()) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800277 ERROR("property '%s' doesn't exist while expanding '%s'\n",
Yabin Cui00ede7d2015-07-24 13:26:04 -0700278 prop_name.c_str(), src);
Dima Zavina6235ea2011-12-16 14:20:12 -0800279 goto err;
280 }
281
Yabin Cui00ede7d2015-07-24 13:26:04 -0700282 dst->append(prop_val);
Dima Zavina6235ea2011-12-16 14:20:12 -0800283 src_ptr = c;
284 continue;
285 }
286
Dima Zavina6235ea2011-12-16 14:20:12 -0800287 return 0;
Dima Zavina6235ea2011-12-16 14:20:12 -0800288err:
289 return -1;
290}
291
Greg Hackmannd68db712013-11-18 09:44:20 -0800292static void parse_import(struct parse_state *state, int nargs, char **args)
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800293{
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800294 if (nargs != 2) {
295 ERROR("single argument needed for import\n");
296 return;
297 }
298
Yabin Cui00ede7d2015-07-24 13:26:04 -0700299 std::string conf_file;
300 int ret = expand_props(args[1], &conf_file);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800301 if (ret) {
302 ERROR("error while handling import on line '%d' in '%s'\n",
303 state->line, state->filename);
304 return;
305 }
306
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800307 struct import* import = (struct import*) calloc(1, sizeof(struct import));
Yabin Cui00ede7d2015-07-24 13:26:04 -0700308 import->filename = strdup(conf_file.c_str());
309
310 struct listnode *import_list = (listnode*) state->priv;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800311 list_add_tail(import_list, &import->list);
Elliott Hughesda40c002015-03-27 23:20:44 -0700312 INFO("Added '%s' to import list\n", import->filename);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800313}
314
Greg Hackmannd68db712013-11-18 09:44:20 -0800315static void parse_new_section(struct parse_state *state, int kw,
Colin Cross6310a822010-04-20 14:29:05 -0700316 int nargs, char **args)
317{
318 printf("[ %s %s ]\n", args[0],
319 nargs > 1 ? args[1] : "");
320 switch(kw) {
321 case K_service:
322 state->context = parse_service(state, nargs, args);
323 if (state->context) {
324 state->parse_line = parse_line_service;
325 return;
326 }
327 break;
328 case K_on:
329 state->context = parse_action(state, nargs, args);
330 if (state->context) {
331 state->parse_line = parse_line_action;
332 return;
333 }
334 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700335 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800336 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800337 break;
Colin Cross6310a822010-04-20 14:29:05 -0700338 }
339 state->parse_line = parse_line_no_op;
340}
341
Elliott Hughesf682b472015-02-06 12:19:48 -0800342static void parse_config(const char *fn, const std::string& data)
Colin Cross6310a822010-04-20 14:29:05 -0700343{
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800344 struct listnode import_list;
345 struct listnode *node;
Colin Cross6310a822010-04-20 14:29:05 -0700346 char *args[INIT_PARSER_MAXARGS];
Colin Cross6310a822010-04-20 14:29:05 -0700347
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700348 int nargs = 0;
349
350 parse_state state;
Colin Cross6310a822010-04-20 14:29:05 -0700351 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800352 state.line = 0;
Elliott Hughesf682b472015-02-06 12:19:48 -0800353 state.ptr = strdup(data.c_str()); // TODO: fix this code!
Colin Cross6310a822010-04-20 14:29:05 -0700354 state.nexttoken = 0;
355 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800356
357 list_init(&import_list);
358 state.priv = &import_list;
359
Colin Cross6310a822010-04-20 14:29:05 -0700360 for (;;) {
361 switch (next_token(&state)) {
362 case T_EOF:
363 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800364 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700365 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800366 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700367 if (nargs) {
368 int kw = lookup_keyword(args[0]);
369 if (kw_is(kw, SECTION)) {
370 state.parse_line(&state, 0, 0);
371 parse_new_section(&state, kw, nargs, args);
372 } else {
373 state.parse_line(&state, nargs, args);
374 }
375 nargs = 0;
376 }
377 break;
378 case T_TEXT:
379 if (nargs < INIT_PARSER_MAXARGS) {
380 args[nargs++] = state.text;
381 }
382 break;
383 }
384 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800385
386parser_done:
387 list_for_each(node, &import_list) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700388 struct import* import = node_to_item(node, struct import, list);
389 if (!init_parse_config_file(import->filename)) {
390 ERROR("could not import file '%s' from '%s': %s\n",
391 import->filename, fn, strerror(errno));
392 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800393 }
Colin Cross6310a822010-04-20 14:29:05 -0700394}
395
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700396bool init_parse_config_file(const char* path) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800397 INFO("Parsing %s...\n", path);
Elliott Hughesda40c002015-03-27 23:20:44 -0700398 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800399 std::string data;
400 if (!read_file(path, &data)) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700401 return false;
Elliott Hughesf682b472015-02-06 12:19:48 -0800402 }
Colin Cross6310a822010-04-20 14:29:05 -0700403
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700404 data.push_back('\n'); // TODO: fix parse_config.
Elliott Hughesf682b472015-02-06 12:19:48 -0800405 parse_config(path, data);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800406 dump_parser_state();
Elliott Hughesda40c002015-03-27 23:20:44 -0700407
408 NOTICE("(Parsing %s took %.2fs.)\n", path, t.duration());
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700409 return true;
Colin Cross6310a822010-04-20 14:29:05 -0700410}
411
412static int valid_name(const char *name)
413{
414 if (strlen(name) > 16) {
415 return 0;
416 }
417 while (*name) {
418 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
419 return 0;
420 }
421 name++;
422 }
423 return 1;
424}
425
426struct service *service_find_by_name(const char *name)
427{
428 struct listnode *node;
429 struct service *svc;
430 list_for_each(node, &service_list) {
431 svc = node_to_item(node, struct service, slist);
432 if (!strcmp(svc->name, name)) {
433 return svc;
434 }
435 }
436 return 0;
437}
438
439struct service *service_find_by_pid(pid_t pid)
440{
441 struct listnode *node;
442 struct service *svc;
443 list_for_each(node, &service_list) {
444 svc = node_to_item(node, struct service, slist);
445 if (svc->pid == pid) {
446 return svc;
447 }
448 }
449 return 0;
450}
451
452struct service *service_find_by_keychord(int keychord_id)
453{
454 struct listnode *node;
455 struct service *svc;
456 list_for_each(node, &service_list) {
457 svc = node_to_item(node, struct service, slist);
458 if (svc->keychord_id == keychord_id) {
459 return svc;
460 }
461 }
462 return 0;
463}
464
465void service_for_each(void (*func)(struct service *svc))
466{
467 struct listnode *node;
468 struct service *svc;
469 list_for_each(node, &service_list) {
470 svc = node_to_item(node, struct service, slist);
471 func(svc);
472 }
473}
474
475void service_for_each_class(const char *classname,
476 void (*func)(struct service *svc))
477{
478 struct listnode *node;
479 struct service *svc;
480 list_for_each(node, &service_list) {
481 svc = node_to_item(node, struct service, slist);
482 if (!strcmp(svc->classname, classname)) {
483 func(svc);
484 }
485 }
486}
487
488void service_for_each_flags(unsigned matchflags,
489 void (*func)(struct service *svc))
490{
491 struct listnode *node;
492 struct service *svc;
493 list_for_each(node, &service_list) {
494 svc = node_to_item(node, struct service, slist);
495 if (svc->flags & matchflags) {
496 func(svc);
497 }
498 }
499}
500
501void action_for_each_trigger(const char *trigger,
502 void (*func)(struct action *act))
503{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700504 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700505 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700506 struct trigger *cur_trigger;
507
Colin Cross6310a822010-04-20 14:29:05 -0700508 list_for_each(node, &action_list) {
509 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700510 list_for_each(node2, &act->triggers) {
511 cur_trigger = node_to_item(node2, struct trigger, nlist);
512 if (!strcmp(cur_trigger->name, trigger)) {
513 func(act);
514 }
Colin Cross6310a822010-04-20 14:29:05 -0700515 }
516 }
517}
518
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700519
Colin Cross6310a822010-04-20 14:29:05 -0700520void queue_property_triggers(const char *name, const char *value)
521{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700522 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700523 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700524 struct trigger *cur_trigger;
525 bool match;
526 int name_length;
527
Colin Cross6310a822010-04-20 14:29:05 -0700528 list_for_each(node, &action_list) {
529 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700530 match = !name;
531 list_for_each(node2, &act->triggers) {
532 cur_trigger = node_to_item(node2, struct trigger, nlist);
533 if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) {
534 const char *test = cur_trigger->name + strlen("property:");
535 if (!match) {
536 name_length = strlen(name);
537 if (!strncmp(name, test, name_length) &&
538 test[name_length] == '=' &&
539 (!strcmp(test + name_length + 1, value) ||
540 !strcmp(test + name_length + 1, "*"))) {
541 match = true;
542 continue;
543 }
544 } else {
545 const char* equals = strchr(test, '=');
546 if (equals) {
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700547 int length = equals - test;
548 if (length <= PROP_NAME_MAX) {
Yabin Cui74edcea2015-07-24 10:11:05 -0700549 std::string prop_name(test, length);
550 std::string value = property_get(prop_name.c_str());
Colin Cross6310a822010-04-20 14:29:05 -0700551
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700552 /* does the property exist, and match the trigger value? */
Yabin Cui74edcea2015-07-24 10:11:05 -0700553 if (!value.empty() && (!strcmp(equals + 1, value.c_str()) ||
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700554 !strcmp(equals + 1, "*"))) {
555 continue;
556 }
557 }
558 }
559 }
560 }
561 match = false;
562 break;
563 }
564 if (match) {
565 action_add_queue_tail(act);
Colin Cross6310a822010-04-20 14:29:05 -0700566 }
567 }
568}
569
570void queue_all_property_triggers()
571{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700572 queue_property_triggers(NULL, NULL);
Colin Cross6310a822010-04-20 14:29:05 -0700573}
574
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800575void queue_builtin_action(int (*func)(int nargs, char **args), const char *name)
Colin Cross6310a822010-04-20 14:29:05 -0700576{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800577 action* act = (action*) calloc(1, sizeof(*act));
578 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700579 cur_trigger->name = name;
580 list_init(&act->triggers);
581 list_add_tail(&act->triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700582 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800583 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700584
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800585 command* cmd = (command*) calloc(1, sizeof(*cmd));
Colin Cross6310a822010-04-20 14:29:05 -0700586 cmd->func = func;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800587 cmd->args[0] = const_cast<char*>(name);
Riley Andrews24a3b782014-06-26 13:56:01 -0700588 cmd->nargs = 1;
Colin Cross6310a822010-04-20 14:29:05 -0700589 list_add_tail(&act->commands, &cmd->clist);
590
591 list_add_tail(&action_list, &act->alist);
592 action_add_queue_tail(act);
593}
594
595void action_add_queue_tail(struct action *act)
596{
Colin Crossa5064622013-03-07 13:42:29 -0800597 if (list_empty(&act->qlist)) {
598 list_add_tail(&action_queue, &act->qlist);
599 }
Colin Cross6310a822010-04-20 14:29:05 -0700600}
601
602struct action *action_remove_queue_head(void)
603{
604 if (list_empty(&action_queue)) {
605 return 0;
606 } else {
607 struct listnode *node = list_head(&action_queue);
608 struct action *act = node_to_item(node, struct action, qlist);
609 list_remove(node);
Colin Crossa5064622013-03-07 13:42:29 -0800610 list_init(node);
Colin Cross6310a822010-04-20 14:29:05 -0700611 return act;
612 }
613}
614
615int action_queue_empty()
616{
617 return list_empty(&action_queue);
618}
619
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800620service* make_exec_oneshot_service(int nargs, char** args) {
621 // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS...
Mark Salyzyn17fff892015-06-02 11:11:02 -0700622 // SECLABEL can be a - to denote default
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800623 int command_arg = 1;
624 for (int i = 1; i < nargs; ++i) {
625 if (strcmp(args[i], "--") == 0) {
626 command_arg = i + 1;
627 break;
628 }
629 }
630 if (command_arg > 4 + NR_SVC_SUPP_GIDS) {
631 ERROR("exec called with too many supplementary group ids\n");
632 return NULL;
633 }
634
635 int argc = nargs - command_arg;
636 char** argv = (args + command_arg);
637 if (argc < 1) {
638 ERROR("exec called without command\n");
639 return NULL;
640 }
641
642 service* svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * argc);
643 if (svc == NULL) {
644 ERROR("Couldn't allocate service for exec of '%s': %s", argv[0], strerror(errno));
645 return NULL;
646 }
647
Mark Salyzyn17fff892015-06-02 11:11:02 -0700648 if ((command_arg > 2) && strcmp(args[1], "-")) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800649 svc->seclabel = args[1];
650 }
651 if (command_arg > 3) {
652 svc->uid = decode_uid(args[2]);
653 }
654 if (command_arg > 4) {
655 svc->gid = decode_uid(args[3]);
656 svc->nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */;
657 for (size_t i = 0; i < svc->nr_supp_gids; ++i) {
658 svc->supp_gids[i] = decode_uid(args[4 + i]);
659 }
660 }
661
662 static int exec_count; // Every service needs a unique name.
663 char* name = NULL;
664 asprintf(&name, "exec %d (%s)", exec_count++, argv[0]);
665 if (name == NULL) {
666 ERROR("Couldn't allocate name for exec service '%s'\n", argv[0]);
667 free(svc);
668 return NULL;
669 }
670 svc->name = name;
671 svc->classname = "default";
672 svc->flags = SVC_EXEC | SVC_ONESHOT;
673 svc->nargs = argc;
674 memcpy(svc->args, argv, sizeof(char*) * svc->nargs);
675 svc->args[argc] = NULL;
676 list_add_tail(&service_list, &svc->slist);
677 return svc;
678}
679
Colin Cross6310a822010-04-20 14:29:05 -0700680static void *parse_service(struct parse_state *state, int nargs, char **args)
681{
Colin Cross6310a822010-04-20 14:29:05 -0700682 if (nargs < 3) {
683 parse_error(state, "services must have a name and a program\n");
684 return 0;
685 }
686 if (!valid_name(args[1])) {
687 parse_error(state, "invalid service name '%s'\n", args[1]);
688 return 0;
689 }
690
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800691 service* svc = (service*) service_find_by_name(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700692 if (svc) {
693 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
694 return 0;
695 }
696
697 nargs -= 2;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800698 svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700699 if (!svc) {
700 parse_error(state, "out of memory\n");
701 return 0;
702 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800703 svc->name = strdup(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700704 svc->classname = "default";
705 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800706 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Colin Cross6310a822010-04-20 14:29:05 -0700707 svc->args[nargs] = 0;
708 svc->nargs = nargs;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700709 list_init(&svc->onrestart.triggers);
710 cur_trigger->name = "onrestart";
711 list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700712 list_init(&svc->onrestart.commands);
713 list_add_tail(&service_list, &svc->slist);
714 return svc;
715}
716
717static void parse_line_service(struct parse_state *state, int nargs, char **args)
718{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800719 struct service *svc = (service*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700720 struct command *cmd;
721 int i, kw, kw_nargs;
722
723 if (nargs == 0) {
724 return;
725 }
726
727 svc->ioprio_class = IoSchedClass_NONE;
728
729 kw = lookup_keyword(args[0]);
730 switch (kw) {
Colin Cross6310a822010-04-20 14:29:05 -0700731 case K_class:
732 if (nargs != 2) {
733 parse_error(state, "class option requires a classname\n");
734 } else {
735 svc->classname = args[1];
736 }
737 break;
738 case K_console:
739 svc->flags |= SVC_CONSOLE;
740 break;
741 case K_disabled:
742 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700743 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700744 break;
745 case K_ioprio:
746 if (nargs != 3) {
747 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
748 } else {
749 svc->ioprio_pri = strtoul(args[2], 0, 8);
750
751 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
752 parse_error(state, "priority value must be range 0 - 7\n");
753 break;
754 }
755
756 if (!strcmp(args[1], "rt")) {
757 svc->ioprio_class = IoSchedClass_RT;
758 } else if (!strcmp(args[1], "be")) {
759 svc->ioprio_class = IoSchedClass_BE;
760 } else if (!strcmp(args[1], "idle")) {
761 svc->ioprio_class = IoSchedClass_IDLE;
762 } else {
763 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
764 }
765 }
766 break;
767 case K_group:
768 if (nargs < 2) {
769 parse_error(state, "group option requires a group id\n");
770 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
771 parse_error(state, "group option accepts at most %d supp. groups\n",
772 NR_SVC_SUPP_GIDS);
773 } else {
774 int n;
775 svc->gid = decode_uid(args[1]);
776 for (n = 2; n < nargs; n++) {
777 svc->supp_gids[n-2] = decode_uid(args[n]);
778 }
779 svc->nr_supp_gids = n - 2;
780 }
781 break;
782 case K_keycodes:
783 if (nargs < 2) {
784 parse_error(state, "keycodes option requires atleast one keycode\n");
785 } else {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800786 svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
Colin Cross6310a822010-04-20 14:29:05 -0700787 if (!svc->keycodes) {
788 parse_error(state, "could not allocate keycodes\n");
789 } else {
790 svc->nkeycodes = nargs - 1;
791 for (i = 1; i < nargs; i++) {
792 svc->keycodes[i - 1] = atoi(args[i]);
793 }
794 }
795 }
796 break;
797 case K_oneshot:
798 svc->flags |= SVC_ONESHOT;
799 break;
800 case K_onrestart:
801 nargs--;
802 args++;
803 kw = lookup_keyword(args[0]);
804 if (!kw_is(kw, COMMAND)) {
805 parse_error(state, "invalid command '%s'\n", args[0]);
806 break;
807 }
808 kw_nargs = kw_nargs(kw);
809 if (nargs < kw_nargs) {
810 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
811 kw_nargs > 2 ? "arguments" : "argument");
812 break;
813 }
814
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800815 cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700816 cmd->func = kw_func(kw);
817 cmd->nargs = nargs;
818 memcpy(cmd->args, args, sizeof(char*) * nargs);
819 list_add_tail(&svc->onrestart.commands, &cmd->clist);
820 break;
821 case K_critical:
822 svc->flags |= SVC_CRITICAL;
823 break;
824 case K_setenv: { /* name value */
Gavin.Changc3a46762013-09-28 00:22:11 +0800825 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700826 parse_error(state, "setenv option requires name and value arguments\n");
827 break;
828 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800829 svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
Colin Cross6310a822010-04-20 14:29:05 -0700830 if (!ei) {
831 parse_error(state, "out of memory\n");
832 break;
833 }
834 ei->name = args[1];
835 ei->value = args[2];
836 ei->next = svc->envvars;
837 svc->envvars = ei;
838 break;
839 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400840 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700841 if (nargs < 4) {
842 parse_error(state, "socket option requires name, type, perm arguments\n");
843 break;
844 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400845 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
846 && strcmp(args[2],"seqpacket")) {
847 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700848 break;
849 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800850 socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
Colin Cross6310a822010-04-20 14:29:05 -0700851 if (!si) {
852 parse_error(state, "out of memory\n");
853 break;
854 }
855 si->name = args[1];
856 si->type = args[2];
857 si->perm = strtoul(args[3], 0, 8);
858 if (nargs > 4)
859 si->uid = decode_uid(args[4]);
860 if (nargs > 5)
861 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400862 if (nargs > 6)
863 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700864 si->next = svc->sockets;
865 svc->sockets = si;
866 break;
867 }
868 case K_user:
869 if (nargs != 2) {
870 parse_error(state, "user option requires a user id\n");
871 } else {
872 svc->uid = decode_uid(args[1]);
873 }
874 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500875 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500876 if (nargs != 2) {
877 parse_error(state, "seclabel option requires a label string\n");
878 } else {
879 svc->seclabel = args[1];
880 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500881 break;
Elliott Hughesd62f0602015-06-12 18:02:20 -0700882 case K_writepid:
883 if (nargs < 2) {
884 parse_error(state, "writepid option requires at least one filename\n");
885 break;
886 }
887 svc->writepid_files_ = new std::vector<std::string>;
888 for (int i = 1; i < nargs; ++i) {
889 svc->writepid_files_->push_back(args[i]);
890 }
891 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500892
Colin Cross6310a822010-04-20 14:29:05 -0700893 default:
894 parse_error(state, "invalid option '%s'\n", args[0]);
895 }
896}
897
898static void *parse_action(struct parse_state *state, int nargs, char **args)
899{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700900 struct trigger *cur_trigger;
901 int i;
Colin Cross6310a822010-04-20 14:29:05 -0700902 if (nargs < 2) {
903 parse_error(state, "actions must have a trigger\n");
904 return 0;
905 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700906
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800907 action* act = (action*) calloc(1, sizeof(*act));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700908 list_init(&act->triggers);
909
910 for (i = 1; i < nargs; i++) {
911 if (!(i % 2)) {
912 if (strcmp(args[i], "&&")) {
Tom Cherryae392cf2015-04-13 13:07:04 -0700913 struct listnode *node;
914 struct listnode *node2;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700915 parse_error(state, "& is the only symbol allowed to concatenate actions\n");
Tom Cherryae392cf2015-04-13 13:07:04 -0700916 list_for_each_safe(node, node2, &act->triggers) {
917 struct trigger *trigger = node_to_item(node, struct trigger, nlist);
918 free(trigger);
919 }
920 free(act);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700921 return 0;
922 } else
923 continue;
924 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800925 cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700926 cur_trigger->name = args[i];
927 list_add_tail(&act->triggers, &cur_trigger->nlist);
928 }
929
Colin Cross6310a822010-04-20 14:29:05 -0700930 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800931 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700932 list_add_tail(&action_list, &act->alist);
933 /* XXX add to hash */
934 return act;
935}
936
937static void parse_line_action(struct parse_state* state, int nargs, char **args)
938{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800939 struct action *act = (action*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700940 int kw, n;
941
942 if (nargs == 0) {
943 return;
944 }
945
946 kw = lookup_keyword(args[0]);
947 if (!kw_is(kw, COMMAND)) {
948 parse_error(state, "invalid command '%s'\n", args[0]);
949 return;
950 }
951
952 n = kw_nargs(kw);
953 if (nargs < n) {
954 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
955 n > 2 ? "arguments" : "argument");
956 return;
957 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800958 command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700959 cmd->func = kw_func(kw);
Riley Andrews24a3b782014-06-26 13:56:01 -0700960 cmd->line = state->line;
961 cmd->filename = state->filename;
Colin Cross6310a822010-04-20 14:29:05 -0700962 cmd->nargs = nargs;
963 memcpy(cmd->args, args, sizeof(char*) * nargs);
964 list_add_tail(&act->commands, &cmd->clist);
965}