blob: 7aef387219ea2feabe989a4de11b6e32263a0f52 [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
Colin Cross3294bbb2010-04-19 17:11:33 -0700199 if (properties_inited()) {
200 get_property_workspace(&fd, &sz);
201 sprintf(tmp, "%d,%d", dup(fd), sz);
202 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
203 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700204
205 for (ei = svc->envvars; ei; ei = ei->next)
206 add_environment(ei->name, ei->value);
207
208 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400209 int socket_type = (
210 !strcmp(si->type, "stream") ? SOCK_STREAM :
211 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
212 int s = create_socket(si->name, socket_type,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700213 si->perm, si->uid, si->gid);
214 if (s >= 0) {
215 publish_socket(si->name, s);
216 }
217 }
218
San Mehat4e221f02010-02-25 14:19:50 -0800219 if (svc->ioprio_class != IoSchedClass_NONE) {
220 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
221 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
222 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
223 }
224 }
225
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700226 if (needs_console) {
227 setsid();
228 open_console();
229 } else {
230 zap_stdio();
231 }
232
233#if 0
234 for (n = 0; svc->args[n]; n++) {
235 INFO("args[%d] = '%s'\n", n, svc->args[n]);
236 }
237 for (n = 0; ENV[n]; n++) {
238 INFO("env[%d] = '%s'\n", n, ENV[n]);
239 }
240#endif
241
242 setpgid(0, getpid());
243
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800244 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700245 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800246 if (setgid(svc->gid) != 0) {
247 ERROR("setgid failed: %s\n", strerror(errno));
248 _exit(127);
249 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700250 }
251 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800252 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
253 ERROR("setgroups failed: %s\n", strerror(errno));
254 _exit(127);
255 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700256 }
257 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800258 if (setuid(svc->uid) != 0) {
259 ERROR("setuid failed: %s\n", strerror(errno));
260 _exit(127);
261 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700262 }
263
San Mehat8ad15682009-05-20 08:50:40 -0700264 if (!dynamic_args) {
265 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
266 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
267 }
268 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700269 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700270 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700271 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700272 char *next = tmp;
273 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700274
275 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700276 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700277
San Mehatd4cdd132009-05-20 09:52:16 -0700278 while((bword = strsep(&next, " "))) {
279 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700280 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700281 break;
San Mehatf24e2522009-05-19 13:30:46 -0700282 }
283 arg_ptrs[arg_idx] = '\0';
284 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100285 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700286 _exit(127);
287 }
288
289 if (pid < 0) {
290 ERROR("failed to start '%s'\n", svc->name);
291 svc->pid = 0;
292 return;
293 }
294
295 svc->time_started = gettime();
296 svc->pid = pid;
297 svc->flags |= SVC_RUNNING;
298
Colin Cross3294bbb2010-04-19 17:11:33 -0700299 if (properties_inited())
300 notify_service_state(svc->name, "running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301}
302
303void service_stop(struct service *svc)
304{
305 /* we are no longer running, nor should we
306 * attempt to restart
307 */
308 svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING));
309
310 /* if the service has not yet started, prevent
311 * it from auto-starting with its class
312 */
313 svc->flags |= SVC_DISABLED;
314
315 if (svc->pid) {
316 NOTICE("service '%s' is being killed\n", svc->name);
317 kill(-svc->pid, SIGTERM);
318 notify_service_state(svc->name, "stopping");
319 } else {
320 notify_service_state(svc->name, "stopped");
321 }
322}
323
324void property_changed(const char *name, const char *value)
325{
Colin Crossebc6ff12010-04-13 19:52:01 -0700326 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700327 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700328}
329
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700330static void restart_service_if_needed(struct service *svc)
331{
332 time_t next_start_time = svc->time_started + 5;
333
334 if (next_start_time <= gettime()) {
335 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700336 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700337 return;
338 }
339
340 if ((next_start_time < process_needs_restart) ||
341 (process_needs_restart == 0)) {
342 process_needs_restart = next_start_time;
343 }
344}
345
346static void restart_processes()
347{
348 process_needs_restart = 0;
349 service_for_each_flags(SVC_RESTARTING,
350 restart_service_if_needed);
351}
352
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700353static void msg_start(const char *name)
354{
San Mehatf24e2522009-05-19 13:30:46 -0700355 struct service *svc;
356 char *tmp = NULL;
357 char *args = NULL;
358
359 if (!strchr(name, ':'))
360 svc = service_find_by_name(name);
361 else {
362 tmp = strdup(name);
San Mehatf24e2522009-05-19 13:30:46 -0700363 args = strchr(tmp, ':');
364 *args = '\0';
365 args++;
366
367 svc = service_find_by_name(tmp);
368 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700369
370 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700371 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700372 } else {
373 ERROR("no such service '%s'\n", name);
374 }
San Mehatf24e2522009-05-19 13:30:46 -0700375 if (tmp)
376 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700377}
378
379static void msg_stop(const char *name)
380{
381 struct service *svc = service_find_by_name(name);
382
383 if (svc) {
384 service_stop(svc);
385 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700386 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700387 }
388}
389
390void handle_control_message(const char *msg, const char *arg)
391{
392 if (!strcmp(msg,"start")) {
393 msg_start(arg);
394 } else if (!strcmp(msg,"stop")) {
395 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700396 } else if (!strcmp(msg,"restart")) {
397 msg_stop(arg);
398 msg_start(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700399 } else {
400 ERROR("unknown control msg '%s'\n", msg);
401 }
402}
403
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700404static void import_kernel_nv(char *name, int in_qemu)
405{
406 char *value = strchr(name, '=');
407
408 if (value == 0) return;
409 *value++ = 0;
410 if (*name == 0) return;
411
412 if (!in_qemu)
413 {
414 /* on a real device, white-list the kernel options */
415 if (!strcmp(name,"qemu")) {
416 strlcpy(qemu, value, sizeof(qemu));
417 } else if (!strcmp(name,"androidboot.console")) {
418 strlcpy(console, value, sizeof(console));
419 } else if (!strcmp(name,"androidboot.mode")) {
420 strlcpy(bootmode, value, sizeof(bootmode));
421 } else if (!strcmp(name,"androidboot.serialno")) {
422 strlcpy(serialno, value, sizeof(serialno));
423 } else if (!strcmp(name,"androidboot.baseband")) {
424 strlcpy(baseband, value, sizeof(baseband));
425 } else if (!strcmp(name,"androidboot.carrier")) {
426 strlcpy(carrier, value, sizeof(carrier));
427 } else if (!strcmp(name,"androidboot.bootloader")) {
428 strlcpy(bootloader, value, sizeof(bootloader));
429 } else if (!strcmp(name,"androidboot.hardware")) {
430 strlcpy(hardware, value, sizeof(hardware));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700431 }
432 } else {
433 /* in the emulator, export any kernel option with the
434 * ro.kernel. prefix */
435 char buff[32];
436 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
437 if (len < (int)sizeof(buff)) {
438 property_set( buff, value );
439 }
440 }
441}
442
443static void import_kernel_cmdline(int in_qemu)
444{
445 char cmdline[1024];
446 char *ptr;
447 int fd;
448
449 fd = open("/proc/cmdline", O_RDONLY);
450 if (fd >= 0) {
451 int n = read(fd, cmdline, 1023);
452 if (n < 0) n = 0;
453
454 /* get rid of trailing newline, it happens */
455 if (n > 0 && cmdline[n-1] == '\n') n--;
456
457 cmdline[n] = 0;
458 close(fd);
459 } else {
460 cmdline[0] = 0;
461 }
462
463 ptr = cmdline;
464 while (ptr && *ptr) {
465 char *x = strchr(ptr, ' ');
466 if (x != 0) *x++ = 0;
467 import_kernel_nv(ptr, in_qemu);
468 ptr = x;
469 }
470
471 /* don't expose the raw commandline to nonpriv processes */
472 chmod("/proc/cmdline", 0440);
473}
474
Colin Crossebc6ff12010-04-13 19:52:01 -0700475static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700476{
477 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700478 node = list_head(&act->commands);
479 if (!node)
480 return NULL;
481
482 return node_to_item(node, struct command, clist);
483}
484
485static struct command *get_next_command(struct action *act, struct command *cmd)
486{
487 struct listnode *node;
488 node = cmd->clist.next;
489 if (!node)
490 return NULL;
491 if (node == &act->commands)
492 return NULL;
493
494 return node_to_item(node, struct command, clist);
495}
496
497static int is_last_command(struct action *act, struct command *cmd)
498{
499 return (list_tail(&act->commands) == &cmd->clist);
500}
501
502void execute_one_command(void)
503{
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700504 int ret;
505
Colin Crossebc6ff12010-04-13 19:52:01 -0700506 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
507 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700508 cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -0700509 if (!cur_action)
510 return;
511 INFO("processing action %p (%s)\n", cur_action, cur_action->name);
512 cur_command = get_first_command(cur_action);
513 } else {
514 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700515 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700516
517 if (!cur_command)
518 return;
519
520 ret = cur_command->func(cur_command->nargs, cur_command->args);
521 INFO("command '%s' r=%d\n", cur_command->args[0], ret);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700522}
523
Colin Crossf83d0b92010-04-21 12:04:20 -0700524static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700525{
Colin Crossf83d0b92010-04-21 12:04:20 -0700526 int ret;
527 INFO("wait for %s\n", coldboot_done);
528 ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT);
529 if (ret)
530 ERROR("Timed out waiting for %s\n", coldboot_done);
531 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700532}
533
534static int property_init_action(int nargs, char **args)
535{
536 INFO("property init\n");
537 property_init();
538 return 0;
539}
540
541static int keychord_init_action(int nargs, char **args)
542{
543 keychord_init();
544 return 0;
545}
546
547static int console_init_action(int nargs, char **args)
548{
549 int fd;
550 char tmp[PROP_VALUE_MAX];
551
552 if (console[0]) {
553 snprintf(tmp, sizeof(tmp), "/dev/%s", console);
554 console_name = strdup(tmp);
555 }
556
557 fd = open(console_name, O_RDWR);
558 if (fd >= 0)
559 have_console = 1;
560 close(fd);
561
562 if( load_565rle_image(INIT_IMAGE_FILE) ) {
563 fd = open("/dev/tty0", O_WRONLY);
564 if (fd >= 0) {
565 const char *msg;
566 msg = "\n"
567 "\n"
568 "\n"
569 "\n"
570 "\n"
571 "\n"
572 "\n" // console is 40 cols x 30 lines
573 "\n"
574 "\n"
575 "\n"
576 "\n"
577 "\n"
578 "\n"
579 "\n"
580 " A N D R O I D ";
581 write(fd, msg, strlen(msg));
582 close(fd);
583 }
584 }
585 return 0;
586}
587
588static int set_init_properties_action(int nargs, char **args)
589{
590 char tmp[PROP_VALUE_MAX];
591
592 if (qemu[0])
593 import_kernel_cmdline(1);
594
595 if (!strcmp(bootmode,"factory"))
596 property_set("ro.factorytest", "1");
597 else if (!strcmp(bootmode,"factory2"))
598 property_set("ro.factorytest", "2");
599 else
600 property_set("ro.factorytest", "0");
601
602 property_set("ro.serialno", serialno[0] ? serialno : "");
603 property_set("ro.bootmode", bootmode[0] ? bootmode : "unknown");
604 property_set("ro.baseband", baseband[0] ? baseband : "unknown");
605 property_set("ro.carrier", carrier[0] ? carrier : "unknown");
606 property_set("ro.bootloader", bootloader[0] ? bootloader : "unknown");
607
608 property_set("ro.hardware", hardware);
609 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
610 property_set("ro.revision", tmp);
611 return 0;
612}
613
614static int property_service_init_action(int nargs, char **args)
615{
616 /* read any property files on system or data and
617 * fire up the property service. This must happen
618 * after the ro.foo properties are set above so
619 * that /data/local.prop cannot interfere with them.
620 */
621 start_property_service();
622 return 0;
623}
624
625static int signal_init_action(int nargs, char **args)
626{
627 signal_init();
628 return 0;
629}
630
631static int check_startup_action(int nargs, char **args)
632{
633 /* make sure we actually have all the pieces we need */
Colin Crossf83d0b92010-04-21 12:04:20 -0700634 if ((get_property_set_fd() < 0) ||
Colin Crossebc6ff12010-04-13 19:52:01 -0700635 (get_signal_fd() < 0)) {
636 ERROR("init startup failure\n");
637 exit(1);
638 }
639 return 0;
640}
641
642static int queue_property_triggers_action(int nargs, char **args)
643{
644 queue_all_property_triggers();
645 /* enable property triggers */
646 property_triggers_enabled = 1;
647 return 0;
648}
649
650#if BOOTCHART
651static int bootchart_init_action(int nargs, char **args)
652{
653 bootchart_count = bootchart_init();
654 if (bootchart_count < 0) {
655 ERROR("bootcharting init failure\n");
656 } else if (bootchart_count > 0) {
657 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
658 } else {
659 NOTICE("bootcharting ignored\n");
660 }
661}
662#endif
663
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700664int main(int argc, char **argv)
665{
Colin Crossebc6ff12010-04-13 19:52:01 -0700666 int fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700667 struct pollfd ufds[4];
668 char *tmpdev;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800669 char* debuggable;
Colin Crossebc6ff12010-04-13 19:52:01 -0700670 char tmp[32];
Colin Crossebc6ff12010-04-13 19:52:01 -0700671 int property_set_fd_init = 0;
672 int signal_fd_init = 0;
673 int keychord_fd_init = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700674
Colin Crossf83d0b92010-04-21 12:04:20 -0700675 if (!strcmp(basename(argv[0]), "ueventd"))
676 return ueventd_main(argc, argv);
677
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700678 /* clear the umask */
679 umask(0);
680
681 /* Get the basic filesystem setup we need put
682 * together in the initramdisk on / and then we'll
683 * let the rc file figure out the rest.
684 */
685 mkdir("/dev", 0755);
686 mkdir("/proc", 0755);
687 mkdir("/sys", 0755);
688
Nick Kralevich150f19e2010-06-22 16:35:43 -0700689 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700690 mkdir("/dev/pts", 0755);
691 mkdir("/dev/socket", 0755);
692 mount("devpts", "/dev/pts", "devpts", 0, NULL);
693 mount("proc", "/proc", "proc", 0, NULL);
694 mount("sysfs", "/sys", "sysfs", 0, NULL);
695
696 /* We must have some place other than / to create the
697 * device nodes for kmsg and null, otherwise we won't
698 * be able to remount / read-only later on.
699 * Now that tmpfs is mounted on /dev, we can actually
700 * talk to the outside world.
701 */
702 open_devnull_stdio();
703 log_init();
704
705 INFO("reading config file\n");
Colin Cross6310a822010-04-20 14:29:05 -0700706 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700707
708 /* pull the kernel commandline and ramdisk properties file in */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700709 import_kernel_cmdline(0);
710
Colin Crossf83d0b92010-04-21 12:04:20 -0700711 get_hardware_name(hardware, &revision);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700712 snprintf(tmp, sizeof(tmp), "/init.%s.rc", hardware);
Colin Cross6310a822010-04-20 14:29:05 -0700713 init_parse_config_file(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700714
715 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700716
Colin Crossf83d0b92010-04-21 12:04:20 -0700717 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Colin Crossebc6ff12010-04-13 19:52:01 -0700718 queue_builtin_action(property_init_action, "property_init");
719 queue_builtin_action(keychord_init_action, "keychord_init");
720 queue_builtin_action(console_init_action, "console_init");
721 queue_builtin_action(set_init_properties_action, "set_init_properties");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700722
723 /* execute all the boot actions to get us started */
724 action_for_each_trigger("init", action_add_queue_tail);
Colin Cross31712be2010-04-09 12:26:06 -0700725 action_for_each_trigger("early-fs", action_add_queue_tail);
726 action_for_each_trigger("fs", action_add_queue_tail);
727 action_for_each_trigger("post-fs", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700728
Colin Crossebc6ff12010-04-13 19:52:01 -0700729 queue_builtin_action(property_service_init_action, "property_service_init");
730 queue_builtin_action(signal_init_action, "signal_init");
731 queue_builtin_action(check_startup_action, "check_startup");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700732
733 /* execute all the boot actions to get us started */
734 action_for_each_trigger("early-boot", action_add_queue_tail);
735 action_for_each_trigger("boot", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700736
737 /* run all property triggers based on current state of the properties */
Colin Crossebc6ff12010-04-13 19:52:01 -0700738 queue_builtin_action(queue_property_triggers_action, "queue_propety_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700739
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700740
741#if BOOTCHART
Colin Crossebc6ff12010-04-13 19:52:01 -0700742 queue_builtin_action(bootchart_init_action, "bootchart_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700743#endif
744
745 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800746 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700747
Colin Crossebc6ff12010-04-13 19:52:01 -0700748 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700749 restart_processes();
750
Colin Crossebc6ff12010-04-13 19:52:01 -0700751 if (!property_set_fd_init && get_property_set_fd() > 0) {
752 ufds[fd_count].fd = get_property_set_fd();
753 ufds[fd_count].events = POLLIN;
754 ufds[fd_count].revents = 0;
755 fd_count++;
756 property_set_fd_init = 1;
757 }
758 if (!signal_fd_init && get_signal_fd() > 0) {
759 ufds[fd_count].fd = get_signal_fd();
760 ufds[fd_count].events = POLLIN;
761 ufds[fd_count].revents = 0;
762 fd_count++;
763 signal_fd_init = 1;
764 }
765 if (!keychord_fd_init && get_keychord_fd() > 0) {
766 ufds[fd_count].fd = get_keychord_fd();
767 ufds[fd_count].events = POLLIN;
768 ufds[fd_count].revents = 0;
769 fd_count++;
770 keychord_fd_init = 1;
771 }
772
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700773 if (process_needs_restart) {
774 timeout = (process_needs_restart - gettime()) * 1000;
775 if (timeout < 0)
776 timeout = 0;
777 }
778
Colin Crossebd46132010-04-22 11:52:23 -0700779 if (!action_queue_empty() || cur_action)
Colin Crossebc6ff12010-04-13 19:52:01 -0700780 timeout = 0;
781
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700782#if BOOTCHART
783 if (bootchart_count > 0) {
784 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
785 timeout = BOOTCHART_POLLING_MS;
786 if (bootchart_step() < 0 || --bootchart_count == 0) {
787 bootchart_finish();
788 bootchart_count = 0;
789 }
790 }
791#endif
Colin Crossebc6ff12010-04-13 19:52:01 -0700792
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800793 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700794 if (nr <= 0)
795 continue;
796
Colin Crossebc6ff12010-04-13 19:52:01 -0700797 for (i = 0; i < fd_count; i++) {
798 if (ufds[i].revents == POLLIN) {
Colin Crossf83d0b92010-04-21 12:04:20 -0700799 if (ufds[i].fd == get_property_set_fd())
Colin Crossebc6ff12010-04-13 19:52:01 -0700800 handle_property_set_fd();
801 else if (ufds[i].fd == get_keychord_fd())
802 handle_keychord();
803 else if (ufds[i].fd == get_signal_fd())
804 handle_signal();
805 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700806 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700807 }
808
809 return 0;
810}