blob: 6df79f6cc918adac068b4bfcc71a5ad91a14656e [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;
Paul Lawrence948410a2015-07-01 14:40:56 -0700163 if (!strcmp(s, "oad_system_props")) return K_load_system_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;
Paul Crowley749af8c2015-05-28 17:35:06 +0100191 if (!strcmp(s, "etusercryptopolicies")) return K_setusercryptopolicies;
Colin Cross6310a822010-04-20 14:29:05 -0700192 if (!strcmp(s, "ocket")) return K_socket;
193 if (!strcmp(s, "tart")) return K_start;
194 if (!strcmp(s, "top")) return K_stop;
Ken Sumralla76baaa2013-07-09 18:42:09 -0700195 if (!strcmp(s, "wapon_all")) return K_swapon_all;
Colin Cross6310a822010-04-20 14:29:05 -0700196 if (!strcmp(s, "ymlink")) return K_symlink;
197 if (!strcmp(s, "ysclktz")) return K_sysclktz;
198 break;
199 case 't':
200 if (!strcmp(s, "rigger")) return K_trigger;
201 break;
202 case 'u':
203 if (!strcmp(s, "ser")) return K_user;
204 break;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000205 case 'v':
206 if (!strcmp(s, "erity_load_state")) return K_verity_load_state;
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000207 if (!strcmp(s, "erity_update_state")) return K_verity_update_state;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000208 break;
Colin Cross6310a822010-04-20 14:29:05 -0700209 case 'w':
210 if (!strcmp(s, "rite")) return K_write;
Elliott Hughesd62f0602015-06-12 18:02:20 -0700211 if (!strcmp(s, "ritepid")) return K_writepid;
Colin Cross6310a822010-04-20 14:29:05 -0700212 if (!strcmp(s, "ait")) return K_wait;
213 break;
214 }
215 return K_UNKNOWN;
216}
217
Elliott Hughesf682b472015-02-06 12:19:48 -0800218static void parse_line_no_op(struct parse_state*, int, char**) {
Colin Cross6310a822010-04-20 14:29:05 -0700219}
220
Yabin Cui00ede7d2015-07-24 13:26:04 -0700221int expand_props(const char *src, std::string *dst) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800222 const char *src_ptr = src;
Dima Zavina6235ea2011-12-16 14:20:12 -0800223
Yabin Cui00ede7d2015-07-24 13:26:04 -0700224 if (!src || !dst) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800225 return -1;
Yabin Cui00ede7d2015-07-24 13:26:04 -0700226 }
Dima Zavina6235ea2011-12-16 14:20:12 -0800227
Dima Zavina6235ea2011-12-16 14:20:12 -0800228 /* - variables can either be $x.y or ${x.y}, in case they are only part
229 * of the string.
230 * - will accept $$ as a literal $.
231 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
232 * bad things will happen
233 */
Yabin Cui00ede7d2015-07-24 13:26:04 -0700234 while (*src_ptr) {
235 const char *c;
Dima Zavina6235ea2011-12-16 14:20:12 -0800236
237 c = strchr(src_ptr, '$');
238 if (!c) {
Yabin Cui00ede7d2015-07-24 13:26:04 -0700239 dst->append(src_ptr);
Dima Zavina6235ea2011-12-16 14:20:12 -0800240 break;
241 }
242
Yabin Cui00ede7d2015-07-24 13:26:04 -0700243 dst->append(src_ptr, c);
Dima Zavina6235ea2011-12-16 14:20:12 -0800244 c++;
245
246 if (*c == '$') {
Yabin Cui00ede7d2015-07-24 13:26:04 -0700247 dst->push_back(*(c++));
Dima Zavina6235ea2011-12-16 14:20:12 -0800248 src_ptr = c;
Dima Zavina6235ea2011-12-16 14:20:12 -0800249 continue;
250 } else if (*c == '\0') {
251 break;
252 }
253
Yabin Cui00ede7d2015-07-24 13:26:04 -0700254 std::string prop_name;
Dima Zavina6235ea2011-12-16 14:20:12 -0800255 if (*c == '{') {
256 c++;
Yabin Cui00ede7d2015-07-24 13:26:04 -0700257 const char* end = strchr(c, '}');
258 if (!end) {
259 // failed to find closing brace, abort.
260 ERROR("unexpected end of string in '%s', looking for }\n", src);
Dima Zavina6235ea2011-12-16 14:20:12 -0800261 goto err;
262 }
Yabin Cui00ede7d2015-07-24 13:26:04 -0700263 prop_name = std::string(c, end);
264 c = end + 1;
265 } else {
266 prop_name = c;
Dima Zavina6235ea2011-12-16 14:20:12 -0800267 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
Yabin Cui00ede7d2015-07-24 13:26:04 -0700268 c);
269 c += prop_name.size();
Dima Zavina6235ea2011-12-16 14:20:12 -0800270 }
271
Yabin Cui00ede7d2015-07-24 13:26:04 -0700272 if (prop_name.empty()) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800273 ERROR("invalid zero-length prop name in '%s'\n", src);
274 goto err;
275 }
276
Yabin Cui00ede7d2015-07-24 13:26:04 -0700277 std::string prop_val = property_get(prop_name.c_str());
Yabin Cui74edcea2015-07-24 10:11:05 -0700278 if (prop_val.empty()) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800279 ERROR("property '%s' doesn't exist while expanding '%s'\n",
Yabin Cui00ede7d2015-07-24 13:26:04 -0700280 prop_name.c_str(), src);
Dima Zavina6235ea2011-12-16 14:20:12 -0800281 goto err;
282 }
283
Yabin Cui00ede7d2015-07-24 13:26:04 -0700284 dst->append(prop_val);
Dima Zavina6235ea2011-12-16 14:20:12 -0800285 src_ptr = c;
286 continue;
287 }
288
Dima Zavina6235ea2011-12-16 14:20:12 -0800289 return 0;
Dima Zavina6235ea2011-12-16 14:20:12 -0800290err:
291 return -1;
292}
293
Greg Hackmannd68db712013-11-18 09:44:20 -0800294static void parse_import(struct parse_state *state, int nargs, char **args)
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800295{
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800296 if (nargs != 2) {
297 ERROR("single argument needed for import\n");
298 return;
299 }
300
Yabin Cui00ede7d2015-07-24 13:26:04 -0700301 std::string conf_file;
302 int ret = expand_props(args[1], &conf_file);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800303 if (ret) {
304 ERROR("error while handling import on line '%d' in '%s'\n",
305 state->line, state->filename);
306 return;
307 }
308
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800309 struct import* import = (struct import*) calloc(1, sizeof(struct import));
Yabin Cui00ede7d2015-07-24 13:26:04 -0700310 import->filename = strdup(conf_file.c_str());
311
312 struct listnode *import_list = (listnode*) state->priv;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800313 list_add_tail(import_list, &import->list);
Elliott Hughesda40c002015-03-27 23:20:44 -0700314 INFO("Added '%s' to import list\n", import->filename);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800315}
316
Greg Hackmannd68db712013-11-18 09:44:20 -0800317static void parse_new_section(struct parse_state *state, int kw,
Colin Cross6310a822010-04-20 14:29:05 -0700318 int nargs, char **args)
319{
320 printf("[ %s %s ]\n", args[0],
321 nargs > 1 ? args[1] : "");
322 switch(kw) {
323 case K_service:
324 state->context = parse_service(state, nargs, args);
325 if (state->context) {
326 state->parse_line = parse_line_service;
327 return;
328 }
329 break;
330 case K_on:
331 state->context = parse_action(state, nargs, args);
332 if (state->context) {
333 state->parse_line = parse_line_action;
334 return;
335 }
336 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700337 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800338 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800339 break;
Colin Cross6310a822010-04-20 14:29:05 -0700340 }
341 state->parse_line = parse_line_no_op;
342}
343
Elliott Hughesf682b472015-02-06 12:19:48 -0800344static void parse_config(const char *fn, const std::string& data)
Colin Cross6310a822010-04-20 14:29:05 -0700345{
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800346 struct listnode import_list;
347 struct listnode *node;
Colin Cross6310a822010-04-20 14:29:05 -0700348 char *args[INIT_PARSER_MAXARGS];
Colin Cross6310a822010-04-20 14:29:05 -0700349
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700350 int nargs = 0;
351
352 parse_state state;
Colin Cross6310a822010-04-20 14:29:05 -0700353 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800354 state.line = 0;
Elliott Hughesf682b472015-02-06 12:19:48 -0800355 state.ptr = strdup(data.c_str()); // TODO: fix this code!
Colin Cross6310a822010-04-20 14:29:05 -0700356 state.nexttoken = 0;
357 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800358
359 list_init(&import_list);
360 state.priv = &import_list;
361
Colin Cross6310a822010-04-20 14:29:05 -0700362 for (;;) {
363 switch (next_token(&state)) {
364 case T_EOF:
365 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800366 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700367 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800368 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700369 if (nargs) {
370 int kw = lookup_keyword(args[0]);
371 if (kw_is(kw, SECTION)) {
372 state.parse_line(&state, 0, 0);
373 parse_new_section(&state, kw, nargs, args);
374 } else {
375 state.parse_line(&state, nargs, args);
376 }
377 nargs = 0;
378 }
379 break;
380 case T_TEXT:
381 if (nargs < INIT_PARSER_MAXARGS) {
382 args[nargs++] = state.text;
383 }
384 break;
385 }
386 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800387
388parser_done:
389 list_for_each(node, &import_list) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700390 struct import* import = node_to_item(node, struct import, list);
Lee Campbellf13b1b32015-07-24 16:57:14 -0700391 if (!init_parse_config(import->filename)) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700392 ERROR("could not import file '%s' from '%s': %s\n",
393 import->filename, fn, strerror(errno));
394 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800395 }
Colin Cross6310a822010-04-20 14:29:05 -0700396}
397
Lee Campbellf13b1b32015-07-24 16:57:14 -0700398static bool init_parse_config_file(const char* path) {
399 INFO("Parsing file %s...\n", path);
Elliott Hughesda40c002015-03-27 23:20:44 -0700400 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800401 std::string data;
402 if (!read_file(path, &data)) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700403 return false;
Elliott Hughesf682b472015-02-06 12:19:48 -0800404 }
Colin Cross6310a822010-04-20 14:29:05 -0700405
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700406 data.push_back('\n'); // TODO: fix parse_config.
Elliott Hughesf682b472015-02-06 12:19:48 -0800407 parse_config(path, data);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800408 dump_parser_state();
Elliott Hughesda40c002015-03-27 23:20:44 -0700409
410 NOTICE("(Parsing %s took %.2fs.)\n", path, t.duration());
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700411 return true;
Colin Cross6310a822010-04-20 14:29:05 -0700412}
413
Lee Campbellf13b1b32015-07-24 16:57:14 -0700414static bool init_parse_config_dir(const char* path) {
415 INFO("Parsing directory %s...\n", path);
416 std::unique_ptr<DIR, int(*)(DIR*)> config_dir(opendir(path), closedir);
417 if (!config_dir) {
418 ERROR("Could not import directory '%s'\n", path);
419 return false;
420 }
421 dirent* current_file;
422 while ((current_file = readdir(config_dir.get()))) {
423 std::string current_path =
424 android::base::StringPrintf("%s/%s", path, current_file->d_name);
425 // Ignore directories and only process regular files.
426 if (current_file->d_type == DT_REG) {
427 if (!init_parse_config_file(current_path.c_str())) {
428 ERROR("could not import file '%s'\n", current_path.c_str());
429 }
430 }
431 }
432 return true;
433}
434
435bool init_parse_config(const char* path) {
436 if (is_dir(path)) {
437 return init_parse_config_dir(path);
438 }
439 return init_parse_config_file(path);
440}
441
Colin Cross6310a822010-04-20 14:29:05 -0700442static int valid_name(const char *name)
443{
444 if (strlen(name) > 16) {
445 return 0;
446 }
447 while (*name) {
448 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
449 return 0;
450 }
451 name++;
452 }
453 return 1;
454}
455
456struct service *service_find_by_name(const char *name)
457{
458 struct listnode *node;
459 struct service *svc;
460 list_for_each(node, &service_list) {
461 svc = node_to_item(node, struct service, slist);
462 if (!strcmp(svc->name, name)) {
463 return svc;
464 }
465 }
466 return 0;
467}
468
469struct service *service_find_by_pid(pid_t pid)
470{
471 struct listnode *node;
472 struct service *svc;
473 list_for_each(node, &service_list) {
474 svc = node_to_item(node, struct service, slist);
475 if (svc->pid == pid) {
476 return svc;
477 }
478 }
479 return 0;
480}
481
482struct service *service_find_by_keychord(int keychord_id)
483{
484 struct listnode *node;
485 struct service *svc;
486 list_for_each(node, &service_list) {
487 svc = node_to_item(node, struct service, slist);
488 if (svc->keychord_id == keychord_id) {
489 return svc;
490 }
491 }
492 return 0;
493}
494
495void service_for_each(void (*func)(struct service *svc))
496{
497 struct listnode *node;
498 struct service *svc;
499 list_for_each(node, &service_list) {
500 svc = node_to_item(node, struct service, slist);
501 func(svc);
502 }
503}
504
505void service_for_each_class(const char *classname,
506 void (*func)(struct service *svc))
507{
508 struct listnode *node;
509 struct service *svc;
510 list_for_each(node, &service_list) {
511 svc = node_to_item(node, struct service, slist);
512 if (!strcmp(svc->classname, classname)) {
513 func(svc);
514 }
515 }
516}
517
518void service_for_each_flags(unsigned matchflags,
519 void (*func)(struct service *svc))
520{
521 struct listnode *node;
522 struct service *svc;
523 list_for_each(node, &service_list) {
524 svc = node_to_item(node, struct service, slist);
525 if (svc->flags & matchflags) {
526 func(svc);
527 }
528 }
529}
530
531void action_for_each_trigger(const char *trigger,
532 void (*func)(struct action *act))
533{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700534 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700535 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700536 struct trigger *cur_trigger;
537
Colin Cross6310a822010-04-20 14:29:05 -0700538 list_for_each(node, &action_list) {
539 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700540 list_for_each(node2, &act->triggers) {
541 cur_trigger = node_to_item(node2, struct trigger, nlist);
542 if (!strcmp(cur_trigger->name, trigger)) {
543 func(act);
544 }
Colin Cross6310a822010-04-20 14:29:05 -0700545 }
546 }
547}
548
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700549
Colin Cross6310a822010-04-20 14:29:05 -0700550void queue_property_triggers(const char *name, const char *value)
551{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700552 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700553 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700554 struct trigger *cur_trigger;
555 bool match;
556 int name_length;
557
Colin Cross6310a822010-04-20 14:29:05 -0700558 list_for_each(node, &action_list) {
559 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700560 match = !name;
561 list_for_each(node2, &act->triggers) {
562 cur_trigger = node_to_item(node2, struct trigger, nlist);
563 if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) {
564 const char *test = cur_trigger->name + strlen("property:");
565 if (!match) {
566 name_length = strlen(name);
567 if (!strncmp(name, test, name_length) &&
568 test[name_length] == '=' &&
569 (!strcmp(test + name_length + 1, value) ||
570 !strcmp(test + name_length + 1, "*"))) {
571 match = true;
572 continue;
573 }
574 } else {
575 const char* equals = strchr(test, '=');
576 if (equals) {
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700577 int length = equals - test;
578 if (length <= PROP_NAME_MAX) {
Yabin Cui74edcea2015-07-24 10:11:05 -0700579 std::string prop_name(test, length);
580 std::string value = property_get(prop_name.c_str());
Colin Cross6310a822010-04-20 14:29:05 -0700581
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700582 /* does the property exist, and match the trigger value? */
Yabin Cui74edcea2015-07-24 10:11:05 -0700583 if (!value.empty() && (!strcmp(equals + 1, value.c_str()) ||
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700584 !strcmp(equals + 1, "*"))) {
585 continue;
586 }
587 }
588 }
589 }
590 }
591 match = false;
592 break;
593 }
594 if (match) {
595 action_add_queue_tail(act);
Colin Cross6310a822010-04-20 14:29:05 -0700596 }
597 }
598}
599
600void queue_all_property_triggers()
601{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700602 queue_property_triggers(NULL, NULL);
Colin Cross6310a822010-04-20 14:29:05 -0700603}
604
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800605void queue_builtin_action(int (*func)(int nargs, char **args), const char *name)
Colin Cross6310a822010-04-20 14:29:05 -0700606{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800607 action* act = (action*) calloc(1, sizeof(*act));
608 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700609 cur_trigger->name = name;
610 list_init(&act->triggers);
611 list_add_tail(&act->triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700612 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800613 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700614
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800615 command* cmd = (command*) calloc(1, sizeof(*cmd));
Colin Cross6310a822010-04-20 14:29:05 -0700616 cmd->func = func;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800617 cmd->args[0] = const_cast<char*>(name);
Riley Andrews24a3b782014-06-26 13:56:01 -0700618 cmd->nargs = 1;
Colin Cross6310a822010-04-20 14:29:05 -0700619 list_add_tail(&act->commands, &cmd->clist);
620
621 list_add_tail(&action_list, &act->alist);
622 action_add_queue_tail(act);
623}
624
625void action_add_queue_tail(struct action *act)
626{
Colin Crossa5064622013-03-07 13:42:29 -0800627 if (list_empty(&act->qlist)) {
628 list_add_tail(&action_queue, &act->qlist);
629 }
Colin Cross6310a822010-04-20 14:29:05 -0700630}
631
632struct action *action_remove_queue_head(void)
633{
634 if (list_empty(&action_queue)) {
635 return 0;
636 } else {
637 struct listnode *node = list_head(&action_queue);
638 struct action *act = node_to_item(node, struct action, qlist);
639 list_remove(node);
Colin Crossa5064622013-03-07 13:42:29 -0800640 list_init(node);
Colin Cross6310a822010-04-20 14:29:05 -0700641 return act;
642 }
643}
644
645int action_queue_empty()
646{
647 return list_empty(&action_queue);
648}
649
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800650service* make_exec_oneshot_service(int nargs, char** args) {
651 // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS...
Mark Salyzyn17fff892015-06-02 11:11:02 -0700652 // SECLABEL can be a - to denote default
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800653 int command_arg = 1;
654 for (int i = 1; i < nargs; ++i) {
655 if (strcmp(args[i], "--") == 0) {
656 command_arg = i + 1;
657 break;
658 }
659 }
660 if (command_arg > 4 + NR_SVC_SUPP_GIDS) {
661 ERROR("exec called with too many supplementary group ids\n");
662 return NULL;
663 }
664
665 int argc = nargs - command_arg;
666 char** argv = (args + command_arg);
667 if (argc < 1) {
668 ERROR("exec called without command\n");
669 return NULL;
670 }
671
672 service* svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * argc);
673 if (svc == NULL) {
674 ERROR("Couldn't allocate service for exec of '%s': %s", argv[0], strerror(errno));
675 return NULL;
676 }
677
Mark Salyzyn17fff892015-06-02 11:11:02 -0700678 if ((command_arg > 2) && strcmp(args[1], "-")) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800679 svc->seclabel = args[1];
680 }
681 if (command_arg > 3) {
682 svc->uid = decode_uid(args[2]);
683 }
684 if (command_arg > 4) {
685 svc->gid = decode_uid(args[3]);
686 svc->nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */;
687 for (size_t i = 0; i < svc->nr_supp_gids; ++i) {
688 svc->supp_gids[i] = decode_uid(args[4 + i]);
689 }
690 }
691
692 static int exec_count; // Every service needs a unique name.
693 char* name = NULL;
694 asprintf(&name, "exec %d (%s)", exec_count++, argv[0]);
695 if (name == NULL) {
696 ERROR("Couldn't allocate name for exec service '%s'\n", argv[0]);
697 free(svc);
698 return NULL;
699 }
700 svc->name = name;
701 svc->classname = "default";
702 svc->flags = SVC_EXEC | SVC_ONESHOT;
703 svc->nargs = argc;
704 memcpy(svc->args, argv, sizeof(char*) * svc->nargs);
705 svc->args[argc] = NULL;
706 list_add_tail(&service_list, &svc->slist);
707 return svc;
708}
709
Colin Cross6310a822010-04-20 14:29:05 -0700710static void *parse_service(struct parse_state *state, int nargs, char **args)
711{
Colin Cross6310a822010-04-20 14:29:05 -0700712 if (nargs < 3) {
713 parse_error(state, "services must have a name and a program\n");
714 return 0;
715 }
716 if (!valid_name(args[1])) {
717 parse_error(state, "invalid service name '%s'\n", args[1]);
718 return 0;
719 }
720
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800721 service* svc = (service*) service_find_by_name(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700722 if (svc) {
723 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
724 return 0;
725 }
726
727 nargs -= 2;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800728 svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700729 if (!svc) {
730 parse_error(state, "out of memory\n");
731 return 0;
732 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800733 svc->name = strdup(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700734 svc->classname = "default";
735 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800736 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Colin Cross6310a822010-04-20 14:29:05 -0700737 svc->args[nargs] = 0;
738 svc->nargs = nargs;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700739 list_init(&svc->onrestart.triggers);
740 cur_trigger->name = "onrestart";
741 list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700742 list_init(&svc->onrestart.commands);
743 list_add_tail(&service_list, &svc->slist);
744 return svc;
745}
746
747static void parse_line_service(struct parse_state *state, int nargs, char **args)
748{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800749 struct service *svc = (service*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700750 struct command *cmd;
751 int i, kw, kw_nargs;
752
753 if (nargs == 0) {
754 return;
755 }
756
757 svc->ioprio_class = IoSchedClass_NONE;
758
759 kw = lookup_keyword(args[0]);
760 switch (kw) {
Colin Cross6310a822010-04-20 14:29:05 -0700761 case K_class:
762 if (nargs != 2) {
763 parse_error(state, "class option requires a classname\n");
764 } else {
765 svc->classname = args[1];
766 }
767 break;
768 case K_console:
769 svc->flags |= SVC_CONSOLE;
770 break;
771 case K_disabled:
772 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700773 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700774 break;
775 case K_ioprio:
776 if (nargs != 3) {
777 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
778 } else {
779 svc->ioprio_pri = strtoul(args[2], 0, 8);
780
781 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
782 parse_error(state, "priority value must be range 0 - 7\n");
783 break;
784 }
785
786 if (!strcmp(args[1], "rt")) {
787 svc->ioprio_class = IoSchedClass_RT;
788 } else if (!strcmp(args[1], "be")) {
789 svc->ioprio_class = IoSchedClass_BE;
790 } else if (!strcmp(args[1], "idle")) {
791 svc->ioprio_class = IoSchedClass_IDLE;
792 } else {
793 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
794 }
795 }
796 break;
797 case K_group:
798 if (nargs < 2) {
799 parse_error(state, "group option requires a group id\n");
800 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
801 parse_error(state, "group option accepts at most %d supp. groups\n",
802 NR_SVC_SUPP_GIDS);
803 } else {
804 int n;
805 svc->gid = decode_uid(args[1]);
806 for (n = 2; n < nargs; n++) {
807 svc->supp_gids[n-2] = decode_uid(args[n]);
808 }
809 svc->nr_supp_gids = n - 2;
810 }
811 break;
812 case K_keycodes:
813 if (nargs < 2) {
814 parse_error(state, "keycodes option requires atleast one keycode\n");
815 } else {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800816 svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
Colin Cross6310a822010-04-20 14:29:05 -0700817 if (!svc->keycodes) {
818 parse_error(state, "could not allocate keycodes\n");
819 } else {
820 svc->nkeycodes = nargs - 1;
821 for (i = 1; i < nargs; i++) {
822 svc->keycodes[i - 1] = atoi(args[i]);
823 }
824 }
825 }
826 break;
827 case K_oneshot:
828 svc->flags |= SVC_ONESHOT;
829 break;
830 case K_onrestart:
831 nargs--;
832 args++;
833 kw = lookup_keyword(args[0]);
834 if (!kw_is(kw, COMMAND)) {
835 parse_error(state, "invalid command '%s'\n", args[0]);
836 break;
837 }
838 kw_nargs = kw_nargs(kw);
839 if (nargs < kw_nargs) {
840 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
841 kw_nargs > 2 ? "arguments" : "argument");
842 break;
843 }
844
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800845 cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700846 cmd->func = kw_func(kw);
847 cmd->nargs = nargs;
848 memcpy(cmd->args, args, sizeof(char*) * nargs);
849 list_add_tail(&svc->onrestart.commands, &cmd->clist);
850 break;
851 case K_critical:
852 svc->flags |= SVC_CRITICAL;
853 break;
854 case K_setenv: { /* name value */
Gavin.Changc3a46762013-09-28 00:22:11 +0800855 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700856 parse_error(state, "setenv option requires name and value arguments\n");
857 break;
858 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800859 svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
Colin Cross6310a822010-04-20 14:29:05 -0700860 if (!ei) {
861 parse_error(state, "out of memory\n");
862 break;
863 }
864 ei->name = args[1];
865 ei->value = args[2];
866 ei->next = svc->envvars;
867 svc->envvars = ei;
868 break;
869 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400870 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700871 if (nargs < 4) {
872 parse_error(state, "socket option requires name, type, perm arguments\n");
873 break;
874 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400875 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
876 && strcmp(args[2],"seqpacket")) {
877 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700878 break;
879 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800880 socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
Colin Cross6310a822010-04-20 14:29:05 -0700881 if (!si) {
882 parse_error(state, "out of memory\n");
883 break;
884 }
885 si->name = args[1];
886 si->type = args[2];
887 si->perm = strtoul(args[3], 0, 8);
888 if (nargs > 4)
889 si->uid = decode_uid(args[4]);
890 if (nargs > 5)
891 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400892 if (nargs > 6)
893 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700894 si->next = svc->sockets;
895 svc->sockets = si;
896 break;
897 }
898 case K_user:
899 if (nargs != 2) {
900 parse_error(state, "user option requires a user id\n");
901 } else {
902 svc->uid = decode_uid(args[1]);
903 }
904 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500905 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500906 if (nargs != 2) {
907 parse_error(state, "seclabel option requires a label string\n");
908 } else {
909 svc->seclabel = args[1];
910 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500911 break;
Elliott Hughesd62f0602015-06-12 18:02:20 -0700912 case K_writepid:
913 if (nargs < 2) {
914 parse_error(state, "writepid option requires at least one filename\n");
915 break;
916 }
917 svc->writepid_files_ = new std::vector<std::string>;
918 for (int i = 1; i < nargs; ++i) {
919 svc->writepid_files_->push_back(args[i]);
920 }
921 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500922
Colin Cross6310a822010-04-20 14:29:05 -0700923 default:
924 parse_error(state, "invalid option '%s'\n", args[0]);
925 }
926}
927
928static void *parse_action(struct parse_state *state, int nargs, char **args)
929{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700930 struct trigger *cur_trigger;
931 int i;
Colin Cross6310a822010-04-20 14:29:05 -0700932 if (nargs < 2) {
933 parse_error(state, "actions must have a trigger\n");
934 return 0;
935 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700936
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800937 action* act = (action*) calloc(1, sizeof(*act));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700938 list_init(&act->triggers);
939
940 for (i = 1; i < nargs; i++) {
941 if (!(i % 2)) {
942 if (strcmp(args[i], "&&")) {
Tom Cherryae392cf2015-04-13 13:07:04 -0700943 struct listnode *node;
944 struct listnode *node2;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700945 parse_error(state, "& is the only symbol allowed to concatenate actions\n");
Tom Cherryae392cf2015-04-13 13:07:04 -0700946 list_for_each_safe(node, node2, &act->triggers) {
947 struct trigger *trigger = node_to_item(node, struct trigger, nlist);
948 free(trigger);
949 }
950 free(act);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700951 return 0;
952 } else
953 continue;
954 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800955 cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700956 cur_trigger->name = args[i];
957 list_add_tail(&act->triggers, &cur_trigger->nlist);
958 }
959
Colin Cross6310a822010-04-20 14:29:05 -0700960 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800961 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700962 list_add_tail(&action_list, &act->alist);
963 /* XXX add to hash */
964 return act;
965}
966
967static void parse_line_action(struct parse_state* state, int nargs, char **args)
968{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800969 struct action *act = (action*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700970 int kw, n;
971
972 if (nargs == 0) {
973 return;
974 }
975
976 kw = lookup_keyword(args[0]);
977 if (!kw_is(kw, COMMAND)) {
978 parse_error(state, "invalid command '%s'\n", args[0]);
979 return;
980 }
981
982 n = kw_nargs(kw);
983 if (nargs < n) {
984 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
985 n > 2 ? "arguments" : "argument");
986 return;
987 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800988 command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700989 cmd->func = kw_func(kw);
Riley Andrews24a3b782014-06-26 13:56:01 -0700990 cmd->line = state->line;
991 cmd->filename = state->filename;
Colin Cross6310a822010-04-20 14:29:05 -0700992 cmd->nargs = nargs;
993 memcpy(cmd->args, args, sizeof(char*) * nargs);
994 list_add_tail(&act->commands, &cmd->clist);
995}