blob: 74c0308bb1d47ab36fac5aadc1601d22b19125f2 [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <ctype.h>
23#include <signal.h>
24#include <sys/wait.h>
25#include <sys/mount.h>
26#include <sys/stat.h>
27#include <sys/poll.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070028#include <errno.h>
29#include <stdarg.h>
30#include <mtd/mtd-user.h>
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <sys/un.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050034
Stephen Smalleye46f9d52012-01-13 08:48:47 -050035#include <selinux/selinux.h>
36#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040037#include <selinux/android.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050038
Colin Crossf83d0b92010-04-21 12:04:20 -070039#include <libgen.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070040
Dima Zavinda04c522011-09-01 17:09:44 -070041#include <cutils/list.h>
Nick Kralevich56fa0ac2013-06-24 17:41:40 -070042#include <cutils/android_reboot.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>
Alex Klyubin0d872d82013-08-16 13:19:24 -070045#include <cutils/fs.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
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070049#include "devices.h"
50#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070051#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070052#include "property_service.h"
The Android Open Source Project35237d12008-12-17 18:08:08 -080053#include "bootchart.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070054#include "signal_handler.h"
Colin Crossa8666952010-04-13 19:20:44 -070055#include "keychords.h"
Colin Cross6310a822010-04-20 14:29:05 -070056#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070057#include "util.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070058#include "ueventd.h"
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070059#include "watchdogd.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070060
Stephen Smalleye46f9d52012-01-13 08:48:47 -050061struct selabel_handle *sehandle;
rpcraig63207cd2012-08-09 10:05:49 -040062struct selabel_handle *sehandle_prop;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050063
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070064static int property_triggers_enabled = 0;
65
66#if BOOTCHART
67static int bootchart_count;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068#endif
69
70static char console[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070071static char bootmode[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072static char hardware[32];
73static unsigned revision = 0;
74static char qemu[32];
75
Colin Crossebc6ff12010-04-13 19:52:01 -070076static struct action *cur_action = NULL;
77static struct command *cur_command = NULL;
78static struct listnode *command_queue = NULL;
79
Colin Cross9c5366b2010-04-13 19:48:59 -070080void notify_service_state(const char *name, const char *state)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070081{
82 char pname[PROP_NAME_MAX];
83 int len = strlen(name);
84 if ((len + 10) > PROP_NAME_MAX)
85 return;
86 snprintf(pname, sizeof(pname), "init.svc.%s", name);
87 property_set(pname, state);
88}
89
90static int have_console;
Hong-Mei Li11467182013-04-01 11:17:51 +080091static char console_name[PROP_VALUE_MAX] = "/dev/console";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070092static time_t process_needs_restart;
93
94static const char *ENV[32];
95
96/* add_environment - add "key=value" to the current environment */
97int add_environment(const char *key, const char *val)
98{
James Morrissey381341f2014-05-16 11:36:36 +010099 size_t n;
100 size_t key_len = strlen(key);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700101
James Morrissey381341f2014-05-16 11:36:36 +0100102 /* The last environment entry is reserved to terminate the list */
103 for (n = 0; n < (ARRAY_SIZE(ENV) - 1); n++) {
104
105 /* Delete any existing entry for this key */
106 if (ENV[n] != NULL) {
107 size_t entry_key_len = strcspn(ENV[n], "=");
108 if ((entry_key_len == key_len) && (strncmp(ENV[n], key, entry_key_len) == 0)) {
109 free((char*)ENV[n]);
110 ENV[n] = NULL;
111 }
112 }
113
114 /* Add entry if a free slot is available */
115 if (ENV[n] == NULL) {
116 size_t len = key_len + strlen(val) + 2;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700117 char *entry = malloc(len);
118 snprintf(entry, len, "%s=%s", key, val);
119 ENV[n] = entry;
120 return 0;
121 }
122 }
123
James Morrissey381341f2014-05-16 11:36:36 +0100124 ERROR("No env. room to store: '%s':'%s'\n", key, val);
125
126 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700127}
128
129static void zap_stdio(void)
130{
131 int fd;
132 fd = open("/dev/null", O_RDWR);
133 dup2(fd, 0);
134 dup2(fd, 1);
135 dup2(fd, 2);
136 close(fd);
137}
138
139static void open_console()
140{
141 int fd;
142 if ((fd = open(console_name, O_RDWR)) < 0) {
143 fd = open("/dev/null", O_RDWR);
144 }
Colin Cross50fb5a62012-03-18 15:38:19 -0700145 ioctl(fd, TIOCSCTTY, 0);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700146 dup2(fd, 0);
147 dup2(fd, 1);
148 dup2(fd, 2);
149 close(fd);
150}
151
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700152static void publish_socket(const char *name, int fd)
153{
154 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
155 char val[64];
156
157 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
158 name,
159 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
160 snprintf(val, sizeof(val), "%d", fd);
161 add_environment(key, val);
162
163 /* make sure we don't close-on-exec */
164 fcntl(fd, F_SETFD, 0);
165}
166
San Mehatf24e2522009-05-19 13:30:46 -0700167void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700168{
169 struct stat s;
170 pid_t pid;
171 int needs_console;
172 int n;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500173 char *scon = NULL;
174 int rc;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700175
Ken Sumrall752923c2010-12-03 16:33:31 -0800176 /* starting a service removes it from the disabled or reset
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700177 * state and immediately takes it out of the restarting
178 * state if it was in there
179 */
JP Abgrall3beec7e2014-05-02 21:14:29 -0700180 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700181 svc->time_started = 0;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700182
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700183 /* running processes require no additional work -- if
184 * they're in the process of exiting, we've ensured
185 * that they will immediately restart on exit, unless
186 * they are ONESHOT
187 */
188 if (svc->flags & SVC_RUNNING) {
189 return;
190 }
191
192 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
193 if (needs_console && (!have_console)) {
194 ERROR("service '%s' requires console\n", svc->name);
195 svc->flags |= SVC_DISABLED;
196 return;
197 }
198
199 if (stat(svc->args[0], &s) != 0) {
200 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
201 svc->flags |= SVC_DISABLED;
202 return;
203 }
204
San Mehatf24e2522009-05-19 13:30:46 -0700205 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700206 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
207 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700208 svc->flags |= SVC_DISABLED;
209 return;
210 }
211
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500212 if (is_selinux_enabled() > 0) {
Stephen Smalley30f30332012-11-16 14:34:27 -0500213 if (svc->seclabel) {
214 scon = strdup(svc->seclabel);
215 if (!scon) {
216 ERROR("Out of memory while starting '%s'\n", svc->name);
217 return;
218 }
219 } else {
220 char *mycon = NULL, *fcon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500221
Stephen Smalley30f30332012-11-16 14:34:27 -0500222 INFO("computing context for service '%s'\n", svc->args[0]);
223 rc = getcon(&mycon);
224 if (rc < 0) {
225 ERROR("could not get context while starting '%s'\n", svc->name);
226 return;
227 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500228
Stephen Smalley30f30332012-11-16 14:34:27 -0500229 rc = getfilecon(svc->args[0], &fcon);
230 if (rc < 0) {
231 ERROR("could not get context while starting '%s'\n", svc->name);
232 freecon(mycon);
233 return;
234 }
235
236 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon);
Stephen Smalleyaf06c672013-12-09 15:40:24 -0500237 if (rc == 0 && !strcmp(scon, mycon)) {
238 ERROR("Warning! Service %s needs a SELinux domain defined; please fix!\n", svc->name);
239 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500240 freecon(mycon);
Stephen Smalley30f30332012-11-16 14:34:27 -0500241 freecon(fcon);
242 if (rc < 0) {
243 ERROR("could not get context while starting '%s'\n", svc->name);
244 return;
245 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500246 }
247 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500248
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700249 NOTICE("starting '%s'\n", svc->name);
250
251 pid = fork();
252
253 if (pid == 0) {
254 struct socketinfo *si;
255 struct svcenvinfo *ei;
256 char tmp[32];
257 int fd, sz;
258
Nick Kralevich6ebf12f2012-03-26 09:09:11 -0700259 umask(077);
Colin Cross3294bbb2010-04-19 17:11:33 -0700260 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
269 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400270 int socket_type = (
271 !strcmp(si->type, "stream") ? SOCK_STREAM :
272 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
273 int s = create_socket(si->name, socket_type,
Stephen Smalley8348d272013-05-13 12:37:04 -0400274 si->perm, si->uid, si->gid, si->socketcon ?: scon);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700275 if (s >= 0) {
276 publish_socket(si->name, s);
277 }
278 }
279
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500280 freecon(scon);
281 scon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500282
San Mehat4e221f02010-02-25 14:19:50 -0800283 if (svc->ioprio_class != IoSchedClass_NONE) {
284 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
285 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
286 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
287 }
288 }
289
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700290 if (needs_console) {
291 setsid();
292 open_console();
293 } else {
294 zap_stdio();
295 }
296
297#if 0
298 for (n = 0; svc->args[n]; n++) {
299 INFO("args[%d] = '%s'\n", n, svc->args[n]);
300 }
301 for (n = 0; ENV[n]; n++) {
302 INFO("env[%d] = '%s'\n", n, ENV[n]);
303 }
304#endif
305
306 setpgid(0, getpid());
307
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800308 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700309 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800310 if (setgid(svc->gid) != 0) {
311 ERROR("setgid failed: %s\n", strerror(errno));
312 _exit(127);
313 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700314 }
315 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800316 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
317 ERROR("setgroups failed: %s\n", strerror(errno));
318 _exit(127);
319 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700320 }
321 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800322 if (setuid(svc->uid) != 0) {
323 ERROR("setuid failed: %s\n", strerror(errno));
324 _exit(127);
325 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700326 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500327 if (svc->seclabel) {
328 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) {
329 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
330 _exit(127);
331 }
332 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500333
San Mehat8ad15682009-05-20 08:50:40 -0700334 if (!dynamic_args) {
335 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
336 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
337 }
338 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700339 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700340 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700341 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700342 char *next = tmp;
343 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700344
345 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700346 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700347
San Mehatd4cdd132009-05-20 09:52:16 -0700348 while((bword = strsep(&next, " "))) {
349 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700350 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700351 break;
San Mehatf24e2522009-05-19 13:30:46 -0700352 }
353 arg_ptrs[arg_idx] = '\0';
354 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100355 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700356 _exit(127);
357 }
358
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500359 freecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500360
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700361 if (pid < 0) {
362 ERROR("failed to start '%s'\n", svc->name);
363 svc->pid = 0;
364 return;
365 }
366
367 svc->time_started = gettime();
368 svc->pid = pid;
369 svc->flags |= SVC_RUNNING;
370
Colin Cross3294bbb2010-04-19 17:11:33 -0700371 if (properties_inited())
372 notify_service_state(svc->name, "running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700373}
374
Mike Kasickb54f39f2012-01-25 23:48:46 -0500375/* The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART */
Ken Sumrall752923c2010-12-03 16:33:31 -0800376static void service_stop_or_reset(struct service *svc, int how)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700377{
Mike Kasick7e36edd2012-02-06 10:32:13 -0500378 /* The service is still SVC_RUNNING until its process exits, but if it has
379 * already exited it shoudn't attempt a restart yet. */
JP Abgrall3beec7e2014-05-02 21:14:29 -0700380 svc->flags &= ~(SVC_RESTARTING | SVC_DISABLED_START);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700381
Mike Kasickb54f39f2012-01-25 23:48:46 -0500382 if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) {
Ken Sumrall752923c2010-12-03 16:33:31 -0800383 /* Hrm, an illegal flag. Default to SVC_DISABLED */
384 how = SVC_DISABLED;
385 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700386 /* if the service has not yet started, prevent
387 * it from auto-starting with its class
388 */
Ken Sumralla2864802011-10-26 16:56:00 -0700389 if (how == SVC_RESET) {
390 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
391 } else {
392 svc->flags |= how;
393 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700394
395 if (svc->pid) {
396 NOTICE("service '%s' is being killed\n", svc->name);
Ken Sumrall752923c2010-12-03 16:33:31 -0800397 kill(-svc->pid, SIGKILL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700398 notify_service_state(svc->name, "stopping");
399 } else {
400 notify_service_state(svc->name, "stopped");
401 }
402}
403
Ken Sumrall752923c2010-12-03 16:33:31 -0800404void service_reset(struct service *svc)
405{
406 service_stop_or_reset(svc, SVC_RESET);
407}
408
409void service_stop(struct service *svc)
410{
411 service_stop_or_reset(svc, SVC_DISABLED);
412}
413
Mike Kasickb54f39f2012-01-25 23:48:46 -0500414void service_restart(struct service *svc)
415{
416 if (svc->flags & SVC_RUNNING) {
417 /* Stop, wait, then start the service. */
418 service_stop_or_reset(svc, SVC_RESTART);
419 } else if (!(svc->flags & SVC_RESTARTING)) {
420 /* Just start the service since it's not running. */
421 service_start(svc, NULL);
422 } /* else: Service is restarting anyways. */
423}
424
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700425void property_changed(const char *name, const char *value)
426{
Colin Crossebc6ff12010-04-13 19:52:01 -0700427 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700428 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700429}
430
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700431static void restart_service_if_needed(struct service *svc)
432{
433 time_t next_start_time = svc->time_started + 5;
434
435 if (next_start_time <= gettime()) {
436 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700437 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700438 return;
439 }
440
441 if ((next_start_time < process_needs_restart) ||
442 (process_needs_restart == 0)) {
443 process_needs_restart = next_start_time;
444 }
445}
446
447static void restart_processes()
448{
449 process_needs_restart = 0;
450 service_for_each_flags(SVC_RESTARTING,
451 restart_service_if_needed);
452}
453
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700454static void msg_start(const char *name)
455{
Hong-Mei Li11467182013-04-01 11:17:51 +0800456 struct service *svc = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700457 char *tmp = NULL;
458 char *args = NULL;
459
460 if (!strchr(name, ':'))
461 svc = service_find_by_name(name);
462 else {
463 tmp = strdup(name);
Hong-Mei Li11467182013-04-01 11:17:51 +0800464 if (tmp) {
465 args = strchr(tmp, ':');
466 *args = '\0';
467 args++;
San Mehatf24e2522009-05-19 13:30:46 -0700468
Hong-Mei Li11467182013-04-01 11:17:51 +0800469 svc = service_find_by_name(tmp);
470 }
San Mehatf24e2522009-05-19 13:30:46 -0700471 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700472
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700473 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700474 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700475 } else {
476 ERROR("no such service '%s'\n", name);
477 }
San Mehatf24e2522009-05-19 13:30:46 -0700478 if (tmp)
479 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700480}
481
482static void msg_stop(const char *name)
483{
484 struct service *svc = service_find_by_name(name);
485
486 if (svc) {
487 service_stop(svc);
488 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700489 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700490 }
491}
492
Mike Kasickb54f39f2012-01-25 23:48:46 -0500493static void msg_restart(const char *name)
494{
495 struct service *svc = service_find_by_name(name);
496
497 if (svc) {
498 service_restart(svc);
499 } else {
500 ERROR("no such service '%s'\n", name);
501 }
502}
503
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700504void handle_control_message(const char *msg, const char *arg)
505{
506 if (!strcmp(msg,"start")) {
507 msg_start(arg);
508 } else if (!strcmp(msg,"stop")) {
509 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700510 } else if (!strcmp(msg,"restart")) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500511 msg_restart(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700512 } else {
513 ERROR("unknown control msg '%s'\n", msg);
514 }
515}
516
Colin Crossebc6ff12010-04-13 19:52:01 -0700517static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700518{
519 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700520 node = list_head(&act->commands);
Dima Zavin3bea0792011-08-26 13:59:18 -0700521 if (!node || list_empty(&act->commands))
Colin Crossebc6ff12010-04-13 19:52:01 -0700522 return NULL;
523
524 return node_to_item(node, struct command, clist);
525}
526
527static struct command *get_next_command(struct action *act, struct command *cmd)
528{
529 struct listnode *node;
530 node = cmd->clist.next;
531 if (!node)
532 return NULL;
533 if (node == &act->commands)
534 return NULL;
535
536 return node_to_item(node, struct command, clist);
537}
538
539static int is_last_command(struct action *act, struct command *cmd)
540{
541 return (list_tail(&act->commands) == &cmd->clist);
542}
543
544void execute_one_command(void)
545{
Riley Andrews24a3b782014-06-26 13:56:01 -0700546 int ret, i;
547 char cmd_str[256] = "";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700548
Colin Crossebc6ff12010-04-13 19:52:01 -0700549 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
550 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700551 cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -0700552 if (!cur_action)
553 return;
554 INFO("processing action %p (%s)\n", cur_action, cur_action->name);
555 cur_command = get_first_command(cur_action);
556 } else {
557 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700558 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700559
560 if (!cur_command)
561 return;
562
563 ret = cur_command->func(cur_command->nargs, cur_command->args);
Riley Andrews24a3b782014-06-26 13:56:01 -0700564 if (klog_get_level() >= KLOG_INFO_LEVEL) {
565 for (i = 0; i < cur_command->nargs; i++) {
566 strlcat(cmd_str, cur_command->args[i], sizeof(cmd_str));
567 if (i < cur_command->nargs - 1) {
568 strlcat(cmd_str, " ", sizeof(cmd_str));
569 }
570 }
571 INFO("command '%s' action=%s status=%d (%s:%d)\n",
572 cmd_str, cur_action ? cur_action->name : "", ret, cur_command->filename,
573 cur_command->line);
574 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700575}
576
Colin Crossf83d0b92010-04-21 12:04:20 -0700577static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700578{
Colin Crossf83d0b92010-04-21 12:04:20 -0700579 int ret;
580 INFO("wait for %s\n", coldboot_done);
581 ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT);
582 if (ret)
583 ERROR("Timed out waiting for %s\n", coldboot_done);
584 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700585}
586
Alex Klyubin0d872d82013-08-16 13:19:24 -0700587/*
588 * Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed
589 * by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom.
590 * Does nothing if Hardware RNG is not present.
591 *
592 * Since we don't yet trust the quality of Hardware RNG, these bytes are not
593 * mixed into the primary pool of Linux RNG and the entropy estimate is left
594 * unmodified.
595 *
596 * If the HW RNG device /dev/hw_random is present, we require that at least
597 * 512 bytes read from it are written into Linux RNG. QA is expected to catch
598 * devices/configurations where these I/O operations are blocking for a long
599 * time. We do not reboot or halt on failures, as this is a best-effort
600 * attempt.
601 */
602static int mix_hwrng_into_linux_rng_action(int nargs, char **args)
603{
604 int result = -1;
605 int hwrandom_fd = -1;
606 int urandom_fd = -1;
607 char buf[512];
608 ssize_t chunk_size;
609 size_t total_bytes_written = 0;
610
611 hwrandom_fd = TEMP_FAILURE_RETRY(
612 open("/dev/hw_random", O_RDONLY | O_NOFOLLOW));
613 if (hwrandom_fd == -1) {
614 if (errno == ENOENT) {
615 ERROR("/dev/hw_random not found\n");
616 /* It's not an error to not have a Hardware RNG. */
617 result = 0;
618 } else {
619 ERROR("Failed to open /dev/hw_random: %s\n", strerror(errno));
620 }
621 goto ret;
622 }
623
624 urandom_fd = TEMP_FAILURE_RETRY(
625 open("/dev/urandom", O_WRONLY | O_NOFOLLOW));
626 if (urandom_fd == -1) {
627 ERROR("Failed to open /dev/urandom: %s\n", strerror(errno));
628 goto ret;
629 }
630
631 while (total_bytes_written < sizeof(buf)) {
632 chunk_size = TEMP_FAILURE_RETRY(
633 read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written));
634 if (chunk_size == -1) {
635 ERROR("Failed to read from /dev/hw_random: %s\n", strerror(errno));
636 goto ret;
637 } else if (chunk_size == 0) {
638 ERROR("Failed to read from /dev/hw_random: EOF\n");
639 goto ret;
640 }
641
642 chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size));
643 if (chunk_size == -1) {
644 ERROR("Failed to write to /dev/urandom: %s\n", strerror(errno));
645 goto ret;
646 }
647 total_bytes_written += chunk_size;
648 }
649
Elliott Hughesccecf142014-01-16 10:53:11 -0800650 INFO("Mixed %zu bytes from /dev/hw_random into /dev/urandom",
Alex Klyubin0d872d82013-08-16 13:19:24 -0700651 total_bytes_written);
652 result = 0;
653
654ret:
655 if (hwrandom_fd != -1) {
656 close(hwrandom_fd);
657 }
658 if (urandom_fd != -1) {
659 close(urandom_fd);
660 }
661 memset(buf, 0, sizeof(buf));
662 return result;
663}
664
Colin Crossebc6ff12010-04-13 19:52:01 -0700665static int keychord_init_action(int nargs, char **args)
666{
667 keychord_init();
668 return 0;
669}
670
671static int console_init_action(int nargs, char **args)
672{
673 int fd;
Colin Crossebc6ff12010-04-13 19:52:01 -0700674
675 if (console[0]) {
Hong-Mei Li11467182013-04-01 11:17:51 +0800676 snprintf(console_name, sizeof(console_name), "/dev/%s", console);
Colin Crossebc6ff12010-04-13 19:52:01 -0700677 }
678
679 fd = open(console_name, O_RDWR);
680 if (fd >= 0)
681 have_console = 1;
682 close(fd);
683
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200684 fd = open("/dev/tty0", O_WRONLY);
685 if (fd >= 0) {
686 const char *msg;
687 msg = "\n"
688 "\n"
689 "\n"
690 "\n"
691 "\n"
692 "\n"
693 "\n" // console is 40 cols x 30 lines
694 "\n"
695 "\n"
696 "\n"
697 "\n"
698 "\n"
699 "\n"
700 "\n"
701 " A N D R O I D ";
702 write(fd, msg, strlen(msg));
703 close(fd);
Colin Crossebc6ff12010-04-13 19:52:01 -0700704 }
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200705
Colin Crossebc6ff12010-04-13 19:52:01 -0700706 return 0;
707}
708
Dima Zavin5511c842011-12-19 11:21:32 -0800709static void import_kernel_nv(char *name, int for_emulator)
710{
711 char *value = strchr(name, '=');
712 int name_len = strlen(name);
713
714 if (value == 0) return;
715 *value++ = 0;
716 if (name_len == 0) return;
717
718 if (for_emulator) {
719 /* in the emulator, export any kernel option with the
720 * ro.kernel. prefix */
721 char buff[PROP_NAME_MAX];
722 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
723
724 if (len < (int)sizeof(buff))
725 property_set( buff, value );
726 return;
727 }
728
729 if (!strcmp(name,"qemu")) {
730 strlcpy(qemu, value, sizeof(qemu));
731 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
732 const char *boot_prop_name = name + 12;
733 char prop[PROP_NAME_MAX];
734 int cnt;
735
736 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
737 if (cnt < PROP_NAME_MAX)
738 property_set(prop, value);
739 }
740}
741
742static void export_kernel_boot_props(void)
Colin Crossebc6ff12010-04-13 19:52:01 -0700743{
744 char tmp[PROP_VALUE_MAX];
Colin Cross1a6f4c32013-01-28 17:13:35 -0800745 int ret;
Dima Zavin5511c842011-12-19 11:21:32 -0800746 unsigned i;
747 struct {
748 const char *src_prop;
749 const char *dest_prop;
750 const char *def_val;
751 } prop_map[] = {
752 { "ro.boot.serialno", "ro.serialno", "", },
753 { "ro.boot.mode", "ro.bootmode", "unknown", },
754 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800755 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
756 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700757
Dima Zavin5511c842011-12-19 11:21:32 -0800758 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
Colin Cross1a6f4c32013-01-28 17:13:35 -0800759 ret = property_get(prop_map[i].src_prop, tmp);
Colin Cross5e484e92013-06-17 16:20:08 -0700760 if (ret > 0)
761 property_set(prop_map[i].dest_prop, tmp);
762 else
Colin Cross1a6f4c32013-01-28 17:13:35 -0800763 property_set(prop_map[i].dest_prop, prop_map[i].def_val);
Dima Zavin5511c842011-12-19 11:21:32 -0800764 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700765
Colin Cross1a6f4c32013-01-28 17:13:35 -0800766 ret = property_get("ro.boot.console", tmp);
767 if (ret)
768 strlcpy(console, tmp, sizeof(console));
Dima Zavin5511c842011-12-19 11:21:32 -0800769
770 /* save a copy for init's usage during boot */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800771 property_get("ro.bootmode", tmp);
772 strlcpy(bootmode, tmp, sizeof(bootmode));
Dima Zavin5511c842011-12-19 11:21:32 -0800773
774 /* if this was given on kernel command line, override what we read
775 * before (e.g. from /proc/cpuinfo), if anything */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800776 ret = property_get("ro.boot.hardware", tmp);
777 if (ret)
778 strlcpy(hardware, tmp, sizeof(hardware));
Dima Zavin5511c842011-12-19 11:21:32 -0800779 property_set("ro.hardware", hardware);
780
781 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
782 property_set("ro.revision", tmp);
783
784 /* TODO: these are obsolete. We should delete them */
Colin Crossebc6ff12010-04-13 19:52:01 -0700785 if (!strcmp(bootmode,"factory"))
786 property_set("ro.factorytest", "1");
787 else if (!strcmp(bootmode,"factory2"))
788 property_set("ro.factorytest", "2");
789 else
790 property_set("ro.factorytest", "0");
Dima Zavin5511c842011-12-19 11:21:32 -0800791}
Colin Crossebc6ff12010-04-13 19:52:01 -0700792
Dima Zavin5511c842011-12-19 11:21:32 -0800793static void process_kernel_cmdline(void)
794{
795 /* don't expose the raw commandline to nonpriv processes */
796 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700797
Dima Zavin5511c842011-12-19 11:21:32 -0800798 /* first pass does the common stuff, and finds if we are in qemu.
799 * second pass is only necessary for qemu to export all kernel params
800 * as props.
801 */
802 import_kernel_cmdline(0, import_kernel_nv);
803 if (qemu[0])
804 import_kernel_cmdline(1, import_kernel_nv);
805
806 /* now propogate the info given on command line to internal variables
807 * used by init as well as the current required properties
808 */
809 export_kernel_boot_props();
Colin Crossebc6ff12010-04-13 19:52:01 -0700810}
811
812static int property_service_init_action(int nargs, char **args)
813{
814 /* read any property files on system or data and
815 * fire up the property service. This must happen
816 * after the ro.foo properties are set above so
817 * that /data/local.prop cannot interfere with them.
818 */
819 start_property_service();
820 return 0;
821}
822
823static int signal_init_action(int nargs, char **args)
824{
825 signal_init();
826 return 0;
827}
828
829static int check_startup_action(int nargs, char **args)
830{
831 /* make sure we actually have all the pieces we need */
Colin Crossf83d0b92010-04-21 12:04:20 -0700832 if ((get_property_set_fd() < 0) ||
Colin Crossebc6ff12010-04-13 19:52:01 -0700833 (get_signal_fd() < 0)) {
834 ERROR("init startup failure\n");
835 exit(1);
836 }
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700837
838 /* signal that we hit this point */
839 unlink("/dev/.booting");
840
Colin Crossebc6ff12010-04-13 19:52:01 -0700841 return 0;
842}
843
844static int queue_property_triggers_action(int nargs, char **args)
845{
846 queue_all_property_triggers();
847 /* enable property triggers */
848 property_triggers_enabled = 1;
849 return 0;
850}
851
852#if BOOTCHART
853static int bootchart_init_action(int nargs, char **args)
854{
855 bootchart_count = bootchart_init();
856 if (bootchart_count < 0) {
857 ERROR("bootcharting init failure\n");
858 } else if (bootchart_count > 0) {
859 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
860 } else {
861 NOTICE("bootcharting ignored\n");
862 }
Carl-Emil Lagerstedt9ab81902011-01-14 09:35:30 +0100863
864 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700865}
866#endif
867
rpcraig63207cd2012-08-09 10:05:49 -0400868static const struct selinux_opt seopts_prop[] = {
rpcraig63207cd2012-08-09 10:05:49 -0400869 { SELABEL_OPT_PATH, "/property_contexts" },
Robert Craig03daf052014-03-17 21:16:53 -0400870 { SELABEL_OPT_PATH, "/data/security/current/property_contexts" },
rpcraig63207cd2012-08-09 10:05:49 -0400871 { 0, NULL }
872};
873
874struct selabel_handle* selinux_android_prop_context_handle(void)
875{
Robert Craig03daf052014-03-17 21:16:53 -0400876 int policy_index = selinux_android_use_data_policy() ? 1 : 0;
877 struct selabel_handle* sehandle = selabel_open(SELABEL_CTX_ANDROID_PROP,
878 &seopts_prop[policy_index], 1);
rpcraig63207cd2012-08-09 10:05:49 -0400879 if (!sehandle) {
880 ERROR("SELinux: Could not load property_contexts: %s\n",
881 strerror(errno));
882 return NULL;
883 }
Robert Craig03daf052014-03-17 21:16:53 -0400884 INFO("SELinux: Loaded property contexts from %s\n", seopts_prop[policy_index].value);
rpcraig63207cd2012-08-09 10:05:49 -0400885 return sehandle;
886}
887
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400888void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500889{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400890 sehandle = selinux_android_file_context_handle();
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500891 selinux_android_set_sehandle(sehandle);
rpcraig63207cd2012-08-09 10:05:49 -0400892 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400893}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500894
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700895static bool selinux_is_disabled(void)
896{
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700897#ifdef ALLOW_DISABLE_SELINUX
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700898 char tmp[PROP_VALUE_MAX];
899
900 if (access("/sys/fs/selinux", F_OK) != 0) {
901 /* SELinux is not compiled into the kernel, or has been disabled
902 * via the kernel command line "selinux=0".
903 */
904 return true;
905 }
906
907 if ((property_get("ro.boot.selinux", tmp) != 0) && (strcmp(tmp, "disabled") == 0)) {
908 /* SELinux is compiled into the kernel, but we've been told to disable it. */
909 return true;
910 }
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700911#endif
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700912
913 return false;
914}
915
916static bool selinux_is_enforcing(void)
917{
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700918#ifdef ALLOW_DISABLE_SELINUX
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700919 char tmp[PROP_VALUE_MAX];
920
921 if (property_get("ro.boot.selinux", tmp) == 0) {
922 /* Property is not set. Assume enforcing */
923 return true;
924 }
925
926 if (strcmp(tmp, "permissive") == 0) {
927 /* SELinux is in the kernel, but we've been told to go into permissive mode */
928 return false;
929 }
930
931 if (strcmp(tmp, "enforcing") != 0) {
932 ERROR("SELinux: Unknown value of ro.boot.selinux. Got: \"%s\". Assuming enforcing.\n", tmp);
933 }
934
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700935#endif
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700936 return true;
937}
938
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400939int selinux_reload_policy(void)
940{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700941 if (selinux_is_disabled()) {
942 return -1;
943 }
944
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400945 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500946
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400947 if (selinux_android_reload_policy() == -1) {
948 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500949 }
950
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400951 if (sehandle)
952 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500953
rpcraig63207cd2012-08-09 10:05:49 -0400954 if (sehandle_prop)
955 selabel_close(sehandle_prop);
956
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400957 selinux_init_all_handles();
958 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500959}
rpcraig63207cd2012-08-09 10:05:49 -0400960
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500961static int audit_callback(void *data, security_class_t cls __attribute__((unused)), char *buf, size_t len)
rpcraig63207cd2012-08-09 10:05:49 -0400962{
963 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
964 return 0;
965}
966
Stephen Smalley439224e2014-06-24 13:45:43 -0400967int log_callback(int type, const char *fmt, ...)
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500968{
969 int level;
970 va_list ap;
971 switch (type) {
972 case SELINUX_WARNING:
973 level = KLOG_WARNING_LEVEL;
974 break;
975 case SELINUX_INFO:
976 level = KLOG_INFO_LEVEL;
977 break;
978 default:
979 level = KLOG_ERROR_LEVEL;
980 break;
981 }
982 va_start(ap, fmt);
983 klog_vwrite(level, fmt, ap);
984 va_end(ap);
985 return 0;
986}
987
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700988static void selinux_initialize(void)
989{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700990 if (selinux_is_disabled()) {
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700991 return;
992 }
993
994 INFO("loading selinux policy\n");
995 if (selinux_android_load_policy() < 0) {
996 ERROR("SELinux: Failed to load policy; rebooting into recovery mode\n");
997 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
998 while (1) { pause(); } // never reached
999 }
1000
1001 selinux_init_all_handles();
Nick Kralevich935bd3e2013-06-26 15:37:26 -07001002 bool is_enforcing = selinux_is_enforcing();
1003 INFO("SELinux: security_setenforce(%d)\n", is_enforcing);
1004 security_setenforce(is_enforcing);
Nick Kralevich56fa0ac2013-06-24 17:41:40 -07001005}
1006
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001007int main(int argc, char **argv)
1008{
Colin Crossebc6ff12010-04-13 19:52:01 -07001009 int fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001010 struct pollfd ufds[4];
1011 char *tmpdev;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001012 char* debuggable;
Colin Crossebc6ff12010-04-13 19:52:01 -07001013 char tmp[32];
Colin Crossebc6ff12010-04-13 19:52:01 -07001014 int property_set_fd_init = 0;
1015 int signal_fd_init = 0;
1016 int keychord_fd_init = 0;
Dima Zavind7634c92011-12-16 14:18:06 -08001017 bool is_charger = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001018
Colin Crossf83d0b92010-04-21 12:04:20 -07001019 if (!strcmp(basename(argv[0]), "ueventd"))
1020 return ueventd_main(argc, argv);
1021
Arve Hjønnevågd97d9072012-06-13 21:51:56 -07001022 if (!strcmp(basename(argv[0]), "watchdogd"))
1023 return watchdogd_main(argc, argv);
1024
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001025 /* clear the umask */
1026 umask(0);
1027
1028 /* Get the basic filesystem setup we need put
1029 * together in the initramdisk on / and then we'll
1030 * let the rc file figure out the rest.
1031 */
1032 mkdir("/dev", 0755);
1033 mkdir("/proc", 0755);
1034 mkdir("/sys", 0755);
1035
Nick Kralevich150f19e2010-06-22 16:35:43 -07001036 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001037 mkdir("/dev/pts", 0755);
1038 mkdir("/dev/socket", 0755);
1039 mount("devpts", "/dev/pts", "devpts", 0, NULL);
1040 mount("proc", "/proc", "proc", 0, NULL);
1041 mount("sysfs", "/sys", "sysfs", 0, NULL);
1042
Brian Swetland8d48c8e2011-03-24 15:45:30 -07001043 /* indicate that booting is in progress to background fw loaders, etc */
1044 close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000));
1045
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001046 /* We must have some place other than / to create the
1047 * device nodes for kmsg and null, otherwise we won't
1048 * be able to remount / read-only later on.
1049 * Now that tmpfs is mounted on /dev, we can actually
1050 * talk to the outside world.
1051 */
1052 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -07001053 klog_init();
Dima Zavin5511c842011-12-19 11:21:32 -08001054 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -07001055
Colin Crossf83d0b92010-04-21 12:04:20 -07001056 get_hardware_name(hardware, &revision);
Dima Zavind7634c92011-12-16 14:18:06 -08001057
Dima Zavin5511c842011-12-19 11:21:32 -08001058 process_kernel_cmdline();
1059
rpcraig63207cd2012-08-09 10:05:49 -04001060 union selinux_callback cb;
Stephen Smalleyeb3f4212014-02-12 16:17:00 -05001061 cb.func_log = log_callback;
rpcraig63207cd2012-08-09 10:05:49 -04001062 selinux_set_callback(SELINUX_CB_LOG, cb);
1063
1064 cb.func_audit = audit_callback;
1065 selinux_set_callback(SELINUX_CB_AUDIT, cb);
1066
Nick Kralevich56fa0ac2013-06-24 17:41:40 -07001067 selinux_initialize();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -04001068 /* These directories were necessarily created before initial policy load
Stephen Smalleye096e362012-06-11 13:37:39 -04001069 * and therefore need their security context restored to the proper value.
1070 * This must happen before /dev is populated by ueventd.
1071 */
1072 restorecon("/dev");
1073 restorecon("/dev/socket");
Geremy Condra8e15eab2013-02-28 17:29:58 -08001074 restorecon("/dev/__properties__");
Nick Kralevichae76f6d2013-07-11 15:38:26 -07001075 restorecon_recursive("/sys");
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001076
Dima Zavind7634c92011-12-16 14:18:06 -08001077 is_charger = !strcmp(bootmode, "charger");
1078
1079 INFO("property init\n");
Riley Andrewse4b7b292014-06-16 15:06:21 -07001080 property_load_boot_defaults();
Dima Zavind7634c92011-12-16 14:18:06 -08001081
1082 INFO("reading config file\n");
1083 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001084
1085 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001086
Colin Crossf83d0b92010-04-21 12:04:20 -07001087 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Alex Klyubin0d872d82013-08-16 13:19:24 -07001088 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001089 queue_builtin_action(keychord_init_action, "keychord_init");
1090 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001091
Dima Zavinca47cef2011-08-24 15:28:23 -07001092 /* execute all the boot actions to get us started */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001093 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001094
Alex Klyubin0d872d82013-08-16 13:19:24 -07001095 /* Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
1096 * wasn't ready immediately after wait_for_coldboot_done
1097 */
1098 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001099 queue_builtin_action(property_service_init_action, "property_service_init");
1100 queue_builtin_action(signal_init_action, "signal_init");
1101 queue_builtin_action(check_startup_action, "check_startup");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001102
Riley Andrewse4b7b292014-06-16 15:06:21 -07001103 /* Don't mount filesystems or start core system services if in charger mode. */
Dima Zavind7634c92011-12-16 14:18:06 -08001104 if (is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -07001105 action_for_each_trigger("charger", action_add_queue_tail);
1106 } else {
Riley Andrewse4b7b292014-06-16 15:06:21 -07001107 action_for_each_trigger("late-init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001108 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001109
1110 /* run all property triggers based on current state of the properties */
Chris Dearman469b7b22012-03-01 15:29:20 -08001111 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001112
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001113
1114#if BOOTCHART
Colin Crossebc6ff12010-04-13 19:52:01 -07001115 queue_builtin_action(bootchart_init_action, "bootchart_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001116#endif
1117
1118 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001119 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001120
Colin Crossebc6ff12010-04-13 19:52:01 -07001121 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001122 restart_processes();
1123
Colin Crossebc6ff12010-04-13 19:52:01 -07001124 if (!property_set_fd_init && get_property_set_fd() > 0) {
1125 ufds[fd_count].fd = get_property_set_fd();
1126 ufds[fd_count].events = POLLIN;
1127 ufds[fd_count].revents = 0;
1128 fd_count++;
1129 property_set_fd_init = 1;
1130 }
1131 if (!signal_fd_init && get_signal_fd() > 0) {
1132 ufds[fd_count].fd = get_signal_fd();
1133 ufds[fd_count].events = POLLIN;
1134 ufds[fd_count].revents = 0;
1135 fd_count++;
1136 signal_fd_init = 1;
1137 }
1138 if (!keychord_fd_init && get_keychord_fd() > 0) {
1139 ufds[fd_count].fd = get_keychord_fd();
1140 ufds[fd_count].events = POLLIN;
1141 ufds[fd_count].revents = 0;
1142 fd_count++;
1143 keychord_fd_init = 1;
1144 }
1145
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001146 if (process_needs_restart) {
1147 timeout = (process_needs_restart - gettime()) * 1000;
1148 if (timeout < 0)
1149 timeout = 0;
1150 }
1151
Colin Crossebd46132010-04-22 11:52:23 -07001152 if (!action_queue_empty() || cur_action)
Colin Crossebc6ff12010-04-13 19:52:01 -07001153 timeout = 0;
1154
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001155#if BOOTCHART
1156 if (bootchart_count > 0) {
1157 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
1158 timeout = BOOTCHART_POLLING_MS;
1159 if (bootchart_step() < 0 || --bootchart_count == 0) {
1160 bootchart_finish();
1161 bootchart_count = 0;
1162 }
1163 }
1164#endif
Colin Crossebc6ff12010-04-13 19:52:01 -07001165
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001166 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001167 if (nr <= 0)
1168 continue;
1169
Colin Crossebc6ff12010-04-13 19:52:01 -07001170 for (i = 0; i < fd_count; i++) {
Amir Goldstein1d4e86c2013-11-10 15:36:58 +02001171 if (ufds[i].revents & POLLIN) {
Colin Crossf83d0b92010-04-21 12:04:20 -07001172 if (ufds[i].fd == get_property_set_fd())
Colin Crossebc6ff12010-04-13 19:52:01 -07001173 handle_property_set_fd();
1174 else if (ufds[i].fd == get_keychord_fd())
1175 handle_keychord();
1176 else if (ufds[i].fd == get_signal_fd())
1177 handle_signal();
1178 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001179 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001180 }
1181
1182 return 0;
1183}