• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
39 static list_declare(service_list);
40 static list_declare(action_list);
41 static list_declare(action_queue);
42 
43 struct import {
44     struct listnode list;
45     const char *filename;
46 };
47 
48 static void *parse_service(struct parse_state *state, int nargs, char **args);
49 static void parse_line_service(struct parse_state *state, int nargs, char **args);
50 
51 static void *parse_action(struct parse_state *state, int nargs, char **args);
52 static 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 
63 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 
lookup_keyword(const char * s)79 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, "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, "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 
parse_line_no_op(struct parse_state * state,int nargs,char ** args)172 void parse_line_no_op(struct parse_state *state, int nargs, char **args)
173 {
174 }
175 
push_chars(char ** dst,int * len,const char * chars,int cnt)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 
expand_props(char * dst,const char * src,int dst_size)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 
parse_import(struct parse_state * state,int nargs,char ** args)295 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 
parse_new_section(struct parse_state * state,int kw,int nargs,char ** args)320 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 
parse_config(const char * fn,char * s)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 
init_parse_config_file(const char * fn)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 
valid_name(const char * name)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 
service_find_by_name(const char * name)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 
service_find_by_pid(pid_t pid)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 
service_find_by_keychord(int keychord_id)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 
service_for_each(void (* func)(struct service * svc))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 
service_for_each_class(const char * classname,void (* func)(struct service * svc))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 
service_for_each_flags(unsigned matchflags,void (* func)(struct service * svc))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 
action_for_each_trigger(const char * trigger,void (* func)(struct action * act))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 
queue_property_triggers(const char * name,const char * value)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 
queue_all_property_triggers()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                     memcpy(prop_name, name, length);
556                     prop_name[length] = 0;
557 
558                     /* does the property exist, and match the trigger value? */
559                     property_get(prop_name, value);
560                     if (!strcmp(equals + 1, value) ||!strcmp(equals + 1, "*")) {
561                         action_add_queue_tail(act);
562                     }
563                 }
564             }
565         }
566     }
567 }
568 
queue_builtin_action(int (* func)(int nargs,char ** args),char * name)569 void queue_builtin_action(int (*func)(int nargs, char **args), char *name)
570 {
571     struct action *act;
572     struct command *cmd;
573 
574     act = calloc(1, sizeof(*act));
575     act->name = name;
576     list_init(&act->commands);
577     list_init(&act->qlist);
578 
579     cmd = calloc(1, sizeof(*cmd));
580     cmd->func = func;
581     cmd->args[0] = name;
582     list_add_tail(&act->commands, &cmd->clist);
583 
584     list_add_tail(&action_list, &act->alist);
585     action_add_queue_tail(act);
586 }
587 
action_add_queue_tail(struct action * act)588 void action_add_queue_tail(struct action *act)
589 {
590     if (list_empty(&act->qlist)) {
591         list_add_tail(&action_queue, &act->qlist);
592     }
593 }
594 
action_remove_queue_head(void)595 struct action *action_remove_queue_head(void)
596 {
597     if (list_empty(&action_queue)) {
598         return 0;
599     } else {
600         struct listnode *node = list_head(&action_queue);
601         struct action *act = node_to_item(node, struct action, qlist);
602         list_remove(node);
603         list_init(node);
604         return act;
605     }
606 }
607 
action_queue_empty()608 int action_queue_empty()
609 {
610     return list_empty(&action_queue);
611 }
612 
parse_service(struct parse_state * state,int nargs,char ** args)613 static void *parse_service(struct parse_state *state, int nargs, char **args)
614 {
615     struct service *svc;
616     if (nargs < 3) {
617         parse_error(state, "services must have a name and a program\n");
618         return 0;
619     }
620     if (!valid_name(args[1])) {
621         parse_error(state, "invalid service name '%s'\n", args[1]);
622         return 0;
623     }
624 
625     svc = service_find_by_name(args[1]);
626     if (svc) {
627         parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
628         return 0;
629     }
630 
631     nargs -= 2;
632     svc = calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
633     if (!svc) {
634         parse_error(state, "out of memory\n");
635         return 0;
636     }
637     svc->name = args[1];
638     svc->classname = "default";
639     memcpy(svc->args, args + 2, sizeof(char*) * nargs);
640     svc->args[nargs] = 0;
641     svc->nargs = nargs;
642     svc->onrestart.name = "onrestart";
643     list_init(&svc->onrestart.commands);
644     list_add_tail(&service_list, &svc->slist);
645     return svc;
646 }
647 
parse_line_service(struct parse_state * state,int nargs,char ** args)648 static void parse_line_service(struct parse_state *state, int nargs, char **args)
649 {
650     struct service *svc = state->context;
651     struct command *cmd;
652     int i, kw, kw_nargs;
653 
654     if (nargs == 0) {
655         return;
656     }
657 
658     svc->ioprio_class = IoSchedClass_NONE;
659 
660     kw = lookup_keyword(args[0]);
661     switch (kw) {
662     case K_capability:
663         break;
664     case K_class:
665         if (nargs != 2) {
666             parse_error(state, "class option requires a classname\n");
667         } else {
668             svc->classname = args[1];
669         }
670         break;
671     case K_console:
672         svc->flags |= SVC_CONSOLE;
673         break;
674     case K_disabled:
675         svc->flags |= SVC_DISABLED;
676         svc->flags |= SVC_RC_DISABLED;
677         break;
678     case K_ioprio:
679         if (nargs != 3) {
680             parse_error(state, "ioprio optin usage: ioprio <rt|be|idle> <ioprio 0-7>\n");
681         } else {
682             svc->ioprio_pri = strtoul(args[2], 0, 8);
683 
684             if (svc->ioprio_pri < 0 || svc->ioprio_pri > 7) {
685                 parse_error(state, "priority value must be range 0 - 7\n");
686                 break;
687             }
688 
689             if (!strcmp(args[1], "rt")) {
690                 svc->ioprio_class = IoSchedClass_RT;
691             } else if (!strcmp(args[1], "be")) {
692                 svc->ioprio_class = IoSchedClass_BE;
693             } else if (!strcmp(args[1], "idle")) {
694                 svc->ioprio_class = IoSchedClass_IDLE;
695             } else {
696                 parse_error(state, "ioprio option usage: ioprio <rt|be|idle> <0-7>\n");
697             }
698         }
699         break;
700     case K_group:
701         if (nargs < 2) {
702             parse_error(state, "group option requires a group id\n");
703         } else if (nargs > NR_SVC_SUPP_GIDS + 2) {
704             parse_error(state, "group option accepts at most %d supp. groups\n",
705                         NR_SVC_SUPP_GIDS);
706         } else {
707             int n;
708             svc->gid = decode_uid(args[1]);
709             for (n = 2; n < nargs; n++) {
710                 svc->supp_gids[n-2] = decode_uid(args[n]);
711             }
712             svc->nr_supp_gids = n - 2;
713         }
714         break;
715     case K_keycodes:
716         if (nargs < 2) {
717             parse_error(state, "keycodes option requires atleast one keycode\n");
718         } else {
719             svc->keycodes = malloc((nargs - 1) * sizeof(svc->keycodes[0]));
720             if (!svc->keycodes) {
721                 parse_error(state, "could not allocate keycodes\n");
722             } else {
723                 svc->nkeycodes = nargs - 1;
724                 for (i = 1; i < nargs; i++) {
725                     svc->keycodes[i - 1] = atoi(args[i]);
726                 }
727             }
728         }
729         break;
730     case K_oneshot:
731         svc->flags |= SVC_ONESHOT;
732         break;
733     case K_onrestart:
734         nargs--;
735         args++;
736         kw = lookup_keyword(args[0]);
737         if (!kw_is(kw, COMMAND)) {
738             parse_error(state, "invalid command '%s'\n", args[0]);
739             break;
740         }
741         kw_nargs = kw_nargs(kw);
742         if (nargs < kw_nargs) {
743             parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1,
744                 kw_nargs > 2 ? "arguments" : "argument");
745             break;
746         }
747 
748         cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
749         cmd->func = kw_func(kw);
750         cmd->nargs = nargs;
751         memcpy(cmd->args, args, sizeof(char*) * nargs);
752         list_add_tail(&svc->onrestart.commands, &cmd->clist);
753         break;
754     case K_critical:
755         svc->flags |= SVC_CRITICAL;
756         break;
757     case K_setenv: { /* name value */
758         struct svcenvinfo *ei;
759         if (nargs < 2) {
760             parse_error(state, "setenv option requires name and value arguments\n");
761             break;
762         }
763         ei = calloc(1, sizeof(*ei));
764         if (!ei) {
765             parse_error(state, "out of memory\n");
766             break;
767         }
768         ei->name = args[1];
769         ei->value = args[2];
770         ei->next = svc->envvars;
771         svc->envvars = ei;
772         break;
773     }
774     case K_socket: {/* name type perm [ uid gid ] */
775         struct socketinfo *si;
776         if (nargs < 4) {
777             parse_error(state, "socket option requires name, type, perm arguments\n");
778             break;
779         }
780         if (strcmp(args[2],"dgram") && strcmp(args[2],"stream")
781                 && strcmp(args[2],"seqpacket")) {
782             parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
783             break;
784         }
785         si = calloc(1, sizeof(*si));
786         if (!si) {
787             parse_error(state, "out of memory\n");
788             break;
789         }
790         si->name = args[1];
791         si->type = args[2];
792         si->perm = strtoul(args[3], 0, 8);
793         if (nargs > 4)
794             si->uid = decode_uid(args[4]);
795         if (nargs > 5)
796             si->gid = decode_uid(args[5]);
797         si->next = svc->sockets;
798         svc->sockets = si;
799         break;
800     }
801     case K_user:
802         if (nargs != 2) {
803             parse_error(state, "user option requires a user id\n");
804         } else {
805             svc->uid = decode_uid(args[1]);
806         }
807         break;
808     case K_seclabel:
809         if (nargs != 2) {
810             parse_error(state, "seclabel option requires a label string\n");
811         } else {
812             svc->seclabel = args[1];
813         }
814         break;
815 
816     default:
817         parse_error(state, "invalid option '%s'\n", args[0]);
818     }
819 }
820 
parse_action(struct parse_state * state,int nargs,char ** args)821 static void *parse_action(struct parse_state *state, int nargs, char **args)
822 {
823     struct action *act;
824     if (nargs < 2) {
825         parse_error(state, "actions must have a trigger\n");
826         return 0;
827     }
828     if (nargs > 2) {
829         parse_error(state, "actions may not have extra parameters\n");
830         return 0;
831     }
832     act = calloc(1, sizeof(*act));
833     act->name = args[1];
834     list_init(&act->commands);
835     list_init(&act->qlist);
836     list_add_tail(&action_list, &act->alist);
837         /* XXX add to hash */
838     return act;
839 }
840 
parse_line_action(struct parse_state * state,int nargs,char ** args)841 static void parse_line_action(struct parse_state* state, int nargs, char **args)
842 {
843     struct command *cmd;
844     struct action *act = state->context;
845     int (*func)(int nargs, char **args);
846     int kw, n;
847 
848     if (nargs == 0) {
849         return;
850     }
851 
852     kw = lookup_keyword(args[0]);
853     if (!kw_is(kw, COMMAND)) {
854         parse_error(state, "invalid command '%s'\n", args[0]);
855         return;
856     }
857 
858     n = kw_nargs(kw);
859     if (nargs < n) {
860         parse_error(state, "%s requires %d %s\n", args[0], n - 1,
861             n > 2 ? "arguments" : "argument");
862         return;
863     }
864     cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
865     cmd->func = kw_func(kw);
866     cmd->nargs = nargs;
867     memcpy(cmd->args, args, sizeof(char*) * nargs);
868     list_add_tail(&act->commands, &cmd->clist);
869 }
870