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