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