blob: 869dff27c26d368d08af5d3e21eff5e16d898146 [file] [log] [blame]
Colin Cross6310a822010-04-20 14:29:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <fcntl.h>
21#include <stdarg.h>
22#include <string.h>
23#include <stddef.h>
24#include <ctype.h>
25
26#include "init.h"
27#include "parser.h"
28#include "init_parser.h"
29#include "log.h"
Colin Cross6310a822010-04-20 14:29:05 -070030#include "property_service.h"
31#include "util.h"
32
33#include <cutils/iosched_policy.h>
Dima Zavinda04c522011-09-01 17:09:44 -070034#include <cutils/list.h>
Colin Cross6310a822010-04-20 14:29:05 -070035
Colin Cross6310a822010-04-20 14:29:05 -070036static list_declare(service_list);
37static list_declare(action_list);
38static list_declare(action_queue);
39
Dima Zavin78a1b1f2011-12-20 12:53:48 -080040struct import {
41 struct listnode list;
42 const char *filename;
43};
44
Colin Cross6310a822010-04-20 14:29:05 -070045static void *parse_service(struct parse_state *state, int nargs, char **args);
46static void parse_line_service(struct parse_state *state, int nargs, char **args);
47
48static void *parse_action(struct parse_state *state, int nargs, char **args);
49static void parse_line_action(struct parse_state *state, int nargs, char **args);
50
51#define SECTION 0x01
52#define COMMAND 0x02
53#define OPTION 0x04
54
55#include "keywords.h"
56
57#define KEYWORD(symbol, flags, nargs, func) \
58 [ K_##symbol ] = { #symbol, func, nargs + 1, flags, },
59
Greg Hackmannd68db712013-11-18 09:44:20 -080060static struct {
Colin Cross6310a822010-04-20 14:29:05 -070061 const char *name;
62 int (*func)(int nargs, char **args);
63 unsigned char nargs;
64 unsigned char flags;
65} keyword_info[KEYWORD_COUNT] = {
66 [ K_UNKNOWN ] = { "unknown", 0, 0, 0 },
67#include "keywords.h"
68};
69#undef KEYWORD
70
71#define kw_is(kw, type) (keyword_info[kw].flags & (type))
72#define kw_name(kw) (keyword_info[kw].name)
73#define kw_func(kw) (keyword_info[kw].func)
74#define kw_nargs(kw) (keyword_info[kw].nargs)
75
Greg Hackmannd68db712013-11-18 09:44:20 -080076static int lookup_keyword(const char *s)
Colin Cross6310a822010-04-20 14:29:05 -070077{
78 switch (*s++) {
79 case 'c':
80 if (!strcmp(s, "opy")) return K_copy;
81 if (!strcmp(s, "apability")) return K_capability;
82 if (!strcmp(s, "hdir")) return K_chdir;
83 if (!strcmp(s, "hroot")) return K_chroot;
84 if (!strcmp(s, "lass")) return K_class;
85 if (!strcmp(s, "lass_start")) return K_class_start;
86 if (!strcmp(s, "lass_stop")) return K_class_stop;
Ken Sumrall752923c2010-12-03 16:33:31 -080087 if (!strcmp(s, "lass_reset")) return K_class_reset;
Colin Cross6310a822010-04-20 14:29:05 -070088 if (!strcmp(s, "onsole")) return K_console;
89 if (!strcmp(s, "hown")) return K_chown;
90 if (!strcmp(s, "hmod")) return K_chmod;
91 if (!strcmp(s, "ritical")) return K_critical;
92 break;
93 case 'd':
94 if (!strcmp(s, "isabled")) return K_disabled;
95 if (!strcmp(s, "omainname")) return K_domainname;
96 break;
97 case 'e':
JP Abgrall3beec7e2014-05-02 21:14:29 -070098 if (!strcmp(s, "nable")) return K_enable;
Colin Cross6310a822010-04-20 14:29:05 -070099 if (!strcmp(s, "xec")) return K_exec;
100 if (!strcmp(s, "xport")) return K_export;
101 break;
102 case 'g':
103 if (!strcmp(s, "roup")) return K_group;
104 break;
105 case 'h':
106 if (!strcmp(s, "ostname")) return K_hostname;
107 break;
108 case 'i':
109 if (!strcmp(s, "oprio")) return K_ioprio;
110 if (!strcmp(s, "fup")) return K_ifup;
111 if (!strcmp(s, "nsmod")) return K_insmod;
112 if (!strcmp(s, "mport")) return K_import;
113 break;
114 case 'k':
115 if (!strcmp(s, "eycodes")) return K_keycodes;
116 break;
117 case 'l':
118 if (!strcmp(s, "oglevel")) return K_loglevel;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800119 if (!strcmp(s, "oad_persist_props")) return K_load_persist_props;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700120 if (!strcmp(s, "oad_all_props")) return K_load_all_props;
Colin Cross6310a822010-04-20 14:29:05 -0700121 break;
122 case 'm':
123 if (!strcmp(s, "kdir")) return K_mkdir;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700124 if (!strcmp(s, "ount_all")) return K_mount_all;
Colin Cross6310a822010-04-20 14:29:05 -0700125 if (!strcmp(s, "ount")) return K_mount;
126 break;
127 case 'o':
128 if (!strcmp(s, "n")) return K_on;
129 if (!strcmp(s, "neshot")) return K_oneshot;
130 if (!strcmp(s, "nrestart")) return K_onrestart;
131 break;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700132 case 'p':
133 if (!strcmp(s, "owerctl")) return K_powerctl;
Colin Cross6310a822010-04-20 14:29:05 -0700134 case 'r':
135 if (!strcmp(s, "estart")) return K_restart;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500136 if (!strcmp(s, "estorecon")) return K_restorecon;
Stephen Smalley726e8f72013-10-09 16:02:09 -0400137 if (!strcmp(s, "estorecon_recursive")) return K_restorecon_recursive;
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;
Ken Sumralla76baaa2013-07-09 18:42:09 -0700154 if (!strcmp(s, "wapon_all")) return K_swapon_all;
Colin Cross6310a822010-04-20 14:29:05 -0700155 if (!strcmp(s, "ymlink")) return K_symlink;
156 if (!strcmp(s, "ysclktz")) return K_sysclktz;
157 break;
158 case 't':
159 if (!strcmp(s, "rigger")) return K_trigger;
160 break;
161 case 'u':
162 if (!strcmp(s, "ser")) return K_user;
163 break;
164 case 'w':
165 if (!strcmp(s, "rite")) return K_write;
166 if (!strcmp(s, "ait")) return K_wait;
167 break;
168 }
169 return K_UNKNOWN;
170}
171
Greg Hackmannd68db712013-11-18 09:44:20 -0800172static void parse_line_no_op(struct parse_state *state, int nargs, char **args)
Colin Cross6310a822010-04-20 14:29:05 -0700173{
174}
175
Dima Zavina6235ea2011-12-16 14:20:12 -0800176static int push_chars(char **dst, int *len, const char *chars, int cnt)
177{
178 if (cnt > *len)
179 return -1;
180
181 memcpy(*dst, chars, cnt);
182 *dst += cnt;
183 *len -= cnt;
184
185 return 0;
186}
187
Dima Zavin84bf9af2011-12-20 13:44:41 -0800188int expand_props(char *dst, const char *src, int dst_size)
Dima Zavina6235ea2011-12-16 14:20:12 -0800189{
190 int cnt = 0;
191 char *dst_ptr = dst;
192 const char *src_ptr = src;
193 int src_len;
194 int idx = 0;
195 int ret = 0;
196 int left = dst_size - 1;
197
198 if (!src || !dst || dst_size == 0)
199 return -1;
200
201 src_len = strlen(src);
202
203 /* - variables can either be $x.y or ${x.y}, in case they are only part
204 * of the string.
205 * - will accept $$ as a literal $.
206 * - no nested property expansion, i.e. ${foo.${bar}} is not supported,
207 * bad things will happen
208 */
209 while (*src_ptr && left > 0) {
210 char *c;
211 char prop[PROP_NAME_MAX + 1];
Colin Cross2deedfe2013-01-28 17:13:35 -0800212 char prop_val[PROP_VALUE_MAX];
Dima Zavina6235ea2011-12-16 14:20:12 -0800213 int prop_len = 0;
Colin Cross2deedfe2013-01-28 17:13:35 -0800214 int prop_val_len;
Dima Zavina6235ea2011-12-16 14:20:12 -0800215
216 c = strchr(src_ptr, '$');
217 if (!c) {
218 while (left-- > 0 && *src_ptr)
219 *(dst_ptr++) = *(src_ptr++);
220 break;
221 }
222
223 memset(prop, 0, sizeof(prop));
224
225 ret = push_chars(&dst_ptr, &left, src_ptr, c - src_ptr);
226 if (ret < 0)
227 goto err_nospace;
228 c++;
229
230 if (*c == '$') {
231 *(dst_ptr++) = *(c++);
232 src_ptr = c;
233 left--;
234 continue;
235 } else if (*c == '\0') {
236 break;
237 }
238
239 if (*c == '{') {
240 c++;
241 while (*c && *c != '}' && prop_len < PROP_NAME_MAX)
242 prop[prop_len++] = *(c++);
243 if (*c != '}') {
244 /* failed to find closing brace, abort. */
245 if (prop_len == PROP_NAME_MAX)
246 ERROR("prop name too long during expansion of '%s'\n",
247 src);
248 else if (*c == '\0')
249 ERROR("unexpected end of string in '%s', looking for }\n",
250 src);
251 goto err;
252 }
253 prop[prop_len] = '\0';
254 c++;
255 } else if (*c) {
256 while (*c && prop_len < PROP_NAME_MAX)
257 prop[prop_len++] = *(c++);
258 if (prop_len == PROP_NAME_MAX && *c != '\0') {
259 ERROR("prop name too long in '%s'\n", src);
260 goto err;
261 }
262 prop[prop_len] = '\0';
263 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
264 prop);
265 }
266
267 if (prop_len == 0) {
268 ERROR("invalid zero-length prop name in '%s'\n", src);
269 goto err;
270 }
271
Colin Cross2deedfe2013-01-28 17:13:35 -0800272 prop_val_len = property_get(prop, prop_val);
273 if (!prop_val_len) {
Dima Zavina6235ea2011-12-16 14:20:12 -0800274 ERROR("property '%s' doesn't exist while expanding '%s'\n",
275 prop, src);
276 goto err;
277 }
278
Colin Cross2deedfe2013-01-28 17:13:35 -0800279 ret = push_chars(&dst_ptr, &left, prop_val, prop_val_len);
Dima Zavina6235ea2011-12-16 14:20:12 -0800280 if (ret < 0)
281 goto err_nospace;
282 src_ptr = c;
283 continue;
284 }
285
286 *dst_ptr = '\0';
287 return 0;
288
289err_nospace:
290 ERROR("destination buffer overflow while expanding '%s'\n", src);
291err:
292 return -1;
293}
294
Greg Hackmannd68db712013-11-18 09:44:20 -0800295static void parse_import(struct parse_state *state, int nargs, char **args)
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800296{
297 struct listnode *import_list = state->priv;
298 struct import *import;
299 char conf_file[PATH_MAX];
300 int ret;
301
302 if (nargs != 2) {
303 ERROR("single argument needed for import\n");
304 return;
305 }
306
307 ret = expand_props(conf_file, args[1], sizeof(conf_file));
308 if (ret) {
309 ERROR("error while handling import on line '%d' in '%s'\n",
310 state->line, state->filename);
311 return;
312 }
313
314 import = calloc(1, sizeof(struct import));
315 import->filename = strdup(conf_file);
316 list_add_tail(import_list, &import->list);
317 INFO("found import '%s', adding to import list", import->filename);
318}
319
Greg Hackmannd68db712013-11-18 09:44:20 -0800320static void parse_new_section(struct parse_state *state, int kw,
Colin Cross6310a822010-04-20 14:29:05 -0700321 int nargs, char **args)
322{
323 printf("[ %s %s ]\n", args[0],
324 nargs > 1 ? args[1] : "");
325 switch(kw) {
326 case K_service:
327 state->context = parse_service(state, nargs, args);
328 if (state->context) {
329 state->parse_line = parse_line_service;
330 return;
331 }
332 break;
333 case K_on:
334 state->context = parse_action(state, nargs, args);
335 if (state->context) {
336 state->parse_line = parse_line_action;
337 return;
338 }
339 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700340 case K_import:
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800341 parse_import(state, nargs, args);
Dima Zavina6235ea2011-12-16 14:20:12 -0800342 break;
Colin Cross6310a822010-04-20 14:29:05 -0700343 }
344 state->parse_line = parse_line_no_op;
345}
346
347static void parse_config(const char *fn, char *s)
348{
349 struct parse_state state;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800350 struct listnode import_list;
351 struct listnode *node;
Colin Cross6310a822010-04-20 14:29:05 -0700352 char *args[INIT_PARSER_MAXARGS];
353 int nargs;
354
355 nargs = 0;
356 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800357 state.line = 0;
Colin Cross6310a822010-04-20 14:29:05 -0700358 state.ptr = s;
359 state.nexttoken = 0;
360 state.parse_line = parse_line_no_op;
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800361
362 list_init(&import_list);
363 state.priv = &import_list;
364
Colin Cross6310a822010-04-20 14:29:05 -0700365 for (;;) {
366 switch (next_token(&state)) {
367 case T_EOF:
368 state.parse_line(&state, 0, 0);
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800369 goto parser_done;
Colin Cross6310a822010-04-20 14:29:05 -0700370 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800371 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700372 if (nargs) {
373 int kw = lookup_keyword(args[0]);
374 if (kw_is(kw, SECTION)) {
375 state.parse_line(&state, 0, 0);
376 parse_new_section(&state, kw, nargs, args);
377 } else {
378 state.parse_line(&state, nargs, args);
379 }
380 nargs = 0;
381 }
382 break;
383 case T_TEXT:
384 if (nargs < INIT_PARSER_MAXARGS) {
385 args[nargs++] = state.text;
386 }
387 break;
388 }
389 }
Dima Zavin78a1b1f2011-12-20 12:53:48 -0800390
391parser_done:
392 list_for_each(node, &import_list) {
393 struct import *import = node_to_item(node, struct import, list);
394 int ret;
395
396 INFO("importing '%s'", import->filename);
397 ret = init_parse_config_file(import->filename);
398 if (ret)
399 ERROR("could not import file '%s' from '%s'\n",
400 import->filename, fn);
401 }
Colin Cross6310a822010-04-20 14:29:05 -0700402}
403
404int init_parse_config_file(const char *fn)
405{
406 char *data;
407 data = read_file(fn, 0);
408 if (!data) return -1;
409
410 parse_config(fn, data);
411 DUMP();
412 return 0;
413}
414
415static int valid_name(const char *name)
416{
417 if (strlen(name) > 16) {
418 return 0;
419 }
420 while (*name) {
421 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
422 return 0;
423 }
424 name++;
425 }
426 return 1;
427}
428
429struct service *service_find_by_name(const char *name)
430{
431 struct listnode *node;
432 struct service *svc;
433 list_for_each(node, &service_list) {
434 svc = node_to_item(node, struct service, slist);
435 if (!strcmp(svc->name, name)) {
436 return svc;
437 }
438 }
439 return 0;
440}
441
442struct service *service_find_by_pid(pid_t pid)
443{
444 struct listnode *node;
445 struct service *svc;
446 list_for_each(node, &service_list) {
447 svc = node_to_item(node, struct service, slist);
448 if (svc->pid == pid) {
449 return svc;
450 }
451 }
452 return 0;
453}
454
455struct service *service_find_by_keychord(int keychord_id)
456{
457 struct listnode *node;
458 struct service *svc;
459 list_for_each(node, &service_list) {
460 svc = node_to_item(node, struct service, slist);
461 if (svc->keychord_id == keychord_id) {
462 return svc;
463 }
464 }
465 return 0;
466}
467
468void service_for_each(void (*func)(struct service *svc))
469{
470 struct listnode *node;
471 struct service *svc;
472 list_for_each(node, &service_list) {
473 svc = node_to_item(node, struct service, slist);
474 func(svc);
475 }
476}
477
478void service_for_each_class(const char *classname,
479 void (*func)(struct service *svc))
480{
481 struct listnode *node;
482 struct service *svc;
483 list_for_each(node, &service_list) {
484 svc = node_to_item(node, struct service, slist);
485 if (!strcmp(svc->classname, classname)) {
486 func(svc);
487 }
488 }
489}
490
491void service_for_each_flags(unsigned matchflags,
492 void (*func)(struct service *svc))
493{
494 struct listnode *node;
495 struct service *svc;
496 list_for_each(node, &service_list) {
497 svc = node_to_item(node, struct service, slist);
498 if (svc->flags & matchflags) {
499 func(svc);
500 }
501 }
502}
503
504void action_for_each_trigger(const char *trigger,
505 void (*func)(struct action *act))
506{
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700507 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700508 struct action *act;
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700509 struct trigger *cur_trigger;
510
Colin Cross6310a822010-04-20 14:29:05 -0700511 list_for_each(node, &action_list) {
512 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700513 list_for_each(node2, &act->triggers) {
514 cur_trigger = node_to_item(node2, struct trigger, nlist);
515 if (!strcmp(cur_trigger->name, trigger)) {
516 func(act);
517 }
Colin Cross6310a822010-04-20 14:29:05 -0700518 }
519 }
520}
521
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700522
Colin Cross6310a822010-04-20 14:29:05 -0700523void queue_property_triggers(const char *name, const char *value)
524{
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700525 struct listnode *node, *node2;
Colin Cross6310a822010-04-20 14:29:05 -0700526 struct action *act;
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700527 struct trigger *cur_trigger;
528 bool match;
529 int name_length;
530
Colin Cross6310a822010-04-20 14:29:05 -0700531 list_for_each(node, &action_list) {
532 act = node_to_item(node, struct action, alist);
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700533 match = !name;
534 list_for_each(node2, &act->triggers) {
535 cur_trigger = node_to_item(node2, struct trigger, nlist);
536 if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) {
537 const char *test = cur_trigger->name + strlen("property:");
538 if (!match) {
539 name_length = strlen(name);
540 if (!strncmp(name, test, name_length) &&
541 test[name_length] == '=' &&
542 (!strcmp(test + name_length + 1, value) ||
543 !strcmp(test + name_length + 1, "*"))) {
544 match = true;
545 continue;
546 }
547 } else {
548 const char* equals = strchr(test, '=');
549 if (equals) {
550 char prop_name[PROP_NAME_MAX + 1];
551 char value[PROP_VALUE_MAX];
552 int length = equals - test;
553 if (length <= PROP_NAME_MAX) {
554 int ret;
555 memcpy(prop_name, test, length);
556 prop_name[length] = 0;
Colin Cross6310a822010-04-20 14:29:05 -0700557
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700558 /* does the property exist, and match the trigger value? */
559 ret = property_get(prop_name, value);
560 if (ret > 0 && (!strcmp(equals + 1, value) ||
561 !strcmp(equals + 1, "*"))) {
562 continue;
563 }
564 }
565 }
566 }
567 }
568 match = false;
569 break;
570 }
571 if (match) {
572 action_add_queue_tail(act);
Colin Cross6310a822010-04-20 14:29:05 -0700573 }
574 }
575}
576
577void queue_all_property_triggers()
578{
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700579 queue_property_triggers(NULL, NULL);
Colin Cross6310a822010-04-20 14:29:05 -0700580}
581
582void queue_builtin_action(int (*func)(int nargs, char **args), char *name)
583{
584 struct action *act;
585 struct command *cmd;
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700586 struct trigger *cur_trigger;
Colin Cross6310a822010-04-20 14:29:05 -0700587
588 act = calloc(1, sizeof(*act));
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700589 cur_trigger = calloc(1, sizeof(*cur_trigger));
590 cur_trigger->name = name;
591 list_init(&act->triggers);
592 list_add_tail(&act->triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700593 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800594 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700595
596 cmd = calloc(1, sizeof(*cmd));
597 cmd->func = func;
598 cmd->args[0] = name;
Riley Andrews24a3b782014-06-26 13:56:01 -0700599 cmd->nargs = 1;
Colin Cross6310a822010-04-20 14:29:05 -0700600 list_add_tail(&act->commands, &cmd->clist);
601
602 list_add_tail(&action_list, &act->alist);
603 action_add_queue_tail(act);
604}
605
606void action_add_queue_tail(struct action *act)
607{
Colin Crossa5064622013-03-07 13:42:29 -0800608 if (list_empty(&act->qlist)) {
609 list_add_tail(&action_queue, &act->qlist);
610 }
Colin Cross6310a822010-04-20 14:29:05 -0700611}
612
613struct action *action_remove_queue_head(void)
614{
615 if (list_empty(&action_queue)) {
616 return 0;
617 } else {
618 struct listnode *node = list_head(&action_queue);
619 struct action *act = node_to_item(node, struct action, qlist);
620 list_remove(node);
Colin Crossa5064622013-03-07 13:42:29 -0800621 list_init(node);
Colin Cross6310a822010-04-20 14:29:05 -0700622 return act;
623 }
624}
625
626int action_queue_empty()
627{
628 return list_empty(&action_queue);
629}
630
631static void *parse_service(struct parse_state *state, int nargs, char **args)
632{
633 struct service *svc;
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700634 struct trigger *cur_trigger;
Colin Cross6310a822010-04-20 14:29:05 -0700635 if (nargs < 3) {
636 parse_error(state, "services must have a name and a program\n");
637 return 0;
638 }
639 if (!valid_name(args[1])) {
640 parse_error(state, "invalid service name '%s'\n", args[1]);
641 return 0;
642 }
643
644 svc = service_find_by_name(args[1]);
645 if (svc) {
646 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
647 return 0;
648 }
649
650 nargs -= 2;
651 svc = calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
652 if (!svc) {
653 parse_error(state, "out of memory\n");
654 return 0;
655 }
656 svc->name = args[1];
657 svc->classname = "default";
658 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700659 cur_trigger = calloc(1, sizeof(*cur_trigger));
Colin Cross6310a822010-04-20 14:29:05 -0700660 svc->args[nargs] = 0;
661 svc->nargs = nargs;
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700662 list_init(&svc->onrestart.triggers);
663 cur_trigger->name = "onrestart";
664 list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist);
Colin Cross6310a822010-04-20 14:29:05 -0700665 list_init(&svc->onrestart.commands);
666 list_add_tail(&service_list, &svc->slist);
667 return svc;
668}
669
670static void parse_line_service(struct parse_state *state, int nargs, char **args)
671{
672 struct service *svc = state->context;
673 struct command *cmd;
674 int i, kw, kw_nargs;
675
676 if (nargs == 0) {
677 return;
678 }
679
680 svc->ioprio_class = IoSchedClass_NONE;
681
682 kw = lookup_keyword(args[0]);
683 switch (kw) {
684 case K_capability:
685 break;
686 case K_class:
687 if (nargs != 2) {
688 parse_error(state, "class option requires a classname\n");
689 } else {
690 svc->classname = args[1];
691 }
692 break;
693 case K_console:
694 svc->flags |= SVC_CONSOLE;
695 break;
696 case K_disabled:
697 svc->flags |= SVC_DISABLED;
Ken Sumralla2864802011-10-26 16:56:00 -0700698 svc->flags |= SVC_RC_DISABLED;
Colin Cross6310a822010-04-20 14:29:05 -0700699 break;
700 case K_ioprio:
701 if (nargs != 3) {
702 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
703 } else {
704 svc->ioprio_pri = strtoul(args[2], 0, 8);
705
706 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
707 parse_error(state, "priority value must be range 0 - 7\n");
708 break;
709 }
710
711 if (!strcmp(args[1], "rt")) {
712 svc->ioprio_class = IoSchedClass_RT;
713 } else if (!strcmp(args[1], "be")) {
714 svc->ioprio_class = IoSchedClass_BE;
715 } else if (!strcmp(args[1], "idle")) {
716 svc->ioprio_class = IoSchedClass_IDLE;
717 } else {
718 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
719 }
720 }
721 break;
722 case K_group:
723 if (nargs < 2) {
724 parse_error(state, "group option requires a group id\n");
725 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
726 parse_error(state, "group option accepts at most %d supp. groups\n",
727 NR_SVC_SUPP_GIDS);
728 } else {
729 int n;
730 svc->gid = decode_uid(args[1]);
731 for (n = 2; n < nargs; n++) {
732 svc->supp_gids[n-2] = decode_uid(args[n]);
733 }
734 svc->nr_supp_gids = n - 2;
735 }
736 break;
737 case K_keycodes:
738 if (nargs < 2) {
739 parse_error(state, "keycodes option requires atleast one keycode\n");
740 } else {
741 svc->keycodes = malloc((nargs - 1) * sizeof(svc->keycodes[0]));
742 if (!svc->keycodes) {
743 parse_error(state, "could not allocate keycodes\n");
744 } else {
745 svc->nkeycodes = nargs - 1;
746 for (i = 1; i < nargs; i++) {
747 svc->keycodes[i - 1] = atoi(args[i]);
748 }
749 }
750 }
751 break;
752 case K_oneshot:
753 svc->flags |= SVC_ONESHOT;
754 break;
755 case K_onrestart:
756 nargs--;
757 args++;
758 kw = lookup_keyword(args[0]);
759 if (!kw_is(kw, COMMAND)) {
760 parse_error(state, "invalid command '%s'\n", args[0]);
761 break;
762 }
763 kw_nargs = kw_nargs(kw);
764 if (nargs < kw_nargs) {
765 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
766 kw_nargs > 2 ? "arguments" : "argument");
767 break;
768 }
769
770 cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
771 cmd->func = kw_func(kw);
772 cmd->nargs = nargs;
773 memcpy(cmd->args, args, sizeof(char*) * nargs);
774 list_add_tail(&svc->onrestart.commands, &cmd->clist);
775 break;
776 case K_critical:
777 svc->flags |= SVC_CRITICAL;
778 break;
779 case K_setenv: { /* name value */
780 struct svcenvinfo *ei;
Gavin.Changc3a46762013-09-28 00:22:11 +0800781 if (nargs < 3) {
Colin Cross6310a822010-04-20 14:29:05 -0700782 parse_error(state, "setenv option requires name and value arguments\n");
783 break;
784 }
785 ei = calloc(1, sizeof(*ei));
786 if (!ei) {
787 parse_error(state, "out of memory\n");
788 break;
789 }
790 ei->name = args[1];
791 ei->value = args[2];
792 ei->next = svc->envvars;
793 svc->envvars = ei;
794 break;
795 }
Stephen Smalley8348d272013-05-13 12:37:04 -0400796 case K_socket: {/* name type perm [ uid gid context ] */
Colin Cross6310a822010-04-20 14:29:05 -0700797 struct socketinfo *si;
798 if (nargs < 4) {
799 parse_error(state, "socket option requires name, type, perm arguments\n");
800 break;
801 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400802 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
803 && strcmp(args[2],"seqpacket")) {
804 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700805 break;
806 }
807 si = calloc(1, sizeof(*si));
808 if (!si) {
809 parse_error(state, "out of memory\n");
810 break;
811 }
812 si->name = args[1];
813 si->type = args[2];
814 si->perm = strtoul(args[3], 0, 8);
815 if (nargs > 4)
816 si->uid = decode_uid(args[4]);
817 if (nargs > 5)
818 si->gid = decode_uid(args[5]);
Stephen Smalley8348d272013-05-13 12:37:04 -0400819 if (nargs > 6)
820 si->socketcon = args[6];
Colin Cross6310a822010-04-20 14:29:05 -0700821 si->next = svc->sockets;
822 svc->sockets = si;
823 break;
824 }
825 case K_user:
826 if (nargs != 2) {
827 parse_error(state, "user option requires a user id\n");
828 } else {
829 svc->uid = decode_uid(args[1]);
830 }
831 break;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500832 case K_seclabel:
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500833 if (nargs != 2) {
834 parse_error(state, "seclabel option requires a label string\n");
835 } else {
836 svc->seclabel = args[1];
837 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500838 break;
839
Colin Cross6310a822010-04-20 14:29:05 -0700840 default:
841 parse_error(state, "invalid option '%s'\n", args[0]);
842 }
843}
844
845static void *parse_action(struct parse_state *state, int nargs, char **args)
846{
847 struct action *act;
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700848 struct trigger *cur_trigger;
849 int i;
Colin Cross6310a822010-04-20 14:29:05 -0700850 if (nargs < 2) {
851 parse_error(state, "actions must have a trigger\n");
852 return 0;
853 }
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700854
Colin Cross6310a822010-04-20 14:29:05 -0700855 act = calloc(1, sizeof(*act));
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700856 list_init(&act->triggers);
857
858 for (i = 1; i < nargs; i++) {
859 if (!(i % 2)) {
860 if (strcmp(args[i], "&&")) {
861 parse_error(state, "& is the only symbol allowed to concatenate actions\n");
862 return 0;
863 } else
864 continue;
865 }
866 cur_trigger = calloc(1, sizeof(*cur_trigger));
867 cur_trigger->name = args[i];
868 list_add_tail(&act->triggers, &cur_trigger->nlist);
869 }
870
Colin Cross6310a822010-04-20 14:29:05 -0700871 list_init(&act->commands);
Colin Crossa5064622013-03-07 13:42:29 -0800872 list_init(&act->qlist);
Colin Cross6310a822010-04-20 14:29:05 -0700873 list_add_tail(&action_list, &act->alist);
874 /* XXX add to hash */
875 return act;
876}
877
878static void parse_line_action(struct parse_state* state, int nargs, char **args)
879{
880 struct command *cmd;
881 struct action *act = state->context;
882 int (*func)(int nargs, char **args);
883 int kw, n;
884
885 if (nargs == 0) {
886 return;
887 }
888
889 kw = lookup_keyword(args[0]);
890 if (!kw_is(kw, COMMAND)) {
891 parse_error(state, "invalid command '%s'\n", args[0]);
892 return;
893 }
894
895 n = kw_nargs(kw);
896 if (nargs < n) {
897 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
898 n > 2 ? "arguments" : "argument");
899 return;
900 }
901 cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
902 cmd->func = kw_func(kw);
Riley Andrews24a3b782014-06-26 13:56:01 -0700903 cmd->line = state->line;
904 cmd->filename = state->filename;
Colin Cross6310a822010-04-20 14:29:05 -0700905 cmd->nargs = nargs;
906 memcpy(cmd->args, args, sizeof(char*) * nargs);
907 list_add_tail(&act->commands, &cmd->clist);
908}