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