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