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