blob: 2c2c91c0ca2d0cb99f8c3a3884d83eb6248d26b7 [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
63struct {
64 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
79int lookup_keyword(const char *s)
80{
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':
101 if (!strcmp(s, "xec")) return K_exec;
102 if (!strcmp(s, "xport")) return K_export;
103 break;
104 case 'g':
105 if (!strcmp(s, "roup")) return K_group;
106 break;
107 case 'h':
108 if (!strcmp(s, "ostname")) return K_hostname;
109 break;
110 case 'i':
111 if (!strcmp(s, "oprio")) return K_ioprio;
112 if (!strcmp(s, "fup")) return K_ifup;
113 if (!strcmp(s, "nsmod")) return K_insmod;
114 if (!strcmp(s, "mport")) return K_import;
115 break;
116 case 'k':
117 if (!strcmp(s, "eycodes")) return K_keycodes;
118 break;
119 case 'l':
120 if (!strcmp(s, "oglevel")) return K_loglevel;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800121 if (!strcmp(s, "oad_persist_props")) return K_load_persist_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;
133 case 'r':
134 if (!strcmp(s, "estart")) return K_restart;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500135 if (!strcmp(s, "estorecon")) return K_restorecon;
Ken Sumrall203bad52011-01-18 17:37:41 -0800136 if (!strcmp(s, "mdir")) return K_rmdir;
137 if (!strcmp(s, "m")) return K_rm;
Colin Cross6310a822010-04-20 14:29:05 -0700138 break;
139 case 's':
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500140 if (!strcmp(s, "eclabel")) return K_seclabel;
Colin Cross6310a822010-04-20 14:29:05 -0700141 if (!strcmp(s, "ervice")) return K_service;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500142 if (!strcmp(s, "etcon")) return K_setcon;
143 if (!strcmp(s, "etenforce")) return K_setenforce;
Colin Cross6310a822010-04-20 14:29:05 -0700144 if (!strcmp(s, "etenv")) return K_setenv;
145 if (!strcmp(s, "etkey")) return K_setkey;
146 if (!strcmp(s, "etprop")) return K_setprop;
147 if (!strcmp(s, "etrlimit")) return K_setrlimit;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500148 if (!strcmp(s, "etsebool")) return K_setsebool;
Colin Cross6310a822010-04-20 14:29:05 -0700149 if (!strcmp(s, "ocket")) return K_socket;
150 if (!strcmp(s, "tart")) return K_start;
151 if (!strcmp(s, "top")) return K_stop;
152 if (!strcmp(s, "ymlink")) return K_symlink;
153 if (!strcmp(s, "ysclktz")) return K_sysclktz;
154 break;
155 case 't':
156 if (!strcmp(s, "rigger")) return K_trigger;
157 break;
158 case 'u':
159 if (!strcmp(s, "ser")) return K_user;
160 break;
161 case 'w':
162 if (!strcmp(s, "rite")) return K_write;
163 if (!strcmp(s, "ait")) return K_wait;
164 break;
165 }
166 return K_UNKNOWN;
167}
168
169void parse_line_no_op(struct parse_state *state, int nargs, char **args)
170{
171}
172
Dima Zavina6235ea2011-12-16 14:20:12 -0800173static int push_chars(char **dst, int *len, const char *chars, int cnt)
174{
175 if (cnt > *len)
176 return -1;
177
178 memcpy(*dst, chars, cnt);
179 *dst += cnt;
180 *len -= cnt;
181
182 return 0;
183}
184
Dima Zavin84bf9af2011-12-20 13:44:41 -0800185int expand_props(char *dst, const char *src, int dst_size)
Dima Zavina6235ea2011-12-16 14:20:12 -0800186{
187 int cnt = 0;
188 char *dst_ptr = dst;
189 const char *src_ptr = src;
190 int src_len;
191 int idx = 0;
192 int ret = 0;
193 int left = dst_size - 1;
194
195 if (!src || !dst || dst_size == 0)
196 return -1;
197
198 src_len = strlen(src);
199
200 /* - variables can either be $x.y or ${x.y}, in case they are only part
201 * of the string.
202 * - will accept $$ as a literal $.
203 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
204 * bad things will happen
205 */
206 while (*src_ptr && left > 0) {
207 char *c;
208 char prop[PROP_NAME_MAX + 1];
Colin Cross1a6f4c32013-01-28 17:13:35 -0800209 char prop_val[PROP_VALUE_MAX];
Dima Zavina6235ea2011-12-16 14:20:12 -0800210 int prop_len = 0;
Colin Cross1a6f4c32013-01-28 17:13:35 -0800211 int prop_val_len;
Dima Zavina6235ea2011-12-16 14:20:12 -0800212
213 c = strchr(src_ptr, '$');
214 if (!c) {
215 while (left-- > 0 && *src_ptr)
216 *(dst_ptr++) = *(src_ptr++);
217 break;
218 }
219
220 memset(prop, 0, sizeof(prop));
221
222 ret = push_chars(&dst_ptr, &left, src_ptr, c - src_ptr);
223 if (ret < 0)
224 goto err_nospace;
225 c++;
226
227 if (*c == '$') {
228 *(dst_ptr++) = *(c++);
229 src_ptr = c;
230 left--;
231 continue;
232 } else if (*c == '\0') {
233 break;
234 }
235
236 if (*c == '{') {
237 c++;
238 while (*c && *c != '}' && prop_len < PROP_NAME_MAX)
239 prop[prop_len++] = *(c++);
240 if (*c != '}') {
241 /* failed to find closing brace, abort. */
242 if (prop_len == PROP_NAME_MAX)
243 ERROR("prop name too long during expansion of '%s'\n",
244 src);
245 else if (*c == '\0')
246 ERROR("unexpected end of string in '%s', looking for }\n",
247 src);
248 goto err;
249 }
250 prop[prop_len] = '\0';
251 c++;
252 } else if (*c) {
253 while (*c && prop_len < PROP_NAME_MAX)
254 prop[prop_len++] = *(c++);
255 if (prop_len == PROP_NAME_MAX && *c != '\0') {
256 ERROR("prop name too long in '%s'\n", src);
257 goto err;
258 }
259 prop[prop_len] = '\0';
260 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
261 prop);
262 }
263
264 if (prop_len == 0) {
265 ERROR("invalid zero-length prop name in '%s'\n", src);
266 goto err;
267 }
268
Colin Cross1a6f4c32013-01-28 17:13:35 -0800269 prop_val_len = property_get(prop, prop_val);
270 if (!prop_val_len) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800271 ERROR("property '%s' doesn't exist while expanding '%s'\n",
272 prop, src);
273 goto err;
274 }
275
Colin Cross1a6f4c32013-01-28 17:13:35 -0800276 ret = push_chars(&dst_ptr, &left, prop_val, prop_val_len);
Dima Zavina6235ea2011-12-16 14:20:12 -0800277 if (ret < 0)
278 goto err_nospace;
279 src_ptr = c;
280 continue;
281 }
282
283 *dst_ptr = '\0';
284 return 0;
285
286err_nospace:
287 ERROR("destination buffer overflow while expanding '%s'\n", src);
288err:
289 return -1;
290}
291
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800292void parse_import(struct parse_state *state, int nargs, char **args)
293{
294 struct listnode *import_list = state->priv;
295 struct import *import;
296 char conf_file[PATH_MAX];
297 int ret;
298
299 if (nargs != 2) {
300 ERROR("single argument needed for import\n");
301 return;
302 }
303
304 ret = expand_props(conf_file, args[1], sizeof(conf_file));
305 if (ret) {
306 ERROR("error while handling import on line '%d' in '%s'\n",
307 state->line, state->filename);
308 return;
309 }
310
311 import = calloc(1, sizeof(struct import));
312 import->filename = strdup(conf_file);
313 list_add_tail(import_list, &import->list);
314 INFO("found import '%s', adding to import list", import->filename);
315}
316
Colin Cross6310a822010-04-20 14:29:05 -0700317void parse_new_section(struct parse_state *state, int kw,
318 int nargs, char **args)
319{
320 printf("[ %s %s ]\n", args[0],
321 nargs > 1 ? args[1] : "");
322 switch(kw) {
323 case K_service:
324 state->context = parse_service(state, nargs, args);
325 if (state->context) {
326 state->parse_line = parse_line_service;
327 return;
328 }
329 break;
330 case K_on:
331 state->context = parse_action(state, nargs, args);
332 if (state->context) {
333 state->parse_line = parse_line_action;
334 return;
335 }
336 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700337 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800338 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800339 break;
Colin Cross6310a822010-04-20 14:29:05 -0700340 }
341 state->parse_line = parse_line_no_op;
342}
343
344static void parse_config(const char *fn, char *s)
345{
346 struct parse_state state;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800347 struct listnode import_list;
348 struct listnode *node;
Colin Cross6310a822010-04-20 14:29:05 -0700349 char *args[INIT_PARSER_MAXARGS];
350 int nargs;
351
352 nargs = 0;
353 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800354 state.line = 0;
Colin Cross6310a822010-04-20 14:29:05 -0700355 state.ptr = s;
356 state.nexttoken = 0;
357 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800358
359 list_init(&import_list);
360 state.priv = &import_list;
361
Colin Cross6310a822010-04-20 14:29:05 -0700362 for (;;) {
363 switch (next_token(&state)) {
364 case T_EOF:
365 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800366 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700367 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800368 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700369 if (nargs) {
370 int kw = lookup_keyword(args[0]);
371 if (kw_is(kw, SECTION)) {
372 state.parse_line(&state, 0, 0);
373 parse_new_section(&state, kw, nargs, args);
374 } else {
375 state.parse_line(&state, nargs, args);
376 }
377 nargs = 0;
378 }
379 break;
380 case T_TEXT:
381 if (nargs < INIT_PARSER_MAXARGS) {
382 args[nargs++] = state.text;
383 }
384 break;
385 }
386 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800387
388parser_done:
389 list_for_each(node, &import_list) {
390 struct import *import = node_to_item(node, struct import, list);
391 int ret;
392
393 INFO("importing '%s'", import->filename);
394 ret = init_parse_config_file(import->filename);
395 if (ret)
396 ERROR("could not import file '%s' from '%s'\n",
397 import->filename, fn);
398 }
Colin Cross6310a822010-04-20 14:29:05 -0700399}
400
401int init_parse_config_file(const char *fn)
402{
403 char *data;
404 data = read_file(fn, 0);
405 if (!data) return -1;
406
407 parse_config(fn, data);
408 DUMP();
409 return 0;
410}
411
412static int valid_name(const char *name)
413{
414 if (strlen(name) > 16) {
415 return 0;
416 }
417 while (*name) {
418 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
419 return 0;
420 }
421 name++;
422 }
423 return 1;
424}
425
426struct service *service_find_by_name(const char *name)
427{
428 struct listnode *node;
429 struct service *svc;
430 list_for_each(node, &service_list) {
431 svc = node_to_item(node, struct service, slist);
432 if (!strcmp(svc->name, name)) {
433 return svc;
434 }
435 }
436 return 0;
437}
438
439struct service *service_find_by_pid(pid_t pid)
440{
441 struct listnode *node;
442 struct service *svc;
443 list_for_each(node, &service_list) {
444 svc = node_to_item(node, struct service, slist);
445 if (svc->pid == pid) {
446 return svc;
447 }
448 }
449 return 0;
450}
451
452struct service *service_find_by_keychord(int keychord_id)
453{
454 struct listnode *node;
455 struct service *svc;
456 list_for_each(node, &service_list) {
457 svc = node_to_item(node, struct service, slist);
458 if (svc->keychord_id == keychord_id) {
459 return svc;
460 }
461 }
462 return 0;
463}
464
465void service_for_each(void (*func)(struct service *svc))
466{
467 struct listnode *node;
468 struct service *svc;
469 list_for_each(node, &service_list) {
470 svc = node_to_item(node, struct service, slist);
471 func(svc);
472 }
473}
474
475void service_for_each_class(const char *classname,
476 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 if (!strcmp(svc->classname, classname)) {
483 func(svc);
484 }
485 }
486}
487
488void service_for_each_flags(unsigned matchflags,
489 void (*func)(struct service *svc))
490{
491 struct listnode *node;
492 struct service *svc;
493 list_for_each(node, &service_list) {
494 svc = node_to_item(node, struct service, slist);
495 if (svc->flags & matchflags) {
496 func(svc);
497 }
498 }
499}
500
501void action_for_each_trigger(const char *trigger,
502 void (*func)(struct action *act))
503{
504 struct listnode *node;
505 struct action *act;
506 list_for_each(node, &action_list) {
507 act = node_to_item(node, struct action, alist);
508 if (!strcmp(act->name, trigger)) {
509 func(act);
510 }
511 }
512}
513
514void queue_property_triggers(const char *name, const char *value)
515{
516 struct listnode *node;
517 struct action *act;
518 list_for_each(node, &action_list) {
519 act = node_to_item(node, struct action, alist);
520 if (!strncmp(act->name, "property:", strlen("property:"))) {
521 const char *test = act->name + strlen("property:");
522 int name_length = strlen(name);
523
524 if (!strncmp(name, test, name_length) &&
525 test[name_length] == '=' &&
Mike Lockwood7ba61b12011-06-07 18:16:55 -0700526 (!strcmp(test + name_length + 1, value) ||
527 !strcmp(test + name_length + 1, "*"))) {
Colin Cross6310a822010-04-20 14:29:05 -0700528 action_add_queue_tail(act);
529 }
530 }
531 }
532}
533
534void queue_all_property_triggers()
535{
536 struct listnode *node;
537 struct action *act;
538 list_for_each(node, &action_list) {
539 act = node_to_item(node, struct action, alist);
540 if (!strncmp(act->name, "property:", strlen("property:"))) {
541 /* parse property name and value
542 syntax is property:<name>=<value> */
543 const char* name = act->name + strlen("property:");
544 const char* equals = strchr(name, '=');
545 if (equals) {
546 char prop_name[PROP_NAME_MAX + 1];
Colin Cross1a6f4c32013-01-28 17:13:35 -0800547 char value[PROP_VALUE_MAX];
Colin Cross6310a822010-04-20 14:29:05 -0700548 int length = equals - name;
549 if (length > PROP_NAME_MAX) {
550 ERROR("property name too long in trigger %s", act->name);
551 } else {
Benoit Gobyd679e1b2013-09-24 14:42:26 -0700552 int ret;
Colin Cross6310a822010-04-20 14:29:05 -0700553 memcpy(prop_name, name, length);
554 prop_name[length] = 0;
555
556 /* does the property exist, and match the trigger value? */
Benoit Gobyd679e1b2013-09-24 14:42:26 -0700557 ret = property_get(prop_name, value);
558 if (ret > 0 && (!strcmp(equals + 1, value) ||
559 !strcmp(equals + 1, "*"))) {
Colin Cross6310a822010-04-20 14:29:05 -0700560 action_add_queue_tail(act);
561 }
562 }
563 }
564 }
565 }
566}
567
568void queue_builtin_action(int (*func)(int nargs, char **args), char *name)
569{
570 struct action *act;
571 struct command *cmd;
572
573 act = calloc(1, sizeof(*act));
574 act->name = name;
575 list_init(&act->commands);
Colin Crosse9ab1622013-03-07 13:42:29 -0800576 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700577
578 cmd = calloc(1, sizeof(*cmd));
579 cmd->func = func;
580 cmd->args[0] = name;
581 list_add_tail(&act->commands, &cmd->clist);
582
583 list_add_tail(&action_list, &act->alist);
584 action_add_queue_tail(act);
585}
586
587void action_add_queue_tail(struct action *act)
588{
Colin Crosse9ab1622013-03-07 13:42:29 -0800589 if (list_empty(&act->qlist)) {
590 list_add_tail(&action_queue, &act->qlist);
591 }
Colin Cross6310a822010-04-20 14:29:05 -0700592}
593
594struct action *action_remove_queue_head(void)
595{
596 if (list_empty(&action_queue)) {
597 return 0;
598 } else {
599 struct listnode *node = list_head(&action_queue);
600 struct action *act = node_to_item(node, struct action, qlist);
601 list_remove(node);
Colin Crosse9ab1622013-03-07 13:42:29 -0800602 list_init(node);
Colin Cross6310a822010-04-20 14:29:05 -0700603 return act;
604 }
605}
606
607int action_queue_empty()
608{
609 return list_empty(&action_queue);
610}
611
612static void *parse_service(struct parse_state *state, int nargs, char **args)
613{
614 struct service *svc;
615 if (nargs < 3) {
616 parse_error(state, "services must have a name and a program\n");
617 return 0;
618 }
619 if (!valid_name(args[1])) {
620 parse_error(state, "invalid service name '%s'\n", args[1]);
621 return 0;
622 }
623
624 svc = service_find_by_name(args[1]);
625 if (svc) {
626 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
627 return 0;
628 }
629
630 nargs -= 2;
631 svc = calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
632 if (!svc) {
633 parse_error(state, "out of memory\n");
634 return 0;
635 }
636 svc->name = args[1];
637 svc->classname = "default";
638 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
639 svc->args[nargs] = 0;
640 svc->nargs = nargs;
641 svc->onrestart.name = "onrestart";
642 list_init(&svc->onrestart.commands);
643 list_add_tail(&service_list, &svc->slist);
644 return svc;
645}
646
647static void parse_line_service(struct parse_state *state, int nargs, char **args)
648{
649 struct service *svc = state->context;
650 struct command *cmd;
651 int i, kw, kw_nargs;
652
653 if (nargs == 0) {
654 return;
655 }
656
657 svc->ioprio_class = IoSchedClass_NONE;
658
659 kw = lookup_keyword(args[0]);
660 switch (kw) {
661 case K_capability:
662 break;
663 case K_class:
664 if (nargs != 2) {
665 parse_error(state, "class option requires a classname\n");
666 } else {
667 svc->classname = args[1];
668 }
669 break;
670 case K_console:
671 svc->flags |= SVC_CONSOLE;
672 break;
673 case K_disabled:
674 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700675 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700676 break;
677 case K_ioprio:
678 if (nargs != 3) {
679 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
680 } else {
681 svc->ioprio_pri = strtoul(args[2], 0, 8);
682
683 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
684 parse_error(state, "priority value must be range 0 - 7\n");
685 break;
686 }
687
688 if (!strcmp(args[1], "rt")) {
689 svc->ioprio_class = IoSchedClass_RT;
690 } else if (!strcmp(args[1], "be")) {
691 svc->ioprio_class = IoSchedClass_BE;
692 } else if (!strcmp(args[1], "idle")) {
693 svc->ioprio_class = IoSchedClass_IDLE;
694 } else {
695 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
696 }
697 }
698 break;
699 case K_group:
700 if (nargs < 2) {
701 parse_error(state, "group option requires a group id\n");
702 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
703 parse_error(state, "group option accepts at most %d supp. groups\n",
704 NR_SVC_SUPP_GIDS);
705 } else {
706 int n;
707 svc->gid = decode_uid(args[1]);
708 for (n = 2; n < nargs; n++) {
709 svc->supp_gids[n-2] = decode_uid(args[n]);
710 }
711 svc->nr_supp_gids = n - 2;
712 }
713 break;
714 case K_keycodes:
715 if (nargs < 2) {
716 parse_error(state, "keycodes option requires atleast one keycode\n");
717 } else {
718 svc->keycodes = malloc((nargs - 1) * sizeof(svc->keycodes[0]));
719 if (!svc->keycodes) {
720 parse_error(state, "could not allocate keycodes\n");
721 } else {
722 svc->nkeycodes = nargs - 1;
723 for (i = 1; i < nargs; i++) {
724 svc->keycodes[i - 1] = atoi(args[i]);
725 }
726 }
727 }
728 break;
729 case K_oneshot:
730 svc->flags |= SVC_ONESHOT;
731 break;
732 case K_onrestart:
733 nargs--;
734 args++;
735 kw = lookup_keyword(args[0]);
736 if (!kw_is(kw, COMMAND)) {
737 parse_error(state, "invalid command '%s'\n", args[0]);
738 break;
739 }
740 kw_nargs = kw_nargs(kw);
741 if (nargs < kw_nargs) {
742 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
743 kw_nargs > 2 ? "arguments" : "argument");
744 break;
745 }
746
747 cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
748 cmd->func = kw_func(kw);
749 cmd->nargs = nargs;
750 memcpy(cmd->args, args, sizeof(char*) * nargs);
751 list_add_tail(&svc->onrestart.commands, &cmd->clist);
752 break;
753 case K_critical:
754 svc->flags |= SVC_CRITICAL;
755 break;
756 case K_setenv: { /* name value */
757 struct svcenvinfo *ei;
758 if (nargs < 2) {
759 parse_error(state, "setenv option requires name and value arguments\n");
760 break;
761 }
762 ei = calloc(1, sizeof(*ei));
763 if (!ei) {
764 parse_error(state, "out of memory\n");
765 break;
766 }
767 ei->name = args[1];
768 ei->value = args[2];
769 ei->next = svc->envvars;
770 svc->envvars = ei;
771 break;
772 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400773 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700774 struct socketinfo *si;
775 if (nargs < 4) {
776 parse_error(state, "socket option requires name, type, perm arguments\n");
777 break;
778 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400779 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
780 && strcmp(args[2],"seqpacket")) {
781 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700782 break;
783 }
784 si = calloc(1, sizeof(*si));
785 if (!si) {
786 parse_error(state, "out of memory\n");
787 break;
788 }
789 si->name = args[1];
790 si->type = args[2];
791 si->perm = strtoul(args[3], 0, 8);
792 if (nargs > 4)
793 si->uid = decode_uid(args[4]);
794 if (nargs > 5)
795 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400796 if (nargs > 6)
797 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700798 si->next = svc->sockets;
799 svc->sockets = si;
800 break;
801 }
802 case K_user:
803 if (nargs != 2) {
804 parse_error(state, "user option requires a user id\n");
805 } else {
806 svc->uid = decode_uid(args[1]);
807 }
808 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500809 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500810 if (nargs != 2) {
811 parse_error(state, "seclabel option requires a label string\n");
812 } else {
813 svc->seclabel = args[1];
814 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500815 break;
816
Colin Cross6310a822010-04-20 14:29:05 -0700817 default:
818 parse_error(state, "invalid option '%s'\n", args[0]);
819 }
820}
821
822static void *parse_action(struct parse_state *state, int nargs, char **args)
823{
824 struct action *act;
825 if (nargs < 2) {
826 parse_error(state, "actions must have a trigger\n");
827 return 0;
828 }
829 if (nargs > 2) {
830 parse_error(state, "actions may not have extra parameters\n");
831 return 0;
832 }
833 act = calloc(1, sizeof(*act));
834 act->name = args[1];
835 list_init(&act->commands);
Colin Crosse9ab1622013-03-07 13:42:29 -0800836 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700837 list_add_tail(&action_list, &act->alist);
838 /* XXX add to hash */
839 return act;
840}
841
842static void parse_line_action(struct parse_state* state, int nargs, char **args)
843{
844 struct command *cmd;
845 struct action *act = state->context;
846 int (*func)(int nargs, char **args);
847 int kw, n;
848
849 if (nargs == 0) {
850 return;
851 }
852
853 kw = lookup_keyword(args[0]);
854 if (!kw_is(kw, COMMAND)) {
855 parse_error(state, "invalid command '%s'\n", args[0]);
856 return;
857 }
858
859 n = kw_nargs(kw);
860 if (nargs < n) {
861 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
862 n > 2 ? "arguments" : "argument");
863 return;
864 }
865 cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
866 cmd->func = kw_func(kw);
867 cmd->nargs = nargs;
868 memcpy(cmd->args, args, sizeof(char*) * nargs);
869 list_add_tail(&act->commands, &cmd->clist);
870}