blob: 460f5acd223d13311449bd84d23764bad9b8e174 [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>
Lee Campbellf13b1b32015-07-24 16:57:14 -070018#include <dirent.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080019#include <errno.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070020#include <fcntl.h>
21#include <inttypes.h>
22#include <stdarg.h>
23#include <stddef.h>
Colin Cross6310a822010-04-20 14:29:05 -070024#include <stdio.h>
25#include <stdlib.h>
Colin Cross6310a822010-04-20 14:29:05 -070026#include <string.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070027#include <unistd.h>
Colin Cross6310a822010-04-20 14:29:05 -070028
29#include "init.h"
30#include "parser.h"
31#include "init_parser.h"
32#include "log.h"
Colin Cross6310a822010-04-20 14:29:05 -070033#include "property_service.h"
34#include "util.h"
35
Lee Campbellf13b1b32015-07-24 16:57:14 -070036#include <base/stringprintf.h>
Colin Cross6310a822010-04-20 14:29:05 -070037#include <cutils/iosched_policy.h>
Dima Zavinda04c522011-09-01 17:09:44 -070038#include <cutils/list.h>
Colin Cross6310a822010-04-20 14:29:05 -070039
Colin Cross6310a822010-04-20 14:29:05 -070040static list_declare(service_list);
41static list_declare(action_list);
42static list_declare(action_queue);
43
Dima Zavin78a1b1f2011-12-20 12:53:48 -080044struct import {
45 struct listnode list;
46 const char *filename;
47};
48
Colin Cross6310a822010-04-20 14:29:05 -070049static void *parse_service(struct parse_state *state, int nargs, char **args);
50static void parse_line_service(struct parse_state *state, int nargs, char **args);
51
52static void *parse_action(struct parse_state *state, int nargs, char **args);
53static void parse_line_action(struct parse_state *state, int nargs, char **args);
54
55#define SECTION 0x01
56#define COMMAND 0x02
57#define OPTION 0x04
58
59#include "keywords.h"
60
61#define KEYWORD(symbol, flags, nargs, func) \
62 [ K_##symbol ] = { #symbol, func, nargs + 1, flags, },
63
Greg Hackmannd68db712013-11-18 09:44:20 -080064static struct {
Colin Cross6310a822010-04-20 14:29:05 -070065 const char *name;
66 int (*func)(int nargs, char **args);
67 unsigned char nargs;
68 unsigned char flags;
69} keyword_info[KEYWORD_COUNT] = {
70 [ K_UNKNOWN ] = { "unknown", 0, 0, 0 },
71#include "keywords.h"
72};
73#undef KEYWORD
74
75#define kw_is(kw, type) (keyword_info[kw].flags & (type))
76#define kw_name(kw) (keyword_info[kw].name)
77#define kw_func(kw) (keyword_info[kw].func)
78#define kw_nargs(kw) (keyword_info[kw].nargs)
79
Elliott Hughesc0e919c2015-02-04 14:46:36 -080080void dump_parser_state() {
81 if (false) {
82 struct listnode* node;
83 list_for_each(node, &service_list) {
84 service* svc = node_to_item(node, struct service, slist);
85 INFO("service %s\n", svc->name);
86 INFO(" class '%s'\n", svc->classname);
87 INFO(" exec");
88 for (int n = 0; n < svc->nargs; n++) {
89 INFO(" '%s'", svc->args[n]);
90 }
91 INFO("\n");
92 for (socketinfo* si = svc->sockets; si; si = si->next) {
93 INFO(" socket %s %s 0%o\n", si->name, si->type, si->perm);
94 }
95 }
96
97 list_for_each(node, &action_list) {
98 action* act = node_to_item(node, struct action, alist);
99 INFO("on ");
Yabin Cui00ede7d2015-07-24 13:26:04 -0700100 std::string trigger_name = build_triggers_string(act);
101 INFO("%s", trigger_name.c_str());
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800102 INFO("\n");
103
104 struct listnode* node2;
105 list_for_each(node2, &act->commands) {
106 command* cmd = node_to_item(node2, struct command, clist);
107 INFO(" %p", cmd->func);
108 for (int n = 0; n < cmd->nargs; n++) {
109 INFO(" %s", cmd->args[n]);
110 }
111 INFO("\n");
112 }
113 INFO("\n");
114 }
115 }
116}
117
Greg Hackmannd68db712013-11-18 09:44:20 -0800118static int lookup_keyword(const char *s)
Colin Cross6310a822010-04-20 14:29:05 -0700119{
120 switch (*s++) {
Yongqin Liua197ff12014-12-05 13:45:02 +0800121 case 'b':
122 if (!strcmp(s, "ootchart_init")) return K_bootchart_init;
Mark Salyzyn7a3d66c2015-03-24 07:29:22 -0700123 break;
Colin Cross6310a822010-04-20 14:29:05 -0700124 case 'c':
Elliott Hughesf682b472015-02-06 12:19:48 -0800125 if (!strcmp(s, "opy")) return K_copy;
Colin Cross6310a822010-04-20 14:29:05 -0700126 if (!strcmp(s, "lass")) return K_class;
127 if (!strcmp(s, "lass_start")) return K_class_start;
128 if (!strcmp(s, "lass_stop")) return K_class_stop;
Ken Sumrall752923c2010-12-03 16:33:31 -0800129 if (!strcmp(s, "lass_reset")) return K_class_reset;
Colin Cross6310a822010-04-20 14:29:05 -0700130 if (!strcmp(s, "onsole")) return K_console;
131 if (!strcmp(s, "hown")) return K_chown;
132 if (!strcmp(s, "hmod")) return K_chmod;
133 if (!strcmp(s, "ritical")) return K_critical;
134 break;
135 case 'd':
136 if (!strcmp(s, "isabled")) return K_disabled;
137 if (!strcmp(s, "omainname")) return K_domainname;
138 break;
139 case 'e':
JP Abgrall3beec7e2014-05-02 21:14:29 -0700140 if (!strcmp(s, "nable")) return K_enable;
Colin Cross6310a822010-04-20 14:29:05 -0700141 if (!strcmp(s, "xec")) return K_exec;
142 if (!strcmp(s, "xport")) return K_export;
143 break;
144 case 'g':
145 if (!strcmp(s, "roup")) return K_group;
146 break;
147 case 'h':
148 if (!strcmp(s, "ostname")) return K_hostname;
149 break;
150 case 'i':
151 if (!strcmp(s, "oprio")) return K_ioprio;
152 if (!strcmp(s, "fup")) return K_ifup;
153 if (!strcmp(s, "nsmod")) return K_insmod;
154 if (!strcmp(s, "mport")) return K_import;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000155 if (!strcmp(s, "nstallkey")) return K_installkey;
Colin Cross6310a822010-04-20 14:29:05 -0700156 break;
157 case 'k':
158 if (!strcmp(s, "eycodes")) return K_keycodes;
159 break;
160 case 'l':
161 if (!strcmp(s, "oglevel")) return K_loglevel;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800162 if (!strcmp(s, "oad_persist_props")) return K_load_persist_props;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700163 if (!strcmp(s, "oad_all_props")) return K_load_all_props;
Colin Cross6310a822010-04-20 14:29:05 -0700164 break;
165 case 'm':
166 if (!strcmp(s, "kdir")) return K_mkdir;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700167 if (!strcmp(s, "ount_all")) return K_mount_all;
Colin Cross6310a822010-04-20 14:29:05 -0700168 if (!strcmp(s, "ount")) return K_mount;
169 break;
170 case 'o':
171 if (!strcmp(s, "n")) return K_on;
172 if (!strcmp(s, "neshot")) return K_oneshot;
173 if (!strcmp(s, "nrestart")) return K_onrestart;
174 break;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700175 case 'p':
176 if (!strcmp(s, "owerctl")) return K_powerctl;
Elliott Hughesf682b472015-02-06 12:19:48 -0800177 break;
Colin Cross6310a822010-04-20 14:29:05 -0700178 case 'r':
179 if (!strcmp(s, "estart")) return K_restart;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500180 if (!strcmp(s, "estorecon")) return K_restorecon;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400181 if (!strcmp(s, "estorecon_recursive")) return K_restorecon_recursive;
Ken Sumrall203bad52011-01-18 17:37:41 -0800182 if (!strcmp(s, "mdir")) return K_rmdir;
183 if (!strcmp(s, "m")) return K_rm;
Colin Cross6310a822010-04-20 14:29:05 -0700184 break;
185 case 's':
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500186 if (!strcmp(s, "eclabel")) return K_seclabel;
Colin Cross6310a822010-04-20 14:29:05 -0700187 if (!strcmp(s, "ervice")) return K_service;
188 if (!strcmp(s, "etenv")) return K_setenv;
Colin Cross6310a822010-04-20 14:29:05 -0700189 if (!strcmp(s, "etprop")) return K_setprop;
190 if (!strcmp(s, "etrlimit")) return K_setrlimit;
191 if (!strcmp(s, "ocket")) return K_socket;
192 if (!strcmp(s, "tart")) return K_start;
193 if (!strcmp(s, "top")) return K_stop;
Ken Sumralla76baaa2013-07-09 18:42:09 -0700194 if (!strcmp(s, "wapon_all")) return K_swapon_all;
Colin Cross6310a822010-04-20 14:29:05 -0700195 if (!strcmp(s, "ymlink")) return K_symlink;
196 if (!strcmp(s, "ysclktz")) return K_sysclktz;
197 break;
198 case 't':
199 if (!strcmp(s, "rigger")) return K_trigger;
200 break;
201 case 'u':
202 if (!strcmp(s, "ser")) return K_user;
203 break;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000204 case 'v':
205 if (!strcmp(s, "erity_load_state")) return K_verity_load_state;
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000206 if (!strcmp(s, "erity_update_state")) return K_verity_update_state;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000207 break;
Colin Cross6310a822010-04-20 14:29:05 -0700208 case 'w':
209 if (!strcmp(s, "rite")) return K_write;
Elliott Hughesd62f0602015-06-12 18:02:20 -0700210 if (!strcmp(s, "ritepid")) return K_writepid;
Colin Cross6310a822010-04-20 14:29:05 -0700211 if (!strcmp(s, "ait")) return K_wait;
212 break;
213 }
214 return K_UNKNOWN;
215}
216
Elliott Hughesf682b472015-02-06 12:19:48 -0800217static void parse_line_no_op(struct parse_state*, int, char**) {
Colin Cross6310a822010-04-20 14:29:05 -0700218}
219
Yabin Cui00ede7d2015-07-24 13:26:04 -0700220int expand_props(const char *src, std::string *dst) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800221 const char *src_ptr = src;
Dima Zavina6235ea2011-12-16 14:20:12 -0800222
Yabin Cui00ede7d2015-07-24 13:26:04 -0700223 if (!src || !dst) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800224 return -1;
Yabin Cui00ede7d2015-07-24 13:26:04 -0700225 }
Dima Zavina6235ea2011-12-16 14:20:12 -0800226
Dima Zavina6235ea2011-12-16 14:20:12 -0800227 /* - variables can either be $x.y or ${x.y}, in case they are only part
228 * of the string.
229 * - will accept $$ as a literal $.
230 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
231 * bad things will happen
232 */
Yabin Cui00ede7d2015-07-24 13:26:04 -0700233 while (*src_ptr) {
234 const char *c;
Dima Zavina6235ea2011-12-16 14:20:12 -0800235
236 c = strchr(src_ptr, '$');
237 if (!c) {
Yabin Cui00ede7d2015-07-24 13:26:04 -0700238 dst->append(src_ptr);
Dima Zavina6235ea2011-12-16 14:20:12 -0800239 break;
240 }
241
Yabin Cui00ede7d2015-07-24 13:26:04 -0700242 dst->append(src_ptr, c);
Dima Zavina6235ea2011-12-16 14:20:12 -0800243 c++;
244
245 if (*c == '$') {
Yabin Cui00ede7d2015-07-24 13:26:04 -0700246 dst->push_back(*(c++));
Dima Zavina6235ea2011-12-16 14:20:12 -0800247 src_ptr = c;
Dima Zavina6235ea2011-12-16 14:20:12 -0800248 continue;
249 } else if (*c == '\0') {
250 break;
251 }
252
Yabin Cui00ede7d2015-07-24 13:26:04 -0700253 std::string prop_name;
Dima Zavina6235ea2011-12-16 14:20:12 -0800254 if (*c == '{') {
255 c++;
Yabin Cui00ede7d2015-07-24 13:26:04 -0700256 const char* end = strchr(c, '}');
257 if (!end) {
258 // failed to find closing brace, abort.
259 ERROR("unexpected end of string in '%s', looking for }\n", src);
Dima Zavina6235ea2011-12-16 14:20:12 -0800260 goto err;
261 }
Yabin Cui00ede7d2015-07-24 13:26:04 -0700262 prop_name = std::string(c, end);
263 c = end + 1;
264 } else {
265 prop_name = c;
Dima Zavina6235ea2011-12-16 14:20:12 -0800266 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
Yabin Cui00ede7d2015-07-24 13:26:04 -0700267 c);
268 c += prop_name.size();
Dima Zavina6235ea2011-12-16 14:20:12 -0800269 }
270
Yabin Cui00ede7d2015-07-24 13:26:04 -0700271 if (prop_name.empty()) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800272 ERROR("invalid zero-length prop name in '%s'\n", src);
273 goto err;
274 }
275
Yabin Cui00ede7d2015-07-24 13:26:04 -0700276 std::string prop_val = property_get(prop_name.c_str());
Yabin Cui74edcea2015-07-24 10:11:05 -0700277 if (prop_val.empty()) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800278 ERROR("property '%s' doesn't exist while expanding '%s'\n",
Yabin Cui00ede7d2015-07-24 13:26:04 -0700279 prop_name.c_str(), src);
Dima Zavina6235ea2011-12-16 14:20:12 -0800280 goto err;
281 }
282
Yabin Cui00ede7d2015-07-24 13:26:04 -0700283 dst->append(prop_val);
Dima Zavina6235ea2011-12-16 14:20:12 -0800284 src_ptr = c;
285 continue;
286 }
287
Dima Zavina6235ea2011-12-16 14:20:12 -0800288 return 0;
Dima Zavina6235ea2011-12-16 14:20:12 -0800289err:
290 return -1;
291}
292
Greg Hackmannd68db712013-11-18 09:44:20 -0800293static void parse_import(struct parse_state *state, int nargs, char **args)
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800294{
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800295 if (nargs != 2) {
296 ERROR("single argument needed for import\n");
297 return;
298 }
299
Yabin Cui00ede7d2015-07-24 13:26:04 -0700300 std::string conf_file;
301 int ret = expand_props(args[1], &conf_file);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800302 if (ret) {
303 ERROR("error while handling import on line '%d' in '%s'\n",
304 state->line, state->filename);
305 return;
306 }
307
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800308 struct import* import = (struct import*) calloc(1, sizeof(struct import));
Yabin Cui00ede7d2015-07-24 13:26:04 -0700309 import->filename = strdup(conf_file.c_str());
310
311 struct listnode *import_list = (listnode*) state->priv;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800312 list_add_tail(import_list, &import->list);
Elliott Hughesda40c002015-03-27 23:20:44 -0700313 INFO("Added '%s' to import list\n", import->filename);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800314}
315
Greg Hackmannd68db712013-11-18 09:44:20 -0800316static void parse_new_section(struct parse_state *state, int kw,
Colin Cross6310a822010-04-20 14:29:05 -0700317 int nargs, char **args)
318{
319 printf("[ %s %s ]\n", args[0],
320 nargs > 1 ? args[1] : "");
321 switch(kw) {
322 case K_service:
323 state->context = parse_service(state, nargs, args);
324 if (state->context) {
325 state->parse_line = parse_line_service;
326 return;
327 }
328 break;
329 case K_on:
330 state->context = parse_action(state, nargs, args);
331 if (state->context) {
332 state->parse_line = parse_line_action;
333 return;
334 }
335 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700336 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800337 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800338 break;
Colin Cross6310a822010-04-20 14:29:05 -0700339 }
340 state->parse_line = parse_line_no_op;
341}
342
Elliott Hughesf682b472015-02-06 12:19:48 -0800343static void parse_config(const char *fn, const std::string& data)
Colin Cross6310a822010-04-20 14:29:05 -0700344{
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800345 struct listnode import_list;
346 struct listnode *node;
Colin Cross6310a822010-04-20 14:29:05 -0700347 char *args[INIT_PARSER_MAXARGS];
Colin Cross6310a822010-04-20 14:29:05 -0700348
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700349 int nargs = 0;
350
351 parse_state state;
Colin Cross6310a822010-04-20 14:29:05 -0700352 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800353 state.line = 0;
Elliott Hughesf682b472015-02-06 12:19:48 -0800354 state.ptr = strdup(data.c_str()); // TODO: fix this code!
Colin Cross6310a822010-04-20 14:29:05 -0700355 state.nexttoken = 0;
356 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800357
358 list_init(&import_list);
359 state.priv = &import_list;
360
Colin Cross6310a822010-04-20 14:29:05 -0700361 for (;;) {
362 switch (next_token(&state)) {
363 case T_EOF:
364 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800365 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700366 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800367 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700368 if (nargs) {
369 int kw = lookup_keyword(args[0]);
370 if (kw_is(kw, SECTION)) {
371 state.parse_line(&state, 0, 0);
372 parse_new_section(&state, kw, nargs, args);
373 } else {
374 state.parse_line(&state, nargs, args);
375 }
376 nargs = 0;
377 }
378 break;
379 case T_TEXT:
380 if (nargs < INIT_PARSER_MAXARGS) {
381 args[nargs++] = state.text;
382 }
383 break;
384 }
385 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800386
387parser_done:
388 list_for_each(node, &import_list) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700389 struct import* import = node_to_item(node, struct import, list);
Lee Campbellf13b1b32015-07-24 16:57:14 -0700390 if (!init_parse_config(import->filename)) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700391 ERROR("could not import file '%s' from '%s': %s\n",
392 import->filename, fn, strerror(errno));
393 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800394 }
Colin Cross6310a822010-04-20 14:29:05 -0700395}
396
Lee Campbellf13b1b32015-07-24 16:57:14 -0700397static bool init_parse_config_file(const char* path) {
398 INFO("Parsing file %s...\n", path);
Elliott Hughesda40c002015-03-27 23:20:44 -0700399 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800400 std::string data;
401 if (!read_file(path, &data)) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700402 return false;
Elliott Hughesf682b472015-02-06 12:19:48 -0800403 }
Colin Cross6310a822010-04-20 14:29:05 -0700404
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700405 data.push_back('\n'); // TODO: fix parse_config.
Elliott Hughesf682b472015-02-06 12:19:48 -0800406 parse_config(path, data);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800407 dump_parser_state();
Elliott Hughesda40c002015-03-27 23:20:44 -0700408
409 NOTICE("(Parsing %s took %.2fs.)\n", path, t.duration());
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700410 return true;
Colin Cross6310a822010-04-20 14:29:05 -0700411}
412
Lee Campbellf13b1b32015-07-24 16:57:14 -0700413static bool init_parse_config_dir(const char* path) {
414 INFO("Parsing directory %s...\n", path);
415 std::unique_ptr<DIR, int(*)(DIR*)> config_dir(opendir(path), closedir);
416 if (!config_dir) {
417 ERROR("Could not import directory '%s'\n", path);
418 return false;
419 }
420 dirent* current_file;
421 while ((current_file = readdir(config_dir.get()))) {
422 std::string current_path =
423 android::base::StringPrintf("%s/%s", path, current_file->d_name);
424 // Ignore directories and only process regular files.
425 if (current_file->d_type == DT_REG) {
426 if (!init_parse_config_file(current_path.c_str())) {
427 ERROR("could not import file '%s'\n", current_path.c_str());
428 }
429 }
430 }
431 return true;
432}
433
434bool init_parse_config(const char* path) {
435 if (is_dir(path)) {
436 return init_parse_config_dir(path);
437 }
438 return init_parse_config_file(path);
439}
440
Colin Cross6310a822010-04-20 14:29:05 -0700441static int valid_name(const char *name)
442{
443 if (strlen(name) > 16) {
444 return 0;
445 }
446 while (*name) {
447 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
448 return 0;
449 }
450 name++;
451 }
452 return 1;
453}
454
455struct service *service_find_by_name(const char *name)
456{
457 struct listnode *node;
458 struct service *svc;
459 list_for_each(node, &service_list) {
460 svc = node_to_item(node, struct service, slist);
461 if (!strcmp(svc->name, name)) {
462 return svc;
463 }
464 }
465 return 0;
466}
467
468struct service *service_find_by_pid(pid_t pid)
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 (svc->pid == pid) {
475 return svc;
476 }
477 }
478 return 0;
479}
480
481struct service *service_find_by_keychord(int keychord_id)
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->keychord_id == keychord_id) {
488 return svc;
489 }
490 }
491 return 0;
492}
493
494void service_for_each(void (*func)(struct service *svc))
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 func(svc);
501 }
502}
503
504void service_for_each_class(const char *classname,
505 void (*func)(struct service *svc))
506{
507 struct listnode *node;
508 struct service *svc;
509 list_for_each(node, &service_list) {
510 svc = node_to_item(node, struct service, slist);
511 if (!strcmp(svc->classname, classname)) {
512 func(svc);
513 }
514 }
515}
516
517void service_for_each_flags(unsigned matchflags,
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 (svc->flags & matchflags) {
525 func(svc);
526 }
527 }
528}
529
530void action_for_each_trigger(const char *trigger,
531 void (*func)(struct action *act))
532{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700533 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700534 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700535 struct trigger *cur_trigger;
536
Colin Cross6310a822010-04-20 14:29:05 -0700537 list_for_each(node, &action_list) {
538 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700539 list_for_each(node2, &act->triggers) {
540 cur_trigger = node_to_item(node2, struct trigger, nlist);
541 if (!strcmp(cur_trigger->name, trigger)) {
542 func(act);
543 }
Colin Cross6310a822010-04-20 14:29:05 -0700544 }
545 }
546}
547
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700548
Colin Cross6310a822010-04-20 14:29:05 -0700549void queue_property_triggers(const char *name, const char *value)
550{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700551 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700552 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700553 struct trigger *cur_trigger;
554 bool match;
555 int name_length;
556
Colin Cross6310a822010-04-20 14:29:05 -0700557 list_for_each(node, &action_list) {
558 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700559 match = !name;
560 list_for_each(node2, &act->triggers) {
561 cur_trigger = node_to_item(node2, struct trigger, nlist);
562 if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) {
563 const char *test = cur_trigger->name + strlen("property:");
564 if (!match) {
565 name_length = strlen(name);
566 if (!strncmp(name, test, name_length) &&
567 test[name_length] == '=' &&
568 (!strcmp(test + name_length + 1, value) ||
569 !strcmp(test + name_length + 1, "*"))) {
570 match = true;
571 continue;
572 }
573 } else {
574 const char* equals = strchr(test, '=');
575 if (equals) {
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700576 int length = equals - test;
577 if (length <= PROP_NAME_MAX) {
Yabin Cui74edcea2015-07-24 10:11:05 -0700578 std::string prop_name(test, length);
579 std::string value = property_get(prop_name.c_str());
Colin Cross6310a822010-04-20 14:29:05 -0700580
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700581 /* does the property exist, and match the trigger value? */
Yabin Cui74edcea2015-07-24 10:11:05 -0700582 if (!value.empty() && (!strcmp(equals + 1, value.c_str()) ||
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700583 !strcmp(equals + 1, "*"))) {
584 continue;
585 }
586 }
587 }
588 }
589 }
590 match = false;
591 break;
592 }
593 if (match) {
594 action_add_queue_tail(act);
Colin Cross6310a822010-04-20 14:29:05 -0700595 }
596 }
597}
598
599void queue_all_property_triggers()
600{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700601 queue_property_triggers(NULL, NULL);
Colin Cross6310a822010-04-20 14:29:05 -0700602}
603
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800604void queue_builtin_action(int (*func)(int nargs, char **args), const char *name)
Colin Cross6310a822010-04-20 14:29:05 -0700605{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800606 action* act = (action*) calloc(1, sizeof(*act));
607 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700608 cur_trigger->name = name;
609 list_init(&act->triggers);
610 list_add_tail(&act->triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700611 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800612 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700613
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800614 command* cmd = (command*) calloc(1, sizeof(*cmd));
Colin Cross6310a822010-04-20 14:29:05 -0700615 cmd->func = func;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800616 cmd->args[0] = const_cast<char*>(name);
Riley Andrews24a3b782014-06-26 13:56:01 -0700617 cmd->nargs = 1;
Colin Cross6310a822010-04-20 14:29:05 -0700618 list_add_tail(&act->commands, &cmd->clist);
619
620 list_add_tail(&action_list, &act->alist);
621 action_add_queue_tail(act);
622}
623
624void action_add_queue_tail(struct action *act)
625{
Colin Crossa5064622013-03-07 13:42:29 -0800626 if (list_empty(&act->qlist)) {
627 list_add_tail(&action_queue, &act->qlist);
628 }
Colin Cross6310a822010-04-20 14:29:05 -0700629}
630
631struct action *action_remove_queue_head(void)
632{
633 if (list_empty(&action_queue)) {
634 return 0;
635 } else {
636 struct listnode *node = list_head(&action_queue);
637 struct action *act = node_to_item(node, struct action, qlist);
638 list_remove(node);
Colin Crossa5064622013-03-07 13:42:29 -0800639 list_init(node);
Colin Cross6310a822010-04-20 14:29:05 -0700640 return act;
641 }
642}
643
644int action_queue_empty()
645{
646 return list_empty(&action_queue);
647}
648
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800649service* make_exec_oneshot_service(int nargs, char** args) {
650 // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS...
Mark Salyzyn17fff892015-06-02 11:11:02 -0700651 // SECLABEL can be a - to denote default
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800652 int command_arg = 1;
653 for (int i = 1; i < nargs; ++i) {
654 if (strcmp(args[i], "--") == 0) {
655 command_arg = i + 1;
656 break;
657 }
658 }
659 if (command_arg > 4 + NR_SVC_SUPP_GIDS) {
660 ERROR("exec called with too many supplementary group ids\n");
661 return NULL;
662 }
663
664 int argc = nargs - command_arg;
665 char** argv = (args + command_arg);
666 if (argc < 1) {
667 ERROR("exec called without command\n");
668 return NULL;
669 }
670
671 service* svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * argc);
672 if (svc == NULL) {
673 ERROR("Couldn't allocate service for exec of '%s': %s", argv[0], strerror(errno));
674 return NULL;
675 }
676
Mark Salyzyn17fff892015-06-02 11:11:02 -0700677 if ((command_arg > 2) && strcmp(args[1], "-")) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800678 svc->seclabel = args[1];
679 }
680 if (command_arg > 3) {
681 svc->uid = decode_uid(args[2]);
682 }
683 if (command_arg > 4) {
684 svc->gid = decode_uid(args[3]);
685 svc->nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */;
686 for (size_t i = 0; i < svc->nr_supp_gids; ++i) {
687 svc->supp_gids[i] = decode_uid(args[4 + i]);
688 }
689 }
690
691 static int exec_count; // Every service needs a unique name.
692 char* name = NULL;
693 asprintf(&name, "exec %d (%s)", exec_count++, argv[0]);
694 if (name == NULL) {
695 ERROR("Couldn't allocate name for exec service '%s'\n", argv[0]);
696 free(svc);
697 return NULL;
698 }
699 svc->name = name;
700 svc->classname = "default";
701 svc->flags = SVC_EXEC | SVC_ONESHOT;
702 svc->nargs = argc;
703 memcpy(svc->args, argv, sizeof(char*) * svc->nargs);
704 svc->args[argc] = NULL;
705 list_add_tail(&service_list, &svc->slist);
706 return svc;
707}
708
Colin Cross6310a822010-04-20 14:29:05 -0700709static void *parse_service(struct parse_state *state, int nargs, char **args)
710{
Colin Cross6310a822010-04-20 14:29:05 -0700711 if (nargs < 3) {
712 parse_error(state, "services must have a name and a program\n");
713 return 0;
714 }
715 if (!valid_name(args[1])) {
716 parse_error(state, "invalid service name '%s'\n", args[1]);
717 return 0;
718 }
719
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800720 service* svc = (service*) service_find_by_name(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700721 if (svc) {
722 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
723 return 0;
724 }
725
726 nargs -= 2;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800727 svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700728 if (!svc) {
729 parse_error(state, "out of memory\n");
730 return 0;
731 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800732 svc->name = strdup(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700733 svc->classname = "default";
734 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800735 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Colin Cross6310a822010-04-20 14:29:05 -0700736 svc->args[nargs] = 0;
737 svc->nargs = nargs;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700738 list_init(&svc->onrestart.triggers);
739 cur_trigger->name = "onrestart";
740 list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700741 list_init(&svc->onrestart.commands);
742 list_add_tail(&service_list, &svc->slist);
743 return svc;
744}
745
746static void parse_line_service(struct parse_state *state, int nargs, char **args)
747{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800748 struct service *svc = (service*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700749 struct command *cmd;
750 int i, kw, kw_nargs;
751
752 if (nargs == 0) {
753 return;
754 }
755
756 svc->ioprio_class = IoSchedClass_NONE;
757
758 kw = lookup_keyword(args[0]);
759 switch (kw) {
Colin Cross6310a822010-04-20 14:29:05 -0700760 case K_class:
761 if (nargs != 2) {
762 parse_error(state, "class option requires a classname\n");
763 } else {
764 svc->classname = args[1];
765 }
766 break;
767 case K_console:
768 svc->flags |= SVC_CONSOLE;
769 break;
770 case K_disabled:
771 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700772 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700773 break;
774 case K_ioprio:
775 if (nargs != 3) {
776 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
777 } else {
778 svc->ioprio_pri = strtoul(args[2], 0, 8);
779
780 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
781 parse_error(state, "priority value must be range 0 - 7\n");
782 break;
783 }
784
785 if (!strcmp(args[1], "rt")) {
786 svc->ioprio_class = IoSchedClass_RT;
787 } else if (!strcmp(args[1], "be")) {
788 svc->ioprio_class = IoSchedClass_BE;
789 } else if (!strcmp(args[1], "idle")) {
790 svc->ioprio_class = IoSchedClass_IDLE;
791 } else {
792 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
793 }
794 }
795 break;
796 case K_group:
797 if (nargs < 2) {
798 parse_error(state, "group option requires a group id\n");
799 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
800 parse_error(state, "group option accepts at most %d supp. groups\n",
801 NR_SVC_SUPP_GIDS);
802 } else {
803 int n;
804 svc->gid = decode_uid(args[1]);
805 for (n = 2; n < nargs; n++) {
806 svc->supp_gids[n-2] = decode_uid(args[n]);
807 }
808 svc->nr_supp_gids = n - 2;
809 }
810 break;
811 case K_keycodes:
812 if (nargs < 2) {
813 parse_error(state, "keycodes option requires atleast one keycode\n");
814 } else {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800815 svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
Colin Cross6310a822010-04-20 14:29:05 -0700816 if (!svc->keycodes) {
817 parse_error(state, "could not allocate keycodes\n");
818 } else {
819 svc->nkeycodes = nargs - 1;
820 for (i = 1; i < nargs; i++) {
821 svc->keycodes[i - 1] = atoi(args[i]);
822 }
823 }
824 }
825 break;
826 case K_oneshot:
827 svc->flags |= SVC_ONESHOT;
828 break;
829 case K_onrestart:
830 nargs--;
831 args++;
832 kw = lookup_keyword(args[0]);
833 if (!kw_is(kw, COMMAND)) {
834 parse_error(state, "invalid command '%s'\n", args[0]);
835 break;
836 }
837 kw_nargs = kw_nargs(kw);
838 if (nargs < kw_nargs) {
839 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
840 kw_nargs > 2 ? "arguments" : "argument");
841 break;
842 }
843
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800844 cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700845 cmd->func = kw_func(kw);
846 cmd->nargs = nargs;
847 memcpy(cmd->args, args, sizeof(char*) * nargs);
848 list_add_tail(&svc->onrestart.commands, &cmd->clist);
849 break;
850 case K_critical:
851 svc->flags |= SVC_CRITICAL;
852 break;
853 case K_setenv: { /* name value */
Gavin.Changc3a46762013-09-28 00:22:11 +0800854 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700855 parse_error(state, "setenv option requires name and value arguments\n");
856 break;
857 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800858 svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
Colin Cross6310a822010-04-20 14:29:05 -0700859 if (!ei) {
860 parse_error(state, "out of memory\n");
861 break;
862 }
863 ei->name = args[1];
864 ei->value = args[2];
865 ei->next = svc->envvars;
866 svc->envvars = ei;
867 break;
868 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400869 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700870 if (nargs < 4) {
871 parse_error(state, "socket option requires name, type, perm arguments\n");
872 break;
873 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400874 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
875 && strcmp(args[2],"seqpacket")) {
876 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700877 break;
878 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800879 socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
Colin Cross6310a822010-04-20 14:29:05 -0700880 if (!si) {
881 parse_error(state, "out of memory\n");
882 break;
883 }
884 si->name = args[1];
885 si->type = args[2];
886 si->perm = strtoul(args[3], 0, 8);
887 if (nargs > 4)
888 si->uid = decode_uid(args[4]);
889 if (nargs > 5)
890 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400891 if (nargs > 6)
892 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700893 si->next = svc->sockets;
894 svc->sockets = si;
895 break;
896 }
897 case K_user:
898 if (nargs != 2) {
899 parse_error(state, "user option requires a user id\n");
900 } else {
901 svc->uid = decode_uid(args[1]);
902 }
903 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500904 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500905 if (nargs != 2) {
906 parse_error(state, "seclabel option requires a label string\n");
907 } else {
908 svc->seclabel = args[1];
909 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500910 break;
Elliott Hughesd62f0602015-06-12 18:02:20 -0700911 case K_writepid:
912 if (nargs < 2) {
913 parse_error(state, "writepid option requires at least one filename\n");
914 break;
915 }
916 svc->writepid_files_ = new std::vector<std::string>;
917 for (int i = 1; i < nargs; ++i) {
918 svc->writepid_files_->push_back(args[i]);
919 }
920 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500921
Colin Cross6310a822010-04-20 14:29:05 -0700922 default:
923 parse_error(state, "invalid option '%s'\n", args[0]);
924 }
925}
926
927static void *parse_action(struct parse_state *state, int nargs, char **args)
928{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700929 struct trigger *cur_trigger;
930 int i;
Colin Cross6310a822010-04-20 14:29:05 -0700931 if (nargs < 2) {
932 parse_error(state, "actions must have a trigger\n");
933 return 0;
934 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700935
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800936 action* act = (action*) calloc(1, sizeof(*act));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700937 list_init(&act->triggers);
938
939 for (i = 1; i < nargs; i++) {
940 if (!(i % 2)) {
941 if (strcmp(args[i], "&&")) {
Tom Cherryae392cf2015-04-13 13:07:04 -0700942 struct listnode *node;
943 struct listnode *node2;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700944 parse_error(state, "& is the only symbol allowed to concatenate actions\n");
Tom Cherryae392cf2015-04-13 13:07:04 -0700945 list_for_each_safe(node, node2, &act->triggers) {
946 struct trigger *trigger = node_to_item(node, struct trigger, nlist);
947 free(trigger);
948 }
949 free(act);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700950 return 0;
951 } else
952 continue;
953 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800954 cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700955 cur_trigger->name = args[i];
956 list_add_tail(&act->triggers, &cur_trigger->nlist);
957 }
958
Colin Cross6310a822010-04-20 14:29:05 -0700959 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800960 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700961 list_add_tail(&action_list, &act->alist);
962 /* XXX add to hash */
963 return act;
964}
965
966static void parse_line_action(struct parse_state* state, int nargs, char **args)
967{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800968 struct action *act = (action*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700969 int kw, n;
970
971 if (nargs == 0) {
972 return;
973 }
974
975 kw = lookup_keyword(args[0]);
976 if (!kw_is(kw, COMMAND)) {
977 parse_error(state, "invalid command '%s'\n", args[0]);
978 return;
979 }
980
981 n = kw_nargs(kw);
982 if (nargs < n) {
983 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
984 n > 2 ? "arguments" : "argument");
985 return;
986 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800987 command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700988 cmd->func = kw_func(kw);
Riley Andrews24a3b782014-06-26 13:56:01 -0700989 cmd->line = state->line;
990 cmd->filename = state->filename;
Colin Cross6310a822010-04-20 14:29:05 -0700991 cmd->nargs = nargs;
992 memcpy(cmd->args, args, sizeof(char*) * nargs);
993 list_add_tail(&act->commands, &cmd->clist);
994}