blob: 4ed23b6c06710d03c4ba4aa9f99f2846be2b5f86 [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>
Rom Lemarchand6a524432015-02-28 06:39:11 -080018#include <dirent.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080019#include <errno.h>
20#include <fcntl.h>
21#include <libgen.h>
Elliott Hughescc86fb22015-03-17 20:01:13 -070022#include <paths.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080023#include <signal.h>
24#include <stdarg.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070028#include <sys/mount.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070029#include <sys/poll.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070030#include <sys/socket.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080031#include <sys/stat.h>
32#include <sys/types.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070033#include <sys/un.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080034#include <sys/wait.h>
35#include <termios.h>
36#include <unistd.h>
37
38#include <mtd/mtd-user.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050039
Stephen Smalleye46f9d52012-01-13 08:48:47 -050040#include <selinux/selinux.h>
41#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040042#include <selinux/android.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050043
Rom Lemarchand6a524432015-02-28 06:39:11 -080044#include <base/file.h>
45#include <base/stringprintf.h>
Nick Kralevich56fa0ac2013-06-24 17:41:40 -070046#include <cutils/android_reboot.h>
Alex Klyubin0d872d82013-08-16 13:19:24 -070047#include <cutils/fs.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080048#include <cutils/iosched_policy.h>
49#include <cutils/list.h>
50#include <cutils/sockets.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070051#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070052
Rom Lemarchand6a524432015-02-28 06:39:11 -080053#include <memory>
54
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070055#include "devices.h"
56#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070057#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070058#include "property_service.h"
The Android Open Source Project35237d12008-12-17 18:08:08 -080059#include "bootchart.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070060#include "signal_handler.h"
Colin Crossa8666952010-04-13 19:20:44 -070061#include "keychords.h"
Colin Cross6310a822010-04-20 14:29:05 -070062#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070063#include "util.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070064#include "ueventd.h"
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070065#include "watchdogd.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070066
Stephen Smalleye46f9d52012-01-13 08:48:47 -050067struct selabel_handle *sehandle;
rpcraig63207cd2012-08-09 10:05:49 -040068struct selabel_handle *sehandle_prop;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050069
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070070static int property_triggers_enabled = 0;
71
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072static char qemu[32];
73
Colin Crossebc6ff12010-04-13 19:52:01 -070074static struct action *cur_action = NULL;
75static struct command *cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -070076
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070077static int have_console;
Hong-Mei Li11467182013-04-01 11:17:51 +080078static char console_name[PROP_VALUE_MAX] = "/dev/console";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070079static time_t process_needs_restart;
80
81static const char *ENV[32];
82
Elliott Hughes8d82ea02015-02-06 20:15:18 -080083bool waiting_for_exec = false;
84
85void service::NotifyStateChange(const char* new_state) {
Elliott Hughesda40c002015-03-27 23:20:44 -070086 if (!properties_initialized()) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -080087 // If properties aren't available yet, we can't set them.
88 return;
89 }
90
91 if ((flags & SVC_EXEC) != 0) {
92 // 'exec' commands don't have properties tracking their state.
93 return;
94 }
95
96 char prop_name[PROP_NAME_MAX];
97 if (snprintf(prop_name, sizeof(prop_name), "init.svc.%s", name) >= PROP_NAME_MAX) {
98 // If the property name would be too long, we can't set it.
99 ERROR("Property name \"init.svc.%s\" too long; not setting to %s\n", name, new_state);
100 return;
101 }
102
103 property_set(prop_name, new_state);
104}
105
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700106/* add_environment - add "key=value" to the current environment */
107int add_environment(const char *key, const char *val)
108{
James Morrissey381341f2014-05-16 11:36:36 +0100109 size_t n;
110 size_t key_len = strlen(key);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700111
James Morrissey381341f2014-05-16 11:36:36 +0100112 /* The last environment entry is reserved to terminate the list */
113 for (n = 0; n < (ARRAY_SIZE(ENV) - 1); n++) {
114
115 /* Delete any existing entry for this key */
116 if (ENV[n] != NULL) {
117 size_t entry_key_len = strcspn(ENV[n], "=");
118 if ((entry_key_len == key_len) && (strncmp(ENV[n], key, entry_key_len) == 0)) {
119 free((char*)ENV[n]);
120 ENV[n] = NULL;
121 }
122 }
123
124 /* Add entry if a free slot is available */
125 if (ENV[n] == NULL) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800126 char* entry;
127 asprintf(&entry, "%s=%s", key, val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700128 ENV[n] = entry;
129 return 0;
130 }
131 }
132
James Morrissey381341f2014-05-16 11:36:36 +0100133 ERROR("No env. room to store: '%s':'%s'\n", key, val);
134
135 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700136}
137
San Mehat429721c2014-09-23 07:48:47 -0700138void zap_stdio(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700139{
140 int fd;
141 fd = open("/dev/null", O_RDWR);
142 dup2(fd, 0);
143 dup2(fd, 1);
144 dup2(fd, 2);
145 close(fd);
146}
147
148static void open_console()
149{
150 int fd;
151 if ((fd = open(console_name, O_RDWR)) < 0) {
152 fd = open("/dev/null", O_RDWR);
153 }
Colin Cross50fb5a62012-03-18 15:38:19 -0700154 ioctl(fd, TIOCSCTTY, 0);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700155 dup2(fd, 0);
156 dup2(fd, 1);
157 dup2(fd, 2);
158 close(fd);
159}
160
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700161static void publish_socket(const char *name, int fd)
162{
163 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
164 char val[64];
165
166 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
167 name,
168 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
169 snprintf(val, sizeof(val), "%d", fd);
170 add_environment(key, val);
171
172 /* make sure we don't close-on-exec */
173 fcntl(fd, F_SETFD, 0);
174}
175
San Mehatf24e2522009-05-19 13:30:46 -0700176void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700177{
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800178 // Starting a service removes it from the disabled or reset state and
179 // immediately takes it out of the restarting state if it was in there.
JP Abgrall3beec7e2014-05-02 21:14:29 -0700180 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700181 svc->time_started = 0;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700182
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800183 // Running processes require no additional work --- if they're in the
184 // process of exiting, we've ensured that they will immediately restart
185 // on exit, unless they are ONESHOT.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700186 if (svc->flags & SVC_RUNNING) {
187 return;
188 }
189
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800190 bool needs_console = (svc->flags & SVC_CONSOLE);
191 if (needs_console && !have_console) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700192 ERROR("service '%s' requires console\n", svc->name);
193 svc->flags |= SVC_DISABLED;
194 return;
195 }
196
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800197 struct stat s;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700198 if (stat(svc->args[0], &s) != 0) {
199 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
200 svc->flags |= SVC_DISABLED;
201 return;
202 }
203
San Mehatf24e2522009-05-19 13:30:46 -0700204 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700205 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
206 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700207 svc->flags |= SVC_DISABLED;
208 return;
209 }
210
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800211 char* scon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500212 if (is_selinux_enabled() > 0) {
Stephen Smalley30f30332012-11-16 14:34:27 -0500213 if (svc->seclabel) {
214 scon = strdup(svc->seclabel);
215 if (!scon) {
216 ERROR("Out of memory while starting '%s'\n", svc->name);
217 return;
218 }
219 } else {
220 char *mycon = NULL, *fcon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500221
Stephen Smalley30f30332012-11-16 14:34:27 -0500222 INFO("computing context for service '%s'\n", svc->args[0]);
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800223 int rc = getcon(&mycon);
Stephen Smalley30f30332012-11-16 14:34:27 -0500224 if (rc < 0) {
225 ERROR("could not get context while starting '%s'\n", svc->name);
226 return;
227 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500228
Stephen Smalley30f30332012-11-16 14:34:27 -0500229 rc = getfilecon(svc->args[0], &fcon);
230 if (rc < 0) {
231 ERROR("could not get context while starting '%s'\n", svc->name);
232 freecon(mycon);
233 return;
234 }
235
236 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon);
Stephen Smalleyaf06c672013-12-09 15:40:24 -0500237 if (rc == 0 && !strcmp(scon, mycon)) {
238 ERROR("Warning! Service %s needs a SELinux domain defined; please fix!\n", svc->name);
239 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500240 freecon(mycon);
Stephen Smalley30f30332012-11-16 14:34:27 -0500241 freecon(fcon);
242 if (rc < 0) {
243 ERROR("could not get context while starting '%s'\n", svc->name);
244 return;
245 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500246 }
247 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500248
Elliott Hughesda40c002015-03-27 23:20:44 -0700249 NOTICE("Starting service '%s'...\n", svc->name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700250
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800251 pid_t pid = fork();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700252 if (pid == 0) {
253 struct socketinfo *si;
254 struct svcenvinfo *ei;
255 char tmp[32];
256 int fd, sz;
257
Nick Kralevich6ebf12f2012-03-26 09:09:11 -0700258 umask(077);
Elliott Hughesda40c002015-03-27 23:20:44 -0700259 if (properties_initialized()) {
Colin Cross3294bbb2010-04-19 17:11:33 -0700260 get_property_workspace(&fd, &sz);
Yabin Cuie2d63af2015-02-17 19:27:51 -0800261 snprintf(tmp, sizeof(tmp), "%d,%d", dup(fd), sz);
Colin Cross3294bbb2010-04-19 17:11:33 -0700262 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
263 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700264
265 for (ei = svc->envvars; ei; ei = ei->next)
266 add_environment(ei->name, ei->value);
267
268 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400269 int socket_type = (
270 !strcmp(si->type, "stream") ? SOCK_STREAM :
271 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
272 int s = create_socket(si->name, socket_type,
Stephen Smalley8348d272013-05-13 12:37:04 -0400273 si->perm, si->uid, si->gid, si->socketcon ?: scon);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700274 if (s >= 0) {
275 publish_socket(si->name, s);
276 }
277 }
278
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500279 freecon(scon);
280 scon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500281
San Mehat4e221f02010-02-25 14:19:50 -0800282 if (svc->ioprio_class != IoSchedClass_NONE) {
283 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
284 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
285 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
286 }
287 }
288
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700289 if (needs_console) {
290 setsid();
291 open_console();
292 } else {
293 zap_stdio();
294 }
295
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800296 if (false) {
297 for (size_t n = 0; svc->args[n]; n++) {
298 INFO("args[%zu] = '%s'\n", n, svc->args[n]);
299 }
300 for (size_t n = 0; ENV[n]; n++) {
301 INFO("env[%zu] = '%s'\n", n, ENV[n]);
302 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700303 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700304
305 setpgid(0, getpid());
306
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800307 // As requested, set our gid, supplemental gids, and uid.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700308 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800309 if (setgid(svc->gid) != 0) {
310 ERROR("setgid failed: %s\n", strerror(errno));
311 _exit(127);
312 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700313 }
314 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800315 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
316 ERROR("setgroups failed: %s\n", strerror(errno));
317 _exit(127);
318 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700319 }
320 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800321 if (setuid(svc->uid) != 0) {
322 ERROR("setuid failed: %s\n", strerror(errno));
323 _exit(127);
324 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700325 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500326 if (svc->seclabel) {
327 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) {
328 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
329 _exit(127);
330 }
331 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500332
San Mehat8ad15682009-05-20 08:50:40 -0700333 if (!dynamic_args) {
334 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
335 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
336 }
337 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700338 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700339 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700340 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700341 char *next = tmp;
342 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700343
344 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700345 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700346
San Mehatd4cdd132009-05-20 09:52:16 -0700347 while((bword = strsep(&next, " "))) {
348 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700349 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700350 break;
San Mehatf24e2522009-05-19 13:30:46 -0700351 }
Andreas Gampe0ab46c92015-02-03 11:20:49 -0800352 arg_ptrs[arg_idx] = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700353 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100354 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700355 _exit(127);
356 }
357
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500358 freecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500359
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700360 if (pid < 0) {
361 ERROR("failed to start '%s'\n", svc->name);
362 svc->pid = 0;
363 return;
364 }
365
366 svc->time_started = gettime();
367 svc->pid = pid;
368 svc->flags |= SVC_RUNNING;
369
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800370 if ((svc->flags & SVC_EXEC) != 0) {
Elliott Hughes58c3bac2015-03-11 12:31:53 -0700371 INFO("SVC_EXEC pid %d (uid %d gid %d+%zu context %s) started; waiting...\n",
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800372 svc->pid, svc->uid, svc->gid, svc->nr_supp_gids, svc->seclabel);
373 waiting_for_exec = true;
374 }
375
376 svc->NotifyStateChange("running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700377}
378
Mike Kasickb54f39f2012-01-25 23:48:46 -0500379/* The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART */
Ken Sumrall752923c2010-12-03 16:33:31 -0800380static void service_stop_or_reset(struct service *svc, int how)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700381{
Mike Kasick7e36edd2012-02-06 10:32:13 -0500382 /* The service is still SVC_RUNNING until its process exits, but if it has
383 * already exited it shoudn't attempt a restart yet. */
JP Abgrall3beec7e2014-05-02 21:14:29 -0700384 svc->flags &= ~(SVC_RESTARTING | SVC_DISABLED_START);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700385
Mike Kasickb54f39f2012-01-25 23:48:46 -0500386 if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) {
Ken Sumrall752923c2010-12-03 16:33:31 -0800387 /* Hrm, an illegal flag. Default to SVC_DISABLED */
388 how = SVC_DISABLED;
389 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700390 /* if the service has not yet started, prevent
391 * it from auto-starting with its class
392 */
Ken Sumralla2864802011-10-26 16:56:00 -0700393 if (how == SVC_RESET) {
394 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
395 } else {
396 svc->flags |= how;
397 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700398
399 if (svc->pid) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700400 NOTICE("Service '%s' is being killed...\n", svc->name);
Ken Sumrall752923c2010-12-03 16:33:31 -0800401 kill(-svc->pid, SIGKILL);
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800402 svc->NotifyStateChange("stopping");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700403 } else {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800404 svc->NotifyStateChange("stopped");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700405 }
406}
407
Ken Sumrall752923c2010-12-03 16:33:31 -0800408void service_reset(struct service *svc)
409{
410 service_stop_or_reset(svc, SVC_RESET);
411}
412
413void service_stop(struct service *svc)
414{
415 service_stop_or_reset(svc, SVC_DISABLED);
416}
417
Mike Kasickb54f39f2012-01-25 23:48:46 -0500418void service_restart(struct service *svc)
419{
420 if (svc->flags & SVC_RUNNING) {
421 /* Stop, wait, then start the service. */
422 service_stop_or_reset(svc, SVC_RESTART);
423 } else if (!(svc->flags & SVC_RESTARTING)) {
424 /* Just start the service since it's not running. */
425 service_start(svc, NULL);
426 } /* else: Service is restarting anyways. */
427}
428
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700429void property_changed(const char *name, const char *value)
430{
Colin Crossebc6ff12010-04-13 19:52:01 -0700431 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700432 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700433}
434
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700435static void restart_service_if_needed(struct service *svc)
436{
437 time_t next_start_time = svc->time_started + 5;
438
439 if (next_start_time <= gettime()) {
440 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700441 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700442 return;
443 }
444
445 if ((next_start_time < process_needs_restart) ||
446 (process_needs_restart == 0)) {
447 process_needs_restart = next_start_time;
448 }
449}
450
451static void restart_processes()
452{
453 process_needs_restart = 0;
454 service_for_each_flags(SVC_RESTARTING,
455 restart_service_if_needed);
456}
457
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700458static void msg_start(const char *name)
459{
Hong-Mei Li11467182013-04-01 11:17:51 +0800460 struct service *svc = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700461 char *tmp = NULL;
462 char *args = NULL;
463
464 if (!strchr(name, ':'))
465 svc = service_find_by_name(name);
466 else {
467 tmp = strdup(name);
Hong-Mei Li11467182013-04-01 11:17:51 +0800468 if (tmp) {
469 args = strchr(tmp, ':');
470 *args = '\0';
471 args++;
San Mehatf24e2522009-05-19 13:30:46 -0700472
Hong-Mei Li11467182013-04-01 11:17:51 +0800473 svc = service_find_by_name(tmp);
474 }
San Mehatf24e2522009-05-19 13:30:46 -0700475 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700476
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700477 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700478 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700479 } else {
480 ERROR("no such service '%s'\n", name);
481 }
San Mehatf24e2522009-05-19 13:30:46 -0700482 if (tmp)
483 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700484}
485
486static void msg_stop(const char *name)
487{
488 struct service *svc = service_find_by_name(name);
489
490 if (svc) {
491 service_stop(svc);
492 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700493 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700494 }
495}
496
Mike Kasickb54f39f2012-01-25 23:48:46 -0500497static void msg_restart(const char *name)
498{
499 struct service *svc = service_find_by_name(name);
500
501 if (svc) {
502 service_restart(svc);
503 } else {
504 ERROR("no such service '%s'\n", name);
505 }
506}
507
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700508void handle_control_message(const char *msg, const char *arg)
509{
510 if (!strcmp(msg,"start")) {
511 msg_start(arg);
512 } else if (!strcmp(msg,"stop")) {
513 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700514 } else if (!strcmp(msg,"restart")) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500515 msg_restart(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700516 } else {
517 ERROR("unknown control msg '%s'\n", msg);
518 }
519}
520
Colin Crossebc6ff12010-04-13 19:52:01 -0700521static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700522{
523 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700524 node = list_head(&act->commands);
Dima Zavin3bea0792011-08-26 13:59:18 -0700525 if (!node || list_empty(&act->commands))
Colin Crossebc6ff12010-04-13 19:52:01 -0700526 return NULL;
527
528 return node_to_item(node, struct command, clist);
529}
530
531static struct command *get_next_command(struct action *act, struct command *cmd)
532{
533 struct listnode *node;
534 node = cmd->clist.next;
535 if (!node)
536 return NULL;
537 if (node == &act->commands)
538 return NULL;
539
540 return node_to_item(node, struct command, clist);
541}
542
543static int is_last_command(struct action *act, struct command *cmd)
544{
545 return (list_tail(&act->commands) == &cmd->clist);
546}
547
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700548
549void build_triggers_string(char *name_str, int length, struct action *cur_action) {
550 struct listnode *node;
551 struct trigger *cur_trigger;
552
553 list_for_each(node, &cur_action->triggers) {
554 cur_trigger = node_to_item(node, struct trigger, nlist);
555 if (node != cur_action->triggers.next) {
556 strlcat(name_str, " " , length);
557 }
558 strlcat(name_str, cur_trigger->name , length);
559 }
560}
561
Elliott Hughesda40c002015-03-27 23:20:44 -0700562void execute_one_command() {
563 Timer t;
564
Riley Andrews24a3b782014-06-26 13:56:01 -0700565 char cmd_str[256] = "";
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700566 char name_str[256] = "";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700567
Colin Crossebc6ff12010-04-13 19:52:01 -0700568 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
569 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700570 cur_command = NULL;
Elliott Hughesda40c002015-03-27 23:20:44 -0700571 if (!cur_action) {
Colin Crossebc6ff12010-04-13 19:52:01 -0700572 return;
Elliott Hughesda40c002015-03-27 23:20:44 -0700573 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700574
575 build_triggers_string(name_str, sizeof(name_str), cur_action);
576
577 INFO("processing action %p (%s)\n", cur_action, name_str);
Colin Crossebc6ff12010-04-13 19:52:01 -0700578 cur_command = get_first_command(cur_action);
579 } else {
580 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700581 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700582
Elliott Hughesda40c002015-03-27 23:20:44 -0700583 if (!cur_command) {
Colin Crossebc6ff12010-04-13 19:52:01 -0700584 return;
Elliott Hughesda40c002015-03-27 23:20:44 -0700585 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700586
Elliott Hughesda40c002015-03-27 23:20:44 -0700587 int result = cur_command->func(cur_command->nargs, cur_command->args);
Riley Andrews24a3b782014-06-26 13:56:01 -0700588 if (klog_get_level() >= KLOG_INFO_LEVEL) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700589 for (int i = 0; i < cur_command->nargs; i++) {
Riley Andrews24a3b782014-06-26 13:56:01 -0700590 strlcat(cmd_str, cur_command->args[i], sizeof(cmd_str));
591 if (i < cur_command->nargs - 1) {
592 strlcat(cmd_str, " ", sizeof(cmd_str));
593 }
594 }
Elliott Hughesda40c002015-03-27 23:20:44 -0700595 char source[256];
596 if (cur_command->filename) {
597 snprintf(source, sizeof(source), " (%s:%d)", cur_command->filename, cur_command->line);
598 } else {
599 *source = '\0';
600 }
601 INFO("Command '%s' action=%s%s returned %d took %.2fs\n",
602 cmd_str, cur_action ? name_str : "", source, result, t.duration());
Riley Andrews24a3b782014-06-26 13:56:01 -0700603 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700604}
605
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700606static int wait_for_coldboot_done_action(int nargs, char **args) {
607 Timer t;
608
609 NOTICE("Waiting for %s...\n", COLDBOOT_DONE);
610 if (wait_for_file(COLDBOOT_DONE, COMMAND_RETRY_TIMEOUT)) {
Andreas Gampea016c422014-11-24 19:52:41 -0800611 ERROR("Timed out waiting for %s\n", COLDBOOT_DONE);
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700612 }
613
614 NOTICE("Waiting for %s took %.2fs.\n", COLDBOOT_DONE, t.duration());
615 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700616}
617
Alex Klyubin0d872d82013-08-16 13:19:24 -0700618/*
619 * Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed
620 * by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom.
621 * Does nothing if Hardware RNG is not present.
622 *
623 * Since we don't yet trust the quality of Hardware RNG, these bytes are not
624 * mixed into the primary pool of Linux RNG and the entropy estimate is left
625 * unmodified.
626 *
627 * If the HW RNG device /dev/hw_random is present, we require that at least
628 * 512 bytes read from it are written into Linux RNG. QA is expected to catch
629 * devices/configurations where these I/O operations are blocking for a long
630 * time. We do not reboot or halt on failures, as this is a best-effort
631 * attempt.
632 */
633static int mix_hwrng_into_linux_rng_action(int nargs, char **args)
634{
635 int result = -1;
636 int hwrandom_fd = -1;
637 int urandom_fd = -1;
638 char buf[512];
639 ssize_t chunk_size;
640 size_t total_bytes_written = 0;
641
642 hwrandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800643 open("/dev/hw_random", O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700644 if (hwrandom_fd == -1) {
645 if (errno == ENOENT) {
646 ERROR("/dev/hw_random not found\n");
647 /* It's not an error to not have a Hardware RNG. */
648 result = 0;
649 } else {
650 ERROR("Failed to open /dev/hw_random: %s\n", strerror(errno));
651 }
652 goto ret;
653 }
654
655 urandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800656 open("/dev/urandom", O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700657 if (urandom_fd == -1) {
658 ERROR("Failed to open /dev/urandom: %s\n", strerror(errno));
659 goto ret;
660 }
661
662 while (total_bytes_written < sizeof(buf)) {
663 chunk_size = TEMP_FAILURE_RETRY(
664 read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written));
665 if (chunk_size == -1) {
666 ERROR("Failed to read from /dev/hw_random: %s\n", strerror(errno));
667 goto ret;
668 } else if (chunk_size == 0) {
669 ERROR("Failed to read from /dev/hw_random: EOF\n");
670 goto ret;
671 }
672
673 chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size));
674 if (chunk_size == -1) {
675 ERROR("Failed to write to /dev/urandom: %s\n", strerror(errno));
676 goto ret;
677 }
678 total_bytes_written += chunk_size;
679 }
680
Elliott Hughesccecf142014-01-16 10:53:11 -0800681 INFO("Mixed %zu bytes from /dev/hw_random into /dev/urandom",
Alex Klyubin0d872d82013-08-16 13:19:24 -0700682 total_bytes_written);
683 result = 0;
684
685ret:
686 if (hwrandom_fd != -1) {
687 close(hwrandom_fd);
688 }
689 if (urandom_fd != -1) {
690 close(urandom_fd);
691 }
Alex Klyubin0d872d82013-08-16 13:19:24 -0700692 return result;
693}
694
Colin Crossebc6ff12010-04-13 19:52:01 -0700695static int keychord_init_action(int nargs, char **args)
696{
697 keychord_init();
698 return 0;
699}
700
701static int console_init_action(int nargs, char **args)
702{
Elliott Hughesd679bc92015-03-20 08:50:46 -0700703 char console[PROP_VALUE_MAX];
704 if (property_get("ro.boot.console", console) > 0) {
Hong-Mei Li11467182013-04-01 11:17:51 +0800705 snprintf(console_name, sizeof(console_name), "/dev/%s", console);
Colin Crossebc6ff12010-04-13 19:52:01 -0700706 }
707
Elliott Hughesd679bc92015-03-20 08:50:46 -0700708 int fd = open(console_name, O_RDWR | O_CLOEXEC);
Colin Crossebc6ff12010-04-13 19:52:01 -0700709 if (fd >= 0)
710 have_console = 1;
711 close(fd);
712
Nick Kralevich45a884f2015-02-02 14:37:22 -0800713 fd = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200714 if (fd >= 0) {
715 const char *msg;
716 msg = "\n"
717 "\n"
718 "\n"
719 "\n"
720 "\n"
721 "\n"
722 "\n" // console is 40 cols x 30 lines
723 "\n"
724 "\n"
725 "\n"
726 "\n"
727 "\n"
728 "\n"
729 "\n"
730 " A N D R O I D ";
731 write(fd, msg, strlen(msg));
732 close(fd);
Colin Crossebc6ff12010-04-13 19:52:01 -0700733 }
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200734
Colin Crossebc6ff12010-04-13 19:52:01 -0700735 return 0;
736}
737
Dima Zavin5511c842011-12-19 11:21:32 -0800738static void import_kernel_nv(char *name, int for_emulator)
739{
740 char *value = strchr(name, '=');
741 int name_len = strlen(name);
742
743 if (value == 0) return;
744 *value++ = 0;
745 if (name_len == 0) return;
746
747 if (for_emulator) {
748 /* in the emulator, export any kernel option with the
749 * ro.kernel. prefix */
750 char buff[PROP_NAME_MAX];
751 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
752
753 if (len < (int)sizeof(buff))
754 property_set( buff, value );
755 return;
756 }
757
758 if (!strcmp(name,"qemu")) {
759 strlcpy(qemu, value, sizeof(qemu));
760 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
761 const char *boot_prop_name = name + 12;
762 char prop[PROP_NAME_MAX];
763 int cnt;
764
765 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
766 if (cnt < PROP_NAME_MAX)
767 property_set(prop, value);
768 }
769}
770
Elliott Hughesd679bc92015-03-20 08:50:46 -0700771static void export_kernel_boot_props() {
Dima Zavin5511c842011-12-19 11:21:32 -0800772 struct {
773 const char *src_prop;
Elliott Hughesd679bc92015-03-20 08:50:46 -0700774 const char *dst_prop;
775 const char *default_value;
Dima Zavin5511c842011-12-19 11:21:32 -0800776 } prop_map[] = {
Elliott Hughesd679bc92015-03-20 08:50:46 -0700777 { "ro.boot.serialno", "ro.serialno", "", },
778 { "ro.boot.mode", "ro.bootmode", "unknown", },
779 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800780 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
Elliott Hughesd679bc92015-03-20 08:50:46 -0700781 { "ro.boot.hardware", "ro.hardware", "unknown", },
782 { "ro.boot.revision", "ro.revision", "0", },
Dima Zavin5511c842011-12-19 11:21:32 -0800783 };
Elliott Hughesd679bc92015-03-20 08:50:46 -0700784 for (size_t i = 0; i < ARRAY_SIZE(prop_map); i++) {
785 char value[PROP_VALUE_MAX];
786 int rc = property_get(prop_map[i].src_prop, value);
787 property_set(prop_map[i].dst_prop, (rc > 0) ? value : prop_map[i].default_value);
Dima Zavin5511c842011-12-19 11:21:32 -0800788 }
Dima Zavin5511c842011-12-19 11:21:32 -0800789}
Colin Crossebc6ff12010-04-13 19:52:01 -0700790
Rom Lemarchand6a524432015-02-28 06:39:11 -0800791static void process_kernel_dt(void)
792{
793 static const char android_dir[] = "/proc/device-tree/firmware/android";
794
795 std::string file_name = android::base::StringPrintf("%s/compatible", android_dir);
796
797 std::string dt_file;
798 android::base::ReadFileToString(file_name, &dt_file);
799 if (!dt_file.compare("android,firmware")) {
800 ERROR("firmware/android is not compatible with 'android,firmware'\n");
801 return;
802 }
803
804 std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(android_dir), closedir);
805 if (!dir)
806 return;
807
808 struct dirent *dp;
809 while ((dp = readdir(dir.get())) != NULL) {
810 if (dp->d_type != DT_REG || !strcmp(dp->d_name, "compatible"))
811 continue;
812
813 file_name = android::base::StringPrintf("%s/%s", android_dir, dp->d_name);
814
815 android::base::ReadFileToString(file_name, &dt_file);
816 std::replace(dt_file.begin(), dt_file.end(), ',', '.');
817
818 std::string property_name = android::base::StringPrintf("ro.boot.%s", dp->d_name);
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700819 property_set(property_name.c_str(), dt_file.c_str());
Rom Lemarchand6a524432015-02-28 06:39:11 -0800820 }
821}
822
Dima Zavin5511c842011-12-19 11:21:32 -0800823static void process_kernel_cmdline(void)
824{
825 /* don't expose the raw commandline to nonpriv processes */
826 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700827
Dima Zavin5511c842011-12-19 11:21:32 -0800828 /* first pass does the common stuff, and finds if we are in qemu.
829 * second pass is only necessary for qemu to export all kernel params
830 * as props.
831 */
832 import_kernel_cmdline(0, import_kernel_nv);
833 if (qemu[0])
834 import_kernel_cmdline(1, import_kernel_nv);
Colin Crossebc6ff12010-04-13 19:52:01 -0700835}
836
Colin Crossebc6ff12010-04-13 19:52:01 -0700837static int queue_property_triggers_action(int nargs, char **args)
838{
839 queue_all_property_triggers();
840 /* enable property triggers */
841 property_triggers_enabled = 1;
842 return 0;
843}
844
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700845static void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500846{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400847 sehandle = selinux_android_file_context_handle();
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500848 selinux_android_set_sehandle(sehandle);
rpcraig63207cd2012-08-09 10:05:49 -0400849 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400850}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500851
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700852static bool selinux_is_disabled(void)
853{
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800854 if (ALLOW_DISABLE_SELINUX) {
855 if (access("/sys/fs/selinux", F_OK) != 0) {
856 // SELinux is not compiled into the kernel, or has been disabled
857 // via the kernel command line "selinux=0".
858 return true;
859 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700860
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800861 char tmp[PROP_VALUE_MAX];
862 if ((property_get("ro.boot.selinux", tmp) != 0) && (strcmp(tmp, "disabled") == 0)) {
863 // SELinux is compiled into the kernel, but we've been told to disable it.
864 return true;
865 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700866 }
867
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700868 return false;
869}
870
871static bool selinux_is_enforcing(void)
872{
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800873 if (ALLOW_DISABLE_SELINUX) {
874 char tmp[PROP_VALUE_MAX];
875 if (property_get("ro.boot.selinux", tmp) == 0) {
876 // Property is not set. Assume enforcing.
877 return true;
878 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700879
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800880 if (strcmp(tmp, "permissive") == 0) {
881 // SELinux is in the kernel, but we've been told to go into permissive mode.
882 return false;
883 }
884
885 if (strcmp(tmp, "enforcing") != 0) {
886 ERROR("SELinux: Unknown value of ro.boot.selinux. Got: \"%s\". Assuming enforcing.\n", tmp);
887 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700888 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700889 return true;
890}
891
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400892int selinux_reload_policy(void)
893{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700894 if (selinux_is_disabled()) {
895 return -1;
896 }
897
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400898 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500899
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400900 if (selinux_android_reload_policy() == -1) {
901 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500902 }
903
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400904 if (sehandle)
905 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500906
rpcraig63207cd2012-08-09 10:05:49 -0400907 if (sehandle_prop)
908 selabel_close(sehandle_prop);
909
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400910 selinux_init_all_handles();
911 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500912}
rpcraig63207cd2012-08-09 10:05:49 -0400913
Elliott Hughesda40c002015-03-27 23:20:44 -0700914static int audit_callback(void *data, security_class_t /*cls*/, char *buf, size_t len) {
rpcraig63207cd2012-08-09 10:05:49 -0400915 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
916 return 0;
917}
918
Elliott Hughesf65730e2015-04-24 18:38:17 +0000919static void security_failure() {
920 ERROR("Security failure; rebooting into recovery mode...\n");
921 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
922 while (true) { pause(); } // never reached
923}
924
925static void selinux_initialize(bool in_kernel_domain) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700926 Timer t;
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500927
Elliott Hughesda40c002015-03-27 23:20:44 -0700928 selinux_callback cb;
929 cb.func_log = selinux_klog_callback;
930 selinux_set_callback(SELINUX_CB_LOG, cb);
931 cb.func_audit = audit_callback;
932 selinux_set_callback(SELINUX_CB_AUDIT, cb);
933
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700934 if (selinux_is_disabled()) {
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700935 return;
936 }
937
Elliott Hughesf65730e2015-04-24 18:38:17 +0000938 if (in_kernel_domain) {
939 if (write_file("/sys/fs/selinux/checkreqprot", "0") == -1) {
940 ERROR("couldn't write to /sys/fs/selinux/checkreqprot: %s\n",
941 strerror(errno));
942 security_failure();
943 }
944
945 INFO("Loading SELinux policy...\n");
946 if (selinux_android_load_policy() < 0) {
947 ERROR("failed to load policy: %s\n", strerror(errno));
948 security_failure();
949 }
950
951 bool is_enforcing = selinux_is_enforcing();
952 security_setenforce(is_enforcing);
953
954 NOTICE("(Initializing SELinux %s took %.2fs.)\n",
955 is_enforcing ? "enforcing" : "non-enforcing", t.duration());
956 } else {
957 selinux_init_all_handles();
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700958 }
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700959}
960
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800961int main(int argc, char** argv) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700962 if (!strcmp(basename(argv[0]), "ueventd")) {
Colin Crossf83d0b92010-04-21 12:04:20 -0700963 return ueventd_main(argc, argv);
Elliott Hughesda40c002015-03-27 23:20:44 -0700964 }
Colin Crossf83d0b92010-04-21 12:04:20 -0700965
Elliott Hughesda40c002015-03-27 23:20:44 -0700966 if (!strcmp(basename(argv[0]), "watchdogd")) {
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700967 return watchdogd_main(argc, argv);
Elliott Hughesda40c002015-03-27 23:20:44 -0700968 }
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700969
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800970 // Clear the umask.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700971 umask(0);
972
Elliott Hughescc86fb22015-03-17 20:01:13 -0700973 add_environment("PATH", _PATH_DEFPATH);
974
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800975 // Get the basic filesystem setup we need put together in the initramdisk
976 // on / and then we'll let the rc file figure out the rest.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700977 mkdir("/dev", 0755);
978 mkdir("/proc", 0755);
979 mkdir("/sys", 0755);
980
Nick Kralevich150f19e2010-06-22 16:35:43 -0700981 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700982 mkdir("/dev/pts", 0755);
983 mkdir("/dev/socket", 0755);
984 mount("devpts", "/dev/pts", "devpts", 0, NULL);
985 mount("proc", "/proc", "proc", 0, NULL);
986 mount("sysfs", "/sys", "sysfs", 0, NULL);
987
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800988 // Indicate that booting is in progress to background fw loaders, etc.
Nick Kralevich45a884f2015-02-02 14:37:22 -0800989 close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700990
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800991 // We must have some place other than / to create the device nodes for
992 // kmsg and null, otherwise we won't be able to remount / read-only
993 // later on. Now that tmpfs is mounted on /dev, we can actually talk
994 // to the outside world.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700995 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -0700996 klog_init();
Elliott Hughesda40c002015-03-27 23:20:44 -0700997 klog_set_level(KLOG_NOTICE_LEVEL);
998
Elliott Hughesf65730e2015-04-24 18:38:17 +0000999 bool is_first_stage = (argc == 1) || (strcmp(argv[1], "--second-stage") != 0);
1000 NOTICE("init%s started!\n", is_first_stage ? "" : " second stage");
Elliott Hughesda40c002015-03-27 23:20:44 -07001001
Dima Zavin5511c842011-12-19 11:21:32 -08001002 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -07001003
Elliott Hughesda40c002015-03-27 23:20:44 -07001004 // If arguments are passed both on the command line and in DT,
1005 // properties set in DT always have priority over the command-line ones.
Rom Lemarchand6a524432015-02-28 06:39:11 -08001006 process_kernel_dt();
Dima Zavin5511c842011-12-19 11:21:32 -08001007 process_kernel_cmdline();
1008
Elliott Hughesda40c002015-03-27 23:20:44 -07001009 // Propogate the kernel variables to internal variables
1010 // used by init as well as the current required properties.
Rom Lemarchand6a524432015-02-28 06:39:11 -08001011 export_kernel_boot_props();
1012
Elliott Hughesf65730e2015-04-24 18:38:17 +00001013 // Set up SELinux, including loading the SELinux policy if we're in the kernel domain.
1014 selinux_initialize(is_first_stage);
1015
1016 // If we're in the kernel domain, re-exec init to transition to the init domain now
1017 // that the SELinux policy has been loaded.
1018 if (is_first_stage) {
1019 if (restorecon("/init") == -1) {
1020 ERROR("restorecon failed: %s\n", strerror(errno));
1021 security_failure();
1022 }
1023 char* path = argv[0];
1024 char* args[] = { path, const_cast<char*>("--second-stage"), nullptr };
1025 if (execv(path, args) == -1) {
1026 ERROR("execv(\"%s\") failed: %s\n", path, strerror(errno));
1027 security_failure();
1028 }
1029 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001030
1031 // These directories were necessarily created before initial policy load
1032 // and therefore need their security context restored to the proper value.
1033 // This must happen before /dev is populated by ueventd.
Elliott Hughesda40c002015-03-27 23:20:44 -07001034 INFO("Running restorecon...\n");
Stephen Smalleye096e362012-06-11 13:37:39 -04001035 restorecon("/dev");
1036 restorecon("/dev/socket");
Geremy Condra8e15eab2013-02-28 17:29:58 -08001037 restorecon("/dev/__properties__");
Nick Kralevichae76f6d2013-07-11 15:38:26 -07001038 restorecon_recursive("/sys");
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001039
Elliott Hughes9042cae2015-04-24 17:43:21 -07001040 signal_init();
1041
Riley Andrewse4b7b292014-06-16 15:06:21 -07001042 property_load_boot_defaults();
Elliott Hughesc6c26ed2015-04-24 18:50:30 -07001043 start_property_service();
Dima Zavind7634c92011-12-16 14:18:06 -08001044
Dima Zavind7634c92011-12-16 14:18:06 -08001045 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001046
1047 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001048
Elliott Hughesc6c26ed2015-04-24 18:50:30 -07001049 // Queue an action that waits for coldboot done so we know ueventd has set up all of /dev...
Colin Crossf83d0b92010-04-21 12:04:20 -07001050 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Elliott Hughesc6c26ed2015-04-24 18:50:30 -07001051 // ... so that we can start queuing up actions that require stuff from /dev.
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(keychord_init_action, "keychord_init");
1054 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001055
Elliott Hughesda40c002015-03-27 23:20:44 -07001056 // Trigger all the boot actions to get us started.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001057 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001058
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001059 // Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
1060 // wasn't ready immediately after wait_for_coldboot_done
Alex Klyubin0d872d82013-08-16 13:19:24 -07001061 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001062
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001063 // Don't mount filesystems or start core system services in charger mode.
Elliott Hughesd679bc92015-03-20 08:50:46 -07001064 char bootmode[PROP_VALUE_MAX];
1065 if (property_get("ro.bootmode", bootmode) > 0 && strcmp(bootmode, "charger") == 0) {
Dima Zavinca47cef2011-08-24 15:28:23 -07001066 action_for_each_trigger("charger", action_add_queue_tail);
1067 } else {
Riley Andrewse4b7b292014-06-16 15:06:21 -07001068 action_for_each_trigger("late-init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001069 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001070
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001071 // Run all property triggers based on current state of the properties.
Chris Dearman469b7b22012-03-01 15:29:20 -08001072 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001073
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001074 size_t fd_count = 0;
1075 struct pollfd ufds[3];
Elliott Hughes9042cae2015-04-24 17:43:21 -07001076 ufds[fd_count++] = { .fd = get_signal_fd(), .events = POLLIN, .revents = 0 };
Elliott Hughesc6c26ed2015-04-24 18:50:30 -07001077 ufds[fd_count++] = { .fd = get_property_set_fd(), .events = POLLIN, .revents = 0 };
1078 // TODO: can we work out when /dev/keychord is first accessible and open this fd then?
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001079 bool keychord_fd_init = false;
1080
Elliott Hughesc6c26ed2015-04-24 18:50:30 -07001081 while (true) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001082 if (!waiting_for_exec) {
1083 execute_one_command();
1084 restart_processes();
1085 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001086
Colin Crossebc6ff12010-04-13 19:52:01 -07001087 if (!keychord_fd_init && get_keychord_fd() > 0) {
1088 ufds[fd_count].fd = get_keychord_fd();
1089 ufds[fd_count].events = POLLIN;
1090 ufds[fd_count].revents = 0;
1091 fd_count++;
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001092 keychord_fd_init = true;
Colin Crossebc6ff12010-04-13 19:52:01 -07001093 }
1094
Yongqin Liua197ff12014-12-05 13:45:02 +08001095 int timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001096 if (process_needs_restart) {
1097 timeout = (process_needs_restart - gettime()) * 1000;
1098 if (timeout < 0)
1099 timeout = 0;
1100 }
1101
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001102 if (!action_queue_empty() || cur_action) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001103 timeout = 0;
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001104 }
Colin Crossebc6ff12010-04-13 19:52:01 -07001105
Yongqin Liua197ff12014-12-05 13:45:02 +08001106 bootchart_sample(&timeout);
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001107
Elliott Hughesc6c26ed2015-04-24 18:50:30 -07001108 int nr = TEMP_FAILURE_RETRY(poll(ufds, fd_count, timeout));
Yongqin Liua197ff12014-12-05 13:45:02 +08001109 if (nr <= 0) {
Elliott Hughesc6c26ed2015-04-24 18:50:30 -07001110 if (nr == -1) {
1111 ERROR("poll failed: %s\n", strerror(errno));
1112 }
Yongqin Liua197ff12014-12-05 13:45:02 +08001113 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001114 }
Colin Crossebc6ff12010-04-13 19:52:01 -07001115
Yongqin Liua197ff12014-12-05 13:45:02 +08001116 for (size_t i = 0; i < fd_count; i++) {
Amir Goldstein1d4e86c2013-11-10 15:36:58 +02001117 if (ufds[i].revents & POLLIN) {
Yongqin Liua197ff12014-12-05 13:45:02 +08001118 if (ufds[i].fd == get_property_set_fd()) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001119 handle_property_set_fd();
Yongqin Liua197ff12014-12-05 13:45:02 +08001120 } else if (ufds[i].fd == get_keychord_fd()) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001121 handle_keychord();
Yongqin Liua197ff12014-12-05 13:45:02 +08001122 } else if (ufds[i].fd == get_signal_fd()) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001123 handle_signal();
Yongqin Liua197ff12014-12-05 13:45:02 +08001124 }
Colin Crossebc6ff12010-04-13 19:52:01 -07001125 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001126 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001127 }
1128
1129 return 0;
1130}