blob: d11db618cdbb1bb2e8ed824d8a454229cd993c5b [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
Stephen Smalleye46f9d52012-01-13 08:48:47 -050036#include <selinux/selinux.h>
37#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040038#include <selinux/android.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050039#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"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070061
Stephen Smalleye46f9d52012-01-13 08:48:47 -050062#ifdef HAVE_SELINUX
63struct selabel_handle *sehandle;
64#endif
65
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070066static int property_triggers_enabled = 0;
67
68#if BOOTCHART
69static int bootchart_count;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070070#endif
71
72static char console[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073static char bootmode[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070074static char hardware[32];
75static unsigned revision = 0;
76static char qemu[32];
77
Stephen Smalleye46f9d52012-01-13 08:48:47 -050078#ifdef HAVE_SELINUX
79static int selinux_enabled = 1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050080#endif
81
Colin Crossebc6ff12010-04-13 19:52:01 -070082static struct action *cur_action = NULL;
83static struct command *cur_command = NULL;
84static struct listnode *command_queue = NULL;
85
Colin Cross9c5366b2010-04-13 19:48:59 -070086void notify_service_state(const char *name, const char *state)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070087{
88 char pname[PROP_NAME_MAX];
89 int len = strlen(name);
90 if ((len + 10) > PROP_NAME_MAX)
91 return;
92 snprintf(pname, sizeof(pname), "init.svc.%s", name);
93 property_set(pname, state);
94}
95
96static int have_console;
97static char *console_name = "/dev/console";
98static time_t process_needs_restart;
99
100static const char *ENV[32];
101
102/* add_environment - add "key=value" to the current environment */
103int add_environment(const char *key, const char *val)
104{
105 int n;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700106
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700107 for (n = 0; n < 31; n++) {
108 if (!ENV[n]) {
109 size_t len = strlen(key) + strlen(val) + 2;
110 char *entry = malloc(len);
111 snprintf(entry, len, "%s=%s", key, val);
112 ENV[n] = entry;
113 return 0;
114 }
115 }
116
117 return 1;
118}
119
120static void zap_stdio(void)
121{
122 int fd;
123 fd = open("/dev/null", O_RDWR);
124 dup2(fd, 0);
125 dup2(fd, 1);
126 dup2(fd, 2);
127 close(fd);
128}
129
130static void open_console()
131{
132 int fd;
133 if ((fd = open(console_name, O_RDWR)) < 0) {
134 fd = open("/dev/null", O_RDWR);
135 }
Colin Cross50fb5a62012-03-18 15:38:19 -0700136 ioctl(fd, TIOCSCTTY, 0);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700137 dup2(fd, 0);
138 dup2(fd, 1);
139 dup2(fd, 2);
140 close(fd);
141}
142
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700143static void publish_socket(const char *name, int fd)
144{
145 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
146 char val[64];
147
148 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
149 name,
150 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
151 snprintf(val, sizeof(val), "%d", fd);
152 add_environment(key, val);
153
154 /* make sure we don't close-on-exec */
155 fcntl(fd, F_SETFD, 0);
156}
157
San Mehatf24e2522009-05-19 13:30:46 -0700158void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700159{
160 struct stat s;
161 pid_t pid;
162 int needs_console;
163 int n;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500164#ifdef HAVE_SELINUX
165 char *scon = NULL;
166 int rc;
167#endif
Ken Sumrall752923c2010-12-03 16:33:31 -0800168 /* starting a service removes it from the disabled or reset
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700169 * state and immediately takes it out of the restarting
170 * state if it was in there
171 */
Ken Sumrall752923c2010-12-03 16:33:31 -0800172 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700173 svc->time_started = 0;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700174
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700175 /* running processes require no additional work -- if
176 * they're in the process of exiting, we've ensured
177 * that they will immediately restart on exit, unless
178 * they are ONESHOT
179 */
180 if (svc->flags & SVC_RUNNING) {
181 return;
182 }
183
184 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
185 if (needs_console && (!have_console)) {
186 ERROR("service '%s' requires console\n", svc->name);
187 svc->flags |= SVC_DISABLED;
188 return;
189 }
190
191 if (stat(svc->args[0], &s) != 0) {
192 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
193 svc->flags |= SVC_DISABLED;
194 return;
195 }
196
San Mehatf24e2522009-05-19 13:30:46 -0700197 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700198 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
199 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700200 svc->flags |= SVC_DISABLED;
201 return;
202 }
203
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500204#ifdef HAVE_SELINUX
205 if (is_selinux_enabled() > 0) {
206 char *mycon = NULL, *fcon = NULL;
207
208 INFO("computing context for service '%s'\n", svc->args[0]);
209 rc = getcon(&mycon);
210 if (rc < 0) {
211 ERROR("could not get context while starting '%s'\n", svc->name);
212 return;
213 }
214
215 rc = getfilecon(svc->args[0], &fcon);
216 if (rc < 0) {
217 ERROR("could not get context while starting '%s'\n", svc->name);
218 freecon(mycon);
219 return;
220 }
221
222 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon);
223 freecon(mycon);
224 freecon(fcon);
225 if (rc < 0) {
226 ERROR("could not get context while starting '%s'\n", svc->name);
227 return;
228 }
229 }
230#endif
231
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700232 NOTICE("starting '%s'\n", svc->name);
233
234 pid = fork();
235
236 if (pid == 0) {
237 struct socketinfo *si;
238 struct svcenvinfo *ei;
239 char tmp[32];
240 int fd, sz;
241
Nick Kralevich6ebf12f2012-03-26 09:09:11 -0700242 umask(077);
Colin Cross3294bbb2010-04-19 17:11:33 -0700243 if (properties_inited()) {
244 get_property_workspace(&fd, &sz);
245 sprintf(tmp, "%d,%d", dup(fd), sz);
246 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
247 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700248
249 for (ei = svc->envvars; ei; ei = ei->next)
250 add_environment(ei->name, ei->value);
251
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500252#ifdef HAVE_SELINUX
253 setsockcreatecon(scon);
254#endif
255
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700256 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400257 int socket_type = (
258 !strcmp(si->type, "stream") ? SOCK_STREAM :
259 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
260 int s = create_socket(si->name, socket_type,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700261 si->perm, si->uid, si->gid);
262 if (s >= 0) {
263 publish_socket(si->name, s);
264 }
265 }
266
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500267#ifdef HAVE_SELINUX
268 freecon(scon);
269 scon = NULL;
270 setsockcreatecon(NULL);
271#endif
272
San Mehat4e221f02010-02-25 14:19:50 -0800273 if (svc->ioprio_class != IoSchedClass_NONE) {
274 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
275 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
276 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
277 }
278 }
279
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700280 if (needs_console) {
281 setsid();
282 open_console();
283 } else {
284 zap_stdio();
285 }
286
287#if 0
288 for (n = 0; svc->args[n]; n++) {
289 INFO("args[%d] = '%s'\n", n, svc->args[n]);
290 }
291 for (n = 0; ENV[n]; n++) {
292 INFO("env[%d] = '%s'\n", n, ENV[n]);
293 }
294#endif
295
296 setpgid(0, getpid());
297
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800298 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700299 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800300 if (setgid(svc->gid) != 0) {
301 ERROR("setgid failed: %s\n", strerror(errno));
302 _exit(127);
303 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700304 }
305 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800306 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
307 ERROR("setgroups failed: %s\n", strerror(errno));
308 _exit(127);
309 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700310 }
311 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800312 if (setuid(svc->uid) != 0) {
313 ERROR("setuid failed: %s\n", strerror(errno));
314 _exit(127);
315 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700316 }
317
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500318#ifdef HAVE_SELINUX
319 if (svc->seclabel) {
320 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) {
321 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
322 _exit(127);
323 }
324 }
325#endif
326
San Mehat8ad15682009-05-20 08:50:40 -0700327 if (!dynamic_args) {
328 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
329 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
330 }
331 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700332 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700333 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700334 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700335 char *next = tmp;
336 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700337
338 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700339 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700340
San Mehatd4cdd132009-05-20 09:52:16 -0700341 while((bword = strsep(&next, " "))) {
342 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700343 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700344 break;
San Mehatf24e2522009-05-19 13:30:46 -0700345 }
346 arg_ptrs[arg_idx] = '\0';
347 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100348 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700349 _exit(127);
350 }
351
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500352#ifdef HAVE_SELINUX
353 freecon(scon);
354#endif
355
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700356 if (pid < 0) {
357 ERROR("failed to start '%s'\n", svc->name);
358 svc->pid = 0;
359 return;
360 }
361
362 svc->time_started = gettime();
363 svc->pid = pid;
364 svc->flags |= SVC_RUNNING;
365
Colin Cross3294bbb2010-04-19 17:11:33 -0700366 if (properties_inited())
367 notify_service_state(svc->name, "running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700368}
369
Ken Sumrall752923c2010-12-03 16:33:31 -0800370/* The how field should be either SVC_DISABLED or SVC_RESET */
371static void service_stop_or_reset(struct service *svc, int how)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700372{
373 /* we are no longer running, nor should we
374 * attempt to restart
375 */
376 svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING));
377
Ken Sumrall752923c2010-12-03 16:33:31 -0800378 if ((how != SVC_DISABLED) && (how != SVC_RESET)) {
379 /* Hrm, an illegal flag. Default to SVC_DISABLED */
380 how = SVC_DISABLED;
381 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700382 /* if the service has not yet started, prevent
383 * it from auto-starting with its class
384 */
Ken Sumralla2864802011-10-26 16:56:00 -0700385 if (how == SVC_RESET) {
386 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
387 } else {
388 svc->flags |= how;
389 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700390
391 if (svc->pid) {
392 NOTICE("service '%s' is being killed\n", svc->name);
Ken Sumrall752923c2010-12-03 16:33:31 -0800393 kill(-svc->pid, SIGKILL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700394 notify_service_state(svc->name, "stopping");
395 } else {
396 notify_service_state(svc->name, "stopped");
397 }
398}
399
Ken Sumrall752923c2010-12-03 16:33:31 -0800400void service_reset(struct service *svc)
401{
402 service_stop_or_reset(svc, SVC_RESET);
403}
404
405void service_stop(struct service *svc)
406{
407 service_stop_or_reset(svc, SVC_DISABLED);
408}
409
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700410void property_changed(const char *name, const char *value)
411{
Colin Crossebc6ff12010-04-13 19:52:01 -0700412 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700413 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700414}
415
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700416static void restart_service_if_needed(struct service *svc)
417{
418 time_t next_start_time = svc->time_started + 5;
419
420 if (next_start_time <= gettime()) {
421 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700422 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700423 return;
424 }
425
426 if ((next_start_time < process_needs_restart) ||
427 (process_needs_restart == 0)) {
428 process_needs_restart = next_start_time;
429 }
430}
431
432static void restart_processes()
433{
434 process_needs_restart = 0;
435 service_for_each_flags(SVC_RESTARTING,
436 restart_service_if_needed);
437}
438
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700439static void msg_start(const char *name)
440{
San Mehatf24e2522009-05-19 13:30:46 -0700441 struct service *svc;
442 char *tmp = NULL;
443 char *args = NULL;
444
445 if (!strchr(name, ':'))
446 svc = service_find_by_name(name);
447 else {
448 tmp = strdup(name);
San Mehatf24e2522009-05-19 13:30:46 -0700449 args = strchr(tmp, ':');
450 *args = '\0';
451 args++;
452
453 svc = service_find_by_name(tmp);
454 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700455
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700456 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700457 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700458 } else {
459 ERROR("no such service '%s'\n", name);
460 }
San Mehatf24e2522009-05-19 13:30:46 -0700461 if (tmp)
462 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700463}
464
465static void msg_stop(const char *name)
466{
467 struct service *svc = service_find_by_name(name);
468
469 if (svc) {
470 service_stop(svc);
471 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700472 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700473 }
474}
475
476void handle_control_message(const char *msg, const char *arg)
477{
478 if (!strcmp(msg,"start")) {
479 msg_start(arg);
480 } else if (!strcmp(msg,"stop")) {
481 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700482 } else if (!strcmp(msg,"restart")) {
483 msg_stop(arg);
484 msg_start(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700485 } else {
486 ERROR("unknown control msg '%s'\n", msg);
487 }
488}
489
Colin Crossebc6ff12010-04-13 19:52:01 -0700490static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700491{
492 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700493 node = list_head(&act->commands);
Dima Zavin3bea0792011-08-26 13:59:18 -0700494 if (!node || list_empty(&act->commands))
Colin Crossebc6ff12010-04-13 19:52:01 -0700495 return NULL;
496
497 return node_to_item(node, struct command, clist);
498}
499
500static struct command *get_next_command(struct action *act, struct command *cmd)
501{
502 struct listnode *node;
503 node = cmd->clist.next;
504 if (!node)
505 return NULL;
506 if (node == &act->commands)
507 return NULL;
508
509 return node_to_item(node, struct command, clist);
510}
511
512static int is_last_command(struct action *act, struct command *cmd)
513{
514 return (list_tail(&act->commands) == &cmd->clist);
515}
516
517void execute_one_command(void)
518{
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700519 int ret;
520
Colin Crossebc6ff12010-04-13 19:52:01 -0700521 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
522 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700523 cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -0700524 if (!cur_action)
525 return;
526 INFO("processing action %p (%s)\n", cur_action, cur_action->name);
527 cur_command = get_first_command(cur_action);
528 } else {
529 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700530 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700531
532 if (!cur_command)
533 return;
534
535 ret = cur_command->func(cur_command->nargs, cur_command->args);
536 INFO("command '%s' r=%d\n", cur_command->args[0], ret);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700537}
538
Colin Crossf83d0b92010-04-21 12:04:20 -0700539static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700540{
Colin Crossf83d0b92010-04-21 12:04:20 -0700541 int ret;
542 INFO("wait for %s\n", coldboot_done);
543 ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT);
544 if (ret)
545 ERROR("Timed out waiting for %s\n", coldboot_done);
546 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700547}
548
Colin Crossebc6ff12010-04-13 19:52:01 -0700549static int keychord_init_action(int nargs, char **args)
550{
551 keychord_init();
552 return 0;
553}
554
555static int console_init_action(int nargs, char **args)
556{
557 int fd;
558 char tmp[PROP_VALUE_MAX];
559
560 if (console[0]) {
561 snprintf(tmp, sizeof(tmp), "/dev/%s", console);
562 console_name = strdup(tmp);
563 }
564
565 fd = open(console_name, O_RDWR);
566 if (fd >= 0)
567 have_console = 1;
568 close(fd);
569
570 if( load_565rle_image(INIT_IMAGE_FILE) ) {
571 fd = open("/dev/tty0", O_WRONLY);
572 if (fd >= 0) {
573 const char *msg;
574 msg = "\n"
575 "\n"
576 "\n"
577 "\n"
578 "\n"
579 "\n"
580 "\n" // console is 40 cols x 30 lines
581 "\n"
582 "\n"
583 "\n"
584 "\n"
585 "\n"
586 "\n"
587 "\n"
588 " A N D R O I D ";
589 write(fd, msg, strlen(msg));
590 close(fd);
591 }
592 }
593 return 0;
594}
595
Dima Zavin5511c842011-12-19 11:21:32 -0800596static void import_kernel_nv(char *name, int for_emulator)
597{
598 char *value = strchr(name, '=');
599 int name_len = strlen(name);
600
601 if (value == 0) return;
602 *value++ = 0;
603 if (name_len == 0) return;
604
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500605#ifdef HAVE_SELINUX
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400606 if (!strcmp(name,"selinux")) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500607 selinux_enabled = atoi(value);
608 }
609#endif
610
Dima Zavin5511c842011-12-19 11:21:32 -0800611 if (for_emulator) {
612 /* in the emulator, export any kernel option with the
613 * ro.kernel. prefix */
614 char buff[PROP_NAME_MAX];
615 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
616
617 if (len < (int)sizeof(buff))
618 property_set( buff, value );
619 return;
620 }
621
622 if (!strcmp(name,"qemu")) {
623 strlcpy(qemu, value, sizeof(qemu));
624 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
625 const char *boot_prop_name = name + 12;
626 char prop[PROP_NAME_MAX];
627 int cnt;
628
629 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
630 if (cnt < PROP_NAME_MAX)
631 property_set(prop, value);
632 }
633}
634
635static void export_kernel_boot_props(void)
Colin Crossebc6ff12010-04-13 19:52:01 -0700636{
637 char tmp[PROP_VALUE_MAX];
Dima Zavin5511c842011-12-19 11:21:32 -0800638 const char *pval;
639 unsigned i;
640 struct {
641 const char *src_prop;
642 const char *dest_prop;
643 const char *def_val;
644 } prop_map[] = {
645 { "ro.boot.serialno", "ro.serialno", "", },
646 { "ro.boot.mode", "ro.bootmode", "unknown", },
647 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800648 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
649 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700650
Dima Zavin5511c842011-12-19 11:21:32 -0800651 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
652 pval = property_get(prop_map[i].src_prop);
653 property_set(prop_map[i].dest_prop, pval ?: prop_map[i].def_val);
654 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700655
Dima Zavin5511c842011-12-19 11:21:32 -0800656 pval = property_get("ro.boot.console");
657 if (pval)
658 strlcpy(console, pval, sizeof(console));
659
660 /* save a copy for init's usage during boot */
661 strlcpy(bootmode, property_get("ro.bootmode"), sizeof(bootmode));
662
663 /* if this was given on kernel command line, override what we read
664 * before (e.g. from /proc/cpuinfo), if anything */
665 pval = property_get("ro.boot.hardware");
666 if (pval)
667 strlcpy(hardware, pval, sizeof(hardware));
668 property_set("ro.hardware", hardware);
669
670 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
671 property_set("ro.revision", tmp);
672
673 /* TODO: these are obsolete. We should delete them */
Colin Crossebc6ff12010-04-13 19:52:01 -0700674 if (!strcmp(bootmode,"factory"))
675 property_set("ro.factorytest", "1");
676 else if (!strcmp(bootmode,"factory2"))
677 property_set("ro.factorytest", "2");
678 else
679 property_set("ro.factorytest", "0");
Dima Zavin5511c842011-12-19 11:21:32 -0800680}
Colin Crossebc6ff12010-04-13 19:52:01 -0700681
Dima Zavin5511c842011-12-19 11:21:32 -0800682static void process_kernel_cmdline(void)
683{
684 /* don't expose the raw commandline to nonpriv processes */
685 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700686
Dima Zavin5511c842011-12-19 11:21:32 -0800687 /* first pass does the common stuff, and finds if we are in qemu.
688 * second pass is only necessary for qemu to export all kernel params
689 * as props.
690 */
691 import_kernel_cmdline(0, import_kernel_nv);
692 if (qemu[0])
693 import_kernel_cmdline(1, import_kernel_nv);
694
695 /* now propogate the info given on command line to internal variables
696 * used by init as well as the current required properties
697 */
698 export_kernel_boot_props();
Colin Crossebc6ff12010-04-13 19:52:01 -0700699}
700
701static int property_service_init_action(int nargs, char **args)
702{
703 /* read any property files on system or data and
704 * fire up the property service. This must happen
705 * after the ro.foo properties are set above so
706 * that /data/local.prop cannot interfere with them.
707 */
708 start_property_service();
709 return 0;
710}
711
712static int signal_init_action(int nargs, char **args)
713{
714 signal_init();
715 return 0;
716}
717
718static int check_startup_action(int nargs, char **args)
719{
720 /* make sure we actually have all the pieces we need */
Colin Crossf83d0b92010-04-21 12:04:20 -0700721 if ((get_property_set_fd() < 0) ||
Colin Crossebc6ff12010-04-13 19:52:01 -0700722 (get_signal_fd() < 0)) {
723 ERROR("init startup failure\n");
724 exit(1);
725 }
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700726
727 /* signal that we hit this point */
728 unlink("/dev/.booting");
729
Colin Crossebc6ff12010-04-13 19:52:01 -0700730 return 0;
731}
732
733static int queue_property_triggers_action(int nargs, char **args)
734{
735 queue_all_property_triggers();
736 /* enable property triggers */
737 property_triggers_enabled = 1;
738 return 0;
739}
740
741#if BOOTCHART
742static int bootchart_init_action(int nargs, char **args)
743{
744 bootchart_count = bootchart_init();
745 if (bootchart_count < 0) {
746 ERROR("bootcharting init failure\n");
747 } else if (bootchart_count > 0) {
748 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
749 } else {
750 NOTICE("bootcharting ignored\n");
751 }
Carl-Emil Lagerstedt9ab81902011-01-14 09:35:30 +0100752
753 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700754}
755#endif
756
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500757#ifdef HAVE_SELINUX
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400758void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500759{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400760 sehandle = selinux_android_file_context_handle();
761}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500762
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400763int selinux_reload_policy(void)
764{
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500765 if (!selinux_enabled) {
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400766 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500767 }
768
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400769 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500770
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400771 if (selinux_android_reload_policy() == -1) {
772 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500773 }
774
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400775 if (sehandle)
776 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500777
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400778 selinux_init_all_handles();
779 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500780}
781#endif
782
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700783int main(int argc, char **argv)
784{
Colin Crossebc6ff12010-04-13 19:52:01 -0700785 int fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700786 struct pollfd ufds[4];
787 char *tmpdev;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800788 char* debuggable;
Colin Crossebc6ff12010-04-13 19:52:01 -0700789 char tmp[32];
Colin Crossebc6ff12010-04-13 19:52:01 -0700790 int property_set_fd_init = 0;
791 int signal_fd_init = 0;
792 int keychord_fd_init = 0;
Dima Zavind7634c92011-12-16 14:18:06 -0800793 bool is_charger = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700794
Colin Crossf83d0b92010-04-21 12:04:20 -0700795 if (!strcmp(basename(argv[0]), "ueventd"))
796 return ueventd_main(argc, argv);
797
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700798 /* clear the umask */
799 umask(0);
800
801 /* Get the basic filesystem setup we need put
802 * together in the initramdisk on / and then we'll
803 * let the rc file figure out the rest.
804 */
805 mkdir("/dev", 0755);
806 mkdir("/proc", 0755);
807 mkdir("/sys", 0755);
808
Nick Kralevich150f19e2010-06-22 16:35:43 -0700809 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700810 mkdir("/dev/pts", 0755);
811 mkdir("/dev/socket", 0755);
812 mount("devpts", "/dev/pts", "devpts", 0, NULL);
813 mount("proc", "/proc", "proc", 0, NULL);
814 mount("sysfs", "/sys", "sysfs", 0, NULL);
815
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700816 /* indicate that booting is in progress to background fw loaders, etc */
817 close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000));
818
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700819 /* We must have some place other than / to create the
820 * device nodes for kmsg and null, otherwise we won't
821 * be able to remount / read-only later on.
822 * Now that tmpfs is mounted on /dev, we can actually
823 * talk to the outside world.
824 */
825 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -0700826 klog_init();
Dima Zavin5511c842011-12-19 11:21:32 -0800827 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700828
Colin Crossf83d0b92010-04-21 12:04:20 -0700829 get_hardware_name(hardware, &revision);
Dima Zavind7634c92011-12-16 14:18:06 -0800830
Dima Zavin5511c842011-12-19 11:21:32 -0800831 process_kernel_cmdline();
832
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500833#ifdef HAVE_SELINUX
834 INFO("loading selinux policy\n");
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400835 if (selinux_enabled) {
836 if (selinux_android_load_policy() < 0) {
837 selinux_enabled = 0;
838 INFO("SELinux: Disabled due to failed policy load\n");
839 } else {
840 selinux_init_all_handles();
841 }
842 } else {
843 INFO("SELinux: Disabled by command line option\n");
844 }
845 /* These directories were necessarily created before initial policy load
Stephen Smalleye096e362012-06-11 13:37:39 -0400846 * and therefore need their security context restored to the proper value.
847 * This must happen before /dev is populated by ueventd.
848 */
849 restorecon("/dev");
850 restorecon("/dev/socket");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500851#endif
852
Dima Zavind7634c92011-12-16 14:18:06 -0800853 is_charger = !strcmp(bootmode, "charger");
854
855 INFO("property init\n");
Dima Zavin5511c842011-12-19 11:21:32 -0800856 if (!is_charger)
857 property_load_boot_defaults();
Dima Zavind7634c92011-12-16 14:18:06 -0800858
859 INFO("reading config file\n");
860 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700861
862 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700863
Colin Crossf83d0b92010-04-21 12:04:20 -0700864 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Colin Crossebc6ff12010-04-13 19:52:01 -0700865 queue_builtin_action(keychord_init_action, "keychord_init");
866 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700867
Dima Zavinca47cef2011-08-24 15:28:23 -0700868 /* execute all the boot actions to get us started */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700869 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -0700870
871 /* skip mounting filesystems in charger mode */
Dima Zavind7634c92011-12-16 14:18:06 -0800872 if (!is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -0700873 action_for_each_trigger("early-fs", action_add_queue_tail);
874 action_for_each_trigger("fs", action_add_queue_tail);
875 action_for_each_trigger("post-fs", action_add_queue_tail);
876 action_for_each_trigger("post-fs-data", action_add_queue_tail);
877 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700878
Colin Crossebc6ff12010-04-13 19:52:01 -0700879 queue_builtin_action(property_service_init_action, "property_service_init");
880 queue_builtin_action(signal_init_action, "signal_init");
881 queue_builtin_action(check_startup_action, "check_startup");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700882
Dima Zavind7634c92011-12-16 14:18:06 -0800883 if (is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -0700884 action_for_each_trigger("charger", action_add_queue_tail);
885 } else {
886 action_for_each_trigger("early-boot", action_add_queue_tail);
887 action_for_each_trigger("boot", action_add_queue_tail);
888 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700889
890 /* run all property triggers based on current state of the properties */
Chris Dearman469b7b22012-03-01 15:29:20 -0800891 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700892
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700893
894#if BOOTCHART
Colin Crossebc6ff12010-04-13 19:52:01 -0700895 queue_builtin_action(bootchart_init_action, "bootchart_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700896#endif
897
898 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800899 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700900
Colin Crossebc6ff12010-04-13 19:52:01 -0700901 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700902 restart_processes();
903
Colin Crossebc6ff12010-04-13 19:52:01 -0700904 if (!property_set_fd_init && get_property_set_fd() > 0) {
905 ufds[fd_count].fd = get_property_set_fd();
906 ufds[fd_count].events = POLLIN;
907 ufds[fd_count].revents = 0;
908 fd_count++;
909 property_set_fd_init = 1;
910 }
911 if (!signal_fd_init && get_signal_fd() > 0) {
912 ufds[fd_count].fd = get_signal_fd();
913 ufds[fd_count].events = POLLIN;
914 ufds[fd_count].revents = 0;
915 fd_count++;
916 signal_fd_init = 1;
917 }
918 if (!keychord_fd_init && get_keychord_fd() > 0) {
919 ufds[fd_count].fd = get_keychord_fd();
920 ufds[fd_count].events = POLLIN;
921 ufds[fd_count].revents = 0;
922 fd_count++;
923 keychord_fd_init = 1;
924 }
925
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700926 if (process_needs_restart) {
927 timeout = (process_needs_restart - gettime()) * 1000;
928 if (timeout < 0)
929 timeout = 0;
930 }
931
Colin Crossebd46132010-04-22 11:52:23 -0700932 if (!action_queue_empty() || cur_action)
Colin Crossebc6ff12010-04-13 19:52:01 -0700933 timeout = 0;
934
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700935#if BOOTCHART
936 if (bootchart_count > 0) {
937 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
938 timeout = BOOTCHART_POLLING_MS;
939 if (bootchart_step() < 0 || --bootchart_count == 0) {
940 bootchart_finish();
941 bootchart_count = 0;
942 }
943 }
944#endif
Colin Crossebc6ff12010-04-13 19:52:01 -0700945
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800946 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700947 if (nr <= 0)
948 continue;
949
Colin Crossebc6ff12010-04-13 19:52:01 -0700950 for (i = 0; i < fd_count; i++) {
951 if (ufds[i].revents == POLLIN) {
Colin Crossf83d0b92010-04-21 12:04:20 -0700952 if (ufds[i].fd == get_property_set_fd())
Colin Crossebc6ff12010-04-13 19:52:01 -0700953 handle_property_set_fd();
954 else if (ufds[i].fd == get_keychord_fd())
955 handle_keychord();
956 else if (ufds[i].fd == get_signal_fd())
957 handle_signal();
958 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700959 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700960 }
961
962 return 0;
963}