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