2008-10-21 16:00:00 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-02-07 05:15:18 +01:00
|
|
|
#include <ctype.h>
|
2015-02-28 15:39:11 +01:00
|
|
|
#include <dirent.h>
|
2015-02-07 05:15:18 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libgen.h>
|
2015-03-18 04:01:13 +01:00
|
|
|
#include <paths.h>
|
2015-02-07 05:15:18 +01:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
2008-10-21 16:00:00 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-04-25 06:13:44 +02:00
|
|
|
#include <sys/epoll.h>
|
2008-10-21 16:00:00 +02:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/socket.h>
|
2015-02-07 05:15:18 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2008-10-21 16:00:00 +02:00
|
|
|
#include <sys/un.h>
|
2015-02-07 05:15:18 +01:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <mtd/mtd-user.h>
|
2012-01-13 14:48:47 +01:00
|
|
|
|
|
|
|
#include <selinux/selinux.h>
|
|
|
|
#include <selinux/label.h>
|
2012-05-01 21:02:53 +02:00
|
|
|
#include <selinux/android.h>
|
2012-01-13 14:48:47 +01:00
|
|
|
|
2015-02-28 15:39:11 +01:00
|
|
|
#include <base/file.h>
|
|
|
|
#include <base/stringprintf.h>
|
2015-05-07 04:19:24 +02:00
|
|
|
#include <base/strings.h>
|
2013-06-25 02:41:40 +02:00
|
|
|
#include <cutils/android_reboot.h>
|
2013-08-16 22:19:24 +02:00
|
|
|
#include <cutils/fs.h>
|
2015-02-07 05:15:18 +01:00
|
|
|
#include <cutils/iosched_policy.h>
|
|
|
|
#include <cutils/list.h>
|
|
|
|
#include <cutils/sockets.h>
|
2010-04-21 21:04:20 +02:00
|
|
|
#include <private/android_filesystem_config.h>
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2015-02-28 15:39:11 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2015-07-24 02:53:11 +02:00
|
|
|
#include "action.h"
|
2008-10-21 16:00:00 +02:00
|
|
|
#include "devices.h"
|
|
|
|
#include "init.h"
|
2010-04-20 02:05:34 +02:00
|
|
|
#include "log.h"
|
2008-10-21 16:00:00 +02:00
|
|
|
#include "property_service.h"
|
2008-12-18 03:08:08 +01:00
|
|
|
#include "bootchart.h"
|
2010-04-14 04:48:59 +02:00
|
|
|
#include "signal_handler.h"
|
2010-04-14 04:20:44 +02:00
|
|
|
#include "keychords.h"
|
2010-04-20 23:29:05 +02:00
|
|
|
#include "init_parser.h"
|
2010-04-14 05:35:46 +02:00
|
|
|
#include "util.h"
|
2010-04-21 21:04:20 +02:00
|
|
|
#include "ueventd.h"
|
2012-06-14 06:51:56 +02:00
|
|
|
#include "watchdogd.h"
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2012-01-13 14:48:47 +01:00
|
|
|
struct selabel_handle *sehandle;
|
2012-08-09 16:05:49 +02:00
|
|
|
struct selabel_handle *sehandle_prop;
|
2012-01-13 14:48:47 +01:00
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
static int property_triggers_enabled = 0;
|
|
|
|
|
|
|
|
static char qemu[32];
|
|
|
|
|
|
|
|
static int have_console;
|
2015-07-24 19:11:05 +02:00
|
|
|
static std::string console_name = "/dev/console";
|
2008-10-21 16:00:00 +02:00
|
|
|
static time_t process_needs_restart;
|
|
|
|
|
|
|
|
static const char *ENV[32];
|
|
|
|
|
2015-02-07 05:15:18 +01:00
|
|
|
bool waiting_for_exec = false;
|
|
|
|
|
2015-04-25 06:13:44 +02:00
|
|
|
static int epoll_fd = -1;
|
|
|
|
|
|
|
|
void register_epoll_handler(int fd, void (*fn)()) {
|
|
|
|
epoll_event ev;
|
|
|
|
ev.events = EPOLLIN;
|
|
|
|
ev.data.ptr = reinterpret_cast<void*>(fn);
|
|
|
|
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
|
|
|
|
ERROR("epoll_ctl failed: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-07 05:15:18 +01:00
|
|
|
void service::NotifyStateChange(const char* new_state) {
|
2015-03-28 07:20:44 +01:00
|
|
|
if (!properties_initialized()) {
|
2015-02-07 05:15:18 +01:00
|
|
|
// If properties aren't available yet, we can't set them.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & SVC_EXEC) != 0) {
|
|
|
|
// 'exec' commands don't have properties tracking their state.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char prop_name[PROP_NAME_MAX];
|
|
|
|
if (snprintf(prop_name, sizeof(prop_name), "init.svc.%s", name) >= PROP_NAME_MAX) {
|
|
|
|
// If the property name would be too long, we can't set it.
|
|
|
|
ERROR("Property name \"init.svc.%s\" too long; not setting to %s\n", name, new_state);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
property_set(prop_name, new_state);
|
|
|
|
}
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
/* add_environment - add "key=value" to the current environment */
|
|
|
|
int add_environment(const char *key, const char *val)
|
|
|
|
{
|
2014-05-16 12:36:36 +02:00
|
|
|
size_t n;
|
|
|
|
size_t key_len = strlen(key);
|
|
|
|
|
|
|
|
/* The last environment entry is reserved to terminate the list */
|
|
|
|
for (n = 0; n < (ARRAY_SIZE(ENV) - 1); n++) {
|
|
|
|
|
|
|
|
/* Delete any existing entry for this key */
|
|
|
|
if (ENV[n] != NULL) {
|
|
|
|
size_t entry_key_len = strcspn(ENV[n], "=");
|
|
|
|
if ((entry_key_len == key_len) && (strncmp(ENV[n], key, entry_key_len) == 0)) {
|
|
|
|
free((char*)ENV[n]);
|
|
|
|
ENV[n] = NULL;
|
|
|
|
}
|
|
|
|
}
|
2011-09-28 18:55:31 +02:00
|
|
|
|
2014-05-16 12:36:36 +02:00
|
|
|
/* Add entry if a free slot is available */
|
|
|
|
if (ENV[n] == NULL) {
|
2015-02-04 02:12:07 +01:00
|
|
|
char* entry;
|
|
|
|
asprintf(&entry, "%s=%s", key, val);
|
2008-10-21 16:00:00 +02:00
|
|
|
ENV[n] = entry;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 12:36:36 +02:00
|
|
|
ERROR("No env. room to store: '%s':'%s'\n", key, val);
|
|
|
|
|
|
|
|
return -1;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 16:48:47 +02:00
|
|
|
void zap_stdio(void)
|
2008-10-21 16:00:00 +02:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
fd = open("/dev/null", O_RDWR);
|
|
|
|
dup2(fd, 0);
|
|
|
|
dup2(fd, 1);
|
|
|
|
dup2(fd, 2);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void open_console()
|
|
|
|
{
|
|
|
|
int fd;
|
2015-07-24 19:11:05 +02:00
|
|
|
if ((fd = open(console_name.c_str(), O_RDWR)) < 0) {
|
2008-10-21 16:00:00 +02:00
|
|
|
fd = open("/dev/null", O_RDWR);
|
|
|
|
}
|
2012-03-18 23:38:19 +01:00
|
|
|
ioctl(fd, TIOCSCTTY, 0);
|
2008-10-21 16:00:00 +02:00
|
|
|
dup2(fd, 0);
|
|
|
|
dup2(fd, 1);
|
|
|
|
dup2(fd, 2);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void publish_socket(const char *name, int fd)
|
|
|
|
{
|
|
|
|
char key[64] = ANDROID_SOCKET_ENV_PREFIX;
|
|
|
|
char val[64];
|
|
|
|
|
|
|
|
strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
|
|
|
|
name,
|
|
|
|
sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
|
|
|
|
snprintf(val, sizeof(val), "%d", fd);
|
|
|
|
add_environment(key, val);
|
|
|
|
|
|
|
|
/* make sure we don't close-on-exec */
|
|
|
|
fcntl(fd, F_SETFD, 0);
|
|
|
|
}
|
|
|
|
|
2009-05-19 22:30:46 +02:00
|
|
|
void service_start(struct service *svc, const char *dynamic_args)
|
2008-10-21 16:00:00 +02:00
|
|
|
{
|
2015-02-07 05:15:18 +01:00
|
|
|
// Starting a service removes it from the disabled or reset state and
|
|
|
|
// immediately takes it out of the restarting state if it was in there.
|
2014-05-03 06:14:29 +02:00
|
|
|
svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START));
|
2008-10-21 16:00:00 +02:00
|
|
|
svc->time_started = 0;
|
2011-09-28 18:55:31 +02:00
|
|
|
|
2015-02-07 05:15:18 +01:00
|
|
|
// Running processes require no additional work --- if they're in the
|
|
|
|
// process of exiting, we've ensured that they will immediately restart
|
|
|
|
// on exit, unless they are ONESHOT.
|
2008-10-21 16:00:00 +02:00
|
|
|
if (svc->flags & SVC_RUNNING) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-07 05:15:18 +01:00
|
|
|
bool needs_console = (svc->flags & SVC_CONSOLE);
|
|
|
|
if (needs_console && !have_console) {
|
2008-10-21 16:00:00 +02:00
|
|
|
ERROR("service '%s' requires console\n", svc->name);
|
|
|
|
svc->flags |= SVC_DISABLED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-07 04:19:24 +02:00
|
|
|
struct stat sb;
|
|
|
|
if (stat(svc->args[0], &sb) == -1) {
|
|
|
|
ERROR("cannot find '%s' (%s), disabling '%s'\n", svc->args[0], strerror(errno), svc->name);
|
2008-10-21 16:00:00 +02:00
|
|
|
svc->flags |= SVC_DISABLED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-19 22:30:46 +02:00
|
|
|
if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
|
2015-05-07 04:19:24 +02:00
|
|
|
ERROR("service '%s' must be one-shot to use dynamic args, disabling\n", svc->args[0]);
|
2009-05-19 22:30:46 +02:00
|
|
|
svc->flags |= SVC_DISABLED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-07 05:15:18 +01:00
|
|
|
char* scon = NULL;
|
2015-06-13 07:03:50 +02:00
|
|
|
if (svc->seclabel) {
|
|
|
|
scon = strdup(svc->seclabel);
|
|
|
|
if (!scon) {
|
|
|
|
ERROR("Out of memory while starting '%s'\n", svc->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
char *mycon = NULL, *fcon = NULL;
|
|
|
|
|
|
|
|
INFO("computing context for service '%s'\n", svc->args[0]);
|
|
|
|
int rc = getcon(&mycon);
|
|
|
|
if (rc < 0) {
|
|
|
|
ERROR("could not get context while starting '%s'\n", svc->name);
|
|
|
|
return;
|
|
|
|
}
|
2012-11-16 20:34:27 +01:00
|
|
|
|
2015-06-13 07:03:50 +02:00
|
|
|
rc = getfilecon(svc->args[0], &fcon);
|
|
|
|
if (rc < 0) {
|
|
|
|
ERROR("could not get context while starting '%s'\n", svc->name);
|
2015-07-16 19:49:51 +02:00
|
|
|
free(mycon);
|
2015-06-13 07:03:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon);
|
|
|
|
if (rc == 0 && !strcmp(scon, mycon)) {
|
2015-07-16 19:49:51 +02:00
|
|
|
ERROR("Service %s does not have a SELinux domain defined.\n", svc->name);
|
|
|
|
free(mycon);
|
|
|
|
free(fcon);
|
|
|
|
free(scon);
|
|
|
|
return;
|
2015-06-13 07:03:50 +02:00
|
|
|
}
|
2015-07-16 19:49:51 +02:00
|
|
|
free(mycon);
|
|
|
|
free(fcon);
|
2015-06-13 07:03:50 +02:00
|
|
|
if (rc < 0) {
|
|
|
|
ERROR("could not get context while starting '%s'\n", svc->name);
|
|
|
|
return;
|
2012-01-13 14:48:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-28 07:20:44 +01:00
|
|
|
NOTICE("Starting service '%s'...\n", svc->name);
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2015-02-07 05:15:18 +01:00
|
|
|
pid_t pid = fork();
|
2008-10-21 16:00:00 +02:00
|
|
|
if (pid == 0) {
|
|
|
|
struct socketinfo *si;
|
|
|
|
struct svcenvinfo *ei;
|
|
|
|
char tmp[32];
|
|
|
|
int fd, sz;
|
|
|
|
|
2012-03-26 18:09:11 +02:00
|
|
|
umask(077);
|
2015-03-28 07:20:44 +01:00
|
|
|
if (properties_initialized()) {
|
2010-04-20 02:11:33 +02:00
|
|
|
get_property_workspace(&fd, &sz);
|
2015-02-18 04:27:51 +01:00
|
|
|
snprintf(tmp, sizeof(tmp), "%d,%d", dup(fd), sz);
|
2010-04-20 02:11:33 +02:00
|
|
|
add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
for (ei = svc->envvars; ei; ei = ei->next)
|
|
|
|
add_environment(ei->name, ei->value);
|
|
|
|
|
|
|
|
for (si = svc->sockets; si; si = si->next) {
|
2010-10-01 14:20:36 +02:00
|
|
|
int socket_type = (
|
|
|
|
!strcmp(si->type, "stream") ? SOCK_STREAM :
|
|
|
|
(!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
|
|
|
|
int s = create_socket(si->name, socket_type,
|
2013-05-13 18:37:04 +02:00
|
|
|
si->perm, si->uid, si->gid, si->socketcon ?: scon);
|
2008-10-21 16:00:00 +02:00
|
|
|
if (s >= 0) {
|
|
|
|
publish_socket(si->name, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-16 19:49:51 +02:00
|
|
|
free(scon);
|
2012-01-13 14:48:47 +01:00
|
|
|
scon = NULL;
|
|
|
|
|
2015-06-13 03:02:20 +02:00
|
|
|
if (svc->writepid_files_) {
|
|
|
|
std::string pid_str = android::base::StringPrintf("%d", pid);
|
|
|
|
for (auto& file : *svc->writepid_files_) {
|
|
|
|
if (!android::base::WriteStringToFile(pid_str, file)) {
|
|
|
|
ERROR("couldn't write %s to %s: %s\n",
|
|
|
|
pid_str.c_str(), file.c_str(), strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-25 23:19:50 +01:00
|
|
|
if (svc->ioprio_class != IoSchedClass_NONE) {
|
|
|
|
if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
|
|
|
|
ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
|
|
|
|
getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
if (needs_console) {
|
|
|
|
setsid();
|
|
|
|
open_console();
|
|
|
|
} else {
|
|
|
|
zap_stdio();
|
|
|
|
}
|
|
|
|
|
2015-02-04 23:46:36 +01:00
|
|
|
if (false) {
|
|
|
|
for (size_t n = 0; svc->args[n]; n++) {
|
|
|
|
INFO("args[%zu] = '%s'\n", n, svc->args[n]);
|
|
|
|
}
|
|
|
|
for (size_t n = 0; ENV[n]; n++) {
|
|
|
|
INFO("env[%zu] = '%s'\n", n, ENV[n]);
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setpgid(0, getpid());
|
|
|
|
|
2015-02-07 05:15:18 +01:00
|
|
|
// As requested, set our gid, supplemental gids, and uid.
|
2008-10-21 16:00:00 +02:00
|
|
|
if (svc->gid) {
|
2010-11-18 01:55:42 +01:00
|
|
|
if (setgid(svc->gid) != 0) {
|
|
|
|
ERROR("setgid failed: %s\n", strerror(errno));
|
|
|
|
_exit(127);
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
if (svc->nr_supp_gids) {
|
2010-11-18 01:55:42 +01:00
|
|
|
if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
|
|
|
|
ERROR("setgroups failed: %s\n", strerror(errno));
|
|
|
|
_exit(127);
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
if (svc->uid) {
|
2010-11-18 01:55:42 +01:00
|
|
|
if (setuid(svc->uid) != 0) {
|
|
|
|
ERROR("setuid failed: %s\n", strerror(errno));
|
|
|
|
_exit(127);
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
2012-01-13 14:48:47 +01:00
|
|
|
if (svc->seclabel) {
|
2015-06-13 07:03:50 +02:00
|
|
|
if (setexeccon(svc->seclabel) < 0) {
|
2012-01-13 14:48:47 +01:00
|
|
|
ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
|
|
|
|
_exit(127);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-20 17:50:40 +02:00
|
|
|
if (!dynamic_args) {
|
|
|
|
if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
|
|
|
|
ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
|
|
|
|
}
|
|
|
|
} else {
|
2010-04-20 23:29:05 +02:00
|
|
|
char *arg_ptrs[INIT_PARSER_MAXARGS+1];
|
2009-05-20 18:52:16 +02:00
|
|
|
int arg_idx = svc->nargs;
|
2009-05-19 22:30:46 +02:00
|
|
|
char *tmp = strdup(dynamic_args);
|
2009-05-20 18:52:16 +02:00
|
|
|
char *next = tmp;
|
|
|
|
char *bword;
|
2009-05-19 22:30:46 +02:00
|
|
|
|
|
|
|
/* Copy the static arguments */
|
2009-05-20 18:52:16 +02:00
|
|
|
memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
|
2009-05-19 22:30:46 +02:00
|
|
|
|
2009-05-20 18:52:16 +02:00
|
|
|
while((bword = strsep(&next, " "))) {
|
|
|
|
arg_ptrs[arg_idx++] = bword;
|
2010-04-20 23:29:05 +02:00
|
|
|
if (arg_idx == INIT_PARSER_MAXARGS)
|
2009-05-19 22:30:46 +02:00
|
|
|
break;
|
|
|
|
}
|
2015-02-03 20:20:49 +01:00
|
|
|
arg_ptrs[arg_idx] = NULL;
|
2009-05-19 22:30:46 +02:00
|
|
|
execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
|
2008-11-23 22:26:39 +01:00
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
_exit(127);
|
|
|
|
}
|
|
|
|
|
2015-07-16 19:49:51 +02:00
|
|
|
free(scon);
|
2012-01-13 14:48:47 +01:00
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
if (pid < 0) {
|
|
|
|
ERROR("failed to start '%s'\n", svc->name);
|
|
|
|
svc->pid = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
svc->time_started = gettime();
|
|
|
|
svc->pid = pid;
|
|
|
|
svc->flags |= SVC_RUNNING;
|
|
|
|
|
2015-02-07 05:15:18 +01:00
|
|
|
if ((svc->flags & SVC_EXEC) != 0) {
|
2015-03-11 20:31:53 +01:00
|
|
|
INFO("SVC_EXEC pid %d (uid %d gid %d+%zu context %s) started; waiting...\n",
|
2015-06-02 20:11:02 +02:00
|
|
|
svc->pid, svc->uid, svc->gid, svc->nr_supp_gids,
|
|
|
|
svc->seclabel ? : "default");
|
2015-02-07 05:15:18 +01:00
|
|
|
waiting_for_exec = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
svc->NotifyStateChange("running");
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2012-01-26 05:48:46 +01:00
|
|
|
/* The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART */
|
2010-12-04 01:33:31 +01:00
|
|
|
static void service_stop_or_reset(struct service *svc, int how)
|
2008-10-21 16:00:00 +02:00
|
|
|
{
|
init: Safely restart services to avoid race conditions.
Previously, service restarts (either an explicit "restart", or a "stop,
start" pair) exhibited a race condition whereby the new (restarting)
service process was often spawned before the old (stopping) process had
terminated. This may have resulted in the new service process failing to
acquire a limited resource (file lock, socket bind, etc.) that the old
process had not yet released.
Now, a stopping service remains in the SVC_RUNNING state until its exiting
process has been reaped by waitpid. This prevents a "stop, start" sequence
from spawning a second service process before resources held by the first
are released. This enables safe service restarts by stopping the service,
waiting for the old service process to terminate, and (only then) starting
the new service process. In the event of "restarting" an already stopped
service, the previous behavior is maintained whereby the service is simply
started.
This scenario could be special-cased by the restart command, however, we
have observed instances where services are, unintentionally, stopped and
started "too quickly," and so simultaneous processes for the same service
should never be allowed.
Note that this commit alters the behaviors for explicit restarts of
critical and oneshot services. Previously these serivces would simply be
restarted, whereas now, an explicit restart of a critical service counts as
a crash (which may result in a recovery reboot) and oneshot services go
into the disabled state.
2012-02-06 16:32:13 +01:00
|
|
|
/* The service is still SVC_RUNNING until its process exits, but if it has
|
|
|
|
* already exited it shoudn't attempt a restart yet. */
|
2014-05-03 06:14:29 +02:00
|
|
|
svc->flags &= ~(SVC_RESTARTING | SVC_DISABLED_START);
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2012-01-26 05:48:46 +01:00
|
|
|
if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) {
|
2010-12-04 01:33:31 +01:00
|
|
|
/* Hrm, an illegal flag. Default to SVC_DISABLED */
|
|
|
|
how = SVC_DISABLED;
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
/* if the service has not yet started, prevent
|
|
|
|
* it from auto-starting with its class
|
|
|
|
*/
|
2011-10-27 01:56:00 +02:00
|
|
|
if (how == SVC_RESET) {
|
|
|
|
svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
|
|
|
|
} else {
|
|
|
|
svc->flags |= how;
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
if (svc->pid) {
|
2015-03-28 07:20:44 +01:00
|
|
|
NOTICE("Service '%s' is being killed...\n", svc->name);
|
2010-12-04 01:33:31 +01:00
|
|
|
kill(-svc->pid, SIGKILL);
|
2015-02-07 05:15:18 +01:00
|
|
|
svc->NotifyStateChange("stopping");
|
2008-10-21 16:00:00 +02:00
|
|
|
} else {
|
2015-02-07 05:15:18 +01:00
|
|
|
svc->NotifyStateChange("stopped");
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-04 01:33:31 +01:00
|
|
|
void service_reset(struct service *svc)
|
|
|
|
{
|
|
|
|
service_stop_or_reset(svc, SVC_RESET);
|
|
|
|
}
|
|
|
|
|
|
|
|
void service_stop(struct service *svc)
|
|
|
|
{
|
|
|
|
service_stop_or_reset(svc, SVC_DISABLED);
|
|
|
|
}
|
|
|
|
|
2012-01-26 05:48:46 +01:00
|
|
|
void service_restart(struct service *svc)
|
|
|
|
{
|
|
|
|
if (svc->flags & SVC_RUNNING) {
|
|
|
|
/* Stop, wait, then start the service. */
|
|
|
|
service_stop_or_reset(svc, SVC_RESTART);
|
|
|
|
} else if (!(svc->flags & SVC_RESTARTING)) {
|
|
|
|
/* Just start the service since it's not running. */
|
|
|
|
service_start(svc, NULL);
|
|
|
|
} /* else: Service is restarting anyways. */
|
|
|
|
}
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
void property_changed(const char *name, const char *value)
|
|
|
|
{
|
2010-04-14 04:52:01 +02:00
|
|
|
if (property_triggers_enabled)
|
2015-07-24 02:53:11 +02:00
|
|
|
ActionManager::GetInstance().QueuePropertyTrigger(name, value);
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void restart_service_if_needed(struct service *svc)
|
|
|
|
{
|
|
|
|
time_t next_start_time = svc->time_started + 5;
|
|
|
|
|
|
|
|
if (next_start_time <= gettime()) {
|
|
|
|
svc->flags &= (~SVC_RESTARTING);
|
2009-05-19 22:30:46 +02:00
|
|
|
service_start(svc, NULL);
|
2008-10-21 16:00:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((next_start_time < process_needs_restart) ||
|
|
|
|
(process_needs_restart == 0)) {
|
|
|
|
process_needs_restart = next_start_time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void restart_processes()
|
|
|
|
{
|
|
|
|
process_needs_restart = 0;
|
|
|
|
service_for_each_flags(SVC_RESTARTING,
|
|
|
|
restart_service_if_needed);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void msg_start(const char *name)
|
|
|
|
{
|
2013-04-01 05:17:51 +02:00
|
|
|
struct service *svc = NULL;
|
2009-05-19 22:30:46 +02:00
|
|
|
char *tmp = NULL;
|
|
|
|
char *args = NULL;
|
|
|
|
|
|
|
|
if (!strchr(name, ':'))
|
|
|
|
svc = service_find_by_name(name);
|
|
|
|
else {
|
|
|
|
tmp = strdup(name);
|
2013-04-01 05:17:51 +02:00
|
|
|
if (tmp) {
|
|
|
|
args = strchr(tmp, ':');
|
|
|
|
*args = '\0';
|
|
|
|
args++;
|
2009-05-19 22:30:46 +02:00
|
|
|
|
2013-04-01 05:17:51 +02:00
|
|
|
svc = service_find_by_name(tmp);
|
|
|
|
}
|
2009-05-19 22:30:46 +02:00
|
|
|
}
|
2011-09-28 18:55:31 +02:00
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
if (svc) {
|
2009-05-19 22:30:46 +02:00
|
|
|
service_start(svc, args);
|
2008-10-21 16:00:00 +02:00
|
|
|
} else {
|
|
|
|
ERROR("no such service '%s'\n", name);
|
|
|
|
}
|
2009-05-19 22:30:46 +02:00
|
|
|
if (tmp)
|
|
|
|
free(tmp);
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void msg_stop(const char *name)
|
|
|
|
{
|
|
|
|
struct service *svc = service_find_by_name(name);
|
|
|
|
|
|
|
|
if (svc) {
|
|
|
|
service_stop(svc);
|
|
|
|
} else {
|
2009-05-06 03:33:07 +02:00
|
|
|
ERROR("no such service '%s'\n", name);
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-26 05:48:46 +01:00
|
|
|
static void msg_restart(const char *name)
|
|
|
|
{
|
|
|
|
struct service *svc = service_find_by_name(name);
|
|
|
|
|
|
|
|
if (svc) {
|
|
|
|
service_restart(svc);
|
|
|
|
} else {
|
|
|
|
ERROR("no such service '%s'\n", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
void handle_control_message(const char *msg, const char *arg)
|
|
|
|
{
|
|
|
|
if (!strcmp(msg,"start")) {
|
|
|
|
msg_start(arg);
|
|
|
|
} else if (!strcmp(msg,"stop")) {
|
|
|
|
msg_stop(arg);
|
2010-10-03 22:30:11 +02:00
|
|
|
} else if (!strcmp(msg,"restart")) {
|
2012-01-26 05:48:46 +01:00
|
|
|
msg_restart(arg);
|
2008-10-21 16:00:00 +02:00
|
|
|
} else {
|
|
|
|
ERROR("unknown control msg '%s'\n", msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 22:52:55 +02:00
|
|
|
static int wait_for_coldboot_done_action(const std::vector<std::string>& args) {
|
2015-04-25 03:50:30 +02:00
|
|
|
Timer t;
|
|
|
|
|
|
|
|
NOTICE("Waiting for %s...\n", COLDBOOT_DONE);
|
2015-05-14 21:03:14 +02:00
|
|
|
// Any longer than 1s is an unreasonable length of time to delay booting.
|
|
|
|
// If you're hitting this timeout, check that you didn't make your
|
|
|
|
// sepolicy regular expressions too expensive (http://b/19899875).
|
|
|
|
if (wait_for_file(COLDBOOT_DONE, 1)) {
|
2014-11-25 04:52:41 +01:00
|
|
|
ERROR("Timed out waiting for %s\n", COLDBOOT_DONE);
|
2015-04-25 03:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NOTICE("Waiting for %s took %.2fs.\n", COLDBOOT_DONE, t.duration());
|
|
|
|
return 0;
|
2010-04-14 04:52:01 +02:00
|
|
|
}
|
|
|
|
|
2013-08-16 22:19:24 +02:00
|
|
|
/*
|
|
|
|
* Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed
|
|
|
|
* by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom.
|
|
|
|
* Does nothing if Hardware RNG is not present.
|
|
|
|
*
|
|
|
|
* Since we don't yet trust the quality of Hardware RNG, these bytes are not
|
|
|
|
* mixed into the primary pool of Linux RNG and the entropy estimate is left
|
|
|
|
* unmodified.
|
|
|
|
*
|
|
|
|
* If the HW RNG device /dev/hw_random is present, we require that at least
|
|
|
|
* 512 bytes read from it are written into Linux RNG. QA is expected to catch
|
|
|
|
* devices/configurations where these I/O operations are blocking for a long
|
|
|
|
* time. We do not reboot or halt on failures, as this is a best-effort
|
|
|
|
* attempt.
|
|
|
|
*/
|
2015-07-30 22:52:55 +02:00
|
|
|
static int mix_hwrng_into_linux_rng_action(const std::vector<std::string>& args)
|
2013-08-16 22:19:24 +02:00
|
|
|
{
|
|
|
|
int result = -1;
|
|
|
|
int hwrandom_fd = -1;
|
|
|
|
int urandom_fd = -1;
|
|
|
|
char buf[512];
|
|
|
|
ssize_t chunk_size;
|
|
|
|
size_t total_bytes_written = 0;
|
|
|
|
|
|
|
|
hwrandom_fd = TEMP_FAILURE_RETRY(
|
2015-02-02 23:37:22 +01:00
|
|
|
open("/dev/hw_random", O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
|
2013-08-16 22:19:24 +02:00
|
|
|
if (hwrandom_fd == -1) {
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
ERROR("/dev/hw_random not found\n");
|
|
|
|
/* It's not an error to not have a Hardware RNG. */
|
|
|
|
result = 0;
|
|
|
|
} else {
|
|
|
|
ERROR("Failed to open /dev/hw_random: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
urandom_fd = TEMP_FAILURE_RETRY(
|
2015-02-02 23:37:22 +01:00
|
|
|
open("/dev/urandom", O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
|
2013-08-16 22:19:24 +02:00
|
|
|
if (urandom_fd == -1) {
|
|
|
|
ERROR("Failed to open /dev/urandom: %s\n", strerror(errno));
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (total_bytes_written < sizeof(buf)) {
|
|
|
|
chunk_size = TEMP_FAILURE_RETRY(
|
|
|
|
read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written));
|
|
|
|
if (chunk_size == -1) {
|
|
|
|
ERROR("Failed to read from /dev/hw_random: %s\n", strerror(errno));
|
|
|
|
goto ret;
|
|
|
|
} else if (chunk_size == 0) {
|
|
|
|
ERROR("Failed to read from /dev/hw_random: EOF\n");
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size));
|
|
|
|
if (chunk_size == -1) {
|
|
|
|
ERROR("Failed to write to /dev/urandom: %s\n", strerror(errno));
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
total_bytes_written += chunk_size;
|
|
|
|
}
|
|
|
|
|
2014-01-16 19:53:11 +01:00
|
|
|
INFO("Mixed %zu bytes from /dev/hw_random into /dev/urandom",
|
2013-08-16 22:19:24 +02:00
|
|
|
total_bytes_written);
|
|
|
|
result = 0;
|
|
|
|
|
|
|
|
ret:
|
|
|
|
if (hwrandom_fd != -1) {
|
|
|
|
close(hwrandom_fd);
|
|
|
|
}
|
|
|
|
if (urandom_fd != -1) {
|
|
|
|
close(urandom_fd);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-07-30 22:52:55 +02:00
|
|
|
static int keychord_init_action(const std::vector<std::string>& args)
|
2010-04-14 04:52:01 +02:00
|
|
|
{
|
|
|
|
keychord_init();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-30 22:52:55 +02:00
|
|
|
static int console_init_action(const std::vector<std::string>& args)
|
2008-10-21 16:00:00 +02:00
|
|
|
{
|
2015-07-24 19:11:05 +02:00
|
|
|
std::string console = property_get("ro.boot.console");
|
|
|
|
if (!console.empty()) {
|
|
|
|
console_name = "/dev/" + console;
|
2010-04-14 04:52:01 +02:00
|
|
|
}
|
|
|
|
|
2015-07-24 19:11:05 +02:00
|
|
|
int fd = open(console_name.c_str(), O_RDWR | O_CLOEXEC);
|
2010-04-14 04:52:01 +02:00
|
|
|
if (fd >= 0)
|
|
|
|
have_console = 1;
|
|
|
|
close(fd);
|
|
|
|
|
2015-02-02 23:37:22 +01:00
|
|
|
fd = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
|
2013-10-16 17:39:16 +02:00
|
|
|
if (fd >= 0) {
|
|
|
|
const char *msg;
|
|
|
|
msg = "\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"\n" // console is 40 cols x 30 lines
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
" A N D R O I D ";
|
|
|
|
write(fd, msg, strlen(msg));
|
|
|
|
close(fd);
|
2010-04-14 04:52:01 +02:00
|
|
|
}
|
2013-10-16 17:39:16 +02:00
|
|
|
|
2010-04-14 04:52:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-07 04:19:24 +02:00
|
|
|
static void import_kernel_nv(const std::string& key, const std::string& value, bool for_emulator) {
|
|
|
|
if (key.empty()) return;
|
2011-12-19 20:21:32 +01:00
|
|
|
|
|
|
|
if (for_emulator) {
|
2015-05-07 04:19:24 +02:00
|
|
|
// In the emulator, export any kernel option with the "ro.kernel." prefix.
|
|
|
|
property_set(android::base::StringPrintf("ro.kernel.%s", key.c_str()).c_str(), value.c_str());
|
2011-12-19 20:21:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-07 04:19:24 +02:00
|
|
|
if (key == "qemu") {
|
|
|
|
strlcpy(qemu, value.c_str(), sizeof(qemu));
|
|
|
|
} else if (android::base::StartsWith(key, "androidboot.")) {
|
|
|
|
property_set(android::base::StringPrintf("ro.boot.%s", key.c_str() + 12).c_str(),
|
|
|
|
value.c_str());
|
2011-12-19 20:21:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 16:50:46 +01:00
|
|
|
static void export_kernel_boot_props() {
|
2011-12-19 20:21:32 +01:00
|
|
|
struct {
|
|
|
|
const char *src_prop;
|
2015-03-20 16:50:46 +01:00
|
|
|
const char *dst_prop;
|
|
|
|
const char *default_value;
|
2011-12-19 20:21:32 +01:00
|
|
|
} prop_map[] = {
|
2015-03-20 16:50:46 +01:00
|
|
|
{ "ro.boot.serialno", "ro.serialno", "", },
|
|
|
|
{ "ro.boot.mode", "ro.bootmode", "unknown", },
|
|
|
|
{ "ro.boot.baseband", "ro.baseband", "unknown", },
|
2011-12-19 20:21:32 +01:00
|
|
|
{ "ro.boot.bootloader", "ro.bootloader", "unknown", },
|
2015-03-20 16:50:46 +01:00
|
|
|
{ "ro.boot.hardware", "ro.hardware", "unknown", },
|
|
|
|
{ "ro.boot.revision", "ro.revision", "0", },
|
2011-12-19 20:21:32 +01:00
|
|
|
};
|
2015-03-20 16:50:46 +01:00
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(prop_map); i++) {
|
2015-07-24 19:11:05 +02:00
|
|
|
std::string value = property_get(prop_map[i].src_prop);
|
|
|
|
property_set(prop_map[i].dst_prop, (!value.empty()) ? value.c_str() : prop_map[i].default_value);
|
2011-12-19 20:21:32 +01:00
|
|
|
}
|
|
|
|
}
|
2010-04-14 04:52:01 +02:00
|
|
|
|
2015-05-07 04:19:24 +02:00
|
|
|
static void process_kernel_dt() {
|
2015-02-28 15:39:11 +01:00
|
|
|
static const char android_dir[] = "/proc/device-tree/firmware/android";
|
|
|
|
|
|
|
|
std::string file_name = android::base::StringPrintf("%s/compatible", android_dir);
|
|
|
|
|
|
|
|
std::string dt_file;
|
|
|
|
android::base::ReadFileToString(file_name, &dt_file);
|
|
|
|
if (!dt_file.compare("android,firmware")) {
|
|
|
|
ERROR("firmware/android is not compatible with 'android,firmware'\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(android_dir), closedir);
|
2015-05-07 04:19:24 +02:00
|
|
|
if (!dir) return;
|
2015-02-28 15:39:11 +01:00
|
|
|
|
|
|
|
struct dirent *dp;
|
|
|
|
while ((dp = readdir(dir.get())) != NULL) {
|
2015-05-07 04:19:24 +02:00
|
|
|
if (dp->d_type != DT_REG || !strcmp(dp->d_name, "compatible")) {
|
2015-02-28 15:39:11 +01:00
|
|
|
continue;
|
2015-05-07 04:19:24 +02:00
|
|
|
}
|
2015-02-28 15:39:11 +01:00
|
|
|
|
|
|
|
file_name = android::base::StringPrintf("%s/%s", android_dir, dp->d_name);
|
|
|
|
|
|
|
|
android::base::ReadFileToString(file_name, &dt_file);
|
|
|
|
std::replace(dt_file.begin(), dt_file.end(), ',', '.');
|
|
|
|
|
|
|
|
std::string property_name = android::base::StringPrintf("ro.boot.%s", dp->d_name);
|
2015-03-20 17:45:18 +01:00
|
|
|
property_set(property_name.c_str(), dt_file.c_str());
|
2015-02-28 15:39:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 04:19:24 +02:00
|
|
|
static void process_kernel_cmdline() {
|
|
|
|
// Don't expose the raw commandline to unprivileged processes.
|
2011-12-19 20:21:32 +01:00
|
|
|
chmod("/proc/cmdline", 0440);
|
2010-04-14 04:52:01 +02:00
|
|
|
|
2015-05-07 04:19:24 +02:00
|
|
|
// The first pass does the common stuff, and finds if we are in qemu.
|
|
|
|
// The second pass is only necessary for qemu to export all kernel params
|
|
|
|
// as properties.
|
2015-04-26 02:42:52 +02:00
|
|
|
import_kernel_cmdline(false, import_kernel_nv);
|
2015-05-07 04:19:24 +02:00
|
|
|
if (qemu[0]) import_kernel_cmdline(true, import_kernel_nv);
|
2010-04-14 04:52:01 +02:00
|
|
|
}
|
|
|
|
|
2015-07-30 22:52:55 +02:00
|
|
|
static int queue_property_triggers_action(const std::vector<std::string>& args)
|
2010-04-14 04:52:01 +02:00
|
|
|
{
|
2015-07-24 02:53:11 +02:00
|
|
|
ActionManager::GetInstance().QueueAllPropertyTriggers();
|
2010-04-14 04:52:01 +02:00
|
|
|
/* enable property triggers */
|
|
|
|
property_triggers_enabled = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-25 03:50:30 +02:00
|
|
|
static void selinux_init_all_handles(void)
|
2012-01-13 14:48:47 +01:00
|
|
|
{
|
2012-05-01 21:02:53 +02:00
|
|
|
sehandle = selinux_android_file_context_handle();
|
2014-01-28 16:34:09 +01:00
|
|
|
selinux_android_set_sehandle(sehandle);
|
2012-08-09 16:05:49 +02:00
|
|
|
sehandle_prop = selinux_android_prop_context_handle();
|
2012-05-01 21:02:53 +02:00
|
|
|
}
|
2012-01-13 14:48:47 +01:00
|
|
|
|
2015-04-28 21:39:41 +02:00
|
|
|
enum selinux_enforcing_status { SELINUX_PERMISSIVE, SELINUX_ENFORCING };
|
2015-04-26 02:42:52 +02:00
|
|
|
|
|
|
|
static selinux_enforcing_status selinux_status_from_cmdline() {
|
|
|
|
selinux_enforcing_status status = SELINUX_ENFORCING;
|
|
|
|
|
2015-05-07 04:19:24 +02:00
|
|
|
import_kernel_cmdline(false, [&](const std::string& key, const std::string& value, bool in_qemu) {
|
|
|
|
if (key == "androidboot.selinux" && value == "permissive") {
|
|
|
|
status = SELINUX_PERMISSIVE;
|
2015-04-26 02:42:52 +02:00
|
|
|
}
|
2015-05-07 04:19:24 +02:00
|
|
|
});
|
2015-04-26 02:42:52 +02:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2013-06-27 00:37:26 +02:00
|
|
|
static bool selinux_is_enforcing(void)
|
|
|
|
{
|
2015-04-28 21:39:41 +02:00
|
|
|
if (ALLOW_PERMISSIVE_SELINUX) {
|
2015-04-26 02:42:52 +02:00
|
|
|
return selinux_status_from_cmdline() == SELINUX_ENFORCING;
|
2013-06-27 00:37:26 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-01 21:02:53 +02:00
|
|
|
int selinux_reload_policy(void)
|
|
|
|
{
|
|
|
|
INFO("SELinux: Attempting to reload policy files\n");
|
2012-01-13 14:48:47 +01:00
|
|
|
|
2012-05-01 21:02:53 +02:00
|
|
|
if (selinux_android_reload_policy() == -1) {
|
|
|
|
return -1;
|
2012-01-13 14:48:47 +01:00
|
|
|
}
|
|
|
|
|
2012-05-01 21:02:53 +02:00
|
|
|
if (sehandle)
|
|
|
|
selabel_close(sehandle);
|
2012-01-13 14:48:47 +01:00
|
|
|
|
2012-08-09 16:05:49 +02:00
|
|
|
if (sehandle_prop)
|
|
|
|
selabel_close(sehandle_prop);
|
|
|
|
|
2012-05-01 21:02:53 +02:00
|
|
|
selinux_init_all_handles();
|
|
|
|
return 0;
|
2012-01-13 14:48:47 +01:00
|
|
|
}
|
2012-08-09 16:05:49 +02:00
|
|
|
|
2015-03-28 07:20:44 +01:00
|
|
|
static int audit_callback(void *data, security_class_t /*cls*/, char *buf, size_t len) {
|
2012-08-09 16:05:49 +02:00
|
|
|
snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-24 20:38:17 +02:00
|
|
|
static void security_failure() {
|
|
|
|
ERROR("Security failure; rebooting into recovery mode...\n");
|
|
|
|
android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
|
|
|
|
while (true) { pause(); } // never reached
|
|
|
|
}
|
|
|
|
|
|
|
|
static void selinux_initialize(bool in_kernel_domain) {
|
2015-03-28 07:20:44 +01:00
|
|
|
Timer t;
|
|
|
|
|
|
|
|
selinux_callback cb;
|
|
|
|
cb.func_log = selinux_klog_callback;
|
|
|
|
selinux_set_callback(SELINUX_CB_LOG, cb);
|
|
|
|
cb.func_audit = audit_callback;
|
|
|
|
selinux_set_callback(SELINUX_CB_AUDIT, cb);
|
2014-02-12 22:17:00 +01:00
|
|
|
|
2015-04-24 20:38:17 +02:00
|
|
|
if (in_kernel_domain) {
|
|
|
|
INFO("Loading SELinux policy...\n");
|
|
|
|
if (selinux_android_load_policy() < 0) {
|
|
|
|
ERROR("failed to load policy: %s\n", strerror(errno));
|
|
|
|
security_failure();
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:39:41 +02:00
|
|
|
bool kernel_enforcing = (security_getenforce() == 1);
|
2015-04-24 20:38:17 +02:00
|
|
|
bool is_enforcing = selinux_is_enforcing();
|
2015-04-28 21:39:41 +02:00
|
|
|
if (kernel_enforcing != is_enforcing) {
|
|
|
|
if (security_setenforce(is_enforcing)) {
|
|
|
|
ERROR("security_setenforce(%s) failed: %s\n",
|
|
|
|
is_enforcing ? "true" : "false", strerror(errno));
|
|
|
|
security_failure();
|
|
|
|
}
|
|
|
|
}
|
2015-03-28 07:20:44 +01:00
|
|
|
|
2015-04-25 23:10:03 +02:00
|
|
|
if (write_file("/sys/fs/selinux/checkreqprot", "0") == -1) {
|
|
|
|
security_failure();
|
|
|
|
}
|
|
|
|
|
2015-04-24 20:38:17 +02:00
|
|
|
NOTICE("(Initializing SELinux %s took %.2fs.)\n",
|
|
|
|
is_enforcing ? "enforcing" : "non-enforcing", t.duration());
|
|
|
|
} else {
|
|
|
|
selinux_init_all_handles();
|
|
|
|
}
|
2013-06-25 02:41:40 +02:00
|
|
|
}
|
|
|
|
|
2015-02-07 05:15:18 +01:00
|
|
|
int main(int argc, char** argv) {
|
2015-03-28 07:20:44 +01:00
|
|
|
if (!strcmp(basename(argv[0]), "ueventd")) {
|
2010-04-21 21:04:20 +02:00
|
|
|
return ueventd_main(argc, argv);
|
2015-03-28 07:20:44 +01:00
|
|
|
}
|
2010-04-21 21:04:20 +02:00
|
|
|
|
2015-03-28 07:20:44 +01:00
|
|
|
if (!strcmp(basename(argv[0]), "watchdogd")) {
|
2012-06-14 06:51:56 +02:00
|
|
|
return watchdogd_main(argc, argv);
|
2015-03-28 07:20:44 +01:00
|
|
|
}
|
2012-06-14 06:51:56 +02:00
|
|
|
|
2015-02-07 05:15:18 +01:00
|
|
|
// Clear the umask.
|
2008-10-21 16:00:00 +02:00
|
|
|
umask(0);
|
|
|
|
|
2015-03-18 04:01:13 +01:00
|
|
|
add_environment("PATH", _PATH_DEFPATH);
|
|
|
|
|
2015-04-26 00:50:03 +02:00
|
|
|
bool is_first_stage = (argc == 1) || (strcmp(argv[1], "--second-stage") != 0);
|
|
|
|
|
2015-02-07 05:15:18 +01:00
|
|
|
// Get the basic filesystem setup we need put together in the initramdisk
|
|
|
|
// on / and then we'll let the rc file figure out the rest.
|
2015-04-26 00:50:03 +02:00
|
|
|
if (is_first_stage) {
|
2015-04-26 02:42:52 +02:00
|
|
|
mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
|
|
|
|
mkdir("/dev/pts", 0755);
|
|
|
|
mkdir("/dev/socket", 0755);
|
|
|
|
mount("devpts", "/dev/pts", "devpts", 0, NULL);
|
2015-04-26 00:50:03 +02:00
|
|
|
mount("proc", "/proc", "proc", 0, NULL);
|
|
|
|
mount("sysfs", "/sys", "sysfs", 0, NULL);
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2015-02-07 05:15:18 +01:00
|
|
|
// We must have some place other than / to create the device nodes for
|
|
|
|
// kmsg and null, otherwise we won't be able to remount / read-only
|
|
|
|
// later on. Now that tmpfs is mounted on /dev, we can actually talk
|
|
|
|
// to the outside world.
|
2008-10-21 16:00:00 +02:00
|
|
|
open_devnull_stdio();
|
2011-09-01 03:26:17 +02:00
|
|
|
klog_init();
|
2015-03-28 07:20:44 +01:00
|
|
|
klog_set_level(KLOG_NOTICE_LEVEL);
|
|
|
|
|
2015-05-07 04:19:24 +02:00
|
|
|
NOTICE("init %s started!\n", is_first_stage ? "first stage" : "second stage");
|
2015-03-28 07:20:44 +01:00
|
|
|
|
2015-04-26 02:42:52 +02:00
|
|
|
if (!is_first_stage) {
|
|
|
|
// Indicate that booting is in progress to background fw loaders, etc.
|
|
|
|
close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));
|
2011-09-28 18:55:31 +02:00
|
|
|
|
2015-04-26 02:42:52 +02:00
|
|
|
property_init();
|
2011-12-19 20:21:32 +01:00
|
|
|
|
2015-04-26 02:42:52 +02:00
|
|
|
// If arguments are passed both on the command line and in DT,
|
|
|
|
// properties set in DT always have priority over the command-line ones.
|
|
|
|
process_kernel_dt();
|
|
|
|
process_kernel_cmdline();
|
|
|
|
|
2015-05-07 04:19:24 +02:00
|
|
|
// Propagate the kernel variables to internal variables
|
2015-04-26 02:42:52 +02:00
|
|
|
// used by init as well as the current required properties.
|
|
|
|
export_kernel_boot_props();
|
|
|
|
}
|
2015-02-28 15:39:11 +01:00
|
|
|
|
2015-04-24 20:38:17 +02:00
|
|
|
// Set up SELinux, including loading the SELinux policy if we're in the kernel domain.
|
|
|
|
selinux_initialize(is_first_stage);
|
|
|
|
|
|
|
|
// If we're in the kernel domain, re-exec init to transition to the init domain now
|
|
|
|
// that the SELinux policy has been loaded.
|
|
|
|
if (is_first_stage) {
|
|
|
|
if (restorecon("/init") == -1) {
|
|
|
|
ERROR("restorecon failed: %s\n", strerror(errno));
|
|
|
|
security_failure();
|
|
|
|
}
|
|
|
|
char* path = argv[0];
|
|
|
|
char* args[] = { path, const_cast<char*>("--second-stage"), nullptr };
|
|
|
|
if (execv(path, args) == -1) {
|
|
|
|
ERROR("execv(\"%s\") failed: %s\n", path, strerror(errno));
|
|
|
|
security_failure();
|
|
|
|
}
|
|
|
|
}
|
2015-02-07 05:15:18 +01:00
|
|
|
|
|
|
|
// These directories were necessarily created before initial policy load
|
|
|
|
// and therefore need their security context restored to the proper value.
|
|
|
|
// This must happen before /dev is populated by ueventd.
|
2015-05-07 04:19:24 +02:00
|
|
|
NOTICE("Running restorecon...\n");
|
2012-06-11 19:37:39 +02:00
|
|
|
restorecon("/dev");
|
|
|
|
restorecon("/dev/socket");
|
2013-03-01 02:29:58 +01:00
|
|
|
restorecon("/dev/__properties__");
|
2013-07-12 00:38:26 +02:00
|
|
|
restorecon_recursive("/sys");
|
2012-01-13 14:48:47 +01:00
|
|
|
|
2015-04-25 06:13:44 +02:00
|
|
|
epoll_fd = epoll_create1(EPOLL_CLOEXEC);
|
|
|
|
if (epoll_fd == -1) {
|
|
|
|
ERROR("epoll_create1 failed: %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
signal_handler_init();
|
2015-04-25 02:43:21 +02:00
|
|
|
|
2014-06-17 00:06:21 +02:00
|
|
|
property_load_boot_defaults();
|
2015-04-25 03:50:30 +02:00
|
|
|
start_property_service();
|
2011-12-16 23:18:06 +01:00
|
|
|
|
2015-07-25 01:57:14 +02:00
|
|
|
init_parse_config("/init.rc");
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2015-07-24 02:53:11 +02:00
|
|
|
ActionManager& am = ActionManager::GetInstance();
|
|
|
|
|
|
|
|
am.QueueEventTrigger("early-init");
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2015-04-25 03:50:30 +02:00
|
|
|
// Queue an action that waits for coldboot done so we know ueventd has set up all of /dev...
|
2015-07-24 02:53:11 +02:00
|
|
|
am.QueueBuiltinAction(wait_for_coldboot_done_action, "wait_for_coldboot_done");
|
2015-04-25 03:50:30 +02:00
|
|
|
// ... so that we can start queuing up actions that require stuff from /dev.
|
2015-07-24 02:53:11 +02:00
|
|
|
am.QueueBuiltinAction(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
|
|
|
|
am.QueueBuiltinAction(keychord_init_action, "keychord_init");
|
|
|
|
am.QueueBuiltinAction(console_init_action, "console_init");
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2015-03-28 07:20:44 +01:00
|
|
|
// Trigger all the boot actions to get us started.
|
2015-07-24 02:53:11 +02:00
|
|
|
am.QueueEventTrigger("init");
|
2011-08-25 00:28:23 +02:00
|
|
|
|
2015-02-07 05:15:18 +01:00
|
|
|
// Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
|
|
|
|
// wasn't ready immediately after wait_for_coldboot_done
|
2015-07-24 02:53:11 +02:00
|
|
|
am.QueueBuiltinAction(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2015-02-07 05:15:18 +01:00
|
|
|
// Don't mount filesystems or start core system services in charger mode.
|
2015-07-24 19:11:05 +02:00
|
|
|
std::string bootmode = property_get("ro.bootmode");
|
|
|
|
if (bootmode == "charger") {
|
2015-07-24 02:53:11 +02:00
|
|
|
am.QueueEventTrigger("charger");
|
2011-08-25 00:28:23 +02:00
|
|
|
} else {
|
2015-07-24 02:53:11 +02:00
|
|
|
am.QueueEventTrigger("late-init");
|
2011-08-25 00:28:23 +02:00
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2015-02-07 05:15:18 +01:00
|
|
|
// Run all property triggers based on current state of the properties.
|
2015-07-24 02:53:11 +02:00
|
|
|
am.QueueBuiltinAction(queue_property_triggers_action, "queue_property_triggers");
|
2010-04-14 04:52:01 +02:00
|
|
|
|
2015-04-25 03:50:30 +02:00
|
|
|
while (true) {
|
2015-02-07 05:15:18 +01:00
|
|
|
if (!waiting_for_exec) {
|
2015-07-24 02:53:11 +02:00
|
|
|
am.ExecuteOneCommand();
|
2015-02-07 05:15:18 +01:00
|
|
|
restart_processes();
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2014-12-05 06:45:02 +01:00
|
|
|
int timeout = -1;
|
2008-10-21 16:00:00 +02:00
|
|
|
if (process_needs_restart) {
|
|
|
|
timeout = (process_needs_restart - gettime()) * 1000;
|
|
|
|
if (timeout < 0)
|
|
|
|
timeout = 0;
|
|
|
|
}
|
|
|
|
|
2015-07-24 02:53:11 +02:00
|
|
|
if (am.HasMoreCommands()) {
|
2010-04-14 04:52:01 +02:00
|
|
|
timeout = 0;
|
2015-02-04 23:46:36 +01:00
|
|
|
}
|
2010-04-14 04:52:01 +02:00
|
|
|
|
2014-12-05 06:45:02 +01:00
|
|
|
bootchart_sample(&timeout);
|
2010-04-14 04:52:01 +02:00
|
|
|
|
2015-04-25 06:13:44 +02:00
|
|
|
epoll_event ev;
|
|
|
|
int nr = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd, &ev, 1, timeout));
|
|
|
|
if (nr == -1) {
|
|
|
|
ERROR("epoll_wait failed: %s\n", strerror(errno));
|
|
|
|
} else if (nr == 1) {
|
|
|
|
((void (*)()) ev.data.ptr)();
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|