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