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