2015-07-31 21:45:25 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "service.h"
|
|
|
|
|
2022-11-15 01:45:47 +01:00
|
|
|
#include <errno.h>
|
2015-07-31 21:45:25 +02:00
|
|
|
#include <fcntl.h>
|
2016-11-11 02:43:47 +01:00
|
|
|
#include <inttypes.h>
|
init: Add support for ambient capabilities.
Ambient capabilities are inherited in a straightforward way across
execve(2):
"
If you are nonroot but you have a capability, you can add it to pA.
If you do so, your children get that capability in pA, pP, and pE.
For example, you can set pA = CAP_NET_BIND_SERVICE, and your
children can automatically bind low-numbered ports.
"
This will allow us to get rid of the special meaning for AID_NET_ADMIN
and AID_NET_RAW, and if desired, to reduce the use of file capabilities
(which grant capabilities to any process that can execute the file). An
additional benefit of the latter is that a single .rc file can specify
all properties for a service, without having to rely on a separate file
for file capabilities.
Ambient capabilities are supported starting with kernel 4.3 and have
been backported to all Android common kernels back to 3.10.
I chose to not use Minijail here (though I'm still using libcap) for
two reasons:
1-The Minijail code is designed to work in situations where the process
is holding any set of capabilities, so it's more complex. The situation
when forking from init allows for simpler code.
2-The way Minijail is structured right now, we would not be able to
make the required SELinux calls between UID/GID dropping and other priv
dropping code. In the future, it will make sense to add some sort of
"hook" to Minijail so that it can be used in situations where we want
to do other operations between some of the privilege-dropping
operations carried out by Minijail.
Bug: 32438163
Test: Use sample service.
Change-Id: I3226cc95769d1beacbae619cb6c6e6a5425890fb
2016-10-27 16:33:03 +02:00
|
|
|
#include <linux/securebits.h>
|
2016-04-22 00:35:09 +02:00
|
|
|
#include <sched.h>
|
|
|
|
#include <sys/prctl.h>
|
2015-07-31 21:45:25 +02:00
|
|
|
#include <sys/stat.h>
|
2016-05-19 02:36:30 +02:00
|
|
|
#include <sys/time.h>
|
2015-07-31 21:45:25 +02:00
|
|
|
#include <termios.h>
|
2015-08-12 01:37:04 +02:00
|
|
|
#include <unistd.h>
|
2022-12-02 00:44:31 +01:00
|
|
|
#include <thread>
|
2015-07-31 21:45:25 +02:00
|
|
|
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/file.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <android-base/logging.h>
|
2018-05-25 03:00:39 +02:00
|
|
|
#include <android-base/properties.h>
|
2019-09-10 19:40:47 +02:00
|
|
|
#include <android-base/scopeguard.h>
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
2016-06-25 00:12:21 +02:00
|
|
|
#include <android-base/strings.h>
|
2019-07-09 22:33:36 +02:00
|
|
|
#include <cutils/sockets.h>
|
2016-06-01 23:03:55 +02:00
|
|
|
#include <processgroup/processgroup.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <selinux/selinux.h>
|
2023-11-16 21:42:35 +01:00
|
|
|
#include <sys/signalfd.h>
|
2016-06-01 23:03:55 +02:00
|
|
|
|
2022-05-11 02:55:08 +02:00
|
|
|
#include <string>
|
|
|
|
|
2022-10-20 01:30:15 +02:00
|
|
|
#include "interprocess_fifo.h"
|
2019-10-23 02:18:42 +02:00
|
|
|
#include "lmkd_service.h"
|
2019-06-26 19:46:20 +02:00
|
|
|
#include "service_list.h"
|
2015-07-31 21:45:25 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
2022-09-13 03:19:47 +02:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
#include <bionic/reserved_signals.h>
|
|
|
|
#endif
|
|
|
|
|
2020-02-20 19:50:00 +01:00
|
|
|
#ifdef INIT_FULL_SOURCES
|
2019-11-19 23:19:40 +01:00
|
|
|
#include <android/api-level.h>
|
2018-02-14 01:50:08 +01:00
|
|
|
|
Proper mount namespace configuration for bionic
This CL fixes the design problem of the previous mechanism for providing
the bootstrap bionic and the runtime bionic to the same path.
Previously, bootstrap bionic was self-bind-mounted; i.e.
/system/bin/libc.so is bind-mounted to itself. And the runtime bionic
was bind-mounted on top of the bootstrap bionic. This has not only caused
problems like `adb sync` not working(b/122737045), but also is quite
difficult to understand due to the double-and-self mounting.
This is the new design:
Most importantly, these four are all distinct:
1) bootstrap bionic (/system/lib/bootstrap/libc.so)
2) runtime bionic (/apex/com.android.runtime/lib/bionic/libc.so)
3) mount point for 1) and 2) (/bionic/lib/libc.so)
4) symlink for 3) (/system/lib/libc.so -> /bionic/lib/libc.so)
Inside the mount namespace of the pre-apexd processes, 1) is
bind-mounted to 3). Likewise, inside the mount namespace of the
post-apexd processes, 2) is bind-mounted to 3). In other words, there is
no self-mount, and no double-mount.
Another change is that mount points are under /bionic and the legacy
paths become symlinks to the mount points. This is to make sure that
there is no bind mounts under /system, which is breaking some apps.
Finally, code for creating mount namespaces, mounting bionic, etc are
refactored to mount_namespace.cpp
Bug: 120266448
Bug: 123275379
Test: m, device boots, adb sync/push/pull works,
especially with following paths:
/bionic/lib64/libc.so
/bionic/bin/linker64
/system/lib64/bootstrap/libc.so
/system/bin/bootstrap/linker64
Change-Id: Icdfbdcc1efca540ac854d4df79e07ee61fca559f
2019-01-16 15:00:59 +01:00
|
|
|
#include "mount_namespace.h"
|
2019-12-26 15:22:28 +01:00
|
|
|
#include "reboot_utils.h"
|
2019-11-19 23:19:40 +01:00
|
|
|
#include "selinux.h"
|
2018-02-14 01:50:08 +01:00
|
|
|
#else
|
|
|
|
#include "host_init_stubs.h"
|
|
|
|
#endif
|
|
|
|
|
2017-03-24 19:43:02 +01:00
|
|
|
using android::base::boot_clock;
|
2021-01-20 05:28:33 +01:00
|
|
|
using android::base::GetBoolProperty;
|
2022-05-11 02:55:08 +02:00
|
|
|
using android::base::GetIntProperty;
|
2017-06-22 21:53:17 +02:00
|
|
|
using android::base::GetProperty;
|
|
|
|
using android::base::Join;
|
2019-09-10 19:40:47 +02:00
|
|
|
using android::base::make_scope_guard;
|
2019-08-20 00:21:25 +02:00
|
|
|
using android::base::SetProperty;
|
2017-06-22 21:53:17 +02:00
|
|
|
using android::base::StartsWith;
|
2015-08-26 20:43:36 +02:00
|
|
|
using android::base::StringPrintf;
|
2023-11-16 21:42:35 +01:00
|
|
|
using android::base::unique_fd;
|
2015-08-26 20:43:36 +02:00
|
|
|
using android::base::WriteStringToFile;
|
|
|
|
|
2017-06-22 21:53:17 +02:00
|
|
|
namespace android {
|
|
|
|
namespace init {
|
|
|
|
|
2018-04-21 01:18:12 +02:00
|
|
|
static Result<std::string> ComputeContextFromExecutable(const std::string& service_path) {
|
2016-07-08 19:32:26 +02:00
|
|
|
std::string computed_context;
|
|
|
|
|
|
|
|
char* raw_con = nullptr;
|
|
|
|
char* raw_filecon = nullptr;
|
|
|
|
|
|
|
|
if (getcon(&raw_con) == -1) {
|
2017-08-23 01:13:59 +02:00
|
|
|
return Error() << "Could not get security context";
|
2016-07-08 19:32:26 +02:00
|
|
|
}
|
2020-12-09 07:30:17 +01:00
|
|
|
std::unique_ptr<char, decltype(&freecon)> mycon(raw_con, freecon);
|
2016-07-08 19:32:26 +02:00
|
|
|
|
|
|
|
if (getfilecon(service_path.c_str(), &raw_filecon) == -1) {
|
2017-08-23 01:13:59 +02:00
|
|
|
return Error() << "Could not get file context";
|
2016-07-08 19:32:26 +02:00
|
|
|
}
|
2020-12-09 07:30:17 +01:00
|
|
|
std::unique_ptr<char, decltype(&freecon)> filecon(raw_filecon, freecon);
|
2016-07-08 19:32:26 +02:00
|
|
|
|
|
|
|
char* new_con = nullptr;
|
|
|
|
int rc = security_compute_create(mycon.get(), filecon.get(),
|
|
|
|
string_to_security_class("process"), &new_con);
|
|
|
|
if (rc == 0) {
|
|
|
|
computed_context = new_con;
|
|
|
|
free(new_con);
|
|
|
|
}
|
|
|
|
if (rc == 0 && computed_context == mycon.get()) {
|
2017-08-25 21:08:57 +02:00
|
|
|
return Error() << "File " << service_path << "(labeled \"" << filecon.get()
|
|
|
|
<< "\") has incorrect label or no domain transition from " << mycon.get()
|
|
|
|
<< " to another SELinux domain defined. Have you configured your "
|
|
|
|
"service correctly? https://source.android.com/security/selinux/"
|
2020-04-09 21:45:03 +02:00
|
|
|
"device-policy#label_new_services_and_address_denials. Note: this "
|
|
|
|
"error shows up even in permissive mode in order to make auditing "
|
|
|
|
"denials possible.";
|
2016-07-08 19:32:26 +02:00
|
|
|
}
|
|
|
|
if (rc < 0) {
|
2017-08-23 01:13:59 +02:00
|
|
|
return Error() << "Could not get process context";
|
2016-07-08 19:32:26 +02:00
|
|
|
}
|
|
|
|
return computed_context;
|
|
|
|
}
|
|
|
|
|
2018-04-17 23:48:44 +02:00
|
|
|
static bool ExpandArgsAndExecv(const std::vector<std::string>& args, bool sigstop) {
|
2016-07-08 19:32:26 +02:00
|
|
|
std::vector<std::string> expanded_args;
|
2017-09-12 01:08:54 +02:00
|
|
|
std::vector<char*> c_strings;
|
|
|
|
|
2016-07-08 19:32:26 +02:00
|
|
|
expanded_args.resize(args.size());
|
2017-09-12 01:08:54 +02:00
|
|
|
c_strings.push_back(const_cast<char*>(args[0].data()));
|
2016-07-08 19:32:26 +02:00
|
|
|
for (std::size_t i = 1; i < args.size(); ++i) {
|
2019-07-31 22:59:15 +02:00
|
|
|
auto expanded_arg = ExpandProps(args[i]);
|
2020-02-05 19:49:33 +01:00
|
|
|
if (!expanded_arg.ok()) {
|
2019-07-31 22:59:15 +02:00
|
|
|
LOG(FATAL) << args[0] << ": cannot expand arguments': " << expanded_arg.error();
|
2016-07-08 19:32:26 +02:00
|
|
|
}
|
2019-07-31 22:59:15 +02:00
|
|
|
expanded_args[i] = *expanded_arg;
|
2017-09-12 01:08:54 +02:00
|
|
|
c_strings.push_back(expanded_args[i].data());
|
2016-07-08 19:32:26 +02:00
|
|
|
}
|
2017-09-12 01:08:54 +02:00
|
|
|
c_strings.push_back(nullptr);
|
|
|
|
|
2018-04-17 23:48:44 +02:00
|
|
|
if (sigstop) {
|
|
|
|
kill(getpid(), SIGSTOP);
|
|
|
|
}
|
|
|
|
|
2017-09-12 01:08:54 +02:00
|
|
|
return execv(c_strings[0], c_strings.data()) == 0;
|
2016-07-08 19:32:26 +02:00
|
|
|
}
|
|
|
|
|
2017-07-27 01:09:09 +02:00
|
|
|
unsigned long Service::next_start_order_ = 1;
|
2017-07-28 23:48:41 +02:00
|
|
|
bool Service::is_exec_service_running_ = false;
|
2017-07-27 01:09:09 +02:00
|
|
|
|
2017-09-13 00:58:47 +02:00
|
|
|
Service::Service(const std::string& name, Subcontext* subcontext_for_restart_commands,
|
2022-07-15 00:51:10 +02:00
|
|
|
const std::string& filename, const std::vector<std::string>& args)
|
2023-04-04 01:29:22 +02:00
|
|
|
: Service(name, 0, std::nullopt, 0, {}, 0, "", subcontext_for_restart_commands, filename,
|
|
|
|
args) {}
|
2015-07-31 21:45:25 +02:00
|
|
|
|
2023-04-04 01:29:22 +02:00
|
|
|
Service::Service(const std::string& name, unsigned flags, std::optional<uid_t> uid, gid_t gid,
|
2019-07-09 00:09:36 +02:00
|
|
|
const std::vector<gid_t>& supp_gids, int namespace_flags,
|
2019-02-06 19:45:56 +01:00
|
|
|
const std::string& seclabel, Subcontext* subcontext_for_restart_commands,
|
2022-07-15 00:51:10 +02:00
|
|
|
const std::string& filename, const std::vector<std::string>& args)
|
2017-03-27 19:59:11 +02:00
|
|
|
: name_(name),
|
|
|
|
classnames_({"default"}),
|
|
|
|
flags_(flags),
|
|
|
|
pid_(0),
|
|
|
|
crash_count_(0),
|
2019-05-30 00:58:32 +02:00
|
|
|
proc_attr_{.ioprio_class = IoSchedClass_NONE,
|
|
|
|
.ioprio_pri = 0,
|
2023-04-04 01:29:22 +02:00
|
|
|
.parsed_uid = uid,
|
2019-05-30 00:58:32 +02:00
|
|
|
.gid = gid,
|
|
|
|
.supp_gids = supp_gids,
|
|
|
|
.priority = 0},
|
|
|
|
namespaces_{.flags = namespace_flags},
|
2017-03-27 19:59:11 +02:00
|
|
|
seclabel_(seclabel),
|
2020-11-10 02:28:24 +01:00
|
|
|
subcontext_(subcontext_for_restart_commands),
|
2018-02-14 01:24:51 +01:00
|
|
|
onrestart_(false, subcontext_for_restart_commands, "<Service '" + name + "' onrestart>", 0,
|
|
|
|
"onrestart", {}),
|
2019-10-23 02:18:42 +02:00
|
|
|
oom_score_adjust_(DEFAULT_OOM_SCORE_ADJUST),
|
2017-07-27 01:09:09 +02:00
|
|
|
start_order_(0),
|
2019-12-05 13:35:19 +01:00
|
|
|
args_(args),
|
2022-07-15 00:51:10 +02:00
|
|
|
filename_(filename) {}
|
2015-07-31 21:45:25 +02:00
|
|
|
|
|
|
|
void Service::NotifyStateChange(const std::string& new_state) const {
|
2017-03-28 01:27:30 +02:00
|
|
|
if ((flags_ & SVC_TEMPORARY) != 0) {
|
|
|
|
// Services created by 'exec' are temporary and don't have properties tracking their state.
|
2015-07-31 21:45:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-23 01:50:31 +02:00
|
|
|
std::string prop_name = "init.svc." + name_;
|
2019-08-20 00:21:25 +02:00
|
|
|
SetProperty(prop_name, new_state);
|
2016-11-11 02:43:47 +01:00
|
|
|
|
|
|
|
if (new_state == "running") {
|
|
|
|
uint64_t start_ns = time_started_.time_since_epoch().count();
|
2017-08-18 19:47:46 +02:00
|
|
|
std::string boottime_property = "ro.boottime." + name_;
|
|
|
|
if (GetProperty(boottime_property, "").empty()) {
|
2019-08-20 00:21:25 +02:00
|
|
|
SetProperty(boottime_property, std::to_string(start_ns));
|
2017-08-18 19:47:46 +02:00
|
|
|
}
|
2016-11-11 02:43:47 +01:00
|
|
|
}
|
2019-07-19 20:04:01 +02:00
|
|
|
|
|
|
|
// init.svc_debug_pid.* properties are only for tests, and should not be used
|
|
|
|
// on device for security checks.
|
|
|
|
std::string pid_property = "init.svc_debug_pid." + name_;
|
|
|
|
if (new_state == "running") {
|
2019-08-20 00:21:25 +02:00
|
|
|
SetProperty(pid_property, std::to_string(pid_));
|
2019-07-19 20:04:01 +02:00
|
|
|
} else if (new_state == "stopped") {
|
2019-08-20 00:21:25 +02:00
|
|
|
SetProperty(pid_property, "");
|
2019-07-19 20:04:01 +02:00
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2023-10-26 22:46:21 +02:00
|
|
|
void Service::KillProcessGroup(int signal) {
|
2023-11-13 22:42:13 +01:00
|
|
|
// Always attempt the process kill if process is still running.
|
|
|
|
// Cgroup clean up routines are idempotent. It's safe to call
|
|
|
|
// killProcessGroup repeatedly. During shutdown, `init` will
|
|
|
|
// call this function to send SIGTERM/SIGKILL to all processes.
|
|
|
|
// These signals must be sent for a successful shutdown.
|
|
|
|
if (!process_cgroup_empty_ || IsRunning()) {
|
2017-05-04 20:32:36 +02:00
|
|
|
LOG(INFO) << "Sending signal " << signal << " to service '" << name_ << "' (pid " << pid_
|
|
|
|
<< ") process group...";
|
|
|
|
int r;
|
|
|
|
if (signal == SIGTERM) {
|
2023-10-26 22:46:21 +02:00
|
|
|
r = killProcessGroupOnce(uid(), pid_, signal);
|
2017-05-04 20:32:36 +02:00
|
|
|
} else {
|
2023-10-26 22:46:21 +02:00
|
|
|
r = killProcessGroup(uid(), pid_, signal);
|
2017-05-04 20:32:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (r == 0) process_cgroup_empty_ = true;
|
|
|
|
}
|
2019-10-23 02:18:42 +02:00
|
|
|
|
|
|
|
if (oom_score_adjust_ != DEFAULT_OOM_SCORE_ADJUST) {
|
|
|
|
LmkdUnregister(name_, pid_);
|
|
|
|
}
|
2016-06-15 23:49:57 +02:00
|
|
|
}
|
|
|
|
|
2022-11-15 01:45:47 +01:00
|
|
|
void Service::SetProcessAttributesAndCaps(InterprocessFifo setsid_finished) {
|
init: Add support for ambient capabilities.
Ambient capabilities are inherited in a straightforward way across
execve(2):
"
If you are nonroot but you have a capability, you can add it to pA.
If you do so, your children get that capability in pA, pP, and pE.
For example, you can set pA = CAP_NET_BIND_SERVICE, and your
children can automatically bind low-numbered ports.
"
This will allow us to get rid of the special meaning for AID_NET_ADMIN
and AID_NET_RAW, and if desired, to reduce the use of file capabilities
(which grant capabilities to any process that can execute the file). An
additional benefit of the latter is that a single .rc file can specify
all properties for a service, without having to rely on a separate file
for file capabilities.
Ambient capabilities are supported starting with kernel 4.3 and have
been backported to all Android common kernels back to 3.10.
I chose to not use Minijail here (though I'm still using libcap) for
two reasons:
1-The Minijail code is designed to work in situations where the process
is holding any set of capabilities, so it's more complex. The situation
when forking from init allows for simpler code.
2-The way Minijail is structured right now, we would not be able to
make the required SELinux calls between UID/GID dropping and other priv
dropping code. In the future, it will make sense to add some sort of
"hook" to Minijail so that it can be used in situations where we want
to do other operations between some of the privilege-dropping
operations carried out by Minijail.
Bug: 32438163
Test: Use sample service.
Change-Id: I3226cc95769d1beacbae619cb6c6e6a5425890fb
2016-10-27 16:33:03 +02:00
|
|
|
// Keep capabilites on uid change.
|
2023-04-04 01:29:22 +02:00
|
|
|
if (capabilities_ && uid()) {
|
2017-06-30 23:04:20 +02:00
|
|
|
// If Android is running in a container, some securebits might already
|
|
|
|
// be locked, so don't change those.
|
2017-07-25 23:37:21 +02:00
|
|
|
unsigned long securebits = prctl(PR_GET_SECUREBITS);
|
|
|
|
if (securebits == -1UL) {
|
2017-06-30 23:04:20 +02:00
|
|
|
PLOG(FATAL) << "prctl(PR_GET_SECUREBITS) failed for " << name_;
|
|
|
|
}
|
|
|
|
securebits |= SECBIT_KEEP_CAPS | SECBIT_KEEP_CAPS_LOCKED;
|
|
|
|
if (prctl(PR_SET_SECUREBITS, securebits) != 0) {
|
|
|
|
PLOG(FATAL) << "prctl(PR_SET_SECUREBITS) failed for " << name_;
|
init: Add support for ambient capabilities.
Ambient capabilities are inherited in a straightforward way across
execve(2):
"
If you are nonroot but you have a capability, you can add it to pA.
If you do so, your children get that capability in pA, pP, and pE.
For example, you can set pA = CAP_NET_BIND_SERVICE, and your
children can automatically bind low-numbered ports.
"
This will allow us to get rid of the special meaning for AID_NET_ADMIN
and AID_NET_RAW, and if desired, to reduce the use of file capabilities
(which grant capabilities to any process that can execute the file). An
additional benefit of the latter is that a single .rc file can specify
all properties for a service, without having to rely on a separate file
for file capabilities.
Ambient capabilities are supported starting with kernel 4.3 and have
been backported to all Android common kernels back to 3.10.
I chose to not use Minijail here (though I'm still using libcap) for
two reasons:
1-The Minijail code is designed to work in situations where the process
is holding any set of capabilities, so it's more complex. The situation
when forking from init allows for simpler code.
2-The way Minijail is structured right now, we would not be able to
make the required SELinux calls between UID/GID dropping and other priv
dropping code. In the future, it will make sense to add some sort of
"hook" to Minijail so that it can be used in situations where we want
to do other operations between some of the privilege-dropping
operations carried out by Minijail.
Bug: 32438163
Test: Use sample service.
Change-Id: I3226cc95769d1beacbae619cb6c6e6a5425890fb
2016-10-27 16:33:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-15 01:45:47 +01:00
|
|
|
if (auto result = SetProcessAttributes(proc_attr_, std::move(setsid_finished)); !result.ok()) {
|
2019-05-30 00:58:32 +02:00
|
|
|
LOG(FATAL) << "cannot set attribute for " << name_ << ": " << result.error();
|
2016-07-08 19:32:26 +02:00
|
|
|
}
|
2019-05-30 00:58:32 +02:00
|
|
|
|
2016-07-08 19:32:26 +02:00
|
|
|
if (!seclabel_.empty()) {
|
|
|
|
if (setexeccon(seclabel_.c_str()) < 0) {
|
2016-07-26 03:18:16 +02:00
|
|
|
PLOG(FATAL) << "cannot setexeccon('" << seclabel_ << "') for " << name_;
|
2016-07-08 19:32:26 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-30 00:58:32 +02:00
|
|
|
|
2019-02-06 19:45:56 +01:00
|
|
|
if (capabilities_) {
|
|
|
|
if (!SetCapsForExec(*capabilities_)) {
|
init: Add support for ambient capabilities.
Ambient capabilities are inherited in a straightforward way across
execve(2):
"
If you are nonroot but you have a capability, you can add it to pA.
If you do so, your children get that capability in pA, pP, and pE.
For example, you can set pA = CAP_NET_BIND_SERVICE, and your
children can automatically bind low-numbered ports.
"
This will allow us to get rid of the special meaning for AID_NET_ADMIN
and AID_NET_RAW, and if desired, to reduce the use of file capabilities
(which grant capabilities to any process that can execute the file). An
additional benefit of the latter is that a single .rc file can specify
all properties for a service, without having to rely on a separate file
for file capabilities.
Ambient capabilities are supported starting with kernel 4.3 and have
been backported to all Android common kernels back to 3.10.
I chose to not use Minijail here (though I'm still using libcap) for
two reasons:
1-The Minijail code is designed to work in situations where the process
is holding any set of capabilities, so it's more complex. The situation
when forking from init allows for simpler code.
2-The way Minijail is structured right now, we would not be able to
make the required SELinux calls between UID/GID dropping and other priv
dropping code. In the future, it will make sense to add some sort of
"hook" to Minijail so that it can be used in situations where we want
to do other operations between some of the privilege-dropping
operations carried out by Minijail.
Bug: 32438163
Test: Use sample service.
Change-Id: I3226cc95769d1beacbae619cb6c6e6a5425890fb
2016-10-27 16:33:03 +02:00
|
|
|
LOG(FATAL) << "cannot set capabilities for " << name_;
|
|
|
|
}
|
2023-04-04 01:29:22 +02:00
|
|
|
} else if (uid()) {
|
2017-11-17 00:52:00 +01:00
|
|
|
// Inheritable caps can be non-zero when running in a container.
|
|
|
|
if (!DropInheritableCaps()) {
|
|
|
|
LOG(FATAL) << "cannot drop inheritable caps for " << name_;
|
|
|
|
}
|
init: Add support for ambient capabilities.
Ambient capabilities are inherited in a straightforward way across
execve(2):
"
If you are nonroot but you have a capability, you can add it to pA.
If you do so, your children get that capability in pA, pP, and pE.
For example, you can set pA = CAP_NET_BIND_SERVICE, and your
children can automatically bind low-numbered ports.
"
This will allow us to get rid of the special meaning for AID_NET_ADMIN
and AID_NET_RAW, and if desired, to reduce the use of file capabilities
(which grant capabilities to any process that can execute the file). An
additional benefit of the latter is that a single .rc file can specify
all properties for a service, without having to rely on a separate file
for file capabilities.
Ambient capabilities are supported starting with kernel 4.3 and have
been backported to all Android common kernels back to 3.10.
I chose to not use Minijail here (though I'm still using libcap) for
two reasons:
1-The Minijail code is designed to work in situations where the process
is holding any set of capabilities, so it's more complex. The situation
when forking from init allows for simpler code.
2-The way Minijail is structured right now, we would not be able to
make the required SELinux calls between UID/GID dropping and other priv
dropping code. In the future, it will make sense to add some sort of
"hook" to Minijail so that it can be used in situations where we want
to do other operations between some of the privilege-dropping
operations carried out by Minijail.
Bug: 32438163
Test: Use sample service.
Change-Id: I3226cc95769d1beacbae619cb6c6e6a5425890fb
2016-10-27 16:33:03 +02:00
|
|
|
}
|
2016-07-08 19:32:26 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 19:38:57 +02:00
|
|
|
void Service::Reap(const siginfo_t& siginfo) {
|
2015-07-31 21:45:25 +02:00
|
|
|
if (!(flags_ & SVC_ONESHOT) || (flags_ & SVC_RESTART)) {
|
2023-10-26 22:46:21 +02:00
|
|
|
KillProcessGroup(SIGKILL);
|
2019-11-19 23:19:40 +01:00
|
|
|
} else {
|
|
|
|
// Legacy behavior from ~2007 until Android R: this else branch did not exist and we did not
|
|
|
|
// kill the process group in this case.
|
|
|
|
if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_R__) {
|
|
|
|
// The new behavior in Android R is to kill these process groups in all cases. The
|
|
|
|
// 'true' parameter instructions KillProcessGroup() to report a warning message where it
|
|
|
|
// detects a difference in behavior has occurred.
|
2023-10-26 22:46:21 +02:00
|
|
|
KillProcessGroup(SIGKILL);
|
2019-11-19 23:19:40 +01:00
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2019-07-09 22:33:36 +02:00
|
|
|
// Remove any socket resources we may have created.
|
|
|
|
for (const auto& socket : sockets_) {
|
2021-07-22 06:53:28 +02:00
|
|
|
if (socket.persist) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-07-09 22:33:36 +02:00
|
|
|
auto path = ANDROID_SOCKET_DIR "/" + socket.name;
|
|
|
|
unlink(path.c_str());
|
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
|
2018-04-13 19:38:57 +02:00
|
|
|
for (const auto& f : reap_callbacks_) {
|
|
|
|
f(siginfo);
|
|
|
|
}
|
|
|
|
|
2021-07-22 21:56:39 +02:00
|
|
|
if ((siginfo.si_code != CLD_EXITED || siginfo.si_status != 0) && on_failure_reboot_target_) {
|
2022-12-03 04:04:22 +01:00
|
|
|
LOG(ERROR) << "Service " << name_
|
|
|
|
<< " has 'reboot_on_failure' option and failed, shutting down system.";
|
2019-11-13 01:21:20 +01:00
|
|
|
trigger_shutdown(*on_failure_reboot_target_);
|
2019-09-10 19:40:47 +02:00
|
|
|
}
|
|
|
|
|
2017-07-28 23:48:41 +02:00
|
|
|
if (flags_ & SVC_EXEC) UnSetExec();
|
|
|
|
|
2022-04-28 22:26:59 +02:00
|
|
|
if (name_ == "zygote" || name_ == "zygote64") {
|
|
|
|
removeAllEmptyProcessGroups();
|
|
|
|
}
|
|
|
|
|
2017-07-28 23:48:41 +02:00
|
|
|
if (flags_ & SVC_TEMPORARY) return;
|
2015-07-31 21:45:25 +02:00
|
|
|
|
|
|
|
pid_ = 0;
|
2021-07-22 21:56:39 +02:00
|
|
|
flags_ &= (~SVC_RUNNING);
|
2017-07-27 01:09:09 +02:00
|
|
|
start_order_ = 0;
|
init: non-crashing service can restart immediately
This CL allows restart_period to be set to a value shorter than 5s.
Previously this was prohibited to rate limit crashing services. That
behavior is considered to be a bit too conservative because some
services don't crash, but exit deliverately.
adbd is the motivating example. When adb root or adb unroot is
requested, it changes its mode of operation (via sysprop), exits itself,
and restarts (by init) to enter into the mode. However, due to the 5s
delay, the mode change can complete no earlier than 5 seconds after adbd
was started last time. This can slow the mode change when it is
requested right after the boot.
With this CL, restart_period can be set to a value smaller than 5. And
services like adbd can make use of it. However, in ordef to rate limit
crashing service, the default is enforced if the service was crashed
last time. In addition, such intended restart is not counted as crashes
when monitoring successive crashes during booting.
Bug: 286061817
Test: /packages/modules/Virtualization/vm/vm_shell.sh start-microdroid \
--auto-connect -- --protected
* with this change: within 2s
* without this change: over 6s
Change-Id: I1b3f0c92d349e8c8760821cf50fb69997b67b242
2023-06-09 02:52:49 +02:00
|
|
|
was_last_exit_ok_ = siginfo.si_code == CLD_EXITED && siginfo.si_status == 0;
|
2015-07-31 21:45:25 +02:00
|
|
|
|
|
|
|
// Oneshot processes go into the disabled state on exit,
|
|
|
|
// except when manually restarted.
|
Support for stopping/starting post-data-mount class subsets.
On devices that use FDE and APEX at the same time, we need to bring up a
minimal framework to be able to mount the /data partition. During this
period, a tmpfs /data filesystem is created, which doesn't contain any
of the updated APEXEs. As a consequence, all those processes will be
using the APEXes from the /system partition.
This is obviously not desired, as APEXes in /system may be old and/or
contain security issues. Additionally, it would create a difference
between FBE and FDE devices at runtime.
Ideally, we restart all processes that have started after we created the
tmpfs /data. We can't (re)start based on class names alone, because some
classes (eg 'hal') contain services that are required to start apexd
itself and that shouldn't be killed (eg the graphics HAL).
To address this, keep track of which processes are started after /data
is mounted, with a new 'mark_post_data' keyword. Additionally, create
'class_reset_post_data', which resets all services in the class that
were created after the initial /data mount, and 'class_start_post_data',
which starts all services in the class that were started after /data was
mounted.
On a device with FBE, these keywords wouldn't be used; on a device with
FDE, we'd use them to bring down the right processes after the user has
entered the correct secret, and restart them.
Bug: 118485723
Test: manually verified process list
Change-Id: I16adb776dacf1dd1feeaff9e60639b99899905eb
2019-04-23 16:26:01 +02:00
|
|
|
if ((flags_ & SVC_ONESHOT) && !(flags_ & SVC_RESTART) && !(flags_ & SVC_RESET)) {
|
2015-07-31 21:45:25 +02:00
|
|
|
flags_ |= SVC_DISABLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disabled and reset processes do not get restarted automatically.
|
|
|
|
if (flags_ & (SVC_DISABLED | SVC_RESET)) {
|
|
|
|
NotifyStateChange("stopped");
|
2017-03-28 01:27:30 +02:00
|
|
|
return;
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2020-02-20 19:50:00 +01:00
|
|
|
#if INIT_FULL_SOURCES
|
2023-06-23 07:16:31 +02:00
|
|
|
static bool is_apex_updatable = true;
|
2019-05-10 14:12:15 +02:00
|
|
|
#else
|
|
|
|
static bool is_apex_updatable = false;
|
|
|
|
#endif
|
2022-07-14 04:23:59 +02:00
|
|
|
const bool use_default_mount_ns =
|
|
|
|
mount_namespace_.has_value() && *mount_namespace_ == NS_DEFAULT;
|
|
|
|
const bool is_process_updatable = use_default_mount_ns && is_apex_updatable;
|
2019-05-10 14:12:15 +02:00
|
|
|
|
2022-09-13 03:19:47 +02:00
|
|
|
#if defined(__BIONIC__) && defined(SEGV_MTEAERR)
|
2022-05-11 02:55:08 +02:00
|
|
|
// As a precaution, we only upgrade a service once per reboot, to limit
|
|
|
|
// the potential impact.
|
2022-09-13 03:19:47 +02:00
|
|
|
//
|
|
|
|
// BIONIC_SIGNAL_ART_PROFILER is a magic value used by deuggerd to signal
|
|
|
|
// that the process crashed with SIGSEGV and SEGV_MTEAERR. This signal will
|
|
|
|
// never be seen otherwise in a crash, because it always gets handled by the
|
|
|
|
// profiling signal handlers in bionic. See also
|
|
|
|
// debuggerd/handler/debuggerd_handler.cpp.
|
|
|
|
bool should_upgrade_mte = siginfo.si_code != CLD_EXITED &&
|
|
|
|
siginfo.si_status == BIONIC_SIGNAL_ART_PROFILER && !upgraded_mte_;
|
2022-05-11 02:55:08 +02:00
|
|
|
|
|
|
|
if (should_upgrade_mte) {
|
2022-09-16 03:10:54 +02:00
|
|
|
constexpr int kDefaultUpgradeSecs = 60;
|
|
|
|
int secs = GetIntProperty("persist.device_config.memory_safety_native.upgrade_secs.default",
|
|
|
|
kDefaultUpgradeSecs);
|
|
|
|
secs = GetIntProperty(
|
|
|
|
"persist.device_config.memory_safety_native.upgrade_secs.service." + name_, secs);
|
|
|
|
if (secs > 0) {
|
|
|
|
LOG(INFO) << "Upgrading service " << name_ << " to sync MTE for " << secs << " seconds";
|
|
|
|
once_environment_vars_.emplace_back("BIONIC_MEMTAG_UPGRADE_SECS", std::to_string(secs));
|
|
|
|
upgraded_mte_ = true;
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Not upgrading service " << name_ << " to sync MTE due to device config";
|
|
|
|
}
|
2022-05-11 02:55:08 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-12-26 15:22:28 +01:00
|
|
|
// If we crash > 4 times in 'fatal_crash_window_' minutes or before boot_completed,
|
2019-04-27 22:10:35 +02:00
|
|
|
// reboot into bootloader or set crashing property
|
2016-11-11 02:43:47 +01:00
|
|
|
boot_clock::time_point now = boot_clock::now();
|
init: non-crashing service can restart immediately
This CL allows restart_period to be set to a value shorter than 5s.
Previously this was prohibited to rate limit crashing services. That
behavior is considered to be a bit too conservative because some
services don't crash, but exit deliverately.
adbd is the motivating example. When adb root or adb unroot is
requested, it changes its mode of operation (via sysprop), exits itself,
and restarts (by init) to enter into the mode. However, due to the 5s
delay, the mode change can complete no earlier than 5 seconds after adbd
was started last time. This can slow the mode change when it is
requested right after the boot.
With this CL, restart_period can be set to a value smaller than 5. And
services like adbd can make use of it. However, in ordef to rate limit
crashing service, the default is enforced if the service was crashed
last time. In addition, such intended restart is not counted as crashes
when monitoring successive crashes during booting.
Bug: 286061817
Test: /packages/modules/Virtualization/vm/vm_shell.sh start-microdroid \
--auto-connect -- --protected
* with this change: within 2s
* without this change: over 6s
Change-Id: I1b3f0c92d349e8c8760821cf50fb69997b67b242
2023-06-09 02:52:49 +02:00
|
|
|
if (((flags_ & SVC_CRITICAL) || is_process_updatable) && !(flags_ & SVC_RESTART) &&
|
|
|
|
!was_last_exit_ok_) {
|
2021-01-20 05:28:33 +01:00
|
|
|
bool boot_completed = GetBoolProperty("sys.boot_completed", false);
|
2019-12-26 15:22:28 +01:00
|
|
|
if (now < time_crashed_ + fatal_crash_window_ || !boot_completed) {
|
2016-11-11 02:43:47 +01:00
|
|
|
if (++crash_count_ > 4) {
|
2019-12-26 15:22:28 +01:00
|
|
|
auto exit_reason = boot_completed ?
|
|
|
|
"in " + std::to_string(fatal_crash_window_.count()) + " minutes" :
|
|
|
|
"before boot completed";
|
2019-01-07 11:19:02 +01:00
|
|
|
if (flags_ & SVC_CRITICAL) {
|
2021-01-20 05:28:33 +01:00
|
|
|
if (!GetBoolProperty("init.svc_debug.no_fatal." + name_, false)) {
|
|
|
|
// Aborts into `fatal_reboot_target_'.
|
|
|
|
SetFatalRebootTarget(fatal_reboot_target_);
|
|
|
|
LOG(FATAL) << "critical process '" << name_ << "' exited 4 times "
|
|
|
|
<< exit_reason;
|
|
|
|
}
|
2019-01-07 11:19:02 +01:00
|
|
|
} else {
|
2020-11-06 18:00:58 +01:00
|
|
|
LOG(ERROR) << "process with updatable components '" << name_
|
|
|
|
<< "' exited 4 times " << exit_reason;
|
2019-01-07 11:19:02 +01:00
|
|
|
// Notifies update_verifier and apexd
|
2019-12-11 00:00:24 +01:00
|
|
|
SetProperty("sys.init.updatable_crashing_process_name", name_);
|
2019-12-20 17:34:48 +01:00
|
|
|
SetProperty("sys.init.updatable_crashing", "1");
|
2019-01-07 11:19:02 +01:00
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
time_crashed_ = now;
|
2016-11-11 02:43:47 +01:00
|
|
|
crash_count_ = 1;
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
flags_ &= (~SVC_RESTART);
|
|
|
|
flags_ |= SVC_RESTARTING;
|
|
|
|
|
|
|
|
// Execute all onrestart commands for this service.
|
|
|
|
onrestart_.ExecuteAllCommands();
|
|
|
|
|
|
|
|
NotifyStateChange("restarting");
|
2017-03-28 01:27:30 +02:00
|
|
|
return;
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Service::DumpState() const {
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(INFO) << "service " << name_;
|
2017-06-22 21:53:17 +02:00
|
|
|
LOG(INFO) << " class '" << Join(classnames_, " ") << "'";
|
|
|
|
LOG(INFO) << " exec " << Join(args_, " ");
|
2019-07-09 22:33:36 +02:00
|
|
|
for (const auto& socket : sockets_) {
|
|
|
|
LOG(INFO) << " socket " << socket.name;
|
|
|
|
}
|
|
|
|
for (const auto& file : files_) {
|
|
|
|
LOG(INFO) << " file " << file.name;
|
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
Result<void> Service::ExecStart() {
|
2019-09-10 19:40:47 +02:00
|
|
|
auto reboot_on_failure = make_scope_guard([this] {
|
|
|
|
if (on_failure_reboot_target_) {
|
2019-11-13 01:21:20 +01:00
|
|
|
trigger_shutdown(*on_failure_reboot_target_);
|
2019-09-10 19:40:47 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-08-21 10:34:37 +02:00
|
|
|
if (is_updatable() && !IsDefaultMountNamespaceReady()) {
|
2018-11-12 04:08:41 +01:00
|
|
|
// Don't delay the service for ExecStart() as the semantic is that
|
|
|
|
// the caller might depend on the side effect of the execution.
|
|
|
|
return Error() << "Cannot start an updatable service '" << name_
|
|
|
|
<< "' before configs from APEXes are all loaded";
|
|
|
|
}
|
|
|
|
|
2017-07-28 23:48:41 +02:00
|
|
|
flags_ |= SVC_ONESHOT;
|
2017-03-28 01:27:30 +02:00
|
|
|
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result = Start(); !result.ok()) {
|
2017-08-23 01:13:59 +02:00
|
|
|
return result;
|
2017-03-28 01:27:30 +02:00
|
|
|
}
|
2017-07-28 23:48:41 +02:00
|
|
|
|
|
|
|
flags_ |= SVC_EXEC;
|
|
|
|
is_exec_service_running_ = true;
|
|
|
|
|
2023-04-04 01:29:22 +02:00
|
|
|
LOG(INFO) << "SVC_EXEC service '" << name_ << "' pid " << pid_ << " (uid " << uid() << " gid "
|
|
|
|
<< proc_attr_.gid << "+" << proc_attr_.supp_gids.size() << " context "
|
2018-06-20 23:54:52 +02:00
|
|
|
<< (!seclabel_.empty() ? seclabel_ : "default") << ") started; waiting...";
|
2017-07-28 23:48:41 +02:00
|
|
|
|
2019-09-10 19:40:47 +02:00
|
|
|
reboot_on_failure.Disable();
|
2019-06-10 20:08:01 +02:00
|
|
|
return {};
|
2017-03-28 01:27:30 +02:00
|
|
|
}
|
|
|
|
|
2022-02-26 00:28:59 +01:00
|
|
|
Result<void> Service::CheckConsole() {
|
|
|
|
if (!(flags_ & SVC_CONSOLE)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
Use ro.boot.serialconsole to disable console services
For many years, services declaring "console" would only be started if the
console device specified by androidboot.console= was present under /dev.
However, they would also be started if the /dev/console node existed.
This fallback causes problems with newer GKI kernel images which now
hard-code "console=ttynull" via CONFIG_CMDLINE, which essentially means
/dev/console always exists, even though this console points nowhere.
It also causes problems on devices where the androidboot.console was not
the same as the kernel dmesg console ("console="), such as cuttlefish,
because those platforms could not simultaneously enable kernel logging
but disable the interactive serial console feature. The framework just
assumed both would be muxed on the same serial port. Cuttlefish had a
workaround, to use "androidboot.console=invalid" to avoid the fallback,
but this doesn't work on devices which still want to mux the kernel logs
and interactive serial console.
This change resolves the issue in a better way, by introducing a new
boolean property called "androidboot.serialconsole". Setting this to "0"
will disable the console services, regardless of whether the
/dev/console or /dev/${ro.boot.console} devices exist. Older kernels
and bootloaders don't need to set this and can rely on the old behavior
in init, but bootloaders booting newer kernels must set it to avoid the
"performance is impacted" message due to console services being started.
Bug: 266982931
Bug: 223797063
Bug: 267428635
Test: "launch_cvd" with "androidboot.console=invalid" removed;
See the "performance is impacted" message.
Test: "launch_cvd" with "androidboot.serialconsole=0";
The "performance is impacted" message is gone.
Change-Id: Iaad4d27ffe4df74ed49606d3cabe83483c350df4
2023-02-22 23:31:23 +01:00
|
|
|
// On newer kernels, /dev/console will always exist because
|
|
|
|
// "console=ttynull" is hard-coded in CONFIG_CMDLINE. This new boot
|
|
|
|
// property should be set via "androidboot.serialconsole=0" to explicitly
|
|
|
|
// disable services requiring the console. For older kernels and boot
|
|
|
|
// images, not setting this at all will fall back to the old behavior
|
|
|
|
if (GetProperty("ro.boot.serialconsole", "") == "0") {
|
2023-03-14 00:09:16 +01:00
|
|
|
flags_ |= SVC_DISABLED;
|
Use ro.boot.serialconsole to disable console services
For many years, services declaring "console" would only be started if the
console device specified by androidboot.console= was present under /dev.
However, they would also be started if the /dev/console node existed.
This fallback causes problems with newer GKI kernel images which now
hard-code "console=ttynull" via CONFIG_CMDLINE, which essentially means
/dev/console always exists, even though this console points nowhere.
It also causes problems on devices where the androidboot.console was not
the same as the kernel dmesg console ("console="), such as cuttlefish,
because those platforms could not simultaneously enable kernel logging
but disable the interactive serial console feature. The framework just
assumed both would be muxed on the same serial port. Cuttlefish had a
workaround, to use "androidboot.console=invalid" to avoid the fallback,
but this doesn't work on devices which still want to mux the kernel logs
and interactive serial console.
This change resolves the issue in a better way, by introducing a new
boolean property called "androidboot.serialconsole". Setting this to "0"
will disable the console services, regardless of whether the
/dev/console or /dev/${ro.boot.console} devices exist. Older kernels
and bootloaders don't need to set this and can rely on the old behavior
in init, but bootloaders booting newer kernels must set it to avoid the
"performance is impacted" message due to console services being started.
Bug: 266982931
Bug: 223797063
Bug: 267428635
Test: "launch_cvd" with "androidboot.console=invalid" removed;
See the "performance is impacted" message.
Test: "launch_cvd" with "androidboot.serialconsole=0";
The "performance is impacted" message is gone.
Change-Id: Iaad4d27ffe4df74ed49606d3cabe83483c350df4
2023-02-22 23:31:23 +01:00
|
|
|
return {};
|
|
|
|
}
|
2022-02-26 00:28:59 +01:00
|
|
|
|
|
|
|
if (proc_attr_.console.empty()) {
|
|
|
|
proc_attr_.console = "/dev/" + GetProperty("ro.boot.console", "console");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that open call succeeds to ensure a console driver is
|
|
|
|
// properly registered for the device node
|
|
|
|
int console_fd = open(proc_attr_.console.c_str(), O_RDWR | O_CLOEXEC);
|
|
|
|
if (console_fd < 0) {
|
|
|
|
flags_ |= SVC_DISABLED;
|
|
|
|
return ErrnoError() << "Couldn't open console '" << proc_attr_.console << "'";
|
|
|
|
}
|
|
|
|
close(console_fd);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-02-25 23:44:40 +01:00
|
|
|
// Configures the memory cgroup properties for the service.
|
|
|
|
void Service::ConfigureMemcg() {
|
|
|
|
if (swappiness_ != -1) {
|
2023-04-04 01:29:22 +02:00
|
|
|
if (!setProcessGroupSwappiness(uid(), pid_, swappiness_)) {
|
2022-02-25 23:44:40 +01:00
|
|
|
PLOG(ERROR) << "setProcessGroupSwappiness failed";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (soft_limit_in_bytes_ != -1) {
|
2023-04-04 01:29:22 +02:00
|
|
|
if (!setProcessGroupSoftLimit(uid(), pid_, soft_limit_in_bytes_)) {
|
2022-02-25 23:44:40 +01:00
|
|
|
PLOG(ERROR) << "setProcessGroupSoftLimit failed";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t computed_limit_in_bytes = limit_in_bytes_;
|
|
|
|
if (limit_percent_ != -1) {
|
|
|
|
long page_size = sysconf(_SC_PAGESIZE);
|
|
|
|
long num_pages = sysconf(_SC_PHYS_PAGES);
|
|
|
|
if (page_size > 0 && num_pages > 0) {
|
|
|
|
size_t max_mem = SIZE_MAX;
|
|
|
|
if (size_t(num_pages) < SIZE_MAX / size_t(page_size)) {
|
|
|
|
max_mem = size_t(num_pages) * size_t(page_size);
|
|
|
|
}
|
|
|
|
computed_limit_in_bytes =
|
|
|
|
std::min(computed_limit_in_bytes, max_mem / 100 * limit_percent_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!limit_property_.empty()) {
|
|
|
|
// This ends up overwriting computed_limit_in_bytes but only if the
|
|
|
|
// property is defined.
|
|
|
|
computed_limit_in_bytes =
|
|
|
|
android::base::GetUintProperty(limit_property_, computed_limit_in_bytes, SIZE_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (computed_limit_in_bytes != size_t(-1)) {
|
2023-04-04 01:29:22 +02:00
|
|
|
if (!setProcessGroupLimit(uid(), pid_, computed_limit_in_bytes)) {
|
2022-02-25 23:44:40 +01:00
|
|
|
PLOG(ERROR) << "setProcessGroupLimit failed";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 23:52:52 +01:00
|
|
|
// Enters namespaces, sets environment variables, writes PID files and runs the service executable.
|
2022-11-15 01:45:42 +01:00
|
|
|
void Service::RunService(const std::vector<Descriptor>& descriptors,
|
2022-11-15 01:45:47 +01:00
|
|
|
InterprocessFifo cgroups_activated, InterprocessFifo setsid_finished) {
|
2022-07-14 04:23:59 +02:00
|
|
|
if (auto result = EnterNamespaces(namespaces_, name_, mount_namespace_); !result.ok()) {
|
2022-02-25 23:52:52 +01:00
|
|
|
LOG(FATAL) << "Service '" << name_ << "' failed to set up namespaces: " << result.error();
|
|
|
|
}
|
|
|
|
|
2022-05-11 02:55:08 +02:00
|
|
|
for (const auto& [key, value] : once_environment_vars_) {
|
|
|
|
setenv(key.c_str(), value.c_str(), 1);
|
|
|
|
}
|
2022-02-25 23:52:52 +01:00
|
|
|
for (const auto& [key, value] : environment_vars_) {
|
|
|
|
setenv(key.c_str(), value.c_str(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& descriptor : descriptors) {
|
|
|
|
descriptor.Publish();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto result = WritePidToFiles(&writepid_files_); !result.ok()) {
|
|
|
|
LOG(ERROR) << "failed to write pid to files: " << result.error();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until the cgroups have been created and until the cgroup controllers have been
|
|
|
|
// activated.
|
2022-11-15 01:45:42 +01:00
|
|
|
Result<uint8_t> byte = cgroups_activated.Read();
|
2022-10-20 01:30:15 +02:00
|
|
|
if (!byte.ok()) {
|
|
|
|
LOG(ERROR) << name_ << ": failed to read from notification channel: " << byte.error();
|
2022-02-25 23:52:52 +01:00
|
|
|
}
|
2022-11-15 01:45:42 +01:00
|
|
|
cgroups_activated.Close();
|
2022-11-18 18:12:52 +01:00
|
|
|
if (*byte != kCgroupsActivated) {
|
2022-04-01 06:18:20 +02:00
|
|
|
LOG(FATAL) << "Service '" << name_ << "' failed to start due to a fatal error";
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
}
|
2022-02-25 23:52:52 +01:00
|
|
|
|
2022-10-12 21:25:46 +02:00
|
|
|
if (task_profiles_.size() > 0) {
|
|
|
|
bool succeeded = SelinuxGetVendorAndroidVersion() < __ANDROID_API_U__
|
|
|
|
?
|
|
|
|
// Compatibility mode: apply the task profiles to the current
|
|
|
|
// thread.
|
|
|
|
SetTaskProfiles(getpid(), task_profiles_)
|
|
|
|
:
|
|
|
|
// Apply the task profiles to the current process.
|
|
|
|
SetProcessProfiles(getuid(), getpid(), task_profiles_);
|
|
|
|
if (!succeeded) {
|
|
|
|
LOG(ERROR) << "failed to set task profiles";
|
|
|
|
}
|
2022-02-25 23:52:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// As requested, set our gid, supplemental gids, uid, context, and
|
|
|
|
// priority. Aborts on failure.
|
2022-11-15 01:45:47 +01:00
|
|
|
SetProcessAttributesAndCaps(std::move(setsid_finished));
|
2022-02-25 23:52:52 +01:00
|
|
|
|
|
|
|
if (!ExpandArgsAndExecv(args_, sigstop_)) {
|
|
|
|
PLOG(ERROR) << "cannot execv('" << args_[0]
|
|
|
|
<< "'). See the 'Debugging init' section of init's README.md for tips";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
Result<void> Service::Start() {
|
2019-09-10 19:40:47 +02:00
|
|
|
auto reboot_on_failure = make_scope_guard([this] {
|
|
|
|
if (on_failure_reboot_target_) {
|
2019-11-13 01:21:20 +01:00
|
|
|
trigger_shutdown(*on_failure_reboot_target_);
|
2019-09-10 19:40:47 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-08-21 10:34:37 +02:00
|
|
|
if (is_updatable() && !IsDefaultMountNamespaceReady()) {
|
2018-11-12 04:08:41 +01:00
|
|
|
ServiceList::GetInstance().DelayService(*this);
|
|
|
|
return Error() << "Cannot start an updatable service '" << name_
|
|
|
|
<< "' before configs from APEXes are all loaded. "
|
|
|
|
<< "Queued for execution.";
|
|
|
|
}
|
|
|
|
|
2017-10-26 19:43:10 +02:00
|
|
|
bool disabled = (flags_ & (SVC_DISABLED | SVC_RESET));
|
2021-07-22 06:53:28 +02:00
|
|
|
ResetFlagsForStart();
|
2015-07-31 21:45:25 +02:00
|
|
|
|
|
|
|
// Running processes require no additional work --- if they're in the
|
|
|
|
// process of exiting, we've ensured that they will immediately restart
|
2017-10-26 19:43:10 +02:00
|
|
|
// on exit, unless they are ONESHOT. For ONESHOT service, if it's in
|
|
|
|
// stopping status, we just set SVC_RESTART flag so it will get restarted
|
|
|
|
// in Reap().
|
2015-07-31 21:45:25 +02:00
|
|
|
if (flags_ & SVC_RUNNING) {
|
2017-10-26 19:43:10 +02:00
|
|
|
if ((flags_ & SVC_ONESHOT) && disabled) {
|
|
|
|
flags_ |= SVC_RESTART;
|
|
|
|
}
|
2022-05-18 00:53:11 +02:00
|
|
|
|
|
|
|
LOG(INFO) << "service '" << name_
|
|
|
|
<< "' requested start, but it is already running (flags: " << flags_ << ")";
|
|
|
|
|
2017-08-23 01:13:59 +02:00
|
|
|
// It is not an error to try to start a service that is already running.
|
2019-09-10 19:40:47 +02:00
|
|
|
reboot_on_failure.Disable();
|
2019-06-10 20:08:01 +02:00
|
|
|
return {};
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2022-11-15 01:45:47 +01:00
|
|
|
// cgroups_activated is used for communication from the parent to the child
|
|
|
|
// while setsid_finished is used for communication from the child process to
|
|
|
|
// the parent process. These two communication channels are separate because
|
|
|
|
// combining these into a single communication channel would introduce a
|
|
|
|
// race between the Write() calls by the parent and by the child.
|
|
|
|
InterprocessFifo cgroups_activated, setsid_finished;
|
|
|
|
OR_RETURN(cgroups_activated.Initialize());
|
|
|
|
OR_RETURN(setsid_finished.Initialize());
|
2022-01-26 21:52:52 +01:00
|
|
|
|
2022-02-26 00:28:59 +01:00
|
|
|
if (Result<void> result = CheckConsole(); !result.ok()) {
|
|
|
|
return result;
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct stat sb;
|
|
|
|
if (stat(args_[0].c_str(), &sb) == -1) {
|
|
|
|
flags_ |= SVC_DISABLED;
|
2017-08-23 01:13:59 +02:00
|
|
|
return ErrnoError() << "Cannot find '" << args_[0] << "'";
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string scon;
|
|
|
|
if (!seclabel_.empty()) {
|
|
|
|
scon = seclabel_;
|
|
|
|
} else {
|
2018-04-21 01:18:12 +02:00
|
|
|
auto result = ComputeContextFromExecutable(args_[0]);
|
2020-02-05 19:49:33 +01:00
|
|
|
if (!result.ok()) {
|
2017-08-23 01:13:59 +02:00
|
|
|
return result.error();
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
2017-08-23 01:13:59 +02:00
|
|
|
scon = *result;
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2022-07-14 04:23:59 +02:00
|
|
|
if (!mount_namespace_.has_value()) {
|
|
|
|
// remember from which mount namespace the service should start
|
|
|
|
SetMountNamespace();
|
2020-06-09 06:44:17 +02:00
|
|
|
}
|
|
|
|
|
Support for stopping/starting post-data-mount class subsets.
On devices that use FDE and APEX at the same time, we need to bring up a
minimal framework to be able to mount the /data partition. During this
period, a tmpfs /data filesystem is created, which doesn't contain any
of the updated APEXEs. As a consequence, all those processes will be
using the APEXes from the /system partition.
This is obviously not desired, as APEXes in /system may be old and/or
contain security issues. Additionally, it would create a difference
between FBE and FDE devices at runtime.
Ideally, we restart all processes that have started after we created the
tmpfs /data. We can't (re)start based on class names alone, because some
classes (eg 'hal') contain services that are required to start apexd
itself and that shouldn't be killed (eg the graphics HAL).
To address this, keep track of which processes are started after /data
is mounted, with a new 'mark_post_data' keyword. Additionally, create
'class_reset_post_data', which resets all services in the class that
were created after the initial /data mount, and 'class_start_post_data',
which starts all services in the class that were started after /data was
mounted.
On a device with FBE, these keywords wouldn't be used; on a device with
FDE, we'd use them to bring down the right processes after the user has
entered the correct secret, and restart them.
Bug: 118485723
Test: manually verified process list
Change-Id: I16adb776dacf1dd1feeaff9e60639b99899905eb
2019-04-23 16:26:01 +02:00
|
|
|
post_data_ = ServiceList::GetInstance().IsPostData();
|
|
|
|
|
2016-10-04 23:05:39 +02:00
|
|
|
LOG(INFO) << "starting service '" << name_ << "'...";
|
2015-07-31 21:45:25 +02:00
|
|
|
|
2019-09-10 23:20:35 +02:00
|
|
|
std::vector<Descriptor> descriptors;
|
|
|
|
for (const auto& socket : sockets_) {
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result = socket.Create(scon); result.ok()) {
|
2019-09-10 23:20:35 +02:00
|
|
|
descriptors.emplace_back(std::move(*result));
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Could not create socket '" << socket.name << "': " << result.error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& file : files_) {
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result = file.Create(); result.ok()) {
|
2019-09-10 23:20:35 +02:00
|
|
|
descriptors.emplace_back(std::move(*result));
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Could not open file '" << file.name << "': " << result.error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-22 00:35:09 +02:00
|
|
|
pid_t pid = -1;
|
2019-05-30 00:58:32 +02:00
|
|
|
if (namespaces_.flags) {
|
|
|
|
pid = clone(nullptr, nullptr, namespaces_.flags | SIGCHLD, nullptr);
|
2016-04-22 00:35:09 +02:00
|
|
|
} else {
|
|
|
|
pid = fork();
|
|
|
|
}
|
|
|
|
|
2015-07-31 21:45:25 +02:00
|
|
|
if (pid == 0) {
|
|
|
|
umask(077);
|
2022-11-15 01:45:42 +01:00
|
|
|
cgroups_activated.CloseWriteFd();
|
2022-11-15 01:45:47 +01:00
|
|
|
setsid_finished.CloseReadFd();
|
|
|
|
RunService(descriptors, std::move(cgroups_activated), std::move(setsid_finished));
|
2015-07-31 21:45:25 +02:00
|
|
|
_exit(127);
|
2022-11-15 01:24:39 +01:00
|
|
|
} else {
|
2022-11-15 01:45:42 +01:00
|
|
|
cgroups_activated.CloseReadFd();
|
2022-11-15 01:45:47 +01:00
|
|
|
setsid_finished.CloseWriteFd();
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pid < 0) {
|
|
|
|
pid_ = 0;
|
2017-08-23 01:13:59 +02:00
|
|
|
return ErrnoError() << "Failed to fork";
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 02:55:08 +02:00
|
|
|
once_environment_vars_.clear();
|
|
|
|
|
2019-10-23 02:18:42 +02:00
|
|
|
if (oom_score_adjust_ != DEFAULT_OOM_SCORE_ADJUST) {
|
2017-06-23 01:50:31 +02:00
|
|
|
std::string oom_str = std::to_string(oom_score_adjust_);
|
2016-07-22 21:07:06 +02:00
|
|
|
std::string oom_file = StringPrintf("/proc/%d/oom_score_adj", pid);
|
|
|
|
if (!WriteStringToFile(oom_str, oom_file)) {
|
2019-03-08 21:34:53 +01:00
|
|
|
PLOG(ERROR) << "couldn't write oom_score_adj";
|
2016-07-22 21:07:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 02:43:47 +01:00
|
|
|
time_started_ = boot_clock::now();
|
2015-07-31 21:45:25 +02:00
|
|
|
pid_ = pid;
|
|
|
|
flags_ |= SVC_RUNNING;
|
2017-07-27 01:09:09 +02:00
|
|
|
start_order_ = next_start_order_++;
|
2017-05-04 20:32:36 +02:00
|
|
|
process_cgroup_empty_ = false;
|
2016-06-15 23:49:57 +02:00
|
|
|
|
2022-10-20 15:14:39 +02:00
|
|
|
if (CgroupsAvailable()) {
|
|
|
|
bool use_memcg = swappiness_ != -1 || soft_limit_in_bytes_ != -1 || limit_in_bytes_ != -1 ||
|
|
|
|
limit_percent_ != -1 || !limit_property_.empty();
|
2023-04-04 01:29:22 +02:00
|
|
|
errno = -createProcessGroup(uid(), pid_, use_memcg);
|
2022-10-20 15:14:39 +02:00
|
|
|
if (errno != 0) {
|
2022-11-18 18:12:52 +01:00
|
|
|
Result<void> result = cgroups_activated.Write(kActivatingCgroupsFailed);
|
2022-10-20 01:30:15 +02:00
|
|
|
if (!result.ok()) {
|
|
|
|
return Error() << "Sending notification failed: " << result.error();
|
2022-10-20 15:14:39 +02:00
|
|
|
}
|
2023-04-04 01:29:22 +02:00
|
|
|
return Error() << "createProcessGroup(" << uid() << ", " << pid_ << ", " << use_memcg
|
|
|
|
<< ") failed for service '" << name_ << "': " << strerror(errno);
|
2022-04-01 06:18:20 +02:00
|
|
|
}
|
|
|
|
|
2022-10-20 15:14:39 +02:00
|
|
|
// When the blkio controller is mounted in the v1 hierarchy, NormalIoPriority is
|
|
|
|
// the default (/dev/blkio). When the blkio controller is mounted in the v2 hierarchy, the
|
|
|
|
// NormalIoPriority profile has to be applied explicitly.
|
2023-04-04 01:29:22 +02:00
|
|
|
SetProcessProfiles(uid(), pid_, {"NormalIoPriority"});
|
2022-08-02 01:09:13 +02:00
|
|
|
|
2022-10-20 15:14:39 +02:00
|
|
|
if (use_memcg) {
|
|
|
|
ConfigureMemcg();
|
|
|
|
}
|
2016-06-15 23:49:57 +02:00
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
|
2019-10-23 02:18:42 +02:00
|
|
|
if (oom_score_adjust_ != DEFAULT_OOM_SCORE_ADJUST) {
|
2023-04-04 01:29:22 +02:00
|
|
|
LmkdRegister(name_, uid(), pid_, oom_score_adjust_);
|
2019-10-23 02:18:42 +02:00
|
|
|
}
|
|
|
|
|
2022-11-18 18:12:52 +01:00
|
|
|
if (Result<void> result = cgroups_activated.Write(kCgroupsActivated); !result.ok()) {
|
2022-10-20 01:30:15 +02:00
|
|
|
return Error() << "Sending cgroups activated notification failed: " << result.error();
|
2022-01-26 21:52:52 +01:00
|
|
|
}
|
|
|
|
|
2022-11-15 01:45:47 +01:00
|
|
|
cgroups_activated.Close();
|
|
|
|
|
|
|
|
// Call setpgid() from the parent process to make sure that this call has
|
|
|
|
// finished before the parent process calls kill(-pgid, ...).
|
|
|
|
if (!RequiresConsole(proc_attr_)) {
|
|
|
|
if (setpgid(pid, pid) < 0) {
|
|
|
|
switch (errno) {
|
|
|
|
case EACCES: // Child has already performed setpgid() followed by execve().
|
|
|
|
case ESRCH: // Child process no longer exists.
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PLOG(ERROR) << "setpgid() from parent failed";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The Read() call below will return an error if the child is killed.
|
|
|
|
if (Result<uint8_t> result = setsid_finished.Read();
|
|
|
|
!result.ok() || *result != kSetSidFinished) {
|
|
|
|
if (!result.ok()) {
|
|
|
|
return Error() << "Waiting for setsid() failed: " << result.error();
|
|
|
|
} else {
|
|
|
|
return Error() << "Waiting for setsid() failed: " << static_cast<uint32_t>(*result)
|
|
|
|
<< " <> " << static_cast<uint32_t>(kSetSidFinished);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setsid_finished.Close();
|
|
|
|
|
2015-07-31 21:45:25 +02:00
|
|
|
NotifyStateChange("running");
|
2019-09-10 19:40:47 +02:00
|
|
|
reboot_on_failure.Disable();
|
2023-02-28 02:42:08 +01:00
|
|
|
|
|
|
|
LOG(INFO) << "... started service '" << name_ << "' has pid " << pid_;
|
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
return {};
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2022-07-14 04:23:59 +02:00
|
|
|
// Set mount namespace for the service.
|
|
|
|
// The reason why remember the mount namespace:
|
|
|
|
// If this service is started before APEXes and corresponding linker configuration
|
|
|
|
// get available, mark it as pre-apexd one. Note that this marking is
|
|
|
|
// permanent. So for example, if the service is re-launched (e.g., due
|
|
|
|
// to crash), it is still recognized as pre-apexd... for consistency.
|
|
|
|
void Service::SetMountNamespace() {
|
|
|
|
// APEXd is always started in the "current" namespace because it is the process to set up
|
|
|
|
// the current namespace. So, leave mount_namespace_ as empty.
|
|
|
|
if (args_[0] == "/system/bin/apexd") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Services in the following list start in the "default" mount namespace.
|
|
|
|
// Note that they should use bootstrap bionic if they start before APEXes are ready.
|
|
|
|
static const std::set<std::string> kUseDefaultMountNamespace = {
|
|
|
|
"ueventd", // load firmwares from APEXes
|
|
|
|
"hwservicemanager", // load VINTF fragments from APEXes
|
|
|
|
"servicemanager", // load VINTF fragments from APEXes
|
|
|
|
};
|
|
|
|
if (kUseDefaultMountNamespace.find(name_) != kUseDefaultMountNamespace.end()) {
|
|
|
|
mount_namespace_ = NS_DEFAULT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Use the "default" mount namespace only if it's ready
|
|
|
|
mount_namespace_ = IsDefaultMountNamespaceReady() ? NS_DEFAULT : NS_BOOTSTRAP;
|
|
|
|
}
|
|
|
|
|
2023-11-16 21:42:35 +01:00
|
|
|
static int ThreadCount() {
|
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/proc/self/task"), closedir);
|
|
|
|
if (!dir) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
dirent* entry;
|
|
|
|
while ((entry = readdir(dir.get())) != nullptr) {
|
|
|
|
if (entry->d_name[0] != '.') {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must be called BEFORE any threads are created. See also the sigprocmask() man page.
|
|
|
|
unique_fd Service::CreateSigchldFd() {
|
|
|
|
CHECK_EQ(ThreadCount(), 1);
|
|
|
|
sigset_t mask;
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGCHLD);
|
|
|
|
if (sigprocmask(SIG_BLOCK, &mask, nullptr) < 0) {
|
|
|
|
PLOG(FATAL) << "Failed to block SIGCHLD";
|
|
|
|
}
|
|
|
|
|
|
|
|
return unique_fd(signalfd(-1, &mask, SFD_CLOEXEC));
|
|
|
|
}
|
|
|
|
|
2021-07-22 06:53:28 +02:00
|
|
|
void Service::SetStartedInFirstStage(pid_t pid) {
|
|
|
|
LOG(INFO) << "adding first-stage service '" << name_ << "'...";
|
|
|
|
|
|
|
|
time_started_ = boot_clock::now(); // not accurate, but doesn't matter here
|
|
|
|
pid_ = pid;
|
|
|
|
flags_ |= SVC_RUNNING;
|
|
|
|
start_order_ = next_start_order_++;
|
|
|
|
|
|
|
|
NotifyStateChange("running");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::ResetFlagsForStart() {
|
|
|
|
// 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.
|
|
|
|
flags_ &= ~(SVC_DISABLED | SVC_RESTARTING | SVC_RESET | SVC_RESTART | SVC_DISABLED_START);
|
|
|
|
}
|
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
Result<void> Service::StartIfNotDisabled() {
|
2015-07-31 21:45:25 +02:00
|
|
|
if (!(flags_ & SVC_DISABLED)) {
|
|
|
|
return Start();
|
|
|
|
} else {
|
|
|
|
flags_ |= SVC_DISABLED_START;
|
|
|
|
}
|
2019-06-10 20:08:01 +02:00
|
|
|
return {};
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
Result<void> Service::Enable() {
|
2015-07-31 21:45:25 +02:00
|
|
|
flags_ &= ~(SVC_DISABLED | SVC_RC_DISABLED);
|
|
|
|
if (flags_ & SVC_DISABLED_START) {
|
|
|
|
return Start();
|
|
|
|
}
|
2019-06-10 20:08:01 +02:00
|
|
|
return {};
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Service::Reset() {
|
|
|
|
StopOrReset(SVC_RESET);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::Stop() {
|
|
|
|
StopOrReset(SVC_DISABLED);
|
|
|
|
}
|
|
|
|
|
2015-12-18 20:39:59 +01:00
|
|
|
void Service::Terminate() {
|
|
|
|
flags_ &= ~(SVC_RESTARTING | SVC_DISABLED_START);
|
|
|
|
flags_ |= SVC_DISABLED;
|
|
|
|
if (pid_) {
|
2016-06-15 23:49:57 +02:00
|
|
|
KillProcessGroup(SIGTERM);
|
2015-12-18 20:39:59 +01:00
|
|
|
NotifyStateChange("stopping");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 01:10:46 +02:00
|
|
|
void Service::Timeout() {
|
|
|
|
// All process state flags will be taken care of in Reap(), we really just want to kill the
|
|
|
|
// process here when it times out. Oneshot processes will transition to be disabled, and
|
|
|
|
// all other processes will transition to be restarting.
|
|
|
|
LOG(INFO) << "Service '" << name_ << "' expired its timeout of " << timeout_period_->count()
|
|
|
|
<< " seconds and will now be killed";
|
|
|
|
if (pid_) {
|
|
|
|
KillProcessGroup(SIGKILL);
|
|
|
|
NotifyStateChange("stopping");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 21:45:25 +02:00
|
|
|
void Service::Restart() {
|
|
|
|
if (flags_ & SVC_RUNNING) {
|
|
|
|
/* Stop, wait, then start the service. */
|
|
|
|
StopOrReset(SVC_RESTART);
|
|
|
|
} else if (!(flags_ & SVC_RESTARTING)) {
|
|
|
|
/* Just start the service since it's not running. */
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result = Start(); !result.ok()) {
|
2017-08-23 01:13:59 +02:00
|
|
|
LOG(ERROR) << "Could not restart '" << name_ << "': " << result.error();
|
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
} /* else: Service is restarting anyways. */
|
|
|
|
}
|
|
|
|
|
2016-06-15 23:49:57 +02:00
|
|
|
// The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART.
|
2015-07-31 21:45:25 +02:00
|
|
|
void Service::StopOrReset(int how) {
|
2016-06-15 23:49:57 +02:00
|
|
|
// The service is still SVC_RUNNING until its process exits, but if it has
|
|
|
|
// already exited it shoudn't attempt a restart yet.
|
2015-07-31 21:45:25 +02:00
|
|
|
flags_ &= ~(SVC_RESTARTING | SVC_DISABLED_START);
|
|
|
|
|
|
|
|
if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) {
|
2016-06-15 23:49:57 +02:00
|
|
|
// An illegal flag: default to SVC_DISABLED.
|
2022-12-07 21:55:35 +01:00
|
|
|
LOG(ERROR) << "service '" << name_ << "' requested unknown flag " << how
|
|
|
|
<< ", defaulting to disabling it.";
|
2015-07-31 21:45:25 +02:00
|
|
|
how = SVC_DISABLED;
|
|
|
|
}
|
2016-06-15 23:49:57 +02:00
|
|
|
|
|
|
|
// If the service has not yet started, prevent it from auto-starting with its class.
|
2015-07-31 21:45:25 +02:00
|
|
|
if (how == SVC_RESET) {
|
|
|
|
flags_ |= (flags_ & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
|
|
|
|
} else {
|
|
|
|
flags_ |= how;
|
|
|
|
}
|
2017-10-27 20:29:13 +02:00
|
|
|
// Make sure it's in right status when a restart immediately follow a
|
|
|
|
// stop/reset or vice versa.
|
|
|
|
if (how == SVC_RESTART) {
|
|
|
|
flags_ &= (~(SVC_DISABLED | SVC_RESET));
|
|
|
|
} else {
|
|
|
|
flags_ &= (~SVC_RESTART);
|
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
|
|
|
|
if (pid_) {
|
2022-12-02 00:44:31 +01:00
|
|
|
if (flags_ & SVC_GENTLE_KILL) {
|
|
|
|
KillProcessGroup(SIGTERM);
|
|
|
|
if (!process_cgroup_empty()) std::this_thread::sleep_for(200ms);
|
|
|
|
}
|
2016-06-15 23:49:57 +02:00
|
|
|
KillProcessGroup(SIGKILL);
|
2015-07-31 21:45:25 +02:00
|
|
|
NotifyStateChange("stopping");
|
|
|
|
} else {
|
|
|
|
NotifyStateChange("stopped");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:34:41 +02:00
|
|
|
Result<std::unique_ptr<Service>> Service::MakeTemporaryOneshotService(
|
|
|
|
const std::vector<std::string>& args) {
|
2015-07-31 21:45:25 +02:00
|
|
|
// Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS...
|
|
|
|
// SECLABEL can be a - to denote default
|
|
|
|
std::size_t command_arg = 1;
|
|
|
|
for (std::size_t i = 1; i < args.size(); ++i) {
|
|
|
|
if (args[i] == "--") {
|
|
|
|
command_arg = i + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (command_arg > 4 + NR_SVC_SUPP_GIDS) {
|
2019-07-30 18:34:41 +02:00
|
|
|
return Error() << "exec called with too many supplementary group ids";
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (command_arg >= args.size()) {
|
2019-07-30 18:34:41 +02:00
|
|
|
return Error() << "exec called without command";
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
std::vector<std::string> str_args(args.begin() + command_arg, args.end());
|
|
|
|
|
2017-07-28 23:48:41 +02:00
|
|
|
static size_t exec_count = 0;
|
|
|
|
exec_count++;
|
|
|
|
std::string name = "exec " + std::to_string(exec_count) + " (" + Join(str_args, " ") + ")";
|
2017-04-26 02:31:06 +02:00
|
|
|
|
2017-07-28 23:48:41 +02:00
|
|
|
unsigned flags = SVC_ONESHOT | SVC_TEMPORARY;
|
2016-04-22 00:35:09 +02:00
|
|
|
unsigned namespace_flags = 0;
|
2015-07-31 21:45:25 +02:00
|
|
|
|
|
|
|
std::string seclabel = "";
|
|
|
|
if (command_arg > 2 && args[1] != "-") {
|
|
|
|
seclabel = args[1];
|
|
|
|
}
|
2017-08-03 21:54:07 +02:00
|
|
|
Result<uid_t> uid = 0;
|
2015-07-31 21:45:25 +02:00
|
|
|
if (command_arg > 3) {
|
2017-08-03 21:54:07 +02:00
|
|
|
uid = DecodeUid(args[2]);
|
2020-02-05 19:49:33 +01:00
|
|
|
if (!uid.ok()) {
|
2019-07-30 18:34:41 +02:00
|
|
|
return Error() << "Unable to decode UID for '" << args[2] << "': " << uid.error();
|
2017-05-05 02:40:33 +02:00
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
2017-08-03 21:54:07 +02:00
|
|
|
Result<gid_t> gid = 0;
|
2015-07-31 21:45:25 +02:00
|
|
|
std::vector<gid_t> supp_gids;
|
|
|
|
if (command_arg > 4) {
|
2017-08-03 21:54:07 +02:00
|
|
|
gid = DecodeUid(args[3]);
|
2020-02-05 19:49:33 +01:00
|
|
|
if (!gid.ok()) {
|
2019-07-30 18:34:41 +02:00
|
|
|
return Error() << "Unable to decode GID for '" << args[3] << "': " << gid.error();
|
2017-05-05 02:40:33 +02:00
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
std::size_t nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */;
|
|
|
|
for (size_t i = 0; i < nr_supp_gids; ++i) {
|
2017-08-03 21:54:07 +02:00
|
|
|
auto supp_gid = DecodeUid(args[4 + i]);
|
2020-02-05 19:49:33 +01:00
|
|
|
if (!supp_gid.ok()) {
|
2019-07-30 18:34:41 +02:00
|
|
|
return Error() << "Unable to decode GID for '" << args[4 + i]
|
|
|
|
<< "': " << supp_gid.error();
|
2017-05-05 02:40:33 +02:00
|
|
|
}
|
2017-08-03 21:54:07 +02:00
|
|
|
supp_gids.push_back(*supp_gid);
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-06 19:45:56 +01:00
|
|
|
return std::make_unique<Service>(name, flags, *uid, *gid, supp_gids, namespace_flags, seclabel,
|
2022-07-15 00:51:10 +02:00
|
|
|
nullptr, /*filename=*/"", str_args);
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2021-07-22 06:53:28 +02:00
|
|
|
// This is used for snapuserd_proxy, which hands off a socket to snapuserd. It's
|
|
|
|
// a special case to support the daemon launched in first-stage init. The persist
|
|
|
|
// feature is not part of the init language and is only used here.
|
|
|
|
bool Service::MarkSocketPersistent(const std::string& socket_name) {
|
|
|
|
for (auto& socket : sockets_) {
|
|
|
|
if (socket.name == socket_name) {
|
|
|
|
socket.persist = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-22 21:53:17 +02:00
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|