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