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