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 | |
Tom Cherry | ad9e7ea | 2018-10-15 17:21:48 -0700 | [diff] [blame] | 372 | // If we crash > 4 times in 4 minutes, reboot into bootloader. |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 403 | Result<Success> Service::ParseCapabilities(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 432 | Result<Success> Service::ParseClass(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 437 | Result<Success> Service::ParseConsole(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 443 | Result<Success> Service::ParseCritical(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 448 | Result<Success> Service::ParseDisabled(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 454 | Result<Success> Service::ParseEnterNamespace(std::vector<std::string>&& args) { |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 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; |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 464 | namespaces_to_enter_.emplace_back(CLONE_NEWNET, std::move(args[2])); |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 465 | return Success(); |
| 466 | } |
| 467 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 468 | Result<Success> Service::ParseGroup(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 485 | Result<Success> Service::ParsePriority(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 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 496 | Result<Success> Service::ParseInterface(std::vector<std::string>&& args) { |
Steven Moreland | e055d73 | 2017-10-05 18:50:22 -0700 | [diff] [blame] | 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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 527 | Result<Success> Service::ParseIoprio(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 545 | Result<Success> Service::ParseKeycodes(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 560 | Result<Success> Service::ParseOneshot(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 565 | Result<Success> Service::ParseOnrestart(std::vector<std::string>&& args) { |
| 566 | args.erase(args.begin()); |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 567 | int line = onrestart_.NumCommands() + 1; |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 568 | if (auto result = onrestart_.AddCommand(std::move(args), line); !result) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 574 | Result<Success> Service::ParseNamespace(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 589 | Result<Success> Service::ParseOomScoreAdjust(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 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 596 | Result<Success> Service::ParseOverride(std::vector<std::string>&& args) { |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 597 | override_ = true; |
| 598 | return Success(); |
| 599 | } |
| 600 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 601 | Result<Success> Service::ParseMemcgSwappiness(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 608 | Result<Success> Service::ParseMemcgLimitInBytes(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 615 | Result<Success> Service::ParseMemcgSoftLimitInBytes(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 622 | Result<Success> Service::ParseProcessRlimit(std::vector<std::string>&& args) { |
Tom Cherry | 7ac013d | 2017-08-25 10:39:25 -0700 | [diff] [blame] | 623 | auto rlimit = ParseRlimit(args); |
| 624 | if (!rlimit) return rlimit.error(); |
| 625 | |
| 626 | rlimits_.emplace_back(*rlimit); |
| 627 | return Success(); |
| 628 | } |
| 629 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 630 | Result<Success> Service::ParseRestartPeriod(std::vector<std::string>&& args) { |
Tom Cherry | 73f535e | 2018-09-27 16:10:46 -0700 | [diff] [blame] | 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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 639 | Result<Success> Service::ParseSeclabel(std::vector<std::string>&& args) { |
| 640 | seclabel_ = std::move(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 644 | Result<Success> Service::ParseSigstop(std::vector<std::string>&& args) { |
Tom Cherry | 8f38048 | 2018-04-17 14:48:44 -0700 | [diff] [blame] | 645 | sigstop_ = true; |
| 646 | return Success(); |
| 647 | } |
| 648 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 649 | Result<Success> Service::ParseSetenv(std::vector<std::string>&& args) { |
| 650 | environment_vars_.emplace_back(std::move(args[1]), std::move(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 654 | Result<Success> Service::ParseShutdown(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 662 | Result<Success> Service::ParseTimeoutPeriod(std::vector<std::string>&& args) { |
Tom Cherry | 73f535e | 2018-09-27 16:10:46 -0700 | [diff] [blame] | 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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 672 | Result<Success> Service::AddDescriptor(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 707 | Result<Success> Service::ParseSocket(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 712 | return AddDescriptor<SocketInfo>(std::move(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 716 | Result<Success> Service::ParseFile(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 723 | return AddDescriptor<FileInfo>(std::move(args)); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 726 | Result<Success> Service::ParseUser(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 | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 735 | Result<Success> Service::ParseWritepid(std::vector<std::string>&& args) { |
| 736 | args.erase(args.begin()); |
| 737 | writepid_files_ = std::move(args); |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 738 | return Success(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 741 | class Service::OptionParserMap : public KeywordMap<OptionParser> { |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 742 | public: |
| 743 | OptionParserMap() {} |
| 744 | |
| 745 | private: |
| 746 | const Map& map() const override; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 747 | }; |
| 748 | |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 749 | const Service::OptionParserMap::Map& Service::OptionParserMap::map() const { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 750 | constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max(); |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 751 | // clang-format off |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 752 | static const Map option_parsers = { |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 753 | {"capabilities", |
| 754 | {1, kMax, &Service::ParseCapabilities}}, |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 755 | {"class", {1, kMax, &Service::ParseClass}}, |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 756 | {"console", {0, 1, &Service::ParseConsole}}, |
| 757 | {"critical", {0, 0, &Service::ParseCritical}}, |
| 758 | {"disabled", {0, 0, &Service::ParseDisabled}}, |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 759 | {"enter_namespace", |
| 760 | {2, 2, &Service::ParseEnterNamespace}}, |
Tom Cherry | e2f341e | 2018-03-08 13:51:10 -0800 | [diff] [blame] | 761 | {"file", {2, 2, &Service::ParseFile}}, |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 762 | {"group", {1, NR_SVC_SUPP_GIDS + 1, &Service::ParseGroup}}, |
Steven Moreland | e055d73 | 2017-10-05 18:50:22 -0700 | [diff] [blame] | 763 | {"interface", {2, 2, &Service::ParseInterface}}, |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 764 | {"ioprio", {2, 2, &Service::ParseIoprio}}, |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 765 | {"keycodes", {1, kMax, &Service::ParseKeycodes}}, |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 766 | {"memcg.limit_in_bytes", |
| 767 | {1, 1, &Service::ParseMemcgLimitInBytes}}, |
Tom Cherry | e2f341e | 2018-03-08 13:51:10 -0800 | [diff] [blame] | 768 | {"memcg.soft_limit_in_bytes", |
| 769 | {1, 1, &Service::ParseMemcgSoftLimitInBytes}}, |
| 770 | {"memcg.swappiness", |
| 771 | {1, 1, &Service::ParseMemcgSwappiness}}, |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 772 | {"namespace", {1, 2, &Service::ParseNamespace}}, |
Tom Cherry | e2f341e | 2018-03-08 13:51:10 -0800 | [diff] [blame] | 773 | {"oneshot", {0, 0, &Service::ParseOneshot}}, |
| 774 | {"onrestart", {1, kMax, &Service::ParseOnrestart}}, |
| 775 | {"oom_score_adjust", |
| 776 | {1, 1, &Service::ParseOomScoreAdjust}}, |
| 777 | {"override", {0, 0, &Service::ParseOverride}}, |
| 778 | {"priority", {1, 1, &Service::ParsePriority}}, |
Tom Cherry | 73f535e | 2018-09-27 16:10:46 -0700 | [diff] [blame] | 779 | {"restart_period", |
| 780 | {1, 1, &Service::ParseRestartPeriod}}, |
Tom Cherry | 7ac013d | 2017-08-25 10:39:25 -0700 | [diff] [blame] | 781 | {"rlimit", {3, 3, &Service::ParseProcessRlimit}}, |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 782 | {"seclabel", {1, 1, &Service::ParseSeclabel}}, |
| 783 | {"setenv", {2, 2, &Service::ParseSetenv}}, |
Keun-young Park | cccb34f | 2017-07-05 11:38:44 -0700 | [diff] [blame] | 784 | {"shutdown", {1, 1, &Service::ParseShutdown}}, |
Tom Cherry | 8f38048 | 2018-04-17 14:48:44 -0700 | [diff] [blame] | 785 | {"sigstop", {0, 0, &Service::ParseSigstop}}, |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 786 | {"socket", {3, 6, &Service::ParseSocket}}, |
Tom Cherry | 73f535e | 2018-09-27 16:10:46 -0700 | [diff] [blame] | 787 | {"timeout_period", |
| 788 | {1, 1, &Service::ParseTimeoutPeriod}}, |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 789 | {"user", {1, 1, &Service::ParseUser}}, |
| 790 | {"writepid", {1, kMax, &Service::ParseWritepid}}, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 791 | }; |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 792 | // clang-format on |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 793 | return option_parsers; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 796 | Result<Success> Service::ParseLine(std::vector<std::string>&& args) { |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 797 | static const OptionParserMap parser_map; |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 798 | auto parser = parser_map.FindFunction(args); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 799 | |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 800 | if (!parser) return parser.error(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 801 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame^] | 802 | return std::invoke(*parser, this, std::move(args)); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 805 | Result<Success> Service::ExecStart() { |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 806 | flags_ |= SVC_ONESHOT; |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 807 | |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 808 | if (auto result = Start(); !result) { |
| 809 | return result; |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 810 | } |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 811 | |
| 812 | flags_ |= SVC_EXEC; |
| 813 | is_exec_service_running_ = true; |
| 814 | |
Wei Wang | 2c4ee75 | 2018-06-20 14:54:52 -0700 | [diff] [blame] | 815 | LOG(INFO) << "SVC_EXEC service '" << name_ << "' pid " << pid_ << " (uid " << uid_ << " gid " |
| 816 | << gid_ << "+" << supp_gids_.size() << " context " |
| 817 | << (!seclabel_.empty() ? seclabel_ : "default") << ") started; waiting..."; |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 818 | |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 819 | return Success(); |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 822 | Result<Success> Service::Start() { |
Tao Wu | 990d43c | 2017-10-26 10:43:10 -0700 | [diff] [blame] | 823 | bool disabled = (flags_ & (SVC_DISABLED | SVC_RESET)); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 824 | // Starting a service removes it from the disabled or reset state and |
| 825 | // immediately takes it out of the restarting state if it was in there. |
| 826 | flags_ &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START)); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 827 | |
| 828 | // Running processes require no additional work --- if they're in the |
| 829 | // process of exiting, we've ensured that they will immediately restart |
Tao Wu | 990d43c | 2017-10-26 10:43:10 -0700 | [diff] [blame] | 830 | // on exit, unless they are ONESHOT. For ONESHOT service, if it's in |
| 831 | // stopping status, we just set SVC_RESTART flag so it will get restarted |
| 832 | // in Reap(). |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 833 | if (flags_ & SVC_RUNNING) { |
Tao Wu | 990d43c | 2017-10-26 10:43:10 -0700 | [diff] [blame] | 834 | if ((flags_ & SVC_ONESHOT) && disabled) { |
| 835 | flags_ |= SVC_RESTART; |
| 836 | } |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 837 | // It is not an error to try to start a service that is already running. |
| 838 | return Success(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | bool needs_console = (flags_ & SVC_CONSOLE); |
Viorel Suman | 70daa67 | 2016-03-21 10:08:07 +0200 | [diff] [blame] | 842 | if (needs_console) { |
| 843 | if (console_.empty()) { |
| 844 | console_ = default_console; |
| 845 | } |
| 846 | |
Adrian Salido | 24ef860 | 2016-12-20 15:52:15 -0800 | [diff] [blame] | 847 | // Make sure that open call succeeds to ensure a console driver is |
| 848 | // properly registered for the device node |
| 849 | int console_fd = open(console_.c_str(), O_RDWR | O_CLOEXEC); |
| 850 | if (console_fd < 0) { |
Viorel Suman | 70daa67 | 2016-03-21 10:08:07 +0200 | [diff] [blame] | 851 | flags_ |= SVC_DISABLED; |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 852 | return ErrnoError() << "Couldn't open console '" << console_ << "'"; |
Viorel Suman | 70daa67 | 2016-03-21 10:08:07 +0200 | [diff] [blame] | 853 | } |
Adrian Salido | 24ef860 | 2016-12-20 15:52:15 -0800 | [diff] [blame] | 854 | close(console_fd); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | struct stat sb; |
| 858 | if (stat(args_[0].c_str(), &sb) == -1) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 859 | flags_ |= SVC_DISABLED; |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 860 | return ErrnoError() << "Cannot find '" << args_[0] << "'"; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 861 | } |
| 862 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 863 | std::string scon; |
| 864 | if (!seclabel_.empty()) { |
| 865 | scon = seclabel_; |
| 866 | } else { |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 867 | auto result = ComputeContextFromExecutable(args_[0]); |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 868 | if (!result) { |
| 869 | return result.error(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 870 | } |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 871 | scon = *result; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Wei Wang | a285dac | 2016-10-04 14:05:39 -0700 | [diff] [blame] | 874 | LOG(INFO) << "starting service '" << name_ << "'..."; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 875 | |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 876 | pid_t pid = -1; |
| 877 | if (namespace_flags_) { |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 878 | pid = clone(nullptr, nullptr, namespace_flags_ | SIGCHLD, nullptr); |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 879 | } else { |
| 880 | pid = fork(); |
| 881 | } |
| 882 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 883 | if (pid == 0) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 884 | umask(077); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 885 | |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 886 | if (auto result = EnterNamespaces(); !result) { |
| 887 | LOG(FATAL) << "Service '" << name_ << "' could not enter namespaces: " << result.error(); |
| 888 | } |
| 889 | |
| 890 | if (namespace_flags_ & CLONE_NEWNS) { |
| 891 | if (auto result = SetUpMountNamespace(); !result) { |
| 892 | LOG(FATAL) << "Service '" << name_ |
| 893 | << "' could not set up mount namespace: " << result.error(); |
| 894 | } |
| 895 | } |
| 896 | |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 897 | if (namespace_flags_ & CLONE_NEWPID) { |
| 898 | // This will fork again to run an init process inside the PID |
| 899 | // namespace. |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 900 | if (auto result = SetUpPidNamespace(); !result) { |
| 901 | LOG(FATAL) << "Service '" << name_ |
| 902 | << "' could not set up PID namespace: " << result.error(); |
| 903 | } |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 904 | } |
| 905 | |
Tom Cherry | 6de21f1 | 2017-08-22 15:41:03 -0700 | [diff] [blame] | 906 | for (const auto& [key, value] : environment_vars_) { |
| 907 | setenv(key.c_str(), value.c_str(), 1); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 908 | } |
| 909 | |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 910 | std::for_each(descriptors_.begin(), descriptors_.end(), |
| 911 | std::bind(&DescriptorInfo::CreateAndPublish, std::placeholders::_1, scon)); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 912 | |
Alex Vakulenko | 0828676 | 2016-05-03 12:00:00 -0700 | [diff] [blame] | 913 | // See if there were "writepid" instructions to write to files under /dev/cpuset/. |
| 914 | auto cpuset_predicate = [](const std::string& path) { |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 915 | return StartsWith(path, "/dev/cpuset/"); |
Alex Vakulenko | 0828676 | 2016-05-03 12:00:00 -0700 | [diff] [blame] | 916 | }; |
| 917 | auto iter = std::find_if(writepid_files_.begin(), writepid_files_.end(), cpuset_predicate); |
| 918 | if (iter == writepid_files_.end()) { |
| 919 | // There were no "writepid" instructions for cpusets, check if the system default |
| 920 | // cpuset is specified to be used for the process. |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 921 | std::string default_cpuset = GetProperty("ro.cpuset.default", ""); |
Alex Vakulenko | 0828676 | 2016-05-03 12:00:00 -0700 | [diff] [blame] | 922 | if (!default_cpuset.empty()) { |
| 923 | // Make sure the cpuset name starts and ends with '/'. |
| 924 | // A single '/' means the 'root' cpuset. |
| 925 | if (default_cpuset.front() != '/') { |
| 926 | default_cpuset.insert(0, 1, '/'); |
| 927 | } |
| 928 | if (default_cpuset.back() != '/') { |
| 929 | default_cpuset.push_back('/'); |
| 930 | } |
| 931 | writepid_files_.push_back( |
| 932 | StringPrintf("/dev/cpuset%stasks", default_cpuset.c_str())); |
| 933 | } |
| 934 | } |
Tom Cherry | 1c3a53f | 2017-06-22 16:50:31 -0700 | [diff] [blame] | 935 | std::string pid_str = std::to_string(getpid()); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 936 | for (const auto& file : writepid_files_) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 937 | if (!WriteStringToFile(pid_str, file)) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 938 | PLOG(ERROR) << "couldn't write " << pid_str << " to " << file; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | |
| 942 | if (ioprio_class_ != IoSchedClass_NONE) { |
| 943 | if (android_set_ioprio(getpid(), ioprio_class_, ioprio_pri_)) { |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 944 | PLOG(ERROR) << "failed to set pid " << getpid() |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 945 | << " ioprio=" << ioprio_class_ << "," << ioprio_pri_; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | |
| 949 | if (needs_console) { |
| 950 | setsid(); |
| 951 | OpenConsole(); |
| 952 | } else { |
| 953 | ZapStdio(); |
| 954 | } |
| 955 | |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 956 | // As requested, set our gid, supplemental gids, uid, context, and |
| 957 | // priority. Aborts on failure. |
| 958 | SetProcessAttributes(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 959 | |
Tom Cherry | 8f38048 | 2018-04-17 14:48:44 -0700 | [diff] [blame] | 960 | if (!ExpandArgsAndExecv(args_, sigstop_)) { |
Tom Cherry | 5e405ca | 2017-09-11 16:08:54 -0700 | [diff] [blame] | 961 | PLOG(ERROR) << "cannot execve('" << args_[0] << "')"; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | _exit(127); |
| 965 | } |
| 966 | |
| 967 | if (pid < 0) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 968 | pid_ = 0; |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 969 | return ErrnoError() << "Failed to fork"; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 970 | } |
| 971 | |
Marco Nelissen | 310f670 | 2016-07-22 12:07:06 -0700 | [diff] [blame] | 972 | if (oom_score_adjust_ != -1000) { |
Tom Cherry | 1c3a53f | 2017-06-22 16:50:31 -0700 | [diff] [blame] | 973 | std::string oom_str = std::to_string(oom_score_adjust_); |
Marco Nelissen | 310f670 | 2016-07-22 12:07:06 -0700 | [diff] [blame] | 974 | std::string oom_file = StringPrintf("/proc/%d/oom_score_adj", pid); |
| 975 | if (!WriteStringToFile(oom_str, oom_file)) { |
| 976 | PLOG(ERROR) << "couldn't write oom_score_adj: " << strerror(errno); |
| 977 | } |
| 978 | } |
| 979 | |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 980 | time_started_ = boot_clock::now(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 981 | pid_ = pid; |
| 982 | flags_ |= SVC_RUNNING; |
Tom Cherry | 5938379 | 2017-07-26 16:09:09 -0700 | [diff] [blame] | 983 | start_order_ = next_start_order_++; |
Tom Cherry | 33838b1 | 2017-05-04 11:32:36 -0700 | [diff] [blame] | 984 | process_cgroup_empty_ = false; |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 985 | |
| 986 | errno = -createProcessGroup(uid_, pid_); |
| 987 | if (errno != 0) { |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 988 | PLOG(ERROR) << "createProcessGroup(" << uid_ << ", " << pid_ << ") failed for service '" |
| 989 | << name_ << "'"; |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 990 | } else { |
| 991 | if (swappiness_ != -1) { |
| 992 | if (!setProcessGroupSwappiness(uid_, pid_, swappiness_)) { |
| 993 | PLOG(ERROR) << "setProcessGroupSwappiness failed"; |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | if (soft_limit_in_bytes_ != -1) { |
| 998 | if (!setProcessGroupSoftLimit(uid_, pid_, soft_limit_in_bytes_)) { |
| 999 | PLOG(ERROR) << "setProcessGroupSoftLimit failed"; |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | if (limit_in_bytes_ != -1) { |
| 1004 | if (!setProcessGroupLimit(uid_, pid_, limit_in_bytes_)) { |
| 1005 | PLOG(ERROR) << "setProcessGroupLimit failed"; |
| 1006 | } |
| 1007 | } |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 1008 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1009 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1010 | NotifyStateChange("running"); |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 1011 | return Success(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 1014 | Result<Success> Service::StartIfNotDisabled() { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1015 | if (!(flags_ & SVC_DISABLED)) { |
| 1016 | return Start(); |
| 1017 | } else { |
| 1018 | flags_ |= SVC_DISABLED_START; |
| 1019 | } |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 1020 | return Success(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 1023 | Result<Success> Service::Enable() { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1024 | flags_ &= ~(SVC_DISABLED | SVC_RC_DISABLED); |
| 1025 | if (flags_ & SVC_DISABLED_START) { |
| 1026 | return Start(); |
| 1027 | } |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 1028 | return Success(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | void Service::Reset() { |
| 1032 | StopOrReset(SVC_RESET); |
| 1033 | } |
| 1034 | |
| 1035 | void Service::Stop() { |
| 1036 | StopOrReset(SVC_DISABLED); |
| 1037 | } |
| 1038 | |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 1039 | void Service::Terminate() { |
| 1040 | flags_ &= ~(SVC_RESTARTING | SVC_DISABLED_START); |
| 1041 | flags_ |= SVC_DISABLED; |
| 1042 | if (pid_) { |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 1043 | KillProcessGroup(SIGTERM); |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 1044 | NotifyStateChange("stopping"); |
| 1045 | } |
| 1046 | } |
| 1047 | |
Tom Cherry | 73f535e | 2018-09-27 16:10:46 -0700 | [diff] [blame] | 1048 | void Service::Timeout() { |
| 1049 | // All process state flags will be taken care of in Reap(), we really just want to kill the |
| 1050 | // process here when it times out. Oneshot processes will transition to be disabled, and |
| 1051 | // all other processes will transition to be restarting. |
| 1052 | LOG(INFO) << "Service '" << name_ << "' expired its timeout of " << timeout_period_->count() |
| 1053 | << " seconds and will now be killed"; |
| 1054 | if (pid_) { |
| 1055 | KillProcessGroup(SIGKILL); |
| 1056 | NotifyStateChange("stopping"); |
| 1057 | } |
| 1058 | } |
| 1059 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1060 | void Service::Restart() { |
| 1061 | if (flags_ & SVC_RUNNING) { |
| 1062 | /* Stop, wait, then start the service. */ |
| 1063 | StopOrReset(SVC_RESTART); |
| 1064 | } else if (!(flags_ & SVC_RESTARTING)) { |
| 1065 | /* Just start the service since it's not running. */ |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 1066 | if (auto result = Start(); !result) { |
| 1067 | LOG(ERROR) << "Could not restart '" << name_ << "': " << result.error(); |
| 1068 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1069 | } /* else: Service is restarting anyways. */ |
| 1070 | } |
| 1071 | |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 1072 | // 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] | 1073 | void Service::StopOrReset(int how) { |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 1074 | // The service is still SVC_RUNNING until its process exits, but if it has |
| 1075 | // already exited it shoudn't attempt a restart yet. |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1076 | flags_ &= ~(SVC_RESTARTING | SVC_DISABLED_START); |
| 1077 | |
| 1078 | if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) { |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 1079 | // An illegal flag: default to SVC_DISABLED. |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1080 | how = SVC_DISABLED; |
| 1081 | } |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 1082 | |
| 1083 | // 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] | 1084 | if (how == SVC_RESET) { |
| 1085 | flags_ |= (flags_ & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET; |
| 1086 | } else { |
| 1087 | flags_ |= how; |
| 1088 | } |
Tao Wu | 84b856d | 2017-10-27 11:29:13 -0700 | [diff] [blame] | 1089 | // Make sure it's in right status when a restart immediately follow a |
| 1090 | // stop/reset or vice versa. |
| 1091 | if (how == SVC_RESTART) { |
| 1092 | flags_ &= (~(SVC_DISABLED | SVC_RESET)); |
| 1093 | } else { |
| 1094 | flags_ &= (~SVC_RESTART); |
| 1095 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1096 | |
| 1097 | if (pid_) { |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 1098 | KillProcessGroup(SIGKILL); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1099 | NotifyStateChange("stopping"); |
| 1100 | } else { |
| 1101 | NotifyStateChange("stopped"); |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | void Service::ZapStdio() const { |
| 1106 | int fd; |
| 1107 | fd = open("/dev/null", O_RDWR); |
| 1108 | dup2(fd, 0); |
| 1109 | dup2(fd, 1); |
| 1110 | dup2(fd, 2); |
| 1111 | close(fd); |
| 1112 | } |
| 1113 | |
| 1114 | void Service::OpenConsole() const { |
Viorel Suman | 70daa67 | 2016-03-21 10:08:07 +0200 | [diff] [blame] | 1115 | int fd = open(console_.c_str(), O_RDWR); |
| 1116 | if (fd == -1) fd = open("/dev/null", O_RDWR); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1117 | ioctl(fd, TIOCSCTTY, 0); |
| 1118 | dup2(fd, 0); |
| 1119 | dup2(fd, 1); |
| 1120 | dup2(fd, 2); |
| 1121 | close(fd); |
| 1122 | } |
| 1123 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 1124 | ServiceList::ServiceList() {} |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1125 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 1126 | ServiceList& ServiceList::GetInstance() { |
| 1127 | static ServiceList instance; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1128 | return instance; |
| 1129 | } |
| 1130 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 1131 | void ServiceList::AddService(std::unique_ptr<Service> service) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1132 | services_.emplace_back(std::move(service)); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 1135 | std::unique_ptr<Service> Service::MakeTemporaryOneshotService(const std::vector<std::string>& args) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1136 | // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS... |
| 1137 | // SECLABEL can be a - to denote default |
| 1138 | std::size_t command_arg = 1; |
| 1139 | for (std::size_t i = 1; i < args.size(); ++i) { |
| 1140 | if (args[i] == "--") { |
| 1141 | command_arg = i + 1; |
| 1142 | break; |
| 1143 | } |
| 1144 | } |
| 1145 | if (command_arg > 4 + NR_SVC_SUPP_GIDS) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 1146 | LOG(ERROR) << "exec called with too many supplementary group ids"; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1147 | return nullptr; |
| 1148 | } |
| 1149 | |
| 1150 | if (command_arg >= args.size()) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 1151 | LOG(ERROR) << "exec called without command"; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1152 | return nullptr; |
| 1153 | } |
| 1154 | std::vector<std::string> str_args(args.begin() + command_arg, args.end()); |
| 1155 | |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 1156 | static size_t exec_count = 0; |
| 1157 | exec_count++; |
| 1158 | std::string name = "exec " + std::to_string(exec_count) + " (" + Join(str_args, " ") + ")"; |
Tom Cherry | 86e31a8 | 2017-04-25 17:31:06 -0700 | [diff] [blame] | 1159 | |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 1160 | unsigned flags = SVC_ONESHOT | SVC_TEMPORARY; |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 1161 | CapSet no_capabilities; |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 1162 | unsigned namespace_flags = 0; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1163 | |
| 1164 | std::string seclabel = ""; |
| 1165 | if (command_arg > 2 && args[1] != "-") { |
| 1166 | seclabel = args[1]; |
| 1167 | } |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 1168 | Result<uid_t> uid = 0; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1169 | if (command_arg > 3) { |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 1170 | uid = DecodeUid(args[2]); |
| 1171 | if (!uid) { |
| 1172 | LOG(ERROR) << "Unable to decode UID for '" << args[2] << "': " << uid.error(); |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 1173 | return nullptr; |
| 1174 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1175 | } |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 1176 | Result<gid_t> gid = 0; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1177 | std::vector<gid_t> supp_gids; |
| 1178 | if (command_arg > 4) { |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 1179 | gid = DecodeUid(args[3]); |
| 1180 | if (!gid) { |
| 1181 | LOG(ERROR) << "Unable to decode GID for '" << args[3] << "': " << gid.error(); |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 1182 | return nullptr; |
| 1183 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1184 | std::size_t nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */; |
| 1185 | for (size_t i = 0; i < nr_supp_gids; ++i) { |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 1186 | auto supp_gid = DecodeUid(args[4 + i]); |
| 1187 | if (!supp_gid) { |
| 1188 | LOG(ERROR) << "Unable to decode GID for '" << args[4 + i] |
| 1189 | << "': " << supp_gid.error(); |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 1190 | return nullptr; |
| 1191 | } |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 1192 | supp_gids.push_back(*supp_gid); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1193 | } |
| 1194 | } |
| 1195 | |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 1196 | 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] | 1197 | namespace_flags, seclabel, nullptr, str_args); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
Tom Cherry | 5938379 | 2017-07-26 16:09:09 -0700 | [diff] [blame] | 1200 | // Shutdown services in the opposite order that they were started. |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 1201 | const std::vector<Service*> ServiceList::services_in_shutdown_order() const { |
Tom Cherry | 5938379 | 2017-07-26 16:09:09 -0700 | [diff] [blame] | 1202 | std::vector<Service*> shutdown_services; |
| 1203 | for (const auto& service : services_) { |
| 1204 | if (service->start_order() > 0) shutdown_services.emplace_back(service.get()); |
| 1205 | } |
| 1206 | std::sort(shutdown_services.begin(), shutdown_services.end(), |
| 1207 | [](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] | 1208 | return shutdown_services; |
Tom Cherry | 5938379 | 2017-07-26 16:09:09 -0700 | [diff] [blame] | 1209 | } |
| 1210 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 1211 | void ServiceList::RemoveService(const Service& svc) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1212 | auto svc_it = std::find_if(services_.begin(), services_.end(), |
| 1213 | [&svc] (const std::unique_ptr<Service>& s) { |
| 1214 | return svc.name() == s->name(); |
| 1215 | }); |
| 1216 | if (svc_it == services_.end()) { |
| 1217 | return; |
| 1218 | } |
| 1219 | |
| 1220 | services_.erase(svc_it); |
| 1221 | } |
| 1222 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 1223 | void ServiceList::DumpState() const { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1224 | for (const auto& s : services_) { |
| 1225 | s->DumpState(); |
| 1226 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 1229 | Result<Success> ServiceParser::ParseSection(std::vector<std::string>&& args, |
| 1230 | const std::string& filename, int line) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1231 | if (args.size() < 3) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 1232 | return Error() << "services must have a name and a program"; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | const std::string& name = args[1]; |
| 1236 | if (!IsValidName(name)) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 1237 | return Error() << "invalid service name '" << name << "'"; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1240 | Subcontext* restart_action_subcontext = nullptr; |
| 1241 | if (subcontexts_) { |
| 1242 | for (auto& subcontext : *subcontexts_) { |
Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 1243 | if (StartsWith(filename, subcontext.path_prefix())) { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1244 | restart_action_subcontext = &subcontext; |
| 1245 | break; |
| 1246 | } |
| 1247 | } |
| 1248 | } |
| 1249 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1250 | std::vector<std::string> str_args(args.begin() + 2, args.end()); |
Tom Cherry | 40acb37 | 2018-08-01 13:41:12 -0700 | [diff] [blame] | 1251 | |
| 1252 | if (SelinuxGetVendorAndroidVersion() <= __ANDROID_API_P__) { |
| 1253 | if (str_args[0] == "/sbin/watchdogd") { |
| 1254 | str_args[0] = "/system/bin/watchdogd"; |
| 1255 | } |
| 1256 | } |
| 1257 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 1258 | service_ = std::make_unique<Service>(name, restart_action_subcontext, str_args); |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 1259 | return Success(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 1262 | Result<Success> ServiceParser::ParseLineSection(std::vector<std::string>&& args, int line) { |
| 1263 | return service_ ? service_->ParseLine(std::move(args)) : Success(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
Steven Moreland | 7d0a5c3 | 2017-11-10 14:20:47 -0800 | [diff] [blame] | 1266 | Result<Success> ServiceParser::EndSection() { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1267 | if (service_) { |
Steven Moreland | 5e1bea3 | 2017-11-10 14:43:58 -0800 | [diff] [blame] | 1268 | Service* old_service = service_list_->FindService(service_->name()); |
| 1269 | if (old_service) { |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 1270 | if (!service_->is_override()) { |
| 1271 | return Error() << "ignored duplicate definition of service '" << service_->name() |
| 1272 | << "'"; |
| 1273 | } |
| 1274 | |
| 1275 | service_list_->RemoveService(*old_service); |
| 1276 | old_service = nullptr; |
Steven Moreland | 5e1bea3 | 2017-11-10 14:43:58 -0800 | [diff] [blame] | 1277 | } |
| 1278 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 1279 | service_list_->AddService(std::move(service_)); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1280 | } |
Steven Moreland | 7d0a5c3 | 2017-11-10 14:20:47 -0800 | [diff] [blame] | 1281 | |
| 1282 | return Success(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | bool ServiceParser::IsValidName(const std::string& name) const { |
Elliott Hughes | b7788fd | 2017-02-28 09:54:36 -0800 | [diff] [blame] | 1286 | // Property names can be any length, but may only contain certain characters. |
| 1287 | // Property values can contain any characters, but may only be a certain length. |
| 1288 | // (The latter restriction is needed because `start` and `stop` work by writing |
| 1289 | // the service name to the "ctl.start" and "ctl.stop" properties.) |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 1290 | return IsLegalPropertyName("init.svc." + name) && name.size() <= PROP_VALUE_MAX; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1291 | } |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 1292 | |
| 1293 | } // namespace init |
| 1294 | } // namespace android |