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