Home | History | Annotate | Download | only in init
      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 static list_declare(service_list);
     37 static list_declare(action_list);
     38 static list_declare(action_queue);
     39 
     40 struct import {
     41     struct listnode list;
     42     const char *filename;
     43 };
     44 
     45 static void *parse_service(struct parse_state *state, int nargs, char **args);
     46 static void parse_line_service(struct parse_state *state, int nargs, char **args);
     47 
     48 static void *parse_action(struct parse_state *state, int nargs, char **args);
     49 static 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 
     60 static struct {
     61     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 
     76 static int lookup_keyword(const char *s)
     77 {
     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;
     87         if (!strcmp(s, "lass_reset")) return K_class_reset;
     88         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':
     98         if (!strcmp(s, "nable")) return K_enable;
     99         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;
    119         if (!strcmp(s, "oad_persist_props")) return K_load_persist_props;
    120         if (!strcmp(s, "oad_all_props")) return K_load_all_props;
    121         break;
    122     case 'm':
    123         if (!strcmp(s, "kdir")) return K_mkdir;
    124         if (!strcmp(s, "ount_all")) return K_mount_all;
    125         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;
    132     case 'p':
    133         if (!strcmp(s, "owerctl")) return K_powerctl;
    134     case 'r':
    135         if (!strcmp(s, "estart")) return K_restart;
    136         if (!strcmp(s, "estorecon")) return K_restorecon;
    137         if (!strcmp(s, "estorecon_recursive")) return K_restorecon_recursive;
    138         if (!strcmp(s, "mdir")) return K_rmdir;
    139         if (!strcmp(s, "m")) return K_rm;
    140         break;
    141     case 's':
    142         if (!strcmp(s, "eclabel")) return K_seclabel;
    143         if (!strcmp(s, "ervice")) return K_service;
    144         if (!strcmp(s, "etcon")) return K_setcon;
    145         if (!strcmp(s, "etenforce")) return K_setenforce;
    146         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;
    150         if (!strcmp(s, "etsebool")) return K_setsebool;
    151         if (!strcmp(s, "ocket")) return K_socket;
    152         if (!strcmp(s, "tart")) return K_start;
    153         if (!strcmp(s, "top")) return K_stop;
    154         if (!strcmp(s, "wapon_all")) return K_swapon_all;
    155         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 
    172 static void parse_line_no_op(struct parse_state *state, int nargs, char **args)
    173 {
    174 }
    175 
    176 static 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 
    188 int expand_props(char *dst, const char *src, int dst_size)
    189 {
    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];
    212         char prop_val[PROP_VALUE_MAX];
    213         int prop_len = 0;
    214         int prop_val_len;
    215 
    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 
    272         prop_val_len = property_get(prop, prop_val);
    273         if (!prop_val_len) {
    274             ERROR("property '%s' doesn't exist while expanding '%s'\n",
    275                   prop, src);
    276             goto err;
    277         }
    278 
    279         ret = push_chars(&dst_ptr, &left, prop_val, prop_val_len);
    280         if (ret < 0)
    281             goto err_nospace;
    282         src_ptr = c;
    283         continue;
    284     }
    285 
    286     *dst_ptr = '\0';
    287     return 0;
    288 
    289 err_nospace:
    290     ERROR("destination buffer overflow while expanding '%s'\n", src);
    291 err:
    292     return -1;
    293 }
    294 
    295 static void parse_import(struct parse_state *state, int nargs, char **args)
    296 {
    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 
    320 static void parse_new_section(struct parse_state *state, int kw,
    321                        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;
    340     case K_import:
    341         parse_import(state, nargs, args);
    342         break;
    343     }
    344     state->parse_line = parse_line_no_op;
    345 }
    346 
    347 static void parse_config(const char *fn, char *s)
    348 {
    349     struct parse_state state;
    350     struct listnode import_list;
    351     struct listnode *node;
    352     char *args[INIT_PARSER_MAXARGS];
    353     int nargs;
    354 
    355     nargs = 0;
    356     state.filename = fn;
    357     state.line = 0;
    358     state.ptr = s;
    359     state.nexttoken = 0;
    360     state.parse_line = parse_line_no_op;
    361 
    362     list_init(&import_list);
    363     state.priv = &import_list;
    364 
    365     for (;;) {
    366         switch (next_token(&state)) {
    367         case T_EOF:
    368             state.parse_line(&state, 0, 0);
    369             goto parser_done;
    370         case T_NEWLINE:
    371             state.line++;
    372             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     }
    390 
    391 parser_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     }
    402 }
    403 
    404 int 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 
    415 static 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 
    429 struct 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 
    442 struct 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 
    455 struct 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 
    468 void 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 
    478 void 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 
    491 void 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 
    504 void action_for_each_trigger(const char *trigger,
    505                              void (*func)(struct action *act))
    506 {
    507     struct listnode *node;
    508     struct action *act;
    509     list_for_each(node, &action_list) {
    510         act = node_to_item(node, struct action, alist);
    511         if (!strcmp(act->name, trigger)) {
    512             func(act);
    513         }
    514     }
    515 }
    516 
    517 void queue_property_triggers(const char *name, const char *value)
    518 {
    519     struct listnode *node;
    520     struct action *act;
    521     list_for_each(node, &action_list) {
    522         act = node_to_item(node, struct action, alist);
    523         if (!strncmp(act->name, "property:", strlen("property:"))) {
    524             const char *test = act->name + strlen("property:");
    525             int name_length = strlen(name);
    526 
    527             if (!strncmp(name, test, name_length) &&
    528                     test[name_length] == '=' &&
    529                     (!strcmp(test + name_length + 1, value) ||
    530                      !strcmp(test + name_length + 1, "*"))) {
    531                 action_add_queue_tail(act);
    532             }
    533         }
    534     }
    535 }
    536 
    537 void queue_all_property_triggers()
    538 {
    539     struct listnode *node;
    540     struct action *act;
    541     list_for_each(node, &action_list) {
    542         act = node_to_item(node, struct action, alist);
    543         if (!strncmp(act->name, "property:", strlen("property:"))) {
    544             /* parse property name and value
    545                syntax is property:<name>=<value> */
    546             const char* name = act->name + strlen("property:");
    547             const char* equals = strchr(name, '=');
    548             if (equals) {
    549                 char prop_name[PROP_NAME_MAX + 1];
    550                 char value[PROP_VALUE_MAX];
    551                 int length = equals - name;
    552                 if (length > PROP_NAME_MAX) {
    553                     ERROR("property name too long in trigger %s", act->name);
    554                 } else {
    555                     int ret;
    556                     memcpy(prop_name, name, length);
    557                     prop_name[length] = 0;
    558 
    559                     /* does the property exist, and match the trigger value? */
    560                     ret = property_get(prop_name, value);
    561                     if (ret > 0 && (!strcmp(equals + 1, value) ||
    562                                     !strcmp(equals + 1, "*"))) {
    563                         action_add_queue_tail(act);
    564                     }
    565                 }
    566             }
    567         }
    568     }
    569 }
    570 
    571 void queue_builtin_action(int (*func)(int nargs, char **args), char *name)
    572 {
    573     struct action *act;
    574     struct command *cmd;
    575 
    576     act = calloc(1, sizeof(*act));
    577     act->name = name;
    578     list_init(&act->commands);
    579     list_init(&act->qlist);
    580 
    581     cmd = calloc(1, sizeof(*cmd));
    582     cmd->func = func;
    583     cmd->args[0] = name;
    584     cmd->nargs = 1;
    585     list_add_tail(&act->commands, &cmd->clist);
    586 
    587     list_add_tail(&action_list, &act->alist);
    588     action_add_queue_tail(act);
    589 }
    590 
    591 void action_add_queue_tail(struct action *act)
    592 {
    593     if (list_empty(&act->qlist)) {
    594         list_add_tail(&action_queue, &act->qlist);
    595     }
    596 }
    597 
    598 struct action *action_remove_queue_head(void)
    599 {
    600     if (list_empty(&action_queue)) {
    601         return 0;
    602     } else {
    603         struct listnode *node = list_head(&action_queue);
    604         struct action *act = node_to_item(node, struct action, qlist);
    605         list_remove(node);
    606         list_init(node);
    607         return act;
    608     }
    609 }
    610 
    611 int action_queue_empty()
    612 {
    613     return list_empty(&action_queue);
    614 }
    615 
    616 static void *parse_service(struct parse_state *state, int nargs, char **args)
    617 {
    618     struct service *svc;
    619     if (nargs < 3) {
    620         parse_error(state, "services must have a name and a program\n");
    621         return 0;
    622     }
    623     if (!valid_name(args[1])) {
    624         parse_error(state, "invalid service name '%s'\n", args[1]);
    625         return 0;
    626     }
    627 
    628     svc = service_find_by_name(args[1]);
    629     if (svc) {
    630         parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
    631         return 0;
    632     }
    633 
    634     nargs -= 2;
    635     svc = calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
    636     if (!svc) {
    637         parse_error(state, "out of memory\n");
    638         return 0;
    639     }
    640     svc->name = args[1];
    641     svc->classname = "default";
    642     memcpy(svc->args, args + 2, sizeof(char*) * nargs);
    643     svc->args[nargs] = 0;
    644     svc->nargs = nargs;
    645     svc->onrestart.name = "onrestart";
    646     list_init(&svc->onrestart.commands);
    647     list_add_tail(&service_list, &svc->slist);
    648     return svc;
    649 }
    650 
    651 static void parse_line_service(struct parse_state *state, int nargs, char **args)
    652 {
    653     struct service *svc = state->context;
    654     struct command *cmd;
    655     int i, kw, kw_nargs;
    656 
    657     if (nargs == 0) {
    658         return;
    659     }
    660 
    661     svc->ioprio_class = IoSchedClass_NONE;
    662 
    663     kw = lookup_keyword(args[0]);
    664     switch (kw) {
    665     case K_capability:
    666         break;
    667     case K_class:
    668         if (nargs != 2) {
    669             parse_error(state, "class option requires a classname\n");
    670         } else {
    671             svc->classname = args[1];
    672         }
    673         break;
    674     case K_console:
    675         svc->flags |= SVC_CONSOLE;
    676         break;
    677     case K_disabled:
    678         svc->flags |= SVC_DISABLED;
    679         svc->flags |= SVC_RC_DISABLED;
    680         break;
    681     case K_ioprio:
    682         if (nargs != 3) {
    683             parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
    684         } else {
    685             svc->ioprio_pri = strtoul(args[2], 0, 8);
    686 
    687             if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
    688                 parse_error(state, "priority value must be range 0 - 7\n");
    689                 break;
    690             }
    691 
    692             if (!strcmp(args[1], "rt")) {
    693                 svc->ioprio_class = IoSchedClass_RT;
    694             } else if (!strcmp(args[1], "be")) {
    695                 svc->ioprio_class = IoSchedClass_BE;
    696             } else if (!strcmp(args[1], "idle")) {
    697                 svc->ioprio_class = IoSchedClass_IDLE;
    698             } else {
    699                 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
    700             }
    701         }
    702         break;
    703     case K_group:
    704         if (nargs < 2) {
    705             parse_error(state, "group option requires a group id\n");
    706         } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
    707             parse_error(state, "group option accepts at most %d supp. groups\n",
    708                         NR_SVC_SUPP_GIDS);
    709         } else {
    710             int n;
    711             svc->gid = decode_uid(args[1]);
    712             for (n = 2; n < nargs; n++) {
    713                 svc->supp_gids[n-2] = decode_uid(args[n]);
    714             }
    715             svc->nr_supp_gids = n - 2;
    716         }
    717         break;
    718     case K_keycodes:
    719         if (nargs < 2) {
    720             parse_error(state, "keycodes option requires atleast one keycode\n");
    721         } else {
    722             svc->keycodes = malloc((nargs - 1) * sizeof(svc->keycodes[0]));
    723             if (!svc->keycodes) {
    724                 parse_error(state, "could not allocate keycodes\n");
    725             } else {
    726                 svc->nkeycodes = nargs - 1;
    727                 for (i = 1; i < nargs; i++) {
    728                     svc->keycodes[i - 1] = atoi(args[i]);
    729                 }
    730             }
    731         }
    732         break;
    733     case K_oneshot:
    734         svc->flags |= SVC_ONESHOT;
    735         break;
    736     case K_onrestart:
    737         nargs--;
    738         args++;
    739         kw = lookup_keyword(args[0]);
    740         if (!kw_is(kw, COMMAND)) {
    741             parse_error(state, "invalid command '%s'\n", args[0]);
    742             break;
    743         }
    744         kw_nargs = kw_nargs(kw);
    745         if (nargs < kw_nargs) {
    746             parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
    747                 kw_nargs > 2 ? "arguments" : "argument");
    748             break;
    749         }
    750 
    751         cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
    752         cmd->func = kw_func(kw);
    753         cmd->nargs = nargs;
    754         memcpy(cmd->args, args, sizeof(char*) * nargs);
    755         list_add_tail(&svc->onrestart.commands, &cmd->clist);
    756         break;
    757     case K_critical:
    758         svc->flags |= SVC_CRITICAL;
    759         break;
    760     case K_setenv: { /* name value */
    761         struct svcenvinfo *ei;
    762         if (nargs < 3) {
    763             parse_error(state, "setenv option requires name and value arguments\n");
    764             break;
    765         }
    766         ei = calloc(1, sizeof(*ei));
    767         if (!ei) {
    768             parse_error(state, "out of memory\n");
    769             break;
    770         }
    771         ei->name = args[1];
    772         ei->value = args[2];
    773         ei->next = svc->envvars;
    774         svc->envvars = ei;
    775         break;
    776     }
    777     case K_socket: {/* name type perm [ uid gid context ] */
    778         struct socketinfo *si;
    779         if (nargs < 4) {
    780             parse_error(state, "socket option requires name, type, perm arguments\n");
    781             break;
    782         }
    783         if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
    784                 && strcmp(args[2],"seqpacket")) {
    785             parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
    786             break;
    787         }
    788         si = calloc(1, sizeof(*si));
    789         if (!si) {
    790             parse_error(state, "out of memory\n");
    791             break;
    792         }
    793         si->name = args[1];
    794         si->type = args[2];
    795         si->perm = strtoul(args[3], 0, 8);
    796         if (nargs > 4)
    797             si->uid = decode_uid(args[4]);
    798         if (nargs > 5)
    799             si->gid = decode_uid(args[5]);
    800         if (nargs > 6)
    801             si->socketcon = args[6];
    802         si->next = svc->sockets;
    803         svc->sockets = si;
    804         break;
    805     }
    806     case K_user:
    807         if (nargs != 2) {
    808             parse_error(state, "user option requires a user id\n");
    809         } else {
    810             svc->uid = decode_uid(args[1]);
    811         }
    812         break;
    813     case K_seclabel:
    814         if (nargs != 2) {
    815             parse_error(state, "seclabel option requires a label string\n");
    816         } else {
    817             svc->seclabel = args[1];
    818         }
    819         break;
    820 
    821     default:
    822         parse_error(state, "invalid option '%s'\n", args[0]);
    823     }
    824 }
    825 
    826 static void *parse_action(struct parse_state *state, int nargs, char **args)
    827 {
    828     struct action *act;
    829     if (nargs < 2) {
    830         parse_error(state, "actions must have a trigger\n");
    831         return 0;
    832     }
    833     if (nargs > 2) {
    834         parse_error(state, "actions may not have extra parameters\n");
    835         return 0;
    836     }
    837     act = calloc(1, sizeof(*act));
    838     act->name = args[1];
    839     list_init(&act->commands);
    840     list_init(&act->qlist);
    841     list_add_tail(&action_list, &act->alist);
    842         /* XXX add to hash */
    843     return act;
    844 }
    845 
    846 static void parse_line_action(struct parse_state* state, int nargs, char **args)
    847 {
    848     struct command *cmd;
    849     struct action *act = state->context;
    850     int (*func)(int nargs, char **args);
    851     int kw, n;
    852 
    853     if (nargs == 0) {
    854         return;
    855     }
    856 
    857     kw = lookup_keyword(args[0]);
    858     if (!kw_is(kw, COMMAND)) {
    859         parse_error(state, "invalid command '%s'\n", args[0]);
    860         return;
    861     }
    862 
    863     n = kw_nargs(kw);
    864     if (nargs < n) {
    865         parse_error(state, "%s requires %d %s\n", args[0], n - 1,
    866             n > 2 ? "arguments" : "argument");
    867         return;
    868     }
    869     cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
    870     cmd->func = kw_func(kw);
    871     cmd->line = state->line;
    872     cmd->filename = state->filename;
    873     cmd->nargs = nargs;
    874     memcpy(cmd->args, args, sizeof(char*) * nargs);
    875     list_add_tail(&act->commands, &cmd->clist);
    876 }
    877