blob: d9d30842067b996876cd679cdb037c07596bcb12 [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"
30#include "list.h"
31#include "property_service.h"
32#include "util.h"
33
34#include <cutils/iosched_policy.h>
35
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
43static void *parse_service(struct parse_state *state, int nargs, char **args);
44static void parse_line_service(struct parse_state *state, int nargs, char **args);
45
46static void *parse_action(struct parse_state *state, int nargs, char **args);
47static void parse_line_action(struct parse_state *state, int nargs, char **args);
48
49#define SECTION 0x01
50#define COMMAND 0x02
51#define OPTION 0x04
52
53#include "keywords.h"
54
55#define KEYWORD(symbol, flags, nargs, func) \
56 [ K_##symbol ] = { #symbol, func, nargs + 1, flags, },
57
58struct {
59 const char *name;
60 int (*func)(int nargs, char **args);
61 unsigned char nargs;
62 unsigned char flags;
63} keyword_info[KEYWORD_COUNT] = {
64 [ K_UNKNOWN ] = { "unknown", 0, 0, 0 },
65#include "keywords.h"
66};
67#undef KEYWORD
68
69#define kw_is(kw, type) (keyword_info[kw].flags & (type))
70#define kw_name(kw) (keyword_info[kw].name)
71#define kw_func(kw) (keyword_info[kw].func)
72#define kw_nargs(kw) (keyword_info[kw].nargs)
73
74int lookup_keyword(const char *s)
75{
76 switch (*s++) {
77 case 'c':
78 if (!strcmp(s, "opy")) return K_copy;
79 if (!strcmp(s, "apability")) return K_capability;
80 if (!strcmp(s, "hdir")) return K_chdir;
81 if (!strcmp(s, "hroot")) return K_chroot;
82 if (!strcmp(s, "lass")) return K_class;
83 if (!strcmp(s, "lass_start")) return K_class_start;
84 if (!strcmp(s, "lass_stop")) return K_class_stop;
Ken Sumrall752923c2010-12-03 16:33:31 -080085 if (!strcmp(s, "lass_reset")) return K_class_reset;
Colin Cross6310a822010-04-20 14:29:05 -070086 if (!strcmp(s, "onsole")) return K_console;
87 if (!strcmp(s, "hown")) return K_chown;
88 if (!strcmp(s, "hmod")) return K_chmod;
89 if (!strcmp(s, "ritical")) return K_critical;
90 break;
91 case 'd':
92 if (!strcmp(s, "isabled")) return K_disabled;
93 if (!strcmp(s, "omainname")) return K_domainname;
94 break;
95 case 'e':
96 if (!strcmp(s, "xec")) return K_exec;
97 if (!strcmp(s, "xport")) return K_export;
98 break;
99 case 'g':
100 if (!strcmp(s, "roup")) return K_group;
101 break;
102 case 'h':
103 if (!strcmp(s, "ostname")) return K_hostname;
104 break;
105 case 'i':
106 if (!strcmp(s, "oprio")) return K_ioprio;
107 if (!strcmp(s, "fup")) return K_ifup;
108 if (!strcmp(s, "nsmod")) return K_insmod;
109 if (!strcmp(s, "mport")) return K_import;
110 break;
111 case 'k':
112 if (!strcmp(s, "eycodes")) return K_keycodes;
113 break;
114 case 'l':
115 if (!strcmp(s, "oglevel")) return K_loglevel;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800116 if (!strcmp(s, "oad_persist_props")) return K_load_persist_props;
Colin Cross6310a822010-04-20 14:29:05 -0700117 break;
118 case 'm':
119 if (!strcmp(s, "kdir")) return K_mkdir;
120 if (!strcmp(s, "ount")) return K_mount;
121 break;
122 case 'o':
123 if (!strcmp(s, "n")) return K_on;
124 if (!strcmp(s, "neshot")) return K_oneshot;
125 if (!strcmp(s, "nrestart")) return K_onrestart;
126 break;
127 case 'r':
128 if (!strcmp(s, "estart")) return K_restart;
Ken Sumrall203bad52011-01-18 17:37:41 -0800129 if (!strcmp(s, "mdir")) return K_rmdir;
130 if (!strcmp(s, "m")) return K_rm;
Colin Cross6310a822010-04-20 14:29:05 -0700131 break;
132 case 's':
133 if (!strcmp(s, "ervice")) return K_service;
134 if (!strcmp(s, "etenv")) return K_setenv;
135 if (!strcmp(s, "etkey")) return K_setkey;
136 if (!strcmp(s, "etprop")) return K_setprop;
137 if (!strcmp(s, "etrlimit")) return K_setrlimit;
138 if (!strcmp(s, "ocket")) return K_socket;
139 if (!strcmp(s, "tart")) return K_start;
140 if (!strcmp(s, "top")) return K_stop;
141 if (!strcmp(s, "ymlink")) return K_symlink;
142 if (!strcmp(s, "ysclktz")) return K_sysclktz;
143 break;
144 case 't':
145 if (!strcmp(s, "rigger")) return K_trigger;
146 break;
147 case 'u':
148 if (!strcmp(s, "ser")) return K_user;
149 break;
150 case 'w':
151 if (!strcmp(s, "rite")) return K_write;
152 if (!strcmp(s, "ait")) return K_wait;
153 break;
154 }
155 return K_UNKNOWN;
156}
157
158void parse_line_no_op(struct parse_state *state, int nargs, char **args)
159{
160}
161
162void parse_new_section(struct parse_state *state, int kw,
163 int nargs, char **args)
164{
165 printf("[ %s %s ]\n", args[0],
166 nargs > 1 ? args[1] : "");
167 switch(kw) {
168 case K_service:
169 state->context = parse_service(state, nargs, args);
170 if (state->context) {
171 state->parse_line = parse_line_service;
172 return;
173 }
174 break;
175 case K_on:
176 state->context = parse_action(state, nargs, args);
177 if (state->context) {
178 state->parse_line = parse_line_action;
179 return;
180 }
181 break;
Mike Lockwoodf5cb5b22011-06-07 20:08:40 -0700182 case K_import:
183 if (nargs != 2) {
184 ERROR("single argument needed for import\n");
185 } else {
186 int ret = init_parse_config_file(args[1]);
187 if (ret)
188 ERROR("could not import file %s\n", args[1]);
189 }
Colin Cross6310a822010-04-20 14:29:05 -0700190 }
191 state->parse_line = parse_line_no_op;
192}
193
194static void parse_config(const char *fn, char *s)
195{
196 struct parse_state state;
197 char *args[INIT_PARSER_MAXARGS];
198 int nargs;
199
200 nargs = 0;
201 state.filename = fn;
Bruce Beare1be69682010-12-26 09:55:10 -0800202 state.line = 0;
Colin Cross6310a822010-04-20 14:29:05 -0700203 state.ptr = s;
204 state.nexttoken = 0;
205 state.parse_line = parse_line_no_op;
206 for (;;) {
207 switch (next_token(&state)) {
208 case T_EOF:
209 state.parse_line(&state, 0, 0);
210 return;
211 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -0800212 state.line++;
Colin Cross6310a822010-04-20 14:29:05 -0700213 if (nargs) {
214 int kw = lookup_keyword(args[0]);
215 if (kw_is(kw, SECTION)) {
216 state.parse_line(&state, 0, 0);
217 parse_new_section(&state, kw, nargs, args);
218 } else {
219 state.parse_line(&state, nargs, args);
220 }
221 nargs = 0;
222 }
223 break;
224 case T_TEXT:
225 if (nargs < INIT_PARSER_MAXARGS) {
226 args[nargs++] = state.text;
227 }
228 break;
229 }
230 }
231}
232
233int init_parse_config_file(const char *fn)
234{
235 char *data;
236 data = read_file(fn, 0);
237 if (!data) return -1;
238
239 parse_config(fn, data);
240 DUMP();
241 return 0;
242}
243
244static int valid_name(const char *name)
245{
246 if (strlen(name) > 16) {
247 return 0;
248 }
249 while (*name) {
250 if (!isalnum(*name) && (*name != '_') && (*name != '-')) {
251 return 0;
252 }
253 name++;
254 }
255 return 1;
256}
257
258struct service *service_find_by_name(const char *name)
259{
260 struct listnode *node;
261 struct service *svc;
262 list_for_each(node, &service_list) {
263 svc = node_to_item(node, struct service, slist);
264 if (!strcmp(svc->name, name)) {
265 return svc;
266 }
267 }
268 return 0;
269}
270
271struct service *service_find_by_pid(pid_t pid)
272{
273 struct listnode *node;
274 struct service *svc;
275 list_for_each(node, &service_list) {
276 svc = node_to_item(node, struct service, slist);
277 if (svc->pid == pid) {
278 return svc;
279 }
280 }
281 return 0;
282}
283
284struct service *service_find_by_keychord(int keychord_id)
285{
286 struct listnode *node;
287 struct service *svc;
288 list_for_each(node, &service_list) {
289 svc = node_to_item(node, struct service, slist);
290 if (svc->keychord_id == keychord_id) {
291 return svc;
292 }
293 }
294 return 0;
295}
296
297void service_for_each(void (*func)(struct service *svc))
298{
299 struct listnode *node;
300 struct service *svc;
301 list_for_each(node, &service_list) {
302 svc = node_to_item(node, struct service, slist);
303 func(svc);
304 }
305}
306
307void service_for_each_class(const char *classname,
308 void (*func)(struct service *svc))
309{
310 struct listnode *node;
311 struct service *svc;
312 list_for_each(node, &service_list) {
313 svc = node_to_item(node, struct service, slist);
314 if (!strcmp(svc->classname, classname)) {
315 func(svc);
316 }
317 }
318}
319
320void service_for_each_flags(unsigned matchflags,
321 void (*func)(struct service *svc))
322{
323 struct listnode *node;
324 struct service *svc;
325 list_for_each(node, &service_list) {
326 svc = node_to_item(node, struct service, slist);
327 if (svc->flags & matchflags) {
328 func(svc);
329 }
330 }
331}
332
333void action_for_each_trigger(const char *trigger,
334 void (*func)(struct action *act))
335{
336 struct listnode *node;
337 struct action *act;
338 list_for_each(node, &action_list) {
339 act = node_to_item(node, struct action, alist);
340 if (!strcmp(act->name, trigger)) {
341 func(act);
342 }
343 }
344}
345
346void queue_property_triggers(const char *name, const char *value)
347{
348 struct listnode *node;
349 struct action *act;
350 list_for_each(node, &action_list) {
351 act = node_to_item(node, struct action, alist);
352 if (!strncmp(act->name, "property:", strlen("property:"))) {
353 const char *test = act->name + strlen("property:");
354 int name_length = strlen(name);
355
356 if (!strncmp(name, test, name_length) &&
357 test[name_length] == '=' &&
Mike Lockwood7ba61b12011-06-07 18:16:55 -0700358 (!strcmp(test + name_length + 1, value) ||
359 !strcmp(test + name_length + 1, "*"))) {
Colin Cross6310a822010-04-20 14:29:05 -0700360 action_add_queue_tail(act);
361 }
362 }
363 }
364}
365
366void queue_all_property_triggers()
367{
368 struct listnode *node;
369 struct action *act;
370 list_for_each(node, &action_list) {
371 act = node_to_item(node, struct action, alist);
372 if (!strncmp(act->name, "property:", strlen("property:"))) {
373 /* parse property name and value
374 syntax is property:<name>=<value> */
375 const char* name = act->name + strlen("property:");
376 const char* equals = strchr(name, '=');
377 if (equals) {
378 char prop_name[PROP_NAME_MAX + 1];
379 const char* value;
380 int length = equals - name;
381 if (length > PROP_NAME_MAX) {
382 ERROR("property name too long in trigger %s", act->name);
383 } else {
384 memcpy(prop_name, name, length);
385 prop_name[length] = 0;
386
387 /* does the property exist, and match the trigger value? */
388 value = property_get(prop_name);
Mike Lockwood7ba61b12011-06-07 18:16:55 -0700389 if (value && (!strcmp(equals + 1, value) ||
390 !strcmp(equals + 1, "*"))) {
Colin Cross6310a822010-04-20 14:29:05 -0700391 action_add_queue_tail(act);
392 }
393 }
394 }
395 }
396 }
397}
398
399void queue_builtin_action(int (*func)(int nargs, char **args), char *name)
400{
401 struct action *act;
402 struct command *cmd;
403
404 act = calloc(1, sizeof(*act));
405 act->name = name;
406 list_init(&act->commands);
407
408 cmd = calloc(1, sizeof(*cmd));
409 cmd->func = func;
410 cmd->args[0] = name;
411 list_add_tail(&act->commands, &cmd->clist);
412
413 list_add_tail(&action_list, &act->alist);
414 action_add_queue_tail(act);
415}
416
417void action_add_queue_tail(struct action *act)
418{
419 list_add_tail(&action_queue, &act->qlist);
420}
421
422struct action *action_remove_queue_head(void)
423{
424 if (list_empty(&action_queue)) {
425 return 0;
426 } else {
427 struct listnode *node = list_head(&action_queue);
428 struct action *act = node_to_item(node, struct action, qlist);
429 list_remove(node);
430 return act;
431 }
432}
433
434int action_queue_empty()
435{
436 return list_empty(&action_queue);
437}
438
439static void *parse_service(struct parse_state *state, int nargs, char **args)
440{
441 struct service *svc;
442 if (nargs < 3) {
443 parse_error(state, "services must have a name and a program\n");
444 return 0;
445 }
446 if (!valid_name(args[1])) {
447 parse_error(state, "invalid service name '%s'\n", args[1]);
448 return 0;
449 }
450
451 svc = service_find_by_name(args[1]);
452 if (svc) {
453 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
454 return 0;
455 }
456
457 nargs -= 2;
458 svc = calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
459 if (!svc) {
460 parse_error(state, "out of memory\n");
461 return 0;
462 }
463 svc->name = args[1];
464 svc->classname = "default";
465 memcpy(svc->args, args + 2, sizeof(char*) * nargs);
466 svc->args[nargs] = 0;
467 svc->nargs = nargs;
468 svc->onrestart.name = "onrestart";
469 list_init(&svc->onrestart.commands);
470 list_add_tail(&service_list, &svc->slist);
471 return svc;
472}
473
474static void parse_line_service(struct parse_state *state, int nargs, char **args)
475{
476 struct service *svc = state->context;
477 struct command *cmd;
478 int i, kw, kw_nargs;
479
480 if (nargs == 0) {
481 return;
482 }
483
484 svc->ioprio_class = IoSchedClass_NONE;
485
486 kw = lookup_keyword(args[0]);
487 switch (kw) {
488 case K_capability:
489 break;
490 case K_class:
491 if (nargs != 2) {
492 parse_error(state, "class option requires a classname\n");
493 } else {
494 svc->classname = args[1];
495 }
496 break;
497 case K_console:
498 svc->flags |= SVC_CONSOLE;
499 break;
500 case K_disabled:
501 svc->flags |= SVC_DISABLED;
502 break;
503 case K_ioprio:
504 if (nargs != 3) {
505 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
506 } else {
507 svc->ioprio_pri = strtoul(args[2], 0, 8);
508
509 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
510 parse_error(state, "priority value must be range 0 - 7\n");
511 break;
512 }
513
514 if (!strcmp(args[1], "rt")) {
515 svc->ioprio_class = IoSchedClass_RT;
516 } else if (!strcmp(args[1], "be")) {
517 svc->ioprio_class = IoSchedClass_BE;
518 } else if (!strcmp(args[1], "idle")) {
519 svc->ioprio_class = IoSchedClass_IDLE;
520 } else {
521 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
522 }
523 }
524 break;
525 case K_group:
526 if (nargs < 2) {
527 parse_error(state, "group option requires a group id\n");
528 } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
529 parse_error(state, "group option accepts at most %d supp. groups\n",
530 NR_SVC_SUPP_GIDS);
531 } else {
532 int n;
533 svc->gid = decode_uid(args[1]);
534 for (n = 2; n < nargs; n++) {
535 svc->supp_gids[n-2] = decode_uid(args[n]);
536 }
537 svc->nr_supp_gids = n - 2;
538 }
539 break;
540 case K_keycodes:
541 if (nargs < 2) {
542 parse_error(state, "keycodes option requires atleast one keycode\n");
543 } else {
544 svc->keycodes = malloc((nargs - 1) * sizeof(svc->keycodes[0]));
545 if (!svc->keycodes) {
546 parse_error(state, "could not allocate keycodes\n");
547 } else {
548 svc->nkeycodes = nargs - 1;
549 for (i = 1; i < nargs; i++) {
550 svc->keycodes[i - 1] = atoi(args[i]);
551 }
552 }
553 }
554 break;
555 case K_oneshot:
556 svc->flags |= SVC_ONESHOT;
557 break;
558 case K_onrestart:
559 nargs--;
560 args++;
561 kw = lookup_keyword(args[0]);
562 if (!kw_is(kw, COMMAND)) {
563 parse_error(state, "invalid command '%s'\n", args[0]);
564 break;
565 }
566 kw_nargs = kw_nargs(kw);
567 if (nargs < kw_nargs) {
568 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
569 kw_nargs > 2 ? "arguments" : "argument");
570 break;
571 }
572
573 cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
574 cmd->func = kw_func(kw);
575 cmd->nargs = nargs;
576 memcpy(cmd->args, args, sizeof(char*) * nargs);
577 list_add_tail(&svc->onrestart.commands, &cmd->clist);
578 break;
579 case K_critical:
580 svc->flags |= SVC_CRITICAL;
581 break;
582 case K_setenv: { /* name value */
583 struct svcenvinfo *ei;
584 if (nargs < 2) {
585 parse_error(state, "setenv option requires name and value arguments\n");
586 break;
587 }
588 ei = calloc(1, sizeof(*ei));
589 if (!ei) {
590 parse_error(state, "out of memory\n");
591 break;
592 }
593 ei->name = args[1];
594 ei->value = args[2];
595 ei->next = svc->envvars;
596 svc->envvars = ei;
597 break;
598 }
599 case K_socket: {/* name type perm [ uid gid ] */
600 struct socketinfo *si;
601 if (nargs < 4) {
602 parse_error(state, "socket option requires name, type, perm arguments\n");
603 break;
604 }
Mike Lockwood912ff852010-10-01 08:20:36 -0400605 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
606 && strcmp(args[2],"seqpacket")) {
607 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
Colin Cross6310a822010-04-20 14:29:05 -0700608 break;
609 }
610 si = calloc(1, sizeof(*si));
611 if (!si) {
612 parse_error(state, "out of memory\n");
613 break;
614 }
615 si->name = args[1];
616 si->type = args[2];
617 si->perm = strtoul(args[3], 0, 8);
618 if (nargs > 4)
619 si->uid = decode_uid(args[4]);
620 if (nargs > 5)
621 si->gid = decode_uid(args[5]);
622 si->next = svc->sockets;
623 svc->sockets = si;
624 break;
625 }
626 case K_user:
627 if (nargs != 2) {
628 parse_error(state, "user option requires a user id\n");
629 } else {
630 svc->uid = decode_uid(args[1]);
631 }
632 break;
633 default:
634 parse_error(state, "invalid option '%s'\n", args[0]);
635 }
636}
637
638static void *parse_action(struct parse_state *state, int nargs, char **args)
639{
640 struct action *act;
641 if (nargs < 2) {
642 parse_error(state, "actions must have a trigger\n");
643 return 0;
644 }
645 if (nargs > 2) {
646 parse_error(state, "actions may not have extra parameters\n");
647 return 0;
648 }
649 act = calloc(1, sizeof(*act));
650 act->name = args[1];
651 list_init(&act->commands);
652 list_add_tail(&action_list, &act->alist);
653 /* XXX add to hash */
654 return act;
655}
656
657static void parse_line_action(struct parse_state* state, int nargs, char **args)
658{
659 struct command *cmd;
660 struct action *act = state->context;
661 int (*func)(int nargs, char **args);
662 int kw, n;
663
664 if (nargs == 0) {
665 return;
666 }
667
668 kw = lookup_keyword(args[0]);
669 if (!kw_is(kw, COMMAND)) {
670 parse_error(state, "invalid command '%s'\n", args[0]);
671 return;
672 }
673
674 n = kw_nargs(kw);
675 if (nargs < n) {
676 parse_error(state, "%s requires %d %s\n", args[0], n - 1,
677 n > 2 ? "arguments" : "argument");
678 return;
679 }
680 cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
681 cmd->func = kw_func(kw);
682 cmd->nargs = nargs;
683 memcpy(cmd->args, args, sizeof(char*) * nargs);
684 list_add_tail(&act->commands, &cmd->clist);
685}