blob: f412de7894d01c3cf31d7b90c65e2d47c74f9672 [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
36#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
37#include <sys/_system_properties.h>
38
39static list_declare(service_list);
40static list_declare(action_list);
41static list_declare(action_queue);
42
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
Greg Hackmannd68db712013-11-18 09:44:20 -080079static int lookup_keyword(const char *s)
Colin Cross6310a822010-04-20 14:29:05 -070080{
81 switch (*s++) {
82 case 'c':
83 if (!strcmp(s, "opy")) return K_copy;
84 if (!strcmp(s, "apability")) return K_capability;
85 if (!strcmp(s, "hdir")) return K_chdir;
86 if (!strcmp(s, "hroot")) return K_chroot;
87 if (!strcmp(s, "lass")) return K_class;
88 if (!strcmp(s, "lass_start")) return K_class_start;
89 if (!strcmp(s, "lass_stop")) return K_class_stop;
Ken Sumrall752923c2010-12-03 16:33:31 -080090 if (!strcmp(s, "lass_reset")) return K_class_reset;
Colin Cross6310a822010-04-20 14:29:05 -070091 if (!strcmp(s, "onsole")) return K_console;
92 if (!strcmp(s, "hown")) return K_chown;
93 if (!strcmp(s, "hmod")) return K_chmod;
94 if (!strcmp(s, "ritical")) return K_critical;
95 break;
96 case 'd':
97 if (!strcmp(s, "isabled")) return K_disabled;
98 if (!strcmp(s, "omainname")) return K_domainname;
99 break;
100 case 'e':
JP Abgrall3beec7e2014-05-02 21:14:29 -0700101 if (!strcmp(s, "nable")) return K_enable;
Colin Cross6310a822010-04-20 14:29:05 -0700102 if (!strcmp(s, "xec")) return K_exec;
103 if (!strcmp(s, "xport")) return K_export;
104 break;
105 case 'g':
106 if (!strcmp(s, "roup")) return K_group;
107 break;
108 case 'h':
109 if (!strcmp(s, "ostname")) return K_hostname;
110 break;
111 case 'i':
112 if (!strcmp(s, "oprio")) return K_ioprio;
113 if (!strcmp(s, "fup")) return K_ifup;
114 if (!strcmp(s, "nsmod")) return K_insmod;
115 if (!strcmp(s, "mport")) return K_import;
116 break;
117 case 'k':
118 if (!strcmp(s, "eycodes")) return K_keycodes;
119 break;
120 case 'l':
121 if (!strcmp(s, "oglevel")) return K_loglevel;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800122 if (!strcmp(s, "oad_persist_props")) return K_load_persist_props;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700123 if (!strcmp(s, "oad_all_props")) return K_load_all_props;
Colin Cross6310a822010-04-20 14:29:05 -0700124 break;
125 case 'm':
126 if (!strcmp(s, "kdir")) return K_mkdir;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700127 if (!strcmp(s, "ount_all")) return K_mount_all;
Colin Cross6310a822010-04-20 14:29:05 -0700128 if (!strcmp(s, "ount")) return K_mount;
129 break;
130 case 'o':
131 if (!strcmp(s, "n")) return K_on;
132 if (!strcmp(s, "neshot")) return K_oneshot;
133 if (!strcmp(s, "nrestart")) return K_onrestart;
134 break;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700135 case 'p':
136 if (!strcmp(s, "owerctl")) return K_powerctl;
Colin Cross6310a822010-04-20 14:29:05 -0700137 case 'r':
138 if (!strcmp(s, "estart")) return K_restart;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500139 if (!strcmp(s, "estorecon")) return K_restorecon;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400140 if (!strcmp(s, "estorecon_recursive")) return K_restorecon_recursive;
Ken Sumrall203bad52011-01-18 17:37:41 -0800141 if (!strcmp(s, "mdir")) return K_rmdir;
142 if (!strcmp(s, "m")) return K_rm;
Colin Cross6310a822010-04-20 14:29:05 -0700143 break;
144 case 's':
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500145 if (!strcmp(s, "eclabel")) return K_seclabel;
Colin Cross6310a822010-04-20 14:29:05 -0700146 if (!strcmp(s, "ervice")) return K_service;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500147 if (!strcmp(s, "etcon")) return K_setcon;
148 if (!strcmp(s, "etenforce")) return K_setenforce;
Colin Cross6310a822010-04-20 14:29:05 -0700149 if (!strcmp(s, "etenv")) return K_setenv;
150 if (!strcmp(s, "etkey")) return K_setkey;
151 if (!strcmp(s, "etprop")) return K_setprop;
152 if (!strcmp(s, "etrlimit")) return K_setrlimit;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500153 if (!strcmp(s, "etsebool")) return K_setsebool;
Colin Cross6310a822010-04-20 14:29:05 -0700154 if (!strcmp(s, "ocket")) return K_socket;
155 if (!strcmp(s, "tart")) return K_start;
156 if (!strcmp(s, "top")) return K_stop;
Ken Sumralla76baaa2013-07-09 18:42:09 -0700157 if (!strcmp(s, "wapon_all")) return K_swapon_all;
Colin Cross6310a822010-04-20 14:29:05 -0700158 if (!strcmp(s, "ymlink")) return K_symlink;
159 if (!strcmp(s, "ysclktz")) return K_sysclktz;
160 break;
161 case 't':
162 if (!strcmp(s, "rigger")) return K_trigger;
163 break;
164 case 'u':
165 if (!strcmp(s, "ser")) return K_user;
166 break;
167 case 'w':
168 if (!strcmp(s, "rite")) return K_write;
169 if (!strcmp(s, "ait")) return K_wait;
170 break;
171 }
172 return K_UNKNOWN;
173}
174
Greg Hackmannd68db712013-11-18 09:44:20 -0800175static void parse_line_no_op(struct parse_state *state, int nargs, char **args)
Colin Cross6310a822010-04-20 14:29:05 -0700176{
177}
178
Dima Zavina6235ea2011-12-16 14:20:12 -0800179static int push_chars(char **dst, int *len, const char *chars, int cnt)
180{
181 if (cnt > *len)
182 return -1;
183
184 memcpy(*dst, chars, cnt);
185 *dst += cnt;
186 *len -= cnt;
187
188 return 0;
189}
190
Dima Zavin84bf9af2011-12-20 13:44:41 -0800191int expand_props(char *dst, const char *src, int dst_size)
Dima Zavina6235ea2011-12-16 14:20:12 -0800192{
193 int cnt = 0;
194 char *dst_ptr = dst;
195 const char *src_ptr = src;
196 int src_len;
197 int idx = 0;
198 int ret = 0;
199 int left = dst_size - 1;
200
201 if (!src || !dst || dst_size == 0)
202 return -1;
203
204 src_len = strlen(src);
205
206 /* - variables can either be $x.y or ${x.y}, in case they are only part
207 * of the string.
208 * - will accept $$ as a literal $.
209 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
210 * bad things will happen
211 */
212 while (*src_ptr && left > 0) {
213 char *c;
214 char prop[PROP_NAME_MAX + 1];
Colin Cross2deedfe2013-01-28 17:13:35 -0800215 char prop_val[PROP_VALUE_MAX];
Dima Zavina6235ea2011-12-16 14:20:12 -0800216 int prop_len = 0;
Colin Cross2deedfe2013-01-28 17:13:35 -0800217 int prop_val_len;
Dima Zavina6235ea2011-12-16 14:20:12 -0800218
219 c = strchr(src_ptr, '$');
220 if (!c) {
221 while (left-- > 0 && *src_ptr)
222 *(dst_ptr++) = *(src_ptr++);
223 break;
224 }
225
226 memset(prop, 0, sizeof(prop));
227
228 ret = push_chars(&dst_ptr, &left, src_ptr, c - src_ptr);
229 if (ret < 0)
230 goto err_nospace;
231 c++;
232
233 if (*c == '$') {
234 *(dst_ptr++) = *(c++);
235 src_ptr = c;
236 left--;
237 continue;
238 } else if (*c == '\0') {
239 break;
240 }
241
242 if (*c == '{') {
243 c++;
244 while (*c && *c != '}' && prop_len < PROP_NAME_MAX)
245 prop[prop_len++] = *(c++);
246 if (*c != '}') {
247 /* failed to find closing brace, abort. */
248 if (prop_len == PROP_NAME_MAX)
249 ERROR("prop name too long during expansion of '%s'\n",
250 src);
251 else if (*c == '\0')
252 ERROR("unexpected end of string in '%s', looking for }\n",
253 src);
254 goto err;
255 }
256 prop[prop_len] = '\0';
257 c++;
258 } else if (*c) {
259 while (*c && prop_len < PROP_NAME_MAX)
260 prop[prop_len++] = *(c++);
261 if (prop_len == PROP_NAME_MAX && *c != '\0') {
262 ERROR("prop name too long in '%s'\n", src);
263 goto err;
264 }
265 prop[prop_len] = '\0';
266 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
267 prop);
268 }
269
270 if (prop_len == 0) {
271 ERROR("invalid zero-length prop name in '%s'\n", src);
272 goto err;
273 }
274
Colin Cross2deedfe2013-01-28 17:13:35 -0800275 prop_val_len = property_get(prop, prop_val);
276 if (!prop_val_len) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800277 ERROR("property '%s' doesn't exist while expanding '%s'\n",
278 prop, src);
279 goto err;
280 }
281
Colin Cross2deedfe2013-01-28 17:13:35 -0800282 ret = push_chars(&dst_ptr, &left, prop_val, prop_val_len);
Dima Zavina6235ea2011-12-16 14:20:12 -0800283 if (ret < 0)
284 goto err_nospace;
285 src_ptr = c;
286 continue;
287 }
288
289 *dst_ptr = '\0';
290 return 0;
291
292err_nospace:
293 ERROR("destination buffer overflow while expanding '%s'\n", src);
294err:
295 return -1;
296}
297
Greg Hackmannd68db712013-11-18 09:44:20 -0800298static void parse_import(struct parse_state *state, int nargs, char **args)
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800299{
300 struct listnode *import_list = state->priv;
301 struct import *import;
302 char conf_file[PATH_MAX];
303 int ret;
304
305 if (nargs != 2) {
306 ERROR("single argument needed for import\n");
307 return;
308 }
309
310 ret = expand_props(conf_file, args[1], sizeof(conf_file));
311 if (ret) {
312 ERROR("error while handling import on line '%d' in '%s'\n",
313 state->line, state->filename);
314 return;
315 }
316
317 import = calloc(1, sizeof(struct import));
318 import->filename = strdup(conf_file);
319 list_add_tail(import_list, &import->list);
320 INFO("found import '%s', adding to import list", import->filename);
321}
322
Greg Hackmannd68db712013-11-18 09:44:20 -0800323static void parse_new_section(struct parse_state *state, int kw,
Colin Cross6310a822010-04-20 14:29:05 -0700324 int nargs, char **args)
325{
326 printf("[ %s %s ]\n", args[0],
327 nargs > 1 ? args[1] : "");
328 switch(kw) {
329 case K_service:
330 state->context = parse_service(state, nargs, args);
331 if (state->context) {
332 state->parse_line = parse_line_service;
333 return;
334 }
335 break;
336 case K_on:
337 state->context = parse_action(state, nargs, args);
338 if (state->context) {
339 state->parse_line = parse_line_action;
340 return;
341 }
342 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700343 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800344 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800345 break;
Colin Cross6310a822010-04-20 14:29:05 -0700346 }
347 state->parse_line = parse_line_no_op;
348}
349
350static void parse_config(const char *fn, char *s)
351{
352 struct parse_state state;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800353 struct listnode import_list;
354 struct listnode *node;
Colin Cross6310a822010-04-20 14:29:05 -0700355 char *args[INIT_PARSER_MAXARGS];
356 int nargs;
357
358 nargs = 0;
359 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800360 state.line = 0;
Colin Cross6310a822010-04-20 14:29:05 -0700361 state.ptr = s;
362 state.nexttoken = 0;
363 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800364
365 list_init(&import_list);
366 state.priv = &import_list;
367
Colin Cross6310a822010-04-20 14:29:05 -0700368 for (;;) {
369 switch (next_token(&state)) {
370 case T_EOF:
371 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800372 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700373 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800374 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700375 if (nargs) {
376 int kw = lookup_keyword(args[0]);
377 if (kw_is(kw, SECTION)) {
378 state.parse_line(&state, 0, 0);
379 parse_new_section(&state, kw, nargs, args);
380 } else {
381 state.parse_line(&state, nargs, args);
382 }
383 nargs = 0;
384 }
385 break;
386 case T_TEXT:
387 if (nargs < INIT_PARSER_MAXARGS) {
388 args[nargs++] = state.text;
389 }
390 break;
391 }
392 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800393
394parser_done:
395 list_for_each(node, &import_list) {
396 struct import *import = node_to_item(node, struct import, list);
397 int ret;
398
399 INFO("importing '%s'", import->filename);
400 ret = init_parse_config_file(import->filename);
401 if (ret)
402 ERROR("could not import file '%s' from '%s'\n",
403 import->filename, fn);
404 }
Colin Cross6310a822010-04-20 14:29:05 -0700405}
406
407int init_parse_config_file(const char *fn)
408{
409 char *data;
410 data = read_file(fn, 0);
411 if (!data) return -1;
412
413 parse_config(fn, data);
414 DUMP();
415 return 0;
416}
417
418static int valid_name(const char *name)
419{
420 if (strlen(name) > 16) {
421 return 0;
422 }
423 while (*name) {
424 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
425 return 0;
426 }
427 name++;
428 }
429 return 1;
430}
431
432struct service *service_find_by_name(const char *name)
433{
434 struct listnode *node;
435 struct service *svc;
436 list_for_each(node, &service_list) {
437 svc = node_to_item(node, struct service, slist);
438 if (!strcmp(svc->name, name)) {
439 return svc;
440 }
441 }
442 return 0;
443}
444
445struct service *service_find_by_pid(pid_t pid)
446{
447 struct listnode *node;
448 struct service *svc;
449 list_for_each(node, &service_list) {
450 svc = node_to_item(node, struct service, slist);
451 if (svc->pid == pid) {
452 return svc;
453 }
454 }
455 return 0;
456}
457
458struct service *service_find_by_keychord(int keychord_id)
459{
460 struct listnode *node;
461 struct service *svc;
462 list_for_each(node, &service_list) {
463 svc = node_to_item(node, struct service, slist);
464 if (svc->keychord_id == keychord_id) {
465 return svc;
466 }
467 }
468 return 0;
469}
470
471void service_for_each(void (*func)(struct service *svc))
472{
473 struct listnode *node;
474 struct service *svc;
475 list_for_each(node, &service_list) {
476 svc = node_to_item(node, struct service, slist);
477 func(svc);
478 }
479}
480
481void service_for_each_class(const char *classname,
482 void (*func)(struct service *svc))
483{
484 struct listnode *node;
485 struct service *svc;
486 list_for_each(node, &service_list) {
487 svc = node_to_item(node, struct service, slist);
488 if (!strcmp(svc->classname, classname)) {
489 func(svc);
490 }
491 }
492}
493
494void service_for_each_flags(unsigned matchflags,
495 void (*func)(struct service *svc))
496{
497 struct listnode *node;
498 struct service *svc;
499 list_for_each(node, &service_list) {
500 svc = node_to_item(node, struct service, slist);
501 if (svc->flags & matchflags) {
502 func(svc);
503 }
504 }
505}
506
507void action_for_each_trigger(const char *trigger,
508 void (*func)(struct action *act))
509{
510 struct listnode *node;
511 struct action *act;
512 list_for_each(node, &action_list) {
513 act = node_to_item(node, struct action, alist);
514 if (!strcmp(act->name, trigger)) {
515 func(act);
516 }
517 }
518}
519
520void queue_property_triggers(const char *name, const char *value)
521{
522 struct listnode *node;
523 struct action *act;
524 list_for_each(node, &action_list) {
525 act = node_to_item(node, struct action, alist);
526 if (!strncmp(act->name, "property:", strlen("property:"))) {
527 const char *test = act->name + strlen("property:");
528 int name_length = strlen(name);
529
530 if (!strncmp(name, test, name_length) &&
531 test[name_length] == '=' &&
Mike Lockwood7ba61b12011-06-07 18:16:55 -0700532 (!strcmp(test + name_length + 1, value) ||
533 !strcmp(test + name_length + 1, "*"))) {
Colin Cross6310a822010-04-20 14:29:05 -0700534 action_add_queue_tail(act);
535 }
536 }
537 }
538}
539
540void queue_all_property_triggers()
541{
542 struct listnode *node;
543 struct action *act;
544 list_for_each(node, &action_list) {
545 act = node_to_item(node, struct action, alist);
546 if (!strncmp(act->name, "property:", strlen("property:"))) {
547 /* parse property name and value
548 syntax is property:<name>=<value> */
549 const char* name = act->name + strlen("property:");
550 const char* equals = strchr(name, '=');
551 if (equals) {
552 char prop_name[PROP_NAME_MAX + 1];
Colin Cross2deedfe2013-01-28 17:13:35 -0800553 char value[PROP_VALUE_MAX];
Colin Cross6310a822010-04-20 14:29:05 -0700554 int length = equals - name;
555 if (length > PROP_NAME_MAX) {
556 ERROR("property name too long in trigger %s", act->name);
557 } else {
Benoit Gobyd679e1b2013-09-24 14:42:26 -0700558 int ret;
Colin Cross6310a822010-04-20 14:29:05 -0700559 memcpy(prop_name, name, length);
560 prop_name[length] = 0;
561
562 /* does the property exist, and match the trigger value? */
Benoit Gobyd679e1b2013-09-24 14:42:26 -0700563 ret = property_get(prop_name, value);
564 if (ret > 0 && (!strcmp(equals + 1, value) ||
565 !strcmp(equals + 1, "*"))) {
Colin Cross6310a822010-04-20 14:29:05 -0700566 action_add_queue_tail(act);
567 }
568 }
569 }
570 }
571 }
572}
573
574void queue_builtin_action(int (*func)(int nargs, char **args), char *name)
575{
576 struct action *act;
577 struct command *cmd;
578
579 act = calloc(1, sizeof(*act));
580 act->name = name;
581 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800582 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700583
584 cmd = calloc(1, sizeof(*cmd));
585 cmd->func = func;
586 cmd->args[0] = name;
Riley Andrews24a3b782014-06-26 13:56:01 -0700587 cmd->nargs = 1;
Colin Cross6310a822010-04-20 14:29:05 -0700588 list_add_tail(&act->commands, &cmd->clist);
589
590 list_add_tail(&action_list, &act->alist);
591 action_add_queue_tail(act);
592}
593
594void action_add_queue_tail(struct action *act)
595{
Colin Crossa5064622013-03-07 13:42:29 -0800596 if (list_empty(&act->qlist)) {
597 list_add_tail(&action_queue, &act->qlist);
598 }
Colin Cross6310a822010-04-20 14:29:05 -0700599}
600
601struct action *action_remove_queue_head(void)
602{
603 if (list_empty(&action_queue)) {
604 return 0;
605 } else {
606 struct listnode *node = list_head(&action_queue);
607 struct action *act = node_to_item(node, struct action, qlist);
608 list_remove(node);
Colin Crossa5064622013-03-07 13:42:29 -0800609 list_init(node);
Colin Cross6310a822010-04-20 14:29:05 -0700610 return act;
611 }
612}
613
614int action_queue_empty()
615{
616 return list_empty(&action_queue);
617}
618
619static void *parse_service(struct parse_state *state, int nargs, char **args)
620{
621 struct service *svc;
622 if (nargs < 3) {
623 parse_error(state, "services must have a name and a program\n");
624 return 0;
625 }
626 if (!valid_name(args[1])) {
627 parse_error(state, "invalid service name '%s'\n", args[1]);
628 return 0;
629 }
630
631 svc = service_find_by_name(args[1]);
632 if (svc) {
633 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
634 return 0;
635 }
636
637 nargs -= 2;
638 svc = calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
639 if (!svc) {
640 parse_error(state, "out of memory\n");
641 return 0;
642 }
643 svc->name = args[1];
644 svc->classname = "default";
645 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
646 svc->args[nargs] = 0;
647 svc->nargs = nargs;
648 svc->onrestart.name = "onrestart";
649 list_init(&svc->onrestart.commands);
650 list_add_tail(&service_list, &svc->slist);
651 return svc;
652}
653
654static void parse_line_service(struct parse_state *state, int nargs, char **args)
655{
656 struct service *svc = state->context;
657 struct command *cmd;
658 int i, kw, kw_nargs;
659
660 if (nargs == 0) {
661 return;
662 }
663
664 svc->ioprio_class = IoSchedClass_NONE;
665
666 kw = lookup_keyword(args[0]);
667 switch (kw) {
668 case K_capability:
669 break;
670 case K_class:
671 if (nargs != 2) {
672 parse_error(state, "class option requires a classname\n");
673 } else {
674 svc->classname = args[1];
675 }
676 break;
677 case K_console:
678 svc->flags |= SVC_CONSOLE;
679 break;
680 case K_disabled:
681 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700682 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700683 break;
684 case K_ioprio:
685 if (nargs != 3) {
686 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
687 } else {
688 svc->ioprio_pri = strtoul(args[2], 0, 8);
689
690 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
691 parse_error(state, "priority value must be range 0 - 7\n");
692 break;
693 }
694
695 if (!strcmp(args[1], "rt")) {
696 svc->ioprio_class = IoSchedClass_RT;
697 } else if (!strcmp(args[1], "be")) {
698 svc->ioprio_class = IoSchedClass_BE;
699 } else if (!strcmp(args[1], "idle")) {
700 svc->ioprio_class = IoSchedClass_IDLE;
701 } else {
702 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
703 }
704 }
705 break;
706 case K_group:
707 if (nargs < 2) {
708 parse_error(state, "group option requires a group id\n");
709 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
710 parse_error(state, "group option accepts at most %d supp. groups\n",
711 NR_SVC_SUPP_GIDS);
712 } else {
713 int n;
714 svc->gid = decode_uid(args[1]);
715 for (n = 2; n < nargs; n++) {
716 svc->supp_gids[n-2] = decode_uid(args[n]);
717 }
718 svc->nr_supp_gids = n - 2;
719 }
720 break;
721 case K_keycodes:
722 if (nargs < 2) {
723 parse_error(state, "keycodes option requires atleast one keycode\n");
724 } else {
725 svc->keycodes = malloc((nargs - 1) * sizeof(svc->keycodes[0]));
726 if (!svc->keycodes) {
727 parse_error(state, "could not allocate keycodes\n");
728 } else {
729 svc->nkeycodes = nargs - 1;
730 for (i = 1; i < nargs; i++) {
731 svc->keycodes[i - 1] = atoi(args[i]);
732 }
733 }
734 }
735 break;
736 case K_oneshot:
737 svc->flags |= SVC_ONESHOT;
738 break;
739 case K_onrestart:
740 nargs--;
741 args++;
742 kw = lookup_keyword(args[0]);
743 if (!kw_is(kw, COMMAND)) {
744 parse_error(state, "invalid command '%s'\n", args[0]);
745 break;
746 }
747 kw_nargs = kw_nargs(kw);
748 if (nargs < kw_nargs) {
749 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
750 kw_nargs > 2 ? "arguments" : "argument");
751 break;
752 }
753
754 cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
755 cmd->func = kw_func(kw);
756 cmd->nargs = nargs;
757 memcpy(cmd->args, args, sizeof(char*) * nargs);
758 list_add_tail(&svc->onrestart.commands, &cmd->clist);
759 break;
760 case K_critical:
761 svc->flags |= SVC_CRITICAL;
762 break;
763 case K_setenv: { /* name value */
764 struct svcenvinfo *ei;
Gavin.Changc3a46762013-09-28 00:22:11 +0800765 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700766 parse_error(state, "setenv option requires name and value arguments\n");
767 break;
768 }
769 ei = calloc(1, sizeof(*ei));
770 if (!ei) {
771 parse_error(state, "out of memory\n");
772 break;
773 }
774 ei->name = args[1];
775 ei->value = args[2];
776 ei->next = svc->envvars;
777 svc->envvars = ei;
778 break;
779 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400780 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700781 struct socketinfo *si;
782 if (nargs < 4) {
783 parse_error(state, "socket option requires name, type, perm arguments\n");
784 break;
785 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400786 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
787 && strcmp(args[2],"seqpacket")) {
788 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700789 break;
790 }
791 si = calloc(1, sizeof(*si));
792 if (!si) {
793 parse_error(state, "out of memory\n");
794 break;
795 }
796 si->name = args[1];
797 si->type = args[2];
798 si->perm = strtoul(args[3], 0, 8);
799 if (nargs > 4)
800 si->uid = decode_uid(args[4]);
801 if (nargs > 5)
802 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400803 if (nargs > 6)
804 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700805 si->next = svc->sockets;
806 svc->sockets = si;
807 break;
808 }
809 case K_user:
810 if (nargs != 2) {
811 parse_error(state, "user option requires a user id\n");
812 } else {
813 svc->uid = decode_uid(args[1]);
814 }
815 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500816 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500817 if (nargs != 2) {
818 parse_error(state, "seclabel option requires a label string\n");
819 } else {
820 svc->seclabel = args[1];
821 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500822 break;
823
Colin Cross6310a822010-04-20 14:29:05 -0700824 default:
825 parse_error(state, "invalid option '%s'\n", args[0]);
826 }
827}
828
829static void *parse_action(struct parse_state *state, int nargs, char **args)
830{
831 struct action *act;
832 if (nargs < 2) {
833 parse_error(state, "actions must have a trigger\n");
834 return 0;
835 }
836 if (nargs > 2) {
837 parse_error(state, "actions may not have extra parameters\n");
838 return 0;
839 }
840 act = calloc(1, sizeof(*act));
841 act->name = args[1];
842 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800843 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700844 list_add_tail(&action_list, &act->alist);
845 /* XXX add to hash */
846 return act;
847}
848
849static void parse_line_action(struct parse_state* state, int nargs, char **args)
850{
851 struct command *cmd;
852 struct action *act = state->context;
853 int (*func)(int nargs, char **args);
854 int kw, n;
855
856 if (nargs == 0) {
857 return;
858 }
859
860 kw = lookup_keyword(args[0]);
861 if (!kw_is(kw, COMMAND)) {
862 parse_error(state, "invalid command '%s'\n", args[0]);
863 return;
864 }
865
866 n = kw_nargs(kw);
867 if (nargs < n) {
868 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
869 n > 2 ? "arguments" : "argument");
870 return;
871 }
872 cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
873 cmd->func = kw_func(kw);
Riley Andrews24a3b782014-06-26 13:56:01 -0700874 cmd->line = state->line;
875 cmd->filename = state->filename;
Colin Cross6310a822010-04-20 14:29:05 -0700876 cmd->nargs = nargs;
877 memcpy(cmd->args, args, sizeof(char*) * nargs);
878 list_add_tail(&act->commands, &cmd->clist);
879}