blob: e2889b6bf4cdbe50af9810654e4908e2c2f58b8a [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
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 Project4f6e8d72008-10-21 07:00:00 -070028#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 Project4f6e8d72008-10-21 07:00:00 -070034
35#include <cutils/sockets.h>
San Mehat4e221f02010-02-25 14:19:50 -080036#include <cutils/iosched_policy.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070037#include <termios.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070038
39#include <sys/system_properties.h>
40
41#include "devices.h"
42#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070043#include "list.h"
44#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070045#include "property_service.h"
The Android Open Source Project35237d12008-12-17 18:08:08 -080046#include "bootchart.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070047#include "signal_handler.h"
Colin Crossa8666952010-04-13 19:20:44 -070048#include "keychords.h"
Colin Crossca7648d2010-04-13 19:29:51 -070049#include "parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070050#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070051
52static int property_triggers_enabled = 0;
53
54#if BOOTCHART
55static int bootchart_count;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070056#endif
57
58static char console[32];
59static char serialno[32];
60static char bootmode[32];
61static char baseband[32];
62static char carrier[32];
63static char bootloader[32];
64static char hardware[32];
65static unsigned revision = 0;
66static char qemu[32];
67
Colin Crossebc6ff12010-04-13 19:52:01 -070068static struct action *cur_action = NULL;
69static struct command *cur_command = NULL;
70static struct listnode *command_queue = NULL;
71
Colin Cross9c5366b2010-04-13 19:48:59 -070072void notify_service_state(const char *name, const char *state)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073{
74 char pname[PROP_NAME_MAX];
75 int len = strlen(name);
76 if ((len + 10) > PROP_NAME_MAX)
77 return;
78 snprintf(pname, sizeof(pname), "init.svc.%s", name);
79 property_set(pname, state);
80}
81
82static int have_console;
83static char *console_name = "/dev/console";
84static time_t process_needs_restart;
85
86static const char *ENV[32];
87
88/* add_environment - add "key=value" to the current environment */
89int add_environment(const char *key, const char *val)
90{
91 int n;
92
93 for (n = 0; n < 31; n++) {
94 if (!ENV[n]) {
95 size_t len = strlen(key) + strlen(val) + 2;
96 char *entry = malloc(len);
97 snprintf(entry, len, "%s=%s", key, val);
98 ENV[n] = entry;
99 return 0;
100 }
101 }
102
103 return 1;
104}
105
106static void zap_stdio(void)
107{
108 int fd;
109 fd = open("/dev/null", O_RDWR);
110 dup2(fd, 0);
111 dup2(fd, 1);
112 dup2(fd, 2);
113 close(fd);
114}
115
116static void open_console()
117{
118 int fd;
119 if ((fd = open(console_name, O_RDWR)) < 0) {
120 fd = open("/dev/null", O_RDWR);
121 }
122 dup2(fd, 0);
123 dup2(fd, 1);
124 dup2(fd, 2);
125 close(fd);
126}
127
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700128static void publish_socket(const char *name, int fd)
129{
130 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
131 char val[64];
132
133 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
134 name,
135 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
136 snprintf(val, sizeof(val), "%d", fd);
137 add_environment(key, val);
138
139 /* make sure we don't close-on-exec */
140 fcntl(fd, F_SETFD, 0);
141}
142
San Mehatf24e2522009-05-19 13:30:46 -0700143void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700144{
145 struct stat s;
146 pid_t pid;
147 int needs_console;
148 int n;
149
150 /* starting a service removes it from the disabled
151 * state and immediately takes it out of the restarting
152 * state if it was in there
153 */
154 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING));
155 svc->time_started = 0;
156
157 /* running processes require no additional work -- if
158 * they're in the process of exiting, we've ensured
159 * that they will immediately restart on exit, unless
160 * they are ONESHOT
161 */
162 if (svc->flags & SVC_RUNNING) {
163 return;
164 }
165
166 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
167 if (needs_console && (!have_console)) {
168 ERROR("service '%s' requires console\n", svc->name);
169 svc->flags |= SVC_DISABLED;
170 return;
171 }
172
173 if (stat(svc->args[0], &s) != 0) {
174 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
175 svc->flags |= SVC_DISABLED;
176 return;
177 }
178
San Mehatf24e2522009-05-19 13:30:46 -0700179 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700180 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
181 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700182 svc->flags |= SVC_DISABLED;
183 return;
184 }
185
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700186 NOTICE("starting '%s'\n", svc->name);
187
188 pid = fork();
189
190 if (pid == 0) {
191 struct socketinfo *si;
192 struct svcenvinfo *ei;
193 char tmp[32];
194 int fd, sz;
195
196 get_property_workspace(&fd, &sz);
197 sprintf(tmp, "%d,%d", dup(fd), sz);
198 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
199
200 for (ei = svc->envvars; ei; ei = ei->next)
201 add_environment(ei->name, ei->value);
202
203 for (si = svc->sockets; si; si = si->next) {
204 int s = create_socket(si->name,
205 !strcmp(si->type, "dgram") ?
206 SOCK_DGRAM : SOCK_STREAM,
207 si->perm, si->uid, si->gid);
208 if (s >= 0) {
209 publish_socket(si->name, s);
210 }
211 }
212
San Mehat4e221f02010-02-25 14:19:50 -0800213 if (svc->ioprio_class != IoSchedClass_NONE) {
214 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
215 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
216 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
217 }
218 }
219
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700220 if (needs_console) {
221 setsid();
222 open_console();
223 } else {
224 zap_stdio();
225 }
226
227#if 0
228 for (n = 0; svc->args[n]; n++) {
229 INFO("args[%d] = '%s'\n", n, svc->args[n]);
230 }
231 for (n = 0; ENV[n]; n++) {
232 INFO("env[%d] = '%s'\n", n, ENV[n]);
233 }
234#endif
235
236 setpgid(0, getpid());
237
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800238 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700239 if (svc->gid) {
240 setgid(svc->gid);
241 }
242 if (svc->nr_supp_gids) {
243 setgroups(svc->nr_supp_gids, svc->supp_gids);
244 }
245 if (svc->uid) {
246 setuid(svc->uid);
247 }
248
San Mehat8ad15682009-05-20 08:50:40 -0700249 if (!dynamic_args) {
250 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
251 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
252 }
253 } else {
San Mehatf24e2522009-05-19 13:30:46 -0700254 char *arg_ptrs[SVC_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700255 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700256 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700257 char *next = tmp;
258 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700259
260 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700261 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700262
San Mehatd4cdd132009-05-20 09:52:16 -0700263 while((bword = strsep(&next, " "))) {
264 arg_ptrs[arg_idx++] = bword;
265 if (arg_idx == SVC_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700266 break;
San Mehatf24e2522009-05-19 13:30:46 -0700267 }
268 arg_ptrs[arg_idx] = '\0';
269 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100270 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700271 _exit(127);
272 }
273
274 if (pid < 0) {
275 ERROR("failed to start '%s'\n", svc->name);
276 svc->pid = 0;
277 return;
278 }
279
280 svc->time_started = gettime();
281 svc->pid = pid;
282 svc->flags |= SVC_RUNNING;
283
284 notify_service_state(svc->name, "running");
285}
286
287void service_stop(struct service *svc)
288{
289 /* we are no longer running, nor should we
290 * attempt to restart
291 */
292 svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING));
293
294 /* if the service has not yet started, prevent
295 * it from auto-starting with its class
296 */
297 svc->flags |= SVC_DISABLED;
298
299 if (svc->pid) {
300 NOTICE("service '%s' is being killed\n", svc->name);
301 kill(-svc->pid, SIGTERM);
302 notify_service_state(svc->name, "stopping");
303 } else {
304 notify_service_state(svc->name, "stopped");
305 }
306}
307
308void property_changed(const char *name, const char *value)
309{
Colin Crossebc6ff12010-04-13 19:52:01 -0700310 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700311 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700312}
313
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700314static void restart_service_if_needed(struct service *svc)
315{
316 time_t next_start_time = svc->time_started + 5;
317
318 if (next_start_time <= gettime()) {
319 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700320 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700321 return;
322 }
323
324 if ((next_start_time < process_needs_restart) ||
325 (process_needs_restart == 0)) {
326 process_needs_restart = next_start_time;
327 }
328}
329
330static void restart_processes()
331{
332 process_needs_restart = 0;
333 service_for_each_flags(SVC_RESTARTING,
334 restart_service_if_needed);
335}
336
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700337static void msg_start(const char *name)
338{
San Mehatf24e2522009-05-19 13:30:46 -0700339 struct service *svc;
340 char *tmp = NULL;
341 char *args = NULL;
342
343 if (!strchr(name, ':'))
344 svc = service_find_by_name(name);
345 else {
346 tmp = strdup(name);
San Mehatf24e2522009-05-19 13:30:46 -0700347 args = strchr(tmp, ':');
348 *args = '\0';
349 args++;
350
351 svc = service_find_by_name(tmp);
352 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700353
354 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700355 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700356 } else {
357 ERROR("no such service '%s'\n", name);
358 }
San Mehatf24e2522009-05-19 13:30:46 -0700359 if (tmp)
360 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700361}
362
363static void msg_stop(const char *name)
364{
365 struct service *svc = service_find_by_name(name);
366
367 if (svc) {
368 service_stop(svc);
369 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700370 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700371 }
372}
373
374void handle_control_message(const char *msg, const char *arg)
375{
376 if (!strcmp(msg,"start")) {
377 msg_start(arg);
378 } else if (!strcmp(msg,"stop")) {
379 msg_stop(arg);
380 } else {
381 ERROR("unknown control msg '%s'\n", msg);
382 }
383}
384
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700385static void import_kernel_nv(char *name, int in_qemu)
386{
387 char *value = strchr(name, '=');
388
389 if (value == 0) return;
390 *value++ = 0;
391 if (*name == 0) return;
392
393 if (!in_qemu)
394 {
395 /* on a real device, white-list the kernel options */
396 if (!strcmp(name,"qemu")) {
397 strlcpy(qemu, value, sizeof(qemu));
398 } else if (!strcmp(name,"androidboot.console")) {
399 strlcpy(console, value, sizeof(console));
400 } else if (!strcmp(name,"androidboot.mode")) {
401 strlcpy(bootmode, value, sizeof(bootmode));
402 } else if (!strcmp(name,"androidboot.serialno")) {
403 strlcpy(serialno, value, sizeof(serialno));
404 } else if (!strcmp(name,"androidboot.baseband")) {
405 strlcpy(baseband, value, sizeof(baseband));
406 } else if (!strcmp(name,"androidboot.carrier")) {
407 strlcpy(carrier, value, sizeof(carrier));
408 } else if (!strcmp(name,"androidboot.bootloader")) {
409 strlcpy(bootloader, value, sizeof(bootloader));
410 } else if (!strcmp(name,"androidboot.hardware")) {
411 strlcpy(hardware, value, sizeof(hardware));
412 } else {
413 qemu_cmdline(name, value);
414 }
415 } else {
416 /* in the emulator, export any kernel option with the
417 * ro.kernel. prefix */
418 char buff[32];
419 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
420 if (len < (int)sizeof(buff)) {
421 property_set( buff, value );
422 }
423 }
424}
425
426static void import_kernel_cmdline(int in_qemu)
427{
428 char cmdline[1024];
429 char *ptr;
430 int fd;
431
432 fd = open("/proc/cmdline", O_RDONLY);
433 if (fd >= 0) {
434 int n = read(fd, cmdline, 1023);
435 if (n < 0) n = 0;
436
437 /* get rid of trailing newline, it happens */
438 if (n > 0 && cmdline[n-1] == '\n') n--;
439
440 cmdline[n] = 0;
441 close(fd);
442 } else {
443 cmdline[0] = 0;
444 }
445
446 ptr = cmdline;
447 while (ptr && *ptr) {
448 char *x = strchr(ptr, ' ');
449 if (x != 0) *x++ = 0;
450 import_kernel_nv(ptr, in_qemu);
451 ptr = x;
452 }
453
454 /* don't expose the raw commandline to nonpriv processes */
455 chmod("/proc/cmdline", 0440);
456}
457
458static void get_hardware_name(void)
459{
460 char data[1024];
461 int fd, n;
462 char *x, *hw, *rev;
463
464 /* Hardware string was provided on kernel command line */
465 if (hardware[0])
466 return;
467
468 fd = open("/proc/cpuinfo", O_RDONLY);
469 if (fd < 0) return;
470
471 n = read(fd, data, 1023);
472 close(fd);
473 if (n < 0) return;
474
475 data[n] = 0;
476 hw = strstr(data, "\nHardware");
477 rev = strstr(data, "\nRevision");
478
479 if (hw) {
480 x = strstr(hw, ": ");
481 if (x) {
482 x += 2;
483 n = 0;
484 while (*x && !isspace(*x)) {
485 hardware[n++] = tolower(*x);
486 x++;
487 if (n == 31) break;
488 }
489 hardware[n] = 0;
490 }
491 }
492
493 if (rev) {
494 x = strstr(rev, ": ");
495 if (x) {
496 revision = strtoul(x + 2, 0, 16);
497 }
498 }
499}
500
Colin Crossebc6ff12010-04-13 19:52:01 -0700501static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700502{
503 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700504 node = list_head(&act->commands);
505 if (!node)
506 return NULL;
507
508 return node_to_item(node, struct command, clist);
509}
510
511static struct command *get_next_command(struct action *act, struct command *cmd)
512{
513 struct listnode *node;
514 node = cmd->clist.next;
515 if (!node)
516 return NULL;
517 if (node == &act->commands)
518 return NULL;
519
520 return node_to_item(node, struct command, clist);
521}
522
523static int is_last_command(struct action *act, struct command *cmd)
524{
525 return (list_tail(&act->commands) == &cmd->clist);
526}
527
528void execute_one_command(void)
529{
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700530 int ret;
531
Colin Crossebc6ff12010-04-13 19:52:01 -0700532 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
533 cur_action = action_remove_queue_head();
534 if (!cur_action)
535 return;
536 INFO("processing action %p (%s)\n", cur_action, cur_action->name);
537 cur_command = get_first_command(cur_action);
538 } else {
539 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700540 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700541
542 if (!cur_command)
543 return;
544
545 ret = cur_command->func(cur_command->nargs, cur_command->args);
546 INFO("command '%s' r=%d\n", cur_command->args[0], ret);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700547}
548
549void open_devnull_stdio(void)
550{
551 int fd;
552 static const char *name = "/dev/__null__";
553 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) {
554 fd = open(name, O_RDWR);
555 unlink(name);
556 if (fd >= 0) {
557 dup2(fd, 0);
558 dup2(fd, 1);
559 dup2(fd, 2);
560 if (fd > 2) {
561 close(fd);
562 }
563 return;
564 }
565 }
566
567 exit(1);
568}
569
Colin Crossebc6ff12010-04-13 19:52:01 -0700570static int device_init_action(int nargs, char **args)
571{
572 INFO("device init\n");
573 device_init();
574 return 0;
575}
576
577static int property_init_action(int nargs, char **args)
578{
579 INFO("property init\n");
580 property_init();
581 return 0;
582}
583
584static int keychord_init_action(int nargs, char **args)
585{
586 keychord_init();
587 return 0;
588}
589
590static int console_init_action(int nargs, char **args)
591{
592 int fd;
593 char tmp[PROP_VALUE_MAX];
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) ) {
606 fd = open("/dev/tty0", O_WRONLY);
607 if (fd >= 0) {
608 const char *msg;
609 msg = "\n"
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 }
627 }
628 return 0;
629}
630
631static int set_init_properties_action(int nargs, char **args)
632{
633 char tmp[PROP_VALUE_MAX];
634
635 if (qemu[0])
636 import_kernel_cmdline(1);
637
638 if (!strcmp(bootmode,"factory"))
639 property_set("ro.factorytest", "1");
640 else if (!strcmp(bootmode,"factory2"))
641 property_set("ro.factorytest", "2");
642 else
643 property_set("ro.factorytest", "0");
644
645 property_set("ro.serialno", serialno[0] ? serialno : "");
646 property_set("ro.bootmode", bootmode[0] ? bootmode : "unknown");
647 property_set("ro.baseband", baseband[0] ? baseband : "unknown");
648 property_set("ro.carrier", carrier[0] ? carrier : "unknown");
649 property_set("ro.bootloader", bootloader[0] ? bootloader : "unknown");
650
651 property_set("ro.hardware", hardware);
652 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
653 property_set("ro.revision", tmp);
654 return 0;
655}
656
657static int property_service_init_action(int nargs, char **args)
658{
659 /* read any property files on system or data and
660 * fire up the property service. This must happen
661 * after the ro.foo properties are set above so
662 * that /data/local.prop cannot interfere with them.
663 */
664 start_property_service();
665 return 0;
666}
667
668static int signal_init_action(int nargs, char **args)
669{
670 signal_init();
671 return 0;
672}
673
674static int check_startup_action(int nargs, char **args)
675{
676 /* make sure we actually have all the pieces we need */
677 if ((get_device_fd() < 0) ||
678 (get_property_set_fd() < 0) ||
679 (get_signal_fd() < 0)) {
680 ERROR("init startup failure\n");
681 exit(1);
682 }
683 return 0;
684}
685
686static int queue_property_triggers_action(int nargs, char **args)
687{
688 queue_all_property_triggers();
689 /* enable property triggers */
690 property_triggers_enabled = 1;
691 return 0;
692}
693
694#if BOOTCHART
695static int bootchart_init_action(int nargs, char **args)
696{
697 bootchart_count = bootchart_init();
698 if (bootchart_count < 0) {
699 ERROR("bootcharting init failure\n");
700 } else if (bootchart_count > 0) {
701 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
702 } else {
703 NOTICE("bootcharting ignored\n");
704 }
705}
706#endif
707
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700708int main(int argc, char **argv)
709{
Colin Crossebc6ff12010-04-13 19:52:01 -0700710 int fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700711 struct pollfd ufds[4];
712 char *tmpdev;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800713 char* debuggable;
Colin Crossebc6ff12010-04-13 19:52:01 -0700714 char tmp[32];
715 int device_fd_init = 0;
716 int property_set_fd_init = 0;
717 int signal_fd_init = 0;
718 int keychord_fd_init = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700719
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700720 /* clear the umask */
721 umask(0);
722
723 /* Get the basic filesystem setup we need put
724 * together in the initramdisk on / and then we'll
725 * let the rc file figure out the rest.
726 */
727 mkdir("/dev", 0755);
728 mkdir("/proc", 0755);
729 mkdir("/sys", 0755);
730
731 mount("tmpfs", "/dev", "tmpfs", 0, "mode=0755");
732 mkdir("/dev/pts", 0755);
733 mkdir("/dev/socket", 0755);
734 mount("devpts", "/dev/pts", "devpts", 0, NULL);
735 mount("proc", "/proc", "proc", 0, NULL);
736 mount("sysfs", "/sys", "sysfs", 0, NULL);
737
738 /* We must have some place other than / to create the
739 * device nodes for kmsg and null, otherwise we won't
740 * be able to remount / read-only later on.
741 * Now that tmpfs is mounted on /dev, we can actually
742 * talk to the outside world.
743 */
744 open_devnull_stdio();
745 log_init();
746
747 INFO("reading config file\n");
748 parse_config_file("/init.rc");
749
750 /* pull the kernel commandline and ramdisk properties file in */
751 qemu_init();
752 import_kernel_cmdline(0);
753
754 get_hardware_name();
755 snprintf(tmp, sizeof(tmp), "/init.%s.rc", hardware);
756 parse_config_file(tmp);
757
758 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700759
Colin Crossebc6ff12010-04-13 19:52:01 -0700760 queue_builtin_action(device_init_action, "device_init");
761 queue_builtin_action(property_init_action, "property_init");
762 queue_builtin_action(keychord_init_action, "keychord_init");
763 queue_builtin_action(console_init_action, "console_init");
764 queue_builtin_action(set_init_properties_action, "set_init_properties");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700765
766 /* execute all the boot actions to get us started */
767 action_for_each_trigger("init", action_add_queue_tail);
Colin Cross31712be2010-04-09 12:26:06 -0700768 action_for_each_trigger("early-fs", action_add_queue_tail);
769 action_for_each_trigger("fs", action_add_queue_tail);
770 action_for_each_trigger("post-fs", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700771
Colin Crossebc6ff12010-04-13 19:52:01 -0700772 queue_builtin_action(property_service_init_action, "property_service_init");
773 queue_builtin_action(signal_init_action, "signal_init");
774 queue_builtin_action(check_startup_action, "check_startup");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700775
776 /* execute all the boot actions to get us started */
777 action_for_each_trigger("early-boot", action_add_queue_tail);
778 action_for_each_trigger("boot", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700779
780 /* run all property triggers based on current state of the properties */
Colin Crossebc6ff12010-04-13 19:52:01 -0700781 queue_builtin_action(queue_property_triggers_action, "queue_propety_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700782
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700783
784#if BOOTCHART
Colin Crossebc6ff12010-04-13 19:52:01 -0700785 queue_builtin_action(bootchart_init_action, "bootchart_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700786#endif
787
788 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800789 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700790
Colin Crossebc6ff12010-04-13 19:52:01 -0700791 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700792 restart_processes();
793
Colin Crossebc6ff12010-04-13 19:52:01 -0700794 if (!device_fd_init && get_device_fd() > 0) {
795 ufds[fd_count].fd = get_device_fd();
796 ufds[fd_count].events = POLLIN;
797 ufds[fd_count].revents = 0;
798 fd_count++;
799 device_fd_init = 1;
800 }
801 if (!property_set_fd_init && get_property_set_fd() > 0) {
802 ufds[fd_count].fd = get_property_set_fd();
803 ufds[fd_count].events = POLLIN;
804 ufds[fd_count].revents = 0;
805 fd_count++;
806 property_set_fd_init = 1;
807 }
808 if (!signal_fd_init && get_signal_fd() > 0) {
809 ufds[fd_count].fd = get_signal_fd();
810 ufds[fd_count].events = POLLIN;
811 ufds[fd_count].revents = 0;
812 fd_count++;
813 signal_fd_init = 1;
814 }
815 if (!keychord_fd_init && get_keychord_fd() > 0) {
816 ufds[fd_count].fd = get_keychord_fd();
817 ufds[fd_count].events = POLLIN;
818 ufds[fd_count].revents = 0;
819 fd_count++;
820 keychord_fd_init = 1;
821 }
822
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700823 if (process_needs_restart) {
824 timeout = (process_needs_restart - gettime()) * 1000;
825 if (timeout < 0)
826 timeout = 0;
827 }
828
Colin Crossebc6ff12010-04-13 19:52:01 -0700829 if (!action_queue_empty() || cur_command)
830 timeout = 0;
831
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700832#if BOOTCHART
833 if (bootchart_count > 0) {
834 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
835 timeout = BOOTCHART_POLLING_MS;
836 if (bootchart_step() < 0 || --bootchart_count == 0) {
837 bootchart_finish();
838 bootchart_count = 0;
839 }
840 }
841#endif
Colin Crossebc6ff12010-04-13 19:52:01 -0700842
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800843 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700844 if (nr <= 0)
845 continue;
846
Colin Crossebc6ff12010-04-13 19:52:01 -0700847 for (i = 0; i < fd_count; i++) {
848 if (ufds[i].revents == POLLIN) {
849 if (ufds[i].fd == get_device_fd())
850 handle_device_fd();
851 else if (ufds[i].fd == get_property_set_fd())
852 handle_property_set_fd();
853 else if (ufds[i].fd == get_keychord_fd())
854 handle_keychord();
855 else if (ufds[i].fd == get_signal_fd())
856 handle_signal();
857 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700858 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700859 }
860
861 return 0;
862}