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