Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "service.h" |
| 18 | |
| 19 | #include <fcntl.h> |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 20 | #include <inttypes.h> |
Mark Salyzyn | eca2507 | 2018-05-16 15:10:24 -0700 | [diff] [blame] | 21 | #include <linux/input.h> |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 22 | #include <linux/securebits.h> |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 23 | #include <sched.h> |
| 24 | #include <sys/mount.h> |
| 25 | #include <sys/prctl.h> |
Vitalii Tomkiv | 081705c | 2016-05-18 17:36:30 -0700 | [diff] [blame] | 26 | #include <sys/resource.h> |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 27 | #include <sys/stat.h> |
Vitalii Tomkiv | 081705c | 2016-05-18 17:36:30 -0700 | [diff] [blame] | 28 | #include <sys/time.h> |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 29 | #include <sys/wait.h> |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 30 | #include <termios.h> |
Dan Albert | af9ba4d | 2015-08-11 16:37:04 -0700 | [diff] [blame] | 31 | #include <unistd.h> |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 32 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 33 | #include <android-base/file.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 34 | #include <android-base/logging.h> |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 35 | #include <android-base/parseint.h> |
Elliott Hughes | dc80312 | 2018-05-24 18:00:39 -0700 | [diff] [blame] | 36 | #include <android-base/properties.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 37 | #include <android-base/stringprintf.h> |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 38 | #include <android-base/strings.h> |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 39 | #include <android-base/unique_fd.h> |
Steven Moreland | e055d73 | 2017-10-05 18:50:22 -0700 | [diff] [blame] | 40 | #include <hidl-util/FQName.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 41 | #include <processgroup/processgroup.h> |
| 42 | #include <selinux/selinux.h> |
Vitalii Tomkiv | 081705c | 2016-05-18 17:36:30 -0700 | [diff] [blame] | 43 | #include <system/thread_defs.h> |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 44 | |
Tom Cherry | 7ac013d | 2017-08-25 10:39:25 -0700 | [diff] [blame] | 45 | #include "rlimit_parser.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 46 | #include "util.h" |
| 47 | |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 48 | #if defined(__ANDROID__) |
Jiyong Park | d7f7c20 | 2019-05-10 21:12:15 +0900 | [diff] [blame^] | 49 | #include <ApexProperties.sysprop.h> |
Tom Cherry | 40acb37 | 2018-08-01 13:41:12 -0700 | [diff] [blame] | 50 | #include <android/api-level.h> |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 51 | #include <sys/system_properties.h> |
| 52 | |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 53 | #include "init.h" |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 54 | #include "mount_namespace.h" |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 55 | #include "property_service.h" |
Tom Cherry | 40acb37 | 2018-08-01 13:41:12 -0700 | [diff] [blame] | 56 | #include "selinux.h" |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 57 | #else |
| 58 | #include "host_init_stubs.h" |
| 59 | #endif |
| 60 | |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 61 | using android::base::boot_clock; |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 62 | using android::base::GetProperty; |
| 63 | using android::base::Join; |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 64 | using android::base::ParseInt; |
Tom Cherry | 7916684 | 2018-10-16 10:58:06 -0700 | [diff] [blame] | 65 | using android::base::Split; |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 66 | using android::base::StartsWith; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 67 | using android::base::StringPrintf; |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 68 | using android::base::unique_fd; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 69 | using android::base::WriteStringToFile; |
| 70 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 71 | namespace android { |
| 72 | namespace init { |
| 73 | |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 74 | static Result<std::string> ComputeContextFromExecutable(const std::string& service_path) { |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 75 | std::string computed_context; |
| 76 | |
| 77 | char* raw_con = nullptr; |
| 78 | char* raw_filecon = nullptr; |
| 79 | |
| 80 | if (getcon(&raw_con) == -1) { |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 81 | return Error() << "Could not get security context"; |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 82 | } |
| 83 | std::unique_ptr<char> mycon(raw_con); |
| 84 | |
| 85 | if (getfilecon(service_path.c_str(), &raw_filecon) == -1) { |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 86 | return Error() << "Could not get file context"; |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 87 | } |
| 88 | std::unique_ptr<char> filecon(raw_filecon); |
| 89 | |
| 90 | char* new_con = nullptr; |
| 91 | int rc = security_compute_create(mycon.get(), filecon.get(), |
| 92 | string_to_security_class("process"), &new_con); |
| 93 | if (rc == 0) { |
| 94 | computed_context = new_con; |
| 95 | free(new_con); |
| 96 | } |
| 97 | if (rc == 0 && computed_context == mycon.get()) { |
Nick Kralevich | 1ea19eb | 2017-08-25 12:08:57 -0700 | [diff] [blame] | 98 | return Error() << "File " << service_path << "(labeled \"" << filecon.get() |
| 99 | << "\") has incorrect label or no domain transition from " << mycon.get() |
| 100 | << " to another SELinux domain defined. Have you configured your " |
| 101 | "service correctly? https://source.android.com/security/selinux/" |
| 102 | "device-policy#label_new_services_and_address_denials"; |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 103 | } |
| 104 | if (rc < 0) { |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 105 | return Error() << "Could not get process context"; |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 106 | } |
| 107 | return computed_context; |
| 108 | } |
| 109 | |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 110 | Result<Success> Service::SetUpMountNamespace() const { |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 111 | constexpr unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID; |
| 112 | |
Joe Tanen | 4bfdcb3 | 2017-10-25 08:07:26 -0400 | [diff] [blame] | 113 | // Recursively remount / as slave like zygote does so unmounting and mounting /proc |
| 114 | // doesn't interfere with the parent namespace's /proc mount. This will also |
| 115 | // prevent any other mounts/unmounts initiated by the service from interfering |
| 116 | // with the parent namespace but will still allow mount events from the parent |
| 117 | // namespace to propagate to the child. |
| 118 | if (mount("rootfs", "/", nullptr, (MS_SLAVE | MS_REC), nullptr) == -1) { |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 119 | return ErrnoError() << "Could not remount(/) recursively as slave"; |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 122 | // umount() then mount() /proc and/or /sys |
| 123 | // Note that it is not sufficient to mount with MS_REMOUNT. |
| 124 | if (namespace_flags_ & CLONE_NEWPID) { |
| 125 | if (umount("/proc") == -1) { |
| 126 | return ErrnoError() << "Could not umount(/proc)"; |
| 127 | } |
| 128 | if (mount("", "/proc", "proc", kSafeFlags, "") == -1) { |
| 129 | return ErrnoError() << "Could not mount(/proc)"; |
| 130 | } |
| 131 | } |
| 132 | bool remount_sys = std::any_of(namespaces_to_enter_.begin(), namespaces_to_enter_.end(), |
| 133 | [](const auto& entry) { return entry.first == CLONE_NEWNET; }); |
| 134 | if (remount_sys) { |
| 135 | if (umount2("/sys", MNT_DETACH) == -1) { |
| 136 | return ErrnoError() << "Could not umount(/sys)"; |
| 137 | } |
Tom Cherry | 9bb0a4d | 2018-07-13 11:07:11 -0700 | [diff] [blame] | 138 | if (mount("", "/sys", "sysfs", kSafeFlags, "") == -1) { |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 139 | return ErrnoError() << "Could not mount(/sys)"; |
| 140 | } |
| 141 | } |
| 142 | return Success(); |
| 143 | } |
| 144 | |
| 145 | Result<Success> Service::SetUpPidNamespace() const { |
| 146 | if (prctl(PR_SET_NAME, name_.c_str()) == -1) { |
| 147 | return ErrnoError() << "Could not set name"; |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | pid_t child_pid = fork(); |
| 151 | if (child_pid == -1) { |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 152 | return ErrnoError() << "Could not fork init inside the PID namespace"; |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | if (child_pid > 0) { |
| 156 | // So that we exit with the right status. |
| 157 | static int init_exitstatus = 0; |
| 158 | signal(SIGTERM, [](int) { _exit(init_exitstatus); }); |
| 159 | |
| 160 | pid_t waited_pid; |
| 161 | int status; |
| 162 | while ((waited_pid = wait(&status)) > 0) { |
| 163 | // This loop will end when there are no processes left inside the |
| 164 | // PID namespace or when the init process inside the PID namespace |
| 165 | // gets a signal. |
| 166 | if (waited_pid == child_pid) { |
| 167 | init_exitstatus = status; |
| 168 | } |
| 169 | } |
| 170 | if (!WIFEXITED(init_exitstatus)) { |
| 171 | _exit(EXIT_FAILURE); |
| 172 | } |
| 173 | _exit(WEXITSTATUS(init_exitstatus)); |
| 174 | } |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 175 | return Success(); |
| 176 | } |
| 177 | |
| 178 | Result<Success> Service::EnterNamespaces() const { |
| 179 | for (const auto& [nstype, path] : namespaces_to_enter_) { |
| 180 | auto fd = unique_fd{open(path.c_str(), O_RDONLY | O_CLOEXEC)}; |
Bernie Innocenti | 7cb72c9 | 2019-03-28 15:32:37 +0900 | [diff] [blame] | 181 | if (fd == -1) { |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 182 | return ErrnoError() << "Could not open namespace at " << path; |
| 183 | } |
| 184 | if (setns(fd, nstype) == -1) { |
| 185 | return ErrnoError() << "Could not setns() namespace at " << path; |
| 186 | } |
| 187 | } |
| 188 | return Success(); |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Tom Cherry | 8f38048 | 2018-04-17 14:48:44 -0700 | [diff] [blame] | 191 | static bool ExpandArgsAndExecv(const std::vector<std::string>& args, bool sigstop) { |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 192 | std::vector<std::string> expanded_args; |
Tom Cherry | 5e405ca | 2017-09-11 16:08:54 -0700 | [diff] [blame] | 193 | std::vector<char*> c_strings; |
| 194 | |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 195 | expanded_args.resize(args.size()); |
Tom Cherry | 5e405ca | 2017-09-11 16:08:54 -0700 | [diff] [blame] | 196 | c_strings.push_back(const_cast<char*>(args[0].data())); |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 197 | for (std::size_t i = 1; i < args.size(); ++i) { |
| 198 | if (!expand_props(args[i], &expanded_args[i])) { |
| 199 | LOG(FATAL) << args[0] << ": cannot expand '" << args[i] << "'"; |
| 200 | } |
Tom Cherry | 5e405ca | 2017-09-11 16:08:54 -0700 | [diff] [blame] | 201 | c_strings.push_back(expanded_args[i].data()); |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 202 | } |
Tom Cherry | 5e405ca | 2017-09-11 16:08:54 -0700 | [diff] [blame] | 203 | c_strings.push_back(nullptr); |
| 204 | |
Tom Cherry | 8f38048 | 2018-04-17 14:48:44 -0700 | [diff] [blame] | 205 | if (sigstop) { |
| 206 | kill(getpid(), SIGSTOP); |
| 207 | } |
| 208 | |
Tom Cherry | 5e405ca | 2017-09-11 16:08:54 -0700 | [diff] [blame] | 209 | return execv(c_strings[0], c_strings.data()) == 0; |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 210 | } |
| 211 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 212 | static bool IsRuntimeApexReady() { |
| 213 | struct stat buf; |
| 214 | return stat("/apex/com.android.runtime/", &buf) == 0; |
| 215 | } |
| 216 | |
Tom Cherry | 5938379 | 2017-07-26 16:09:09 -0700 | [diff] [blame] | 217 | unsigned long Service::next_start_order_ = 1; |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 218 | bool Service::is_exec_service_running_ = false; |
Tom Cherry | 5938379 | 2017-07-26 16:09:09 -0700 | [diff] [blame] | 219 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 220 | Service::Service(const std::string& name, Subcontext* subcontext_for_restart_commands, |
| 221 | const std::vector<std::string>& args) |
Tom Cherry | 1cd082d | 2019-02-06 10:45:56 -0800 | [diff] [blame] | 222 | : Service(name, 0, 0, 0, {}, 0, "", subcontext_for_restart_commands, args) {} |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 223 | |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 224 | Service::Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid, |
Tom Cherry | 1cd082d | 2019-02-06 10:45:56 -0800 | [diff] [blame] | 225 | const std::vector<gid_t>& supp_gids, unsigned namespace_flags, |
| 226 | const std::string& seclabel, Subcontext* subcontext_for_restart_commands, |
| 227 | const std::vector<std::string>& args) |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 228 | : name_(name), |
| 229 | classnames_({"default"}), |
| 230 | flags_(flags), |
| 231 | pid_(0), |
| 232 | crash_count_(0), |
| 233 | uid_(uid), |
| 234 | gid_(gid), |
| 235 | supp_gids_(supp_gids), |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 236 | namespace_flags_(namespace_flags), |
| 237 | seclabel_(seclabel), |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 238 | onrestart_(false, subcontext_for_restart_commands, "<Service '" + name + "' onrestart>", 0, |
| 239 | "onrestart", {}), |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 240 | ioprio_class_(IoSchedClass_NONE), |
| 241 | ioprio_pri_(0), |
| 242 | priority_(0), |
| 243 | oom_score_adjust_(-1000), |
Tom Cherry | 5938379 | 2017-07-26 16:09:09 -0700 | [diff] [blame] | 244 | start_order_(0), |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 245 | args_(args) {} |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 246 | |
| 247 | void Service::NotifyStateChange(const std::string& new_state) const { |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 248 | if ((flags_ & SVC_TEMPORARY) != 0) { |
| 249 | // Services created by 'exec' are temporary and don't have properties tracking their state. |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 250 | return; |
| 251 | } |
| 252 | |
Tom Cherry | 1c3a53f | 2017-06-22 16:50:31 -0700 | [diff] [blame] | 253 | std::string prop_name = "init.svc." + name_; |
| 254 | property_set(prop_name, new_state); |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 255 | |
| 256 | if (new_state == "running") { |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 257 | uint64_t start_ns = time_started_.time_since_epoch().count(); |
Tom Cherry | fed3373 | 2017-08-18 10:47:46 -0700 | [diff] [blame] | 258 | std::string boottime_property = "ro.boottime." + name_; |
| 259 | if (GetProperty(boottime_property, "").empty()) { |
| 260 | property_set(boottime_property, std::to_string(start_ns)); |
| 261 | } |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 262 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 265 | void Service::KillProcessGroup(int signal) { |
Tom Cherry | 33838b1 | 2017-05-04 11:32:36 -0700 | [diff] [blame] | 266 | // If we've already seen a successful result from killProcessGroup*(), then we have removed |
| 267 | // the cgroup already and calling these functions a second time will simply result in an error. |
| 268 | // This is true regardless of which signal was sent. |
| 269 | // These functions handle their own logging, so no additional logging is needed. |
| 270 | if (!process_cgroup_empty_) { |
| 271 | LOG(INFO) << "Sending signal " << signal << " to service '" << name_ << "' (pid " << pid_ |
| 272 | << ") process group..."; |
| 273 | int r; |
| 274 | if (signal == SIGTERM) { |
| 275 | r = killProcessGroupOnce(uid_, pid_, signal); |
| 276 | } else { |
| 277 | r = killProcessGroup(uid_, pid_, signal); |
| 278 | } |
| 279 | |
| 280 | if (r == 0) process_cgroup_empty_ = true; |
| 281 | } |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 284 | void Service::SetProcessAttributes() { |
Tom Cherry | 7ac013d | 2017-08-25 10:39:25 -0700 | [diff] [blame] | 285 | for (const auto& rlimit : rlimits_) { |
| 286 | if (setrlimit(rlimit.first, &rlimit.second) == -1) { |
| 287 | LOG(FATAL) << StringPrintf("setrlimit(%d, {rlim_cur=%ld, rlim_max=%ld}) failed", |
| 288 | rlimit.first, rlimit.second.rlim_cur, rlimit.second.rlim_max); |
| 289 | } |
| 290 | } |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 291 | // Keep capabilites on uid change. |
Tom Cherry | 1cd082d | 2019-02-06 10:45:56 -0800 | [diff] [blame] | 292 | if (capabilities_ && uid_) { |
Luis Hector Chavez | f596551 | 2017-06-30 14:04:20 -0700 | [diff] [blame] | 293 | // If Android is running in a container, some securebits might already |
| 294 | // be locked, so don't change those. |
Ben Fennema | a724360 | 2017-07-25 14:37:21 -0700 | [diff] [blame] | 295 | unsigned long securebits = prctl(PR_GET_SECUREBITS); |
| 296 | if (securebits == -1UL) { |
Luis Hector Chavez | f596551 | 2017-06-30 14:04:20 -0700 | [diff] [blame] | 297 | PLOG(FATAL) << "prctl(PR_GET_SECUREBITS) failed for " << name_; |
| 298 | } |
| 299 | securebits |= SECBIT_KEEP_CAPS | SECBIT_KEEP_CAPS_LOCKED; |
| 300 | if (prctl(PR_SET_SECUREBITS, securebits) != 0) { |
| 301 | PLOG(FATAL) << "prctl(PR_SET_SECUREBITS) failed for " << name_; |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
Elliott Hughes | e18e7e5 | 2016-07-25 18:18:16 -0700 | [diff] [blame] | 305 | // TODO: work out why this fails for `console` then upgrade to FATAL. |
| 306 | if (setpgid(0, getpid()) == -1) PLOG(ERROR) << "setpgid failed for " << name_; |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 307 | |
| 308 | if (gid_) { |
| 309 | if (setgid(gid_) != 0) { |
Elliott Hughes | e18e7e5 | 2016-07-25 18:18:16 -0700 | [diff] [blame] | 310 | PLOG(FATAL) << "setgid failed for " << name_; |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 311 | } |
| 312 | } |
Nick Kralevich | 80960d2 | 2016-10-29 12:20:00 -0700 | [diff] [blame] | 313 | if (setgroups(supp_gids_.size(), &supp_gids_[0]) != 0) { |
| 314 | PLOG(FATAL) << "setgroups failed for " << name_; |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 315 | } |
| 316 | if (uid_) { |
| 317 | if (setuid(uid_) != 0) { |
Elliott Hughes | e18e7e5 | 2016-07-25 18:18:16 -0700 | [diff] [blame] | 318 | PLOG(FATAL) << "setuid failed for " << name_; |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | if (!seclabel_.empty()) { |
| 322 | if (setexeccon(seclabel_.c_str()) < 0) { |
Elliott Hughes | e18e7e5 | 2016-07-25 18:18:16 -0700 | [diff] [blame] | 323 | PLOG(FATAL) << "cannot setexeccon('" << seclabel_ << "') for " << name_; |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | if (priority_ != 0) { |
| 327 | if (setpriority(PRIO_PROCESS, 0, priority_) != 0) { |
Elliott Hughes | e18e7e5 | 2016-07-25 18:18:16 -0700 | [diff] [blame] | 328 | PLOG(FATAL) << "setpriority failed for " << name_; |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 329 | } |
| 330 | } |
Tom Cherry | 1cd082d | 2019-02-06 10:45:56 -0800 | [diff] [blame] | 331 | if (capabilities_) { |
| 332 | if (!SetCapsForExec(*capabilities_)) { |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 333 | LOG(FATAL) << "cannot set capabilities for " << name_; |
| 334 | } |
Luis Hector Chavez | 94fb5b0 | 2017-11-16 15:52:00 -0800 | [diff] [blame] | 335 | } else if (uid_) { |
| 336 | // Inheritable caps can be non-zero when running in a container. |
| 337 | if (!DropInheritableCaps()) { |
| 338 | LOG(FATAL) << "cannot drop inheritable caps for " << name_; |
| 339 | } |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 340 | } |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 341 | } |
| 342 | |
Paul Crowley | c73b215 | 2018-04-13 17:38:57 +0000 | [diff] [blame] | 343 | void Service::Reap(const siginfo_t& siginfo) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 344 | if (!(flags_ & SVC_ONESHOT) || (flags_ & SVC_RESTART)) { |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 345 | KillProcessGroup(SIGKILL); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 348 | // Remove any descriptor resources we may have created. |
| 349 | std::for_each(descriptors_.begin(), descriptors_.end(), |
| 350 | std::bind(&DescriptorInfo::Clean, std::placeholders::_1)); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 351 | |
Paul Crowley | c73b215 | 2018-04-13 17:38:57 +0000 | [diff] [blame] | 352 | for (const auto& f : reap_callbacks_) { |
| 353 | f(siginfo); |
| 354 | } |
| 355 | |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 356 | if (flags_ & SVC_EXEC) UnSetExec(); |
| 357 | |
| 358 | if (flags_ & SVC_TEMPORARY) return; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 359 | |
| 360 | pid_ = 0; |
| 361 | flags_ &= (~SVC_RUNNING); |
Tom Cherry | 5938379 | 2017-07-26 16:09:09 -0700 | [diff] [blame] | 362 | start_order_ = 0; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 363 | |
| 364 | // Oneshot processes go into the disabled state on exit, |
| 365 | // except when manually restarted. |
Martijn Coenen | 70788f9 | 2019-04-23 16:26:01 +0200 | [diff] [blame] | 366 | if ((flags_ & SVC_ONESHOT) && !(flags_ & SVC_RESTART) && !(flags_ & SVC_RESET)) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 367 | flags_ |= SVC_DISABLED; |
| 368 | } |
| 369 | |
| 370 | // Disabled and reset processes do not get restarted automatically. |
| 371 | if (flags_ & (SVC_DISABLED | SVC_RESET)) { |
| 372 | NotifyStateChange("stopped"); |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 373 | return; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Jiyong Park | d7f7c20 | 2019-05-10 21:12:15 +0900 | [diff] [blame^] | 376 | #if defined(__ANDROID__) |
| 377 | static bool is_apex_updatable = android::sysprop::ApexProperties::updatable().value_or(false); |
| 378 | #else |
| 379 | static bool is_apex_updatable = false; |
| 380 | #endif |
| 381 | const bool is_process_updatable = !pre_apexd_ && is_apex_updatable; |
| 382 | |
Zimuzo | 88de80f | 2019-04-27 21:10:35 +0100 | [diff] [blame] | 383 | // If we crash > 4 times in 4 minutes or before boot_completed, |
| 384 | // reboot into bootloader or set crashing property |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 385 | boot_clock::time_point now = boot_clock::now(); |
Jiyong Park | d7f7c20 | 2019-05-10 21:12:15 +0900 | [diff] [blame^] | 386 | if (((flags_ & SVC_CRITICAL) || is_process_updatable) && !(flags_ & SVC_RESTART)) { |
Zimuzo | 88de80f | 2019-04-27 21:10:35 +0100 | [diff] [blame] | 387 | bool boot_completed = android::base::GetBoolProperty("sys.boot_completed", false); |
| 388 | if (now < time_crashed_ + 4min || !boot_completed) { |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 389 | if (++crash_count_ > 4) { |
Zimuzo | c55a8c6 | 2019-01-07 10:19:02 +0000 | [diff] [blame] | 390 | if (flags_ & SVC_CRITICAL) { |
| 391 | // Aborts into bootloader |
Zimuzo | 88de80f | 2019-04-27 21:10:35 +0100 | [diff] [blame] | 392 | LOG(FATAL) << "critical process '" << name_ << "' exited 4 times " |
| 393 | << (boot_completed ? "in 4 minutes" : "before boot completed"); |
Zimuzo | c55a8c6 | 2019-01-07 10:19:02 +0000 | [diff] [blame] | 394 | } else { |
Zimuzo | 88de80f | 2019-04-27 21:10:35 +0100 | [diff] [blame] | 395 | LOG(ERROR) << "updatable process '" << name_ << "' exited 4 times " |
| 396 | << (boot_completed ? "in 4 minutes" : "before boot completed"); |
Zimuzo | c55a8c6 | 2019-01-07 10:19:02 +0000 | [diff] [blame] | 397 | // Notifies update_verifier and apexd |
| 398 | property_set("ro.init.updatable_crashing", "1"); |
| 399 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 400 | } |
| 401 | } else { |
| 402 | time_crashed_ = now; |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 403 | crash_count_ = 1; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
| 407 | flags_ &= (~SVC_RESTART); |
| 408 | flags_ |= SVC_RESTARTING; |
| 409 | |
| 410 | // Execute all onrestart commands for this service. |
| 411 | onrestart_.ExecuteAllCommands(); |
| 412 | |
| 413 | NotifyStateChange("restarting"); |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 414 | return; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | void Service::DumpState() const { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 418 | LOG(INFO) << "service " << name_; |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 419 | LOG(INFO) << " class '" << Join(classnames_, " ") << "'"; |
| 420 | LOG(INFO) << " exec " << Join(args_, " "); |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 421 | std::for_each(descriptors_.begin(), descriptors_.end(), |
| 422 | [] (const auto& info) { LOG(INFO) << *info; }); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 425 | Result<Success> Service::ParseCapabilities(std::vector<std::string>&& args) { |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 426 | capabilities_ = 0; |
| 427 | |
Jorge Lucangeli Obes | f3f824e | 2016-12-15 12:13:38 -0500 | [diff] [blame] | 428 | if (!CapAmbientSupported()) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 429 | return Error() |
| 430 | << "capabilities requested but the kernel does not support ambient capabilities"; |
Jorge Lucangeli Obes | f3f824e | 2016-12-15 12:13:38 -0500 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | unsigned int last_valid_cap = GetLastValidCap(); |
Tom Cherry | 1cd082d | 2019-02-06 10:45:56 -0800 | [diff] [blame] | 434 | if (last_valid_cap >= capabilities_->size()) { |
Jorge Lucangeli Obes | f3f824e | 2016-12-15 12:13:38 -0500 | [diff] [blame] | 435 | LOG(WARNING) << "last valid run-time capability is larger than CAP_LAST_CAP"; |
| 436 | } |
| 437 | |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 438 | for (size_t i = 1; i < args.size(); i++) { |
| 439 | const std::string& arg = args[i]; |
Jorge Lucangeli Obes | f3f824e | 2016-12-15 12:13:38 -0500 | [diff] [blame] | 440 | int res = LookupCap(arg); |
| 441 | if (res < 0) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 442 | return Error() << StringPrintf("invalid capability '%s'", arg.c_str()); |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 443 | } |
Jorge Lucangeli Obes | f3f824e | 2016-12-15 12:13:38 -0500 | [diff] [blame] | 444 | unsigned int cap = static_cast<unsigned int>(res); // |res| is >= 0. |
| 445 | if (cap > last_valid_cap) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 446 | return Error() << StringPrintf("capability '%s' not supported by the kernel", |
| 447 | arg.c_str()); |
Jorge Lucangeli Obes | f3f824e | 2016-12-15 12:13:38 -0500 | [diff] [blame] | 448 | } |
Tom Cherry | 1cd082d | 2019-02-06 10:45:56 -0800 | [diff] [blame] | 449 | (*capabilities_)[cap] = true; |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 450 | } |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 451 | return Success(); |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 452 | } |
| 453 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 454 | Result<Success> Service::ParseClass(std::vector<std::string>&& args) { |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 455 | classnames_ = std::set<std::string>(args.begin() + 1, args.end()); |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 456 | return Success(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 457 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 458 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 459 | Result<Success> Service::ParseConsole(std::vector<std::string>&& args) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 460 | flags_ |= SVC_CONSOLE; |
Viorel Suman | 70daa67 | 2016-03-21 10:08:07 +0200 | [diff] [blame] | 461 | console_ = args.size() > 1 ? "/dev/" + args[1] : ""; |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 462 | return Success(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 463 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 464 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 465 | Result<Success> Service::ParseCritical(std::vector<std::string>&& args) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 466 | flags_ |= SVC_CRITICAL; |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 467 | return Success(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 468 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 469 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 470 | Result<Success> Service::ParseDisabled(std::vector<std::string>&& args) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 471 | flags_ |= SVC_DISABLED; |
| 472 | flags_ |= SVC_RC_DISABLED; |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 473 | return Success(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 474 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 475 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 476 | Result<Success> Service::ParseEnterNamespace(std::vector<std::string>&& args) { |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 477 | if (args[1] != "net") { |
| 478 | return Error() << "Init only supports entering network namespaces"; |
| 479 | } |
| 480 | if (!namespaces_to_enter_.empty()) { |
| 481 | return Error() << "Only one network namespace may be entered"; |
| 482 | } |
| 483 | // Network namespaces require that /sys is remounted, otherwise the old adapters will still be |
| 484 | // present. Therefore, they also require mount namespaces. |
| 485 | namespace_flags_ |= CLONE_NEWNS; |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 486 | namespaces_to_enter_.emplace_back(CLONE_NEWNET, std::move(args[2])); |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 487 | return Success(); |
| 488 | } |
| 489 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 490 | Result<Success> Service::ParseGroup(std::vector<std::string>&& args) { |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 491 | auto gid = DecodeUid(args[1]); |
| 492 | if (!gid) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 493 | return Error() << "Unable to decode GID for '" << args[1] << "': " << gid.error(); |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 494 | } |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 495 | gid_ = *gid; |
| 496 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 497 | for (std::size_t n = 2; n < args.size(); n++) { |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 498 | gid = DecodeUid(args[n]); |
| 499 | if (!gid) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 500 | return Error() << "Unable to decode GID for '" << args[n] << "': " << gid.error(); |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 501 | } |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 502 | supp_gids_.emplace_back(*gid); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 503 | } |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 504 | return Success(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 507 | Result<Success> Service::ParsePriority(std::vector<std::string>&& args) { |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 508 | priority_ = 0; |
| 509 | if (!ParseInt(args[1], &priority_, |
Keun-young Park | dd34ca4 | 2016-11-11 18:06:31 -0800 | [diff] [blame] | 510 | static_cast<int>(ANDROID_PRIORITY_HIGHEST), // highest is negative |
| 511 | static_cast<int>(ANDROID_PRIORITY_LOWEST))) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 512 | return Error() << StringPrintf("process priority value must be range %d - %d", |
| 513 | ANDROID_PRIORITY_HIGHEST, ANDROID_PRIORITY_LOWEST); |
Vitalii Tomkiv | 081705c | 2016-05-18 17:36:30 -0700 | [diff] [blame] | 514 | } |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 515 | return Success(); |
Vitalii Tomkiv | 081705c | 2016-05-18 17:36:30 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 518 | Result<Success> Service::ParseInterface(std::vector<std::string>&& args) { |
Steven Moreland | e055d73 | 2017-10-05 18:50:22 -0700 | [diff] [blame] | 519 | const std::string& interface_name = args[1]; |
| 520 | const std::string& instance_name = args[2]; |
| 521 | |
Steven Moreland | 422367b | 2018-03-06 14:57:46 -0800 | [diff] [blame] | 522 | FQName fq_name; |
| 523 | if (!FQName::parse(interface_name, &fq_name)) { |
Steven Moreland | e055d73 | 2017-10-05 18:50:22 -0700 | [diff] [blame] | 524 | return Error() << "Invalid fully-qualified name for interface '" << interface_name << "'"; |
| 525 | } |
| 526 | |
| 527 | if (!fq_name.isFullyQualified()) { |
| 528 | return Error() << "Interface name not fully-qualified '" << interface_name << "'"; |
| 529 | } |
| 530 | |
| 531 | if (fq_name.isValidValueName()) { |
| 532 | return Error() << "Interface name must not be a value name '" << interface_name << "'"; |
| 533 | } |
| 534 | |
| 535 | const std::string fullname = interface_name + "/" + instance_name; |
| 536 | |
| 537 | for (const auto& svc : ServiceList::GetInstance()) { |
| 538 | if (svc->interfaces().count(fullname) > 0) { |
| 539 | return Error() << "Interface '" << fullname << "' redefined in " << name() |
| 540 | << " but is already defined by " << svc->name(); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | interfaces_.insert(fullname); |
| 545 | |
| 546 | return Success(); |
| 547 | } |
| 548 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 549 | Result<Success> Service::ParseIoprio(std::vector<std::string>&& args) { |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 550 | if (!ParseInt(args[2], &ioprio_pri_, 0, 7)) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 551 | return Error() << "priority value must be range 0 - 7"; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | if (args[1] == "rt") { |
| 555 | ioprio_class_ = IoSchedClass_RT; |
| 556 | } else if (args[1] == "be") { |
| 557 | ioprio_class_ = IoSchedClass_BE; |
| 558 | } else if (args[1] == "idle") { |
| 559 | ioprio_class_ = IoSchedClass_IDLE; |
| 560 | } else { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 561 | return Error() << "ioprio option usage: ioprio <rt|be|idle> <0-7>"; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 564 | return Success(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 567 | Result<Success> Service::ParseKeycodes(std::vector<std::string>&& args) { |
Tom Cherry | 7916684 | 2018-10-16 10:58:06 -0700 | [diff] [blame] | 568 | auto it = args.begin() + 1; |
| 569 | if (args.size() == 2 && StartsWith(args[1], "$")) { |
| 570 | std::string expanded; |
| 571 | if (!expand_props(args[1], &expanded)) { |
| 572 | return Error() << "Could not expand property '" << args[1] << "'"; |
| 573 | } |
| 574 | |
| 575 | // If the property is not set, it defaults to none, in which case there are no keycodes |
| 576 | // for this service. |
| 577 | if (expanded == "none") { |
| 578 | return Success(); |
| 579 | } |
| 580 | |
| 581 | args = Split(expanded, ","); |
| 582 | it = args.begin(); |
| 583 | } |
| 584 | |
| 585 | for (; it != args.end(); ++it) { |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 586 | int code; |
Tom Cherry | 7916684 | 2018-10-16 10:58:06 -0700 | [diff] [blame] | 587 | if (ParseInt(*it, &code, 0, KEY_MAX)) { |
Mark Salyzyn | eca2507 | 2018-05-16 15:10:24 -0700 | [diff] [blame] | 588 | for (auto& key : keycodes_) { |
Tom Cherry | 7916684 | 2018-10-16 10:58:06 -0700 | [diff] [blame] | 589 | if (key == code) return Error() << "duplicate keycode: " << *it; |
Mark Salyzyn | eca2507 | 2018-05-16 15:10:24 -0700 | [diff] [blame] | 590 | } |
Mark Salyzyn | 1385725 | 2018-05-18 15:25:15 -0700 | [diff] [blame] | 591 | keycodes_.insert(std::upper_bound(keycodes_.begin(), keycodes_.end(), code), code); |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 592 | } else { |
Tom Cherry | 7916684 | 2018-10-16 10:58:06 -0700 | [diff] [blame] | 593 | return Error() << "invalid keycode: " << *it; |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 594 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 595 | } |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 596 | return Success(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 599 | Result<Success> Service::ParseOneshot(std::vector<std::string>&& args) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 600 | flags_ |= SVC_ONESHOT; |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 601 | return Success(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 604 | Result<Success> Service::ParseOnrestart(std::vector<std::string>&& args) { |
| 605 | args.erase(args.begin()); |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 606 | int line = onrestart_.NumCommands() + 1; |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 607 | if (auto result = onrestart_.AddCommand(std::move(args), line); !result) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 608 | return Error() << "cannot add Onrestart command: " << result.error(); |
| 609 | } |
| 610 | return Success(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 613 | Result<Success> Service::ParseNamespace(std::vector<std::string>&& args) { |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 614 | for (size_t i = 1; i < args.size(); i++) { |
| 615 | if (args[i] == "pid") { |
| 616 | namespace_flags_ |= CLONE_NEWPID; |
| 617 | // PID namespaces require mount namespaces. |
| 618 | namespace_flags_ |= CLONE_NEWNS; |
| 619 | } else if (args[i] == "mnt") { |
| 620 | namespace_flags_ |= CLONE_NEWNS; |
| 621 | } else { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 622 | return Error() << "namespace must be 'pid' or 'mnt'"; |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 623 | } |
| 624 | } |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 625 | return Success(); |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 628 | Result<Success> Service::ParseOomScoreAdjust(std::vector<std::string>&& args) { |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 629 | if (!ParseInt(args[1], &oom_score_adjust_, -1000, 1000)) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 630 | return Error() << "oom_score_adjust value must be in range -1000 - +1000"; |
Marco Nelissen | 310f670 | 2016-07-22 12:07:06 -0700 | [diff] [blame] | 631 | } |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 632 | return Success(); |
Marco Nelissen | 310f670 | 2016-07-22 12:07:06 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 635 | Result<Success> Service::ParseOverride(std::vector<std::string>&& args) { |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 636 | override_ = true; |
| 637 | return Success(); |
| 638 | } |
| 639 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 640 | Result<Success> Service::ParseMemcgSwappiness(std::vector<std::string>&& args) { |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 641 | if (!ParseInt(args[1], &swappiness_, 0)) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 642 | return Error() << "swappiness value must be equal or greater than 0"; |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 643 | } |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 644 | return Success(); |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 647 | Result<Success> Service::ParseMemcgLimitInBytes(std::vector<std::string>&& args) { |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 648 | if (!ParseInt(args[1], &limit_in_bytes_, 0)) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 649 | return Error() << "limit_in_bytes value must be equal or greater than 0"; |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 650 | } |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 651 | return Success(); |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 654 | Result<Success> Service::ParseMemcgLimitPercent(std::vector<std::string>&& args) { |
| 655 | if (!ParseInt(args[1], &limit_percent_, 0)) { |
| 656 | return Error() << "limit_percent value must be equal or greater than 0"; |
| 657 | } |
| 658 | return Success(); |
| 659 | } |
| 660 | |
| 661 | Result<Success> Service::ParseMemcgLimitProperty(std::vector<std::string>&& args) { |
| 662 | limit_property_ = std::move(args[1]); |
| 663 | return Success(); |
| 664 | } |
| 665 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 666 | Result<Success> Service::ParseMemcgSoftLimitInBytes(std::vector<std::string>&& args) { |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 667 | if (!ParseInt(args[1], &soft_limit_in_bytes_, 0)) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 668 | return Error() << "soft_limit_in_bytes value must be equal or greater than 0"; |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 669 | } |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 670 | return Success(); |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 673 | Result<Success> Service::ParseProcessRlimit(std::vector<std::string>&& args) { |
Tom Cherry | 7ac013d | 2017-08-25 10:39:25 -0700 | [diff] [blame] | 674 | auto rlimit = ParseRlimit(args); |
| 675 | if (!rlimit) return rlimit.error(); |
| 676 | |
| 677 | rlimits_.emplace_back(*rlimit); |
| 678 | return Success(); |
| 679 | } |
| 680 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 681 | Result<Success> Service::ParseRestartPeriod(std::vector<std::string>&& args) { |
Tom Cherry | 73f535e | 2018-09-27 16:10:46 -0700 | [diff] [blame] | 682 | int period; |
| 683 | if (!ParseInt(args[1], &period, 5)) { |
| 684 | return Error() << "restart_period value must be an integer >= 5"; |
| 685 | } |
| 686 | restart_period_ = std::chrono::seconds(period); |
| 687 | return Success(); |
| 688 | } |
| 689 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 690 | Result<Success> Service::ParseSeclabel(std::vector<std::string>&& args) { |
| 691 | seclabel_ = std::move(args[1]); |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 692 | return Success(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 695 | Result<Success> Service::ParseSigstop(std::vector<std::string>&& args) { |
Tom Cherry | 8f38048 | 2018-04-17 14:48:44 -0700 | [diff] [blame] | 696 | sigstop_ = true; |
| 697 | return Success(); |
| 698 | } |
| 699 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 700 | Result<Success> Service::ParseSetenv(std::vector<std::string>&& args) { |
| 701 | environment_vars_.emplace_back(std::move(args[1]), std::move(args[2])); |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 702 | return Success(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 705 | Result<Success> Service::ParseShutdown(std::vector<std::string>&& args) { |
Keun-young Park | cccb34f | 2017-07-05 11:38:44 -0700 | [diff] [blame] | 706 | if (args[1] == "critical") { |
| 707 | flags_ |= SVC_SHUTDOWN_CRITICAL; |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 708 | return Success(); |
Keun-young Park | cccb34f | 2017-07-05 11:38:44 -0700 | [diff] [blame] | 709 | } |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 710 | return Error() << "Invalid shutdown option"; |
Keun-young Park | cccb34f | 2017-07-05 11:38:44 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 713 | Result<Success> Service::ParseTimeoutPeriod(std::vector<std::string>&& args) { |
Tom Cherry | 73f535e | 2018-09-27 16:10:46 -0700 | [diff] [blame] | 714 | int period; |
| 715 | if (!ParseInt(args[1], &period, 1)) { |
| 716 | return Error() << "timeout_period value must be an integer >= 1"; |
| 717 | } |
| 718 | timeout_period_ = std::chrono::seconds(period); |
| 719 | return Success(); |
| 720 | } |
| 721 | |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 722 | template <typename T> |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 723 | Result<Success> Service::AddDescriptor(std::vector<std::string>&& args) { |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 724 | int perm = args.size() > 3 ? std::strtoul(args[3].c_str(), 0, 8) : -1; |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 725 | Result<uid_t> uid = 0; |
| 726 | Result<gid_t> gid = 0; |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 727 | std::string context = args.size() > 6 ? args[6] : ""; |
| 728 | |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 729 | if (args.size() > 4) { |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 730 | uid = DecodeUid(args[4]); |
| 731 | if (!uid) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 732 | return Error() << "Unable to find UID for '" << args[4] << "': " << uid.error(); |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | |
| 736 | if (args.size() > 5) { |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 737 | gid = DecodeUid(args[5]); |
| 738 | if (!gid) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 739 | return Error() << "Unable to find GID for '" << args[5] << "': " << gid.error(); |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 743 | auto descriptor = std::make_unique<T>(args[1], args[2], *uid, *gid, perm, context); |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 744 | |
| 745 | auto old = |
| 746 | std::find_if(descriptors_.begin(), descriptors_.end(), |
| 747 | [&descriptor] (const auto& other) { return descriptor.get() == other.get(); }); |
| 748 | |
| 749 | if (old != descriptors_.end()) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 750 | return Error() << "duplicate descriptor " << args[1] << " " << args[2]; |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | descriptors_.emplace_back(std::move(descriptor)); |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 754 | return Success(); |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | // name type perm [ uid gid context ] |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 758 | Result<Success> Service::ParseSocket(std::vector<std::string>&& args) { |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 759 | if (!StartsWith(args[2], "dgram") && !StartsWith(args[2], "stream") && |
| 760 | !StartsWith(args[2], "seqpacket")) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 761 | return Error() << "socket type must be 'dgram', 'stream' or 'seqpacket'"; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 762 | } |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 763 | return AddDescriptor<SocketInfo>(std::move(args)); |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 764 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 765 | |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 766 | // name type perm [ uid gid context ] |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 767 | Result<Success> Service::ParseFile(std::vector<std::string>&& args) { |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 768 | if (args[2] != "r" && args[2] != "w" && args[2] != "rw") { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 769 | return Error() << "file type must be 'r', 'w' or 'rw'"; |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 770 | } |
Yifan Hong | 567f187 | 2019-03-19 14:38:48 -0700 | [diff] [blame] | 771 | std::string expanded; |
| 772 | if (!expand_props(args[1], &expanded)) { |
| 773 | return Error() << "Could not expand property in file path '" << args[1] << "'"; |
| 774 | } |
| 775 | args[1] = std::move(expanded); |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 776 | if ((args[1][0] != '/') || (args[1].find("../") != std::string::npos)) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 777 | return Error() << "file name must not be relative"; |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 778 | } |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 779 | return AddDescriptor<FileInfo>(std::move(args)); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 782 | Result<Success> Service::ParseUser(std::vector<std::string>&& args) { |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 783 | auto uid = DecodeUid(args[1]); |
| 784 | if (!uid) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 785 | return Error() << "Unable to find UID for '" << args[1] << "': " << uid.error(); |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 786 | } |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 787 | uid_ = *uid; |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 788 | return Success(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 791 | Result<Success> Service::ParseWritepid(std::vector<std::string>&& args) { |
| 792 | args.erase(args.begin()); |
| 793 | writepid_files_ = std::move(args); |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 794 | return Success(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Jiyong Park | 80aa447 | 2018-11-12 12:08:41 +0900 | [diff] [blame] | 797 | Result<Success> Service::ParseUpdatable(std::vector<std::string>&& args) { |
| 798 | updatable_ = true; |
| 799 | return Success(); |
| 800 | } |
| 801 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 802 | class Service::OptionParserMap : public KeywordMap<OptionParser> { |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 803 | public: |
| 804 | OptionParserMap() {} |
| 805 | |
| 806 | private: |
| 807 | const Map& map() const override; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 808 | }; |
| 809 | |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 810 | const Service::OptionParserMap::Map& Service::OptionParserMap::map() const { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 811 | constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max(); |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 812 | // clang-format off |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 813 | static const Map option_parsers = { |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 814 | {"capabilities", |
Tom Cherry | 1cd082d | 2019-02-06 10:45:56 -0800 | [diff] [blame] | 815 | {0, kMax, &Service::ParseCapabilities}}, |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 816 | {"class", {1, kMax, &Service::ParseClass}}, |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 817 | {"console", {0, 1, &Service::ParseConsole}}, |
| 818 | {"critical", {0, 0, &Service::ParseCritical}}, |
| 819 | {"disabled", {0, 0, &Service::ParseDisabled}}, |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 820 | {"enter_namespace", |
| 821 | {2, 2, &Service::ParseEnterNamespace}}, |
Tom Cherry | e2f341e | 2018-03-08 13:51:10 -0800 | [diff] [blame] | 822 | {"file", {2, 2, &Service::ParseFile}}, |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 823 | {"group", {1, NR_SVC_SUPP_GIDS + 1, &Service::ParseGroup}}, |
Steven Moreland | e055d73 | 2017-10-05 18:50:22 -0700 | [diff] [blame] | 824 | {"interface", {2, 2, &Service::ParseInterface}}, |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 825 | {"ioprio", {2, 2, &Service::ParseIoprio}}, |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 826 | {"keycodes", {1, kMax, &Service::ParseKeycodes}}, |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 827 | {"memcg.limit_in_bytes", |
| 828 | {1, 1, &Service::ParseMemcgLimitInBytes}}, |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 829 | {"memcg.limit_percent", |
| 830 | {1, 1, &Service::ParseMemcgLimitPercent}}, |
| 831 | {"memcg.limit_property", |
| 832 | {1, 1, &Service::ParseMemcgLimitProperty}}, |
Tom Cherry | e2f341e | 2018-03-08 13:51:10 -0800 | [diff] [blame] | 833 | {"memcg.soft_limit_in_bytes", |
| 834 | {1, 1, &Service::ParseMemcgSoftLimitInBytes}}, |
| 835 | {"memcg.swappiness", |
| 836 | {1, 1, &Service::ParseMemcgSwappiness}}, |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 837 | {"namespace", {1, 2, &Service::ParseNamespace}}, |
Tom Cherry | e2f341e | 2018-03-08 13:51:10 -0800 | [diff] [blame] | 838 | {"oneshot", {0, 0, &Service::ParseOneshot}}, |
| 839 | {"onrestart", {1, kMax, &Service::ParseOnrestart}}, |
| 840 | {"oom_score_adjust", |
| 841 | {1, 1, &Service::ParseOomScoreAdjust}}, |
| 842 | {"override", {0, 0, &Service::ParseOverride}}, |
| 843 | {"priority", {1, 1, &Service::ParsePriority}}, |
Tom Cherry | 73f535e | 2018-09-27 16:10:46 -0700 | [diff] [blame] | 844 | {"restart_period", |
| 845 | {1, 1, &Service::ParseRestartPeriod}}, |
Tom Cherry | 7ac013d | 2017-08-25 10:39:25 -0700 | [diff] [blame] | 846 | {"rlimit", {3, 3, &Service::ParseProcessRlimit}}, |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 847 | {"seclabel", {1, 1, &Service::ParseSeclabel}}, |
| 848 | {"setenv", {2, 2, &Service::ParseSetenv}}, |
Keun-young Park | cccb34f | 2017-07-05 11:38:44 -0700 | [diff] [blame] | 849 | {"shutdown", {1, 1, &Service::ParseShutdown}}, |
Tom Cherry | 8f38048 | 2018-04-17 14:48:44 -0700 | [diff] [blame] | 850 | {"sigstop", {0, 0, &Service::ParseSigstop}}, |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 851 | {"socket", {3, 6, &Service::ParseSocket}}, |
Tom Cherry | 73f535e | 2018-09-27 16:10:46 -0700 | [diff] [blame] | 852 | {"timeout_period", |
| 853 | {1, 1, &Service::ParseTimeoutPeriod}}, |
Jiyong Park | 80aa447 | 2018-11-12 12:08:41 +0900 | [diff] [blame] | 854 | {"updatable", {0, 0, &Service::ParseUpdatable}}, |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 855 | {"user", {1, 1, &Service::ParseUser}}, |
| 856 | {"writepid", {1, kMax, &Service::ParseWritepid}}, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 857 | }; |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 858 | // clang-format on |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 859 | return option_parsers; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 862 | Result<Success> Service::ParseLine(std::vector<std::string>&& args) { |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 863 | static const OptionParserMap parser_map; |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 864 | auto parser = parser_map.FindFunction(args); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 865 | |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 866 | if (!parser) return parser.error(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 867 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 868 | return std::invoke(*parser, this, std::move(args)); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 871 | Result<Success> Service::ExecStart() { |
Jiyong Park | 80aa447 | 2018-11-12 12:08:41 +0900 | [diff] [blame] | 872 | if (is_updatable() && !ServiceList::GetInstance().IsServicesUpdated()) { |
| 873 | // Don't delay the service for ExecStart() as the semantic is that |
| 874 | // the caller might depend on the side effect of the execution. |
| 875 | return Error() << "Cannot start an updatable service '" << name_ |
| 876 | << "' before configs from APEXes are all loaded"; |
| 877 | } |
| 878 | |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 879 | flags_ |= SVC_ONESHOT; |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 880 | |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 881 | if (auto result = Start(); !result) { |
| 882 | return result; |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 883 | } |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 884 | |
| 885 | flags_ |= SVC_EXEC; |
| 886 | is_exec_service_running_ = true; |
| 887 | |
Wei Wang | 2c4ee75 | 2018-06-20 14:54:52 -0700 | [diff] [blame] | 888 | LOG(INFO) << "SVC_EXEC service '" << name_ << "' pid " << pid_ << " (uid " << uid_ << " gid " |
| 889 | << gid_ << "+" << supp_gids_.size() << " context " |
| 890 | << (!seclabel_.empty() ? seclabel_ : "default") << ") started; waiting..."; |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 891 | |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 892 | return Success(); |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 895 | Result<Success> Service::Start() { |
Jiyong Park | 80aa447 | 2018-11-12 12:08:41 +0900 | [diff] [blame] | 896 | if (is_updatable() && !ServiceList::GetInstance().IsServicesUpdated()) { |
| 897 | ServiceList::GetInstance().DelayService(*this); |
| 898 | return Error() << "Cannot start an updatable service '" << name_ |
| 899 | << "' before configs from APEXes are all loaded. " |
| 900 | << "Queued for execution."; |
| 901 | } |
| 902 | |
Tao Wu | 990d43c | 2017-10-26 10:43:10 -0700 | [diff] [blame] | 903 | bool disabled = (flags_ & (SVC_DISABLED | SVC_RESET)); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 904 | // Starting a service removes it from the disabled or reset state and |
| 905 | // immediately takes it out of the restarting state if it was in there. |
| 906 | flags_ &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START)); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 907 | |
| 908 | // Running processes require no additional work --- if they're in the |
| 909 | // process of exiting, we've ensured that they will immediately restart |
Tao Wu | 990d43c | 2017-10-26 10:43:10 -0700 | [diff] [blame] | 910 | // on exit, unless they are ONESHOT. For ONESHOT service, if it's in |
| 911 | // stopping status, we just set SVC_RESTART flag so it will get restarted |
| 912 | // in Reap(). |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 913 | if (flags_ & SVC_RUNNING) { |
Tao Wu | 990d43c | 2017-10-26 10:43:10 -0700 | [diff] [blame] | 914 | if ((flags_ & SVC_ONESHOT) && disabled) { |
| 915 | flags_ |= SVC_RESTART; |
| 916 | } |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 917 | // It is not an error to try to start a service that is already running. |
| 918 | return Success(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | bool needs_console = (flags_ & SVC_CONSOLE); |
Viorel Suman | 70daa67 | 2016-03-21 10:08:07 +0200 | [diff] [blame] | 922 | if (needs_console) { |
| 923 | if (console_.empty()) { |
| 924 | console_ = default_console; |
| 925 | } |
| 926 | |
Adrian Salido | 24ef860 | 2016-12-20 15:52:15 -0800 | [diff] [blame] | 927 | // Make sure that open call succeeds to ensure a console driver is |
| 928 | // properly registered for the device node |
| 929 | int console_fd = open(console_.c_str(), O_RDWR | O_CLOEXEC); |
| 930 | if (console_fd < 0) { |
Viorel Suman | 70daa67 | 2016-03-21 10:08:07 +0200 | [diff] [blame] | 931 | flags_ |= SVC_DISABLED; |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 932 | return ErrnoError() << "Couldn't open console '" << console_ << "'"; |
Viorel Suman | 70daa67 | 2016-03-21 10:08:07 +0200 | [diff] [blame] | 933 | } |
Adrian Salido | 24ef860 | 2016-12-20 15:52:15 -0800 | [diff] [blame] | 934 | close(console_fd); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | struct stat sb; |
| 938 | if (stat(args_[0].c_str(), &sb) == -1) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 939 | flags_ |= SVC_DISABLED; |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 940 | return ErrnoError() << "Cannot find '" << args_[0] << "'"; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 943 | std::string scon; |
| 944 | if (!seclabel_.empty()) { |
| 945 | scon = seclabel_; |
| 946 | } else { |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 947 | auto result = ComputeContextFromExecutable(args_[0]); |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 948 | if (!result) { |
| 949 | return result.error(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 950 | } |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 951 | scon = *result; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 952 | } |
| 953 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 954 | if (!IsRuntimeApexReady() && !pre_apexd_) { |
| 955 | // If this service is started before the runtime APEX gets available, |
| 956 | // mark it as pre-apexd one. Note that this marking is permanent. So |
| 957 | // for example, if the service is re-launched (e.g., due to crash), |
| 958 | // it is still recognized as pre-apexd... for consistency. |
| 959 | pre_apexd_ = true; |
| 960 | } |
| 961 | |
Martijn Coenen | 70788f9 | 2019-04-23 16:26:01 +0200 | [diff] [blame] | 962 | post_data_ = ServiceList::GetInstance().IsPostData(); |
| 963 | |
Wei Wang | a285dac | 2016-10-04 14:05:39 -0700 | [diff] [blame] | 964 | LOG(INFO) << "starting service '" << name_ << "'..."; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 965 | |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 966 | pid_t pid = -1; |
| 967 | if (namespace_flags_) { |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 968 | pid = clone(nullptr, nullptr, namespace_flags_ | SIGCHLD, nullptr); |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 969 | } else { |
| 970 | pid = fork(); |
| 971 | } |
| 972 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 973 | if (pid == 0) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 974 | umask(077); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 975 | |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 976 | if (auto result = EnterNamespaces(); !result) { |
| 977 | LOG(FATAL) << "Service '" << name_ << "' could not enter namespaces: " << result.error(); |
| 978 | } |
| 979 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 980 | #if defined(__ANDROID__) |
| 981 | if (pre_apexd_) { |
| 982 | if (!SwitchToBootstrapMountNamespaceIfNeeded()) { |
| 983 | LOG(FATAL) << "Service '" << name_ << "' could not enter " |
| 984 | << "into the bootstrap mount namespace"; |
| 985 | } |
| 986 | } |
| 987 | #endif |
| 988 | |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 989 | if (namespace_flags_ & CLONE_NEWNS) { |
| 990 | if (auto result = SetUpMountNamespace(); !result) { |
| 991 | LOG(FATAL) << "Service '" << name_ |
| 992 | << "' could not set up mount namespace: " << result.error(); |
| 993 | } |
| 994 | } |
| 995 | |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 996 | if (namespace_flags_ & CLONE_NEWPID) { |
| 997 | // This will fork again to run an init process inside the PID |
| 998 | // namespace. |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 999 | if (auto result = SetUpPidNamespace(); !result) { |
| 1000 | LOG(FATAL) << "Service '" << name_ |
| 1001 | << "' could not set up PID namespace: " << result.error(); |
| 1002 | } |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 1003 | } |
| 1004 | |
Tom Cherry | 6de21f1 | 2017-08-22 15:41:03 -0700 | [diff] [blame] | 1005 | for (const auto& [key, value] : environment_vars_) { |
| 1006 | setenv(key.c_str(), value.c_str(), 1); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 1009 | std::for_each(descriptors_.begin(), descriptors_.end(), |
| 1010 | std::bind(&DescriptorInfo::CreateAndPublish, std::placeholders::_1, scon)); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1011 | |
Suren Baghdasaryan | e01ae8d | 2018-12-21 12:36:20 -0800 | [diff] [blame] | 1012 | // See if there were "writepid" instructions to write to files under cpuset path. |
| 1013 | std::string cpuset_path; |
| 1014 | if (CgroupGetControllerPath("cpuset", &cpuset_path)) { |
| 1015 | auto cpuset_predicate = [&cpuset_path](const std::string& path) { |
| 1016 | return StartsWith(path, cpuset_path + "/"); |
| 1017 | }; |
| 1018 | auto iter = |
| 1019 | std::find_if(writepid_files_.begin(), writepid_files_.end(), cpuset_predicate); |
| 1020 | if (iter == writepid_files_.end()) { |
| 1021 | // There were no "writepid" instructions for cpusets, check if the system default |
| 1022 | // cpuset is specified to be used for the process. |
| 1023 | std::string default_cpuset = GetProperty("ro.cpuset.default", ""); |
| 1024 | if (!default_cpuset.empty()) { |
| 1025 | // Make sure the cpuset name starts and ends with '/'. |
| 1026 | // A single '/' means the 'root' cpuset. |
| 1027 | if (default_cpuset.front() != '/') { |
| 1028 | default_cpuset.insert(0, 1, '/'); |
| 1029 | } |
| 1030 | if (default_cpuset.back() != '/') { |
| 1031 | default_cpuset.push_back('/'); |
| 1032 | } |
| 1033 | writepid_files_.push_back( |
| 1034 | StringPrintf("%s%stasks", cpuset_path.c_str(), default_cpuset.c_str())); |
Alex Vakulenko | 0828676 | 2016-05-03 12:00:00 -0700 | [diff] [blame] | 1035 | } |
Alex Vakulenko | 0828676 | 2016-05-03 12:00:00 -0700 | [diff] [blame] | 1036 | } |
Suren Baghdasaryan | e01ae8d | 2018-12-21 12:36:20 -0800 | [diff] [blame] | 1037 | } else { |
| 1038 | LOG(ERROR) << "cpuset cgroup controller is not mounted!"; |
Alex Vakulenko | 0828676 | 2016-05-03 12:00:00 -0700 | [diff] [blame] | 1039 | } |
Tom Cherry | 1c3a53f | 2017-06-22 16:50:31 -0700 | [diff] [blame] | 1040 | std::string pid_str = std::to_string(getpid()); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1041 | for (const auto& file : writepid_files_) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1042 | if (!WriteStringToFile(pid_str, file)) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 1043 | PLOG(ERROR) << "couldn't write " << pid_str << " to " << file; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | if (ioprio_class_ != IoSchedClass_NONE) { |
| 1048 | if (android_set_ioprio(getpid(), ioprio_class_, ioprio_pri_)) { |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 1049 | PLOG(ERROR) << "failed to set pid " << getpid() |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 1050 | << " ioprio=" << ioprio_class_ << "," << ioprio_pri_; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | if (needs_console) { |
| 1055 | setsid(); |
| 1056 | OpenConsole(); |
| 1057 | } else { |
| 1058 | ZapStdio(); |
| 1059 | } |
| 1060 | |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 1061 | // As requested, set our gid, supplemental gids, uid, context, and |
| 1062 | // priority. Aborts on failure. |
| 1063 | SetProcessAttributes(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1064 | |
Tom Cherry | 8f38048 | 2018-04-17 14:48:44 -0700 | [diff] [blame] | 1065 | if (!ExpandArgsAndExecv(args_, sigstop_)) { |
Tom Cherry | 5e405ca | 2017-09-11 16:08:54 -0700 | [diff] [blame] | 1066 | PLOG(ERROR) << "cannot execve('" << args_[0] << "')"; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | _exit(127); |
| 1070 | } |
| 1071 | |
| 1072 | if (pid < 0) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1073 | pid_ = 0; |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 1074 | return ErrnoError() << "Failed to fork"; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
Marco Nelissen | 310f670 | 2016-07-22 12:07:06 -0700 | [diff] [blame] | 1077 | if (oom_score_adjust_ != -1000) { |
Tom Cherry | 1c3a53f | 2017-06-22 16:50:31 -0700 | [diff] [blame] | 1078 | std::string oom_str = std::to_string(oom_score_adjust_); |
Marco Nelissen | 310f670 | 2016-07-22 12:07:06 -0700 | [diff] [blame] | 1079 | std::string oom_file = StringPrintf("/proc/%d/oom_score_adj", pid); |
| 1080 | if (!WriteStringToFile(oom_str, oom_file)) { |
Elliott Hughes | 076305e | 2019-03-08 12:34:53 -0800 | [diff] [blame] | 1081 | PLOG(ERROR) << "couldn't write oom_score_adj"; |
Marco Nelissen | 310f670 | 2016-07-22 12:07:06 -0700 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 1085 | time_started_ = boot_clock::now(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1086 | pid_ = pid; |
| 1087 | flags_ |= SVC_RUNNING; |
Tom Cherry | 5938379 | 2017-07-26 16:09:09 -0700 | [diff] [blame] | 1088 | start_order_ = next_start_order_++; |
Tom Cherry | 33838b1 | 2017-05-04 11:32:36 -0700 | [diff] [blame] | 1089 | process_cgroup_empty_ = false; |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 1090 | |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 1091 | bool use_memcg = swappiness_ != -1 || soft_limit_in_bytes_ != -1 || limit_in_bytes_ != -1 || |
| 1092 | limit_percent_ != -1 || !limit_property_.empty(); |
| 1093 | errno = -createProcessGroup(uid_, pid_, use_memcg); |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 1094 | if (errno != 0) { |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 1095 | PLOG(ERROR) << "createProcessGroup(" << uid_ << ", " << pid_ << ") failed for service '" |
| 1096 | << name_ << "'"; |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 1097 | } else if (use_memcg) { |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 1098 | if (swappiness_ != -1) { |
| 1099 | if (!setProcessGroupSwappiness(uid_, pid_, swappiness_)) { |
| 1100 | PLOG(ERROR) << "setProcessGroupSwappiness failed"; |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | if (soft_limit_in_bytes_ != -1) { |
| 1105 | if (!setProcessGroupSoftLimit(uid_, pid_, soft_limit_in_bytes_)) { |
| 1106 | PLOG(ERROR) << "setProcessGroupSoftLimit failed"; |
| 1107 | } |
| 1108 | } |
| 1109 | |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 1110 | size_t computed_limit_in_bytes = limit_in_bytes_; |
| 1111 | if (limit_percent_ != -1) { |
| 1112 | long page_size = sysconf(_SC_PAGESIZE); |
| 1113 | long num_pages = sysconf(_SC_PHYS_PAGES); |
| 1114 | if (page_size > 0 && num_pages > 0) { |
| 1115 | size_t max_mem = SIZE_MAX; |
| 1116 | if (size_t(num_pages) < SIZE_MAX / size_t(page_size)) { |
| 1117 | max_mem = size_t(num_pages) * size_t(page_size); |
| 1118 | } |
| 1119 | computed_limit_in_bytes = |
| 1120 | std::min(computed_limit_in_bytes, max_mem / 100 * limit_percent_); |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | if (!limit_property_.empty()) { |
| 1125 | // This ends up overwriting computed_limit_in_bytes but only if the |
| 1126 | // property is defined. |
| 1127 | computed_limit_in_bytes = android::base::GetUintProperty( |
| 1128 | limit_property_, computed_limit_in_bytes, SIZE_MAX); |
| 1129 | } |
| 1130 | |
| 1131 | if (computed_limit_in_bytes != size_t(-1)) { |
| 1132 | if (!setProcessGroupLimit(uid_, pid_, computed_limit_in_bytes)) { |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 1133 | PLOG(ERROR) << "setProcessGroupLimit failed"; |
| 1134 | } |
| 1135 | } |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 1136 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1137 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1138 | NotifyStateChange("running"); |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 1139 | return Success(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 1142 | Result<Success> Service::StartIfNotDisabled() { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1143 | if (!(flags_ & SVC_DISABLED)) { |
| 1144 | return Start(); |
| 1145 | } else { |
| 1146 | flags_ |= SVC_DISABLED_START; |
| 1147 | } |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 1148 | return Success(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1149 | } |
| 1150 | |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 1151 | Result<Success> Service::Enable() { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1152 | flags_ &= ~(SVC_DISABLED | SVC_RC_DISABLED); |
| 1153 | if (flags_ & SVC_DISABLED_START) { |
| 1154 | return Start(); |
| 1155 | } |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 1156 | return Success(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | void Service::Reset() { |
| 1160 | StopOrReset(SVC_RESET); |
| 1161 | } |
| 1162 | |
Martijn Coenen | 70788f9 | 2019-04-23 16:26:01 +0200 | [diff] [blame] | 1163 | void Service::ResetIfPostData() { |
| 1164 | if (post_data_) { |
Martijn Coenen | acc45aa | 2019-05-15 22:04:13 +0200 | [diff] [blame] | 1165 | if (flags_ & SVC_RUNNING) { |
| 1166 | running_at_post_data_reset_ = true; |
| 1167 | } |
Martijn Coenen | 70788f9 | 2019-04-23 16:26:01 +0200 | [diff] [blame] | 1168 | StopOrReset(SVC_RESET); |
| 1169 | } |
| 1170 | } |
| 1171 | |
Martijn Coenen | acc45aa | 2019-05-15 22:04:13 +0200 | [diff] [blame] | 1172 | Result<Success> Service::StartIfPostData() { |
| 1173 | // Start the service, but only if it was started after /data was mounted, |
| 1174 | // and it was still running when we reset the post-data services. |
| 1175 | if (running_at_post_data_reset_) { |
| 1176 | return Start(); |
| 1177 | } |
| 1178 | |
| 1179 | return Success(); |
| 1180 | } |
| 1181 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1182 | void Service::Stop() { |
| 1183 | StopOrReset(SVC_DISABLED); |
| 1184 | } |
| 1185 | |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 1186 | void Service::Terminate() { |
| 1187 | flags_ &= ~(SVC_RESTARTING | SVC_DISABLED_START); |
| 1188 | flags_ |= SVC_DISABLED; |
| 1189 | if (pid_) { |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 1190 | KillProcessGroup(SIGTERM); |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 1191 | NotifyStateChange("stopping"); |
| 1192 | } |
| 1193 | } |
| 1194 | |
Tom Cherry | 73f535e | 2018-09-27 16:10:46 -0700 | [diff] [blame] | 1195 | void Service::Timeout() { |
| 1196 | // All process state flags will be taken care of in Reap(), we really just want to kill the |
| 1197 | // process here when it times out. Oneshot processes will transition to be disabled, and |
| 1198 | // all other processes will transition to be restarting. |
| 1199 | LOG(INFO) << "Service '" << name_ << "' expired its timeout of " << timeout_period_->count() |
| 1200 | << " seconds and will now be killed"; |
| 1201 | if (pid_) { |
| 1202 | KillProcessGroup(SIGKILL); |
| 1203 | NotifyStateChange("stopping"); |
| 1204 | } |
| 1205 | } |
| 1206 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1207 | void Service::Restart() { |
| 1208 | if (flags_ & SVC_RUNNING) { |
| 1209 | /* Stop, wait, then start the service. */ |
| 1210 | StopOrReset(SVC_RESTART); |
| 1211 | } else if (!(flags_ & SVC_RESTARTING)) { |
| 1212 | /* Just start the service since it's not running. */ |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 1213 | if (auto result = Start(); !result) { |
| 1214 | LOG(ERROR) << "Could not restart '" << name_ << "': " << result.error(); |
| 1215 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1216 | } /* else: Service is restarting anyways. */ |
| 1217 | } |
| 1218 | |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 1219 | // The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART. |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1220 | void Service::StopOrReset(int how) { |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 1221 | // The service is still SVC_RUNNING until its process exits, but if it has |
| 1222 | // already exited it shoudn't attempt a restart yet. |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1223 | flags_ &= ~(SVC_RESTARTING | SVC_DISABLED_START); |
| 1224 | |
| 1225 | if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) { |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 1226 | // An illegal flag: default to SVC_DISABLED. |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1227 | how = SVC_DISABLED; |
| 1228 | } |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 1229 | |
| 1230 | // If the service has not yet started, prevent it from auto-starting with its class. |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1231 | if (how == SVC_RESET) { |
| 1232 | flags_ |= (flags_ & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET; |
| 1233 | } else { |
| 1234 | flags_ |= how; |
| 1235 | } |
Tao Wu | 84b856d | 2017-10-27 11:29:13 -0700 | [diff] [blame] | 1236 | // Make sure it's in right status when a restart immediately follow a |
| 1237 | // stop/reset or vice versa. |
| 1238 | if (how == SVC_RESTART) { |
| 1239 | flags_ &= (~(SVC_DISABLED | SVC_RESET)); |
| 1240 | } else { |
| 1241 | flags_ &= (~SVC_RESTART); |
| 1242 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1243 | |
| 1244 | if (pid_) { |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 1245 | KillProcessGroup(SIGKILL); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1246 | NotifyStateChange("stopping"); |
| 1247 | } else { |
| 1248 | NotifyStateChange("stopped"); |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | void Service::ZapStdio() const { |
| 1253 | int fd; |
| 1254 | fd = open("/dev/null", O_RDWR); |
| 1255 | dup2(fd, 0); |
| 1256 | dup2(fd, 1); |
| 1257 | dup2(fd, 2); |
| 1258 | close(fd); |
| 1259 | } |
| 1260 | |
| 1261 | void Service::OpenConsole() const { |
Viorel Suman | 70daa67 | 2016-03-21 10:08:07 +0200 | [diff] [blame] | 1262 | int fd = open(console_.c_str(), O_RDWR); |
| 1263 | if (fd == -1) fd = open("/dev/null", O_RDWR); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1264 | ioctl(fd, TIOCSCTTY, 0); |
| 1265 | dup2(fd, 0); |
| 1266 | dup2(fd, 1); |
| 1267 | dup2(fd, 2); |
| 1268 | close(fd); |
| 1269 | } |
| 1270 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 1271 | ServiceList::ServiceList() {} |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1272 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 1273 | ServiceList& ServiceList::GetInstance() { |
| 1274 | static ServiceList instance; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1275 | return instance; |
| 1276 | } |
| 1277 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 1278 | void ServiceList::AddService(std::unique_ptr<Service> service) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1279 | services_.emplace_back(std::move(service)); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1280 | } |
| 1281 | |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 1282 | std::unique_ptr<Service> Service::MakeTemporaryOneshotService(const std::vector<std::string>& args) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1283 | // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS... |
| 1284 | // SECLABEL can be a - to denote default |
| 1285 | std::size_t command_arg = 1; |
| 1286 | for (std::size_t i = 1; i < args.size(); ++i) { |
| 1287 | if (args[i] == "--") { |
| 1288 | command_arg = i + 1; |
| 1289 | break; |
| 1290 | } |
| 1291 | } |
| 1292 | if (command_arg > 4 + NR_SVC_SUPP_GIDS) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 1293 | LOG(ERROR) << "exec called with too many supplementary group ids"; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1294 | return nullptr; |
| 1295 | } |
| 1296 | |
| 1297 | if (command_arg >= args.size()) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 1298 | LOG(ERROR) << "exec called without command"; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1299 | return nullptr; |
| 1300 | } |
| 1301 | std::vector<std::string> str_args(args.begin() + command_arg, args.end()); |
| 1302 | |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 1303 | static size_t exec_count = 0; |
| 1304 | exec_count++; |
| 1305 | std::string name = "exec " + std::to_string(exec_count) + " (" + Join(str_args, " ") + ")"; |
Tom Cherry | 86e31a8 | 2017-04-25 17:31:06 -0700 | [diff] [blame] | 1306 | |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 1307 | unsigned flags = SVC_ONESHOT | SVC_TEMPORARY; |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 1308 | unsigned namespace_flags = 0; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1309 | |
| 1310 | std::string seclabel = ""; |
| 1311 | if (command_arg > 2 && args[1] != "-") { |
| 1312 | seclabel = args[1]; |
| 1313 | } |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 1314 | Result<uid_t> uid = 0; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1315 | if (command_arg > 3) { |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 1316 | uid = DecodeUid(args[2]); |
| 1317 | if (!uid) { |
| 1318 | LOG(ERROR) << "Unable to decode UID for '" << args[2] << "': " << uid.error(); |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 1319 | return nullptr; |
| 1320 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1321 | } |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 1322 | Result<gid_t> gid = 0; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1323 | std::vector<gid_t> supp_gids; |
| 1324 | if (command_arg > 4) { |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 1325 | gid = DecodeUid(args[3]); |
| 1326 | if (!gid) { |
| 1327 | LOG(ERROR) << "Unable to decode GID for '" << args[3] << "': " << gid.error(); |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 1328 | return nullptr; |
| 1329 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1330 | std::size_t nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */; |
| 1331 | for (size_t i = 0; i < nr_supp_gids; ++i) { |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 1332 | auto supp_gid = DecodeUid(args[4 + i]); |
| 1333 | if (!supp_gid) { |
| 1334 | LOG(ERROR) << "Unable to decode GID for '" << args[4 + i] |
| 1335 | << "': " << supp_gid.error(); |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 1336 | return nullptr; |
| 1337 | } |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 1338 | supp_gids.push_back(*supp_gid); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1339 | } |
| 1340 | } |
| 1341 | |
Tom Cherry | 1cd082d | 2019-02-06 10:45:56 -0800 | [diff] [blame] | 1342 | return std::make_unique<Service>(name, flags, *uid, *gid, supp_gids, namespace_flags, seclabel, |
| 1343 | nullptr, str_args); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
Tom Cherry | 5938379 | 2017-07-26 16:09:09 -0700 | [diff] [blame] | 1346 | // Shutdown services in the opposite order that they were started. |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 1347 | const std::vector<Service*> ServiceList::services_in_shutdown_order() const { |
Tom Cherry | 5938379 | 2017-07-26 16:09:09 -0700 | [diff] [blame] | 1348 | std::vector<Service*> shutdown_services; |
| 1349 | for (const auto& service : services_) { |
| 1350 | if (service->start_order() > 0) shutdown_services.emplace_back(service.get()); |
| 1351 | } |
| 1352 | std::sort(shutdown_services.begin(), shutdown_services.end(), |
| 1353 | [](const auto& a, const auto& b) { return a->start_order() > b->start_order(); }); |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 1354 | return shutdown_services; |
Tom Cherry | 5938379 | 2017-07-26 16:09:09 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 1357 | void ServiceList::RemoveService(const Service& svc) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1358 | auto svc_it = std::find_if(services_.begin(), services_.end(), |
| 1359 | [&svc] (const std::unique_ptr<Service>& s) { |
| 1360 | return svc.name() == s->name(); |
| 1361 | }); |
| 1362 | if (svc_it == services_.end()) { |
| 1363 | return; |
| 1364 | } |
| 1365 | |
| 1366 | services_.erase(svc_it); |
| 1367 | } |
| 1368 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 1369 | void ServiceList::DumpState() const { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1370 | for (const auto& s : services_) { |
| 1371 | s->DumpState(); |
| 1372 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1373 | } |
| 1374 | |
Martijn Coenen | 70788f9 | 2019-04-23 16:26:01 +0200 | [diff] [blame] | 1375 | void ServiceList::MarkPostData() { |
| 1376 | post_data_ = true; |
| 1377 | } |
| 1378 | |
| 1379 | bool ServiceList::IsPostData() { |
| 1380 | return post_data_; |
| 1381 | } |
| 1382 | |
Jiyong Park | 80aa447 | 2018-11-12 12:08:41 +0900 | [diff] [blame] | 1383 | void ServiceList::MarkServicesUpdate() { |
| 1384 | services_update_finished_ = true; |
| 1385 | |
| 1386 | // start the delayed services |
| 1387 | for (const auto& name : delayed_service_names_) { |
| 1388 | Service* service = FindService(name); |
| 1389 | if (service == nullptr) { |
| 1390 | LOG(ERROR) << "delayed service '" << name << "' could not be found."; |
| 1391 | continue; |
| 1392 | } |
| 1393 | if (auto result = service->Start(); !result) { |
Tom Cherry | 9949ec5 | 2019-05-16 16:54:49 -0700 | [diff] [blame] | 1394 | LOG(ERROR) << result.error().as_string; |
Jiyong Park | 80aa447 | 2018-11-12 12:08:41 +0900 | [diff] [blame] | 1395 | } |
| 1396 | } |
| 1397 | delayed_service_names_.clear(); |
| 1398 | } |
| 1399 | |
| 1400 | void ServiceList::DelayService(const Service& service) { |
| 1401 | if (services_update_finished_) { |
| 1402 | LOG(ERROR) << "Cannot delay the start of service '" << service.name() |
| 1403 | << "' because all services are already updated. Ignoring."; |
| 1404 | return; |
| 1405 | } |
| 1406 | delayed_service_names_.emplace_back(service.name()); |
| 1407 | } |
| 1408 | |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 1409 | Result<Success> ServiceParser::ParseSection(std::vector<std::string>&& args, |
| 1410 | const std::string& filename, int line) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1411 | if (args.size() < 3) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 1412 | return Error() << "services must have a name and a program"; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | const std::string& name = args[1]; |
| 1416 | if (!IsValidName(name)) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 1417 | return Error() << "invalid service name '" << name << "'"; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
Jiyong Park | 80aa447 | 2018-11-12 12:08:41 +0900 | [diff] [blame] | 1420 | filename_ = filename; |
| 1421 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1422 | Subcontext* restart_action_subcontext = nullptr; |
| 1423 | if (subcontexts_) { |
| 1424 | for (auto& subcontext : *subcontexts_) { |
Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 1425 | if (StartsWith(filename, subcontext.path_prefix())) { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1426 | restart_action_subcontext = &subcontext; |
| 1427 | break; |
| 1428 | } |
| 1429 | } |
| 1430 | } |
| 1431 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1432 | std::vector<std::string> str_args(args.begin() + 2, args.end()); |
Tom Cherry | 40acb37 | 2018-08-01 13:41:12 -0700 | [diff] [blame] | 1433 | |
| 1434 | if (SelinuxGetVendorAndroidVersion() <= __ANDROID_API_P__) { |
| 1435 | if (str_args[0] == "/sbin/watchdogd") { |
| 1436 | str_args[0] = "/system/bin/watchdogd"; |
| 1437 | } |
| 1438 | } |
| 1439 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1440 | service_ = std::make_unique<Service>(name, restart_action_subcontext, str_args); |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 1441 | return Success(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 1444 | Result<Success> ServiceParser::ParseLineSection(std::vector<std::string>&& args, int line) { |
| 1445 | return service_ ? service_->ParseLine(std::move(args)) : Success(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
Steven Moreland | 7d0a5c3 | 2017-11-10 14:20:47 -0800 | [diff] [blame] | 1448 | Result<Success> ServiceParser::EndSection() { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1449 | if (service_) { |
Steven Moreland | 5e1bea3 | 2017-11-10 14:43:58 -0800 | [diff] [blame] | 1450 | Service* old_service = service_list_->FindService(service_->name()); |
| 1451 | if (old_service) { |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 1452 | if (!service_->is_override()) { |
| 1453 | return Error() << "ignored duplicate definition of service '" << service_->name() |
| 1454 | << "'"; |
| 1455 | } |
| 1456 | |
Jiyong Park | 80aa447 | 2018-11-12 12:08:41 +0900 | [diff] [blame] | 1457 | if (StartsWith(filename_, "/apex/") && !old_service->is_updatable()) { |
| 1458 | return Error() << "cannot update a non-updatable service '" << service_->name() |
| 1459 | << "' with a config in APEX"; |
| 1460 | } |
| 1461 | |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 1462 | service_list_->RemoveService(*old_service); |
| 1463 | old_service = nullptr; |
Steven Moreland | 5e1bea3 | 2017-11-10 14:43:58 -0800 | [diff] [blame] | 1464 | } |
| 1465 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 1466 | service_list_->AddService(std::move(service_)); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1467 | } |
Steven Moreland | 7d0a5c3 | 2017-11-10 14:20:47 -0800 | [diff] [blame] | 1468 | |
| 1469 | return Success(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | bool ServiceParser::IsValidName(const std::string& name) const { |
Elliott Hughes | b7788fd | 2017-02-28 09:54:36 -0800 | [diff] [blame] | 1473 | // Property names can be any length, but may only contain certain characters. |
| 1474 | // Property values can contain any characters, but may only be a certain length. |
| 1475 | // (The latter restriction is needed because `start` and `stop` work by writing |
| 1476 | // the service name to the "ctl.start" and "ctl.stop" properties.) |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 1477 | return IsLegalPropertyName("init.svc." + name) && name.size() <= PROP_VALUE_MAX; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1478 | } |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 1479 | |
| 1480 | } // namespace init |
| 1481 | } // namespace android |