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