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