blob: c3be93d03db2a7f7a645094406bd5db7acf22b51 [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
Dima Zavinda04c522011-09-01 17:09:44 -070036#include <cutils/list.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070037#include <cutils/sockets.h>
San Mehat4e221f02010-02-25 14:19:50 -080038#include <cutils/iosched_policy.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070039#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070040#include <termios.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070041
42#include <sys/system_properties.h>
43
44#include "devices.h"
45#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070046#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];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070062static char bootmode[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070063static char hardware[32];
64static unsigned revision = 0;
65static char qemu[32];
66
Colin Crossebc6ff12010-04-13 19:52:01 -070067static struct action *cur_action = NULL;
68static struct command *cur_command = NULL;
69static struct listnode *command_queue = NULL;
70
Colin Cross9c5366b2010-04-13 19:48:59 -070071void notify_service_state(const char *name, const char *state)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072{
73 char pname[PROP_NAME_MAX];
74 int len = strlen(name);
75 if ((len + 10) > PROP_NAME_MAX)
76 return;
77 snprintf(pname, sizeof(pname), "init.svc.%s", name);
78 property_set(pname, state);
79}
80
81static int have_console;
82static char *console_name = "/dev/console";
83static time_t process_needs_restart;
84
85static const char *ENV[32];
86
87/* add_environment - add "key=value" to the current environment */
88int add_environment(const char *key, const char *val)
89{
90 int n;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -070091
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070092 for (n = 0; n < 31; n++) {
93 if (!ENV[n]) {
94 size_t len = strlen(key) + strlen(val) + 2;
95 char *entry = malloc(len);
96 snprintf(entry, len, "%s=%s", key, val);
97 ENV[n] = entry;
98 return 0;
99 }
100 }
101
102 return 1;
103}
104
105static void zap_stdio(void)
106{
107 int fd;
108 fd = open("/dev/null", O_RDWR);
109 dup2(fd, 0);
110 dup2(fd, 1);
111 dup2(fd, 2);
112 close(fd);
113}
114
115static void open_console()
116{
117 int fd;
118 if ((fd = open(console_name, O_RDWR)) < 0) {
119 fd = open("/dev/null", O_RDWR);
120 }
121 dup2(fd, 0);
122 dup2(fd, 1);
123 dup2(fd, 2);
124 close(fd);
125}
126
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700127static void publish_socket(const char *name, int fd)
128{
129 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
130 char val[64];
131
132 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
133 name,
134 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
135 snprintf(val, sizeof(val), "%d", fd);
136 add_environment(key, val);
137
138 /* make sure we don't close-on-exec */
139 fcntl(fd, F_SETFD, 0);
140}
141
San Mehatf24e2522009-05-19 13:30:46 -0700142void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700143{
144 struct stat s;
145 pid_t pid;
146 int needs_console;
147 int n;
148
Ken Sumrall752923c2010-12-03 16:33:31 -0800149 /* starting a service removes it from the disabled or reset
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700150 * state and immediately takes it out of the restarting
151 * state if it was in there
152 */
Ken Sumrall752923c2010-12-03 16:33:31 -0800153 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700154 svc->time_started = 0;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700155
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700156 /* running processes require no additional work -- if
157 * they're in the process of exiting, we've ensured
158 * that they will immediately restart on exit, unless
159 * they are ONESHOT
160 */
161 if (svc->flags & SVC_RUNNING) {
162 return;
163 }
164
165 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
166 if (needs_console && (!have_console)) {
167 ERROR("service '%s' requires console\n", svc->name);
168 svc->flags |= SVC_DISABLED;
169 return;
170 }
171
172 if (stat(svc->args[0], &s) != 0) {
173 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
174 svc->flags |= SVC_DISABLED;
175 return;
176 }
177
San Mehatf24e2522009-05-19 13:30:46 -0700178 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700179 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
180 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700181 svc->flags |= SVC_DISABLED;
182 return;
183 }
184
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700185 NOTICE("starting '%s'\n", svc->name);
186
187 pid = fork();
188
189 if (pid == 0) {
190 struct socketinfo *si;
191 struct svcenvinfo *ei;
192 char tmp[32];
193 int fd, sz;
194
Colin Cross3294bbb2010-04-19 17:11:33 -0700195 if (properties_inited()) {
196 get_property_workspace(&fd, &sz);
197 sprintf(tmp, "%d,%d", dup(fd), sz);
198 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
199 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700200
201 for (ei = svc->envvars; ei; ei = ei->next)
202 add_environment(ei->name, ei->value);
203
204 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400205 int socket_type = (
206 !strcmp(si->type, "stream") ? SOCK_STREAM :
207 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
208 int s = create_socket(si->name, socket_type,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700209 si->perm, si->uid, si->gid);
210 if (s >= 0) {
211 publish_socket(si->name, s);
212 }
213 }
214
San Mehat4e221f02010-02-25 14:19:50 -0800215 if (svc->ioprio_class != IoSchedClass_NONE) {
216 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
217 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
218 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
219 }
220 }
221
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700222 if (needs_console) {
223 setsid();
224 open_console();
225 } else {
226 zap_stdio();
227 }
228
229#if 0
230 for (n = 0; svc->args[n]; n++) {
231 INFO("args[%d] = '%s'\n", n, svc->args[n]);
232 }
233 for (n = 0; ENV[n]; n++) {
234 INFO("env[%d] = '%s'\n", n, ENV[n]);
235 }
236#endif
237
238 setpgid(0, getpid());
239
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800240 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700241 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800242 if (setgid(svc->gid) != 0) {
243 ERROR("setgid failed: %s\n", strerror(errno));
244 _exit(127);
245 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700246 }
247 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800248 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
249 ERROR("setgroups failed: %s\n", strerror(errno));
250 _exit(127);
251 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700252 }
253 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800254 if (setuid(svc->uid) != 0) {
255 ERROR("setuid failed: %s\n", strerror(errno));
256 _exit(127);
257 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700258 }
259
San Mehat8ad15682009-05-20 08:50:40 -0700260 if (!dynamic_args) {
261 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
262 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
263 }
264 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700265 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700266 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700267 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700268 char *next = tmp;
269 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700270
271 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700272 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700273
San Mehatd4cdd132009-05-20 09:52:16 -0700274 while((bword = strsep(&next, " "))) {
275 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700276 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700277 break;
San Mehatf24e2522009-05-19 13:30:46 -0700278 }
279 arg_ptrs[arg_idx] = '\0';
280 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100281 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700282 _exit(127);
283 }
284
285 if (pid < 0) {
286 ERROR("failed to start '%s'\n", svc->name);
287 svc->pid = 0;
288 return;
289 }
290
291 svc->time_started = gettime();
292 svc->pid = pid;
293 svc->flags |= SVC_RUNNING;
294
Colin Cross3294bbb2010-04-19 17:11:33 -0700295 if (properties_inited())
296 notify_service_state(svc->name, "running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700297}
298
Ken Sumrall752923c2010-12-03 16:33:31 -0800299/* The how field should be either SVC_DISABLED or SVC_RESET */
300static void service_stop_or_reset(struct service *svc, int how)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301{
302 /* we are no longer running, nor should we
303 * attempt to restart
304 */
305 svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING));
306
Ken Sumrall752923c2010-12-03 16:33:31 -0800307 if ((how != SVC_DISABLED) && (how != SVC_RESET)) {
308 /* Hrm, an illegal flag. Default to SVC_DISABLED */
309 how = SVC_DISABLED;
310 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700311 /* if the service has not yet started, prevent
312 * it from auto-starting with its class
313 */
Ken Sumralla2864802011-10-26 16:56:00 -0700314 if (how == SVC_RESET) {
315 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
316 } else {
317 svc->flags |= how;
318 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700319
320 if (svc->pid) {
321 NOTICE("service '%s' is being killed\n", svc->name);
Ken Sumrall752923c2010-12-03 16:33:31 -0800322 kill(-svc->pid, SIGKILL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700323 notify_service_state(svc->name, "stopping");
324 } else {
325 notify_service_state(svc->name, "stopped");
326 }
327}
328
Ken Sumrall752923c2010-12-03 16:33:31 -0800329void service_reset(struct service *svc)
330{
331 service_stop_or_reset(svc, SVC_RESET);
332}
333
334void service_stop(struct service *svc)
335{
336 service_stop_or_reset(svc, SVC_DISABLED);
337}
338
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700339void property_changed(const char *name, const char *value)
340{
Colin Crossebc6ff12010-04-13 19:52:01 -0700341 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700342 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700343}
344
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700345static void restart_service_if_needed(struct service *svc)
346{
347 time_t next_start_time = svc->time_started + 5;
348
349 if (next_start_time <= gettime()) {
350 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700351 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700352 return;
353 }
354
355 if ((next_start_time < process_needs_restart) ||
356 (process_needs_restart == 0)) {
357 process_needs_restart = next_start_time;
358 }
359}
360
361static void restart_processes()
362{
363 process_needs_restart = 0;
364 service_for_each_flags(SVC_RESTARTING,
365 restart_service_if_needed);
366}
367
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700368static void msg_start(const char *name)
369{
San Mehatf24e2522009-05-19 13:30:46 -0700370 struct service *svc;
371 char *tmp = NULL;
372 char *args = NULL;
373
374 if (!strchr(name, ':'))
375 svc = service_find_by_name(name);
376 else {
377 tmp = strdup(name);
San Mehatf24e2522009-05-19 13:30:46 -0700378 args = strchr(tmp, ':');
379 *args = '\0';
380 args++;
381
382 svc = service_find_by_name(tmp);
383 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700384
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700385 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700386 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700387 } else {
388 ERROR("no such service '%s'\n", name);
389 }
San Mehatf24e2522009-05-19 13:30:46 -0700390 if (tmp)
391 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700392}
393
394static void msg_stop(const char *name)
395{
396 struct service *svc = service_find_by_name(name);
397
398 if (svc) {
399 service_stop(svc);
400 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700401 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700402 }
403}
404
405void handle_control_message(const char *msg, const char *arg)
406{
407 if (!strcmp(msg,"start")) {
408 msg_start(arg);
409 } else if (!strcmp(msg,"stop")) {
410 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700411 } else if (!strcmp(msg,"restart")) {
412 msg_stop(arg);
413 msg_start(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700414 } else {
415 ERROR("unknown control msg '%s'\n", msg);
416 }
417}
418
Colin Crossebc6ff12010-04-13 19:52:01 -0700419static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700420{
421 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700422 node = list_head(&act->commands);
Dima Zavin3bea0792011-08-26 13:59:18 -0700423 if (!node || list_empty(&act->commands))
Colin Crossebc6ff12010-04-13 19:52:01 -0700424 return NULL;
425
426 return node_to_item(node, struct command, clist);
427}
428
429static struct command *get_next_command(struct action *act, struct command *cmd)
430{
431 struct listnode *node;
432 node = cmd->clist.next;
433 if (!node)
434 return NULL;
435 if (node == &act->commands)
436 return NULL;
437
438 return node_to_item(node, struct command, clist);
439}
440
441static int is_last_command(struct action *act, struct command *cmd)
442{
443 return (list_tail(&act->commands) == &cmd->clist);
444}
445
446void execute_one_command(void)
447{
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700448 int ret;
449
Colin Crossebc6ff12010-04-13 19:52:01 -0700450 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
451 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700452 cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -0700453 if (!cur_action)
454 return;
455 INFO("processing action %p (%s)\n", cur_action, cur_action->name);
456 cur_command = get_first_command(cur_action);
457 } else {
458 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700459 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700460
461 if (!cur_command)
462 return;
463
464 ret = cur_command->func(cur_command->nargs, cur_command->args);
465 INFO("command '%s' r=%d\n", cur_command->args[0], ret);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700466}
467
Colin Crossf83d0b92010-04-21 12:04:20 -0700468static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700469{
Colin Crossf83d0b92010-04-21 12:04:20 -0700470 int ret;
471 INFO("wait for %s\n", coldboot_done);
472 ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT);
473 if (ret)
474 ERROR("Timed out waiting for %s\n", coldboot_done);
475 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700476}
477
Colin Crossebc6ff12010-04-13 19:52:01 -0700478static int keychord_init_action(int nargs, char **args)
479{
480 keychord_init();
481 return 0;
482}
483
484static int console_init_action(int nargs, char **args)
485{
486 int fd;
487 char tmp[PROP_VALUE_MAX];
488
489 if (console[0]) {
490 snprintf(tmp, sizeof(tmp), "/dev/%s", console);
491 console_name = strdup(tmp);
492 }
493
494 fd = open(console_name, O_RDWR);
495 if (fd >= 0)
496 have_console = 1;
497 close(fd);
498
499 if( load_565rle_image(INIT_IMAGE_FILE) ) {
500 fd = open("/dev/tty0", O_WRONLY);
501 if (fd >= 0) {
502 const char *msg;
503 msg = "\n"
504 "\n"
505 "\n"
506 "\n"
507 "\n"
508 "\n"
509 "\n" // console is 40 cols x 30 lines
510 "\n"
511 "\n"
512 "\n"
513 "\n"
514 "\n"
515 "\n"
516 "\n"
517 " A N D R O I D ";
518 write(fd, msg, strlen(msg));
519 close(fd);
520 }
521 }
522 return 0;
523}
524
Dima Zavin88861122011-12-19 11:21:32 -0800525static void import_kernel_nv(char *name, int for_emulator)
526{
527 char *value = strchr(name, '=');
528 int name_len = strlen(name);
529
530 if (value == 0) return;
531 *value++ = 0;
532 if (name_len == 0) return;
533
534 if (for_emulator) {
535 /* in the emulator, export any kernel option with the
536 * ro.kernel. prefix */
537 char buff[PROP_NAME_MAX];
538 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
539
540 if (len < (int)sizeof(buff))
541 property_set( buff, value );
542 return;
543 }
544
545 if (!strcmp(name,"qemu")) {
546 strlcpy(qemu, value, sizeof(qemu));
547 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
548 const char *boot_prop_name = name + 12;
549 char prop[PROP_NAME_MAX];
550 int cnt;
551
552 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
553 if (cnt < PROP_NAME_MAX)
554 property_set(prop, value);
555 }
556}
557
558static void export_kernel_boot_props(void)
Colin Crossebc6ff12010-04-13 19:52:01 -0700559{
560 char tmp[PROP_VALUE_MAX];
Dima Zavin88861122011-12-19 11:21:32 -0800561 const char *pval;
562 unsigned i;
563 struct {
564 const char *src_prop;
565 const char *dest_prop;
566 const char *def_val;
567 } prop_map[] = {
568 { "ro.boot.serialno", "ro.serialno", "", },
569 { "ro.boot.mode", "ro.bootmode", "unknown", },
570 { "ro.boot.baseband", "ro.baseband", "unknown", },
571 { "ro.boot.carrier", "ro.carrier", "unknown", },
572 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
573 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700574
Dima Zavin88861122011-12-19 11:21:32 -0800575 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
576 pval = property_get(prop_map[i].src_prop);
577 property_set(prop_map[i].dest_prop, pval ?: prop_map[i].def_val);
578 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700579
Dima Zavin88861122011-12-19 11:21:32 -0800580 pval = property_get("ro.boot.console");
581 if (pval)
582 strlcpy(console, pval, sizeof(console));
583
584 /* save a copy for init's usage during boot */
585 strlcpy(bootmode, property_get("ro.bootmode"), sizeof(bootmode));
586
587 /* if this was given on kernel command line, override what we read
588 * before (e.g. from /proc/cpuinfo), if anything */
589 pval = property_get("ro.boot.hardware");
590 if (pval)
591 strlcpy(hardware, pval, sizeof(hardware));
592 property_set("ro.hardware", hardware);
593
594 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
595 property_set("ro.revision", tmp);
596
597 /* TODO: these are obsolete. We should delete them */
Colin Crossebc6ff12010-04-13 19:52:01 -0700598 if (!strcmp(bootmode,"factory"))
599 property_set("ro.factorytest", "1");
600 else if (!strcmp(bootmode,"factory2"))
601 property_set("ro.factorytest", "2");
602 else
603 property_set("ro.factorytest", "0");
Dima Zavin88861122011-12-19 11:21:32 -0800604}
Colin Crossebc6ff12010-04-13 19:52:01 -0700605
Dima Zavin88861122011-12-19 11:21:32 -0800606static void process_kernel_cmdline(void)
607{
608 /* don't expose the raw commandline to nonpriv processes */
609 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700610
Dima Zavin88861122011-12-19 11:21:32 -0800611 /* first pass does the common stuff, and finds if we are in qemu.
612 * second pass is only necessary for qemu to export all kernel params
613 * as props.
614 */
615 import_kernel_cmdline(0, import_kernel_nv);
616 if (qemu[0])
617 import_kernel_cmdline(1, import_kernel_nv);
618
619 /* now propogate the info given on command line to internal variables
620 * used by init as well as the current required properties
621 */
622 export_kernel_boot_props();
Colin Crossebc6ff12010-04-13 19:52:01 -0700623}
624
625static int property_service_init_action(int nargs, char **args)
626{
627 /* read any property files on system or data and
628 * fire up the property service. This must happen
629 * after the ro.foo properties are set above so
630 * that /data/local.prop cannot interfere with them.
631 */
632 start_property_service();
633 return 0;
634}
635
636static int signal_init_action(int nargs, char **args)
637{
638 signal_init();
639 return 0;
640}
641
642static int check_startup_action(int nargs, char **args)
643{
644 /* make sure we actually have all the pieces we need */
Colin Crossf83d0b92010-04-21 12:04:20 -0700645 if ((get_property_set_fd() < 0) ||
Colin Crossebc6ff12010-04-13 19:52:01 -0700646 (get_signal_fd() < 0)) {
647 ERROR("init startup failure\n");
648 exit(1);
649 }
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700650
651 /* signal that we hit this point */
652 unlink("/dev/.booting");
653
Colin Crossebc6ff12010-04-13 19:52:01 -0700654 return 0;
655}
656
657static int queue_property_triggers_action(int nargs, char **args)
658{
659 queue_all_property_triggers();
660 /* enable property triggers */
661 property_triggers_enabled = 1;
662 return 0;
663}
664
665#if BOOTCHART
666static int bootchart_init_action(int nargs, char **args)
667{
668 bootchart_count = bootchart_init();
669 if (bootchart_count < 0) {
670 ERROR("bootcharting init failure\n");
671 } else if (bootchart_count > 0) {
672 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
673 } else {
674 NOTICE("bootcharting ignored\n");
675 }
Carl-Emil Lagerstedt9ab81902011-01-14 09:35:30 +0100676
677 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700678}
679#endif
680
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700681int main(int argc, char **argv)
682{
Colin Crossebc6ff12010-04-13 19:52:01 -0700683 int fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700684 struct pollfd ufds[4];
685 char *tmpdev;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800686 char* debuggable;
Colin Crossebc6ff12010-04-13 19:52:01 -0700687 char tmp[32];
Colin Crossebc6ff12010-04-13 19:52:01 -0700688 int property_set_fd_init = 0;
689 int signal_fd_init = 0;
690 int keychord_fd_init = 0;
Dima Zavin65a83602011-12-16 14:18:06 -0800691 bool is_charger = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700692
Colin Crossf83d0b92010-04-21 12:04:20 -0700693 if (!strcmp(basename(argv[0]), "ueventd"))
694 return ueventd_main(argc, argv);
695
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700696 /* clear the umask */
697 umask(0);
698
699 /* Get the basic filesystem setup we need put
700 * together in the initramdisk on / and then we'll
701 * let the rc file figure out the rest.
702 */
703 mkdir("/dev", 0755);
704 mkdir("/proc", 0755);
705 mkdir("/sys", 0755);
706
Nick Kralevich150f19e2010-06-22 16:35:43 -0700707 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700708 mkdir("/dev/pts", 0755);
709 mkdir("/dev/socket", 0755);
710 mount("devpts", "/dev/pts", "devpts", 0, NULL);
711 mount("proc", "/proc", "proc", 0, NULL);
712 mount("sysfs", "/sys", "sysfs", 0, NULL);
713
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700714 /* indicate that booting is in progress to background fw loaders, etc */
715 close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000));
716
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700717 /* We must have some place other than / to create the
718 * device nodes for kmsg and null, otherwise we won't
719 * be able to remount / read-only later on.
720 * Now that tmpfs is mounted on /dev, we can actually
721 * talk to the outside world.
722 */
723 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -0700724 klog_init();
Dima Zavin88861122011-12-19 11:21:32 -0800725 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700726
Colin Crossf83d0b92010-04-21 12:04:20 -0700727 get_hardware_name(hardware, &revision);
Dima Zavin65a83602011-12-16 14:18:06 -0800728
Dima Zavin88861122011-12-19 11:21:32 -0800729 process_kernel_cmdline();
730
Dima Zavin65a83602011-12-16 14:18:06 -0800731 is_charger = !strcmp(bootmode, "charger");
732
733 INFO("property init\n");
Dima Zavin88861122011-12-19 11:21:32 -0800734 if (!is_charger)
735 property_load_boot_defaults();
Dima Zavin65a83602011-12-16 14:18:06 -0800736
737 INFO("reading config file\n");
738 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700739
740 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700741
Colin Crossf83d0b92010-04-21 12:04:20 -0700742 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Colin Crossebc6ff12010-04-13 19:52:01 -0700743 queue_builtin_action(keychord_init_action, "keychord_init");
744 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700745
Dima Zavinca47cef2011-08-24 15:28:23 -0700746 /* execute all the boot actions to get us started */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700747 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -0700748
749 /* skip mounting filesystems in charger mode */
Dima Zavin65a83602011-12-16 14:18:06 -0800750 if (!is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -0700751 action_for_each_trigger("early-fs", action_add_queue_tail);
752 action_for_each_trigger("fs", action_add_queue_tail);
753 action_for_each_trigger("post-fs", action_add_queue_tail);
754 action_for_each_trigger("post-fs-data", action_add_queue_tail);
755 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700756
Colin Crossebc6ff12010-04-13 19:52:01 -0700757 queue_builtin_action(property_service_init_action, "property_service_init");
758 queue_builtin_action(signal_init_action, "signal_init");
759 queue_builtin_action(check_startup_action, "check_startup");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700760
Dima Zavin65a83602011-12-16 14:18:06 -0800761 if (is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -0700762 action_for_each_trigger("charger", action_add_queue_tail);
763 } else {
764 action_for_each_trigger("early-boot", action_add_queue_tail);
765 action_for_each_trigger("boot", action_add_queue_tail);
766 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700767
768 /* run all property triggers based on current state of the properties */
Colin Crossebc6ff12010-04-13 19:52:01 -0700769 queue_builtin_action(queue_property_triggers_action, "queue_propety_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700770
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700771
772#if BOOTCHART
Colin Crossebc6ff12010-04-13 19:52:01 -0700773 queue_builtin_action(bootchart_init_action, "bootchart_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700774#endif
775
776 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800777 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700778
Colin Crossebc6ff12010-04-13 19:52:01 -0700779 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700780 restart_processes();
781
Colin Crossebc6ff12010-04-13 19:52:01 -0700782 if (!property_set_fd_init && get_property_set_fd() > 0) {
783 ufds[fd_count].fd = get_property_set_fd();
784 ufds[fd_count].events = POLLIN;
785 ufds[fd_count].revents = 0;
786 fd_count++;
787 property_set_fd_init = 1;
788 }
789 if (!signal_fd_init && get_signal_fd() > 0) {
790 ufds[fd_count].fd = get_signal_fd();
791 ufds[fd_count].events = POLLIN;
792 ufds[fd_count].revents = 0;
793 fd_count++;
794 signal_fd_init = 1;
795 }
796 if (!keychord_fd_init && get_keychord_fd() > 0) {
797 ufds[fd_count].fd = get_keychord_fd();
798 ufds[fd_count].events = POLLIN;
799 ufds[fd_count].revents = 0;
800 fd_count++;
801 keychord_fd_init = 1;
802 }
803
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700804 if (process_needs_restart) {
805 timeout = (process_needs_restart - gettime()) * 1000;
806 if (timeout < 0)
807 timeout = 0;
808 }
809
Colin Crossebd46132010-04-22 11:52:23 -0700810 if (!action_queue_empty() || cur_action)
Colin Crossebc6ff12010-04-13 19:52:01 -0700811 timeout = 0;
812
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700813#if BOOTCHART
814 if (bootchart_count > 0) {
815 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
816 timeout = BOOTCHART_POLLING_MS;
817 if (bootchart_step() < 0 || --bootchart_count == 0) {
818 bootchart_finish();
819 bootchart_count = 0;
820 }
821 }
822#endif
Colin Crossebc6ff12010-04-13 19:52:01 -0700823
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800824 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700825 if (nr <= 0)
826 continue;
827
Colin Crossebc6ff12010-04-13 19:52:01 -0700828 for (i = 0; i < fd_count; i++) {
829 if (ufds[i].revents == POLLIN) {
Colin Crossf83d0b92010-04-21 12:04:20 -0700830 if (ufds[i].fd == get_property_set_fd())
Colin Crossebc6ff12010-04-13 19:52:01 -0700831 handle_property_set_fd();
832 else if (ufds[i].fd == get_keychord_fd())
833 handle_keychord();
834 else if (ufds[i].fd == get_signal_fd())
835 handle_signal();
836 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700837 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700838 }
839
840 return 0;
841}