blob: 9f09cefb8618e6077657bde8be293140027b0ce4 [file] [log] [blame]
Tom Cherrybac32992015-07-31 12:45:25 -07001/*
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
Tom Cherryb1ffb1d2019-06-26 11:22:52 -070017#pragma once
Tom Cherrybac32992015-07-31 12:45:25 -070018
Paul Crowleyc73b2152018-04-13 17:38:57 +000019#include <signal.h>
Tom Cherrybac32992015-07-31 12:45:25 -070020#include <sys/types.h>
21
Jiyong Park0d277d72023-06-09 09:52:49 +090022#include <algorithm>
Tom Cherry73f535e2018-09-27 16:10:46 -070023#include <chrono>
Tom Cherrybac32992015-07-31 12:45:25 -070024#include <memory>
Tom Cherry73f535e2018-09-27 16:10:46 -070025#include <optional>
Wei Wang641ff0a2017-03-27 10:59:11 -070026#include <set>
Tom Cherrybac32992015-07-31 12:45:25 -070027#include <string>
28#include <vector>
29
James Hawkinse78ea772017-03-24 11:43:02 -070030#include <android-base/chrono_utils.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070031#include <cutils/iosched_policy.h>
James Hawkinse78ea772017-03-24 11:43:02 -070032
Tom Cherrybac32992015-07-31 12:45:25 -070033#include "action.h"
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -040034#include "capabilities.h"
Bart Van Assche1693f422022-10-19 16:30:15 -070035#include "interprocess_fifo.h"
Tom Cherryb7349902015-08-26 11:43:36 -070036#include "keyword_map.h"
Jooyung Hanc5fa15e2022-07-14 11:23:59 +090037#include "mount_namespace.h"
Tom Cherry67dee622017-07-27 12:54:48 -070038#include "parser.h"
Vic Yange01ca4d2019-05-29 15:58:32 -070039#include "service_utils.h"
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070040#include "subcontext.h"
Tom Cherrybac32992015-07-31 12:45:25 -070041
Keun-young Park8d01f632017-03-13 11:54:47 -070042#define SVC_DISABLED 0x001 // do not autostart with class
43#define SVC_ONESHOT 0x002 // do not restart on exit
44#define SVC_RUNNING 0x004 // currently active
45#define SVC_RESTARTING 0x008 // waiting to restart
46#define SVC_CONSOLE 0x010 // requires console
Tom Cherryad9e7ea2018-10-15 17:21:48 -070047#define SVC_CRITICAL 0x020 // will reboot into bootloader if keeps crashing
Keun-young Park8d01f632017-03-13 11:54:47 -070048#define SVC_RESET 0x040 // Use when stopping a process,
Tom Cherrybac32992015-07-31 12:45:25 -070049 // but not disabling so it can be restarted with its class.
Keun-young Park8d01f632017-03-13 11:54:47 -070050#define SVC_RC_DISABLED 0x080 // Remember if the disabled flag was set in the rc script.
51#define SVC_RESTART 0x100 // Use to safely restart (stop, wait, start) a service.
Tom Cherrybac32992015-07-31 12:45:25 -070052#define SVC_DISABLED_START 0x200 // A start was requested but it was disabled at the time.
Tom Cherryb27004a2017-03-27 16:27:30 -070053#define SVC_EXEC 0x400 // This service was started by either 'exec' or 'exec_start' and stops
54 // init from processing more commands until it completes
Keun-young Park8d01f632017-03-13 11:54:47 -070055
56#define SVC_SHUTDOWN_CRITICAL 0x800 // This service is critical for shutdown and
57 // should not be killed during shutdown
Tom Cherryb27004a2017-03-27 16:27:30 -070058#define SVC_TEMPORARY 0x1000 // This service was started by 'exec' and should be removed from the
59 // service list once it is reaped.
Daniel Rosenbergde766882022-12-01 15:44:31 -080060#define SVC_GENTLE_KILL 0x2000 // This service should be stopped with SIGTERM instead of SIGKILL
61 // Will still be SIGKILLed after timeout period of 200 ms
Tom Cherrybac32992015-07-31 12:45:25 -070062
Yinchu Chen3343ca22023-08-25 04:22:06 +000063#define NR_SVC_SUPP_GIDS 32 // thirty two supplementary groups
Tom Cherrybac32992015-07-31 12:45:25 -070064
Tom Cherry81f5d3e2017-06-22 12:53:17 -070065namespace android {
66namespace init {
Tom Cherrybac32992015-07-31 12:45:25 -070067
Tom Cherrybac32992015-07-31 12:45:25 -070068class Service {
Tom Cherryb1ffb1d2019-06-26 11:22:52 -070069 friend class ServiceParser;
70
Wei Wang641ff0a2017-03-27 10:59:11 -070071 public:
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070072 Service(const std::string& name, Subcontext* subcontext_for_restart_commands,
Deyao Rendf40ed12022-07-14 22:51:10 +000073 const std::string& filename, const std::vector<std::string>& args);
Tom Cherrybac32992015-07-31 12:45:25 -070074
Steven Morelandf5d22ef2023-04-03 23:29:22 +000075 Service(const std::string& name, unsigned flags, std::optional<uid_t> uid, gid_t gid,
Tom Cherry247ffbf2019-07-08 15:09:36 -070076 const std::vector<gid_t>& supp_gids, int namespace_flags, const std::string& seclabel,
Deyao Rendf40ed12022-07-14 22:51:10 +000077 Subcontext* subcontext_for_restart_commands, const std::string& filename,
78 const std::vector<std::string>& args);
Bart Van Assche0bb47572022-12-05 10:43:00 -080079 Service(const Service&) = delete;
80 void operator=(const Service&) = delete;
Tom Cherrybac32992015-07-31 12:45:25 -070081
Tom Cherry4772f1d2019-07-30 09:34:41 -070082 static Result<std::unique_ptr<Service>> MakeTemporaryOneshotService(
83 const std::vector<std::string>& args);
Tom Cherry3b81f2d2017-07-28 14:48:41 -070084
Keun-young Park8d01f632017-03-13 11:54:47 -070085 bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; }
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010086 bool IsEnabled() { return (flags_ & SVC_DISABLED) == 0; }
Tom Cherry832f9f12020-03-10 11:47:24 -070087 Result<void> ExecStart();
88 Result<void> Start();
89 Result<void> StartIfNotDisabled();
Tom Cherry832f9f12020-03-10 11:47:24 -070090 Result<void> Enable();
Tom Cherrybac32992015-07-31 12:45:25 -070091 void Reset();
92 void Stop();
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080093 void Terminate();
Tom Cherry73f535e2018-09-27 16:10:46 -070094 void Timeout();
Tom Cherry832f9f12020-03-10 11:47:24 -070095 void Restart();
Paul Crowleyc73b2152018-04-13 17:38:57 +000096 void Reap(const siginfo_t& siginfo);
Tom Cherrybac32992015-07-31 12:45:25 -070097 void DumpState() const;
Keun-young Park8d01f632017-03-13 11:54:47 -070098 void SetShutdownCritical() { flags_ |= SVC_SHUTDOWN_CRITICAL; }
Wei Wang641ff0a2017-03-27 10:59:11 -070099 bool IsShutdownCritical() const { return (flags_ & SVC_SHUTDOWN_CRITICAL) != 0; }
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700100 void UnSetExec() {
101 is_exec_service_running_ = false;
102 flags_ &= ~SVC_EXEC;
103 }
Paul Crowleyc73b2152018-04-13 17:38:57 +0000104 void AddReapCallback(std::function<void(const siginfo_t& siginfo)> callback) {
105 reap_callbacks_.emplace_back(std::move(callback));
106 }
David Anderson0e5ad5a2021-07-21 21:53:28 -0700107 void SetStartedInFirstStage(pid_t pid);
108 bool MarkSocketPersistent(const std::string& socket_name);
Tom Cherry6737a6b2019-08-05 15:03:58 -0700109 size_t CheckAllCommands() const { return onrestart_.CheckAllCommands(); }
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700110
111 static bool is_exec_service_running() { return is_exec_service_running_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700112
113 const std::string& name() const { return name_; }
Wei Wang641ff0a2017-03-27 10:59:11 -0700114 const std::set<std::string>& classnames() const { return classnames_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700115 unsigned flags() const { return flags_; }
116 pid_t pid() const { return pid_; }
Tom Cherryd269e3a2017-07-31 13:23:18 -0700117 android::base::boot_clock::time_point time_started() const { return time_started_; }
Tom Cherry7da54852017-05-01 14:16:41 -0700118 int crash_count() const { return crash_count_; }
Jiyong Park0d277d72023-06-09 09:52:49 +0900119 int was_last_exit_ok() const { return was_last_exit_ok_; }
Steven Morelandf5d22ef2023-04-03 23:29:22 +0000120 uid_t uid() const { return proc_attr_.uid(); }
Vic Yange01ca4d2019-05-29 15:58:32 -0700121 gid_t gid() const { return proc_attr_.gid; }
Tom Cherry247ffbf2019-07-08 15:09:36 -0700122 int namespace_flags() const { return namespaces_.flags; }
Vic Yange01ca4d2019-05-29 15:58:32 -0700123 const std::vector<gid_t>& supp_gids() const { return proc_attr_.supp_gids; }
Tom Cherrybac32992015-07-31 12:45:25 -0700124 const std::string& seclabel() const { return seclabel_; }
125 const std::vector<int>& keycodes() const { return keycodes_; }
Vic Yange01ca4d2019-05-29 15:58:32 -0700126 IoSchedClass ioprio_class() const { return proc_attr_.ioprio_class; }
127 int ioprio_pri() const { return proc_attr_.ioprio_pri; }
Steven Morelande055d732017-10-05 18:50:22 -0700128 const std::set<std::string>& interfaces() const { return interfaces_; }
Vic Yange01ca4d2019-05-29 15:58:32 -0700129 int priority() const { return proc_attr_.priority; }
Tom Cherry7da54852017-05-01 14:16:41 -0700130 int oom_score_adjust() const { return oom_score_adjust_; }
Steven Moreland6f5333a2017-11-13 15:31:54 -0800131 bool is_override() const { return override_; }
Tom Cherry33838b12017-05-04 11:32:36 -0700132 bool process_cgroup_empty() const { return process_cgroup_empty_; }
Tom Cherry59383792017-07-26 16:09:09 -0700133 unsigned long start_order() const { return start_order_; }
Tom Cherry8f380482018-04-17 14:48:44 -0700134 void set_sigstop(bool value) { sigstop_ = value; }
Jiyong Park0d277d72023-06-09 09:52:49 +0900135 std::chrono::seconds restart_period() const {
136 // If the service exited abnormally or due to timeout, late limit the restart even if
137 // restart_period is set to a very short value.
138 // If not, i.e. restart after a deliberate and successful exit, respect the period.
139 if (!was_last_exit_ok_) {
140 return std::max(restart_period_, default_restart_period_);
141 }
142 return restart_period_;
143 }
Tom Cherry73f535e2018-09-27 16:10:46 -0700144 std::optional<std::chrono::seconds> timeout_period() const { return timeout_period_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700145 const std::vector<std::string>& args() const { return args_; }
Jiyong Park80aa4472018-11-12 12:08:41 +0900146 bool is_updatable() const { return updatable_; }
Martijn Coenen70788f92019-04-23 16:26:01 +0200147 bool is_post_data() const { return post_data_; }
Deyao Rendf40ed12022-07-14 22:51:10 +0000148 bool is_from_apex() const { return base::StartsWith(filename_, "/apex/"); }
Tom Cherrybdbf5042020-03-04 10:52:08 -0800149 void set_oneshot(bool value) {
150 if (value) {
151 flags_ |= SVC_ONESHOT;
152 } else {
153 flags_ &= ~SVC_ONESHOT;
154 }
155 }
Bart Van Asschebfcf4372022-11-14 11:24:12 -0800156 const Subcontext* subcontext() const { return subcontext_; }
Deyao Ren07595e12022-07-15 22:02:14 +0000157 const std::string& filename() const { return filename_; }
158 void set_filename(const std::string& name) { filename_ = name; }
Tom Cherrybac32992015-07-31 12:45:25 -0700159
Wei Wang641ff0a2017-03-27 10:59:11 -0700160 private:
Tom Cherrybac32992015-07-31 12:45:25 -0700161 void NotifyStateChange(const std::string& new_state) const;
162 void StopOrReset(int how);
Tom Cherryd89ed132019-11-19 14:19:40 -0800163 void KillProcessGroup(int signal, bool report_oneshot = false);
Bart Van Assche01e66692022-11-14 16:45:47 -0800164 void SetProcessAttributesAndCaps(InterprocessFifo setsid_finished);
David Anderson0e5ad5a2021-07-21 21:53:28 -0700165 void ResetFlagsForStart();
Bart Van Assche847b80a2022-02-25 23:28:59 +0000166 Result<void> CheckConsole();
Bart Van Asschef2222aa2022-02-25 22:44:40 +0000167 void ConfigureMemcg();
Bart Van Assche01e66692022-11-14 16:45:47 -0800168 void RunService(const std::vector<Descriptor>& descriptors, InterprocessFifo cgroups_activated,
169 InterprocessFifo setsid_finished);
Jooyung Hanc5fa15e2022-07-14 11:23:59 +0900170 void SetMountNamespace();
Tom Cherry59383792017-07-26 16:09:09 -0700171 static unsigned long next_start_order_;
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700172 static bool is_exec_service_running_;
Tom Cherry59383792017-07-26 16:09:09 -0700173
Bart Van Asschebfcf4372022-11-14 11:24:12 -0800174 const std::string name_;
Wei Wang641ff0a2017-03-27 10:59:11 -0700175 std::set<std::string> classnames_;
Tom Cherrybac32992015-07-31 12:45:25 -0700176
177 unsigned flags_;
178 pid_t pid_;
James Hawkinse78ea772017-03-24 11:43:02 -0700179 android::base::boot_clock::time_point time_started_; // time of last start
180 android::base::boot_clock::time_point time_crashed_; // first crash within inspection window
Elliott Hughes9605a942016-11-10 17:43:47 -0800181 int crash_count_; // number of times crashed within window
Florian Mayer2ef47f82022-05-10 17:55:08 -0700182 bool upgraded_mte_ = false; // whether we upgraded async MTE -> sync MTE before
Woody Lin45215ae2019-12-26 22:22:28 +0800183 std::chrono::minutes fatal_crash_window_ = 4min; // fatal() when more than 4 crashes in it
184 std::optional<std::string> fatal_reboot_target_; // reboot target of fatal handler
Jiyong Park0d277d72023-06-09 09:52:49 +0900185 bool was_last_exit_ok_ =
186 true; // true if the service never exited, or exited with status code 0
Tom Cherrybac32992015-07-31 12:45:25 -0700187
Tom Cherry1cd082d2019-02-06 10:45:56 -0800188 std::optional<CapSet> capabilities_;
Vic Yange01ca4d2019-05-29 15:58:32 -0700189 ProcessAttributes proc_attr_;
190 NamespaceInfo namespaces_;
Tom Cherrybac32992015-07-31 12:45:25 -0700191
192 std::string seclabel_;
193
Tom Cherry2e4c85f2019-07-09 13:33:36 -0700194 std::vector<SocketDescriptor> sockets_;
195 std::vector<FileDescriptor> files_;
Tom Cherry6de21f12017-08-22 15:41:03 -0700196 std::vector<std::pair<std::string, std::string>> environment_vars_;
Florian Mayer2ef47f82022-05-10 17:55:08 -0700197 // Environment variables that only get applied to the next run.
198 std::vector<std::pair<std::string, std::string>> once_environment_vars_;
Tom Cherrybac32992015-07-31 12:45:25 -0700199
Bart Van Asschebfcf4372022-11-14 11:24:12 -0800200 const Subcontext* const subcontext_;
Tom Cherrybac32992015-07-31 12:45:25 -0700201 Action onrestart_; // Commands to execute on restart.
202
203 std::vector<std::string> writepid_files_;
204
Suren Baghdasaryanc9c0bba2020-04-30 11:58:39 -0700205 std::vector<std::string> task_profiles_;
206
Steven Morelande055d732017-10-05 18:50:22 -0700207 std::set<std::string> interfaces_; // e.g. some.package.foo@1.0::IBaz/instance-name
208
Mark Salyzyn13857252018-05-18 15:25:15 -0700209 // keycodes for triggering this service via /dev/input/input*
Tom Cherrybac32992015-07-31 12:45:25 -0700210 std::vector<int> keycodes_;
Tom Cherrybac32992015-07-31 12:45:25 -0700211
Marco Nelissen310f6702016-07-22 12:07:06 -0700212 int oom_score_adjust_;
213
Peter Collingbourned7157c22018-10-30 15:49:33 -0700214 int swappiness_ = -1;
215 int soft_limit_in_bytes_ = -1;
216
217 int limit_in_bytes_ = -1;
218 int limit_percent_ = -1;
219 std::string limit_property_;
Robert Benead4852262017-07-16 19:38:11 -0700220
Tom Cherry33838b12017-05-04 11:32:36 -0700221 bool process_cgroup_empty_ = false;
222
Steven Moreland6f5333a2017-11-13 15:31:54 -0800223 bool override_ = false;
224
Tom Cherry59383792017-07-26 16:09:09 -0700225 unsigned long start_order_;
226
Tom Cherry8f380482018-04-17 14:48:44 -0700227 bool sigstop_ = false;
228
Jiyong Park0d277d72023-06-09 09:52:49 +0900229 const std::chrono::seconds default_restart_period_ = 5s;
230 std::chrono::seconds restart_period_ = default_restart_period_;
Tom Cherry73f535e2018-09-27 16:10:46 -0700231 std::optional<std::chrono::seconds> timeout_period_;
232
Jiyong Park80aa4472018-11-12 12:08:41 +0900233 bool updatable_ = false;
234
Bart Van Asschebfcf4372022-11-14 11:24:12 -0800235 const std::vector<std::string> args_;
Paul Crowleyc73b2152018-04-13 17:38:57 +0000236
237 std::vector<std::function<void(const siginfo_t& siginfo)>> reap_callbacks_;
Jiyong Park68660412019-01-16 23:00:59 +0900238
Jooyung Hanc5fa15e2022-07-14 11:23:59 +0900239 std::optional<MountNamespace> mount_namespace_;
Martijn Coenen70788f92019-04-23 16:26:01 +0200240
241 bool post_data_ = false;
Martijn Coenenacc45aa2019-05-15 22:04:13 +0200242
Tom Cherry60971e62019-09-10 10:40:47 -0700243 std::optional<std::string> on_failure_reboot_target_;
Nikita Ioffe091c4d12019-12-05 12:35:19 +0000244
Deyao Rendf40ed12022-07-14 22:51:10 +0000245 std::string filename_;
Tom Cherrybac32992015-07-31 12:45:25 -0700246};
247
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700248} // namespace init
249} // namespace android