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