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