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> |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 34 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 35 | #include <selinux/selinux.h> |
| 36 | #include <selinux/label.h> |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 37 | #include <selinux/android.h> |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 38 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 39 | #include <libgen.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 40 | |
Dima Zavin | da04c52 | 2011-09-01 17:09:44 -0700 | [diff] [blame] | 41 | #include <cutils/list.h> |
Nick Kralevich | b710ed2 | 2013-06-24 17:41:40 -0700 | [diff] [blame] | 42 | #include <cutils/android_reboot.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 43 | #include <cutils/sockets.h> |
San Mehat | 4e221f0 | 2010-02-25 14:19:50 -0800 | [diff] [blame] | 44 | #include <cutils/iosched_policy.h> |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 45 | #include <cutils/fs.h> |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 46 | #include <private/android_filesystem_config.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 47 | #include <termios.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 48 | |
| 49 | #include <sys/system_properties.h> |
| 50 | |
| 51 | #include "devices.h" |
| 52 | #include "init.h" |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 53 | #include "log.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 54 | #include "property_service.h" |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 55 | #include "bootchart.h" |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 56 | #include "signal_handler.h" |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 57 | #include "keychords.h" |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 58 | #include "init_parser.h" |
Colin Cross | 3899e9f | 2010-04-13 20:35:46 -0700 | [diff] [blame] | 59 | #include "util.h" |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 60 | #include "ueventd.h" |
Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 61 | #include "watchdogd.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 62 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 63 | struct selabel_handle *sehandle; |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 64 | struct selabel_handle *sehandle_prop; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 65 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 66 | static int property_triggers_enabled = 0; |
| 67 | |
| 68 | #if BOOTCHART |
| 69 | static int bootchart_count; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 70 | #endif |
| 71 | |
| 72 | static char console[32]; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 73 | static char bootmode[32]; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 74 | static char hardware[32]; |
| 75 | static unsigned revision = 0; |
| 76 | static char qemu[32]; |
| 77 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 78 | static struct action *cur_action = NULL; |
| 79 | static struct command *cur_command = NULL; |
| 80 | static struct listnode *command_queue = NULL; |
| 81 | |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 82 | 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] | 83 | { |
| 84 | char pname[PROP_NAME_MAX]; |
| 85 | int len = strlen(name); |
| 86 | if ((len + 10) > PROP_NAME_MAX) |
| 87 | return; |
| 88 | snprintf(pname, sizeof(pname), "init.svc.%s", name); |
| 89 | property_set(pname, state); |
| 90 | } |
| 91 | |
| 92 | static int have_console; |
Hong-Mei Li | 1146718 | 2013-04-01 11:17:51 +0800 | [diff] [blame] | 93 | static char console_name[PROP_VALUE_MAX] = "/dev/console"; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 94 | static time_t process_needs_restart; |
| 95 | |
| 96 | static const char *ENV[32]; |
| 97 | |
| 98 | /* add_environment - add "key=value" to the current environment */ |
| 99 | int add_environment(const char *key, const char *val) |
| 100 | { |
| 101 | int n; |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 102 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 103 | for (n = 0; n < 31; n++) { |
| 104 | if (!ENV[n]) { |
| 105 | size_t len = strlen(key) + strlen(val) + 2; |
| 106 | char *entry = malloc(len); |
| 107 | snprintf(entry, len, "%s=%s", key, val); |
| 108 | ENV[n] = entry; |
| 109 | return 0; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return 1; |
| 114 | } |
| 115 | |
| 116 | static void zap_stdio(void) |
| 117 | { |
| 118 | int fd; |
| 119 | fd = open("/dev/null", O_RDWR); |
| 120 | dup2(fd, 0); |
| 121 | dup2(fd, 1); |
| 122 | dup2(fd, 2); |
| 123 | close(fd); |
| 124 | } |
| 125 | |
| 126 | static void open_console() |
| 127 | { |
| 128 | int fd; |
| 129 | if ((fd = open(console_name, O_RDWR)) < 0) { |
| 130 | fd = open("/dev/null", O_RDWR); |
| 131 | } |
Colin Cross | 50fb5a6 | 2012-03-18 15:38:19 -0700 | [diff] [blame] | 132 | ioctl(fd, TIOCSCTTY, 0); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | dup2(fd, 0); |
| 134 | dup2(fd, 1); |
| 135 | dup2(fd, 2); |
| 136 | close(fd); |
| 137 | } |
| 138 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 139 | static void publish_socket(const char *name, int fd) |
| 140 | { |
| 141 | char key[64] = ANDROID_SOCKET_ENV_PREFIX; |
| 142 | char val[64]; |
| 143 | |
| 144 | strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, |
| 145 | name, |
| 146 | sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); |
| 147 | snprintf(val, sizeof(val), "%d", fd); |
| 148 | add_environment(key, val); |
| 149 | |
| 150 | /* make sure we don't close-on-exec */ |
| 151 | fcntl(fd, F_SETFD, 0); |
| 152 | } |
| 153 | |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 154 | 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] | 155 | { |
| 156 | struct stat s; |
| 157 | pid_t pid; |
| 158 | int needs_console; |
| 159 | int n; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 160 | char *scon = NULL; |
| 161 | int rc; |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 162 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 163 | /* 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] | 164 | * state and immediately takes it out of the restarting |
| 165 | * state if it was in there |
| 166 | */ |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 167 | svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 168 | svc->time_started = 0; |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 169 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 170 | /* running processes require no additional work -- if |
| 171 | * they're in the process of exiting, we've ensured |
| 172 | * that they will immediately restart on exit, unless |
| 173 | * they are ONESHOT |
| 174 | */ |
| 175 | if (svc->flags & SVC_RUNNING) { |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0; |
| 180 | if (needs_console && (!have_console)) { |
| 181 | ERROR("service '%s' requires console\n", svc->name); |
| 182 | svc->flags |= SVC_DISABLED; |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | if (stat(svc->args[0], &s) != 0) { |
| 187 | ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name); |
| 188 | svc->flags |= SVC_DISABLED; |
| 189 | return; |
| 190 | } |
| 191 | |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 192 | if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) { |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 193 | ERROR("service '%s' must be one-shot to use dynamic args, disabling\n", |
| 194 | svc->args[0]); |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 195 | svc->flags |= SVC_DISABLED; |
| 196 | return; |
| 197 | } |
| 198 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 199 | if (is_selinux_enabled() > 0) { |
Stephen Smalley | 30f3033 | 2012-11-16 14:34:27 -0500 | [diff] [blame] | 200 | if (svc->seclabel) { |
| 201 | scon = strdup(svc->seclabel); |
| 202 | if (!scon) { |
| 203 | ERROR("Out of memory while starting '%s'\n", svc->name); |
| 204 | return; |
| 205 | } |
| 206 | } else { |
| 207 | char *mycon = NULL, *fcon = NULL; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 208 | |
Stephen Smalley | 30f3033 | 2012-11-16 14:34:27 -0500 | [diff] [blame] | 209 | INFO("computing context for service '%s'\n", svc->args[0]); |
| 210 | rc = getcon(&mycon); |
| 211 | if (rc < 0) { |
| 212 | ERROR("could not get context while starting '%s'\n", svc->name); |
| 213 | return; |
| 214 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 215 | |
Stephen Smalley | 30f3033 | 2012-11-16 14:34:27 -0500 | [diff] [blame] | 216 | rc = getfilecon(svc->args[0], &fcon); |
| 217 | if (rc < 0) { |
| 218 | ERROR("could not get context while starting '%s'\n", svc->name); |
| 219 | freecon(mycon); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon); |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 224 | freecon(mycon); |
Stephen Smalley | 30f3033 | 2012-11-16 14:34:27 -0500 | [diff] [blame] | 225 | freecon(fcon); |
| 226 | if (rc < 0) { |
| 227 | ERROR("could not get context while starting '%s'\n", svc->name); |
| 228 | return; |
| 229 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 230 | } |
| 231 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 232 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 233 | NOTICE("starting '%s'\n", svc->name); |
| 234 | |
| 235 | pid = fork(); |
| 236 | |
| 237 | if (pid == 0) { |
| 238 | struct socketinfo *si; |
| 239 | struct svcenvinfo *ei; |
| 240 | char tmp[32]; |
| 241 | int fd, sz; |
| 242 | |
Nick Kralevich | 6ebf12f | 2012-03-26 09:09:11 -0700 | [diff] [blame] | 243 | umask(077); |
Colin Cross | 3294bbb | 2010-04-19 17:11:33 -0700 | [diff] [blame] | 244 | if (properties_inited()) { |
| 245 | get_property_workspace(&fd, &sz); |
| 246 | sprintf(tmp, "%d,%d", dup(fd), sz); |
| 247 | add_environment("ANDROID_PROPERTY_WORKSPACE", tmp); |
| 248 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 249 | |
| 250 | for (ei = svc->envvars; ei; ei = ei->next) |
| 251 | add_environment(ei->name, ei->value); |
| 252 | |
| 253 | for (si = svc->sockets; si; si = si->next) { |
Mike Lockwood | 912ff85 | 2010-10-01 08:20:36 -0400 | [diff] [blame] | 254 | int socket_type = ( |
| 255 | !strcmp(si->type, "stream") ? SOCK_STREAM : |
| 256 | (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET)); |
| 257 | int s = create_socket(si->name, socket_type, |
Stephen Smalley | 8348d27 | 2013-05-13 12:37:04 -0400 | [diff] [blame] | 258 | si->perm, si->uid, si->gid, si->socketcon ?: scon); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 259 | if (s >= 0) { |
| 260 | publish_socket(si->name, s); |
| 261 | } |
| 262 | } |
| 263 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 264 | freecon(scon); |
| 265 | scon = NULL; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 266 | |
San Mehat | 4e221f0 | 2010-02-25 14:19:50 -0800 | [diff] [blame] | 267 | if (svc->ioprio_class != IoSchedClass_NONE) { |
| 268 | if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) { |
| 269 | ERROR("Failed to set pid %d ioprio = %d,%d: %s\n", |
| 270 | getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno)); |
| 271 | } |
| 272 | } |
| 273 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 274 | if (needs_console) { |
| 275 | setsid(); |
| 276 | open_console(); |
| 277 | } else { |
| 278 | zap_stdio(); |
| 279 | } |
| 280 | |
| 281 | #if 0 |
| 282 | for (n = 0; svc->args[n]; n++) { |
| 283 | INFO("args[%d] = '%s'\n", n, svc->args[n]); |
| 284 | } |
| 285 | for (n = 0; ENV[n]; n++) { |
| 286 | INFO("env[%d] = '%s'\n", n, ENV[n]); |
| 287 | } |
| 288 | #endif |
| 289 | |
| 290 | setpgid(0, getpid()); |
| 291 | |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 292 | /* as requested, set our gid, supplemental gids, and uid */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 293 | if (svc->gid) { |
Nick Kralevich | 2268718 | 2010-11-17 16:55:42 -0800 | [diff] [blame] | 294 | if (setgid(svc->gid) != 0) { |
| 295 | ERROR("setgid failed: %s\n", strerror(errno)); |
| 296 | _exit(127); |
| 297 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 298 | } |
| 299 | if (svc->nr_supp_gids) { |
Nick Kralevich | 2268718 | 2010-11-17 16:55:42 -0800 | [diff] [blame] | 300 | if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) { |
| 301 | ERROR("setgroups failed: %s\n", strerror(errno)); |
| 302 | _exit(127); |
| 303 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 304 | } |
| 305 | if (svc->uid) { |
Nick Kralevich | 2268718 | 2010-11-17 16:55:42 -0800 | [diff] [blame] | 306 | if (setuid(svc->uid) != 0) { |
| 307 | ERROR("setuid failed: %s\n", strerror(errno)); |
| 308 | _exit(127); |
| 309 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 310 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 311 | if (svc->seclabel) { |
| 312 | if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) { |
| 313 | ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno)); |
| 314 | _exit(127); |
| 315 | } |
| 316 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 317 | |
San Mehat | 8ad1568 | 2009-05-20 08:50:40 -0700 | [diff] [blame] | 318 | if (!dynamic_args) { |
| 319 | if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) { |
| 320 | ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno)); |
| 321 | } |
| 322 | } else { |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 323 | char *arg_ptrs[INIT_PARSER_MAXARGS+1]; |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 324 | int arg_idx = svc->nargs; |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 325 | char *tmp = strdup(dynamic_args); |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 326 | char *next = tmp; |
| 327 | char *bword; |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 328 | |
| 329 | /* Copy the static arguments */ |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 330 | memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *))); |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 331 | |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 332 | while((bword = strsep(&next, " "))) { |
| 333 | arg_ptrs[arg_idx++] = bword; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 334 | if (arg_idx == INIT_PARSER_MAXARGS) |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 335 | break; |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 336 | } |
| 337 | arg_ptrs[arg_idx] = '\0'; |
| 338 | execve(svc->args[0], (char**) arg_ptrs, (char**) ENV); |
Ivan Djelic | 165de92 | 2008-11-23 22:26:39 +0100 | [diff] [blame] | 339 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 340 | _exit(127); |
| 341 | } |
| 342 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 343 | freecon(scon); |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 344 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 345 | if (pid < 0) { |
| 346 | ERROR("failed to start '%s'\n", svc->name); |
| 347 | svc->pid = 0; |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | svc->time_started = gettime(); |
| 352 | svc->pid = pid; |
| 353 | svc->flags |= SVC_RUNNING; |
| 354 | |
Colin Cross | 3294bbb | 2010-04-19 17:11:33 -0700 | [diff] [blame] | 355 | if (properties_inited()) |
| 356 | notify_service_state(svc->name, "running"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 359 | /* The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART */ |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 360 | 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] | 361 | { |
Mike Kasick | 7e36edd | 2012-02-06 10:32:13 -0500 | [diff] [blame] | 362 | /* The service is still SVC_RUNNING until its process exits, but if it has |
| 363 | * already exited it shoudn't attempt a restart yet. */ |
| 364 | svc->flags &= (~SVC_RESTARTING); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 365 | |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 366 | if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) { |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 367 | /* Hrm, an illegal flag. Default to SVC_DISABLED */ |
| 368 | how = SVC_DISABLED; |
| 369 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 370 | /* if the service has not yet started, prevent |
| 371 | * it from auto-starting with its class |
| 372 | */ |
Ken Sumrall | a286480 | 2011-10-26 16:56:00 -0700 | [diff] [blame] | 373 | if (how == SVC_RESET) { |
| 374 | svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET; |
| 375 | } else { |
| 376 | svc->flags |= how; |
| 377 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 378 | |
| 379 | if (svc->pid) { |
| 380 | NOTICE("service '%s' is being killed\n", svc->name); |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 381 | kill(-svc->pid, SIGKILL); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 382 | notify_service_state(svc->name, "stopping"); |
| 383 | } else { |
| 384 | notify_service_state(svc->name, "stopped"); |
| 385 | } |
| 386 | } |
| 387 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 388 | void service_reset(struct service *svc) |
| 389 | { |
| 390 | service_stop_or_reset(svc, SVC_RESET); |
| 391 | } |
| 392 | |
| 393 | void service_stop(struct service *svc) |
| 394 | { |
| 395 | service_stop_or_reset(svc, SVC_DISABLED); |
| 396 | } |
| 397 | |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 398 | void service_restart(struct service *svc) |
| 399 | { |
| 400 | if (svc->flags & SVC_RUNNING) { |
| 401 | /* Stop, wait, then start the service. */ |
| 402 | service_stop_or_reset(svc, SVC_RESTART); |
| 403 | } else if (!(svc->flags & SVC_RESTARTING)) { |
| 404 | /* Just start the service since it's not running. */ |
| 405 | service_start(svc, NULL); |
| 406 | } /* else: Service is restarting anyways. */ |
| 407 | } |
| 408 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 409 | void property_changed(const char *name, const char *value) |
| 410 | { |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 411 | if (property_triggers_enabled) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 412 | queue_property_triggers(name, value); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 413 | } |
| 414 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 415 | static void restart_service_if_needed(struct service *svc) |
| 416 | { |
| 417 | time_t next_start_time = svc->time_started + 5; |
| 418 | |
| 419 | if (next_start_time <= gettime()) { |
| 420 | svc->flags &= (~SVC_RESTARTING); |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 421 | service_start(svc, NULL); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 422 | return; |
| 423 | } |
| 424 | |
| 425 | if ((next_start_time < process_needs_restart) || |
| 426 | (process_needs_restart == 0)) { |
| 427 | process_needs_restart = next_start_time; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | static void restart_processes() |
| 432 | { |
| 433 | process_needs_restart = 0; |
| 434 | service_for_each_flags(SVC_RESTARTING, |
| 435 | restart_service_if_needed); |
| 436 | } |
| 437 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 438 | static void msg_start(const char *name) |
| 439 | { |
Hong-Mei Li | 1146718 | 2013-04-01 11:17:51 +0800 | [diff] [blame] | 440 | struct service *svc = NULL; |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 441 | char *tmp = NULL; |
| 442 | char *args = NULL; |
| 443 | |
| 444 | if (!strchr(name, ':')) |
| 445 | svc = service_find_by_name(name); |
| 446 | else { |
| 447 | tmp = strdup(name); |
Hong-Mei Li | 1146718 | 2013-04-01 11:17:51 +0800 | [diff] [blame] | 448 | if (tmp) { |
| 449 | args = strchr(tmp, ':'); |
| 450 | *args = '\0'; |
| 451 | args++; |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 452 | |
Hong-Mei Li | 1146718 | 2013-04-01 11:17:51 +0800 | [diff] [blame] | 453 | svc = service_find_by_name(tmp); |
| 454 | } |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 455 | } |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 456 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 457 | if (svc) { |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 458 | service_start(svc, args); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 459 | } else { |
| 460 | ERROR("no such service '%s'\n", name); |
| 461 | } |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 462 | if (tmp) |
| 463 | free(tmp); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | static void msg_stop(const char *name) |
| 467 | { |
| 468 | struct service *svc = service_find_by_name(name); |
| 469 | |
| 470 | if (svc) { |
| 471 | service_stop(svc); |
| 472 | } else { |
Dima Zavin | 770354d | 2009-05-05 18:33:07 -0700 | [diff] [blame] | 473 | ERROR("no such service '%s'\n", name); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 477 | static void msg_restart(const char *name) |
| 478 | { |
| 479 | struct service *svc = service_find_by_name(name); |
| 480 | |
| 481 | if (svc) { |
| 482 | service_restart(svc); |
| 483 | } else { |
| 484 | ERROR("no such service '%s'\n", name); |
| 485 | } |
| 486 | } |
| 487 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 488 | void handle_control_message(const char *msg, const char *arg) |
| 489 | { |
| 490 | if (!strcmp(msg,"start")) { |
| 491 | msg_start(arg); |
| 492 | } else if (!strcmp(msg,"stop")) { |
| 493 | msg_stop(arg); |
Wink Saville | cfa0d84 | 2010-10-03 13:30:11 -0700 | [diff] [blame] | 494 | } else if (!strcmp(msg,"restart")) { |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 495 | msg_restart(arg); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 496 | } else { |
| 497 | ERROR("unknown control msg '%s'\n", msg); |
| 498 | } |
| 499 | } |
| 500 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 501 | static struct command *get_first_command(struct action *act) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 502 | { |
| 503 | struct listnode *node; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 504 | node = list_head(&act->commands); |
Dima Zavin | 3bea079 | 2011-08-26 13:59:18 -0700 | [diff] [blame] | 505 | if (!node || list_empty(&act->commands)) |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 506 | return NULL; |
| 507 | |
| 508 | return node_to_item(node, struct command, clist); |
| 509 | } |
| 510 | |
| 511 | static struct command *get_next_command(struct action *act, struct command *cmd) |
| 512 | { |
| 513 | struct listnode *node; |
| 514 | node = cmd->clist.next; |
| 515 | if (!node) |
| 516 | return NULL; |
| 517 | if (node == &act->commands) |
| 518 | return NULL; |
| 519 | |
| 520 | return node_to_item(node, struct command, clist); |
| 521 | } |
| 522 | |
| 523 | static int is_last_command(struct action *act, struct command *cmd) |
| 524 | { |
| 525 | return (list_tail(&act->commands) == &cmd->clist); |
| 526 | } |
| 527 | |
| 528 | void execute_one_command(void) |
| 529 | { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 530 | int ret; |
| 531 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 532 | if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) { |
| 533 | cur_action = action_remove_queue_head(); |
Colin Cross | ebd4613 | 2010-04-22 11:52:23 -0700 | [diff] [blame] | 534 | cur_command = NULL; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 535 | if (!cur_action) |
| 536 | return; |
| 537 | INFO("processing action %p (%s)\n", cur_action, cur_action->name); |
| 538 | cur_command = get_first_command(cur_action); |
| 539 | } else { |
| 540 | cur_command = get_next_command(cur_action, cur_command); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 541 | } |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 542 | |
| 543 | if (!cur_command) |
| 544 | return; |
| 545 | |
| 546 | ret = cur_command->func(cur_command->nargs, cur_command->args); |
| 547 | 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] | 548 | } |
| 549 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 550 | 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] | 551 | { |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 552 | int ret; |
| 553 | INFO("wait for %s\n", coldboot_done); |
| 554 | ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT); |
| 555 | if (ret) |
| 556 | ERROR("Timed out waiting for %s\n", coldboot_done); |
| 557 | return ret; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 560 | /* |
| 561 | * Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed |
| 562 | * by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom. |
| 563 | * Does nothing if Hardware RNG is not present. |
| 564 | * |
| 565 | * Since we don't yet trust the quality of Hardware RNG, these bytes are not |
| 566 | * mixed into the primary pool of Linux RNG and the entropy estimate is left |
| 567 | * unmodified. |
| 568 | * |
| 569 | * If the HW RNG device /dev/hw_random is present, we require that at least |
| 570 | * 512 bytes read from it are written into Linux RNG. QA is expected to catch |
| 571 | * devices/configurations where these I/O operations are blocking for a long |
| 572 | * time. We do not reboot or halt on failures, as this is a best-effort |
| 573 | * attempt. |
| 574 | */ |
| 575 | static int mix_hwrng_into_linux_rng_action(int nargs, char **args) |
| 576 | { |
| 577 | int result = -1; |
| 578 | int hwrandom_fd = -1; |
| 579 | int urandom_fd = -1; |
| 580 | char buf[512]; |
| 581 | ssize_t chunk_size; |
| 582 | size_t total_bytes_written = 0; |
| 583 | |
| 584 | hwrandom_fd = TEMP_FAILURE_RETRY( |
| 585 | open("/dev/hw_random", O_RDONLY | O_NOFOLLOW)); |
| 586 | if (hwrandom_fd == -1) { |
| 587 | if (errno == ENOENT) { |
| 588 | ERROR("/dev/hw_random not found\n"); |
| 589 | /* It's not an error to not have a Hardware RNG. */ |
| 590 | result = 0; |
| 591 | } else { |
| 592 | ERROR("Failed to open /dev/hw_random: %s\n", strerror(errno)); |
| 593 | } |
| 594 | goto ret; |
| 595 | } |
| 596 | |
| 597 | urandom_fd = TEMP_FAILURE_RETRY( |
| 598 | open("/dev/urandom", O_WRONLY | O_NOFOLLOW)); |
| 599 | if (urandom_fd == -1) { |
| 600 | ERROR("Failed to open /dev/urandom: %s\n", strerror(errno)); |
| 601 | goto ret; |
| 602 | } |
| 603 | |
| 604 | while (total_bytes_written < sizeof(buf)) { |
| 605 | chunk_size = TEMP_FAILURE_RETRY( |
| 606 | read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written)); |
| 607 | if (chunk_size == -1) { |
| 608 | ERROR("Failed to read from /dev/hw_random: %s\n", strerror(errno)); |
| 609 | goto ret; |
| 610 | } else if (chunk_size == 0) { |
| 611 | ERROR("Failed to read from /dev/hw_random: EOF\n"); |
| 612 | goto ret; |
| 613 | } |
| 614 | |
| 615 | chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size)); |
| 616 | if (chunk_size == -1) { |
| 617 | ERROR("Failed to write to /dev/urandom: %s\n", strerror(errno)); |
| 618 | goto ret; |
| 619 | } |
| 620 | total_bytes_written += chunk_size; |
| 621 | } |
| 622 | |
| 623 | INFO("Mixed %d bytes from /dev/hw_random into /dev/urandom", |
| 624 | total_bytes_written); |
| 625 | result = 0; |
| 626 | |
| 627 | ret: |
| 628 | if (hwrandom_fd != -1) { |
| 629 | close(hwrandom_fd); |
| 630 | } |
| 631 | if (urandom_fd != -1) { |
| 632 | close(urandom_fd); |
| 633 | } |
| 634 | memset(buf, 0, sizeof(buf)); |
| 635 | return result; |
| 636 | } |
| 637 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 638 | static int keychord_init_action(int nargs, char **args) |
| 639 | { |
| 640 | keychord_init(); |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | static int console_init_action(int nargs, char **args) |
| 645 | { |
| 646 | int fd; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 647 | |
| 648 | if (console[0]) { |
Hong-Mei Li | 1146718 | 2013-04-01 11:17:51 +0800 | [diff] [blame] | 649 | snprintf(console_name, sizeof(console_name), "/dev/%s", console); |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | fd = open(console_name, O_RDWR); |
| 653 | if (fd >= 0) |
| 654 | have_console = 1; |
| 655 | close(fd); |
| 656 | |
| 657 | if( load_565rle_image(INIT_IMAGE_FILE) ) { |
| 658 | fd = open("/dev/tty0", O_WRONLY); |
| 659 | if (fd >= 0) { |
| 660 | const char *msg; |
| 661 | msg = "\n" |
| 662 | "\n" |
| 663 | "\n" |
| 664 | "\n" |
| 665 | "\n" |
| 666 | "\n" |
| 667 | "\n" // console is 40 cols x 30 lines |
| 668 | "\n" |
| 669 | "\n" |
| 670 | "\n" |
| 671 | "\n" |
| 672 | "\n" |
| 673 | "\n" |
| 674 | "\n" |
| 675 | " A N D R O I D "; |
| 676 | write(fd, msg, strlen(msg)); |
| 677 | close(fd); |
| 678 | } |
| 679 | } |
| 680 | return 0; |
| 681 | } |
| 682 | |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 683 | static void import_kernel_nv(char *name, int for_emulator) |
| 684 | { |
| 685 | char *value = strchr(name, '='); |
| 686 | int name_len = strlen(name); |
| 687 | |
| 688 | if (value == 0) return; |
| 689 | *value++ = 0; |
| 690 | if (name_len == 0) return; |
| 691 | |
| 692 | if (for_emulator) { |
| 693 | /* in the emulator, export any kernel option with the |
| 694 | * ro.kernel. prefix */ |
| 695 | char buff[PROP_NAME_MAX]; |
| 696 | int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name ); |
| 697 | |
| 698 | if (len < (int)sizeof(buff)) |
| 699 | property_set( buff, value ); |
| 700 | return; |
| 701 | } |
| 702 | |
| 703 | if (!strcmp(name,"qemu")) { |
| 704 | strlcpy(qemu, value, sizeof(qemu)); |
| 705 | } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) { |
| 706 | const char *boot_prop_name = name + 12; |
| 707 | char prop[PROP_NAME_MAX]; |
| 708 | int cnt; |
| 709 | |
| 710 | cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name); |
| 711 | if (cnt < PROP_NAME_MAX) |
| 712 | property_set(prop, value); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | static void export_kernel_boot_props(void) |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 717 | { |
| 718 | char tmp[PROP_VALUE_MAX]; |
Colin Cross | 1a6f4c3 | 2013-01-28 17:13:35 -0800 | [diff] [blame] | 719 | int ret; |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 720 | unsigned i; |
| 721 | struct { |
| 722 | const char *src_prop; |
| 723 | const char *dest_prop; |
| 724 | const char *def_val; |
| 725 | } prop_map[] = { |
| 726 | { "ro.boot.serialno", "ro.serialno", "", }, |
| 727 | { "ro.boot.mode", "ro.bootmode", "unknown", }, |
| 728 | { "ro.boot.baseband", "ro.baseband", "unknown", }, |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 729 | { "ro.boot.bootloader", "ro.bootloader", "unknown", }, |
| 730 | }; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 731 | |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 732 | for (i = 0; i < ARRAY_SIZE(prop_map); i++) { |
Colin Cross | 1a6f4c3 | 2013-01-28 17:13:35 -0800 | [diff] [blame] | 733 | ret = property_get(prop_map[i].src_prop, tmp); |
Colin Cross | 5e484e9 | 2013-06-17 16:20:08 -0700 | [diff] [blame] | 734 | if (ret > 0) |
| 735 | property_set(prop_map[i].dest_prop, tmp); |
| 736 | else |
Colin Cross | 1a6f4c3 | 2013-01-28 17:13:35 -0800 | [diff] [blame] | 737 | property_set(prop_map[i].dest_prop, prop_map[i].def_val); |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 738 | } |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 739 | |
Colin Cross | 1a6f4c3 | 2013-01-28 17:13:35 -0800 | [diff] [blame] | 740 | ret = property_get("ro.boot.console", tmp); |
| 741 | if (ret) |
| 742 | strlcpy(console, tmp, sizeof(console)); |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 743 | |
| 744 | /* save a copy for init's usage during boot */ |
Colin Cross | 1a6f4c3 | 2013-01-28 17:13:35 -0800 | [diff] [blame] | 745 | property_get("ro.bootmode", tmp); |
| 746 | strlcpy(bootmode, tmp, sizeof(bootmode)); |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 747 | |
| 748 | /* if this was given on kernel command line, override what we read |
| 749 | * before (e.g. from /proc/cpuinfo), if anything */ |
Colin Cross | 1a6f4c3 | 2013-01-28 17:13:35 -0800 | [diff] [blame] | 750 | ret = property_get("ro.boot.hardware", tmp); |
| 751 | if (ret) |
| 752 | strlcpy(hardware, tmp, sizeof(hardware)); |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 753 | property_set("ro.hardware", hardware); |
| 754 | |
| 755 | snprintf(tmp, PROP_VALUE_MAX, "%d", revision); |
| 756 | property_set("ro.revision", tmp); |
| 757 | |
| 758 | /* TODO: these are obsolete. We should delete them */ |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 759 | if (!strcmp(bootmode,"factory")) |
| 760 | property_set("ro.factorytest", "1"); |
| 761 | else if (!strcmp(bootmode,"factory2")) |
| 762 | property_set("ro.factorytest", "2"); |
| 763 | else |
| 764 | property_set("ro.factorytest", "0"); |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 765 | } |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 766 | |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 767 | static void process_kernel_cmdline(void) |
| 768 | { |
| 769 | /* don't expose the raw commandline to nonpriv processes */ |
| 770 | chmod("/proc/cmdline", 0440); |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 771 | |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 772 | /* first pass does the common stuff, and finds if we are in qemu. |
| 773 | * second pass is only necessary for qemu to export all kernel params |
| 774 | * as props. |
| 775 | */ |
| 776 | import_kernel_cmdline(0, import_kernel_nv); |
| 777 | if (qemu[0]) |
| 778 | import_kernel_cmdline(1, import_kernel_nv); |
| 779 | |
| 780 | /* now propogate the info given on command line to internal variables |
| 781 | * used by init as well as the current required properties |
| 782 | */ |
| 783 | export_kernel_boot_props(); |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | static int property_service_init_action(int nargs, char **args) |
| 787 | { |
| 788 | /* read any property files on system or data and |
| 789 | * fire up the property service. This must happen |
| 790 | * after the ro.foo properties are set above so |
| 791 | * that /data/local.prop cannot interfere with them. |
| 792 | */ |
| 793 | start_property_service(); |
| 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | static int signal_init_action(int nargs, char **args) |
| 798 | { |
| 799 | signal_init(); |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | static int check_startup_action(int nargs, char **args) |
| 804 | { |
| 805 | /* make sure we actually have all the pieces we need */ |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 806 | if ((get_property_set_fd() < 0) || |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 807 | (get_signal_fd() < 0)) { |
| 808 | ERROR("init startup failure\n"); |
| 809 | exit(1); |
| 810 | } |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 811 | |
| 812 | /* signal that we hit this point */ |
| 813 | unlink("/dev/.booting"); |
| 814 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | static int queue_property_triggers_action(int nargs, char **args) |
| 819 | { |
| 820 | queue_all_property_triggers(); |
| 821 | /* enable property triggers */ |
| 822 | property_triggers_enabled = 1; |
| 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | #if BOOTCHART |
| 827 | static int bootchart_init_action(int nargs, char **args) |
| 828 | { |
| 829 | bootchart_count = bootchart_init(); |
| 830 | if (bootchart_count < 0) { |
| 831 | ERROR("bootcharting init failure\n"); |
| 832 | } else if (bootchart_count > 0) { |
| 833 | NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS); |
| 834 | } else { |
| 835 | NOTICE("bootcharting ignored\n"); |
| 836 | } |
Carl-Emil Lagerstedt | 9ab8190 | 2011-01-14 09:35:30 +0100 | [diff] [blame] | 837 | |
| 838 | return 0; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 839 | } |
| 840 | #endif |
| 841 | |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 842 | static const struct selinux_opt seopts_prop[] = { |
repo sync | 49be240 | 2013-05-17 12:48:34 -0700 | [diff] [blame] | 843 | { SELABEL_OPT_PATH, "/data/security/property_contexts" }, |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 844 | { SELABEL_OPT_PATH, "/property_contexts" }, |
| 845 | { 0, NULL } |
| 846 | }; |
| 847 | |
| 848 | struct selabel_handle* selinux_android_prop_context_handle(void) |
| 849 | { |
| 850 | int i = 0; |
| 851 | struct selabel_handle* sehandle = NULL; |
| 852 | while ((sehandle == NULL) && seopts_prop[i].value) { |
| 853 | sehandle = selabel_open(SELABEL_CTX_ANDROID_PROP, &seopts_prop[i], 1); |
| 854 | i++; |
| 855 | } |
| 856 | |
| 857 | if (!sehandle) { |
| 858 | ERROR("SELinux: Could not load property_contexts: %s\n", |
| 859 | strerror(errno)); |
| 860 | return NULL; |
| 861 | } |
| 862 | INFO("SELinux: Loaded property contexts from %s\n", seopts_prop[i - 1].value); |
| 863 | return sehandle; |
| 864 | } |
| 865 | |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 866 | void selinux_init_all_handles(void) |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 867 | { |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 868 | sehandle = selinux_android_file_context_handle(); |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 869 | sehandle_prop = selinux_android_prop_context_handle(); |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 870 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 871 | |
Nick Kralevich | 4838aa1 | 2013-06-26 15:37:26 -0700 | [diff] [blame] | 872 | static bool selinux_is_disabled(void) |
| 873 | { |
| 874 | char tmp[PROP_VALUE_MAX]; |
| 875 | |
| 876 | if (access("/sys/fs/selinux", F_OK) != 0) { |
| 877 | /* SELinux is not compiled into the kernel, or has been disabled |
| 878 | * via the kernel command line "selinux=0". |
| 879 | */ |
| 880 | return true; |
| 881 | } |
| 882 | |
| 883 | if ((property_get("ro.boot.selinux", tmp) != 0) && (strcmp(tmp, "disabled") == 0)) { |
| 884 | /* SELinux is compiled into the kernel, but we've been told to disable it. */ |
| 885 | return true; |
| 886 | } |
| 887 | |
| 888 | return false; |
| 889 | } |
| 890 | |
| 891 | static bool selinux_is_enforcing(void) |
| 892 | { |
| 893 | char tmp[PROP_VALUE_MAX]; |
| 894 | |
| 895 | if (property_get("ro.boot.selinux", tmp) == 0) { |
| 896 | /* Property is not set. Assume enforcing */ |
| 897 | return true; |
| 898 | } |
| 899 | |
| 900 | if (strcmp(tmp, "permissive") == 0) { |
| 901 | /* SELinux is in the kernel, but we've been told to go into permissive mode */ |
| 902 | return false; |
| 903 | } |
| 904 | |
| 905 | if (strcmp(tmp, "enforcing") != 0) { |
| 906 | ERROR("SELinux: Unknown value of ro.boot.selinux. Got: \"%s\". Assuming enforcing.\n", tmp); |
| 907 | } |
| 908 | |
| 909 | return true; |
| 910 | } |
| 911 | |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 912 | int selinux_reload_policy(void) |
| 913 | { |
Nick Kralevich | 4838aa1 | 2013-06-26 15:37:26 -0700 | [diff] [blame] | 914 | if (selinux_is_disabled()) { |
| 915 | return -1; |
| 916 | } |
| 917 | |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 918 | INFO("SELinux: Attempting to reload policy files\n"); |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 919 | |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 920 | if (selinux_android_reload_policy() == -1) { |
| 921 | return -1; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 922 | } |
| 923 | |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 924 | if (sehandle) |
| 925 | selabel_close(sehandle); |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 926 | |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 927 | if (sehandle_prop) |
| 928 | selabel_close(sehandle_prop); |
| 929 | |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 930 | selinux_init_all_handles(); |
| 931 | return 0; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 932 | } |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 933 | |
| 934 | int audit_callback(void *data, security_class_t cls, char *buf, size_t len) |
| 935 | { |
| 936 | snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data); |
| 937 | return 0; |
| 938 | } |
| 939 | |
Nick Kralevich | b710ed2 | 2013-06-24 17:41:40 -0700 | [diff] [blame] | 940 | static void selinux_initialize(void) |
| 941 | { |
Nick Kralevich | 4838aa1 | 2013-06-26 15:37:26 -0700 | [diff] [blame] | 942 | if (selinux_is_disabled()) { |
Nick Kralevich | b710ed2 | 2013-06-24 17:41:40 -0700 | [diff] [blame] | 943 | return; |
| 944 | } |
| 945 | |
| 946 | INFO("loading selinux policy\n"); |
| 947 | if (selinux_android_load_policy() < 0) { |
| 948 | ERROR("SELinux: Failed to load policy; rebooting into recovery mode\n"); |
| 949 | android_reboot(ANDROID_RB_RESTART2, 0, "recovery"); |
| 950 | while (1) { pause(); } // never reached |
| 951 | } |
| 952 | |
| 953 | selinux_init_all_handles(); |
Nick Kralevich | 4838aa1 | 2013-06-26 15:37:26 -0700 | [diff] [blame] | 954 | bool is_enforcing = selinux_is_enforcing(); |
| 955 | INFO("SELinux: security_setenforce(%d)\n", is_enforcing); |
| 956 | security_setenforce(is_enforcing); |
Nick Kralevich | b710ed2 | 2013-06-24 17:41:40 -0700 | [diff] [blame] | 957 | } |
| 958 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 959 | int main(int argc, char **argv) |
| 960 | { |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 961 | int fd_count = 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 962 | struct pollfd ufds[4]; |
| 963 | char *tmpdev; |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 964 | char* debuggable; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 965 | char tmp[32]; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 966 | int property_set_fd_init = 0; |
| 967 | int signal_fd_init = 0; |
| 968 | int keychord_fd_init = 0; |
Dima Zavin | d7634c9 | 2011-12-16 14:18:06 -0800 | [diff] [blame] | 969 | bool is_charger = false; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 970 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 971 | if (!strcmp(basename(argv[0]), "ueventd")) |
| 972 | return ueventd_main(argc, argv); |
| 973 | |
Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 974 | if (!strcmp(basename(argv[0]), "watchdogd")) |
| 975 | return watchdogd_main(argc, argv); |
| 976 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 977 | /* clear the umask */ |
| 978 | umask(0); |
| 979 | |
| 980 | /* Get the basic filesystem setup we need put |
| 981 | * together in the initramdisk on / and then we'll |
| 982 | * let the rc file figure out the rest. |
| 983 | */ |
| 984 | mkdir("/dev", 0755); |
| 985 | mkdir("/proc", 0755); |
| 986 | mkdir("/sys", 0755); |
| 987 | |
Nick Kralevich | 150f19e | 2010-06-22 16:35:43 -0700 | [diff] [blame] | 988 | mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 989 | mkdir("/dev/pts", 0755); |
| 990 | mkdir("/dev/socket", 0755); |
| 991 | mount("devpts", "/dev/pts", "devpts", 0, NULL); |
| 992 | mount("proc", "/proc", "proc", 0, NULL); |
| 993 | mount("sysfs", "/sys", "sysfs", 0, NULL); |
| 994 | |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 995 | /* indicate that booting is in progress to background fw loaders, etc */ |
| 996 | close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000)); |
| 997 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 998 | /* We must have some place other than / to create the |
| 999 | * device nodes for kmsg and null, otherwise we won't |
| 1000 | * be able to remount / read-only later on. |
| 1001 | * Now that tmpfs is mounted on /dev, we can actually |
| 1002 | * talk to the outside world. |
| 1003 | */ |
| 1004 | open_devnull_stdio(); |
Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 1005 | klog_init(); |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 1006 | property_init(); |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 1007 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 1008 | get_hardware_name(hardware, &revision); |
Dima Zavin | d7634c9 | 2011-12-16 14:18:06 -0800 | [diff] [blame] | 1009 | |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 1010 | process_kernel_cmdline(); |
| 1011 | |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 1012 | union selinux_callback cb; |
| 1013 | cb.func_log = klog_write; |
| 1014 | selinux_set_callback(SELINUX_CB_LOG, cb); |
| 1015 | |
| 1016 | cb.func_audit = audit_callback; |
| 1017 | selinux_set_callback(SELINUX_CB_AUDIT, cb); |
| 1018 | |
Nick Kralevich | b710ed2 | 2013-06-24 17:41:40 -0700 | [diff] [blame] | 1019 | selinux_initialize(); |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 1020 | /* These directories were necessarily created before initial policy load |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 1021 | * and therefore need their security context restored to the proper value. |
| 1022 | * This must happen before /dev is populated by ueventd. |
| 1023 | */ |
| 1024 | restorecon("/dev"); |
| 1025 | restorecon("/dev/socket"); |
Geremy Condra | 8e15eab | 2013-02-28 17:29:58 -0800 | [diff] [blame] | 1026 | restorecon("/dev/__properties__"); |
Nick Kralevich | f29c533 | 2013-07-11 15:38:26 -0700 | [diff] [blame] | 1027 | restorecon_recursive("/sys"); |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 1028 | |
Dima Zavin | d7634c9 | 2011-12-16 14:18:06 -0800 | [diff] [blame] | 1029 | is_charger = !strcmp(bootmode, "charger"); |
| 1030 | |
| 1031 | INFO("property init\n"); |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 1032 | if (!is_charger) |
| 1033 | property_load_boot_defaults(); |
Dima Zavin | d7634c9 | 2011-12-16 14:18:06 -0800 | [diff] [blame] | 1034 | |
| 1035 | INFO("reading config file\n"); |
| 1036 | init_parse_config_file("/init.rc"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1037 | |
| 1038 | 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] | 1039 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 1040 | queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done"); |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 1041 | queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng"); |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 1042 | queue_builtin_action(keychord_init_action, "keychord_init"); |
| 1043 | queue_builtin_action(console_init_action, "console_init"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1044 | |
Dima Zavin | ca47cef | 2011-08-24 15:28:23 -0700 | [diff] [blame] | 1045 | /* execute all the boot actions to get us started */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1046 | action_for_each_trigger("init", action_add_queue_tail); |
Dima Zavin | ca47cef | 2011-08-24 15:28:23 -0700 | [diff] [blame] | 1047 | |
| 1048 | /* skip mounting filesystems in charger mode */ |
Dima Zavin | d7634c9 | 2011-12-16 14:18:06 -0800 | [diff] [blame] | 1049 | if (!is_charger) { |
Dima Zavin | ca47cef | 2011-08-24 15:28:23 -0700 | [diff] [blame] | 1050 | action_for_each_trigger("early-fs", action_add_queue_tail); |
| 1051 | action_for_each_trigger("fs", action_add_queue_tail); |
| 1052 | action_for_each_trigger("post-fs", action_add_queue_tail); |
| 1053 | action_for_each_trigger("post-fs-data", action_add_queue_tail); |
| 1054 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1055 | |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 1056 | /* Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random |
| 1057 | * wasn't ready immediately after wait_for_coldboot_done |
| 1058 | */ |
| 1059 | queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng"); |
| 1060 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 1061 | queue_builtin_action(property_service_init_action, "property_service_init"); |
| 1062 | queue_builtin_action(signal_init_action, "signal_init"); |
| 1063 | queue_builtin_action(check_startup_action, "check_startup"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1064 | |
Dima Zavin | d7634c9 | 2011-12-16 14:18:06 -0800 | [diff] [blame] | 1065 | if (is_charger) { |
Dima Zavin | ca47cef | 2011-08-24 15:28:23 -0700 | [diff] [blame] | 1066 | action_for_each_trigger("charger", action_add_queue_tail); |
| 1067 | } else { |
| 1068 | action_for_each_trigger("early-boot", action_add_queue_tail); |
| 1069 | action_for_each_trigger("boot", action_add_queue_tail); |
| 1070 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1071 | |
| 1072 | /* run all property triggers based on current state of the properties */ |
Chris Dearman | 469b7b2 | 2012-03-01 15:29:20 -0800 | [diff] [blame] | 1073 | queue_builtin_action(queue_property_triggers_action, "queue_property_triggers"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1074 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1075 | |
| 1076 | #if BOOTCHART |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 1077 | queue_builtin_action(bootchart_init_action, "bootchart_init"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1078 | #endif |
| 1079 | |
| 1080 | for(;;) { |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 1081 | int nr, i, timeout = -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1082 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 1083 | execute_one_command(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1084 | restart_processes(); |
| 1085 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 1086 | if (!property_set_fd_init && get_property_set_fd() > 0) { |
| 1087 | ufds[fd_count].fd = get_property_set_fd(); |
| 1088 | ufds[fd_count].events = POLLIN; |
| 1089 | ufds[fd_count].revents = 0; |
| 1090 | fd_count++; |
| 1091 | property_set_fd_init = 1; |
| 1092 | } |
| 1093 | if (!signal_fd_init && get_signal_fd() > 0) { |
| 1094 | ufds[fd_count].fd = get_signal_fd(); |
| 1095 | ufds[fd_count].events = POLLIN; |
| 1096 | ufds[fd_count].revents = 0; |
| 1097 | fd_count++; |
| 1098 | signal_fd_init = 1; |
| 1099 | } |
| 1100 | if (!keychord_fd_init && get_keychord_fd() > 0) { |
| 1101 | ufds[fd_count].fd = get_keychord_fd(); |
| 1102 | ufds[fd_count].events = POLLIN; |
| 1103 | ufds[fd_count].revents = 0; |
| 1104 | fd_count++; |
| 1105 | keychord_fd_init = 1; |
| 1106 | } |
| 1107 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1108 | if (process_needs_restart) { |
| 1109 | timeout = (process_needs_restart - gettime()) * 1000; |
| 1110 | if (timeout < 0) |
| 1111 | timeout = 0; |
| 1112 | } |
| 1113 | |
Colin Cross | ebd4613 | 2010-04-22 11:52:23 -0700 | [diff] [blame] | 1114 | if (!action_queue_empty() || cur_action) |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 1115 | timeout = 0; |
| 1116 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1117 | #if BOOTCHART |
| 1118 | if (bootchart_count > 0) { |
| 1119 | if (timeout < 0 || timeout > BOOTCHART_POLLING_MS) |
| 1120 | timeout = BOOTCHART_POLLING_MS; |
| 1121 | if (bootchart_step() < 0 || --bootchart_count == 0) { |
| 1122 | bootchart_finish(); |
| 1123 | bootchart_count = 0; |
| 1124 | } |
| 1125 | } |
| 1126 | #endif |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 1127 | |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 1128 | nr = poll(ufds, fd_count, timeout); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1129 | if (nr <= 0) |
| 1130 | continue; |
| 1131 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 1132 | for (i = 0; i < fd_count; i++) { |
| 1133 | if (ufds[i].revents == POLLIN) { |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 1134 | if (ufds[i].fd == get_property_set_fd()) |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 1135 | handle_property_set_fd(); |
| 1136 | else if (ufds[i].fd == get_keychord_fd()) |
| 1137 | handle_keychord(); |
| 1138 | else if (ufds[i].fd == get_signal_fd()) |
| 1139 | handle_signal(); |
| 1140 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1141 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | return 0; |
| 1145 | } |