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