blob: 02cbf3dd52ba4719c618aec0b8d6d91220f83c86 [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
17#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <fcntl.h>
21#include <stdarg.h>
22#include <string.h>
23#include <stddef.h>
24#include <ctype.h>
25
26#include "init.h"
27#include "parser.h"
28#include "init_parser.h"
29#include "log.h"
Colin Cross6310a822010-04-20 14:29:05 -070030#include "property_service.h"
31#include "util.h"
32
33#include <cutils/iosched_policy.h>
Dima Zavinda04c522011-09-01 17:09:44 -070034#include <cutils/list.h>
Colin Cross6310a822010-04-20 14:29:05 -070035
Colin Cross6310a822010-04-20 14:29:05 -070036static list_declare(service_list);
37static list_declare(action_list);
38static list_declare(action_queue);
39
Dima Zavin78a1b1f2011-12-20 12:53:48 -080040struct import {
41 struct listnode list;
42 const char *filename;
43};
44
Colin Cross6310a822010-04-20 14:29:05 -070045static void *parse_service(struct parse_state *state, int nargs, char **args);
46static void parse_line_service(struct parse_state *state, int nargs, char **args);
47
48static void *parse_action(struct parse_state *state, int nargs, char **args);
49static void parse_line_action(struct parse_state *state, int nargs, char **args);
50
51#define SECTION 0x01
52#define COMMAND 0x02
53#define OPTION 0x04
54
55#include "keywords.h"
56
57#define KEYWORD(symbol, flags, nargs, func) \
58 [ K_##symbol ] = { #symbol, func, nargs + 1, flags, },
59
Greg Hackmannd68db712013-11-18 09:44:20 -080060static struct {
Colin Cross6310a822010-04-20 14:29:05 -070061 const char *name;
62 int (*func)(int nargs, char **args);
63 unsigned char nargs;
64 unsigned char flags;
65} keyword_info[KEYWORD_COUNT] = {
66 [ K_UNKNOWN ] = { "unknown", 0, 0, 0 },
67#include "keywords.h"
68};
69#undef KEYWORD
70
71#define kw_is(kw, type) (keyword_info[kw].flags & (type))
72#define kw_name(kw) (keyword_info[kw].name)
73#define kw_func(kw) (keyword_info[kw].func)
74#define kw_nargs(kw) (keyword_info[kw].nargs)
75
Greg Hackmannd68db712013-11-18 09:44:20 -080076static int lookup_keyword(const char *s)
Colin Cross6310a822010-04-20 14:29:05 -070077{
78 switch (*s++) {
79 case 'c':
80 if (!strcmp(s, "opy")) return K_copy;
81 if (!strcmp(s, "apability")) return K_capability;
82 if (!strcmp(s, "hdir")) return K_chdir;
83 if (!strcmp(s, "hroot")) return K_chroot;
84 if (!strcmp(s, "lass")) return K_class;
85 if (!strcmp(s, "lass_start")) return K_class_start;
86 if (!strcmp(s, "lass_stop")) return K_class_stop;
Ken Sumrall752923c2010-12-03 16:33:31 -080087 if (!strcmp(s, "lass_reset")) return K_class_reset;
Colin Cross6310a822010-04-20 14:29:05 -070088 if (!strcmp(s, "onsole")) return K_console;
89 if (!strcmp(s, "hown")) return K_chown;
90 if (!strcmp(s, "hmod")) return K_chmod;
91 if (!strcmp(s, "ritical")) return K_critical;
92 break;
93 case 'd':
94 if (!strcmp(s, "isabled")) return K_disabled;
95 if (!strcmp(s, "omainname")) return K_domainname;
96 break;
97 case 'e':
JP Abgrall3beec7e2014-05-02 21:14:29 -070098 if (!strcmp(s, "nable")) return K_enable;
Colin Cross6310a822010-04-20 14:29:05 -070099 if (!strcmp(s, "xec")) return K_exec;
San Mehat429721c2014-09-23 07:48:47 -0700100 if (!strcmp(s, "xeconce")) return K_execonce;
Colin Cross6310a822010-04-20 14:29:05 -0700101 if (!strcmp(s, "xport")) return K_export;
102 break;
103 case 'g':
104 if (!strcmp(s, "roup")) return K_group;
105 break;
106 case 'h':
107 if (!strcmp(s, "ostname")) return K_hostname;
108 break;
109 case 'i':
110 if (!strcmp(s, "oprio")) return K_ioprio;
111 if (!strcmp(s, "fup")) return K_ifup;
112 if (!strcmp(s, "nsmod")) return K_insmod;
113 if (!strcmp(s, "mport")) return K_import;
114 break;
115 case 'k':
116 if (!strcmp(s, "eycodes")) return K_keycodes;
117 break;
118 case 'l':
119 if (!strcmp(s, "oglevel")) return K_loglevel;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800120 if (!strcmp(s, "oad_persist_props")) return K_load_persist_props;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700121 if (!strcmp(s, "oad_all_props")) return K_load_all_props;
Colin Cross6310a822010-04-20 14:29:05 -0700122 break;
123 case 'm':
124 if (!strcmp(s, "kdir")) return K_mkdir;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700125 if (!strcmp(s, "ount_all")) return K_mount_all;
Colin Cross6310a822010-04-20 14:29:05 -0700126 if (!strcmp(s, "ount")) return K_mount;
127 break;
128 case 'o':
129 if (!strcmp(s, "n")) return K_on;
130 if (!strcmp(s, "neshot")) return K_oneshot;
131 if (!strcmp(s, "nrestart")) return K_onrestart;
132 break;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700133 case 'p':
134 if (!strcmp(s, "owerctl")) return K_powerctl;
Colin Cross6310a822010-04-20 14:29:05 -0700135 case 'r':
136 if (!strcmp(s, "estart")) return K_restart;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500137 if (!strcmp(s, "estorecon")) return K_restorecon;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400138 if (!strcmp(s, "estorecon_recursive")) return K_restorecon_recursive;
Ken Sumrall203bad52011-01-18 17:37:41 -0800139 if (!strcmp(s, "mdir")) return K_rmdir;
140 if (!strcmp(s, "m")) return K_rm;
Colin Cross6310a822010-04-20 14:29:05 -0700141 break;
142 case 's':
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500143 if (!strcmp(s, "eclabel")) return K_seclabel;
Colin Cross6310a822010-04-20 14:29:05 -0700144 if (!strcmp(s, "ervice")) return K_service;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500145 if (!strcmp(s, "etcon")) return K_setcon;
146 if (!strcmp(s, "etenforce")) return K_setenforce;
Colin Cross6310a822010-04-20 14:29:05 -0700147 if (!strcmp(s, "etenv")) return K_setenv;
148 if (!strcmp(s, "etkey")) return K_setkey;
149 if (!strcmp(s, "etprop")) return K_setprop;
150 if (!strcmp(s, "etrlimit")) return K_setrlimit;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500151 if (!strcmp(s, "etsebool")) return K_setsebool;
Colin Cross6310a822010-04-20 14:29:05 -0700152 if (!strcmp(s, "ocket")) return K_socket;
153 if (!strcmp(s, "tart")) return K_start;
154 if (!strcmp(s, "top")) return K_stop;
Ken Sumralla76baaa2013-07-09 18:42:09 -0700155 if (!strcmp(s, "wapon_all")) return K_swapon_all;
Colin Cross6310a822010-04-20 14:29:05 -0700156 if (!strcmp(s, "ymlink")) return K_symlink;
157 if (!strcmp(s, "ysclktz")) return K_sysclktz;
158 break;
159 case 't':
160 if (!strcmp(s, "rigger")) return K_trigger;
161 break;
162 case 'u':
163 if (!strcmp(s, "ser")) return K_user;
164 break;
165 case 'w':
166 if (!strcmp(s, "rite")) return K_write;
167 if (!strcmp(s, "ait")) return K_wait;
168 break;
169 }
170 return K_UNKNOWN;
171}
172
Greg Hackmannd68db712013-11-18 09:44:20 -0800173static void parse_line_no_op(struct parse_state *state, int nargs, char **args)
Colin Cross6310a822010-04-20 14:29:05 -0700174{
175}
176
Dima Zavina6235ea2011-12-16 14:20:12 -0800177static int push_chars(char **dst, int *len, const char *chars, int cnt)
178{
179 if (cnt > *len)
180 return -1;
181
182 memcpy(*dst, chars, cnt);
183 *dst += cnt;
184 *len -= cnt;
185
186 return 0;
187}
188
Dima Zavin84bf9af2011-12-20 13:44:41 -0800189int expand_props(char *dst, const char *src, int dst_size)
Dima Zavina6235ea2011-12-16 14:20:12 -0800190{
Dima Zavina6235ea2011-12-16 14:20:12 -0800191 char *dst_ptr = dst;
192 const char *src_ptr = src;
Dima Zavina6235ea2011-12-16 14:20:12 -0800193 int ret = 0;
194 int left = dst_size - 1;
195
196 if (!src || !dst || dst_size == 0)
197 return -1;
198
Dima Zavina6235ea2011-12-16 14:20:12 -0800199 /* - variables can either be $x.y or ${x.y}, in case they are only part
200 * of the string.
201 * - will accept $$ as a literal $.
202 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
203 * bad things will happen
204 */
205 while (*src_ptr && left > 0) {
206 char *c;
207 char prop[PROP_NAME_MAX + 1];
Colin Cross2deedfe2013-01-28 17:13:35 -0800208 char prop_val[PROP_VALUE_MAX];
Dima Zavina6235ea2011-12-16 14:20:12 -0800209 int prop_len = 0;
Colin Cross2deedfe2013-01-28 17:13:35 -0800210 int prop_val_len;
Dima Zavina6235ea2011-12-16 14:20:12 -0800211
212 c = strchr(src_ptr, '$');
213 if (!c) {
214 while (left-- > 0 && *src_ptr)
215 *(dst_ptr++) = *(src_ptr++);
216 break;
217 }
218
219 memset(prop, 0, sizeof(prop));
220
221 ret = push_chars(&dst_ptr, &left, src_ptr, c - src_ptr);
222 if (ret < 0)
223 goto err_nospace;
224 c++;
225
226 if (*c == '$') {
227 *(dst_ptr++) = *(c++);
228 src_ptr = c;
229 left--;
230 continue;
231 } else if (*c == '\0') {
232 break;
233 }
234
235 if (*c == '{') {
236 c++;
237 while (*c && *c != '}' && prop_len < PROP_NAME_MAX)
238 prop[prop_len++] = *(c++);
239 if (*c != '}') {
240 /* failed to find closing brace, abort. */
241 if (prop_len == PROP_NAME_MAX)
242 ERROR("prop name too long during expansion of '%s'\n",
243 src);
244 else if (*c == '\0')
245 ERROR("unexpected end of string in '%s', looking for }\n",
246 src);
247 goto err;
248 }
249 prop[prop_len] = '\0';
250 c++;
251 } else if (*c) {
252 while (*c && prop_len < PROP_NAME_MAX)
253 prop[prop_len++] = *(c++);
254 if (prop_len == PROP_NAME_MAX && *c != '\0') {
255 ERROR("prop name too long in '%s'\n", src);
256 goto err;
257 }
258 prop[prop_len] = '\0';
259 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
260 prop);
261 }
262
263 if (prop_len == 0) {
264 ERROR("invalid zero-length prop name in '%s'\n", src);
265 goto err;
266 }
267
Colin Cross2deedfe2013-01-28 17:13:35 -0800268 prop_val_len = property_get(prop, prop_val);
269 if (!prop_val_len) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800270 ERROR("property '%s' doesn't exist while expanding '%s'\n",
271 prop, src);
272 goto err;
273 }
274
Colin Cross2deedfe2013-01-28 17:13:35 -0800275 ret = push_chars(&dst_ptr, &left, prop_val, prop_val_len);
Dima Zavina6235ea2011-12-16 14:20:12 -0800276 if (ret < 0)
277 goto err_nospace;
278 src_ptr = c;
279 continue;
280 }
281
282 *dst_ptr = '\0';
283 return 0;
284
285err_nospace:
286 ERROR("destination buffer overflow while expanding '%s'\n", src);
287err:
288 return -1;
289}
290
Greg Hackmannd68db712013-11-18 09:44:20 -0800291static void parse_import(struct parse_state *state, int nargs, char **args)
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800292{
293 struct listnode *import_list = state->priv;
294 struct import *import;
295 char conf_file[PATH_MAX];
296 int ret;
297
298 if (nargs != 2) {
299 ERROR("single argument needed for import\n");
300 return;
301 }
302
303 ret = expand_props(conf_file, args[1], sizeof(conf_file));
304 if (ret) {
305 ERROR("error while handling import on line '%d' in '%s'\n",
306 state->line, state->filename);
307 return;
308 }
309
310 import = calloc(1, sizeof(struct import));
311 import->filename = strdup(conf_file);
312 list_add_tail(import_list, &import->list);
313 INFO("found import '%s', adding to import list", import->filename);
314}
315
Greg Hackmannd68db712013-11-18 09:44:20 -0800316static void parse_new_section(struct parse_state *state, int kw,
Colin Cross6310a822010-04-20 14:29:05 -0700317 int nargs, char **args)
318{
319 printf("[ %s %s ]\n", args[0],
320 nargs > 1 ? args[1] : "");
321 switch(kw) {
322 case K_service:
323 state->context = parse_service(state, nargs, args);
324 if (state->context) {
325 state->parse_line = parse_line_service;
326 return;
327 }
328 break;
329 case K_on:
330 state->context = parse_action(state, nargs, args);
331 if (state->context) {
332 state->parse_line = parse_line_action;
333 return;
334 }
335 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700336 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800337 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800338 break;
Colin Cross6310a822010-04-20 14:29:05 -0700339 }
340 state->parse_line = parse_line_no_op;
341}
342
343static void parse_config(const char *fn, char *s)
344{
345 struct parse_state state;
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];
349 int nargs;
350
351 nargs = 0;
352 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800353 state.line = 0;
Colin Cross6310a822010-04-20 14:29:05 -0700354 state.ptr = s;
355 state.nexttoken = 0;
356 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800357
358 list_init(&import_list);
359 state.priv = &import_list;
360
Colin Cross6310a822010-04-20 14:29:05 -0700361 for (;;) {
362 switch (next_token(&state)) {
363 case T_EOF:
364 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800365 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700366 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800367 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700368 if (nargs) {
369 int kw = lookup_keyword(args[0]);
370 if (kw_is(kw, SECTION)) {
371 state.parse_line(&state, 0, 0);
372 parse_new_section(&state, kw, nargs, args);
373 } else {
374 state.parse_line(&state, nargs, args);
375 }
376 nargs = 0;
377 }
378 break;
379 case T_TEXT:
380 if (nargs < INIT_PARSER_MAXARGS) {
381 args[nargs++] = state.text;
382 }
383 break;
384 }
385 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800386
387parser_done:
388 list_for_each(node, &import_list) {
389 struct import *import = node_to_item(node, struct import, list);
390 int ret;
391
392 INFO("importing '%s'", import->filename);
393 ret = init_parse_config_file(import->filename);
394 if (ret)
395 ERROR("could not import file '%s' from '%s'\n",
396 import->filename, fn);
397 }
Colin Cross6310a822010-04-20 14:29:05 -0700398}
399
400int init_parse_config_file(const char *fn)
401{
402 char *data;
403 data = read_file(fn, 0);
404 if (!data) return -1;
405
406 parse_config(fn, data);
407 DUMP();
408 return 0;
409}
410
411static int valid_name(const char *name)
412{
413 if (strlen(name) > 16) {
414 return 0;
415 }
416 while (*name) {
417 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
418 return 0;
419 }
420 name++;
421 }
422 return 1;
423}
424
425struct service *service_find_by_name(const char *name)
426{
427 struct listnode *node;
428 struct service *svc;
429 list_for_each(node, &service_list) {
430 svc = node_to_item(node, struct service, slist);
431 if (!strcmp(svc->name, name)) {
432 return svc;
433 }
434 }
435 return 0;
436}
437
438struct service *service_find_by_pid(pid_t pid)
439{
440 struct listnode *node;
441 struct service *svc;
442 list_for_each(node, &service_list) {
443 svc = node_to_item(node, struct service, slist);
444 if (svc->pid == pid) {
445 return svc;
446 }
447 }
448 return 0;
449}
450
451struct service *service_find_by_keychord(int keychord_id)
452{
453 struct listnode *node;
454 struct service *svc;
455 list_for_each(node, &service_list) {
456 svc = node_to_item(node, struct service, slist);
457 if (svc->keychord_id == keychord_id) {
458 return svc;
459 }
460 }
461 return 0;
462}
463
464void service_for_each(void (*func)(struct service *svc))
465{
466 struct listnode *node;
467 struct service *svc;
468 list_for_each(node, &service_list) {
469 svc = node_to_item(node, struct service, slist);
470 func(svc);
471 }
472}
473
474void service_for_each_class(const char *classname,
475 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 if (!strcmp(svc->classname, classname)) {
482 func(svc);
483 }
484 }
485}
486
487void service_for_each_flags(unsigned matchflags,
488 void (*func)(struct service *svc))
489{
490 struct listnode *node;
491 struct service *svc;
492 list_for_each(node, &service_list) {
493 svc = node_to_item(node, struct service, slist);
494 if (svc->flags & matchflags) {
495 func(svc);
496 }
497 }
498}
499
500void action_for_each_trigger(const char *trigger,
501 void (*func)(struct action *act))
502{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700503 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700504 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700505 struct trigger *cur_trigger;
506
Colin Cross6310a822010-04-20 14:29:05 -0700507 list_for_each(node, &action_list) {
508 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700509 list_for_each(node2, &act->triggers) {
510 cur_trigger = node_to_item(node2, struct trigger, nlist);
511 if (!strcmp(cur_trigger->name, trigger)) {
512 func(act);
513 }
Colin Cross6310a822010-04-20 14:29:05 -0700514 }
515 }
516}
517
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700518
Colin Cross6310a822010-04-20 14:29:05 -0700519void queue_property_triggers(const char *name, const char *value)
520{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700521 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700522 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700523 struct trigger *cur_trigger;
524 bool match;
525 int name_length;
526
Colin Cross6310a822010-04-20 14:29:05 -0700527 list_for_each(node, &action_list) {
528 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700529 match = !name;
530 list_for_each(node2, &act->triggers) {
531 cur_trigger = node_to_item(node2, struct trigger, nlist);
532 if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) {
533 const char *test = cur_trigger->name + strlen("property:");
534 if (!match) {
535 name_length = strlen(name);
536 if (!strncmp(name, test, name_length) &&
537 test[name_length] == '=' &&
538 (!strcmp(test + name_length + 1, value) ||
539 !strcmp(test + name_length + 1, "*"))) {
540 match = true;
541 continue;
542 }
543 } else {
544 const char* equals = strchr(test, '=');
545 if (equals) {
546 char prop_name[PROP_NAME_MAX + 1];
547 char value[PROP_VALUE_MAX];
548 int length = equals - test;
549 if (length <= PROP_NAME_MAX) {
550 int ret;
551 memcpy(prop_name, test, length);
552 prop_name[length] = 0;
Colin Cross6310a822010-04-20 14:29:05 -0700553
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700554 /* does the property exist, and match the trigger value? */
555 ret = property_get(prop_name, value);
556 if (ret > 0 && (!strcmp(equals + 1, value) ||
557 !strcmp(equals + 1, "*"))) {
558 continue;
559 }
560 }
561 }
562 }
563 }
564 match = false;
565 break;
566 }
567 if (match) {
568 action_add_queue_tail(act);
Colin Cross6310a822010-04-20 14:29:05 -0700569 }
570 }
571}
572
573void queue_all_property_triggers()
574{
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700575 queue_property_triggers(NULL, NULL);
Colin Cross6310a822010-04-20 14:29:05 -0700576}
577
578void queue_builtin_action(int (*func)(int nargs, char **args), char *name)
579{
580 struct action *act;
581 struct command *cmd;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700582 struct trigger *cur_trigger;
Colin Cross6310a822010-04-20 14:29:05 -0700583
584 act = calloc(1, sizeof(*act));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700585 cur_trigger = calloc(1, sizeof(*cur_trigger));
586 cur_trigger->name = name;
587 list_init(&act->triggers);
588 list_add_tail(&act->triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700589 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800590 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700591
592 cmd = calloc(1, sizeof(*cmd));
593 cmd->func = func;
594 cmd->args[0] = name;
Riley Andrews24a3b782014-06-26 13:56:01 -0700595 cmd->nargs = 1;
Colin Cross6310a822010-04-20 14:29:05 -0700596 list_add_tail(&act->commands, &cmd->clist);
597
598 list_add_tail(&action_list, &act->alist);
599 action_add_queue_tail(act);
600}
601
602void action_add_queue_tail(struct action *act)
603{
Colin Crossa5064622013-03-07 13:42:29 -0800604 if (list_empty(&act->qlist)) {
605 list_add_tail(&action_queue, &act->qlist);
606 }
Colin Cross6310a822010-04-20 14:29:05 -0700607}
608
609struct action *action_remove_queue_head(void)
610{
611 if (list_empty(&action_queue)) {
612 return 0;
613 } else {
614 struct listnode *node = list_head(&action_queue);
615 struct action *act = node_to_item(node, struct action, qlist);
616 list_remove(node);
Colin Crossa5064622013-03-07 13:42:29 -0800617 list_init(node);
Colin Cross6310a822010-04-20 14:29:05 -0700618 return act;
619 }
620}
621
622int action_queue_empty()
623{
624 return list_empty(&action_queue);
625}
626
627static void *parse_service(struct parse_state *state, int nargs, char **args)
628{
629 struct service *svc;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700630 struct trigger *cur_trigger;
Colin Cross6310a822010-04-20 14:29:05 -0700631 if (nargs < 3) {
632 parse_error(state, "services must have a name and a program\n");
633 return 0;
634 }
635 if (!valid_name(args[1])) {
636 parse_error(state, "invalid service name '%s'\n", args[1]);
637 return 0;
638 }
639
640 svc = service_find_by_name(args[1]);
641 if (svc) {
642 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
643 return 0;
644 }
645
646 nargs -= 2;
647 svc = calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
648 if (!svc) {
649 parse_error(state, "out of memory\n");
650 return 0;
651 }
652 svc->name = args[1];
653 svc->classname = "default";
654 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700655 cur_trigger = calloc(1, sizeof(*cur_trigger));
Colin Cross6310a822010-04-20 14:29:05 -0700656 svc->args[nargs] = 0;
657 svc->nargs = nargs;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700658 list_init(&svc->onrestart.triggers);
659 cur_trigger->name = "onrestart";
660 list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700661 list_init(&svc->onrestart.commands);
662 list_add_tail(&service_list, &svc->slist);
663 return svc;
664}
665
666static void parse_line_service(struct parse_state *state, int nargs, char **args)
667{
668 struct service *svc = state->context;
669 struct command *cmd;
670 int i, kw, kw_nargs;
671
672 if (nargs == 0) {
673 return;
674 }
675
676 svc->ioprio_class = IoSchedClass_NONE;
677
678 kw = lookup_keyword(args[0]);
679 switch (kw) {
680 case K_capability:
681 break;
682 case K_class:
683 if (nargs != 2) {
684 parse_error(state, "class option requires a classname\n");
685 } else {
686 svc->classname = args[1];
687 }
688 break;
689 case K_console:
690 svc->flags |= SVC_CONSOLE;
691 break;
692 case K_disabled:
693 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700694 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700695 break;
696 case K_ioprio:
697 if (nargs != 3) {
698 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
699 } else {
700 svc->ioprio_pri = strtoul(args[2], 0, 8);
701
702 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
703 parse_error(state, "priority value must be range 0 - 7\n");
704 break;
705 }
706
707 if (!strcmp(args[1], "rt")) {
708 svc->ioprio_class = IoSchedClass_RT;
709 } else if (!strcmp(args[1], "be")) {
710 svc->ioprio_class = IoSchedClass_BE;
711 } else if (!strcmp(args[1], "idle")) {
712 svc->ioprio_class = IoSchedClass_IDLE;
713 } else {
714 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
715 }
716 }
717 break;
718 case K_group:
719 if (nargs < 2) {
720 parse_error(state, "group option requires a group id\n");
721 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
722 parse_error(state, "group option accepts at most %d supp. groups\n",
723 NR_SVC_SUPP_GIDS);
724 } else {
725 int n;
726 svc->gid = decode_uid(args[1]);
727 for (n = 2; n < nargs; n++) {
728 svc->supp_gids[n-2] = decode_uid(args[n]);
729 }
730 svc->nr_supp_gids = n - 2;
731 }
732 break;
733 case K_keycodes:
734 if (nargs < 2) {
735 parse_error(state, "keycodes option requires atleast one keycode\n");
736 } else {
737 svc->keycodes = malloc((nargs - 1) * sizeof(svc->keycodes[0]));
738 if (!svc->keycodes) {
739 parse_error(state, "could not allocate keycodes\n");
740 } else {
741 svc->nkeycodes = nargs - 1;
742 for (i = 1; i < nargs; i++) {
743 svc->keycodes[i - 1] = atoi(args[i]);
744 }
745 }
746 }
747 break;
748 case K_oneshot:
749 svc->flags |= SVC_ONESHOT;
750 break;
751 case K_onrestart:
752 nargs--;
753 args++;
754 kw = lookup_keyword(args[0]);
755 if (!kw_is(kw, COMMAND)) {
756 parse_error(state, "invalid command '%s'\n", args[0]);
757 break;
758 }
759 kw_nargs = kw_nargs(kw);
760 if (nargs < kw_nargs) {
761 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
762 kw_nargs > 2 ? "arguments" : "argument");
763 break;
764 }
765
766 cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
767 cmd->func = kw_func(kw);
768 cmd->nargs = nargs;
769 memcpy(cmd->args, args, sizeof(char*) * nargs);
770 list_add_tail(&svc->onrestart.commands, &cmd->clist);
771 break;
772 case K_critical:
773 svc->flags |= SVC_CRITICAL;
774 break;
775 case K_setenv: { /* name value */
776 struct svcenvinfo *ei;
Gavin.Changc3a46762013-09-28 00:22:11 +0800777 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700778 parse_error(state, "setenv option requires name and value arguments\n");
779 break;
780 }
781 ei = calloc(1, sizeof(*ei));
782 if (!ei) {
783 parse_error(state, "out of memory\n");
784 break;
785 }
786 ei->name = args[1];
787 ei->value = args[2];
788 ei->next = svc->envvars;
789 svc->envvars = ei;
790 break;
791 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400792 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700793 struct socketinfo *si;
794 if (nargs < 4) {
795 parse_error(state, "socket option requires name, type, perm arguments\n");
796 break;
797 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400798 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
799 && strcmp(args[2],"seqpacket")) {
800 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700801 break;
802 }
803 si = calloc(1, sizeof(*si));
804 if (!si) {
805 parse_error(state, "out of memory\n");
806 break;
807 }
808 si->name = args[1];
809 si->type = args[2];
810 si->perm = strtoul(args[3], 0, 8);
811 if (nargs > 4)
812 si->uid = decode_uid(args[4]);
813 if (nargs > 5)
814 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400815 if (nargs > 6)
816 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700817 si->next = svc->sockets;
818 svc->sockets = si;
819 break;
820 }
821 case K_user:
822 if (nargs != 2) {
823 parse_error(state, "user option requires a user id\n");
824 } else {
825 svc->uid = decode_uid(args[1]);
826 }
827 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500828 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500829 if (nargs != 2) {
830 parse_error(state, "seclabel option requires a label string\n");
831 } else {
832 svc->seclabel = args[1];
833 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500834 break;
835
Colin Cross6310a822010-04-20 14:29:05 -0700836 default:
837 parse_error(state, "invalid option '%s'\n", args[0]);
838 }
839}
840
841static void *parse_action(struct parse_state *state, int nargs, char **args)
842{
843 struct action *act;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700844 struct trigger *cur_trigger;
845 int i;
Colin Cross6310a822010-04-20 14:29:05 -0700846 if (nargs < 2) {
847 parse_error(state, "actions must have a trigger\n");
848 return 0;
849 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700850
Colin Cross6310a822010-04-20 14:29:05 -0700851 act = calloc(1, sizeof(*act));
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700852 list_init(&act->triggers);
853
854 for (i = 1; i < nargs; i++) {
855 if (!(i % 2)) {
856 if (strcmp(args[i], "&&")) {
857 parse_error(state, "& is the only symbol allowed to concatenate actions\n");
858 return 0;
859 } else
860 continue;
861 }
862 cur_trigger = calloc(1, sizeof(*cur_trigger));
863 cur_trigger->name = args[i];
864 list_add_tail(&act->triggers, &cur_trigger->nlist);
865 }
866
Colin Cross6310a822010-04-20 14:29:05 -0700867 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800868 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700869 list_add_tail(&action_list, &act->alist);
870 /* XXX add to hash */
871 return act;
872}
873
874static void parse_line_action(struct parse_state* state, int nargs, char **args)
875{
876 struct command *cmd;
877 struct action *act = state->context;
Colin Cross6310a822010-04-20 14:29:05 -0700878 int kw, n;
879
880 if (nargs == 0) {
881 return;
882 }
883
884 kw = lookup_keyword(args[0]);
885 if (!kw_is(kw, COMMAND)) {
886 parse_error(state, "invalid command '%s'\n", args[0]);
887 return;
888 }
889
890 n = kw_nargs(kw);
891 if (nargs < n) {
892 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
893 n > 2 ? "arguments" : "argument");
894 return;
895 }
896 cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
897 cmd->func = kw_func(kw);
Riley Andrews24a3b782014-06-26 13:56:01 -0700898 cmd->line = state->line;
899 cmd->filename = state->filename;
Colin Cross6310a822010-04-20 14:29:05 -0700900 cmd->nargs = nargs;
901 memcpy(cmd->args, args, sizeof(char*) * nargs);
902 list_add_tail(&act->commands, &cmd->clist);
903}