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