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> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 34 | |
| 35 | #include <cutils/sockets.h> |
San Mehat | 4e221f0 | 2010-02-25 14:19:50 -0800 | [diff] [blame] | 36 | #include <cutils/iosched_policy.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 37 | #include <termios.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 38 | |
| 39 | #include <sys/system_properties.h> |
| 40 | |
| 41 | #include "devices.h" |
| 42 | #include "init.h" |
| 43 | #include "property_service.h" |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 44 | #include "bootchart.h" |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 45 | #include "signal_handler.h" |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 46 | #include "keychords.h" |
Colin Cross | ca7648d | 2010-04-13 19:29:51 -0700 | [diff] [blame] | 47 | #include "parser.h" |
Colin Cross | 3899e9f | 2010-04-13 20:35:46 -0700 | [diff] [blame^] | 48 | #include "util.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 49 | |
| 50 | static int property_triggers_enabled = 0; |
| 51 | |
| 52 | #if BOOTCHART |
| 53 | static int bootchart_count; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 54 | #endif |
| 55 | |
| 56 | static char console[32]; |
| 57 | static char serialno[32]; |
| 58 | static char bootmode[32]; |
| 59 | static char baseband[32]; |
| 60 | static char carrier[32]; |
| 61 | static char bootloader[32]; |
| 62 | static char hardware[32]; |
| 63 | static unsigned revision = 0; |
| 64 | static char qemu[32]; |
| 65 | |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 66 | 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] | 67 | { |
| 68 | char pname[PROP_NAME_MAX]; |
| 69 | int len = strlen(name); |
| 70 | if ((len + 10) > PROP_NAME_MAX) |
| 71 | return; |
| 72 | snprintf(pname, sizeof(pname), "init.svc.%s", name); |
| 73 | property_set(pname, state); |
| 74 | } |
| 75 | |
| 76 | static int have_console; |
| 77 | static char *console_name = "/dev/console"; |
| 78 | static time_t process_needs_restart; |
| 79 | |
| 80 | static const char *ENV[32]; |
| 81 | |
| 82 | /* add_environment - add "key=value" to the current environment */ |
| 83 | int add_environment(const char *key, const char *val) |
| 84 | { |
| 85 | int n; |
| 86 | |
| 87 | for (n = 0; n < 31; n++) { |
| 88 | if (!ENV[n]) { |
| 89 | size_t len = strlen(key) + strlen(val) + 2; |
| 90 | char *entry = malloc(len); |
| 91 | snprintf(entry, len, "%s=%s", key, val); |
| 92 | ENV[n] = entry; |
| 93 | return 0; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return 1; |
| 98 | } |
| 99 | |
| 100 | static void zap_stdio(void) |
| 101 | { |
| 102 | int fd; |
| 103 | fd = open("/dev/null", O_RDWR); |
| 104 | dup2(fd, 0); |
| 105 | dup2(fd, 1); |
| 106 | dup2(fd, 2); |
| 107 | close(fd); |
| 108 | } |
| 109 | |
| 110 | static void open_console() |
| 111 | { |
| 112 | int fd; |
| 113 | if ((fd = open(console_name, O_RDWR)) < 0) { |
| 114 | fd = open("/dev/null", O_RDWR); |
| 115 | } |
| 116 | dup2(fd, 0); |
| 117 | dup2(fd, 1); |
| 118 | dup2(fd, 2); |
| 119 | close(fd); |
| 120 | } |
| 121 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 122 | static void publish_socket(const char *name, int fd) |
| 123 | { |
| 124 | char key[64] = ANDROID_SOCKET_ENV_PREFIX; |
| 125 | char val[64]; |
| 126 | |
| 127 | strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, |
| 128 | name, |
| 129 | sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); |
| 130 | snprintf(val, sizeof(val), "%d", fd); |
| 131 | add_environment(key, val); |
| 132 | |
| 133 | /* make sure we don't close-on-exec */ |
| 134 | fcntl(fd, F_SETFD, 0); |
| 135 | } |
| 136 | |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 137 | 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] | 138 | { |
| 139 | struct stat s; |
| 140 | pid_t pid; |
| 141 | int needs_console; |
| 142 | int n; |
| 143 | |
| 144 | /* starting a service removes it from the disabled |
| 145 | * state and immediately takes it out of the restarting |
| 146 | * state if it was in there |
| 147 | */ |
| 148 | svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING)); |
| 149 | svc->time_started = 0; |
| 150 | |
| 151 | /* running processes require no additional work -- if |
| 152 | * they're in the process of exiting, we've ensured |
| 153 | * that they will immediately restart on exit, unless |
| 154 | * they are ONESHOT |
| 155 | */ |
| 156 | if (svc->flags & SVC_RUNNING) { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0; |
| 161 | if (needs_console && (!have_console)) { |
| 162 | ERROR("service '%s' requires console\n", svc->name); |
| 163 | svc->flags |= SVC_DISABLED; |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | if (stat(svc->args[0], &s) != 0) { |
| 168 | ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name); |
| 169 | svc->flags |= SVC_DISABLED; |
| 170 | return; |
| 171 | } |
| 172 | |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 173 | if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) { |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 174 | ERROR("service '%s' must be one-shot to use dynamic args, disabling\n", |
| 175 | svc->args[0]); |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 176 | svc->flags |= SVC_DISABLED; |
| 177 | return; |
| 178 | } |
| 179 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 180 | NOTICE("starting '%s'\n", svc->name); |
| 181 | |
| 182 | pid = fork(); |
| 183 | |
| 184 | if (pid == 0) { |
| 185 | struct socketinfo *si; |
| 186 | struct svcenvinfo *ei; |
| 187 | char tmp[32]; |
| 188 | int fd, sz; |
| 189 | |
| 190 | get_property_workspace(&fd, &sz); |
| 191 | sprintf(tmp, "%d,%d", dup(fd), sz); |
| 192 | add_environment("ANDROID_PROPERTY_WORKSPACE", tmp); |
| 193 | |
| 194 | for (ei = svc->envvars; ei; ei = ei->next) |
| 195 | add_environment(ei->name, ei->value); |
| 196 | |
| 197 | for (si = svc->sockets; si; si = si->next) { |
| 198 | int s = create_socket(si->name, |
| 199 | !strcmp(si->type, "dgram") ? |
| 200 | SOCK_DGRAM : SOCK_STREAM, |
| 201 | si->perm, si->uid, si->gid); |
| 202 | if (s >= 0) { |
| 203 | publish_socket(si->name, s); |
| 204 | } |
| 205 | } |
| 206 | |
San Mehat | 4e221f0 | 2010-02-25 14:19:50 -0800 | [diff] [blame] | 207 | if (svc->ioprio_class != IoSchedClass_NONE) { |
| 208 | if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) { |
| 209 | ERROR("Failed to set pid %d ioprio = %d,%d: %s\n", |
| 210 | getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno)); |
| 211 | } |
| 212 | } |
| 213 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 214 | if (needs_console) { |
| 215 | setsid(); |
| 216 | open_console(); |
| 217 | } else { |
| 218 | zap_stdio(); |
| 219 | } |
| 220 | |
| 221 | #if 0 |
| 222 | for (n = 0; svc->args[n]; n++) { |
| 223 | INFO("args[%d] = '%s'\n", n, svc->args[n]); |
| 224 | } |
| 225 | for (n = 0; ENV[n]; n++) { |
| 226 | INFO("env[%d] = '%s'\n", n, ENV[n]); |
| 227 | } |
| 228 | #endif |
| 229 | |
| 230 | setpgid(0, getpid()); |
| 231 | |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 232 | /* as requested, set our gid, supplemental gids, and uid */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 233 | if (svc->gid) { |
| 234 | setgid(svc->gid); |
| 235 | } |
| 236 | if (svc->nr_supp_gids) { |
| 237 | setgroups(svc->nr_supp_gids, svc->supp_gids); |
| 238 | } |
| 239 | if (svc->uid) { |
| 240 | setuid(svc->uid); |
| 241 | } |
| 242 | |
San Mehat | 8ad1568 | 2009-05-20 08:50:40 -0700 | [diff] [blame] | 243 | if (!dynamic_args) { |
| 244 | if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) { |
| 245 | ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno)); |
| 246 | } |
| 247 | } else { |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 248 | char *arg_ptrs[SVC_MAXARGS+1]; |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 249 | int arg_idx = svc->nargs; |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 250 | char *tmp = strdup(dynamic_args); |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 251 | char *next = tmp; |
| 252 | char *bword; |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 253 | |
| 254 | /* Copy the static arguments */ |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 255 | memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *))); |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 256 | |
San Mehat | d4cdd13 | 2009-05-20 09:52:16 -0700 | [diff] [blame] | 257 | while((bword = strsep(&next, " "))) { |
| 258 | arg_ptrs[arg_idx++] = bword; |
| 259 | if (arg_idx == SVC_MAXARGS) |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 260 | break; |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 261 | } |
| 262 | arg_ptrs[arg_idx] = '\0'; |
| 263 | execve(svc->args[0], (char**) arg_ptrs, (char**) ENV); |
Ivan Djelic | 165de92 | 2008-11-23 22:26:39 +0100 | [diff] [blame] | 264 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 265 | _exit(127); |
| 266 | } |
| 267 | |
| 268 | if (pid < 0) { |
| 269 | ERROR("failed to start '%s'\n", svc->name); |
| 270 | svc->pid = 0; |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | svc->time_started = gettime(); |
| 275 | svc->pid = pid; |
| 276 | svc->flags |= SVC_RUNNING; |
| 277 | |
| 278 | notify_service_state(svc->name, "running"); |
| 279 | } |
| 280 | |
| 281 | void service_stop(struct service *svc) |
| 282 | { |
| 283 | /* we are no longer running, nor should we |
| 284 | * attempt to restart |
| 285 | */ |
| 286 | svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING)); |
| 287 | |
| 288 | /* if the service has not yet started, prevent |
| 289 | * it from auto-starting with its class |
| 290 | */ |
| 291 | svc->flags |= SVC_DISABLED; |
| 292 | |
| 293 | if (svc->pid) { |
| 294 | NOTICE("service '%s' is being killed\n", svc->name); |
| 295 | kill(-svc->pid, SIGTERM); |
| 296 | notify_service_state(svc->name, "stopping"); |
| 297 | } else { |
| 298 | notify_service_state(svc->name, "stopped"); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | void property_changed(const char *name, const char *value) |
| 303 | { |
| 304 | if (property_triggers_enabled) { |
| 305 | queue_property_triggers(name, value); |
| 306 | drain_action_queue(); |
| 307 | } |
| 308 | } |
| 309 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 310 | static void restart_service_if_needed(struct service *svc) |
| 311 | { |
| 312 | time_t next_start_time = svc->time_started + 5; |
| 313 | |
| 314 | if (next_start_time <= gettime()) { |
| 315 | svc->flags &= (~SVC_RESTARTING); |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 316 | service_start(svc, NULL); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 317 | return; |
| 318 | } |
| 319 | |
| 320 | if ((next_start_time < process_needs_restart) || |
| 321 | (process_needs_restart == 0)) { |
| 322 | process_needs_restart = next_start_time; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | static void restart_processes() |
| 327 | { |
| 328 | process_needs_restart = 0; |
| 329 | service_for_each_flags(SVC_RESTARTING, |
| 330 | restart_service_if_needed); |
| 331 | } |
| 332 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 333 | static void msg_start(const char *name) |
| 334 | { |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 335 | struct service *svc; |
| 336 | char *tmp = NULL; |
| 337 | char *args = NULL; |
| 338 | |
| 339 | if (!strchr(name, ':')) |
| 340 | svc = service_find_by_name(name); |
| 341 | else { |
| 342 | tmp = strdup(name); |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 343 | args = strchr(tmp, ':'); |
| 344 | *args = '\0'; |
| 345 | args++; |
| 346 | |
| 347 | svc = service_find_by_name(tmp); |
| 348 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 349 | |
| 350 | if (svc) { |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 351 | service_start(svc, args); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 352 | } else { |
| 353 | ERROR("no such service '%s'\n", name); |
| 354 | } |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 355 | if (tmp) |
| 356 | free(tmp); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static void msg_stop(const char *name) |
| 360 | { |
| 361 | struct service *svc = service_find_by_name(name); |
| 362 | |
| 363 | if (svc) { |
| 364 | service_stop(svc); |
| 365 | } else { |
Dima Zavin | 770354d | 2009-05-05 18:33:07 -0700 | [diff] [blame] | 366 | ERROR("no such service '%s'\n", name); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | void handle_control_message(const char *msg, const char *arg) |
| 371 | { |
| 372 | if (!strcmp(msg,"start")) { |
| 373 | msg_start(arg); |
| 374 | } else if (!strcmp(msg,"stop")) { |
| 375 | msg_stop(arg); |
| 376 | } else { |
| 377 | ERROR("unknown control msg '%s'\n", msg); |
| 378 | } |
| 379 | } |
| 380 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 381 | static void import_kernel_nv(char *name, int in_qemu) |
| 382 | { |
| 383 | char *value = strchr(name, '='); |
| 384 | |
| 385 | if (value == 0) return; |
| 386 | *value++ = 0; |
| 387 | if (*name == 0) return; |
| 388 | |
| 389 | if (!in_qemu) |
| 390 | { |
| 391 | /* on a real device, white-list the kernel options */ |
| 392 | if (!strcmp(name,"qemu")) { |
| 393 | strlcpy(qemu, value, sizeof(qemu)); |
| 394 | } else if (!strcmp(name,"androidboot.console")) { |
| 395 | strlcpy(console, value, sizeof(console)); |
| 396 | } else if (!strcmp(name,"androidboot.mode")) { |
| 397 | strlcpy(bootmode, value, sizeof(bootmode)); |
| 398 | } else if (!strcmp(name,"androidboot.serialno")) { |
| 399 | strlcpy(serialno, value, sizeof(serialno)); |
| 400 | } else if (!strcmp(name,"androidboot.baseband")) { |
| 401 | strlcpy(baseband, value, sizeof(baseband)); |
| 402 | } else if (!strcmp(name,"androidboot.carrier")) { |
| 403 | strlcpy(carrier, value, sizeof(carrier)); |
| 404 | } else if (!strcmp(name,"androidboot.bootloader")) { |
| 405 | strlcpy(bootloader, value, sizeof(bootloader)); |
| 406 | } else if (!strcmp(name,"androidboot.hardware")) { |
| 407 | strlcpy(hardware, value, sizeof(hardware)); |
| 408 | } else { |
| 409 | qemu_cmdline(name, value); |
| 410 | } |
| 411 | } else { |
| 412 | /* in the emulator, export any kernel option with the |
| 413 | * ro.kernel. prefix */ |
| 414 | char buff[32]; |
| 415 | int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name ); |
| 416 | if (len < (int)sizeof(buff)) { |
| 417 | property_set( buff, value ); |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | static void import_kernel_cmdline(int in_qemu) |
| 423 | { |
| 424 | char cmdline[1024]; |
| 425 | char *ptr; |
| 426 | int fd; |
| 427 | |
| 428 | fd = open("/proc/cmdline", O_RDONLY); |
| 429 | if (fd >= 0) { |
| 430 | int n = read(fd, cmdline, 1023); |
| 431 | if (n < 0) n = 0; |
| 432 | |
| 433 | /* get rid of trailing newline, it happens */ |
| 434 | if (n > 0 && cmdline[n-1] == '\n') n--; |
| 435 | |
| 436 | cmdline[n] = 0; |
| 437 | close(fd); |
| 438 | } else { |
| 439 | cmdline[0] = 0; |
| 440 | } |
| 441 | |
| 442 | ptr = cmdline; |
| 443 | while (ptr && *ptr) { |
| 444 | char *x = strchr(ptr, ' '); |
| 445 | if (x != 0) *x++ = 0; |
| 446 | import_kernel_nv(ptr, in_qemu); |
| 447 | ptr = x; |
| 448 | } |
| 449 | |
| 450 | /* don't expose the raw commandline to nonpriv processes */ |
| 451 | chmod("/proc/cmdline", 0440); |
| 452 | } |
| 453 | |
| 454 | static void get_hardware_name(void) |
| 455 | { |
| 456 | char data[1024]; |
| 457 | int fd, n; |
| 458 | char *x, *hw, *rev; |
| 459 | |
| 460 | /* Hardware string was provided on kernel command line */ |
| 461 | if (hardware[0]) |
| 462 | return; |
| 463 | |
| 464 | fd = open("/proc/cpuinfo", O_RDONLY); |
| 465 | if (fd < 0) return; |
| 466 | |
| 467 | n = read(fd, data, 1023); |
| 468 | close(fd); |
| 469 | if (n < 0) return; |
| 470 | |
| 471 | data[n] = 0; |
| 472 | hw = strstr(data, "\nHardware"); |
| 473 | rev = strstr(data, "\nRevision"); |
| 474 | |
| 475 | if (hw) { |
| 476 | x = strstr(hw, ": "); |
| 477 | if (x) { |
| 478 | x += 2; |
| 479 | n = 0; |
| 480 | while (*x && !isspace(*x)) { |
| 481 | hardware[n++] = tolower(*x); |
| 482 | x++; |
| 483 | if (n == 31) break; |
| 484 | } |
| 485 | hardware[n] = 0; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | if (rev) { |
| 490 | x = strstr(rev, ": "); |
| 491 | if (x) { |
| 492 | revision = strtoul(x + 2, 0, 16); |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
Jay Freeman (saurik) | 11e1c42 | 2008-11-17 06:35:08 +0000 | [diff] [blame] | 497 | void drain_action_queue(void) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 498 | { |
| 499 | struct listnode *node; |
| 500 | struct command *cmd; |
| 501 | struct action *act; |
| 502 | int ret; |
| 503 | |
| 504 | while ((act = action_remove_queue_head())) { |
| 505 | INFO("processing action %p (%s)\n", act, act->name); |
| 506 | list_for_each(node, &act->commands) { |
| 507 | cmd = node_to_item(node, struct command, clist); |
| 508 | ret = cmd->func(cmd->nargs, cmd->args); |
| 509 | INFO("command '%s' r=%d\n", cmd->args[0], ret); |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | void open_devnull_stdio(void) |
| 515 | { |
| 516 | int fd; |
| 517 | static const char *name = "/dev/__null__"; |
| 518 | if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) { |
| 519 | fd = open(name, O_RDWR); |
| 520 | unlink(name); |
| 521 | if (fd >= 0) { |
| 522 | dup2(fd, 0); |
| 523 | dup2(fd, 1); |
| 524 | dup2(fd, 2); |
| 525 | if (fd > 2) { |
| 526 | close(fd); |
| 527 | } |
| 528 | return; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | exit(1); |
| 533 | } |
| 534 | |
| 535 | int main(int argc, char **argv) |
| 536 | { |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 537 | int fd_count; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 538 | int s[2]; |
| 539 | int fd; |
| 540 | struct sigaction act; |
| 541 | char tmp[PROP_VALUE_MAX]; |
| 542 | struct pollfd ufds[4]; |
| 543 | char *tmpdev; |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 544 | char* debuggable; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 545 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 546 | /* clear the umask */ |
| 547 | umask(0); |
| 548 | |
| 549 | /* Get the basic filesystem setup we need put |
| 550 | * together in the initramdisk on / and then we'll |
| 551 | * let the rc file figure out the rest. |
| 552 | */ |
| 553 | mkdir("/dev", 0755); |
| 554 | mkdir("/proc", 0755); |
| 555 | mkdir("/sys", 0755); |
| 556 | |
| 557 | mount("tmpfs", "/dev", "tmpfs", 0, "mode=0755"); |
| 558 | mkdir("/dev/pts", 0755); |
| 559 | mkdir("/dev/socket", 0755); |
| 560 | mount("devpts", "/dev/pts", "devpts", 0, NULL); |
| 561 | mount("proc", "/proc", "proc", 0, NULL); |
| 562 | mount("sysfs", "/sys", "sysfs", 0, NULL); |
| 563 | |
| 564 | /* We must have some place other than / to create the |
| 565 | * device nodes for kmsg and null, otherwise we won't |
| 566 | * be able to remount / read-only later on. |
| 567 | * Now that tmpfs is mounted on /dev, we can actually |
| 568 | * talk to the outside world. |
| 569 | */ |
| 570 | open_devnull_stdio(); |
| 571 | log_init(); |
| 572 | |
| 573 | INFO("reading config file\n"); |
| 574 | parse_config_file("/init.rc"); |
| 575 | |
| 576 | /* pull the kernel commandline and ramdisk properties file in */ |
| 577 | qemu_init(); |
| 578 | import_kernel_cmdline(0); |
| 579 | |
| 580 | get_hardware_name(); |
| 581 | snprintf(tmp, sizeof(tmp), "/init.%s.rc", hardware); |
| 582 | parse_config_file(tmp); |
| 583 | |
| 584 | action_for_each_trigger("early-init", action_add_queue_tail); |
| 585 | drain_action_queue(); |
| 586 | |
| 587 | INFO("device init\n"); |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 588 | device_init(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 589 | |
| 590 | property_init(); |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 591 | |
| 592 | // only listen for keychords if ro.debuggable is true |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 593 | keychord_init(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 594 | |
| 595 | if (console[0]) { |
| 596 | snprintf(tmp, sizeof(tmp), "/dev/%s", console); |
| 597 | console_name = strdup(tmp); |
| 598 | } |
| 599 | |
| 600 | fd = open(console_name, O_RDWR); |
| 601 | if (fd >= 0) |
| 602 | have_console = 1; |
| 603 | close(fd); |
| 604 | |
| 605 | if( load_565rle_image(INIT_IMAGE_FILE) ) { |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 606 | fd = open("/dev/tty0", O_WRONLY); |
| 607 | if (fd >= 0) { |
| 608 | const char *msg; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 609 | msg = "\n" |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 610 | "\n" |
| 611 | "\n" |
| 612 | "\n" |
| 613 | "\n" |
| 614 | "\n" |
| 615 | "\n" // console is 40 cols x 30 lines |
| 616 | "\n" |
| 617 | "\n" |
| 618 | "\n" |
| 619 | "\n" |
| 620 | "\n" |
| 621 | "\n" |
| 622 | "\n" |
| 623 | " A N D R O I D "; |
| 624 | write(fd, msg, strlen(msg)); |
| 625 | close(fd); |
| 626 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | if (qemu[0]) |
| 630 | import_kernel_cmdline(1); |
| 631 | |
| 632 | if (!strcmp(bootmode,"factory")) |
| 633 | property_set("ro.factorytest", "1"); |
| 634 | else if (!strcmp(bootmode,"factory2")) |
| 635 | property_set("ro.factorytest", "2"); |
| 636 | else |
| 637 | property_set("ro.factorytest", "0"); |
| 638 | |
| 639 | property_set("ro.serialno", serialno[0] ? serialno : ""); |
| 640 | property_set("ro.bootmode", bootmode[0] ? bootmode : "unknown"); |
| 641 | property_set("ro.baseband", baseband[0] ? baseband : "unknown"); |
| 642 | property_set("ro.carrier", carrier[0] ? carrier : "unknown"); |
| 643 | property_set("ro.bootloader", bootloader[0] ? bootloader : "unknown"); |
| 644 | |
| 645 | property_set("ro.hardware", hardware); |
| 646 | snprintf(tmp, PROP_VALUE_MAX, "%d", revision); |
| 647 | property_set("ro.revision", tmp); |
| 648 | |
| 649 | /* execute all the boot actions to get us started */ |
| 650 | action_for_each_trigger("init", action_add_queue_tail); |
Colin Cross | 31712be | 2010-04-09 12:26:06 -0700 | [diff] [blame] | 651 | action_for_each_trigger("early-fs", action_add_queue_tail); |
| 652 | action_for_each_trigger("fs", action_add_queue_tail); |
| 653 | action_for_each_trigger("post-fs", action_add_queue_tail); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 654 | drain_action_queue(); |
| 655 | |
| 656 | /* read any property files on system or data and |
| 657 | * fire up the property service. This must happen |
| 658 | * after the ro.foo properties are set above so |
| 659 | * that /data/local.prop cannot interfere with them. |
| 660 | */ |
Colin Cross | d11beb2 | 2010-04-13 19:33:37 -0700 | [diff] [blame] | 661 | start_property_service(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 662 | |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 663 | signal_init(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 664 | |
| 665 | /* make sure we actually have all the pieces we need */ |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 666 | if ((get_device_fd() < 0) || |
Colin Cross | d11beb2 | 2010-04-13 19:33:37 -0700 | [diff] [blame] | 667 | (get_property_set_fd() < 0) || |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 668 | (get_signal_fd() < 0)) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 669 | ERROR("init startup failure\n"); |
| 670 | return 1; |
| 671 | } |
| 672 | |
| 673 | /* execute all the boot actions to get us started */ |
| 674 | action_for_each_trigger("early-boot", action_add_queue_tail); |
| 675 | action_for_each_trigger("boot", action_add_queue_tail); |
| 676 | drain_action_queue(); |
| 677 | |
| 678 | /* run all property triggers based on current state of the properties */ |
| 679 | queue_all_property_triggers(); |
| 680 | drain_action_queue(); |
| 681 | |
| 682 | /* enable property triggers */ |
| 683 | property_triggers_enabled = 1; |
| 684 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 685 | ufds[0].fd = get_device_fd(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 686 | ufds[0].events = POLLIN; |
Colin Cross | d11beb2 | 2010-04-13 19:33:37 -0700 | [diff] [blame] | 687 | ufds[1].fd = get_property_set_fd(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 688 | ufds[1].events = POLLIN; |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 689 | ufds[2].fd = get_signal_fd(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 690 | ufds[2].events = POLLIN; |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 691 | fd_count = 3; |
| 692 | |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 693 | if (get_keychord_fd() > 0) { |
| 694 | ufds[3].fd = get_keychord_fd(); |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 695 | ufds[3].events = POLLIN; |
| 696 | fd_count++; |
| 697 | } else { |
| 698 | ufds[3].events = 0; |
| 699 | ufds[3].revents = 0; |
| 700 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 701 | |
| 702 | #if BOOTCHART |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 703 | bootchart_count = bootchart_init(); |
| 704 | if (bootchart_count < 0) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 705 | ERROR("bootcharting init failure\n"); |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 706 | } else if (bootchart_count > 0) { |
| 707 | NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS); |
| 708 | } else { |
| 709 | NOTICE("bootcharting ignored\n"); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 710 | } |
| 711 | #endif |
| 712 | |
| 713 | for(;;) { |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 714 | int nr, i, timeout = -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 715 | |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 716 | for (i = 0; i < fd_count; i++) |
| 717 | ufds[i].revents = 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 718 | |
| 719 | drain_action_queue(); |
| 720 | restart_processes(); |
| 721 | |
| 722 | if (process_needs_restart) { |
| 723 | timeout = (process_needs_restart - gettime()) * 1000; |
| 724 | if (timeout < 0) |
| 725 | timeout = 0; |
| 726 | } |
| 727 | |
| 728 | #if BOOTCHART |
| 729 | if (bootchart_count > 0) { |
| 730 | if (timeout < 0 || timeout > BOOTCHART_POLLING_MS) |
| 731 | timeout = BOOTCHART_POLLING_MS; |
| 732 | if (bootchart_step() < 0 || --bootchart_count == 0) { |
| 733 | bootchart_finish(); |
| 734 | bootchart_count = 0; |
| 735 | } |
| 736 | } |
| 737 | #endif |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 738 | nr = poll(ufds, fd_count, timeout); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 739 | if (nr <= 0) |
| 740 | continue; |
| 741 | |
| 742 | if (ufds[2].revents == POLLIN) { |
Colin Cross | 9c5366b | 2010-04-13 19:48:59 -0700 | [diff] [blame] | 743 | handle_signal(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 744 | continue; |
| 745 | } |
| 746 | |
| 747 | if (ufds[0].revents == POLLIN) |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 748 | handle_device_fd(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 749 | |
| 750 | if (ufds[1].revents == POLLIN) |
Colin Cross | d11beb2 | 2010-04-13 19:33:37 -0700 | [diff] [blame] | 751 | handle_property_set_fd(); |
The Android Open Source Project | 5ae090e | 2009-01-09 17:51:25 -0800 | [diff] [blame] | 752 | if (ufds[3].revents == POLLIN) |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 753 | handle_keychord(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | return 0; |
| 757 | } |