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