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