blob: ea7687d364bcfb9782f6f31136837ce901784bdb [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>
Colin Crossf83d0b92010-04-21 12:04:20 -070034#include <libgen.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070035
36#include <cutils/sockets.h>
San Mehat4e221f02010-02-25 14:19:50 -080037#include <cutils/iosched_policy.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070038#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070039#include <termios.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070040
41#include <sys/system_properties.h>
42
43#include "devices.h"
44#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070045#include "list.h"
46#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070047#include "property_service.h"
The Android Open Source Project35237d12008-12-17 18:08:08 -080048#include "bootchart.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070049#include "signal_handler.h"
Colin Crossa8666952010-04-13 19:20:44 -070050#include "keychords.h"
Colin Cross6310a822010-04-20 14:29:05 -070051#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070052#include "util.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070053#include "ueventd.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054
55static int property_triggers_enabled = 0;
56
57#if BOOTCHART
58static int bootchart_count;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070059#endif
60
61static char console[32];
62static char serialno[32];
63static char bootmode[32];
64static char baseband[32];
65static char carrier[32];
66static char bootloader[32];
67static char hardware[32];
68static unsigned revision = 0;
69static char qemu[32];
70
Colin Crossebc6ff12010-04-13 19:52:01 -070071static struct action *cur_action = NULL;
72static struct command *cur_command = NULL;
73static struct listnode *command_queue = NULL;
74
Colin Cross9c5366b2010-04-13 19:48:59 -070075void notify_service_state(const char *name, const char *state)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076{
77 char pname[PROP_NAME_MAX];
78 int len = strlen(name);
79 if ((len + 10) > PROP_NAME_MAX)
80 return;
81 snprintf(pname, sizeof(pname), "init.svc.%s", name);
82 property_set(pname, state);
83}
84
85static int have_console;
86static char *console_name = "/dev/console";
87static time_t process_needs_restart;
88
89static const char *ENV[32];
90
91/* add_environment - add "key=value" to the current environment */
92int add_environment(const char *key, const char *val)
93{
94 int n;
95
96 for (n = 0; n < 31; n++) {
97 if (!ENV[n]) {
98 size_t len = strlen(key) + strlen(val) + 2;
99 char *entry = malloc(len);
100 snprintf(entry, len, "%s=%s", key, val);
101 ENV[n] = entry;
102 return 0;
103 }
104 }
105
106 return 1;
107}
108
109static void zap_stdio(void)
110{
111 int fd;
112 fd = open("/dev/null", O_RDWR);
113 dup2(fd, 0);
114 dup2(fd, 1);
115 dup2(fd, 2);
116 close(fd);
117}
118
119static void open_console()
120{
121 int fd;
122 if ((fd = open(console_name, O_RDWR)) < 0) {
123 fd = open("/dev/null", O_RDWR);
124 }
125 dup2(fd, 0);
126 dup2(fd, 1);
127 dup2(fd, 2);
128 close(fd);
129}
130
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700131static void publish_socket(const char *name, int fd)
132{
133 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
134 char val[64];
135
136 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
137 name,
138 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
139 snprintf(val, sizeof(val), "%d", fd);
140 add_environment(key, val);
141
142 /* make sure we don't close-on-exec */
143 fcntl(fd, F_SETFD, 0);
144}
145
San Mehatf24e2522009-05-19 13:30:46 -0700146void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700147{
148 struct stat s;
149 pid_t pid;
150 int needs_console;
151 int n;
152
153 /* starting a service removes it from the disabled
154 * state and immediately takes it out of the restarting
155 * state if it was in there
156 */
157 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING));
158 svc->time_started = 0;
159
160 /* running processes require no additional work -- if
161 * they're in the process of exiting, we've ensured
162 * that they will immediately restart on exit, unless
163 * they are ONESHOT
164 */
165 if (svc->flags & SVC_RUNNING) {
166 return;
167 }
168
169 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
170 if (needs_console && (!have_console)) {
171 ERROR("service '%s' requires console\n", svc->name);
172 svc->flags |= SVC_DISABLED;
173 return;
174 }
175
176 if (stat(svc->args[0], &s) != 0) {
177 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
178 svc->flags |= SVC_DISABLED;
179 return;
180 }
181
San Mehatf24e2522009-05-19 13:30:46 -0700182 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700183 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
184 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700185 svc->flags |= SVC_DISABLED;
186 return;
187 }
188
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700189 NOTICE("starting '%s'\n", svc->name);
190
191 pid = fork();
192
193 if (pid == 0) {
194 struct socketinfo *si;
195 struct svcenvinfo *ei;
196 char tmp[32];
197 int fd, sz;
198
Paul Eastham50b3afd2010-12-04 17:29:31 -0800199 if (svc->flags & SVC_SLOW_START) {
200 sleep(5);
201 }
202
Colin Cross3294bbb2010-04-19 17:11:33 -0700203 if (properties_inited()) {
204 get_property_workspace(&fd, &sz);
205 sprintf(tmp, "%d,%d", dup(fd), sz);
206 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
207 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700208
209 for (ei = svc->envvars; ei; ei = ei->next)
210 add_environment(ei->name, ei->value);
211
212 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400213 int socket_type = (
214 !strcmp(si->type, "stream") ? SOCK_STREAM :
215 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
216 int s = create_socket(si->name, socket_type,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700217 si->perm, si->uid, si->gid);
218 if (s >= 0) {
219 publish_socket(si->name, s);
220 }
221 }
222
San Mehat4e221f02010-02-25 14:19:50 -0800223 if (svc->ioprio_class != IoSchedClass_NONE) {
224 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
225 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
226 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
227 }
228 }
229
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700230 if (needs_console) {
231 setsid();
232 open_console();
233 } else {
234 zap_stdio();
235 }
236
237#if 0
238 for (n = 0; svc->args[n]; n++) {
239 INFO("args[%d] = '%s'\n", n, svc->args[n]);
240 }
241 for (n = 0; ENV[n]; n++) {
242 INFO("env[%d] = '%s'\n", n, ENV[n]);
243 }
244#endif
245
246 setpgid(0, getpid());
247
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800248 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700249 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800250 if (setgid(svc->gid) != 0) {
251 ERROR("setgid failed: %s\n", strerror(errno));
252 _exit(127);
253 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700254 }
255 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800256 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
257 ERROR("setgroups failed: %s\n", strerror(errno));
258 _exit(127);
259 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700260 }
261 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800262 if (setuid(svc->uid) != 0) {
263 ERROR("setuid failed: %s\n", strerror(errno));
264 _exit(127);
265 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700266 }
267
San Mehat8ad15682009-05-20 08:50:40 -0700268 if (!dynamic_args) {
269 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
270 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
271 }
272 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700273 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700274 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700275 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700276 char *next = tmp;
277 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700278
279 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700280 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700281
San Mehatd4cdd132009-05-20 09:52:16 -0700282 while((bword = strsep(&next, " "))) {
283 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700284 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700285 break;
San Mehatf24e2522009-05-19 13:30:46 -0700286 }
287 arg_ptrs[arg_idx] = '\0';
288 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100289 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700290 _exit(127);
291 }
292
293 if (pid < 0) {
294 ERROR("failed to start '%s'\n", svc->name);
295 svc->pid = 0;
296 return;
297 }
298
299 svc->time_started = gettime();
300 svc->pid = pid;
301 svc->flags |= SVC_RUNNING;
302
Colin Cross3294bbb2010-04-19 17:11:33 -0700303 if (properties_inited())
304 notify_service_state(svc->name, "running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700305}
306
307void service_stop(struct service *svc)
308{
309 /* we are no longer running, nor should we
310 * attempt to restart
311 */
312 svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING));
313
314 /* if the service has not yet started, prevent
315 * it from auto-starting with its class
316 */
317 svc->flags |= SVC_DISABLED;
318
319 if (svc->pid) {
320 NOTICE("service '%s' is being killed\n", svc->name);
321 kill(-svc->pid, SIGTERM);
322 notify_service_state(svc->name, "stopping");
323 } else {
324 notify_service_state(svc->name, "stopped");
325 }
326}
327
328void property_changed(const char *name, const char *value)
329{
Colin Crossebc6ff12010-04-13 19:52:01 -0700330 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700331 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700332}
333
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700334static void restart_service_if_needed(struct service *svc)
335{
336 time_t next_start_time = svc->time_started + 5;
337
338 if (next_start_time <= gettime()) {
339 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700340 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700341 return;
342 }
343
344 if ((next_start_time < process_needs_restart) ||
345 (process_needs_restart == 0)) {
346 process_needs_restart = next_start_time;
347 }
348}
349
350static void restart_processes()
351{
352 process_needs_restart = 0;
353 service_for_each_flags(SVC_RESTARTING,
354 restart_service_if_needed);
355}
356
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700357static void msg_start(const char *name)
358{
San Mehatf24e2522009-05-19 13:30:46 -0700359 struct service *svc;
360 char *tmp = NULL;
361 char *args = NULL;
362
363 if (!strchr(name, ':'))
364 svc = service_find_by_name(name);
365 else {
366 tmp = strdup(name);
San Mehatf24e2522009-05-19 13:30:46 -0700367 args = strchr(tmp, ':');
368 *args = '\0';
369 args++;
370
371 svc = service_find_by_name(tmp);
372 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700373
374 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700375 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700376 } else {
377 ERROR("no such service '%s'\n", name);
378 }
San Mehatf24e2522009-05-19 13:30:46 -0700379 if (tmp)
380 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700381}
382
383static void msg_stop(const char *name)
384{
385 struct service *svc = service_find_by_name(name);
386
387 if (svc) {
388 service_stop(svc);
389 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700390 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700391 }
392}
393
394void handle_control_message(const char *msg, const char *arg)
395{
396 if (!strcmp(msg,"start")) {
397 msg_start(arg);
398 } else if (!strcmp(msg,"stop")) {
399 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700400 } else if (!strcmp(msg,"restart")) {
401 msg_stop(arg);
402 msg_start(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700403 } else {
404 ERROR("unknown control msg '%s'\n", msg);
405 }
406}
407
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700408static void import_kernel_nv(char *name, int in_qemu)
409{
410 char *value = strchr(name, '=');
411
412 if (value == 0) return;
413 *value++ = 0;
414 if (*name == 0) return;
415
416 if (!in_qemu)
417 {
418 /* on a real device, white-list the kernel options */
419 if (!strcmp(name,"qemu")) {
420 strlcpy(qemu, value, sizeof(qemu));
421 } else if (!strcmp(name,"androidboot.console")) {
422 strlcpy(console, value, sizeof(console));
423 } else if (!strcmp(name,"androidboot.mode")) {
424 strlcpy(bootmode, value, sizeof(bootmode));
425 } else if (!strcmp(name,"androidboot.serialno")) {
426 strlcpy(serialno, value, sizeof(serialno));
427 } else if (!strcmp(name,"androidboot.baseband")) {
428 strlcpy(baseband, value, sizeof(baseband));
429 } else if (!strcmp(name,"androidboot.carrier")) {
430 strlcpy(carrier, value, sizeof(carrier));
431 } else if (!strcmp(name,"androidboot.bootloader")) {
432 strlcpy(bootloader, value, sizeof(bootloader));
433 } else if (!strcmp(name,"androidboot.hardware")) {
434 strlcpy(hardware, value, sizeof(hardware));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700435 }
436 } else {
437 /* in the emulator, export any kernel option with the
438 * ro.kernel. prefix */
439 char buff[32];
440 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
441 if (len < (int)sizeof(buff)) {
442 property_set( buff, value );
443 }
444 }
445}
446
447static void import_kernel_cmdline(int in_qemu)
448{
449 char cmdline[1024];
450 char *ptr;
451 int fd;
452
453 fd = open("/proc/cmdline", O_RDONLY);
454 if (fd >= 0) {
455 int n = read(fd, cmdline, 1023);
456 if (n < 0) n = 0;
457
458 /* get rid of trailing newline, it happens */
459 if (n > 0 && cmdline[n-1] == '\n') n--;
460
461 cmdline[n] = 0;
462 close(fd);
463 } else {
464 cmdline[0] = 0;
465 }
466
467 ptr = cmdline;
468 while (ptr && *ptr) {
469 char *x = strchr(ptr, ' ');
470 if (x != 0) *x++ = 0;
471 import_kernel_nv(ptr, in_qemu);
472 ptr = x;
473 }
474
475 /* don't expose the raw commandline to nonpriv processes */
476 chmod("/proc/cmdline", 0440);
477}
478
Colin Crossebc6ff12010-04-13 19:52:01 -0700479static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700480{
481 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700482 node = list_head(&act->commands);
483 if (!node)
484 return NULL;
485
486 return node_to_item(node, struct command, clist);
487}
488
489static struct command *get_next_command(struct action *act, struct command *cmd)
490{
491 struct listnode *node;
492 node = cmd->clist.next;
493 if (!node)
494 return NULL;
495 if (node == &act->commands)
496 return NULL;
497
498 return node_to_item(node, struct command, clist);
499}
500
501static int is_last_command(struct action *act, struct command *cmd)
502{
503 return (list_tail(&act->commands) == &cmd->clist);
504}
505
506void execute_one_command(void)
507{
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700508 int ret;
509
Colin Crossebc6ff12010-04-13 19:52:01 -0700510 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
511 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700512 cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -0700513 if (!cur_action)
514 return;
515 INFO("processing action %p (%s)\n", cur_action, cur_action->name);
516 cur_command = get_first_command(cur_action);
517 } else {
518 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700519 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700520
521 if (!cur_command)
522 return;
523
524 ret = cur_command->func(cur_command->nargs, cur_command->args);
525 INFO("command '%s' r=%d\n", cur_command->args[0], ret);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700526}
527
Colin Crossf83d0b92010-04-21 12:04:20 -0700528static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700529{
Colin Crossf83d0b92010-04-21 12:04:20 -0700530 int ret;
531 INFO("wait for %s\n", coldboot_done);
532 ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT);
533 if (ret)
534 ERROR("Timed out waiting for %s\n", coldboot_done);
535 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700536}
537
538static int property_init_action(int nargs, char **args)
539{
540 INFO("property init\n");
541 property_init();
542 return 0;
543}
544
545static int keychord_init_action(int nargs, char **args)
546{
547 keychord_init();
548 return 0;
549}
550
551static int console_init_action(int nargs, char **args)
552{
553 int fd;
554 char tmp[PROP_VALUE_MAX];
555
556 if (console[0]) {
557 snprintf(tmp, sizeof(tmp), "/dev/%s", console);
558 console_name = strdup(tmp);
559 }
560
561 fd = open(console_name, O_RDWR);
562 if (fd >= 0)
563 have_console = 1;
564 close(fd);
565
566 if( load_565rle_image(INIT_IMAGE_FILE) ) {
567 fd = open("/dev/tty0", O_WRONLY);
568 if (fd >= 0) {
569 const char *msg;
570 msg = "\n"
571 "\n"
572 "\n"
573 "\n"
574 "\n"
575 "\n"
576 "\n" // console is 40 cols x 30 lines
577 "\n"
578 "\n"
579 "\n"
580 "\n"
581 "\n"
582 "\n"
583 "\n"
584 " A N D R O I D ";
585 write(fd, msg, strlen(msg));
586 close(fd);
587 }
588 }
589 return 0;
590}
591
592static int set_init_properties_action(int nargs, char **args)
593{
594 char tmp[PROP_VALUE_MAX];
595
596 if (qemu[0])
597 import_kernel_cmdline(1);
598
599 if (!strcmp(bootmode,"factory"))
600 property_set("ro.factorytest", "1");
601 else if (!strcmp(bootmode,"factory2"))
602 property_set("ro.factorytest", "2");
603 else
604 property_set("ro.factorytest", "0");
605
606 property_set("ro.serialno", serialno[0] ? serialno : "");
607 property_set("ro.bootmode", bootmode[0] ? bootmode : "unknown");
608 property_set("ro.baseband", baseband[0] ? baseband : "unknown");
609 property_set("ro.carrier", carrier[0] ? carrier : "unknown");
610 property_set("ro.bootloader", bootloader[0] ? bootloader : "unknown");
611
612 property_set("ro.hardware", hardware);
613 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
614 property_set("ro.revision", tmp);
615 return 0;
616}
617
618static int property_service_init_action(int nargs, char **args)
619{
620 /* read any property files on system or data and
621 * fire up the property service. This must happen
622 * after the ro.foo properties are set above so
623 * that /data/local.prop cannot interfere with them.
624 */
625 start_property_service();
626 return 0;
627}
628
629static int signal_init_action(int nargs, char **args)
630{
631 signal_init();
632 return 0;
633}
634
635static int check_startup_action(int nargs, char **args)
636{
637 /* make sure we actually have all the pieces we need */
Colin Crossf83d0b92010-04-21 12:04:20 -0700638 if ((get_property_set_fd() < 0) ||
Colin Crossebc6ff12010-04-13 19:52:01 -0700639 (get_signal_fd() < 0)) {
640 ERROR("init startup failure\n");
641 exit(1);
642 }
643 return 0;
644}
645
646static int queue_property_triggers_action(int nargs, char **args)
647{
648 queue_all_property_triggers();
649 /* enable property triggers */
650 property_triggers_enabled = 1;
651 return 0;
652}
653
654#if BOOTCHART
655static int bootchart_init_action(int nargs, char **args)
656{
657 bootchart_count = bootchart_init();
658 if (bootchart_count < 0) {
659 ERROR("bootcharting init failure\n");
660 } else if (bootchart_count > 0) {
661 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
662 } else {
663 NOTICE("bootcharting ignored\n");
664 }
665}
666#endif
667
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700668int main(int argc, char **argv)
669{
Colin Crossebc6ff12010-04-13 19:52:01 -0700670 int fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700671 struct pollfd ufds[4];
672 char *tmpdev;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800673 char* debuggable;
Colin Crossebc6ff12010-04-13 19:52:01 -0700674 char tmp[32];
Colin Crossebc6ff12010-04-13 19:52:01 -0700675 int property_set_fd_init = 0;
676 int signal_fd_init = 0;
677 int keychord_fd_init = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700678
Colin Crossf83d0b92010-04-21 12:04:20 -0700679 if (!strcmp(basename(argv[0]), "ueventd"))
680 return ueventd_main(argc, argv);
681
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700682 /* clear the umask */
683 umask(0);
684
685 /* Get the basic filesystem setup we need put
686 * together in the initramdisk on / and then we'll
687 * let the rc file figure out the rest.
688 */
689 mkdir("/dev", 0755);
690 mkdir("/proc", 0755);
691 mkdir("/sys", 0755);
692
Nick Kralevich150f19e2010-06-22 16:35:43 -0700693 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700694 mkdir("/dev/pts", 0755);
695 mkdir("/dev/socket", 0755);
696 mount("devpts", "/dev/pts", "devpts", 0, NULL);
697 mount("proc", "/proc", "proc", 0, NULL);
698 mount("sysfs", "/sys", "sysfs", 0, NULL);
699
700 /* We must have some place other than / to create the
701 * device nodes for kmsg and null, otherwise we won't
702 * be able to remount / read-only later on.
703 * Now that tmpfs is mounted on /dev, we can actually
704 * talk to the outside world.
705 */
706 open_devnull_stdio();
707 log_init();
708
709 INFO("reading config file\n");
Colin Cross6310a822010-04-20 14:29:05 -0700710 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700711
712 /* pull the kernel commandline and ramdisk properties file in */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700713 import_kernel_cmdline(0);
714
Colin Crossf83d0b92010-04-21 12:04:20 -0700715 get_hardware_name(hardware, &revision);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700716 snprintf(tmp, sizeof(tmp), "/init.%s.rc", hardware);
Colin Cross6310a822010-04-20 14:29:05 -0700717 init_parse_config_file(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700718
719 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700720
Colin Crossf83d0b92010-04-21 12:04:20 -0700721 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Colin Crossebc6ff12010-04-13 19:52:01 -0700722 queue_builtin_action(property_init_action, "property_init");
723 queue_builtin_action(keychord_init_action, "keychord_init");
724 queue_builtin_action(console_init_action, "console_init");
725 queue_builtin_action(set_init_properties_action, "set_init_properties");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700726
727 /* execute all the boot actions to get us started */
728 action_for_each_trigger("init", action_add_queue_tail);
Colin Cross31712be2010-04-09 12:26:06 -0700729 action_for_each_trigger("early-fs", action_add_queue_tail);
730 action_for_each_trigger("fs", action_add_queue_tail);
731 action_for_each_trigger("post-fs", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700732
Colin Crossebc6ff12010-04-13 19:52:01 -0700733 queue_builtin_action(property_service_init_action, "property_service_init");
734 queue_builtin_action(signal_init_action, "signal_init");
735 queue_builtin_action(check_startup_action, "check_startup");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700736
737 /* execute all the boot actions to get us started */
738 action_for_each_trigger("early-boot", action_add_queue_tail);
739 action_for_each_trigger("boot", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700740
741 /* run all property triggers based on current state of the properties */
Colin Crossebc6ff12010-04-13 19:52:01 -0700742 queue_builtin_action(queue_property_triggers_action, "queue_propety_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700743
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700744
745#if BOOTCHART
Colin Crossebc6ff12010-04-13 19:52:01 -0700746 queue_builtin_action(bootchart_init_action, "bootchart_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700747#endif
748
749 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800750 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700751
Colin Crossebc6ff12010-04-13 19:52:01 -0700752 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700753 restart_processes();
754
Colin Crossebc6ff12010-04-13 19:52:01 -0700755 if (!property_set_fd_init && get_property_set_fd() > 0) {
756 ufds[fd_count].fd = get_property_set_fd();
757 ufds[fd_count].events = POLLIN;
758 ufds[fd_count].revents = 0;
759 fd_count++;
760 property_set_fd_init = 1;
761 }
762 if (!signal_fd_init && get_signal_fd() > 0) {
763 ufds[fd_count].fd = get_signal_fd();
764 ufds[fd_count].events = POLLIN;
765 ufds[fd_count].revents = 0;
766 fd_count++;
767 signal_fd_init = 1;
768 }
769 if (!keychord_fd_init && get_keychord_fd() > 0) {
770 ufds[fd_count].fd = get_keychord_fd();
771 ufds[fd_count].events = POLLIN;
772 ufds[fd_count].revents = 0;
773 fd_count++;
774 keychord_fd_init = 1;
775 }
776
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700777 if (process_needs_restart) {
778 timeout = (process_needs_restart - gettime()) * 1000;
779 if (timeout < 0)
780 timeout = 0;
781 }
782
Colin Crossebd46132010-04-22 11:52:23 -0700783 if (!action_queue_empty() || cur_action)
Colin Crossebc6ff12010-04-13 19:52:01 -0700784 timeout = 0;
785
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700786#if BOOTCHART
787 if (bootchart_count > 0) {
788 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
789 timeout = BOOTCHART_POLLING_MS;
790 if (bootchart_step() < 0 || --bootchart_count == 0) {
791 bootchart_finish();
792 bootchart_count = 0;
793 }
794 }
795#endif
Colin Crossebc6ff12010-04-13 19:52:01 -0700796
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800797 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700798 if (nr <= 0)
799 continue;
800
Colin Crossebc6ff12010-04-13 19:52:01 -0700801 for (i = 0; i < fd_count; i++) {
802 if (ufds[i].revents == POLLIN) {
Colin Crossf83d0b92010-04-21 12:04:20 -0700803 if (ufds[i].fd == get_property_set_fd())
Colin Crossebc6ff12010-04-13 19:52:01 -0700804 handle_property_set_fd();
805 else if (ufds[i].fd == get_keychord_fd())
806 handle_keychord();
807 else if (ufds[i].fd == get_signal_fd())
808 handle_signal();
809 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700810 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700811 }
812
813 return 0;
814}