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