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