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> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <termios.h> |
Dan Albert | af9ba4d | 2015-08-11 16:37:04 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 24 | |
| 25 | #include <selinux/selinux.h> |
| 26 | |
| 27 | #include <base/file.h> |
| 28 | #include <base/stringprintf.h> |
| 29 | #include <cutils/android_reboot.h> |
| 30 | #include <cutils/sockets.h> |
| 31 | |
| 32 | #include "action.h" |
| 33 | #include "init.h" |
| 34 | #include "init_parser.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 35 | #include "log.h" |
| 36 | #include "property_service.h" |
| 37 | #include "util.h" |
| 38 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 39 | using android::base::StringPrintf; |
| 40 | using android::base::WriteStringToFile; |
| 41 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 42 | #define CRITICAL_CRASH_THRESHOLD 4 // if we crash >4 times ... |
| 43 | #define CRITICAL_CRASH_WINDOW (4*60) // ... in 4 minutes, goto recovery |
| 44 | |
| 45 | SocketInfo::SocketInfo() : uid(0), gid(0), perm(0) { |
| 46 | } |
| 47 | |
| 48 | SocketInfo::SocketInfo(const std::string& name, const std::string& type, uid_t uid, |
| 49 | gid_t gid, int perm, const std::string& socketcon) |
| 50 | : name(name), type(type), uid(uid), gid(gid), perm(perm), socketcon(socketcon) { |
| 51 | } |
| 52 | |
| 53 | ServiceEnvironmentInfo::ServiceEnvironmentInfo() { |
| 54 | } |
| 55 | |
| 56 | ServiceEnvironmentInfo::ServiceEnvironmentInfo(const std::string& name, |
| 57 | const std::string& value) |
| 58 | : name(name), value(value) { |
| 59 | } |
| 60 | |
| 61 | Service::Service(const std::string& name, const std::string& classname, |
| 62 | const std::vector<std::string>& args) |
| 63 | : name_(name), classname_(classname), flags_(0), pid_(0), time_started_(0), |
| 64 | time_crashed_(0), nr_crashed_(0), uid_(0), gid_(0), seclabel_(""), |
| 65 | ioprio_class_(IoSchedClass_NONE), ioprio_pri_(0), args_(args) { |
| 66 | onrestart_.InitSingleTrigger("onrestart"); |
| 67 | } |
| 68 | |
| 69 | Service::Service(const std::string& name, const std::string& classname, |
| 70 | unsigned flags, uid_t uid, gid_t gid, const std::vector<gid_t>& supp_gids, |
| 71 | const std::string& seclabel, const std::vector<std::string>& args) |
| 72 | : name_(name), classname_(classname), flags_(flags), pid_(0), time_started_(0), |
| 73 | time_crashed_(0), nr_crashed_(0), uid_(uid), gid_(gid), supp_gids_(supp_gids), |
| 74 | seclabel_(seclabel), ioprio_class_(IoSchedClass_NONE), ioprio_pri_(0), args_(args) { |
| 75 | onrestart_.InitSingleTrigger("onrestart"); |
| 76 | } |
| 77 | |
| 78 | void Service::NotifyStateChange(const std::string& new_state) const { |
| 79 | if (!properties_initialized()) { |
| 80 | // If properties aren't available yet, we can't set them. |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | if ((flags_ & SVC_EXEC) != 0) { |
| 85 | // 'exec' commands don't have properties tracking their state. |
| 86 | return; |
| 87 | } |
| 88 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 89 | std::string prop_name = StringPrintf("init.svc.%s", name_.c_str()); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 90 | if (prop_name.length() >= PROP_NAME_MAX) { |
| 91 | // If the property name would be too long, we can't set it. |
| 92 | ERROR("Property name \"init.svc.%s\" too long; not setting to %s\n", |
| 93 | name_.c_str(), new_state.c_str()); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | property_set(prop_name.c_str(), new_state.c_str()); |
| 98 | } |
| 99 | |
| 100 | bool Service::Reap() { |
| 101 | if (!(flags_ & SVC_ONESHOT) || (flags_ & SVC_RESTART)) { |
| 102 | NOTICE("Service '%s' (pid %d) killing any children in process group\n", |
| 103 | name_.c_str(), pid_); |
| 104 | kill(-pid_, SIGKILL); |
| 105 | } |
| 106 | |
| 107 | // Remove any sockets we may have created. |
| 108 | for (const auto& si : sockets_) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 109 | std::string tmp = StringPrintf(ANDROID_SOCKET_DIR "/%s", si.name.c_str()); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 110 | unlink(tmp.c_str()); |
| 111 | } |
| 112 | |
| 113 | if (flags_ & SVC_EXEC) { |
| 114 | INFO("SVC_EXEC pid %d finished...\n", pid_); |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | pid_ = 0; |
| 119 | flags_ &= (~SVC_RUNNING); |
| 120 | |
| 121 | // Oneshot processes go into the disabled state on exit, |
| 122 | // except when manually restarted. |
| 123 | if ((flags_ & SVC_ONESHOT) && !(flags_ & SVC_RESTART)) { |
| 124 | flags_ |= SVC_DISABLED; |
| 125 | } |
| 126 | |
| 127 | // Disabled and reset processes do not get restarted automatically. |
| 128 | if (flags_ & (SVC_DISABLED | SVC_RESET)) { |
| 129 | NotifyStateChange("stopped"); |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | time_t now = gettime(); |
| 134 | if ((flags_ & SVC_CRITICAL) && !(flags_ & SVC_RESTART)) { |
| 135 | if (time_crashed_ + CRITICAL_CRASH_WINDOW >= now) { |
| 136 | if (++nr_crashed_ > CRITICAL_CRASH_THRESHOLD) { |
| 137 | ERROR("critical process '%s' exited %d times in %d minutes; " |
| 138 | "rebooting into recovery mode\n", name_.c_str(), |
| 139 | CRITICAL_CRASH_THRESHOLD, CRITICAL_CRASH_WINDOW / 60); |
| 140 | android_reboot(ANDROID_RB_RESTART2, 0, "recovery"); |
| 141 | return false; |
| 142 | } |
| 143 | } else { |
| 144 | time_crashed_ = now; |
| 145 | nr_crashed_ = 1; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | flags_ &= (~SVC_RESTART); |
| 150 | flags_ |= SVC_RESTARTING; |
| 151 | |
| 152 | // Execute all onrestart commands for this service. |
| 153 | onrestart_.ExecuteAllCommands(); |
| 154 | |
| 155 | NotifyStateChange("restarting"); |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | void Service::DumpState() const { |
| 160 | INFO("service %s\n", name_.c_str()); |
| 161 | INFO(" class '%s'\n", classname_.c_str()); |
| 162 | INFO(" exec"); |
| 163 | for (const auto& s : args_) { |
| 164 | INFO(" '%s'", s.c_str()); |
| 165 | } |
| 166 | INFO("\n"); |
| 167 | for (const auto& si : sockets_) { |
| 168 | INFO(" socket %s %s 0%o\n", si.name.c_str(), si.type.c_str(), si.perm); |
| 169 | } |
| 170 | } |
| 171 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 172 | bool Service::HandleClass(const std::vector<std::string>& args, std::string* err) { |
| 173 | classname_ = args[1]; |
| 174 | return true; |
| 175 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 176 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 177 | bool Service::HandleConsole(const std::vector<std::string>& args, std::string* err) { |
| 178 | flags_ |= SVC_CONSOLE; |
| 179 | return true; |
| 180 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 181 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 182 | bool Service::HandleCritical(const std::vector<std::string>& args, std::string* err) { |
| 183 | flags_ |= SVC_CRITICAL; |
| 184 | return true; |
| 185 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 186 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 187 | bool Service::HandleDisabled(const std::vector<std::string>& args, std::string* err) { |
| 188 | flags_ |= SVC_DISABLED; |
| 189 | flags_ |= SVC_RC_DISABLED; |
| 190 | return true; |
| 191 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 192 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 193 | bool Service::HandleGroup(const std::vector<std::string>& args, std::string* err) { |
| 194 | gid_ = decode_uid(args[1].c_str()); |
| 195 | for (std::size_t n = 2; n < args.size(); n++) { |
| 196 | supp_gids_.emplace_back(decode_uid(args[n].c_str())); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 197 | } |
| 198 | return true; |
| 199 | } |
| 200 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 201 | bool Service::HandleIoprio(const std::vector<std::string>& args, std::string* err) { |
| 202 | ioprio_pri_ = std::stoul(args[2], 0, 8); |
| 203 | |
| 204 | if (ioprio_pri_ < 0 || ioprio_pri_ > 7) { |
| 205 | *err = "priority value must be range 0 - 7"; |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | if (args[1] == "rt") { |
| 210 | ioprio_class_ = IoSchedClass_RT; |
| 211 | } else if (args[1] == "be") { |
| 212 | ioprio_class_ = IoSchedClass_BE; |
| 213 | } else if (args[1] == "idle") { |
| 214 | ioprio_class_ = IoSchedClass_IDLE; |
| 215 | } else { |
| 216 | *err = "ioprio option usage: ioprio <rt|be|idle> <0-7>"; |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | bool Service::HandleKeycodes(const std::vector<std::string>& args, std::string* err) { |
| 224 | for (std::size_t i = 1; i < args.size(); i++) { |
| 225 | keycodes_.emplace_back(std::stoi(args[i])); |
| 226 | } |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | bool Service::HandleOneshot(const std::vector<std::string>& args, std::string* err) { |
| 231 | flags_ |= SVC_ONESHOT; |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | bool Service::HandleOnrestart(const std::vector<std::string>& args, std::string* err) { |
| 236 | std::vector<std::string> str_args(args.begin() + 1, args.end()); |
| 237 | onrestart_.AddCommand(str_args, "", 0, err); |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | bool Service::HandleSeclabel(const std::vector<std::string>& args, std::string* err) { |
| 242 | seclabel_ = args[1]; |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | bool Service::HandleSetenv(const std::vector<std::string>& args, std::string* err) { |
| 247 | envvars_.emplace_back(args[1], args[2]); |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | /* name type perm [ uid gid context ] */ |
| 252 | bool Service::HandleSocket(const std::vector<std::string>& args, std::string* err) { |
| 253 | if (args[2] != "dgram" && args[2] != "stream" && args[2] != "seqpacket") { |
| 254 | *err = "socket type must be 'dgram', 'stream' or 'seqpacket'"; |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | int perm = std::stoul(args[3], 0, 8); |
| 259 | uid_t uid = args.size() > 4 ? decode_uid(args[4].c_str()) : 0; |
| 260 | gid_t gid = args.size() > 5 ? decode_uid(args[5].c_str()) : 0; |
| 261 | std::string socketcon = args.size() > 6 ? args[6] : ""; |
| 262 | |
| 263 | sockets_.emplace_back(args[1], args[2], uid, gid, perm, socketcon); |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | bool Service::HandleUser(const std::vector<std::string>& args, std::string* err) { |
| 268 | uid_ = decode_uid(args[1].c_str()); |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | bool Service::HandleWritepid(const std::vector<std::string>& args, std::string* err) { |
| 273 | writepid_files_.assign(args.begin() + 1, args.end()); |
| 274 | return true; |
| 275 | } |
| 276 | |
| 277 | class Service::OptionHandlerMap : public KeywordMap<OptionHandler> { |
| 278 | public: |
| 279 | OptionHandlerMap() { |
| 280 | } |
| 281 | private: |
| 282 | Map& map() const override; |
| 283 | }; |
| 284 | |
| 285 | Service::OptionHandlerMap::Map& Service::OptionHandlerMap::map() const { |
| 286 | constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max(); |
| 287 | static const Map option_handlers = { |
| 288 | {"class", {1, 1, &Service::HandleClass}}, |
| 289 | {"console", {0, 0, &Service::HandleConsole}}, |
| 290 | {"critical", {0, 0, &Service::HandleCritical}}, |
| 291 | {"disabled", {0, 0, &Service::HandleDisabled}}, |
| 292 | {"group", {1, NR_SVC_SUPP_GIDS + 1, &Service::HandleGroup}}, |
| 293 | {"ioprio", {2, 2, &Service::HandleIoprio}}, |
| 294 | {"keycodes", {1, kMax, &Service::HandleKeycodes}}, |
| 295 | {"oneshot", {0, 0, &Service::HandleOneshot}}, |
| 296 | {"onrestart", {1, kMax, &Service::HandleOnrestart}}, |
| 297 | {"seclabel", {1, 1, &Service::HandleSeclabel}}, |
| 298 | {"setenv", {2, 2, &Service::HandleSetenv}}, |
| 299 | {"socket", {3, 6, &Service::HandleSocket}}, |
| 300 | {"user", {1, 1, &Service::HandleUser}}, |
| 301 | {"writepid", {1, kMax, &Service::HandleWritepid}}, |
| 302 | }; |
| 303 | return option_handlers; |
| 304 | } |
| 305 | |
| 306 | bool Service::HandleLine(const std::vector<std::string>& args, std::string* err) { |
| 307 | if (args.empty()) { |
| 308 | *err = "option needed, but not provided"; |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | static const OptionHandlerMap handler_map; |
| 313 | auto handler = handler_map.FindFunction(args[0], args.size() - 1, err); |
| 314 | |
| 315 | if (!handler) { |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | return (this->*handler)(args, err); |
| 320 | } |
| 321 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 322 | bool Service::Start(const std::vector<std::string>& dynamic_args) { |
| 323 | // Starting a service removes it from the disabled or reset state and |
| 324 | // immediately takes it out of the restarting state if it was in there. |
| 325 | flags_ &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START)); |
| 326 | time_started_ = 0; |
| 327 | |
| 328 | // Running processes require no additional work --- if they're in the |
| 329 | // process of exiting, we've ensured that they will immediately restart |
| 330 | // on exit, unless they are ONESHOT. |
| 331 | if (flags_ & SVC_RUNNING) { |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | bool needs_console = (flags_ & SVC_CONSOLE); |
| 336 | if (needs_console && !have_console) { |
| 337 | ERROR("service '%s' requires console\n", name_.c_str()); |
| 338 | flags_ |= SVC_DISABLED; |
| 339 | return false; |
| 340 | } |
| 341 | |
| 342 | struct stat sb; |
| 343 | if (stat(args_[0].c_str(), &sb) == -1) { |
| 344 | ERROR("cannot find '%s' (%s), disabling '%s'\n", |
| 345 | args_[0].c_str(), strerror(errno), name_.c_str()); |
| 346 | flags_ |= SVC_DISABLED; |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | if ((!(flags_ & SVC_ONESHOT)) && !dynamic_args.empty()) { |
| 351 | ERROR("service '%s' must be one-shot to use dynamic args, disabling\n", |
| 352 | args_[0].c_str()); |
| 353 | flags_ |= SVC_DISABLED; |
| 354 | return false; |
| 355 | } |
| 356 | |
| 357 | std::string scon; |
| 358 | if (!seclabel_.empty()) { |
| 359 | scon = seclabel_; |
| 360 | } else { |
| 361 | char* mycon = nullptr; |
| 362 | char* fcon = nullptr; |
| 363 | |
| 364 | INFO("computing context for service '%s'\n", args_[0].c_str()); |
| 365 | int rc = getcon(&mycon); |
| 366 | if (rc < 0) { |
| 367 | ERROR("could not get context while starting '%s'\n", name_.c_str()); |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | rc = getfilecon(args_[0].c_str(), &fcon); |
| 372 | if (rc < 0) { |
| 373 | ERROR("could not get context while starting '%s'\n", name_.c_str()); |
| 374 | free(mycon); |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | char* ret_scon = nullptr; |
| 379 | rc = security_compute_create(mycon, fcon, string_to_security_class("process"), |
| 380 | &ret_scon); |
| 381 | if (rc == 0) { |
| 382 | scon = ret_scon; |
| 383 | free(ret_scon); |
| 384 | } |
| 385 | if (rc == 0 && scon == mycon) { |
| 386 | ERROR("Service %s does not have a SELinux domain defined.\n", name_.c_str()); |
| 387 | free(mycon); |
| 388 | free(fcon); |
| 389 | return false; |
| 390 | } |
| 391 | free(mycon); |
| 392 | free(fcon); |
| 393 | if (rc < 0) { |
| 394 | ERROR("could not get context while starting '%s'\n", name_.c_str()); |
| 395 | return false; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | NOTICE("Starting service '%s'...\n", name_.c_str()); |
| 400 | |
| 401 | pid_t pid = fork(); |
| 402 | if (pid == 0) { |
| 403 | int fd, sz; |
| 404 | |
| 405 | umask(077); |
| 406 | if (properties_initialized()) { |
| 407 | get_property_workspace(&fd, &sz); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 408 | std::string tmp = StringPrintf("%d,%d", dup(fd), sz); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 409 | add_environment("ANDROID_PROPERTY_WORKSPACE", tmp.c_str()); |
| 410 | } |
| 411 | |
| 412 | for (const auto& ei : envvars_) { |
| 413 | add_environment(ei.name.c_str(), ei.value.c_str()); |
| 414 | } |
| 415 | |
| 416 | for (const auto& si : sockets_) { |
| 417 | int socket_type = ((si.type == "stream" ? SOCK_STREAM : |
| 418 | (si.type == "dgram" ? SOCK_DGRAM : |
| 419 | SOCK_SEQPACKET))); |
| 420 | const char* socketcon = |
| 421 | !si.socketcon.empty() ? si.socketcon.c_str() : scon.c_str(); |
| 422 | |
| 423 | int s = create_socket(si.name.c_str(), socket_type, si.perm, |
| 424 | si.uid, si.gid, socketcon); |
| 425 | if (s >= 0) { |
| 426 | PublishSocket(si.name, s); |
| 427 | } |
| 428 | } |
| 429 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 430 | std::string pid_str = StringPrintf("%d", pid); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 431 | for (const auto& file : writepid_files_) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 432 | if (!WriteStringToFile(pid_str, file)) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 433 | ERROR("couldn't write %s to %s: %s\n", |
| 434 | pid_str.c_str(), file.c_str(), strerror(errno)); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | if (ioprio_class_ != IoSchedClass_NONE) { |
| 439 | if (android_set_ioprio(getpid(), ioprio_class_, ioprio_pri_)) { |
| 440 | ERROR("Failed to set pid %d ioprio = %d,%d: %s\n", |
| 441 | getpid(), ioprio_class_, ioprio_pri_, strerror(errno)); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | if (needs_console) { |
| 446 | setsid(); |
| 447 | OpenConsole(); |
| 448 | } else { |
| 449 | ZapStdio(); |
| 450 | } |
| 451 | |
| 452 | setpgid(0, getpid()); |
| 453 | |
| 454 | // As requested, set our gid, supplemental gids, and uid. |
| 455 | if (gid_) { |
| 456 | if (setgid(gid_) != 0) { |
| 457 | ERROR("setgid failed: %s\n", strerror(errno)); |
| 458 | _exit(127); |
| 459 | } |
| 460 | } |
| 461 | if (!supp_gids_.empty()) { |
| 462 | if (setgroups(supp_gids_.size(), &supp_gids_[0]) != 0) { |
| 463 | ERROR("setgroups failed: %s\n", strerror(errno)); |
| 464 | _exit(127); |
| 465 | } |
| 466 | } |
| 467 | if (uid_) { |
| 468 | if (setuid(uid_) != 0) { |
| 469 | ERROR("setuid failed: %s\n", strerror(errno)); |
| 470 | _exit(127); |
| 471 | } |
| 472 | } |
| 473 | if (!seclabel_.empty()) { |
| 474 | if (setexeccon(seclabel_.c_str()) < 0) { |
| 475 | ERROR("cannot setexeccon('%s'): %s\n", |
| 476 | seclabel_.c_str(), strerror(errno)); |
| 477 | _exit(127); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | std::vector<char*> strs; |
| 482 | for (const auto& s : args_) { |
| 483 | strs.push_back(const_cast<char*>(s.c_str())); |
| 484 | } |
| 485 | for (const auto& s : dynamic_args) { |
| 486 | strs.push_back(const_cast<char*>(s.c_str())); |
| 487 | } |
| 488 | strs.push_back(nullptr); |
| 489 | if (execve(args_[0].c_str(), (char**) &strs[0], (char**) ENV) < 0) { |
| 490 | ERROR("cannot execve('%s'): %s\n", args_[0].c_str(), strerror(errno)); |
| 491 | } |
| 492 | |
| 493 | _exit(127); |
| 494 | } |
| 495 | |
| 496 | if (pid < 0) { |
| 497 | ERROR("failed to start '%s'\n", name_.c_str()); |
| 498 | pid_ = 0; |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | time_started_ = gettime(); |
| 503 | pid_ = pid; |
| 504 | flags_ |= SVC_RUNNING; |
| 505 | |
| 506 | if ((flags_ & SVC_EXEC) != 0) { |
| 507 | INFO("SVC_EXEC pid %d (uid %d gid %d+%zu context %s) started; waiting...\n", |
| 508 | pid_, uid_, gid_, supp_gids_.size(), |
| 509 | !seclabel_.empty() ? seclabel_.c_str() : "default"); |
| 510 | } |
| 511 | |
| 512 | NotifyStateChange("running"); |
| 513 | return true; |
| 514 | } |
| 515 | |
| 516 | bool Service::Start() { |
| 517 | const std::vector<std::string> null_dynamic_args; |
| 518 | return Start(null_dynamic_args); |
| 519 | } |
| 520 | |
| 521 | bool Service::StartIfNotDisabled() { |
| 522 | if (!(flags_ & SVC_DISABLED)) { |
| 523 | return Start(); |
| 524 | } else { |
| 525 | flags_ |= SVC_DISABLED_START; |
| 526 | } |
| 527 | return true; |
| 528 | } |
| 529 | |
| 530 | bool Service::Enable() { |
| 531 | flags_ &= ~(SVC_DISABLED | SVC_RC_DISABLED); |
| 532 | if (flags_ & SVC_DISABLED_START) { |
| 533 | return Start(); |
| 534 | } |
| 535 | return true; |
| 536 | } |
| 537 | |
| 538 | void Service::Reset() { |
| 539 | StopOrReset(SVC_RESET); |
| 540 | } |
| 541 | |
| 542 | void Service::Stop() { |
| 543 | StopOrReset(SVC_DISABLED); |
| 544 | } |
| 545 | |
| 546 | void Service::Restart() { |
| 547 | if (flags_ & SVC_RUNNING) { |
| 548 | /* Stop, wait, then start the service. */ |
| 549 | StopOrReset(SVC_RESTART); |
| 550 | } else if (!(flags_ & SVC_RESTARTING)) { |
| 551 | /* Just start the service since it's not running. */ |
| 552 | Start(); |
| 553 | } /* else: Service is restarting anyways. */ |
| 554 | } |
| 555 | |
| 556 | void Service::RestartIfNeeded(time_t& process_needs_restart) { |
| 557 | time_t next_start_time = time_started_ + 5; |
| 558 | |
| 559 | if (next_start_time <= gettime()) { |
| 560 | flags_ &= (~SVC_RESTARTING); |
| 561 | Start(); |
| 562 | return; |
| 563 | } |
| 564 | |
| 565 | if ((next_start_time < process_needs_restart) || |
| 566 | (process_needs_restart == 0)) { |
| 567 | process_needs_restart = next_start_time; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | /* The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART */ |
| 572 | void Service::StopOrReset(int how) { |
| 573 | /* The service is still SVC_RUNNING until its process exits, but if it has |
| 574 | * already exited it shoudn't attempt a restart yet. */ |
| 575 | flags_ &= ~(SVC_RESTARTING | SVC_DISABLED_START); |
| 576 | |
| 577 | if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) { |
| 578 | /* Hrm, an illegal flag. Default to SVC_DISABLED */ |
| 579 | how = SVC_DISABLED; |
| 580 | } |
| 581 | /* if the service has not yet started, prevent |
| 582 | * it from auto-starting with its class |
| 583 | */ |
| 584 | if (how == SVC_RESET) { |
| 585 | flags_ |= (flags_ & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET; |
| 586 | } else { |
| 587 | flags_ |= how; |
| 588 | } |
| 589 | |
| 590 | if (pid_) { |
| 591 | NOTICE("Service '%s' is being killed...\n", name_.c_str()); |
| 592 | kill(-pid_, SIGKILL); |
| 593 | NotifyStateChange("stopping"); |
| 594 | } else { |
| 595 | NotifyStateChange("stopped"); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | void Service::ZapStdio() const { |
| 600 | int fd; |
| 601 | fd = open("/dev/null", O_RDWR); |
| 602 | dup2(fd, 0); |
| 603 | dup2(fd, 1); |
| 604 | dup2(fd, 2); |
| 605 | close(fd); |
| 606 | } |
| 607 | |
| 608 | void Service::OpenConsole() const { |
| 609 | int fd; |
| 610 | if ((fd = open(console_name.c_str(), O_RDWR)) < 0) { |
| 611 | fd = open("/dev/null", O_RDWR); |
| 612 | } |
| 613 | ioctl(fd, TIOCSCTTY, 0); |
| 614 | dup2(fd, 0); |
| 615 | dup2(fd, 1); |
| 616 | dup2(fd, 2); |
| 617 | close(fd); |
| 618 | } |
| 619 | |
| 620 | void Service::PublishSocket(const std::string& name, int fd) const { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 621 | std::string key = StringPrintf(ANDROID_SOCKET_ENV_PREFIX "%s", name.c_str()); |
| 622 | std::string val = StringPrintf("%d", fd); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 623 | add_environment(key.c_str(), val.c_str()); |
| 624 | |
| 625 | /* make sure we don't close-on-exec */ |
| 626 | fcntl(fd, F_SETFD, 0); |
| 627 | } |
| 628 | |
| 629 | int ServiceManager::exec_count_ = 0; |
| 630 | |
| 631 | ServiceManager::ServiceManager() { |
| 632 | } |
| 633 | |
| 634 | ServiceManager& ServiceManager::GetInstance() { |
| 635 | static ServiceManager instance; |
| 636 | return instance; |
| 637 | } |
| 638 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 639 | void ServiceManager::AddService(std::unique_ptr<Service> service) { |
| 640 | Service* old_service = FindServiceByName(service->name()); |
| 641 | if (old_service) { |
| 642 | ERROR("ignored duplicate definition of service '%s'", |
| 643 | service->name().c_str()); |
| 644 | return; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 645 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 646 | services_.emplace_back(std::move(service)); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | Service* ServiceManager::MakeExecOneshotService(const std::vector<std::string>& args) { |
| 650 | // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS... |
| 651 | // SECLABEL can be a - to denote default |
| 652 | std::size_t command_arg = 1; |
| 653 | for (std::size_t i = 1; i < args.size(); ++i) { |
| 654 | if (args[i] == "--") { |
| 655 | command_arg = i + 1; |
| 656 | break; |
| 657 | } |
| 658 | } |
| 659 | if (command_arg > 4 + NR_SVC_SUPP_GIDS) { |
| 660 | ERROR("exec called with too many supplementary group ids\n"); |
| 661 | return nullptr; |
| 662 | } |
| 663 | |
| 664 | if (command_arg >= args.size()) { |
| 665 | ERROR("exec called without command\n"); |
| 666 | return nullptr; |
| 667 | } |
| 668 | std::vector<std::string> str_args(args.begin() + command_arg, args.end()); |
| 669 | |
| 670 | exec_count_++; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 671 | std::string name = StringPrintf("exec %d (%s)", exec_count_, str_args[0].c_str()); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 672 | unsigned flags = SVC_EXEC | SVC_ONESHOT; |
| 673 | |
| 674 | std::string seclabel = ""; |
| 675 | if (command_arg > 2 && args[1] != "-") { |
| 676 | seclabel = args[1]; |
| 677 | } |
| 678 | uid_t uid = 0; |
| 679 | if (command_arg > 3) { |
| 680 | uid = decode_uid(args[2].c_str()); |
| 681 | } |
| 682 | gid_t gid = 0; |
| 683 | std::vector<gid_t> supp_gids; |
| 684 | if (command_arg > 4) { |
| 685 | gid = decode_uid(args[3].c_str()); |
| 686 | std::size_t nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */; |
| 687 | for (size_t i = 0; i < nr_supp_gids; ++i) { |
| 688 | supp_gids.push_back(decode_uid(args[4 + i].c_str())); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | std::unique_ptr<Service> svc_p(new Service(name, "default", flags, uid, gid, |
| 693 | supp_gids, seclabel, str_args)); |
| 694 | if (!svc_p) { |
| 695 | ERROR("Couldn't allocate service for exec of '%s'", |
| 696 | str_args[0].c_str()); |
| 697 | return nullptr; |
| 698 | } |
| 699 | Service* svc = svc_p.get(); |
| 700 | services_.push_back(std::move(svc_p)); |
| 701 | |
| 702 | return svc; |
| 703 | } |
| 704 | |
| 705 | Service* ServiceManager::FindServiceByName(const std::string& name) const { |
| 706 | auto svc = std::find_if(services_.begin(), services_.end(), |
| 707 | [&name] (const std::unique_ptr<Service>& s) { |
| 708 | return name == s->name(); |
| 709 | }); |
| 710 | if (svc != services_.end()) { |
| 711 | return svc->get(); |
| 712 | } |
| 713 | return nullptr; |
| 714 | } |
| 715 | |
| 716 | Service* ServiceManager::FindServiceByPid(pid_t pid) const { |
| 717 | auto svc = std::find_if(services_.begin(), services_.end(), |
| 718 | [&pid] (const std::unique_ptr<Service>& s) { |
| 719 | return s->pid() == pid; |
| 720 | }); |
| 721 | if (svc != services_.end()) { |
| 722 | return svc->get(); |
| 723 | } |
| 724 | return nullptr; |
| 725 | } |
| 726 | |
| 727 | Service* ServiceManager::FindServiceByKeychord(int keychord_id) const { |
| 728 | auto svc = std::find_if(services_.begin(), services_.end(), |
| 729 | [&keychord_id] (const std::unique_ptr<Service>& s) { |
| 730 | return s->keychord_id() == keychord_id; |
| 731 | }); |
| 732 | |
| 733 | if (svc != services_.end()) { |
| 734 | return svc->get(); |
| 735 | } |
| 736 | return nullptr; |
| 737 | } |
| 738 | |
| 739 | void ServiceManager::ForEachService(void (*func)(Service* svc)) const { |
| 740 | for (const auto& s : services_) { |
| 741 | func(s.get()); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | void ServiceManager::ForEachServiceInClass(const std::string& classname, |
| 746 | void (*func)(Service* svc)) const { |
| 747 | for (const auto& s : services_) { |
| 748 | if (classname == s->classname()) { |
| 749 | func(s.get()); |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | void ServiceManager::ForEachServiceWithFlags(unsigned matchflags, |
| 755 | void (*func)(Service* svc)) const { |
| 756 | for (const auto& s : services_) { |
| 757 | if (s->flags() & matchflags) { |
| 758 | func(s.get()); |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 763 | void ServiceManager::RemoveService(const Service& svc) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 764 | auto svc_it = std::find_if(services_.begin(), services_.end(), |
| 765 | [&svc] (const std::unique_ptr<Service>& s) { |
| 766 | return svc.name() == s->name(); |
| 767 | }); |
| 768 | if (svc_it == services_.end()) { |
| 769 | return; |
| 770 | } |
| 771 | |
| 772 | services_.erase(svc_it); |
| 773 | } |
| 774 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 775 | void ServiceManager::DumpState() const { |
| 776 | for (const auto& s : services_) { |
| 777 | s->DumpState(); |
| 778 | } |
| 779 | INFO("\n"); |
| 780 | } |
| 781 | |
| 782 | bool ServiceParser::ParseSection(const std::vector<std::string>& args, |
| 783 | std::string* err) { |
| 784 | if (args.size() < 3) { |
| 785 | *err = "services must have a name and a program"; |
| 786 | return false; |
| 787 | } |
| 788 | |
| 789 | const std::string& name = args[1]; |
| 790 | if (!IsValidName(name)) { |
| 791 | *err = StringPrintf("invalid service name '%s'", name.c_str()); |
| 792 | return false; |
| 793 | } |
| 794 | |
| 795 | std::vector<std::string> str_args(args.begin() + 2, args.end()); |
| 796 | service_ = std::make_unique<Service>(name, "default", str_args); |
| 797 | return true; |
| 798 | } |
| 799 | |
| 800 | bool ServiceParser::ParseLineSection(const std::vector<std::string>& args, |
| 801 | const std::string& filename, int line, |
| 802 | std::string* err) const { |
| 803 | return service_ ? service_->HandleLine(args, err) : false; |
| 804 | } |
| 805 | |
| 806 | void ServiceParser::EndSection() { |
| 807 | if (service_) { |
| 808 | ServiceManager::GetInstance().AddService(std::move(service_)); |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | bool ServiceParser::IsValidName(const std::string& name) const { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 813 | if (name.size() > 16) { |
| 814 | return false; |
| 815 | } |
| 816 | for (const auto& c : name) { |
| 817 | if (!isalnum(c) && (c != '_') && (c != '-')) { |
| 818 | return false; |
| 819 | } |
| 820 | } |
| 821 | return true; |
| 822 | } |