blob: 48d8559725ef7744c30d0cc7b7a6bf83bf45e5b5 [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>
Dave Burkeeb22e6a2013-02-11 19:48:43 +000034#include <sys/personality.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050035
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
Colin Crossf83d0b92010-04-21 12:04:20 -070040#include <libgen.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070041
Dima Zavinda04c522011-09-01 17:09:44 -070042#include <cutils/list.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070043#include <cutils/sockets.h>
San Mehat4e221f02010-02-25 14:19:50 -080044#include <cutils/iosched_policy.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070045#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070046#include <termios.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070047
48#include <sys/system_properties.h>
49
50#include "devices.h"
51#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070052#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070053#include "property_service.h"
The Android Open Source Project35237d12008-12-17 18:08:08 -080054#include "bootchart.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070055#include "signal_handler.h"
Colin Crossa8666952010-04-13 19:20:44 -070056#include "keychords.h"
Colin Cross6310a822010-04-20 14:29:05 -070057#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070058#include "util.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070059#include "ueventd.h"
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070060#include "watchdogd.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070061
Stephen Smalleye46f9d52012-01-13 08:48:47 -050062struct selabel_handle *sehandle;
rpcraig63207cd2012-08-09 10:05:49 -040063struct selabel_handle *sehandle_prop;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050064
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070065static int property_triggers_enabled = 0;
66
67#if BOOTCHART
68static int bootchart_count;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069#endif
70
71static char console[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072static char bootmode[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073static char hardware[32];
74static unsigned revision = 0;
75static char qemu[32];
76
Stephen Smalleye46f9d52012-01-13 08:48:47 -050077static int selinux_enabled = 1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050078
Colin Crossebc6ff12010-04-13 19:52:01 -070079static struct action *cur_action = NULL;
80static struct command *cur_command = NULL;
81static struct listnode *command_queue = NULL;
82
Colin Cross9c5366b2010-04-13 19:48:59 -070083void notify_service_state(const char *name, const char *state)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070084{
85 char pname[PROP_NAME_MAX];
86 int len = strlen(name);
87 if ((len + 10) > PROP_NAME_MAX)
88 return;
89 snprintf(pname, sizeof(pname), "init.svc.%s", name);
90 property_set(pname, state);
91}
92
93static int have_console;
94static char *console_name = "/dev/console";
95static time_t process_needs_restart;
96
97static const char *ENV[32];
98
99/* add_environment - add "key=value" to the current environment */
100int add_environment(const char *key, const char *val)
101{
102 int n;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700103
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700104 for (n = 0; n < 31; n++) {
105 if (!ENV[n]) {
106 size_t len = strlen(key) + strlen(val) + 2;
107 char *entry = malloc(len);
108 snprintf(entry, len, "%s=%s", key, val);
109 ENV[n] = entry;
110 return 0;
111 }
112 }
113
114 return 1;
115}
116
117static void zap_stdio(void)
118{
119 int fd;
120 fd = open("/dev/null", O_RDWR);
121 dup2(fd, 0);
122 dup2(fd, 1);
123 dup2(fd, 2);
124 close(fd);
125}
126
127static void open_console()
128{
129 int fd;
130 if ((fd = open(console_name, O_RDWR)) < 0) {
131 fd = open("/dev/null", O_RDWR);
132 }
Colin Cross50fb5a62012-03-18 15:38:19 -0700133 ioctl(fd, TIOCSCTTY, 0);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700134 dup2(fd, 0);
135 dup2(fd, 1);
136 dup2(fd, 2);
137 close(fd);
138}
139
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700140static void publish_socket(const char *name, int fd)
141{
142 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
143 char val[64];
144
145 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
146 name,
147 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
148 snprintf(val, sizeof(val), "%d", fd);
149 add_environment(key, val);
150
151 /* make sure we don't close-on-exec */
152 fcntl(fd, F_SETFD, 0);
153}
154
San Mehatf24e2522009-05-19 13:30:46 -0700155void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700156{
157 struct stat s;
158 pid_t pid;
159 int needs_console;
160 int n;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500161 char *scon = NULL;
162 int rc;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700163
Ken Sumrall752923c2010-12-03 16:33:31 -0800164 /* starting a service removes it from the disabled or reset
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700165 * state and immediately takes it out of the restarting
166 * state if it was in there
167 */
Ken Sumrall752923c2010-12-03 16:33:31 -0800168 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700169 svc->time_started = 0;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700170
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700171 /* running processes require no additional work -- if
172 * they're in the process of exiting, we've ensured
173 * that they will immediately restart on exit, unless
174 * they are ONESHOT
175 */
176 if (svc->flags & SVC_RUNNING) {
177 return;
178 }
179
180 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
181 if (needs_console && (!have_console)) {
182 ERROR("service '%s' requires console\n", svc->name);
183 svc->flags |= SVC_DISABLED;
184 return;
185 }
186
187 if (stat(svc->args[0], &s) != 0) {
188 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
189 svc->flags |= SVC_DISABLED;
190 return;
191 }
192
San Mehatf24e2522009-05-19 13:30:46 -0700193 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700194 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
195 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700196 svc->flags |= SVC_DISABLED;
197 return;
198 }
199
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500200 if (is_selinux_enabled() > 0) {
Stephen Smalley30f30332012-11-16 14:34:27 -0500201 if (svc->seclabel) {
202 scon = strdup(svc->seclabel);
203 if (!scon) {
204 ERROR("Out of memory while starting '%s'\n", svc->name);
205 return;
206 }
207 } else {
208 char *mycon = NULL, *fcon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500209
Stephen Smalley30f30332012-11-16 14:34:27 -0500210 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 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500216
Stephen Smalley30f30332012-11-16 14:34:27 -0500217 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);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500225 freecon(mycon);
Stephen Smalley30f30332012-11-16 14:34:27 -0500226 freecon(fcon);
227 if (rc < 0) {
228 ERROR("could not get context while starting '%s'\n", svc->name);
229 return;
230 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500231 }
232 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500233
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);
Dave Burkeeb22e6a2013-02-11 19:48:43 +0000245#ifdef __arm__
246 /*
247 * b/7188322 - Temporarily revert to the compat memory layout
248 * to avoid breaking third party apps.
249 *
250 * THIS WILL GO AWAY IN A FUTURE ANDROID RELEASE.
251 *
252 * http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commitdiff;h=7dbaa466
253 * changes the kernel mapping from bottom up to top-down.
254 * This breaks some programs which improperly embed
255 * an out of date copy of Android's linker.
256 */
257 int current = personality(0xffffFFFF);
258 personality(current | ADDR_COMPAT_LAYOUT);
259#endif
Nick Kralevich28406472013-01-22 12:46:09 -0800260 if (properties_inited()) {
261 get_property_workspace(&fd, &sz);
262 sprintf(tmp, "%d,%d", dup(fd), sz);
263 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
264 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700265
266 for (ei = svc->envvars; ei; ei = ei->next)
267 add_environment(ei->name, ei->value);
268
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500269 setsockcreatecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500270
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700271 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400272 int socket_type = (
273 !strcmp(si->type, "stream") ? SOCK_STREAM :
274 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
275 int s = create_socket(si->name, socket_type,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700276 si->perm, si->uid, si->gid);
277 if (s >= 0) {
278 publish_socket(si->name, s);
279 }
280 }
281
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500282 freecon(scon);
283 scon = NULL;
284 setsockcreatecon(NULL);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500285
San Mehat4e221f02010-02-25 14:19:50 -0800286 if (svc->ioprio_class != IoSchedClass_NONE) {
287 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
288 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
289 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
290 }
291 }
292
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700293 if (needs_console) {
294 setsid();
295 open_console();
296 } else {
297 zap_stdio();
298 }
299
300#if 0
301 for (n = 0; svc->args[n]; n++) {
302 INFO("args[%d] = '%s'\n", n, svc->args[n]);
303 }
304 for (n = 0; ENV[n]; n++) {
305 INFO("env[%d] = '%s'\n", n, ENV[n]);
306 }
307#endif
308
309 setpgid(0, getpid());
310
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800311 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700312 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800313 if (setgid(svc->gid) != 0) {
314 ERROR("setgid failed: %s\n", strerror(errno));
315 _exit(127);
316 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700317 }
318 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800319 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
320 ERROR("setgroups failed: %s\n", strerror(errno));
321 _exit(127);
322 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700323 }
324 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800325 if (setuid(svc->uid) != 0) {
326 ERROR("setuid failed: %s\n", strerror(errno));
327 _exit(127);
328 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700329 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500330 if (svc->seclabel) {
331 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) {
332 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
333 _exit(127);
334 }
335 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500336
San Mehat8ad15682009-05-20 08:50:40 -0700337 if (!dynamic_args) {
338 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
339 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
340 }
341 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700342 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700343 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700344 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700345 char *next = tmp;
346 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700347
348 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700349 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700350
San Mehatd4cdd132009-05-20 09:52:16 -0700351 while((bword = strsep(&next, " "))) {
352 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700353 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700354 break;
San Mehatf24e2522009-05-19 13:30:46 -0700355 }
356 arg_ptrs[arg_idx] = '\0';
357 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100358 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700359 _exit(127);
360 }
361
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500362 freecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500363
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700364 if (pid < 0) {
365 ERROR("failed to start '%s'\n", svc->name);
366 svc->pid = 0;
367 return;
368 }
369
370 svc->time_started = gettime();
371 svc->pid = pid;
372 svc->flags |= SVC_RUNNING;
373
Colin Cross3294bbb2010-04-19 17:11:33 -0700374 if (properties_inited())
375 notify_service_state(svc->name, "running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700376}
377
Ken Sumrall752923c2010-12-03 16:33:31 -0800378/* The how field should be either SVC_DISABLED or SVC_RESET */
379static void service_stop_or_reset(struct service *svc, int how)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700380{
381 /* we are no longer running, nor should we
382 * attempt to restart
383 */
384 svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING));
385
Ken Sumrall752923c2010-12-03 16:33:31 -0800386 if ((how != SVC_DISABLED) && (how != SVC_RESET)) {
387 /* Hrm, an illegal flag. Default to SVC_DISABLED */
388 how = SVC_DISABLED;
389 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700390 /* if the service has not yet started, prevent
391 * it from auto-starting with its class
392 */
Ken Sumralla2864802011-10-26 16:56:00 -0700393 if (how == SVC_RESET) {
394 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
395 } else {
396 svc->flags |= how;
397 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700398
399 if (svc->pid) {
400 NOTICE("service '%s' is being killed\n", svc->name);
Ken Sumrall752923c2010-12-03 16:33:31 -0800401 kill(-svc->pid, SIGKILL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700402 notify_service_state(svc->name, "stopping");
403 } else {
404 notify_service_state(svc->name, "stopped");
405 }
406}
407
Ken Sumrall752923c2010-12-03 16:33:31 -0800408void service_reset(struct service *svc)
409{
410 service_stop_or_reset(svc, SVC_RESET);
411}
412
413void service_stop(struct service *svc)
414{
415 service_stop_or_reset(svc, SVC_DISABLED);
416}
417
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700418void property_changed(const char *name, const char *value)
419{
Colin Crossebc6ff12010-04-13 19:52:01 -0700420 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700421 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700422}
423
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700424static void restart_service_if_needed(struct service *svc)
425{
426 time_t next_start_time = svc->time_started + 5;
427
428 if (next_start_time <= gettime()) {
429 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700430 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700431 return;
432 }
433
434 if ((next_start_time < process_needs_restart) ||
435 (process_needs_restart == 0)) {
436 process_needs_restart = next_start_time;
437 }
438}
439
440static void restart_processes()
441{
442 process_needs_restart = 0;
443 service_for_each_flags(SVC_RESTARTING,
444 restart_service_if_needed);
445}
446
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700447static void msg_start(const char *name)
448{
San Mehatf24e2522009-05-19 13:30:46 -0700449 struct service *svc;
450 char *tmp = NULL;
451 char *args = NULL;
452
453 if (!strchr(name, ':'))
454 svc = service_find_by_name(name);
455 else {
456 tmp = strdup(name);
San Mehatf24e2522009-05-19 13:30:46 -0700457 args = strchr(tmp, ':');
458 *args = '\0';
459 args++;
460
461 svc = service_find_by_name(tmp);
462 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700463
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700464 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700465 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700466 } else {
467 ERROR("no such service '%s'\n", name);
468 }
San Mehatf24e2522009-05-19 13:30:46 -0700469 if (tmp)
470 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700471}
472
473static void msg_stop(const char *name)
474{
475 struct service *svc = service_find_by_name(name);
476
477 if (svc) {
478 service_stop(svc);
479 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700480 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700481 }
482}
483
484void handle_control_message(const char *msg, const char *arg)
485{
486 if (!strcmp(msg,"start")) {
487 msg_start(arg);
488 } else if (!strcmp(msg,"stop")) {
489 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700490 } else if (!strcmp(msg,"restart")) {
491 msg_stop(arg);
492 msg_start(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700493 } else {
494 ERROR("unknown control msg '%s'\n", msg);
495 }
496}
497
Colin Crossebc6ff12010-04-13 19:52:01 -0700498static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700499{
500 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700501 node = list_head(&act->commands);
Dima Zavin3bea0792011-08-26 13:59:18 -0700502 if (!node || list_empty(&act->commands))
Colin Crossebc6ff12010-04-13 19:52:01 -0700503 return NULL;
504
505 return node_to_item(node, struct command, clist);
506}
507
508static struct command *get_next_command(struct action *act, struct command *cmd)
509{
510 struct listnode *node;
511 node = cmd->clist.next;
512 if (!node)
513 return NULL;
514 if (node == &act->commands)
515 return NULL;
516
517 return node_to_item(node, struct command, clist);
518}
519
520static int is_last_command(struct action *act, struct command *cmd)
521{
522 return (list_tail(&act->commands) == &cmd->clist);
523}
524
525void execute_one_command(void)
526{
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700527 int ret;
528
Colin Crossebc6ff12010-04-13 19:52:01 -0700529 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
530 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700531 cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -0700532 if (!cur_action)
533 return;
534 INFO("processing action %p (%s)\n", cur_action, cur_action->name);
535 cur_command = get_first_command(cur_action);
536 } else {
537 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700538 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700539
540 if (!cur_command)
541 return;
542
543 ret = cur_command->func(cur_command->nargs, cur_command->args);
544 INFO("command '%s' r=%d\n", cur_command->args[0], ret);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700545}
546
Colin Crossf83d0b92010-04-21 12:04:20 -0700547static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700548{
Colin Crossf83d0b92010-04-21 12:04:20 -0700549 int ret;
550 INFO("wait for %s\n", coldboot_done);
551 ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT);
552 if (ret)
553 ERROR("Timed out waiting for %s\n", coldboot_done);
554 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700555}
556
Colin Crossebc6ff12010-04-13 19:52:01 -0700557static int keychord_init_action(int nargs, char **args)
558{
559 keychord_init();
560 return 0;
561}
562
563static int console_init_action(int nargs, char **args)
564{
565 int fd;
566 char tmp[PROP_VALUE_MAX];
567
568 if (console[0]) {
569 snprintf(tmp, sizeof(tmp), "/dev/%s", console);
570 console_name = strdup(tmp);
571 }
572
573 fd = open(console_name, O_RDWR);
574 if (fd >= 0)
575 have_console = 1;
576 close(fd);
577
578 if( load_565rle_image(INIT_IMAGE_FILE) ) {
579 fd = open("/dev/tty0", O_WRONLY);
580 if (fd >= 0) {
581 const char *msg;
582 msg = "\n"
583 "\n"
584 "\n"
585 "\n"
586 "\n"
587 "\n"
588 "\n" // console is 40 cols x 30 lines
589 "\n"
590 "\n"
591 "\n"
592 "\n"
593 "\n"
594 "\n"
595 "\n"
596 " A N D R O I D ";
597 write(fd, msg, strlen(msg));
598 close(fd);
599 }
600 }
601 return 0;
602}
603
Dima Zavin5511c842011-12-19 11:21:32 -0800604static void import_kernel_nv(char *name, int for_emulator)
605{
606 char *value = strchr(name, '=');
607 int name_len = strlen(name);
608
609 if (value == 0) return;
610 *value++ = 0;
611 if (name_len == 0) return;
612
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400613 if (!strcmp(name,"selinux")) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500614 selinux_enabled = atoi(value);
615 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500616
Dima Zavin5511c842011-12-19 11:21:32 -0800617 if (for_emulator) {
618 /* in the emulator, export any kernel option with the
619 * ro.kernel. prefix */
620 char buff[PROP_NAME_MAX];
621 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
622
623 if (len < (int)sizeof(buff))
624 property_set( buff, value );
625 return;
626 }
627
628 if (!strcmp(name,"qemu")) {
629 strlcpy(qemu, value, sizeof(qemu));
630 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
631 const char *boot_prop_name = name + 12;
632 char prop[PROP_NAME_MAX];
633 int cnt;
634
635 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
636 if (cnt < PROP_NAME_MAX)
637 property_set(prop, value);
638 }
639}
640
641static void export_kernel_boot_props(void)
Colin Crossebc6ff12010-04-13 19:52:01 -0700642{
643 char tmp[PROP_VALUE_MAX];
Dima Zavin5511c842011-12-19 11:21:32 -0800644 const char *pval;
645 unsigned i;
646 struct {
647 const char *src_prop;
648 const char *dest_prop;
649 const char *def_val;
650 } prop_map[] = {
651 { "ro.boot.serialno", "ro.serialno", "", },
652 { "ro.boot.mode", "ro.bootmode", "unknown", },
653 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800654 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
655 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700656
Dima Zavin5511c842011-12-19 11:21:32 -0800657 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
658 pval = property_get(prop_map[i].src_prop);
659 property_set(prop_map[i].dest_prop, pval ?: prop_map[i].def_val);
660 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700661
Dima Zavin5511c842011-12-19 11:21:32 -0800662 pval = property_get("ro.boot.console");
663 if (pval)
664 strlcpy(console, pval, sizeof(console));
665
666 /* save a copy for init's usage during boot */
667 strlcpy(bootmode, property_get("ro.bootmode"), sizeof(bootmode));
668
669 /* if this was given on kernel command line, override what we read
670 * before (e.g. from /proc/cpuinfo), if anything */
671 pval = property_get("ro.boot.hardware");
672 if (pval)
673 strlcpy(hardware, pval, sizeof(hardware));
674 property_set("ro.hardware", hardware);
675
676 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
677 property_set("ro.revision", tmp);
678
679 /* TODO: these are obsolete. We should delete them */
Colin Crossebc6ff12010-04-13 19:52:01 -0700680 if (!strcmp(bootmode,"factory"))
681 property_set("ro.factorytest", "1");
682 else if (!strcmp(bootmode,"factory2"))
683 property_set("ro.factorytest", "2");
684 else
685 property_set("ro.factorytest", "0");
Dima Zavin5511c842011-12-19 11:21:32 -0800686}
Colin Crossebc6ff12010-04-13 19:52:01 -0700687
Dima Zavin5511c842011-12-19 11:21:32 -0800688static void process_kernel_cmdline(void)
689{
690 /* don't expose the raw commandline to nonpriv processes */
691 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700692
Dima Zavin5511c842011-12-19 11:21:32 -0800693 /* first pass does the common stuff, and finds if we are in qemu.
694 * second pass is only necessary for qemu to export all kernel params
695 * as props.
696 */
697 import_kernel_cmdline(0, import_kernel_nv);
698 if (qemu[0])
699 import_kernel_cmdline(1, import_kernel_nv);
700
701 /* now propogate the info given on command line to internal variables
702 * used by init as well as the current required properties
703 */
704 export_kernel_boot_props();
Colin Crossebc6ff12010-04-13 19:52:01 -0700705}
706
707static int property_service_init_action(int nargs, char **args)
708{
709 /* read any property files on system or data and
710 * fire up the property service. This must happen
711 * after the ro.foo properties are set above so
712 * that /data/local.prop cannot interfere with them.
713 */
714 start_property_service();
715 return 0;
716}
717
718static int signal_init_action(int nargs, char **args)
719{
720 signal_init();
721 return 0;
722}
723
724static int check_startup_action(int nargs, char **args)
725{
726 /* make sure we actually have all the pieces we need */
Colin Crossf83d0b92010-04-21 12:04:20 -0700727 if ((get_property_set_fd() < 0) ||
Colin Crossebc6ff12010-04-13 19:52:01 -0700728 (get_signal_fd() < 0)) {
729 ERROR("init startup failure\n");
730 exit(1);
731 }
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700732
733 /* signal that we hit this point */
734 unlink("/dev/.booting");
735
Colin Crossebc6ff12010-04-13 19:52:01 -0700736 return 0;
737}
738
739static int queue_property_triggers_action(int nargs, char **args)
740{
741 queue_all_property_triggers();
742 /* enable property triggers */
743 property_triggers_enabled = 1;
744 return 0;
745}
746
747#if BOOTCHART
748static int bootchart_init_action(int nargs, char **args)
749{
750 bootchart_count = bootchart_init();
751 if (bootchart_count < 0) {
752 ERROR("bootcharting init failure\n");
753 } else if (bootchart_count > 0) {
754 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
755 } else {
756 NOTICE("bootcharting ignored\n");
757 }
Carl-Emil Lagerstedt9ab81902011-01-14 09:35:30 +0100758
759 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700760}
761#endif
762
rpcraig63207cd2012-08-09 10:05:49 -0400763static const struct selinux_opt seopts_prop[] = {
764 { SELABEL_OPT_PATH, "/data/system/property_contexts" },
765 { SELABEL_OPT_PATH, "/property_contexts" },
766 { 0, NULL }
767};
768
769struct selabel_handle* selinux_android_prop_context_handle(void)
770{
771 int i = 0;
772 struct selabel_handle* sehandle = NULL;
773 while ((sehandle == NULL) && seopts_prop[i].value) {
774 sehandle = selabel_open(SELABEL_CTX_ANDROID_PROP, &seopts_prop[i], 1);
775 i++;
776 }
777
778 if (!sehandle) {
779 ERROR("SELinux: Could not load property_contexts: %s\n",
780 strerror(errno));
781 return NULL;
782 }
783 INFO("SELinux: Loaded property contexts from %s\n", seopts_prop[i - 1].value);
784 return sehandle;
785}
786
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400787void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500788{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400789 sehandle = selinux_android_file_context_handle();
rpcraig63207cd2012-08-09 10:05:49 -0400790 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400791}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500792
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400793int selinux_reload_policy(void)
794{
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500795 if (!selinux_enabled) {
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400796 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500797 }
798
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400799 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500800
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400801 if (selinux_android_reload_policy() == -1) {
802 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500803 }
804
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400805 if (sehandle)
806 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500807
rpcraig63207cd2012-08-09 10:05:49 -0400808 if (sehandle_prop)
809 selabel_close(sehandle_prop);
810
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400811 selinux_init_all_handles();
812 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500813}
rpcraig63207cd2012-08-09 10:05:49 -0400814
815int audit_callback(void *data, security_class_t cls, char *buf, size_t len)
816{
817 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
818 return 0;
819}
820
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700821int main(int argc, char **argv)
822{
Colin Crossebc6ff12010-04-13 19:52:01 -0700823 int fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700824 struct pollfd ufds[4];
825 char *tmpdev;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800826 char* debuggable;
Colin Crossebc6ff12010-04-13 19:52:01 -0700827 char tmp[32];
Colin Crossebc6ff12010-04-13 19:52:01 -0700828 int property_set_fd_init = 0;
829 int signal_fd_init = 0;
830 int keychord_fd_init = 0;
Dima Zavind7634c92011-12-16 14:18:06 -0800831 bool is_charger = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700832
Colin Crossf83d0b92010-04-21 12:04:20 -0700833 if (!strcmp(basename(argv[0]), "ueventd"))
834 return ueventd_main(argc, argv);
835
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700836 if (!strcmp(basename(argv[0]), "watchdogd"))
837 return watchdogd_main(argc, argv);
838
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700839 /* clear the umask */
840 umask(0);
841
842 /* Get the basic filesystem setup we need put
843 * together in the initramdisk on / and then we'll
844 * let the rc file figure out the rest.
845 */
846 mkdir("/dev", 0755);
847 mkdir("/proc", 0755);
848 mkdir("/sys", 0755);
849
Nick Kralevich150f19e2010-06-22 16:35:43 -0700850 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700851 mkdir("/dev/pts", 0755);
852 mkdir("/dev/socket", 0755);
853 mount("devpts", "/dev/pts", "devpts", 0, NULL);
854 mount("proc", "/proc", "proc", 0, NULL);
855 mount("sysfs", "/sys", "sysfs", 0, NULL);
856
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700857 /* indicate that booting is in progress to background fw loaders, etc */
858 close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000));
859
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700860 /* We must have some place other than / to create the
861 * device nodes for kmsg and null, otherwise we won't
862 * be able to remount / read-only later on.
863 * Now that tmpfs is mounted on /dev, we can actually
864 * talk to the outside world.
865 */
866 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -0700867 klog_init();
Dima Zavin5511c842011-12-19 11:21:32 -0800868 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700869
Colin Crossf83d0b92010-04-21 12:04:20 -0700870 get_hardware_name(hardware, &revision);
Dima Zavind7634c92011-12-16 14:18:06 -0800871
Dima Zavin5511c842011-12-19 11:21:32 -0800872 process_kernel_cmdline();
873
rpcraig63207cd2012-08-09 10:05:49 -0400874 union selinux_callback cb;
875 cb.func_log = klog_write;
876 selinux_set_callback(SELINUX_CB_LOG, cb);
877
878 cb.func_audit = audit_callback;
879 selinux_set_callback(SELINUX_CB_AUDIT, cb);
880
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500881 INFO("loading selinux policy\n");
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400882 if (selinux_enabled) {
883 if (selinux_android_load_policy() < 0) {
884 selinux_enabled = 0;
885 INFO("SELinux: Disabled due to failed policy load\n");
886 } else {
887 selinux_init_all_handles();
888 }
889 } else {
890 INFO("SELinux: Disabled by command line option\n");
891 }
892 /* These directories were necessarily created before initial policy load
Stephen Smalleye096e362012-06-11 13:37:39 -0400893 * and therefore need their security context restored to the proper value.
894 * This must happen before /dev is populated by ueventd.
895 */
896 restorecon("/dev");
897 restorecon("/dev/socket");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500898
Dima Zavind7634c92011-12-16 14:18:06 -0800899 is_charger = !strcmp(bootmode, "charger");
900
901 INFO("property init\n");
Dima Zavin5511c842011-12-19 11:21:32 -0800902 if (!is_charger)
903 property_load_boot_defaults();
Dima Zavind7634c92011-12-16 14:18:06 -0800904
905 INFO("reading config file\n");
906 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700907
908 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700909
Colin Crossf83d0b92010-04-21 12:04:20 -0700910 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Colin Crossebc6ff12010-04-13 19:52:01 -0700911 queue_builtin_action(keychord_init_action, "keychord_init");
912 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700913
Dima Zavinca47cef2011-08-24 15:28:23 -0700914 /* execute all the boot actions to get us started */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700915 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -0700916
917 /* skip mounting filesystems in charger mode */
Dima Zavind7634c92011-12-16 14:18:06 -0800918 if (!is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -0700919 action_for_each_trigger("early-fs", action_add_queue_tail);
920 action_for_each_trigger("fs", action_add_queue_tail);
921 action_for_each_trigger("post-fs", action_add_queue_tail);
922 action_for_each_trigger("post-fs-data", action_add_queue_tail);
923 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700924
Colin Crossebc6ff12010-04-13 19:52:01 -0700925 queue_builtin_action(property_service_init_action, "property_service_init");
926 queue_builtin_action(signal_init_action, "signal_init");
927 queue_builtin_action(check_startup_action, "check_startup");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700928
Dima Zavind7634c92011-12-16 14:18:06 -0800929 if (is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -0700930 action_for_each_trigger("charger", action_add_queue_tail);
931 } else {
932 action_for_each_trigger("early-boot", action_add_queue_tail);
933 action_for_each_trigger("boot", action_add_queue_tail);
934 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700935
936 /* run all property triggers based on current state of the properties */
Chris Dearman469b7b22012-03-01 15:29:20 -0800937 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700938
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700939
940#if BOOTCHART
Colin Crossebc6ff12010-04-13 19:52:01 -0700941 queue_builtin_action(bootchart_init_action, "bootchart_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700942#endif
943
944 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800945 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700946
Colin Crossebc6ff12010-04-13 19:52:01 -0700947 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700948 restart_processes();
949
Colin Crossebc6ff12010-04-13 19:52:01 -0700950 if (!property_set_fd_init && get_property_set_fd() > 0) {
951 ufds[fd_count].fd = get_property_set_fd();
952 ufds[fd_count].events = POLLIN;
953 ufds[fd_count].revents = 0;
954 fd_count++;
955 property_set_fd_init = 1;
956 }
957 if (!signal_fd_init && get_signal_fd() > 0) {
958 ufds[fd_count].fd = get_signal_fd();
959 ufds[fd_count].events = POLLIN;
960 ufds[fd_count].revents = 0;
961 fd_count++;
962 signal_fd_init = 1;
963 }
964 if (!keychord_fd_init && get_keychord_fd() > 0) {
965 ufds[fd_count].fd = get_keychord_fd();
966 ufds[fd_count].events = POLLIN;
967 ufds[fd_count].revents = 0;
968 fd_count++;
969 keychord_fd_init = 1;
970 }
971
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700972 if (process_needs_restart) {
973 timeout = (process_needs_restart - gettime()) * 1000;
974 if (timeout < 0)
975 timeout = 0;
976 }
977
Colin Crossebd46132010-04-22 11:52:23 -0700978 if (!action_queue_empty() || cur_action)
Colin Crossebc6ff12010-04-13 19:52:01 -0700979 timeout = 0;
980
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700981#if BOOTCHART
982 if (bootchart_count > 0) {
983 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
984 timeout = BOOTCHART_POLLING_MS;
985 if (bootchart_step() < 0 || --bootchart_count == 0) {
986 bootchart_finish();
987 bootchart_count = 0;
988 }
989 }
990#endif
Colin Crossebc6ff12010-04-13 19:52:01 -0700991
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800992 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700993 if (nr <= 0)
994 continue;
995
Colin Crossebc6ff12010-04-13 19:52:01 -0700996 for (i = 0; i < fd_count; i++) {
997 if (ufds[i].revents == POLLIN) {
Colin Crossf83d0b92010-04-21 12:04:20 -0700998 if (ufds[i].fd == get_property_set_fd())
Colin Crossebc6ff12010-04-13 19:52:01 -0700999 handle_property_set_fd();
1000 else if (ufds[i].fd == get_keychord_fd())
1001 handle_keychord();
1002 else if (ufds[i].fd == get_signal_fd())
1003 handle_signal();
1004 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001005 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001006 }
1007
1008 return 0;
1009}