blob: 85d79093f3350d2eb89f4f3d7409b4c22c8911e8 [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>
Elliott Hughes929f4072015-04-24 21:13:44 -070028#include <sys/epoll.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070029#include <sys/mount.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
Elliott Hughes929f4072015-04-24 21:13:44 -070085static int epoll_fd = -1;
86
87void register_epoll_handler(int fd, void (*fn)()) {
88 epoll_event ev;
89 ev.events = EPOLLIN;
90 ev.data.ptr = reinterpret_cast<void*>(fn);
91 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
92 ERROR("epoll_ctl failed: %s\n", strerror(errno));
93 }
94}
95
Elliott Hughes8d82ea02015-02-06 20:15:18 -080096void service::NotifyStateChange(const char* new_state) {
Elliott Hughesda40c002015-03-27 23:20:44 -070097 if (!properties_initialized()) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -080098 // If properties aren't available yet, we can't set them.
99 return;
100 }
101
102 if ((flags & SVC_EXEC) != 0) {
103 // 'exec' commands don't have properties tracking their state.
104 return;
105 }
106
107 char prop_name[PROP_NAME_MAX];
108 if (snprintf(prop_name, sizeof(prop_name), "init.svc.%s", name) >= PROP_NAME_MAX) {
109 // If the property name would be too long, we can't set it.
110 ERROR("Property name \"init.svc.%s\" too long; not setting to %s\n", name, new_state);
111 return;
112 }
113
114 property_set(prop_name, new_state);
115}
116
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700117/* add_environment - add "key=value" to the current environment */
118int add_environment(const char *key, const char *val)
119{
James Morrissey381341f2014-05-16 11:36:36 +0100120 size_t n;
121 size_t key_len = strlen(key);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700122
James Morrissey381341f2014-05-16 11:36:36 +0100123 /* The last environment entry is reserved to terminate the list */
124 for (n = 0; n < (ARRAY_SIZE(ENV) - 1); n++) {
125
126 /* Delete any existing entry for this key */
127 if (ENV[n] != NULL) {
128 size_t entry_key_len = strcspn(ENV[n], "=");
129 if ((entry_key_len == key_len) && (strncmp(ENV[n], key, entry_key_len) == 0)) {
130 free((char*)ENV[n]);
131 ENV[n] = NULL;
132 }
133 }
134
135 /* Add entry if a free slot is available */
136 if (ENV[n] == NULL) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800137 char* entry;
138 asprintf(&entry, "%s=%s", key, val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700139 ENV[n] = entry;
140 return 0;
141 }
142 }
143
James Morrissey381341f2014-05-16 11:36:36 +0100144 ERROR("No env. room to store: '%s':'%s'\n", key, val);
145
146 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700147}
148
San Mehat429721c2014-09-23 07:48:47 -0700149void zap_stdio(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700150{
151 int fd;
152 fd = open("/dev/null", O_RDWR);
153 dup2(fd, 0);
154 dup2(fd, 1);
155 dup2(fd, 2);
156 close(fd);
157}
158
159static void open_console()
160{
161 int fd;
162 if ((fd = open(console_name, O_RDWR)) < 0) {
163 fd = open("/dev/null", O_RDWR);
164 }
Colin Cross50fb5a62012-03-18 15:38:19 -0700165 ioctl(fd, TIOCSCTTY, 0);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700166 dup2(fd, 0);
167 dup2(fd, 1);
168 dup2(fd, 2);
169 close(fd);
170}
171
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700172static void publish_socket(const char *name, int fd)
173{
174 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
175 char val[64];
176
177 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
178 name,
179 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
180 snprintf(val, sizeof(val), "%d", fd);
181 add_environment(key, val);
182
183 /* make sure we don't close-on-exec */
184 fcntl(fd, F_SETFD, 0);
185}
186
San Mehatf24e2522009-05-19 13:30:46 -0700187void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700188{
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800189 // Starting a service removes it from the disabled or reset state and
190 // immediately takes it out of the restarting state if it was in there.
JP Abgrall3beec7e2014-05-02 21:14:29 -0700191 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700192 svc->time_started = 0;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700193
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800194 // Running processes require no additional work --- if they're in the
195 // process of exiting, we've ensured that they will immediately restart
196 // on exit, unless they are ONESHOT.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700197 if (svc->flags & SVC_RUNNING) {
198 return;
199 }
200
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800201 bool needs_console = (svc->flags & SVC_CONSOLE);
202 if (needs_console && !have_console) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700203 ERROR("service '%s' requires console\n", svc->name);
204 svc->flags |= SVC_DISABLED;
205 return;
206 }
207
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800208 struct stat s;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700209 if (stat(svc->args[0], &s) != 0) {
210 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
211 svc->flags |= SVC_DISABLED;
212 return;
213 }
214
San Mehatf24e2522009-05-19 13:30:46 -0700215 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700216 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
217 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700218 svc->flags |= SVC_DISABLED;
219 return;
220 }
221
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800222 char* scon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500223 if (is_selinux_enabled() > 0) {
Stephen Smalley30f30332012-11-16 14:34:27 -0500224 if (svc->seclabel) {
225 scon = strdup(svc->seclabel);
226 if (!scon) {
227 ERROR("Out of memory while starting '%s'\n", svc->name);
228 return;
229 }
230 } else {
231 char *mycon = NULL, *fcon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500232
Stephen Smalley30f30332012-11-16 14:34:27 -0500233 INFO("computing context for service '%s'\n", svc->args[0]);
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800234 int rc = getcon(&mycon);
Stephen Smalley30f30332012-11-16 14:34:27 -0500235 if (rc < 0) {
236 ERROR("could not get context while starting '%s'\n", svc->name);
237 return;
238 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500239
Stephen Smalley30f30332012-11-16 14:34:27 -0500240 rc = getfilecon(svc->args[0], &fcon);
241 if (rc < 0) {
242 ERROR("could not get context while starting '%s'\n", svc->name);
243 freecon(mycon);
244 return;
245 }
246
247 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon);
Stephen Smalleyaf06c672013-12-09 15:40:24 -0500248 if (rc == 0 && !strcmp(scon, mycon)) {
249 ERROR("Warning! Service %s needs a SELinux domain defined; please fix!\n", svc->name);
250 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500251 freecon(mycon);
Stephen Smalley30f30332012-11-16 14:34:27 -0500252 freecon(fcon);
253 if (rc < 0) {
254 ERROR("could not get context while starting '%s'\n", svc->name);
255 return;
256 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500257 }
258 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500259
Elliott Hughesda40c002015-03-27 23:20:44 -0700260 NOTICE("Starting service '%s'...\n", svc->name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700261
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800262 pid_t pid = fork();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700263 if (pid == 0) {
264 struct socketinfo *si;
265 struct svcenvinfo *ei;
266 char tmp[32];
267 int fd, sz;
268
Nick Kralevich6ebf12f2012-03-26 09:09:11 -0700269 umask(077);
Elliott Hughesda40c002015-03-27 23:20:44 -0700270 if (properties_initialized()) {
Colin Cross3294bbb2010-04-19 17:11:33 -0700271 get_property_workspace(&fd, &sz);
Yabin Cuie2d63af2015-02-17 19:27:51 -0800272 snprintf(tmp, sizeof(tmp), "%d,%d", dup(fd), sz);
Colin Cross3294bbb2010-04-19 17:11:33 -0700273 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
274 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700275
276 for (ei = svc->envvars; ei; ei = ei->next)
277 add_environment(ei->name, ei->value);
278
279 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400280 int socket_type = (
281 !strcmp(si->type, "stream") ? SOCK_STREAM :
282 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
283 int s = create_socket(si->name, socket_type,
Stephen Smalley8348d272013-05-13 12:37:04 -0400284 si->perm, si->uid, si->gid, si->socketcon ?: scon);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700285 if (s >= 0) {
286 publish_socket(si->name, s);
287 }
288 }
289
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500290 freecon(scon);
291 scon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500292
San Mehat4e221f02010-02-25 14:19:50 -0800293 if (svc->ioprio_class != IoSchedClass_NONE) {
294 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
295 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
296 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
297 }
298 }
299
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700300 if (needs_console) {
301 setsid();
302 open_console();
303 } else {
304 zap_stdio();
305 }
306
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800307 if (false) {
308 for (size_t n = 0; svc->args[n]; n++) {
309 INFO("args[%zu] = '%s'\n", n, svc->args[n]);
310 }
311 for (size_t n = 0; ENV[n]; n++) {
312 INFO("env[%zu] = '%s'\n", n, ENV[n]);
313 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700314 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700315
316 setpgid(0, getpid());
317
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800318 // As requested, set our gid, supplemental gids, and uid.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700319 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800320 if (setgid(svc->gid) != 0) {
321 ERROR("setgid failed: %s\n", strerror(errno));
322 _exit(127);
323 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700324 }
325 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800326 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
327 ERROR("setgroups failed: %s\n", strerror(errno));
328 _exit(127);
329 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700330 }
331 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800332 if (setuid(svc->uid) != 0) {
333 ERROR("setuid failed: %s\n", strerror(errno));
334 _exit(127);
335 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700336 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500337 if (svc->seclabel) {
338 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) {
339 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
340 _exit(127);
341 }
342 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500343
San Mehat8ad15682009-05-20 08:50:40 -0700344 if (!dynamic_args) {
345 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
346 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
347 }
348 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700349 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700350 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700351 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700352 char *next = tmp;
353 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700354
355 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700356 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700357
San Mehatd4cdd132009-05-20 09:52:16 -0700358 while((bword = strsep(&next, " "))) {
359 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700360 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700361 break;
San Mehatf24e2522009-05-19 13:30:46 -0700362 }
Andreas Gampe0ab46c92015-02-03 11:20:49 -0800363 arg_ptrs[arg_idx] = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700364 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100365 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700366 _exit(127);
367 }
368
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500369 freecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500370
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700371 if (pid < 0) {
372 ERROR("failed to start '%s'\n", svc->name);
373 svc->pid = 0;
374 return;
375 }
376
377 svc->time_started = gettime();
378 svc->pid = pid;
379 svc->flags |= SVC_RUNNING;
380
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800381 if ((svc->flags & SVC_EXEC) != 0) {
Elliott Hughes58c3bac2015-03-11 12:31:53 -0700382 INFO("SVC_EXEC pid %d (uid %d gid %d+%zu context %s) started; waiting...\n",
Mark Salyzyncdb73b02015-06-02 11:11:02 -0700383 svc->pid, svc->uid, svc->gid, svc->nr_supp_gids,
384 svc->seclabel ? : "default");
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800385 waiting_for_exec = true;
386 }
387
388 svc->NotifyStateChange("running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700389}
390
Mike Kasickb54f39f2012-01-25 23:48:46 -0500391/* The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART */
Ken Sumrall752923c2010-12-03 16:33:31 -0800392static void service_stop_or_reset(struct service *svc, int how)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700393{
Mike Kasick7e36edd2012-02-06 10:32:13 -0500394 /* The service is still SVC_RUNNING until its process exits, but if it has
395 * already exited it shoudn't attempt a restart yet. */
JP Abgrall3beec7e2014-05-02 21:14:29 -0700396 svc->flags &= ~(SVC_RESTARTING | SVC_DISABLED_START);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700397
Mike Kasickb54f39f2012-01-25 23:48:46 -0500398 if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) {
Ken Sumrall752923c2010-12-03 16:33:31 -0800399 /* Hrm, an illegal flag. Default to SVC_DISABLED */
400 how = SVC_DISABLED;
401 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700402 /* if the service has not yet started, prevent
403 * it from auto-starting with its class
404 */
Ken Sumralla2864802011-10-26 16:56:00 -0700405 if (how == SVC_RESET) {
406 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
407 } else {
408 svc->flags |= how;
409 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700410
411 if (svc->pid) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700412 NOTICE("Service '%s' is being killed...\n", svc->name);
Ken Sumrall752923c2010-12-03 16:33:31 -0800413 kill(-svc->pid, SIGKILL);
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800414 svc->NotifyStateChange("stopping");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700415 } else {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800416 svc->NotifyStateChange("stopped");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700417 }
418}
419
Ken Sumrall752923c2010-12-03 16:33:31 -0800420void service_reset(struct service *svc)
421{
422 service_stop_or_reset(svc, SVC_RESET);
423}
424
425void service_stop(struct service *svc)
426{
427 service_stop_or_reset(svc, SVC_DISABLED);
428}
429
Mike Kasickb54f39f2012-01-25 23:48:46 -0500430void service_restart(struct service *svc)
431{
432 if (svc->flags & SVC_RUNNING) {
433 /* Stop, wait, then start the service. */
434 service_stop_or_reset(svc, SVC_RESTART);
435 } else if (!(svc->flags & SVC_RESTARTING)) {
436 /* Just start the service since it's not running. */
437 service_start(svc, NULL);
438 } /* else: Service is restarting anyways. */
439}
440
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700441void property_changed(const char *name, const char *value)
442{
Colin Crossebc6ff12010-04-13 19:52:01 -0700443 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700444 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700445}
446
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700447static void restart_service_if_needed(struct service *svc)
448{
449 time_t next_start_time = svc->time_started + 5;
450
451 if (next_start_time <= gettime()) {
452 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700453 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700454 return;
455 }
456
457 if ((next_start_time < process_needs_restart) ||
458 (process_needs_restart == 0)) {
459 process_needs_restart = next_start_time;
460 }
461}
462
463static void restart_processes()
464{
465 process_needs_restart = 0;
466 service_for_each_flags(SVC_RESTARTING,
467 restart_service_if_needed);
468}
469
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700470static void msg_start(const char *name)
471{
Hong-Mei Li11467182013-04-01 11:17:51 +0800472 struct service *svc = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700473 char *tmp = NULL;
474 char *args = NULL;
475
476 if (!strchr(name, ':'))
477 svc = service_find_by_name(name);
478 else {
479 tmp = strdup(name);
Hong-Mei Li11467182013-04-01 11:17:51 +0800480 if (tmp) {
481 args = strchr(tmp, ':');
482 *args = '\0';
483 args++;
San Mehatf24e2522009-05-19 13:30:46 -0700484
Hong-Mei Li11467182013-04-01 11:17:51 +0800485 svc = service_find_by_name(tmp);
486 }
San Mehatf24e2522009-05-19 13:30:46 -0700487 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700488
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700489 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700490 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700491 } else {
492 ERROR("no such service '%s'\n", name);
493 }
San Mehatf24e2522009-05-19 13:30:46 -0700494 if (tmp)
495 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700496}
497
498static void msg_stop(const char *name)
499{
500 struct service *svc = service_find_by_name(name);
501
502 if (svc) {
503 service_stop(svc);
504 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700505 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700506 }
507}
508
Mike Kasickb54f39f2012-01-25 23:48:46 -0500509static void msg_restart(const char *name)
510{
511 struct service *svc = service_find_by_name(name);
512
513 if (svc) {
514 service_restart(svc);
515 } else {
516 ERROR("no such service '%s'\n", name);
517 }
518}
519
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700520void handle_control_message(const char *msg, const char *arg)
521{
522 if (!strcmp(msg,"start")) {
523 msg_start(arg);
524 } else if (!strcmp(msg,"stop")) {
525 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700526 } else if (!strcmp(msg,"restart")) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500527 msg_restart(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700528 } else {
529 ERROR("unknown control msg '%s'\n", msg);
530 }
531}
532
Colin Crossebc6ff12010-04-13 19:52:01 -0700533static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700534{
535 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700536 node = list_head(&act->commands);
Dima Zavin3bea0792011-08-26 13:59:18 -0700537 if (!node || list_empty(&act->commands))
Colin Crossebc6ff12010-04-13 19:52:01 -0700538 return NULL;
539
540 return node_to_item(node, struct command, clist);
541}
542
543static struct command *get_next_command(struct action *act, struct command *cmd)
544{
545 struct listnode *node;
546 node = cmd->clist.next;
547 if (!node)
548 return NULL;
549 if (node == &act->commands)
550 return NULL;
551
552 return node_to_item(node, struct command, clist);
553}
554
555static int is_last_command(struct action *act, struct command *cmd)
556{
557 return (list_tail(&act->commands) == &cmd->clist);
558}
559
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700560
561void build_triggers_string(char *name_str, int length, struct action *cur_action) {
562 struct listnode *node;
563 struct trigger *cur_trigger;
564
565 list_for_each(node, &cur_action->triggers) {
566 cur_trigger = node_to_item(node, struct trigger, nlist);
567 if (node != cur_action->triggers.next) {
568 strlcat(name_str, " " , length);
569 }
570 strlcat(name_str, cur_trigger->name , length);
571 }
572}
573
Elliott Hughesda40c002015-03-27 23:20:44 -0700574void execute_one_command() {
575 Timer t;
576
Riley Andrews24a3b782014-06-26 13:56:01 -0700577 char cmd_str[256] = "";
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700578 char name_str[256] = "";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700579
Colin Crossebc6ff12010-04-13 19:52:01 -0700580 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
581 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700582 cur_command = NULL;
Elliott Hughesda40c002015-03-27 23:20:44 -0700583 if (!cur_action) {
Colin Crossebc6ff12010-04-13 19:52:01 -0700584 return;
Elliott Hughesda40c002015-03-27 23:20:44 -0700585 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700586
587 build_triggers_string(name_str, sizeof(name_str), cur_action);
588
589 INFO("processing action %p (%s)\n", cur_action, name_str);
Colin Crossebc6ff12010-04-13 19:52:01 -0700590 cur_command = get_first_command(cur_action);
591 } else {
592 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700593 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700594
Elliott Hughesda40c002015-03-27 23:20:44 -0700595 if (!cur_command) {
Colin Crossebc6ff12010-04-13 19:52:01 -0700596 return;
Elliott Hughesda40c002015-03-27 23:20:44 -0700597 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700598
Elliott Hughesda40c002015-03-27 23:20:44 -0700599 int result = cur_command->func(cur_command->nargs, cur_command->args);
Riley Andrews24a3b782014-06-26 13:56:01 -0700600 if (klog_get_level() >= KLOG_INFO_LEVEL) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700601 for (int i = 0; i < cur_command->nargs; i++) {
Riley Andrews24a3b782014-06-26 13:56:01 -0700602 strlcat(cmd_str, cur_command->args[i], sizeof(cmd_str));
603 if (i < cur_command->nargs - 1) {
604 strlcat(cmd_str, " ", sizeof(cmd_str));
605 }
606 }
Elliott Hughesda40c002015-03-27 23:20:44 -0700607 char source[256];
608 if (cur_command->filename) {
609 snprintf(source, sizeof(source), " (%s:%d)", cur_command->filename, cur_command->line);
610 } else {
611 *source = '\0';
612 }
613 INFO("Command '%s' action=%s%s returned %d took %.2fs\n",
614 cmd_str, cur_action ? name_str : "", source, result, t.duration());
Riley Andrews24a3b782014-06-26 13:56:01 -0700615 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700616}
617
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700618static int wait_for_coldboot_done_action(int nargs, char **args) {
619 Timer t;
620
621 NOTICE("Waiting for %s...\n", COLDBOOT_DONE);
Elliott Hughes651fae32015-05-14 12:03:14 -0700622 // Any longer than 1s is an unreasonable length of time to delay booting.
623 // If you're hitting this timeout, check that you didn't make your
624 // sepolicy regular expressions too expensive (http://b/19899875).
625 if (wait_for_file(COLDBOOT_DONE, 1)) {
Andreas Gampea016c422014-11-24 19:52:41 -0800626 ERROR("Timed out waiting for %s\n", COLDBOOT_DONE);
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700627 }
628
629 NOTICE("Waiting for %s took %.2fs.\n", COLDBOOT_DONE, t.duration());
630 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700631}
632
Alex Klyubin0d872d82013-08-16 13:19:24 -0700633/*
634 * Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed
635 * by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom.
636 * Does nothing if Hardware RNG is not present.
637 *
638 * Since we don't yet trust the quality of Hardware RNG, these bytes are not
639 * mixed into the primary pool of Linux RNG and the entropy estimate is left
640 * unmodified.
641 *
642 * If the HW RNG device /dev/hw_random is present, we require that at least
643 * 512 bytes read from it are written into Linux RNG. QA is expected to catch
644 * devices/configurations where these I/O operations are blocking for a long
645 * time. We do not reboot or halt on failures, as this is a best-effort
646 * attempt.
647 */
648static int mix_hwrng_into_linux_rng_action(int nargs, char **args)
649{
650 int result = -1;
651 int hwrandom_fd = -1;
652 int urandom_fd = -1;
653 char buf[512];
654 ssize_t chunk_size;
655 size_t total_bytes_written = 0;
656
657 hwrandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800658 open("/dev/hw_random", O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700659 if (hwrandom_fd == -1) {
660 if (errno == ENOENT) {
661 ERROR("/dev/hw_random not found\n");
662 /* It's not an error to not have a Hardware RNG. */
663 result = 0;
664 } else {
665 ERROR("Failed to open /dev/hw_random: %s\n", strerror(errno));
666 }
667 goto ret;
668 }
669
670 urandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800671 open("/dev/urandom", O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700672 if (urandom_fd == -1) {
673 ERROR("Failed to open /dev/urandom: %s\n", strerror(errno));
674 goto ret;
675 }
676
677 while (total_bytes_written < sizeof(buf)) {
678 chunk_size = TEMP_FAILURE_RETRY(
679 read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written));
680 if (chunk_size == -1) {
681 ERROR("Failed to read from /dev/hw_random: %s\n", strerror(errno));
682 goto ret;
683 } else if (chunk_size == 0) {
684 ERROR("Failed to read from /dev/hw_random: EOF\n");
685 goto ret;
686 }
687
688 chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size));
689 if (chunk_size == -1) {
690 ERROR("Failed to write to /dev/urandom: %s\n", strerror(errno));
691 goto ret;
692 }
693 total_bytes_written += chunk_size;
694 }
695
Elliott Hughesccecf142014-01-16 10:53:11 -0800696 INFO("Mixed %zu bytes from /dev/hw_random into /dev/urandom",
Alex Klyubin0d872d82013-08-16 13:19:24 -0700697 total_bytes_written);
698 result = 0;
699
700ret:
701 if (hwrandom_fd != -1) {
702 close(hwrandom_fd);
703 }
704 if (urandom_fd != -1) {
705 close(urandom_fd);
706 }
Alex Klyubin0d872d82013-08-16 13:19:24 -0700707 return result;
708}
709
Colin Crossebc6ff12010-04-13 19:52:01 -0700710static int keychord_init_action(int nargs, char **args)
711{
712 keychord_init();
713 return 0;
714}
715
716static int console_init_action(int nargs, char **args)
717{
Elliott Hughesd679bc92015-03-20 08:50:46 -0700718 char console[PROP_VALUE_MAX];
719 if (property_get("ro.boot.console", console) > 0) {
Hong-Mei Li11467182013-04-01 11:17:51 +0800720 snprintf(console_name, sizeof(console_name), "/dev/%s", console);
Colin Crossebc6ff12010-04-13 19:52:01 -0700721 }
722
Elliott Hughesd679bc92015-03-20 08:50:46 -0700723 int fd = open(console_name, O_RDWR | O_CLOEXEC);
Colin Crossebc6ff12010-04-13 19:52:01 -0700724 if (fd >= 0)
725 have_console = 1;
726 close(fd);
727
Nick Kralevich45a884f2015-02-02 14:37:22 -0800728 fd = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200729 if (fd >= 0) {
730 const char *msg;
731 msg = "\n"
732 "\n"
733 "\n"
734 "\n"
735 "\n"
736 "\n"
737 "\n" // console is 40 cols x 30 lines
738 "\n"
739 "\n"
740 "\n"
741 "\n"
742 "\n"
743 "\n"
744 "\n"
745 " A N D R O I D ";
746 write(fd, msg, strlen(msg));
747 close(fd);
Colin Crossebc6ff12010-04-13 19:52:01 -0700748 }
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200749
Colin Crossebc6ff12010-04-13 19:52:01 -0700750 return 0;
751}
752
Nick Kralevichf667a322015-04-25 17:42:52 -0700753static void import_kernel_nv(char *name, bool for_emulator)
Dima Zavin5511c842011-12-19 11:21:32 -0800754{
755 char *value = strchr(name, '=');
756 int name_len = strlen(name);
757
758 if (value == 0) return;
759 *value++ = 0;
760 if (name_len == 0) return;
761
762 if (for_emulator) {
763 /* in the emulator, export any kernel option with the
764 * ro.kernel. prefix */
765 char buff[PROP_NAME_MAX];
766 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
767
768 if (len < (int)sizeof(buff))
769 property_set( buff, value );
770 return;
771 }
772
773 if (!strcmp(name,"qemu")) {
774 strlcpy(qemu, value, sizeof(qemu));
775 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
776 const char *boot_prop_name = name + 12;
777 char prop[PROP_NAME_MAX];
778 int cnt;
779
780 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
781 if (cnt < PROP_NAME_MAX)
782 property_set(prop, value);
783 }
784}
785
Elliott Hughesd679bc92015-03-20 08:50:46 -0700786static void export_kernel_boot_props() {
Dima Zavin5511c842011-12-19 11:21:32 -0800787 struct {
788 const char *src_prop;
Elliott Hughesd679bc92015-03-20 08:50:46 -0700789 const char *dst_prop;
790 const char *default_value;
Dima Zavin5511c842011-12-19 11:21:32 -0800791 } prop_map[] = {
Elliott Hughesd679bc92015-03-20 08:50:46 -0700792 { "ro.boot.serialno", "ro.serialno", "", },
793 { "ro.boot.mode", "ro.bootmode", "unknown", },
794 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800795 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
Elliott Hughesd679bc92015-03-20 08:50:46 -0700796 { "ro.boot.hardware", "ro.hardware", "unknown", },
797 { "ro.boot.revision", "ro.revision", "0", },
Dima Zavin5511c842011-12-19 11:21:32 -0800798 };
Elliott Hughesd679bc92015-03-20 08:50:46 -0700799 for (size_t i = 0; i < ARRAY_SIZE(prop_map); i++) {
800 char value[PROP_VALUE_MAX];
801 int rc = property_get(prop_map[i].src_prop, value);
802 property_set(prop_map[i].dst_prop, (rc > 0) ? value : prop_map[i].default_value);
Dima Zavin5511c842011-12-19 11:21:32 -0800803 }
Dima Zavin5511c842011-12-19 11:21:32 -0800804}
Colin Crossebc6ff12010-04-13 19:52:01 -0700805
Rom Lemarchand6a524432015-02-28 06:39:11 -0800806static void process_kernel_dt(void)
807{
808 static const char android_dir[] = "/proc/device-tree/firmware/android";
809
810 std::string file_name = android::base::StringPrintf("%s/compatible", android_dir);
811
812 std::string dt_file;
813 android::base::ReadFileToString(file_name, &dt_file);
814 if (!dt_file.compare("android,firmware")) {
815 ERROR("firmware/android is not compatible with 'android,firmware'\n");
816 return;
817 }
818
819 std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(android_dir), closedir);
820 if (!dir)
821 return;
822
823 struct dirent *dp;
824 while ((dp = readdir(dir.get())) != NULL) {
825 if (dp->d_type != DT_REG || !strcmp(dp->d_name, "compatible"))
826 continue;
827
828 file_name = android::base::StringPrintf("%s/%s", android_dir, dp->d_name);
829
830 android::base::ReadFileToString(file_name, &dt_file);
831 std::replace(dt_file.begin(), dt_file.end(), ',', '.');
832
833 std::string property_name = android::base::StringPrintf("ro.boot.%s", dp->d_name);
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700834 property_set(property_name.c_str(), dt_file.c_str());
Rom Lemarchand6a524432015-02-28 06:39:11 -0800835 }
836}
837
Dima Zavin5511c842011-12-19 11:21:32 -0800838static void process_kernel_cmdline(void)
839{
840 /* don't expose the raw commandline to nonpriv processes */
841 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700842
Dima Zavin5511c842011-12-19 11:21:32 -0800843 /* first pass does the common stuff, and finds if we are in qemu.
844 * second pass is only necessary for qemu to export all kernel params
845 * as props.
846 */
Nick Kralevichf667a322015-04-25 17:42:52 -0700847 import_kernel_cmdline(false, import_kernel_nv);
Dima Zavin5511c842011-12-19 11:21:32 -0800848 if (qemu[0])
Nick Kralevichf667a322015-04-25 17:42:52 -0700849 import_kernel_cmdline(true, import_kernel_nv);
Colin Crossebc6ff12010-04-13 19:52:01 -0700850}
851
Colin Crossebc6ff12010-04-13 19:52:01 -0700852static int queue_property_triggers_action(int nargs, char **args)
853{
854 queue_all_property_triggers();
855 /* enable property triggers */
856 property_triggers_enabled = 1;
857 return 0;
858}
859
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700860static void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500861{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400862 sehandle = selinux_android_file_context_handle();
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500863 selinux_android_set_sehandle(sehandle);
rpcraig63207cd2012-08-09 10:05:49 -0400864 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400865}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500866
Nick Kralevichf667a322015-04-25 17:42:52 -0700867enum selinux_enforcing_status { SELINUX_DISABLED, SELINUX_PERMISSIVE, SELINUX_ENFORCING };
868
869static selinux_enforcing_status selinux_status_from_cmdline() {
870 selinux_enforcing_status status = SELINUX_ENFORCING;
871
872 std::function<void(char*,bool)> fn = [&](char* name, bool in_qemu) {
873 char *value = strchr(name, '=');
874 if (value == nullptr) { return; }
875 *value++ = '\0';
876 if (strcmp(name, "androidboot.selinux") == 0) {
877 if (strcmp(value, "disabled") == 0) {
878 status = SELINUX_DISABLED;
879 } else if (strcmp(value, "permissive") == 0) {
880 status = SELINUX_PERMISSIVE;
881 }
882 }
883 };
884 import_kernel_cmdline(false, fn);
885
886 return status;
887}
888
889
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700890static bool selinux_is_disabled(void)
891{
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800892 if (ALLOW_DISABLE_SELINUX) {
893 if (access("/sys/fs/selinux", F_OK) != 0) {
894 // SELinux is not compiled into the kernel, or has been disabled
895 // via the kernel command line "selinux=0".
896 return true;
897 }
Nick Kralevichf667a322015-04-25 17:42:52 -0700898 return selinux_status_from_cmdline() == SELINUX_DISABLED;
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700899 }
900
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700901 return false;
902}
903
904static bool selinux_is_enforcing(void)
905{
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800906 if (ALLOW_DISABLE_SELINUX) {
Nick Kralevichf667a322015-04-25 17:42:52 -0700907 return selinux_status_from_cmdline() == SELINUX_ENFORCING;
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700908 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700909 return true;
910}
911
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400912int selinux_reload_policy(void)
913{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700914 if (selinux_is_disabled()) {
915 return -1;
916 }
917
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400918 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500919
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400920 if (selinux_android_reload_policy() == -1) {
921 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500922 }
923
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400924 if (sehandle)
925 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500926
rpcraig63207cd2012-08-09 10:05:49 -0400927 if (sehandle_prop)
928 selabel_close(sehandle_prop);
929
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400930 selinux_init_all_handles();
931 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500932}
rpcraig63207cd2012-08-09 10:05:49 -0400933
Elliott Hughesda40c002015-03-27 23:20:44 -0700934static int audit_callback(void *data, security_class_t /*cls*/, char *buf, size_t len) {
rpcraig63207cd2012-08-09 10:05:49 -0400935 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
936 return 0;
937}
938
Elliott Hughesf65730e2015-04-24 18:38:17 +0000939static void security_failure() {
940 ERROR("Security failure; rebooting into recovery mode...\n");
941 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
942 while (true) { pause(); } // never reached
943}
944
945static void selinux_initialize(bool in_kernel_domain) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700946 Timer t;
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500947
Elliott Hughesda40c002015-03-27 23:20:44 -0700948 selinux_callback cb;
949 cb.func_log = selinux_klog_callback;
950 selinux_set_callback(SELINUX_CB_LOG, cb);
951 cb.func_audit = audit_callback;
952 selinux_set_callback(SELINUX_CB_AUDIT, cb);
953
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700954 if (selinux_is_disabled()) {
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700955 return;
956 }
957
Elliott Hughesf65730e2015-04-24 18:38:17 +0000958 if (in_kernel_domain) {
Elliott Hughesf65730e2015-04-24 18:38:17 +0000959 INFO("Loading SELinux policy...\n");
960 if (selinux_android_load_policy() < 0) {
961 ERROR("failed to load policy: %s\n", strerror(errno));
962 security_failure();
963 }
964
965 bool is_enforcing = selinux_is_enforcing();
966 security_setenforce(is_enforcing);
967
Nick Kralevicheedbe812015-04-25 14:10:03 -0700968 if (write_file("/sys/fs/selinux/checkreqprot", "0") == -1) {
969 security_failure();
970 }
971
Elliott Hughesf65730e2015-04-24 18:38:17 +0000972 NOTICE("(Initializing SELinux %s took %.2fs.)\n",
973 is_enforcing ? "enforcing" : "non-enforcing", t.duration());
974 } else {
975 selinux_init_all_handles();
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700976 }
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700977}
978
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800979int main(int argc, char** argv) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700980 if (!strcmp(basename(argv[0]), "ueventd")) {
Colin Crossf83d0b92010-04-21 12:04:20 -0700981 return ueventd_main(argc, argv);
Elliott Hughesda40c002015-03-27 23:20:44 -0700982 }
Colin Crossf83d0b92010-04-21 12:04:20 -0700983
Elliott Hughesda40c002015-03-27 23:20:44 -0700984 if (!strcmp(basename(argv[0]), "watchdogd")) {
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700985 return watchdogd_main(argc, argv);
Elliott Hughesda40c002015-03-27 23:20:44 -0700986 }
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700987
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 Hughescc86fb22015-03-17 20:01:13 -0700991 add_environment("PATH", _PATH_DEFPATH);
992
Nick Kralevich9dec93b2015-04-25 15:50:03 -0700993 bool is_first_stage = (argc == 1) || (strcmp(argv[1], "--second-stage") != 0);
994
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800995 // Get the basic filesystem setup we need put together in the initramdisk
996 // on / and then we'll let the rc file figure out the rest.
Nick Kralevich9dec93b2015-04-25 15:50:03 -0700997 if (is_first_stage) {
Nick Kralevichf667a322015-04-25 17:42:52 -0700998 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
999 mkdir("/dev/pts", 0755);
1000 mkdir("/dev/socket", 0755);
1001 mount("devpts", "/dev/pts", "devpts", 0, NULL);
Nick Kralevich9dec93b2015-04-25 15:50:03 -07001002 mount("proc", "/proc", "proc", 0, NULL);
1003 mount("sysfs", "/sys", "sysfs", 0, NULL);
1004 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001005
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001006 // We must have some place other than / to create the device nodes for
1007 // kmsg and null, otherwise we won't be able to remount / read-only
1008 // later on. Now that tmpfs is mounted on /dev, we can actually talk
1009 // to the outside world.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001010 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -07001011 klog_init();
Elliott Hughesda40c002015-03-27 23:20:44 -07001012 klog_set_level(KLOG_NOTICE_LEVEL);
1013
Elliott Hughesf65730e2015-04-24 18:38:17 +00001014 NOTICE("init%s started!\n", is_first_stage ? "" : " second stage");
Elliott Hughesda40c002015-03-27 23:20:44 -07001015
Nick Kralevichf667a322015-04-25 17:42:52 -07001016 if (!is_first_stage) {
1017 // Indicate that booting is in progress to background fw loaders, etc.
1018 close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -07001019
Nick Kralevichf667a322015-04-25 17:42:52 -07001020 property_init();
Dima Zavin5511c842011-12-19 11:21:32 -08001021
Nick Kralevichf667a322015-04-25 17:42:52 -07001022 // If arguments are passed both on the command line and in DT,
1023 // properties set in DT always have priority over the command-line ones.
1024 process_kernel_dt();
1025 process_kernel_cmdline();
1026
1027 // Propogate the kernel variables to internal variables
1028 // used by init as well as the current required properties.
1029 export_kernel_boot_props();
1030 }
Rom Lemarchand6a524432015-02-28 06:39:11 -08001031
Elliott Hughesf65730e2015-04-24 18:38:17 +00001032 // Set up SELinux, including loading the SELinux policy if we're in the kernel domain.
1033 selinux_initialize(is_first_stage);
1034
1035 // If we're in the kernel domain, re-exec init to transition to the init domain now
1036 // that the SELinux policy has been loaded.
1037 if (is_first_stage) {
1038 if (restorecon("/init") == -1) {
1039 ERROR("restorecon failed: %s\n", strerror(errno));
1040 security_failure();
1041 }
1042 char* path = argv[0];
1043 char* args[] = { path, const_cast<char*>("--second-stage"), nullptr };
1044 if (execv(path, args) == -1) {
1045 ERROR("execv(\"%s\") failed: %s\n", path, strerror(errno));
1046 security_failure();
1047 }
1048 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001049
1050 // These directories were necessarily created before initial policy load
1051 // and therefore need their security context restored to the proper value.
1052 // This must happen before /dev is populated by ueventd.
Elliott Hughesda40c002015-03-27 23:20:44 -07001053 INFO("Running restorecon...\n");
Stephen Smalleye096e362012-06-11 13:37:39 -04001054 restorecon("/dev");
1055 restorecon("/dev/socket");
Geremy Condra8e15eab2013-02-28 17:29:58 -08001056 restorecon("/dev/__properties__");
Nick Kralevichae76f6d2013-07-11 15:38:26 -07001057 restorecon_recursive("/sys");
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001058
Elliott Hughes929f4072015-04-24 21:13:44 -07001059 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
1060 if (epoll_fd == -1) {
1061 ERROR("epoll_create1 failed: %s\n", strerror(errno));
1062 exit(1);
1063 }
1064
1065 signal_handler_init();
Elliott Hughes9042cae2015-04-24 17:43:21 -07001066
Riley Andrewse4b7b292014-06-16 15:06:21 -07001067 property_load_boot_defaults();
Elliott Hughesc6c26ed2015-04-24 18:50:30 -07001068 start_property_service();
Dima Zavind7634c92011-12-16 14:18:06 -08001069
Dima Zavind7634c92011-12-16 14:18:06 -08001070 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001071
1072 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001073
Elliott Hughesc6c26ed2015-04-24 18:50:30 -07001074 // 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 -07001075 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Elliott Hughesc6c26ed2015-04-24 18:50:30 -07001076 // ... so that we can start queuing up actions that require stuff from /dev.
Alex Klyubin0d872d82013-08-16 13:19:24 -07001077 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001078 queue_builtin_action(keychord_init_action, "keychord_init");
1079 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001080
Elliott Hughesda40c002015-03-27 23:20:44 -07001081 // Trigger all the boot actions to get us started.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001082 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001083
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001084 // Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
1085 // wasn't ready immediately after wait_for_coldboot_done
Alex Klyubin0d872d82013-08-16 13:19:24 -07001086 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001087
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001088 // Don't mount filesystems or start core system services in charger mode.
Elliott Hughesd679bc92015-03-20 08:50:46 -07001089 char bootmode[PROP_VALUE_MAX];
1090 if (property_get("ro.bootmode", bootmode) > 0 && strcmp(bootmode, "charger") == 0) {
Dima Zavinca47cef2011-08-24 15:28:23 -07001091 action_for_each_trigger("charger", action_add_queue_tail);
1092 } else {
Riley Andrewse4b7b292014-06-16 15:06:21 -07001093 action_for_each_trigger("late-init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001094 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001095
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001096 // Run all property triggers based on current state of the properties.
Chris Dearman469b7b22012-03-01 15:29:20 -08001097 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001098
Elliott Hughesc6c26ed2015-04-24 18:50:30 -07001099 while (true) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001100 if (!waiting_for_exec) {
1101 execute_one_command();
1102 restart_processes();
1103 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001104
Yongqin Liua197ff12014-12-05 13:45:02 +08001105 int timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001106 if (process_needs_restart) {
1107 timeout = (process_needs_restart - gettime()) * 1000;
1108 if (timeout < 0)
1109 timeout = 0;
1110 }
1111
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001112 if (!action_queue_empty() || cur_action) {
Colin Crossebc6ff12010-04-13 19:52:01 -07001113 timeout = 0;
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001114 }
Colin Crossebc6ff12010-04-13 19:52:01 -07001115
Yongqin Liua197ff12014-12-05 13:45:02 +08001116 bootchart_sample(&timeout);
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001117
Elliott Hughes929f4072015-04-24 21:13:44 -07001118 epoll_event ev;
1119 int nr = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd, &ev, 1, timeout));
1120 if (nr == -1) {
1121 ERROR("epoll_wait failed: %s\n", strerror(errno));
1122 } else if (nr == 1) {
1123 ((void (*)()) ev.data.ptr)();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001124 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001125 }
1126
1127 return 0;
1128}