blob: 9cb35b8814b552c9450d0ce3944f114b3199b37e [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 Cherrybac32992015-07-31 12:45:25 -070024#include <memory>
Wei Wang641ff0a2017-03-27 10:59:11 -070025#include <set>
Tom Cherrybac32992015-07-31 12:45:25 -070026#include <string>
27#include <vector>
28
James Hawkinse78ea772017-03-24 11:43:02 -070029#include <android-base/chrono_utils.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070030#include <cutils/iosched_policy.h>
James Hawkinse78ea772017-03-24 11:43:02 -070031
Tom Cherrybac32992015-07-31 12:45:25 -070032#include "action.h"
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -040033#include "capabilities.h"
Mark Salyzyn62767fe2016-10-27 07:45:34 -070034#include "descriptors.h"
Tom Cherryb7349902015-08-26 11:43:36 -070035#include "keyword_map.h"
Tom Cherry67dee622017-07-27 12:54:48 -070036#include "parser.h"
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070037#include "subcontext.h"
Tom Cherrybac32992015-07-31 12:45:25 -070038
Keun-young Park8d01f632017-03-13 11:54:47 -070039#define SVC_DISABLED 0x001 // do not autostart with class
40#define SVC_ONESHOT 0x002 // do not restart on exit
41#define SVC_RUNNING 0x004 // currently active
42#define SVC_RESTARTING 0x008 // waiting to restart
43#define SVC_CONSOLE 0x010 // requires console
44#define SVC_CRITICAL 0x020 // will reboot into recovery if keeps crashing
45#define SVC_RESET 0x040 // Use when stopping a process,
Tom Cherrybac32992015-07-31 12:45:25 -070046 // but not disabling so it can be restarted with its class.
Keun-young Park8d01f632017-03-13 11:54:47 -070047#define SVC_RC_DISABLED 0x080 // Remember if the disabled flag was set in the rc script.
48#define SVC_RESTART 0x100 // Use to safely restart (stop, wait, start) a service.
Tom Cherrybac32992015-07-31 12:45:25 -070049#define SVC_DISABLED_START 0x200 // A start was requested but it was disabled at the time.
Tom Cherryb27004a2017-03-27 16:27:30 -070050#define SVC_EXEC 0x400 // This service was started by either 'exec' or 'exec_start' and stops
51 // init from processing more commands until it completes
Keun-young Park8d01f632017-03-13 11:54:47 -070052
53#define SVC_SHUTDOWN_CRITICAL 0x800 // This service is critical for shutdown and
54 // should not be killed during shutdown
Tom Cherryb27004a2017-03-27 16:27:30 -070055#define SVC_TEMPORARY 0x1000 // This service was started by 'exec' and should be removed from the
56 // service list once it is reaped.
Tom Cherrybac32992015-07-31 12:45:25 -070057
58#define NR_SVC_SUPP_GIDS 12 // twelve supplementary groups
59
Tom Cherry81f5d3e2017-06-22 12:53:17 -070060namespace android {
61namespace init {
Tom Cherrybac32992015-07-31 12:45:25 -070062
Tom Cherrybac32992015-07-31 12:45:25 -070063class Service {
Wei Wang641ff0a2017-03-27 10:59:11 -070064 public:
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070065 Service(const std::string& name, Subcontext* subcontext_for_restart_commands,
66 const std::vector<std::string>& args);
Tom Cherrybac32992015-07-31 12:45:25 -070067
Wei Wang641ff0a2017-03-27 10:59:11 -070068 Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid,
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -040069 const std::vector<gid_t>& supp_gids, const CapSet& capabilities,
70 unsigned namespace_flags, const std::string& seclabel,
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070071 Subcontext* subcontext_for_restart_commands, const std::vector<std::string>& args);
Tom Cherrybac32992015-07-31 12:45:25 -070072
Tom Cherry3b81f2d2017-07-28 14:48:41 -070073 static std::unique_ptr<Service> MakeTemporaryOneshotService(const std::vector<std::string>& args);
74
Keun-young Park8d01f632017-03-13 11:54:47 -070075 bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; }
Tom Cherry89bcc852017-08-02 17:01:36 -070076 Result<Success> ParseLine(const std::vector<std::string>& args);
Tom Cherry76af7e62017-08-22 16:13:59 -070077 Result<Success> ExecStart();
78 Result<Success> Start();
79 Result<Success> StartIfNotDisabled();
80 Result<Success> Enable();
Tom Cherrybac32992015-07-31 12:45:25 -070081 void Reset();
82 void Stop();
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080083 void Terminate();
Tom Cherrybac32992015-07-31 12:45:25 -070084 void Restart();
Paul Crowleyc73b2152018-04-13 17:38:57 +000085 void Reap(const siginfo_t& siginfo);
Tom Cherrybac32992015-07-31 12:45:25 -070086 void DumpState() const;
Keun-young Park8d01f632017-03-13 11:54:47 -070087 void SetShutdownCritical() { flags_ |= SVC_SHUTDOWN_CRITICAL; }
Wei Wang641ff0a2017-03-27 10:59:11 -070088 bool IsShutdownCritical() const { return (flags_ & SVC_SHUTDOWN_CRITICAL) != 0; }
Tom Cherry3b81f2d2017-07-28 14:48:41 -070089 void UnSetExec() {
90 is_exec_service_running_ = false;
91 flags_ &= ~SVC_EXEC;
92 }
Paul Crowleyc73b2152018-04-13 17:38:57 +000093 void AddReapCallback(std::function<void(const siginfo_t& siginfo)> callback) {
94 reap_callbacks_.emplace_back(std::move(callback));
95 }
Tom Cherry3b81f2d2017-07-28 14:48:41 -070096
97 static bool is_exec_service_running() { return is_exec_service_running_; }
Tom Cherrybac32992015-07-31 12:45:25 -070098
99 const std::string& name() const { return name_; }
Wei Wang641ff0a2017-03-27 10:59:11 -0700100 const std::set<std::string>& classnames() const { return classnames_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700101 unsigned flags() const { return flags_; }
102 pid_t pid() const { return pid_; }
Tom Cherryd269e3a2017-07-31 13:23:18 -0700103 android::base::boot_clock::time_point time_started() const { return time_started_; }
Tom Cherry7da54852017-05-01 14:16:41 -0700104 int crash_count() const { return crash_count_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700105 uid_t uid() const { return uid_; }
106 gid_t gid() const { return gid_; }
Tom Cherry7da54852017-05-01 14:16:41 -0700107 unsigned namespace_flags() const { return namespace_flags_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700108 const std::vector<gid_t>& supp_gids() const { return supp_gids_; }
109 const std::string& seclabel() const { return seclabel_; }
110 const std::vector<int>& keycodes() const { return keycodes_; }
111 int keychord_id() const { return keychord_id_; }
112 void set_keychord_id(int keychord_id) { keychord_id_ = keychord_id; }
Tom Cherry7da54852017-05-01 14:16:41 -0700113 IoSchedClass ioprio_class() const { return ioprio_class_; }
114 int ioprio_pri() const { return ioprio_pri_; }
Steven Morelande055d732017-10-05 18:50:22 -0700115 const std::set<std::string>& interfaces() const { return interfaces_; }
Tom Cherry7da54852017-05-01 14:16:41 -0700116 int priority() const { return priority_; }
117 int oom_score_adjust() const { return oom_score_adjust_; }
Steven Moreland6f5333a2017-11-13 15:31:54 -0800118 bool is_override() const { return override_; }
Tom Cherry33838b12017-05-04 11:32:36 -0700119 bool process_cgroup_empty() const { return process_cgroup_empty_; }
Tom Cherry59383792017-07-26 16:09:09 -0700120 unsigned long start_order() const { return start_order_; }
Tom Cherry8f380482018-04-17 14:48:44 -0700121 void set_sigstop(bool value) { sigstop_ = value; }
Tom Cherrybac32992015-07-31 12:45:25 -0700122 const std::vector<std::string>& args() const { return args_; }
123
Wei Wang641ff0a2017-03-27 10:59:11 -0700124 private:
Tom Cherry89bcc852017-08-02 17:01:36 -0700125 using OptionParser = Result<Success> (Service::*)(const std::vector<std::string>& args);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400126 class OptionParserMap;
Tom Cherryb7349902015-08-26 11:43:36 -0700127
Tom Cherrybac32992015-07-31 12:45:25 -0700128 void NotifyStateChange(const std::string& new_state) const;
129 void StopOrReset(int how);
130 void ZapStdio() const;
131 void OpenConsole() const;
Elliott Hughesad8e94e2016-06-15 14:49:57 -0700132 void KillProcessGroup(int signal);
Jorge Lucangeli Obes344d01f2016-07-08 13:32:26 -0400133 void SetProcessAttributes();
Tom Cherrybac32992015-07-31 12:45:25 -0700134
Tom Cherry89bcc852017-08-02 17:01:36 -0700135 Result<Success> ParseCapabilities(const std::vector<std::string>& args);
136 Result<Success> ParseClass(const std::vector<std::string>& args);
137 Result<Success> ParseConsole(const std::vector<std::string>& args);
138 Result<Success> ParseCritical(const std::vector<std::string>& args);
139 Result<Success> ParseDisabled(const std::vector<std::string>& args);
140 Result<Success> ParseGroup(const std::vector<std::string>& args);
141 Result<Success> ParsePriority(const std::vector<std::string>& args);
Steven Morelande055d732017-10-05 18:50:22 -0700142 Result<Success> ParseInterface(const std::vector<std::string>& args);
Tom Cherry89bcc852017-08-02 17:01:36 -0700143 Result<Success> ParseIoprio(const std::vector<std::string>& args);
144 Result<Success> ParseKeycodes(const std::vector<std::string>& args);
145 Result<Success> ParseOneshot(const std::vector<std::string>& args);
146 Result<Success> ParseOnrestart(const std::vector<std::string>& args);
147 Result<Success> ParseOomScoreAdjust(const std::vector<std::string>& args);
Steven Moreland6f5333a2017-11-13 15:31:54 -0800148 Result<Success> ParseOverride(const std::vector<std::string>& args);
Tom Cherry89bcc852017-08-02 17:01:36 -0700149 Result<Success> ParseMemcgLimitInBytes(const std::vector<std::string>& args);
150 Result<Success> ParseMemcgSoftLimitInBytes(const std::vector<std::string>& args);
151 Result<Success> ParseMemcgSwappiness(const std::vector<std::string>& args);
152 Result<Success> ParseNamespace(const std::vector<std::string>& args);
Tom Cherry7ac013d2017-08-25 10:39:25 -0700153 Result<Success> ParseProcessRlimit(const std::vector<std::string>& args);
Tom Cherry89bcc852017-08-02 17:01:36 -0700154 Result<Success> ParseSeclabel(const std::vector<std::string>& args);
155 Result<Success> ParseSetenv(const std::vector<std::string>& args);
156 Result<Success> ParseShutdown(const std::vector<std::string>& args);
Tom Cherry8f380482018-04-17 14:48:44 -0700157 Result<Success> ParseSigstop(const std::vector<std::string>& args);
Tom Cherry89bcc852017-08-02 17:01:36 -0700158 Result<Success> ParseSocket(const std::vector<std::string>& args);
159 Result<Success> ParseFile(const std::vector<std::string>& args);
160 Result<Success> ParseUser(const std::vector<std::string>& args);
161 Result<Success> ParseWritepid(const std::vector<std::string>& args);
Tom Cherryb7349902015-08-26 11:43:36 -0700162
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700163 template <typename T>
Tom Cherry89bcc852017-08-02 17:01:36 -0700164 Result<Success> AddDescriptor(const std::vector<std::string>& args);
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700165
Tom Cherry59383792017-07-26 16:09:09 -0700166 static unsigned long next_start_order_;
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700167 static bool is_exec_service_running_;
Tom Cherry59383792017-07-26 16:09:09 -0700168
Tom Cherrybac32992015-07-31 12:45:25 -0700169 std::string name_;
Wei Wang641ff0a2017-03-27 10:59:11 -0700170 std::set<std::string> classnames_;
Viorel Suman70daa672016-03-21 10:08:07 +0200171 std::string console_;
Tom Cherrybac32992015-07-31 12:45:25 -0700172
173 unsigned flags_;
174 pid_t pid_;
James Hawkinse78ea772017-03-24 11:43:02 -0700175 android::base::boot_clock::time_point time_started_; // time of last start
176 android::base::boot_clock::time_point time_crashed_; // first crash within inspection window
Elliott Hughes9605a942016-11-10 17:43:47 -0800177 int crash_count_; // number of times crashed within window
Tom Cherrybac32992015-07-31 12:45:25 -0700178
179 uid_t uid_;
180 gid_t gid_;
181 std::vector<gid_t> supp_gids_;
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -0400182 CapSet capabilities_;
Jorge Lucangeli Obes1b3fa3d2016-04-21 15:35:09 -0700183 unsigned namespace_flags_;
Tom Cherrybac32992015-07-31 12:45:25 -0700184
185 std::string seclabel_;
186
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700187 std::vector<std::unique_ptr<DescriptorInfo>> descriptors_;
Tom Cherry6de21f12017-08-22 15:41:03 -0700188 std::vector<std::pair<std::string, std::string>> environment_vars_;
Tom Cherrybac32992015-07-31 12:45:25 -0700189
190 Action onrestart_; // Commands to execute on restart.
191
192 std::vector<std::string> writepid_files_;
193
Steven Morelande055d732017-10-05 18:50:22 -0700194 std::set<std::string> interfaces_; // e.g. some.package.foo@1.0::IBaz/instance-name
195
Tom Cherrybac32992015-07-31 12:45:25 -0700196 // keycodes for triggering this service via /dev/keychord
197 std::vector<int> keycodes_;
198 int keychord_id_;
199
200 IoSchedClass ioprio_class_;
201 int ioprio_pri_;
Vitalii Tomkiv081705c2016-05-18 17:36:30 -0700202 int priority_;
Tom Cherrybac32992015-07-31 12:45:25 -0700203
Marco Nelissen310f6702016-07-22 12:07:06 -0700204 int oom_score_adjust_;
205
Robert Benead4852262017-07-16 19:38:11 -0700206 int swappiness_;
207 int soft_limit_in_bytes_;
208 int limit_in_bytes_;
209
Tom Cherry33838b12017-05-04 11:32:36 -0700210 bool process_cgroup_empty_ = false;
211
Steven Moreland6f5333a2017-11-13 15:31:54 -0800212 bool override_ = false;
213
Tom Cherry59383792017-07-26 16:09:09 -0700214 unsigned long start_order_;
215
Tom Cherry7ac013d2017-08-25 10:39:25 -0700216 std::vector<std::pair<int, rlimit>> rlimits_;
217
Tom Cherry8f380482018-04-17 14:48:44 -0700218 bool sigstop_ = false;
219
Tom Cherrybac32992015-07-31 12:45:25 -0700220 std::vector<std::string> args_;
Paul Crowleyc73b2152018-04-13 17:38:57 +0000221
222 std::vector<std::function<void(const siginfo_t& siginfo)>> reap_callbacks_;
Tom Cherrybac32992015-07-31 12:45:25 -0700223};
224
Tom Cherry911b9b12017-07-27 16:20:58 -0700225class ServiceList {
Wei Wangeeab4912017-06-27 22:08:45 -0700226 public:
Tom Cherry911b9b12017-07-27 16:20:58 -0700227 static ServiceList& GetInstance();
Tom Cherrybac32992015-07-31 12:45:25 -0700228
Tom Cherry7da54852017-05-01 14:16:41 -0700229 // Exposed for testing
Tom Cherry911b9b12017-07-27 16:20:58 -0700230 ServiceList();
Tom Cherry7da54852017-05-01 14:16:41 -0700231
Tom Cherryb7349902015-08-26 11:43:36 -0700232 void AddService(std::unique_ptr<Service> service);
Tom Cherrybac32992015-07-31 12:45:25 -0700233 void RemoveService(const Service& svc);
Tom Cherry911b9b12017-07-27 16:20:58 -0700234
235 template <typename T, typename F = decltype(&Service::name)>
236 Service* FindService(T value, F function = &Service::name) const {
237 auto svc = std::find_if(services_.begin(), services_.end(),
238 [&function, &value](const std::unique_ptr<Service>& s) {
239 return std::invoke(function, s) == value;
240 });
241 if (svc != services_.end()) {
242 return svc->get();
243 }
244 return nullptr;
245 }
246
Steven Moreland6227e342018-05-08 13:46:39 -0700247 Service* FindInterface(const std::string& interface_name) {
248 for (const auto& svc : services_) {
249 if (svc->interfaces().count(interface_name) > 0) {
250 return svc.get();
251 }
252 }
253
254 return nullptr;
255 }
256
Tom Cherrybac32992015-07-31 12:45:25 -0700257 void DumpState() const;
Tom Cherryb7349902015-08-26 11:43:36 -0700258
Tom Cherry911b9b12017-07-27 16:20:58 -0700259 auto begin() const { return services_.begin(); }
260 auto end() const { return services_.end(); }
261 const std::vector<std::unique_ptr<Service>>& services() const { return services_; }
262 const std::vector<Service*> services_in_shutdown_order() const;
263
Wei Wangeeab4912017-06-27 22:08:45 -0700264 private:
Tom Cherrybac32992015-07-31 12:45:25 -0700265 std::vector<std::unique_ptr<Service>> services_;
266};
267
Tom Cherryb7349902015-08-26 11:43:36 -0700268class ServiceParser : public SectionParser {
Tom Cherry012c5732017-04-18 13:21:54 -0700269 public:
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700270 ServiceParser(ServiceList* service_list, std::vector<Subcontext>* subcontexts)
271 : service_list_(service_list), subcontexts_(subcontexts), service_(nullptr) {}
Tom Cherry89bcc852017-08-02 17:01:36 -0700272 Result<Success> ParseSection(std::vector<std::string>&& args, const std::string& filename,
273 int line) override;
274 Result<Success> ParseLineSection(std::vector<std::string>&& args, int line) override;
Steven Moreland7d0a5c32017-11-10 14:20:47 -0800275 Result<Success> EndSection() override;
Tom Cherry012c5732017-04-18 13:21:54 -0700276
277 private:
Tom Cherryb7349902015-08-26 11:43:36 -0700278 bool IsValidName(const std::string& name) const;
279
Tom Cherry911b9b12017-07-27 16:20:58 -0700280 ServiceList* service_list_;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700281 std::vector<Subcontext>* subcontexts_;
Tom Cherryb7349902015-08-26 11:43:36 -0700282 std::unique_ptr<Service> service_;
283};
284
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700285} // namespace init
286} // namespace android
287
Tom Cherrybac32992015-07-31 12:45:25 -0700288#endif