blob: 06a7326a5483508facd2d5db932906f4edecb96b [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;
Bo (Andover) Zhang37003732014-07-24 13:11:35 -040068static long long bootchart_time = 0;
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
Colin Crossebc6ff12010-04-13 19:52:01 -070077static struct action *cur_action = NULL;
78static struct command *cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -070079
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) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800116 char* entry;
117 asprintf(&entry, "%s=%s", key, val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700118 ENV[n] = entry;
119 return 0;
120 }
121 }
122
James Morrissey381341f2014-05-16 11:36:36 +0100123 ERROR("No env. room to store: '%s':'%s'\n", key, val);
124
125 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700126}
127
San Mehat429721c2014-09-23 07:48:47 -0700128void zap_stdio(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700129{
130 int fd;
131 fd = open("/dev/null", O_RDWR);
132 dup2(fd, 0);
133 dup2(fd, 1);
134 dup2(fd, 2);
135 close(fd);
136}
137
138static void open_console()
139{
140 int fd;
141 if ((fd = open(console_name, O_RDWR)) < 0) {
142 fd = open("/dev/null", O_RDWR);
143 }
Colin Cross50fb5a62012-03-18 15:38:19 -0700144 ioctl(fd, TIOCSCTTY, 0);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700145 dup2(fd, 0);
146 dup2(fd, 1);
147 dup2(fd, 2);
148 close(fd);
149}
150
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700151static void publish_socket(const char *name, int fd)
152{
153 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
154 char val[64];
155
156 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
157 name,
158 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
159 snprintf(val, sizeof(val), "%d", fd);
160 add_environment(key, val);
161
162 /* make sure we don't close-on-exec */
163 fcntl(fd, F_SETFD, 0);
164}
165
San Mehatf24e2522009-05-19 13:30:46 -0700166void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700167{
168 struct stat s;
169 pid_t pid;
170 int needs_console;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500171 char *scon = NULL;
172 int rc;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700173
Ken Sumrall752923c2010-12-03 16:33:31 -0800174 /* starting a service removes it from the disabled or reset
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700175 * state and immediately takes it out of the restarting
176 * state if it was in there
177 */
JP Abgrall3beec7e2014-05-02 21:14:29 -0700178 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700179 svc->time_started = 0;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700180
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700181 /* running processes require no additional work -- if
182 * they're in the process of exiting, we've ensured
183 * that they will immediately restart on exit, unless
184 * they are ONESHOT
185 */
186 if (svc->flags & SVC_RUNNING) {
187 return;
188 }
189
190 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
191 if (needs_console && (!have_console)) {
192 ERROR("service '%s' requires console\n", svc->name);
193 svc->flags |= SVC_DISABLED;
194 return;
195 }
196
197 if (stat(svc->args[0], &s) != 0) {
198 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
199 svc->flags |= SVC_DISABLED;
200 return;
201 }
202
San Mehatf24e2522009-05-19 13:30:46 -0700203 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700204 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
205 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700206 svc->flags |= SVC_DISABLED;
207 return;
208 }
209
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500210 if (is_selinux_enabled() > 0) {
Stephen Smalley30f30332012-11-16 14:34:27 -0500211 if (svc->seclabel) {
212 scon = strdup(svc->seclabel);
213 if (!scon) {
214 ERROR("Out of memory while starting '%s'\n", svc->name);
215 return;
216 }
217 } else {
218 char *mycon = NULL, *fcon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500219
Stephen Smalley30f30332012-11-16 14:34:27 -0500220 INFO("computing context for service '%s'\n", svc->args[0]);
221 rc = getcon(&mycon);
222 if (rc < 0) {
223 ERROR("could not get context while starting '%s'\n", svc->name);
224 return;
225 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500226
Stephen Smalley30f30332012-11-16 14:34:27 -0500227 rc = getfilecon(svc->args[0], &fcon);
228 if (rc < 0) {
229 ERROR("could not get context while starting '%s'\n", svc->name);
230 freecon(mycon);
231 return;
232 }
233
234 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon);
Stephen Smalleyaf06c672013-12-09 15:40:24 -0500235 if (rc == 0 && !strcmp(scon, mycon)) {
236 ERROR("Warning! Service %s needs a SELinux domain defined; please fix!\n", svc->name);
237 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500238 freecon(mycon);
Stephen Smalley30f30332012-11-16 14:34:27 -0500239 freecon(fcon);
240 if (rc < 0) {
241 ERROR("could not get context while starting '%s'\n", svc->name);
242 return;
243 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500244 }
245 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500246
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700247 NOTICE("starting '%s'\n", svc->name);
248
249 pid = fork();
250
251 if (pid == 0) {
252 struct socketinfo *si;
253 struct svcenvinfo *ei;
254 char tmp[32];
255 int fd, sz;
256
Nick Kralevich6ebf12f2012-03-26 09:09:11 -0700257 umask(077);
Colin Cross3294bbb2010-04-19 17:11:33 -0700258 if (properties_inited()) {
259 get_property_workspace(&fd, &sz);
260 sprintf(tmp, "%d,%d", dup(fd), sz);
261 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
262 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700263
264 for (ei = svc->envvars; ei; ei = ei->next)
265 add_environment(ei->name, ei->value);
266
267 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400268 int socket_type = (
269 !strcmp(si->type, "stream") ? SOCK_STREAM :
270 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
271 int s = create_socket(si->name, socket_type,
Stephen Smalley8348d272013-05-13 12:37:04 -0400272 si->perm, si->uid, si->gid, si->socketcon ?: scon);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700273 if (s >= 0) {
274 publish_socket(si->name, s);
275 }
276 }
277
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500278 freecon(scon);
279 scon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500280
San Mehat4e221f02010-02-25 14:19:50 -0800281 if (svc->ioprio_class != IoSchedClass_NONE) {
282 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
283 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
284 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
285 }
286 }
287
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700288 if (needs_console) {
289 setsid();
290 open_console();
291 } else {
292 zap_stdio();
293 }
294
295#if 0
296 for (n = 0; svc->args[n]; n++) {
297 INFO("args[%d] = '%s'\n", n, svc->args[n]);
298 }
299 for (n = 0; ENV[n]; n++) {
300 INFO("env[%d] = '%s'\n", n, ENV[n]);
301 }
302#endif
303
304 setpgid(0, getpid());
305
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800306 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700307 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800308 if (setgid(svc->gid) != 0) {
309 ERROR("setgid failed: %s\n", strerror(errno));
310 _exit(127);
311 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700312 }
313 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800314 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
315 ERROR("setgroups failed: %s\n", strerror(errno));
316 _exit(127);
317 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700318 }
319 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800320 if (setuid(svc->uid) != 0) {
321 ERROR("setuid failed: %s\n", strerror(errno));
322 _exit(127);
323 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700324 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500325 if (svc->seclabel) {
326 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) {
327 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
328 _exit(127);
329 }
330 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500331
San Mehat8ad15682009-05-20 08:50:40 -0700332 if (!dynamic_args) {
333 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
334 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
335 }
336 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700337 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700338 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700339 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700340 char *next = tmp;
341 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700342
343 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700344 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700345
San Mehatd4cdd132009-05-20 09:52:16 -0700346 while((bword = strsep(&next, " "))) {
347 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700348 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700349 break;
San Mehatf24e2522009-05-19 13:30:46 -0700350 }
Andreas Gampe0ab46c92015-02-03 11:20:49 -0800351 arg_ptrs[arg_idx] = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700352 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100353 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700354 _exit(127);
355 }
356
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500357 freecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500358
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700359 if (pid < 0) {
360 ERROR("failed to start '%s'\n", svc->name);
361 svc->pid = 0;
362 return;
363 }
364
365 svc->time_started = gettime();
366 svc->pid = pid;
367 svc->flags |= SVC_RUNNING;
368
Colin Cross3294bbb2010-04-19 17:11:33 -0700369 if (properties_inited())
370 notify_service_state(svc->name, "running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700371}
372
Mike Kasickb54f39f2012-01-25 23:48:46 -0500373/* The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART */
Ken Sumrall752923c2010-12-03 16:33:31 -0800374static void service_stop_or_reset(struct service *svc, int how)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700375{
Mike Kasick7e36edd2012-02-06 10:32:13 -0500376 /* The service is still SVC_RUNNING until its process exits, but if it has
377 * already exited it shoudn't attempt a restart yet. */
JP Abgrall3beec7e2014-05-02 21:14:29 -0700378 svc->flags &= ~(SVC_RESTARTING | SVC_DISABLED_START);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700379
Mike Kasickb54f39f2012-01-25 23:48:46 -0500380 if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) {
Ken Sumrall752923c2010-12-03 16:33:31 -0800381 /* 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
Mike Kasickb54f39f2012-01-25 23:48:46 -0500412void service_restart(struct service *svc)
413{
414 if (svc->flags & SVC_RUNNING) {
415 /* Stop, wait, then start the service. */
416 service_stop_or_reset(svc, SVC_RESTART);
417 } else if (!(svc->flags & SVC_RESTARTING)) {
418 /* Just start the service since it's not running. */
419 service_start(svc, NULL);
420 } /* else: Service is restarting anyways. */
421}
422
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700423void property_changed(const char *name, const char *value)
424{
Colin Crossebc6ff12010-04-13 19:52:01 -0700425 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700426 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700427}
428
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700429static void restart_service_if_needed(struct service *svc)
430{
431 time_t next_start_time = svc->time_started + 5;
432
433 if (next_start_time <= gettime()) {
434 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700435 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700436 return;
437 }
438
439 if ((next_start_time < process_needs_restart) ||
440 (process_needs_restart == 0)) {
441 process_needs_restart = next_start_time;
442 }
443}
444
445static void restart_processes()
446{
447 process_needs_restart = 0;
448 service_for_each_flags(SVC_RESTARTING,
449 restart_service_if_needed);
450}
451
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700452static void msg_start(const char *name)
453{
Hong-Mei Li11467182013-04-01 11:17:51 +0800454 struct service *svc = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700455 char *tmp = NULL;
456 char *args = NULL;
457
458 if (!strchr(name, ':'))
459 svc = service_find_by_name(name);
460 else {
461 tmp = strdup(name);
Hong-Mei Li11467182013-04-01 11:17:51 +0800462 if (tmp) {
463 args = strchr(tmp, ':');
464 *args = '\0';
465 args++;
San Mehatf24e2522009-05-19 13:30:46 -0700466
Hong-Mei Li11467182013-04-01 11:17:51 +0800467 svc = service_find_by_name(tmp);
468 }
San Mehatf24e2522009-05-19 13:30:46 -0700469 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700470
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700471 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700472 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700473 } else {
474 ERROR("no such service '%s'\n", name);
475 }
San Mehatf24e2522009-05-19 13:30:46 -0700476 if (tmp)
477 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700478}
479
480static void msg_stop(const char *name)
481{
482 struct service *svc = service_find_by_name(name);
483
484 if (svc) {
485 service_stop(svc);
486 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700487 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700488 }
489}
490
Mike Kasickb54f39f2012-01-25 23:48:46 -0500491static void msg_restart(const char *name)
492{
493 struct service *svc = service_find_by_name(name);
494
495 if (svc) {
496 service_restart(svc);
497 } else {
498 ERROR("no such service '%s'\n", name);
499 }
500}
501
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700502void handle_control_message(const char *msg, const char *arg)
503{
504 if (!strcmp(msg,"start")) {
505 msg_start(arg);
506 } else if (!strcmp(msg,"stop")) {
507 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700508 } else if (!strcmp(msg,"restart")) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500509 msg_restart(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700510 } else {
511 ERROR("unknown control msg '%s'\n", msg);
512 }
513}
514
Colin Crossebc6ff12010-04-13 19:52:01 -0700515static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700516{
517 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700518 node = list_head(&act->commands);
Dima Zavin3bea0792011-08-26 13:59:18 -0700519 if (!node || list_empty(&act->commands))
Colin Crossebc6ff12010-04-13 19:52:01 -0700520 return NULL;
521
522 return node_to_item(node, struct command, clist);
523}
524
525static struct command *get_next_command(struct action *act, struct command *cmd)
526{
527 struct listnode *node;
528 node = cmd->clist.next;
529 if (!node)
530 return NULL;
531 if (node == &act->commands)
532 return NULL;
533
534 return node_to_item(node, struct command, clist);
535}
536
537static int is_last_command(struct action *act, struct command *cmd)
538{
539 return (list_tail(&act->commands) == &cmd->clist);
540}
541
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700542
543void build_triggers_string(char *name_str, int length, struct action *cur_action) {
544 struct listnode *node;
545 struct trigger *cur_trigger;
546
547 list_for_each(node, &cur_action->triggers) {
548 cur_trigger = node_to_item(node, struct trigger, nlist);
549 if (node != cur_action->triggers.next) {
550 strlcat(name_str, " " , length);
551 }
552 strlcat(name_str, cur_trigger->name , length);
553 }
554}
555
Colin Crossebc6ff12010-04-13 19:52:01 -0700556void execute_one_command(void)
557{
Riley Andrews24a3b782014-06-26 13:56:01 -0700558 int ret, i;
559 char cmd_str[256] = "";
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700560 char name_str[256] = "";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700561
Colin Crossebc6ff12010-04-13 19:52:01 -0700562 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
563 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700564 cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -0700565 if (!cur_action)
566 return;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700567
568 build_triggers_string(name_str, sizeof(name_str), cur_action);
569
570 INFO("processing action %p (%s)\n", cur_action, name_str);
Colin Crossebc6ff12010-04-13 19:52:01 -0700571 cur_command = get_first_command(cur_action);
572 } else {
573 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700574 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700575
576 if (!cur_command)
577 return;
578
579 ret = cur_command->func(cur_command->nargs, cur_command->args);
Riley Andrews24a3b782014-06-26 13:56:01 -0700580 if (klog_get_level() >= KLOG_INFO_LEVEL) {
581 for (i = 0; i < cur_command->nargs; i++) {
582 strlcat(cmd_str, cur_command->args[i], sizeof(cmd_str));
583 if (i < cur_command->nargs - 1) {
584 strlcat(cmd_str, " ", sizeof(cmd_str));
585 }
586 }
587 INFO("command '%s' action=%s status=%d (%s:%d)\n",
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700588 cmd_str, cur_action ? name_str : "", ret, cur_command->filename,
Riley Andrews24a3b782014-06-26 13:56:01 -0700589 cur_command->line);
590 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700591}
592
Colin Crossf83d0b92010-04-21 12:04:20 -0700593static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700594{
Colin Crossf83d0b92010-04-21 12:04:20 -0700595 int ret;
Andreas Gampea016c422014-11-24 19:52:41 -0800596 INFO("wait for %s\n", COLDBOOT_DONE);
597 ret = wait_for_file(COLDBOOT_DONE, COMMAND_RETRY_TIMEOUT);
Colin Crossf83d0b92010-04-21 12:04:20 -0700598 if (ret)
Andreas Gampea016c422014-11-24 19:52:41 -0800599 ERROR("Timed out waiting for %s\n", COLDBOOT_DONE);
Colin Crossf83d0b92010-04-21 12:04:20 -0700600 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700601}
602
Alex Klyubin0d872d82013-08-16 13:19:24 -0700603/*
604 * Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed
605 * by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom.
606 * Does nothing if Hardware RNG is not present.
607 *
608 * Since we don't yet trust the quality of Hardware RNG, these bytes are not
609 * mixed into the primary pool of Linux RNG and the entropy estimate is left
610 * unmodified.
611 *
612 * If the HW RNG device /dev/hw_random is present, we require that at least
613 * 512 bytes read from it are written into Linux RNG. QA is expected to catch
614 * devices/configurations where these I/O operations are blocking for a long
615 * time. We do not reboot or halt on failures, as this is a best-effort
616 * attempt.
617 */
618static int mix_hwrng_into_linux_rng_action(int nargs, char **args)
619{
620 int result = -1;
621 int hwrandom_fd = -1;
622 int urandom_fd = -1;
623 char buf[512];
624 ssize_t chunk_size;
625 size_t total_bytes_written = 0;
626
627 hwrandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800628 open("/dev/hw_random", O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700629 if (hwrandom_fd == -1) {
630 if (errno == ENOENT) {
631 ERROR("/dev/hw_random not found\n");
632 /* It's not an error to not have a Hardware RNG. */
633 result = 0;
634 } else {
635 ERROR("Failed to open /dev/hw_random: %s\n", strerror(errno));
636 }
637 goto ret;
638 }
639
640 urandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800641 open("/dev/urandom", O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700642 if (urandom_fd == -1) {
643 ERROR("Failed to open /dev/urandom: %s\n", strerror(errno));
644 goto ret;
645 }
646
647 while (total_bytes_written < sizeof(buf)) {
648 chunk_size = TEMP_FAILURE_RETRY(
649 read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written));
650 if (chunk_size == -1) {
651 ERROR("Failed to read from /dev/hw_random: %s\n", strerror(errno));
652 goto ret;
653 } else if (chunk_size == 0) {
654 ERROR("Failed to read from /dev/hw_random: EOF\n");
655 goto ret;
656 }
657
658 chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size));
659 if (chunk_size == -1) {
660 ERROR("Failed to write to /dev/urandom: %s\n", strerror(errno));
661 goto ret;
662 }
663 total_bytes_written += chunk_size;
664 }
665
Elliott Hughesccecf142014-01-16 10:53:11 -0800666 INFO("Mixed %zu bytes from /dev/hw_random into /dev/urandom",
Alex Klyubin0d872d82013-08-16 13:19:24 -0700667 total_bytes_written);
668 result = 0;
669
670ret:
671 if (hwrandom_fd != -1) {
672 close(hwrandom_fd);
673 }
674 if (urandom_fd != -1) {
675 close(urandom_fd);
676 }
677 memset(buf, 0, sizeof(buf));
678 return result;
679}
680
Colin Crossebc6ff12010-04-13 19:52:01 -0700681static int keychord_init_action(int nargs, char **args)
682{
683 keychord_init();
684 return 0;
685}
686
687static int console_init_action(int nargs, char **args)
688{
689 int fd;
Colin Crossebc6ff12010-04-13 19:52:01 -0700690
691 if (console[0]) {
Hong-Mei Li11467182013-04-01 11:17:51 +0800692 snprintf(console_name, sizeof(console_name), "/dev/%s", console);
Colin Crossebc6ff12010-04-13 19:52:01 -0700693 }
694
Nick Kralevich45a884f2015-02-02 14:37:22 -0800695 fd = open(console_name, O_RDWR | O_CLOEXEC);
Colin Crossebc6ff12010-04-13 19:52:01 -0700696 if (fd >= 0)
697 have_console = 1;
698 close(fd);
699
Nick Kralevich45a884f2015-02-02 14:37:22 -0800700 fd = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200701 if (fd >= 0) {
702 const char *msg;
703 msg = "\n"
704 "\n"
705 "\n"
706 "\n"
707 "\n"
708 "\n"
709 "\n" // console is 40 cols x 30 lines
710 "\n"
711 "\n"
712 "\n"
713 "\n"
714 "\n"
715 "\n"
716 "\n"
717 " A N D R O I D ";
718 write(fd, msg, strlen(msg));
719 close(fd);
Colin Crossebc6ff12010-04-13 19:52:01 -0700720 }
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200721
Colin Crossebc6ff12010-04-13 19:52:01 -0700722 return 0;
723}
724
Dima Zavin5511c842011-12-19 11:21:32 -0800725static void import_kernel_nv(char *name, int for_emulator)
726{
727 char *value = strchr(name, '=');
728 int name_len = strlen(name);
729
730 if (value == 0) return;
731 *value++ = 0;
732 if (name_len == 0) return;
733
734 if (for_emulator) {
735 /* in the emulator, export any kernel option with the
736 * ro.kernel. prefix */
737 char buff[PROP_NAME_MAX];
738 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
739
740 if (len < (int)sizeof(buff))
741 property_set( buff, value );
742 return;
743 }
744
745 if (!strcmp(name,"qemu")) {
746 strlcpy(qemu, value, sizeof(qemu));
747 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
748 const char *boot_prop_name = name + 12;
749 char prop[PROP_NAME_MAX];
750 int cnt;
751
752 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
753 if (cnt < PROP_NAME_MAX)
754 property_set(prop, value);
755 }
756}
757
758static void export_kernel_boot_props(void)
Colin Crossebc6ff12010-04-13 19:52:01 -0700759{
760 char tmp[PROP_VALUE_MAX];
Colin Cross1a6f4c32013-01-28 17:13:35 -0800761 int ret;
Dima Zavin5511c842011-12-19 11:21:32 -0800762 unsigned i;
763 struct {
764 const char *src_prop;
765 const char *dest_prop;
766 const char *def_val;
767 } prop_map[] = {
768 { "ro.boot.serialno", "ro.serialno", "", },
769 { "ro.boot.mode", "ro.bootmode", "unknown", },
770 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800771 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
772 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700773
Dima Zavin5511c842011-12-19 11:21:32 -0800774 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
Colin Cross1a6f4c32013-01-28 17:13:35 -0800775 ret = property_get(prop_map[i].src_prop, tmp);
Colin Cross5e484e92013-06-17 16:20:08 -0700776 if (ret > 0)
777 property_set(prop_map[i].dest_prop, tmp);
778 else
Colin Cross1a6f4c32013-01-28 17:13:35 -0800779 property_set(prop_map[i].dest_prop, prop_map[i].def_val);
Dima Zavin5511c842011-12-19 11:21:32 -0800780 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700781
Colin Cross1a6f4c32013-01-28 17:13:35 -0800782 ret = property_get("ro.boot.console", tmp);
783 if (ret)
784 strlcpy(console, tmp, sizeof(console));
Dima Zavin5511c842011-12-19 11:21:32 -0800785
786 /* save a copy for init's usage during boot */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800787 property_get("ro.bootmode", tmp);
788 strlcpy(bootmode, tmp, sizeof(bootmode));
Dima Zavin5511c842011-12-19 11:21:32 -0800789
790 /* if this was given on kernel command line, override what we read
791 * before (e.g. from /proc/cpuinfo), if anything */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800792 ret = property_get("ro.boot.hardware", tmp);
793 if (ret)
794 strlcpy(hardware, tmp, sizeof(hardware));
Dima Zavin5511c842011-12-19 11:21:32 -0800795 property_set("ro.hardware", hardware);
796
797 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
798 property_set("ro.revision", tmp);
799
800 /* TODO: these are obsolete. We should delete them */
Colin Crossebc6ff12010-04-13 19:52:01 -0700801 if (!strcmp(bootmode,"factory"))
802 property_set("ro.factorytest", "1");
803 else if (!strcmp(bootmode,"factory2"))
804 property_set("ro.factorytest", "2");
805 else
806 property_set("ro.factorytest", "0");
Dima Zavin5511c842011-12-19 11:21:32 -0800807}
Colin Crossebc6ff12010-04-13 19:52:01 -0700808
Dima Zavin5511c842011-12-19 11:21:32 -0800809static void process_kernel_cmdline(void)
810{
811 /* don't expose the raw commandline to nonpriv processes */
812 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700813
Dima Zavin5511c842011-12-19 11:21:32 -0800814 /* first pass does the common stuff, and finds if we are in qemu.
815 * second pass is only necessary for qemu to export all kernel params
816 * as props.
817 */
818 import_kernel_cmdline(0, import_kernel_nv);
819 if (qemu[0])
820 import_kernel_cmdline(1, import_kernel_nv);
821
822 /* now propogate the info given on command line to internal variables
823 * used by init as well as the current required properties
824 */
825 export_kernel_boot_props();
Colin Crossebc6ff12010-04-13 19:52:01 -0700826}
827
828static int property_service_init_action(int nargs, char **args)
829{
830 /* read any property files on system or data and
831 * fire up the property service. This must happen
832 * after the ro.foo properties are set above so
833 * that /data/local.prop cannot interfere with them.
834 */
835 start_property_service();
Riley Andrews9464e5a2014-07-11 15:05:23 -0700836 if (get_property_set_fd() < 0) {
837 ERROR("start_property_service() failed\n");
838 exit(1);
839 }
840
Colin Crossebc6ff12010-04-13 19:52:01 -0700841 return 0;
842}
843
844static int signal_init_action(int nargs, char **args)
845{
846 signal_init();
Riley Andrews9464e5a2014-07-11 15:05:23 -0700847 if (get_signal_fd() < 0) {
848 ERROR("signal_init() failed\n");
Colin Crossebc6ff12010-04-13 19:52:01 -0700849 exit(1);
850 }
851 return 0;
852}
853
854static int queue_property_triggers_action(int nargs, char **args)
855{
856 queue_all_property_triggers();
857 /* enable property triggers */
858 property_triggers_enabled = 1;
859 return 0;
860}
861
862#if BOOTCHART
863static int bootchart_init_action(int nargs, char **args)
864{
865 bootchart_count = bootchart_init();
866 if (bootchart_count < 0) {
867 ERROR("bootcharting init failure\n");
868 } else if (bootchart_count > 0) {
869 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
870 } else {
871 NOTICE("bootcharting ignored\n");
872 }
Carl-Emil Lagerstedt9ab81902011-01-14 09:35:30 +0100873
874 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700875}
876#endif
877
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400878void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500879{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400880 sehandle = selinux_android_file_context_handle();
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500881 selinux_android_set_sehandle(sehandle);
rpcraig63207cd2012-08-09 10:05:49 -0400882 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400883}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500884
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700885static bool selinux_is_disabled(void)
886{
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700887#ifdef ALLOW_DISABLE_SELINUX
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700888 char tmp[PROP_VALUE_MAX];
889
890 if (access("/sys/fs/selinux", F_OK) != 0) {
891 /* SELinux is not compiled into the kernel, or has been disabled
892 * via the kernel command line "selinux=0".
893 */
894 return true;
895 }
896
897 if ((property_get("ro.boot.selinux", tmp) != 0) && (strcmp(tmp, "disabled") == 0)) {
898 /* SELinux is compiled into the kernel, but we've been told to disable it. */
899 return true;
900 }
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700901#endif
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700902
903 return false;
904}
905
906static bool selinux_is_enforcing(void)
907{
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700908#ifdef ALLOW_DISABLE_SELINUX
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700909 char tmp[PROP_VALUE_MAX];
910
911 if (property_get("ro.boot.selinux", tmp) == 0) {
912 /* Property is not set. Assume enforcing */
913 return true;
914 }
915
916 if (strcmp(tmp, "permissive") == 0) {
917 /* SELinux is in the kernel, but we've been told to go into permissive mode */
918 return false;
919 }
920
921 if (strcmp(tmp, "enforcing") != 0) {
922 ERROR("SELinux: Unknown value of ro.boot.selinux. Got: \"%s\". Assuming enforcing.\n", tmp);
923 }
924
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700925#endif
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700926 return true;
927}
928
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400929int selinux_reload_policy(void)
930{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700931 if (selinux_is_disabled()) {
932 return -1;
933 }
934
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400935 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500936
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400937 if (selinux_android_reload_policy() == -1) {
938 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500939 }
940
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400941 if (sehandle)
942 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500943
rpcraig63207cd2012-08-09 10:05:49 -0400944 if (sehandle_prop)
945 selabel_close(sehandle_prop);
946
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400947 selinux_init_all_handles();
948 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500949}
rpcraig63207cd2012-08-09 10:05:49 -0400950
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500951static int audit_callback(void *data, security_class_t cls __attribute__((unused)), char *buf, size_t len)
rpcraig63207cd2012-08-09 10:05:49 -0400952{
953 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
954 return 0;
955}
956
Stephen Smalley439224e2014-06-24 13:45:43 -0400957int log_callback(int type, const char *fmt, ...)
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500958{
959 int level;
960 va_list ap;
961 switch (type) {
962 case SELINUX_WARNING:
963 level = KLOG_WARNING_LEVEL;
964 break;
965 case SELINUX_INFO:
966 level = KLOG_INFO_LEVEL;
967 break;
968 default:
969 level = KLOG_ERROR_LEVEL;
970 break;
971 }
972 va_start(ap, fmt);
973 klog_vwrite(level, fmt, ap);
974 va_end(ap);
975 return 0;
976}
977
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700978static void selinux_initialize(void)
979{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700980 if (selinux_is_disabled()) {
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700981 return;
982 }
983
984 INFO("loading selinux policy\n");
985 if (selinux_android_load_policy() < 0) {
986 ERROR("SELinux: Failed to load policy; rebooting into recovery mode\n");
987 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
988 while (1) { pause(); } // never reached
989 }
990
991 selinux_init_all_handles();
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700992 bool is_enforcing = selinux_is_enforcing();
993 INFO("SELinux: security_setenforce(%d)\n", is_enforcing);
994 security_setenforce(is_enforcing);
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700995}
996
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700997int main(int argc, char **argv)
998{
Colin Crossebc6ff12010-04-13 19:52:01 -0700999 int fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001000 struct pollfd ufds[4];
Colin Crossebc6ff12010-04-13 19:52:01 -07001001 int property_set_fd_init = 0;
1002 int signal_fd_init = 0;
1003 int keychord_fd_init = 0;
Dima Zavind7634c92011-12-16 14:18:06 -08001004 bool is_charger = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001005
Colin Crossf83d0b92010-04-21 12:04:20 -07001006 if (!strcmp(basename(argv[0]), "ueventd"))
1007 return ueventd_main(argc, argv);
1008
Arve Hjønnevågd97d9072012-06-13 21:51:56 -07001009 if (!strcmp(basename(argv[0]), "watchdogd"))
1010 return watchdogd_main(argc, argv);
1011
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001012 /* clear the umask */
1013 umask(0);
1014
1015 /* Get the basic filesystem setup we need put
1016 * together in the initramdisk on / and then we'll
1017 * let the rc file figure out the rest.
1018 */
1019 mkdir("/dev", 0755);
1020 mkdir("/proc", 0755);
1021 mkdir("/sys", 0755);
1022
Nick Kralevich150f19e2010-06-22 16:35:43 -07001023 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001024 mkdir("/dev/pts", 0755);
1025 mkdir("/dev/socket", 0755);
1026 mount("devpts", "/dev/pts", "devpts", 0, NULL);
1027 mount("proc", "/proc", "proc", 0, NULL);
1028 mount("sysfs", "/sys", "sysfs", 0, NULL);
1029
Brian Swetland8d48c8e2011-03-24 15:45:30 -07001030 /* indicate that booting is in progress to background fw loaders, etc */
Nick Kralevich45a884f2015-02-02 14:37:22 -08001031 close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));
Brian Swetland8d48c8e2011-03-24 15:45:30 -07001032
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001033 /* We must have some place other than / to create the
1034 * device nodes for kmsg and null, otherwise we won't
1035 * be able to remount / read-only later on.
1036 * Now that tmpfs is mounted on /dev, we can actually
1037 * talk to the outside world.
1038 */
1039 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -07001040 klog_init();
Dima Zavin5511c842011-12-19 11:21:32 -08001041 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -07001042
Colin Crossf83d0b92010-04-21 12:04:20 -07001043 get_hardware_name(hardware, &revision);
Dima Zavind7634c92011-12-16 14:18:06 -08001044
Dima Zavin5511c842011-12-19 11:21:32 -08001045 process_kernel_cmdline();
1046
rpcraig63207cd2012-08-09 10:05:49 -04001047 union selinux_callback cb;
Stephen Smalleyeb3f4212014-02-12 16:17:00 -05001048 cb.func_log = log_callback;
rpcraig63207cd2012-08-09 10:05:49 -04001049 selinux_set_callback(SELINUX_CB_LOG, cb);
1050
1051 cb.func_audit = audit_callback;
1052 selinux_set_callback(SELINUX_CB_AUDIT, cb);
1053
Nick Kralevich56fa0ac2013-06-24 17:41:40 -07001054 selinux_initialize();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -04001055 /* These directories were necessarily created before initial policy load
Stephen Smalleye096e362012-06-11 13:37:39 -04001056 * and therefore need their security context restored to the proper value.
1057 * This must happen before /dev is populated by ueventd.
1058 */
1059 restorecon("/dev");
1060 restorecon("/dev/socket");
Geremy Condra8e15eab2013-02-28 17:29:58 -08001061 restorecon("/dev/__properties__");
Nick Kralevichae76f6d2013-07-11 15:38:26 -07001062 restorecon_recursive("/sys");
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001063
Dima Zavind7634c92011-12-16 14:18:06 -08001064 is_charger = !strcmp(bootmode, "charger");
1065
1066 INFO("property init\n");
Riley Andrewse4b7b292014-06-16 15:06:21 -07001067 property_load_boot_defaults();
Dima Zavind7634c92011-12-16 14:18:06 -08001068
1069 INFO("reading config file\n");
1070 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001071
1072 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001073
Colin Crossf83d0b92010-04-21 12:04:20 -07001074 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Alex Klyubin0d872d82013-08-16 13:19:24 -07001075 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001076 queue_builtin_action(keychord_init_action, "keychord_init");
1077 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001078
Dima Zavinca47cef2011-08-24 15:28:23 -07001079 /* execute all the boot actions to get us started */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001080 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001081
Alex Klyubin0d872d82013-08-16 13:19:24 -07001082 /* Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
1083 * wasn't ready immediately after wait_for_coldboot_done
1084 */
1085 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001086 queue_builtin_action(property_service_init_action, "property_service_init");
1087 queue_builtin_action(signal_init_action, "signal_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001088
Riley Andrewse4b7b292014-06-16 15:06:21 -07001089 /* Don't mount filesystems or start core system services if in charger mode. */
Dima Zavind7634c92011-12-16 14:18:06 -08001090 if (is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -07001091 action_for_each_trigger("charger", action_add_queue_tail);
1092 } else {
Riley Andrewse4b7b292014-06-16 15:06:21 -07001093 action_for_each_trigger("late-init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001094 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001095
Riley Andrews9464e5a2014-07-11 15:05:23 -07001096 /* run all property triggers based on current state of the properties */
Chris Dearman469b7b22012-03-01 15:29:20 -08001097 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001098
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001099
1100#if BOOTCHART
Colin Crossebc6ff12010-04-13 19:52:01 -07001101 queue_builtin_action(bootchart_init_action, "bootchart_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001102#endif
1103
1104 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001105 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001106
Colin Crossebc6ff12010-04-13 19:52:01 -07001107 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001108 restart_processes();
1109
Colin Crossebc6ff12010-04-13 19:52:01 -07001110 if (!property_set_fd_init && get_property_set_fd() > 0) {
1111 ufds[fd_count].fd = get_property_set_fd();
1112 ufds[fd_count].events = POLLIN;
1113 ufds[fd_count].revents = 0;
1114 fd_count++;
1115 property_set_fd_init = 1;
1116 }
1117 if (!signal_fd_init && get_signal_fd() > 0) {
1118 ufds[fd_count].fd = get_signal_fd();
1119 ufds[fd_count].events = POLLIN;
1120 ufds[fd_count].revents = 0;
1121 fd_count++;
1122 signal_fd_init = 1;
1123 }
1124 if (!keychord_fd_init && get_keychord_fd() > 0) {
1125 ufds[fd_count].fd = get_keychord_fd();
1126 ufds[fd_count].events = POLLIN;
1127 ufds[fd_count].revents = 0;
1128 fd_count++;
1129 keychord_fd_init = 1;
1130 }
1131
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001132 if (process_needs_restart) {
1133 timeout = (process_needs_restart - gettime()) * 1000;
1134 if (timeout < 0)
1135 timeout = 0;
1136 }
1137
Colin Crossebd46132010-04-22 11:52:23 -07001138 if (!action_queue_empty() || cur_action)
Colin Crossebc6ff12010-04-13 19:52:01 -07001139 timeout = 0;
1140
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001141#if BOOTCHART
1142 if (bootchart_count > 0) {
Bo (Andover) Zhang37003732014-07-24 13:11:35 -04001143 long long current_time;
1144 int elapsed_time, remaining_time;
1145
1146 current_time = bootchart_gettime();
1147 elapsed_time = current_time - bootchart_time;
1148
1149 if (elapsed_time >= BOOTCHART_POLLING_MS) {
1150 /* count missed samples */
1151 while (elapsed_time >= BOOTCHART_POLLING_MS) {
1152 elapsed_time -= BOOTCHART_POLLING_MS;
1153 bootchart_count--;
1154 }
1155 /* count may be negative, take a sample anyway */
1156 bootchart_time = current_time;
1157 if (bootchart_step() < 0 || bootchart_count <= 0) {
1158 bootchart_finish();
1159 bootchart_count = 0;
1160 }
1161 }
1162 if (bootchart_count > 0) {
1163 remaining_time = BOOTCHART_POLLING_MS - elapsed_time;
1164 if (timeout < 0 || timeout > remaining_time)
1165 timeout = remaining_time;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001166 }
1167 }
1168#endif
Colin Crossebc6ff12010-04-13 19:52:01 -07001169
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001170 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001171 if (nr <= 0)
1172 continue;
1173
Colin Crossebc6ff12010-04-13 19:52:01 -07001174 for (i = 0; i < fd_count; i++) {
Amir Goldstein1d4e86c2013-11-10 15:36:58 +02001175 if (ufds[i].revents & POLLIN) {
Colin Crossf83d0b92010-04-21 12:04:20 -07001176 if (ufds[i].fd == get_property_set_fd())
Colin Crossebc6ff12010-04-13 19:52:01 -07001177 handle_property_set_fd();
1178 else if (ufds[i].fd == get_keychord_fd())
1179 handle_keychord();
1180 else if (ufds[i].fd == get_signal_fd())
1181 handle_signal();
1182 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001183 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001184 }
1185
1186 return 0;
1187}