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