blob: c29723a4ef77d131c3827d5f1b71b8b9ee308c48 [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"
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070039#include "subcontext.h"
Tom Cherrybac32992015-07-31 12:45:25 -070040
Keun-young Park8d01f632017-03-13 11:54:47 -070041#define SVC_DISABLED 0x001 // do not autostart with class
42#define SVC_ONESHOT 0x002 // do not restart on exit
43#define SVC_RUNNING 0x004 // currently active
44#define SVC_RESTARTING 0x008 // waiting to restart
45#define SVC_CONSOLE 0x010 // requires console
Tom Cherryad9e7ea2018-10-15 17:21:48 -070046#define SVC_CRITICAL 0x020 // will reboot into bootloader if keeps crashing
Keun-young Park8d01f632017-03-13 11:54:47 -070047#define SVC_RESET 0x040 // Use when stopping a process,
Tom Cherrybac32992015-07-31 12:45:25 -070048 // but not disabling so it can be restarted with its class.
Keun-young Park8d01f632017-03-13 11:54:47 -070049#define SVC_RC_DISABLED 0x080 // Remember if the disabled flag was set in the rc script.
50#define SVC_RESTART 0x100 // Use to safely restart (stop, wait, start) a service.
Tom Cherrybac32992015-07-31 12:45:25 -070051#define SVC_DISABLED_START 0x200 // A start was requested but it was disabled at the time.
Tom Cherryb27004a2017-03-27 16:27:30 -070052#define SVC_EXEC 0x400 // This service was started by either 'exec' or 'exec_start' and stops
53 // init from processing more commands until it completes
Keun-young Park8d01f632017-03-13 11:54:47 -070054
55#define SVC_SHUTDOWN_CRITICAL 0x800 // This service is critical for shutdown and
56 // should not be killed during shutdown
Tom Cherryb27004a2017-03-27 16:27:30 -070057#define SVC_TEMPORARY 0x1000 // This service was started by 'exec' and should be removed from the
58 // service list once it is reaped.
Tom Cherrybac32992015-07-31 12:45:25 -070059
60#define NR_SVC_SUPP_GIDS 12 // twelve supplementary groups
61
Tom Cherry81f5d3e2017-06-22 12:53:17 -070062namespace android {
63namespace init {
Tom Cherrybac32992015-07-31 12:45:25 -070064
Tom Cherrybac32992015-07-31 12:45:25 -070065class Service {
Wei Wang641ff0a2017-03-27 10:59:11 -070066 public:
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070067 Service(const std::string& name, Subcontext* subcontext_for_restart_commands,
68 const std::vector<std::string>& args);
Tom Cherrybac32992015-07-31 12:45:25 -070069
Wei Wang641ff0a2017-03-27 10:59:11 -070070 Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid,
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -040071 const std::vector<gid_t>& supp_gids, const CapSet& capabilities,
72 unsigned namespace_flags, const std::string& seclabel,
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070073 Subcontext* subcontext_for_restart_commands, const std::vector<std::string>& args);
Tom Cherrybac32992015-07-31 12:45:25 -070074
Tom Cherry3b81f2d2017-07-28 14:48:41 -070075 static std::unique_ptr<Service> MakeTemporaryOneshotService(const std::vector<std::string>& args);
76
Keun-young Park8d01f632017-03-13 11:54:47 -070077 bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; }
Tom Cherry018a4382018-10-17 11:11:23 -070078 Result<Success> ParseLine(std::vector<std::string>&& args);
Tom Cherry76af7e62017-08-22 16:13:59 -070079 Result<Success> ExecStart();
80 Result<Success> Start();
81 Result<Success> StartIfNotDisabled();
82 Result<Success> Enable();
Tom Cherrybac32992015-07-31 12:45:25 -070083 void Reset();
84 void Stop();
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080085 void Terminate();
Tom Cherry73f535e2018-09-27 16:10:46 -070086 void Timeout();
Tom Cherrybac32992015-07-31 12:45:25 -070087 void Restart();
Paul Crowleyc73b2152018-04-13 17:38:57 +000088 void Reap(const siginfo_t& siginfo);
Tom Cherrybac32992015-07-31 12:45:25 -070089 void DumpState() const;
Keun-young Park8d01f632017-03-13 11:54:47 -070090 void SetShutdownCritical() { flags_ |= SVC_SHUTDOWN_CRITICAL; }
Wei Wang641ff0a2017-03-27 10:59:11 -070091 bool IsShutdownCritical() const { return (flags_ & SVC_SHUTDOWN_CRITICAL) != 0; }
Tom Cherry3b81f2d2017-07-28 14:48:41 -070092 void UnSetExec() {
93 is_exec_service_running_ = false;
94 flags_ &= ~SVC_EXEC;
95 }
Paul Crowleyc73b2152018-04-13 17:38:57 +000096 void AddReapCallback(std::function<void(const siginfo_t& siginfo)> callback) {
97 reap_callbacks_.emplace_back(std::move(callback));
98 }
Tom Cherry3b81f2d2017-07-28 14:48:41 -070099
100 static bool is_exec_service_running() { return is_exec_service_running_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700101
102 const std::string& name() const { return name_; }
Wei Wang641ff0a2017-03-27 10:59:11 -0700103 const std::set<std::string>& classnames() const { return classnames_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700104 unsigned flags() const { return flags_; }
105 pid_t pid() const { return pid_; }
Tom Cherryd269e3a2017-07-31 13:23:18 -0700106 android::base::boot_clock::time_point time_started() const { return time_started_; }
Tom Cherry7da54852017-05-01 14:16:41 -0700107 int crash_count() const { return crash_count_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700108 uid_t uid() const { return uid_; }
109 gid_t gid() const { return gid_; }
Tom Cherry7da54852017-05-01 14:16:41 -0700110 unsigned namespace_flags() const { return namespace_flags_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700111 const std::vector<gid_t>& supp_gids() const { return supp_gids_; }
112 const std::string& seclabel() const { return seclabel_; }
113 const std::vector<int>& keycodes() const { return keycodes_; }
Tom Cherry7da54852017-05-01 14:16:41 -0700114 IoSchedClass ioprio_class() const { return ioprio_class_; }
115 int ioprio_pri() const { return ioprio_pri_; }
Steven Morelande055d732017-10-05 18:50:22 -0700116 const std::set<std::string>& interfaces() const { return interfaces_; }
Tom Cherry7da54852017-05-01 14:16:41 -0700117 int priority() const { return priority_; }
118 int oom_score_adjust() const { return oom_score_adjust_; }
Steven Moreland6f5333a2017-11-13 15:31:54 -0800119 bool is_override() const { return override_; }
Tom Cherry33838b12017-05-04 11:32:36 -0700120 bool process_cgroup_empty() const { return process_cgroup_empty_; }
Tom Cherry59383792017-07-26 16:09:09 -0700121 unsigned long start_order() const { return start_order_; }
Tom Cherry8f380482018-04-17 14:48:44 -0700122 void set_sigstop(bool value) { sigstop_ = value; }
Tom Cherry73f535e2018-09-27 16:10:46 -0700123 std::chrono::seconds restart_period() const { return restart_period_; }
124 std::optional<std::chrono::seconds> timeout_period() const { return timeout_period_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700125 const std::vector<std::string>& args() const { return args_; }
Jiyong Park80aa4472018-11-12 12:08:41 +0900126 bool is_updatable() const { return updatable_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700127
Wei Wang641ff0a2017-03-27 10:59:11 -0700128 private:
Tom Cherry018a4382018-10-17 11:11:23 -0700129 using OptionParser = Result<Success> (Service::*)(std::vector<std::string>&& args);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400130 class OptionParserMap;
Tom Cherryb7349902015-08-26 11:43:36 -0700131
Tom Cherryaead51b2018-04-20 16:18:12 -0700132 Result<Success> SetUpMountNamespace() const;
133 Result<Success> SetUpPidNamespace() const;
134 Result<Success> EnterNamespaces() const;
Tom Cherrybac32992015-07-31 12:45:25 -0700135 void NotifyStateChange(const std::string& new_state) const;
136 void StopOrReset(int how);
137 void ZapStdio() const;
138 void OpenConsole() const;
Elliott Hughesad8e94e2016-06-15 14:49:57 -0700139 void KillProcessGroup(int signal);
Jorge Lucangeli Obes344d01f2016-07-08 13:32:26 -0400140 void SetProcessAttributes();
Tom Cherrybac32992015-07-31 12:45:25 -0700141
Tom Cherry018a4382018-10-17 11:11:23 -0700142 Result<Success> ParseCapabilities(std::vector<std::string>&& args);
143 Result<Success> ParseClass(std::vector<std::string>&& args);
144 Result<Success> ParseConsole(std::vector<std::string>&& args);
145 Result<Success> ParseCritical(std::vector<std::string>&& args);
146 Result<Success> ParseDisabled(std::vector<std::string>&& args);
147 Result<Success> ParseEnterNamespace(std::vector<std::string>&& args);
148 Result<Success> ParseGroup(std::vector<std::string>&& args);
149 Result<Success> ParsePriority(std::vector<std::string>&& args);
150 Result<Success> ParseInterface(std::vector<std::string>&& args);
151 Result<Success> ParseIoprio(std::vector<std::string>&& args);
152 Result<Success> ParseKeycodes(std::vector<std::string>&& args);
153 Result<Success> ParseOneshot(std::vector<std::string>&& args);
154 Result<Success> ParseOnrestart(std::vector<std::string>&& args);
155 Result<Success> ParseOomScoreAdjust(std::vector<std::string>&& args);
156 Result<Success> ParseOverride(std::vector<std::string>&& args);
157 Result<Success> ParseMemcgLimitInBytes(std::vector<std::string>&& args);
Peter Collingbourned7157c22018-10-30 15:49:33 -0700158 Result<Success> ParseMemcgLimitPercent(std::vector<std::string>&& args);
159 Result<Success> ParseMemcgLimitProperty(std::vector<std::string>&& args);
Tom Cherry018a4382018-10-17 11:11:23 -0700160 Result<Success> ParseMemcgSoftLimitInBytes(std::vector<std::string>&& args);
161 Result<Success> ParseMemcgSwappiness(std::vector<std::string>&& args);
162 Result<Success> ParseNamespace(std::vector<std::string>&& args);
163 Result<Success> ParseProcessRlimit(std::vector<std::string>&& args);
164 Result<Success> ParseRestartPeriod(std::vector<std::string>&& args);
165 Result<Success> ParseSeclabel(std::vector<std::string>&& args);
166 Result<Success> ParseSetenv(std::vector<std::string>&& args);
167 Result<Success> ParseShutdown(std::vector<std::string>&& args);
168 Result<Success> ParseSigstop(std::vector<std::string>&& args);
169 Result<Success> ParseSocket(std::vector<std::string>&& args);
170 Result<Success> ParseTimeoutPeriod(std::vector<std::string>&& args);
171 Result<Success> ParseFile(std::vector<std::string>&& args);
172 Result<Success> ParseUser(std::vector<std::string>&& args);
173 Result<Success> ParseWritepid(std::vector<std::string>&& args);
Jiyong Park80aa4472018-11-12 12:08:41 +0900174 Result<Success> ParseUpdatable(std::vector<std::string>&& args);
Tom Cherryb7349902015-08-26 11:43:36 -0700175
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700176 template <typename T>
Tom Cherry018a4382018-10-17 11:11:23 -0700177 Result<Success> AddDescriptor(std::vector<std::string>&& args);
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700178
Tom Cherry59383792017-07-26 16:09:09 -0700179 static unsigned long next_start_order_;
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700180 static bool is_exec_service_running_;
Tom Cherry59383792017-07-26 16:09:09 -0700181
Tom Cherrybac32992015-07-31 12:45:25 -0700182 std::string name_;
Wei Wang641ff0a2017-03-27 10:59:11 -0700183 std::set<std::string> classnames_;
Viorel Suman70daa672016-03-21 10:08:07 +0200184 std::string console_;
Tom Cherrybac32992015-07-31 12:45:25 -0700185
186 unsigned flags_;
187 pid_t pid_;
James Hawkinse78ea772017-03-24 11:43:02 -0700188 android::base::boot_clock::time_point time_started_; // time of last start
189 android::base::boot_clock::time_point time_crashed_; // first crash within inspection window
Elliott Hughes9605a942016-11-10 17:43:47 -0800190 int crash_count_; // number of times crashed within window
Tom Cherrybac32992015-07-31 12:45:25 -0700191
192 uid_t uid_;
193 gid_t gid_;
194 std::vector<gid_t> supp_gids_;
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -0400195 CapSet capabilities_;
Jorge Lucangeli Obes1b3fa3d2016-04-21 15:35:09 -0700196 unsigned namespace_flags_;
Tom Cherryaead51b2018-04-20 16:18:12 -0700197 // Pair of namespace type, path to namespace.
198 std::vector<std::pair<int, std::string>> namespaces_to_enter_;
Tom Cherrybac32992015-07-31 12:45:25 -0700199
200 std::string seclabel_;
201
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700202 std::vector<std::unique_ptr<DescriptorInfo>> descriptors_;
Tom Cherry6de21f12017-08-22 15:41:03 -0700203 std::vector<std::pair<std::string, std::string>> environment_vars_;
Tom Cherrybac32992015-07-31 12:45:25 -0700204
205 Action onrestart_; // Commands to execute on restart.
206
207 std::vector<std::string> writepid_files_;
208
Steven Morelande055d732017-10-05 18:50:22 -0700209 std::set<std::string> interfaces_; // e.g. some.package.foo@1.0::IBaz/instance-name
210
Mark Salyzyn13857252018-05-18 15:25:15 -0700211 // keycodes for triggering this service via /dev/input/input*
Tom Cherrybac32992015-07-31 12:45:25 -0700212 std::vector<int> keycodes_;
Tom Cherrybac32992015-07-31 12:45:25 -0700213
214 IoSchedClass ioprio_class_;
215 int ioprio_pri_;
Vitalii Tomkiv081705c2016-05-18 17:36:30 -0700216 int priority_;
Tom Cherrybac32992015-07-31 12:45:25 -0700217
Marco Nelissen310f6702016-07-22 12:07:06 -0700218 int oom_score_adjust_;
219
Peter Collingbourned7157c22018-10-30 15:49:33 -0700220 int swappiness_ = -1;
221 int soft_limit_in_bytes_ = -1;
222
223 int limit_in_bytes_ = -1;
224 int limit_percent_ = -1;
225 std::string limit_property_;
Robert Benead4852262017-07-16 19:38:11 -0700226
Tom Cherry33838b12017-05-04 11:32:36 -0700227 bool process_cgroup_empty_ = false;
228
Steven Moreland6f5333a2017-11-13 15:31:54 -0800229 bool override_ = false;
230
Tom Cherry59383792017-07-26 16:09:09 -0700231 unsigned long start_order_;
232
Tom Cherry7ac013d2017-08-25 10:39:25 -0700233 std::vector<std::pair<int, rlimit>> rlimits_;
234
Tom Cherry8f380482018-04-17 14:48:44 -0700235 bool sigstop_ = false;
236
Tom Cherry73f535e2018-09-27 16:10:46 -0700237 std::chrono::seconds restart_period_ = 5s;
238 std::optional<std::chrono::seconds> timeout_period_;
239
Jiyong Park80aa4472018-11-12 12:08:41 +0900240 bool updatable_ = false;
241
Tom Cherrybac32992015-07-31 12:45:25 -0700242 std::vector<std::string> args_;
Paul Crowleyc73b2152018-04-13 17:38:57 +0000243
244 std::vector<std::function<void(const siginfo_t& siginfo)>> reap_callbacks_;
Jiyong Park68660412019-01-16 23:00:59 +0900245
246 bool pre_apexd_ = false;
Tom Cherrybac32992015-07-31 12:45:25 -0700247};
248
Tom Cherry911b9b12017-07-27 16:20:58 -0700249class ServiceList {
Wei Wangeeab4912017-06-27 22:08:45 -0700250 public:
Tom Cherry911b9b12017-07-27 16:20:58 -0700251 static ServiceList& GetInstance();
Tom Cherrybac32992015-07-31 12:45:25 -0700252
Tom Cherry7da54852017-05-01 14:16:41 -0700253 // Exposed for testing
Tom Cherry911b9b12017-07-27 16:20:58 -0700254 ServiceList();
Tom Cherry7da54852017-05-01 14:16:41 -0700255
Tom Cherryb7349902015-08-26 11:43:36 -0700256 void AddService(std::unique_ptr<Service> service);
Tom Cherrybac32992015-07-31 12:45:25 -0700257 void RemoveService(const Service& svc);
Tom Cherry911b9b12017-07-27 16:20:58 -0700258
259 template <typename T, typename F = decltype(&Service::name)>
260 Service* FindService(T value, F function = &Service::name) const {
261 auto svc = std::find_if(services_.begin(), services_.end(),
262 [&function, &value](const std::unique_ptr<Service>& s) {
263 return std::invoke(function, s) == value;
264 });
265 if (svc != services_.end()) {
266 return svc->get();
267 }
268 return nullptr;
269 }
270
Steven Moreland6227e342018-05-08 13:46:39 -0700271 Service* FindInterface(const std::string& interface_name) {
272 for (const auto& svc : services_) {
273 if (svc->interfaces().count(interface_name) > 0) {
274 return svc.get();
275 }
276 }
277
278 return nullptr;
279 }
280
Tom Cherrybac32992015-07-31 12:45:25 -0700281 void DumpState() const;
Tom Cherryb7349902015-08-26 11:43:36 -0700282
Tom Cherry911b9b12017-07-27 16:20:58 -0700283 auto begin() const { return services_.begin(); }
284 auto end() const { return services_.end(); }
285 const std::vector<std::unique_ptr<Service>>& services() const { return services_; }
286 const std::vector<Service*> services_in_shutdown_order() const;
287
Jiyong Park80aa4472018-11-12 12:08:41 +0900288 void MarkServicesUpdate();
289 bool IsServicesUpdated() const { return services_update_finished_; }
290 void DelayService(const Service& service);
291
Wei Wangeeab4912017-06-27 22:08:45 -0700292 private:
Tom Cherrybac32992015-07-31 12:45:25 -0700293 std::vector<std::unique_ptr<Service>> services_;
Jiyong Park80aa4472018-11-12 12:08:41 +0900294
295 bool services_update_finished_ = false;
296 std::vector<std::string> delayed_service_names_;
Tom Cherrybac32992015-07-31 12:45:25 -0700297};
298
Tom Cherryb7349902015-08-26 11:43:36 -0700299class ServiceParser : public SectionParser {
Tom Cherry012c5732017-04-18 13:21:54 -0700300 public:
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700301 ServiceParser(ServiceList* service_list, std::vector<Subcontext>* subcontexts)
302 : service_list_(service_list), subcontexts_(subcontexts), service_(nullptr) {}
Tom Cherry89bcc852017-08-02 17:01:36 -0700303 Result<Success> ParseSection(std::vector<std::string>&& args, const std::string& filename,
304 int line) override;
305 Result<Success> ParseLineSection(std::vector<std::string>&& args, int line) override;
Steven Moreland7d0a5c32017-11-10 14:20:47 -0800306 Result<Success> EndSection() override;
Jiyong Park80aa4472018-11-12 12:08:41 +0900307 void EndFile() override { filename_ = ""; }
Tom Cherry012c5732017-04-18 13:21:54 -0700308
309 private:
Tom Cherryb7349902015-08-26 11:43:36 -0700310 bool IsValidName(const std::string& name) const;
311
Tom Cherry911b9b12017-07-27 16:20:58 -0700312 ServiceList* service_list_;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700313 std::vector<Subcontext>* subcontexts_;
Tom Cherryb7349902015-08-26 11:43:36 -0700314 std::unique_ptr<Service> service_;
Jiyong Park80aa4472018-11-12 12:08:41 +0900315 std::string filename_;
Tom Cherryb7349902015-08-26 11:43:36 -0700316};
317
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700318} // namespace init
319} // namespace android
320
Tom Cherrybac32992015-07-31 12:45:25 -0700321#endif