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"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
2016-11-11 02:43:47 +01:00
|
|
|
#include <inttypes.h>
|
2018-05-17 00:10:24 +02:00
|
|
|
#include <linux/input.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/mount.h>
|
|
|
|
#include <sys/prctl.h>
|
2016-05-19 02:36:30 +02:00
|
|
|
#include <sys/resource.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-12-18 20:39:59 +01:00
|
|
|
#include <sys/wait.h>
|
2015-07-31 21:45:25 +02:00
|
|
|
#include <termios.h>
|
2015-08-12 01:37:04 +02:00
|
|
|
#include <unistd.h>
|
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>
|
2016-10-12 02:09:00 +02:00
|
|
|
#include <android-base/parseint.h>
|
2018-05-25 03:00:39 +02:00
|
|
|
#include <android-base/properties.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>
|
2018-04-21 01:18:12 +02:00
|
|
|
#include <android-base/unique_fd.h>
|
2017-10-06 03:50:22 +02:00
|
|
|
#include <hidl-util/FQName.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>
|
|
|
|
#include <system/thread_defs.h>
|
2016-06-01 23:03:55 +02:00
|
|
|
|
2017-08-25 19:39:25 +02:00
|
|
|
#include "rlimit_parser.h"
|
2015-07-31 21:45:25 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
2018-02-14 01:50:08 +01:00
|
|
|
#if defined(__ANDROID__)
|
2018-08-01 22:41:12 +02:00
|
|
|
#include <android/api-level.h>
|
2018-02-14 01:50:08 +01:00
|
|
|
#include <sys/system_properties.h>
|
|
|
|
|
|
|
|
#include "init.h"
|
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"
|
2018-02-14 01:50:08 +01:00
|
|
|
#include "property_service.h"
|
2018-08-01 22:41:12 +02: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;
|
2017-06-22 21:53:17 +02:00
|
|
|
using android::base::GetProperty;
|
|
|
|
using android::base::Join;
|
2016-10-12 02:09:00 +02:00
|
|
|
using android::base::ParseInt;
|
2018-10-16 19:58:06 +02:00
|
|
|
using android::base::Split;
|
2017-06-22 21:53:17 +02:00
|
|
|
using android::base::StartsWith;
|
2015-08-26 20:43:36 +02:00
|
|
|
using android::base::StringPrintf;
|
2018-04-21 01:18:12 +02: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
|
|
|
}
|
|
|
|
std::unique_ptr<char> mycon(raw_con);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
std::unique_ptr<char> filecon(raw_filecon);
|
|
|
|
|
|
|
|
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/"
|
|
|
|
"device-policy#label_new_services_and_address_denials";
|
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-21 01:18:12 +02:00
|
|
|
Result<Success> Service::SetUpMountNamespace() const {
|
2016-04-22 00:35:09 +02:00
|
|
|
constexpr unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
|
|
|
|
|
2017-10-25 14:07:26 +02:00
|
|
|
// Recursively remount / as slave like zygote does so unmounting and mounting /proc
|
|
|
|
// doesn't interfere with the parent namespace's /proc mount. This will also
|
|
|
|
// prevent any other mounts/unmounts initiated by the service from interfering
|
|
|
|
// with the parent namespace but will still allow mount events from the parent
|
|
|
|
// namespace to propagate to the child.
|
|
|
|
if (mount("rootfs", "/", nullptr, (MS_SLAVE | MS_REC), nullptr) == -1) {
|
2018-04-21 01:18:12 +02:00
|
|
|
return ErrnoError() << "Could not remount(/) recursively as slave";
|
2017-10-25 14:07:26 +02:00
|
|
|
}
|
2018-04-21 01:18:12 +02:00
|
|
|
|
|
|
|
// umount() then mount() /proc and/or /sys
|
2017-10-25 14:07:26 +02:00
|
|
|
// Note that it is not sufficient to mount with MS_REMOUNT.
|
2018-04-21 01:18:12 +02:00
|
|
|
if (namespace_flags_ & CLONE_NEWPID) {
|
|
|
|
if (umount("/proc") == -1) {
|
|
|
|
return ErrnoError() << "Could not umount(/proc)";
|
|
|
|
}
|
|
|
|
if (mount("", "/proc", "proc", kSafeFlags, "") == -1) {
|
|
|
|
return ErrnoError() << "Could not mount(/proc)";
|
|
|
|
}
|
2017-10-25 14:07:26 +02:00
|
|
|
}
|
2018-04-21 01:18:12 +02:00
|
|
|
bool remount_sys = std::any_of(namespaces_to_enter_.begin(), namespaces_to_enter_.end(),
|
|
|
|
[](const auto& entry) { return entry.first == CLONE_NEWNET; });
|
|
|
|
if (remount_sys) {
|
|
|
|
if (umount2("/sys", MNT_DETACH) == -1) {
|
|
|
|
return ErrnoError() << "Could not umount(/sys)";
|
|
|
|
}
|
2018-07-13 20:07:11 +02:00
|
|
|
if (mount("", "/sys", "sysfs", kSafeFlags, "") == -1) {
|
2018-04-21 01:18:12 +02:00
|
|
|
return ErrnoError() << "Could not mount(/sys)";
|
|
|
|
}
|
2016-04-22 00:35:09 +02:00
|
|
|
}
|
2018-04-21 01:18:12 +02:00
|
|
|
return Success();
|
|
|
|
}
|
2016-04-22 00:35:09 +02:00
|
|
|
|
2018-04-21 01:18:12 +02:00
|
|
|
Result<Success> Service::SetUpPidNamespace() const {
|
|
|
|
if (prctl(PR_SET_NAME, name_.c_str()) == -1) {
|
|
|
|
return ErrnoError() << "Could not set name";
|
2016-04-22 00:35:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pid_t child_pid = fork();
|
|
|
|
if (child_pid == -1) {
|
2018-04-21 01:18:12 +02:00
|
|
|
return ErrnoError() << "Could not fork init inside the PID namespace";
|
2016-04-22 00:35:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (child_pid > 0) {
|
|
|
|
// So that we exit with the right status.
|
|
|
|
static int init_exitstatus = 0;
|
|
|
|
signal(SIGTERM, [](int) { _exit(init_exitstatus); });
|
|
|
|
|
|
|
|
pid_t waited_pid;
|
|
|
|
int status;
|
|
|
|
while ((waited_pid = wait(&status)) > 0) {
|
|
|
|
// This loop will end when there are no processes left inside the
|
|
|
|
// PID namespace or when the init process inside the PID namespace
|
|
|
|
// gets a signal.
|
|
|
|
if (waited_pid == child_pid) {
|
|
|
|
init_exitstatus = status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!WIFEXITED(init_exitstatus)) {
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
_exit(WEXITSTATUS(init_exitstatus));
|
|
|
|
}
|
2018-04-21 01:18:12 +02:00
|
|
|
return Success();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<Success> Service::EnterNamespaces() const {
|
|
|
|
for (const auto& [nstype, path] : namespaces_to_enter_) {
|
|
|
|
auto fd = unique_fd{open(path.c_str(), O_RDONLY | O_CLOEXEC)};
|
|
|
|
if (!fd) {
|
|
|
|
return ErrnoError() << "Could not open namespace at " << path;
|
|
|
|
}
|
|
|
|
if (setns(fd, nstype) == -1) {
|
|
|
|
return ErrnoError() << "Could not setns() namespace at " << path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Success();
|
2016-04-22 00:35:09 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if (!expand_props(args[i], &expanded_args[i])) {
|
|
|
|
LOG(FATAL) << args[0] << ": cannot expand '" << args[i] << "'";
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
static bool IsRuntimeApexReady() {
|
|
|
|
struct stat buf;
|
|
|
|
return stat("/apex/com.android.runtime/", &buf) == 0;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
const std::vector<std::string>& args)
|
2019-02-06 19:45:56 +01:00
|
|
|
: Service(name, 0, 0, 0, {}, 0, "", subcontext_for_restart_commands, args) {}
|
2015-07-31 21:45:25 +02:00
|
|
|
|
2017-03-27 19:59:11 +02:00
|
|
|
Service::Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid,
|
2019-02-06 19:45:56 +01:00
|
|
|
const std::vector<gid_t>& supp_gids, unsigned namespace_flags,
|
|
|
|
const std::string& seclabel, Subcontext* subcontext_for_restart_commands,
|
|
|
|
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),
|
|
|
|
uid_(uid),
|
|
|
|
gid_(gid),
|
|
|
|
supp_gids_(supp_gids),
|
|
|
|
namespace_flags_(namespace_flags),
|
|
|
|
seclabel_(seclabel),
|
2018-02-14 01:24:51 +01:00
|
|
|
onrestart_(false, subcontext_for_restart_commands, "<Service '" + name + "' onrestart>", 0,
|
|
|
|
"onrestart", {}),
|
2017-03-27 19:59:11 +02:00
|
|
|
ioprio_class_(IoSchedClass_NONE),
|
|
|
|
ioprio_pri_(0),
|
|
|
|
priority_(0),
|
|
|
|
oom_score_adjust_(-1000),
|
2017-07-27 01:09:09 +02:00
|
|
|
start_order_(0),
|
2018-02-14 01:24:51 +01:00
|
|
|
args_(args) {}
|
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_;
|
|
|
|
property_set(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()) {
|
|
|
|
property_set(boottime_property, std::to_string(start_ns));
|
|
|
|
}
|
2016-11-11 02:43:47 +01:00
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2016-06-15 23:49:57 +02:00
|
|
|
void Service::KillProcessGroup(int signal) {
|
2017-05-04 20:32:36 +02:00
|
|
|
// If we've already seen a successful result from killProcessGroup*(), then we have removed
|
|
|
|
// the cgroup already and calling these functions a second time will simply result in an error.
|
|
|
|
// This is true regardless of which signal was sent.
|
|
|
|
// These functions handle their own logging, so no additional logging is needed.
|
|
|
|
if (!process_cgroup_empty_) {
|
|
|
|
LOG(INFO) << "Sending signal " << signal << " to service '" << name_ << "' (pid " << pid_
|
|
|
|
<< ") process group...";
|
|
|
|
int r;
|
|
|
|
if (signal == SIGTERM) {
|
|
|
|
r = killProcessGroupOnce(uid_, pid_, signal);
|
|
|
|
} else {
|
|
|
|
r = killProcessGroup(uid_, pid_, signal);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r == 0) process_cgroup_empty_ = true;
|
|
|
|
}
|
2016-06-15 23:49:57 +02:00
|
|
|
}
|
|
|
|
|
2016-07-08 19:32:26 +02:00
|
|
|
void Service::SetProcessAttributes() {
|
2017-08-25 19:39:25 +02:00
|
|
|
for (const auto& rlimit : rlimits_) {
|
|
|
|
if (setrlimit(rlimit.first, &rlimit.second) == -1) {
|
|
|
|
LOG(FATAL) << StringPrintf("setrlimit(%d, {rlim_cur=%ld, rlim_max=%ld}) failed",
|
|
|
|
rlimit.first, rlimit.second.rlim_cur, rlimit.second.rlim_max);
|
|
|
|
}
|
|
|
|
}
|
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.
|
2019-02-06 19:45:56 +01: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 03:18:16 +02:00
|
|
|
// TODO: work out why this fails for `console` then upgrade to FATAL.
|
|
|
|
if (setpgid(0, getpid()) == -1) PLOG(ERROR) << "setpgid failed for " << name_;
|
2016-07-08 19:32:26 +02:00
|
|
|
|
|
|
|
if (gid_) {
|
|
|
|
if (setgid(gid_) != 0) {
|
2016-07-26 03:18:16 +02:00
|
|
|
PLOG(FATAL) << "setgid failed for " << name_;
|
2016-07-08 19:32:26 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-29 21:20:00 +02:00
|
|
|
if (setgroups(supp_gids_.size(), &supp_gids_[0]) != 0) {
|
|
|
|
PLOG(FATAL) << "setgroups failed for " << name_;
|
2016-07-08 19:32:26 +02:00
|
|
|
}
|
|
|
|
if (uid_) {
|
|
|
|
if (setuid(uid_) != 0) {
|
2016-07-26 03:18:16 +02:00
|
|
|
PLOG(FATAL) << "setuid failed for " << name_;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
if (priority_ != 0) {
|
|
|
|
if (setpriority(PRIO_PROCESS, 0, priority_) != 0) {
|
2016-07-26 03:18:16 +02:00
|
|
|
PLOG(FATAL) << "setpriority failed for " << name_;
|
2016-07-08 19:32:26 +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_;
|
|
|
|
}
|
2017-11-17 00:52:00 +01:00
|
|
|
} else if (uid_) {
|
|
|
|
// 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)) {
|
2016-06-15 23:49:57 +02:00
|
|
|
KillProcessGroup(SIGKILL);
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 16:45:34 +02:00
|
|
|
// Remove any descriptor resources we may have created.
|
|
|
|
std::for_each(descriptors_.begin(), descriptors_.end(),
|
|
|
|
std::bind(&DescriptorInfo::Clean, std::placeholders::_1));
|
2015-07-31 21:45:25 +02:00
|
|
|
|
2018-04-13 19:38:57 +02:00
|
|
|
for (const auto& f : reap_callbacks_) {
|
|
|
|
f(siginfo);
|
|
|
|
}
|
|
|
|
|
2017-07-28 23:48:41 +02:00
|
|
|
if (flags_ & SVC_EXEC) UnSetExec();
|
|
|
|
|
|
|
|
if (flags_ & SVC_TEMPORARY) return;
|
2015-07-31 21:45:25 +02:00
|
|
|
|
|
|
|
pid_ = 0;
|
|
|
|
flags_ &= (~SVC_RUNNING);
|
2017-07-27 01:09:09 +02:00
|
|
|
start_order_ = 0;
|
2015-07-31 21:45:25 +02:00
|
|
|
|
|
|
|
// Oneshot processes go into the disabled state on exit,
|
|
|
|
// except when manually restarted.
|
|
|
|
if ((flags_ & SVC_ONESHOT) && !(flags_ & SVC_RESTART)) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-01-07 11:19:02 +01:00
|
|
|
// If we crash > 4 times in 4 minutes, reboot into bootloader or set crashing property
|
2016-11-11 02:43:47 +01:00
|
|
|
boot_clock::time_point now = boot_clock::now();
|
2019-02-04 21:11:43 +01:00
|
|
|
if (((flags_ & SVC_CRITICAL) || !pre_apexd_) && !(flags_ & SVC_RESTART)) {
|
2016-11-11 02:43:47 +01:00
|
|
|
if (now < time_crashed_ + 4min) {
|
|
|
|
if (++crash_count_ > 4) {
|
2019-01-07 11:19:02 +01:00
|
|
|
if (flags_ & SVC_CRITICAL) {
|
|
|
|
// Aborts into bootloader
|
|
|
|
LOG(FATAL) << "critical process '" << name_ << "' exited 4 times in 4 minutes";
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "updatable process '" << name_ << "' exited 4 times in 4 minutes";
|
|
|
|
// Notifies update_verifier and apexd
|
|
|
|
property_set("ro.init.updatable_crashing", "1");
|
|
|
|
}
|
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_, " ");
|
2016-10-27 16:45:34 +02:00
|
|
|
std::for_each(descriptors_.begin(), descriptors_.end(),
|
|
|
|
[] (const auto& info) { LOG(INFO) << *info; });
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseCapabilities(std::vector<std::string>&& args) {
|
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
|
|
|
capabilities_ = 0;
|
|
|
|
|
2016-12-15 18:13:38 +01:00
|
|
|
if (!CapAmbientSupported()) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error()
|
|
|
|
<< "capabilities requested but the kernel does not support ambient capabilities";
|
2016-12-15 18:13:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int last_valid_cap = GetLastValidCap();
|
2019-02-06 19:45:56 +01:00
|
|
|
if (last_valid_cap >= capabilities_->size()) {
|
2016-12-15 18:13:38 +01:00
|
|
|
LOG(WARNING) << "last valid run-time capability is larger than CAP_LAST_CAP";
|
|
|
|
}
|
|
|
|
|
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
|
|
|
for (size_t i = 1; i < args.size(); i++) {
|
|
|
|
const std::string& arg = args[i];
|
2016-12-15 18:13:38 +01:00
|
|
|
int res = LookupCap(arg);
|
|
|
|
if (res < 0) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << StringPrintf("invalid capability '%s'", arg.c_str());
|
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-12-15 18:13:38 +01:00
|
|
|
unsigned int cap = static_cast<unsigned int>(res); // |res| is >= 0.
|
|
|
|
if (cap > last_valid_cap) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << StringPrintf("capability '%s' not supported by the kernel",
|
|
|
|
arg.c_str());
|
2016-12-15 18:13:38 +01:00
|
|
|
}
|
2019-02-06 19:45:56 +01:00
|
|
|
(*capabilities_)[cap] = true;
|
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
|
|
|
}
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
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
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseClass(std::vector<std::string>&& args) {
|
2017-03-27 19:59:11 +02:00
|
|
|
classnames_ = std::set<std::string>(args.begin() + 1, args.end());
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseConsole(std::vector<std::string>&& args) {
|
2015-08-26 20:43:36 +02:00
|
|
|
flags_ |= SVC_CONSOLE;
|
2016-03-21 09:08:07 +01:00
|
|
|
console_ = args.size() > 1 ? "/dev/" + args[1] : "";
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseCritical(std::vector<std::string>&& args) {
|
2015-08-26 20:43:36 +02:00
|
|
|
flags_ |= SVC_CRITICAL;
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseDisabled(std::vector<std::string>&& args) {
|
2015-08-26 20:43:36 +02:00
|
|
|
flags_ |= SVC_DISABLED;
|
|
|
|
flags_ |= SVC_RC_DISABLED;
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseEnterNamespace(std::vector<std::string>&& args) {
|
2018-04-21 01:18:12 +02:00
|
|
|
if (args[1] != "net") {
|
|
|
|
return Error() << "Init only supports entering network namespaces";
|
|
|
|
}
|
|
|
|
if (!namespaces_to_enter_.empty()) {
|
|
|
|
return Error() << "Only one network namespace may be entered";
|
|
|
|
}
|
|
|
|
// Network namespaces require that /sys is remounted, otherwise the old adapters will still be
|
|
|
|
// present. Therefore, they also require mount namespaces.
|
|
|
|
namespace_flags_ |= CLONE_NEWNS;
|
2018-10-17 20:11:23 +02:00
|
|
|
namespaces_to_enter_.emplace_back(CLONE_NEWNET, std::move(args[2]));
|
2018-04-21 01:18:12 +02:00
|
|
|
return Success();
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseGroup(std::vector<std::string>&& args) {
|
2017-08-03 21:54:07 +02:00
|
|
|
auto gid = DecodeUid(args[1]);
|
|
|
|
if (!gid) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "Unable to decode GID for '" << args[1] << "': " << gid.error();
|
2017-05-05 02:40:33 +02:00
|
|
|
}
|
2017-08-03 21:54:07 +02:00
|
|
|
gid_ = *gid;
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
for (std::size_t n = 2; n < args.size(); n++) {
|
2017-08-03 21:54:07 +02:00
|
|
|
gid = DecodeUid(args[n]);
|
|
|
|
if (!gid) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "Unable to decode GID for '" << args[n] << "': " << gid.error();
|
2017-05-05 02:40:33 +02:00
|
|
|
}
|
2017-08-03 21:54:07 +02:00
|
|
|
supp_gids_.emplace_back(*gid);
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParsePriority(std::vector<std::string>&& args) {
|
2016-10-12 02:09:00 +02:00
|
|
|
priority_ = 0;
|
|
|
|
if (!ParseInt(args[1], &priority_,
|
2016-11-12 03:06:31 +01:00
|
|
|
static_cast<int>(ANDROID_PRIORITY_HIGHEST), // highest is negative
|
|
|
|
static_cast<int>(ANDROID_PRIORITY_LOWEST))) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << StringPrintf("process priority value must be range %d - %d",
|
|
|
|
ANDROID_PRIORITY_HIGHEST, ANDROID_PRIORITY_LOWEST);
|
2016-05-19 02:36:30 +02:00
|
|
|
}
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2016-05-19 02:36:30 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseInterface(std::vector<std::string>&& args) {
|
2017-10-06 03:50:22 +02:00
|
|
|
const std::string& interface_name = args[1];
|
|
|
|
const std::string& instance_name = args[2];
|
|
|
|
|
2018-03-06 23:57:46 +01:00
|
|
|
FQName fq_name;
|
|
|
|
if (!FQName::parse(interface_name, &fq_name)) {
|
2017-10-06 03:50:22 +02:00
|
|
|
return Error() << "Invalid fully-qualified name for interface '" << interface_name << "'";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fq_name.isFullyQualified()) {
|
|
|
|
return Error() << "Interface name not fully-qualified '" << interface_name << "'";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fq_name.isValidValueName()) {
|
|
|
|
return Error() << "Interface name must not be a value name '" << interface_name << "'";
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string fullname = interface_name + "/" + instance_name;
|
|
|
|
|
|
|
|
for (const auto& svc : ServiceList::GetInstance()) {
|
|
|
|
if (svc->interfaces().count(fullname) > 0) {
|
|
|
|
return Error() << "Interface '" << fullname << "' redefined in " << name()
|
|
|
|
<< " but is already defined by " << svc->name();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interfaces_.insert(fullname);
|
|
|
|
|
|
|
|
return Success();
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseIoprio(std::vector<std::string>&& args) {
|
2016-10-12 02:09:00 +02:00
|
|
|
if (!ParseInt(args[2], &ioprio_pri_, 0, 7)) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "priority value must be range 0 - 7";
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (args[1] == "rt") {
|
|
|
|
ioprio_class_ = IoSchedClass_RT;
|
|
|
|
} else if (args[1] == "be") {
|
|
|
|
ioprio_class_ = IoSchedClass_BE;
|
|
|
|
} else if (args[1] == "idle") {
|
|
|
|
ioprio_class_ = IoSchedClass_IDLE;
|
|
|
|
} else {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "ioprio option usage: ioprio <rt|be|idle> <0-7>";
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseKeycodes(std::vector<std::string>&& args) {
|
2018-10-16 19:58:06 +02:00
|
|
|
auto it = args.begin() + 1;
|
|
|
|
if (args.size() == 2 && StartsWith(args[1], "$")) {
|
|
|
|
std::string expanded;
|
|
|
|
if (!expand_props(args[1], &expanded)) {
|
|
|
|
return Error() << "Could not expand property '" << args[1] << "'";
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the property is not set, it defaults to none, in which case there are no keycodes
|
|
|
|
// for this service.
|
|
|
|
if (expanded == "none") {
|
|
|
|
return Success();
|
|
|
|
}
|
|
|
|
|
|
|
|
args = Split(expanded, ",");
|
|
|
|
it = args.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; it != args.end(); ++it) {
|
2016-10-12 02:09:00 +02:00
|
|
|
int code;
|
2018-10-16 19:58:06 +02:00
|
|
|
if (ParseInt(*it, &code, 0, KEY_MAX)) {
|
2018-05-17 00:10:24 +02:00
|
|
|
for (auto& key : keycodes_) {
|
2018-10-16 19:58:06 +02:00
|
|
|
if (key == code) return Error() << "duplicate keycode: " << *it;
|
2018-05-17 00:10:24 +02:00
|
|
|
}
|
2018-05-19 00:25:15 +02:00
|
|
|
keycodes_.insert(std::upper_bound(keycodes_.begin(), keycodes_.end(), code), code);
|
2016-10-12 02:09:00 +02:00
|
|
|
} else {
|
2018-10-16 19:58:06 +02:00
|
|
|
return Error() << "invalid keycode: " << *it;
|
2016-10-12 02:09:00 +02:00
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseOneshot(std::vector<std::string>&& args) {
|
2015-08-26 20:43:36 +02:00
|
|
|
flags_ |= SVC_ONESHOT;
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseOnrestart(std::vector<std::string>&& args) {
|
|
|
|
args.erase(args.begin());
|
init: Stop combining actions
In the past, I had thought it didn't make sense to have multiple
Action classes with identical triggers within ActionManager::actions_,
and opted to instead combine these into a single action. In theory,
it should reduce memory overhead as only one copy of the triggers
needs to be stored.
In practice, this ends up not being a good idea.
Most importantly, given a file with the below three sections in this
same order:
on boot
setprop a b
on boot && property:true=true
setprop c d
on boot
setprop e f
Assuming that property 'true' == 'true', when the `boot` event
happens, the order of the setprop commands will actually be:
setprop a b
setprop e f
setprop c d
instead of the more intuitive order of:
setprop a b
setprop c d
setprop e f
This is a mistake and this CL fixes it. It also documents this order.
Secondly, with a given 'Action' now spanning multiple files, in order
to keep track of which file a command is run from, the 'Command'
itself needs to store this. Ironically to the original intention,
this increases total ram usage. This change now only stores the file
name in each 'Action' instead of each 'Command'. All in all this is a
negligible trade off of ram usage.
Thirdly, this requires a bunch of extra code and assumptions that
don't help anything else. In particular it forces to keep property triggers
sorted for easy comparison, which I'm using an std::map for currently,
but that is not the best data structure to contain them.
Lastly, I added the filename and line number to the 'processing
action' LOG(INFO) message.
Test: Boot bullhead, observe above changes
Test: Boot sailfish, observe no change in boot time
Change-Id: I3fbcac4ee677351314e33012c758145be82346e9
2017-04-18 22:21:54 +02:00
|
|
|
int line = onrestart_.NumCommands() + 1;
|
2018-10-17 20:11:23 +02:00
|
|
|
if (auto result = onrestart_.AddCommand(std::move(args), line); !result) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "cannot add Onrestart command: " << result.error();
|
|
|
|
}
|
|
|
|
return Success();
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseNamespace(std::vector<std::string>&& args) {
|
2016-04-22 00:35:09 +02:00
|
|
|
for (size_t i = 1; i < args.size(); i++) {
|
|
|
|
if (args[i] == "pid") {
|
|
|
|
namespace_flags_ |= CLONE_NEWPID;
|
|
|
|
// PID namespaces require mount namespaces.
|
|
|
|
namespace_flags_ |= CLONE_NEWNS;
|
|
|
|
} else if (args[i] == "mnt") {
|
|
|
|
namespace_flags_ |= CLONE_NEWNS;
|
|
|
|
} else {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "namespace must be 'pid' or 'mnt'";
|
2016-04-22 00:35:09 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2016-04-22 00:35:09 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseOomScoreAdjust(std::vector<std::string>&& args) {
|
2016-10-12 02:09:00 +02:00
|
|
|
if (!ParseInt(args[1], &oom_score_adjust_, -1000, 1000)) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "oom_score_adjust value must be in range -1000 - +1000";
|
2016-07-22 21:07:06 +02:00
|
|
|
}
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2016-07-22 21:07:06 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseOverride(std::vector<std::string>&& args) {
|
2017-11-14 00:31:54 +01:00
|
|
|
override_ = true;
|
|
|
|
return Success();
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseMemcgSwappiness(std::vector<std::string>&& args) {
|
2017-07-17 04:38:11 +02:00
|
|
|
if (!ParseInt(args[1], &swappiness_, 0)) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "swappiness value must be equal or greater than 0";
|
2017-07-17 04:38:11 +02:00
|
|
|
}
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2017-07-17 04:38:11 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseMemcgLimitInBytes(std::vector<std::string>&& args) {
|
2017-07-17 04:38:11 +02:00
|
|
|
if (!ParseInt(args[1], &limit_in_bytes_, 0)) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "limit_in_bytes value must be equal or greater than 0";
|
2017-07-17 04:38:11 +02:00
|
|
|
}
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2017-07-17 04:38:11 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 23:49:33 +01:00
|
|
|
Result<Success> Service::ParseMemcgLimitPercent(std::vector<std::string>&& args) {
|
|
|
|
if (!ParseInt(args[1], &limit_percent_, 0)) {
|
|
|
|
return Error() << "limit_percent value must be equal or greater than 0";
|
|
|
|
}
|
|
|
|
return Success();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<Success> Service::ParseMemcgLimitProperty(std::vector<std::string>&& args) {
|
|
|
|
limit_property_ = std::move(args[1]);
|
|
|
|
return Success();
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseMemcgSoftLimitInBytes(std::vector<std::string>&& args) {
|
2017-07-17 04:38:11 +02:00
|
|
|
if (!ParseInt(args[1], &soft_limit_in_bytes_, 0)) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "soft_limit_in_bytes value must be equal or greater than 0";
|
2017-07-17 04:38:11 +02:00
|
|
|
}
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2017-07-17 04:38:11 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseProcessRlimit(std::vector<std::string>&& args) {
|
2017-08-25 19:39:25 +02:00
|
|
|
auto rlimit = ParseRlimit(args);
|
|
|
|
if (!rlimit) return rlimit.error();
|
|
|
|
|
|
|
|
rlimits_.emplace_back(*rlimit);
|
|
|
|
return Success();
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseRestartPeriod(std::vector<std::string>&& args) {
|
2018-09-28 01:10:46 +02:00
|
|
|
int period;
|
|
|
|
if (!ParseInt(args[1], &period, 5)) {
|
|
|
|
return Error() << "restart_period value must be an integer >= 5";
|
|
|
|
}
|
|
|
|
restart_period_ = std::chrono::seconds(period);
|
|
|
|
return Success();
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseSeclabel(std::vector<std::string>&& args) {
|
|
|
|
seclabel_ = std::move(args[1]);
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseSigstop(std::vector<std::string>&& args) {
|
2018-04-17 23:48:44 +02:00
|
|
|
sigstop_ = true;
|
|
|
|
return Success();
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseSetenv(std::vector<std::string>&& args) {
|
|
|
|
environment_vars_.emplace_back(std::move(args[1]), std::move(args[2]));
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseShutdown(std::vector<std::string>&& args) {
|
2017-07-05 20:38:44 +02:00
|
|
|
if (args[1] == "critical") {
|
|
|
|
flags_ |= SVC_SHUTDOWN_CRITICAL;
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2017-07-05 20:38:44 +02:00
|
|
|
}
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "Invalid shutdown option";
|
2017-07-05 20:38:44 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseTimeoutPeriod(std::vector<std::string>&& args) {
|
2018-09-28 01:10:46 +02:00
|
|
|
int period;
|
|
|
|
if (!ParseInt(args[1], &period, 1)) {
|
|
|
|
return Error() << "timeout_period value must be an integer >= 1";
|
|
|
|
}
|
|
|
|
timeout_period_ = std::chrono::seconds(period);
|
|
|
|
return Success();
|
|
|
|
}
|
|
|
|
|
2016-10-27 16:45:34 +02:00
|
|
|
template <typename T>
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::AddDescriptor(std::vector<std::string>&& args) {
|
2016-10-27 16:45:34 +02:00
|
|
|
int perm = args.size() > 3 ? std::strtoul(args[3].c_str(), 0, 8) : -1;
|
2017-08-03 21:54:07 +02:00
|
|
|
Result<uid_t> uid = 0;
|
|
|
|
Result<gid_t> gid = 0;
|
2016-10-27 16:45:34 +02:00
|
|
|
std::string context = args.size() > 6 ? args[6] : "";
|
|
|
|
|
2017-05-05 02:40:33 +02:00
|
|
|
if (args.size() > 4) {
|
2017-08-03 21:54:07 +02:00
|
|
|
uid = DecodeUid(args[4]);
|
|
|
|
if (!uid) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "Unable to find UID for '" << args[4] << "': " << uid.error();
|
2017-05-05 02:40:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.size() > 5) {
|
2017-08-03 21:54:07 +02:00
|
|
|
gid = DecodeUid(args[5]);
|
|
|
|
if (!gid) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "Unable to find GID for '" << args[5] << "': " << gid.error();
|
2017-05-05 02:40:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 21:54:07 +02:00
|
|
|
auto descriptor = std::make_unique<T>(args[1], args[2], *uid, *gid, perm, context);
|
2016-10-27 16:45:34 +02:00
|
|
|
|
|
|
|
auto old =
|
|
|
|
std::find_if(descriptors_.begin(), descriptors_.end(),
|
|
|
|
[&descriptor] (const auto& other) { return descriptor.get() == other.get(); });
|
|
|
|
|
|
|
|
if (old != descriptors_.end()) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "duplicate descriptor " << args[1] << " " << args[2];
|
2016-10-27 16:45:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
descriptors_.emplace_back(std::move(descriptor));
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2016-10-27 16:45:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// name type perm [ uid gid context ]
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseSocket(std::vector<std::string>&& args) {
|
2017-06-22 21:53:17 +02:00
|
|
|
if (!StartsWith(args[2], "dgram") && !StartsWith(args[2], "stream") &&
|
|
|
|
!StartsWith(args[2], "seqpacket")) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "socket type must be 'dgram', 'stream' or 'seqpacket'";
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
2018-10-17 20:11:23 +02:00
|
|
|
return AddDescriptor<SocketInfo>(std::move(args));
|
2016-10-27 16:45:34 +02:00
|
|
|
}
|
2015-08-26 20:43:36 +02:00
|
|
|
|
2016-10-27 16:45:34 +02:00
|
|
|
// name type perm [ uid gid context ]
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseFile(std::vector<std::string>&& args) {
|
2016-10-27 16:45:34 +02:00
|
|
|
if (args[2] != "r" && args[2] != "w" && args[2] != "rw") {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "file type must be 'r', 'w' or 'rw'";
|
2016-10-27 16:45:34 +02:00
|
|
|
}
|
|
|
|
if ((args[1][0] != '/') || (args[1].find("../") != std::string::npos)) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "file name must not be relative";
|
2016-10-27 16:45:34 +02:00
|
|
|
}
|
2018-10-17 20:11:23 +02:00
|
|
|
return AddDescriptor<FileInfo>(std::move(args));
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseUser(std::vector<std::string>&& args) {
|
2017-08-03 21:54:07 +02:00
|
|
|
auto uid = DecodeUid(args[1]);
|
|
|
|
if (!uid) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "Unable to find UID for '" << args[1] << "': " << uid.error();
|
2017-05-05 02:40:33 +02:00
|
|
|
}
|
2017-08-03 21:54:07 +02:00
|
|
|
uid_ = *uid;
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseWritepid(std::vector<std::string>&& args) {
|
|
|
|
args.erase(args.begin());
|
|
|
|
writepid_files_ = std::move(args);
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
|
|
|
|
2018-11-12 04:08:41 +01:00
|
|
|
Result<Success> Service::ParseUpdatable(std::vector<std::string>&& args) {
|
|
|
|
updatable_ = true;
|
|
|
|
return Success();
|
|
|
|
}
|
|
|
|
|
2016-06-29 20:32:49 +02:00
|
|
|
class Service::OptionParserMap : public KeywordMap<OptionParser> {
|
2017-04-20 01:18:50 +02:00
|
|
|
public:
|
|
|
|
OptionParserMap() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Map& map() const override;
|
2015-08-26 20:43:36 +02:00
|
|
|
};
|
|
|
|
|
2017-04-20 01:18:50 +02:00
|
|
|
const Service::OptionParserMap::Map& Service::OptionParserMap::map() const {
|
2015-08-26 20:43:36 +02:00
|
|
|
constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
|
2017-03-27 19:59:11 +02:00
|
|
|
// clang-format off
|
2016-06-29 20:32:49 +02:00
|
|
|
static const Map option_parsers = {
|
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
|
|
|
{"capabilities",
|
2019-02-06 19:45:56 +01:00
|
|
|
{0, kMax, &Service::ParseCapabilities}},
|
2017-03-27 19:59:11 +02:00
|
|
|
{"class", {1, kMax, &Service::ParseClass}},
|
2016-06-29 20:32:49 +02:00
|
|
|
{"console", {0, 1, &Service::ParseConsole}},
|
|
|
|
{"critical", {0, 0, &Service::ParseCritical}},
|
|
|
|
{"disabled", {0, 0, &Service::ParseDisabled}},
|
2018-04-21 01:18:12 +02:00
|
|
|
{"enter_namespace",
|
|
|
|
{2, 2, &Service::ParseEnterNamespace}},
|
2018-03-08 22:51:10 +01:00
|
|
|
{"file", {2, 2, &Service::ParseFile}},
|
2016-06-29 20:32:49 +02:00
|
|
|
{"group", {1, NR_SVC_SUPP_GIDS + 1, &Service::ParseGroup}},
|
2017-10-06 03:50:22 +02:00
|
|
|
{"interface", {2, 2, &Service::ParseInterface}},
|
2016-06-29 20:32:49 +02:00
|
|
|
{"ioprio", {2, 2, &Service::ParseIoprio}},
|
|
|
|
{"keycodes", {1, kMax, &Service::ParseKeycodes}},
|
2018-03-08 22:51:10 +01:00
|
|
|
{"memcg.limit_in_bytes",
|
|
|
|
{1, 1, &Service::ParseMemcgLimitInBytes}},
|
2018-10-30 23:49:33 +01:00
|
|
|
{"memcg.limit_percent",
|
|
|
|
{1, 1, &Service::ParseMemcgLimitPercent}},
|
|
|
|
{"memcg.limit_property",
|
|
|
|
{1, 1, &Service::ParseMemcgLimitProperty}},
|
2018-03-08 22:51:10 +01:00
|
|
|
{"memcg.soft_limit_in_bytes",
|
|
|
|
{1, 1, &Service::ParseMemcgSoftLimitInBytes}},
|
|
|
|
{"memcg.swappiness",
|
|
|
|
{1, 1, &Service::ParseMemcgSwappiness}},
|
|
|
|
{"namespace", {1, 2, &Service::ParseNamespace}},
|
2016-06-29 20:32:49 +02:00
|
|
|
{"oneshot", {0, 0, &Service::ParseOneshot}},
|
|
|
|
{"onrestart", {1, kMax, &Service::ParseOnrestart}},
|
2016-07-22 21:07:06 +02:00
|
|
|
{"oom_score_adjust",
|
|
|
|
{1, 1, &Service::ParseOomScoreAdjust}},
|
2018-03-08 22:51:10 +01:00
|
|
|
{"override", {0, 0, &Service::ParseOverride}},
|
|
|
|
{"priority", {1, 1, &Service::ParsePriority}},
|
2018-09-28 01:10:46 +02:00
|
|
|
{"restart_period",
|
|
|
|
{1, 1, &Service::ParseRestartPeriod}},
|
2017-08-25 19:39:25 +02:00
|
|
|
{"rlimit", {3, 3, &Service::ParseProcessRlimit}},
|
2016-06-29 20:32:49 +02:00
|
|
|
{"seclabel", {1, 1, &Service::ParseSeclabel}},
|
|
|
|
{"setenv", {2, 2, &Service::ParseSetenv}},
|
2017-07-05 20:38:44 +02:00
|
|
|
{"shutdown", {1, 1, &Service::ParseShutdown}},
|
2018-04-17 23:48:44 +02:00
|
|
|
{"sigstop", {0, 0, &Service::ParseSigstop}},
|
2016-06-29 20:32:49 +02:00
|
|
|
{"socket", {3, 6, &Service::ParseSocket}},
|
2018-09-28 01:10:46 +02:00
|
|
|
{"timeout_period",
|
|
|
|
{1, 1, &Service::ParseTimeoutPeriod}},
|
2018-11-12 04:08:41 +01:00
|
|
|
{"updatable", {0, 0, &Service::ParseUpdatable}},
|
2016-06-29 20:32:49 +02:00
|
|
|
{"user", {1, 1, &Service::ParseUser}},
|
|
|
|
{"writepid", {1, kMax, &Service::ParseWritepid}},
|
2015-08-26 20:43:36 +02:00
|
|
|
};
|
2017-03-27 19:59:11 +02:00
|
|
|
// clang-format on
|
2016-06-29 20:32:49 +02:00
|
|
|
return option_parsers;
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Result<Success> Service::ParseLine(std::vector<std::string>&& args) {
|
2016-06-29 20:32:49 +02:00
|
|
|
static const OptionParserMap parser_map;
|
2017-08-03 02:01:36 +02:00
|
|
|
auto parser = parser_map.FindFunction(args);
|
2015-08-26 20:43:36 +02:00
|
|
|
|
2017-08-23 01:13:59 +02:00
|
|
|
if (!parser) return parser.error();
|
2015-08-26 20:43:36 +02:00
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
return std::invoke(*parser, this, std::move(args));
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
|
|
|
|
2017-08-23 01:13:59 +02:00
|
|
|
Result<Success> Service::ExecStart() {
|
2018-11-12 04:08:41 +01:00
|
|
|
if (is_updatable() && !ServiceList::GetInstance().IsServicesUpdated()) {
|
|
|
|
// 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
|
|
|
|
2017-08-23 01:13:59 +02:00
|
|
|
if (auto result = Start(); !result) {
|
|
|
|
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;
|
|
|
|
|
2018-06-20 23:54:52 +02:00
|
|
|
LOG(INFO) << "SVC_EXEC service '" << name_ << "' pid " << pid_ << " (uid " << uid_ << " gid "
|
|
|
|
<< gid_ << "+" << supp_gids_.size() << " context "
|
|
|
|
<< (!seclabel_.empty() ? seclabel_ : "default") << ") started; waiting...";
|
2017-07-28 23:48:41 +02:00
|
|
|
|
2017-08-23 01:13:59 +02:00
|
|
|
return Success();
|
2017-03-28 01:27:30 +02:00
|
|
|
}
|
|
|
|
|
2017-08-23 01:13:59 +02:00
|
|
|
Result<Success> Service::Start() {
|
2018-11-12 04:08:41 +01:00
|
|
|
if (is_updatable() && !ServiceList::GetInstance().IsServicesUpdated()) {
|
|
|
|
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));
|
2015-07-31 21:45:25 +02: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.
|
|
|
|
flags_ &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START));
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2017-08-23 01:13:59 +02:00
|
|
|
// It is not an error to try to start a service that is already running.
|
|
|
|
return Success();
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool needs_console = (flags_ & SVC_CONSOLE);
|
2016-03-21 09:08:07 +01:00
|
|
|
if (needs_console) {
|
|
|
|
if (console_.empty()) {
|
|
|
|
console_ = default_console;
|
|
|
|
}
|
|
|
|
|
2016-12-21 00:52:15 +01:00
|
|
|
// Make sure that open call succeeds to ensure a console driver is
|
|
|
|
// properly registered for the device node
|
|
|
|
int console_fd = open(console_.c_str(), O_RDWR | O_CLOEXEC);
|
|
|
|
if (console_fd < 0) {
|
2016-03-21 09:08:07 +01:00
|
|
|
flags_ |= SVC_DISABLED;
|
2017-08-23 01:13:59 +02:00
|
|
|
return ErrnoError() << "Couldn't open console '" << console_ << "'";
|
2016-03-21 09:08:07 +01:00
|
|
|
}
|
2016-12-21 00:52:15 +01:00
|
|
|
close(console_fd);
|
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]);
|
2017-08-23 01:13:59 +02:00
|
|
|
if (!result) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
if (!IsRuntimeApexReady() && !pre_apexd_) {
|
|
|
|
// If this service is started before the runtime APEX gets 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.
|
|
|
|
pre_apexd_ = true;
|
|
|
|
}
|
|
|
|
|
2016-10-04 23:05:39 +02:00
|
|
|
LOG(INFO) << "starting service '" << name_ << "'...";
|
2015-07-31 21:45:25 +02:00
|
|
|
|
2016-04-22 00:35:09 +02:00
|
|
|
pid_t pid = -1;
|
|
|
|
if (namespace_flags_) {
|
2016-07-08 19:32:26 +02:00
|
|
|
pid = clone(nullptr, nullptr, namespace_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);
|
|
|
|
|
2018-04-21 01:18:12 +02:00
|
|
|
if (auto result = EnterNamespaces(); !result) {
|
|
|
|
LOG(FATAL) << "Service '" << name_ << "' could not enter namespaces: " << result.error();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
#if defined(__ANDROID__)
|
|
|
|
if (pre_apexd_) {
|
|
|
|
if (!SwitchToBootstrapMountNamespaceIfNeeded()) {
|
|
|
|
LOG(FATAL) << "Service '" << name_ << "' could not enter "
|
|
|
|
<< "into the bootstrap mount namespace";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-04-21 01:18:12 +02:00
|
|
|
if (namespace_flags_ & CLONE_NEWNS) {
|
|
|
|
if (auto result = SetUpMountNamespace(); !result) {
|
|
|
|
LOG(FATAL) << "Service '" << name_
|
|
|
|
<< "' could not set up mount namespace: " << result.error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-22 00:35:09 +02:00
|
|
|
if (namespace_flags_ & CLONE_NEWPID) {
|
|
|
|
// This will fork again to run an init process inside the PID
|
|
|
|
// namespace.
|
2018-04-21 01:18:12 +02:00
|
|
|
if (auto result = SetUpPidNamespace(); !result) {
|
|
|
|
LOG(FATAL) << "Service '" << name_
|
|
|
|
<< "' could not set up PID namespace: " << result.error();
|
|
|
|
}
|
2016-04-22 00:35:09 +02:00
|
|
|
}
|
|
|
|
|
2017-08-23 00:41:03 +02:00
|
|
|
for (const auto& [key, value] : environment_vars_) {
|
|
|
|
setenv(key.c_str(), value.c_str(), 1);
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 16:45:34 +02:00
|
|
|
std::for_each(descriptors_.begin(), descriptors_.end(),
|
|
|
|
std::bind(&DescriptorInfo::CreateAndPublish, std::placeholders::_1, scon));
|
2015-07-31 21:45:25 +02:00
|
|
|
|
2018-12-21 21:36:20 +01:00
|
|
|
// See if there were "writepid" instructions to write to files under cpuset path.
|
|
|
|
std::string cpuset_path;
|
|
|
|
if (CgroupGetControllerPath("cpuset", &cpuset_path)) {
|
|
|
|
auto cpuset_predicate = [&cpuset_path](const std::string& path) {
|
|
|
|
return StartsWith(path, cpuset_path + "/");
|
|
|
|
};
|
|
|
|
auto iter =
|
|
|
|
std::find_if(writepid_files_.begin(), writepid_files_.end(), cpuset_predicate);
|
|
|
|
if (iter == writepid_files_.end()) {
|
|
|
|
// There were no "writepid" instructions for cpusets, check if the system default
|
|
|
|
// cpuset is specified to be used for the process.
|
|
|
|
std::string default_cpuset = GetProperty("ro.cpuset.default", "");
|
|
|
|
if (!default_cpuset.empty()) {
|
|
|
|
// Make sure the cpuset name starts and ends with '/'.
|
|
|
|
// A single '/' means the 'root' cpuset.
|
|
|
|
if (default_cpuset.front() != '/') {
|
|
|
|
default_cpuset.insert(0, 1, '/');
|
|
|
|
}
|
|
|
|
if (default_cpuset.back() != '/') {
|
|
|
|
default_cpuset.push_back('/');
|
|
|
|
}
|
|
|
|
writepid_files_.push_back(
|
|
|
|
StringPrintf("%s%stasks", cpuset_path.c_str(), default_cpuset.c_str()));
|
2016-05-03 21:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-21 21:36:20 +01:00
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "cpuset cgroup controller is not mounted!";
|
2016-05-03 21:00:00 +02:00
|
|
|
}
|
2017-06-23 01:50:31 +02:00
|
|
|
std::string pid_str = std::to_string(getpid());
|
2015-07-31 21:45:25 +02:00
|
|
|
for (const auto& file : writepid_files_) {
|
2015-08-26 20:43:36 +02:00
|
|
|
if (!WriteStringToFile(pid_str, file)) {
|
2016-06-25 00:12:21 +02:00
|
|
|
PLOG(ERROR) << "couldn't write " << pid_str << " to " << file;
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioprio_class_ != IoSchedClass_NONE) {
|
|
|
|
if (android_set_ioprio(getpid(), ioprio_class_, ioprio_pri_)) {
|
2016-07-08 19:32:26 +02:00
|
|
|
PLOG(ERROR) << "failed to set pid " << getpid()
|
2016-06-25 00:12:21 +02:00
|
|
|
<< " ioprio=" << ioprio_class_ << "," << ioprio_pri_;
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needs_console) {
|
|
|
|
setsid();
|
|
|
|
OpenConsole();
|
|
|
|
} else {
|
|
|
|
ZapStdio();
|
|
|
|
}
|
|
|
|
|
2016-07-08 19:32:26 +02:00
|
|
|
// As requested, set our gid, supplemental gids, uid, context, and
|
|
|
|
// priority. Aborts on failure.
|
|
|
|
SetProcessAttributes();
|
2015-07-31 21:45:25 +02:00
|
|
|
|
2018-04-17 23:48:44 +02:00
|
|
|
if (!ExpandArgsAndExecv(args_, sigstop_)) {
|
2017-09-12 01:08:54 +02:00
|
|
|
PLOG(ERROR) << "cannot execve('" << args_[0] << "')";
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_exit(127);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-07-22 21:07:06 +02:00
|
|
|
if (oom_score_adjust_ != -1000) {
|
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)) {
|
|
|
|
PLOG(ERROR) << "couldn't write oom_score_adj: " << strerror(errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-10-30 23:49:33 +01:00
|
|
|
bool use_memcg = swappiness_ != -1 || soft_limit_in_bytes_ != -1 || limit_in_bytes_ != -1 ||
|
|
|
|
limit_percent_ != -1 || !limit_property_.empty();
|
|
|
|
errno = -createProcessGroup(uid_, pid_, use_memcg);
|
2016-06-15 23:49:57 +02:00
|
|
|
if (errno != 0) {
|
2016-07-08 19:32:26 +02:00
|
|
|
PLOG(ERROR) << "createProcessGroup(" << uid_ << ", " << pid_ << ") failed for service '"
|
|
|
|
<< name_ << "'";
|
2018-10-30 23:49:33 +01:00
|
|
|
} else if (use_memcg) {
|
2017-07-17 04:38:11 +02:00
|
|
|
if (swappiness_ != -1) {
|
|
|
|
if (!setProcessGroupSwappiness(uid_, pid_, swappiness_)) {
|
|
|
|
PLOG(ERROR) << "setProcessGroupSwappiness failed";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (soft_limit_in_bytes_ != -1) {
|
|
|
|
if (!setProcessGroupSoftLimit(uid_, pid_, soft_limit_in_bytes_)) {
|
|
|
|
PLOG(ERROR) << "setProcessGroupSoftLimit failed";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 23:49:33 +01:00
|
|
|
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)) {
|
|
|
|
if (!setProcessGroupLimit(uid_, pid_, computed_limit_in_bytes)) {
|
2017-07-17 04:38:11 +02:00
|
|
|
PLOG(ERROR) << "setProcessGroupLimit failed";
|
|
|
|
}
|
|
|
|
}
|
2016-06-15 23:49:57 +02:00
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
|
|
|
|
NotifyStateChange("running");
|
2017-08-23 01:13:59 +02:00
|
|
|
return Success();
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2017-08-23 01:13:59 +02:00
|
|
|
Result<Success> Service::StartIfNotDisabled() {
|
2015-07-31 21:45:25 +02:00
|
|
|
if (!(flags_ & SVC_DISABLED)) {
|
|
|
|
return Start();
|
|
|
|
} else {
|
|
|
|
flags_ |= SVC_DISABLED_START;
|
|
|
|
}
|
2017-08-23 01:13:59 +02:00
|
|
|
return Success();
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2017-08-23 01:13:59 +02:00
|
|
|
Result<Success> Service::Enable() {
|
2015-07-31 21:45:25 +02:00
|
|
|
flags_ &= ~(SVC_DISABLED | SVC_RC_DISABLED);
|
|
|
|
if (flags_ & SVC_DISABLED_START) {
|
|
|
|
return Start();
|
|
|
|
}
|
2017-08-23 01:13:59 +02:00
|
|
|
return Success();
|
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. */
|
2017-08-23 01:13:59 +02:00
|
|
|
if (auto result = Start(); !result) {
|
|
|
|
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.
|
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_) {
|
2016-06-15 23:49:57 +02:00
|
|
|
KillProcessGroup(SIGKILL);
|
2015-07-31 21:45:25 +02:00
|
|
|
NotifyStateChange("stopping");
|
|
|
|
} else {
|
|
|
|
NotifyStateChange("stopped");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::ZapStdio() const {
|
|
|
|
int fd;
|
|
|
|
fd = open("/dev/null", O_RDWR);
|
|
|
|
dup2(fd, 0);
|
|
|
|
dup2(fd, 1);
|
|
|
|
dup2(fd, 2);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Service::OpenConsole() const {
|
2016-03-21 09:08:07 +01:00
|
|
|
int fd = open(console_.c_str(), O_RDWR);
|
|
|
|
if (fd == -1) fd = open("/dev/null", O_RDWR);
|
2015-07-31 21:45:25 +02:00
|
|
|
ioctl(fd, TIOCSCTTY, 0);
|
|
|
|
dup2(fd, 0);
|
|
|
|
dup2(fd, 1);
|
|
|
|
dup2(fd, 2);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2017-07-28 01:20:58 +02:00
|
|
|
ServiceList::ServiceList() {}
|
2015-07-31 21:45:25 +02:00
|
|
|
|
2017-07-28 01:20:58 +02:00
|
|
|
ServiceList& ServiceList::GetInstance() {
|
|
|
|
static ServiceList instance;
|
2015-07-31 21:45:25 +02:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2017-07-28 01:20:58 +02:00
|
|
|
void ServiceList::AddService(std::unique_ptr<Service> service) {
|
2015-08-26 20:43:36 +02:00
|
|
|
services_.emplace_back(std::move(service));
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2017-07-28 23:48:41 +02:00
|
|
|
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) {
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(ERROR) << "exec called with too many supplementary group ids";
|
2015-07-31 21:45:25 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (command_arg >= args.size()) {
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(ERROR) << "exec called without command";
|
2015-07-31 21:45:25 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
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]);
|
|
|
|
if (!uid) {
|
|
|
|
LOG(ERROR) << "Unable to decode UID for '" << args[2] << "': " << uid.error();
|
2017-05-05 02:40:33 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
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]);
|
|
|
|
if (!gid) {
|
|
|
|
LOG(ERROR) << "Unable to decode GID for '" << args[3] << "': " << gid.error();
|
2017-05-05 02:40:33 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
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]);
|
|
|
|
if (!supp_gid) {
|
|
|
|
LOG(ERROR) << "Unable to decode GID for '" << args[4 + i]
|
|
|
|
<< "': " << supp_gid.error();
|
2017-05-05 02:40:33 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
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,
|
|
|
|
nullptr, str_args);
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2017-07-27 01:09:09 +02:00
|
|
|
// Shutdown services in the opposite order that they were started.
|
2017-07-28 01:20:58 +02:00
|
|
|
const std::vector<Service*> ServiceList::services_in_shutdown_order() const {
|
2017-07-27 01:09:09 +02:00
|
|
|
std::vector<Service*> shutdown_services;
|
|
|
|
for (const auto& service : services_) {
|
|
|
|
if (service->start_order() > 0) shutdown_services.emplace_back(service.get());
|
|
|
|
}
|
|
|
|
std::sort(shutdown_services.begin(), shutdown_services.end(),
|
|
|
|
[](const auto& a, const auto& b) { return a->start_order() > b->start_order(); });
|
2017-07-28 01:20:58 +02:00
|
|
|
return shutdown_services;
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
|
|
|
|
2017-07-28 01:20:58 +02:00
|
|
|
void ServiceList::RemoveService(const Service& svc) {
|
2015-07-31 21:45:25 +02:00
|
|
|
auto svc_it = std::find_if(services_.begin(), services_.end(),
|
|
|
|
[&svc] (const std::unique_ptr<Service>& s) {
|
|
|
|
return svc.name() == s->name();
|
|
|
|
});
|
|
|
|
if (svc_it == services_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
services_.erase(svc_it);
|
|
|
|
}
|
|
|
|
|
2017-07-28 01:20:58 +02:00
|
|
|
void ServiceList::DumpState() const {
|
2015-08-26 20:43:36 +02:00
|
|
|
for (const auto& s : services_) {
|
|
|
|
s->DumpState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-12 04:08:41 +01:00
|
|
|
void ServiceList::MarkServicesUpdate() {
|
|
|
|
services_update_finished_ = true;
|
|
|
|
|
|
|
|
// start the delayed services
|
|
|
|
for (const auto& name : delayed_service_names_) {
|
|
|
|
Service* service = FindService(name);
|
|
|
|
if (service == nullptr) {
|
|
|
|
LOG(ERROR) << "delayed service '" << name << "' could not be found.";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (auto result = service->Start(); !result) {
|
|
|
|
LOG(ERROR) << result.error_string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delayed_service_names_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServiceList::DelayService(const Service& service) {
|
|
|
|
if (services_update_finished_) {
|
|
|
|
LOG(ERROR) << "Cannot delay the start of service '" << service.name()
|
|
|
|
<< "' because all services are already updated. Ignoring.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delayed_service_names_.emplace_back(service.name());
|
|
|
|
}
|
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
Result<Success> ServiceParser::ParseSection(std::vector<std::string>&& args,
|
|
|
|
const std::string& filename, int line) {
|
2015-08-26 20:43:36 +02:00
|
|
|
if (args.size() < 3) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "services must have a name and a program";
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& name = args[1];
|
|
|
|
if (!IsValidName(name)) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "invalid service name '" << name << "'";
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
|
|
|
|
2018-11-12 04:08:41 +01:00
|
|
|
filename_ = filename;
|
|
|
|
|
2017-09-13 00:58:47 +02:00
|
|
|
Subcontext* restart_action_subcontext = nullptr;
|
|
|
|
if (subcontexts_) {
|
|
|
|
for (auto& subcontext : *subcontexts_) {
|
2017-12-20 18:41:00 +01:00
|
|
|
if (StartsWith(filename, subcontext.path_prefix())) {
|
2017-09-13 00:58:47 +02:00
|
|
|
restart_action_subcontext = &subcontext;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
std::vector<std::string> str_args(args.begin() + 2, args.end());
|
2018-08-01 22:41:12 +02:00
|
|
|
|
|
|
|
if (SelinuxGetVendorAndroidVersion() <= __ANDROID_API_P__) {
|
|
|
|
if (str_args[0] == "/sbin/watchdogd") {
|
|
|
|
str_args[0] = "/system/bin/watchdogd";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 00:58:47 +02:00
|
|
|
service_ = std::make_unique<Service>(name, restart_action_subcontext, str_args);
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
Result<Success> ServiceParser::ParseLineSection(std::vector<std::string>&& args, int line) {
|
|
|
|
return service_ ? service_->ParseLine(std::move(args)) : Success();
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
|
|
|
|
2017-11-10 23:20:47 +01:00
|
|
|
Result<Success> ServiceParser::EndSection() {
|
2015-08-26 20:43:36 +02:00
|
|
|
if (service_) {
|
2017-11-10 23:43:58 +01:00
|
|
|
Service* old_service = service_list_->FindService(service_->name());
|
|
|
|
if (old_service) {
|
2017-11-14 00:31:54 +01:00
|
|
|
if (!service_->is_override()) {
|
|
|
|
return Error() << "ignored duplicate definition of service '" << service_->name()
|
|
|
|
<< "'";
|
|
|
|
}
|
|
|
|
|
2018-11-12 04:08:41 +01:00
|
|
|
if (StartsWith(filename_, "/apex/") && !old_service->is_updatable()) {
|
|
|
|
return Error() << "cannot update a non-updatable service '" << service_->name()
|
|
|
|
<< "' with a config in APEX";
|
|
|
|
}
|
|
|
|
|
2017-11-14 00:31:54 +01:00
|
|
|
service_list_->RemoveService(*old_service);
|
|
|
|
old_service = nullptr;
|
2017-11-10 23:43:58 +01:00
|
|
|
}
|
|
|
|
|
2017-07-28 01:20:58 +02:00
|
|
|
service_list_->AddService(std::move(service_));
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
2017-11-10 23:20:47 +01:00
|
|
|
|
|
|
|
return Success();
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ServiceParser::IsValidName(const std::string& name) const {
|
2017-02-28 18:54:36 +01:00
|
|
|
// Property names can be any length, but may only contain certain characters.
|
|
|
|
// Property values can contain any characters, but may only be a certain length.
|
|
|
|
// (The latter restriction is needed because `start` and `stop` work by writing
|
|
|
|
// the service name to the "ctl.start" and "ctl.stop" properties.)
|
2018-02-14 01:50:08 +01:00
|
|
|
return IsLegalPropertyName("init.svc." + name) && name.size() <= PROP_VALUE_MAX;
|
2015-07-31 21:45:25 +02:00
|
|
|
}
|
2017-06-22 21:53:17 +02:00
|
|
|
|
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|