blob: 579d0e702b1b05b8658c1eae5d2df3856dd7d1c3 [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
Elliott Hughes8d82ea02015-02-06 20:15:18 -080017#include <ctype.h>
18#include <errno.h>
19#include <fcntl.h>
20#include <libgen.h>
21#include <signal.h>
22#include <stdarg.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070026#include <sys/mount.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070027#include <sys/poll.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070028#include <sys/socket.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080029#include <sys/stat.h>
30#include <sys/types.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070031#include <sys/un.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080032#include <sys/wait.h>
33#include <termios.h>
34#include <unistd.h>
35
36#include <mtd/mtd-user.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050037
Stephen Smalleye46f9d52012-01-13 08:48:47 -050038#include <selinux/selinux.h>
39#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040040#include <selinux/android.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050041
Nick Kralevich56fa0ac2013-06-24 17:41:40 -070042#include <cutils/android_reboot.h>
Alex Klyubin0d872d82013-08-16 13:19:24 -070043#include <cutils/fs.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080044#include <cutils/iosched_policy.h>
45#include <cutils/list.h>
46#include <cutils/sockets.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070047#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070048
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070049#include "devices.h"
50#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070051#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070052#include "property_service.h"
The Android Open Source Project35237d12008-12-17 18:08:08 -080053#include "bootchart.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070054#include "signal_handler.h"
Colin Crossa8666952010-04-13 19:20:44 -070055#include "keychords.h"
Colin Cross6310a822010-04-20 14:29:05 -070056#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070057#include "util.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070058#include "ueventd.h"
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070059#include "watchdogd.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070060
Stephen Smalleye46f9d52012-01-13 08:48:47 -050061struct selabel_handle *sehandle;
rpcraig63207cd2012-08-09 10:05:49 -040062struct selabel_handle *sehandle_prop;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050063
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070064static int property_triggers_enabled = 0;
65
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070066static char console[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070067static char bootmode[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068static char hardware[32];
69static unsigned revision = 0;
70static char qemu[32];
71
Colin Crossebc6ff12010-04-13 19:52:01 -070072static struct action *cur_action = NULL;
73static struct command *cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -070074
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075static int have_console;
Hong-Mei Li11467182013-04-01 11:17:51 +080076static char console_name[PROP_VALUE_MAX] = "/dev/console";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070077static time_t process_needs_restart;
78
79static const char *ENV[32];
80
Elliott Hughes8d82ea02015-02-06 20:15:18 -080081bool waiting_for_exec = false;
82
83void service::NotifyStateChange(const char* new_state) {
84 if (!properties_inited()) {
85 // If properties aren't available yet, we can't set them.
86 return;
87 }
88
89 if ((flags & SVC_EXEC) != 0) {
90 // 'exec' commands don't have properties tracking their state.
91 return;
92 }
93
94 char prop_name[PROP_NAME_MAX];
95 if (snprintf(prop_name, sizeof(prop_name), "init.svc.%s", name) >= PROP_NAME_MAX) {
96 // If the property name would be too long, we can't set it.
97 ERROR("Property name \"init.svc.%s\" too long; not setting to %s\n", name, new_state);
98 return;
99 }
100
101 property_set(prop_name, new_state);
102}
103
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700104/* add_environment - add "key=value" to the current environment */
105int add_environment(const char *key, const char *val)
106{
James Morrissey381341f2014-05-16 11:36:36 +0100107 size_t n;
108 size_t key_len = strlen(key);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700109
James Morrissey381341f2014-05-16 11:36:36 +0100110 /* The last environment entry is reserved to terminate the list */
111 for (n = 0; n < (ARRAY_SIZE(ENV) - 1); n++) {
112
113 /* Delete any existing entry for this key */
114 if (ENV[n] != NULL) {
115 size_t entry_key_len = strcspn(ENV[n], "=");
116 if ((entry_key_len == key_len) && (strncmp(ENV[n], key, entry_key_len) == 0)) {
117 free((char*)ENV[n]);
118 ENV[n] = NULL;
119 }
120 }
121
122 /* Add entry if a free slot is available */
123 if (ENV[n] == NULL) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800124 char* entry;
125 asprintf(&entry, "%s=%s", key, val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700126 ENV[n] = entry;
127 return 0;
128 }
129 }
130
James Morrissey381341f2014-05-16 11:36:36 +0100131 ERROR("No env. room to store: '%s':'%s'\n", key, val);
132
133 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700134}
135
San Mehat429721c2014-09-23 07:48:47 -0700136void zap_stdio(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700137{
138 int fd;
139 fd = open("/dev/null", O_RDWR);
140 dup2(fd, 0);
141 dup2(fd, 1);
142 dup2(fd, 2);
143 close(fd);
144}
145
146static void open_console()
147{
148 int fd;
149 if ((fd = open(console_name, O_RDWR)) < 0) {
150 fd = open("/dev/null", O_RDWR);
151 }
Colin Cross50fb5a62012-03-18 15:38:19 -0700152 ioctl(fd, TIOCSCTTY, 0);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700153 dup2(fd, 0);
154 dup2(fd, 1);
155 dup2(fd, 2);
156 close(fd);
157}
158
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700159static void publish_socket(const char *name, int fd)
160{
161 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
162 char val[64];
163
164 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
165 name,
166 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
167 snprintf(val, sizeof(val), "%d", fd);
168 add_environment(key, val);
169
170 /* make sure we don't close-on-exec */
171 fcntl(fd, F_SETFD, 0);
172}
173
San Mehatf24e2522009-05-19 13:30:46 -0700174void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700175{
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800176 // Starting a service removes it from the disabled or reset state and
177 // immediately takes it out of the restarting state if it was in there.
JP Abgrall3beec7e2014-05-02 21:14:29 -0700178 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700179 svc->time_started = 0;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700180
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800181 // Running processes require no additional work --- if they're in the
182 // process of exiting, we've ensured that they will immediately restart
183 // on exit, unless they are ONESHOT.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700184 if (svc->flags & SVC_RUNNING) {
185 return;
186 }
187
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800188 bool needs_console = (svc->flags & SVC_CONSOLE);
189 if (needs_console && !have_console) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700190 ERROR("service '%s' requires console\n", svc->name);
191 svc->flags |= SVC_DISABLED;
192 return;
193 }
194
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800195 struct stat s;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700196 if (stat(svc->args[0], &s) != 0) {
197 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
198 svc->flags |= SVC_DISABLED;
199 return;
200 }
201
San Mehatf24e2522009-05-19 13:30:46 -0700202 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700203 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
204 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700205 svc->flags |= SVC_DISABLED;
206 return;
207 }
208
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800209 char* scon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500210 if (is_selinux_enabled() > 0) {
Stephen Smalley30f30332012-11-16 14:34:27 -0500211 if (svc->seclabel) {
212 scon = strdup(svc->seclabel);
213 if (!scon) {
214 ERROR("Out of memory while starting '%s'\n", svc->name);
215 return;
216 }
217 } else {
218 char *mycon = NULL, *fcon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500219
Stephen Smalley30f30332012-11-16 14:34:27 -0500220 INFO("computing context for service '%s'\n", svc->args[0]);
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800221 int rc = getcon(&mycon);
Stephen Smalley30f30332012-11-16 14:34:27 -0500222 if (rc < 0) {
223 ERROR("could not get context while starting '%s'\n", svc->name);
224 return;
225 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500226
Stephen Smalley30f30332012-11-16 14:34:27 -0500227 rc = getfilecon(svc->args[0], &fcon);
228 if (rc < 0) {
229 ERROR("could not get context while starting '%s'\n", svc->name);
230 freecon(mycon);
231 return;
232 }
233
234 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon);
Stephen Smalleyaf06c672013-12-09 15:40:24 -0500235 if (rc == 0 && !strcmp(scon, mycon)) {
236 ERROR("Warning! Service %s needs a SELinux domain defined; please fix!\n", svc->name);
237 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500238 freecon(mycon);
Stephen Smalley30f30332012-11-16 14:34:27 -0500239 freecon(fcon);
240 if (rc < 0) {
241 ERROR("could not get context while starting '%s'\n", svc->name);
242 return;
243 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500244 }
245 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500246
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700247 NOTICE("starting '%s'\n", svc->name);
248
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800249 pid_t pid = fork();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700250 if (pid == 0) {
251 struct socketinfo *si;
252 struct svcenvinfo *ei;
253 char tmp[32];
254 int fd, sz;
255
Nick Kralevich6ebf12f2012-03-26 09:09:11 -0700256 umask(077);
Colin Cross3294bbb2010-04-19 17:11:33 -0700257 if (properties_inited()) {
258 get_property_workspace(&fd, &sz);
Yabin Cuie2d63af2015-02-17 19:27:51 -0800259 snprintf(tmp, sizeof(tmp), "%d,%d", dup(fd), sz);
Colin Cross3294bbb2010-04-19 17:11:33 -0700260 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
261 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700262
263 for (ei = svc->envvars; ei; ei = ei->next)
264 add_environment(ei->name, ei->value);
265
266 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400267 int socket_type = (
268 !strcmp(si->type, "stream") ? SOCK_STREAM :
269 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
270 int s = create_socket(si->name, socket_type,
Stephen Smalley8348d272013-05-13 12:37:04 -0400271 si->perm, si->uid, si->gid, si->socketcon ?: scon);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700272 if (s >= 0) {
273 publish_socket(si->name, s);
274 }
275 }
276
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500277 freecon(scon);
278 scon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500279
San Mehat4e221f02010-02-25 14:19:50 -0800280 if (svc->ioprio_class != IoSchedClass_NONE) {
281 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
282 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
283 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
284 }
285 }
286
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700287 if (needs_console) {
288 setsid();
289 open_console();
290 } else {
291 zap_stdio();
292 }
293
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800294 if (false) {
295 for (size_t n = 0; svc->args[n]; n++) {
296 INFO("args[%zu] = '%s'\n", n, svc->args[n]);
297 }
298 for (size_t n = 0; ENV[n]; n++) {
299 INFO("env[%zu] = '%s'\n", n, ENV[n]);
300 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700302
303 setpgid(0, getpid());
304
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800305 // As requested, set our gid, supplemental gids, and uid.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700306 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800307 if (setgid(svc->gid) != 0) {
308 ERROR("setgid failed: %s\n", strerror(errno));
309 _exit(127);
310 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700311 }
312 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800313 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
314 ERROR("setgroups failed: %s\n", strerror(errno));
315 _exit(127);
316 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700317 }
318 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800319 if (setuid(svc->uid) != 0) {
320 ERROR("setuid failed: %s\n", strerror(errno));
321 _exit(127);
322 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700323 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500324 if (svc->seclabel) {
325 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) {
326 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
327 _exit(127);
328 }
329 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500330
San Mehat8ad15682009-05-20 08:50:40 -0700331 if (!dynamic_args) {
332 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
333 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
334 }
335 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700336 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700337 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700338 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700339 char *next = tmp;
340 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700341
342 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700343 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700344
San Mehatd4cdd132009-05-20 09:52:16 -0700345 while((bword = strsep(&next, " "))) {
346 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700347 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700348 break;
San Mehatf24e2522009-05-19 13:30:46 -0700349 }
Andreas Gampe0ab46c92015-02-03 11:20:49 -0800350 arg_ptrs[arg_idx] = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700351 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100352 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700353 _exit(127);
354 }
355
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500356 freecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500357
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700358 if (pid < 0) {
359 ERROR("failed to start '%s'\n", svc->name);
360 svc->pid = 0;
361 return;
362 }
363
364 svc->time_started = gettime();
365 svc->pid = pid;
366 svc->flags |= SVC_RUNNING;
367
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800368 if ((svc->flags & SVC_EXEC) != 0) {
369 INFO("SVC_EXEC pid %d (uid %d gid %d+%d context %s) started; waiting...\n",
370 svc->pid, svc->uid, svc->gid, svc->nr_supp_gids, svc->seclabel);
371 waiting_for_exec = true;
372 }
373
374 svc->NotifyStateChange("running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700375}
376
Mike Kasickb54f39f2012-01-25 23:48:46 -0500377/* The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART */
Ken Sumrall752923c2010-12-03 16:33:31 -0800378static void service_stop_or_reset(struct service *svc, int how)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700379{
Mike Kasick7e36edd2012-02-06 10:32:13 -0500380 /* The service is still SVC_RUNNING until its process exits, but if it has
381 * already exited it shoudn't attempt a restart yet. */
JP Abgrall3beec7e2014-05-02 21:14:29 -0700382 svc->flags &= ~(SVC_RESTARTING | SVC_DISABLED_START);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700383
Mike Kasickb54f39f2012-01-25 23:48:46 -0500384 if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) {
Ken Sumrall752923c2010-12-03 16:33:31 -0800385 /* Hrm, an illegal flag. Default to SVC_DISABLED */
386 how = SVC_DISABLED;
387 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700388 /* if the service has not yet started, prevent
389 * it from auto-starting with its class
390 */
Ken Sumralla2864802011-10-26 16:56:00 -0700391 if (how == SVC_RESET) {
392 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
393 } else {
394 svc->flags |= how;
395 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700396
397 if (svc->pid) {
398 NOTICE("service '%s' is being killed\n", svc->name);
Ken Sumrall752923c2010-12-03 16:33:31 -0800399 kill(-svc->pid, SIGKILL);
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800400 svc->NotifyStateChange("stopping");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700401 } else {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800402 svc->NotifyStateChange("stopped");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700403 }
404}
405
Ken Sumrall752923c2010-12-03 16:33:31 -0800406void service_reset(struct service *svc)
407{
408 service_stop_or_reset(svc, SVC_RESET);
409}
410
411void service_stop(struct service *svc)
412{
413 service_stop_or_reset(svc, SVC_DISABLED);
414}
415
Mike Kasickb54f39f2012-01-25 23:48:46 -0500416void service_restart(struct service *svc)
417{
418 if (svc->flags & SVC_RUNNING) {
419 /* Stop, wait, then start the service. */
420 service_stop_or_reset(svc, SVC_RESTART);
421 } else if (!(svc->flags & SVC_RESTARTING)) {
422 /* Just start the service since it's not running. */
423 service_start(svc, NULL);
424 } /* else: Service is restarting anyways. */
425}
426
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700427void property_changed(const char *name, const char *value)
428{
Colin Crossebc6ff12010-04-13 19:52:01 -0700429 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700430 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700431}
432
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700433static void restart_service_if_needed(struct service *svc)
434{
435 time_t next_start_time = svc->time_started + 5;
436
437 if (next_start_time <= gettime()) {
438 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700439 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700440 return;
441 }
442
443 if ((next_start_time < process_needs_restart) ||
444 (process_needs_restart == 0)) {
445 process_needs_restart = next_start_time;
446 }
447}
448
449static void restart_processes()
450{
451 process_needs_restart = 0;
452 service_for_each_flags(SVC_RESTARTING,
453 restart_service_if_needed);
454}
455
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700456static void msg_start(const char *name)
457{
Hong-Mei Li11467182013-04-01 11:17:51 +0800458 struct service *svc = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700459 char *tmp = NULL;
460 char *args = NULL;
461
462 if (!strchr(name, ':'))
463 svc = service_find_by_name(name);
464 else {
465 tmp = strdup(name);
Hong-Mei Li11467182013-04-01 11:17:51 +0800466 if (tmp) {
467 args = strchr(tmp, ':');
468 *args = '\0';
469 args++;
San Mehatf24e2522009-05-19 13:30:46 -0700470
Hong-Mei Li11467182013-04-01 11:17:51 +0800471 svc = service_find_by_name(tmp);
472 }
San Mehatf24e2522009-05-19 13:30:46 -0700473 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700474
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700475 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700476 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700477 } else {
478 ERROR("no such service '%s'\n", name);
479 }
San Mehatf24e2522009-05-19 13:30:46 -0700480 if (tmp)
481 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700482}
483
484static void msg_stop(const char *name)
485{
486 struct service *svc = service_find_by_name(name);
487
488 if (svc) {
489 service_stop(svc);
490 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700491 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700492 }
493}
494
Mike Kasickb54f39f2012-01-25 23:48:46 -0500495static void msg_restart(const char *name)
496{
497 struct service *svc = service_find_by_name(name);
498
499 if (svc) {
500 service_restart(svc);
501 } else {
502 ERROR("no such service '%s'\n", name);
503 }
504}
505
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700506void handle_control_message(const char *msg, const char *arg)
507{
508 if (!strcmp(msg,"start")) {
509 msg_start(arg);
510 } else if (!strcmp(msg,"stop")) {
511 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700512 } else if (!strcmp(msg,"restart")) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500513 msg_restart(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700514 } else {
515 ERROR("unknown control msg '%s'\n", msg);
516 }
517}
518
Colin Crossebc6ff12010-04-13 19:52:01 -0700519static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700520{
521 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700522 node = list_head(&act->commands);
Dima Zavin3bea0792011-08-26 13:59:18 -0700523 if (!node || list_empty(&act->commands))
Colin Crossebc6ff12010-04-13 19:52:01 -0700524 return NULL;
525
526 return node_to_item(node, struct command, clist);
527}
528
529static struct command *get_next_command(struct action *act, struct command *cmd)
530{
531 struct listnode *node;
532 node = cmd->clist.next;
533 if (!node)
534 return NULL;
535 if (node == &act->commands)
536 return NULL;
537
538 return node_to_item(node, struct command, clist);
539}
540
541static int is_last_command(struct action *act, struct command *cmd)
542{
543 return (list_tail(&act->commands) == &cmd->clist);
544}
545
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700546
547void build_triggers_string(char *name_str, int length, struct action *cur_action) {
548 struct listnode *node;
549 struct trigger *cur_trigger;
550
551 list_for_each(node, &cur_action->triggers) {
552 cur_trigger = node_to_item(node, struct trigger, nlist);
553 if (node != cur_action->triggers.next) {
554 strlcat(name_str, " " , length);
555 }
556 strlcat(name_str, cur_trigger->name , length);
557 }
558}
559
Colin Crossebc6ff12010-04-13 19:52:01 -0700560void execute_one_command(void)
561{
Riley Andrews24a3b782014-06-26 13:56:01 -0700562 int ret, i;
563 char cmd_str[256] = "";
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700564 char name_str[256] = "";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700565
Colin Crossebc6ff12010-04-13 19:52:01 -0700566 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
567 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700568 cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -0700569 if (!cur_action)
570 return;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700571
572 build_triggers_string(name_str, sizeof(name_str), cur_action);
573
574 INFO("processing action %p (%s)\n", cur_action, name_str);
Colin Crossebc6ff12010-04-13 19:52:01 -0700575 cur_command = get_first_command(cur_action);
576 } else {
577 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700578 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700579
580 if (!cur_command)
581 return;
582
583 ret = cur_command->func(cur_command->nargs, cur_command->args);
Riley Andrews24a3b782014-06-26 13:56:01 -0700584 if (klog_get_level() >= KLOG_INFO_LEVEL) {
585 for (i = 0; i < cur_command->nargs; i++) {
586 strlcat(cmd_str, cur_command->args[i], sizeof(cmd_str));
587 if (i < cur_command->nargs - 1) {
588 strlcat(cmd_str, " ", sizeof(cmd_str));
589 }
590 }
591 INFO("command '%s' action=%s status=%d (%s:%d)\n",
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700592 cmd_str, cur_action ? name_str : "", ret, cur_command->filename,
Riley Andrews24a3b782014-06-26 13:56:01 -0700593 cur_command->line);
594 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700595}
596
Colin Crossf83d0b92010-04-21 12:04:20 -0700597static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700598{
Colin Crossf83d0b92010-04-21 12:04:20 -0700599 int ret;
Andreas Gampea016c422014-11-24 19:52:41 -0800600 INFO("wait for %s\n", COLDBOOT_DONE);
601 ret = wait_for_file(COLDBOOT_DONE, COMMAND_RETRY_TIMEOUT);
Colin Crossf83d0b92010-04-21 12:04:20 -0700602 if (ret)
Andreas Gampea016c422014-11-24 19:52:41 -0800603 ERROR("Timed out waiting for %s\n", COLDBOOT_DONE);
Colin Crossf83d0b92010-04-21 12:04:20 -0700604 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700605}
606
Alex Klyubin0d872d82013-08-16 13:19:24 -0700607/*
608 * Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed
609 * by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom.
610 * Does nothing if Hardware RNG is not present.
611 *
612 * Since we don't yet trust the quality of Hardware RNG, these bytes are not
613 * mixed into the primary pool of Linux RNG and the entropy estimate is left
614 * unmodified.
615 *
616 * If the HW RNG device /dev/hw_random is present, we require that at least
617 * 512 bytes read from it are written into Linux RNG. QA is expected to catch
618 * devices/configurations where these I/O operations are blocking for a long
619 * time. We do not reboot or halt on failures, as this is a best-effort
620 * attempt.
621 */
622static int mix_hwrng_into_linux_rng_action(int nargs, char **args)
623{
624 int result = -1;
625 int hwrandom_fd = -1;
626 int urandom_fd = -1;
627 char buf[512];
628 ssize_t chunk_size;
629 size_t total_bytes_written = 0;
630
631 hwrandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800632 open("/dev/hw_random", O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700633 if (hwrandom_fd == -1) {
634 if (errno == ENOENT) {
635 ERROR("/dev/hw_random not found\n");
636 /* It's not an error to not have a Hardware RNG. */
637 result = 0;
638 } else {
639 ERROR("Failed to open /dev/hw_random: %s\n", strerror(errno));
640 }
641 goto ret;
642 }
643
644 urandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800645 open("/dev/urandom", O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700646 if (urandom_fd == -1) {
647 ERROR("Failed to open /dev/urandom: %s\n", strerror(errno));
648 goto ret;
649 }
650
651 while (total_bytes_written < sizeof(buf)) {
652 chunk_size = TEMP_FAILURE_RETRY(
653 read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written));
654 if (chunk_size == -1) {
655 ERROR("Failed to read from /dev/hw_random: %s\n", strerror(errno));
656 goto ret;
657 } else if (chunk_size == 0) {
658 ERROR("Failed to read from /dev/hw_random: EOF\n");
659 goto ret;
660 }
661
662 chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size));
663 if (chunk_size == -1) {
664 ERROR("Failed to write to /dev/urandom: %s\n", strerror(errno));
665 goto ret;
666 }
667 total_bytes_written += chunk_size;
668 }
669
Elliott Hughesccecf142014-01-16 10:53:11 -0800670 INFO("Mixed %zu bytes from /dev/hw_random into /dev/urandom",
Alex Klyubin0d872d82013-08-16 13:19:24 -0700671 total_bytes_written);
672 result = 0;
673
674ret:
675 if (hwrandom_fd != -1) {
676 close(hwrandom_fd);
677 }
678 if (urandom_fd != -1) {
679 close(urandom_fd);
680 }
681 memset(buf, 0, sizeof(buf));
682 return result;
683}
684
Colin Crossebc6ff12010-04-13 19:52:01 -0700685static int keychord_init_action(int nargs, char **args)
686{
687 keychord_init();
688 return 0;
689}
690
691static int console_init_action(int nargs, char **args)
692{
693 int fd;
Colin Crossebc6ff12010-04-13 19:52:01 -0700694
695 if (console[0]) {
Hong-Mei Li11467182013-04-01 11:17:51 +0800696 snprintf(console_name, sizeof(console_name), "/dev/%s", console);
Colin Crossebc6ff12010-04-13 19:52:01 -0700697 }
698
Nick Kralevich45a884f2015-02-02 14:37:22 -0800699 fd = open(console_name, O_RDWR | O_CLOEXEC);
Colin Crossebc6ff12010-04-13 19:52:01 -0700700 if (fd >= 0)
701 have_console = 1;
702 close(fd);
703
Nick Kralevich45a884f2015-02-02 14:37:22 -0800704 fd = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200705 if (fd >= 0) {
706 const char *msg;
707 msg = "\n"
708 "\n"
709 "\n"
710 "\n"
711 "\n"
712 "\n"
713 "\n" // console is 40 cols x 30 lines
714 "\n"
715 "\n"
716 "\n"
717 "\n"
718 "\n"
719 "\n"
720 "\n"
721 " A N D R O I D ";
722 write(fd, msg, strlen(msg));
723 close(fd);
Colin Crossebc6ff12010-04-13 19:52:01 -0700724 }
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200725
Colin Crossebc6ff12010-04-13 19:52:01 -0700726 return 0;
727}
728
Dima Zavin5511c842011-12-19 11:21:32 -0800729static void import_kernel_nv(char *name, int for_emulator)
730{
731 char *value = strchr(name, '=');
732 int name_len = strlen(name);
733
734 if (value == 0) return;
735 *value++ = 0;
736 if (name_len == 0) return;
737
738 if (for_emulator) {
739 /* in the emulator, export any kernel option with the
740 * ro.kernel. prefix */
741 char buff[PROP_NAME_MAX];
742 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
743
744 if (len < (int)sizeof(buff))
745 property_set( buff, value );
746 return;
747 }
748
749 if (!strcmp(name,"qemu")) {
750 strlcpy(qemu, value, sizeof(qemu));
751 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
752 const char *boot_prop_name = name + 12;
753 char prop[PROP_NAME_MAX];
754 int cnt;
755
756 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
757 if (cnt < PROP_NAME_MAX)
758 property_set(prop, value);
759 }
760}
761
762static void export_kernel_boot_props(void)
Colin Crossebc6ff12010-04-13 19:52:01 -0700763{
764 char tmp[PROP_VALUE_MAX];
Colin Cross1a6f4c32013-01-28 17:13:35 -0800765 int ret;
Dima Zavin5511c842011-12-19 11:21:32 -0800766 unsigned i;
767 struct {
768 const char *src_prop;
769 const char *dest_prop;
770 const char *def_val;
771 } prop_map[] = {
772 { "ro.boot.serialno", "ro.serialno", "", },
773 { "ro.boot.mode", "ro.bootmode", "unknown", },
774 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800775 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
776 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700777
Dima Zavin5511c842011-12-19 11:21:32 -0800778 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
Colin Cross1a6f4c32013-01-28 17:13:35 -0800779 ret = property_get(prop_map[i].src_prop, tmp);
Colin Cross5e484e92013-06-17 16:20:08 -0700780 if (ret > 0)
781 property_set(prop_map[i].dest_prop, tmp);
782 else
Colin Cross1a6f4c32013-01-28 17:13:35 -0800783 property_set(prop_map[i].dest_prop, prop_map[i].def_val);
Dima Zavin5511c842011-12-19 11:21:32 -0800784 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700785
Colin Cross1a6f4c32013-01-28 17:13:35 -0800786 ret = property_get("ro.boot.console", tmp);
787 if (ret)
788 strlcpy(console, tmp, sizeof(console));
Dima Zavin5511c842011-12-19 11:21:32 -0800789
790 /* save a copy for init's usage during boot */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800791 property_get("ro.bootmode", tmp);
792 strlcpy(bootmode, tmp, sizeof(bootmode));
Dima Zavin5511c842011-12-19 11:21:32 -0800793
794 /* if this was given on kernel command line, override what we read
795 * before (e.g. from /proc/cpuinfo), if anything */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800796 ret = property_get("ro.boot.hardware", tmp);
797 if (ret)
798 strlcpy(hardware, tmp, sizeof(hardware));
Dima Zavin5511c842011-12-19 11:21:32 -0800799 property_set("ro.hardware", hardware);
800
801 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
802 property_set("ro.revision", tmp);
803
804 /* TODO: these are obsolete. We should delete them */
Colin Crossebc6ff12010-04-13 19:52:01 -0700805 if (!strcmp(bootmode,"factory"))
806 property_set("ro.factorytest", "1");
807 else if (!strcmp(bootmode,"factory2"))
808 property_set("ro.factorytest", "2");
809 else
810 property_set("ro.factorytest", "0");
Dima Zavin5511c842011-12-19 11:21:32 -0800811}
Colin Crossebc6ff12010-04-13 19:52:01 -0700812
Dima Zavin5511c842011-12-19 11:21:32 -0800813static void process_kernel_cmdline(void)
814{
815 /* don't expose the raw commandline to nonpriv processes */
816 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700817
Dima Zavin5511c842011-12-19 11:21:32 -0800818 /* first pass does the common stuff, and finds if we are in qemu.
819 * second pass is only necessary for qemu to export all kernel params
820 * as props.
821 */
822 import_kernel_cmdline(0, import_kernel_nv);
823 if (qemu[0])
824 import_kernel_cmdline(1, import_kernel_nv);
825
826 /* now propogate the info given on command line to internal variables
827 * used by init as well as the current required properties
828 */
829 export_kernel_boot_props();
Colin Crossebc6ff12010-04-13 19:52:01 -0700830}
831
832static int property_service_init_action(int nargs, char **args)
833{
834 /* read any property files on system or data and
835 * fire up the property service. This must happen
836 * after the ro.foo properties are set above so
837 * that /data/local.prop cannot interfere with them.
838 */
839 start_property_service();
Riley Andrews9464e5a2014-07-11 15:05:23 -0700840 if (get_property_set_fd() < 0) {
841 ERROR("start_property_service() failed\n");
842 exit(1);
843 }
844
Colin Crossebc6ff12010-04-13 19:52:01 -0700845 return 0;
846}
847
848static int signal_init_action(int nargs, char **args)
849{
850 signal_init();
Riley Andrews9464e5a2014-07-11 15:05:23 -0700851 if (get_signal_fd() < 0) {
852 ERROR("signal_init() failed\n");
Colin Crossebc6ff12010-04-13 19:52:01 -0700853 exit(1);
854 }
855 return 0;
856}
857
858static int queue_property_triggers_action(int nargs, char **args)
859{
860 queue_all_property_triggers();
861 /* enable property triggers */
862 property_triggers_enabled = 1;
863 return 0;
864}
865
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400866void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500867{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400868 sehandle = selinux_android_file_context_handle();
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500869 selinux_android_set_sehandle(sehandle);
rpcraig63207cd2012-08-09 10:05:49 -0400870 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400871}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500872
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700873static bool selinux_is_disabled(void)
874{
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800875 if (ALLOW_DISABLE_SELINUX) {
876 if (access("/sys/fs/selinux", F_OK) != 0) {
877 // SELinux is not compiled into the kernel, or has been disabled
878 // via the kernel command line "selinux=0".
879 return true;
880 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700881
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800882 char tmp[PROP_VALUE_MAX];
883 if ((property_get("ro.boot.selinux", tmp) != 0) && (strcmp(tmp, "disabled") == 0)) {
884 // SELinux is compiled into the kernel, but we've been told to disable it.
885 return true;
886 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700887 }
888
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700889 return false;
890}
891
892static bool selinux_is_enforcing(void)
893{
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800894 if (ALLOW_DISABLE_SELINUX) {
895 char tmp[PROP_VALUE_MAX];
896 if (property_get("ro.boot.selinux", tmp) == 0) {
897 // Property is not set. Assume enforcing.
898 return true;
899 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700900
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800901 if (strcmp(tmp, "permissive") == 0) {
902 // SELinux is in the kernel, but we've been told to go into permissive mode.
903 return false;
904 }
905
906 if (strcmp(tmp, "enforcing") != 0) {
907 ERROR("SELinux: Unknown value of ro.boot.selinux. Got: \"%s\". Assuming enforcing.\n", tmp);
908 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700909 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700910 return true;
911}
912
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400913int selinux_reload_policy(void)
914{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700915 if (selinux_is_disabled()) {
916 return -1;
917 }
918
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400919 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500920
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400921 if (selinux_android_reload_policy() == -1) {
922 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500923 }
924
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400925 if (sehandle)
926 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500927
rpcraig63207cd2012-08-09 10:05:49 -0400928 if (sehandle_prop)
929 selabel_close(sehandle_prop);
930
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400931 selinux_init_all_handles();
932 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500933}
rpcraig63207cd2012-08-09 10:05:49 -0400934
Elliott Hughesf682b472015-02-06 12:19:48 -0800935static int audit_callback(void *data, security_class_t /*cls*/, char *buf, size_t len)
rpcraig63207cd2012-08-09 10:05:49 -0400936{
937 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
938 return 0;
939}
940
Stephen Smalley439224e2014-06-24 13:45:43 -0400941int log_callback(int type, const char *fmt, ...)
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500942{
943 int level;
944 va_list ap;
945 switch (type) {
946 case SELINUX_WARNING:
947 level = KLOG_WARNING_LEVEL;
948 break;
949 case SELINUX_INFO:
950 level = KLOG_INFO_LEVEL;
951 break;
952 default:
953 level = KLOG_ERROR_LEVEL;
954 break;
955 }
956 va_start(ap, fmt);
957 klog_vwrite(level, fmt, ap);
958 va_end(ap);
959 return 0;
960}
961
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700962static void selinux_initialize(void)
963{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700964 if (selinux_is_disabled()) {
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700965 return;
966 }
967
968 INFO("loading selinux policy\n");
969 if (selinux_android_load_policy() < 0) {
970 ERROR("SELinux: Failed to load policy; rebooting into recovery mode\n");
971 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
972 while (1) { pause(); } // never reached
973 }
974
975 selinux_init_all_handles();
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700976 bool is_enforcing = selinux_is_enforcing();
977 INFO("SELinux: security_setenforce(%d)\n", is_enforcing);
978 security_setenforce(is_enforcing);
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700979}
980
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800981int main(int argc, char** argv) {
Colin Crossf83d0b92010-04-21 12:04:20 -0700982 if (!strcmp(basename(argv[0]), "ueventd"))
983 return ueventd_main(argc, argv);
984
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700985 if (!strcmp(basename(argv[0]), "watchdogd"))
986 return watchdogd_main(argc, argv);
987
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800988 // Clear the umask.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700989 umask(0);
990
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800991 // Get the basic filesystem setup we need put together in the initramdisk
992 // on / and then we'll let the rc file figure out the rest.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700993 mkdir("/dev", 0755);
994 mkdir("/proc", 0755);
995 mkdir("/sys", 0755);
996
Nick Kralevich150f19e2010-06-22 16:35:43 -0700997 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700998 mkdir("/dev/pts", 0755);
999 mkdir("/dev/socket", 0755);
1000 mount("devpts", "/dev/pts", "devpts", 0, NULL);
1001 mount("proc", "/proc", "proc", 0, NULL);
1002 mount("sysfs", "/sys", "sysfs", 0, NULL);
1003
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001004 // Indicate that booting is in progress to background fw loaders, etc.
Nick Kralevich45a884f2015-02-02 14:37:22 -08001005 close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));
Brian Swetland8d48c8e2011-03-24 15:45:30 -07001006
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001007 // We must have some place other than / to create the device nodes for
1008 // kmsg and null, otherwise we won't be able to remount / read-only
1009 // later on. Now that tmpfs is mounted on /dev, we can actually talk
1010 // to the outside world.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001011 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -07001012 klog_init();
Dima Zavin5511c842011-12-19 11:21:32 -08001013 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -07001014
Colin Crossf83d0b92010-04-21 12:04:20 -07001015 get_hardware_name(hardware, &revision);
Dima Zavind7634c92011-12-16 14:18:06 -08001016
Dima Zavin5511c842011-12-19 11:21:32 -08001017 process_kernel_cmdline();
1018
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001019 selinux_callback cb;
Stephen Smalleyeb3f4212014-02-12 16:17:00 -05001020 cb.func_log = log_callback;
rpcraig63207cd2012-08-09 10:05:49 -04001021 selinux_set_callback(SELINUX_CB_LOG, cb);
rpcraig63207cd2012-08-09 10:05:49 -04001022 cb.func_audit = audit_callback;
1023 selinux_set_callback(SELINUX_CB_AUDIT, cb);
1024
Nick Kralevich56fa0ac2013-06-24 17:41:40 -07001025 selinux_initialize();
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001026
1027 // These directories were necessarily created before initial policy load
1028 // and therefore need their security context restored to the proper value.
1029 // This must happen before /dev is populated by ueventd.
Stephen Smalleye096e362012-06-11 13:37:39 -04001030 restorecon("/dev");
1031 restorecon("/dev/socket");
Geremy Condra8e15eab2013-02-28 17:29:58 -08001032 restorecon("/dev/__properties__");
Nick Kralevichae76f6d2013-07-11 15:38:26 -07001033 restorecon_recursive("/sys");
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001034
Dima Zavind7634c92011-12-16 14:18:06 -08001035 INFO("property init\n");
Riley Andrewse4b7b292014-06-16 15:06:21 -07001036 property_load_boot_defaults();
Dima Zavind7634c92011-12-16 14:18:06 -08001037
Dima Zavind7634c92011-12-16 14:18:06 -08001038 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001039
1040 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001041
Colin Crossf83d0b92010-04-21 12:04:20 -07001042 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Alex Klyubin0d872d82013-08-16 13:19:24 -07001043 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001044 queue_builtin_action(keychord_init_action, "keychord_init");
1045 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001046
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001047 // Execute all the boot actions to get us started.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001048 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001049
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001050 // Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
1051 // wasn't ready immediately after wait_for_coldboot_done
Alex Klyubin0d872d82013-08-16 13:19:24 -07001052 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001053 queue_builtin_action(property_service_init_action, "property_service_init");
1054 queue_builtin_action(signal_init_action, "signal_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001055
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001056 // Don't mount filesystems or start core system services in charger mode.
1057 if (strcmp(bootmode, "charger") == 0) {
Dima Zavinca47cef2011-08-24 15:28:23 -07001058 action_for_each_trigger("charger", action_add_queue_tail);
1059 } else {
Riley Andrewse4b7b292014-06-16 15:06:21 -07001060 action_for_each_trigger("late-init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001061 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001062
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001063 // Run all property triggers based on current state of the properties.
Chris Dearman469b7b22012-03-01 15:29:20 -08001064 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001065
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001066 // TODO: why do we only initialize ufds after execute_one_command and restart_processes?
1067 size_t fd_count = 0;
1068 struct pollfd ufds[3];
1069 bool property_set_fd_init = false;
1070 bool signal_fd_init = false;
1071 bool keychord_fd_init = false;
1072
Yongqin Liua197ff12014-12-05 13:45:02 +08001073 for (;;) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001074 if (!waiting_for_exec) {
1075 execute_one_command();
1076 restart_processes();
1077 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001078
Colin Crossebc6ff12010-04-13 19:52:01 -07001079 if (!property_set_fd_init && get_property_set_fd() > 0) {
1080 ufds[fd_count].fd = get_property_set_fd();
1081 ufds[fd_count].events = POLLIN;
1082 ufds[fd_count].revents = 0;
1083 fd_count++;
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001084 property_set_fd_init = true;
Colin Crossebc6ff12010-04-13 19:52:01 -07001085 }
1086 if (!signal_fd_init && get_signal_fd() > 0) {
1087 ufds[fd_count].fd = get_signal_fd();
1088 ufds[fd_count].events = POLLIN;
1089 ufds[fd_count].revents = 0;
1090 fd_count++;
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001091 signal_fd_init = true;
Colin Crossebc6ff12010-04-13 19:52:01 -07001092 }
1093 if (!keychord_fd_init && get_keychord_fd() > 0) {
1094 ufds[fd_count].fd = get_keychord_fd();
1095 ufds[fd_count].events = POLLIN;
1096 ufds[fd_count].revents = 0;
1097 fd_count++;
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001098 keychord_fd_init = true;
Colin Crossebc6ff12010-04-13 19:52:01 -07001099 }
1100
Yongqin Liua197ff12014-12-05 13:45:02 +08001101 int timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001102 if (process_needs_restart) {
1103 timeout = (process_needs_restart - gettime()) * 1000;
1104 if (timeout < 0)
1105 timeout = 0;
1106 }
1107
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001108 if (!action_queue_empty() || cur_action) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001109 timeout = 0;
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001110 }
Colin Crossebc6ff12010-04-13 19:52:01 -07001111
Yongqin Liua197ff12014-12-05 13:45:02 +08001112 bootchart_sample(&timeout);
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001113
Yongqin Liua197ff12014-12-05 13:45:02 +08001114 int nr = poll(ufds, fd_count, timeout);
1115 if (nr <= 0) {
1116 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001117 }
Colin Crossebc6ff12010-04-13 19:52:01 -07001118
Yongqin Liua197ff12014-12-05 13:45:02 +08001119 for (size_t i = 0; i < fd_count; i++) {
Amir Goldstein1d4e86c2013-11-10 15:36:58 +02001120 if (ufds[i].revents & POLLIN) {
Yongqin Liua197ff12014-12-05 13:45:02 +08001121 if (ufds[i].fd == get_property_set_fd()) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001122 handle_property_set_fd();
Yongqin Liua197ff12014-12-05 13:45:02 +08001123 } else if (ufds[i].fd == get_keychord_fd()) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001124 handle_keychord();
Yongqin Liua197ff12014-12-05 13:45:02 +08001125 } else if (ufds[i].fd == get_signal_fd()) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001126 handle_signal();
Yongqin Liua197ff12014-12-05 13:45:02 +08001127 }
Colin Crossebc6ff12010-04-13 19:52:01 -07001128 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001129 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001130 }
1131
1132 return 0;
1133}