blob: 48d9c9ae634eba82395ddc8e9b06223d332f871e [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>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050034
35#ifdef HAVE_SELINUX
36#include <sys/mman.h>
37#include <selinux/selinux.h>
38#include <selinux/label.h>
39#endif
40
Colin Crossf83d0b92010-04-21 12:04:20 -070041#include <libgen.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070042
Dima Zavinda04c522011-09-01 17:09:44 -070043#include <cutils/list.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070044#include <cutils/sockets.h>
San Mehat4e221f02010-02-25 14:19:50 -080045#include <cutils/iosched_policy.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070046#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070047#include <termios.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070048
49#include <sys/system_properties.h>
50
51#include "devices.h"
52#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070053#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054#include "property_service.h"
The Android Open Source Project35237d12008-12-17 18:08:08 -080055#include "bootchart.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070056#include "signal_handler.h"
Colin Crossa8666952010-04-13 19:20:44 -070057#include "keychords.h"
Colin Cross6310a822010-04-20 14:29:05 -070058#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070059#include "util.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070060#include "ueventd.h"
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070061#include "watchdogd.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070062
Stephen Smalleye46f9d52012-01-13 08:48:47 -050063#ifdef HAVE_SELINUX
64struct selabel_handle *sehandle;
65#endif
66
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070067static int property_triggers_enabled = 0;
68
69#if BOOTCHART
70static int bootchart_count;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070071#endif
72
73static char console[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070074static char bootmode[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075static char hardware[32];
76static unsigned revision = 0;
77static char qemu[32];
78
Stephen Smalleye46f9d52012-01-13 08:48:47 -050079#ifdef HAVE_SELINUX
80static int selinux_enabled = 1;
81static int selinux_enforcing = 0;
82#endif
83
Colin Crossebc6ff12010-04-13 19:52:01 -070084static struct action *cur_action = NULL;
85static struct command *cur_command = NULL;
86static struct listnode *command_queue = NULL;
87
Colin Cross9c5366b2010-04-13 19:48:59 -070088void notify_service_state(const char *name, const char *state)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070089{
90 char pname[PROP_NAME_MAX];
91 int len = strlen(name);
92 if ((len + 10) > PROP_NAME_MAX)
93 return;
94 snprintf(pname, sizeof(pname), "init.svc.%s", name);
95 property_set(pname, state);
96}
97
98static int have_console;
99static char *console_name = "/dev/console";
100static time_t process_needs_restart;
101
102static const char *ENV[32];
103
104/* add_environment - add "key=value" to the current environment */
105int add_environment(const char *key, const char *val)
106{
107 int n;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700108
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700109 for (n = 0; n < 31; n++) {
110 if (!ENV[n]) {
111 size_t len = strlen(key) + strlen(val) + 2;
112 char *entry = malloc(len);
113 snprintf(entry, len, "%s=%s", key, val);
114 ENV[n] = entry;
115 return 0;
116 }
117 }
118
119 return 1;
120}
121
122static void zap_stdio(void)
123{
124 int fd;
125 fd = open("/dev/null", O_RDWR);
126 dup2(fd, 0);
127 dup2(fd, 1);
128 dup2(fd, 2);
129 close(fd);
130}
131
132static void open_console()
133{
134 int fd;
135 if ((fd = open(console_name, O_RDWR)) < 0) {
136 fd = open("/dev/null", O_RDWR);
137 }
Colin Cross50fb5a62012-03-18 15:38:19 -0700138 ioctl(fd, TIOCSCTTY, 0);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700139 dup2(fd, 0);
140 dup2(fd, 1);
141 dup2(fd, 2);
142 close(fd);
143}
144
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700145static void publish_socket(const char *name, int fd)
146{
147 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
148 char val[64];
149
150 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
151 name,
152 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
153 snprintf(val, sizeof(val), "%d", fd);
154 add_environment(key, val);
155
156 /* make sure we don't close-on-exec */
157 fcntl(fd, F_SETFD, 0);
158}
159
San Mehatf24e2522009-05-19 13:30:46 -0700160void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700161{
162 struct stat s;
163 pid_t pid;
164 int needs_console;
165 int n;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500166#ifdef HAVE_SELINUX
167 char *scon = NULL;
168 int rc;
169#endif
Ken Sumrall752923c2010-12-03 16:33:31 -0800170 /* starting a service removes it from the disabled or reset
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700171 * state and immediately takes it out of the restarting
172 * state if it was in there
173 */
Ken Sumrall752923c2010-12-03 16:33:31 -0800174 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700175 svc->time_started = 0;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700176
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700177 /* running processes require no additional work -- if
178 * they're in the process of exiting, we've ensured
179 * that they will immediately restart on exit, unless
180 * they are ONESHOT
181 */
182 if (svc->flags & SVC_RUNNING) {
183 return;
184 }
185
186 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
187 if (needs_console && (!have_console)) {
188 ERROR("service '%s' requires console\n", svc->name);
189 svc->flags |= SVC_DISABLED;
190 return;
191 }
192
193 if (stat(svc->args[0], &s) != 0) {
194 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
195 svc->flags |= SVC_DISABLED;
196 return;
197 }
198
San Mehatf24e2522009-05-19 13:30:46 -0700199 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700200 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
201 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700202 svc->flags |= SVC_DISABLED;
203 return;
204 }
205
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500206#ifdef HAVE_SELINUX
207 if (is_selinux_enabled() > 0) {
208 char *mycon = NULL, *fcon = NULL;
209
210 INFO("computing context for service '%s'\n", svc->args[0]);
211 rc = getcon(&mycon);
212 if (rc < 0) {
213 ERROR("could not get context while starting '%s'\n", svc->name);
214 return;
215 }
216
217 rc = getfilecon(svc->args[0], &fcon);
218 if (rc < 0) {
219 ERROR("could not get context while starting '%s'\n", svc->name);
220 freecon(mycon);
221 return;
222 }
223
224 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon);
225 freecon(mycon);
226 freecon(fcon);
227 if (rc < 0) {
228 ERROR("could not get context while starting '%s'\n", svc->name);
229 return;
230 }
231 }
232#endif
233
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700234 NOTICE("starting '%s'\n", svc->name);
235
236 pid = fork();
237
238 if (pid == 0) {
239 struct socketinfo *si;
240 struct svcenvinfo *ei;
241 char tmp[32];
242 int fd, sz;
243
Nick Kralevich6ebf12f2012-03-26 09:09:11 -0700244 umask(077);
Colin Cross3294bbb2010-04-19 17:11:33 -0700245 if (properties_inited()) {
246 get_property_workspace(&fd, &sz);
247 sprintf(tmp, "%d,%d", dup(fd), sz);
248 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
249 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700250
251 for (ei = svc->envvars; ei; ei = ei->next)
252 add_environment(ei->name, ei->value);
253
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500254#ifdef HAVE_SELINUX
255 setsockcreatecon(scon);
256#endif
257
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700258 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400259 int socket_type = (
260 !strcmp(si->type, "stream") ? SOCK_STREAM :
261 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
262 int s = create_socket(si->name, socket_type,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700263 si->perm, si->uid, si->gid);
264 if (s >= 0) {
265 publish_socket(si->name, s);
266 }
267 }
268
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500269#ifdef HAVE_SELINUX
270 freecon(scon);
271 scon = NULL;
272 setsockcreatecon(NULL);
273#endif
274
San Mehat4e221f02010-02-25 14:19:50 -0800275 if (svc->ioprio_class != IoSchedClass_NONE) {
276 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
277 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
278 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
279 }
280 }
281
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700282 if (needs_console) {
283 setsid();
284 open_console();
285 } else {
286 zap_stdio();
287 }
288
289#if 0
290 for (n = 0; svc->args[n]; n++) {
291 INFO("args[%d] = '%s'\n", n, svc->args[n]);
292 }
293 for (n = 0; ENV[n]; n++) {
294 INFO("env[%d] = '%s'\n", n, ENV[n]);
295 }
296#endif
297
298 setpgid(0, getpid());
299
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800300 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800302 if (setgid(svc->gid) != 0) {
303 ERROR("setgid failed: %s\n", strerror(errno));
304 _exit(127);
305 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700306 }
307 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800308 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
309 ERROR("setgroups failed: %s\n", strerror(errno));
310 _exit(127);
311 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700312 }
313 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800314 if (setuid(svc->uid) != 0) {
315 ERROR("setuid failed: %s\n", strerror(errno));
316 _exit(127);
317 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700318 }
319
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500320#ifdef HAVE_SELINUX
321 if (svc->seclabel) {
322 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) {
323 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
324 _exit(127);
325 }
326 }
327#endif
328
San Mehat8ad15682009-05-20 08:50:40 -0700329 if (!dynamic_args) {
330 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
331 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
332 }
333 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700334 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700335 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700336 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700337 char *next = tmp;
338 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700339
340 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700341 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700342
San Mehatd4cdd132009-05-20 09:52:16 -0700343 while((bword = strsep(&next, " "))) {
344 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700345 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700346 break;
San Mehatf24e2522009-05-19 13:30:46 -0700347 }
348 arg_ptrs[arg_idx] = '\0';
349 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100350 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700351 _exit(127);
352 }
353
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500354#ifdef HAVE_SELINUX
355 freecon(scon);
356#endif
357
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700358 if (pid < 0) {
359 ERROR("failed to start '%s'\n", svc->name);
360 svc->pid = 0;
361 return;
362 }
363
364 svc->time_started = gettime();
365 svc->pid = pid;
366 svc->flags |= SVC_RUNNING;
367
Colin Cross3294bbb2010-04-19 17:11:33 -0700368 if (properties_inited())
369 notify_service_state(svc->name, "running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700370}
371
Ken Sumrall752923c2010-12-03 16:33:31 -0800372/* The how field should be either SVC_DISABLED or SVC_RESET */
373static void service_stop_or_reset(struct service *svc, int how)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700374{
375 /* we are no longer running, nor should we
376 * attempt to restart
377 */
378 svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING));
379
Ken Sumrall752923c2010-12-03 16:33:31 -0800380 if ((how != SVC_DISABLED) && (how != SVC_RESET)) {
381 /* Hrm, an illegal flag. Default to SVC_DISABLED */
382 how = SVC_DISABLED;
383 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700384 /* if the service has not yet started, prevent
385 * it from auto-starting with its class
386 */
Ken Sumralla2864802011-10-26 16:56:00 -0700387 if (how == SVC_RESET) {
388 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
389 } else {
390 svc->flags |= how;
391 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700392
393 if (svc->pid) {
394 NOTICE("service '%s' is being killed\n", svc->name);
Ken Sumrall752923c2010-12-03 16:33:31 -0800395 kill(-svc->pid, SIGKILL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700396 notify_service_state(svc->name, "stopping");
397 } else {
398 notify_service_state(svc->name, "stopped");
399 }
400}
401
Ken Sumrall752923c2010-12-03 16:33:31 -0800402void service_reset(struct service *svc)
403{
404 service_stop_or_reset(svc, SVC_RESET);
405}
406
407void service_stop(struct service *svc)
408{
409 service_stop_or_reset(svc, SVC_DISABLED);
410}
411
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700412void property_changed(const char *name, const char *value)
413{
Colin Crossebc6ff12010-04-13 19:52:01 -0700414 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700415 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700416}
417
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700418static void restart_service_if_needed(struct service *svc)
419{
420 time_t next_start_time = svc->time_started + 5;
421
422 if (next_start_time <= gettime()) {
423 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700424 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700425 return;
426 }
427
428 if ((next_start_time < process_needs_restart) ||
429 (process_needs_restart == 0)) {
430 process_needs_restart = next_start_time;
431 }
432}
433
434static void restart_processes()
435{
436 process_needs_restart = 0;
437 service_for_each_flags(SVC_RESTARTING,
438 restart_service_if_needed);
439}
440
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700441static void msg_start(const char *name)
442{
San Mehatf24e2522009-05-19 13:30:46 -0700443 struct service *svc;
444 char *tmp = NULL;
445 char *args = NULL;
446
447 if (!strchr(name, ':'))
448 svc = service_find_by_name(name);
449 else {
450 tmp = strdup(name);
San Mehatf24e2522009-05-19 13:30:46 -0700451 args = strchr(tmp, ':');
452 *args = '\0';
453 args++;
454
455 svc = service_find_by_name(tmp);
456 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700457
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700458 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700459 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700460 } else {
461 ERROR("no such service '%s'\n", name);
462 }
San Mehatf24e2522009-05-19 13:30:46 -0700463 if (tmp)
464 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700465}
466
467static void msg_stop(const char *name)
468{
469 struct service *svc = service_find_by_name(name);
470
471 if (svc) {
472 service_stop(svc);
473 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700474 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700475 }
476}
477
478void handle_control_message(const char *msg, const char *arg)
479{
480 if (!strcmp(msg,"start")) {
481 msg_start(arg);
482 } else if (!strcmp(msg,"stop")) {
483 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700484 } else if (!strcmp(msg,"restart")) {
485 msg_stop(arg);
486 msg_start(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700487 } else {
488 ERROR("unknown control msg '%s'\n", msg);
489 }
490}
491
Colin Crossebc6ff12010-04-13 19:52:01 -0700492static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700493{
494 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700495 node = list_head(&act->commands);
Dima Zavin3bea0792011-08-26 13:59:18 -0700496 if (!node || list_empty(&act->commands))
Colin Crossebc6ff12010-04-13 19:52:01 -0700497 return NULL;
498
499 return node_to_item(node, struct command, clist);
500}
501
502static struct command *get_next_command(struct action *act, struct command *cmd)
503{
504 struct listnode *node;
505 node = cmd->clist.next;
506 if (!node)
507 return NULL;
508 if (node == &act->commands)
509 return NULL;
510
511 return node_to_item(node, struct command, clist);
512}
513
514static int is_last_command(struct action *act, struct command *cmd)
515{
516 return (list_tail(&act->commands) == &cmd->clist);
517}
518
519void execute_one_command(void)
520{
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700521 int ret;
522
Colin Crossebc6ff12010-04-13 19:52:01 -0700523 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
524 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700525 cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -0700526 if (!cur_action)
527 return;
528 INFO("processing action %p (%s)\n", cur_action, cur_action->name);
529 cur_command = get_first_command(cur_action);
530 } else {
531 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700532 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700533
534 if (!cur_command)
535 return;
536
537 ret = cur_command->func(cur_command->nargs, cur_command->args);
538 INFO("command '%s' r=%d\n", cur_command->args[0], ret);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700539}
540
Colin Crossf83d0b92010-04-21 12:04:20 -0700541static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700542{
Colin Crossf83d0b92010-04-21 12:04:20 -0700543 int ret;
544 INFO("wait for %s\n", coldboot_done);
545 ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT);
546 if (ret)
547 ERROR("Timed out waiting for %s\n", coldboot_done);
548 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700549}
550
Colin Crossebc6ff12010-04-13 19:52:01 -0700551static int keychord_init_action(int nargs, char **args)
552{
553 keychord_init();
554 return 0;
555}
556
557static int console_init_action(int nargs, char **args)
558{
559 int fd;
560 char tmp[PROP_VALUE_MAX];
561
562 if (console[0]) {
563 snprintf(tmp, sizeof(tmp), "/dev/%s", console);
564 console_name = strdup(tmp);
565 }
566
567 fd = open(console_name, O_RDWR);
568 if (fd >= 0)
569 have_console = 1;
570 close(fd);
571
572 if( load_565rle_image(INIT_IMAGE_FILE) ) {
573 fd = open("/dev/tty0", O_WRONLY);
574 if (fd >= 0) {
575 const char *msg;
576 msg = "\n"
577 "\n"
578 "\n"
579 "\n"
580 "\n"
581 "\n"
582 "\n" // console is 40 cols x 30 lines
583 "\n"
584 "\n"
585 "\n"
586 "\n"
587 "\n"
588 "\n"
589 "\n"
590 " A N D R O I D ";
591 write(fd, msg, strlen(msg));
592 close(fd);
593 }
594 }
595 return 0;
596}
597
Dima Zavin5511c842011-12-19 11:21:32 -0800598static void import_kernel_nv(char *name, int for_emulator)
599{
600 char *value = strchr(name, '=');
601 int name_len = strlen(name);
602
603 if (value == 0) return;
604 *value++ = 0;
605 if (name_len == 0) return;
606
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500607#ifdef HAVE_SELINUX
608 if (!strcmp(name,"enforcing")) {
609 selinux_enforcing = atoi(value);
610 } else if (!strcmp(name,"selinux")) {
611 selinux_enabled = atoi(value);
612 }
613#endif
614
Dima Zavin5511c842011-12-19 11:21:32 -0800615 if (for_emulator) {
616 /* in the emulator, export any kernel option with the
617 * ro.kernel. prefix */
618 char buff[PROP_NAME_MAX];
619 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
620
621 if (len < (int)sizeof(buff))
622 property_set( buff, value );
623 return;
624 }
625
626 if (!strcmp(name,"qemu")) {
627 strlcpy(qemu, value, sizeof(qemu));
628 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
629 const char *boot_prop_name = name + 12;
630 char prop[PROP_NAME_MAX];
631 int cnt;
632
633 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
634 if (cnt < PROP_NAME_MAX)
635 property_set(prop, value);
636 }
637}
638
639static void export_kernel_boot_props(void)
Colin Crossebc6ff12010-04-13 19:52:01 -0700640{
641 char tmp[PROP_VALUE_MAX];
Dima Zavin5511c842011-12-19 11:21:32 -0800642 const char *pval;
643 unsigned i;
644 struct {
645 const char *src_prop;
646 const char *dest_prop;
647 const char *def_val;
648 } prop_map[] = {
649 { "ro.boot.serialno", "ro.serialno", "", },
650 { "ro.boot.mode", "ro.bootmode", "unknown", },
651 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800652 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
653 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700654
Dima Zavin5511c842011-12-19 11:21:32 -0800655 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
656 pval = property_get(prop_map[i].src_prop);
657 property_set(prop_map[i].dest_prop, pval ?: prop_map[i].def_val);
658 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700659
Dima Zavin5511c842011-12-19 11:21:32 -0800660 pval = property_get("ro.boot.console");
661 if (pval)
662 strlcpy(console, pval, sizeof(console));
663
664 /* save a copy for init's usage during boot */
665 strlcpy(bootmode, property_get("ro.bootmode"), sizeof(bootmode));
666
667 /* if this was given on kernel command line, override what we read
668 * before (e.g. from /proc/cpuinfo), if anything */
669 pval = property_get("ro.boot.hardware");
670 if (pval)
671 strlcpy(hardware, pval, sizeof(hardware));
672 property_set("ro.hardware", hardware);
673
674 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
675 property_set("ro.revision", tmp);
676
677 /* TODO: these are obsolete. We should delete them */
Colin Crossebc6ff12010-04-13 19:52:01 -0700678 if (!strcmp(bootmode,"factory"))
679 property_set("ro.factorytest", "1");
680 else if (!strcmp(bootmode,"factory2"))
681 property_set("ro.factorytest", "2");
682 else
683 property_set("ro.factorytest", "0");
Dima Zavin5511c842011-12-19 11:21:32 -0800684}
Colin Crossebc6ff12010-04-13 19:52:01 -0700685
Dima Zavin5511c842011-12-19 11:21:32 -0800686static void process_kernel_cmdline(void)
687{
688 /* don't expose the raw commandline to nonpriv processes */
689 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700690
Dima Zavin5511c842011-12-19 11:21:32 -0800691 /* first pass does the common stuff, and finds if we are in qemu.
692 * second pass is only necessary for qemu to export all kernel params
693 * as props.
694 */
695 import_kernel_cmdline(0, import_kernel_nv);
696 if (qemu[0])
697 import_kernel_cmdline(1, import_kernel_nv);
698
699 /* now propogate the info given on command line to internal variables
700 * used by init as well as the current required properties
701 */
702 export_kernel_boot_props();
Colin Crossebc6ff12010-04-13 19:52:01 -0700703}
704
705static int property_service_init_action(int nargs, char **args)
706{
707 /* read any property files on system or data and
708 * fire up the property service. This must happen
709 * after the ro.foo properties are set above so
710 * that /data/local.prop cannot interfere with them.
711 */
712 start_property_service();
713 return 0;
714}
715
716static int signal_init_action(int nargs, char **args)
717{
718 signal_init();
719 return 0;
720}
721
722static int check_startup_action(int nargs, char **args)
723{
724 /* make sure we actually have all the pieces we need */
Colin Crossf83d0b92010-04-21 12:04:20 -0700725 if ((get_property_set_fd() < 0) ||
Colin Crossebc6ff12010-04-13 19:52:01 -0700726 (get_signal_fd() < 0)) {
727 ERROR("init startup failure\n");
728 exit(1);
729 }
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700730
731 /* signal that we hit this point */
732 unlink("/dev/.booting");
733
Colin Crossebc6ff12010-04-13 19:52:01 -0700734 return 0;
735}
736
737static int queue_property_triggers_action(int nargs, char **args)
738{
739 queue_all_property_triggers();
740 /* enable property triggers */
741 property_triggers_enabled = 1;
742 return 0;
743}
744
745#if BOOTCHART
746static int bootchart_init_action(int nargs, char **args)
747{
748 bootchart_count = bootchart_init();
749 if (bootchart_count < 0) {
750 ERROR("bootcharting init failure\n");
751 } else if (bootchart_count > 0) {
752 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
753 } else {
754 NOTICE("bootcharting ignored\n");
755 }
Carl-Emil Lagerstedt9ab81902011-01-14 09:35:30 +0100756
757 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700758}
759#endif
760
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500761#ifdef HAVE_SELINUX
762void selinux_load_policy(void)
763{
764 const char path_prefix[] = "/sepolicy";
765 struct selinux_opt seopts[] = {
766 { SELABEL_OPT_PATH, "/file_contexts" }
767 };
768 char path[PATH_MAX];
769 int fd, rc, vers;
770 struct stat sb;
771 void *map;
772
773 sehandle = NULL;
774 if (!selinux_enabled) {
775 INFO("SELinux: Disabled by command line option\n");
776 return;
777 }
778
779 mkdir(SELINUXMNT, 0755);
780 if (mount("selinuxfs", SELINUXMNT, "selinuxfs", 0, NULL)) {
781 if (errno == ENODEV) {
782 /* SELinux not enabled in kernel */
783 return;
784 }
785 ERROR("SELinux: Could not mount selinuxfs: %s\n",
786 strerror(errno));
787 return;
788 }
789 set_selinuxmnt(SELINUXMNT);
790
791 vers = security_policyvers();
792 if (vers <= 0) {
793 ERROR("SELinux: Unable to read policy version\n");
794 return;
795 }
796 INFO("SELinux: Maximum supported policy version: %d\n", vers);
797
798 snprintf(path, sizeof(path), "%s.%d",
799 path_prefix, vers);
800 fd = open(path, O_RDONLY);
801 while (fd < 0 && errno == ENOENT && --vers) {
802 snprintf(path, sizeof(path), "%s.%d",
803 path_prefix, vers);
804 fd = open(path, O_RDONLY);
805 }
806 if (fd < 0) {
807 ERROR("SELinux: Could not open %s: %s\n",
808 path, strerror(errno));
809 return;
810 }
811 if (fstat(fd, &sb) < 0) {
812 ERROR("SELinux: Could not stat %s: %s\n",
813 path, strerror(errno));
814 return;
815 }
816 map = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
817 if (map == MAP_FAILED) {
818 ERROR("SELinux: Could not map %s: %s\n",
819 path, strerror(errno));
820 return;
821 }
822
823 rc = security_load_policy(map, sb.st_size);
824 if (rc < 0) {
825 ERROR("SELinux: Could not load policy: %s\n",
826 strerror(errno));
827 return;
828 }
829
830 rc = security_setenforce(selinux_enforcing);
831 if (rc < 0) {
832 ERROR("SELinux: Could not set enforcing mode to %s: %s\n",
833 selinux_enforcing ? "enforcing" : "permissive", strerror(errno));
834 return;
835 }
836
837 munmap(map, sb.st_size);
838 close(fd);
839 INFO("SELinux: Loaded policy from %s\n", path);
840
841 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
842 if (!sehandle) {
843 ERROR("SELinux: Could not load file_contexts: %s\n",
844 strerror(errno));
845 return;
846 }
847 INFO("SELinux: Loaded file contexts from %s\n", seopts[0].value);
848 return;
849}
850#endif
851
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700852int main(int argc, char **argv)
853{
Colin Crossebc6ff12010-04-13 19:52:01 -0700854 int fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700855 struct pollfd ufds[4];
856 char *tmpdev;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800857 char* debuggable;
Colin Crossebc6ff12010-04-13 19:52:01 -0700858 char tmp[32];
Colin Crossebc6ff12010-04-13 19:52:01 -0700859 int property_set_fd_init = 0;
860 int signal_fd_init = 0;
861 int keychord_fd_init = 0;
Dima Zavind7634c92011-12-16 14:18:06 -0800862 bool is_charger = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700863
Colin Crossf83d0b92010-04-21 12:04:20 -0700864 if (!strcmp(basename(argv[0]), "ueventd"))
865 return ueventd_main(argc, argv);
866
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700867 if (!strcmp(basename(argv[0]), "watchdogd"))
868 return watchdogd_main(argc, argv);
869
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700870 /* clear the umask */
871 umask(0);
872
873 /* Get the basic filesystem setup we need put
874 * together in the initramdisk on / and then we'll
875 * let the rc file figure out the rest.
876 */
877 mkdir("/dev", 0755);
878 mkdir("/proc", 0755);
879 mkdir("/sys", 0755);
880
Nick Kralevich150f19e2010-06-22 16:35:43 -0700881 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700882 mkdir("/dev/pts", 0755);
883 mkdir("/dev/socket", 0755);
884 mount("devpts", "/dev/pts", "devpts", 0, NULL);
885 mount("proc", "/proc", "proc", 0, NULL);
886 mount("sysfs", "/sys", "sysfs", 0, NULL);
887
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700888 /* indicate that booting is in progress to background fw loaders, etc */
889 close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000));
890
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700891 /* We must have some place other than / to create the
892 * device nodes for kmsg and null, otherwise we won't
893 * be able to remount / read-only later on.
894 * Now that tmpfs is mounted on /dev, we can actually
895 * talk to the outside world.
896 */
897 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -0700898 klog_init();
Dima Zavin5511c842011-12-19 11:21:32 -0800899 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700900
Colin Crossf83d0b92010-04-21 12:04:20 -0700901 get_hardware_name(hardware, &revision);
Dima Zavind7634c92011-12-16 14:18:06 -0800902
Dima Zavin5511c842011-12-19 11:21:32 -0800903 process_kernel_cmdline();
904
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500905#ifdef HAVE_SELINUX
906 INFO("loading selinux policy\n");
907 selinux_load_policy();
908#endif
909
Dima Zavind7634c92011-12-16 14:18:06 -0800910 is_charger = !strcmp(bootmode, "charger");
911
912 INFO("property init\n");
Dima Zavin5511c842011-12-19 11:21:32 -0800913 if (!is_charger)
914 property_load_boot_defaults();
Dima Zavind7634c92011-12-16 14:18:06 -0800915
916 INFO("reading config file\n");
917 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700918
919 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700920
Colin Crossf83d0b92010-04-21 12:04:20 -0700921 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Colin Crossebc6ff12010-04-13 19:52:01 -0700922 queue_builtin_action(keychord_init_action, "keychord_init");
923 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700924
Dima Zavinca47cef2011-08-24 15:28:23 -0700925 /* execute all the boot actions to get us started */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700926 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -0700927
928 /* skip mounting filesystems in charger mode */
Dima Zavind7634c92011-12-16 14:18:06 -0800929 if (!is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -0700930 action_for_each_trigger("early-fs", action_add_queue_tail);
931 action_for_each_trigger("fs", action_add_queue_tail);
932 action_for_each_trigger("post-fs", action_add_queue_tail);
933 action_for_each_trigger("post-fs-data", action_add_queue_tail);
934 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700935
Colin Crossebc6ff12010-04-13 19:52:01 -0700936 queue_builtin_action(property_service_init_action, "property_service_init");
937 queue_builtin_action(signal_init_action, "signal_init");
938 queue_builtin_action(check_startup_action, "check_startup");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700939
Dima Zavind7634c92011-12-16 14:18:06 -0800940 if (is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -0700941 action_for_each_trigger("charger", action_add_queue_tail);
942 } else {
943 action_for_each_trigger("early-boot", action_add_queue_tail);
944 action_for_each_trigger("boot", action_add_queue_tail);
945 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700946
947 /* run all property triggers based on current state of the properties */
Chris Dearman469b7b22012-03-01 15:29:20 -0800948 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700949
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700950
951#if BOOTCHART
Colin Crossebc6ff12010-04-13 19:52:01 -0700952 queue_builtin_action(bootchart_init_action, "bootchart_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700953#endif
954
955 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800956 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700957
Colin Crossebc6ff12010-04-13 19:52:01 -0700958 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700959 restart_processes();
960
Colin Crossebc6ff12010-04-13 19:52:01 -0700961 if (!property_set_fd_init && get_property_set_fd() > 0) {
962 ufds[fd_count].fd = get_property_set_fd();
963 ufds[fd_count].events = POLLIN;
964 ufds[fd_count].revents = 0;
965 fd_count++;
966 property_set_fd_init = 1;
967 }
968 if (!signal_fd_init && get_signal_fd() > 0) {
969 ufds[fd_count].fd = get_signal_fd();
970 ufds[fd_count].events = POLLIN;
971 ufds[fd_count].revents = 0;
972 fd_count++;
973 signal_fd_init = 1;
974 }
975 if (!keychord_fd_init && get_keychord_fd() > 0) {
976 ufds[fd_count].fd = get_keychord_fd();
977 ufds[fd_count].events = POLLIN;
978 ufds[fd_count].revents = 0;
979 fd_count++;
980 keychord_fd_init = 1;
981 }
982
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700983 if (process_needs_restart) {
984 timeout = (process_needs_restart - gettime()) * 1000;
985 if (timeout < 0)
986 timeout = 0;
987 }
988
Colin Crossebd46132010-04-22 11:52:23 -0700989 if (!action_queue_empty() || cur_action)
Colin Crossebc6ff12010-04-13 19:52:01 -0700990 timeout = 0;
991
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700992#if BOOTCHART
993 if (bootchart_count > 0) {
994 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
995 timeout = BOOTCHART_POLLING_MS;
996 if (bootchart_step() < 0 || --bootchart_count == 0) {
997 bootchart_finish();
998 bootchart_count = 0;
999 }
1000 }
1001#endif
Colin Crossebc6ff12010-04-13 19:52:01 -07001002
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001003 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001004 if (nr <= 0)
1005 continue;
1006
Colin Crossebc6ff12010-04-13 19:52:01 -07001007 for (i = 0; i < fd_count; i++) {
1008 if (ufds[i].revents == POLLIN) {
Colin Crossf83d0b92010-04-21 12:04:20 -07001009 if (ufds[i].fd == get_property_set_fd())
Colin Crossebc6ff12010-04-13 19:52:01 -07001010 handle_property_set_fd();
1011 else if (ufds[i].fd == get_keychord_fd())
1012 handle_keychord();
1013 else if (ufds[i].fd == get_signal_fd())
1014 handle_signal();
1015 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001016 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001017 }
1018
1019 return 0;
1020}