blob: bc88ba952d2374a1b82e9b35bf7c1da9ce8384f8 [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
Stephen Smalleye46f9d52012-01-13 08:48:47 -050035#include <selinux/selinux.h>
36#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040037#include <selinux/android.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050038
Colin Crossf83d0b92010-04-21 12:04:20 -070039#include <libgen.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070040
Dima Zavinda04c522011-09-01 17:09:44 -070041#include <cutils/list.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070042#include <cutils/sockets.h>
San Mehat4e221f02010-02-25 14:19:50 -080043#include <cutils/iosched_policy.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070044#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070045#include <termios.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070046
47#include <sys/system_properties.h>
48
49#include "devices.h"
50#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070051#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070052#include "property_service.h"
The Android Open Source Project35237d12008-12-17 18:08:08 -080053#include "bootchart.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070054#include "signal_handler.h"
Colin Crossa8666952010-04-13 19:20:44 -070055#include "keychords.h"
Colin Cross6310a822010-04-20 14:29:05 -070056#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070057#include "util.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070058#include "ueventd.h"
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070059#include "watchdogd.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070060
Stephen Smalleye46f9d52012-01-13 08:48:47 -050061struct selabel_handle *sehandle;
rpcraig63207cd2012-08-09 10:05:49 -040062struct selabel_handle *sehandle_prop;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050063
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070064static int property_triggers_enabled = 0;
65
66#if BOOTCHART
67static int bootchart_count;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068#endif
69
70static char console[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070071static char bootmode[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072static char hardware[32];
73static unsigned revision = 0;
74static char qemu[32];
75
Stephen Smalleye46f9d52012-01-13 08:48:47 -050076static int selinux_enabled = 1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050077
Colin Crossebc6ff12010-04-13 19:52:01 -070078static struct action *cur_action = NULL;
79static struct command *cur_command = NULL;
80static struct listnode *command_queue = NULL;
81
Colin Cross9c5366b2010-04-13 19:48:59 -070082void notify_service_state(const char *name, const char *state)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070083{
84 char pname[PROP_NAME_MAX];
85 int len = strlen(name);
86 if ((len + 10) > PROP_NAME_MAX)
87 return;
88 snprintf(pname, sizeof(pname), "init.svc.%s", name);
89 property_set(pname, state);
90}
91
92static int have_console;
93static char *console_name = "/dev/console";
94static time_t process_needs_restart;
95
96static const char *ENV[32];
97
98/* add_environment - add "key=value" to the current environment */
99int add_environment(const char *key, const char *val)
100{
101 int n;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700102
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700103 for (n = 0; n < 31; n++) {
104 if (!ENV[n]) {
105 size_t len = strlen(key) + strlen(val) + 2;
106 char *entry = malloc(len);
107 snprintf(entry, len, "%s=%s", key, val);
108 ENV[n] = entry;
109 return 0;
110 }
111 }
112
113 return 1;
114}
115
116static void zap_stdio(void)
117{
118 int fd;
119 fd = open("/dev/null", O_RDWR);
120 dup2(fd, 0);
121 dup2(fd, 1);
122 dup2(fd, 2);
123 close(fd);
124}
125
126static void open_console()
127{
128 int fd;
129 if ((fd = open(console_name, O_RDWR)) < 0) {
130 fd = open("/dev/null", O_RDWR);
131 }
Colin Cross50fb5a62012-03-18 15:38:19 -0700132 ioctl(fd, TIOCSCTTY, 0);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700133 dup2(fd, 0);
134 dup2(fd, 1);
135 dup2(fd, 2);
136 close(fd);
137}
138
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700139static void publish_socket(const char *name, int fd)
140{
141 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
142 char val[64];
143
144 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
145 name,
146 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
147 snprintf(val, sizeof(val), "%d", fd);
148 add_environment(key, val);
149
150 /* make sure we don't close-on-exec */
151 fcntl(fd, F_SETFD, 0);
152}
153
San Mehatf24e2522009-05-19 13:30:46 -0700154void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700155{
156 struct stat s;
157 pid_t pid;
158 int needs_console;
159 int n;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500160 char *scon = NULL;
161 int rc;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700162
Ken Sumrall752923c2010-12-03 16:33:31 -0800163 /* starting a service removes it from the disabled or reset
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700164 * state and immediately takes it out of the restarting
165 * state if it was in there
166 */
Ken Sumrall752923c2010-12-03 16:33:31 -0800167 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700168 svc->time_started = 0;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700169
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700170 /* running processes require no additional work -- if
171 * they're in the process of exiting, we've ensured
172 * that they will immediately restart on exit, unless
173 * they are ONESHOT
174 */
175 if (svc->flags & SVC_RUNNING) {
176 return;
177 }
178
179 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
180 if (needs_console && (!have_console)) {
181 ERROR("service '%s' requires console\n", svc->name);
182 svc->flags |= SVC_DISABLED;
183 return;
184 }
185
186 if (stat(svc->args[0], &s) != 0) {
187 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
188 svc->flags |= SVC_DISABLED;
189 return;
190 }
191
San Mehatf24e2522009-05-19 13:30:46 -0700192 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700193 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
194 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700195 svc->flags |= SVC_DISABLED;
196 return;
197 }
198
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500199 if (is_selinux_enabled() > 0) {
200 char *mycon = NULL, *fcon = NULL;
201
202 INFO("computing context for service '%s'\n", svc->args[0]);
203 rc = getcon(&mycon);
204 if (rc < 0) {
205 ERROR("could not get context while starting '%s'\n", svc->name);
206 return;
207 }
208
209 rc = getfilecon(svc->args[0], &fcon);
210 if (rc < 0) {
211 ERROR("could not get context while starting '%s'\n", svc->name);
212 freecon(mycon);
213 return;
214 }
215
216 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon);
217 freecon(mycon);
218 freecon(fcon);
219 if (rc < 0) {
220 ERROR("could not get context while starting '%s'\n", svc->name);
221 return;
222 }
223 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500224
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700225 NOTICE("starting '%s'\n", svc->name);
226
227 pid = fork();
228
229 if (pid == 0) {
230 struct socketinfo *si;
231 struct svcenvinfo *ei;
232 char tmp[32];
233 int fd, sz;
234
Nick Kralevich6ebf12f2012-03-26 09:09:11 -0700235 umask(077);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700236
237 for (ei = svc->envvars; ei; ei = ei->next)
238 add_environment(ei->name, ei->value);
239
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500240 setsockcreatecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500241
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700242 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400243 int socket_type = (
244 !strcmp(si->type, "stream") ? SOCK_STREAM :
245 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
246 int s = create_socket(si->name, socket_type,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700247 si->perm, si->uid, si->gid);
248 if (s >= 0) {
249 publish_socket(si->name, s);
250 }
251 }
252
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500253 freecon(scon);
254 scon = NULL;
255 setsockcreatecon(NULL);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500256
San Mehat4e221f02010-02-25 14:19:50 -0800257 if (svc->ioprio_class != IoSchedClass_NONE) {
258 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
259 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
260 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
261 }
262 }
263
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700264 if (needs_console) {
265 setsid();
266 open_console();
267 } else {
268 zap_stdio();
269 }
270
271#if 0
272 for (n = 0; svc->args[n]; n++) {
273 INFO("args[%d] = '%s'\n", n, svc->args[n]);
274 }
275 for (n = 0; ENV[n]; n++) {
276 INFO("env[%d] = '%s'\n", n, ENV[n]);
277 }
278#endif
279
280 setpgid(0, getpid());
281
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800282 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700283 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800284 if (setgid(svc->gid) != 0) {
285 ERROR("setgid failed: %s\n", strerror(errno));
286 _exit(127);
287 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700288 }
289 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800290 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
291 ERROR("setgroups failed: %s\n", strerror(errno));
292 _exit(127);
293 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700294 }
295 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800296 if (setuid(svc->uid) != 0) {
297 ERROR("setuid failed: %s\n", strerror(errno));
298 _exit(127);
299 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700300 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500301 if (svc->seclabel) {
302 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) {
303 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
304 _exit(127);
305 }
306 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500307
San Mehat8ad15682009-05-20 08:50:40 -0700308 if (!dynamic_args) {
309 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
310 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
311 }
312 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700313 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700314 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700315 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700316 char *next = tmp;
317 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700318
319 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700320 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700321
San Mehatd4cdd132009-05-20 09:52:16 -0700322 while((bword = strsep(&next, " "))) {
323 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700324 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700325 break;
San Mehatf24e2522009-05-19 13:30:46 -0700326 }
327 arg_ptrs[arg_idx] = '\0';
328 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100329 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700330 _exit(127);
331 }
332
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500333 freecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500334
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700335 if (pid < 0) {
336 ERROR("failed to start '%s'\n", svc->name);
337 svc->pid = 0;
338 return;
339 }
340
341 svc->time_started = gettime();
342 svc->pid = pid;
343 svc->flags |= SVC_RUNNING;
344
Colin Cross3294bbb2010-04-19 17:11:33 -0700345 if (properties_inited())
346 notify_service_state(svc->name, "running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700347}
348
Ken Sumrall752923c2010-12-03 16:33:31 -0800349/* The how field should be either SVC_DISABLED or SVC_RESET */
350static void service_stop_or_reset(struct service *svc, int how)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700351{
352 /* we are no longer running, nor should we
353 * attempt to restart
354 */
355 svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING));
356
Ken Sumrall752923c2010-12-03 16:33:31 -0800357 if ((how != SVC_DISABLED) && (how != SVC_RESET)) {
358 /* Hrm, an illegal flag. Default to SVC_DISABLED */
359 how = SVC_DISABLED;
360 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700361 /* if the service has not yet started, prevent
362 * it from auto-starting with its class
363 */
Ken Sumralla2864802011-10-26 16:56:00 -0700364 if (how == SVC_RESET) {
365 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
366 } else {
367 svc->flags |= how;
368 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700369
370 if (svc->pid) {
371 NOTICE("service '%s' is being killed\n", svc->name);
Ken Sumrall752923c2010-12-03 16:33:31 -0800372 kill(-svc->pid, SIGKILL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700373 notify_service_state(svc->name, "stopping");
374 } else {
375 notify_service_state(svc->name, "stopped");
376 }
377}
378
Ken Sumrall752923c2010-12-03 16:33:31 -0800379void service_reset(struct service *svc)
380{
381 service_stop_or_reset(svc, SVC_RESET);
382}
383
384void service_stop(struct service *svc)
385{
386 service_stop_or_reset(svc, SVC_DISABLED);
387}
388
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700389void property_changed(const char *name, const char *value)
390{
Colin Crossebc6ff12010-04-13 19:52:01 -0700391 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700392 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700393}
394
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700395static void restart_service_if_needed(struct service *svc)
396{
397 time_t next_start_time = svc->time_started + 5;
398
399 if (next_start_time <= gettime()) {
400 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700401 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700402 return;
403 }
404
405 if ((next_start_time < process_needs_restart) ||
406 (process_needs_restart == 0)) {
407 process_needs_restart = next_start_time;
408 }
409}
410
411static void restart_processes()
412{
413 process_needs_restart = 0;
414 service_for_each_flags(SVC_RESTARTING,
415 restart_service_if_needed);
416}
417
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700418static void msg_start(const char *name)
419{
San Mehatf24e2522009-05-19 13:30:46 -0700420 struct service *svc;
421 char *tmp = NULL;
422 char *args = NULL;
423
424 if (!strchr(name, ':'))
425 svc = service_find_by_name(name);
426 else {
427 tmp = strdup(name);
San Mehatf24e2522009-05-19 13:30:46 -0700428 args = strchr(tmp, ':');
429 *args = '\0';
430 args++;
431
432 svc = service_find_by_name(tmp);
433 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700434
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700435 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700436 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700437 } else {
438 ERROR("no such service '%s'\n", name);
439 }
San Mehatf24e2522009-05-19 13:30:46 -0700440 if (tmp)
441 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700442}
443
444static void msg_stop(const char *name)
445{
446 struct service *svc = service_find_by_name(name);
447
448 if (svc) {
449 service_stop(svc);
450 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700451 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700452 }
453}
454
455void handle_control_message(const char *msg, const char *arg)
456{
457 if (!strcmp(msg,"start")) {
458 msg_start(arg);
459 } else if (!strcmp(msg,"stop")) {
460 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700461 } else if (!strcmp(msg,"restart")) {
462 msg_stop(arg);
463 msg_start(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700464 } else {
465 ERROR("unknown control msg '%s'\n", msg);
466 }
467}
468
Colin Crossebc6ff12010-04-13 19:52:01 -0700469static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700470{
471 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700472 node = list_head(&act->commands);
Dima Zavin3bea0792011-08-26 13:59:18 -0700473 if (!node || list_empty(&act->commands))
Colin Crossebc6ff12010-04-13 19:52:01 -0700474 return NULL;
475
476 return node_to_item(node, struct command, clist);
477}
478
479static struct command *get_next_command(struct action *act, struct command *cmd)
480{
481 struct listnode *node;
482 node = cmd->clist.next;
483 if (!node)
484 return NULL;
485 if (node == &act->commands)
486 return NULL;
487
488 return node_to_item(node, struct command, clist);
489}
490
491static int is_last_command(struct action *act, struct command *cmd)
492{
493 return (list_tail(&act->commands) == &cmd->clist);
494}
495
496void execute_one_command(void)
497{
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700498 int ret;
499
Colin Crossebc6ff12010-04-13 19:52:01 -0700500 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
501 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700502 cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -0700503 if (!cur_action)
504 return;
505 INFO("processing action %p (%s)\n", cur_action, cur_action->name);
506 cur_command = get_first_command(cur_action);
507 } else {
508 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700509 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700510
511 if (!cur_command)
512 return;
513
514 ret = cur_command->func(cur_command->nargs, cur_command->args);
515 INFO("command '%s' r=%d\n", cur_command->args[0], ret);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700516}
517
Colin Crossf83d0b92010-04-21 12:04:20 -0700518static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700519{
Colin Crossf83d0b92010-04-21 12:04:20 -0700520 int ret;
521 INFO("wait for %s\n", coldboot_done);
522 ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT);
523 if (ret)
524 ERROR("Timed out waiting for %s\n", coldboot_done);
525 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700526}
527
Colin Crossebc6ff12010-04-13 19:52:01 -0700528static int keychord_init_action(int nargs, char **args)
529{
530 keychord_init();
531 return 0;
532}
533
534static int console_init_action(int nargs, char **args)
535{
536 int fd;
537 char tmp[PROP_VALUE_MAX];
538
539 if (console[0]) {
540 snprintf(tmp, sizeof(tmp), "/dev/%s", console);
541 console_name = strdup(tmp);
542 }
543
544 fd = open(console_name, O_RDWR);
545 if (fd >= 0)
546 have_console = 1;
547 close(fd);
548
549 if( load_565rle_image(INIT_IMAGE_FILE) ) {
550 fd = open("/dev/tty0", O_WRONLY);
551 if (fd >= 0) {
552 const char *msg;
553 msg = "\n"
554 "\n"
555 "\n"
556 "\n"
557 "\n"
558 "\n"
559 "\n" // console is 40 cols x 30 lines
560 "\n"
561 "\n"
562 "\n"
563 "\n"
564 "\n"
565 "\n"
566 "\n"
567 " A N D R O I D ";
568 write(fd, msg, strlen(msg));
569 close(fd);
570 }
571 }
572 return 0;
573}
574
Dima Zavin5511c842011-12-19 11:21:32 -0800575static void import_kernel_nv(char *name, int for_emulator)
576{
577 char *value = strchr(name, '=');
578 int name_len = strlen(name);
579
580 if (value == 0) return;
581 *value++ = 0;
582 if (name_len == 0) return;
583
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400584 if (!strcmp(name,"selinux")) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500585 selinux_enabled = atoi(value);
586 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500587
Dima Zavin5511c842011-12-19 11:21:32 -0800588 if (for_emulator) {
589 /* in the emulator, export any kernel option with the
590 * ro.kernel. prefix */
591 char buff[PROP_NAME_MAX];
592 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
593
594 if (len < (int)sizeof(buff))
595 property_set( buff, value );
596 return;
597 }
598
599 if (!strcmp(name,"qemu")) {
600 strlcpy(qemu, value, sizeof(qemu));
601 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
602 const char *boot_prop_name = name + 12;
603 char prop[PROP_NAME_MAX];
604 int cnt;
605
606 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
607 if (cnt < PROP_NAME_MAX)
608 property_set(prop, value);
609 }
610}
611
612static void export_kernel_boot_props(void)
Colin Crossebc6ff12010-04-13 19:52:01 -0700613{
614 char tmp[PROP_VALUE_MAX];
Dima Zavin5511c842011-12-19 11:21:32 -0800615 const char *pval;
616 unsigned i;
617 struct {
618 const char *src_prop;
619 const char *dest_prop;
620 const char *def_val;
621 } prop_map[] = {
622 { "ro.boot.serialno", "ro.serialno", "", },
623 { "ro.boot.mode", "ro.bootmode", "unknown", },
624 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800625 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
626 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700627
Dima Zavin5511c842011-12-19 11:21:32 -0800628 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
629 pval = property_get(prop_map[i].src_prop);
630 property_set(prop_map[i].dest_prop, pval ?: prop_map[i].def_val);
631 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700632
Dima Zavin5511c842011-12-19 11:21:32 -0800633 pval = property_get("ro.boot.console");
634 if (pval)
635 strlcpy(console, pval, sizeof(console));
636
637 /* save a copy for init's usage during boot */
638 strlcpy(bootmode, property_get("ro.bootmode"), sizeof(bootmode));
639
640 /* if this was given on kernel command line, override what we read
641 * before (e.g. from /proc/cpuinfo), if anything */
642 pval = property_get("ro.boot.hardware");
643 if (pval)
644 strlcpy(hardware, pval, sizeof(hardware));
645 property_set("ro.hardware", hardware);
646
647 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
648 property_set("ro.revision", tmp);
649
650 /* TODO: these are obsolete. We should delete them */
Colin Crossebc6ff12010-04-13 19:52:01 -0700651 if (!strcmp(bootmode,"factory"))
652 property_set("ro.factorytest", "1");
653 else if (!strcmp(bootmode,"factory2"))
654 property_set("ro.factorytest", "2");
655 else
656 property_set("ro.factorytest", "0");
Dima Zavin5511c842011-12-19 11:21:32 -0800657}
Colin Crossebc6ff12010-04-13 19:52:01 -0700658
Dima Zavin5511c842011-12-19 11:21:32 -0800659static void process_kernel_cmdline(void)
660{
661 /* don't expose the raw commandline to nonpriv processes */
662 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700663
Dima Zavin5511c842011-12-19 11:21:32 -0800664 /* first pass does the common stuff, and finds if we are in qemu.
665 * second pass is only necessary for qemu to export all kernel params
666 * as props.
667 */
668 import_kernel_cmdline(0, import_kernel_nv);
669 if (qemu[0])
670 import_kernel_cmdline(1, import_kernel_nv);
671
672 /* now propogate the info given on command line to internal variables
673 * used by init as well as the current required properties
674 */
675 export_kernel_boot_props();
Colin Crossebc6ff12010-04-13 19:52:01 -0700676}
677
678static int property_service_init_action(int nargs, char **args)
679{
680 /* read any property files on system or data and
681 * fire up the property service. This must happen
682 * after the ro.foo properties are set above so
683 * that /data/local.prop cannot interfere with them.
684 */
685 start_property_service();
686 return 0;
687}
688
689static int signal_init_action(int nargs, char **args)
690{
691 signal_init();
692 return 0;
693}
694
695static int check_startup_action(int nargs, char **args)
696{
697 /* make sure we actually have all the pieces we need */
Colin Crossf83d0b92010-04-21 12:04:20 -0700698 if ((get_property_set_fd() < 0) ||
Colin Crossebc6ff12010-04-13 19:52:01 -0700699 (get_signal_fd() < 0)) {
700 ERROR("init startup failure\n");
701 exit(1);
702 }
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700703
704 /* signal that we hit this point */
705 unlink("/dev/.booting");
706
Colin Crossebc6ff12010-04-13 19:52:01 -0700707 return 0;
708}
709
710static int queue_property_triggers_action(int nargs, char **args)
711{
712 queue_all_property_triggers();
713 /* enable property triggers */
714 property_triggers_enabled = 1;
715 return 0;
716}
717
718#if BOOTCHART
719static int bootchart_init_action(int nargs, char **args)
720{
721 bootchart_count = bootchart_init();
722 if (bootchart_count < 0) {
723 ERROR("bootcharting init failure\n");
724 } else if (bootchart_count > 0) {
725 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
726 } else {
727 NOTICE("bootcharting ignored\n");
728 }
Carl-Emil Lagerstedt9ab81902011-01-14 09:35:30 +0100729
730 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700731}
732#endif
733
rpcraig63207cd2012-08-09 10:05:49 -0400734static const struct selinux_opt seopts_prop[] = {
735 { SELABEL_OPT_PATH, "/data/system/property_contexts" },
736 { SELABEL_OPT_PATH, "/property_contexts" },
737 { 0, NULL }
738};
739
740struct selabel_handle* selinux_android_prop_context_handle(void)
741{
742 int i = 0;
743 struct selabel_handle* sehandle = NULL;
744 while ((sehandle == NULL) && seopts_prop[i].value) {
745 sehandle = selabel_open(SELABEL_CTX_ANDROID_PROP, &seopts_prop[i], 1);
746 i++;
747 }
748
749 if (!sehandle) {
750 ERROR("SELinux: Could not load property_contexts: %s\n",
751 strerror(errno));
752 return NULL;
753 }
754 INFO("SELinux: Loaded property contexts from %s\n", seopts_prop[i - 1].value);
755 return sehandle;
756}
757
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();
rpcraig63207cd2012-08-09 10:05:49 -0400761 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400762}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500763
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400764int selinux_reload_policy(void)
765{
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500766 if (!selinux_enabled) {
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400767 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500768 }
769
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400770 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500771
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400772 if (selinux_android_reload_policy() == -1) {
773 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500774 }
775
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400776 if (sehandle)
777 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500778
rpcraig63207cd2012-08-09 10:05:49 -0400779 if (sehandle_prop)
780 selabel_close(sehandle_prop);
781
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400782 selinux_init_all_handles();
783 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500784}
rpcraig63207cd2012-08-09 10:05:49 -0400785
786int audit_callback(void *data, security_class_t cls, char *buf, size_t len)
787{
788 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
789 return 0;
790}
791
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700792int main(int argc, char **argv)
793{
Colin Crossebc6ff12010-04-13 19:52:01 -0700794 int fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700795 struct pollfd ufds[4];
796 char *tmpdev;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800797 char* debuggable;
Colin Crossebc6ff12010-04-13 19:52:01 -0700798 char tmp[32];
Colin Crossebc6ff12010-04-13 19:52:01 -0700799 int property_set_fd_init = 0;
800 int signal_fd_init = 0;
801 int keychord_fd_init = 0;
Dima Zavind7634c92011-12-16 14:18:06 -0800802 bool is_charger = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700803
Colin Crossf83d0b92010-04-21 12:04:20 -0700804 if (!strcmp(basename(argv[0]), "ueventd"))
805 return ueventd_main(argc, argv);
806
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700807 if (!strcmp(basename(argv[0]), "watchdogd"))
808 return watchdogd_main(argc, argv);
809
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700810 /* clear the umask */
811 umask(0);
812
813 /* Get the basic filesystem setup we need put
814 * together in the initramdisk on / and then we'll
815 * let the rc file figure out the rest.
816 */
817 mkdir("/dev", 0755);
818 mkdir("/proc", 0755);
819 mkdir("/sys", 0755);
820
Nick Kralevich150f19e2010-06-22 16:35:43 -0700821 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700822 mkdir("/dev/pts", 0755);
823 mkdir("/dev/socket", 0755);
824 mount("devpts", "/dev/pts", "devpts", 0, NULL);
825 mount("proc", "/proc", "proc", 0, NULL);
826 mount("sysfs", "/sys", "sysfs", 0, NULL);
827
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700828 /* indicate that booting is in progress to background fw loaders, etc */
829 close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000));
830
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700831 /* We must have some place other than / to create the
832 * device nodes for kmsg and null, otherwise we won't
833 * be able to remount / read-only later on.
834 * Now that tmpfs is mounted on /dev, we can actually
835 * talk to the outside world.
836 */
837 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -0700838 klog_init();
Dima Zavin5511c842011-12-19 11:21:32 -0800839 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700840
Colin Crossf83d0b92010-04-21 12:04:20 -0700841 get_hardware_name(hardware, &revision);
Dima Zavind7634c92011-12-16 14:18:06 -0800842
Dima Zavin5511c842011-12-19 11:21:32 -0800843 process_kernel_cmdline();
844
rpcraig63207cd2012-08-09 10:05:49 -0400845 union selinux_callback cb;
846 cb.func_log = klog_write;
847 selinux_set_callback(SELINUX_CB_LOG, cb);
848
849 cb.func_audit = audit_callback;
850 selinux_set_callback(SELINUX_CB_AUDIT, cb);
851
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500852 INFO("loading selinux policy\n");
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400853 if (selinux_enabled) {
854 if (selinux_android_load_policy() < 0) {
855 selinux_enabled = 0;
856 INFO("SELinux: Disabled due to failed policy load\n");
857 } else {
858 selinux_init_all_handles();
859 }
860 } else {
861 INFO("SELinux: Disabled by command line option\n");
862 }
863 /* These directories were necessarily created before initial policy load
Stephen Smalleye096e362012-06-11 13:37:39 -0400864 * and therefore need their security context restored to the proper value.
865 * This must happen before /dev is populated by ueventd.
866 */
867 restorecon("/dev");
868 restorecon("/dev/socket");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500869
Dima Zavind7634c92011-12-16 14:18:06 -0800870 is_charger = !strcmp(bootmode, "charger");
871
872 INFO("property init\n");
Dima Zavin5511c842011-12-19 11:21:32 -0800873 if (!is_charger)
874 property_load_boot_defaults();
Dima Zavind7634c92011-12-16 14:18:06 -0800875
876 INFO("reading config file\n");
877 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700878
879 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700880
Colin Crossf83d0b92010-04-21 12:04:20 -0700881 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Colin Crossebc6ff12010-04-13 19:52:01 -0700882 queue_builtin_action(keychord_init_action, "keychord_init");
883 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700884
Dima Zavinca47cef2011-08-24 15:28:23 -0700885 /* execute all the boot actions to get us started */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700886 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -0700887
888 /* skip mounting filesystems in charger mode */
Dima Zavind7634c92011-12-16 14:18:06 -0800889 if (!is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -0700890 action_for_each_trigger("early-fs", action_add_queue_tail);
891 action_for_each_trigger("fs", action_add_queue_tail);
892 action_for_each_trigger("post-fs", action_add_queue_tail);
893 action_for_each_trigger("post-fs-data", action_add_queue_tail);
894 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700895
Colin Crossebc6ff12010-04-13 19:52:01 -0700896 queue_builtin_action(property_service_init_action, "property_service_init");
897 queue_builtin_action(signal_init_action, "signal_init");
898 queue_builtin_action(check_startup_action, "check_startup");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700899
Dima Zavind7634c92011-12-16 14:18:06 -0800900 if (is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -0700901 action_for_each_trigger("charger", action_add_queue_tail);
902 } else {
903 action_for_each_trigger("early-boot", action_add_queue_tail);
904 action_for_each_trigger("boot", action_add_queue_tail);
905 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700906
907 /* run all property triggers based on current state of the properties */
Chris Dearman469b7b22012-03-01 15:29:20 -0800908 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700909
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700910
911#if BOOTCHART
Colin Crossebc6ff12010-04-13 19:52:01 -0700912 queue_builtin_action(bootchart_init_action, "bootchart_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700913#endif
914
915 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800916 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700917
Colin Crossebc6ff12010-04-13 19:52:01 -0700918 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700919 restart_processes();
920
Colin Crossebc6ff12010-04-13 19:52:01 -0700921 if (!property_set_fd_init && get_property_set_fd() > 0) {
922 ufds[fd_count].fd = get_property_set_fd();
923 ufds[fd_count].events = POLLIN;
924 ufds[fd_count].revents = 0;
925 fd_count++;
926 property_set_fd_init = 1;
927 }
928 if (!signal_fd_init && get_signal_fd() > 0) {
929 ufds[fd_count].fd = get_signal_fd();
930 ufds[fd_count].events = POLLIN;
931 ufds[fd_count].revents = 0;
932 fd_count++;
933 signal_fd_init = 1;
934 }
935 if (!keychord_fd_init && get_keychord_fd() > 0) {
936 ufds[fd_count].fd = get_keychord_fd();
937 ufds[fd_count].events = POLLIN;
938 ufds[fd_count].revents = 0;
939 fd_count++;
940 keychord_fd_init = 1;
941 }
942
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700943 if (process_needs_restart) {
944 timeout = (process_needs_restart - gettime()) * 1000;
945 if (timeout < 0)
946 timeout = 0;
947 }
948
Colin Crossebd46132010-04-22 11:52:23 -0700949 if (!action_queue_empty() || cur_action)
Colin Crossebc6ff12010-04-13 19:52:01 -0700950 timeout = 0;
951
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700952#if BOOTCHART
953 if (bootchart_count > 0) {
954 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
955 timeout = BOOTCHART_POLLING_MS;
956 if (bootchart_step() < 0 || --bootchart_count == 0) {
957 bootchart_finish();
958 bootchart_count = 0;
959 }
960 }
961#endif
Colin Crossebc6ff12010-04-13 19:52:01 -0700962
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800963 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700964 if (nr <= 0)
965 continue;
966
Colin Crossebc6ff12010-04-13 19:52:01 -0700967 for (i = 0; i < fd_count; i++) {
968 if (ufds[i].revents == POLLIN) {
Colin Crossf83d0b92010-04-21 12:04:20 -0700969 if (ufds[i].fd == get_property_set_fd())
Colin Crossebc6ff12010-04-13 19:52:01 -0700970 handle_property_set_fd();
971 else if (ufds[i].fd == get_keychord_fd())
972 handle_keychord();
973 else if (ufds[i].fd == get_signal_fd())
974 handle_signal();
975 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700976 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700977 }
978
979 return 0;
980}