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