blob: fb675cb92ea8a2f1f914766cc541012368cd3db3 [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;
Paul Lawrence948410a2015-07-01 14:40:56 -0700144 if (!strcmp(s, "oad_system_props")) return K_load_system_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;
Paul Crowley749af8c2015-05-28 17:35:06 +0100172 if (!strcmp(s, "etusercryptopolicies")) return K_setusercryptopolicies;
Colin Cross6310a822010-04-20 14:29:05 -0700173 if (!strcmp(s, "ocket")) return K_socket;
174 if (!strcmp(s, "tart")) return K_start;
175 if (!strcmp(s, "top")) return K_stop;
Ken Sumralla76baaa2013-07-09 18:42:09 -0700176 if (!strcmp(s, "wapon_all")) return K_swapon_all;
Colin Cross6310a822010-04-20 14:29:05 -0700177 if (!strcmp(s, "ymlink")) return K_symlink;
178 if (!strcmp(s, "ysclktz")) return K_sysclktz;
179 break;
180 case 't':
181 if (!strcmp(s, "rigger")) return K_trigger;
182 break;
183 case 'u':
184 if (!strcmp(s, "ser")) return K_user;
185 break;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000186 case 'v':
187 if (!strcmp(s, "erity_load_state")) return K_verity_load_state;
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000188 if (!strcmp(s, "erity_update_state")) return K_verity_update_state;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000189 break;
Colin Cross6310a822010-04-20 14:29:05 -0700190 case 'w':
191 if (!strcmp(s, "rite")) return K_write;
Elliott Hughesd62f0602015-06-12 18:02:20 -0700192 if (!strcmp(s, "ritepid")) return K_writepid;
Colin Cross6310a822010-04-20 14:29:05 -0700193 if (!strcmp(s, "ait")) return K_wait;
194 break;
195 }
196 return K_UNKNOWN;
197}
198
Elliott Hughesf682b472015-02-06 12:19:48 -0800199static void parse_line_no_op(struct parse_state*, int, char**) {
Colin Cross6310a822010-04-20 14:29:05 -0700200}
201
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700202int expand_props(const std::string& src, std::string* dst) {
203 const char *src_ptr = src.c_str();
Dima Zavina6235ea2011-12-16 14:20:12 -0800204
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700205 if (!dst) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800206 return -1;
Yabin Cui00ede7d2015-07-24 13:26:04 -0700207 }
Dima Zavina6235ea2011-12-16 14:20:12 -0800208
Dima Zavina6235ea2011-12-16 14:20:12 -0800209 /* - variables can either be $x.y or ${x.y}, in case they are only part
210 * of the string.
211 * - will accept $$ as a literal $.
212 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
213 * bad things will happen
214 */
Yabin Cui00ede7d2015-07-24 13:26:04 -0700215 while (*src_ptr) {
216 const char *c;
Dima Zavina6235ea2011-12-16 14:20:12 -0800217
218 c = strchr(src_ptr, '$');
219 if (!c) {
Yabin Cui00ede7d2015-07-24 13:26:04 -0700220 dst->append(src_ptr);
Dima Zavina6235ea2011-12-16 14:20:12 -0800221 break;
222 }
223
Yabin Cui00ede7d2015-07-24 13:26:04 -0700224 dst->append(src_ptr, c);
Dima Zavina6235ea2011-12-16 14:20:12 -0800225 c++;
226
227 if (*c == '$') {
Yabin Cui00ede7d2015-07-24 13:26:04 -0700228 dst->push_back(*(c++));
Dima Zavina6235ea2011-12-16 14:20:12 -0800229 src_ptr = c;
Dima Zavina6235ea2011-12-16 14:20:12 -0800230 continue;
231 } else if (*c == '\0') {
232 break;
233 }
234
Yabin Cui00ede7d2015-07-24 13:26:04 -0700235 std::string prop_name;
Dima Zavina6235ea2011-12-16 14:20:12 -0800236 if (*c == '{') {
237 c++;
Yabin Cui00ede7d2015-07-24 13:26:04 -0700238 const char* end = strchr(c, '}');
239 if (!end) {
240 // failed to find closing brace, abort.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700241 ERROR("unexpected end of string in '%s', looking for }\n", src.c_str());
Dima Zavina6235ea2011-12-16 14:20:12 -0800242 goto err;
243 }
Yabin Cui00ede7d2015-07-24 13:26:04 -0700244 prop_name = std::string(c, end);
245 c = end + 1;
246 } else {
247 prop_name = c;
Dima Zavina6235ea2011-12-16 14:20:12 -0800248 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
Yabin Cui00ede7d2015-07-24 13:26:04 -0700249 c);
250 c += prop_name.size();
Dima Zavina6235ea2011-12-16 14:20:12 -0800251 }
252
Yabin Cui00ede7d2015-07-24 13:26:04 -0700253 if (prop_name.empty()) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700254 ERROR("invalid zero-length prop name in '%s'\n", src.c_str());
Dima Zavina6235ea2011-12-16 14:20:12 -0800255 goto err;
256 }
257
Yabin Cui00ede7d2015-07-24 13:26:04 -0700258 std::string prop_val = property_get(prop_name.c_str());
Yabin Cui74edcea2015-07-24 10:11:05 -0700259 if (prop_val.empty()) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800260 ERROR("property '%s' doesn't exist while expanding '%s'\n",
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700261 prop_name.c_str(), src.c_str());
Dima Zavina6235ea2011-12-16 14:20:12 -0800262 goto err;
263 }
264
Yabin Cui00ede7d2015-07-24 13:26:04 -0700265 dst->append(prop_val);
Dima Zavina6235ea2011-12-16 14:20:12 -0800266 src_ptr = c;
267 continue;
268 }
269
Dima Zavina6235ea2011-12-16 14:20:12 -0800270 return 0;
Dima Zavina6235ea2011-12-16 14:20:12 -0800271err:
272 return -1;
273}
274
Greg Hackmannd68db712013-11-18 09:44:20 -0800275static void parse_import(struct parse_state *state, int nargs, char **args)
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800276{
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800277 if (nargs != 2) {
278 ERROR("single argument needed for import\n");
279 return;
280 }
281
Yabin Cui00ede7d2015-07-24 13:26:04 -0700282 std::string conf_file;
283 int ret = expand_props(args[1], &conf_file);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800284 if (ret) {
285 ERROR("error while handling import on line '%d' in '%s'\n",
286 state->line, state->filename);
287 return;
288 }
289
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800290 struct import* import = (struct import*) calloc(1, sizeof(struct import));
Yabin Cui00ede7d2015-07-24 13:26:04 -0700291 import->filename = strdup(conf_file.c_str());
292
293 struct listnode *import_list = (listnode*) state->priv;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800294 list_add_tail(import_list, &import->list);
Elliott Hughesda40c002015-03-27 23:20:44 -0700295 INFO("Added '%s' to import list\n", import->filename);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800296}
297
Greg Hackmannd68db712013-11-18 09:44:20 -0800298static void parse_new_section(struct parse_state *state, int kw,
Colin Cross6310a822010-04-20 14:29:05 -0700299 int nargs, char **args)
300{
301 printf("[ %s %s ]\n", args[0],
302 nargs > 1 ? args[1] : "");
303 switch(kw) {
304 case K_service:
305 state->context = parse_service(state, nargs, args);
306 if (state->context) {
307 state->parse_line = parse_line_service;
308 return;
309 }
310 break;
311 case K_on:
312 state->context = parse_action(state, nargs, args);
313 if (state->context) {
314 state->parse_line = parse_line_action;
315 return;
316 }
317 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700318 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800319 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800320 break;
Colin Cross6310a822010-04-20 14:29:05 -0700321 }
322 state->parse_line = parse_line_no_op;
323}
324
Elliott Hughesf682b472015-02-06 12:19:48 -0800325static void parse_config(const char *fn, const std::string& data)
Colin Cross6310a822010-04-20 14:29:05 -0700326{
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800327 struct listnode import_list;
328 struct listnode *node;
Colin Cross6310a822010-04-20 14:29:05 -0700329 char *args[INIT_PARSER_MAXARGS];
Colin Cross6310a822010-04-20 14:29:05 -0700330
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700331 int nargs = 0;
332
333 parse_state state;
Colin Cross6310a822010-04-20 14:29:05 -0700334 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800335 state.line = 0;
Elliott Hughesf682b472015-02-06 12:19:48 -0800336 state.ptr = strdup(data.c_str()); // TODO: fix this code!
Colin Cross6310a822010-04-20 14:29:05 -0700337 state.nexttoken = 0;
338 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800339
340 list_init(&import_list);
341 state.priv = &import_list;
342
Colin Cross6310a822010-04-20 14:29:05 -0700343 for (;;) {
344 switch (next_token(&state)) {
345 case T_EOF:
346 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800347 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700348 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800349 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700350 if (nargs) {
351 int kw = lookup_keyword(args[0]);
352 if (kw_is(kw, SECTION)) {
353 state.parse_line(&state, 0, 0);
354 parse_new_section(&state, kw, nargs, args);
355 } else {
356 state.parse_line(&state, nargs, args);
357 }
358 nargs = 0;
359 }
360 break;
361 case T_TEXT:
362 if (nargs < INIT_PARSER_MAXARGS) {
363 args[nargs++] = state.text;
364 }
365 break;
366 }
367 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800368
369parser_done:
370 list_for_each(node, &import_list) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700371 struct import* import = node_to_item(node, struct import, list);
Lee Campbellf13b1b32015-07-24 16:57:14 -0700372 if (!init_parse_config(import->filename)) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700373 ERROR("could not import file '%s' from '%s': %s\n",
374 import->filename, fn, strerror(errno));
375 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800376 }
Colin Cross6310a822010-04-20 14:29:05 -0700377}
378
Lee Campbellf13b1b32015-07-24 16:57:14 -0700379static bool init_parse_config_file(const char* path) {
380 INFO("Parsing file %s...\n", path);
Elliott Hughesda40c002015-03-27 23:20:44 -0700381 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800382 std::string data;
383 if (!read_file(path, &data)) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700384 return false;
Elliott Hughesf682b472015-02-06 12:19:48 -0800385 }
Colin Cross6310a822010-04-20 14:29:05 -0700386
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700387 data.push_back('\n'); // TODO: fix parse_config.
Elliott Hughesf682b472015-02-06 12:19:48 -0800388 parse_config(path, data);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800389 dump_parser_state();
Elliott Hughesda40c002015-03-27 23:20:44 -0700390
391 NOTICE("(Parsing %s took %.2fs.)\n", path, t.duration());
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700392 return true;
Colin Cross6310a822010-04-20 14:29:05 -0700393}
394
Lee Campbellf13b1b32015-07-24 16:57:14 -0700395static bool init_parse_config_dir(const char* path) {
396 INFO("Parsing directory %s...\n", path);
397 std::unique_ptr<DIR, int(*)(DIR*)> config_dir(opendir(path), closedir);
398 if (!config_dir) {
399 ERROR("Could not import directory '%s'\n", path);
400 return false;
401 }
402 dirent* current_file;
403 while ((current_file = readdir(config_dir.get()))) {
404 std::string current_path =
405 android::base::StringPrintf("%s/%s", path, current_file->d_name);
406 // Ignore directories and only process regular files.
407 if (current_file->d_type == DT_REG) {
408 if (!init_parse_config_file(current_path.c_str())) {
409 ERROR("could not import file '%s'\n", current_path.c_str());
410 }
411 }
412 }
413 return true;
414}
415
416bool init_parse_config(const char* path) {
417 if (is_dir(path)) {
418 return init_parse_config_dir(path);
419 }
420 return init_parse_config_file(path);
421}
422
Colin Cross6310a822010-04-20 14:29:05 -0700423static int valid_name(const char *name)
424{
425 if (strlen(name) > 16) {
426 return 0;
427 }
428 while (*name) {
429 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
430 return 0;
431 }
432 name++;
433 }
434 return 1;
435}
436
437struct service *service_find_by_name(const char *name)
438{
439 struct listnode *node;
440 struct service *svc;
441 list_for_each(node, &service_list) {
442 svc = node_to_item(node, struct service, slist);
443 if (!strcmp(svc->name, name)) {
444 return svc;
445 }
446 }
447 return 0;
448}
449
450struct service *service_find_by_pid(pid_t pid)
451{
452 struct listnode *node;
453 struct service *svc;
454 list_for_each(node, &service_list) {
455 svc = node_to_item(node, struct service, slist);
456 if (svc->pid == pid) {
457 return svc;
458 }
459 }
460 return 0;
461}
462
463struct service *service_find_by_keychord(int keychord_id)
464{
465 struct listnode *node;
466 struct service *svc;
467 list_for_each(node, &service_list) {
468 svc = node_to_item(node, struct service, slist);
469 if (svc->keychord_id == keychord_id) {
470 return svc;
471 }
472 }
473 return 0;
474}
475
476void service_for_each(void (*func)(struct service *svc))
477{
478 struct listnode *node;
479 struct service *svc;
480 list_for_each(node, &service_list) {
481 svc = node_to_item(node, struct service, slist);
482 func(svc);
483 }
484}
485
486void service_for_each_class(const char *classname,
487 void (*func)(struct service *svc))
488{
489 struct listnode *node;
490 struct service *svc;
491 list_for_each(node, &service_list) {
492 svc = node_to_item(node, struct service, slist);
493 if (!strcmp(svc->classname, classname)) {
494 func(svc);
495 }
496 }
497}
498
499void service_for_each_flags(unsigned matchflags,
500 void (*func)(struct service *svc))
501{
502 struct listnode *node;
503 struct service *svc;
504 list_for_each(node, &service_list) {
505 svc = node_to_item(node, struct service, slist);
506 if (svc->flags & matchflags) {
507 func(svc);
508 }
509 }
510}
511
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800512service* make_exec_oneshot_service(int nargs, char** args) {
513 // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS...
Mark Salyzyn17fff892015-06-02 11:11:02 -0700514 // SECLABEL can be a - to denote default
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800515 int command_arg = 1;
516 for (int i = 1; i < nargs; ++i) {
517 if (strcmp(args[i], "--") == 0) {
518 command_arg = i + 1;
519 break;
520 }
521 }
522 if (command_arg > 4 + NR_SVC_SUPP_GIDS) {
523 ERROR("exec called with too many supplementary group ids\n");
524 return NULL;
525 }
526
527 int argc = nargs - command_arg;
528 char** argv = (args + command_arg);
529 if (argc < 1) {
530 ERROR("exec called without command\n");
531 return NULL;
532 }
533
534 service* svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * argc);
535 if (svc == NULL) {
536 ERROR("Couldn't allocate service for exec of '%s': %s", argv[0], strerror(errno));
537 return NULL;
538 }
539
Mark Salyzyn17fff892015-06-02 11:11:02 -0700540 if ((command_arg > 2) && strcmp(args[1], "-")) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800541 svc->seclabel = args[1];
542 }
543 if (command_arg > 3) {
544 svc->uid = decode_uid(args[2]);
545 }
546 if (command_arg > 4) {
547 svc->gid = decode_uid(args[3]);
548 svc->nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */;
549 for (size_t i = 0; i < svc->nr_supp_gids; ++i) {
550 svc->supp_gids[i] = decode_uid(args[4 + i]);
551 }
552 }
553
554 static int exec_count; // Every service needs a unique name.
555 char* name = NULL;
556 asprintf(&name, "exec %d (%s)", exec_count++, argv[0]);
557 if (name == NULL) {
558 ERROR("Couldn't allocate name for exec service '%s'\n", argv[0]);
559 free(svc);
560 return NULL;
561 }
562 svc->name = name;
563 svc->classname = "default";
564 svc->flags = SVC_EXEC | SVC_ONESHOT;
565 svc->nargs = argc;
566 memcpy(svc->args, argv, sizeof(char*) * svc->nargs);
567 svc->args[argc] = NULL;
568 list_add_tail(&service_list, &svc->slist);
569 return svc;
570}
571
Colin Cross6310a822010-04-20 14:29:05 -0700572static void *parse_service(struct parse_state *state, int nargs, char **args)
573{
Colin Cross6310a822010-04-20 14:29:05 -0700574 if (nargs < 3) {
575 parse_error(state, "services must have a name and a program\n");
576 return 0;
577 }
578 if (!valid_name(args[1])) {
579 parse_error(state, "invalid service name '%s'\n", args[1]);
580 return 0;
581 }
582
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800583 service* svc = (service*) service_find_by_name(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700584 if (svc) {
585 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
586 return 0;
587 }
588
589 nargs -= 2;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800590 svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
Colin Cross6310a822010-04-20 14:29:05 -0700591 if (!svc) {
592 parse_error(state, "out of memory\n");
593 return 0;
594 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800595 svc->name = strdup(args[1]);
Colin Cross6310a822010-04-20 14:29:05 -0700596 svc->classname = "default";
597 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
598 svc->args[nargs] = 0;
599 svc->nargs = nargs;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700600 svc->onrestart = new Action();
601 svc->onrestart->InitSingleTrigger("onrestart");
Colin Cross6310a822010-04-20 14:29:05 -0700602 list_add_tail(&service_list, &svc->slist);
603 return svc;
604}
605
606static void parse_line_service(struct parse_state *state, int nargs, char **args)
607{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800608 struct service *svc = (service*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700609 int i, kw, kw_nargs;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700610 std::vector<std::string> str_args;
Colin Cross6310a822010-04-20 14:29:05 -0700611
612 if (nargs == 0) {
613 return;
614 }
615
616 svc->ioprio_class = IoSchedClass_NONE;
617
618 kw = lookup_keyword(args[0]);
619 switch (kw) {
Colin Cross6310a822010-04-20 14:29:05 -0700620 case K_class:
621 if (nargs != 2) {
622 parse_error(state, "class option requires a classname\n");
623 } else {
624 svc->classname = args[1];
625 }
626 break;
627 case K_console:
628 svc->flags |= SVC_CONSOLE;
629 break;
630 case K_disabled:
631 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700632 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700633 break;
634 case K_ioprio:
635 if (nargs != 3) {
636 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
637 } else {
638 svc->ioprio_pri = strtoul(args[2], 0, 8);
639
640 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
641 parse_error(state, "priority value must be range 0 - 7\n");
642 break;
643 }
644
645 if (!strcmp(args[1], "rt")) {
646 svc->ioprio_class = IoSchedClass_RT;
647 } else if (!strcmp(args[1], "be")) {
648 svc->ioprio_class = IoSchedClass_BE;
649 } else if (!strcmp(args[1], "idle")) {
650 svc->ioprio_class = IoSchedClass_IDLE;
651 } else {
652 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
653 }
654 }
655 break;
656 case K_group:
657 if (nargs < 2) {
658 parse_error(state, "group option requires a group id\n");
659 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
660 parse_error(state, "group option accepts at most %d supp. groups\n",
661 NR_SVC_SUPP_GIDS);
662 } else {
663 int n;
664 svc->gid = decode_uid(args[1]);
665 for (n = 2; n < nargs; n++) {
666 svc->supp_gids[n-2] = decode_uid(args[n]);
667 }
668 svc->nr_supp_gids = n - 2;
669 }
670 break;
671 case K_keycodes:
672 if (nargs < 2) {
673 parse_error(state, "keycodes option requires atleast one keycode\n");
674 } else {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800675 svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
Colin Cross6310a822010-04-20 14:29:05 -0700676 if (!svc->keycodes) {
677 parse_error(state, "could not allocate keycodes\n");
678 } else {
679 svc->nkeycodes = nargs - 1;
680 for (i = 1; i < nargs; i++) {
681 svc->keycodes[i - 1] = atoi(args[i]);
682 }
683 }
684 }
685 break;
686 case K_oneshot:
687 svc->flags |= SVC_ONESHOT;
688 break;
689 case K_onrestart:
690 nargs--;
691 args++;
692 kw = lookup_keyword(args[0]);
693 if (!kw_is(kw, COMMAND)) {
694 parse_error(state, "invalid command '%s'\n", args[0]);
695 break;
696 }
697 kw_nargs = kw_nargs(kw);
698 if (nargs < kw_nargs) {
699 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
700 kw_nargs > 2 ? "arguments" : "argument");
701 break;
702 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700703 str_args.assign(args, args + nargs);
704 svc->onrestart->AddCommand(kw_func(kw), str_args);
Colin Cross6310a822010-04-20 14:29:05 -0700705 break;
706 case K_critical:
707 svc->flags |= SVC_CRITICAL;
708 break;
709 case K_setenv: { /* name value */
Gavin.Changc3a46762013-09-28 00:22:11 +0800710 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700711 parse_error(state, "setenv option requires name and value arguments\n");
712 break;
713 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800714 svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
Colin Cross6310a822010-04-20 14:29:05 -0700715 if (!ei) {
716 parse_error(state, "out of memory\n");
717 break;
718 }
719 ei->name = args[1];
720 ei->value = args[2];
721 ei->next = svc->envvars;
722 svc->envvars = ei;
723 break;
724 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400725 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700726 if (nargs < 4) {
727 parse_error(state, "socket option requires name, type, perm arguments\n");
728 break;
729 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400730 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
731 && strcmp(args[2],"seqpacket")) {
732 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700733 break;
734 }
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800735 socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
Colin Cross6310a822010-04-20 14:29:05 -0700736 if (!si) {
737 parse_error(state, "out of memory\n");
738 break;
739 }
740 si->name = args[1];
741 si->type = args[2];
742 si->perm = strtoul(args[3], 0, 8);
743 if (nargs > 4)
744 si->uid = decode_uid(args[4]);
745 if (nargs > 5)
746 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400747 if (nargs > 6)
748 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700749 si->next = svc->sockets;
750 svc->sockets = si;
751 break;
752 }
753 case K_user:
754 if (nargs != 2) {
755 parse_error(state, "user option requires a user id\n");
756 } else {
757 svc->uid = decode_uid(args[1]);
758 }
759 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500760 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500761 if (nargs != 2) {
762 parse_error(state, "seclabel option requires a label string\n");
763 } else {
764 svc->seclabel = args[1];
765 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500766 break;
Elliott Hughesd62f0602015-06-12 18:02:20 -0700767 case K_writepid:
768 if (nargs < 2) {
769 parse_error(state, "writepid option requires at least one filename\n");
770 break;
771 }
772 svc->writepid_files_ = new std::vector<std::string>;
773 for (int i = 1; i < nargs; ++i) {
774 svc->writepid_files_->push_back(args[i]);
775 }
776 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500777
Colin Cross6310a822010-04-20 14:29:05 -0700778 default:
779 parse_error(state, "invalid option '%s'\n", args[0]);
780 }
781}
782
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700783static void *parse_action(struct parse_state* state, int nargs, char **args)
Colin Cross6310a822010-04-20 14:29:05 -0700784{
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700785 std::string ret_err;
786 std::vector<std::string> triggers(args + 1, args + nargs);
787 Action* ret = ActionManager::GetInstance().AddNewAction(triggers, &ret_err);
788
789 if (!ret) {
790 parse_error(state, "%s\n", ret_err.c_str());
Colin Cross6310a822010-04-20 14:29:05 -0700791 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700792
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700793 return ret;
Colin Cross6310a822010-04-20 14:29:05 -0700794}
795
796static void parse_line_action(struct parse_state* state, int nargs, char **args)
797{
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700798 Action* act = (Action*) state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700799 int kw, n;
800
801 if (nargs == 0) {
802 return;
803 }
804
805 kw = lookup_keyword(args[0]);
806 if (!kw_is(kw, COMMAND)) {
807 parse_error(state, "invalid command '%s'\n", args[0]);
808 return;
809 }
810
811 n = kw_nargs(kw);
812 if (nargs < n) {
813 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
814 n > 2 ? "arguments" : "argument");
815 return;
816 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700817
818 std::vector<std::string> str_args(args, args + nargs);
819 act->AddCommand(kw_func(kw), str_args, state->filename, state->line);
Colin Cross6310a822010-04-20 14:29:05 -0700820}