init_parser.cpp revision a4d98484a2b64d215d1d35830693a16cf2873308
1/* 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 <errno.h> 18#include <stdio.h> 19#include <stdlib.h> 20#include <unistd.h> 21#include <fcntl.h> 22#include <stdarg.h> 23#include <string.h> 24#include <stddef.h> 25#include <ctype.h> 26 27#include "init.h" 28#include "parser.h" 29#include "init_parser.h" 30#include "log.h" 31#include "property_service.h" 32#include "util.h" 33 34#include <cutils/iosched_policy.h> 35#include <cutils/list.h> 36 37static list_declare(service_list); 38static list_declare(action_list); 39static list_declare(action_queue); 40 41struct import { 42 struct listnode list; 43 const char *filename; 44}; 45 46static void *parse_service(struct parse_state *state, int nargs, char **args); 47static void parse_line_service(struct parse_state *state, int nargs, char **args); 48 49static void *parse_action(struct parse_state *state, int nargs, char **args); 50static void parse_line_action(struct parse_state *state, int nargs, char **args); 51 52#define SECTION 0x01 53#define COMMAND 0x02 54#define OPTION 0x04 55 56#include "keywords.h" 57 58#define KEYWORD(symbol, flags, nargs, func) \ 59 [ K_##symbol ] = { #symbol, func, nargs + 1, flags, }, 60 61static struct { 62 const char *name; 63 int (*func)(int nargs, char **args); 64 unsigned char nargs; 65 unsigned char flags; 66} keyword_info[KEYWORD_COUNT] = { 67 [ K_UNKNOWN ] = { "unknown", 0, 0, 0 }, 68#include "keywords.h" 69}; 70#undef KEYWORD 71 72#define kw_is(kw, type) (keyword_info[kw].flags & (type)) 73#define kw_name(kw) (keyword_info[kw].name) 74#define kw_func(kw) (keyword_info[kw].func) 75#define kw_nargs(kw) (keyword_info[kw].nargs) 76 77void dump_parser_state() { 78 if (false) { 79 struct listnode* node; 80 list_for_each(node, &service_list) { 81 service* svc = node_to_item(node, struct service, slist); 82 INFO("service %s\n", svc->name); 83 INFO(" class '%s'\n", svc->classname); 84 INFO(" exec"); 85 for (int n = 0; n < svc->nargs; n++) { 86 INFO(" '%s'", svc->args[n]); 87 } 88 INFO("\n"); 89 for (socketinfo* si = svc->sockets; si; si = si->next) { 90 INFO(" socket %s %s 0%o\n", si->name, si->type, si->perm); 91 } 92 } 93 94 list_for_each(node, &action_list) { 95 action* act = node_to_item(node, struct action, alist); 96 INFO("on "); 97 char name_str[256] = ""; 98 build_triggers_string(name_str, sizeof(name_str), act); 99 INFO("%s", name_str); 100 INFO("\n"); 101 102 struct listnode* node2; 103 list_for_each(node2, &act->commands) { 104 command* cmd = node_to_item(node2, struct command, clist); 105 INFO(" %p", cmd->func); 106 for (int n = 0; n < cmd->nargs; n++) { 107 INFO(" %s", cmd->args[n]); 108 } 109 INFO("\n"); 110 } 111 INFO("\n"); 112 } 113 } 114} 115 116static int lookup_keyword(const char *s) 117{ 118 switch (*s++) { 119 case 'b': 120 if (!strcmp(s, "ootchart_init")) return K_bootchart_init; 121 case 'c': 122 if (!strcmp(s, "opy")) return K_copy; 123 if (!strcmp(s, "apability")) return K_capability; 124 if (!strcmp(s, "hdir")) return K_chdir; 125 if (!strcmp(s, "hroot")) return K_chroot; 126 if (!strcmp(s, "lass")) return K_class; 127 if (!strcmp(s, "lass_start")) return K_class_start; 128 if (!strcmp(s, "lass_stop")) return K_class_stop; 129 if (!strcmp(s, "lass_reset")) return K_class_reset; 130 if (!strcmp(s, "onsole")) return K_console; 131 if (!strcmp(s, "hown")) return K_chown; 132 if (!strcmp(s, "hmod")) return K_chmod; 133 if (!strcmp(s, "ritical")) return K_critical; 134 break; 135 case 'd': 136 if (!strcmp(s, "isabled")) return K_disabled; 137 if (!strcmp(s, "omainname")) return K_domainname; 138 break; 139 case 'e': 140 if (!strcmp(s, "nable")) return K_enable; 141 if (!strcmp(s, "xec")) return K_exec; 142 if (!strcmp(s, "xeconce")) return K_execonce; 143 if (!strcmp(s, "xport")) return K_export; 144 break; 145 case 'g': 146 if (!strcmp(s, "roup")) return K_group; 147 break; 148 case 'h': 149 if (!strcmp(s, "ostname")) return K_hostname; 150 break; 151 case 'i': 152 if (!strcmp(s, "oprio")) return K_ioprio; 153 if (!strcmp(s, "fup")) return K_ifup; 154 if (!strcmp(s, "nsmod")) return K_insmod; 155 if (!strcmp(s, "mport")) return K_import; 156 break; 157 case 'k': 158 if (!strcmp(s, "eycodes")) return K_keycodes; 159 break; 160 case 'l': 161 if (!strcmp(s, "oglevel")) return K_loglevel; 162 if (!strcmp(s, "oad_persist_props")) return K_load_persist_props; 163 if (!strcmp(s, "oad_all_props")) return K_load_all_props; 164 break; 165 case 'm': 166 if (!strcmp(s, "kdir")) return K_mkdir; 167 if (!strcmp(s, "ount_all")) return K_mount_all; 168 if (!strcmp(s, "ount")) return K_mount; 169 break; 170 case 'o': 171 if (!strcmp(s, "n")) return K_on; 172 if (!strcmp(s, "neshot")) return K_oneshot; 173 if (!strcmp(s, "nrestart")) return K_onrestart; 174 break; 175 case 'p': 176 if (!strcmp(s, "owerctl")) return K_powerctl; 177 break; 178 case 'r': 179 if (!strcmp(s, "estart")) return K_restart; 180 if (!strcmp(s, "estorecon")) return K_restorecon; 181 if (!strcmp(s, "estorecon_recursive")) return K_restorecon_recursive; 182 if (!strcmp(s, "mdir")) return K_rmdir; 183 if (!strcmp(s, "m")) return K_rm; 184 break; 185 case 's': 186 if (!strcmp(s, "eclabel")) return K_seclabel; 187 if (!strcmp(s, "ervice")) return K_service; 188 if (!strcmp(s, "etcon")) return K_setcon; 189 if (!strcmp(s, "etenforce")) return K_setenforce; 190 if (!strcmp(s, "etenv")) return K_setenv; 191 if (!strcmp(s, "etkey")) return K_setkey; 192 if (!strcmp(s, "etprop")) return K_setprop; 193 if (!strcmp(s, "etrlimit")) return K_setrlimit; 194 if (!strcmp(s, "etsebool")) return K_setsebool; 195 if (!strcmp(s, "ocket")) return K_socket; 196 if (!strcmp(s, "tart")) return K_start; 197 if (!strcmp(s, "top")) return K_stop; 198 if (!strcmp(s, "wapon_all")) return K_swapon_all; 199 if (!strcmp(s, "ymlink")) return K_symlink; 200 if (!strcmp(s, "ysclktz")) return K_sysclktz; 201 break; 202 case 't': 203 if (!strcmp(s, "rigger")) return K_trigger; 204 break; 205 case 'u': 206 if (!strcmp(s, "ser")) return K_user; 207 break; 208 case 'v': 209 if (!strcmp(s, "erity_load_state")) return K_verity_load_state; 210 break; 211 case 'w': 212 if (!strcmp(s, "rite")) return K_write; 213 if (!strcmp(s, "ait")) return K_wait; 214 break; 215 } 216 return K_UNKNOWN; 217} 218 219static void parse_line_no_op(struct parse_state*, int, char**) { 220} 221 222static int push_chars(char **dst, int *len, const char *chars, int cnt) 223{ 224 if (cnt > *len) 225 return -1; 226 227 memcpy(*dst, chars, cnt); 228 *dst += cnt; 229 *len -= cnt; 230 231 return 0; 232} 233 234int expand_props(char *dst, const char *src, int dst_size) 235{ 236 char *dst_ptr = dst; 237 const char *src_ptr = src; 238 int ret = 0; 239 int left = dst_size - 1; 240 241 if (!src || !dst || dst_size == 0) 242 return -1; 243 244 /* - variables can either be $x.y or ${x.y}, in case they are only part 245 * of the string. 246 * - will accept $$ as a literal $. 247 * - no nested property expansion, i.e. ${foo.${bar}} is not supported, 248 * bad things will happen 249 */ 250 while (*src_ptr && left > 0) { 251 char *c; 252 char prop[PROP_NAME_MAX + 1]; 253 char prop_val[PROP_VALUE_MAX]; 254 int prop_len = 0; 255 int prop_val_len; 256 257 c = strchr(src_ptr, '$'); 258 if (!c) { 259 while (left-- > 0 && *src_ptr) 260 *(dst_ptr++) = *(src_ptr++); 261 break; 262 } 263 264 memset(prop, 0, sizeof(prop)); 265 266 ret = push_chars(&dst_ptr, &left, src_ptr, c - src_ptr); 267 if (ret < 0) 268 goto err_nospace; 269 c++; 270 271 if (*c == '$') { 272 *(dst_ptr++) = *(c++); 273 src_ptr = c; 274 left--; 275 continue; 276 } else if (*c == '\0') { 277 break; 278 } 279 280 if (*c == '{') { 281 c++; 282 while (*c && *c != '}' && prop_len < PROP_NAME_MAX) 283 prop[prop_len++] = *(c++); 284 if (*c != '}') { 285 /* failed to find closing brace, abort. */ 286 if (prop_len == PROP_NAME_MAX) 287 ERROR("prop name too long during expansion of '%s'\n", 288 src); 289 else if (*c == '\0') 290 ERROR("unexpected end of string in '%s', looking for }\n", 291 src); 292 goto err; 293 } 294 prop[prop_len] = '\0'; 295 c++; 296 } else if (*c) { 297 while (*c && prop_len < PROP_NAME_MAX) 298 prop[prop_len++] = *(c++); 299 if (prop_len == PROP_NAME_MAX && *c != '\0') { 300 ERROR("prop name too long in '%s'\n", src); 301 goto err; 302 } 303 prop[prop_len] = '\0'; 304 ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n", 305 prop); 306 } 307 308 if (prop_len == 0) { 309 ERROR("invalid zero-length prop name in '%s'\n", src); 310 goto err; 311 } 312 313 prop_val_len = property_get(prop, prop_val); 314 if (!prop_val_len) { 315 ERROR("property '%s' doesn't exist while expanding '%s'\n", 316 prop, src); 317 goto err; 318 } 319 320 ret = push_chars(&dst_ptr, &left, prop_val, prop_val_len); 321 if (ret < 0) 322 goto err_nospace; 323 src_ptr = c; 324 continue; 325 } 326 327 *dst_ptr = '\0'; 328 return 0; 329 330err_nospace: 331 ERROR("destination buffer overflow while expanding '%s'\n", src); 332err: 333 return -1; 334} 335 336static void parse_import(struct parse_state *state, int nargs, char **args) 337{ 338 struct listnode *import_list = (listnode*) state->priv; 339 char conf_file[PATH_MAX]; 340 int ret; 341 342 if (nargs != 2) { 343 ERROR("single argument needed for import\n"); 344 return; 345 } 346 347 ret = expand_props(conf_file, args[1], sizeof(conf_file)); 348 if (ret) { 349 ERROR("error while handling import on line '%d' in '%s'\n", 350 state->line, state->filename); 351 return; 352 } 353 354 struct import* import = (struct import*) calloc(1, sizeof(struct import)); 355 import->filename = strdup(conf_file); 356 list_add_tail(import_list, &import->list); 357 INFO("found import '%s', adding to import list", import->filename); 358} 359 360static void parse_new_section(struct parse_state *state, int kw, 361 int nargs, char **args) 362{ 363 printf("[ %s %s ]\n", args[0], 364 nargs > 1 ? args[1] : ""); 365 switch(kw) { 366 case K_service: 367 state->context = parse_service(state, nargs, args); 368 if (state->context) { 369 state->parse_line = parse_line_service; 370 return; 371 } 372 break; 373 case K_on: 374 state->context = parse_action(state, nargs, args); 375 if (state->context) { 376 state->parse_line = parse_line_action; 377 return; 378 } 379 break; 380 case K_import: 381 parse_import(state, nargs, args); 382 break; 383 } 384 state->parse_line = parse_line_no_op; 385} 386 387static void parse_config(const char *fn, const std::string& data) 388{ 389 struct parse_state state; 390 struct listnode import_list; 391 struct listnode *node; 392 char *args[INIT_PARSER_MAXARGS]; 393 int nargs; 394 395 nargs = 0; 396 state.filename = fn; 397 state.line = 0; 398 state.ptr = strdup(data.c_str()); // TODO: fix this code! 399 state.nexttoken = 0; 400 state.parse_line = parse_line_no_op; 401 402 list_init(&import_list); 403 state.priv = &import_list; 404 405 for (;;) { 406 switch (next_token(&state)) { 407 case T_EOF: 408 state.parse_line(&state, 0, 0); 409 goto parser_done; 410 case T_NEWLINE: 411 state.line++; 412 if (nargs) { 413 int kw = lookup_keyword(args[0]); 414 if (kw_is(kw, SECTION)) { 415 state.parse_line(&state, 0, 0); 416 parse_new_section(&state, kw, nargs, args); 417 } else { 418 state.parse_line(&state, nargs, args); 419 } 420 nargs = 0; 421 } 422 break; 423 case T_TEXT: 424 if (nargs < INIT_PARSER_MAXARGS) { 425 args[nargs++] = state.text; 426 } 427 break; 428 } 429 } 430 431parser_done: 432 list_for_each(node, &import_list) { 433 struct import *import = node_to_item(node, struct import, list); 434 int ret; 435 436 ret = init_parse_config_file(import->filename); 437 if (ret) 438 ERROR("could not import file '%s' from '%s'\n", 439 import->filename, fn); 440 } 441} 442 443int init_parse_config_file(const char* path) { 444 INFO("Parsing %s...\n", path); 445 std::string data; 446 if (!read_file(path, &data)) { 447 return -1; 448 } 449 450 parse_config(path, data); 451 dump_parser_state(); 452 return 0; 453} 454 455static int valid_name(const char *name) 456{ 457 if (strlen(name) > 16) { 458 return 0; 459 } 460 while (*name) { 461 if (!isalnum(*name) && (*name != '_') && (*name != '-')) { 462 return 0; 463 } 464 name++; 465 } 466 return 1; 467} 468 469struct service *service_find_by_name(const char *name) 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 if (!strcmp(svc->name, name)) { 476 return svc; 477 } 478 } 479 return 0; 480} 481 482struct service *service_find_by_pid(pid_t pid) 483{ 484 struct listnode *node; 485 struct service *svc; 486 list_for_each(node, &service_list) { 487 svc = node_to_item(node, struct service, slist); 488 if (svc->pid == pid) { 489 return svc; 490 } 491 } 492 return 0; 493} 494 495struct service *service_find_by_keychord(int keychord_id) 496{ 497 struct listnode *node; 498 struct service *svc; 499 list_for_each(node, &service_list) { 500 svc = node_to_item(node, struct service, slist); 501 if (svc->keychord_id == keychord_id) { 502 return svc; 503 } 504 } 505 return 0; 506} 507 508void service_for_each(void (*func)(struct service *svc)) 509{ 510 struct listnode *node; 511 struct service *svc; 512 list_for_each(node, &service_list) { 513 svc = node_to_item(node, struct service, slist); 514 func(svc); 515 } 516} 517 518void service_for_each_class(const char *classname, 519 void (*func)(struct service *svc)) 520{ 521 struct listnode *node; 522 struct service *svc; 523 list_for_each(node, &service_list) { 524 svc = node_to_item(node, struct service, slist); 525 if (!strcmp(svc->classname, classname)) { 526 func(svc); 527 } 528 } 529} 530 531void service_for_each_flags(unsigned matchflags, 532 void (*func)(struct service *svc)) 533{ 534 struct listnode *node; 535 struct service *svc; 536 list_for_each(node, &service_list) { 537 svc = node_to_item(node, struct service, slist); 538 if (svc->flags & matchflags) { 539 func(svc); 540 } 541 } 542} 543 544void action_for_each_trigger(const char *trigger, 545 void (*func)(struct action *act)) 546{ 547 struct listnode *node, *node2; 548 struct action *act; 549 struct trigger *cur_trigger; 550 551 list_for_each(node, &action_list) { 552 act = node_to_item(node, struct action, alist); 553 list_for_each(node2, &act->triggers) { 554 cur_trigger = node_to_item(node2, struct trigger, nlist); 555 if (!strcmp(cur_trigger->name, trigger)) { 556 func(act); 557 } 558 } 559 } 560} 561 562 563void queue_property_triggers(const char *name, const char *value) 564{ 565 struct listnode *node, *node2; 566 struct action *act; 567 struct trigger *cur_trigger; 568 bool match; 569 int name_length; 570 571 list_for_each(node, &action_list) { 572 act = node_to_item(node, struct action, alist); 573 match = !name; 574 list_for_each(node2, &act->triggers) { 575 cur_trigger = node_to_item(node2, struct trigger, nlist); 576 if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) { 577 const char *test = cur_trigger->name + strlen("property:"); 578 if (!match) { 579 name_length = strlen(name); 580 if (!strncmp(name, test, name_length) && 581 test[name_length] == '=' && 582 (!strcmp(test + name_length + 1, value) || 583 !strcmp(test + name_length + 1, "*"))) { 584 match = true; 585 continue; 586 } 587 } else { 588 const char* equals = strchr(test, '='); 589 if (equals) { 590 char prop_name[PROP_NAME_MAX + 1]; 591 char value[PROP_VALUE_MAX]; 592 int length = equals - test; 593 if (length <= PROP_NAME_MAX) { 594 int ret; 595 memcpy(prop_name, test, length); 596 prop_name[length] = 0; 597 598 /* does the property exist, and match the trigger value? */ 599 ret = property_get(prop_name, value); 600 if (ret > 0 && (!strcmp(equals + 1, value) || 601 !strcmp(equals + 1, "*"))) { 602 continue; 603 } 604 } 605 } 606 } 607 } 608 match = false; 609 break; 610 } 611 if (match) { 612 action_add_queue_tail(act); 613 } 614 } 615} 616 617void queue_all_property_triggers() 618{ 619 queue_property_triggers(NULL, NULL); 620} 621 622void queue_builtin_action(int (*func)(int nargs, char **args), const char *name) 623{ 624 action* act = (action*) calloc(1, sizeof(*act)); 625 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger)); 626 cur_trigger->name = name; 627 list_init(&act->triggers); 628 list_add_tail(&act->triggers, &cur_trigger->nlist); 629 list_init(&act->commands); 630 list_init(&act->qlist); 631 632 command* cmd = (command*) calloc(1, sizeof(*cmd)); 633 cmd->func = func; 634 cmd->args[0] = const_cast<char*>(name); 635 cmd->nargs = 1; 636 list_add_tail(&act->commands, &cmd->clist); 637 638 list_add_tail(&action_list, &act->alist); 639 action_add_queue_tail(act); 640} 641 642void action_add_queue_tail(struct action *act) 643{ 644 if (list_empty(&act->qlist)) { 645 list_add_tail(&action_queue, &act->qlist); 646 } 647} 648 649struct action *action_remove_queue_head(void) 650{ 651 if (list_empty(&action_queue)) { 652 return 0; 653 } else { 654 struct listnode *node = list_head(&action_queue); 655 struct action *act = node_to_item(node, struct action, qlist); 656 list_remove(node); 657 list_init(node); 658 return act; 659 } 660} 661 662int action_queue_empty() 663{ 664 return list_empty(&action_queue); 665} 666 667service* make_exec_oneshot_service(int nargs, char** args) { 668 // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS... 669 int command_arg = 1; 670 for (int i = 1; i < nargs; ++i) { 671 if (strcmp(args[i], "--") == 0) { 672 command_arg = i + 1; 673 break; 674 } 675 } 676 if (command_arg > 4 + NR_SVC_SUPP_GIDS) { 677 ERROR("exec called with too many supplementary group ids\n"); 678 return NULL; 679 } 680 681 int argc = nargs - command_arg; 682 char** argv = (args + command_arg); 683 if (argc < 1) { 684 ERROR("exec called without command\n"); 685 return NULL; 686 } 687 688 service* svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * argc); 689 if (svc == NULL) { 690 ERROR("Couldn't allocate service for exec of '%s': %s", argv[0], strerror(errno)); 691 return NULL; 692 } 693 694 if (command_arg > 2) { 695 svc->seclabel = args[1]; 696 } 697 if (command_arg > 3) { 698 svc->uid = decode_uid(args[2]); 699 } 700 if (command_arg > 4) { 701 svc->gid = decode_uid(args[3]); 702 svc->nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */; 703 for (size_t i = 0; i < svc->nr_supp_gids; ++i) { 704 svc->supp_gids[i] = decode_uid(args[4 + i]); 705 } 706 } 707 708 static int exec_count; // Every service needs a unique name. 709 char* name = NULL; 710 asprintf(&name, "exec %d (%s)", exec_count++, argv[0]); 711 if (name == NULL) { 712 ERROR("Couldn't allocate name for exec service '%s'\n", argv[0]); 713 free(svc); 714 return NULL; 715 } 716 svc->name = name; 717 svc->classname = "default"; 718 svc->flags = SVC_EXEC | SVC_ONESHOT; 719 svc->nargs = argc; 720 memcpy(svc->args, argv, sizeof(char*) * svc->nargs); 721 svc->args[argc] = NULL; 722 list_add_tail(&service_list, &svc->slist); 723 return svc; 724} 725 726static void *parse_service(struct parse_state *state, int nargs, char **args) 727{ 728 if (nargs < 3) { 729 parse_error(state, "services must have a name and a program\n"); 730 return 0; 731 } 732 if (!valid_name(args[1])) { 733 parse_error(state, "invalid service name '%s'\n", args[1]); 734 return 0; 735 } 736 737 service* svc = (service*) service_find_by_name(args[1]); 738 if (svc) { 739 parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]); 740 return 0; 741 } 742 743 nargs -= 2; 744 svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs); 745 if (!svc) { 746 parse_error(state, "out of memory\n"); 747 return 0; 748 } 749 svc->name = strdup(args[1]); 750 svc->classname = "default"; 751 memcpy(svc->args, args + 2, sizeof(char*) * nargs); 752 trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger)); 753 svc->args[nargs] = 0; 754 svc->nargs = nargs; 755 list_init(&svc->onrestart.triggers); 756 cur_trigger->name = "onrestart"; 757 list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist); 758 list_init(&svc->onrestart.commands); 759 list_add_tail(&service_list, &svc->slist); 760 return svc; 761} 762 763static void parse_line_service(struct parse_state *state, int nargs, char **args) 764{ 765 struct service *svc = (service*) state->context; 766 struct command *cmd; 767 int i, kw, kw_nargs; 768 769 if (nargs == 0) { 770 return; 771 } 772 773 svc->ioprio_class = IoSchedClass_NONE; 774 775 kw = lookup_keyword(args[0]); 776 switch (kw) { 777 case K_capability: 778 break; 779 case K_class: 780 if (nargs != 2) { 781 parse_error(state, "class option requires a classname\n"); 782 } else { 783 svc->classname = args[1]; 784 } 785 break; 786 case K_console: 787 svc->flags |= SVC_CONSOLE; 788 break; 789 case K_disabled: 790 svc->flags |= SVC_DISABLED; 791 svc->flags |= SVC_RC_DISABLED; 792 break; 793 case K_ioprio: 794 if (nargs != 3) { 795 parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n"); 796 } else { 797 svc->ioprio_pri = strtoul(args[2], 0, 8); 798 799 if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) { 800 parse_error(state, "priority value must be range 0 - 7\n"); 801 break; 802 } 803 804 if (!strcmp(args[1], "rt")) { 805 svc->ioprio_class = IoSchedClass_RT; 806 } else if (!strcmp(args[1], "be")) { 807 svc->ioprio_class = IoSchedClass_BE; 808 } else if (!strcmp(args[1], "idle")) { 809 svc->ioprio_class = IoSchedClass_IDLE; 810 } else { 811 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n"); 812 } 813 } 814 break; 815 case K_group: 816 if (nargs < 2) { 817 parse_error(state, "group option requires a group id\n"); 818 } else if (nargs > NR_SVC_SUPP_GIDS + 2) { 819 parse_error(state, "group option accepts at most %d supp. groups\n", 820 NR_SVC_SUPP_GIDS); 821 } else { 822 int n; 823 svc->gid = decode_uid(args[1]); 824 for (n = 2; n < nargs; n++) { 825 svc->supp_gids[n-2] = decode_uid(args[n]); 826 } 827 svc->nr_supp_gids = n - 2; 828 } 829 break; 830 case K_keycodes: 831 if (nargs < 2) { 832 parse_error(state, "keycodes option requires atleast one keycode\n"); 833 } else { 834 svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0])); 835 if (!svc->keycodes) { 836 parse_error(state, "could not allocate keycodes\n"); 837 } else { 838 svc->nkeycodes = nargs - 1; 839 for (i = 1; i < nargs; i++) { 840 svc->keycodes[i - 1] = atoi(args[i]); 841 } 842 } 843 } 844 break; 845 case K_oneshot: 846 svc->flags |= SVC_ONESHOT; 847 break; 848 case K_onrestart: 849 nargs--; 850 args++; 851 kw = lookup_keyword(args[0]); 852 if (!kw_is(kw, COMMAND)) { 853 parse_error(state, "invalid command '%s'\n", args[0]); 854 break; 855 } 856 kw_nargs = kw_nargs(kw); 857 if (nargs < kw_nargs) { 858 parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1, 859 kw_nargs > 2 ? "arguments" : "argument"); 860 break; 861 } 862 863 cmd = (command*) 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(&svc->onrestart.commands, &cmd->clist); 868 break; 869 case K_critical: 870 svc->flags |= SVC_CRITICAL; 871 break; 872 case K_setenv: { /* name value */ 873 if (nargs < 3) { 874 parse_error(state, "setenv option requires name and value arguments\n"); 875 break; 876 } 877 svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei)); 878 if (!ei) { 879 parse_error(state, "out of memory\n"); 880 break; 881 } 882 ei->name = args[1]; 883 ei->value = args[2]; 884 ei->next = svc->envvars; 885 svc->envvars = ei; 886 break; 887 } 888 case K_socket: {/* name type perm [ uid gid context ] */ 889 if (nargs < 4) { 890 parse_error(state, "socket option requires name, type, perm arguments\n"); 891 break; 892 } 893 if (strcmp(args[2],"dgram") && strcmp(args[2],"stream") 894 && strcmp(args[2],"seqpacket")) { 895 parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n"); 896 break; 897 } 898 socketinfo* si = (socketinfo*) calloc(1, sizeof(*si)); 899 if (!si) { 900 parse_error(state, "out of memory\n"); 901 break; 902 } 903 si->name = args[1]; 904 si->type = args[2]; 905 si->perm = strtoul(args[3], 0, 8); 906 if (nargs > 4) 907 si->uid = decode_uid(args[4]); 908 if (nargs > 5) 909 si->gid = decode_uid(args[5]); 910 if (nargs > 6) 911 si->socketcon = args[6]; 912 si->next = svc->sockets; 913 svc->sockets = si; 914 break; 915 } 916 case K_user: 917 if (nargs != 2) { 918 parse_error(state, "user option requires a user id\n"); 919 } else { 920 svc->uid = decode_uid(args[1]); 921 } 922 break; 923 case K_seclabel: 924 if (nargs != 2) { 925 parse_error(state, "seclabel option requires a label string\n"); 926 } else { 927 svc->seclabel = args[1]; 928 } 929 break; 930 931 default: 932 parse_error(state, "invalid option '%s'\n", args[0]); 933 } 934} 935 936static void *parse_action(struct parse_state *state, int nargs, char **args) 937{ 938 struct trigger *cur_trigger; 939 int i; 940 if (nargs < 2) { 941 parse_error(state, "actions must have a trigger\n"); 942 return 0; 943 } 944 945 action* act = (action*) calloc(1, sizeof(*act)); 946 list_init(&act->triggers); 947 948 for (i = 1; i < nargs; i++) { 949 if (!(i % 2)) { 950 if (strcmp(args[i], "&&")) { 951 parse_error(state, "& is the only symbol allowed to concatenate actions\n"); 952 return 0; 953 } else 954 continue; 955 } 956 cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger)); 957 cur_trigger->name = args[i]; 958 list_add_tail(&act->triggers, &cur_trigger->nlist); 959 } 960 961 list_init(&act->commands); 962 list_init(&act->qlist); 963 list_add_tail(&action_list, &act->alist); 964 /* XXX add to hash */ 965 return act; 966} 967 968static void parse_line_action(struct parse_state* state, int nargs, char **args) 969{ 970 struct action *act = (action*) state->context; 971 int kw, n; 972 973 if (nargs == 0) { 974 return; 975 } 976 977 kw = lookup_keyword(args[0]); 978 if (!kw_is(kw, COMMAND)) { 979 parse_error(state, "invalid command '%s'\n", args[0]); 980 return; 981 } 982 983 n = kw_nargs(kw); 984 if (nargs < n) { 985 parse_error(state, "%s requires %d %s\n", args[0], n - 1, 986 n > 2 ? "arguments" : "argument"); 987 return; 988 } 989 command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs); 990 cmd->func = kw_func(kw); 991 cmd->line = state->line; 992 cmd->filename = state->filename; 993 cmd->nargs = nargs; 994 memcpy(cmd->args, args, sizeof(char*) * nargs); 995 list_add_tail(&act->commands, &cmd->clist); 996} 997