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