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