blob: f49e6981dba8475d65d703bf24a09213804541f9 [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':
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;
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{
191 int cnt = 0;
192 char *dst_ptr = dst;
193 const char *src_ptr = src;
194 int src_len;
195 int idx = 0;
196 int ret = 0;
197 int left = dst_size - 1;
198
199 if (!src || !dst || dst_size == 0)
200 return -1;
201
202 src_len = strlen(src);
203
204 /* - variables can either be $x.y or ${x.y}, in case they are only part
205 * of the string.
206 * - will accept $$ as a literal $.
207 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
208 * bad things will happen
209 */
210 while (*src_ptr && left > 0) {
211 char *c;
212 char prop[PROP_NAME_MAX + 1];
Colin Cross2deedfe2013-01-28 17:13:35 -0800213 char prop_val[PROP_VALUE_MAX];
Dima Zavina6235ea2011-12-16 14:20:12 -0800214 int prop_len = 0;
Colin Cross2deedfe2013-01-28 17:13:35 -0800215 int prop_val_len;
Dima Zavina6235ea2011-12-16 14:20:12 -0800216
217 c = strchr(src_ptr, '$');
218 if (!c) {
219 while (left-- > 0 && *src_ptr)
220 *(dst_ptr++) = *(src_ptr++);
221 break;
222 }
223
224 memset(prop, 0, sizeof(prop));
225
226 ret = push_chars(&dst_ptr, &left, src_ptr, c - src_ptr);
227 if (ret < 0)
228 goto err_nospace;
229 c++;
230
231 if (*c == '$') {
232 *(dst_ptr++) = *(c++);
233 src_ptr = c;
234 left--;
235 continue;
236 } else if (*c == '\0') {
237 break;
238 }
239
240 if (*c == '{') {
241 c++;
242 while (*c && *c != '}' && prop_len < PROP_NAME_MAX)
243 prop[prop_len++] = *(c++);
244 if (*c != '}') {
245 /* failed to find closing brace, abort. */
246 if (prop_len == PROP_NAME_MAX)
247 ERROR("prop name too long during expansion of '%s'\n",
248 src);
249 else if (*c == '\0')
250 ERROR("unexpected end of string in '%s', looking for }\n",
251 src);
252 goto err;
253 }
254 prop[prop_len] = '\0';
255 c++;
256 } else if (*c) {
257 while (*c && prop_len < PROP_NAME_MAX)
258 prop[prop_len++] = *(c++);
259 if (prop_len == PROP_NAME_MAX && *c != '\0') {
260 ERROR("prop name too long in '%s'\n", src);
261 goto err;
262 }
263 prop[prop_len] = '\0';
264 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
265 prop);
266 }
267
268 if (prop_len == 0) {
269 ERROR("invalid zero-length prop name in '%s'\n", src);
270 goto err;
271 }
272
Colin Cross2deedfe2013-01-28 17:13:35 -0800273 prop_val_len = property_get(prop, prop_val);
274 if (!prop_val_len) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800275 ERROR("property '%s' doesn't exist while expanding '%s'\n",
276 prop, src);
277 goto err;
278 }
279
Colin Cross2deedfe2013-01-28 17:13:35 -0800280 ret = push_chars(&dst_ptr, &left, prop_val, prop_val_len);
Dima Zavina6235ea2011-12-16 14:20:12 -0800281 if (ret < 0)
282 goto err_nospace;
283 src_ptr = c;
284 continue;
285 }
286
287 *dst_ptr = '\0';
288 return 0;
289
290err_nospace:
291 ERROR("destination buffer overflow while expanding '%s'\n", src);
292err:
293 return -1;
294}
295
Greg Hackmannd68db712013-11-18 09:44:20 -0800296static void parse_import(struct parse_state *state, int nargs, char **args)
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800297{
298 struct listnode *import_list = state->priv;
299 struct import *import;
300 char conf_file[PATH_MAX];
301 int ret;
302
303 if (nargs != 2) {
304 ERROR("single argument needed for import\n");
305 return;
306 }
307
308 ret = expand_props(conf_file, args[1], sizeof(conf_file));
309 if (ret) {
310 ERROR("error while handling import on line '%d' in '%s'\n",
311 state->line, state->filename);
312 return;
313 }
314
315 import = calloc(1, sizeof(struct import));
316 import->filename = strdup(conf_file);
317 list_add_tail(import_list, &import->list);
318 INFO("found import '%s', adding to import list", import->filename);
319}
320
Greg Hackmannd68db712013-11-18 09:44:20 -0800321static void parse_new_section(struct parse_state *state, int kw,
Colin Cross6310a822010-04-20 14:29:05 -0700322 int nargs, char **args)
323{
324 printf("[ %s %s ]\n", args[0],
325 nargs > 1 ? args[1] : "");
326 switch(kw) {
327 case K_service:
328 state->context = parse_service(state, nargs, args);
329 if (state->context) {
330 state->parse_line = parse_line_service;
331 return;
332 }
333 break;
334 case K_on:
335 state->context = parse_action(state, nargs, args);
336 if (state->context) {
337 state->parse_line = parse_line_action;
338 return;
339 }
340 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700341 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800342 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800343 break;
Colin Cross6310a822010-04-20 14:29:05 -0700344 }
345 state->parse_line = parse_line_no_op;
346}
347
348static void parse_config(const char *fn, char *s)
349{
350 struct parse_state state;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800351 struct listnode import_list;
352 struct listnode *node;
Colin Cross6310a822010-04-20 14:29:05 -0700353 char *args[INIT_PARSER_MAXARGS];
354 int nargs;
355
356 nargs = 0;
357 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800358 state.line = 0;
Colin Cross6310a822010-04-20 14:29:05 -0700359 state.ptr = s;
360 state.nexttoken = 0;
361 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800362
363 list_init(&import_list);
364 state.priv = &import_list;
365
Colin Cross6310a822010-04-20 14:29:05 -0700366 for (;;) {
367 switch (next_token(&state)) {
368 case T_EOF:
369 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800370 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700371 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800372 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700373 if (nargs) {
374 int kw = lookup_keyword(args[0]);
375 if (kw_is(kw, SECTION)) {
376 state.parse_line(&state, 0, 0);
377 parse_new_section(&state, kw, nargs, args);
378 } else {
379 state.parse_line(&state, nargs, args);
380 }
381 nargs = 0;
382 }
383 break;
384 case T_TEXT:
385 if (nargs < INIT_PARSER_MAXARGS) {
386 args[nargs++] = state.text;
387 }
388 break;
389 }
390 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800391
392parser_done:
393 list_for_each(node, &import_list) {
394 struct import *import = node_to_item(node, struct import, list);
395 int ret;
396
397 INFO("importing '%s'", import->filename);
398 ret = init_parse_config_file(import->filename);
399 if (ret)
400 ERROR("could not import file '%s' from '%s'\n",
401 import->filename, fn);
402 }
Colin Cross6310a822010-04-20 14:29:05 -0700403}
404
405int init_parse_config_file(const char *fn)
406{
407 char *data;
408 data = read_file(fn, 0);
409 if (!data) return -1;
410
411 parse_config(fn, data);
412 DUMP();
413 return 0;
414}
415
416static int valid_name(const char *name)
417{
418 if (strlen(name) > 16) {
419 return 0;
420 }
421 while (*name) {
422 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
423 return 0;
424 }
425 name++;
426 }
427 return 1;
428}
429
430struct service *service_find_by_name(const char *name)
431{
432 struct listnode *node;
433 struct service *svc;
434 list_for_each(node, &service_list) {
435 svc = node_to_item(node, struct service, slist);
436 if (!strcmp(svc->name, name)) {
437 return svc;
438 }
439 }
440 return 0;
441}
442
443struct service *service_find_by_pid(pid_t pid)
444{
445 struct listnode *node;
446 struct service *svc;
447 list_for_each(node, &service_list) {
448 svc = node_to_item(node, struct service, slist);
449 if (svc->pid == pid) {
450 return svc;
451 }
452 }
453 return 0;
454}
455
456struct service *service_find_by_keychord(int keychord_id)
457{
458 struct listnode *node;
459 struct service *svc;
460 list_for_each(node, &service_list) {
461 svc = node_to_item(node, struct service, slist);
462 if (svc->keychord_id == keychord_id) {
463 return svc;
464 }
465 }
466 return 0;
467}
468
469void service_for_each(void (*func)(struct service *svc))
470{
471 struct listnode *node;
472 struct service *svc;
473 list_for_each(node, &service_list) {
474 svc = node_to_item(node, struct service, slist);
475 func(svc);
476 }
477}
478
479void service_for_each_class(const char *classname,
480 void (*func)(struct service *svc))
481{
482 struct listnode *node;
483 struct service *svc;
484 list_for_each(node, &service_list) {
485 svc = node_to_item(node, struct service, slist);
486 if (!strcmp(svc->classname, classname)) {
487 func(svc);
488 }
489 }
490}
491
492void service_for_each_flags(unsigned matchflags,
493 void (*func)(struct service *svc))
494{
495 struct listnode *node;
496 struct service *svc;
497 list_for_each(node, &service_list) {
498 svc = node_to_item(node, struct service, slist);
499 if (svc->flags & matchflags) {
500 func(svc);
501 }
502 }
503}
504
505void action_for_each_trigger(const char *trigger,
506 void (*func)(struct action *act))
507{
508 struct listnode *node;
509 struct action *act;
510 list_for_each(node, &action_list) {
511 act = node_to_item(node, struct action, alist);
512 if (!strcmp(act->name, trigger)) {
513 func(act);
514 }
515 }
516}
517
518void queue_property_triggers(const char *name, const char *value)
519{
520 struct listnode *node;
521 struct action *act;
522 list_for_each(node, &action_list) {
523 act = node_to_item(node, struct action, alist);
524 if (!strncmp(act->name, "property:", strlen("property:"))) {
525 const char *test = act->name + strlen("property:");
526 int name_length = strlen(name);
527
528 if (!strncmp(name, test, name_length) &&
529 test[name_length] == '=' &&
Mike Lockwood7ba61b12011-06-07 18:16:55 -0700530 (!strcmp(test + name_length + 1, value) ||
531 !strcmp(test + name_length + 1, "*"))) {
Colin Cross6310a822010-04-20 14:29:05 -0700532 action_add_queue_tail(act);
533 }
534 }
535 }
536}
537
538void queue_all_property_triggers()
539{
540 struct listnode *node;
541 struct action *act;
542 list_for_each(node, &action_list) {
543 act = node_to_item(node, struct action, alist);
544 if (!strncmp(act->name, "property:", strlen("property:"))) {
545 /* parse property name and value
546 syntax is property:<name>=<value> */
547 const char* name = act->name + strlen("property:");
548 const char* equals = strchr(name, '=');
549 if (equals) {
550 char prop_name[PROP_NAME_MAX + 1];
Colin Cross2deedfe2013-01-28 17:13:35 -0800551 char value[PROP_VALUE_MAX];
Colin Cross6310a822010-04-20 14:29:05 -0700552 int length = equals - name;
553 if (length > PROP_NAME_MAX) {
554 ERROR("property name too long in trigger %s", act->name);
555 } else {
Benoit Gobyd679e1b2013-09-24 14:42:26 -0700556 int ret;
Colin Cross6310a822010-04-20 14:29:05 -0700557 memcpy(prop_name, name, length);
558 prop_name[length] = 0;
559
560 /* does the property exist, and match the trigger value? */
Benoit Gobyd679e1b2013-09-24 14:42:26 -0700561 ret = property_get(prop_name, value);
562 if (ret > 0 && (!strcmp(equals + 1, value) ||
563 !strcmp(equals + 1, "*"))) {
Colin Cross6310a822010-04-20 14:29:05 -0700564 action_add_queue_tail(act);
565 }
566 }
567 }
568 }
569 }
570}
571
572void queue_builtin_action(int (*func)(int nargs, char **args), char *name)
573{
574 struct action *act;
575 struct command *cmd;
576
577 act = calloc(1, sizeof(*act));
578 act->name = name;
579 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800580 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700581
582 cmd = calloc(1, sizeof(*cmd));
583 cmd->func = func;
584 cmd->args[0] = name;
585 list_add_tail(&act->commands, &cmd->clist);
586
587 list_add_tail(&action_list, &act->alist);
588 action_add_queue_tail(act);
589}
590
591void action_add_queue_tail(struct action *act)
592{
Colin Crossa5064622013-03-07 13:42:29 -0800593 if (list_empty(&act->qlist)) {
594 list_add_tail(&action_queue, &act->qlist);
595 }
Colin Cross6310a822010-04-20 14:29:05 -0700596}
597
598struct action *action_remove_queue_head(void)
599{
600 if (list_empty(&action_queue)) {
601 return 0;
602 } else {
603 struct listnode *node = list_head(&action_queue);
604 struct action *act = node_to_item(node, struct action, qlist);
605 list_remove(node);
Colin Crossa5064622013-03-07 13:42:29 -0800606 list_init(node);
Colin Cross6310a822010-04-20 14:29:05 -0700607 return act;
608 }
609}
610
611int action_queue_empty()
612{
613 return list_empty(&action_queue);
614}
615
616static void *parse_service(struct parse_state *state, int nargs, char **args)
617{
618 struct service *svc;
619 if (nargs < 3) {
620 parse_error(state, "services must have a name and a program\n");
621 return 0;
622 }
623 if (!valid_name(args[1])) {
624 parse_error(state, "invalid service name '%s'\n", args[1]);
625 return 0;
626 }
627
628 svc = service_find_by_name(args[1]);
629 if (svc) {
630 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
631 return 0;
632 }
633
634 nargs -= 2;
635 svc = calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
636 if (!svc) {
637 parse_error(state, "out of memory\n");
638 return 0;
639 }
640 svc->name = args[1];
641 svc->classname = "default";
642 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
643 svc->args[nargs] = 0;
644 svc->nargs = nargs;
645 svc->onrestart.name = "onrestart";
646 list_init(&svc->onrestart.commands);
647 list_add_tail(&service_list, &svc->slist);
648 return svc;
649}
650
651static void parse_line_service(struct parse_state *state, int nargs, char **args)
652{
653 struct service *svc = state->context;
654 struct command *cmd;
655 int i, kw, kw_nargs;
656
657 if (nargs == 0) {
658 return;
659 }
660
661 svc->ioprio_class = IoSchedClass_NONE;
662
663 kw = lookup_keyword(args[0]);
664 switch (kw) {
665 case K_capability:
666 break;
667 case K_class:
668 if (nargs != 2) {
669 parse_error(state, "class option requires a classname\n");
670 } else {
671 svc->classname = args[1];
672 }
673 break;
674 case K_console:
675 svc->flags |= SVC_CONSOLE;
676 break;
677 case K_disabled:
678 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700679 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700680 break;
681 case K_ioprio:
682 if (nargs != 3) {
683 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
684 } else {
685 svc->ioprio_pri = strtoul(args[2], 0, 8);
686
687 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
688 parse_error(state, "priority value must be range 0 - 7\n");
689 break;
690 }
691
692 if (!strcmp(args[1], "rt")) {
693 svc->ioprio_class = IoSchedClass_RT;
694 } else if (!strcmp(args[1], "be")) {
695 svc->ioprio_class = IoSchedClass_BE;
696 } else if (!strcmp(args[1], "idle")) {
697 svc->ioprio_class = IoSchedClass_IDLE;
698 } else {
699 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
700 }
701 }
702 break;
703 case K_group:
704 if (nargs < 2) {
705 parse_error(state, "group option requires a group id\n");
706 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
707 parse_error(state, "group option accepts at most %d supp. groups\n",
708 NR_SVC_SUPP_GIDS);
709 } else {
710 int n;
711 svc->gid = decode_uid(args[1]);
712 for (n = 2; n < nargs; n++) {
713 svc->supp_gids[n-2] = decode_uid(args[n]);
714 }
715 svc->nr_supp_gids = n - 2;
716 }
717 break;
718 case K_keycodes:
719 if (nargs < 2) {
720 parse_error(state, "keycodes option requires atleast one keycode\n");
721 } else {
722 svc->keycodes = malloc((nargs - 1) * sizeof(svc->keycodes[0]));
723 if (!svc->keycodes) {
724 parse_error(state, "could not allocate keycodes\n");
725 } else {
726 svc->nkeycodes = nargs - 1;
727 for (i = 1; i < nargs; i++) {
728 svc->keycodes[i - 1] = atoi(args[i]);
729 }
730 }
731 }
732 break;
733 case K_oneshot:
734 svc->flags |= SVC_ONESHOT;
735 break;
736 case K_onrestart:
737 nargs--;
738 args++;
739 kw = lookup_keyword(args[0]);
740 if (!kw_is(kw, COMMAND)) {
741 parse_error(state, "invalid command '%s'\n", args[0]);
742 break;
743 }
744 kw_nargs = kw_nargs(kw);
745 if (nargs < kw_nargs) {
746 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
747 kw_nargs > 2 ? "arguments" : "argument");
748 break;
749 }
750
751 cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
752 cmd->func = kw_func(kw);
753 cmd->nargs = nargs;
754 memcpy(cmd->args, args, sizeof(char*) * nargs);
755 list_add_tail(&svc->onrestart.commands, &cmd->clist);
756 break;
757 case K_critical:
758 svc->flags |= SVC_CRITICAL;
759 break;
760 case K_setenv: { /* name value */
761 struct svcenvinfo *ei;
762 if (nargs < 2) {
763 parse_error(state, "setenv option requires name and value arguments\n");
764 break;
765 }
766 ei = calloc(1, sizeof(*ei));
767 if (!ei) {
768 parse_error(state, "out of memory\n");
769 break;
770 }
771 ei->name = args[1];
772 ei->value = args[2];
773 ei->next = svc->envvars;
774 svc->envvars = ei;
775 break;
776 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400777 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700778 struct socketinfo *si;
779 if (nargs < 4) {
780 parse_error(state, "socket option requires name, type, perm arguments\n");
781 break;
782 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400783 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
784 && strcmp(args[2],"seqpacket")) {
785 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700786 break;
787 }
788 si = calloc(1, sizeof(*si));
789 if (!si) {
790 parse_error(state, "out of memory\n");
791 break;
792 }
793 si->name = args[1];
794 si->type = args[2];
795 si->perm = strtoul(args[3], 0, 8);
796 if (nargs > 4)
797 si->uid = decode_uid(args[4]);
798 if (nargs > 5)
799 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400800 if (nargs > 6)
801 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700802 si->next = svc->sockets;
803 svc->sockets = si;
804 break;
805 }
806 case K_user:
807 if (nargs != 2) {
808 parse_error(state, "user option requires a user id\n");
809 } else {
810 svc->uid = decode_uid(args[1]);
811 }
812 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500813 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500814 if (nargs != 2) {
815 parse_error(state, "seclabel option requires a label string\n");
816 } else {
817 svc->seclabel = args[1];
818 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500819 break;
820
Colin Cross6310a822010-04-20 14:29:05 -0700821 default:
822 parse_error(state, "invalid option '%s'\n", args[0]);
823 }
824}
825
826static void *parse_action(struct parse_state *state, int nargs, char **args)
827{
828 struct action *act;
829 if (nargs < 2) {
830 parse_error(state, "actions must have a trigger\n");
831 return 0;
832 }
833 if (nargs > 2) {
834 parse_error(state, "actions may not have extra parameters\n");
835 return 0;
836 }
837 act = calloc(1, sizeof(*act));
838 act->name = args[1];
839 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800840 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700841 list_add_tail(&action_list, &act->alist);
842 /* XXX add to hash */
843 return act;
844}
845
846static void parse_line_action(struct parse_state* state, int nargs, char **args)
847{
848 struct command *cmd;
849 struct action *act = state->context;
850 int (*func)(int nargs, char **args);
851 int kw, n;
852
853 if (nargs == 0) {
854 return;
855 }
856
857 kw = lookup_keyword(args[0]);
858 if (!kw_is(kw, COMMAND)) {
859 parse_error(state, "invalid command '%s'\n", args[0]);
860 return;
861 }
862
863 n = kw_nargs(kw);
864 if (nargs < n) {
865 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
866 n > 2 ? "arguments" : "argument");
867 return;
868 }
869 cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
870 cmd->func = kw_func(kw);
871 cmd->nargs = nargs;
872 memcpy(cmd->args, args, sizeof(char*) * nargs);
873 list_add_tail(&act->commands, &cmd->clist);
874}