blob: 99474e6f9d43f4872292a62bc7682e5ad3fc82e3 [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;
79static struct listnode *command_queue = NULL;
80
Colin Cross9c5366b2010-04-13 19:48:59 -070081void notify_service_state(const char *name, const char *state)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070082{
83 char pname[PROP_NAME_MAX];
84 int len = strlen(name);
85 if ((len + 10) > PROP_NAME_MAX)
86 return;
87 snprintf(pname, sizeof(pname), "init.svc.%s", name);
88 property_set(pname, state);
89}
90
91static int have_console;
Hong-Mei Li11467182013-04-01 11:17:51 +080092static char console_name[PROP_VALUE_MAX] = "/dev/console";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070093static time_t process_needs_restart;
94
95static const char *ENV[32];
96
97/* add_environment - add "key=value" to the current environment */
98int add_environment(const char *key, const char *val)
99{
James Morrissey381341f2014-05-16 11:36:36 +0100100 size_t n;
101 size_t key_len = strlen(key);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700102
James Morrissey381341f2014-05-16 11:36:36 +0100103 /* The last environment entry is reserved to terminate the list */
104 for (n = 0; n < (ARRAY_SIZE(ENV) - 1); n++) {
105
106 /* Delete any existing entry for this key */
107 if (ENV[n] != NULL) {
108 size_t entry_key_len = strcspn(ENV[n], "=");
109 if ((entry_key_len == key_len) && (strncmp(ENV[n], key, entry_key_len) == 0)) {
110 free((char*)ENV[n]);
111 ENV[n] = NULL;
112 }
113 }
114
115 /* Add entry if a free slot is available */
116 if (ENV[n] == NULL) {
117 size_t len = key_len + strlen(val) + 2;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700118 char *entry = malloc(len);
119 snprintf(entry, len, "%s=%s", key, val);
120 ENV[n] = entry;
121 return 0;
122 }
123 }
124
James Morrissey381341f2014-05-16 11:36:36 +0100125 ERROR("No env. room to store: '%s':'%s'\n", key, val);
126
127 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700128}
129
130static void zap_stdio(void)
131{
132 int fd;
133 fd = open("/dev/null", O_RDWR);
134 dup2(fd, 0);
135 dup2(fd, 1);
136 dup2(fd, 2);
137 close(fd);
138}
139
140static void open_console()
141{
142 int fd;
143 if ((fd = open(console_name, O_RDWR)) < 0) {
144 fd = open("/dev/null", O_RDWR);
145 }
Colin Cross50fb5a62012-03-18 15:38:19 -0700146 ioctl(fd, TIOCSCTTY, 0);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700147 dup2(fd, 0);
148 dup2(fd, 1);
149 dup2(fd, 2);
150 close(fd);
151}
152
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700153static void publish_socket(const char *name, int fd)
154{
155 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
156 char val[64];
157
158 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
159 name,
160 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
161 snprintf(val, sizeof(val), "%d", fd);
162 add_environment(key, val);
163
164 /* make sure we don't close-on-exec */
165 fcntl(fd, F_SETFD, 0);
166}
167
San Mehatf24e2522009-05-19 13:30:46 -0700168void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700169{
170 struct stat s;
171 pid_t pid;
172 int needs_console;
173 int n;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500174 char *scon = NULL;
175 int rc;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700176
Ken Sumrall752923c2010-12-03 16:33:31 -0800177 /* starting a service removes it from the disabled or reset
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700178 * state and immediately takes it out of the restarting
179 * state if it was in there
180 */
JP Abgrall3beec7e2014-05-02 21:14:29 -0700181 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700182 svc->time_started = 0;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700183
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700184 /* running processes require no additional work -- if
185 * they're in the process of exiting, we've ensured
186 * that they will immediately restart on exit, unless
187 * they are ONESHOT
188 */
189 if (svc->flags & SVC_RUNNING) {
190 return;
191 }
192
193 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
194 if (needs_console && (!have_console)) {
195 ERROR("service '%s' requires console\n", svc->name);
196 svc->flags |= SVC_DISABLED;
197 return;
198 }
199
200 if (stat(svc->args[0], &s) != 0) {
201 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
202 svc->flags |= SVC_DISABLED;
203 return;
204 }
205
San Mehatf24e2522009-05-19 13:30:46 -0700206 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700207 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
208 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700209 svc->flags |= SVC_DISABLED;
210 return;
211 }
212
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500213 if (is_selinux_enabled() > 0) {
Stephen Smalley30f30332012-11-16 14:34:27 -0500214 if (svc->seclabel) {
215 scon = strdup(svc->seclabel);
216 if (!scon) {
217 ERROR("Out of memory while starting '%s'\n", svc->name);
218 return;
219 }
220 } else {
221 char *mycon = NULL, *fcon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500222
Stephen Smalley30f30332012-11-16 14:34:27 -0500223 INFO("computing context for service '%s'\n", svc->args[0]);
224 rc = getcon(&mycon);
225 if (rc < 0) {
226 ERROR("could not get context while starting '%s'\n", svc->name);
227 return;
228 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500229
Stephen Smalley30f30332012-11-16 14:34:27 -0500230 rc = getfilecon(svc->args[0], &fcon);
231 if (rc < 0) {
232 ERROR("could not get context while starting '%s'\n", svc->name);
233 freecon(mycon);
234 return;
235 }
236
237 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon);
Stephen Smalleyaf06c672013-12-09 15:40:24 -0500238 if (rc == 0 && !strcmp(scon, mycon)) {
239 ERROR("Warning! Service %s needs a SELinux domain defined; please fix!\n", svc->name);
240 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500241 freecon(mycon);
Stephen Smalley30f30332012-11-16 14:34:27 -0500242 freecon(fcon);
243 if (rc < 0) {
244 ERROR("could not get context while starting '%s'\n", svc->name);
245 return;
246 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500247 }
248 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500249
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700250 NOTICE("starting '%s'\n", svc->name);
251
252 pid = fork();
253
254 if (pid == 0) {
255 struct socketinfo *si;
256 struct svcenvinfo *ei;
257 char tmp[32];
258 int fd, sz;
259
Nick Kralevich6ebf12f2012-03-26 09:09:11 -0700260 umask(077);
Colin Cross3294bbb2010-04-19 17:11:33 -0700261 if (properties_inited()) {
262 get_property_workspace(&fd, &sz);
263 sprintf(tmp, "%d,%d", dup(fd), sz);
264 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
265 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700266
267 for (ei = svc->envvars; ei; ei = ei->next)
268 add_environment(ei->name, ei->value);
269
270 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400271 int socket_type = (
272 !strcmp(si->type, "stream") ? SOCK_STREAM :
273 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
274 int s = create_socket(si->name, socket_type,
Stephen Smalley8348d272013-05-13 12:37:04 -0400275 si->perm, si->uid, si->gid, si->socketcon ?: scon);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700276 if (s >= 0) {
277 publish_socket(si->name, s);
278 }
279 }
280
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500281 freecon(scon);
282 scon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500283
San Mehat4e221f02010-02-25 14:19:50 -0800284 if (svc->ioprio_class != IoSchedClass_NONE) {
285 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
286 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
287 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
288 }
289 }
290
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700291 if (needs_console) {
292 setsid();
293 open_console();
294 } else {
295 zap_stdio();
296 }
297
298#if 0
299 for (n = 0; svc->args[n]; n++) {
300 INFO("args[%d] = '%s'\n", n, svc->args[n]);
301 }
302 for (n = 0; ENV[n]; n++) {
303 INFO("env[%d] = '%s'\n", n, ENV[n]);
304 }
305#endif
306
307 setpgid(0, getpid());
308
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800309 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700310 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800311 if (setgid(svc->gid) != 0) {
312 ERROR("setgid failed: %s\n", strerror(errno));
313 _exit(127);
314 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700315 }
316 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800317 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
318 ERROR("setgroups failed: %s\n", strerror(errno));
319 _exit(127);
320 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700321 }
322 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800323 if (setuid(svc->uid) != 0) {
324 ERROR("setuid failed: %s\n", strerror(errno));
325 _exit(127);
326 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700327 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500328 if (svc->seclabel) {
329 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) {
330 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
331 _exit(127);
332 }
333 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500334
San Mehat8ad15682009-05-20 08:50:40 -0700335 if (!dynamic_args) {
336 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
337 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
338 }
339 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700340 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700341 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700342 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700343 char *next = tmp;
344 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700345
346 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700347 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700348
San Mehatd4cdd132009-05-20 09:52:16 -0700349 while((bword = strsep(&next, " "))) {
350 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700351 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700352 break;
San Mehatf24e2522009-05-19 13:30:46 -0700353 }
354 arg_ptrs[arg_idx] = '\0';
355 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100356 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700357 _exit(127);
358 }
359
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500360 freecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500361
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700362 if (pid < 0) {
363 ERROR("failed to start '%s'\n", svc->name);
364 svc->pid = 0;
365 return;
366 }
367
368 svc->time_started = gettime();
369 svc->pid = pid;
370 svc->flags |= SVC_RUNNING;
371
Colin Cross3294bbb2010-04-19 17:11:33 -0700372 if (properties_inited())
373 notify_service_state(svc->name, "running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700374}
375
Mike Kasickb54f39f2012-01-25 23:48:46 -0500376/* The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART */
Ken Sumrall752923c2010-12-03 16:33:31 -0800377static void service_stop_or_reset(struct service *svc, int how)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700378{
Mike Kasick7e36edd2012-02-06 10:32:13 -0500379 /* The service is still SVC_RUNNING until its process exits, but if it has
380 * already exited it shoudn't attempt a restart yet. */
JP Abgrall3beec7e2014-05-02 21:14:29 -0700381 svc->flags &= ~(SVC_RESTARTING | SVC_DISABLED_START);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700382
Mike Kasickb54f39f2012-01-25 23:48:46 -0500383 if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) {
Ken Sumrall752923c2010-12-03 16:33:31 -0800384 /* Hrm, an illegal flag. Default to SVC_DISABLED */
385 how = SVC_DISABLED;
386 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700387 /* if the service has not yet started, prevent
388 * it from auto-starting with its class
389 */
Ken Sumralla2864802011-10-26 16:56:00 -0700390 if (how == SVC_RESET) {
391 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
392 } else {
393 svc->flags |= how;
394 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700395
396 if (svc->pid) {
397 NOTICE("service '%s' is being killed\n", svc->name);
Ken Sumrall752923c2010-12-03 16:33:31 -0800398 kill(-svc->pid, SIGKILL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700399 notify_service_state(svc->name, "stopping");
400 } else {
401 notify_service_state(svc->name, "stopped");
402 }
403}
404
Ken Sumrall752923c2010-12-03 16:33:31 -0800405void service_reset(struct service *svc)
406{
407 service_stop_or_reset(svc, SVC_RESET);
408}
409
410void service_stop(struct service *svc)
411{
412 service_stop_or_reset(svc, SVC_DISABLED);
413}
414
Mike Kasickb54f39f2012-01-25 23:48:46 -0500415void service_restart(struct service *svc)
416{
417 if (svc->flags & SVC_RUNNING) {
418 /* Stop, wait, then start the service. */
419 service_stop_or_reset(svc, SVC_RESTART);
420 } else if (!(svc->flags & SVC_RESTARTING)) {
421 /* Just start the service since it's not running. */
422 service_start(svc, NULL);
423 } /* else: Service is restarting anyways. */
424}
425
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700426void property_changed(const char *name, const char *value)
427{
Colin Crossebc6ff12010-04-13 19:52:01 -0700428 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700429 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700430}
431
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700432static void restart_service_if_needed(struct service *svc)
433{
434 time_t next_start_time = svc->time_started + 5;
435
436 if (next_start_time <= gettime()) {
437 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700438 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700439 return;
440 }
441
442 if ((next_start_time < process_needs_restart) ||
443 (process_needs_restart == 0)) {
444 process_needs_restart = next_start_time;
445 }
446}
447
448static void restart_processes()
449{
450 process_needs_restart = 0;
451 service_for_each_flags(SVC_RESTARTING,
452 restart_service_if_needed);
453}
454
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700455static void msg_start(const char *name)
456{
Hong-Mei Li11467182013-04-01 11:17:51 +0800457 struct service *svc = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700458 char *tmp = NULL;
459 char *args = NULL;
460
461 if (!strchr(name, ':'))
462 svc = service_find_by_name(name);
463 else {
464 tmp = strdup(name);
Hong-Mei Li11467182013-04-01 11:17:51 +0800465 if (tmp) {
466 args = strchr(tmp, ':');
467 *args = '\0';
468 args++;
San Mehatf24e2522009-05-19 13:30:46 -0700469
Hong-Mei Li11467182013-04-01 11:17:51 +0800470 svc = service_find_by_name(tmp);
471 }
San Mehatf24e2522009-05-19 13:30:46 -0700472 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700473
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700474 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700475 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700476 } else {
477 ERROR("no such service '%s'\n", name);
478 }
San Mehatf24e2522009-05-19 13:30:46 -0700479 if (tmp)
480 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700481}
482
483static void msg_stop(const char *name)
484{
485 struct service *svc = service_find_by_name(name);
486
487 if (svc) {
488 service_stop(svc);
489 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700490 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700491 }
492}
493
Mike Kasickb54f39f2012-01-25 23:48:46 -0500494static void msg_restart(const char *name)
495{
496 struct service *svc = service_find_by_name(name);
497
498 if (svc) {
499 service_restart(svc);
500 } else {
501 ERROR("no such service '%s'\n", name);
502 }
503}
504
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700505void handle_control_message(const char *msg, const char *arg)
506{
507 if (!strcmp(msg,"start")) {
508 msg_start(arg);
509 } else if (!strcmp(msg,"stop")) {
510 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700511 } else if (!strcmp(msg,"restart")) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500512 msg_restart(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700513 } else {
514 ERROR("unknown control msg '%s'\n", msg);
515 }
516}
517
Colin Crossebc6ff12010-04-13 19:52:01 -0700518static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700519{
520 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700521 node = list_head(&act->commands);
Dima Zavin3bea0792011-08-26 13:59:18 -0700522 if (!node || list_empty(&act->commands))
Colin Crossebc6ff12010-04-13 19:52:01 -0700523 return NULL;
524
525 return node_to_item(node, struct command, clist);
526}
527
528static struct command *get_next_command(struct action *act, struct command *cmd)
529{
530 struct listnode *node;
531 node = cmd->clist.next;
532 if (!node)
533 return NULL;
534 if (node == &act->commands)
535 return NULL;
536
537 return node_to_item(node, struct command, clist);
538}
539
540static int is_last_command(struct action *act, struct command *cmd)
541{
542 return (list_tail(&act->commands) == &cmd->clist);
543}
544
545void execute_one_command(void)
546{
Riley Andrews24a3b782014-06-26 13:56:01 -0700547 int ret, i;
548 char cmd_str[256] = "";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700549
Colin Crossebc6ff12010-04-13 19:52:01 -0700550 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
551 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700552 cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -0700553 if (!cur_action)
554 return;
555 INFO("processing action %p (%s)\n", cur_action, cur_action->name);
556 cur_command = get_first_command(cur_action);
557 } else {
558 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700559 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700560
561 if (!cur_command)
562 return;
563
564 ret = cur_command->func(cur_command->nargs, cur_command->args);
Riley Andrews24a3b782014-06-26 13:56:01 -0700565 if (klog_get_level() >= KLOG_INFO_LEVEL) {
566 for (i = 0; i < cur_command->nargs; i++) {
567 strlcat(cmd_str, cur_command->args[i], sizeof(cmd_str));
568 if (i < cur_command->nargs - 1) {
569 strlcat(cmd_str, " ", sizeof(cmd_str));
570 }
571 }
572 INFO("command '%s' action=%s status=%d (%s:%d)\n",
573 cmd_str, cur_action ? cur_action->name : "", ret, cur_command->filename,
574 cur_command->line);
575 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700576}
577
Colin Crossf83d0b92010-04-21 12:04:20 -0700578static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700579{
Colin Crossf83d0b92010-04-21 12:04:20 -0700580 int ret;
581 INFO("wait for %s\n", coldboot_done);
582 ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT);
583 if (ret)
584 ERROR("Timed out waiting for %s\n", coldboot_done);
585 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700586}
587
Alex Klyubin0d872d82013-08-16 13:19:24 -0700588/*
589 * Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed
590 * by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom.
591 * Does nothing if Hardware RNG is not present.
592 *
593 * Since we don't yet trust the quality of Hardware RNG, these bytes are not
594 * mixed into the primary pool of Linux RNG and the entropy estimate is left
595 * unmodified.
596 *
597 * If the HW RNG device /dev/hw_random is present, we require that at least
598 * 512 bytes read from it are written into Linux RNG. QA is expected to catch
599 * devices/configurations where these I/O operations are blocking for a long
600 * time. We do not reboot or halt on failures, as this is a best-effort
601 * attempt.
602 */
603static int mix_hwrng_into_linux_rng_action(int nargs, char **args)
604{
605 int result = -1;
606 int hwrandom_fd = -1;
607 int urandom_fd = -1;
608 char buf[512];
609 ssize_t chunk_size;
610 size_t total_bytes_written = 0;
611
612 hwrandom_fd = TEMP_FAILURE_RETRY(
613 open("/dev/hw_random", O_RDONLY | O_NOFOLLOW));
614 if (hwrandom_fd == -1) {
615 if (errno == ENOENT) {
616 ERROR("/dev/hw_random not found\n");
617 /* It's not an error to not have a Hardware RNG. */
618 result = 0;
619 } else {
620 ERROR("Failed to open /dev/hw_random: %s\n", strerror(errno));
621 }
622 goto ret;
623 }
624
625 urandom_fd = TEMP_FAILURE_RETRY(
626 open("/dev/urandom", O_WRONLY | O_NOFOLLOW));
627 if (urandom_fd == -1) {
628 ERROR("Failed to open /dev/urandom: %s\n", strerror(errno));
629 goto ret;
630 }
631
632 while (total_bytes_written < sizeof(buf)) {
633 chunk_size = TEMP_FAILURE_RETRY(
634 read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written));
635 if (chunk_size == -1) {
636 ERROR("Failed to read from /dev/hw_random: %s\n", strerror(errno));
637 goto ret;
638 } else if (chunk_size == 0) {
639 ERROR("Failed to read from /dev/hw_random: EOF\n");
640 goto ret;
641 }
642
643 chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size));
644 if (chunk_size == -1) {
645 ERROR("Failed to write to /dev/urandom: %s\n", strerror(errno));
646 goto ret;
647 }
648 total_bytes_written += chunk_size;
649 }
650
Elliott Hughesccecf142014-01-16 10:53:11 -0800651 INFO("Mixed %zu bytes from /dev/hw_random into /dev/urandom",
Alex Klyubin0d872d82013-08-16 13:19:24 -0700652 total_bytes_written);
653 result = 0;
654
655ret:
656 if (hwrandom_fd != -1) {
657 close(hwrandom_fd);
658 }
659 if (urandom_fd != -1) {
660 close(urandom_fd);
661 }
662 memset(buf, 0, sizeof(buf));
663 return result;
664}
665
Colin Crossebc6ff12010-04-13 19:52:01 -0700666static int keychord_init_action(int nargs, char **args)
667{
668 keychord_init();
669 return 0;
670}
671
672static int console_init_action(int nargs, char **args)
673{
674 int fd;
Colin Crossebc6ff12010-04-13 19:52:01 -0700675
676 if (console[0]) {
Hong-Mei Li11467182013-04-01 11:17:51 +0800677 snprintf(console_name, sizeof(console_name), "/dev/%s", console);
Colin Crossebc6ff12010-04-13 19:52:01 -0700678 }
679
680 fd = open(console_name, O_RDWR);
681 if (fd >= 0)
682 have_console = 1;
683 close(fd);
684
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200685 fd = open("/dev/tty0", O_WRONLY);
686 if (fd >= 0) {
687 const char *msg;
688 msg = "\n"
689 "\n"
690 "\n"
691 "\n"
692 "\n"
693 "\n"
694 "\n" // console is 40 cols x 30 lines
695 "\n"
696 "\n"
697 "\n"
698 "\n"
699 "\n"
700 "\n"
701 "\n"
702 " A N D R O I D ";
703 write(fd, msg, strlen(msg));
704 close(fd);
Colin Crossebc6ff12010-04-13 19:52:01 -0700705 }
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200706
Colin Crossebc6ff12010-04-13 19:52:01 -0700707 return 0;
708}
709
Dima Zavin5511c842011-12-19 11:21:32 -0800710static void import_kernel_nv(char *name, int for_emulator)
711{
712 char *value = strchr(name, '=');
713 int name_len = strlen(name);
714
715 if (value == 0) return;
716 *value++ = 0;
717 if (name_len == 0) return;
718
719 if (for_emulator) {
720 /* in the emulator, export any kernel option with the
721 * ro.kernel. prefix */
722 char buff[PROP_NAME_MAX];
723 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
724
725 if (len < (int)sizeof(buff))
726 property_set( buff, value );
727 return;
728 }
729
730 if (!strcmp(name,"qemu")) {
731 strlcpy(qemu, value, sizeof(qemu));
732 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
733 const char *boot_prop_name = name + 12;
734 char prop[PROP_NAME_MAX];
735 int cnt;
736
737 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
738 if (cnt < PROP_NAME_MAX)
739 property_set(prop, value);
740 }
741}
742
743static void export_kernel_boot_props(void)
Colin Crossebc6ff12010-04-13 19:52:01 -0700744{
745 char tmp[PROP_VALUE_MAX];
Colin Cross1a6f4c32013-01-28 17:13:35 -0800746 int ret;
Dima Zavin5511c842011-12-19 11:21:32 -0800747 unsigned i;
748 struct {
749 const char *src_prop;
750 const char *dest_prop;
751 const char *def_val;
752 } prop_map[] = {
753 { "ro.boot.serialno", "ro.serialno", "", },
754 { "ro.boot.mode", "ro.bootmode", "unknown", },
755 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800756 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
757 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700758
Dima Zavin5511c842011-12-19 11:21:32 -0800759 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
Colin Cross1a6f4c32013-01-28 17:13:35 -0800760 ret = property_get(prop_map[i].src_prop, tmp);
Colin Cross5e484e92013-06-17 16:20:08 -0700761 if (ret > 0)
762 property_set(prop_map[i].dest_prop, tmp);
763 else
Colin Cross1a6f4c32013-01-28 17:13:35 -0800764 property_set(prop_map[i].dest_prop, prop_map[i].def_val);
Dima Zavin5511c842011-12-19 11:21:32 -0800765 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700766
Colin Cross1a6f4c32013-01-28 17:13:35 -0800767 ret = property_get("ro.boot.console", tmp);
768 if (ret)
769 strlcpy(console, tmp, sizeof(console));
Dima Zavin5511c842011-12-19 11:21:32 -0800770
771 /* save a copy for init's usage during boot */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800772 property_get("ro.bootmode", tmp);
773 strlcpy(bootmode, tmp, sizeof(bootmode));
Dima Zavin5511c842011-12-19 11:21:32 -0800774
775 /* if this was given on kernel command line, override what we read
776 * before (e.g. from /proc/cpuinfo), if anything */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800777 ret = property_get("ro.boot.hardware", tmp);
778 if (ret)
779 strlcpy(hardware, tmp, sizeof(hardware));
Dima Zavin5511c842011-12-19 11:21:32 -0800780 property_set("ro.hardware", hardware);
781
782 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
783 property_set("ro.revision", tmp);
784
785 /* 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
847#if BOOTCHART
848static int bootchart_init_action(int nargs, char **args)
849{
850 bootchart_count = bootchart_init();
851 if (bootchart_count < 0) {
852 ERROR("bootcharting init failure\n");
853 } else if (bootchart_count > 0) {
854 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
855 } else {
856 NOTICE("bootcharting ignored\n");
857 }
Carl-Emil Lagerstedt9ab81902011-01-14 09:35:30 +0100858
859 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700860}
861#endif
862
rpcraig63207cd2012-08-09 10:05:49 -0400863static const struct selinux_opt seopts_prop[] = {
rpcraig63207cd2012-08-09 10:05:49 -0400864 { SELABEL_OPT_PATH, "/property_contexts" },
Robert Craig03daf052014-03-17 21:16:53 -0400865 { SELABEL_OPT_PATH, "/data/security/current/property_contexts" },
rpcraig63207cd2012-08-09 10:05:49 -0400866 { 0, NULL }
867};
868
869struct selabel_handle* selinux_android_prop_context_handle(void)
870{
Robert Craig03daf052014-03-17 21:16:53 -0400871 int policy_index = selinux_android_use_data_policy() ? 1 : 0;
872 struct selabel_handle* sehandle = selabel_open(SELABEL_CTX_ANDROID_PROP,
873 &seopts_prop[policy_index], 1);
rpcraig63207cd2012-08-09 10:05:49 -0400874 if (!sehandle) {
875 ERROR("SELinux: Could not load property_contexts: %s\n",
876 strerror(errno));
877 return NULL;
878 }
Robert Craig03daf052014-03-17 21:16:53 -0400879 INFO("SELinux: Loaded property contexts from %s\n", seopts_prop[policy_index].value);
rpcraig63207cd2012-08-09 10:05:49 -0400880 return sehandle;
881}
882
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400883void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500884{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400885 sehandle = selinux_android_file_context_handle();
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500886 selinux_android_set_sehandle(sehandle);
rpcraig63207cd2012-08-09 10:05:49 -0400887 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400888}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500889
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700890static bool selinux_is_disabled(void)
891{
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700892#ifdef ALLOW_DISABLE_SELINUX
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700893 char tmp[PROP_VALUE_MAX];
894
895 if (access("/sys/fs/selinux", F_OK) != 0) {
896 /* SELinux is not compiled into the kernel, or has been disabled
897 * via the kernel command line "selinux=0".
898 */
899 return true;
900 }
901
902 if ((property_get("ro.boot.selinux", tmp) != 0) && (strcmp(tmp, "disabled") == 0)) {
903 /* SELinux is compiled into the kernel, but we've been told to disable it. */
904 return true;
905 }
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700906#endif
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700907
908 return false;
909}
910
911static bool selinux_is_enforcing(void)
912{
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700913#ifdef ALLOW_DISABLE_SELINUX
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700914 char tmp[PROP_VALUE_MAX];
915
916 if (property_get("ro.boot.selinux", tmp) == 0) {
917 /* Property is not set. Assume enforcing */
918 return true;
919 }
920
921 if (strcmp(tmp, "permissive") == 0) {
922 /* SELinux is in the kernel, but we've been told to go into permissive mode */
923 return false;
924 }
925
926 if (strcmp(tmp, "enforcing") != 0) {
927 ERROR("SELinux: Unknown value of ro.boot.selinux. Got: \"%s\". Assuming enforcing.\n", tmp);
928 }
929
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700930#endif
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700931 return true;
932}
933
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400934int selinux_reload_policy(void)
935{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700936 if (selinux_is_disabled()) {
937 return -1;
938 }
939
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400940 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500941
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400942 if (selinux_android_reload_policy() == -1) {
943 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500944 }
945
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400946 if (sehandle)
947 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500948
rpcraig63207cd2012-08-09 10:05:49 -0400949 if (sehandle_prop)
950 selabel_close(sehandle_prop);
951
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400952 selinux_init_all_handles();
953 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500954}
rpcraig63207cd2012-08-09 10:05:49 -0400955
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500956static int audit_callback(void *data, security_class_t cls __attribute__((unused)), char *buf, size_t len)
rpcraig63207cd2012-08-09 10:05:49 -0400957{
958 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
959 return 0;
960}
961
Stephen Smalley439224e2014-06-24 13:45:43 -0400962int log_callback(int type, const char *fmt, ...)
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500963{
964 int level;
965 va_list ap;
966 switch (type) {
967 case SELINUX_WARNING:
968 level = KLOG_WARNING_LEVEL;
969 break;
970 case SELINUX_INFO:
971 level = KLOG_INFO_LEVEL;
972 break;
973 default:
974 level = KLOG_ERROR_LEVEL;
975 break;
976 }
977 va_start(ap, fmt);
978 klog_vwrite(level, fmt, ap);
979 va_end(ap);
980 return 0;
981}
982
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700983static void selinux_initialize(void)
984{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700985 if (selinux_is_disabled()) {
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700986 return;
987 }
988
989 INFO("loading selinux policy\n");
990 if (selinux_android_load_policy() < 0) {
991 ERROR("SELinux: Failed to load policy; rebooting into recovery mode\n");
992 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
993 while (1) { pause(); } // never reached
994 }
995
996 selinux_init_all_handles();
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700997 bool is_enforcing = selinux_is_enforcing();
998 INFO("SELinux: security_setenforce(%d)\n", is_enforcing);
999 security_setenforce(is_enforcing);
Nick Kralevich56fa0ac2013-06-24 17:41:40 -07001000}
1001
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001002int main(int argc, char **argv)
1003{
Colin Crossebc6ff12010-04-13 19:52:01 -07001004 int fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001005 struct pollfd ufds[4];
1006 char *tmpdev;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001007 char* debuggable;
Colin Crossebc6ff12010-04-13 19:52:01 -07001008 char tmp[32];
Colin Crossebc6ff12010-04-13 19:52:01 -07001009 int property_set_fd_init = 0;
1010 int signal_fd_init = 0;
1011 int keychord_fd_init = 0;
Dima Zavind7634c92011-12-16 14:18:06 -08001012 bool is_charger = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001013
Colin Crossf83d0b92010-04-21 12:04:20 -07001014 if (!strcmp(basename(argv[0]), "ueventd"))
1015 return ueventd_main(argc, argv);
1016
Arve Hjønnevågd97d9072012-06-13 21:51:56 -07001017 if (!strcmp(basename(argv[0]), "watchdogd"))
1018 return watchdogd_main(argc, argv);
1019
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001020 /* clear the umask */
1021 umask(0);
1022
1023 /* Get the basic filesystem setup we need put
1024 * together in the initramdisk on / and then we'll
1025 * let the rc file figure out the rest.
1026 */
1027 mkdir("/dev", 0755);
1028 mkdir("/proc", 0755);
1029 mkdir("/sys", 0755);
1030
Nick Kralevich150f19e2010-06-22 16:35:43 -07001031 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001032 mkdir("/dev/pts", 0755);
1033 mkdir("/dev/socket", 0755);
1034 mount("devpts", "/dev/pts", "devpts", 0, NULL);
1035 mount("proc", "/proc", "proc", 0, NULL);
1036 mount("sysfs", "/sys", "sysfs", 0, NULL);
1037
Brian Swetland8d48c8e2011-03-24 15:45:30 -07001038 /* indicate that booting is in progress to background fw loaders, etc */
1039 close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000));
1040
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001041 /* We must have some place other than / to create the
1042 * device nodes for kmsg and null, otherwise we won't
1043 * be able to remount / read-only later on.
1044 * Now that tmpfs is mounted on /dev, we can actually
1045 * talk to the outside world.
1046 */
1047 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -07001048 klog_init();
Dima Zavin5511c842011-12-19 11:21:32 -08001049 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -07001050
Colin Crossf83d0b92010-04-21 12:04:20 -07001051 get_hardware_name(hardware, &revision);
Dima Zavind7634c92011-12-16 14:18:06 -08001052
Dima Zavin5511c842011-12-19 11:21:32 -08001053 process_kernel_cmdline();
1054
rpcraig63207cd2012-08-09 10:05:49 -04001055 union selinux_callback cb;
Stephen Smalleyeb3f4212014-02-12 16:17:00 -05001056 cb.func_log = log_callback;
rpcraig63207cd2012-08-09 10:05:49 -04001057 selinux_set_callback(SELINUX_CB_LOG, cb);
1058
1059 cb.func_audit = audit_callback;
1060 selinux_set_callback(SELINUX_CB_AUDIT, cb);
1061
Nick Kralevich56fa0ac2013-06-24 17:41:40 -07001062 selinux_initialize();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -04001063 /* These directories were necessarily created before initial policy load
Stephen Smalleye096e362012-06-11 13:37:39 -04001064 * and therefore need their security context restored to the proper value.
1065 * This must happen before /dev is populated by ueventd.
1066 */
1067 restorecon("/dev");
1068 restorecon("/dev/socket");
Geremy Condra8e15eab2013-02-28 17:29:58 -08001069 restorecon("/dev/__properties__");
Nick Kralevichae76f6d2013-07-11 15:38:26 -07001070 restorecon_recursive("/sys");
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001071
Dima Zavind7634c92011-12-16 14:18:06 -08001072 is_charger = !strcmp(bootmode, "charger");
1073
1074 INFO("property init\n");
Riley Andrewse4b7b292014-06-16 15:06:21 -07001075 property_load_boot_defaults();
Dima Zavind7634c92011-12-16 14:18:06 -08001076
1077 INFO("reading config file\n");
1078 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001079
1080 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001081
Colin Crossf83d0b92010-04-21 12:04:20 -07001082 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Alex Klyubin0d872d82013-08-16 13:19:24 -07001083 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001084 queue_builtin_action(keychord_init_action, "keychord_init");
1085 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001086
Dima Zavinca47cef2011-08-24 15:28:23 -07001087 /* execute all the boot actions to get us started */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001088 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001089
Alex Klyubin0d872d82013-08-16 13:19:24 -07001090 /* Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
1091 * wasn't ready immediately after wait_for_coldboot_done
1092 */
1093 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001094 queue_builtin_action(property_service_init_action, "property_service_init");
1095 queue_builtin_action(signal_init_action, "signal_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001096
Riley Andrewse4b7b292014-06-16 15:06:21 -07001097 /* Don't mount filesystems or start core system services if in charger mode. */
Dima Zavind7634c92011-12-16 14:18:06 -08001098 if (is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -07001099 action_for_each_trigger("charger", action_add_queue_tail);
1100 } else {
Riley Andrewse4b7b292014-06-16 15:06:21 -07001101 action_for_each_trigger("late-init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001102 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001103
Riley Andrews9464e5a2014-07-11 15:05:23 -07001104 /* run all property triggers based on current state of the properties */
Chris Dearman469b7b22012-03-01 15:29:20 -08001105 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001106
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001107
1108#if BOOTCHART
Colin Crossebc6ff12010-04-13 19:52:01 -07001109 queue_builtin_action(bootchart_init_action, "bootchart_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001110#endif
1111
1112 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001113 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001114
Colin Crossebc6ff12010-04-13 19:52:01 -07001115 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001116 restart_processes();
1117
Colin Crossebc6ff12010-04-13 19:52:01 -07001118 if (!property_set_fd_init && get_property_set_fd() > 0) {
1119 ufds[fd_count].fd = get_property_set_fd();
1120 ufds[fd_count].events = POLLIN;
1121 ufds[fd_count].revents = 0;
1122 fd_count++;
1123 property_set_fd_init = 1;
1124 }
1125 if (!signal_fd_init && get_signal_fd() > 0) {
1126 ufds[fd_count].fd = get_signal_fd();
1127 ufds[fd_count].events = POLLIN;
1128 ufds[fd_count].revents = 0;
1129 fd_count++;
1130 signal_fd_init = 1;
1131 }
1132 if (!keychord_fd_init && get_keychord_fd() > 0) {
1133 ufds[fd_count].fd = get_keychord_fd();
1134 ufds[fd_count].events = POLLIN;
1135 ufds[fd_count].revents = 0;
1136 fd_count++;
1137 keychord_fd_init = 1;
1138 }
1139
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001140 if (process_needs_restart) {
1141 timeout = (process_needs_restart - gettime()) * 1000;
1142 if (timeout < 0)
1143 timeout = 0;
1144 }
1145
Colin Crossebd46132010-04-22 11:52:23 -07001146 if (!action_queue_empty() || cur_action)
Colin Crossebc6ff12010-04-13 19:52:01 -07001147 timeout = 0;
1148
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001149#if BOOTCHART
1150 if (bootchart_count > 0) {
Bo (Andover) Zhang37003732014-07-24 13:11:35 -04001151 long long current_time;
1152 int elapsed_time, remaining_time;
1153
1154 current_time = bootchart_gettime();
1155 elapsed_time = current_time - bootchart_time;
1156
1157 if (elapsed_time >= BOOTCHART_POLLING_MS) {
1158 /* count missed samples */
1159 while (elapsed_time >= BOOTCHART_POLLING_MS) {
1160 elapsed_time -= BOOTCHART_POLLING_MS;
1161 bootchart_count--;
1162 }
1163 /* count may be negative, take a sample anyway */
1164 bootchart_time = current_time;
1165 if (bootchart_step() < 0 || bootchart_count <= 0) {
1166 bootchart_finish();
1167 bootchart_count = 0;
1168 }
1169 }
1170 if (bootchart_count > 0) {
1171 remaining_time = BOOTCHART_POLLING_MS - elapsed_time;
1172 if (timeout < 0 || timeout > remaining_time)
1173 timeout = remaining_time;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001174 }
1175 }
1176#endif
Colin Crossebc6ff12010-04-13 19:52:01 -07001177
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001178 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001179 if (nr <= 0)
1180 continue;
1181
Colin Crossebc6ff12010-04-13 19:52:01 -07001182 for (i = 0; i < fd_count; i++) {
Amir Goldstein1d4e86c2013-11-10 15:36:58 +02001183 if (ufds[i].revents & POLLIN) {
Colin Crossf83d0b92010-04-21 12:04:20 -07001184 if (ufds[i].fd == get_property_set_fd())
Colin Crossebc6ff12010-04-13 19:52:01 -07001185 handle_property_set_fd();
1186 else if (ufds[i].fd == get_keychord_fd())
1187 handle_keychord();
1188 else if (ufds[i].fd == get_signal_fd())
1189 handle_signal();
1190 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001191 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001192 }
1193
1194 return 0;
1195}