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