blob: 0bfc2e6aac021b2fe1be8d6682eeca09c7344cc8 [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
17#ifndef _INIT_SERVICE_H
18#define _INIT_SERVICE_H
19
Paul Crowleyc73b2152018-04-13 17:38:57 +000020#include <signal.h>
Tom Cherry7ac013d2017-08-25 10:39:25 -070021#include <sys/resource.h>
Tom Cherrybac32992015-07-31 12:45:25 -070022#include <sys/types.h>
23
Tom Cherry73f535e2018-09-27 16:10:46 -070024#include <chrono>
Tom Cherrybac32992015-07-31 12:45:25 -070025#include <memory>
Tom Cherry73f535e2018-09-27 16:10:46 -070026#include <optional>
Wei Wang641ff0a2017-03-27 10:59:11 -070027#include <set>
Tom Cherrybac32992015-07-31 12:45:25 -070028#include <string>
29#include <vector>
30
James Hawkinse78ea772017-03-24 11:43:02 -070031#include <android-base/chrono_utils.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070032#include <cutils/iosched_policy.h>
James Hawkinse78ea772017-03-24 11:43:02 -070033
Tom Cherrybac32992015-07-31 12:45:25 -070034#include "action.h"
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -040035#include "capabilities.h"
Mark Salyzyn62767fe2016-10-27 07:45:34 -070036#include "descriptors.h"
Tom Cherryb7349902015-08-26 11:43:36 -070037#include "keyword_map.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.
Tom Cherrybac32992015-07-31 12:45:25 -070060
61#define NR_SVC_SUPP_GIDS 12 // twelve supplementary groups
62
Tom Cherry81f5d3e2017-06-22 12:53:17 -070063namespace android {
64namespace init {
Tom Cherrybac32992015-07-31 12:45:25 -070065
Tom Cherrybac32992015-07-31 12:45:25 -070066class Service {
Wei Wang641ff0a2017-03-27 10:59:11 -070067 public:
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070068 Service(const std::string& name, Subcontext* subcontext_for_restart_commands,
69 const std::vector<std::string>& args);
Tom Cherrybac32992015-07-31 12:45:25 -070070
Wei Wang641ff0a2017-03-27 10:59:11 -070071 Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid,
Tom Cherry1cd082d2019-02-06 10:45:56 -080072 const std::vector<gid_t>& supp_gids, unsigned namespace_flags,
73 const std::string& seclabel, Subcontext* subcontext_for_restart_commands,
74 const std::vector<std::string>& args);
Tom Cherrybac32992015-07-31 12:45:25 -070075
Tom Cherry3b81f2d2017-07-28 14:48:41 -070076 static std::unique_ptr<Service> MakeTemporaryOneshotService(const std::vector<std::string>& args);
77
Keun-young Park8d01f632017-03-13 11:54:47 -070078 bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070079 Result<void> ParseLine(std::vector<std::string>&& args);
80 Result<void> ExecStart();
81 Result<void> Start();
82 Result<void> StartIfNotDisabled();
83 Result<void> StartIfPostData();
84 Result<void> Enable();
Tom Cherrybac32992015-07-31 12:45:25 -070085 void Reset();
Martijn Coenen70788f92019-04-23 16:26:01 +020086 void ResetIfPostData();
Tom Cherrybac32992015-07-31 12:45:25 -070087 void Stop();
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080088 void Terminate();
Tom Cherry73f535e2018-09-27 16:10:46 -070089 void Timeout();
Tom Cherrybac32992015-07-31 12:45:25 -070090 void Restart();
Paul Crowleyc73b2152018-04-13 17:38:57 +000091 void Reap(const siginfo_t& siginfo);
Tom Cherrybac32992015-07-31 12:45:25 -070092 void DumpState() const;
Keun-young Park8d01f632017-03-13 11:54:47 -070093 void SetShutdownCritical() { flags_ |= SVC_SHUTDOWN_CRITICAL; }
Wei Wang641ff0a2017-03-27 10:59:11 -070094 bool IsShutdownCritical() const { return (flags_ & SVC_SHUTDOWN_CRITICAL) != 0; }
Tom Cherry3b81f2d2017-07-28 14:48:41 -070095 void UnSetExec() {
96 is_exec_service_running_ = false;
97 flags_ &= ~SVC_EXEC;
98 }
Paul Crowleyc73b2152018-04-13 17:38:57 +000099 void AddReapCallback(std::function<void(const siginfo_t& siginfo)> callback) {
100 reap_callbacks_.emplace_back(std::move(callback));
101 }
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700102
103 static bool is_exec_service_running() { return is_exec_service_running_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700104
105 const std::string& name() const { return name_; }
Wei Wang641ff0a2017-03-27 10:59:11 -0700106 const std::set<std::string>& classnames() const { return classnames_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700107 unsigned flags() const { return flags_; }
108 pid_t pid() const { return pid_; }
Tom Cherryd269e3a2017-07-31 13:23:18 -0700109 android::base::boot_clock::time_point time_started() const { return time_started_; }
Tom Cherry7da54852017-05-01 14:16:41 -0700110 int crash_count() const { return crash_count_; }
Vic Yange01ca4d2019-05-29 15:58:32 -0700111 uid_t uid() const { return proc_attr_.uid; }
112 gid_t gid() const { return proc_attr_.gid; }
113 unsigned namespace_flags() const { return namespaces_.flags; }
114 const std::vector<gid_t>& supp_gids() const { return proc_attr_.supp_gids; }
Tom Cherrybac32992015-07-31 12:45:25 -0700115 const std::string& seclabel() const { return seclabel_; }
116 const std::vector<int>& keycodes() const { return keycodes_; }
Vic Yange01ca4d2019-05-29 15:58:32 -0700117 IoSchedClass ioprio_class() const { return proc_attr_.ioprio_class; }
118 int ioprio_pri() const { return proc_attr_.ioprio_pri; }
Steven Morelande055d732017-10-05 18:50:22 -0700119 const std::set<std::string>& interfaces() const { return interfaces_; }
Vic Yange01ca4d2019-05-29 15:58:32 -0700120 int priority() const { return proc_attr_.priority; }
Tom Cherry7da54852017-05-01 14:16:41 -0700121 int oom_score_adjust() const { return oom_score_adjust_; }
Steven Moreland6f5333a2017-11-13 15:31:54 -0800122 bool is_override() const { return override_; }
Tom Cherry33838b12017-05-04 11:32:36 -0700123 bool process_cgroup_empty() const { return process_cgroup_empty_; }
Tom Cherry59383792017-07-26 16:09:09 -0700124 unsigned long start_order() const { return start_order_; }
Tom Cherry8f380482018-04-17 14:48:44 -0700125 void set_sigstop(bool value) { sigstop_ = value; }
Tom Cherry73f535e2018-09-27 16:10:46 -0700126 std::chrono::seconds restart_period() const { return restart_period_; }
127 std::optional<std::chrono::seconds> timeout_period() const { return timeout_period_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700128 const std::vector<std::string>& args() const { return args_; }
Jiyong Park80aa4472018-11-12 12:08:41 +0900129 bool is_updatable() const { return updatable_; }
Martijn Coenen70788f92019-04-23 16:26:01 +0200130 bool is_post_data() const { return post_data_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700131
Wei Wang641ff0a2017-03-27 10:59:11 -0700132 private:
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700133 using OptionParser = Result<void> (Service::*)(std::vector<std::string>&& args);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400134 class OptionParserMap;
Tom Cherryb7349902015-08-26 11:43:36 -0700135
Tom Cherrybac32992015-07-31 12:45:25 -0700136 void NotifyStateChange(const std::string& new_state) const;
137 void StopOrReset(int how);
Elliott Hughesad8e94e2016-06-15 14:49:57 -0700138 void KillProcessGroup(int signal);
Vic Yange01ca4d2019-05-29 15:58:32 -0700139 void SetProcessAttributesAndCaps();
Tom Cherrybac32992015-07-31 12:45:25 -0700140
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700141 Result<void> ParseCapabilities(std::vector<std::string>&& args);
142 Result<void> ParseClass(std::vector<std::string>&& args);
143 Result<void> ParseConsole(std::vector<std::string>&& args);
144 Result<void> ParseCritical(std::vector<std::string>&& args);
145 Result<void> ParseDisabled(std::vector<std::string>&& args);
146 Result<void> ParseEnterNamespace(std::vector<std::string>&& args);
147 Result<void> ParseGroup(std::vector<std::string>&& args);
148 Result<void> ParsePriority(std::vector<std::string>&& args);
149 Result<void> ParseInterface(std::vector<std::string>&& args);
150 Result<void> ParseIoprio(std::vector<std::string>&& args);
151 Result<void> ParseKeycodes(std::vector<std::string>&& args);
152 Result<void> ParseOneshot(std::vector<std::string>&& args);
153 Result<void> ParseOnrestart(std::vector<std::string>&& args);
154 Result<void> ParseOomScoreAdjust(std::vector<std::string>&& args);
155 Result<void> ParseOverride(std::vector<std::string>&& args);
156 Result<void> ParseMemcgLimitInBytes(std::vector<std::string>&& args);
157 Result<void> ParseMemcgLimitPercent(std::vector<std::string>&& args);
158 Result<void> ParseMemcgLimitProperty(std::vector<std::string>&& args);
159 Result<void> ParseMemcgSoftLimitInBytes(std::vector<std::string>&& args);
160 Result<void> ParseMemcgSwappiness(std::vector<std::string>&& args);
161 Result<void> ParseNamespace(std::vector<std::string>&& args);
162 Result<void> ParseProcessRlimit(std::vector<std::string>&& args);
163 Result<void> ParseRestartPeriod(std::vector<std::string>&& args);
164 Result<void> ParseSeclabel(std::vector<std::string>&& args);
165 Result<void> ParseSetenv(std::vector<std::string>&& args);
166 Result<void> ParseShutdown(std::vector<std::string>&& args);
167 Result<void> ParseSigstop(std::vector<std::string>&& args);
168 Result<void> ParseSocket(std::vector<std::string>&& args);
169 Result<void> ParseTimeoutPeriod(std::vector<std::string>&& args);
170 Result<void> ParseFile(std::vector<std::string>&& args);
171 Result<void> ParseUser(std::vector<std::string>&& args);
172 Result<void> ParseWritepid(std::vector<std::string>&& args);
173 Result<void> ParseUpdatable(std::vector<std::string>&& args);
Tom Cherryb7349902015-08-26 11:43:36 -0700174
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700175 template <typename T>
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700176 Result<void> AddDescriptor(std::vector<std::string>&& args);
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700177
Tom Cherry59383792017-07-26 16:09:09 -0700178 static unsigned long next_start_order_;
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700179 static bool is_exec_service_running_;
Tom Cherry59383792017-07-26 16:09:09 -0700180
Tom Cherrybac32992015-07-31 12:45:25 -0700181 std::string name_;
Wei Wang641ff0a2017-03-27 10:59:11 -0700182 std::set<std::string> classnames_;
Tom Cherrybac32992015-07-31 12:45:25 -0700183
184 unsigned flags_;
185 pid_t pid_;
James Hawkinse78ea772017-03-24 11:43:02 -0700186 android::base::boot_clock::time_point time_started_; // time of last start
187 android::base::boot_clock::time_point time_crashed_; // first crash within inspection window
Elliott Hughes9605a942016-11-10 17:43:47 -0800188 int crash_count_; // number of times crashed within window
Tom Cherrybac32992015-07-31 12:45:25 -0700189
Tom Cherry1cd082d2019-02-06 10:45:56 -0800190 std::optional<CapSet> capabilities_;
Vic Yange01ca4d2019-05-29 15:58:32 -0700191 ProcessAttributes proc_attr_;
192 NamespaceInfo namespaces_;
Tom Cherrybac32992015-07-31 12:45:25 -0700193
194 std::string seclabel_;
195
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700196 std::vector<std::unique_ptr<DescriptorInfo>> descriptors_;
Tom Cherry6de21f12017-08-22 15:41:03 -0700197 std::vector<std::pair<std::string, std::string>> environment_vars_;
Tom Cherrybac32992015-07-31 12:45:25 -0700198
199 Action onrestart_; // Commands to execute on restart.
200
201 std::vector<std::string> writepid_files_;
202
Steven Morelande055d732017-10-05 18:50:22 -0700203 std::set<std::string> interfaces_; // e.g. some.package.foo@1.0::IBaz/instance-name
204
Mark Salyzyn13857252018-05-18 15:25:15 -0700205 // keycodes for triggering this service via /dev/input/input*
Tom Cherrybac32992015-07-31 12:45:25 -0700206 std::vector<int> keycodes_;
Tom Cherrybac32992015-07-31 12:45:25 -0700207
Marco Nelissen310f6702016-07-22 12:07:06 -0700208 int oom_score_adjust_;
209
Peter Collingbourned7157c22018-10-30 15:49:33 -0700210 int swappiness_ = -1;
211 int soft_limit_in_bytes_ = -1;
212
213 int limit_in_bytes_ = -1;
214 int limit_percent_ = -1;
215 std::string limit_property_;
Robert Benead4852262017-07-16 19:38:11 -0700216
Tom Cherry33838b12017-05-04 11:32:36 -0700217 bool process_cgroup_empty_ = false;
218
Steven Moreland6f5333a2017-11-13 15:31:54 -0800219 bool override_ = false;
220
Tom Cherry59383792017-07-26 16:09:09 -0700221 unsigned long start_order_;
222
Tom Cherry8f380482018-04-17 14:48:44 -0700223 bool sigstop_ = false;
224
Tom Cherry73f535e2018-09-27 16:10:46 -0700225 std::chrono::seconds restart_period_ = 5s;
226 std::optional<std::chrono::seconds> timeout_period_;
227
Jiyong Park80aa4472018-11-12 12:08:41 +0900228 bool updatable_ = false;
229
Tom Cherrybac32992015-07-31 12:45:25 -0700230 std::vector<std::string> args_;
Paul Crowleyc73b2152018-04-13 17:38:57 +0000231
232 std::vector<std::function<void(const siginfo_t& siginfo)>> reap_callbacks_;
Jiyong Park68660412019-01-16 23:00:59 +0900233
234 bool pre_apexd_ = false;
Martijn Coenen70788f92019-04-23 16:26:01 +0200235
236 bool post_data_ = false;
Martijn Coenenacc45aa2019-05-15 22:04:13 +0200237
238 bool running_at_post_data_reset_ = false;
Tom Cherrybac32992015-07-31 12:45:25 -0700239};
240
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700241} // namespace init
242} // namespace android
243
Tom Cherrybac32992015-07-31 12:45:25 -0700244#endif