blob: 28bf30c14a70a41b8801e7d3372f0c9dc334a051 [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;
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;
Ken Sumrall203bad52011-01-18 17:37:41 -0800138 if (!strcmp(s, "mdir")) return K_rmdir;
139 if (!strcmp(s, "m")) return K_rm;
Colin Cross6310a822010-04-20 14:29:05 -0700140 break;
141 case 's':
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500142 if (!strcmp(s, "eclabel")) return K_seclabel;
Colin Cross6310a822010-04-20 14:29:05 -0700143 if (!strcmp(s, "ervice")) return K_service;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500144 if (!strcmp(s, "etcon")) return K_setcon;
145 if (!strcmp(s, "etenforce")) return K_setenforce;
Colin Cross6310a822010-04-20 14:29:05 -0700146 if (!strcmp(s, "etenv")) return K_setenv;
147 if (!strcmp(s, "etkey")) return K_setkey;
148 if (!strcmp(s, "etprop")) return K_setprop;
149 if (!strcmp(s, "etrlimit")) return K_setrlimit;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500150 if (!strcmp(s, "etsebool")) return K_setsebool;
Colin Cross6310a822010-04-20 14:29:05 -0700151 if (!strcmp(s, "ocket")) return K_socket;
152 if (!strcmp(s, "tart")) return K_start;
153 if (!strcmp(s, "top")) return K_stop;
154 if (!strcmp(s, "ymlink")) return K_symlink;
155 if (!strcmp(s, "ysclktz")) return K_sysclktz;
156 break;
157 case 't':
158 if (!strcmp(s, "rigger")) return K_trigger;
159 break;
160 case 'u':
161 if (!strcmp(s, "ser")) return K_user;
162 break;
163 case 'w':
164 if (!strcmp(s, "rite")) return K_write;
165 if (!strcmp(s, "ait")) return K_wait;
166 break;
167 }
168 return K_UNKNOWN;
169}
170
171void parse_line_no_op(struct parse_state *state, int nargs, char **args)
172{
173}
174
Dima Zavina6235ea2011-12-16 14:20:12 -0800175static int push_chars(char **dst, int *len, const char *chars, int cnt)
176{
177 if (cnt > *len)
178 return -1;
179
180 memcpy(*dst, chars, cnt);
181 *dst += cnt;
182 *len -= cnt;
183
184 return 0;
185}
186
Dima Zavin84bf9af2011-12-20 13:44:41 -0800187int expand_props(char *dst, const char *src, int dst_size)
Dima Zavina6235ea2011-12-16 14:20:12 -0800188{
189 int cnt = 0;
190 char *dst_ptr = dst;
191 const char *src_ptr = src;
192 int src_len;
193 int idx = 0;
194 int ret = 0;
195 int left = dst_size - 1;
196
197 if (!src || !dst || dst_size == 0)
198 return -1;
199
200 src_len = strlen(src);
201
202 /* - variables can either be $x.y or ${x.y}, in case they are only part
203 * of the string.
204 * - will accept $$ as a literal $.
205 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
206 * bad things will happen
207 */
208 while (*src_ptr && left > 0) {
209 char *c;
210 char prop[PROP_NAME_MAX + 1];
Colin Cross2deedfe2013-01-28 17:13:35 -0800211 char prop_val[PROP_VALUE_MAX];
Dima Zavina6235ea2011-12-16 14:20:12 -0800212 int prop_len = 0;
Colin Cross2deedfe2013-01-28 17:13:35 -0800213 int prop_val_len;
Dima Zavina6235ea2011-12-16 14:20:12 -0800214
215 c = strchr(src_ptr, '$');
216 if (!c) {
217 while (left-- > 0 && *src_ptr)
218 *(dst_ptr++) = *(src_ptr++);
219 break;
220 }
221
222 memset(prop, 0, sizeof(prop));
223
224 ret = push_chars(&dst_ptr, &left, src_ptr, c - src_ptr);
225 if (ret < 0)
226 goto err_nospace;
227 c++;
228
229 if (*c == '$') {
230 *(dst_ptr++) = *(c++);
231 src_ptr = c;
232 left--;
233 continue;
234 } else if (*c == '\0') {
235 break;
236 }
237
238 if (*c == '{') {
239 c++;
240 while (*c && *c != '}' && prop_len < PROP_NAME_MAX)
241 prop[prop_len++] = *(c++);
242 if (*c != '}') {
243 /* failed to find closing brace, abort. */
244 if (prop_len == PROP_NAME_MAX)
245 ERROR("prop name too long during expansion of '%s'\n",
246 src);
247 else if (*c == '\0')
248 ERROR("unexpected end of string in '%s', looking for }\n",
249 src);
250 goto err;
251 }
252 prop[prop_len] = '\0';
253 c++;
254 } else if (*c) {
255 while (*c && prop_len < PROP_NAME_MAX)
256 prop[prop_len++] = *(c++);
257 if (prop_len == PROP_NAME_MAX && *c != '\0') {
258 ERROR("prop name too long in '%s'\n", src);
259 goto err;
260 }
261 prop[prop_len] = '\0';
262 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
263 prop);
264 }
265
266 if (prop_len == 0) {
267 ERROR("invalid zero-length prop name in '%s'\n", src);
268 goto err;
269 }
270
Colin Cross2deedfe2013-01-28 17:13:35 -0800271 prop_val_len = property_get(prop, prop_val);
272 if (!prop_val_len) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800273 ERROR("property '%s' doesn't exist while expanding '%s'\n",
274 prop, src);
275 goto err;
276 }
277
Colin Cross2deedfe2013-01-28 17:13:35 -0800278 ret = push_chars(&dst_ptr, &left, prop_val, prop_val_len);
Dima Zavina6235ea2011-12-16 14:20:12 -0800279 if (ret < 0)
280 goto err_nospace;
281 src_ptr = c;
282 continue;
283 }
284
285 *dst_ptr = '\0';
286 return 0;
287
288err_nospace:
289 ERROR("destination buffer overflow while expanding '%s'\n", src);
290err:
291 return -1;
292}
293
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800294void parse_import(struct parse_state *state, int nargs, char **args)
295{
296 struct listnode *import_list = state->priv;
297 struct import *import;
298 char conf_file[PATH_MAX];
299 int ret;
300
301 if (nargs != 2) {
302 ERROR("single argument needed for import\n");
303 return;
304 }
305
306 ret = expand_props(conf_file, args[1], sizeof(conf_file));
307 if (ret) {
308 ERROR("error while handling import on line '%d' in '%s'\n",
309 state->line, state->filename);
310 return;
311 }
312
313 import = calloc(1, sizeof(struct import));
314 import->filename = strdup(conf_file);
315 list_add_tail(import_list, &import->list);
316 INFO("found import '%s', adding to import list", import->filename);
317}
318
Colin Cross6310a822010-04-20 14:29:05 -0700319void parse_new_section(struct parse_state *state, int kw,
320 int nargs, char **args)
321{
322 printf("[ %s %s ]\n", args[0],
323 nargs > 1 ? args[1] : "");
324 switch(kw) {
325 case K_service:
326 state->context = parse_service(state, nargs, args);
327 if (state->context) {
328 state->parse_line = parse_line_service;
329 return;
330 }
331 break;
332 case K_on:
333 state->context = parse_action(state, nargs, args);
334 if (state->context) {
335 state->parse_line = parse_line_action;
336 return;
337 }
338 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700339 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800340 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800341 break;
Colin Cross6310a822010-04-20 14:29:05 -0700342 }
343 state->parse_line = parse_line_no_op;
344}
345
346static void parse_config(const char *fn, char *s)
347{
348 struct parse_state state;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800349 struct listnode import_list;
350 struct listnode *node;
Colin Cross6310a822010-04-20 14:29:05 -0700351 char *args[INIT_PARSER_MAXARGS];
352 int nargs;
353
354 nargs = 0;
355 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800356 state.line = 0;
Colin Cross6310a822010-04-20 14:29:05 -0700357 state.ptr = s;
358 state.nexttoken = 0;
359 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800360
361 list_init(&import_list);
362 state.priv = &import_list;
363
Colin Cross6310a822010-04-20 14:29:05 -0700364 for (;;) {
365 switch (next_token(&state)) {
366 case T_EOF:
367 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800368 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700369 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800370 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700371 if (nargs) {
372 int kw = lookup_keyword(args[0]);
373 if (kw_is(kw, SECTION)) {
374 state.parse_line(&state, 0, 0);
375 parse_new_section(&state, kw, nargs, args);
376 } else {
377 state.parse_line(&state, nargs, args);
378 }
379 nargs = 0;
380 }
381 break;
382 case T_TEXT:
383 if (nargs < INIT_PARSER_MAXARGS) {
384 args[nargs++] = state.text;
385 }
386 break;
387 }
388 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800389
390parser_done:
391 list_for_each(node, &import_list) {
392 struct import *import = node_to_item(node, struct import, list);
393 int ret;
394
395 INFO("importing '%s'", import->filename);
396 ret = init_parse_config_file(import->filename);
397 if (ret)
398 ERROR("could not import file '%s' from '%s'\n",
399 import->filename, fn);
400 }
Colin Cross6310a822010-04-20 14:29:05 -0700401}
402
403int init_parse_config_file(const char *fn)
404{
405 char *data;
406 data = read_file(fn, 0);
407 if (!data) return -1;
408
409 parse_config(fn, data);
410 DUMP();
411 return 0;
412}
413
414static int valid_name(const char *name)
415{
416 if (strlen(name) > 16) {
417 return 0;
418 }
419 while (*name) {
420 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
421 return 0;
422 }
423 name++;
424 }
425 return 1;
426}
427
428struct service *service_find_by_name(const char *name)
429{
430 struct listnode *node;
431 struct service *svc;
432 list_for_each(node, &service_list) {
433 svc = node_to_item(node, struct service, slist);
434 if (!strcmp(svc->name, name)) {
435 return svc;
436 }
437 }
438 return 0;
439}
440
441struct service *service_find_by_pid(pid_t pid)
442{
443 struct listnode *node;
444 struct service *svc;
445 list_for_each(node, &service_list) {
446 svc = node_to_item(node, struct service, slist);
447 if (svc->pid == pid) {
448 return svc;
449 }
450 }
451 return 0;
452}
453
454struct service *service_find_by_keychord(int keychord_id)
455{
456 struct listnode *node;
457 struct service *svc;
458 list_for_each(node, &service_list) {
459 svc = node_to_item(node, struct service, slist);
460 if (svc->keychord_id == keychord_id) {
461 return svc;
462 }
463 }
464 return 0;
465}
466
467void service_for_each(void (*func)(struct service *svc))
468{
469 struct listnode *node;
470 struct service *svc;
471 list_for_each(node, &service_list) {
472 svc = node_to_item(node, struct service, slist);
473 func(svc);
474 }
475}
476
477void service_for_each_class(const char *classname,
478 void (*func)(struct service *svc))
479{
480 struct listnode *node;
481 struct service *svc;
482 list_for_each(node, &service_list) {
483 svc = node_to_item(node, struct service, slist);
484 if (!strcmp(svc->classname, classname)) {
485 func(svc);
486 }
487 }
488}
489
490void service_for_each_flags(unsigned matchflags,
491 void (*func)(struct service *svc))
492{
493 struct listnode *node;
494 struct service *svc;
495 list_for_each(node, &service_list) {
496 svc = node_to_item(node, struct service, slist);
497 if (svc->flags & matchflags) {
498 func(svc);
499 }
500 }
501}
502
503void action_for_each_trigger(const char *trigger,
504 void (*func)(struct action *act))
505{
506 struct listnode *node;
507 struct action *act;
508 list_for_each(node, &action_list) {
509 act = node_to_item(node, struct action, alist);
510 if (!strcmp(act->name, trigger)) {
511 func(act);
512 }
513 }
514}
515
516void queue_property_triggers(const char *name, const char *value)
517{
518 struct listnode *node;
519 struct action *act;
520 list_for_each(node, &action_list) {
521 act = node_to_item(node, struct action, alist);
522 if (!strncmp(act->name, "property:", strlen("property:"))) {
523 const char *test = act->name + strlen("property:");
524 int name_length = strlen(name);
525
526 if (!strncmp(name, test, name_length) &&
527 test[name_length] == '=' &&
Mike Lockwood7ba61b12011-06-07 18:16:55 -0700528 (!strcmp(test + name_length + 1, value) ||
529 !strcmp(test + name_length + 1, "*"))) {
Colin Cross6310a822010-04-20 14:29:05 -0700530 action_add_queue_tail(act);
531 }
532 }
533 }
534}
535
536void queue_all_property_triggers()
537{
538 struct listnode *node;
539 struct action *act;
540 list_for_each(node, &action_list) {
541 act = node_to_item(node, struct action, alist);
542 if (!strncmp(act->name, "property:", strlen("property:"))) {
543 /* parse property name and value
544 syntax is property:<name>=<value> */
545 const char* name = act->name + strlen("property:");
546 const char* equals = strchr(name, '=');
547 if (equals) {
548 char prop_name[PROP_NAME_MAX + 1];
Colin Cross2deedfe2013-01-28 17:13:35 -0800549 char value[PROP_VALUE_MAX];
Colin Cross6310a822010-04-20 14:29:05 -0700550 int length = equals - name;
551 if (length > PROP_NAME_MAX) {
552 ERROR("property name too long in trigger %s", act->name);
553 } else {
554 memcpy(prop_name, name, length);
555 prop_name[length] = 0;
556
557 /* does the property exist, and match the trigger value? */
Colin Cross2deedfe2013-01-28 17:13:35 -0800558 property_get(prop_name, value);
559 if (!strcmp(equals + 1, value) ||!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 Crossa5064622013-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 Crossa5064622013-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 Crossa5064622013-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 }
773 case K_socket: {/* name type perm [ uid gid ] */
774 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]);
796 si->next = svc->sockets;
797 svc->sockets = si;
798 break;
799 }
800 case K_user:
801 if (nargs != 2) {
802 parse_error(state, "user option requires a user id\n");
803 } else {
804 svc->uid = decode_uid(args[1]);
805 }
806 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500807 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500808 if (nargs != 2) {
809 parse_error(state, "seclabel option requires a label string\n");
810 } else {
811 svc->seclabel = args[1];
812 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500813 break;
814
Colin Cross6310a822010-04-20 14:29:05 -0700815 default:
816 parse_error(state, "invalid option '%s'\n", args[0]);
817 }
818}
819
820static void *parse_action(struct parse_state *state, int nargs, char **args)
821{
822 struct action *act;
823 if (nargs < 2) {
824 parse_error(state, "actions must have a trigger\n");
825 return 0;
826 }
827 if (nargs > 2) {
828 parse_error(state, "actions may not have extra parameters\n");
829 return 0;
830 }
831 act = calloc(1, sizeof(*act));
832 act->name = args[1];
833 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800834 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700835 list_add_tail(&action_list, &act->alist);
836 /* XXX add to hash */
837 return act;
838}
839
840static void parse_line_action(struct parse_state* state, int nargs, char **args)
841{
842 struct command *cmd;
843 struct action *act = state->context;
844 int (*func)(int nargs, char **args);
845 int kw, n;
846
847 if (nargs == 0) {
848 return;
849 }
850
851 kw = lookup_keyword(args[0]);
852 if (!kw_is(kw, COMMAND)) {
853 parse_error(state, "invalid command '%s'\n", args[0]);
854 return;
855 }
856
857 n = kw_nargs(kw);
858 if (nargs < n) {
859 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
860 n > 2 ? "arguments" : "argument");
861 return;
862 }
863 cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
864 cmd->func = kw_func(kw);
865 cmd->nargs = nargs;
866 memcpy(cmd->args, args, sizeof(char*) * nargs);
867 list_add_tail(&act->commands, &cmd->clist);
868}