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 <errno.h>
20 #include <fcntl.h>
21 #include <inttypes.h>
22 #include <linux/securebits.h>
23 #include <sched.h>
24 #include <sys/prctl.h>
25 #include <sys/stat.h>
26 #include <sys/time.h>
27 #include <termios.h>
28 #include <unistd.h>
29 #include <thread>
30
31 #include <android-base/file.h>
32 #include <android-base/logging.h>
33 #include <android-base/properties.h>
34 #include <android-base/scopeguard.h>
35 #include <android-base/stringprintf.h>
36 #include <android-base/strings.h>
37 #include <cutils/sockets.h>
38 #include <processgroup/processgroup.h>
39 #include <selinux/selinux.h>
40
41 #include <string>
42
43 #include "interprocess_fifo.h"
44 #include "lmkd_service.h"
45 #include "service_list.h"
46 #include "util.h"
47
48 #if defined(__BIONIC__)
49 #include <bionic/reserved_signals.h>
50 #endif
51
52 #ifdef INIT_FULL_SOURCES
53 #include <ApexProperties.sysprop.h>
54 #include <android/api-level.h>
55
56 #include "mount_namespace.h"
57 #include "reboot_utils.h"
58 #include "selinux.h"
59 #else
60 #include "host_init_stubs.h"
61 #endif
62
63 using android::base::boot_clock;
64 using android::base::GetBoolProperty;
65 using android::base::GetIntProperty;
66 using android::base::GetProperty;
67 using android::base::Join;
68 using android::base::make_scope_guard;
69 using android::base::SetProperty;
70 using android::base::StartsWith;
71 using android::base::StringPrintf;
72 using android::base::WriteStringToFile;
73
74 namespace android {
75 namespace init {
76
ComputeContextFromExecutable(const std::string & service_path)77 static Result<std::string> ComputeContextFromExecutable(const std::string& service_path) {
78 std::string computed_context;
79
80 char* raw_con = nullptr;
81 char* raw_filecon = nullptr;
82
83 if (getcon(&raw_con) == -1) {
84 return Error() << "Could not get security context";
85 }
86 std::unique_ptr<char, decltype(&freecon)> mycon(raw_con, freecon);
87
88 if (getfilecon(service_path.c_str(), &raw_filecon) == -1) {
89 return Error() << "Could not get file context";
90 }
91 std::unique_ptr<char, decltype(&freecon)> filecon(raw_filecon, freecon);
92
93 char* new_con = nullptr;
94 int rc = security_compute_create(mycon.get(), filecon.get(),
95 string_to_security_class("process"), &new_con);
96 if (rc == 0) {
97 computed_context = new_con;
98 free(new_con);
99 }
100 if (rc == 0 && computed_context == mycon.get()) {
101 return Error() << "File " << service_path << "(labeled \"" << filecon.get()
102 << "\") has incorrect label or no domain transition from " << mycon.get()
103 << " to another SELinux domain defined. Have you configured your "
104 "service correctly? https://source.android.com/security/selinux/"
105 "device-policy#label_new_services_and_address_denials. Note: this "
106 "error shows up even in permissive mode in order to make auditing "
107 "denials possible.";
108 }
109 if (rc < 0) {
110 return Error() << "Could not get process context";
111 }
112 return computed_context;
113 }
114
ExpandArgsAndExecv(const std::vector<std::string> & args,bool sigstop)115 static bool ExpandArgsAndExecv(const std::vector<std::string>& args, bool sigstop) {
116 std::vector<std::string> expanded_args;
117 std::vector<char*> c_strings;
118
119 expanded_args.resize(args.size());
120 c_strings.push_back(const_cast<char*>(args[0].data()));
121 for (std::size_t i = 1; i < args.size(); ++i) {
122 auto expanded_arg = ExpandProps(args[i]);
123 if (!expanded_arg.ok()) {
124 LOG(FATAL) << args[0] << ": cannot expand arguments': " << expanded_arg.error();
125 }
126 expanded_args[i] = *expanded_arg;
127 c_strings.push_back(expanded_args[i].data());
128 }
129 c_strings.push_back(nullptr);
130
131 if (sigstop) {
132 kill(getpid(), SIGSTOP);
133 }
134
135 return execv(c_strings[0], c_strings.data()) == 0;
136 }
137
138 unsigned long Service::next_start_order_ = 1;
139 bool Service::is_exec_service_running_ = false;
140
Service(const std::string & name,Subcontext * subcontext_for_restart_commands,const std::string & filename,const std::vector<std::string> & args)141 Service::Service(const std::string& name, Subcontext* subcontext_for_restart_commands,
142 const std::string& filename, const std::vector<std::string>& args)
143 : Service(name, 0, 0, 0, {}, 0, "", subcontext_for_restart_commands, filename, args) {}
144
Service(const std::string & name,unsigned flags,uid_t uid,gid_t gid,const std::vector<gid_t> & supp_gids,int namespace_flags,const std::string & seclabel,Subcontext * subcontext_for_restart_commands,const std::string & filename,const std::vector<std::string> & args)145 Service::Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid,
146 const std::vector<gid_t>& supp_gids, int namespace_flags,
147 const std::string& seclabel, Subcontext* subcontext_for_restart_commands,
148 const std::string& filename, const std::vector<std::string>& args)
149 : name_(name),
150 classnames_({"default"}),
151 flags_(flags),
152 pid_(0),
153 crash_count_(0),
154 proc_attr_{.ioprio_class = IoSchedClass_NONE,
155 .ioprio_pri = 0,
156 .uid = uid,
157 .gid = gid,
158 .supp_gids = supp_gids,
159 .priority = 0},
160 namespaces_{.flags = namespace_flags},
161 seclabel_(seclabel),
162 subcontext_(subcontext_for_restart_commands),
163 onrestart_(false, subcontext_for_restart_commands, "<Service '" + name + "' onrestart>", 0,
164 "onrestart", {}),
165 oom_score_adjust_(DEFAULT_OOM_SCORE_ADJUST),
166 start_order_(0),
167 args_(args),
168 filename_(filename) {}
169
NotifyStateChange(const std::string & new_state) const170 void Service::NotifyStateChange(const std::string& new_state) const {
171 if ((flags_ & SVC_TEMPORARY) != 0) {
172 // Services created by 'exec' are temporary and don't have properties tracking their state.
173 return;
174 }
175
176 std::string prop_name = "init.svc." + name_;
177 SetProperty(prop_name, new_state);
178
179 if (new_state == "running") {
180 uint64_t start_ns = time_started_.time_since_epoch().count();
181 std::string boottime_property = "ro.boottime." + name_;
182 if (GetProperty(boottime_property, "").empty()) {
183 SetProperty(boottime_property, std::to_string(start_ns));
184 }
185 }
186
187 // init.svc_debug_pid.* properties are only for tests, and should not be used
188 // on device for security checks.
189 std::string pid_property = "init.svc_debug_pid." + name_;
190 if (new_state == "running") {
191 SetProperty(pid_property, std::to_string(pid_));
192 } else if (new_state == "stopped") {
193 SetProperty(pid_property, "");
194 }
195 }
196
KillProcessGroup(int signal,bool report_oneshot)197 void Service::KillProcessGroup(int signal, bool report_oneshot) {
198 // If we've already seen a successful result from killProcessGroup*(), then we have removed
199 // the cgroup already and calling these functions a second time will simply result in an error.
200 // This is true regardless of which signal was sent.
201 // These functions handle their own logging, so no additional logging is needed.
202 if (!process_cgroup_empty_) {
203 LOG(INFO) << "Sending signal " << signal << " to service '" << name_ << "' (pid " << pid_
204 << ") process group...";
205 int max_processes = 0;
206 int r;
207 if (signal == SIGTERM) {
208 r = killProcessGroupOnce(proc_attr_.uid, pid_, signal, &max_processes);
209 } else {
210 r = killProcessGroup(proc_attr_.uid, pid_, signal, &max_processes);
211 }
212
213 if (report_oneshot && max_processes > 0) {
214 LOG(WARNING)
215 << "Killed " << max_processes
216 << " additional processes from a oneshot process group for service '" << name_
217 << "'. This is new behavior, previously child processes would not be killed in "
218 "this case.";
219 }
220
221 if (r == 0) process_cgroup_empty_ = true;
222 }
223
224 if (oom_score_adjust_ != DEFAULT_OOM_SCORE_ADJUST) {
225 LmkdUnregister(name_, pid_);
226 }
227 }
228
SetProcessAttributesAndCaps(InterprocessFifo setsid_finished)229 void Service::SetProcessAttributesAndCaps(InterprocessFifo setsid_finished) {
230 // Keep capabilites on uid change.
231 if (capabilities_ && proc_attr_.uid) {
232 // If Android is running in a container, some securebits might already
233 // be locked, so don't change those.
234 unsigned long securebits = prctl(PR_GET_SECUREBITS);
235 if (securebits == -1UL) {
236 PLOG(FATAL) << "prctl(PR_GET_SECUREBITS) failed for " << name_;
237 }
238 securebits |= SECBIT_KEEP_CAPS | SECBIT_KEEP_CAPS_LOCKED;
239 if (prctl(PR_SET_SECUREBITS, securebits) != 0) {
240 PLOG(FATAL) << "prctl(PR_SET_SECUREBITS) failed for " << name_;
241 }
242 }
243
244 if (auto result = SetProcessAttributes(proc_attr_, std::move(setsid_finished)); !result.ok()) {
245 LOG(FATAL) << "cannot set attribute for " << name_ << ": " << result.error();
246 }
247
248 if (!seclabel_.empty()) {
249 if (setexeccon(seclabel_.c_str()) < 0) {
250 PLOG(FATAL) << "cannot setexeccon('" << seclabel_ << "') for " << name_;
251 }
252 }
253
254 if (capabilities_) {
255 if (!SetCapsForExec(*capabilities_)) {
256 LOG(FATAL) << "cannot set capabilities for " << name_;
257 }
258 } else if (proc_attr_.uid) {
259 // Inheritable caps can be non-zero when running in a container.
260 if (!DropInheritableCaps()) {
261 LOG(FATAL) << "cannot drop inheritable caps for " << name_;
262 }
263 }
264 }
265
Reap(const siginfo_t & siginfo)266 void Service::Reap(const siginfo_t& siginfo) {
267 if (!(flags_ & SVC_ONESHOT) || (flags_ & SVC_RESTART)) {
268 KillProcessGroup(SIGKILL, false);
269 } else {
270 // Legacy behavior from ~2007 until Android R: this else branch did not exist and we did not
271 // kill the process group in this case.
272 if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_R__) {
273 // The new behavior in Android R is to kill these process groups in all cases. The
274 // 'true' parameter instructions KillProcessGroup() to report a warning message where it
275 // detects a difference in behavior has occurred.
276 KillProcessGroup(SIGKILL, true);
277 }
278 }
279
280 // Remove any socket resources we may have created.
281 for (const auto& socket : sockets_) {
282 if (socket.persist) {
283 continue;
284 }
285 auto path = ANDROID_SOCKET_DIR "/" + socket.name;
286 unlink(path.c_str());
287 }
288
289 for (const auto& f : reap_callbacks_) {
290 f(siginfo);
291 }
292
293 if ((siginfo.si_code != CLD_EXITED || siginfo.si_status != 0) && on_failure_reboot_target_) {
294 LOG(ERROR) << "Service " << name_
295 << " has 'reboot_on_failure' option and failed, shutting down system.";
296 trigger_shutdown(*on_failure_reboot_target_);
297 }
298
299 if (flags_ & SVC_EXEC) UnSetExec();
300
301 if (name_ == "zygote" || name_ == "zygote64") {
302 removeAllEmptyProcessGroups();
303 }
304
305 if (flags_ & SVC_TEMPORARY) return;
306
307 pid_ = 0;
308 flags_ &= (~SVC_RUNNING);
309 start_order_ = 0;
310
311 // Oneshot processes go into the disabled state on exit,
312 // except when manually restarted.
313 if ((flags_ & SVC_ONESHOT) && !(flags_ & SVC_RESTART) && !(flags_ & SVC_RESET)) {
314 flags_ |= SVC_DISABLED;
315 }
316
317 // Disabled and reset processes do not get restarted automatically.
318 if (flags_ & (SVC_DISABLED | SVC_RESET)) {
319 NotifyStateChange("stopped");
320 return;
321 }
322
323 #if INIT_FULL_SOURCES
324 static bool is_apex_updatable = android::sysprop::ApexProperties::updatable().value_or(false);
325 #else
326 static bool is_apex_updatable = false;
327 #endif
328 const bool use_default_mount_ns =
329 mount_namespace_.has_value() && *mount_namespace_ == NS_DEFAULT;
330 const bool is_process_updatable = use_default_mount_ns && is_apex_updatable;
331
332 #if defined(__BIONIC__) && defined(SEGV_MTEAERR)
333 // As a precaution, we only upgrade a service once per reboot, to limit
334 // the potential impact.
335 //
336 // BIONIC_SIGNAL_ART_PROFILER is a magic value used by deuggerd to signal
337 // that the process crashed with SIGSEGV and SEGV_MTEAERR. This signal will
338 // never be seen otherwise in a crash, because it always gets handled by the
339 // profiling signal handlers in bionic. See also
340 // debuggerd/handler/debuggerd_handler.cpp.
341 bool should_upgrade_mte = siginfo.si_code != CLD_EXITED &&
342 siginfo.si_status == BIONIC_SIGNAL_ART_PROFILER && !upgraded_mte_;
343
344 if (should_upgrade_mte) {
345 constexpr int kDefaultUpgradeSecs = 60;
346 int secs = GetIntProperty("persist.device_config.memory_safety_native.upgrade_secs.default",
347 kDefaultUpgradeSecs);
348 secs = GetIntProperty(
349 "persist.device_config.memory_safety_native.upgrade_secs.service." + name_, secs);
350 if (secs > 0) {
351 LOG(INFO) << "Upgrading service " << name_ << " to sync MTE for " << secs << " seconds";
352 once_environment_vars_.emplace_back("BIONIC_MEMTAG_UPGRADE_SECS", std::to_string(secs));
353 upgraded_mte_ = true;
354 } else {
355 LOG(INFO) << "Not upgrading service " << name_ << " to sync MTE due to device config";
356 }
357 }
358 #endif
359
360 // If we crash > 4 times in 'fatal_crash_window_' minutes or before boot_completed,
361 // reboot into bootloader or set crashing property
362 boot_clock::time_point now = boot_clock::now();
363 if (((flags_ & SVC_CRITICAL) || is_process_updatable) && !(flags_ & SVC_RESTART)) {
364 bool boot_completed = GetBoolProperty("sys.boot_completed", false);
365 if (now < time_crashed_ + fatal_crash_window_ || !boot_completed) {
366 if (++crash_count_ > 4) {
367 auto exit_reason = boot_completed ?
368 "in " + std::to_string(fatal_crash_window_.count()) + " minutes" :
369 "before boot completed";
370 if (flags_ & SVC_CRITICAL) {
371 if (!GetBoolProperty("init.svc_debug.no_fatal." + name_, false)) {
372 // Aborts into `fatal_reboot_target_'.
373 SetFatalRebootTarget(fatal_reboot_target_);
374 LOG(FATAL) << "critical process '" << name_ << "' exited 4 times "
375 << exit_reason;
376 }
377 } else {
378 LOG(ERROR) << "process with updatable components '" << name_
379 << "' exited 4 times " << exit_reason;
380 // Notifies update_verifier and apexd
381 SetProperty("sys.init.updatable_crashing_process_name", name_);
382 SetProperty("sys.init.updatable_crashing", "1");
383 }
384 }
385 } else {
386 time_crashed_ = now;
387 crash_count_ = 1;
388 }
389 }
390
391 flags_ &= (~SVC_RESTART);
392 flags_ |= SVC_RESTARTING;
393
394 // Execute all onrestart commands for this service.
395 onrestart_.ExecuteAllCommands();
396
397 NotifyStateChange("restarting");
398 return;
399 }
400
DumpState() const401 void Service::DumpState() const {
402 LOG(INFO) << "service " << name_;
403 LOG(INFO) << " class '" << Join(classnames_, " ") << "'";
404 LOG(INFO) << " exec " << Join(args_, " ");
405 for (const auto& socket : sockets_) {
406 LOG(INFO) << " socket " << socket.name;
407 }
408 for (const auto& file : files_) {
409 LOG(INFO) << " file " << file.name;
410 }
411 }
412
413
ExecStart()414 Result<void> Service::ExecStart() {
415 auto reboot_on_failure = make_scope_guard([this] {
416 if (on_failure_reboot_target_) {
417 trigger_shutdown(*on_failure_reboot_target_);
418 }
419 });
420
421 if (is_updatable() && !ServiceList::GetInstance().IsServicesUpdated()) {
422 // Don't delay the service for ExecStart() as the semantic is that
423 // the caller might depend on the side effect of the execution.
424 return Error() << "Cannot start an updatable service '" << name_
425 << "' before configs from APEXes are all loaded";
426 }
427
428 flags_ |= SVC_ONESHOT;
429
430 if (auto result = Start(); !result.ok()) {
431 return result;
432 }
433
434 flags_ |= SVC_EXEC;
435 is_exec_service_running_ = true;
436
437 LOG(INFO) << "SVC_EXEC service '" << name_ << "' pid " << pid_ << " (uid " << proc_attr_.uid
438 << " gid " << proc_attr_.gid << "+" << proc_attr_.supp_gids.size() << " context "
439 << (!seclabel_.empty() ? seclabel_ : "default") << ") started; waiting...";
440
441 reboot_on_failure.Disable();
442 return {};
443 }
444
CheckConsole()445 Result<void> Service::CheckConsole() {
446 if (!(flags_ & SVC_CONSOLE)) {
447 return {};
448 }
449
450 // On newer kernels, /dev/console will always exist because
451 // "console=ttynull" is hard-coded in CONFIG_CMDLINE. This new boot
452 // property should be set via "androidboot.serialconsole=0" to explicitly
453 // disable services requiring the console. For older kernels and boot
454 // images, not setting this at all will fall back to the old behavior
455 if (GetProperty("ro.boot.serialconsole", "") == "0") {
456 flags_ |= SVC_DISABLED;
457 return {};
458 }
459
460 if (proc_attr_.console.empty()) {
461 proc_attr_.console = "/dev/" + GetProperty("ro.boot.console", "console");
462 }
463
464 // Make sure that open call succeeds to ensure a console driver is
465 // properly registered for the device node
466 int console_fd = open(proc_attr_.console.c_str(), O_RDWR | O_CLOEXEC);
467 if (console_fd < 0) {
468 flags_ |= SVC_DISABLED;
469 return ErrnoError() << "Couldn't open console '" << proc_attr_.console << "'";
470 }
471 close(console_fd);
472 return {};
473 }
474
475 // Configures the memory cgroup properties for the service.
ConfigureMemcg()476 void Service::ConfigureMemcg() {
477 if (swappiness_ != -1) {
478 if (!setProcessGroupSwappiness(proc_attr_.uid, pid_, swappiness_)) {
479 PLOG(ERROR) << "setProcessGroupSwappiness failed";
480 }
481 }
482
483 if (soft_limit_in_bytes_ != -1) {
484 if (!setProcessGroupSoftLimit(proc_attr_.uid, pid_, soft_limit_in_bytes_)) {
485 PLOG(ERROR) << "setProcessGroupSoftLimit failed";
486 }
487 }
488
489 size_t computed_limit_in_bytes = limit_in_bytes_;
490 if (limit_percent_ != -1) {
491 long page_size = sysconf(_SC_PAGESIZE);
492 long num_pages = sysconf(_SC_PHYS_PAGES);
493 if (page_size > 0 && num_pages > 0) {
494 size_t max_mem = SIZE_MAX;
495 if (size_t(num_pages) < SIZE_MAX / size_t(page_size)) {
496 max_mem = size_t(num_pages) * size_t(page_size);
497 }
498 computed_limit_in_bytes =
499 std::min(computed_limit_in_bytes, max_mem / 100 * limit_percent_);
500 }
501 }
502
503 if (!limit_property_.empty()) {
504 // This ends up overwriting computed_limit_in_bytes but only if the
505 // property is defined.
506 computed_limit_in_bytes =
507 android::base::GetUintProperty(limit_property_, computed_limit_in_bytes, SIZE_MAX);
508 }
509
510 if (computed_limit_in_bytes != size_t(-1)) {
511 if (!setProcessGroupLimit(proc_attr_.uid, pid_, computed_limit_in_bytes)) {
512 PLOG(ERROR) << "setProcessGroupLimit failed";
513 }
514 }
515 }
516
517 // Enters namespaces, sets environment variables, writes PID files and runs the service executable.
RunService(const std::vector<Descriptor> & descriptors,InterprocessFifo cgroups_activated,InterprocessFifo setsid_finished)518 void Service::RunService(const std::vector<Descriptor>& descriptors,
519 InterprocessFifo cgroups_activated, InterprocessFifo setsid_finished) {
520 if (auto result = EnterNamespaces(namespaces_, name_, mount_namespace_); !result.ok()) {
521 LOG(FATAL) << "Service '" << name_ << "' failed to set up namespaces: " << result.error();
522 }
523
524 for (const auto& [key, value] : once_environment_vars_) {
525 setenv(key.c_str(), value.c_str(), 1);
526 }
527 for (const auto& [key, value] : environment_vars_) {
528 setenv(key.c_str(), value.c_str(), 1);
529 }
530
531 for (const auto& descriptor : descriptors) {
532 descriptor.Publish();
533 }
534
535 if (auto result = WritePidToFiles(&writepid_files_); !result.ok()) {
536 LOG(ERROR) << "failed to write pid to files: " << result.error();
537 }
538
539 // Wait until the cgroups have been created and until the cgroup controllers have been
540 // activated.
541 Result<uint8_t> byte = cgroups_activated.Read();
542 if (!byte.ok()) {
543 LOG(ERROR) << name_ << ": failed to read from notification channel: " << byte.error();
544 }
545 cgroups_activated.Close();
546 if (*byte != kCgroupsActivated) {
547 LOG(FATAL) << "Service '" << name_ << "' failed to start due to a fatal error";
548 _exit(EXIT_FAILURE);
549 }
550
551 if (task_profiles_.size() > 0) {
552 bool succeeded = SelinuxGetVendorAndroidVersion() < __ANDROID_API_U__
553 ?
554 // Compatibility mode: apply the task profiles to the current
555 // thread.
556 SetTaskProfiles(getpid(), task_profiles_)
557 :
558 // Apply the task profiles to the current process.
559 SetProcessProfiles(getuid(), getpid(), task_profiles_);
560 if (!succeeded) {
561 LOG(ERROR) << "failed to set task profiles";
562 }
563 }
564
565 // As requested, set our gid, supplemental gids, uid, context, and
566 // priority. Aborts on failure.
567 SetProcessAttributesAndCaps(std::move(setsid_finished));
568
569 if (!ExpandArgsAndExecv(args_, sigstop_)) {
570 PLOG(ERROR) << "cannot execv('" << args_[0]
571 << "'). See the 'Debugging init' section of init's README.md for tips";
572 }
573 }
574
Start()575 Result<void> Service::Start() {
576 auto reboot_on_failure = make_scope_guard([this] {
577 if (on_failure_reboot_target_) {
578 trigger_shutdown(*on_failure_reboot_target_);
579 }
580 });
581
582 if (is_updatable() && !ServiceList::GetInstance().IsServicesUpdated()) {
583 ServiceList::GetInstance().DelayService(*this);
584 return Error() << "Cannot start an updatable service '" << name_
585 << "' before configs from APEXes are all loaded. "
586 << "Queued for execution.";
587 }
588
589 bool disabled = (flags_ & (SVC_DISABLED | SVC_RESET));
590 ResetFlagsForStart();
591
592 // Running processes require no additional work --- if they're in the
593 // process of exiting, we've ensured that they will immediately restart
594 // on exit, unless they are ONESHOT. For ONESHOT service, if it's in
595 // stopping status, we just set SVC_RESTART flag so it will get restarted
596 // in Reap().
597 if (flags_ & SVC_RUNNING) {
598 if ((flags_ & SVC_ONESHOT) && disabled) {
599 flags_ |= SVC_RESTART;
600 }
601
602 LOG(INFO) << "service '" << name_
603 << "' requested start, but it is already running (flags: " << flags_ << ")";
604
605 // It is not an error to try to start a service that is already running.
606 reboot_on_failure.Disable();
607 return {};
608 }
609
610 // cgroups_activated is used for communication from the parent to the child
611 // while setsid_finished is used for communication from the child process to
612 // the parent process. These two communication channels are separate because
613 // combining these into a single communication channel would introduce a
614 // race between the Write() calls by the parent and by the child.
615 InterprocessFifo cgroups_activated, setsid_finished;
616 OR_RETURN(cgroups_activated.Initialize());
617 OR_RETURN(setsid_finished.Initialize());
618
619 if (Result<void> result = CheckConsole(); !result.ok()) {
620 return result;
621 }
622
623 struct stat sb;
624 if (stat(args_[0].c_str(), &sb) == -1) {
625 flags_ |= SVC_DISABLED;
626 return ErrnoError() << "Cannot find '" << args_[0] << "'";
627 }
628
629 std::string scon;
630 if (!seclabel_.empty()) {
631 scon = seclabel_;
632 } else {
633 auto result = ComputeContextFromExecutable(args_[0]);
634 if (!result.ok()) {
635 return result.error();
636 }
637 scon = *result;
638 }
639
640 if (!mount_namespace_.has_value()) {
641 // remember from which mount namespace the service should start
642 SetMountNamespace();
643 }
644
645 post_data_ = ServiceList::GetInstance().IsPostData();
646
647 LOG(INFO) << "starting service '" << name_ << "'...";
648
649 std::vector<Descriptor> descriptors;
650 for (const auto& socket : sockets_) {
651 if (auto result = socket.Create(scon); result.ok()) {
652 descriptors.emplace_back(std::move(*result));
653 } else {
654 LOG(INFO) << "Could not create socket '" << socket.name << "': " << result.error();
655 }
656 }
657
658 for (const auto& file : files_) {
659 if (auto result = file.Create(); result.ok()) {
660 descriptors.emplace_back(std::move(*result));
661 } else {
662 LOG(INFO) << "Could not open file '" << file.name << "': " << result.error();
663 }
664 }
665
666 pid_t pid = -1;
667 if (namespaces_.flags) {
668 pid = clone(nullptr, nullptr, namespaces_.flags | SIGCHLD, nullptr);
669 } else {
670 pid = fork();
671 }
672
673 if (pid == 0) {
674 umask(077);
675 cgroups_activated.CloseWriteFd();
676 setsid_finished.CloseReadFd();
677 RunService(descriptors, std::move(cgroups_activated), std::move(setsid_finished));
678 _exit(127);
679 } else {
680 cgroups_activated.CloseReadFd();
681 setsid_finished.CloseWriteFd();
682 }
683
684 if (pid < 0) {
685 pid_ = 0;
686 return ErrnoError() << "Failed to fork";
687 }
688
689 once_environment_vars_.clear();
690
691 if (oom_score_adjust_ != DEFAULT_OOM_SCORE_ADJUST) {
692 std::string oom_str = std::to_string(oom_score_adjust_);
693 std::string oom_file = StringPrintf("/proc/%d/oom_score_adj", pid);
694 if (!WriteStringToFile(oom_str, oom_file)) {
695 PLOG(ERROR) << "couldn't write oom_score_adj";
696 }
697 }
698
699 time_started_ = boot_clock::now();
700 pid_ = pid;
701 flags_ |= SVC_RUNNING;
702 start_order_ = next_start_order_++;
703 process_cgroup_empty_ = false;
704
705 if (CgroupsAvailable()) {
706 bool use_memcg = swappiness_ != -1 || soft_limit_in_bytes_ != -1 || limit_in_bytes_ != -1 ||
707 limit_percent_ != -1 || !limit_property_.empty();
708 errno = -createProcessGroup(proc_attr_.uid, pid_, use_memcg);
709 if (errno != 0) {
710 Result<void> result = cgroups_activated.Write(kActivatingCgroupsFailed);
711 if (!result.ok()) {
712 return Error() << "Sending notification failed: " << result.error();
713 }
714 return Error() << "createProcessGroup(" << proc_attr_.uid << ", " << pid_ << ", "
715 << use_memcg << ") failed for service '" << name_
716 << "': " << strerror(errno);
717 }
718
719 // When the blkio controller is mounted in the v1 hierarchy, NormalIoPriority is
720 // the default (/dev/blkio). When the blkio controller is mounted in the v2 hierarchy, the
721 // NormalIoPriority profile has to be applied explicitly.
722 SetProcessProfiles(proc_attr_.uid, pid_, {"NormalIoPriority"});
723
724 if (use_memcg) {
725 ConfigureMemcg();
726 }
727 }
728
729 if (oom_score_adjust_ != DEFAULT_OOM_SCORE_ADJUST) {
730 LmkdRegister(name_, proc_attr_.uid, pid_, oom_score_adjust_);
731 }
732
733 if (Result<void> result = cgroups_activated.Write(kCgroupsActivated); !result.ok()) {
734 return Error() << "Sending cgroups activated notification failed: " << result.error();
735 }
736
737 cgroups_activated.Close();
738
739 // Call setpgid() from the parent process to make sure that this call has
740 // finished before the parent process calls kill(-pgid, ...).
741 if (!RequiresConsole(proc_attr_)) {
742 if (setpgid(pid, pid) < 0) {
743 switch (errno) {
744 case EACCES: // Child has already performed setpgid() followed by execve().
745 case ESRCH: // Child process no longer exists.
746 break;
747 default:
748 PLOG(ERROR) << "setpgid() from parent failed";
749 }
750 }
751 } else {
752 // The Read() call below will return an error if the child is killed.
753 if (Result<uint8_t> result = setsid_finished.Read();
754 !result.ok() || *result != kSetSidFinished) {
755 if (!result.ok()) {
756 return Error() << "Waiting for setsid() failed: " << result.error();
757 } else {
758 return Error() << "Waiting for setsid() failed: " << static_cast<uint32_t>(*result)
759 << " <> " << static_cast<uint32_t>(kSetSidFinished);
760 }
761 }
762 }
763
764 setsid_finished.Close();
765
766 NotifyStateChange("running");
767 reboot_on_failure.Disable();
768
769 LOG(INFO) << "... started service '" << name_ << "' has pid " << pid_;
770
771 return {};
772 }
773
774 // Set mount namespace for the service.
775 // The reason why remember the mount namespace:
776 // If this service is started before APEXes and corresponding linker configuration
777 // get available, mark it as pre-apexd one. Note that this marking is
778 // permanent. So for example, if the service is re-launched (e.g., due
779 // to crash), it is still recognized as pre-apexd... for consistency.
SetMountNamespace()780 void Service::SetMountNamespace() {
781 // APEXd is always started in the "current" namespace because it is the process to set up
782 // the current namespace. So, leave mount_namespace_ as empty.
783 if (args_[0] == "/system/bin/apexd") {
784 return;
785 }
786 // Services in the following list start in the "default" mount namespace.
787 // Note that they should use bootstrap bionic if they start before APEXes are ready.
788 static const std::set<std::string> kUseDefaultMountNamespace = {
789 "ueventd", // load firmwares from APEXes
790 "hwservicemanager", // load VINTF fragments from APEXes
791 "servicemanager", // load VINTF fragments from APEXes
792 };
793 if (kUseDefaultMountNamespace.find(name_) != kUseDefaultMountNamespace.end()) {
794 mount_namespace_ = NS_DEFAULT;
795 return;
796 }
797 // Use the "default" mount namespace only if it's ready
798 mount_namespace_ = IsDefaultMountNamespaceReady() ? NS_DEFAULT : NS_BOOTSTRAP;
799 }
800
SetStartedInFirstStage(pid_t pid)801 void Service::SetStartedInFirstStage(pid_t pid) {
802 LOG(INFO) << "adding first-stage service '" << name_ << "'...";
803
804 time_started_ = boot_clock::now(); // not accurate, but doesn't matter here
805 pid_ = pid;
806 flags_ |= SVC_RUNNING;
807 start_order_ = next_start_order_++;
808
809 NotifyStateChange("running");
810 }
811
ResetFlagsForStart()812 void Service::ResetFlagsForStart() {
813 // Starting a service removes it from the disabled or reset state and
814 // immediately takes it out of the restarting state if it was in there.
815 flags_ &= ~(SVC_DISABLED | SVC_RESTARTING | SVC_RESET | SVC_RESTART | SVC_DISABLED_START);
816 }
817
StartIfNotDisabled()818 Result<void> Service::StartIfNotDisabled() {
819 if (!(flags_ & SVC_DISABLED)) {
820 return Start();
821 } else {
822 flags_ |= SVC_DISABLED_START;
823 }
824 return {};
825 }
826
Enable()827 Result<void> Service::Enable() {
828 flags_ &= ~(SVC_DISABLED | SVC_RC_DISABLED);
829 if (flags_ & SVC_DISABLED_START) {
830 return Start();
831 }
832 return {};
833 }
834
Reset()835 void Service::Reset() {
836 StopOrReset(SVC_RESET);
837 }
838
Stop()839 void Service::Stop() {
840 StopOrReset(SVC_DISABLED);
841 }
842
Terminate()843 void Service::Terminate() {
844 flags_ &= ~(SVC_RESTARTING | SVC_DISABLED_START);
845 flags_ |= SVC_DISABLED;
846 if (pid_) {
847 KillProcessGroup(SIGTERM);
848 NotifyStateChange("stopping");
849 }
850 }
851
Timeout()852 void Service::Timeout() {
853 // All process state flags will be taken care of in Reap(), we really just want to kill the
854 // process here when it times out. Oneshot processes will transition to be disabled, and
855 // all other processes will transition to be restarting.
856 LOG(INFO) << "Service '" << name_ << "' expired its timeout of " << timeout_period_->count()
857 << " seconds and will now be killed";
858 if (pid_) {
859 KillProcessGroup(SIGKILL);
860 NotifyStateChange("stopping");
861 }
862 }
863
Restart()864 void Service::Restart() {
865 if (flags_ & SVC_RUNNING) {
866 /* Stop, wait, then start the service. */
867 StopOrReset(SVC_RESTART);
868 } else if (!(flags_ & SVC_RESTARTING)) {
869 /* Just start the service since it's not running. */
870 if (auto result = Start(); !result.ok()) {
871 LOG(ERROR) << "Could not restart '" << name_ << "': " << result.error();
872 }
873 } /* else: Service is restarting anyways. */
874 }
875
876 // The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART.
StopOrReset(int how)877 void Service::StopOrReset(int how) {
878 // The service is still SVC_RUNNING until its process exits, but if it has
879 // already exited it shoudn't attempt a restart yet.
880 flags_ &= ~(SVC_RESTARTING | SVC_DISABLED_START);
881
882 if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) {
883 // An illegal flag: default to SVC_DISABLED.
884 LOG(ERROR) << "service '" << name_ << "' requested unknown flag " << how
885 << ", defaulting to disabling it.";
886 how = SVC_DISABLED;
887 }
888
889 // If the service has not yet started, prevent it from auto-starting with its class.
890 if (how == SVC_RESET) {
891 flags_ |= (flags_ & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
892 } else {
893 flags_ |= how;
894 }
895 // Make sure it's in right status when a restart immediately follow a
896 // stop/reset or vice versa.
897 if (how == SVC_RESTART) {
898 flags_ &= (~(SVC_DISABLED | SVC_RESET));
899 } else {
900 flags_ &= (~SVC_RESTART);
901 }
902
903 if (pid_) {
904 if (flags_ & SVC_GENTLE_KILL) {
905 KillProcessGroup(SIGTERM);
906 if (!process_cgroup_empty()) std::this_thread::sleep_for(200ms);
907 }
908 KillProcessGroup(SIGKILL);
909 NotifyStateChange("stopping");
910 } else {
911 NotifyStateChange("stopped");
912 }
913 }
914
MakeTemporaryOneshotService(const std::vector<std::string> & args)915 Result<std::unique_ptr<Service>> Service::MakeTemporaryOneshotService(
916 const std::vector<std::string>& args) {
917 // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS...
918 // SECLABEL can be a - to denote default
919 std::size_t command_arg = 1;
920 for (std::size_t i = 1; i < args.size(); ++i) {
921 if (args[i] == "--") {
922 command_arg = i + 1;
923 break;
924 }
925 }
926 if (command_arg > 4 + NR_SVC_SUPP_GIDS) {
927 return Error() << "exec called with too many supplementary group ids";
928 }
929
930 if (command_arg >= args.size()) {
931 return Error() << "exec called without command";
932 }
933 std::vector<std::string> str_args(args.begin() + command_arg, args.end());
934
935 static size_t exec_count = 0;
936 exec_count++;
937 std::string name = "exec " + std::to_string(exec_count) + " (" + Join(str_args, " ") + ")";
938
939 unsigned flags = SVC_ONESHOT | SVC_TEMPORARY;
940 unsigned namespace_flags = 0;
941
942 std::string seclabel = "";
943 if (command_arg > 2 && args[1] != "-") {
944 seclabel = args[1];
945 }
946 Result<uid_t> uid = 0;
947 if (command_arg > 3) {
948 uid = DecodeUid(args[2]);
949 if (!uid.ok()) {
950 return Error() << "Unable to decode UID for '" << args[2] << "': " << uid.error();
951 }
952 }
953 Result<gid_t> gid = 0;
954 std::vector<gid_t> supp_gids;
955 if (command_arg > 4) {
956 gid = DecodeUid(args[3]);
957 if (!gid.ok()) {
958 return Error() << "Unable to decode GID for '" << args[3] << "': " << gid.error();
959 }
960 std::size_t nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */;
961 for (size_t i = 0; i < nr_supp_gids; ++i) {
962 auto supp_gid = DecodeUid(args[4 + i]);
963 if (!supp_gid.ok()) {
964 return Error() << "Unable to decode GID for '" << args[4 + i]
965 << "': " << supp_gid.error();
966 }
967 supp_gids.push_back(*supp_gid);
968 }
969 }
970
971 return std::make_unique<Service>(name, flags, *uid, *gid, supp_gids, namespace_flags, seclabel,
972 nullptr, /*filename=*/"", str_args);
973 }
974
975 // This is used for snapuserd_proxy, which hands off a socket to snapuserd. It's
976 // a special case to support the daemon launched in first-stage init. The persist
977 // feature is not part of the init language and is only used here.
MarkSocketPersistent(const std::string & socket_name)978 bool Service::MarkSocketPersistent(const std::string& socket_name) {
979 for (auto& socket : sockets_) {
980 if (socket.name == socket_name) {
981 socket.persist = true;
982 return true;
983 }
984 }
985 return false;
986 }
987
988 } // namespace init
989 } // namespace android
990