blob: 9d7bf27afc8a62642caa79acd509a822044d8097 [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
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700545
546void build_triggers_string(char *name_str, int length, struct action *cur_action) {
547 struct listnode *node;
548 struct trigger *cur_trigger;
549
550 list_for_each(node, &cur_action->triggers) {
551 cur_trigger = node_to_item(node, struct trigger, nlist);
552 if (node != cur_action->triggers.next) {
553 strlcat(name_str, " " , length);
554 }
555 strlcat(name_str, cur_trigger->name , length);
556 }
557}
558
Colin Crossebc6ff12010-04-13 19:52:01 -0700559void execute_one_command(void)
560{
Riley Andrews24a3b782014-06-26 13:56:01 -0700561 int ret, i;
562 char cmd_str[256] = "";
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700563 char name_str[256] = "";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700564
Colin Crossebc6ff12010-04-13 19:52:01 -0700565 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
566 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700567 cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -0700568 if (!cur_action)
569 return;
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700570
571 build_triggers_string(name_str, sizeof(name_str), cur_action);
572
573 INFO("processing action %p (%s)\n", cur_action, name_str);
Colin Crossebc6ff12010-04-13 19:52:01 -0700574 cur_command = get_first_command(cur_action);
575 } else {
576 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700577 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700578
579 if (!cur_command)
580 return;
581
582 ret = cur_command->func(cur_command->nargs, cur_command->args);
Riley Andrews24a3b782014-06-26 13:56:01 -0700583 if (klog_get_level() >= KLOG_INFO_LEVEL) {
584 for (i = 0; i < cur_command->nargs; i++) {
585 strlcat(cmd_str, cur_command->args[i], sizeof(cmd_str));
586 if (i < cur_command->nargs - 1) {
587 strlcat(cmd_str, " ", sizeof(cmd_str));
588 }
589 }
590 INFO("command '%s' action=%s status=%d (%s:%d)\n",
Badhri Jagan Sridharan162f7d72014-10-10 23:19:06 -0700591 cmd_str, cur_action ? name_str : "", ret, cur_command->filename,
Riley Andrews24a3b782014-06-26 13:56:01 -0700592 cur_command->line);
593 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700594}
595
Colin Crossf83d0b92010-04-21 12:04:20 -0700596static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700597{
Colin Crossf83d0b92010-04-21 12:04:20 -0700598 int ret;
599 INFO("wait for %s\n", coldboot_done);
600 ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT);
601 if (ret)
602 ERROR("Timed out waiting for %s\n", coldboot_done);
603 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700604}
605
Alex Klyubin0d872d82013-08-16 13:19:24 -0700606/*
607 * Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed
608 * by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom.
609 * Does nothing if Hardware RNG is not present.
610 *
611 * Since we don't yet trust the quality of Hardware RNG, these bytes are not
612 * mixed into the primary pool of Linux RNG and the entropy estimate is left
613 * unmodified.
614 *
615 * If the HW RNG device /dev/hw_random is present, we require that at least
616 * 512 bytes read from it are written into Linux RNG. QA is expected to catch
617 * devices/configurations where these I/O operations are blocking for a long
618 * time. We do not reboot or halt on failures, as this is a best-effort
619 * attempt.
620 */
621static int mix_hwrng_into_linux_rng_action(int nargs, char **args)
622{
623 int result = -1;
624 int hwrandom_fd = -1;
625 int urandom_fd = -1;
626 char buf[512];
627 ssize_t chunk_size;
628 size_t total_bytes_written = 0;
629
630 hwrandom_fd = TEMP_FAILURE_RETRY(
631 open("/dev/hw_random", O_RDONLY | O_NOFOLLOW));
632 if (hwrandom_fd == -1) {
633 if (errno == ENOENT) {
634 ERROR("/dev/hw_random not found\n");
635 /* It's not an error to not have a Hardware RNG. */
636 result = 0;
637 } else {
638 ERROR("Failed to open /dev/hw_random: %s\n", strerror(errno));
639 }
640 goto ret;
641 }
642
643 urandom_fd = TEMP_FAILURE_RETRY(
644 open("/dev/urandom", O_WRONLY | O_NOFOLLOW));
645 if (urandom_fd == -1) {
646 ERROR("Failed to open /dev/urandom: %s\n", strerror(errno));
647 goto ret;
648 }
649
650 while (total_bytes_written < sizeof(buf)) {
651 chunk_size = TEMP_FAILURE_RETRY(
652 read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written));
653 if (chunk_size == -1) {
654 ERROR("Failed to read from /dev/hw_random: %s\n", strerror(errno));
655 goto ret;
656 } else if (chunk_size == 0) {
657 ERROR("Failed to read from /dev/hw_random: EOF\n");
658 goto ret;
659 }
660
661 chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size));
662 if (chunk_size == -1) {
663 ERROR("Failed to write to /dev/urandom: %s\n", strerror(errno));
664 goto ret;
665 }
666 total_bytes_written += chunk_size;
667 }
668
Elliott Hughesccecf142014-01-16 10:53:11 -0800669 INFO("Mixed %zu bytes from /dev/hw_random into /dev/urandom",
Alex Klyubin0d872d82013-08-16 13:19:24 -0700670 total_bytes_written);
671 result = 0;
672
673ret:
674 if (hwrandom_fd != -1) {
675 close(hwrandom_fd);
676 }
677 if (urandom_fd != -1) {
678 close(urandom_fd);
679 }
680 memset(buf, 0, sizeof(buf));
681 return result;
682}
683
Colin Crossebc6ff12010-04-13 19:52:01 -0700684static int keychord_init_action(int nargs, char **args)
685{
686 keychord_init();
687 return 0;
688}
689
690static int console_init_action(int nargs, char **args)
691{
692 int fd;
Colin Crossebc6ff12010-04-13 19:52:01 -0700693
694 if (console[0]) {
Hong-Mei Li11467182013-04-01 11:17:51 +0800695 snprintf(console_name, sizeof(console_name), "/dev/%s", console);
Colin Crossebc6ff12010-04-13 19:52:01 -0700696 }
697
698 fd = open(console_name, O_RDWR);
699 if (fd >= 0)
700 have_console = 1;
701 close(fd);
702
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200703 fd = open("/dev/tty0", O_WRONLY);
704 if (fd >= 0) {
705 const char *msg;
706 msg = "\n"
707 "\n"
708 "\n"
709 "\n"
710 "\n"
711 "\n"
712 "\n" // console is 40 cols x 30 lines
713 "\n"
714 "\n"
715 "\n"
716 "\n"
717 "\n"
718 "\n"
719 "\n"
720 " A N D R O I D ";
721 write(fd, msg, strlen(msg));
722 close(fd);
Colin Crossebc6ff12010-04-13 19:52:01 -0700723 }
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200724
Colin Crossebc6ff12010-04-13 19:52:01 -0700725 return 0;
726}
727
Dima Zavin5511c842011-12-19 11:21:32 -0800728static void import_kernel_nv(char *name, int for_emulator)
729{
730 char *value = strchr(name, '=');
731 int name_len = strlen(name);
732
733 if (value == 0) return;
734 *value++ = 0;
735 if (name_len == 0) return;
736
737 if (for_emulator) {
738 /* in the emulator, export any kernel option with the
739 * ro.kernel. prefix */
740 char buff[PROP_NAME_MAX];
741 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
742
743 if (len < (int)sizeof(buff))
744 property_set( buff, value );
745 return;
746 }
747
748 if (!strcmp(name,"qemu")) {
749 strlcpy(qemu, value, sizeof(qemu));
750 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
751 const char *boot_prop_name = name + 12;
752 char prop[PROP_NAME_MAX];
753 int cnt;
754
755 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
756 if (cnt < PROP_NAME_MAX)
757 property_set(prop, value);
758 }
759}
760
761static void export_kernel_boot_props(void)
Colin Crossebc6ff12010-04-13 19:52:01 -0700762{
763 char tmp[PROP_VALUE_MAX];
Colin Cross1a6f4c32013-01-28 17:13:35 -0800764 int ret;
Dima Zavin5511c842011-12-19 11:21:32 -0800765 unsigned i;
766 struct {
767 const char *src_prop;
768 const char *dest_prop;
769 const char *def_val;
770 } prop_map[] = {
771 { "ro.boot.serialno", "ro.serialno", "", },
772 { "ro.boot.mode", "ro.bootmode", "unknown", },
773 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800774 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
775 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700776
Dima Zavin5511c842011-12-19 11:21:32 -0800777 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
Colin Cross1a6f4c32013-01-28 17:13:35 -0800778 ret = property_get(prop_map[i].src_prop, tmp);
Colin Cross5e484e92013-06-17 16:20:08 -0700779 if (ret > 0)
780 property_set(prop_map[i].dest_prop, tmp);
781 else
Colin Cross1a6f4c32013-01-28 17:13:35 -0800782 property_set(prop_map[i].dest_prop, prop_map[i].def_val);
Dima Zavin5511c842011-12-19 11:21:32 -0800783 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700784
Colin Cross1a6f4c32013-01-28 17:13:35 -0800785 ret = property_get("ro.boot.console", tmp);
786 if (ret)
787 strlcpy(console, tmp, sizeof(console));
Dima Zavin5511c842011-12-19 11:21:32 -0800788
789 /* save a copy for init's usage during boot */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800790 property_get("ro.bootmode", tmp);
791 strlcpy(bootmode, tmp, sizeof(bootmode));
Dima Zavin5511c842011-12-19 11:21:32 -0800792
793 /* if this was given on kernel command line, override what we read
794 * before (e.g. from /proc/cpuinfo), if anything */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800795 ret = property_get("ro.boot.hardware", tmp);
796 if (ret)
797 strlcpy(hardware, tmp, sizeof(hardware));
Dima Zavin5511c842011-12-19 11:21:32 -0800798 property_set("ro.hardware", hardware);
799
800 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
801 property_set("ro.revision", tmp);
802
803 /* TODO: these are obsolete. We should delete them */
Colin Crossebc6ff12010-04-13 19:52:01 -0700804 if (!strcmp(bootmode,"factory"))
805 property_set("ro.factorytest", "1");
806 else if (!strcmp(bootmode,"factory2"))
807 property_set("ro.factorytest", "2");
808 else
809 property_set("ro.factorytest", "0");
Dima Zavin5511c842011-12-19 11:21:32 -0800810}
Colin Crossebc6ff12010-04-13 19:52:01 -0700811
Dima Zavin5511c842011-12-19 11:21:32 -0800812static void process_kernel_cmdline(void)
813{
814 /* don't expose the raw commandline to nonpriv processes */
815 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700816
Dima Zavin5511c842011-12-19 11:21:32 -0800817 /* first pass does the common stuff, and finds if we are in qemu.
818 * second pass is only necessary for qemu to export all kernel params
819 * as props.
820 */
821 import_kernel_cmdline(0, import_kernel_nv);
822 if (qemu[0])
823 import_kernel_cmdline(1, import_kernel_nv);
824
825 /* now propogate the info given on command line to internal variables
826 * used by init as well as the current required properties
827 */
828 export_kernel_boot_props();
Colin Crossebc6ff12010-04-13 19:52:01 -0700829}
830
831static int property_service_init_action(int nargs, char **args)
832{
833 /* read any property files on system or data and
834 * fire up the property service. This must happen
835 * after the ro.foo properties are set above so
836 * that /data/local.prop cannot interfere with them.
837 */
838 start_property_service();
Riley Andrews9464e5a2014-07-11 15:05:23 -0700839 if (get_property_set_fd() < 0) {
840 ERROR("start_property_service() failed\n");
841 exit(1);
842 }
843
Colin Crossebc6ff12010-04-13 19:52:01 -0700844 return 0;
845}
846
847static int signal_init_action(int nargs, char **args)
848{
849 signal_init();
Riley Andrews9464e5a2014-07-11 15:05:23 -0700850 if (get_signal_fd() < 0) {
851 ERROR("signal_init() failed\n");
Colin Crossebc6ff12010-04-13 19:52:01 -0700852 exit(1);
853 }
854 return 0;
855}
856
857static int queue_property_triggers_action(int nargs, char **args)
858{
859 queue_all_property_triggers();
860 /* enable property triggers */
861 property_triggers_enabled = 1;
862 return 0;
863}
864
865#if BOOTCHART
866static int bootchart_init_action(int nargs, char **args)
867{
868 bootchart_count = bootchart_init();
869 if (bootchart_count < 0) {
870 ERROR("bootcharting init failure\n");
871 } else if (bootchart_count > 0) {
872 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
873 } else {
874 NOTICE("bootcharting ignored\n");
875 }
Carl-Emil Lagerstedt9ab81902011-01-14 09:35:30 +0100876
877 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700878}
879#endif
880
rpcraig63207cd2012-08-09 10:05:49 -0400881static const struct selinux_opt seopts_prop[] = {
rpcraig63207cd2012-08-09 10:05:49 -0400882 { SELABEL_OPT_PATH, "/property_contexts" },
Robert Craig03daf052014-03-17 21:16:53 -0400883 { SELABEL_OPT_PATH, "/data/security/current/property_contexts" },
rpcraig63207cd2012-08-09 10:05:49 -0400884 { 0, NULL }
885};
886
887struct selabel_handle* selinux_android_prop_context_handle(void)
888{
Robert Craig03daf052014-03-17 21:16:53 -0400889 int policy_index = selinux_android_use_data_policy() ? 1 : 0;
890 struct selabel_handle* sehandle = selabel_open(SELABEL_CTX_ANDROID_PROP,
891 &seopts_prop[policy_index], 1);
rpcraig63207cd2012-08-09 10:05:49 -0400892 if (!sehandle) {
893 ERROR("SELinux: Could not load property_contexts: %s\n",
894 strerror(errno));
895 return NULL;
896 }
Robert Craig03daf052014-03-17 21:16:53 -0400897 INFO("SELinux: Loaded property contexts from %s\n", seopts_prop[policy_index].value);
rpcraig63207cd2012-08-09 10:05:49 -0400898 return sehandle;
899}
900
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400901void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500902{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400903 sehandle = selinux_android_file_context_handle();
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500904 selinux_android_set_sehandle(sehandle);
rpcraig63207cd2012-08-09 10:05:49 -0400905 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400906}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500907
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700908static bool selinux_is_disabled(void)
909{
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700910#ifdef ALLOW_DISABLE_SELINUX
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700911 char tmp[PROP_VALUE_MAX];
912
913 if (access("/sys/fs/selinux", F_OK) != 0) {
914 /* SELinux is not compiled into the kernel, or has been disabled
915 * via the kernel command line "selinux=0".
916 */
917 return true;
918 }
919
920 if ((property_get("ro.boot.selinux", tmp) != 0) && (strcmp(tmp, "disabled") == 0)) {
921 /* SELinux is compiled into the kernel, but we've been told to disable it. */
922 return true;
923 }
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700924#endif
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700925
926 return false;
927}
928
929static bool selinux_is_enforcing(void)
930{
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700931#ifdef ALLOW_DISABLE_SELINUX
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700932 char tmp[PROP_VALUE_MAX];
933
934 if (property_get("ro.boot.selinux", tmp) == 0) {
935 /* Property is not set. Assume enforcing */
936 return true;
937 }
938
939 if (strcmp(tmp, "permissive") == 0) {
940 /* SELinux is in the kernel, but we've been told to go into permissive mode */
941 return false;
942 }
943
944 if (strcmp(tmp, "enforcing") != 0) {
945 ERROR("SELinux: Unknown value of ro.boot.selinux. Got: \"%s\". Assuming enforcing.\n", tmp);
946 }
947
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700948#endif
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700949 return true;
950}
951
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400952int selinux_reload_policy(void)
953{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700954 if (selinux_is_disabled()) {
955 return -1;
956 }
957
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400958 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500959
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400960 if (selinux_android_reload_policy() == -1) {
961 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500962 }
963
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400964 if (sehandle)
965 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500966
rpcraig63207cd2012-08-09 10:05:49 -0400967 if (sehandle_prop)
968 selabel_close(sehandle_prop);
969
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400970 selinux_init_all_handles();
971 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500972}
rpcraig63207cd2012-08-09 10:05:49 -0400973
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500974static int audit_callback(void *data, security_class_t cls __attribute__((unused)), char *buf, size_t len)
rpcraig63207cd2012-08-09 10:05:49 -0400975{
976 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
977 return 0;
978}
979
Stephen Smalley439224e2014-06-24 13:45:43 -0400980int log_callback(int type, const char *fmt, ...)
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500981{
982 int level;
983 va_list ap;
984 switch (type) {
985 case SELINUX_WARNING:
986 level = KLOG_WARNING_LEVEL;
987 break;
988 case SELINUX_INFO:
989 level = KLOG_INFO_LEVEL;
990 break;
991 default:
992 level = KLOG_ERROR_LEVEL;
993 break;
994 }
995 va_start(ap, fmt);
996 klog_vwrite(level, fmt, ap);
997 va_end(ap);
998 return 0;
999}
1000
Nick Kralevich56fa0ac2013-06-24 17:41:40 -07001001static void selinux_initialize(void)
1002{
Nick Kralevich935bd3e2013-06-26 15:37:26 -07001003 if (selinux_is_disabled()) {
Nick Kralevich56fa0ac2013-06-24 17:41:40 -07001004 return;
1005 }
1006
1007 INFO("loading selinux policy\n");
1008 if (selinux_android_load_policy() < 0) {
1009 ERROR("SELinux: Failed to load policy; rebooting into recovery mode\n");
1010 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
1011 while (1) { pause(); } // never reached
1012 }
1013
1014 selinux_init_all_handles();
Nick Kralevich935bd3e2013-06-26 15:37:26 -07001015 bool is_enforcing = selinux_is_enforcing();
1016 INFO("SELinux: security_setenforce(%d)\n", is_enforcing);
1017 security_setenforce(is_enforcing);
Nick Kralevich56fa0ac2013-06-24 17:41:40 -07001018}
1019
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001020int main(int argc, char **argv)
1021{
Colin Crossebc6ff12010-04-13 19:52:01 -07001022 int fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001023 struct pollfd ufds[4];
1024 char *tmpdev;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001025 char* debuggable;
Colin Crossebc6ff12010-04-13 19:52:01 -07001026 char tmp[32];
Colin Crossebc6ff12010-04-13 19:52:01 -07001027 int property_set_fd_init = 0;
1028 int signal_fd_init = 0;
1029 int keychord_fd_init = 0;
Dima Zavind7634c92011-12-16 14:18:06 -08001030 bool is_charger = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001031
Colin Crossf83d0b92010-04-21 12:04:20 -07001032 if (!strcmp(basename(argv[0]), "ueventd"))
1033 return ueventd_main(argc, argv);
1034
Arve Hjønnevågd97d9072012-06-13 21:51:56 -07001035 if (!strcmp(basename(argv[0]), "watchdogd"))
1036 return watchdogd_main(argc, argv);
1037
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001038 /* clear the umask */
1039 umask(0);
1040
1041 /* Get the basic filesystem setup we need put
1042 * together in the initramdisk on / and then we'll
1043 * let the rc file figure out the rest.
1044 */
1045 mkdir("/dev", 0755);
1046 mkdir("/proc", 0755);
1047 mkdir("/sys", 0755);
1048
Nick Kralevich150f19e2010-06-22 16:35:43 -07001049 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001050 mkdir("/dev/pts", 0755);
1051 mkdir("/dev/socket", 0755);
1052 mount("devpts", "/dev/pts", "devpts", 0, NULL);
1053 mount("proc", "/proc", "proc", 0, NULL);
1054 mount("sysfs", "/sys", "sysfs", 0, NULL);
1055
Brian Swetland8d48c8e2011-03-24 15:45:30 -07001056 /* indicate that booting is in progress to background fw loaders, etc */
1057 close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000));
1058
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001059 /* We must have some place other than / to create the
1060 * device nodes for kmsg and null, otherwise we won't
1061 * be able to remount / read-only later on.
1062 * Now that tmpfs is mounted on /dev, we can actually
1063 * talk to the outside world.
1064 */
1065 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -07001066 klog_init();
Dima Zavin5511c842011-12-19 11:21:32 -08001067 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -07001068
Colin Crossf83d0b92010-04-21 12:04:20 -07001069 get_hardware_name(hardware, &revision);
Dima Zavind7634c92011-12-16 14:18:06 -08001070
Dima Zavin5511c842011-12-19 11:21:32 -08001071 process_kernel_cmdline();
1072
rpcraig63207cd2012-08-09 10:05:49 -04001073 union selinux_callback cb;
Stephen Smalleyeb3f4212014-02-12 16:17:00 -05001074 cb.func_log = log_callback;
rpcraig63207cd2012-08-09 10:05:49 -04001075 selinux_set_callback(SELINUX_CB_LOG, cb);
1076
1077 cb.func_audit = audit_callback;
1078 selinux_set_callback(SELINUX_CB_AUDIT, cb);
1079
Nick Kralevich56fa0ac2013-06-24 17:41:40 -07001080 selinux_initialize();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -04001081 /* These directories were necessarily created before initial policy load
Stephen Smalleye096e362012-06-11 13:37:39 -04001082 * and therefore need their security context restored to the proper value.
1083 * This must happen before /dev is populated by ueventd.
1084 */
1085 restorecon("/dev");
1086 restorecon("/dev/socket");
Geremy Condra8e15eab2013-02-28 17:29:58 -08001087 restorecon("/dev/__properties__");
Nick Kralevichae76f6d2013-07-11 15:38:26 -07001088 restorecon_recursive("/sys");
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001089
Dima Zavind7634c92011-12-16 14:18:06 -08001090 is_charger = !strcmp(bootmode, "charger");
1091
1092 INFO("property init\n");
Riley Andrewse4b7b292014-06-16 15:06:21 -07001093 property_load_boot_defaults();
Dima Zavind7634c92011-12-16 14:18:06 -08001094
1095 INFO("reading config file\n");
1096 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001097
1098 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001099
Colin Crossf83d0b92010-04-21 12:04:20 -07001100 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Alex Klyubin0d872d82013-08-16 13:19:24 -07001101 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001102 queue_builtin_action(keychord_init_action, "keychord_init");
1103 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001104
Dima Zavinca47cef2011-08-24 15:28:23 -07001105 /* execute all the boot actions to get us started */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001106 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001107
Alex Klyubin0d872d82013-08-16 13:19:24 -07001108 /* Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
1109 * wasn't ready immediately after wait_for_coldboot_done
1110 */
1111 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001112 queue_builtin_action(property_service_init_action, "property_service_init");
1113 queue_builtin_action(signal_init_action, "signal_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001114
Riley Andrewse4b7b292014-06-16 15:06:21 -07001115 /* Don't mount filesystems or start core system services if in charger mode. */
Dima Zavind7634c92011-12-16 14:18:06 -08001116 if (is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -07001117 action_for_each_trigger("charger", action_add_queue_tail);
1118 } else {
Riley Andrewse4b7b292014-06-16 15:06:21 -07001119 action_for_each_trigger("late-init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001120 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001121
Riley Andrews9464e5a2014-07-11 15:05:23 -07001122 /* run all property triggers based on current state of the properties */
Chris Dearman469b7b22012-03-01 15:29:20 -08001123 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001124
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001125
1126#if BOOTCHART
Colin Crossebc6ff12010-04-13 19:52:01 -07001127 queue_builtin_action(bootchart_init_action, "bootchart_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001128#endif
1129
1130 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001131 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001132
Colin Crossebc6ff12010-04-13 19:52:01 -07001133 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001134 restart_processes();
1135
Colin Crossebc6ff12010-04-13 19:52:01 -07001136 if (!property_set_fd_init && get_property_set_fd() > 0) {
1137 ufds[fd_count].fd = get_property_set_fd();
1138 ufds[fd_count].events = POLLIN;
1139 ufds[fd_count].revents = 0;
1140 fd_count++;
1141 property_set_fd_init = 1;
1142 }
1143 if (!signal_fd_init && get_signal_fd() > 0) {
1144 ufds[fd_count].fd = get_signal_fd();
1145 ufds[fd_count].events = POLLIN;
1146 ufds[fd_count].revents = 0;
1147 fd_count++;
1148 signal_fd_init = 1;
1149 }
1150 if (!keychord_fd_init && get_keychord_fd() > 0) {
1151 ufds[fd_count].fd = get_keychord_fd();
1152 ufds[fd_count].events = POLLIN;
1153 ufds[fd_count].revents = 0;
1154 fd_count++;
1155 keychord_fd_init = 1;
1156 }
1157
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001158 if (process_needs_restart) {
1159 timeout = (process_needs_restart - gettime()) * 1000;
1160 if (timeout < 0)
1161 timeout = 0;
1162 }
1163
Colin Crossebd46132010-04-22 11:52:23 -07001164 if (!action_queue_empty() || cur_action)
Colin Crossebc6ff12010-04-13 19:52:01 -07001165 timeout = 0;
1166
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001167#if BOOTCHART
1168 if (bootchart_count > 0) {
Bo (Andover) Zhang37003732014-07-24 13:11:35 -04001169 long long current_time;
1170 int elapsed_time, remaining_time;
1171
1172 current_time = bootchart_gettime();
1173 elapsed_time = current_time - bootchart_time;
1174
1175 if (elapsed_time >= BOOTCHART_POLLING_MS) {
1176 /* count missed samples */
1177 while (elapsed_time >= BOOTCHART_POLLING_MS) {
1178 elapsed_time -= BOOTCHART_POLLING_MS;
1179 bootchart_count--;
1180 }
1181 /* count may be negative, take a sample anyway */
1182 bootchart_time = current_time;
1183 if (bootchart_step() < 0 || bootchart_count <= 0) {
1184 bootchart_finish();
1185 bootchart_count = 0;
1186 }
1187 }
1188 if (bootchart_count > 0) {
1189 remaining_time = BOOTCHART_POLLING_MS - elapsed_time;
1190 if (timeout < 0 || timeout > remaining_time)
1191 timeout = remaining_time;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001192 }
1193 }
1194#endif
Colin Crossebc6ff12010-04-13 19:52:01 -07001195
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001196 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001197 if (nr <= 0)
1198 continue;
1199
Colin Crossebc6ff12010-04-13 19:52:01 -07001200 for (i = 0; i < fd_count; i++) {
Amir Goldstein1d4e86c2013-11-10 15:36:58 +02001201 if (ufds[i].revents & POLLIN) {
Colin Crossf83d0b92010-04-21 12:04:20 -07001202 if (ufds[i].fd == get_property_set_fd())
Colin Crossebc6ff12010-04-13 19:52:01 -07001203 handle_property_set_fd();
1204 else if (ufds[i].fd == get_keychord_fd())
1205 handle_keychord();
1206 else if (ufds[i].fd == get_signal_fd())
1207 handle_signal();
1208 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001209 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001210 }
1211
1212 return 0;
1213}