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