blob: 46734a3f47b431a5d64b244b4dffae2910905a2c [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
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070066static char console[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070067static char bootmode[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068static char qemu[32];
69
Colin Crossebc6ff12010-04-13 19:52:01 -070070static struct action *cur_action = NULL;
71static struct command *cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -070072
Colin Cross9c5366b2010-04-13 19:48:59 -070073void notify_service_state(const char *name, const char *state)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070074{
75 char pname[PROP_NAME_MAX];
76 int len = strlen(name);
77 if ((len + 10) > PROP_NAME_MAX)
78 return;
79 snprintf(pname, sizeof(pname), "init.svc.%s", name);
80 property_set(pname, state);
81}
82
83static int have_console;
Hong-Mei Li11467182013-04-01 11:17:51 +080084static char console_name[PROP_VALUE_MAX] = "/dev/console";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070085static time_t process_needs_restart;
86
87static const char *ENV[32];
88
89/* add_environment - add "key=value" to the current environment */
90int add_environment(const char *key, const char *val)
91{
James Morrissey381341f2014-05-16 11:36:36 +010092 size_t n;
93 size_t key_len = strlen(key);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -070094
James Morrissey381341f2014-05-16 11:36:36 +010095 /* The last environment entry is reserved to terminate the list */
96 for (n = 0; n < (ARRAY_SIZE(ENV) - 1); n++) {
97
98 /* Delete any existing entry for this key */
99 if (ENV[n] != NULL) {
100 size_t entry_key_len = strcspn(ENV[n], "=");
101 if ((entry_key_len == key_len) && (strncmp(ENV[n], key, entry_key_len) == 0)) {
102 free((char*)ENV[n]);
103 ENV[n] = NULL;
104 }
105 }
106
107 /* Add entry if a free slot is available */
108 if (ENV[n] == NULL) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800109 char* entry;
110 asprintf(&entry, "%s=%s", key, val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700111 ENV[n] = entry;
112 return 0;
113 }
114 }
115
James Morrissey381341f2014-05-16 11:36:36 +0100116 ERROR("No env. room to store: '%s':'%s'\n", key, val);
117
118 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700119}
120
San Mehat429721c2014-09-23 07:48:47 -0700121void zap_stdio(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700122{
123 int fd;
124 fd = open("/dev/null", O_RDWR);
125 dup2(fd, 0);
126 dup2(fd, 1);
127 dup2(fd, 2);
128 close(fd);
129}
130
131static void open_console()
132{
133 int fd;
134 if ((fd = open(console_name, O_RDWR)) < 0) {
135 fd = open("/dev/null", O_RDWR);
136 }
Colin Cross50fb5a62012-03-18 15:38:19 -0700137 ioctl(fd, TIOCSCTTY, 0);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700138 dup2(fd, 0);
139 dup2(fd, 1);
140 dup2(fd, 2);
141 close(fd);
142}
143
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700144static void publish_socket(const char *name, int fd)
145{
146 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
147 char val[64];
148
149 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
150 name,
151 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
152 snprintf(val, sizeof(val), "%d", fd);
153 add_environment(key, val);
154
155 /* make sure we don't close-on-exec */
156 fcntl(fd, F_SETFD, 0);
157}
158
San Mehatf24e2522009-05-19 13:30:46 -0700159void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700160{
161 struct stat s;
162 pid_t pid;
163 int needs_console;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500164 char *scon = NULL;
165 int rc;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700166
Ken Sumrall752923c2010-12-03 16:33:31 -0800167 /* starting a service removes it from the disabled or reset
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700168 * state and immediately takes it out of the restarting
169 * state if it was in there
170 */
JP Abgrall3beec7e2014-05-02 21:14:29 -0700171 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700172 svc->time_started = 0;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700173
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700174 /* running processes require no additional work -- if
175 * they're in the process of exiting, we've ensured
176 * that they will immediately restart on exit, unless
177 * they are ONESHOT
178 */
179 if (svc->flags & SVC_RUNNING) {
180 return;
181 }
182
183 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
184 if (needs_console && (!have_console)) {
185 ERROR("service '%s' requires console\n", svc->name);
186 svc->flags |= SVC_DISABLED;
187 return;
188 }
189
190 if (stat(svc->args[0], &s) != 0) {
191 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
192 svc->flags |= SVC_DISABLED;
193 return;
194 }
195
San Mehatf24e2522009-05-19 13:30:46 -0700196 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700197 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
198 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700199 svc->flags |= SVC_DISABLED;
200 return;
201 }
202
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500203 if (is_selinux_enabled() > 0) {
Stephen Smalley30f30332012-11-16 14:34:27 -0500204 if (svc->seclabel) {
205 scon = strdup(svc->seclabel);
206 if (!scon) {
207 ERROR("Out of memory while starting '%s'\n", svc->name);
208 return;
209 }
210 } else {
211 char *mycon = NULL, *fcon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500212
Stephen Smalley30f30332012-11-16 14:34:27 -0500213 INFO("computing context for service '%s'\n", svc->args[0]);
214 rc = getcon(&mycon);
215 if (rc < 0) {
216 ERROR("could not get context while starting '%s'\n", svc->name);
217 return;
218 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500219
Stephen Smalley30f30332012-11-16 14:34:27 -0500220 rc = getfilecon(svc->args[0], &fcon);
221 if (rc < 0) {
222 ERROR("could not get context while starting '%s'\n", svc->name);
223 freecon(mycon);
224 return;
225 }
226
227 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon);
Stephen Smalleyaf06c672013-12-09 15:40:24 -0500228 if (rc == 0 && !strcmp(scon, mycon)) {
229 ERROR("Warning! Service %s needs a SELinux domain defined; please fix!\n", svc->name);
230 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500231 freecon(mycon);
Stephen Smalley30f30332012-11-16 14:34:27 -0500232 freecon(fcon);
233 if (rc < 0) {
234 ERROR("could not get context while starting '%s'\n", svc->name);
235 return;
236 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500237 }
238 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500239
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700240 NOTICE("starting '%s'\n", svc->name);
241
242 pid = fork();
243
244 if (pid == 0) {
245 struct socketinfo *si;
246 struct svcenvinfo *ei;
247 char tmp[32];
248 int fd, sz;
249
Nick Kralevich6ebf12f2012-03-26 09:09:11 -0700250 umask(077);
Colin Cross3294bbb2010-04-19 17:11:33 -0700251 if (properties_inited()) {
252 get_property_workspace(&fd, &sz);
Yabin Cuie2d63af2015-02-17 19:27:51 -0800253 snprintf(tmp, sizeof(tmp), "%d,%d", dup(fd), sz);
Colin Cross3294bbb2010-04-19 17:11:33 -0700254 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
255 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700256
257 for (ei = svc->envvars; ei; ei = ei->next)
258 add_environment(ei->name, ei->value);
259
260 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400261 int socket_type = (
262 !strcmp(si->type, "stream") ? SOCK_STREAM :
263 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
264 int s = create_socket(si->name, socket_type,
Stephen Smalley8348d272013-05-13 12:37:04 -0400265 si->perm, si->uid, si->gid, si->socketcon ?: scon);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700266 if (s >= 0) {
267 publish_socket(si->name, s);
268 }
269 }
270
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500271 freecon(scon);
272 scon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500273
San Mehat4e221f02010-02-25 14:19:50 -0800274 if (svc->ioprio_class != IoSchedClass_NONE) {
275 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
276 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
277 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
278 }
279 }
280
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700281 if (needs_console) {
282 setsid();
283 open_console();
284 } else {
285 zap_stdio();
286 }
287
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800288 if (false) {
289 for (size_t n = 0; svc->args[n]; n++) {
290 INFO("args[%zu] = '%s'\n", n, svc->args[n]);
291 }
292 for (size_t n = 0; ENV[n]; n++) {
293 INFO("env[%zu] = '%s'\n", n, ENV[n]);
294 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700295 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700296
297 setpgid(0, getpid());
298
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800299 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700300 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800301 if (setgid(svc->gid) != 0) {
302 ERROR("setgid failed: %s\n", strerror(errno));
303 _exit(127);
304 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700305 }
306 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800307 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
308 ERROR("setgroups failed: %s\n", strerror(errno));
309 _exit(127);
310 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700311 }
312 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800313 if (setuid(svc->uid) != 0) {
314 ERROR("setuid failed: %s\n", strerror(errno));
315 _exit(127);
316 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700317 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500318 if (svc->seclabel) {
319 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) {
320 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
321 _exit(127);
322 }
323 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500324
San Mehat8ad15682009-05-20 08:50:40 -0700325 if (!dynamic_args) {
326 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
327 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
328 }
329 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700330 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700331 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700332 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700333 char *next = tmp;
334 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700335
336 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700337 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700338
San Mehatd4cdd132009-05-20 09:52:16 -0700339 while((bword = strsep(&next, " "))) {
340 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700341 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700342 break;
San Mehatf24e2522009-05-19 13:30:46 -0700343 }
Andreas Gampe0ab46c92015-02-03 11:20:49 -0800344 arg_ptrs[arg_idx] = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700345 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100346 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700347 _exit(127);
348 }
349
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500350 freecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500351
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700352 if (pid < 0) {
353 ERROR("failed to start '%s'\n", svc->name);
354 svc->pid = 0;
355 return;
356 }
357
358 svc->time_started = gettime();
359 svc->pid = pid;
360 svc->flags |= SVC_RUNNING;
361
Colin Cross3294bbb2010-04-19 17:11:33 -0700362 if (properties_inited())
363 notify_service_state(svc->name, "running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700364}
365
Mike Kasickb54f39f2012-01-25 23:48:46 -0500366/* The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART */
Ken Sumrall752923c2010-12-03 16:33:31 -0800367static void service_stop_or_reset(struct service *svc, int how)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700368{
Mike Kasick7e36edd2012-02-06 10:32:13 -0500369 /* The service is still SVC_RUNNING until its process exits, but if it has
370 * already exited it shoudn't attempt a restart yet. */
JP Abgrall3beec7e2014-05-02 21:14:29 -0700371 svc->flags &= ~(SVC_RESTARTING | SVC_DISABLED_START);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700372
Mike Kasickb54f39f2012-01-25 23:48:46 -0500373 if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) {
Ken Sumrall752923c2010-12-03 16:33:31 -0800374 /* Hrm, an illegal flag. Default to SVC_DISABLED */
375 how = SVC_DISABLED;
376 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700377 /* if the service has not yet started, prevent
378 * it from auto-starting with its class
379 */
Ken Sumralla2864802011-10-26 16:56:00 -0700380 if (how == SVC_RESET) {
381 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
382 } else {
383 svc->flags |= how;
384 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700385
386 if (svc->pid) {
387 NOTICE("service '%s' is being killed\n", svc->name);
Ken Sumrall752923c2010-12-03 16:33:31 -0800388 kill(-svc->pid, SIGKILL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700389 notify_service_state(svc->name, "stopping");
390 } else {
391 notify_service_state(svc->name, "stopped");
392 }
393}
394
Ken Sumrall752923c2010-12-03 16:33:31 -0800395void service_reset(struct service *svc)
396{
397 service_stop_or_reset(svc, SVC_RESET);
398}
399
400void service_stop(struct service *svc)
401{
402 service_stop_or_reset(svc, SVC_DISABLED);
403}
404
Mike Kasickb54f39f2012-01-25 23:48:46 -0500405void service_restart(struct service *svc)
406{
407 if (svc->flags & SVC_RUNNING) {
408 /* Stop, wait, then start the service. */
409 service_stop_or_reset(svc, SVC_RESTART);
410 } else if (!(svc->flags & SVC_RESTARTING)) {
411 /* Just start the service since it's not running. */
412 service_start(svc, NULL);
413 } /* else: Service is restarting anyways. */
414}
415
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700416void property_changed(const char *name, const char *value)
417{
Colin Crossebc6ff12010-04-13 19:52:01 -0700418 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700419 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700420}
421
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700422static void restart_service_if_needed(struct service *svc)
423{
424 time_t next_start_time = svc->time_started + 5;
425
426 if (next_start_time <= gettime()) {
427 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700428 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700429 return;
430 }
431
432 if ((next_start_time < process_needs_restart) ||
433 (process_needs_restart == 0)) {
434 process_needs_restart = next_start_time;
435 }
436}
437
438static void restart_processes()
439{
440 process_needs_restart = 0;
441 service_for_each_flags(SVC_RESTARTING,
442 restart_service_if_needed);
443}
444
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700445static void msg_start(const char *name)
446{
Hong-Mei Li11467182013-04-01 11:17:51 +0800447 struct service *svc = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700448 char *tmp = NULL;
449 char *args = NULL;
450
451 if (!strchr(name, ':'))
452 svc = service_find_by_name(name);
453 else {
454 tmp = strdup(name);
Hong-Mei Li11467182013-04-01 11:17:51 +0800455 if (tmp) {
456 args = strchr(tmp, ':');
457 *args = '\0';
458 args++;
San Mehatf24e2522009-05-19 13:30:46 -0700459
Hong-Mei Li11467182013-04-01 11:17:51 +0800460 svc = service_find_by_name(tmp);
461 }
San Mehatf24e2522009-05-19 13:30:46 -0700462 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700463
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700464 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700465 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700466 } else {
467 ERROR("no such service '%s'\n", name);
468 }
San Mehatf24e2522009-05-19 13:30:46 -0700469 if (tmp)
470 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700471}
472
473static void msg_stop(const char *name)
474{
475 struct service *svc = service_find_by_name(name);
476
477 if (svc) {
478 service_stop(svc);
479 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700480 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700481 }
482}
483
Mike Kasickb54f39f2012-01-25 23:48:46 -0500484static void msg_restart(const char *name)
485{
486 struct service *svc = service_find_by_name(name);
487
488 if (svc) {
489 service_restart(svc);
490 } else {
491 ERROR("no such service '%s'\n", name);
492 }
493}
494
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700495void handle_control_message(const char *msg, const char *arg)
496{
497 if (!strcmp(msg,"start")) {
498 msg_start(arg);
499 } else if (!strcmp(msg,"stop")) {
500 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700501 } else if (!strcmp(msg,"restart")) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500502 msg_restart(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700503 } else {
504 ERROR("unknown control msg '%s'\n", msg);
505 }
506}
507
Colin Crossebc6ff12010-04-13 19:52:01 -0700508static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700509{
510 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700511 node = list_head(&act->commands);
Dima Zavin3bea0792011-08-26 13:59:18 -0700512 if (!node || list_empty(&act->commands))
Colin Crossebc6ff12010-04-13 19:52:01 -0700513 return NULL;
514
515 return node_to_item(node, struct command, clist);
516}
517
518static struct command *get_next_command(struct action *act, struct command *cmd)
519{
520 struct listnode *node;
521 node = cmd->clist.next;
522 if (!node)
523 return NULL;
524 if (node == &act->commands)
525 return NULL;
526
527 return node_to_item(node, struct command, clist);
528}
529
530static int is_last_command(struct action *act, struct command *cmd)
531{
532 return (list_tail(&act->commands) == &cmd->clist);
533}
534
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700535
536void build_triggers_string(char *name_str, int length, struct action *cur_action) {
537 struct listnode *node;
538 struct trigger *cur_trigger;
539
540 list_for_each(node, &cur_action->triggers) {
541 cur_trigger = node_to_item(node, struct trigger, nlist);
542 if (node != cur_action->triggers.next) {
543 strlcat(name_str, " " , length);
544 }
545 strlcat(name_str, cur_trigger->name , length);
546 }
547}
548
Colin Crossebc6ff12010-04-13 19:52:01 -0700549void execute_one_command(void)
550{
Riley Andrews24a3b782014-06-26 13:56:01 -0700551 int ret, i;
552 char cmd_str[256] = "";
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700553 char name_str[256] = "";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700554
Colin Crossebc6ff12010-04-13 19:52:01 -0700555 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
556 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700557 cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -0700558 if (!cur_action)
559 return;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700560
561 build_triggers_string(name_str, sizeof(name_str), cur_action);
562
563 INFO("processing action %p (%s)\n", cur_action, name_str);
Colin Crossebc6ff12010-04-13 19:52:01 -0700564 cur_command = get_first_command(cur_action);
565 } else {
566 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700567 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700568
569 if (!cur_command)
570 return;
571
572 ret = cur_command->func(cur_command->nargs, cur_command->args);
Riley Andrews24a3b782014-06-26 13:56:01 -0700573 if (klog_get_level() >= KLOG_INFO_LEVEL) {
574 for (i = 0; i < cur_command->nargs; i++) {
575 strlcat(cmd_str, cur_command->args[i], sizeof(cmd_str));
576 if (i < cur_command->nargs - 1) {
577 strlcat(cmd_str, " ", sizeof(cmd_str));
578 }
579 }
580 INFO("command '%s' action=%s status=%d (%s:%d)\n",
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700581 cmd_str, cur_action ? name_str : "", ret, cur_command->filename,
Riley Andrews24a3b782014-06-26 13:56:01 -0700582 cur_command->line);
583 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700584}
585
Colin Crossf83d0b92010-04-21 12:04:20 -0700586static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700587{
Colin Crossf83d0b92010-04-21 12:04:20 -0700588 int ret;
Andreas Gampea016c422014-11-24 19:52:41 -0800589 INFO("wait for %s\n", COLDBOOT_DONE);
590 ret = wait_for_file(COLDBOOT_DONE, COMMAND_RETRY_TIMEOUT);
Colin Crossf83d0b92010-04-21 12:04:20 -0700591 if (ret)
Andreas Gampea016c422014-11-24 19:52:41 -0800592 ERROR("Timed out waiting for %s\n", COLDBOOT_DONE);
Colin Crossf83d0b92010-04-21 12:04:20 -0700593 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700594}
595
Alex Klyubin0d872d82013-08-16 13:19:24 -0700596/*
597 * Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed
598 * by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom.
599 * Does nothing if Hardware RNG is not present.
600 *
601 * Since we don't yet trust the quality of Hardware RNG, these bytes are not
602 * mixed into the primary pool of Linux RNG and the entropy estimate is left
603 * unmodified.
604 *
605 * If the HW RNG device /dev/hw_random is present, we require that at least
606 * 512 bytes read from it are written into Linux RNG. QA is expected to catch
607 * devices/configurations where these I/O operations are blocking for a long
608 * time. We do not reboot or halt on failures, as this is a best-effort
609 * attempt.
610 */
611static int mix_hwrng_into_linux_rng_action(int nargs, char **args)
612{
613 int result = -1;
614 int hwrandom_fd = -1;
615 int urandom_fd = -1;
616 char buf[512];
617 ssize_t chunk_size;
618 size_t total_bytes_written = 0;
619
620 hwrandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800621 open("/dev/hw_random", O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700622 if (hwrandom_fd == -1) {
623 if (errno == ENOENT) {
624 ERROR("/dev/hw_random not found\n");
625 /* It's not an error to not have a Hardware RNG. */
626 result = 0;
627 } else {
628 ERROR("Failed to open /dev/hw_random: %s\n", strerror(errno));
629 }
630 goto ret;
631 }
632
633 urandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800634 open("/dev/urandom", O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700635 if (urandom_fd == -1) {
636 ERROR("Failed to open /dev/urandom: %s\n", strerror(errno));
637 goto ret;
638 }
639
640 while (total_bytes_written < sizeof(buf)) {
641 chunk_size = TEMP_FAILURE_RETRY(
642 read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written));
643 if (chunk_size == -1) {
644 ERROR("Failed to read from /dev/hw_random: %s\n", strerror(errno));
645 goto ret;
646 } else if (chunk_size == 0) {
647 ERROR("Failed to read from /dev/hw_random: EOF\n");
648 goto ret;
649 }
650
651 chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size));
652 if (chunk_size == -1) {
653 ERROR("Failed to write to /dev/urandom: %s\n", strerror(errno));
654 goto ret;
655 }
656 total_bytes_written += chunk_size;
657 }
658
Elliott Hughesccecf142014-01-16 10:53:11 -0800659 INFO("Mixed %zu bytes from /dev/hw_random into /dev/urandom",
Alex Klyubin0d872d82013-08-16 13:19:24 -0700660 total_bytes_written);
661 result = 0;
662
663ret:
664 if (hwrandom_fd != -1) {
665 close(hwrandom_fd);
666 }
667 if (urandom_fd != -1) {
668 close(urandom_fd);
669 }
670 memset(buf, 0, sizeof(buf));
671 return result;
672}
673
Colin Crossebc6ff12010-04-13 19:52:01 -0700674static int keychord_init_action(int nargs, char **args)
675{
676 keychord_init();
677 return 0;
678}
679
680static int console_init_action(int nargs, char **args)
681{
682 int fd;
Colin Crossebc6ff12010-04-13 19:52:01 -0700683
684 if (console[0]) {
Hong-Mei Li11467182013-04-01 11:17:51 +0800685 snprintf(console_name, sizeof(console_name), "/dev/%s", console);
Colin Crossebc6ff12010-04-13 19:52:01 -0700686 }
687
Nick Kralevich45a884f2015-02-02 14:37:22 -0800688 fd = open(console_name, O_RDWR | O_CLOEXEC);
Colin Crossebc6ff12010-04-13 19:52:01 -0700689 if (fd >= 0)
690 have_console = 1;
691 close(fd);
692
Nick Kralevich45a884f2015-02-02 14:37:22 -0800693 fd = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200694 if (fd >= 0) {
695 const char *msg;
696 msg = "\n"
697 "\n"
698 "\n"
699 "\n"
700 "\n"
701 "\n"
702 "\n" // console is 40 cols x 30 lines
703 "\n"
704 "\n"
705 "\n"
706 "\n"
707 "\n"
708 "\n"
709 "\n"
710 " A N D R O I D ";
711 write(fd, msg, strlen(msg));
712 close(fd);
Colin Crossebc6ff12010-04-13 19:52:01 -0700713 }
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200714
Colin Crossebc6ff12010-04-13 19:52:01 -0700715 return 0;
716}
717
Dima Zavin5511c842011-12-19 11:21:32 -0800718static void import_kernel_nv(char *name, int for_emulator)
719{
720 char *value = strchr(name, '=');
721 int name_len = strlen(name);
722
723 if (value == 0) return;
724 *value++ = 0;
725 if (name_len == 0) return;
726
727 if (for_emulator) {
728 /* in the emulator, export any kernel option with the
729 * ro.kernel. prefix */
730 char buff[PROP_NAME_MAX];
731 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
732
733 if (len < (int)sizeof(buff))
734 property_set( buff, value );
735 return;
736 }
737
738 if (!strcmp(name,"qemu")) {
739 strlcpy(qemu, value, sizeof(qemu));
740 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
741 const char *boot_prop_name = name + 12;
742 char prop[PROP_NAME_MAX];
743 int cnt;
744
745 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
746 if (cnt < PROP_NAME_MAX)
747 property_set(prop, value);
748 }
749}
750
751static void export_kernel_boot_props(void)
Colin Crossebc6ff12010-04-13 19:52:01 -0700752{
753 char tmp[PROP_VALUE_MAX];
Colin Cross1a6f4c32013-01-28 17:13:35 -0800754 int ret;
Dima Zavin5511c842011-12-19 11:21:32 -0800755 unsigned i;
756 struct {
757 const char *src_prop;
758 const char *dest_prop;
759 const char *def_val;
760 } prop_map[] = {
761 { "ro.boot.serialno", "ro.serialno", "", },
762 { "ro.boot.mode", "ro.bootmode", "unknown", },
763 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800764 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
Rom Lemarchand38b340a2015-02-27 17:20:29 -0800765 { "ro.boot.hardware", "ro.hardware", "unknown", },
766 { "ro.boot.revision", "ro.revision", "0", },
Dima Zavin5511c842011-12-19 11:21:32 -0800767 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700768
Dima Zavin5511c842011-12-19 11:21:32 -0800769 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
Colin Cross1a6f4c32013-01-28 17:13:35 -0800770 ret = property_get(prop_map[i].src_prop, tmp);
Colin Cross5e484e92013-06-17 16:20:08 -0700771 if (ret > 0)
772 property_set(prop_map[i].dest_prop, tmp);
773 else
Colin Cross1a6f4c32013-01-28 17:13:35 -0800774 property_set(prop_map[i].dest_prop, prop_map[i].def_val);
Dima Zavin5511c842011-12-19 11:21:32 -0800775 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700776
Colin Cross1a6f4c32013-01-28 17:13:35 -0800777 ret = property_get("ro.boot.console", tmp);
778 if (ret)
779 strlcpy(console, tmp, sizeof(console));
Dima Zavin5511c842011-12-19 11:21:32 -0800780
781 /* save a copy for init's usage during boot */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800782 property_get("ro.bootmode", tmp);
783 strlcpy(bootmode, tmp, sizeof(bootmode));
Dima Zavin5511c842011-12-19 11:21:32 -0800784
Dima Zavin5511c842011-12-19 11:21:32 -0800785 /* TODO: these are obsolete. We should delete them */
Colin Crossebc6ff12010-04-13 19:52:01 -0700786 if (!strcmp(bootmode,"factory"))
787 property_set("ro.factorytest", "1");
788 else if (!strcmp(bootmode,"factory2"))
789 property_set("ro.factorytest", "2");
790 else
791 property_set("ro.factorytest", "0");
Dima Zavin5511c842011-12-19 11:21:32 -0800792}
Colin Crossebc6ff12010-04-13 19:52:01 -0700793
Dima Zavin5511c842011-12-19 11:21:32 -0800794static void process_kernel_cmdline(void)
795{
796 /* don't expose the raw commandline to nonpriv processes */
797 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700798
Dima Zavin5511c842011-12-19 11:21:32 -0800799 /* first pass does the common stuff, and finds if we are in qemu.
800 * second pass is only necessary for qemu to export all kernel params
801 * as props.
802 */
803 import_kernel_cmdline(0, import_kernel_nv);
804 if (qemu[0])
805 import_kernel_cmdline(1, import_kernel_nv);
806
807 /* now propogate the info given on command line to internal variables
808 * used by init as well as the current required properties
809 */
810 export_kernel_boot_props();
Colin Crossebc6ff12010-04-13 19:52:01 -0700811}
812
813static int property_service_init_action(int nargs, char **args)
814{
815 /* read any property files on system or data and
816 * fire up the property service. This must happen
817 * after the ro.foo properties are set above so
818 * that /data/local.prop cannot interfere with them.
819 */
820 start_property_service();
Riley Andrews9464e5a2014-07-11 15:05:23 -0700821 if (get_property_set_fd() < 0) {
822 ERROR("start_property_service() failed\n");
823 exit(1);
824 }
825
Colin Crossebc6ff12010-04-13 19:52:01 -0700826 return 0;
827}
828
829static int signal_init_action(int nargs, char **args)
830{
831 signal_init();
Riley Andrews9464e5a2014-07-11 15:05:23 -0700832 if (get_signal_fd() < 0) {
833 ERROR("signal_init() failed\n");
Colin Crossebc6ff12010-04-13 19:52:01 -0700834 exit(1);
835 }
836 return 0;
837}
838
839static int queue_property_triggers_action(int nargs, char **args)
840{
841 queue_all_property_triggers();
842 /* enable property triggers */
843 property_triggers_enabled = 1;
844 return 0;
845}
846
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400847void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500848{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400849 sehandle = selinux_android_file_context_handle();
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500850 selinux_android_set_sehandle(sehandle);
rpcraig63207cd2012-08-09 10:05:49 -0400851 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400852}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500853
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700854static bool selinux_is_disabled(void)
855{
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800856 if (ALLOW_DISABLE_SELINUX) {
857 if (access("/sys/fs/selinux", F_OK) != 0) {
858 // SELinux is not compiled into the kernel, or has been disabled
859 // via the kernel command line "selinux=0".
860 return true;
861 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700862
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800863 char tmp[PROP_VALUE_MAX];
864 if ((property_get("ro.boot.selinux", tmp) != 0) && (strcmp(tmp, "disabled") == 0)) {
865 // SELinux is compiled into the kernel, but we've been told to disable it.
866 return true;
867 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700868 }
869
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700870 return false;
871}
872
873static bool selinux_is_enforcing(void)
874{
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800875 if (ALLOW_DISABLE_SELINUX) {
876 char tmp[PROP_VALUE_MAX];
877 if (property_get("ro.boot.selinux", tmp) == 0) {
878 // Property is not set. Assume enforcing.
879 return true;
880 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700881
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800882 if (strcmp(tmp, "permissive") == 0) {
883 // SELinux is in the kernel, but we've been told to go into permissive mode.
884 return false;
885 }
886
887 if (strcmp(tmp, "enforcing") != 0) {
888 ERROR("SELinux: Unknown value of ro.boot.selinux. Got: \"%s\". Assuming enforcing.\n", tmp);
889 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700890 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700891 return true;
892}
893
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400894int selinux_reload_policy(void)
895{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700896 if (selinux_is_disabled()) {
897 return -1;
898 }
899
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400900 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500901
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400902 if (selinux_android_reload_policy() == -1) {
903 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500904 }
905
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400906 if (sehandle)
907 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500908
rpcraig63207cd2012-08-09 10:05:49 -0400909 if (sehandle_prop)
910 selabel_close(sehandle_prop);
911
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400912 selinux_init_all_handles();
913 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500914}
rpcraig63207cd2012-08-09 10:05:49 -0400915
Elliott Hughesf682b472015-02-06 12:19:48 -0800916static int audit_callback(void *data, security_class_t /*cls*/, char *buf, size_t len)
rpcraig63207cd2012-08-09 10:05:49 -0400917{
918 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
919 return 0;
920}
921
Stephen Smalley439224e2014-06-24 13:45:43 -0400922int log_callback(int type, const char *fmt, ...)
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500923{
924 int level;
925 va_list ap;
926 switch (type) {
927 case SELINUX_WARNING:
928 level = KLOG_WARNING_LEVEL;
929 break;
930 case SELINUX_INFO:
931 level = KLOG_INFO_LEVEL;
932 break;
933 default:
934 level = KLOG_ERROR_LEVEL;
935 break;
936 }
937 va_start(ap, fmt);
938 klog_vwrite(level, fmt, ap);
939 va_end(ap);
940 return 0;
941}
942
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700943static void selinux_initialize(void)
944{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700945 if (selinux_is_disabled()) {
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700946 return;
947 }
948
949 INFO("loading selinux policy\n");
950 if (selinux_android_load_policy() < 0) {
951 ERROR("SELinux: Failed to load policy; rebooting into recovery mode\n");
952 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
953 while (1) { pause(); } // never reached
954 }
955
956 selinux_init_all_handles();
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700957 bool is_enforcing = selinux_is_enforcing();
958 INFO("SELinux: security_setenforce(%d)\n", is_enforcing);
959 security_setenforce(is_enforcing);
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700960}
961
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700962int main(int argc, char **argv)
963{
Yongqin Liua197ff12014-12-05 13:45:02 +0800964 size_t fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700965 struct pollfd ufds[4];
Colin Crossebc6ff12010-04-13 19:52:01 -0700966 int property_set_fd_init = 0;
967 int signal_fd_init = 0;
968 int keychord_fd_init = 0;
Dima Zavind7634c92011-12-16 14:18:06 -0800969 bool is_charger = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700970
Colin Crossf83d0b92010-04-21 12:04:20 -0700971 if (!strcmp(basename(argv[0]), "ueventd"))
972 return ueventd_main(argc, argv);
973
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700974 if (!strcmp(basename(argv[0]), "watchdogd"))
975 return watchdogd_main(argc, argv);
976
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700977 /* clear the umask */
978 umask(0);
979
980 /* Get the basic filesystem setup we need put
981 * together in the initramdisk on / and then we'll
982 * let the rc file figure out the rest.
983 */
984 mkdir("/dev", 0755);
985 mkdir("/proc", 0755);
986 mkdir("/sys", 0755);
987
Nick Kralevich150f19e2010-06-22 16:35:43 -0700988 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700989 mkdir("/dev/pts", 0755);
990 mkdir("/dev/socket", 0755);
991 mount("devpts", "/dev/pts", "devpts", 0, NULL);
992 mount("proc", "/proc", "proc", 0, NULL);
993 mount("sysfs", "/sys", "sysfs", 0, NULL);
994
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700995 /* indicate that booting is in progress to background fw loaders, etc */
Nick Kralevich45a884f2015-02-02 14:37:22 -0800996 close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700997
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700998 /* We must have some place other than / to create the
999 * device nodes for kmsg and null, otherwise we won't
1000 * be able to remount / read-only later on.
1001 * Now that tmpfs is mounted on /dev, we can actually
1002 * talk to the outside world.
1003 */
1004 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -07001005 klog_init();
Dima Zavin5511c842011-12-19 11:21:32 -08001006 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -07001007
Dima Zavin5511c842011-12-19 11:21:32 -08001008 process_kernel_cmdline();
1009
rpcraig63207cd2012-08-09 10:05:49 -04001010 union selinux_callback cb;
Stephen Smalleyeb3f4212014-02-12 16:17:00 -05001011 cb.func_log = log_callback;
rpcraig63207cd2012-08-09 10:05:49 -04001012 selinux_set_callback(SELINUX_CB_LOG, cb);
1013
1014 cb.func_audit = audit_callback;
1015 selinux_set_callback(SELINUX_CB_AUDIT, cb);
1016
Nick Kralevich56fa0ac2013-06-24 17:41:40 -07001017 selinux_initialize();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -04001018 /* These directories were necessarily created before initial policy load
Stephen Smalleye096e362012-06-11 13:37:39 -04001019 * and therefore need their security context restored to the proper value.
1020 * This must happen before /dev is populated by ueventd.
1021 */
1022 restorecon("/dev");
1023 restorecon("/dev/socket");
Geremy Condra8e15eab2013-02-28 17:29:58 -08001024 restorecon("/dev/__properties__");
Nick Kralevichae76f6d2013-07-11 15:38:26 -07001025 restorecon_recursive("/sys");
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001026
Dima Zavind7634c92011-12-16 14:18:06 -08001027 is_charger = !strcmp(bootmode, "charger");
1028
1029 INFO("property init\n");
Riley Andrewse4b7b292014-06-16 15:06:21 -07001030 property_load_boot_defaults();
Dima Zavind7634c92011-12-16 14:18:06 -08001031
Dima Zavind7634c92011-12-16 14:18:06 -08001032 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001033
1034 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001035
Colin Crossf83d0b92010-04-21 12:04:20 -07001036 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Alex Klyubin0d872d82013-08-16 13:19:24 -07001037 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001038 queue_builtin_action(keychord_init_action, "keychord_init");
1039 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001040
Dima Zavinca47cef2011-08-24 15:28:23 -07001041 /* execute all the boot actions to get us started */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001042 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001043
Alex Klyubin0d872d82013-08-16 13:19:24 -07001044 /* Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
1045 * wasn't ready immediately after wait_for_coldboot_done
1046 */
1047 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001048 queue_builtin_action(property_service_init_action, "property_service_init");
1049 queue_builtin_action(signal_init_action, "signal_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001050
Riley Andrewse4b7b292014-06-16 15:06:21 -07001051 /* Don't mount filesystems or start core system services if in charger mode. */
Dima Zavind7634c92011-12-16 14:18:06 -08001052 if (is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -07001053 action_for_each_trigger("charger", action_add_queue_tail);
1054 } else {
Riley Andrewse4b7b292014-06-16 15:06:21 -07001055 action_for_each_trigger("late-init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001056 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001057
Riley Andrews9464e5a2014-07-11 15:05:23 -07001058 /* run all property triggers based on current state of the properties */
Chris Dearman469b7b22012-03-01 15:29:20 -08001059 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001060
Yongqin Liua197ff12014-12-05 13:45:02 +08001061 for (;;) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001062 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001063 restart_processes();
1064
Colin Crossebc6ff12010-04-13 19:52:01 -07001065 if (!property_set_fd_init && get_property_set_fd() > 0) {
1066 ufds[fd_count].fd = get_property_set_fd();
1067 ufds[fd_count].events = POLLIN;
1068 ufds[fd_count].revents = 0;
1069 fd_count++;
1070 property_set_fd_init = 1;
1071 }
1072 if (!signal_fd_init && get_signal_fd() > 0) {
1073 ufds[fd_count].fd = get_signal_fd();
1074 ufds[fd_count].events = POLLIN;
1075 ufds[fd_count].revents = 0;
1076 fd_count++;
1077 signal_fd_init = 1;
1078 }
1079 if (!keychord_fd_init && get_keychord_fd() > 0) {
1080 ufds[fd_count].fd = get_keychord_fd();
1081 ufds[fd_count].events = POLLIN;
1082 ufds[fd_count].revents = 0;
1083 fd_count++;
1084 keychord_fd_init = 1;
1085 }
1086
Yongqin Liua197ff12014-12-05 13:45:02 +08001087 int timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001088 if (process_needs_restart) {
1089 timeout = (process_needs_restart - gettime()) * 1000;
1090 if (timeout < 0)
1091 timeout = 0;
1092 }
1093
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001094 if (!action_queue_empty() || cur_action) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001095 timeout = 0;
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001096 }
Colin Crossebc6ff12010-04-13 19:52:01 -07001097
Yongqin Liua197ff12014-12-05 13:45:02 +08001098 bootchart_sample(&timeout);
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001099
Yongqin Liua197ff12014-12-05 13:45:02 +08001100 int nr = poll(ufds, fd_count, timeout);
1101 if (nr <= 0) {
1102 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001103 }
Colin Crossebc6ff12010-04-13 19:52:01 -07001104
Yongqin Liua197ff12014-12-05 13:45:02 +08001105 for (size_t i = 0; i < fd_count; i++) {
Amir Goldstein1d4e86c2013-11-10 15:36:58 +02001106 if (ufds[i].revents & POLLIN) {
Yongqin Liua197ff12014-12-05 13:45:02 +08001107 if (ufds[i].fd == get_property_set_fd()) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001108 handle_property_set_fd();
Yongqin Liua197ff12014-12-05 13:45:02 +08001109 } else if (ufds[i].fd == get_keychord_fd()) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001110 handle_keychord();
Yongqin Liua197ff12014-12-05 13:45:02 +08001111 } else if (ufds[i].fd == get_signal_fd()) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001112 handle_signal();
Yongqin Liua197ff12014-12-05 13:45:02 +08001113 }
Colin Crossebc6ff12010-04-13 19:52:01 -07001114 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001115 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001116 }
1117
1118 return 0;
1119}