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