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 | |
| 35 | #ifdef HAVE_SELINUX |
| 36 | #include <sys/mman.h> |
| 37 | #include <selinux/selinux.h> |
| 38 | #include <selinux/label.h> |
| 39 | #endif |
| 40 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 41 | #include <libgen.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 42 | |
Dima Zavin | da04c52 | 2011-09-01 17:09:44 -0700 | [diff] [blame] | 43 | #include <cutils/list.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 44 | #include <cutils/sockets.h> |
San Mehat | 4e221f0 | 2010-02-25 14:19:50 -0800 | [diff] [blame] | 45 | #include <cutils/iosched_policy.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 | |
| 49 | #include <sys/system_properties.h> |
| 50 | |
| 51 | #include "devices.h" |
| 52 | #include "init.h" |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 53 | #include "log.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 54 | #include "property_service.h" |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 55 | #include "bootchart.h" |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 56 | #include "signal_handler.h" |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 57 | #include "keychords.h" |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 58 | #include "init_parser.h" |
Colin Cross | 3899e9f | 2010-04-13 20:35:46 -0700 | [diff] [blame] | 59 | #include "util.h" |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 60 | #include "ueventd.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 61 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 62 | #ifdef HAVE_SELINUX |
| 63 | struct selabel_handle *sehandle; |
| 64 | #endif |
| 65 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 66 | static int property_triggers_enabled = 0; |
| 67 | |
| 68 | #if BOOTCHART |
| 69 | static int bootchart_count; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 70 | #endif |
| 71 | |
| 72 | static char console[32]; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 73 | static char bootmode[32]; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 74 | static char hardware[32]; |
| 75 | static unsigned revision = 0; |
| 76 | static char qemu[32]; |
| 77 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 78 | #ifdef HAVE_SELINUX |
| 79 | static int selinux_enabled = 1; |
| 80 | static int selinux_enforcing = 0; |
| 81 | #endif |
| 82 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 83 | static struct action *cur_action = NULL; |
| 84 | static struct command *cur_command = NULL; |
| 85 | static struct listnode *command_queue = NULL; |
| 86 | |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 87 | 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] | 88 | { |
| 89 | char pname[PROP_NAME_MAX]; |
| 90 | int len = strlen(name); |
| 91 | if ((len + 10) > PROP_NAME_MAX) |
| 92 | return; |
| 93 | snprintf(pname, sizeof(pname), "init.svc.%s", name); |
| 94 | property_set(pname, state); |
| 95 | } |
| 96 | |
| 97 | static int have_console; |
| 98 | static char *console_name = "/dev/console"; |
| 99 | static time_t process_needs_restart; |
| 100 | |
| 101 | static const char *ENV[32]; |
| 102 | |
| 103 | /* add_environment - add "key=value" to the current environment */ |
| 104 | int add_environment(const char *key, const char *val) |
| 105 | { |
| 106 | int n; |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 107 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 108 | for (n = 0; n < 31; n++) { |
| 109 | if (!ENV[n]) { |
| 110 | size_t len = strlen(key) + strlen(val) + 2; |
| 111 | char *entry = malloc(len); |
| 112 | snprintf(entry, len, "%s=%s", key, val); |
| 113 | ENV[n] = entry; |
| 114 | return 0; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return 1; |
| 119 | } |
| 120 | |
| 121 | static void zap_stdio(void) |
| 122 | { |
| 123 | int fd; |
| 124 | fd = open("/dev/null", O_RDWR); |
| 125 | dup2(fd, 0); |
| 126 | dup2(fd, 1); |
| 127 | dup2(fd, 2); |
| 128 | close(fd); |
| 129 | } |
| 130 | |
| 131 | static void open_console() |
| 132 | { |
| 133 | int fd; |
| 134 | if ((fd = open(console_name, O_RDWR)) < 0) { |
| 135 | fd = open("/dev/null", O_RDWR); |
| 136 | } |
| 137 | dup2(fd, 0); |
| 138 | dup2(fd, 1); |
| 139 | dup2(fd, 2); |
| 140 | close(fd); |
| 141 | } |
| 142 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 143 | static void publish_socket(const char *name, int fd) |
| 144 | { |
| 145 | char key[64] = ANDROID_SOCKET_ENV_PREFIX; |
| 146 | char val[64]; |
| 147 | |
| 148 | strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, |
| 149 | name, |
| 150 | sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); |
| 151 | snprintf(val, sizeof(val), "%d", fd); |
| 152 | add_environment(key, val); |
| 153 | |
| 154 | /* make sure we don't close-on-exec */ |
| 155 | fcntl(fd, F_SETFD, 0); |
| 156 | } |
| 157 | |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 158 | 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] | 159 | { |
| 160 | struct stat s; |
| 161 | pid_t pid; |
| 162 | int needs_console; |
| 163 | int n; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 164 | #ifdef HAVE_SELINUX |
| 165 | char *scon = NULL; |
| 166 | int rc; |
| 167 | #endif |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 168 | /* 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] | 169 | * state and immediately takes it out of the restarting |
| 170 | * state if it was in there |
| 171 | */ |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame^] | 172 | svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 173 | svc->time_started = 0; |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 174 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 175 | /* running processes require no additional work -- if |
| 176 | * they're in the process of exiting, we've ensured |
| 177 | * that they will immediately restart on exit, unless |
| 178 | * they are ONESHOT |
| 179 | */ |
| 180 | if (svc->flags & SVC_RUNNING) { |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0; |
| 185 | if (needs_console && (!have_console)) { |
| 186 | ERROR("service '%s' requires console\n", svc->name); |
| 187 | svc->flags |= SVC_DISABLED; |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | if (stat(svc->args[0], &s) != 0) { |
| 192 | ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name); |
| 193 | svc->flags |= SVC_DISABLED; |
| 194 | return; |
| 195 | } |
| 196 | |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 197 | if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) { |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 198 | ERROR("service '%s' must be one-shot to use dynamic args, disabling\n", |
| 199 | svc->args[0]); |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 200 | svc->flags |= SVC_DISABLED; |
| 201 | return; |
| 202 | } |
| 203 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 204 | #ifdef HAVE_SELINUX |
| 205 | if (is_selinux_enabled() > 0) { |
| 206 | char *mycon = NULL, *fcon = NULL; |
| 207 | |
| 208 | INFO("computing context for service '%s'\n", svc->args[0]); |
| 209 | rc = getcon(&mycon); |
| 210 | if (rc < 0) { |
| 211 | ERROR("could not get context while starting '%s'\n", svc->name); |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | rc = getfilecon(svc->args[0], &fcon); |
| 216 | if (rc < 0) { |
| 217 | ERROR("could not get context while starting '%s'\n", svc->name); |
| 218 | freecon(mycon); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon); |
| 223 | freecon(mycon); |
| 224 | freecon(fcon); |
| 225 | if (rc < 0) { |
| 226 | ERROR("could not get context while starting '%s'\n", svc->name); |
| 227 | return; |
| 228 | } |
| 229 | } |
| 230 | #endif |
| 231 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 232 | NOTICE("starting '%s'\n", svc->name); |
| 233 | |
| 234 | pid = fork(); |
| 235 | |
| 236 | if (pid == 0) { |
| 237 | struct socketinfo *si; |
| 238 | struct svcenvinfo *ei; |
| 239 | char tmp[32]; |
| 240 | int fd, sz; |
| 241 | |
Colin Cross | 3294bbb | 2010-04-19 17:11:33 -0700 | [diff] [blame] | 242 | if (properties_inited()) { |
| 243 | get_property_workspace(&fd, &sz); |
| 244 | sprintf(tmp, "%d,%d", dup(fd), sz); |
| 245 | add_environment("ANDROID_PROPERTY_WORKSPACE", tmp); |
| 246 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 247 | |
| 248 | for (ei = svc->envvars; ei; ei = ei->next) |
| 249 | add_environment(ei->name, ei->value); |
| 250 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 251 | #ifdef HAVE_SELINUX |
| 252 | setsockcreatecon(scon); |
| 253 | #endif |
| 254 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 255 | for (si = svc->sockets; si; si = si->next) { |
Mike Lockwood | 912ff85 | 2010-10-01 08:20:36 -0400 | [diff] [blame] | 256 | int socket_type = ( |
| 257 | !strcmp(si->type, "stream") ? SOCK_STREAM : |
| 258 | (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET)); |
| 259 | int s = create_socket(si->name, socket_type, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 260 | si->perm, si->uid, si->gid); |
| 261 | if (s >= 0) { |
| 262 | publish_socket(si->name, s); |
| 263 | } |
| 264 | } |
| 265 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 266 | #ifdef HAVE_SELINUX |
| 267 | freecon(scon); |
| 268 | scon = NULL; |
| 269 | setsockcreatecon(NULL); |
| 270 | #endif |
| 271 | |
San Mehat | 4e221f0 | 2010-02-25 14:19:50 -0800 | [diff] [blame] | 272 | if (svc->ioprio_class != IoSchedClass_NONE) { |
| 273 | if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) { |
| 274 | ERROR("Failed to set pid %d ioprio = %d,%d: %s\n", |
| 275 | getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno)); |
| 276 | } |
| 277 | } |
| 278 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 279 | if (needs_console) { |
| 280 | setsid(); |
| 281 | open_console(); |
| 282 | } else { |
| 283 | zap_stdio(); |
| 284 | } |
| 285 | |
| 286 | #if 0 |
| 287 | for (n = 0; svc->args[n]; n++) { |
| 288 | INFO("args[%d] = '%s'\n", n, svc->args[n]); |
| 289 | } |
| 290 | for (n = 0; ENV[n]; n++) { |
| 291 | INFO("env[%d] = '%s'\n", n, ENV[n]); |
| 292 | } |
| 293 | #endif |
| 294 | |
| 295 | setpgid(0, getpid()); |
| 296 | |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 297 | /* as requested, set our gid, supplemental gids, and uid */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 298 | if (svc->gid) { |
Nick Kralevich | 2268718 | 2010-11-17 16:55:42 -0800 | [diff] [blame] | 299 | if (setgid(svc->gid) != 0) { |
| 300 | ERROR("setgid failed: %s\n", strerror(errno)); |
| 301 | _exit(127); |
| 302 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 303 | } |
| 304 | if (svc->nr_supp_gids) { |
Nick Kralevich | 2268718 | 2010-11-17 16:55:42 -0800 | [diff] [blame] | 305 | if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) { |
| 306 | ERROR("setgroups failed: %s\n", strerror(errno)); |
| 307 | _exit(127); |
| 308 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 309 | } |
| 310 | if (svc->uid) { |
Nick Kralevich | 2268718 | 2010-11-17 16:55:42 -0800 | [diff] [blame] | 311 | if (setuid(svc->uid) != 0) { |
| 312 | ERROR("setuid 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 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 317 | #ifdef HAVE_SELINUX |
| 318 | if (svc->seclabel) { |
| 319 | if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) { |
| 320 | ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno)); |
| 321 | _exit(127); |
| 322 | } |
| 323 | } |
| 324 | #endif |
| 325 | |
San Mehat | 8ad1568 | 2009-05-20 08:50:40 -0700 | [diff] [blame] | 326 | if (!dynamic_args) { |
| 327 | if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) { |
| 328 | ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno)); |
| 329 | } |
| 330 | } else { |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 331 | char *arg_ptrs[INIT_PARSER_MAXARGS+1]; |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 332 | int arg_idx = svc->nargs; |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 333 | char *tmp = strdup(dynamic_args); |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 334 | char *next = tmp; |
| 335 | char *bword; |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 336 | |
| 337 | /* Copy the static arguments */ |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 338 | memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *))); |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 339 | |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 340 | while((bword = strsep(&next, " "))) { |
| 341 | arg_ptrs[arg_idx++] = bword; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 342 | if (arg_idx == INIT_PARSER_MAXARGS) |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 343 | break; |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 344 | } |
| 345 | arg_ptrs[arg_idx] = '\0'; |
| 346 | execve(svc->args[0], (char**) arg_ptrs, (char**) ENV); |
Ivan Djelic | 165de92 | 2008-11-23 22:26:39 +0100 | [diff] [blame] | 347 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 348 | _exit(127); |
| 349 | } |
| 350 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 351 | #ifdef HAVE_SELINUX |
| 352 | freecon(scon); |
| 353 | #endif |
| 354 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 355 | if (pid < 0) { |
| 356 | ERROR("failed to start '%s'\n", svc->name); |
| 357 | svc->pid = 0; |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | svc->time_started = gettime(); |
| 362 | svc->pid = pid; |
| 363 | svc->flags |= SVC_RUNNING; |
| 364 | |
Colin Cross | 3294bbb | 2010-04-19 17:11:33 -0700 | [diff] [blame] | 365 | if (properties_inited()) |
| 366 | notify_service_state(svc->name, "running"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame^] | 369 | /* 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] | 370 | 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] | 371 | { |
Mike Kasick | 7e36edd | 2012-02-06 10:32:13 -0500 | [diff] [blame] | 372 | /* The service is still SVC_RUNNING until its process exits, but if it has |
| 373 | * already exited it shoudn't attempt a restart yet. */ |
| 374 | svc->flags &= (~SVC_RESTARTING); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 375 | |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame^] | 376 | if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) { |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 377 | /* Hrm, an illegal flag. Default to SVC_DISABLED */ |
| 378 | how = SVC_DISABLED; |
| 379 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 380 | /* if the service has not yet started, prevent |
| 381 | * it from auto-starting with its class |
| 382 | */ |
Ken Sumrall | a286480 | 2011-10-26 16:56:00 -0700 | [diff] [blame] | 383 | if (how == SVC_RESET) { |
| 384 | svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET; |
| 385 | } else { |
| 386 | svc->flags |= how; |
| 387 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 388 | |
| 389 | if (svc->pid) { |
| 390 | NOTICE("service '%s' is being killed\n", svc->name); |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 391 | kill(-svc->pid, SIGKILL); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 392 | notify_service_state(svc->name, "stopping"); |
| 393 | } else { |
| 394 | notify_service_state(svc->name, "stopped"); |
| 395 | } |
| 396 | } |
| 397 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 398 | void service_reset(struct service *svc) |
| 399 | { |
| 400 | service_stop_or_reset(svc, SVC_RESET); |
| 401 | } |
| 402 | |
| 403 | void service_stop(struct service *svc) |
| 404 | { |
| 405 | service_stop_or_reset(svc, SVC_DISABLED); |
| 406 | } |
| 407 | |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame^] | 408 | void service_restart(struct service *svc) |
| 409 | { |
| 410 | if (svc->flags & SVC_RUNNING) { |
| 411 | /* Stop, wait, then start the service. */ |
| 412 | service_stop_or_reset(svc, SVC_RESTART); |
| 413 | } else if (!(svc->flags & SVC_RESTARTING)) { |
| 414 | /* Just start the service since it's not running. */ |
| 415 | service_start(svc, NULL); |
| 416 | } /* else: Service is restarting anyways. */ |
| 417 | } |
| 418 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 419 | void property_changed(const char *name, const char *value) |
| 420 | { |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 421 | if (property_triggers_enabled) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 422 | queue_property_triggers(name, value); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 423 | } |
| 424 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 425 | static void restart_service_if_needed(struct service *svc) |
| 426 | { |
| 427 | time_t next_start_time = svc->time_started + 5; |
| 428 | |
| 429 | if (next_start_time <= gettime()) { |
| 430 | svc->flags &= (~SVC_RESTARTING); |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 431 | service_start(svc, NULL); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | |
| 435 | if ((next_start_time < process_needs_restart) || |
| 436 | (process_needs_restart == 0)) { |
| 437 | process_needs_restart = next_start_time; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | static void restart_processes() |
| 442 | { |
| 443 | process_needs_restart = 0; |
| 444 | service_for_each_flags(SVC_RESTARTING, |
| 445 | restart_service_if_needed); |
| 446 | } |
| 447 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 448 | static void msg_start(const char *name) |
| 449 | { |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 450 | struct service *svc; |
| 451 | char *tmp = NULL; |
| 452 | char *args = NULL; |
| 453 | |
| 454 | if (!strchr(name, ':')) |
| 455 | svc = service_find_by_name(name); |
| 456 | else { |
| 457 | tmp = strdup(name); |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 458 | args = strchr(tmp, ':'); |
| 459 | *args = '\0'; |
| 460 | args++; |
| 461 | |
| 462 | svc = service_find_by_name(tmp); |
| 463 | } |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 464 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 465 | if (svc) { |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 466 | service_start(svc, args); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 467 | } else { |
| 468 | ERROR("no such service '%s'\n", name); |
| 469 | } |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 470 | if (tmp) |
| 471 | free(tmp); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | static void msg_stop(const char *name) |
| 475 | { |
| 476 | struct service *svc = service_find_by_name(name); |
| 477 | |
| 478 | if (svc) { |
| 479 | service_stop(svc); |
| 480 | } else { |
Dima Zavin | 770354d | 2009-05-05 18:33:07 -0700 | [diff] [blame] | 481 | ERROR("no such service '%s'\n", name); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame^] | 485 | static void msg_restart(const char *name) |
| 486 | { |
| 487 | struct service *svc = service_find_by_name(name); |
| 488 | |
| 489 | if (svc) { |
| 490 | service_restart(svc); |
| 491 | } else { |
| 492 | ERROR("no such service '%s'\n", name); |
| 493 | } |
| 494 | } |
| 495 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 496 | void handle_control_message(const char *msg, const char *arg) |
| 497 | { |
| 498 | if (!strcmp(msg,"start")) { |
| 499 | msg_start(arg); |
| 500 | } else if (!strcmp(msg,"stop")) { |
| 501 | msg_stop(arg); |
Wink Saville | cfa0d84 | 2010-10-03 13:30:11 -0700 | [diff] [blame] | 502 | } else if (!strcmp(msg,"restart")) { |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame^] | 503 | msg_restart(arg); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 504 | } else { |
| 505 | ERROR("unknown control msg '%s'\n", msg); |
| 506 | } |
| 507 | } |
| 508 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 509 | static struct command *get_first_command(struct action *act) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 510 | { |
| 511 | struct listnode *node; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 512 | node = list_head(&act->commands); |
Dima Zavin | 3bea079 | 2011-08-26 13:59:18 -0700 | [diff] [blame] | 513 | if (!node || list_empty(&act->commands)) |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 514 | return NULL; |
| 515 | |
| 516 | return node_to_item(node, struct command, clist); |
| 517 | } |
| 518 | |
| 519 | static struct command *get_next_command(struct action *act, struct command *cmd) |
| 520 | { |
| 521 | struct listnode *node; |
| 522 | node = cmd->clist.next; |
| 523 | if (!node) |
| 524 | return NULL; |
| 525 | if (node == &act->commands) |
| 526 | return NULL; |
| 527 | |
| 528 | return node_to_item(node, struct command, clist); |
| 529 | } |
| 530 | |
| 531 | static int is_last_command(struct action *act, struct command *cmd) |
| 532 | { |
| 533 | return (list_tail(&act->commands) == &cmd->clist); |
| 534 | } |
| 535 | |
| 536 | void execute_one_command(void) |
| 537 | { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 538 | int ret; |
| 539 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 540 | if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) { |
| 541 | cur_action = action_remove_queue_head(); |
Colin Cross | ebd4613 | 2010-04-22 11:52:23 -0700 | [diff] [blame] | 542 | cur_command = NULL; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 543 | if (!cur_action) |
| 544 | return; |
| 545 | INFO("processing action %p (%s)\n", cur_action, cur_action->name); |
| 546 | cur_command = get_first_command(cur_action); |
| 547 | } else { |
| 548 | cur_command = get_next_command(cur_action, cur_command); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 549 | } |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 550 | |
| 551 | if (!cur_command) |
| 552 | return; |
| 553 | |
| 554 | ret = cur_command->func(cur_command->nargs, cur_command->args); |
| 555 | INFO("command '%s' r=%d\n", cur_command->args[0], ret); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 558 | 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] | 559 | { |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 560 | int ret; |
| 561 | INFO("wait for %s\n", coldboot_done); |
| 562 | ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT); |
| 563 | if (ret) |
| 564 | ERROR("Timed out waiting for %s\n", coldboot_done); |
| 565 | return ret; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 568 | static int keychord_init_action(int nargs, char **args) |
| 569 | { |
| 570 | keychord_init(); |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | static int console_init_action(int nargs, char **args) |
| 575 | { |
| 576 | int fd; |
| 577 | char tmp[PROP_VALUE_MAX]; |
| 578 | |
| 579 | if (console[0]) { |
| 580 | snprintf(tmp, sizeof(tmp), "/dev/%s", console); |
| 581 | console_name = strdup(tmp); |
| 582 | } |
| 583 | |
| 584 | fd = open(console_name, O_RDWR); |
| 585 | if (fd >= 0) |
| 586 | have_console = 1; |
| 587 | close(fd); |
| 588 | |
| 589 | if( load_565rle_image(INIT_IMAGE_FILE) ) { |
| 590 | fd = open("/dev/tty0", O_WRONLY); |
| 591 | if (fd >= 0) { |
| 592 | const char *msg; |
| 593 | msg = "\n" |
| 594 | "\n" |
| 595 | "\n" |
| 596 | "\n" |
| 597 | "\n" |
| 598 | "\n" |
| 599 | "\n" // console is 40 cols x 30 lines |
| 600 | "\n" |
| 601 | "\n" |
| 602 | "\n" |
| 603 | "\n" |
| 604 | "\n" |
| 605 | "\n" |
| 606 | "\n" |
| 607 | " A N D R O I D "; |
| 608 | write(fd, msg, strlen(msg)); |
| 609 | close(fd); |
| 610 | } |
| 611 | } |
| 612 | return 0; |
| 613 | } |
| 614 | |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 615 | static void import_kernel_nv(char *name, int for_emulator) |
| 616 | { |
| 617 | char *value = strchr(name, '='); |
| 618 | int name_len = strlen(name); |
| 619 | |
| 620 | if (value == 0) return; |
| 621 | *value++ = 0; |
| 622 | if (name_len == 0) return; |
| 623 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 624 | #ifdef HAVE_SELINUX |
| 625 | if (!strcmp(name,"enforcing")) { |
| 626 | selinux_enforcing = atoi(value); |
| 627 | } else if (!strcmp(name,"selinux")) { |
| 628 | selinux_enabled = atoi(value); |
| 629 | } |
| 630 | #endif |
| 631 | |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 632 | if (for_emulator) { |
| 633 | /* in the emulator, export any kernel option with the |
| 634 | * ro.kernel. prefix */ |
| 635 | char buff[PROP_NAME_MAX]; |
| 636 | int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name ); |
| 637 | |
| 638 | if (len < (int)sizeof(buff)) |
| 639 | property_set( buff, value ); |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | if (!strcmp(name,"qemu")) { |
| 644 | strlcpy(qemu, value, sizeof(qemu)); |
| 645 | } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) { |
| 646 | const char *boot_prop_name = name + 12; |
| 647 | char prop[PROP_NAME_MAX]; |
| 648 | int cnt; |
| 649 | |
| 650 | cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name); |
| 651 | if (cnt < PROP_NAME_MAX) |
| 652 | property_set(prop, value); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | static void export_kernel_boot_props(void) |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 657 | { |
| 658 | char tmp[PROP_VALUE_MAX]; |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 659 | const char *pval; |
| 660 | unsigned i; |
| 661 | struct { |
| 662 | const char *src_prop; |
| 663 | const char *dest_prop; |
| 664 | const char *def_val; |
| 665 | } prop_map[] = { |
| 666 | { "ro.boot.serialno", "ro.serialno", "", }, |
| 667 | { "ro.boot.mode", "ro.bootmode", "unknown", }, |
| 668 | { "ro.boot.baseband", "ro.baseband", "unknown", }, |
| 669 | { "ro.boot.carrier", "ro.carrier", "unknown", }, |
| 670 | { "ro.boot.bootloader", "ro.bootloader", "unknown", }, |
| 671 | }; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 672 | |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 673 | for (i = 0; i < ARRAY_SIZE(prop_map); i++) { |
| 674 | pval = property_get(prop_map[i].src_prop); |
| 675 | property_set(prop_map[i].dest_prop, pval ?: prop_map[i].def_val); |
| 676 | } |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 677 | |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 678 | pval = property_get("ro.boot.console"); |
| 679 | if (pval) |
| 680 | strlcpy(console, pval, sizeof(console)); |
| 681 | |
| 682 | /* save a copy for init's usage during boot */ |
| 683 | strlcpy(bootmode, property_get("ro.bootmode"), sizeof(bootmode)); |
| 684 | |
| 685 | /* if this was given on kernel command line, override what we read |
| 686 | * before (e.g. from /proc/cpuinfo), if anything */ |
| 687 | pval = property_get("ro.boot.hardware"); |
| 688 | if (pval) |
| 689 | strlcpy(hardware, pval, sizeof(hardware)); |
| 690 | property_set("ro.hardware", hardware); |
| 691 | |
| 692 | snprintf(tmp, PROP_VALUE_MAX, "%d", revision); |
| 693 | property_set("ro.revision", tmp); |
| 694 | |
| 695 | /* TODO: these are obsolete. We should delete them */ |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 696 | if (!strcmp(bootmode,"factory")) |
| 697 | property_set("ro.factorytest", "1"); |
| 698 | else if (!strcmp(bootmode,"factory2")) |
| 699 | property_set("ro.factorytest", "2"); |
| 700 | else |
| 701 | property_set("ro.factorytest", "0"); |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 702 | } |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 703 | |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 704 | static void process_kernel_cmdline(void) |
| 705 | { |
| 706 | /* don't expose the raw commandline to nonpriv processes */ |
| 707 | chmod("/proc/cmdline", 0440); |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 708 | |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 709 | /* first pass does the common stuff, and finds if we are in qemu. |
| 710 | * second pass is only necessary for qemu to export all kernel params |
| 711 | * as props. |
| 712 | */ |
| 713 | import_kernel_cmdline(0, import_kernel_nv); |
| 714 | if (qemu[0]) |
| 715 | import_kernel_cmdline(1, import_kernel_nv); |
| 716 | |
| 717 | /* now propogate the info given on command line to internal variables |
| 718 | * used by init as well as the current required properties |
| 719 | */ |
| 720 | export_kernel_boot_props(); |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | static int property_service_init_action(int nargs, char **args) |
| 724 | { |
| 725 | /* read any property files on system or data and |
| 726 | * fire up the property service. This must happen |
| 727 | * after the ro.foo properties are set above so |
| 728 | * that /data/local.prop cannot interfere with them. |
| 729 | */ |
| 730 | start_property_service(); |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | static int signal_init_action(int nargs, char **args) |
| 735 | { |
| 736 | signal_init(); |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | static int check_startup_action(int nargs, char **args) |
| 741 | { |
| 742 | /* make sure we actually have all the pieces we need */ |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 743 | if ((get_property_set_fd() < 0) || |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 744 | (get_signal_fd() < 0)) { |
| 745 | ERROR("init startup failure\n"); |
| 746 | exit(1); |
| 747 | } |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 748 | |
| 749 | /* signal that we hit this point */ |
| 750 | unlink("/dev/.booting"); |
| 751 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | static int queue_property_triggers_action(int nargs, char **args) |
| 756 | { |
| 757 | queue_all_property_triggers(); |
| 758 | /* enable property triggers */ |
| 759 | property_triggers_enabled = 1; |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | #if BOOTCHART |
| 764 | static int bootchart_init_action(int nargs, char **args) |
| 765 | { |
| 766 | bootchart_count = bootchart_init(); |
| 767 | if (bootchart_count < 0) { |
| 768 | ERROR("bootcharting init failure\n"); |
| 769 | } else if (bootchart_count > 0) { |
| 770 | NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS); |
| 771 | } else { |
| 772 | NOTICE("bootcharting ignored\n"); |
| 773 | } |
Carl-Emil Lagerstedt | 9ab8190 | 2011-01-14 09:35:30 +0100 | [diff] [blame] | 774 | |
| 775 | return 0; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 776 | } |
| 777 | #endif |
| 778 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 779 | #ifdef HAVE_SELINUX |
| 780 | void selinux_load_policy(void) |
| 781 | { |
| 782 | const char path_prefix[] = "/sepolicy"; |
| 783 | struct selinux_opt seopts[] = { |
| 784 | { SELABEL_OPT_PATH, "/file_contexts" } |
| 785 | }; |
| 786 | char path[PATH_MAX]; |
| 787 | int fd, rc, vers; |
| 788 | struct stat sb; |
| 789 | void *map; |
| 790 | |
| 791 | sehandle = NULL; |
| 792 | if (!selinux_enabled) { |
| 793 | INFO("SELinux: Disabled by command line option\n"); |
| 794 | return; |
| 795 | } |
| 796 | |
| 797 | mkdir(SELINUXMNT, 0755); |
| 798 | if (mount("selinuxfs", SELINUXMNT, "selinuxfs", 0, NULL)) { |
| 799 | if (errno == ENODEV) { |
| 800 | /* SELinux not enabled in kernel */ |
| 801 | return; |
| 802 | } |
| 803 | ERROR("SELinux: Could not mount selinuxfs: %s\n", |
| 804 | strerror(errno)); |
| 805 | return; |
| 806 | } |
| 807 | set_selinuxmnt(SELINUXMNT); |
| 808 | |
| 809 | vers = security_policyvers(); |
| 810 | if (vers <= 0) { |
| 811 | ERROR("SELinux: Unable to read policy version\n"); |
| 812 | return; |
| 813 | } |
| 814 | INFO("SELinux: Maximum supported policy version: %d\n", vers); |
| 815 | |
| 816 | snprintf(path, sizeof(path), "%s.%d", |
| 817 | path_prefix, vers); |
| 818 | fd = open(path, O_RDONLY); |
| 819 | while (fd < 0 && errno == ENOENT && --vers) { |
| 820 | snprintf(path, sizeof(path), "%s.%d", |
| 821 | path_prefix, vers); |
| 822 | fd = open(path, O_RDONLY); |
| 823 | } |
| 824 | if (fd < 0) { |
| 825 | ERROR("SELinux: Could not open %s: %s\n", |
| 826 | path, strerror(errno)); |
| 827 | return; |
| 828 | } |
| 829 | if (fstat(fd, &sb) < 0) { |
| 830 | ERROR("SELinux: Could not stat %s: %s\n", |
| 831 | path, strerror(errno)); |
| 832 | return; |
| 833 | } |
| 834 | map = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 835 | if (map == MAP_FAILED) { |
| 836 | ERROR("SELinux: Could not map %s: %s\n", |
| 837 | path, strerror(errno)); |
| 838 | return; |
| 839 | } |
| 840 | |
| 841 | rc = security_load_policy(map, sb.st_size); |
| 842 | if (rc < 0) { |
| 843 | ERROR("SELinux: Could not load policy: %s\n", |
| 844 | strerror(errno)); |
| 845 | return; |
| 846 | } |
| 847 | |
| 848 | rc = security_setenforce(selinux_enforcing); |
| 849 | if (rc < 0) { |
| 850 | ERROR("SELinux: Could not set enforcing mode to %s: %s\n", |
| 851 | selinux_enforcing ? "enforcing" : "permissive", strerror(errno)); |
| 852 | return; |
| 853 | } |
| 854 | |
| 855 | munmap(map, sb.st_size); |
| 856 | close(fd); |
| 857 | INFO("SELinux: Loaded policy from %s\n", path); |
| 858 | |
| 859 | sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1); |
| 860 | if (!sehandle) { |
| 861 | ERROR("SELinux: Could not load file_contexts: %s\n", |
| 862 | strerror(errno)); |
| 863 | return; |
| 864 | } |
| 865 | INFO("SELinux: Loaded file contexts from %s\n", seopts[0].value); |
| 866 | return; |
| 867 | } |
| 868 | #endif |
| 869 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 870 | int main(int argc, char **argv) |
| 871 | { |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 872 | int fd_count = 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 873 | struct pollfd ufds[4]; |
| 874 | char *tmpdev; |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 875 | char* debuggable; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 876 | char tmp[32]; |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 877 | int property_set_fd_init = 0; |
| 878 | int signal_fd_init = 0; |
| 879 | int keychord_fd_init = 0; |
Dima Zavin | d7634c9 | 2011-12-16 14:18:06 -0800 | [diff] [blame] | 880 | bool is_charger = false; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 881 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 882 | if (!strcmp(basename(argv[0]), "ueventd")) |
| 883 | return ueventd_main(argc, argv); |
| 884 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 885 | /* clear the umask */ |
| 886 | umask(0); |
| 887 | |
| 888 | /* Get the basic filesystem setup we need put |
| 889 | * together in the initramdisk on / and then we'll |
| 890 | * let the rc file figure out the rest. |
| 891 | */ |
| 892 | mkdir("/dev", 0755); |
| 893 | mkdir("/proc", 0755); |
| 894 | mkdir("/sys", 0755); |
| 895 | |
Nick Kralevich | 150f19e | 2010-06-22 16:35:43 -0700 | [diff] [blame] | 896 | mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 897 | mkdir("/dev/pts", 0755); |
| 898 | mkdir("/dev/socket", 0755); |
| 899 | mount("devpts", "/dev/pts", "devpts", 0, NULL); |
| 900 | mount("proc", "/proc", "proc", 0, NULL); |
| 901 | mount("sysfs", "/sys", "sysfs", 0, NULL); |
| 902 | |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 903 | /* indicate that booting is in progress to background fw loaders, etc */ |
| 904 | close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000)); |
| 905 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 906 | /* We must have some place other than / to create the |
| 907 | * device nodes for kmsg and null, otherwise we won't |
| 908 | * be able to remount / read-only later on. |
| 909 | * Now that tmpfs is mounted on /dev, we can actually |
| 910 | * talk to the outside world. |
| 911 | */ |
| 912 | open_devnull_stdio(); |
Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 913 | klog_init(); |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 914 | property_init(); |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 915 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 916 | get_hardware_name(hardware, &revision); |
Dima Zavin | d7634c9 | 2011-12-16 14:18:06 -0800 | [diff] [blame] | 917 | |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 918 | process_kernel_cmdline(); |
| 919 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 920 | #ifdef HAVE_SELINUX |
| 921 | INFO("loading selinux policy\n"); |
| 922 | selinux_load_policy(); |
| 923 | #endif |
| 924 | |
Dima Zavin | d7634c9 | 2011-12-16 14:18:06 -0800 | [diff] [blame] | 925 | is_charger = !strcmp(bootmode, "charger"); |
| 926 | |
| 927 | INFO("property init\n"); |
Dima Zavin | 5511c84 | 2011-12-19 11:21:32 -0800 | [diff] [blame] | 928 | if (!is_charger) |
| 929 | property_load_boot_defaults(); |
Dima Zavin | d7634c9 | 2011-12-16 14:18:06 -0800 | [diff] [blame] | 930 | |
| 931 | INFO("reading config file\n"); |
| 932 | init_parse_config_file("/init.rc"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 933 | |
| 934 | 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] | 935 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 936 | queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done"); |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 937 | queue_builtin_action(keychord_init_action, "keychord_init"); |
| 938 | queue_builtin_action(console_init_action, "console_init"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 939 | |
Dima Zavin | ca47cef | 2011-08-24 15:28:23 -0700 | [diff] [blame] | 940 | /* execute all the boot actions to get us started */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 941 | action_for_each_trigger("init", action_add_queue_tail); |
Dima Zavin | ca47cef | 2011-08-24 15:28:23 -0700 | [diff] [blame] | 942 | |
| 943 | /* skip mounting filesystems in charger mode */ |
Dima Zavin | d7634c9 | 2011-12-16 14:18:06 -0800 | [diff] [blame] | 944 | if (!is_charger) { |
Dima Zavin | ca47cef | 2011-08-24 15:28:23 -0700 | [diff] [blame] | 945 | action_for_each_trigger("early-fs", action_add_queue_tail); |
| 946 | action_for_each_trigger("fs", action_add_queue_tail); |
| 947 | action_for_each_trigger("post-fs", action_add_queue_tail); |
| 948 | action_for_each_trigger("post-fs-data", action_add_queue_tail); |
| 949 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 950 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 951 | queue_builtin_action(property_service_init_action, "property_service_init"); |
| 952 | queue_builtin_action(signal_init_action, "signal_init"); |
| 953 | queue_builtin_action(check_startup_action, "check_startup"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 954 | |
Dima Zavin | d7634c9 | 2011-12-16 14:18:06 -0800 | [diff] [blame] | 955 | if (is_charger) { |
Dima Zavin | ca47cef | 2011-08-24 15:28:23 -0700 | [diff] [blame] | 956 | action_for_each_trigger("charger", action_add_queue_tail); |
| 957 | } else { |
| 958 | action_for_each_trigger("early-boot", action_add_queue_tail); |
| 959 | action_for_each_trigger("boot", action_add_queue_tail); |
| 960 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 961 | |
| 962 | /* run all property triggers based on current state of the properties */ |
Chris Dearman | 469b7b2 | 2012-03-01 15:29:20 -0800 | [diff] [blame] | 963 | 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] | 964 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 965 | |
| 966 | #if BOOTCHART |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 967 | queue_builtin_action(bootchart_init_action, "bootchart_init"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 968 | #endif |
| 969 | |
| 970 | for(;;) { |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 971 | int nr, i, timeout = -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 972 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 973 | execute_one_command(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 974 | restart_processes(); |
| 975 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 976 | if (!property_set_fd_init && get_property_set_fd() > 0) { |
| 977 | ufds[fd_count].fd = get_property_set_fd(); |
| 978 | ufds[fd_count].events = POLLIN; |
| 979 | ufds[fd_count].revents = 0; |
| 980 | fd_count++; |
| 981 | property_set_fd_init = 1; |
| 982 | } |
| 983 | if (!signal_fd_init && get_signal_fd() > 0) { |
| 984 | ufds[fd_count].fd = get_signal_fd(); |
| 985 | ufds[fd_count].events = POLLIN; |
| 986 | ufds[fd_count].revents = 0; |
| 987 | fd_count++; |
| 988 | signal_fd_init = 1; |
| 989 | } |
| 990 | if (!keychord_fd_init && get_keychord_fd() > 0) { |
| 991 | ufds[fd_count].fd = get_keychord_fd(); |
| 992 | ufds[fd_count].events = POLLIN; |
| 993 | ufds[fd_count].revents = 0; |
| 994 | fd_count++; |
| 995 | keychord_fd_init = 1; |
| 996 | } |
| 997 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 998 | if (process_needs_restart) { |
| 999 | timeout = (process_needs_restart - gettime()) * 1000; |
| 1000 | if (timeout < 0) |
| 1001 | timeout = 0; |
| 1002 | } |
| 1003 | |
Colin Cross | ebd4613 | 2010-04-22 11:52:23 -0700 | [diff] [blame] | 1004 | if (!action_queue_empty() || cur_action) |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 1005 | timeout = 0; |
| 1006 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1007 | #if BOOTCHART |
| 1008 | if (bootchart_count > 0) { |
| 1009 | if (timeout < 0 || timeout > BOOTCHART_POLLING_MS) |
| 1010 | timeout = BOOTCHART_POLLING_MS; |
| 1011 | if (bootchart_step() < 0 || --bootchart_count == 0) { |
| 1012 | bootchart_finish(); |
| 1013 | bootchart_count = 0; |
| 1014 | } |
| 1015 | } |
| 1016 | #endif |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 1017 | |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 1018 | nr = poll(ufds, fd_count, timeout); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1019 | if (nr <= 0) |
| 1020 | continue; |
| 1021 | |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 1022 | for (i = 0; i < fd_count; i++) { |
| 1023 | if (ufds[i].revents == POLLIN) { |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 1024 | if (ufds[i].fd == get_property_set_fd()) |
Colin Cross | ebc6ff1 | 2010-04-13 19:52:01 -0700 | [diff] [blame] | 1025 | handle_property_set_fd(); |
| 1026 | else if (ufds[i].fd == get_keychord_fd()) |
| 1027 | handle_keychord(); |
| 1028 | else if (ufds[i].fd == get_signal_fd()) |
| 1029 | handle_signal(); |
| 1030 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1031 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | return 0; |
| 1035 | } |