The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 <string.h> |
| 20 | #include <unistd.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <ctype.h> |
| 23 | #include <signal.h> |
| 24 | #include <sys/wait.h> |
| 25 | #include <sys/mount.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/poll.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 28 | #include <errno.h> |
| 29 | #include <stdarg.h> |
| 30 | #include <mtd/mtd-user.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <sys/un.h> |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 34 | #include <libgen.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 35 | |
Dima Zavin | da04c52 | 2011-09-01 17:09:44 -0700 | [diff] [blame] | 36 | #include <cutils/list.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 37 | #include <cutils/sockets.h> |
San Mehat | 4e221f0 | 2010-02-25 14:19:50 -0800 | [diff] [blame] | 38 | #include <cutils/iosched_policy.h> |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 39 | #include <private/android_filesystem_config.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 40 | #include <termios.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 41 | |
| 42 | #include <sys/system_properties.h> |
| 43 | |
| 44 | #include "devices.h" |
| 45 | #include "init.h" |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 46 | #include "log.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 47 | #include "property_service.h" |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 48 | #include "bootchart.h" |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 49 | #include "signal_handler.h" |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 50 | #include "keychords.h" |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 51 | #include "init_parser.h" |
Colin Cross | 3899e9f | 2010-04-13 20:35:46 -0700 | [diff] [blame] | 52 | #include "util.h" |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 53 | #include "ueventd.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 54 | |
| 55 | static int property_triggers_enabled = 0; |
| 56 | |
| 57 | #if BOOTCHART |
| 58 | static int bootchart_count; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 59 | #endif |
| 60 | |
| 61 | static char console[32]; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 62 | static char bootmode[32]; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 63 | static char hardware[32]; |
| 64 | static unsigned revision = 0; |
| 65 | static char qemu[32]; |
| 66 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 67 | static struct action *cur_action = NULL; |
| 68 | static struct command *cur_command = NULL; |
| 69 | static struct listnode *command_queue = NULL; |
| 70 | |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 71 | void notify_service_state(const char *name, const char *state) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 72 | { |
| 73 | char pname[PROP_NAME_MAX]; |
| 74 | int len = strlen(name); |
| 75 | if ((len + 10) > PROP_NAME_MAX) |
| 76 | return; |
| 77 | snprintf(pname, sizeof(pname), "init.svc.%s", name); |
| 78 | property_set(pname, state); |
| 79 | } |
| 80 | |
| 81 | static int have_console; |
| 82 | static char *console_name = "/dev/console"; |
| 83 | static time_t process_needs_restart; |
| 84 | |
| 85 | static const char *ENV[32]; |
| 86 | |
| 87 | /* add_environment - add "key=value" to the current environment */ |
| 88 | int add_environment(const char *key, const char *val) |
| 89 | { |
| 90 | int n; |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 91 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 92 | for (n = 0; n < 31; n++) { |
| 93 | if (!ENV[n]) { |
| 94 | size_t len = strlen(key) + strlen(val) + 2; |
| 95 | char *entry = malloc(len); |
| 96 | snprintf(entry, len, "%s=%s", key, val); |
| 97 | ENV[n] = entry; |
| 98 | return 0; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return 1; |
| 103 | } |
| 104 | |
| 105 | static void zap_stdio(void) |
| 106 | { |
| 107 | int fd; |
| 108 | fd = open("/dev/null", O_RDWR); |
| 109 | dup2(fd, 0); |
| 110 | dup2(fd, 1); |
| 111 | dup2(fd, 2); |
| 112 | close(fd); |
| 113 | } |
| 114 | |
| 115 | static void open_console() |
| 116 | { |
| 117 | int fd; |
| 118 | if ((fd = open(console_name, O_RDWR)) < 0) { |
| 119 | fd = open("/dev/null", O_RDWR); |
| 120 | } |
| 121 | dup2(fd, 0); |
| 122 | dup2(fd, 1); |
| 123 | dup2(fd, 2); |
| 124 | close(fd); |
| 125 | } |
| 126 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 127 | static void publish_socket(const char *name, int fd) |
| 128 | { |
| 129 | char key[64] = ANDROID_SOCKET_ENV_PREFIX; |
| 130 | char val[64]; |
| 131 | |
| 132 | strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, |
| 133 | name, |
| 134 | sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); |
| 135 | snprintf(val, sizeof(val), "%d", fd); |
| 136 | add_environment(key, val); |
| 137 | |
| 138 | /* make sure we don't close-on-exec */ |
| 139 | fcntl(fd, F_SETFD, 0); |
| 140 | } |
| 141 | |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 142 | void service_start(struct service *svc, const char *dynamic_args) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 143 | { |
| 144 | struct stat s; |
| 145 | pid_t pid; |
| 146 | int needs_console; |
| 147 | int n; |
| 148 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 149 | /* starting a service removes it from the disabled or reset |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 150 | * state and immediately takes it out of the restarting |
| 151 | * state if it was in there |
| 152 | */ |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 153 | svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 154 | svc->time_started = 0; |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 155 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 156 | /* running processes require no additional work -- if |
| 157 | * they're in the process of exiting, we've ensured |
| 158 | * that they will immediately restart on exit, unless |
| 159 | * they are ONESHOT |
| 160 | */ |
| 161 | if (svc->flags & SVC_RUNNING) { |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0; |
| 166 | if (needs_console && (!have_console)) { |
| 167 | ERROR("service '%s' requires console\n", svc->name); |
| 168 | svc->flags |= SVC_DISABLED; |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | if (stat(svc->args[0], &s) != 0) { |
| 173 | ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name); |
| 174 | svc->flags |= SVC_DISABLED; |
| 175 | return; |
| 176 | } |
| 177 | |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 178 | if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) { |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 179 | ERROR("service '%s' must be one-shot to use dynamic args, disabling\n", |
| 180 | svc->args[0]); |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 181 | svc->flags |= SVC_DISABLED; |
| 182 | return; |
| 183 | } |
| 184 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 185 | NOTICE("starting '%s'\n", svc->name); |
| 186 | |
| 187 | pid = fork(); |
| 188 | |
| 189 | if (pid == 0) { |
| 190 | struct socketinfo *si; |
| 191 | struct svcenvinfo *ei; |
| 192 | char tmp[32]; |
| 193 | int fd, sz; |
| 194 | |
Colin Cross | 3294bbb | 2010-04-19 17:11:33 -0700 | [diff] [blame] | 195 | if (properties_inited()) { |
| 196 | get_property_workspace(&fd, &sz); |
| 197 | sprintf(tmp, "%d,%d", dup(fd), sz); |
| 198 | add_environment("ANDROID_PROPERTY_WORKSPACE", tmp); |
| 199 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 200 | |
| 201 | for (ei = svc->envvars; ei; ei = ei->next) |
| 202 | add_environment(ei->name, ei->value); |
| 203 | |
| 204 | for (si = svc->sockets; si; si = si->next) { |
Mike Lockwood | 912ff85 | 2010-10-01 08:20:36 -0400 | [diff] [blame] | 205 | int socket_type = ( |
| 206 | !strcmp(si->type, "stream") ? SOCK_STREAM : |
| 207 | (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET)); |
| 208 | int s = create_socket(si->name, socket_type, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 209 | si->perm, si->uid, si->gid); |
| 210 | if (s >= 0) { |
| 211 | publish_socket(si->name, s); |
| 212 | } |
| 213 | } |
| 214 | |
San Mehat | 4e221f0 | 2010-02-25 14:19:50 -0800 | [diff] [blame] | 215 | if (svc->ioprio_class != IoSchedClass_NONE) { |
| 216 | if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) { |
| 217 | ERROR("Failed to set pid %d ioprio = %d,%d: %s\n", |
| 218 | getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno)); |
| 219 | } |
| 220 | } |
| 221 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 222 | if (needs_console) { |
| 223 | setsid(); |
| 224 | open_console(); |
| 225 | } else { |
| 226 | zap_stdio(); |
| 227 | } |
| 228 | |
| 229 | #if 0 |
| 230 | for (n = 0; svc->args[n]; n++) { |
| 231 | INFO("args[%d] = '%s'\n", n, svc->args[n]); |
| 232 | } |
| 233 | for (n = 0; ENV[n]; n++) { |
| 234 | INFO("env[%d] = '%s'\n", n, ENV[n]); |
| 235 | } |
| 236 | #endif |
| 237 | |
| 238 | setpgid(0, getpid()); |
| 239 | |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 240 | /* as requested, set our gid, supplemental gids, and uid */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 241 | if (svc->gid) { |
Nick Kralevich | 2268718 | 2010-11-17 16:55:42 -0800 | [diff] [blame] | 242 | if (setgid(svc->gid) != 0) { |
| 243 | ERROR("setgid failed: %s\n", strerror(errno)); |
| 244 | _exit(127); |
| 245 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 246 | } |
| 247 | if (svc->nr_supp_gids) { |
Nick Kralevich | 2268718 | 2010-11-17 16:55:42 -0800 | [diff] [blame] | 248 | if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) { |
| 249 | ERROR("setgroups failed: %s\n", strerror(errno)); |
| 250 | _exit(127); |
| 251 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 252 | } |
| 253 | if (svc->uid) { |
Nick Kralevich | 2268718 | 2010-11-17 16:55:42 -0800 | [diff] [blame] | 254 | if (setuid(svc->uid) != 0) { |
| 255 | ERROR("setuid failed: %s\n", strerror(errno)); |
| 256 | _exit(127); |
| 257 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 258 | } |
| 259 | |
San Mehat | 8ad1568 | 2009-05-20 08:50:40 -0700 | [diff] [blame] | 260 | if (!dynamic_args) { |
| 261 | if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) { |
| 262 | ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno)); |
| 263 | } |
| 264 | } else { |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 265 | char *arg_ptrs[INIT_PARSER_MAXARGS+1]; |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 266 | int arg_idx = svc->nargs; |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 267 | char *tmp = strdup(dynamic_args); |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 268 | char *next = tmp; |
| 269 | char *bword; |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 270 | |
| 271 | /* Copy the static arguments */ |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 272 | memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *))); |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 273 | |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 274 | while((bword = strsep(&next, " "))) { |
| 275 | arg_ptrs[arg_idx++] = bword; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 276 | if (arg_idx == INIT_PARSER_MAXARGS) |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 277 | break; |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 278 | } |
| 279 | arg_ptrs[arg_idx] = '\0'; |
| 280 | execve(svc->args[0], (char**) arg_ptrs, (char**) ENV); |
Ivan Djelic | 165de92 | 2008-11-23 22:26:39 +0100 | [diff] [blame] | 281 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 282 | _exit(127); |
| 283 | } |
| 284 | |
| 285 | if (pid < 0) { |
| 286 | ERROR("failed to start '%s'\n", svc->name); |
| 287 | svc->pid = 0; |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | svc->time_started = gettime(); |
| 292 | svc->pid = pid; |
| 293 | svc->flags |= SVC_RUNNING; |
| 294 | |
Colin Cross | 3294bbb | 2010-04-19 17:11:33 -0700 | [diff] [blame] | 295 | if (properties_inited()) |
| 296 | notify_service_state(svc->name, "running"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 299 | /* The how field should be either SVC_DISABLED or SVC_RESET */ |
| 300 | static void service_stop_or_reset(struct service *svc, int how) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 301 | { |
| 302 | /* we are no longer running, nor should we |
| 303 | * attempt to restart |
| 304 | */ |
| 305 | svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING)); |
| 306 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 307 | if ((how != SVC_DISABLED) && (how != SVC_RESET)) { |
| 308 | /* Hrm, an illegal flag. Default to SVC_DISABLED */ |
| 309 | how = SVC_DISABLED; |
| 310 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 311 | /* if the service has not yet started, prevent |
| 312 | * it from auto-starting with its class |
| 313 | */ |
Ken Sumrall | a286480 | 2011-10-26 16:56:00 -0700 | [diff] [blame] | 314 | if (how == SVC_RESET) { |
| 315 | svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET; |
| 316 | } else { |
| 317 | svc->flags |= how; |
| 318 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 319 | |
| 320 | if (svc->pid) { |
| 321 | NOTICE("service '%s' is being killed\n", svc->name); |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 322 | kill(-svc->pid, SIGKILL); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 323 | notify_service_state(svc->name, "stopping"); |
| 324 | } else { |
| 325 | notify_service_state(svc->name, "stopped"); |
| 326 | } |
| 327 | } |
| 328 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 329 | void service_reset(struct service *svc) |
| 330 | { |
| 331 | service_stop_or_reset(svc, SVC_RESET); |
| 332 | } |
| 333 | |
| 334 | void service_stop(struct service *svc) |
| 335 | { |
| 336 | service_stop_or_reset(svc, SVC_DISABLED); |
| 337 | } |
| 338 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 339 | void property_changed(const char *name, const char *value) |
| 340 | { |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 341 | if (property_triggers_enabled) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 342 | queue_property_triggers(name, value); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 343 | } |
| 344 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 345 | static void restart_service_if_needed(struct service *svc) |
| 346 | { |
| 347 | time_t next_start_time = svc->time_started + 5; |
| 348 | |
| 349 | if (next_start_time <= gettime()) { |
| 350 | svc->flags &= (~SVC_RESTARTING); |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 351 | service_start(svc, NULL); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 352 | return; |
| 353 | } |
| 354 | |
| 355 | if ((next_start_time < process_needs_restart) || |
| 356 | (process_needs_restart == 0)) { |
| 357 | process_needs_restart = next_start_time; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | static void restart_processes() |
| 362 | { |
| 363 | process_needs_restart = 0; |
| 364 | service_for_each_flags(SVC_RESTARTING, |
| 365 | restart_service_if_needed); |
| 366 | } |
| 367 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 368 | static void msg_start(const char *name) |
| 369 | { |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 370 | struct service *svc; |
| 371 | char *tmp = NULL; |
| 372 | char *args = NULL; |
| 373 | |
| 374 | if (!strchr(name, ':')) |
| 375 | svc = service_find_by_name(name); |
| 376 | else { |
| 377 | tmp = strdup(name); |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 378 | args = strchr(tmp, ':'); |
| 379 | *args = '\0'; |
| 380 | args++; |
| 381 | |
| 382 | svc = service_find_by_name(tmp); |
| 383 | } |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 384 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 385 | if (svc) { |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 386 | service_start(svc, args); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 387 | } else { |
| 388 | ERROR("no such service '%s'\n", name); |
| 389 | } |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 390 | if (tmp) |
| 391 | free(tmp); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | static void msg_stop(const char *name) |
| 395 | { |
| 396 | struct service *svc = service_find_by_name(name); |
| 397 | |
| 398 | if (svc) { |
| 399 | service_stop(svc); |
| 400 | } else { |
Dima Zavin | 770354d | 2009-05-05 18:33:07 -0700 | [diff] [blame] | 401 | ERROR("no such service '%s'\n", name); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
| 405 | void handle_control_message(const char *msg, const char *arg) |
| 406 | { |
| 407 | if (!strcmp(msg,"start")) { |
| 408 | msg_start(arg); |
| 409 | } else if (!strcmp(msg,"stop")) { |
| 410 | msg_stop(arg); |
Wink Saville | cfa0d84 | 2010-10-03 13:30:11 -0700 | [diff] [blame] | 411 | } else if (!strcmp(msg,"restart")) { |
| 412 | msg_stop(arg); |
| 413 | msg_start(arg); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 414 | } else { |
| 415 | ERROR("unknown control msg '%s'\n", msg); |
| 416 | } |
| 417 | } |
| 418 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 419 | static struct command *get_first_command(struct action *act) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 420 | { |
| 421 | struct listnode *node; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 422 | node = list_head(&act->commands); |
Dima Zavin | 3bea079 | 2011-08-26 13:59:18 -0700 | [diff] [blame] | 423 | if (!node || list_empty(&act->commands)) |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 424 | return NULL; |
| 425 | |
| 426 | return node_to_item(node, struct command, clist); |
| 427 | } |
| 428 | |
| 429 | static struct command *get_next_command(struct action *act, struct command *cmd) |
| 430 | { |
| 431 | struct listnode *node; |
| 432 | node = cmd->clist.next; |
| 433 | if (!node) |
| 434 | return NULL; |
| 435 | if (node == &act->commands) |
| 436 | return NULL; |
| 437 | |
| 438 | return node_to_item(node, struct command, clist); |
| 439 | } |
| 440 | |
| 441 | static int is_last_command(struct action *act, struct command *cmd) |
| 442 | { |
| 443 | return (list_tail(&act->commands) == &cmd->clist); |
| 444 | } |
| 445 | |
| 446 | void execute_one_command(void) |
| 447 | { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 448 | int ret; |
| 449 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 450 | if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) { |
| 451 | cur_action = action_remove_queue_head(); |
Colin Cross | ebd4613 | 2010-04-22 11:52:23 -0700 | [diff] [blame] | 452 | cur_command = NULL; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 453 | if (!cur_action) |
| 454 | return; |
| 455 | INFO("processing action %p (%s)\n", cur_action, cur_action->name); |
| 456 | cur_command = get_first_command(cur_action); |
| 457 | } else { |
| 458 | cur_command = get_next_command(cur_action, cur_command); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 459 | } |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 460 | |
| 461 | if (!cur_command) |
| 462 | return; |
| 463 | |
| 464 | ret = cur_command->func(cur_command->nargs, cur_command->args); |
| 465 | INFO("command '%s' r=%d\n", cur_command->args[0], ret); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 468 | static int wait_for_coldboot_done_action(int nargs, char **args) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 469 | { |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 470 | int ret; |
| 471 | INFO("wait for %s\n", coldboot_done); |
| 472 | ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT); |
| 473 | if (ret) |
| 474 | ERROR("Timed out waiting for %s\n", coldboot_done); |
| 475 | return ret; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 478 | static int keychord_init_action(int nargs, char **args) |
| 479 | { |
| 480 | keychord_init(); |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | static int console_init_action(int nargs, char **args) |
| 485 | { |
| 486 | int fd; |
| 487 | char tmp[PROP_VALUE_MAX]; |
| 488 | |
| 489 | if (console[0]) { |
| 490 | snprintf(tmp, sizeof(tmp), "/dev/%s", console); |
| 491 | console_name = strdup(tmp); |
| 492 | } |
| 493 | |
| 494 | fd = open(console_name, O_RDWR); |
| 495 | if (fd >= 0) |
| 496 | have_console = 1; |
| 497 | close(fd); |
| 498 | |
| 499 | if( load_565rle_image(INIT_IMAGE_FILE) ) { |
| 500 | fd = open("/dev/tty0", O_WRONLY); |
| 501 | if (fd >= 0) { |
| 502 | const char *msg; |
| 503 | msg = "\n" |
| 504 | "\n" |
| 505 | "\n" |
| 506 | "\n" |
| 507 | "\n" |
| 508 | "\n" |
| 509 | "\n" // console is 40 cols x 30 lines |
| 510 | "\n" |
| 511 | "\n" |
| 512 | "\n" |
| 513 | "\n" |
| 514 | "\n" |
| 515 | "\n" |
| 516 | "\n" |
| 517 | " A N D R O I D "; |
| 518 | write(fd, msg, strlen(msg)); |
| 519 | close(fd); |
| 520 | } |
| 521 | } |
| 522 | return 0; |
| 523 | } |
| 524 | |
Dima Zavin | 8886112 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 525 | static void import_kernel_nv(char *name, int for_emulator) |
| 526 | { |
| 527 | char *value = strchr(name, '='); |
| 528 | int name_len = strlen(name); |
| 529 | |
| 530 | if (value == 0) return; |
| 531 | *value++ = 0; |
| 532 | if (name_len == 0) return; |
| 533 | |
| 534 | if (for_emulator) { |
| 535 | /* in the emulator, export any kernel option with the |
| 536 | * ro.kernel. prefix */ |
| 537 | char buff[PROP_NAME_MAX]; |
| 538 | int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name ); |
| 539 | |
| 540 | if (len < (int)sizeof(buff)) |
| 541 | property_set( buff, value ); |
| 542 | return; |
| 543 | } |
| 544 | |
| 545 | if (!strcmp(name,"qemu")) { |
| 546 | strlcpy(qemu, value, sizeof(qemu)); |
| 547 | } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) { |
| 548 | const char *boot_prop_name = name + 12; |
| 549 | char prop[PROP_NAME_MAX]; |
| 550 | int cnt; |
| 551 | |
| 552 | cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name); |
| 553 | if (cnt < PROP_NAME_MAX) |
| 554 | property_set(prop, value); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | static void export_kernel_boot_props(void) |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 559 | { |
| 560 | char tmp[PROP_VALUE_MAX]; |
Dima Zavin | 8886112 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 561 | const char *pval; |
| 562 | unsigned i; |
| 563 | struct { |
| 564 | const char *src_prop; |
| 565 | const char *dest_prop; |
| 566 | const char *def_val; |
| 567 | } prop_map[] = { |
| 568 | { "ro.boot.serialno", "ro.serialno", "", }, |
| 569 | { "ro.boot.mode", "ro.bootmode", "unknown", }, |
| 570 | { "ro.boot.baseband", "ro.baseband", "unknown", }, |
| 571 | { "ro.boot.carrier", "ro.carrier", "unknown", }, |
| 572 | { "ro.boot.bootloader", "ro.bootloader", "unknown", }, |
| 573 | }; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 574 | |
Dima Zavin | 8886112 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 575 | for (i = 0; i < ARRAY_SIZE(prop_map); i++) { |
| 576 | pval = property_get(prop_map[i].src_prop); |
| 577 | property_set(prop_map[i].dest_prop, pval ?: prop_map[i].def_val); |
| 578 | } |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 579 | |
Dima Zavin | 8886112 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 580 | pval = property_get("ro.boot.console"); |
| 581 | if (pval) |
| 582 | strlcpy(console, pval, sizeof(console)); |
| 583 | |
| 584 | /* save a copy for init's usage during boot */ |
| 585 | strlcpy(bootmode, property_get("ro.bootmode"), sizeof(bootmode)); |
| 586 | |
| 587 | /* if this was given on kernel command line, override what we read |
| 588 | * before (e.g. from /proc/cpuinfo), if anything */ |
| 589 | pval = property_get("ro.boot.hardware"); |
| 590 | if (pval) |
| 591 | strlcpy(hardware, pval, sizeof(hardware)); |
| 592 | property_set("ro.hardware", hardware); |
| 593 | |
| 594 | snprintf(tmp, PROP_VALUE_MAX, "%d", revision); |
| 595 | property_set("ro.revision", tmp); |
| 596 | |
| 597 | /* TODO: these are obsolete. We should delete them */ |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 598 | if (!strcmp(bootmode,"factory")) |
| 599 | property_set("ro.factorytest", "1"); |
| 600 | else if (!strcmp(bootmode,"factory2")) |
| 601 | property_set("ro.factorytest", "2"); |
| 602 | else |
| 603 | property_set("ro.factorytest", "0"); |
Dima Zavin | 8886112 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 604 | } |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 605 | |
Dima Zavin | 8886112 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 606 | static void process_kernel_cmdline(void) |
| 607 | { |
| 608 | /* don't expose the raw commandline to nonpriv processes */ |
| 609 | chmod("/proc/cmdline", 0440); |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 610 | |
Dima Zavin | 8886112 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 611 | /* first pass does the common stuff, and finds if we are in qemu. |
| 612 | * second pass is only necessary for qemu to export all kernel params |
| 613 | * as props. |
| 614 | */ |
| 615 | import_kernel_cmdline(0, import_kernel_nv); |
| 616 | if (qemu[0]) |
| 617 | import_kernel_cmdline(1, import_kernel_nv); |
| 618 | |
| 619 | /* now propogate the info given on command line to internal variables |
| 620 | * used by init as well as the current required properties |
| 621 | */ |
| 622 | export_kernel_boot_props(); |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | static int property_service_init_action(int nargs, char **args) |
| 626 | { |
| 627 | /* read any property files on system or data and |
| 628 | * fire up the property service. This must happen |
| 629 | * after the ro.foo properties are set above so |
| 630 | * that /data/local.prop cannot interfere with them. |
| 631 | */ |
| 632 | start_property_service(); |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | static int signal_init_action(int nargs, char **args) |
| 637 | { |
| 638 | signal_init(); |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | static int check_startup_action(int nargs, char **args) |
| 643 | { |
| 644 | /* make sure we actually have all the pieces we need */ |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 645 | if ((get_property_set_fd() < 0) || |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 646 | (get_signal_fd() < 0)) { |
| 647 | ERROR("init startup failure\n"); |
| 648 | exit(1); |
| 649 | } |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 650 | |
| 651 | /* signal that we hit this point */ |
| 652 | unlink("/dev/.booting"); |
| 653 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | static int queue_property_triggers_action(int nargs, char **args) |
| 658 | { |
| 659 | queue_all_property_triggers(); |
| 660 | /* enable property triggers */ |
| 661 | property_triggers_enabled = 1; |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | #if BOOTCHART |
| 666 | static int bootchart_init_action(int nargs, char **args) |
| 667 | { |
| 668 | bootchart_count = bootchart_init(); |
| 669 | if (bootchart_count < 0) { |
| 670 | ERROR("bootcharting init failure\n"); |
| 671 | } else if (bootchart_count > 0) { |
| 672 | NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS); |
| 673 | } else { |
| 674 | NOTICE("bootcharting ignored\n"); |
| 675 | } |
Carl-Emil Lagerstedt | 9ab8190 | 2011-01-14 09:35:30 +0100 | [diff] [blame] | 676 | |
| 677 | return 0; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 678 | } |
| 679 | #endif |
| 680 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 681 | int main(int argc, char **argv) |
| 682 | { |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 683 | int fd_count = 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 684 | struct pollfd ufds[4]; |
| 685 | char *tmpdev; |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 686 | char* debuggable; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 687 | char tmp[32]; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 688 | int property_set_fd_init = 0; |
| 689 | int signal_fd_init = 0; |
| 690 | int keychord_fd_init = 0; |
Dima Zavin | 65a8360 | 2011-12-16 14:18:06 -0800 | [diff] [blame] | 691 | bool is_charger = false; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 692 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 693 | if (!strcmp(basename(argv[0]), "ueventd")) |
| 694 | return ueventd_main(argc, argv); |
| 695 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 696 | /* clear the umask */ |
| 697 | umask(0); |
| 698 | |
| 699 | /* Get the basic filesystem setup we need put |
| 700 | * together in the initramdisk on / and then we'll |
| 701 | * let the rc file figure out the rest. |
| 702 | */ |
| 703 | mkdir("/dev", 0755); |
| 704 | mkdir("/proc", 0755); |
| 705 | mkdir("/sys", 0755); |
| 706 | |
Nick Kralevich | 150f19e | 2010-06-22 16:35:43 -0700 | [diff] [blame] | 707 | mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 708 | mkdir("/dev/pts", 0755); |
| 709 | mkdir("/dev/socket", 0755); |
| 710 | mount("devpts", "/dev/pts", "devpts", 0, NULL); |
| 711 | mount("proc", "/proc", "proc", 0, NULL); |
| 712 | mount("sysfs", "/sys", "sysfs", 0, NULL); |
| 713 | |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 714 | /* indicate that booting is in progress to background fw loaders, etc */ |
| 715 | close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000)); |
| 716 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 717 | /* We must have some place other than / to create the |
| 718 | * device nodes for kmsg and null, otherwise we won't |
| 719 | * be able to remount / read-only later on. |
| 720 | * Now that tmpfs is mounted on /dev, we can actually |
| 721 | * talk to the outside world. |
| 722 | */ |
| 723 | open_devnull_stdio(); |
Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 724 | klog_init(); |
Dima Zavin | 8886112 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 725 | property_init(); |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 726 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 727 | get_hardware_name(hardware, &revision); |
Dima Zavin | 65a8360 | 2011-12-16 14:18:06 -0800 | [diff] [blame] | 728 | |
Dima Zavin | 8886112 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 729 | process_kernel_cmdline(); |
| 730 | |
Dima Zavin | 65a8360 | 2011-12-16 14:18:06 -0800 | [diff] [blame] | 731 | is_charger = !strcmp(bootmode, "charger"); |
| 732 | |
| 733 | INFO("property init\n"); |
Dima Zavin | 8886112 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 734 | if (!is_charger) |
| 735 | property_load_boot_defaults(); |
Dima Zavin | 65a8360 | 2011-12-16 14:18:06 -0800 | [diff] [blame] | 736 | |
| 737 | INFO("reading config file\n"); |
| 738 | init_parse_config_file("/init.rc"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 739 | |
| 740 | action_for_each_trigger("early-init", action_add_queue_tail); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 741 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 742 | queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done"); |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 743 | queue_builtin_action(keychord_init_action, "keychord_init"); |
| 744 | queue_builtin_action(console_init_action, "console_init"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 745 | |
Dima Zavin | ca47cef | 2011-08-24 15:28:23 -0700 | [diff] [blame] | 746 | /* execute all the boot actions to get us started */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 747 | action_for_each_trigger("init", action_add_queue_tail); |
Dima Zavin | ca47cef | 2011-08-24 15:28:23 -0700 | [diff] [blame] | 748 | |
| 749 | /* skip mounting filesystems in charger mode */ |
Dima Zavin | 65a8360 | 2011-12-16 14:18:06 -0800 | [diff] [blame] | 750 | if (!is_charger) { |
Dima Zavin | ca47cef | 2011-08-24 15:28:23 -0700 | [diff] [blame] | 751 | action_for_each_trigger("early-fs", action_add_queue_tail); |
| 752 | action_for_each_trigger("fs", action_add_queue_tail); |
| 753 | action_for_each_trigger("post-fs", action_add_queue_tail); |
| 754 | action_for_each_trigger("post-fs-data", action_add_queue_tail); |
| 755 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 756 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 757 | queue_builtin_action(property_service_init_action, "property_service_init"); |
| 758 | queue_builtin_action(signal_init_action, "signal_init"); |
| 759 | queue_builtin_action(check_startup_action, "check_startup"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 760 | |
Dima Zavin | 65a8360 | 2011-12-16 14:18:06 -0800 | [diff] [blame] | 761 | if (is_charger) { |
Dima Zavin | ca47cef | 2011-08-24 15:28:23 -0700 | [diff] [blame] | 762 | action_for_each_trigger("charger", action_add_queue_tail); |
| 763 | } else { |
| 764 | action_for_each_trigger("early-boot", action_add_queue_tail); |
| 765 | action_for_each_trigger("boot", action_add_queue_tail); |
| 766 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 767 | |
| 768 | /* run all property triggers based on current state of the properties */ |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 769 | queue_builtin_action(queue_property_triggers_action, "queue_propety_triggers"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 770 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 771 | |
| 772 | #if BOOTCHART |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 773 | queue_builtin_action(bootchart_init_action, "bootchart_init"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 774 | #endif |
| 775 | |
| 776 | for(;;) { |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 777 | int nr, i, timeout = -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 778 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 779 | execute_one_command(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 780 | restart_processes(); |
| 781 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 782 | if (!property_set_fd_init && get_property_set_fd() > 0) { |
| 783 | ufds[fd_count].fd = get_property_set_fd(); |
| 784 | ufds[fd_count].events = POLLIN; |
| 785 | ufds[fd_count].revents = 0; |
| 786 | fd_count++; |
| 787 | property_set_fd_init = 1; |
| 788 | } |
| 789 | if (!signal_fd_init && get_signal_fd() > 0) { |
| 790 | ufds[fd_count].fd = get_signal_fd(); |
| 791 | ufds[fd_count].events = POLLIN; |
| 792 | ufds[fd_count].revents = 0; |
| 793 | fd_count++; |
| 794 | signal_fd_init = 1; |
| 795 | } |
| 796 | if (!keychord_fd_init && get_keychord_fd() > 0) { |
| 797 | ufds[fd_count].fd = get_keychord_fd(); |
| 798 | ufds[fd_count].events = POLLIN; |
| 799 | ufds[fd_count].revents = 0; |
| 800 | fd_count++; |
| 801 | keychord_fd_init = 1; |
| 802 | } |
| 803 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 804 | if (process_needs_restart) { |
| 805 | timeout = (process_needs_restart - gettime()) * 1000; |
| 806 | if (timeout < 0) |
| 807 | timeout = 0; |
| 808 | } |
| 809 | |
Colin Cross | ebd4613 | 2010-04-22 11:52:23 -0700 | [diff] [blame] | 810 | if (!action_queue_empty() || cur_action) |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 811 | timeout = 0; |
| 812 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 813 | #if BOOTCHART |
| 814 | if (bootchart_count > 0) { |
| 815 | if (timeout < 0 || timeout > BOOTCHART_POLLING_MS) |
| 816 | timeout = BOOTCHART_POLLING_MS; |
| 817 | if (bootchart_step() < 0 || --bootchart_count == 0) { |
| 818 | bootchart_finish(); |
| 819 | bootchart_count = 0; |
| 820 | } |
| 821 | } |
| 822 | #endif |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 823 | |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 824 | nr = poll(ufds, fd_count, timeout); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 825 | if (nr <= 0) |
| 826 | continue; |
| 827 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 828 | for (i = 0; i < fd_count; i++) { |
| 829 | if (ufds[i].revents == POLLIN) { |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 830 | if (ufds[i].fd == get_property_set_fd()) |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 831 | handle_property_set_fd(); |
| 832 | else if (ufds[i].fd == get_keychord_fd()) |
| 833 | handle_keychord(); |
| 834 | else if (ufds[i].fd == get_signal_fd()) |
| 835 | handle_signal(); |
| 836 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 837 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | return 0; |
| 841 | } |