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