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