blob: b20b434b02a1e3d0cacd1d851255216b762a92b8 [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>
Nick Kralevich01b1dee2012-10-05 11:17:21 -070034#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) {
201 char *mycon = NULL, *fcon = NULL;
202
203 INFO("computing context for service '%s'\n", svc->args[0]);
204 rc = getcon(&mycon);
205 if (rc < 0) {
206 ERROR("could not get context while starting '%s'\n", svc->name);
207 return;
208 }
209
210 rc = getfilecon(svc->args[0], &fcon);
211 if (rc < 0) {
212 ERROR("could not get context while starting '%s'\n", svc->name);
213 freecon(mycon);
214 return;
215 }
216
217 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon);
218 freecon(mycon);
219 freecon(fcon);
220 if (rc < 0) {
221 ERROR("could not get context while starting '%s'\n", svc->name);
222 return;
223 }
224 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500225
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700226 NOTICE("starting '%s'\n", svc->name);
227
228 pid = fork();
229
230 if (pid == 0) {
231 struct socketinfo *si;
232 struct svcenvinfo *ei;
233 char tmp[32];
234 int fd, sz;
235
Nick Kralevich6ebf12f2012-03-26 09:09:11 -0700236 umask(077);
Nick Kralevich01b1dee2012-10-05 11:17:21 -0700237#ifdef __arm__
238 /*
239 * b/7188322 - Temporarily revert to the compat memory layout
240 * to avoid breaking third party apps.
241 *
242 * THIS WILL GO AWAY IN A FUTURE ANDROID RELEASE.
243 *
244 * http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commitdiff;h=7dbaa466
245 * changes the kernel mapping from bottom up to top-down.
246 * This breaks some programs which improperly embed
247 * an out of date copy of Android's linker.
248 */
249 int current = personality(0xffffFFFF);
250 personality(current | ADDR_COMPAT_LAYOUT);
251#endif
Colin Cross3294bbb2010-04-19 17:11:33 -0700252 if (properties_inited()) {
253 get_property_workspace(&fd, &sz);
254 sprintf(tmp, "%d,%d", dup(fd), sz);
255 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
256 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700257
258 for (ei = svc->envvars; ei; ei = ei->next)
259 add_environment(ei->name, ei->value);
260
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500261 setsockcreatecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500262
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700263 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400264 int socket_type = (
265 !strcmp(si->type, "stream") ? SOCK_STREAM :
266 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
267 int s = create_socket(si->name, socket_type,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700268 si->perm, si->uid, si->gid);
269 if (s >= 0) {
270 publish_socket(si->name, s);
271 }
272 }
273
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500274 freecon(scon);
275 scon = NULL;
276 setsockcreatecon(NULL);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500277
San Mehat4e221f02010-02-25 14:19:50 -0800278 if (svc->ioprio_class != IoSchedClass_NONE) {
279 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
280 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
281 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
282 }
283 }
284
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700285 if (needs_console) {
286 setsid();
287 open_console();
288 } else {
289 zap_stdio();
290 }
291
292#if 0
293 for (n = 0; svc->args[n]; n++) {
294 INFO("args[%d] = '%s'\n", n, svc->args[n]);
295 }
296 for (n = 0; ENV[n]; n++) {
297 INFO("env[%d] = '%s'\n", n, ENV[n]);
298 }
299#endif
300
301 setpgid(0, getpid());
302
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800303 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700304 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800305 if (setgid(svc->gid) != 0) {
306 ERROR("setgid failed: %s\n", strerror(errno));
307 _exit(127);
308 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700309 }
310 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800311 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
312 ERROR("setgroups failed: %s\n", strerror(errno));
313 _exit(127);
314 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700315 }
316 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800317 if (setuid(svc->uid) != 0) {
318 ERROR("setuid failed: %s\n", strerror(errno));
319 _exit(127);
320 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700321 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500322 if (svc->seclabel) {
323 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) {
324 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
325 _exit(127);
326 }
327 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500328
San Mehat8ad15682009-05-20 08:50:40 -0700329 if (!dynamic_args) {
330 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
331 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
332 }
333 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700334 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700335 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700336 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700337 char *next = tmp;
338 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700339
340 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700341 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700342
San Mehatd4cdd132009-05-20 09:52:16 -0700343 while((bword = strsep(&next, " "))) {
344 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700345 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700346 break;
San Mehatf24e2522009-05-19 13:30:46 -0700347 }
348 arg_ptrs[arg_idx] = '\0';
349 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100350 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700351 _exit(127);
352 }
353
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500354 freecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500355
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 Smalleyae6f3d72012-05-01 15:02:53 -0400605 if (!strcmp(name,"selinux")) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500606 selinux_enabled = atoi(value);
607 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500608
Dima Zavin5511c842011-12-19 11:21:32 -0800609 if (for_emulator) {
610 /* in the emulator, export any kernel option with the
611 * ro.kernel. prefix */
612 char buff[PROP_NAME_MAX];
613 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
614
615 if (len < (int)sizeof(buff))
616 property_set( buff, value );
617 return;
618 }
619
620 if (!strcmp(name,"qemu")) {
621 strlcpy(qemu, value, sizeof(qemu));
622 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
623 const char *boot_prop_name = name + 12;
624 char prop[PROP_NAME_MAX];
625 int cnt;
626
627 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
628 if (cnt < PROP_NAME_MAX)
629 property_set(prop, value);
630 }
631}
632
633static void export_kernel_boot_props(void)
Colin Crossebc6ff12010-04-13 19:52:01 -0700634{
635 char tmp[PROP_VALUE_MAX];
Dima Zavin5511c842011-12-19 11:21:32 -0800636 const char *pval;
637 unsigned i;
638 struct {
639 const char *src_prop;
640 const char *dest_prop;
641 const char *def_val;
642 } prop_map[] = {
643 { "ro.boot.serialno", "ro.serialno", "", },
644 { "ro.boot.mode", "ro.bootmode", "unknown", },
645 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800646 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
647 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700648
Dima Zavin5511c842011-12-19 11:21:32 -0800649 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
650 pval = property_get(prop_map[i].src_prop);
651 property_set(prop_map[i].dest_prop, pval ?: prop_map[i].def_val);
652 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700653
Dima Zavin5511c842011-12-19 11:21:32 -0800654 pval = property_get("ro.boot.console");
655 if (pval)
656 strlcpy(console, pval, sizeof(console));
657
658 /* save a copy for init's usage during boot */
659 strlcpy(bootmode, property_get("ro.bootmode"), sizeof(bootmode));
660
661 /* if this was given on kernel command line, override what we read
662 * before (e.g. from /proc/cpuinfo), if anything */
663 pval = property_get("ro.boot.hardware");
664 if (pval)
665 strlcpy(hardware, pval, sizeof(hardware));
666 property_set("ro.hardware", hardware);
667
668 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
669 property_set("ro.revision", tmp);
670
671 /* TODO: these are obsolete. We should delete them */
Colin Crossebc6ff12010-04-13 19:52:01 -0700672 if (!strcmp(bootmode,"factory"))
673 property_set("ro.factorytest", "1");
674 else if (!strcmp(bootmode,"factory2"))
675 property_set("ro.factorytest", "2");
676 else
677 property_set("ro.factorytest", "0");
Dima Zavin5511c842011-12-19 11:21:32 -0800678}
Colin Crossebc6ff12010-04-13 19:52:01 -0700679
Dima Zavin5511c842011-12-19 11:21:32 -0800680static void process_kernel_cmdline(void)
681{
682 /* don't expose the raw commandline to nonpriv processes */
683 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700684
Dima Zavin5511c842011-12-19 11:21:32 -0800685 /* first pass does the common stuff, and finds if we are in qemu.
686 * second pass is only necessary for qemu to export all kernel params
687 * as props.
688 */
689 import_kernel_cmdline(0, import_kernel_nv);
690 if (qemu[0])
691 import_kernel_cmdline(1, import_kernel_nv);
692
693 /* now propogate the info given on command line to internal variables
694 * used by init as well as the current required properties
695 */
696 export_kernel_boot_props();
Colin Crossebc6ff12010-04-13 19:52:01 -0700697}
698
699static int property_service_init_action(int nargs, char **args)
700{
701 /* read any property files on system or data and
702 * fire up the property service. This must happen
703 * after the ro.foo properties are set above so
704 * that /data/local.prop cannot interfere with them.
705 */
706 start_property_service();
707 return 0;
708}
709
710static int signal_init_action(int nargs, char **args)
711{
712 signal_init();
713 return 0;
714}
715
716static int check_startup_action(int nargs, char **args)
717{
718 /* make sure we actually have all the pieces we need */
Colin Crossf83d0b92010-04-21 12:04:20 -0700719 if ((get_property_set_fd() < 0) ||
Colin Crossebc6ff12010-04-13 19:52:01 -0700720 (get_signal_fd() < 0)) {
721 ERROR("init startup failure\n");
722 exit(1);
723 }
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700724
725 /* signal that we hit this point */
726 unlink("/dev/.booting");
727
Colin Crossebc6ff12010-04-13 19:52:01 -0700728 return 0;
729}
730
731static int queue_property_triggers_action(int nargs, char **args)
732{
733 queue_all_property_triggers();
734 /* enable property triggers */
735 property_triggers_enabled = 1;
736 return 0;
737}
738
739#if BOOTCHART
740static int bootchart_init_action(int nargs, char **args)
741{
742 bootchart_count = bootchart_init();
743 if (bootchart_count < 0) {
744 ERROR("bootcharting init failure\n");
745 } else if (bootchart_count > 0) {
746 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
747 } else {
748 NOTICE("bootcharting ignored\n");
749 }
Carl-Emil Lagerstedt9ab81902011-01-14 09:35:30 +0100750
751 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700752}
753#endif
754
rpcraig63207cd2012-08-09 10:05:49 -0400755static const struct selinux_opt seopts_prop[] = {
756 { SELABEL_OPT_PATH, "/data/system/property_contexts" },
757 { SELABEL_OPT_PATH, "/property_contexts" },
758 { 0, NULL }
759};
760
761struct selabel_handle* selinux_android_prop_context_handle(void)
762{
763 int i = 0;
764 struct selabel_handle* sehandle = NULL;
765 while ((sehandle == NULL) && seopts_prop[i].value) {
766 sehandle = selabel_open(SELABEL_CTX_ANDROID_PROP, &seopts_prop[i], 1);
767 i++;
768 }
769
770 if (!sehandle) {
771 ERROR("SELinux: Could not load property_contexts: %s\n",
772 strerror(errno));
773 return NULL;
774 }
775 INFO("SELinux: Loaded property contexts from %s\n", seopts_prop[i - 1].value);
776 return sehandle;
777}
778
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400779void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500780{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400781 sehandle = selinux_android_file_context_handle();
rpcraig63207cd2012-08-09 10:05:49 -0400782 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400783}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500784
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400785int selinux_reload_policy(void)
786{
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500787 if (!selinux_enabled) {
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400788 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500789 }
790
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400791 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500792
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400793 if (selinux_android_reload_policy() == -1) {
794 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500795 }
796
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400797 if (sehandle)
798 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500799
rpcraig63207cd2012-08-09 10:05:49 -0400800 if (sehandle_prop)
801 selabel_close(sehandle_prop);
802
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400803 selinux_init_all_handles();
804 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500805}
rpcraig63207cd2012-08-09 10:05:49 -0400806
807int audit_callback(void *data, security_class_t cls, char *buf, size_t len)
808{
809 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
810 return 0;
811}
812
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700813int main(int argc, char **argv)
814{
Colin Crossebc6ff12010-04-13 19:52:01 -0700815 int fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700816 struct pollfd ufds[4];
817 char *tmpdev;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800818 char* debuggable;
Colin Crossebc6ff12010-04-13 19:52:01 -0700819 char tmp[32];
Colin Crossebc6ff12010-04-13 19:52:01 -0700820 int property_set_fd_init = 0;
821 int signal_fd_init = 0;
822 int keychord_fd_init = 0;
Dima Zavind7634c92011-12-16 14:18:06 -0800823 bool is_charger = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700824
Colin Crossf83d0b92010-04-21 12:04:20 -0700825 if (!strcmp(basename(argv[0]), "ueventd"))
826 return ueventd_main(argc, argv);
827
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700828 if (!strcmp(basename(argv[0]), "watchdogd"))
829 return watchdogd_main(argc, argv);
830
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700831 /* clear the umask */
832 umask(0);
833
834 /* Get the basic filesystem setup we need put
835 * together in the initramdisk on / and then we'll
836 * let the rc file figure out the rest.
837 */
838 mkdir("/dev", 0755);
839 mkdir("/proc", 0755);
840 mkdir("/sys", 0755);
841
Nick Kralevich150f19e2010-06-22 16:35:43 -0700842 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700843 mkdir("/dev/pts", 0755);
844 mkdir("/dev/socket", 0755);
845 mount("devpts", "/dev/pts", "devpts", 0, NULL);
846 mount("proc", "/proc", "proc", 0, NULL);
847 mount("sysfs", "/sys", "sysfs", 0, NULL);
848
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700849 /* indicate that booting is in progress to background fw loaders, etc */
850 close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000));
851
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700852 /* We must have some place other than / to create the
853 * device nodes for kmsg and null, otherwise we won't
854 * be able to remount / read-only later on.
855 * Now that tmpfs is mounted on /dev, we can actually
856 * talk to the outside world.
857 */
858 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -0700859 klog_init();
Dima Zavin5511c842011-12-19 11:21:32 -0800860 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700861
Colin Crossf83d0b92010-04-21 12:04:20 -0700862 get_hardware_name(hardware, &revision);
Dima Zavind7634c92011-12-16 14:18:06 -0800863
Dima Zavin5511c842011-12-19 11:21:32 -0800864 process_kernel_cmdline();
865
rpcraig63207cd2012-08-09 10:05:49 -0400866 union selinux_callback cb;
867 cb.func_log = klog_write;
868 selinux_set_callback(SELINUX_CB_LOG, cb);
869
870 cb.func_audit = audit_callback;
871 selinux_set_callback(SELINUX_CB_AUDIT, cb);
872
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500873 INFO("loading selinux policy\n");
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400874 if (selinux_enabled) {
875 if (selinux_android_load_policy() < 0) {
876 selinux_enabled = 0;
877 INFO("SELinux: Disabled due to failed policy load\n");
878 } else {
879 selinux_init_all_handles();
880 }
881 } else {
882 INFO("SELinux: Disabled by command line option\n");
883 }
884 /* These directories were necessarily created before initial policy load
Stephen Smalleye096e362012-06-11 13:37:39 -0400885 * and therefore need their security context restored to the proper value.
886 * This must happen before /dev is populated by ueventd.
887 */
888 restorecon("/dev");
889 restorecon("/dev/socket");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500890
Dima Zavind7634c92011-12-16 14:18:06 -0800891 is_charger = !strcmp(bootmode, "charger");
892
893 INFO("property init\n");
Dima Zavin5511c842011-12-19 11:21:32 -0800894 if (!is_charger)
895 property_load_boot_defaults();
Dima Zavind7634c92011-12-16 14:18:06 -0800896
897 INFO("reading config file\n");
898 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700899
900 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700901
Colin Crossf83d0b92010-04-21 12:04:20 -0700902 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Colin Crossebc6ff12010-04-13 19:52:01 -0700903 queue_builtin_action(keychord_init_action, "keychord_init");
904 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700905
Dima Zavinca47cef2011-08-24 15:28:23 -0700906 /* execute all the boot actions to get us started */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700907 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -0700908
909 /* skip mounting filesystems in charger mode */
Dima Zavind7634c92011-12-16 14:18:06 -0800910 if (!is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -0700911 action_for_each_trigger("early-fs", action_add_queue_tail);
912 action_for_each_trigger("fs", action_add_queue_tail);
913 action_for_each_trigger("post-fs", action_add_queue_tail);
914 action_for_each_trigger("post-fs-data", action_add_queue_tail);
915 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700916
Colin Crossebc6ff12010-04-13 19:52:01 -0700917 queue_builtin_action(property_service_init_action, "property_service_init");
918 queue_builtin_action(signal_init_action, "signal_init");
919 queue_builtin_action(check_startup_action, "check_startup");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700920
Dima Zavind7634c92011-12-16 14:18:06 -0800921 if (is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -0700922 action_for_each_trigger("charger", action_add_queue_tail);
923 } else {
924 action_for_each_trigger("early-boot", action_add_queue_tail);
925 action_for_each_trigger("boot", action_add_queue_tail);
926 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700927
928 /* run all property triggers based on current state of the properties */
Chris Dearman469b7b22012-03-01 15:29:20 -0800929 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700930
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700931
932#if BOOTCHART
Colin Crossebc6ff12010-04-13 19:52:01 -0700933 queue_builtin_action(bootchart_init_action, "bootchart_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700934#endif
935
936 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800937 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700938
Colin Crossebc6ff12010-04-13 19:52:01 -0700939 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700940 restart_processes();
941
Colin Crossebc6ff12010-04-13 19:52:01 -0700942 if (!property_set_fd_init && get_property_set_fd() > 0) {
943 ufds[fd_count].fd = get_property_set_fd();
944 ufds[fd_count].events = POLLIN;
945 ufds[fd_count].revents = 0;
946 fd_count++;
947 property_set_fd_init = 1;
948 }
949 if (!signal_fd_init && get_signal_fd() > 0) {
950 ufds[fd_count].fd = get_signal_fd();
951 ufds[fd_count].events = POLLIN;
952 ufds[fd_count].revents = 0;
953 fd_count++;
954 signal_fd_init = 1;
955 }
956 if (!keychord_fd_init && get_keychord_fd() > 0) {
957 ufds[fd_count].fd = get_keychord_fd();
958 ufds[fd_count].events = POLLIN;
959 ufds[fd_count].revents = 0;
960 fd_count++;
961 keychord_fd_init = 1;
962 }
963
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700964 if (process_needs_restart) {
965 timeout = (process_needs_restart - gettime()) * 1000;
966 if (timeout < 0)
967 timeout = 0;
968 }
969
Colin Crossebd46132010-04-22 11:52:23 -0700970 if (!action_queue_empty() || cur_action)
Colin Crossebc6ff12010-04-13 19:52:01 -0700971 timeout = 0;
972
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700973#if BOOTCHART
974 if (bootchart_count > 0) {
975 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
976 timeout = BOOTCHART_POLLING_MS;
977 if (bootchart_step() < 0 || --bootchart_count == 0) {
978 bootchart_finish();
979 bootchart_count = 0;
980 }
981 }
982#endif
Colin Crossebc6ff12010-04-13 19:52:01 -0700983
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800984 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700985 if (nr <= 0)
986 continue;
987
Colin Crossebc6ff12010-04-13 19:52:01 -0700988 for (i = 0; i < fd_count; i++) {
989 if (ufds[i].revents == POLLIN) {
Colin Crossf83d0b92010-04-21 12:04:20 -0700990 if (ufds[i].fd == get_property_set_fd())
Colin Crossebc6ff12010-04-13 19:52:01 -0700991 handle_property_set_fd();
992 else if (ufds[i].fd == get_keychord_fd())
993 handle_keychord();
994 else if (ufds[i].fd == get_signal_fd())
995 handle_signal();
996 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700997 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700998 }
999
1000 return 0;
1001}