blob: 4be16ea00b5fcc09aded65a7afed26ea902cc211 [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>
Elliott Hughese5ce30f2015-05-06 19:19:24 -070046#include <base/strings.h>
Nick Kralevich56fa0ac2013-06-24 17:41:40 -070047#include <cutils/android_reboot.h>
Alex Klyubin0d872d82013-08-16 13:19:24 -070048#include <cutils/fs.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080049#include <cutils/iosched_policy.h>
50#include <cutils/list.h>
51#include <cutils/sockets.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070052#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070053
Rom Lemarchand6a524432015-02-28 06:39:11 -080054#include <memory>
55
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070056#include "devices.h"
57#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070058#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070059#include "property_service.h"
The Android Open Source Project35237d12008-12-17 18:08:08 -080060#include "bootchart.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070061#include "signal_handler.h"
Colin Crossa8666952010-04-13 19:20:44 -070062#include "keychords.h"
Colin Cross6310a822010-04-20 14:29:05 -070063#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070064#include "util.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070065#include "ueventd.h"
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070066#include "watchdogd.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070067
Stephen Smalleye46f9d52012-01-13 08:48:47 -050068struct selabel_handle *sehandle;
rpcraig63207cd2012-08-09 10:05:49 -040069struct selabel_handle *sehandle_prop;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050070
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070071static int property_triggers_enabled = 0;
72
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073static char qemu[32];
74
Colin Crossebc6ff12010-04-13 19:52:01 -070075static struct action *cur_action = NULL;
76static struct command *cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -070077
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070078static int have_console;
Yabin Cui74edcea2015-07-24 10:11:05 -070079static std::string console_name = "/dev/console";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070080static time_t process_needs_restart;
81
82static const char *ENV[32];
83
Elliott Hughes8d82ea02015-02-06 20:15:18 -080084bool waiting_for_exec = false;
85
Elliott Hughes929f4072015-04-24 21:13:44 -070086static int epoll_fd = -1;
87
88void register_epoll_handler(int fd, void (*fn)()) {
89 epoll_event ev;
90 ev.events = EPOLLIN;
91 ev.data.ptr = reinterpret_cast<void*>(fn);
92 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
93 ERROR("epoll_ctl failed: %s\n", strerror(errno));
94 }
95}
96
Elliott Hughes8d82ea02015-02-06 20:15:18 -080097void service::NotifyStateChange(const char* new_state) {
Elliott Hughesda40c002015-03-27 23:20:44 -070098 if (!properties_initialized()) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -080099 // If properties aren't available yet, we can't set them.
100 return;
101 }
102
103 if ((flags & SVC_EXEC) != 0) {
104 // 'exec' commands don't have properties tracking their state.
105 return;
106 }
107
108 char prop_name[PROP_NAME_MAX];
109 if (snprintf(prop_name, sizeof(prop_name), "init.svc.%s", name) >= PROP_NAME_MAX) {
110 // If the property name would be too long, we can't set it.
111 ERROR("Property name \"init.svc.%s\" too long; not setting to %s\n", name, new_state);
112 return;
113 }
114
115 property_set(prop_name, new_state);
116}
117
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700118/* add_environment - add "key=value" to the current environment */
119int add_environment(const char *key, const char *val)
120{
James Morrissey381341f2014-05-16 11:36:36 +0100121 size_t n;
122 size_t key_len = strlen(key);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700123
James Morrissey381341f2014-05-16 11:36:36 +0100124 /* The last environment entry is reserved to terminate the list */
125 for (n = 0; n < (ARRAY_SIZE(ENV) - 1); n++) {
126
127 /* Delete any existing entry for this key */
128 if (ENV[n] != NULL) {
129 size_t entry_key_len = strcspn(ENV[n], "=");
130 if ((entry_key_len == key_len) && (strncmp(ENV[n], key, entry_key_len) == 0)) {
131 free((char*)ENV[n]);
132 ENV[n] = NULL;
133 }
134 }
135
136 /* Add entry if a free slot is available */
137 if (ENV[n] == NULL) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800138 char* entry;
139 asprintf(&entry, "%s=%s", key, val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700140 ENV[n] = entry;
141 return 0;
142 }
143 }
144
James Morrissey381341f2014-05-16 11:36:36 +0100145 ERROR("No env. room to store: '%s':'%s'\n", key, val);
146
147 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700148}
149
San Mehat429721c2014-09-23 07:48:47 -0700150void zap_stdio(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700151{
152 int fd;
153 fd = open("/dev/null", O_RDWR);
154 dup2(fd, 0);
155 dup2(fd, 1);
156 dup2(fd, 2);
157 close(fd);
158}
159
160static void open_console()
161{
162 int fd;
Yabin Cui74edcea2015-07-24 10:11:05 -0700163 if ((fd = open(console_name.c_str(), O_RDWR)) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700164 fd = open("/dev/null", O_RDWR);
165 }
Colin Cross50fb5a62012-03-18 15:38:19 -0700166 ioctl(fd, TIOCSCTTY, 0);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700167 dup2(fd, 0);
168 dup2(fd, 1);
169 dup2(fd, 2);
170 close(fd);
171}
172
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700173static void publish_socket(const char *name, int fd)
174{
175 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
176 char val[64];
177
178 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
179 name,
180 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
181 snprintf(val, sizeof(val), "%d", fd);
182 add_environment(key, val);
183
184 /* make sure we don't close-on-exec */
185 fcntl(fd, F_SETFD, 0);
186}
187
San Mehatf24e2522009-05-19 13:30:46 -0700188void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700189{
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800190 // Starting a service removes it from the disabled or reset state and
191 // immediately takes it out of the restarting state if it was in there.
JP Abgrall3beec7e2014-05-02 21:14:29 -0700192 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700193 svc->time_started = 0;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700194
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800195 // Running processes require no additional work --- if they're in the
196 // process of exiting, we've ensured that they will immediately restart
197 // on exit, unless they are ONESHOT.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700198 if (svc->flags & SVC_RUNNING) {
199 return;
200 }
201
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800202 bool needs_console = (svc->flags & SVC_CONSOLE);
203 if (needs_console && !have_console) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700204 ERROR("service '%s' requires console\n", svc->name);
205 svc->flags |= SVC_DISABLED;
206 return;
207 }
208
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700209 struct stat sb;
210 if (stat(svc->args[0], &sb) == -1) {
211 ERROR("cannot find '%s' (%s), disabling '%s'\n", svc->args[0], strerror(errno), svc->name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700212 svc->flags |= SVC_DISABLED;
213 return;
214 }
215
San Mehatf24e2522009-05-19 13:30:46 -0700216 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700217 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n", 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;
Nick Kralevich4d870952015-06-12 22:03:50 -0700223 if (svc->seclabel) {
224 scon = strdup(svc->seclabel);
225 if (!scon) {
226 ERROR("Out of memory while starting '%s'\n", svc->name);
227 return;
228 }
229 } else {
230 char *mycon = NULL, *fcon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500231
Nick Kralevich4d870952015-06-12 22:03:50 -0700232 INFO("computing context for service '%s'\n", svc->args[0]);
233 int rc = getcon(&mycon);
234 if (rc < 0) {
235 ERROR("could not get context while starting '%s'\n", svc->name);
236 return;
237 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500238
Nick Kralevich4d870952015-06-12 22:03:50 -0700239 rc = getfilecon(svc->args[0], &fcon);
240 if (rc < 0) {
241 ERROR("could not get context while starting '%s'\n", svc->name);
Nick Kralevich4800dbf2015-07-16 10:49:51 -0700242 free(mycon);
Nick Kralevich4d870952015-06-12 22:03:50 -0700243 return;
244 }
245
246 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon);
247 if (rc == 0 && !strcmp(scon, mycon)) {
Nick Kralevich4800dbf2015-07-16 10:49:51 -0700248 ERROR("Service %s does not have a SELinux domain defined.\n", svc->name);
249 free(mycon);
250 free(fcon);
251 free(scon);
252 return;
Nick Kralevich4d870952015-06-12 22:03:50 -0700253 }
Nick Kralevich4800dbf2015-07-16 10:49:51 -0700254 free(mycon);
255 free(fcon);
Nick Kralevich4d870952015-06-12 22:03:50 -0700256 if (rc < 0) {
257 ERROR("could not get context while starting '%s'\n", svc->name);
258 return;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500259 }
260 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500261
Elliott Hughesda40c002015-03-27 23:20:44 -0700262 NOTICE("Starting service '%s'...\n", svc->name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700263
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800264 pid_t pid = fork();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700265 if (pid == 0) {
266 struct socketinfo *si;
267 struct svcenvinfo *ei;
268 char tmp[32];
269 int fd, sz;
270
Nick Kralevich6ebf12f2012-03-26 09:09:11 -0700271 umask(077);
Elliott Hughesda40c002015-03-27 23:20:44 -0700272 if (properties_initialized()) {
Colin Cross3294bbb2010-04-19 17:11:33 -0700273 get_property_workspace(&fd, &sz);
Yabin Cuie2d63af2015-02-17 19:27:51 -0800274 snprintf(tmp, sizeof(tmp), "%d,%d", dup(fd), sz);
Colin Cross3294bbb2010-04-19 17:11:33 -0700275 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
276 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700277
278 for (ei = svc->envvars; ei; ei = ei->next)
279 add_environment(ei->name, ei->value);
280
281 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400282 int socket_type = (
283 !strcmp(si->type, "stream") ? SOCK_STREAM :
284 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
285 int s = create_socket(si->name, socket_type,
Stephen Smalley8348d272013-05-13 12:37:04 -0400286 si->perm, si->uid, si->gid, si->socketcon ?: scon);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700287 if (s >= 0) {
288 publish_socket(si->name, s);
289 }
290 }
291
Nick Kralevich4800dbf2015-07-16 10:49:51 -0700292 free(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500293 scon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500294
Elliott Hughesd62f0602015-06-12 18:02:20 -0700295 if (svc->writepid_files_) {
296 std::string pid_str = android::base::StringPrintf("%d", pid);
297 for (auto& file : *svc->writepid_files_) {
298 if (!android::base::WriteStringToFile(pid_str, file)) {
299 ERROR("couldn't write %s to %s: %s\n",
300 pid_str.c_str(), file.c_str(), strerror(errno));
301 }
302 }
303 }
304
San Mehat4e221f02010-02-25 14:19:50 -0800305 if (svc->ioprio_class != IoSchedClass_NONE) {
306 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
307 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
308 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
309 }
310 }
311
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700312 if (needs_console) {
313 setsid();
314 open_console();
315 } else {
316 zap_stdio();
317 }
318
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800319 if (false) {
320 for (size_t n = 0; svc->args[n]; n++) {
321 INFO("args[%zu] = '%s'\n", n, svc->args[n]);
322 }
323 for (size_t n = 0; ENV[n]; n++) {
324 INFO("env[%zu] = '%s'\n", n, ENV[n]);
325 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700326 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700327
328 setpgid(0, getpid());
329
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800330 // As requested, set our gid, supplemental gids, and uid.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700331 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800332 if (setgid(svc->gid) != 0) {
333 ERROR("setgid failed: %s\n", strerror(errno));
334 _exit(127);
335 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700336 }
337 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800338 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
339 ERROR("setgroups failed: %s\n", strerror(errno));
340 _exit(127);
341 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700342 }
343 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800344 if (setuid(svc->uid) != 0) {
345 ERROR("setuid failed: %s\n", strerror(errno));
346 _exit(127);
347 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700348 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500349 if (svc->seclabel) {
Nick Kralevich4d870952015-06-12 22:03:50 -0700350 if (setexeccon(svc->seclabel) < 0) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500351 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
352 _exit(127);
353 }
354 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500355
San Mehat8ad15682009-05-20 08:50:40 -0700356 if (!dynamic_args) {
357 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
358 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
359 }
360 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700361 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700362 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700363 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700364 char *next = tmp;
365 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700366
367 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700368 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700369
San Mehatd4cdd132009-05-20 09:52:16 -0700370 while((bword = strsep(&next, " "))) {
371 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700372 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700373 break;
San Mehatf24e2522009-05-19 13:30:46 -0700374 }
Andreas Gampe0ab46c92015-02-03 11:20:49 -0800375 arg_ptrs[arg_idx] = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700376 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100377 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700378 _exit(127);
379 }
380
Nick Kralevich4800dbf2015-07-16 10:49:51 -0700381 free(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500382
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700383 if (pid < 0) {
384 ERROR("failed to start '%s'\n", svc->name);
385 svc->pid = 0;
386 return;
387 }
388
389 svc->time_started = gettime();
390 svc->pid = pid;
391 svc->flags |= SVC_RUNNING;
392
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800393 if ((svc->flags & SVC_EXEC) != 0) {
Elliott Hughes58c3bac2015-03-11 12:31:53 -0700394 INFO("SVC_EXEC pid %d (uid %d gid %d+%zu context %s) started; waiting...\n",
Mark Salyzyn17fff892015-06-02 11:11:02 -0700395 svc->pid, svc->uid, svc->gid, svc->nr_supp_gids,
396 svc->seclabel ? : "default");
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800397 waiting_for_exec = true;
398 }
399
400 svc->NotifyStateChange("running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700401}
402
Mike Kasickb54f39f2012-01-25 23:48:46 -0500403/* The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART */
Ken Sumrall752923c2010-12-03 16:33:31 -0800404static void service_stop_or_reset(struct service *svc, int how)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700405{
Mike Kasick7e36edd2012-02-06 10:32:13 -0500406 /* The service is still SVC_RUNNING until its process exits, but if it has
407 * already exited it shoudn't attempt a restart yet. */
JP Abgrall3beec7e2014-05-02 21:14:29 -0700408 svc->flags &= ~(SVC_RESTARTING | SVC_DISABLED_START);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700409
Mike Kasickb54f39f2012-01-25 23:48:46 -0500410 if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) {
Ken Sumrall752923c2010-12-03 16:33:31 -0800411 /* Hrm, an illegal flag. Default to SVC_DISABLED */
412 how = SVC_DISABLED;
413 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700414 /* if the service has not yet started, prevent
415 * it from auto-starting with its class
416 */
Ken Sumralla2864802011-10-26 16:56:00 -0700417 if (how == SVC_RESET) {
418 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
419 } else {
420 svc->flags |= how;
421 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700422
423 if (svc->pid) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700424 NOTICE("Service '%s' is being killed...\n", svc->name);
Ken Sumrall752923c2010-12-03 16:33:31 -0800425 kill(-svc->pid, SIGKILL);
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800426 svc->NotifyStateChange("stopping");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700427 } else {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800428 svc->NotifyStateChange("stopped");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700429 }
430}
431
Ken Sumrall752923c2010-12-03 16:33:31 -0800432void service_reset(struct service *svc)
433{
434 service_stop_or_reset(svc, SVC_RESET);
435}
436
437void service_stop(struct service *svc)
438{
439 service_stop_or_reset(svc, SVC_DISABLED);
440}
441
Mike Kasickb54f39f2012-01-25 23:48:46 -0500442void service_restart(struct service *svc)
443{
444 if (svc->flags & SVC_RUNNING) {
445 /* Stop, wait, then start the service. */
446 service_stop_or_reset(svc, SVC_RESTART);
447 } else if (!(svc->flags & SVC_RESTARTING)) {
448 /* Just start the service since it's not running. */
449 service_start(svc, NULL);
450 } /* else: Service is restarting anyways. */
451}
452
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700453void property_changed(const char *name, const char *value)
454{
Colin Crossebc6ff12010-04-13 19:52:01 -0700455 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700456 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700457}
458
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700459static void restart_service_if_needed(struct service *svc)
460{
461 time_t next_start_time = svc->time_started + 5;
462
463 if (next_start_time <= gettime()) {
464 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700465 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700466 return;
467 }
468
469 if ((next_start_time < process_needs_restart) ||
470 (process_needs_restart == 0)) {
471 process_needs_restart = next_start_time;
472 }
473}
474
475static void restart_processes()
476{
477 process_needs_restart = 0;
478 service_for_each_flags(SVC_RESTARTING,
479 restart_service_if_needed);
480}
481
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700482static void msg_start(const char *name)
483{
Hong-Mei Li11467182013-04-01 11:17:51 +0800484 struct service *svc = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700485 char *tmp = NULL;
486 char *args = NULL;
487
488 if (!strchr(name, ':'))
489 svc = service_find_by_name(name);
490 else {
491 tmp = strdup(name);
Hong-Mei Li11467182013-04-01 11:17:51 +0800492 if (tmp) {
493 args = strchr(tmp, ':');
494 *args = '\0';
495 args++;
San Mehatf24e2522009-05-19 13:30:46 -0700496
Hong-Mei Li11467182013-04-01 11:17:51 +0800497 svc = service_find_by_name(tmp);
498 }
San Mehatf24e2522009-05-19 13:30:46 -0700499 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700500
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700501 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700502 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700503 } else {
504 ERROR("no such service '%s'\n", name);
505 }
San Mehatf24e2522009-05-19 13:30:46 -0700506 if (tmp)
507 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700508}
509
510static void msg_stop(const char *name)
511{
512 struct service *svc = service_find_by_name(name);
513
514 if (svc) {
515 service_stop(svc);
516 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700517 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700518 }
519}
520
Mike Kasickb54f39f2012-01-25 23:48:46 -0500521static void msg_restart(const char *name)
522{
523 struct service *svc = service_find_by_name(name);
524
525 if (svc) {
526 service_restart(svc);
527 } else {
528 ERROR("no such service '%s'\n", name);
529 }
530}
531
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700532void handle_control_message(const char *msg, const char *arg)
533{
534 if (!strcmp(msg,"start")) {
535 msg_start(arg);
536 } else if (!strcmp(msg,"stop")) {
537 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700538 } else if (!strcmp(msg,"restart")) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500539 msg_restart(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700540 } else {
541 ERROR("unknown control msg '%s'\n", msg);
542 }
543}
544
Colin Crossebc6ff12010-04-13 19:52:01 -0700545static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700546{
547 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700548 node = list_head(&act->commands);
Dima Zavin3bea0792011-08-26 13:59:18 -0700549 if (!node || list_empty(&act->commands))
Colin Crossebc6ff12010-04-13 19:52:01 -0700550 return NULL;
551
552 return node_to_item(node, struct command, clist);
553}
554
555static struct command *get_next_command(struct action *act, struct command *cmd)
556{
557 struct listnode *node;
558 node = cmd->clist.next;
559 if (!node)
560 return NULL;
561 if (node == &act->commands)
562 return NULL;
563
564 return node_to_item(node, struct command, clist);
565}
566
567static int is_last_command(struct action *act, struct command *cmd)
568{
569 return (list_tail(&act->commands) == &cmd->clist);
570}
571
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700572
Yabin Cui00ede7d2015-07-24 13:26:04 -0700573std::string build_triggers_string(struct action *cur_action) {
574 std::string result;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700575 struct listnode *node;
576 struct trigger *cur_trigger;
577
578 list_for_each(node, &cur_action->triggers) {
579 cur_trigger = node_to_item(node, struct trigger, nlist);
580 if (node != cur_action->triggers.next) {
Yabin Cui00ede7d2015-07-24 13:26:04 -0700581 result.push_back(' ');
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700582 }
Yabin Cui00ede7d2015-07-24 13:26:04 -0700583 result += cur_trigger->name;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700584 }
Yabin Cui00ede7d2015-07-24 13:26:04 -0700585 return result;
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700586}
587
Yabin Cuiee530062015-07-24 18:17:16 -0700588bool expand_command_arguments(int nargs, char** args, std::vector<std::string>* expanded_args) {
589 std::vector<std::string>& strs = *expanded_args;
590 strs.resize(nargs);
591 strs[0] = args[0];
592 for (int i = 1; i < nargs; ++i) {
593 if (expand_props(args[i], &strs[i]) == -1) {
594 ERROR("%s: cannot expand '%s'\n", args[0], args[i]);
595 return false;
596 }
597 }
598 return true;
599}
600
Elliott Hughesda40c002015-03-27 23:20:44 -0700601void execute_one_command() {
602 Timer t;
603
Colin Crossebc6ff12010-04-13 19:52:01 -0700604 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
605 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700606 cur_command = NULL;
Elliott Hughesda40c002015-03-27 23:20:44 -0700607 if (!cur_action) {
Colin Crossebc6ff12010-04-13 19:52:01 -0700608 return;
Elliott Hughesda40c002015-03-27 23:20:44 -0700609 }
Badhri Jagan Sridharan0b415122014-10-10 23:19:06 -0700610
Yabin Cui00ede7d2015-07-24 13:26:04 -0700611 std::string trigger_name = build_triggers_string(cur_action);
612 INFO("processing action %p (%s)\n", cur_action, trigger_name.c_str());
Colin Crossebc6ff12010-04-13 19:52:01 -0700613 cur_command = get_first_command(cur_action);
614 } else {
615 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700616 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700617
Elliott Hughesda40c002015-03-27 23:20:44 -0700618 if (!cur_command) {
Colin Crossebc6ff12010-04-13 19:52:01 -0700619 return;
Elliott Hughesda40c002015-03-27 23:20:44 -0700620 }
Yabin Cui00ede7d2015-07-24 13:26:04 -0700621 int result = 0;
Yabin Cuiee530062015-07-24 18:17:16 -0700622 std::vector<std::string> arg_strs;
623 if (!expand_command_arguments(cur_command->nargs, cur_command->args, &arg_strs)) {
624 result = -EINVAL;
Yabin Cui00ede7d2015-07-24 13:26:04 -0700625 }
626 if (result == 0) {
627 std::vector<char*> args;
628 for (auto& s : arg_strs) {
629 args.push_back(&s[0]);
630 }
631 result = cur_command->func(args.size(), &args[0]);
632 }
Riley Andrews24a3b782014-06-26 13:56:01 -0700633 if (klog_get_level() >= KLOG_INFO_LEVEL) {
Yabin Cui00ede7d2015-07-24 13:26:04 -0700634 std::string cmd_str;
635 for (int i = 0; i < cur_command->nargs; ++i) {
636 if (i > 0) {
637 cmd_str.push_back(' ');
Riley Andrews24a3b782014-06-26 13:56:01 -0700638 }
Yabin Cui00ede7d2015-07-24 13:26:04 -0700639 cmd_str += cur_command->args[i];
Riley Andrews24a3b782014-06-26 13:56:01 -0700640 }
Yabin Cui00ede7d2015-07-24 13:26:04 -0700641 std::string trigger_name = build_triggers_string(cur_action);
642
643 std::string source;
Elliott Hughesda40c002015-03-27 23:20:44 -0700644 if (cur_command->filename) {
Yabin Cui00ede7d2015-07-24 13:26:04 -0700645 source = android::base::StringPrintf(" (%s:%d)", cur_command->filename, cur_command->line);
Elliott Hughesda40c002015-03-27 23:20:44 -0700646 }
Yabin Cui00ede7d2015-07-24 13:26:04 -0700647
Elliott Hughesda40c002015-03-27 23:20:44 -0700648 INFO("Command '%s' action=%s%s returned %d took %.2fs\n",
Yabin Cui00ede7d2015-07-24 13:26:04 -0700649 cmd_str.c_str(), trigger_name.c_str(), source.c_str(), result, t.duration());
Riley Andrews24a3b782014-06-26 13:56:01 -0700650 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700651}
652
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700653static int wait_for_coldboot_done_action(int nargs, char **args) {
654 Timer t;
655
656 NOTICE("Waiting for %s...\n", COLDBOOT_DONE);
Elliott Hughesc7331d02015-05-14 12:03:14 -0700657 // Any longer than 1s is an unreasonable length of time to delay booting.
658 // If you're hitting this timeout, check that you didn't make your
659 // sepolicy regular expressions too expensive (http://b/19899875).
660 if (wait_for_file(COLDBOOT_DONE, 1)) {
Andreas Gampea016c422014-11-24 19:52:41 -0800661 ERROR("Timed out waiting for %s\n", COLDBOOT_DONE);
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700662 }
663
664 NOTICE("Waiting for %s took %.2fs.\n", COLDBOOT_DONE, t.duration());
665 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700666}
667
Alex Klyubin0d872d82013-08-16 13:19:24 -0700668/*
669 * Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed
670 * by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom.
671 * Does nothing if Hardware RNG is not present.
672 *
673 * Since we don't yet trust the quality of Hardware RNG, these bytes are not
674 * mixed into the primary pool of Linux RNG and the entropy estimate is left
675 * unmodified.
676 *
677 * If the HW RNG device /dev/hw_random is present, we require that at least
678 * 512 bytes read from it are written into Linux RNG. QA is expected to catch
679 * devices/configurations where these I/O operations are blocking for a long
680 * time. We do not reboot or halt on failures, as this is a best-effort
681 * attempt.
682 */
683static int mix_hwrng_into_linux_rng_action(int nargs, char **args)
684{
685 int result = -1;
686 int hwrandom_fd = -1;
687 int urandom_fd = -1;
688 char buf[512];
689 ssize_t chunk_size;
690 size_t total_bytes_written = 0;
691
692 hwrandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800693 open("/dev/hw_random", O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700694 if (hwrandom_fd == -1) {
695 if (errno == ENOENT) {
696 ERROR("/dev/hw_random not found\n");
697 /* It's not an error to not have a Hardware RNG. */
698 result = 0;
699 } else {
700 ERROR("Failed to open /dev/hw_random: %s\n", strerror(errno));
701 }
702 goto ret;
703 }
704
705 urandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800706 open("/dev/urandom", O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700707 if (urandom_fd == -1) {
708 ERROR("Failed to open /dev/urandom: %s\n", strerror(errno));
709 goto ret;
710 }
711
712 while (total_bytes_written < sizeof(buf)) {
713 chunk_size = TEMP_FAILURE_RETRY(
714 read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written));
715 if (chunk_size == -1) {
716 ERROR("Failed to read from /dev/hw_random: %s\n", strerror(errno));
717 goto ret;
718 } else if (chunk_size == 0) {
719 ERROR("Failed to read from /dev/hw_random: EOF\n");
720 goto ret;
721 }
722
723 chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size));
724 if (chunk_size == -1) {
725 ERROR("Failed to write to /dev/urandom: %s\n", strerror(errno));
726 goto ret;
727 }
728 total_bytes_written += chunk_size;
729 }
730
Elliott Hughesccecf142014-01-16 10:53:11 -0800731 INFO("Mixed %zu bytes from /dev/hw_random into /dev/urandom",
Alex Klyubin0d872d82013-08-16 13:19:24 -0700732 total_bytes_written);
733 result = 0;
734
735ret:
736 if (hwrandom_fd != -1) {
737 close(hwrandom_fd);
738 }
739 if (urandom_fd != -1) {
740 close(urandom_fd);
741 }
Alex Klyubin0d872d82013-08-16 13:19:24 -0700742 return result;
743}
744
Colin Crossebc6ff12010-04-13 19:52:01 -0700745static int keychord_init_action(int nargs, char **args)
746{
747 keychord_init();
748 return 0;
749}
750
751static int console_init_action(int nargs, char **args)
752{
Yabin Cui74edcea2015-07-24 10:11:05 -0700753 std::string console = property_get("ro.boot.console");
754 if (!console.empty()) {
755 console_name = "/dev/" + console;
Colin Crossebc6ff12010-04-13 19:52:01 -0700756 }
757
Yabin Cui74edcea2015-07-24 10:11:05 -0700758 int fd = open(console_name.c_str(), O_RDWR | O_CLOEXEC);
Colin Crossebc6ff12010-04-13 19:52:01 -0700759 if (fd >= 0)
760 have_console = 1;
761 close(fd);
762
Nick Kralevich45a884f2015-02-02 14:37:22 -0800763 fd = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200764 if (fd >= 0) {
765 const char *msg;
766 msg = "\n"
767 "\n"
768 "\n"
769 "\n"
770 "\n"
771 "\n"
772 "\n" // console is 40 cols x 30 lines
773 "\n"
774 "\n"
775 "\n"
776 "\n"
777 "\n"
778 "\n"
779 "\n"
780 " A N D R O I D ";
781 write(fd, msg, strlen(msg));
782 close(fd);
Colin Crossebc6ff12010-04-13 19:52:01 -0700783 }
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200784
Colin Crossebc6ff12010-04-13 19:52:01 -0700785 return 0;
786}
787
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700788static void import_kernel_nv(const std::string& key, const std::string& value, bool for_emulator) {
789 if (key.empty()) return;
Dima Zavin5511c842011-12-19 11:21:32 -0800790
791 if (for_emulator) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700792 // In the emulator, export any kernel option with the "ro.kernel." prefix.
793 property_set(android::base::StringPrintf("ro.kernel.%s", key.c_str()).c_str(), value.c_str());
Dima Zavin5511c842011-12-19 11:21:32 -0800794 return;
795 }
796
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700797 if (key == "qemu") {
798 strlcpy(qemu, value.c_str(), sizeof(qemu));
799 } else if (android::base::StartsWith(key, "androidboot.")) {
800 property_set(android::base::StringPrintf("ro.boot.%s", key.c_str() + 12).c_str(),
801 value.c_str());
Dima Zavin5511c842011-12-19 11:21:32 -0800802 }
803}
804
Elliott Hughesd679bc92015-03-20 08:50:46 -0700805static void export_kernel_boot_props() {
Dima Zavin5511c842011-12-19 11:21:32 -0800806 struct {
807 const char *src_prop;
Elliott Hughesd679bc92015-03-20 08:50:46 -0700808 const char *dst_prop;
809 const char *default_value;
Dima Zavin5511c842011-12-19 11:21:32 -0800810 } prop_map[] = {
Elliott Hughesd679bc92015-03-20 08:50:46 -0700811 { "ro.boot.serialno", "ro.serialno", "", },
812 { "ro.boot.mode", "ro.bootmode", "unknown", },
813 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800814 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
Elliott Hughesd679bc92015-03-20 08:50:46 -0700815 { "ro.boot.hardware", "ro.hardware", "unknown", },
816 { "ro.boot.revision", "ro.revision", "0", },
Dima Zavin5511c842011-12-19 11:21:32 -0800817 };
Elliott Hughesd679bc92015-03-20 08:50:46 -0700818 for (size_t i = 0; i < ARRAY_SIZE(prop_map); i++) {
Yabin Cui74edcea2015-07-24 10:11:05 -0700819 std::string value = property_get(prop_map[i].src_prop);
820 property_set(prop_map[i].dst_prop, (!value.empty()) ? value.c_str() : prop_map[i].default_value);
Dima Zavin5511c842011-12-19 11:21:32 -0800821 }
Dima Zavin5511c842011-12-19 11:21:32 -0800822}
Colin Crossebc6ff12010-04-13 19:52:01 -0700823
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700824static void process_kernel_dt() {
Rom Lemarchand6a524432015-02-28 06:39:11 -0800825 static const char android_dir[] = "/proc/device-tree/firmware/android";
826
827 std::string file_name = android::base::StringPrintf("%s/compatible", android_dir);
828
829 std::string dt_file;
830 android::base::ReadFileToString(file_name, &dt_file);
831 if (!dt_file.compare("android,firmware")) {
832 ERROR("firmware/android is not compatible with 'android,firmware'\n");
833 return;
834 }
835
836 std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(android_dir), closedir);
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700837 if (!dir) return;
Rom Lemarchand6a524432015-02-28 06:39:11 -0800838
839 struct dirent *dp;
840 while ((dp = readdir(dir.get())) != NULL) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700841 if (dp->d_type != DT_REG || !strcmp(dp->d_name, "compatible")) {
Rom Lemarchand6a524432015-02-28 06:39:11 -0800842 continue;
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700843 }
Rom Lemarchand6a524432015-02-28 06:39:11 -0800844
845 file_name = android::base::StringPrintf("%s/%s", android_dir, dp->d_name);
846
847 android::base::ReadFileToString(file_name, &dt_file);
848 std::replace(dt_file.begin(), dt_file.end(), ',', '.');
849
850 std::string property_name = android::base::StringPrintf("ro.boot.%s", dp->d_name);
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700851 property_set(property_name.c_str(), dt_file.c_str());
Rom Lemarchand6a524432015-02-28 06:39:11 -0800852 }
853}
854
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700855static void process_kernel_cmdline() {
856 // Don't expose the raw commandline to unprivileged processes.
Dima Zavin5511c842011-12-19 11:21:32 -0800857 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700858
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700859 // The first pass does the common stuff, and finds if we are in qemu.
860 // The second pass is only necessary for qemu to export all kernel params
861 // as properties.
Nick Kralevichf667a322015-04-25 17:42:52 -0700862 import_kernel_cmdline(false, import_kernel_nv);
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700863 if (qemu[0]) import_kernel_cmdline(true, import_kernel_nv);
Colin Crossebc6ff12010-04-13 19:52:01 -0700864}
865
Colin Crossebc6ff12010-04-13 19:52:01 -0700866static int queue_property_triggers_action(int nargs, char **args)
867{
868 queue_all_property_triggers();
869 /* enable property triggers */
870 property_triggers_enabled = 1;
871 return 0;
872}
873
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700874static void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500875{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400876 sehandle = selinux_android_file_context_handle();
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500877 selinux_android_set_sehandle(sehandle);
rpcraig63207cd2012-08-09 10:05:49 -0400878 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400879}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500880
Nick Kralevichd34e4072015-04-28 12:39:41 -0700881enum selinux_enforcing_status { SELINUX_PERMISSIVE, SELINUX_ENFORCING };
Nick Kralevichf667a322015-04-25 17:42:52 -0700882
883static selinux_enforcing_status selinux_status_from_cmdline() {
884 selinux_enforcing_status status = SELINUX_ENFORCING;
885
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700886 import_kernel_cmdline(false, [&](const std::string& key, const std::string& value, bool in_qemu) {
887 if (key == "androidboot.selinux" && value == "permissive") {
888 status = SELINUX_PERMISSIVE;
Nick Kralevichf667a322015-04-25 17:42:52 -0700889 }
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700890 });
Nick Kralevichf667a322015-04-25 17:42:52 -0700891
892 return status;
893}
894
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700895static bool selinux_is_enforcing(void)
896{
Nick Kralevichd34e4072015-04-28 12:39:41 -0700897 if (ALLOW_PERMISSIVE_SELINUX) {
Nick Kralevichf667a322015-04-25 17:42:52 -0700898 return selinux_status_from_cmdline() == SELINUX_ENFORCING;
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700899 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700900 return true;
901}
902
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400903int selinux_reload_policy(void)
904{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400905 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500906
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400907 if (selinux_android_reload_policy() == -1) {
908 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500909 }
910
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400911 if (sehandle)
912 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500913
rpcraig63207cd2012-08-09 10:05:49 -0400914 if (sehandle_prop)
915 selabel_close(sehandle_prop);
916
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400917 selinux_init_all_handles();
918 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500919}
rpcraig63207cd2012-08-09 10:05:49 -0400920
Elliott Hughesda40c002015-03-27 23:20:44 -0700921static int audit_callback(void *data, security_class_t /*cls*/, char *buf, size_t len) {
rpcraig63207cd2012-08-09 10:05:49 -0400922 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
923 return 0;
924}
925
Elliott Hughesf65730e2015-04-24 18:38:17 +0000926static void security_failure() {
927 ERROR("Security failure; rebooting into recovery mode...\n");
928 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
929 while (true) { pause(); } // never reached
930}
931
932static void selinux_initialize(bool in_kernel_domain) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700933 Timer t;
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500934
Elliott Hughesda40c002015-03-27 23:20:44 -0700935 selinux_callback cb;
936 cb.func_log = selinux_klog_callback;
937 selinux_set_callback(SELINUX_CB_LOG, cb);
938 cb.func_audit = audit_callback;
939 selinux_set_callback(SELINUX_CB_AUDIT, cb);
940
Elliott Hughesf65730e2015-04-24 18:38:17 +0000941 if (in_kernel_domain) {
Elliott Hughesf65730e2015-04-24 18:38:17 +0000942 INFO("Loading SELinux policy...\n");
943 if (selinux_android_load_policy() < 0) {
944 ERROR("failed to load policy: %s\n", strerror(errno));
945 security_failure();
946 }
947
Nick Kralevichd34e4072015-04-28 12:39:41 -0700948 bool kernel_enforcing = (security_getenforce() == 1);
Elliott Hughesf65730e2015-04-24 18:38:17 +0000949 bool is_enforcing = selinux_is_enforcing();
Nick Kralevichd34e4072015-04-28 12:39:41 -0700950 if (kernel_enforcing != is_enforcing) {
951 if (security_setenforce(is_enforcing)) {
952 ERROR("security_setenforce(%s) failed: %s\n",
953 is_enforcing ? "true" : "false", strerror(errno));
954 security_failure();
955 }
956 }
Elliott Hughesf65730e2015-04-24 18:38:17 +0000957
Nick Kralevicheedbe812015-04-25 14:10:03 -0700958 if (write_file("/sys/fs/selinux/checkreqprot", "0") == -1) {
959 security_failure();
960 }
961
Elliott Hughesf65730e2015-04-24 18:38:17 +0000962 NOTICE("(Initializing SELinux %s took %.2fs.)\n",
963 is_enforcing ? "enforcing" : "non-enforcing", t.duration());
964 } else {
965 selinux_init_all_handles();
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700966 }
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700967}
968
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800969int main(int argc, char** argv) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700970 if (!strcmp(basename(argv[0]), "ueventd")) {
Colin Crossf83d0b92010-04-21 12:04:20 -0700971 return ueventd_main(argc, argv);
Elliott Hughesda40c002015-03-27 23:20:44 -0700972 }
Colin Crossf83d0b92010-04-21 12:04:20 -0700973
Elliott Hughesda40c002015-03-27 23:20:44 -0700974 if (!strcmp(basename(argv[0]), "watchdogd")) {
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700975 return watchdogd_main(argc, argv);
Elliott Hughesda40c002015-03-27 23:20:44 -0700976 }
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700977
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800978 // Clear the umask.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700979 umask(0);
980
Elliott Hughescc86fb22015-03-17 20:01:13 -0700981 add_environment("PATH", _PATH_DEFPATH);
982
Nick Kralevich9dec93b2015-04-25 15:50:03 -0700983 bool is_first_stage = (argc == 1) || (strcmp(argv[1], "--second-stage") != 0);
984
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800985 // Get the basic filesystem setup we need put together in the initramdisk
986 // on / and then we'll let the rc file figure out the rest.
Nick Kralevich9dec93b2015-04-25 15:50:03 -0700987 if (is_first_stage) {
Nick Kralevichf667a322015-04-25 17:42:52 -0700988 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
989 mkdir("/dev/pts", 0755);
990 mkdir("/dev/socket", 0755);
991 mount("devpts", "/dev/pts", "devpts", 0, NULL);
Nick Kralevich9dec93b2015-04-25 15:50:03 -0700992 mount("proc", "/proc", "proc", 0, NULL);
993 mount("sysfs", "/sys", "sysfs", 0, NULL);
994 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700995
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800996 // We must have some place other than / to create the device nodes for
997 // kmsg and null, otherwise we won't be able to remount / read-only
998 // later on. Now that tmpfs is mounted on /dev, we can actually talk
999 // to the outside world.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001000 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -07001001 klog_init();
Elliott Hughesda40c002015-03-27 23:20:44 -07001002 klog_set_level(KLOG_NOTICE_LEVEL);
1003
Elliott Hughese5ce30f2015-05-06 19:19:24 -07001004 NOTICE("init %s started!\n", is_first_stage ? "first stage" : "second stage");
Elliott Hughesda40c002015-03-27 23:20:44 -07001005
Nick Kralevichf667a322015-04-25 17:42:52 -07001006 if (!is_first_stage) {
1007 // Indicate that booting is in progress to background fw loaders, etc.
1008 close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -07001009
Nick Kralevichf667a322015-04-25 17:42:52 -07001010 property_init();
Dima Zavin5511c842011-12-19 11:21:32 -08001011
Nick Kralevichf667a322015-04-25 17:42:52 -07001012 // If arguments are passed both on the command line and in DT,
1013 // properties set in DT always have priority over the command-line ones.
1014 process_kernel_dt();
1015 process_kernel_cmdline();
1016
Elliott Hughese5ce30f2015-05-06 19:19:24 -07001017 // Propagate the kernel variables to internal variables
Nick Kralevichf667a322015-04-25 17:42:52 -07001018 // used by init as well as the current required properties.
1019 export_kernel_boot_props();
1020 }
Rom Lemarchand6a524432015-02-28 06:39:11 -08001021
Elliott Hughesf65730e2015-04-24 18:38:17 +00001022 // Set up SELinux, including loading the SELinux policy if we're in the kernel domain.
1023 selinux_initialize(is_first_stage);
1024
1025 // If we're in the kernel domain, re-exec init to transition to the init domain now
1026 // that the SELinux policy has been loaded.
1027 if (is_first_stage) {
1028 if (restorecon("/init") == -1) {
1029 ERROR("restorecon failed: %s\n", strerror(errno));
1030 security_failure();
1031 }
1032 char* path = argv[0];
1033 char* args[] = { path, const_cast<char*>("--second-stage"), nullptr };
1034 if (execv(path, args) == -1) {
1035 ERROR("execv(\"%s\") failed: %s\n", path, strerror(errno));
1036 security_failure();
1037 }
1038 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001039
1040 // These directories were necessarily created before initial policy load
1041 // and therefore need their security context restored to the proper value.
1042 // This must happen before /dev is populated by ueventd.
Elliott Hughese5ce30f2015-05-06 19:19:24 -07001043 NOTICE("Running restorecon...\n");
Stephen Smalleye096e362012-06-11 13:37:39 -04001044 restorecon("/dev");
1045 restorecon("/dev/socket");
Geremy Condra8e15eab2013-02-28 17:29:58 -08001046 restorecon("/dev/__properties__");
Nick Kralevichae76f6d2013-07-11 15:38:26 -07001047 restorecon_recursive("/sys");
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001048
Elliott Hughes929f4072015-04-24 21:13:44 -07001049 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
1050 if (epoll_fd == -1) {
1051 ERROR("epoll_create1 failed: %s\n", strerror(errno));
1052 exit(1);
1053 }
1054
1055 signal_handler_init();
Elliott Hughes9042cae2015-04-24 17:43:21 -07001056
Riley Andrewse4b7b292014-06-16 15:06:21 -07001057 property_load_boot_defaults();
Elliott Hughesc6c26ed2015-04-24 18:50:30 -07001058 start_property_service();
Dima Zavind7634c92011-12-16 14:18:06 -08001059
Lee Campbellf13b1b32015-07-24 16:57:14 -07001060 init_parse_config("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001061
1062 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001063
Elliott Hughesc6c26ed2015-04-24 18:50:30 -07001064 // 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 -07001065 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Elliott Hughesc6c26ed2015-04-24 18:50:30 -07001066 // ... so that we can start queuing up actions that require stuff from /dev.
Alex Klyubin0d872d82013-08-16 13:19:24 -07001067 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001068 queue_builtin_action(keychord_init_action, "keychord_init");
1069 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001070
Elliott Hughesda40c002015-03-27 23:20:44 -07001071 // Trigger all the boot actions to get us started.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001072 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001073
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001074 // Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
1075 // wasn't ready immediately after wait_for_coldboot_done
Alex Klyubin0d872d82013-08-16 13:19:24 -07001076 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001077
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001078 // Don't mount filesystems or start core system services in charger mode.
Yabin Cui74edcea2015-07-24 10:11:05 -07001079 std::string bootmode = property_get("ro.bootmode");
1080 if (bootmode == "charger") {
Dima Zavinca47cef2011-08-24 15:28:23 -07001081 action_for_each_trigger("charger", action_add_queue_tail);
1082 } else {
Riley Andrewse4b7b292014-06-16 15:06:21 -07001083 action_for_each_trigger("late-init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001084 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001085
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001086 // Run all property triggers based on current state of the properties.
Chris Dearman469b7b22012-03-01 15:29:20 -08001087 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001088
Elliott Hughesc6c26ed2015-04-24 18:50:30 -07001089 while (true) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001090 if (!waiting_for_exec) {
1091 execute_one_command();
1092 restart_processes();
1093 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001094
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 Hughes929f4072015-04-24 21:13:44 -07001108 epoll_event ev;
1109 int nr = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd, &ev, 1, timeout));
1110 if (nr == -1) {
1111 ERROR("epoll_wait failed: %s\n", strerror(errno));
1112 } else if (nr == 1) {
1113 ((void (*)()) ev.data.ptr)();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001114 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001115 }
1116
1117 return 0;
1118}