blob: 6127fd38e19488099dad9cc4e6ebc121567617c9 [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <ctype.h>
23#include <signal.h>
24#include <sys/wait.h>
25#include <sys/mount.h>
26#include <sys/stat.h>
27#include <sys/poll.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070028#include <errno.h>
29#include <stdarg.h>
30#include <mtd/mtd-user.h>
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <sys/un.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050034
35#ifdef HAVE_SELINUX
Stephen Smalleye46f9d52012-01-13 08:48:47 -050036#include <selinux/selinux.h>
37#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040038#include <selinux/android.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050039#endif
40
Colin Crossf83d0b92010-04-21 12:04:20 -070041#include <libgen.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070042
Dima Zavinda04c522011-09-01 17:09:44 -070043#include <cutils/list.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070044#include <cutils/sockets.h>
San Mehat4e221f02010-02-25 14:19:50 -080045#include <cutils/iosched_policy.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070046#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070047#include <termios.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070048
49#include <sys/system_properties.h>
50
51#include "devices.h"
52#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070053#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054#include "property_service.h"
The Android Open Source Project35237d12008-12-17 18:08:08 -080055#include "bootchart.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070056#include "signal_handler.h"
Colin Crossa8666952010-04-13 19:20:44 -070057#include "keychords.h"
Colin Cross6310a822010-04-20 14:29:05 -070058#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070059#include "util.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070060#include "ueventd.h"
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070061#include "watchdogd.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070062
Stephen Smalleye46f9d52012-01-13 08:48:47 -050063#ifdef HAVE_SELINUX
64struct selabel_handle *sehandle;
rpcraig63207cd2012-08-09 10:05:49 -040065struct selabel_handle *sehandle_prop;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050066#endif
67
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068static int property_triggers_enabled = 0;
69
70#if BOOTCHART
71static int bootchart_count;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072#endif
73
74static char console[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075static char bootmode[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076static char hardware[32];
77static unsigned revision = 0;
78static char qemu[32];
79
Stephen Smalleye46f9d52012-01-13 08:48:47 -050080#ifdef HAVE_SELINUX
81static int selinux_enabled = 1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050082#endif
83
Colin Crossebc6ff12010-04-13 19:52:01 -070084static struct action *cur_action = NULL;
85static struct command *cur_command = NULL;
86static struct listnode *command_queue = NULL;
87
Colin Cross9c5366b2010-04-13 19:48:59 -070088void notify_service_state(const char *name, const char *state)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070089{
90 char pname[PROP_NAME_MAX];
91 int len = strlen(name);
92 if ((len + 10) > PROP_NAME_MAX)
93 return;
94 snprintf(pname, sizeof(pname), "init.svc.%s", name);
95 property_set(pname, state);
96}
97
98static int have_console;
99static char *console_name = "/dev/console";
100static time_t process_needs_restart;
101
102static const char *ENV[32];
103
104/* add_environment - add "key=value" to the current environment */
105int add_environment(const char *key, const char *val)
106{
107 int n;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700108
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700109 for (n = 0; n < 31; n++) {
110 if (!ENV[n]) {
111 size_t len = strlen(key) + strlen(val) + 2;
112 char *entry = malloc(len);
113 snprintf(entry, len, "%s=%s", key, val);
114 ENV[n] = entry;
115 return 0;
116 }
117 }
118
119 return 1;
120}
121
122static void zap_stdio(void)
123{
124 int fd;
125 fd = open("/dev/null", O_RDWR);
126 dup2(fd, 0);
127 dup2(fd, 1);
128 dup2(fd, 2);
129 close(fd);
130}
131
132static void open_console()
133{
134 int fd;
135 if ((fd = open(console_name, O_RDWR)) < 0) {
136 fd = open("/dev/null", O_RDWR);
137 }
Colin Cross50fb5a62012-03-18 15:38:19 -0700138 ioctl(fd, TIOCSCTTY, 0);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700139 dup2(fd, 0);
140 dup2(fd, 1);
141 dup2(fd, 2);
142 close(fd);
143}
144
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700145static void publish_socket(const char *name, int fd)
146{
147 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
148 char val[64];
149
150 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
151 name,
152 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
153 snprintf(val, sizeof(val), "%d", fd);
154 add_environment(key, val);
155
156 /* make sure we don't close-on-exec */
157 fcntl(fd, F_SETFD, 0);
158}
159
San Mehatf24e2522009-05-19 13:30:46 -0700160void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700161{
162 struct stat s;
163 pid_t pid;
164 int needs_console;
165 int n;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500166#ifdef HAVE_SELINUX
167 char *scon = NULL;
168 int rc;
169#endif
Ken Sumrall752923c2010-12-03 16:33:31 -0800170 /* starting a service removes it from the disabled or reset
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700171 * state and immediately takes it out of the restarting
172 * state if it was in there
173 */
Ken Sumrall752923c2010-12-03 16:33:31 -0800174 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700175 svc->time_started = 0;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700176
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700177 /* running processes require no additional work -- if
178 * they're in the process of exiting, we've ensured
179 * that they will immediately restart on exit, unless
180 * they are ONESHOT
181 */
182 if (svc->flags & SVC_RUNNING) {
183 return;
184 }
185
186 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
187 if (needs_console && (!have_console)) {
188 ERROR("service '%s' requires console\n", svc->name);
189 svc->flags |= SVC_DISABLED;
190 return;
191 }
192
193 if (stat(svc->args[0], &s) != 0) {
194 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
195 svc->flags |= SVC_DISABLED;
196 return;
197 }
198
San Mehatf24e2522009-05-19 13:30:46 -0700199 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700200 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
201 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700202 svc->flags |= SVC_DISABLED;
203 return;
204 }
205
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500206#ifdef HAVE_SELINUX
207 if (is_selinux_enabled() > 0) {
208 char *mycon = NULL, *fcon = NULL;
209
210 INFO("computing context for service '%s'\n", svc->args[0]);
211 rc = getcon(&mycon);
212 if (rc < 0) {
213 ERROR("could not get context while starting '%s'\n", svc->name);
214 return;
215 }
216
217 rc = getfilecon(svc->args[0], &fcon);
218 if (rc < 0) {
219 ERROR("could not get context while starting '%s'\n", svc->name);
220 freecon(mycon);
221 return;
222 }
223
224 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon);
225 freecon(mycon);
226 freecon(fcon);
227 if (rc < 0) {
228 ERROR("could not get context while starting '%s'\n", svc->name);
229 return;
230 }
231 }
232#endif
233
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700234 NOTICE("starting '%s'\n", svc->name);
235
236 pid = fork();
237
238 if (pid == 0) {
239 struct socketinfo *si;
240 struct svcenvinfo *ei;
241 char tmp[32];
242 int fd, sz;
243
Nick Kralevich6ebf12f2012-03-26 09:09:11 -0700244 umask(077);
Colin Cross3294bbb2010-04-19 17:11:33 -0700245 if (properties_inited()) {
246 get_property_workspace(&fd, &sz);
247 sprintf(tmp, "%d,%d", dup(fd), sz);
248 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
249 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700250
251 for (ei = svc->envvars; ei; ei = ei->next)
252 add_environment(ei->name, ei->value);
253
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500254#ifdef HAVE_SELINUX
255 setsockcreatecon(scon);
256#endif
257
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700258 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400259 int socket_type = (
260 !strcmp(si->type, "stream") ? SOCK_STREAM :
261 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
262 int s = create_socket(si->name, socket_type,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700263 si->perm, si->uid, si->gid);
264 if (s >= 0) {
265 publish_socket(si->name, s);
266 }
267 }
268
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500269#ifdef HAVE_SELINUX
270 freecon(scon);
271 scon = NULL;
272 setsockcreatecon(NULL);
273#endif
274
San Mehat4e221f02010-02-25 14:19:50 -0800275 if (svc->ioprio_class != IoSchedClass_NONE) {
276 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
277 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
278 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
279 }
280 }
281
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700282 if (needs_console) {
283 setsid();
284 open_console();
285 } else {
286 zap_stdio();
287 }
288
289#if 0
290 for (n = 0; svc->args[n]; n++) {
291 INFO("args[%d] = '%s'\n", n, svc->args[n]);
292 }
293 for (n = 0; ENV[n]; n++) {
294 INFO("env[%d] = '%s'\n", n, ENV[n]);
295 }
296#endif
297
298 setpgid(0, getpid());
299
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800300 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800302 if (setgid(svc->gid) != 0) {
303 ERROR("setgid failed: %s\n", strerror(errno));
304 _exit(127);
305 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700306 }
307 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800308 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
309 ERROR("setgroups failed: %s\n", strerror(errno));
310 _exit(127);
311 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700312 }
313 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800314 if (setuid(svc->uid) != 0) {
315 ERROR("setuid failed: %s\n", strerror(errno));
316 _exit(127);
317 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700318 }
319
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500320#ifdef HAVE_SELINUX
321 if (svc->seclabel) {
322 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) {
323 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
324 _exit(127);
325 }
326 }
327#endif
328
San Mehat8ad15682009-05-20 08:50:40 -0700329 if (!dynamic_args) {
330 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
331 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
332 }
333 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700334 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700335 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700336 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700337 char *next = tmp;
338 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700339
340 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700341 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700342
San Mehatd4cdd132009-05-20 09:52:16 -0700343 while((bword = strsep(&next, " "))) {
344 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700345 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700346 break;
San Mehatf24e2522009-05-19 13:30:46 -0700347 }
348 arg_ptrs[arg_idx] = '\0';
349 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100350 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700351 _exit(127);
352 }
353
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500354#ifdef HAVE_SELINUX
355 freecon(scon);
356#endif
357
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700358 if (pid < 0) {
359 ERROR("failed to start '%s'\n", svc->name);
360 svc->pid = 0;
361 return;
362 }
363
364 svc->time_started = gettime();
365 svc->pid = pid;
366 svc->flags |= SVC_RUNNING;
367
Colin Cross3294bbb2010-04-19 17:11:33 -0700368 if (properties_inited())
369 notify_service_state(svc->name, "running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700370}
371
Ken Sumrall752923c2010-12-03 16:33:31 -0800372/* The how field should be either SVC_DISABLED or SVC_RESET */
373static void service_stop_or_reset(struct service *svc, int how)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700374{
375 /* we are no longer running, nor should we
376 * attempt to restart
377 */
378 svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING));
379
Ken Sumrall752923c2010-12-03 16:33:31 -0800380 if ((how != SVC_DISABLED) && (how != SVC_RESET)) {
381 /* Hrm, an illegal flag. Default to SVC_DISABLED */
382 how = SVC_DISABLED;
383 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700384 /* if the service has not yet started, prevent
385 * it from auto-starting with its class
386 */
Ken Sumralla2864802011-10-26 16:56:00 -0700387 if (how == SVC_RESET) {
388 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
389 } else {
390 svc->flags |= how;
391 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700392
393 if (svc->pid) {
394 NOTICE("service '%s' is being killed\n", svc->name);
Ken Sumrall752923c2010-12-03 16:33:31 -0800395 kill(-svc->pid, SIGKILL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700396 notify_service_state(svc->name, "stopping");
397 } else {
398 notify_service_state(svc->name, "stopped");
399 }
400}
401
Ken Sumrall752923c2010-12-03 16:33:31 -0800402void service_reset(struct service *svc)
403{
404 service_stop_or_reset(svc, SVC_RESET);
405}
406
407void service_stop(struct service *svc)
408{
409 service_stop_or_reset(svc, SVC_DISABLED);
410}
411
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700412void property_changed(const char *name, const char *value)
413{
Colin Crossebc6ff12010-04-13 19:52:01 -0700414 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700415 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700416}
417
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700418static void restart_service_if_needed(struct service *svc)
419{
420 time_t next_start_time = svc->time_started + 5;
421
422 if (next_start_time <= gettime()) {
423 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700424 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700425 return;
426 }
427
428 if ((next_start_time < process_needs_restart) ||
429 (process_needs_restart == 0)) {
430 process_needs_restart = next_start_time;
431 }
432}
433
434static void restart_processes()
435{
436 process_needs_restart = 0;
437 service_for_each_flags(SVC_RESTARTING,
438 restart_service_if_needed);
439}
440
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700441static void msg_start(const char *name)
442{
San Mehatf24e2522009-05-19 13:30:46 -0700443 struct service *svc;
444 char *tmp = NULL;
445 char *args = NULL;
446
447 if (!strchr(name, ':'))
448 svc = service_find_by_name(name);
449 else {
450 tmp = strdup(name);
San Mehatf24e2522009-05-19 13:30:46 -0700451 args = strchr(tmp, ':');
452 *args = '\0';
453 args++;
454
455 svc = service_find_by_name(tmp);
456 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700457
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700458 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700459 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700460 } else {
461 ERROR("no such service '%s'\n", name);
462 }
San Mehatf24e2522009-05-19 13:30:46 -0700463 if (tmp)
464 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700465}
466
467static void msg_stop(const char *name)
468{
469 struct service *svc = service_find_by_name(name);
470
471 if (svc) {
472 service_stop(svc);
473 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700474 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700475 }
476}
477
478void handle_control_message(const char *msg, const char *arg)
479{
480 if (!strcmp(msg,"start")) {
481 msg_start(arg);
482 } else if (!strcmp(msg,"stop")) {
483 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700484 } else if (!strcmp(msg,"restart")) {
485 msg_stop(arg);
486 msg_start(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700487 } else {
488 ERROR("unknown control msg '%s'\n", msg);
489 }
490}
491
Colin Crossebc6ff12010-04-13 19:52:01 -0700492static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700493{
494 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700495 node = list_head(&act->commands);
Dima Zavin3bea0792011-08-26 13:59:18 -0700496 if (!node || list_empty(&act->commands))
Colin Crossebc6ff12010-04-13 19:52:01 -0700497 return NULL;
498
499 return node_to_item(node, struct command, clist);
500}
501
502static struct command *get_next_command(struct action *act, struct command *cmd)
503{
504 struct listnode *node;
505 node = cmd->clist.next;
506 if (!node)
507 return NULL;
508 if (node == &act->commands)
509 return NULL;
510
511 return node_to_item(node, struct command, clist);
512}
513
514static int is_last_command(struct action *act, struct command *cmd)
515{
516 return (list_tail(&act->commands) == &cmd->clist);
517}
518
519void execute_one_command(void)
520{
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700521 int ret;
522
Colin Crossebc6ff12010-04-13 19:52:01 -0700523 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
524 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700525 cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -0700526 if (!cur_action)
527 return;
528 INFO("processing action %p (%s)\n", cur_action, cur_action->name);
529 cur_command = get_first_command(cur_action);
530 } else {
531 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700532 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700533
534 if (!cur_command)
535 return;
536
537 ret = cur_command->func(cur_command->nargs, cur_command->args);
538 INFO("command '%s' r=%d\n", cur_command->args[0], ret);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700539}
540
Colin Crossf83d0b92010-04-21 12:04:20 -0700541static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700542{
Colin Crossf83d0b92010-04-21 12:04:20 -0700543 int ret;
544 INFO("wait for %s\n", coldboot_done);
545 ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT);
546 if (ret)
547 ERROR("Timed out waiting for %s\n", coldboot_done);
548 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700549}
550
Colin Crossebc6ff12010-04-13 19:52:01 -0700551static int keychord_init_action(int nargs, char **args)
552{
553 keychord_init();
554 return 0;
555}
556
557static int console_init_action(int nargs, char **args)
558{
559 int fd;
560 char tmp[PROP_VALUE_MAX];
561
562 if (console[0]) {
563 snprintf(tmp, sizeof(tmp), "/dev/%s", console);
564 console_name = strdup(tmp);
565 }
566
567 fd = open(console_name, O_RDWR);
568 if (fd >= 0)
569 have_console = 1;
570 close(fd);
571
572 if( load_565rle_image(INIT_IMAGE_FILE) ) {
573 fd = open("/dev/tty0", O_WRONLY);
574 if (fd >= 0) {
575 const char *msg;
576 msg = "\n"
577 "\n"
578 "\n"
579 "\n"
580 "\n"
581 "\n"
582 "\n" // console is 40 cols x 30 lines
583 "\n"
584 "\n"
585 "\n"
586 "\n"
587 "\n"
588 "\n"
589 "\n"
590 " A N D R O I D ";
591 write(fd, msg, strlen(msg));
592 close(fd);
593 }
594 }
595 return 0;
596}
597
Dima Zavin5511c842011-12-19 11:21:32 -0800598static void import_kernel_nv(char *name, int for_emulator)
599{
600 char *value = strchr(name, '=');
601 int name_len = strlen(name);
602
603 if (value == 0) return;
604 *value++ = 0;
605 if (name_len == 0) return;
606
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500607#ifdef HAVE_SELINUX
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400608 if (!strcmp(name,"selinux")) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500609 selinux_enabled = atoi(value);
610 }
611#endif
612
Dima Zavin5511c842011-12-19 11:21:32 -0800613 if (for_emulator) {
614 /* in the emulator, export any kernel option with the
615 * ro.kernel. prefix */
616 char buff[PROP_NAME_MAX];
617 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
618
619 if (len < (int)sizeof(buff))
620 property_set( buff, value );
621 return;
622 }
623
624 if (!strcmp(name,"qemu")) {
625 strlcpy(qemu, value, sizeof(qemu));
626 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
627 const char *boot_prop_name = name + 12;
628 char prop[PROP_NAME_MAX];
629 int cnt;
630
631 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
632 if (cnt < PROP_NAME_MAX)
633 property_set(prop, value);
634 }
635}
636
637static void export_kernel_boot_props(void)
Colin Crossebc6ff12010-04-13 19:52:01 -0700638{
639 char tmp[PROP_VALUE_MAX];
Dima Zavin5511c842011-12-19 11:21:32 -0800640 const char *pval;
641 unsigned i;
642 struct {
643 const char *src_prop;
644 const char *dest_prop;
645 const char *def_val;
646 } prop_map[] = {
647 { "ro.boot.serialno", "ro.serialno", "", },
648 { "ro.boot.mode", "ro.bootmode", "unknown", },
649 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800650 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
651 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700652
Dima Zavin5511c842011-12-19 11:21:32 -0800653 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
654 pval = property_get(prop_map[i].src_prop);
655 property_set(prop_map[i].dest_prop, pval ?: prop_map[i].def_val);
656 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700657
Dima Zavin5511c842011-12-19 11:21:32 -0800658 pval = property_get("ro.boot.console");
659 if (pval)
660 strlcpy(console, pval, sizeof(console));
661
662 /* save a copy for init's usage during boot */
663 strlcpy(bootmode, property_get("ro.bootmode"), sizeof(bootmode));
664
665 /* if this was given on kernel command line, override what we read
666 * before (e.g. from /proc/cpuinfo), if anything */
667 pval = property_get("ro.boot.hardware");
668 if (pval)
669 strlcpy(hardware, pval, sizeof(hardware));
670 property_set("ro.hardware", hardware);
671
672 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
673 property_set("ro.revision", tmp);
674
675 /* TODO: these are obsolete. We should delete them */
Colin Crossebc6ff12010-04-13 19:52:01 -0700676 if (!strcmp(bootmode,"factory"))
677 property_set("ro.factorytest", "1");
678 else if (!strcmp(bootmode,"factory2"))
679 property_set("ro.factorytest", "2");
680 else
681 property_set("ro.factorytest", "0");
Dima Zavin5511c842011-12-19 11:21:32 -0800682}
Colin Crossebc6ff12010-04-13 19:52:01 -0700683
Dima Zavin5511c842011-12-19 11:21:32 -0800684static void process_kernel_cmdline(void)
685{
686 /* don't expose the raw commandline to nonpriv processes */
687 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700688
Dima Zavin5511c842011-12-19 11:21:32 -0800689 /* first pass does the common stuff, and finds if we are in qemu.
690 * second pass is only necessary for qemu to export all kernel params
691 * as props.
692 */
693 import_kernel_cmdline(0, import_kernel_nv);
694 if (qemu[0])
695 import_kernel_cmdline(1, import_kernel_nv);
696
697 /* now propogate the info given on command line to internal variables
698 * used by init as well as the current required properties
699 */
700 export_kernel_boot_props();
Colin Crossebc6ff12010-04-13 19:52:01 -0700701}
702
703static int property_service_init_action(int nargs, char **args)
704{
705 /* read any property files on system or data and
706 * fire up the property service. This must happen
707 * after the ro.foo properties are set above so
708 * that /data/local.prop cannot interfere with them.
709 */
710 start_property_service();
711 return 0;
712}
713
714static int signal_init_action(int nargs, char **args)
715{
716 signal_init();
717 return 0;
718}
719
720static int check_startup_action(int nargs, char **args)
721{
722 /* make sure we actually have all the pieces we need */
Colin Crossf83d0b92010-04-21 12:04:20 -0700723 if ((get_property_set_fd() < 0) ||
Colin Crossebc6ff12010-04-13 19:52:01 -0700724 (get_signal_fd() < 0)) {
725 ERROR("init startup failure\n");
726 exit(1);
727 }
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700728
729 /* signal that we hit this point */
730 unlink("/dev/.booting");
731
Colin Crossebc6ff12010-04-13 19:52:01 -0700732 return 0;
733}
734
735static int queue_property_triggers_action(int nargs, char **args)
736{
737 queue_all_property_triggers();
738 /* enable property triggers */
739 property_triggers_enabled = 1;
740 return 0;
741}
742
743#if BOOTCHART
744static int bootchart_init_action(int nargs, char **args)
745{
746 bootchart_count = bootchart_init();
747 if (bootchart_count < 0) {
748 ERROR("bootcharting init failure\n");
749 } else if (bootchart_count > 0) {
750 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
751 } else {
752 NOTICE("bootcharting ignored\n");
753 }
Carl-Emil Lagerstedt9ab81902011-01-14 09:35:30 +0100754
755 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700756}
757#endif
758
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500759#ifdef HAVE_SELINUX
rpcraig63207cd2012-08-09 10:05:49 -0400760static const struct selinux_opt seopts_prop[] = {
761 { SELABEL_OPT_PATH, "/data/system/property_contexts" },
762 { SELABEL_OPT_PATH, "/property_contexts" },
763 { 0, NULL }
764};
765
766struct selabel_handle* selinux_android_prop_context_handle(void)
767{
768 int i = 0;
769 struct selabel_handle* sehandle = NULL;
770 while ((sehandle == NULL) && seopts_prop[i].value) {
771 sehandle = selabel_open(SELABEL_CTX_ANDROID_PROP, &seopts_prop[i], 1);
772 i++;
773 }
774
775 if (!sehandle) {
776 ERROR("SELinux: Could not load property_contexts: %s\n",
777 strerror(errno));
778 return NULL;
779 }
780 INFO("SELinux: Loaded property contexts from %s\n", seopts_prop[i - 1].value);
781 return sehandle;
782}
783
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400784void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500785{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400786 sehandle = selinux_android_file_context_handle();
rpcraig63207cd2012-08-09 10:05:49 -0400787 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400788}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500789
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400790int selinux_reload_policy(void)
791{
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500792 if (!selinux_enabled) {
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400793 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500794 }
795
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400796 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500797
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400798 if (selinux_android_reload_policy() == -1) {
799 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500800 }
801
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400802 if (sehandle)
803 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500804
rpcraig63207cd2012-08-09 10:05:49 -0400805 if (sehandle_prop)
806 selabel_close(sehandle_prop);
807
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400808 selinux_init_all_handles();
809 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500810}
rpcraig63207cd2012-08-09 10:05:49 -0400811
812int audit_callback(void *data, security_class_t cls, char *buf, size_t len)
813{
814 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
815 return 0;
816}
817
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500818#endif
819
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700820int main(int argc, char **argv)
821{
Colin Crossebc6ff12010-04-13 19:52:01 -0700822 int fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700823 struct pollfd ufds[4];
824 char *tmpdev;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800825 char* debuggable;
Colin Crossebc6ff12010-04-13 19:52:01 -0700826 char tmp[32];
Colin Crossebc6ff12010-04-13 19:52:01 -0700827 int property_set_fd_init = 0;
828 int signal_fd_init = 0;
829 int keychord_fd_init = 0;
Dima Zavind7634c92011-12-16 14:18:06 -0800830 bool is_charger = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700831
Colin Crossf83d0b92010-04-21 12:04:20 -0700832 if (!strcmp(basename(argv[0]), "ueventd"))
833 return ueventd_main(argc, argv);
834
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700835 if (!strcmp(basename(argv[0]), "watchdogd"))
836 return watchdogd_main(argc, argv);
837
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700838 /* clear the umask */
839 umask(0);
840
841 /* Get the basic filesystem setup we need put
842 * together in the initramdisk on / and then we'll
843 * let the rc file figure out the rest.
844 */
845 mkdir("/dev", 0755);
846 mkdir("/proc", 0755);
847 mkdir("/sys", 0755);
848
Nick Kralevich150f19e2010-06-22 16:35:43 -0700849 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700850 mkdir("/dev/pts", 0755);
851 mkdir("/dev/socket", 0755);
852 mount("devpts", "/dev/pts", "devpts", 0, NULL);
853 mount("proc", "/proc", "proc", 0, NULL);
854 mount("sysfs", "/sys", "sysfs", 0, NULL);
855
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700856 /* indicate that booting is in progress to background fw loaders, etc */
857 close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000));
858
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700859 /* We must have some place other than / to create the
860 * device nodes for kmsg and null, otherwise we won't
861 * be able to remount / read-only later on.
862 * Now that tmpfs is mounted on /dev, we can actually
863 * talk to the outside world.
864 */
865 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -0700866 klog_init();
Dima Zavin5511c842011-12-19 11:21:32 -0800867 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700868
Colin Crossf83d0b92010-04-21 12:04:20 -0700869 get_hardware_name(hardware, &revision);
Dima Zavind7634c92011-12-16 14:18:06 -0800870
Dima Zavin5511c842011-12-19 11:21:32 -0800871 process_kernel_cmdline();
872
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500873#ifdef HAVE_SELINUX
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#endif
899
Dima Zavind7634c92011-12-16 14:18:06 -0800900 is_charger = !strcmp(bootmode, "charger");
901
902 INFO("property init\n");
Dima Zavin5511c842011-12-19 11:21:32 -0800903 if (!is_charger)
904 property_load_boot_defaults();
Dima Zavind7634c92011-12-16 14:18:06 -0800905
906 INFO("reading config file\n");
907 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700908
909 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700910
Colin Crossf83d0b92010-04-21 12:04:20 -0700911 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Colin Crossebc6ff12010-04-13 19:52:01 -0700912 queue_builtin_action(keychord_init_action, "keychord_init");
913 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700914
Dima Zavinca47cef2011-08-24 15:28:23 -0700915 /* execute all the boot actions to get us started */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700916 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -0700917
918 /* skip mounting filesystems in charger mode */
Dima Zavind7634c92011-12-16 14:18:06 -0800919 if (!is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -0700920 action_for_each_trigger("early-fs", action_add_queue_tail);
921 action_for_each_trigger("fs", action_add_queue_tail);
922 action_for_each_trigger("post-fs", action_add_queue_tail);
923 action_for_each_trigger("post-fs-data", action_add_queue_tail);
924 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700925
Colin Crossebc6ff12010-04-13 19:52:01 -0700926 queue_builtin_action(property_service_init_action, "property_service_init");
927 queue_builtin_action(signal_init_action, "signal_init");
928 queue_builtin_action(check_startup_action, "check_startup");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700929
Dima Zavind7634c92011-12-16 14:18:06 -0800930 if (is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -0700931 action_for_each_trigger("charger", action_add_queue_tail);
932 } else {
933 action_for_each_trigger("early-boot", action_add_queue_tail);
934 action_for_each_trigger("boot", action_add_queue_tail);
935 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700936
937 /* run all property triggers based on current state of the properties */
Chris Dearman469b7b22012-03-01 15:29:20 -0800938 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700939
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700940
941#if BOOTCHART
Colin Crossebc6ff12010-04-13 19:52:01 -0700942 queue_builtin_action(bootchart_init_action, "bootchart_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700943#endif
944
945 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800946 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700947
Colin Crossebc6ff12010-04-13 19:52:01 -0700948 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700949 restart_processes();
950
Colin Crossebc6ff12010-04-13 19:52:01 -0700951 if (!property_set_fd_init && get_property_set_fd() > 0) {
952 ufds[fd_count].fd = get_property_set_fd();
953 ufds[fd_count].events = POLLIN;
954 ufds[fd_count].revents = 0;
955 fd_count++;
956 property_set_fd_init = 1;
957 }
958 if (!signal_fd_init && get_signal_fd() > 0) {
959 ufds[fd_count].fd = get_signal_fd();
960 ufds[fd_count].events = POLLIN;
961 ufds[fd_count].revents = 0;
962 fd_count++;
963 signal_fd_init = 1;
964 }
965 if (!keychord_fd_init && get_keychord_fd() > 0) {
966 ufds[fd_count].fd = get_keychord_fd();
967 ufds[fd_count].events = POLLIN;
968 ufds[fd_count].revents = 0;
969 fd_count++;
970 keychord_fd_init = 1;
971 }
972
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700973 if (process_needs_restart) {
974 timeout = (process_needs_restart - gettime()) * 1000;
975 if (timeout < 0)
976 timeout = 0;
977 }
978
Colin Crossebd46132010-04-22 11:52:23 -0700979 if (!action_queue_empty() || cur_action)
Colin Crossebc6ff12010-04-13 19:52:01 -0700980 timeout = 0;
981
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700982#if BOOTCHART
983 if (bootchart_count > 0) {
984 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
985 timeout = BOOTCHART_POLLING_MS;
986 if (bootchart_step() < 0 || --bootchart_count == 0) {
987 bootchart_finish();
988 bootchart_count = 0;
989 }
990 }
991#endif
Colin Crossebc6ff12010-04-13 19:52:01 -0700992
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800993 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700994 if (nr <= 0)
995 continue;
996
Colin Crossebc6ff12010-04-13 19:52:01 -0700997 for (i = 0; i < fd_count; i++) {
998 if (ufds[i].revents == POLLIN) {
Colin Crossf83d0b92010-04-21 12:04:20 -0700999 if (ufds[i].fd == get_property_set_fd())
Colin Crossebc6ff12010-04-13 19:52:01 -07001000 handle_property_set_fd();
1001 else if (ufds[i].fd == get_keychord_fd())
1002 handle_keychord();
1003 else if (ufds[i].fd == get_signal_fd())
1004 handle_signal();
1005 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001006 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001007 }
1008
1009 return 0;
1010}