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