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 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 17 | #include <ctype.h> |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 18 | #include <dirent.h> |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <libgen.h> |
Elliott Hughes | cc86fb2 | 2015-03-17 20:01:13 -0700 | [diff] [blame] | 22 | #include <paths.h> |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 23 | #include <signal.h> |
| 24 | #include <stdarg.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 28 | #include <sys/epoll.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | #include <sys/mount.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 30 | #include <sys/socket.h> |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 31 | #include <sys/stat.h> |
| 32 | #include <sys/types.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | #include <sys/un.h> |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 34 | #include <sys/wait.h> |
| 35 | #include <termios.h> |
| 36 | #include <unistd.h> |
| 37 | |
| 38 | #include <mtd/mtd-user.h> |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 39 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 40 | #include <selinux/selinux.h> |
| 41 | #include <selinux/label.h> |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 42 | #include <selinux/android.h> |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 43 | |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 44 | #include <base/file.h> |
| 45 | #include <base/stringprintf.h> |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 46 | #include <base/strings.h> |
Nick Kralevich | 56fa0ac | 2013-06-24 17:41:40 -0700 | [diff] [blame] | 47 | #include <cutils/android_reboot.h> |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 48 | #include <cutils/fs.h> |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 49 | #include <cutils/iosched_policy.h> |
| 50 | #include <cutils/list.h> |
| 51 | #include <cutils/sockets.h> |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 52 | #include <private/android_filesystem_config.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 53 | |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 54 | #include <memory> |
| 55 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 56 | #include "action.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 57 | #include "devices.h" |
| 58 | #include "init.h" |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 59 | #include "log.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 60 | #include "property_service.h" |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 61 | #include "bootchart.h" |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 62 | #include "signal_handler.h" |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 63 | #include "keychords.h" |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 64 | #include "init_parser.h" |
Colin Cross | 3899e9f | 2010-04-13 20:35:46 -0700 | [diff] [blame] | 65 | #include "util.h" |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 66 | #include "ueventd.h" |
Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 67 | #include "watchdogd.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 68 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 69 | struct selabel_handle *sehandle; |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 70 | struct selabel_handle *sehandle_prop; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 71 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 72 | static int property_triggers_enabled = 0; |
| 73 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 74 | static char qemu[32]; |
| 75 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 76 | static int have_console; |
Yabin Cui | 74edcea | 2015-07-24 10:11:05 -0700 | [diff] [blame] | 77 | static std::string console_name = "/dev/console"; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 78 | static time_t process_needs_restart; |
| 79 | |
| 80 | static const char *ENV[32]; |
| 81 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 82 | bool waiting_for_exec = false; |
| 83 | |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 84 | static int epoll_fd = -1; |
| 85 | |
| 86 | void register_epoll_handler(int fd, void (*fn)()) { |
| 87 | epoll_event ev; |
| 88 | ev.events = EPOLLIN; |
| 89 | ev.data.ptr = reinterpret_cast<void*>(fn); |
| 90 | if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) { |
| 91 | ERROR("epoll_ctl failed: %s\n", strerror(errno)); |
| 92 | } |
| 93 | } |
| 94 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 95 | void service::NotifyStateChange(const char* new_state) { |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 96 | if (!properties_initialized()) { |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 97 | // If properties aren't available yet, we can't set them. |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | if ((flags & SVC_EXEC) != 0) { |
| 102 | // 'exec' commands don't have properties tracking their state. |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | char prop_name[PROP_NAME_MAX]; |
| 107 | if (snprintf(prop_name, sizeof(prop_name), "init.svc.%s", name) >= PROP_NAME_MAX) { |
| 108 | // If the property name would be too long, we can't set it. |
| 109 | ERROR("Property name \"init.svc.%s\" too long; not setting to %s\n", name, new_state); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | property_set(prop_name, new_state); |
| 114 | } |
| 115 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 116 | /* add_environment - add "key=value" to the current environment */ |
| 117 | int add_environment(const char *key, const char *val) |
| 118 | { |
James Morrissey | 381341f | 2014-05-16 11:36:36 +0100 | [diff] [blame] | 119 | size_t n; |
| 120 | size_t key_len = strlen(key); |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 121 | |
James Morrissey | 381341f | 2014-05-16 11:36:36 +0100 | [diff] [blame] | 122 | /* The last environment entry is reserved to terminate the list */ |
| 123 | for (n = 0; n < (ARRAY_SIZE(ENV) - 1); n++) { |
| 124 | |
| 125 | /* Delete any existing entry for this key */ |
| 126 | if (ENV[n] != NULL) { |
| 127 | size_t entry_key_len = strcspn(ENV[n], "="); |
| 128 | if ((entry_key_len == key_len) && (strncmp(ENV[n], key, entry_key_len) == 0)) { |
| 129 | free((char*)ENV[n]); |
| 130 | ENV[n] = NULL; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /* Add entry if a free slot is available */ |
| 135 | if (ENV[n] == NULL) { |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 136 | char* entry; |
| 137 | asprintf(&entry, "%s=%s", key, val); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 138 | ENV[n] = entry; |
| 139 | return 0; |
| 140 | } |
| 141 | } |
| 142 | |
James Morrissey | 381341f | 2014-05-16 11:36:36 +0100 | [diff] [blame] | 143 | ERROR("No env. room to store: '%s':'%s'\n", key, val); |
| 144 | |
| 145 | return -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 146 | } |
| 147 | |
San Mehat | 429721c | 2014-09-23 07:48:47 -0700 | [diff] [blame] | 148 | void zap_stdio(void) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 149 | { |
| 150 | int fd; |
| 151 | fd = open("/dev/null", O_RDWR); |
| 152 | dup2(fd, 0); |
| 153 | dup2(fd, 1); |
| 154 | dup2(fd, 2); |
| 155 | close(fd); |
| 156 | } |
| 157 | |
| 158 | static void open_console() |
| 159 | { |
| 160 | int fd; |
Yabin Cui | 74edcea | 2015-07-24 10:11:05 -0700 | [diff] [blame] | 161 | if ((fd = open(console_name.c_str(), O_RDWR)) < 0) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 162 | fd = open("/dev/null", O_RDWR); |
| 163 | } |
Colin Cross | 50fb5a6 | 2012-03-18 15:38:19 -0700 | [diff] [blame] | 164 | ioctl(fd, TIOCSCTTY, 0); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 165 | dup2(fd, 0); |
| 166 | dup2(fd, 1); |
| 167 | dup2(fd, 2); |
| 168 | close(fd); |
| 169 | } |
| 170 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 171 | static void publish_socket(const char *name, int fd) |
| 172 | { |
| 173 | char key[64] = ANDROID_SOCKET_ENV_PREFIX; |
| 174 | char val[64]; |
| 175 | |
| 176 | strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, |
| 177 | name, |
| 178 | sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); |
| 179 | snprintf(val, sizeof(val), "%d", fd); |
| 180 | add_environment(key, val); |
| 181 | |
| 182 | /* make sure we don't close-on-exec */ |
| 183 | fcntl(fd, F_SETFD, 0); |
| 184 | } |
| 185 | |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 186 | 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] | 187 | { |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 188 | // Starting a service removes it from the disabled or reset state and |
| 189 | // immediately takes it out of the restarting state if it was in there. |
JP Abgrall | 3beec7e | 2014-05-02 21:14:29 -0700 | [diff] [blame] | 190 | 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] | 191 | svc->time_started = 0; |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 192 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 193 | // Running processes require no additional work --- if they're in the |
| 194 | // process of exiting, we've ensured that they will immediately restart |
| 195 | // on exit, unless they are ONESHOT. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 196 | if (svc->flags & SVC_RUNNING) { |
| 197 | return; |
| 198 | } |
| 199 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 200 | bool needs_console = (svc->flags & SVC_CONSOLE); |
| 201 | if (needs_console && !have_console) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 202 | ERROR("service '%s' requires console\n", svc->name); |
| 203 | svc->flags |= SVC_DISABLED; |
| 204 | return; |
| 205 | } |
| 206 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 207 | struct stat sb; |
| 208 | if (stat(svc->args[0], &sb) == -1) { |
| 209 | ERROR("cannot find '%s' (%s), disabling '%s'\n", svc->args[0], strerror(errno), svc->name); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 210 | svc->flags |= SVC_DISABLED; |
| 211 | return; |
| 212 | } |
| 213 | |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 214 | if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) { |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 215 | ERROR("service '%s' must be one-shot to use dynamic args, disabling\n", svc->args[0]); |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 216 | svc->flags |= SVC_DISABLED; |
| 217 | return; |
| 218 | } |
| 219 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 220 | char* scon = NULL; |
Nick Kralevich | 4d87095 | 2015-06-12 22:03:50 -0700 | [diff] [blame] | 221 | if (svc->seclabel) { |
| 222 | scon = strdup(svc->seclabel); |
| 223 | if (!scon) { |
| 224 | ERROR("Out of memory while starting '%s'\n", svc->name); |
| 225 | return; |
| 226 | } |
| 227 | } else { |
| 228 | char *mycon = NULL, *fcon = NULL; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 229 | |
Nick Kralevich | 4d87095 | 2015-06-12 22:03:50 -0700 | [diff] [blame] | 230 | INFO("computing context for service '%s'\n", svc->args[0]); |
| 231 | int rc = getcon(&mycon); |
| 232 | if (rc < 0) { |
| 233 | ERROR("could not get context while starting '%s'\n", svc->name); |
| 234 | return; |
| 235 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 236 | |
Nick Kralevich | 4d87095 | 2015-06-12 22:03:50 -0700 | [diff] [blame] | 237 | rc = getfilecon(svc->args[0], &fcon); |
| 238 | if (rc < 0) { |
| 239 | ERROR("could not get context while starting '%s'\n", svc->name); |
Nick Kralevich | 4800dbf | 2015-07-16 10:49:51 -0700 | [diff] [blame] | 240 | free(mycon); |
Nick Kralevich | 4d87095 | 2015-06-12 22:03:50 -0700 | [diff] [blame] | 241 | return; |
| 242 | } |
| 243 | |
| 244 | rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon); |
| 245 | if (rc == 0 && !strcmp(scon, mycon)) { |
Nick Kralevich | 4800dbf | 2015-07-16 10:49:51 -0700 | [diff] [blame] | 246 | ERROR("Service %s does not have a SELinux domain defined.\n", svc->name); |
| 247 | free(mycon); |
| 248 | free(fcon); |
| 249 | free(scon); |
| 250 | return; |
Nick Kralevich | 4d87095 | 2015-06-12 22:03:50 -0700 | [diff] [blame] | 251 | } |
Nick Kralevich | 4800dbf | 2015-07-16 10:49:51 -0700 | [diff] [blame] | 252 | free(mycon); |
| 253 | free(fcon); |
Nick Kralevich | 4d87095 | 2015-06-12 22:03:50 -0700 | [diff] [blame] | 254 | if (rc < 0) { |
| 255 | ERROR("could not get context while starting '%s'\n", svc->name); |
| 256 | return; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 257 | } |
| 258 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 259 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 260 | NOTICE("Starting service '%s'...\n", svc->name); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 261 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 262 | pid_t pid = fork(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 263 | if (pid == 0) { |
| 264 | struct socketinfo *si; |
| 265 | struct svcenvinfo *ei; |
| 266 | char tmp[32]; |
| 267 | int fd, sz; |
| 268 | |
Nick Kralevich | 6ebf12f | 2012-03-26 09:09:11 -0700 | [diff] [blame] | 269 | umask(077); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 270 | if (properties_initialized()) { |
Colin Cross | 3294bbb | 2010-04-19 17:11:33 -0700 | [diff] [blame] | 271 | get_property_workspace(&fd, &sz); |
Yabin Cui | e2d63af | 2015-02-17 19:27:51 -0800 | [diff] [blame] | 272 | snprintf(tmp, sizeof(tmp), "%d,%d", dup(fd), sz); |
Colin Cross | 3294bbb | 2010-04-19 17:11:33 -0700 | [diff] [blame] | 273 | add_environment("ANDROID_PROPERTY_WORKSPACE", tmp); |
| 274 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 275 | |
| 276 | for (ei = svc->envvars; ei; ei = ei->next) |
| 277 | add_environment(ei->name, ei->value); |
| 278 | |
| 279 | for (si = svc->sockets; si; si = si->next) { |
Mike Lockwood | 912ff85 | 2010-10-01 08:20:36 -0400 | [diff] [blame] | 280 | int socket_type = ( |
| 281 | !strcmp(si->type, "stream") ? SOCK_STREAM : |
| 282 | (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET)); |
| 283 | int s = create_socket(si->name, socket_type, |
Stephen Smalley | 8348d27 | 2013-05-13 12:37:04 -0400 | [diff] [blame] | 284 | si->perm, si->uid, si->gid, si->socketcon ?: scon); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 285 | if (s >= 0) { |
| 286 | publish_socket(si->name, s); |
| 287 | } |
| 288 | } |
| 289 | |
Nick Kralevich | 4800dbf | 2015-07-16 10:49:51 -0700 | [diff] [blame] | 290 | free(scon); |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 291 | scon = NULL; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 292 | |
Elliott Hughes | d62f060 | 2015-06-12 18:02:20 -0700 | [diff] [blame] | 293 | if (svc->writepid_files_) { |
| 294 | std::string pid_str = android::base::StringPrintf("%d", pid); |
| 295 | for (auto& file : *svc->writepid_files_) { |
| 296 | if (!android::base::WriteStringToFile(pid_str, file)) { |
| 297 | ERROR("couldn't write %s to %s: %s\n", |
| 298 | pid_str.c_str(), file.c_str(), strerror(errno)); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
San Mehat | 4e221f0 | 2010-02-25 14:19:50 -0800 | [diff] [blame] | 303 | if (svc->ioprio_class != IoSchedClass_NONE) { |
| 304 | if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) { |
| 305 | ERROR("Failed to set pid %d ioprio = %d,%d: %s\n", |
| 306 | getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno)); |
| 307 | } |
| 308 | } |
| 309 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 310 | if (needs_console) { |
| 311 | setsid(); |
| 312 | open_console(); |
| 313 | } else { |
| 314 | zap_stdio(); |
| 315 | } |
| 316 | |
Elliott Hughes | c0e919c | 2015-02-04 14:46:36 -0800 | [diff] [blame] | 317 | if (false) { |
| 318 | for (size_t n = 0; svc->args[n]; n++) { |
| 319 | INFO("args[%zu] = '%s'\n", n, svc->args[n]); |
| 320 | } |
| 321 | for (size_t n = 0; ENV[n]; n++) { |
| 322 | INFO("env[%zu] = '%s'\n", n, ENV[n]); |
| 323 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 324 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 325 | |
| 326 | setpgid(0, getpid()); |
| 327 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 328 | // As requested, set our gid, supplemental gids, and uid. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 329 | if (svc->gid) { |
Nick Kralevich | 2268718 | 2010-11-17 16:55:42 -0800 | [diff] [blame] | 330 | if (setgid(svc->gid) != 0) { |
| 331 | ERROR("setgid failed: %s\n", strerror(errno)); |
| 332 | _exit(127); |
| 333 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 334 | } |
| 335 | if (svc->nr_supp_gids) { |
Nick Kralevich | 2268718 | 2010-11-17 16:55:42 -0800 | [diff] [blame] | 336 | if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) { |
| 337 | ERROR("setgroups failed: %s\n", strerror(errno)); |
| 338 | _exit(127); |
| 339 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 340 | } |
| 341 | if (svc->uid) { |
Nick Kralevich | 2268718 | 2010-11-17 16:55:42 -0800 | [diff] [blame] | 342 | if (setuid(svc->uid) != 0) { |
| 343 | ERROR("setuid failed: %s\n", strerror(errno)); |
| 344 | _exit(127); |
| 345 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 346 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 347 | if (svc->seclabel) { |
Nick Kralevich | 4d87095 | 2015-06-12 22:03:50 -0700 | [diff] [blame] | 348 | if (setexeccon(svc->seclabel) < 0) { |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 349 | ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno)); |
| 350 | _exit(127); |
| 351 | } |
| 352 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 353 | |
San Mehat | 8ad1568 | 2009-05-20 08:50:40 -0700 | [diff] [blame] | 354 | if (!dynamic_args) { |
| 355 | if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) { |
| 356 | ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno)); |
| 357 | } |
| 358 | } else { |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 359 | char *arg_ptrs[INIT_PARSER_MAXARGS+1]; |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 360 | int arg_idx = svc->nargs; |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 361 | char *tmp = strdup(dynamic_args); |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 362 | char *next = tmp; |
| 363 | char *bword; |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 364 | |
| 365 | /* Copy the static arguments */ |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 366 | memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *))); |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 367 | |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 368 | while((bword = strsep(&next, " "))) { |
| 369 | arg_ptrs[arg_idx++] = bword; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 370 | if (arg_idx == INIT_PARSER_MAXARGS) |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 371 | break; |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 372 | } |
Andreas Gampe | 0ab46c9 | 2015-02-03 11:20:49 -0800 | [diff] [blame] | 373 | arg_ptrs[arg_idx] = NULL; |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 374 | execve(svc->args[0], (char**) arg_ptrs, (char**) ENV); |
Ivan Djelic | 165de92 | 2008-11-23 22:26:39 +0100 | [diff] [blame] | 375 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 376 | _exit(127); |
| 377 | } |
| 378 | |
Nick Kralevich | 4800dbf | 2015-07-16 10:49:51 -0700 | [diff] [blame] | 379 | free(scon); |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 380 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 381 | if (pid < 0) { |
| 382 | ERROR("failed to start '%s'\n", svc->name); |
| 383 | svc->pid = 0; |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | svc->time_started = gettime(); |
| 388 | svc->pid = pid; |
| 389 | svc->flags |= SVC_RUNNING; |
| 390 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 391 | if ((svc->flags & SVC_EXEC) != 0) { |
Elliott Hughes | 58c3bac | 2015-03-11 12:31:53 -0700 | [diff] [blame] | 392 | INFO("SVC_EXEC pid %d (uid %d gid %d+%zu context %s) started; waiting...\n", |
Mark Salyzyn | 17fff89 | 2015-06-02 11:11:02 -0700 | [diff] [blame] | 393 | svc->pid, svc->uid, svc->gid, svc->nr_supp_gids, |
| 394 | svc->seclabel ? : "default"); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 395 | waiting_for_exec = true; |
| 396 | } |
| 397 | |
| 398 | svc->NotifyStateChange("running"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 401 | /* 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] | 402 | 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] | 403 | { |
Mike Kasick | 7e36edd | 2012-02-06 10:32:13 -0500 | [diff] [blame] | 404 | /* The service is still SVC_RUNNING until its process exits, but if it has |
| 405 | * already exited it shoudn't attempt a restart yet. */ |
JP Abgrall | 3beec7e | 2014-05-02 21:14:29 -0700 | [diff] [blame] | 406 | svc->flags &= ~(SVC_RESTARTING | SVC_DISABLED_START); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 407 | |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 408 | if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) { |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 409 | /* Hrm, an illegal flag. Default to SVC_DISABLED */ |
| 410 | how = SVC_DISABLED; |
| 411 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 412 | /* if the service has not yet started, prevent |
| 413 | * it from auto-starting with its class |
| 414 | */ |
Ken Sumrall | a286480 | 2011-10-26 16:56:00 -0700 | [diff] [blame] | 415 | if (how == SVC_RESET) { |
| 416 | svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET; |
| 417 | } else { |
| 418 | svc->flags |= how; |
| 419 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 420 | |
| 421 | if (svc->pid) { |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 422 | NOTICE("Service '%s' is being killed...\n", svc->name); |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 423 | kill(-svc->pid, SIGKILL); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 424 | svc->NotifyStateChange("stopping"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 425 | } else { |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 426 | svc->NotifyStateChange("stopped"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 430 | void service_reset(struct service *svc) |
| 431 | { |
| 432 | service_stop_or_reset(svc, SVC_RESET); |
| 433 | } |
| 434 | |
| 435 | void service_stop(struct service *svc) |
| 436 | { |
| 437 | service_stop_or_reset(svc, SVC_DISABLED); |
| 438 | } |
| 439 | |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 440 | void service_restart(struct service *svc) |
| 441 | { |
| 442 | if (svc->flags & SVC_RUNNING) { |
| 443 | /* Stop, wait, then start the service. */ |
| 444 | service_stop_or_reset(svc, SVC_RESTART); |
| 445 | } else if (!(svc->flags & SVC_RESTARTING)) { |
| 446 | /* Just start the service since it's not running. */ |
| 447 | service_start(svc, NULL); |
| 448 | } /* else: Service is restarting anyways. */ |
| 449 | } |
| 450 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 451 | void property_changed(const char *name, const char *value) |
| 452 | { |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 453 | if (property_triggers_enabled) |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 454 | ActionManager::GetInstance().QueuePropertyTrigger(name, value); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 455 | } |
| 456 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 457 | static void restart_service_if_needed(struct service *svc) |
| 458 | { |
| 459 | time_t next_start_time = svc->time_started + 5; |
| 460 | |
| 461 | if (next_start_time <= gettime()) { |
| 462 | svc->flags &= (~SVC_RESTARTING); |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 463 | service_start(svc, NULL); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 464 | return; |
| 465 | } |
| 466 | |
| 467 | if ((next_start_time < process_needs_restart) || |
| 468 | (process_needs_restart == 0)) { |
| 469 | process_needs_restart = next_start_time; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | static void restart_processes() |
| 474 | { |
| 475 | process_needs_restart = 0; |
| 476 | service_for_each_flags(SVC_RESTARTING, |
| 477 | restart_service_if_needed); |
| 478 | } |
| 479 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 480 | static void msg_start(const char *name) |
| 481 | { |
Hong-Mei Li | 1146718 | 2013-04-01 11:17:51 +0800 | [diff] [blame] | 482 | struct service *svc = NULL; |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 483 | char *tmp = NULL; |
| 484 | char *args = NULL; |
| 485 | |
| 486 | if (!strchr(name, ':')) |
| 487 | svc = service_find_by_name(name); |
| 488 | else { |
| 489 | tmp = strdup(name); |
Hong-Mei Li | 1146718 | 2013-04-01 11:17:51 +0800 | [diff] [blame] | 490 | if (tmp) { |
| 491 | args = strchr(tmp, ':'); |
| 492 | *args = '\0'; |
| 493 | args++; |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 494 | |
Hong-Mei Li | 1146718 | 2013-04-01 11:17:51 +0800 | [diff] [blame] | 495 | svc = service_find_by_name(tmp); |
| 496 | } |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 497 | } |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 498 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 499 | if (svc) { |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 500 | service_start(svc, args); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 501 | } else { |
| 502 | ERROR("no such service '%s'\n", name); |
| 503 | } |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 504 | if (tmp) |
| 505 | free(tmp); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static void msg_stop(const char *name) |
| 509 | { |
| 510 | struct service *svc = service_find_by_name(name); |
| 511 | |
| 512 | if (svc) { |
| 513 | service_stop(svc); |
| 514 | } else { |
Dima Zavin | 770354d | 2009-05-05 18:33:07 -0700 | [diff] [blame] | 515 | ERROR("no such service '%s'\n", name); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 519 | static void msg_restart(const char *name) |
| 520 | { |
| 521 | struct service *svc = service_find_by_name(name); |
| 522 | |
| 523 | if (svc) { |
| 524 | service_restart(svc); |
| 525 | } else { |
| 526 | ERROR("no such service '%s'\n", name); |
| 527 | } |
| 528 | } |
| 529 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 530 | void handle_control_message(const char *msg, const char *arg) |
| 531 | { |
| 532 | if (!strcmp(msg,"start")) { |
| 533 | msg_start(arg); |
| 534 | } else if (!strcmp(msg,"stop")) { |
| 535 | msg_stop(arg); |
Wink Saville | cfa0d84 | 2010-10-03 13:30:11 -0700 | [diff] [blame] | 536 | } else if (!strcmp(msg,"restart")) { |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 537 | msg_restart(arg); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 538 | } else { |
| 539 | ERROR("unknown control msg '%s'\n", msg); |
| 540 | } |
| 541 | } |
| 542 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 543 | static int wait_for_coldboot_done_action(const std::vector<std::string>& args) { |
Elliott Hughes | c6c26ed | 2015-04-24 18:50:30 -0700 | [diff] [blame] | 544 | Timer t; |
| 545 | |
| 546 | NOTICE("Waiting for %s...\n", COLDBOOT_DONE); |
Elliott Hughes | c7331d0 | 2015-05-14 12:03:14 -0700 | [diff] [blame] | 547 | // Any longer than 1s is an unreasonable length of time to delay booting. |
| 548 | // If you're hitting this timeout, check that you didn't make your |
| 549 | // sepolicy regular expressions too expensive (http://b/19899875). |
| 550 | if (wait_for_file(COLDBOOT_DONE, 1)) { |
Andreas Gampe | a016c42 | 2014-11-24 19:52:41 -0800 | [diff] [blame] | 551 | ERROR("Timed out waiting for %s\n", COLDBOOT_DONE); |
Elliott Hughes | c6c26ed | 2015-04-24 18:50:30 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | NOTICE("Waiting for %s took %.2fs.\n", COLDBOOT_DONE, t.duration()); |
| 555 | return 0; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 558 | /* |
| 559 | * Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed |
| 560 | * by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom. |
| 561 | * Does nothing if Hardware RNG is not present. |
| 562 | * |
| 563 | * Since we don't yet trust the quality of Hardware RNG, these bytes are not |
| 564 | * mixed into the primary pool of Linux RNG and the entropy estimate is left |
| 565 | * unmodified. |
| 566 | * |
| 567 | * If the HW RNG device /dev/hw_random is present, we require that at least |
| 568 | * 512 bytes read from it are written into Linux RNG. QA is expected to catch |
| 569 | * devices/configurations where these I/O operations are blocking for a long |
| 570 | * time. We do not reboot or halt on failures, as this is a best-effort |
| 571 | * attempt. |
| 572 | */ |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 573 | static int mix_hwrng_into_linux_rng_action(const std::vector<std::string>& args) |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 574 | { |
| 575 | int result = -1; |
| 576 | int hwrandom_fd = -1; |
| 577 | int urandom_fd = -1; |
| 578 | char buf[512]; |
| 579 | ssize_t chunk_size; |
| 580 | size_t total_bytes_written = 0; |
| 581 | |
| 582 | hwrandom_fd = TEMP_FAILURE_RETRY( |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 583 | open("/dev/hw_random", O_RDONLY | O_NOFOLLOW | O_CLOEXEC)); |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 584 | if (hwrandom_fd == -1) { |
| 585 | if (errno == ENOENT) { |
| 586 | ERROR("/dev/hw_random not found\n"); |
| 587 | /* It's not an error to not have a Hardware RNG. */ |
| 588 | result = 0; |
| 589 | } else { |
| 590 | ERROR("Failed to open /dev/hw_random: %s\n", strerror(errno)); |
| 591 | } |
| 592 | goto ret; |
| 593 | } |
| 594 | |
| 595 | urandom_fd = TEMP_FAILURE_RETRY( |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 596 | open("/dev/urandom", O_WRONLY | O_NOFOLLOW | O_CLOEXEC)); |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 597 | if (urandom_fd == -1) { |
| 598 | ERROR("Failed to open /dev/urandom: %s\n", strerror(errno)); |
| 599 | goto ret; |
| 600 | } |
| 601 | |
| 602 | while (total_bytes_written < sizeof(buf)) { |
| 603 | chunk_size = TEMP_FAILURE_RETRY( |
| 604 | read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written)); |
| 605 | if (chunk_size == -1) { |
| 606 | ERROR("Failed to read from /dev/hw_random: %s\n", strerror(errno)); |
| 607 | goto ret; |
| 608 | } else if (chunk_size == 0) { |
| 609 | ERROR("Failed to read from /dev/hw_random: EOF\n"); |
| 610 | goto ret; |
| 611 | } |
| 612 | |
| 613 | chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size)); |
| 614 | if (chunk_size == -1) { |
| 615 | ERROR("Failed to write to /dev/urandom: %s\n", strerror(errno)); |
| 616 | goto ret; |
| 617 | } |
| 618 | total_bytes_written += chunk_size; |
| 619 | } |
| 620 | |
Elliott Hughes | ccecf14 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 621 | INFO("Mixed %zu bytes from /dev/hw_random into /dev/urandom", |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 622 | total_bytes_written); |
| 623 | result = 0; |
| 624 | |
| 625 | ret: |
| 626 | if (hwrandom_fd != -1) { |
| 627 | close(hwrandom_fd); |
| 628 | } |
| 629 | if (urandom_fd != -1) { |
| 630 | close(urandom_fd); |
| 631 | } |
Alex Klyubin | 0d872d8 | 2013-08-16 13:19:24 -0700 | [diff] [blame] | 632 | return result; |
| 633 | } |
| 634 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 635 | static int keychord_init_action(const std::vector<std::string>& args) |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 636 | { |
| 637 | keychord_init(); |
| 638 | return 0; |
| 639 | } |
| 640 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 641 | static int console_init_action(const std::vector<std::string>& args) |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 642 | { |
Yabin Cui | 74edcea | 2015-07-24 10:11:05 -0700 | [diff] [blame] | 643 | std::string console = property_get("ro.boot.console"); |
| 644 | if (!console.empty()) { |
| 645 | console_name = "/dev/" + console; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Yabin Cui | 74edcea | 2015-07-24 10:11:05 -0700 | [diff] [blame] | 648 | int fd = open(console_name.c_str(), O_RDWR | O_CLOEXEC); |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 649 | if (fd >= 0) |
| 650 | have_console = 1; |
| 651 | close(fd); |
| 652 | |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 653 | fd = open("/dev/tty0", O_WRONLY | O_CLOEXEC); |
Marcin Chojnacki | 50dc936 | 2013-10-16 17:39:16 +0200 | [diff] [blame] | 654 | if (fd >= 0) { |
| 655 | const char *msg; |
| 656 | msg = "\n" |
| 657 | "\n" |
| 658 | "\n" |
| 659 | "\n" |
| 660 | "\n" |
| 661 | "\n" |
| 662 | "\n" // console is 40 cols x 30 lines |
| 663 | "\n" |
| 664 | "\n" |
| 665 | "\n" |
| 666 | "\n" |
| 667 | "\n" |
| 668 | "\n" |
| 669 | "\n" |
| 670 | " A N D R O I D "; |
| 671 | write(fd, msg, strlen(msg)); |
| 672 | close(fd); |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 673 | } |
Marcin Chojnacki | 50dc936 | 2013-10-16 17:39:16 +0200 | [diff] [blame] | 674 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 675 | return 0; |
| 676 | } |
| 677 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 678 | static void import_kernel_nv(const std::string& key, const std::string& value, bool for_emulator) { |
| 679 | if (key.empty()) return; |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 680 | |
| 681 | if (for_emulator) { |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 682 | // In the emulator, export any kernel option with the "ro.kernel." prefix. |
| 683 | property_set(android::base::StringPrintf("ro.kernel.%s", key.c_str()).c_str(), value.c_str()); |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 684 | return; |
| 685 | } |
| 686 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 687 | if (key == "qemu") { |
| 688 | strlcpy(qemu, value.c_str(), sizeof(qemu)); |
| 689 | } else if (android::base::StartsWith(key, "androidboot.")) { |
| 690 | property_set(android::base::StringPrintf("ro.boot.%s", key.c_str() + 12).c_str(), |
| 691 | value.c_str()); |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | |
Elliott Hughes | d679bc9 | 2015-03-20 08:50:46 -0700 | [diff] [blame] | 695 | static void export_kernel_boot_props() { |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 696 | struct { |
| 697 | const char *src_prop; |
Elliott Hughes | d679bc9 | 2015-03-20 08:50:46 -0700 | [diff] [blame] | 698 | const char *dst_prop; |
| 699 | const char *default_value; |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 700 | } prop_map[] = { |
Elliott Hughes | d679bc9 | 2015-03-20 08:50:46 -0700 | [diff] [blame] | 701 | { "ro.boot.serialno", "ro.serialno", "", }, |
| 702 | { "ro.boot.mode", "ro.bootmode", "unknown", }, |
| 703 | { "ro.boot.baseband", "ro.baseband", "unknown", }, |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 704 | { "ro.boot.bootloader", "ro.bootloader", "unknown", }, |
Elliott Hughes | d679bc9 | 2015-03-20 08:50:46 -0700 | [diff] [blame] | 705 | { "ro.boot.hardware", "ro.hardware", "unknown", }, |
| 706 | { "ro.boot.revision", "ro.revision", "0", }, |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 707 | }; |
Elliott Hughes | d679bc9 | 2015-03-20 08:50:46 -0700 | [diff] [blame] | 708 | for (size_t i = 0; i < ARRAY_SIZE(prop_map); i++) { |
Yabin Cui | 74edcea | 2015-07-24 10:11:05 -0700 | [diff] [blame] | 709 | std::string value = property_get(prop_map[i].src_prop); |
| 710 | property_set(prop_map[i].dst_prop, (!value.empty()) ? value.c_str() : prop_map[i].default_value); |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 711 | } |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 712 | } |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 713 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 714 | static void process_kernel_dt() { |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 715 | static const char android_dir[] = "/proc/device-tree/firmware/android"; |
| 716 | |
| 717 | std::string file_name = android::base::StringPrintf("%s/compatible", android_dir); |
| 718 | |
| 719 | std::string dt_file; |
| 720 | android::base::ReadFileToString(file_name, &dt_file); |
| 721 | if (!dt_file.compare("android,firmware")) { |
| 722 | ERROR("firmware/android is not compatible with 'android,firmware'\n"); |
| 723 | return; |
| 724 | } |
| 725 | |
| 726 | std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(android_dir), closedir); |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 727 | if (!dir) return; |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 728 | |
| 729 | struct dirent *dp; |
| 730 | while ((dp = readdir(dir.get())) != NULL) { |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 731 | if (dp->d_type != DT_REG || !strcmp(dp->d_name, "compatible")) { |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 732 | continue; |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 733 | } |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 734 | |
| 735 | file_name = android::base::StringPrintf("%s/%s", android_dir, dp->d_name); |
| 736 | |
| 737 | android::base::ReadFileToString(file_name, &dt_file); |
| 738 | std::replace(dt_file.begin(), dt_file.end(), ',', '.'); |
| 739 | |
| 740 | std::string property_name = android::base::StringPrintf("ro.boot.%s", dp->d_name); |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 741 | property_set(property_name.c_str(), dt_file.c_str()); |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 745 | static void process_kernel_cmdline() { |
| 746 | // Don't expose the raw commandline to unprivileged processes. |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 747 | chmod("/proc/cmdline", 0440); |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 748 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 749 | // The first pass does the common stuff, and finds if we are in qemu. |
| 750 | // The second pass is only necessary for qemu to export all kernel params |
| 751 | // as properties. |
Nick Kralevich | f667a32 | 2015-04-25 17:42:52 -0700 | [diff] [blame] | 752 | import_kernel_cmdline(false, import_kernel_nv); |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 753 | if (qemu[0]) import_kernel_cmdline(true, import_kernel_nv); |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 756 | static int queue_property_triggers_action(const std::vector<std::string>& args) |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 757 | { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 758 | ActionManager::GetInstance().QueueAllPropertyTriggers(); |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 759 | /* enable property triggers */ |
| 760 | property_triggers_enabled = 1; |
| 761 | return 0; |
| 762 | } |
| 763 | |
Elliott Hughes | c6c26ed | 2015-04-24 18:50:30 -0700 | [diff] [blame] | 764 | static void selinux_init_all_handles(void) |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 765 | { |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 766 | sehandle = selinux_android_file_context_handle(); |
Stephen Smalley | dbd37f2 | 2014-01-28 10:34:09 -0500 | [diff] [blame] | 767 | selinux_android_set_sehandle(sehandle); |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 768 | sehandle_prop = selinux_android_prop_context_handle(); |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 769 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 770 | |
Nick Kralevich | d34e407 | 2015-04-28 12:39:41 -0700 | [diff] [blame] | 771 | enum selinux_enforcing_status { SELINUX_PERMISSIVE, SELINUX_ENFORCING }; |
Nick Kralevich | f667a32 | 2015-04-25 17:42:52 -0700 | [diff] [blame] | 772 | |
| 773 | static selinux_enforcing_status selinux_status_from_cmdline() { |
| 774 | selinux_enforcing_status status = SELINUX_ENFORCING; |
| 775 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 776 | import_kernel_cmdline(false, [&](const std::string& key, const std::string& value, bool in_qemu) { |
| 777 | if (key == "androidboot.selinux" && value == "permissive") { |
| 778 | status = SELINUX_PERMISSIVE; |
Nick Kralevich | f667a32 | 2015-04-25 17:42:52 -0700 | [diff] [blame] | 779 | } |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 780 | }); |
Nick Kralevich | f667a32 | 2015-04-25 17:42:52 -0700 | [diff] [blame] | 781 | |
| 782 | return status; |
| 783 | } |
| 784 | |
Nick Kralevich | 935bd3e | 2013-06-26 15:37:26 -0700 | [diff] [blame] | 785 | static bool selinux_is_enforcing(void) |
| 786 | { |
Nick Kralevich | d34e407 | 2015-04-28 12:39:41 -0700 | [diff] [blame] | 787 | if (ALLOW_PERMISSIVE_SELINUX) { |
Nick Kralevich | f667a32 | 2015-04-25 17:42:52 -0700 | [diff] [blame] | 788 | return selinux_status_from_cmdline() == SELINUX_ENFORCING; |
Nick Kralevich | 935bd3e | 2013-06-26 15:37:26 -0700 | [diff] [blame] | 789 | } |
Nick Kralevich | 935bd3e | 2013-06-26 15:37:26 -0700 | [diff] [blame] | 790 | return true; |
| 791 | } |
| 792 | |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 793 | int selinux_reload_policy(void) |
| 794 | { |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 795 | INFO("SELinux: Attempting to reload policy files\n"); |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 796 | |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 797 | if (selinux_android_reload_policy() == -1) { |
| 798 | return -1; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 799 | } |
| 800 | |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 801 | if (sehandle) |
| 802 | selabel_close(sehandle); |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 803 | |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 804 | if (sehandle_prop) |
| 805 | selabel_close(sehandle_prop); |
| 806 | |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 807 | selinux_init_all_handles(); |
| 808 | return 0; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 809 | } |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 810 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 811 | static int audit_callback(void *data, security_class_t /*cls*/, char *buf, size_t len) { |
rpcraig | 63207cd | 2012-08-09 10:05:49 -0400 | [diff] [blame] | 812 | snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data); |
| 813 | return 0; |
| 814 | } |
| 815 | |
Elliott Hughes | f65730e | 2015-04-24 18:38:17 +0000 | [diff] [blame] | 816 | static void security_failure() { |
| 817 | ERROR("Security failure; rebooting into recovery mode...\n"); |
| 818 | android_reboot(ANDROID_RB_RESTART2, 0, "recovery"); |
| 819 | while (true) { pause(); } // never reached |
| 820 | } |
| 821 | |
| 822 | static void selinux_initialize(bool in_kernel_domain) { |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 823 | Timer t; |
Stephen Smalley | eb3f421 | 2014-02-12 16:17:00 -0500 | [diff] [blame] | 824 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 825 | selinux_callback cb; |
| 826 | cb.func_log = selinux_klog_callback; |
| 827 | selinux_set_callback(SELINUX_CB_LOG, cb); |
| 828 | cb.func_audit = audit_callback; |
| 829 | selinux_set_callback(SELINUX_CB_AUDIT, cb); |
| 830 | |
Elliott Hughes | f65730e | 2015-04-24 18:38:17 +0000 | [diff] [blame] | 831 | if (in_kernel_domain) { |
Elliott Hughes | f65730e | 2015-04-24 18:38:17 +0000 | [diff] [blame] | 832 | INFO("Loading SELinux policy...\n"); |
| 833 | if (selinux_android_load_policy() < 0) { |
| 834 | ERROR("failed to load policy: %s\n", strerror(errno)); |
| 835 | security_failure(); |
| 836 | } |
| 837 | |
Nick Kralevich | d34e407 | 2015-04-28 12:39:41 -0700 | [diff] [blame] | 838 | bool kernel_enforcing = (security_getenforce() == 1); |
Elliott Hughes | f65730e | 2015-04-24 18:38:17 +0000 | [diff] [blame] | 839 | bool is_enforcing = selinux_is_enforcing(); |
Nick Kralevich | d34e407 | 2015-04-28 12:39:41 -0700 | [diff] [blame] | 840 | if (kernel_enforcing != is_enforcing) { |
| 841 | if (security_setenforce(is_enforcing)) { |
| 842 | ERROR("security_setenforce(%s) failed: %s\n", |
| 843 | is_enforcing ? "true" : "false", strerror(errno)); |
| 844 | security_failure(); |
| 845 | } |
| 846 | } |
Elliott Hughes | f65730e | 2015-04-24 18:38:17 +0000 | [diff] [blame] | 847 | |
Nick Kralevich | eedbe81 | 2015-04-25 14:10:03 -0700 | [diff] [blame] | 848 | if (write_file("/sys/fs/selinux/checkreqprot", "0") == -1) { |
| 849 | security_failure(); |
| 850 | } |
| 851 | |
Elliott Hughes | f65730e | 2015-04-24 18:38:17 +0000 | [diff] [blame] | 852 | NOTICE("(Initializing SELinux %s took %.2fs.)\n", |
| 853 | is_enforcing ? "enforcing" : "non-enforcing", t.duration()); |
| 854 | } else { |
| 855 | selinux_init_all_handles(); |
Nick Kralevich | 56fa0ac | 2013-06-24 17:41:40 -0700 | [diff] [blame] | 856 | } |
Nick Kralevich | 56fa0ac | 2013-06-24 17:41:40 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 859 | int main(int argc, char** argv) { |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 860 | if (!strcmp(basename(argv[0]), "ueventd")) { |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 861 | return ueventd_main(argc, argv); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 862 | } |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 863 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 864 | if (!strcmp(basename(argv[0]), "watchdogd")) { |
Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 865 | return watchdogd_main(argc, argv); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 866 | } |
Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 867 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 868 | // Clear the umask. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 869 | umask(0); |
| 870 | |
Elliott Hughes | cc86fb2 | 2015-03-17 20:01:13 -0700 | [diff] [blame] | 871 | add_environment("PATH", _PATH_DEFPATH); |
| 872 | |
Nick Kralevich | 9dec93b | 2015-04-25 15:50:03 -0700 | [diff] [blame] | 873 | bool is_first_stage = (argc == 1) || (strcmp(argv[1], "--second-stage") != 0); |
| 874 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 875 | // Get the basic filesystem setup we need put together in the initramdisk |
| 876 | // on / and then we'll let the rc file figure out the rest. |
Nick Kralevich | 9dec93b | 2015-04-25 15:50:03 -0700 | [diff] [blame] | 877 | if (is_first_stage) { |
Nick Kralevich | f667a32 | 2015-04-25 17:42:52 -0700 | [diff] [blame] | 878 | mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755"); |
| 879 | mkdir("/dev/pts", 0755); |
| 880 | mkdir("/dev/socket", 0755); |
| 881 | mount("devpts", "/dev/pts", "devpts", 0, NULL); |
Nick Kralevich | 9dec93b | 2015-04-25 15:50:03 -0700 | [diff] [blame] | 882 | mount("proc", "/proc", "proc", 0, NULL); |
| 883 | mount("sysfs", "/sys", "sysfs", 0, NULL); |
| 884 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 885 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 886 | // We must have some place other than / to create the device nodes for |
| 887 | // kmsg and null, otherwise we won't be able to remount / read-only |
| 888 | // later on. Now that tmpfs is mounted on /dev, we can actually talk |
| 889 | // to the outside world. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 890 | open_devnull_stdio(); |
Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 891 | klog_init(); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 892 | klog_set_level(KLOG_NOTICE_LEVEL); |
| 893 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 894 | NOTICE("init %s started!\n", is_first_stage ? "first stage" : "second stage"); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 895 | |
Nick Kralevich | f667a32 | 2015-04-25 17:42:52 -0700 | [diff] [blame] | 896 | if (!is_first_stage) { |
| 897 | // Indicate that booting is in progress to background fw loaders, etc. |
| 898 | close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000)); |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 899 | |
Nick Kralevich | f667a32 | 2015-04-25 17:42:52 -0700 | [diff] [blame] | 900 | property_init(); |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 901 | |
Nick Kralevich | f667a32 | 2015-04-25 17:42:52 -0700 | [diff] [blame] | 902 | // If arguments are passed both on the command line and in DT, |
| 903 | // properties set in DT always have priority over the command-line ones. |
| 904 | process_kernel_dt(); |
| 905 | process_kernel_cmdline(); |
| 906 | |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 907 | // Propagate the kernel variables to internal variables |
Nick Kralevich | f667a32 | 2015-04-25 17:42:52 -0700 | [diff] [blame] | 908 | // used by init as well as the current required properties. |
| 909 | export_kernel_boot_props(); |
| 910 | } |
Rom Lemarchand | 6a52443 | 2015-02-28 06:39:11 -0800 | [diff] [blame] | 911 | |
Elliott Hughes | f65730e | 2015-04-24 18:38:17 +0000 | [diff] [blame] | 912 | // Set up SELinux, including loading the SELinux policy if we're in the kernel domain. |
| 913 | selinux_initialize(is_first_stage); |
| 914 | |
| 915 | // If we're in the kernel domain, re-exec init to transition to the init domain now |
| 916 | // that the SELinux policy has been loaded. |
| 917 | if (is_first_stage) { |
| 918 | if (restorecon("/init") == -1) { |
| 919 | ERROR("restorecon failed: %s\n", strerror(errno)); |
| 920 | security_failure(); |
| 921 | } |
| 922 | char* path = argv[0]; |
| 923 | char* args[] = { path, const_cast<char*>("--second-stage"), nullptr }; |
| 924 | if (execv(path, args) == -1) { |
| 925 | ERROR("execv(\"%s\") failed: %s\n", path, strerror(errno)); |
| 926 | security_failure(); |
| 927 | } |
| 928 | } |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 929 | |
| 930 | // These directories were necessarily created before initial policy load |
| 931 | // and therefore need their security context restored to the proper value. |
| 932 | // This must happen before /dev is populated by ueventd. |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 933 | NOTICE("Running restorecon...\n"); |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 934 | restorecon("/dev"); |
| 935 | restorecon("/dev/socket"); |
Geremy Condra | 8e15eab | 2013-02-28 17:29:58 -0800 | [diff] [blame] | 936 | restorecon("/dev/__properties__"); |
Nick Kralevich | ae76f6d | 2013-07-11 15:38:26 -0700 | [diff] [blame] | 937 | restorecon_recursive("/sys"); |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 938 | |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 939 | epoll_fd = epoll_create1(EPOLL_CLOEXEC); |
| 940 | if (epoll_fd == -1) { |
| 941 | ERROR("epoll_create1 failed: %s\n", strerror(errno)); |
| 942 | exit(1); |
| 943 | } |
| 944 | |
| 945 | signal_handler_init(); |
Elliott Hughes | 9042cae | 2015-04-24 17:43:21 -0700 | [diff] [blame] | 946 | |
Riley Andrews | e4b7b29 | 2014-06-16 15:06:21 -0700 | [diff] [blame] | 947 | property_load_boot_defaults(); |
Elliott Hughes | c6c26ed | 2015-04-24 18:50:30 -0700 | [diff] [blame] | 948 | start_property_service(); |
Dima Zavin | d7634c9 | 2011-12-16 14:18:06 -0800 | [diff] [blame] | 949 | |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame] | 950 | init_parse_config("/init.rc"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 951 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 952 | ActionManager& am = ActionManager::GetInstance(); |
| 953 | |
| 954 | am.QueueEventTrigger("early-init"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 955 | |
Elliott Hughes | c6c26ed | 2015-04-24 18:50:30 -0700 | [diff] [blame] | 956 | // Queue an action that waits for coldboot done so we know ueventd has set up all of /dev... |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 957 | am.QueueBuiltinAction(wait_for_coldboot_done_action, "wait_for_coldboot_done"); |
Elliott Hughes | c6c26ed | 2015-04-24 18:50:30 -0700 | [diff] [blame] | 958 | // ... so that we can start queuing up actions that require stuff from /dev. |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 959 | am.QueueBuiltinAction(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng"); |
| 960 | am.QueueBuiltinAction(keychord_init_action, "keychord_init"); |
| 961 | am.QueueBuiltinAction(console_init_action, "console_init"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 962 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 963 | // Trigger all the boot actions to get us started. |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 964 | am.QueueEventTrigger("init"); |
Dima Zavin | ca47cef | 2011-08-24 15:28:23 -0700 | [diff] [blame] | 965 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 966 | // Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random |
| 967 | // wasn't ready immediately after wait_for_coldboot_done |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 968 | am.QueueBuiltinAction(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 969 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 970 | // Don't mount filesystems or start core system services in charger mode. |
Yabin Cui | 74edcea | 2015-07-24 10:11:05 -0700 | [diff] [blame] | 971 | std::string bootmode = property_get("ro.bootmode"); |
| 972 | if (bootmode == "charger") { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 973 | am.QueueEventTrigger("charger"); |
Dima Zavin | ca47cef | 2011-08-24 15:28:23 -0700 | [diff] [blame] | 974 | } else { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 975 | am.QueueEventTrigger("late-init"); |
Dima Zavin | ca47cef | 2011-08-24 15:28:23 -0700 | [diff] [blame] | 976 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 977 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 978 | // Run all property triggers based on current state of the properties. |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 979 | am.QueueBuiltinAction(queue_property_triggers_action, "queue_property_triggers"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 980 | |
Elliott Hughes | c6c26ed | 2015-04-24 18:50:30 -0700 | [diff] [blame] | 981 | while (true) { |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 982 | if (!waiting_for_exec) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 983 | am.ExecuteOneCommand(); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 984 | restart_processes(); |
| 985 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 986 | |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 987 | int timeout = -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 988 | if (process_needs_restart) { |
| 989 | timeout = (process_needs_restart - gettime()) * 1000; |
| 990 | if (timeout < 0) |
| 991 | timeout = 0; |
| 992 | } |
| 993 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 994 | if (am.HasMoreCommands()) { |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 995 | timeout = 0; |
Elliott Hughes | c0e919c | 2015-02-04 14:46:36 -0800 | [diff] [blame] | 996 | } |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 997 | |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 998 | bootchart_sample(&timeout); |
Elliott Hughes | c0e919c | 2015-02-04 14:46:36 -0800 | [diff] [blame] | 999 | |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame] | 1000 | epoll_event ev; |
| 1001 | int nr = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd, &ev, 1, timeout)); |
| 1002 | if (nr == -1) { |
| 1003 | ERROR("epoll_wait failed: %s\n", strerror(errno)); |
| 1004 | } else if (nr == 1) { |
| 1005 | ((void (*)()) ev.data.ptr)(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1006 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | return 0; |
| 1010 | } |