blob: ab0dbc3e65ea9815c08f6b6c4fdbbf06627c35fa [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
Tom Cherryfa0c21c2015-07-23 17:53:11 -070029#include "action.h"
Colin Cross6310a822010-04-20 14:29:05 -070030#include "init.h"
31#include "parser.h"
32#include "init_parser.h"
33#include "log.h"
Colin Cross6310a822010-04-20 14:29:05 -070034#include "property_service.h"
35#include "util.h"
36
Lee Campbellf13b1b32015-07-24 16:57:14 -070037#include <base/stringprintf.h>
Colin Cross6310a822010-04-20 14:29:05 -070038#include <cutils/iosched_policy.h>
Dima Zavinda04c522011-09-01 17:09:44 -070039#include <cutils/list.h>
Colin Cross6310a822010-04-20 14:29:05 -070040
Colin Cross6310a822010-04-20 14:29:05 -070041static list_declare(service_list);
Colin Cross6310a822010-04-20 14:29:05 -070042
Dima Zavin78a1b1f2011-12-20 12:53:48 -080043struct import {
44 struct listnode list;
45 const char *filename;
46};
47
Colin Cross6310a822010-04-20 14:29:05 -070048static void *parse_service(struct parse_state *state, int nargs, char **args);
49static void parse_line_service(struct parse_state *state, int nargs, char **args);
50
51static void *parse_action(struct parse_state *state, int nargs, char **args);
52static void parse_line_action(struct parse_state *state, int nargs, char **args);
53
54#define SECTION 0x01
55#define COMMAND 0x02
56#define OPTION 0x04
57
58#include "keywords.h"
59
60#define KEYWORD(symbol, flags, nargs, func) \
61 [ K_##symbol ] = { #symbol, func, nargs + 1, flags, },
62
Greg Hackmannd68db712013-11-18 09:44:20 -080063static struct {
Colin Cross6310a822010-04-20 14:29:05 -070064 const char *name;
65 int (*func)(int nargs, char **args);
66 unsigned char nargs;
67 unsigned char flags;
68} keyword_info[KEYWORD_COUNT] = {
69 [ K_UNKNOWN ] = { "unknown", 0, 0, 0 },
70#include "keywords.h"
71};
72#undef KEYWORD
73
74#define kw_is(kw, type) (keyword_info[kw].flags & (type))
75#define kw_name(kw) (keyword_info[kw].name)
76#define kw_func(kw) (keyword_info[kw].func)
77#define kw_nargs(kw) (keyword_info[kw].nargs)
78
Elliott Hughesc0e919c2015-02-04 14:46:36 -080079void dump_parser_state() {
80 if (false) {
81 struct listnode* node;
82 list_for_each(node, &service_list) {
83 service* svc = node_to_item(node, struct service, slist);
84 INFO("service %s\n", svc->name);
85 INFO(" class '%s'\n", svc->classname);
86 INFO(" exec");
87 for (int n = 0; n < svc->nargs; n++) {
88 INFO(" '%s'", svc->args[n]);
89 }
90 INFO("\n");
91 for (socketinfo* si = svc->sockets; si; si = si->next) {
92 INFO(" socket %s %s 0%o\n", si->name, si->type, si->perm);
93 }
94 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -070095 ActionManager::GetInstance().DumpState();
Elliott Hughesc0e919c2015-02-04 14:46:36 -080096 }
97}
98
Greg Hackmannd68db712013-11-18 09:44:20 -080099static int lookup_keyword(const char *s)
Colin Cross6310a822010-04-20 14:29:05 -0700100{
101 switch (*s++) {
Yongqin Liua197ff12014-12-05 13:45:02 +0800102 case 'b':
103 if (!strcmp(s, "ootchart_init")) return K_bootchart_init;
Mark Salyzyn7a3d66c2015-03-24 07:29:22 -0700104 break;
Colin Cross6310a822010-04-20 14:29:05 -0700105 case 'c':
Elliott Hughesf682b472015-02-06 12:19:48 -0800106 if (!strcmp(s, "opy")) return K_copy;
Colin Cross6310a822010-04-20 14:29:05 -0700107 if (!strcmp(s, "lass")) return K_class;
108 if (!strcmp(s, "lass_start")) return K_class_start;
109 if (!strcmp(s, "lass_stop")) return K_class_stop;
Ken Sumrall752923c2010-12-03 16:33:31 -0800110 if (!strcmp(s, "lass_reset")) return K_class_reset;
Colin Cross6310a822010-04-20 14:29:05 -0700111 if (!strcmp(s, "onsole")) return K_console;
112 if (!strcmp(s, "hown")) return K_chown;
113 if (!strcmp(s, "hmod")) return K_chmod;
114 if (!strcmp(s, "ritical")) return K_critical;
115 break;
116 case 'd':
117 if (!strcmp(s, "isabled")) return K_disabled;
118 if (!strcmp(s, "omainname")) return K_domainname;
119 break;
120 case 'e':
JP Abgrall3beec7e2014-05-02 21:14:29 -0700121 if (!strcmp(s, "nable")) return K_enable;
Colin Cross6310a822010-04-20 14:29:05 -0700122 if (!strcmp(s, "xec")) return K_exec;
123 if (!strcmp(s, "xport")) return K_export;
124 break;
125 case 'g':
126 if (!strcmp(s, "roup")) return K_group;
127 break;
128 case 'h':
129 if (!strcmp(s, "ostname")) return K_hostname;
130 break;
131 case 'i':
132 if (!strcmp(s, "oprio")) return K_ioprio;
133 if (!strcmp(s, "fup")) return K_ifup;
134 if (!strcmp(s, "nsmod")) return K_insmod;
135 if (!strcmp(s, "mport")) return K_import;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000136 if (!strcmp(s, "nstallkey")) return K_installkey;
Colin Cross6310a822010-04-20 14:29:05 -0700137 break;
138 case 'k':
139 if (!strcmp(s, "eycodes")) return K_keycodes;
140 break;
141 case 'l':
142 if (!strcmp(s, "oglevel")) return K_loglevel;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800143 if (!strcmp(s, "oad_persist_props")) return K_load_persist_props;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700144 if (!strcmp(s, "oad_all_props")) return K_load_all_props;
Colin Cross6310a822010-04-20 14:29:05 -0700145 break;
146 case 'm':
147 if (!strcmp(s, "kdir")) return K_mkdir;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700148 if (!strcmp(s, "ount_all")) return K_mount_all;
Colin Cross6310a822010-04-20 14:29:05 -0700149 if (!strcmp(s, "ount")) return K_mount;
150 break;
151 case 'o':
152 if (!strcmp(s, "n")) return K_on;
153 if (!strcmp(s, "neshot")) return K_oneshot;
154 if (!strcmp(s, "nrestart")) return K_onrestart;
155 break;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700156 case 'p':
157 if (!strcmp(s, "owerctl")) return K_powerctl;
Elliott Hughesf682b472015-02-06 12:19:48 -0800158 break;
Colin Cross6310a822010-04-20 14:29:05 -0700159 case 'r':
160 if (!strcmp(s, "estart")) return K_restart;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500161 if (!strcmp(s, "estorecon")) return K_restorecon;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400162 if (!strcmp(s, "estorecon_recursive")) return K_restorecon_recursive;
Ken Sumrall203bad52011-01-18 17:37:41 -0800163 if (!strcmp(s, "mdir")) return K_rmdir;
164 if (!strcmp(s, "m")) return K_rm;
Colin Cross6310a822010-04-20 14:29:05 -0700165 break;
166 case 's':
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500167 if (!strcmp(s, "eclabel")) return K_seclabel;
Colin Cross6310a822010-04-20 14:29:05 -0700168 if (!strcmp(s, "ervice")) return K_service;
169 if (!strcmp(s, "etenv")) return K_setenv;
Colin Cross6310a822010-04-20 14:29:05 -0700170 if (!strcmp(s, "etprop")) return K_setprop;
171 if (!strcmp(s, "etrlimit")) return K_setrlimit;
172 if (!strcmp(s, "ocket")) return K_socket;
173 if (!strcmp(s, "tart")) return K_start;
174 if (!strcmp(s, "top")) return K_stop;
Ken Sumralla76baaa2013-07-09 18:42:09 -0700175 if (!strcmp(s, "wapon_all")) return K_swapon_all;
Colin Cross6310a822010-04-20 14:29:05 -0700176 if (!strcmp(s, "ymlink")) return K_symlink;
177 if (!strcmp(s, "ysclktz")) return K_sysclktz;
178 break;
179 case 't':
180 if (!strcmp(s, "rigger")) return K_trigger;
181 break;
182 case 'u':
183 if (!strcmp(s, "ser")) return K_user;
184 break;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000185 case 'v':
186 if (!strcmp(s, "erity_load_state")) return K_verity_load_state;
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000187 if (!strcmp(s, "erity_update_state")) return K_verity_update_state;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000188 break;
Colin Cross6310a822010-04-20 14:29:05 -0700189 case 'w':
190 if (!strcmp(s, "rite")) return K_write;
Elliott Hughesd62f0602015-06-12 18:02:20 -0700191 if (!strcmp(s, "ritepid")) return K_writepid;
Colin Cross6310a822010-04-20 14:29:05 -0700192 if (!strcmp(s, "ait")) return K_wait;
193 break;
194 }
195 return K_UNKNOWN;
196}
197
Elliott Hughesf682b472015-02-06 12:19:48 -0800198static void parse_line_no_op(struct parse_state*, int, char**) {
Colin Cross6310a822010-04-20 14:29:05 -0700199}
200
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700201int expand_props(const std::string& src, std::string* dst) {
202 const char *src_ptr = src.c_str();
Dima Zavina6235ea2011-12-16 14:20:12 -0800203
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700204 if (!dst) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800205 return -1;
Yabin Cui00ede7d2015-07-24 13:26:04 -0700206 }
Dima Zavina6235ea2011-12-16 14:20:12 -0800207
Dima Zavina6235ea2011-12-16 14:20:12 -0800208 /* - variables can either be $x.y or ${x.y}, in case they are only part
209 * of the string.
210 * - will accept $$ as a literal $.
211 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
212 * bad things will happen
213 */
Yabin Cui00ede7d2015-07-24 13:26:04 -0700214 while (*src_ptr) {
215 const char *c;
Dima Zavina6235ea2011-12-16 14:20:12 -0800216
217 c = strchr(src_ptr, '$');
218 if (!c) {
Yabin Cui00ede7d2015-07-24 13:26:04 -0700219 dst->append(src_ptr);
Dima Zavina6235ea2011-12-16 14:20:12 -0800220 break;
221 }
222
Yabin Cui00ede7d2015-07-24 13:26:04 -0700223 dst->append(src_ptr, c);
Dima Zavina6235ea2011-12-16 14:20:12 -0800224 c++;
225
226 if (*c == '$') {
Yabin Cui00ede7d2015-07-24 13:26:04 -0700227 dst->push_back(*(c++));
Dima Zavina6235ea2011-12-16 14:20:12 -0800228 src_ptr = c;
Dima Zavina6235ea2011-12-16 14:20:12 -0800229 continue;
230 } else if (*c == '\0') {
231 break;
232 }
233
Yabin Cui00ede7d2015-07-24 13:26:04 -0700234 std::string prop_name;
Dima Zavina6235ea2011-12-16 14:20:12 -0800235 if (*c == '{') {
236 c++;
Yabin Cui00ede7d2015-07-24 13:26:04 -0700237 const char* end = strchr(c, '}');
238 if (!end) {
239 // failed to find closing brace, abort.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700240 ERROR("unexpected end of string in '%s', looking for }\n", src.c_str());
Dima Zavina6235ea2011-12-16 14:20:12 -0800241 goto err;
242 }
Yabin Cui00ede7d2015-07-24 13:26:04 -0700243 prop_name = std::string(c, end);
244 c = end + 1;
245 } else {
246 prop_name = c;
Dima Zavina6235ea2011-12-16 14:20:12 -0800247 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
Yabin Cui00ede7d2015-07-24 13:26:04 -0700248 c);
249 c += prop_name.size();
Dima Zavina6235ea2011-12-16 14:20:12 -0800250 }
251
Yabin Cui00ede7d2015-07-24 13:26:04 -0700252 if (prop_name.empty()) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700253 ERROR("invalid zero-length prop name in '%s'\n", src.c_str());
Dima Zavina6235ea2011-12-16 14:20:12 -0800254 goto err;
255 }
256
Yabin Cui00ede7d2015-07-24 13:26:04 -0700257 std::string prop_val = property_get(prop_name.c_str());
Yabin Cui74edcea2015-07-24 10:11:05 -0700258 if (prop_val.empty()) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800259 ERROR("property '%s' doesn't exist while expanding '%s'\n",
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700260 prop_name.c_str(), src.c_str());
Dima Zavina6235ea2011-12-16 14:20:12 -0800261 goto err;
262 }
263
Yabin Cui00ede7d2015-07-24 13:26:04 -0700264 dst->append(prop_val);
Dima Zavina6235ea2011-12-16 14:20:12 -0800265 src_ptr = c;
266 continue;
267 }
268
Dima Zavina6235ea2011-12-16 14:20:12 -0800269 return 0;
Dima Zavina6235ea2011-12-16 14:20:12 -0800270err:
271 return -1;
272}
273
Greg Hackmannd68db712013-11-18 09:44:20 -0800274static void parse_import(struct parse_state *state, int nargs, char **args)
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800275{
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800276 if (nargs != 2) {
277 ERROR("single argument needed for import\n");
278 return;
279 }
280
Yabin Cui00ede7d2015-07-24 13:26:04 -0700281 std::string conf_file;
282 int ret = expand_props(args[1], &conf_file);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800283 if (ret) {
284 ERROR("error while handling import on line '%d' in '%s'\n",
285 state->line, state->filename);
286 return;
287 }
288
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800289 struct import* import = (struct import*) calloc(1, sizeof(struct import));
Yabin Cui00ede7d2015-07-24 13:26:04 -0700290 import->filename = strdup(conf_file.c_str());
291
292 struct listnode *import_list = (listnode*) state->priv;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800293 list_add_tail(import_list, &import->list);
Elliott Hughesda40c002015-03-27 23:20:44 -0700294 INFO("Added '%s' to import list\n", import->filename);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800295}
296
Greg Hackmannd68db712013-11-18 09:44:20 -0800297static void parse_new_section(struct parse_state *state, int kw,
Colin Cross6310a822010-04-20 14:29:05 -0700298 int nargs, char **args)
299{
300 printf("[ %s %s ]\n", args[0],
301 nargs > 1 ? args[1] : "");
302 switch(kw) {
303 case K_service:
304 state->context = parse_service(state, nargs, args);
305 if (state->context) {
306 state->parse_line = parse_line_service;
307 return;
308 }
309 break;
310 case K_on:
311 state->context = parse_action(state, nargs, args);
312 if (state->context) {
313 state->parse_line = parse_line_action;
314 return;
315 }
316 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700317 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800318 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800319 break;
Colin Cross6310a822010-04-20 14:29:05 -0700320 }
321 state->parse_line = parse_line_no_op;
322}
323
Elliott Hughesf682b472015-02-06 12:19:48 -0800324static void parse_config(const char *fn, const std::string& data)
Colin Cross6310a822010-04-20 14:29:05 -0700325{
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800326 struct listnode import_list;
327 struct listnode *node;
Colin Cross6310a822010-04-20 14:29:05 -0700328 char *args[INIT_PARSER_MAXARGS];
Colin Cross6310a822010-04-20 14:29:05 -0700329
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700330 int nargs = 0;
331
332 parse_state state;
Colin Cross6310a822010-04-20 14:29:05 -0700333 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800334 state.line = 0;
Elliott Hughesf682b472015-02-06 12:19:48 -0800335 state.ptr = strdup(data.c_str()); // TODO: fix this code!
Colin Cross6310a822010-04-20 14:29:05 -0700336 state.nexttoken = 0;
337 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800338
339 list_init(&import_list);
340 state.priv = &import_list;
341
Colin Cross6310a822010-04-20 14:29:05 -0700342 for (;;) {
343 switch (next_token(&state)) {
344 case T_EOF:
345 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800346 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700347 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800348 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700349 if (nargs) {
350 int kw = lookup_keyword(args[0]);
351 if (kw_is(kw, SECTION)) {
352 state.parse_line(&state, 0, 0);
353 parse_new_section(&state, kw, nargs, args);
354 } else {
355 state.parse_line(&state, nargs, args);
356 }
357 nargs = 0;
358 }
359 break;
360 case T_TEXT:
361 if (nargs < INIT_PARSER_MAXARGS) {
362 args[nargs++] = state.text;
363 }
364 break;
365 }
366 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800367
368parser_done:
369 list_for_each(node, &import_list) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700370 struct import* import = node_to_item(node, struct import, list);
Lee Campbellf13b1b32015-07-24 16:57:14 -0700371 if (!init_parse_config(import->filename)) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700372 ERROR("could not import file '%s' from '%s': %s\n",
373 import->filename, fn, strerror(errno));
374 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800375 }
Colin Cross6310a822010-04-20 14:29:05 -0700376}
377
Lee Campbellf13b1b32015-07-24 16:57:14 -0700378static bool init_parse_config_file(const char* path) {
379 INFO("Parsing file %s...\n", path);
Elliott Hughesda40c002015-03-27 23:20:44 -0700380 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800381 std::string data;
382 if (!read_file(path, &data)) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700383 return false;
Elliott Hughesf682b472015-02-06 12:19:48 -0800384 }
Colin Cross6310a822010-04-20 14:29:05 -0700385
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700386 data.push_back('\n'); // TODO: fix parse_config.
Elliott Hughesf682b472015-02-06 12:19:48 -0800387 parse_config(path, data);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800388 dump_parser_state();
Elliott Hughesda40c002015-03-27 23:20:44 -0700389
390 NOTICE("(Parsing %s took %.2fs.)\n", path, t.duration());
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700391 return true;
Colin Cross6310a822010-04-20 14:29:05 -0700392}
393
Lee Campbellf13b1b32015-07-24 16:57:14 -0700394static bool init_parse_config_dir(const char* path) {
395 INFO("Parsing directory %s...\n", path);
396 std::unique_ptr<DIR, int(*)(DIR*)> config_dir(opendir(path), closedir);
397 if (!config_dir) {
398 ERROR("Could not import directory '%s'\n", path);
399 return false;
400 }
401 dirent* current_file;
402 while ((current_file = readdir(config_dir.get()))) {
403 std::string current_path =
404 android::base::StringPrintf("%s/%s", path, current_file->d_name);
405 // Ignore directories and only process regular files.
406 if (current_file->d_type == DT_REG) {
407 if (!init_parse_config_file(current_path.c_str())) {
408 ERROR("could not import file '%s'\n", current_path.c_str());
409 }
410 }
411 }
412 return true;
413}
414
415bool init_parse_config(const char* path) {
416 if (is_dir(path)) {
417 return init_parse_config_dir(path);
418 }
419 return init_parse_config_file(path);
420}
421
Colin Cross6310a822010-04-20 14:29:05 -0700422static int valid_name(const char *name)
423{
424 if (strlen(name) > 16) {
425 return 0;
426 }
427 while (*name) {
428 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
429 return 0;
430 }
431 name++;
432 }
433 return 1;
434}
435
436struct service *service_find_by_name(const char *name)
437{
438 struct listnode *node;
439 struct service *svc;
440 list_for_each(node, &service_list) {
441 svc = node_to_item(node, struct service, slist);
442 if (!strcmp(svc->name, name)) {
443 return svc;
444 }
445 }
446 return 0;
447}
448
449struct service *service_find_by_pid(pid_t pid)
450{
451 struct listnode *node;
452 struct service *svc;
453 list_for_each(node, &service_list) {
454 svc = node_to_item(node, struct service, slist);
455 if (svc->pid == pid) {
456 return svc;
457 }
458 }
459 return 0;
460}
461
462struct service *service_find_by_keychord(int keychord_id)
463{
464 struct listnode *node;
465 struct service *svc;
466 list_for_each(node, &service_list) {
467 svc = node_to_item(node, struct service, slist);
468 if (svc->keychord_id == keychord_id) {
469 return svc;
470 }
471 }
472 return 0;
473}
474
475void service_for_each(void (*func)(struct service *svc))
476{
477 struct listnode *node;
478 struct service *svc;
479 list_for_each(node, &service_list) {
480 svc = node_to_item(node, struct service, slist);
481 func(svc);
482 }
483}
484
485void service_for_each_class(const char *classname,
486 void (*func)(struct service *svc))
487{
488 struct listnode *node;
489 struct service *svc;
490 list_for_each(node, &service_list) {
491 svc = node_to_item(node, struct service, slist);
492 if (!strcmp(svc->classname, classname)) {
493 func(svc);
494 }
495 }
496}
497
498void service_for_each_flags(unsigned matchflags,
499 void (*func)(struct service *svc))
500{
501 struct listnode *node;
502 struct service *svc;
503 list_for_each(node, &service_list) {
504 svc = node_to_item(node, struct service, slist);
505 if (svc->flags & matchflags) {
506 func(svc);
507 }
508 }
509}
510
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800511service* make_exec_oneshot_service(int nargs, char** args) {
512 // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS...
Mark Salyzyn17fff892015-06-02 11:11:02 -0700513 // SECLABEL can be a - to denote default
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800514 int command_arg = 1;
515 for (int i = 1; i < nargs; ++i) {
516 if (strcmp(args[i], "--") == 0) {
517 command_arg = i + 1;
518 break;
519 }
520 }
521 if (command_arg > 4 + NR_SVC_SUPP_GIDS) {
522 ERROR("exec called with too many supplementary group ids\n");
523 return NULL;
524 }
525
526 int argc = nargs - command_arg;
527 char** argv = (args + command_arg);
528 if (argc < 1) {
529 ERROR("exec called without command\n");
530 return NULL;
531 }
532
533 service* svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * argc);
534 if (svc == NULL) {
535 ERROR("Couldn't allocate service for exec of '%s': %s", argv[0], strerror(errno));
536 return NULL;
537 }
538
Mark Salyzyn17fff892015-06-02 11:11:02 -0700539 if ((command_arg > 2) && strcmp(args[1], "-")) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800540 svc->seclabel = args[1];
541 }
542 if (command_arg > 3) {
543 svc->uid = decode_uid(args[2]);
544 }
545 if (command_arg > 4) {
546 svc->gid = decode_uid(args[3]);
547 svc->nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */;
548 for (size_t i = 0; i < svc->nr_supp_gids; ++i) {
549 svc->supp_gids[i] = decode_uid(args[4 + i]);
550 }
551 }
552
553 static int exec_count; // Every service needs a unique name.
554 char* name = NULL;
555 asprintf(&name, "exec %d (%s)", exec_count++, argv[0]);
556 if (name == NULL) {
557 ERROR("Couldn't allocate name for exec service '%s'\n", argv[0]);
558 free(svc);
559 return NULL;
560 }
561 svc->name = name;
562 svc->classname = "default";
563 svc->flags = SVC_EXEC | SVC_ONESHOT;
564 svc->nargs = argc;
565 memcpy(svc->args, argv, sizeof(char*) * svc->nargs);
566 svc->args[argc] = NULL;
567 list_add_tail(&service_list, &svc->slist);
568 return svc;
569}
570
Colin Cross6310a822010-04-20 14:29:05 -0700571static void *parse_service(struct parse_state *state, int nargs, char **args)
572{
Colin Cross6310a822010-04-20 14:29:05 -0700573 if (nargs < 3) {
574 parse_error(state, "services must have a name and a program\n");
575 return 0;
576 }
577 if (!valid_name(args[1])) {
578 parse_error(state, "invalid service name '%s'\n", args[1]);
579 return 0;
580 }
581
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800582 service* svc = (service*) service_find_by_name(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700583 if (svc) {
584 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
585 return 0;
586 }
587
588 nargs -= 2;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800589 svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700590 if (!svc) {
591 parse_error(state, "out of memory\n");
592 return 0;
593 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800594 svc->name = strdup(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700595 svc->classname = "default";
596 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
597 svc->args[nargs] = 0;
598 svc->nargs = nargs;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700599 svc->onrestart = new Action();
600 svc->onrestart->InitSingleTrigger("onrestart");
Colin Cross6310a822010-04-20 14:29:05 -0700601 list_add_tail(&service_list, &svc->slist);
602 return svc;
603}
604
605static void parse_line_service(struct parse_state *state, int nargs, char **args)
606{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800607 struct service *svc = (service*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700608 int i, kw, kw_nargs;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700609 std::vector<std::string> str_args;
Colin Cross6310a822010-04-20 14:29:05 -0700610
611 if (nargs == 0) {
612 return;
613 }
614
615 svc->ioprio_class = IoSchedClass_NONE;
616
617 kw = lookup_keyword(args[0]);
618 switch (kw) {
Colin Cross6310a822010-04-20 14:29:05 -0700619 case K_class:
620 if (nargs != 2) {
621 parse_error(state, "class option requires a classname\n");
622 } else {
623 svc->classname = args[1];
624 }
625 break;
626 case K_console:
627 svc->flags |= SVC_CONSOLE;
628 break;
629 case K_disabled:
630 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700631 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700632 break;
633 case K_ioprio:
634 if (nargs != 3) {
635 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
636 } else {
637 svc->ioprio_pri = strtoul(args[2], 0, 8);
638
639 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
640 parse_error(state, "priority value must be range 0 - 7\n");
641 break;
642 }
643
644 if (!strcmp(args[1], "rt")) {
645 svc->ioprio_class = IoSchedClass_RT;
646 } else if (!strcmp(args[1], "be")) {
647 svc->ioprio_class = IoSchedClass_BE;
648 } else if (!strcmp(args[1], "idle")) {
649 svc->ioprio_class = IoSchedClass_IDLE;
650 } else {
651 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
652 }
653 }
654 break;
655 case K_group:
656 if (nargs < 2) {
657 parse_error(state, "group option requires a group id\n");
658 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
659 parse_error(state, "group option accepts at most %d supp. groups\n",
660 NR_SVC_SUPP_GIDS);
661 } else {
662 int n;
663 svc->gid = decode_uid(args[1]);
664 for (n = 2; n < nargs; n++) {
665 svc->supp_gids[n-2] = decode_uid(args[n]);
666 }
667 svc->nr_supp_gids = n - 2;
668 }
669 break;
670 case K_keycodes:
671 if (nargs < 2) {
672 parse_error(state, "keycodes option requires atleast one keycode\n");
673 } else {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800674 svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
Colin Cross6310a822010-04-20 14:29:05 -0700675 if (!svc->keycodes) {
676 parse_error(state, "could not allocate keycodes\n");
677 } else {
678 svc->nkeycodes = nargs - 1;
679 for (i = 1; i < nargs; i++) {
680 svc->keycodes[i - 1] = atoi(args[i]);
681 }
682 }
683 }
684 break;
685 case K_oneshot:
686 svc->flags |= SVC_ONESHOT;
687 break;
688 case K_onrestart:
689 nargs--;
690 args++;
691 kw = lookup_keyword(args[0]);
692 if (!kw_is(kw, COMMAND)) {
693 parse_error(state, "invalid command '%s'\n", args[0]);
694 break;
695 }
696 kw_nargs = kw_nargs(kw);
697 if (nargs < kw_nargs) {
698 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
699 kw_nargs > 2 ? "arguments" : "argument");
700 break;
701 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700702 str_args.assign(args, args + nargs);
703 svc->onrestart->AddCommand(kw_func(kw), str_args);
Colin Cross6310a822010-04-20 14:29:05 -0700704 break;
705 case K_critical:
706 svc->flags |= SVC_CRITICAL;
707 break;
708 case K_setenv: { /* name value */
Gavin.Changc3a46762013-09-28 00:22:11 +0800709 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700710 parse_error(state, "setenv option requires name and value arguments\n");
711 break;
712 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800713 svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
Colin Cross6310a822010-04-20 14:29:05 -0700714 if (!ei) {
715 parse_error(state, "out of memory\n");
716 break;
717 }
718 ei->name = args[1];
719 ei->value = args[2];
720 ei->next = svc->envvars;
721 svc->envvars = ei;
722 break;
723 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400724 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700725 if (nargs < 4) {
726 parse_error(state, "socket option requires name, type, perm arguments\n");
727 break;
728 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400729 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
730 && strcmp(args[2],"seqpacket")) {
731 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700732 break;
733 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800734 socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
Colin Cross6310a822010-04-20 14:29:05 -0700735 if (!si) {
736 parse_error(state, "out of memory\n");
737 break;
738 }
739 si->name = args[1];
740 si->type = args[2];
741 si->perm = strtoul(args[3], 0, 8);
742 if (nargs > 4)
743 si->uid = decode_uid(args[4]);
744 if (nargs > 5)
745 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400746 if (nargs > 6)
747 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700748 si->next = svc->sockets;
749 svc->sockets = si;
750 break;
751 }
752 case K_user:
753 if (nargs != 2) {
754 parse_error(state, "user option requires a user id\n");
755 } else {
756 svc->uid = decode_uid(args[1]);
757 }
758 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500759 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500760 if (nargs != 2) {
761 parse_error(state, "seclabel option requires a label string\n");
762 } else {
763 svc->seclabel = args[1];
764 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500765 break;
Elliott Hughesd62f0602015-06-12 18:02:20 -0700766 case K_writepid:
767 if (nargs < 2) {
768 parse_error(state, "writepid option requires at least one filename\n");
769 break;
770 }
771 svc->writepid_files_ = new std::vector<std::string>;
772 for (int i = 1; i < nargs; ++i) {
773 svc->writepid_files_->push_back(args[i]);
774 }
775 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500776
Colin Cross6310a822010-04-20 14:29:05 -0700777 default:
778 parse_error(state, "invalid option '%s'\n", args[0]);
779 }
780}
781
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700782static void *parse_action(struct parse_state* state, int nargs, char **args)
Colin Cross6310a822010-04-20 14:29:05 -0700783{
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700784 std::string ret_err;
785 std::vector<std::string> triggers(args + 1, args + nargs);
786 Action* ret = ActionManager::GetInstance().AddNewAction(triggers, &ret_err);
787
788 if (!ret) {
789 parse_error(state, "%s\n", ret_err.c_str());
Colin Cross6310a822010-04-20 14:29:05 -0700790 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700791
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700792 return ret;
Colin Cross6310a822010-04-20 14:29:05 -0700793}
794
795static void parse_line_action(struct parse_state* state, int nargs, char **args)
796{
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700797 Action* act = (Action*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700798 int kw, n;
799
800 if (nargs == 0) {
801 return;
802 }
803
804 kw = lookup_keyword(args[0]);
805 if (!kw_is(kw, COMMAND)) {
806 parse_error(state, "invalid command '%s'\n", args[0]);
807 return;
808 }
809
810 n = kw_nargs(kw);
811 if (nargs < n) {
812 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
813 n > 2 ? "arguments" : "argument");
814 return;
815 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700816
817 std::vector<std::string> str_args(args, args + nargs);
818 act->AddCommand(kw_func(kw), str_args, state->filename, state->line);
Colin Cross6310a822010-04-20 14:29:05 -0700819}