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