blob: 6c143cb6166f2b5f8781d62e45faac2a232f9a5f [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
20#include <sys/types.h>
21
Tom Cherrybac32992015-07-31 12:45:25 -070022#include <memory>
Wei Wang641ff0a2017-03-27 10:59:11 -070023#include <set>
Tom Cherrybac32992015-07-31 12:45:25 -070024#include <string>
25#include <vector>
26
James Hawkinse78ea772017-03-24 11:43:02 -070027#include <android-base/chrono_utils.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070028#include <cutils/iosched_policy.h>
James Hawkinse78ea772017-03-24 11:43:02 -070029
Tom Cherrybac32992015-07-31 12:45:25 -070030#include "action.h"
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -040031#include "capabilities.h"
Mark Salyzyn62767fe2016-10-27 07:45:34 -070032#include "descriptors.h"
Tom Cherryb7349902015-08-26 11:43:36 -070033#include "keyword_map.h"
Tom Cherry67dee622017-07-27 12:54:48 -070034#include "parser.h"
Tom Cherrybac32992015-07-31 12:45:25 -070035
Keun-young Park8d01f632017-03-13 11:54:47 -070036#define SVC_DISABLED 0x001 // do not autostart with class
37#define SVC_ONESHOT 0x002 // do not restart on exit
38#define SVC_RUNNING 0x004 // currently active
39#define SVC_RESTARTING 0x008 // waiting to restart
40#define SVC_CONSOLE 0x010 // requires console
41#define SVC_CRITICAL 0x020 // will reboot into recovery if keeps crashing
42#define SVC_RESET 0x040 // Use when stopping a process,
Tom Cherrybac32992015-07-31 12:45:25 -070043 // but not disabling so it can be restarted with its class.
Keun-young Park8d01f632017-03-13 11:54:47 -070044#define SVC_RC_DISABLED 0x080 // Remember if the disabled flag was set in the rc script.
45#define SVC_RESTART 0x100 // Use to safely restart (stop, wait, start) a service.
Tom Cherrybac32992015-07-31 12:45:25 -070046#define SVC_DISABLED_START 0x200 // A start was requested but it was disabled at the time.
Tom Cherryb27004a2017-03-27 16:27:30 -070047#define SVC_EXEC 0x400 // This service was started by either 'exec' or 'exec_start' and stops
48 // init from processing more commands until it completes
Keun-young Park8d01f632017-03-13 11:54:47 -070049
50#define SVC_SHUTDOWN_CRITICAL 0x800 // This service is critical for shutdown and
51 // should not be killed during shutdown
Tom Cherryb27004a2017-03-27 16:27:30 -070052#define SVC_TEMPORARY 0x1000 // This service was started by 'exec' and should be removed from the
53 // service list once it is reaped.
Tom Cherrybac32992015-07-31 12:45:25 -070054
55#define NR_SVC_SUPP_GIDS 12 // twelve supplementary groups
56
Tom Cherry81f5d3e2017-06-22 12:53:17 -070057namespace android {
58namespace init {
Tom Cherrybac32992015-07-31 12:45:25 -070059
Tom Cherrybac32992015-07-31 12:45:25 -070060struct ServiceEnvironmentInfo {
61 ServiceEnvironmentInfo();
62 ServiceEnvironmentInfo(const std::string& name, const std::string& value);
63 std::string name;
64 std::string value;
65};
66
67class Service {
Wei Wang641ff0a2017-03-27 10:59:11 -070068 public:
69 Service(const std::string& name, 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,
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -040072 const std::vector<gid_t>& supp_gids, const CapSet& capabilities,
73 unsigned namespace_flags, const std::string& seclabel,
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; }
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -040079 bool ParseLine(const std::vector<std::string>& args, std::string* err);
Tom Cherry3b81f2d2017-07-28 14:48:41 -070080 bool ExecStart();
Tom Cherrybac32992015-07-31 12:45:25 -070081 bool Start();
82 bool StartIfNotDisabled();
83 bool Enable();
84 void Reset();
85 void Stop();
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080086 void Terminate();
Tom Cherrybac32992015-07-31 12:45:25 -070087 void Restart();
Tom Cherryb27004a2017-03-27 16:27:30 -070088 void Reap();
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 }
96
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_; }
115 int priority() const { return priority_; }
116 int oom_score_adjust() const { return oom_score_adjust_; }
Tom Cherry33838b12017-05-04 11:32:36 -0700117 bool process_cgroup_empty() const { return process_cgroup_empty_; }
Tom Cherry59383792017-07-26 16:09:09 -0700118 unsigned long start_order() const { return start_order_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700119 const std::vector<std::string>& args() const { return args_; }
120
Wei Wang641ff0a2017-03-27 10:59:11 -0700121 private:
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400122 using OptionParser = bool (Service::*) (const std::vector<std::string>& args,
123 std::string* err);
124 class OptionParserMap;
Tom Cherryb7349902015-08-26 11:43:36 -0700125
Tom Cherrybac32992015-07-31 12:45:25 -0700126 void NotifyStateChange(const std::string& new_state) const;
127 void StopOrReset(int how);
128 void ZapStdio() const;
129 void OpenConsole() const;
Elliott Hughesad8e94e2016-06-15 14:49:57 -0700130 void KillProcessGroup(int signal);
Jorge Lucangeli Obes344d01f2016-07-08 13:32:26 -0400131 void SetProcessAttributes();
Tom Cherrybac32992015-07-31 12:45:25 -0700132
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -0400133 bool ParseCapabilities(const std::vector<std::string>& args, std::string *err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400134 bool ParseClass(const std::vector<std::string>& args, std::string* err);
135 bool ParseConsole(const std::vector<std::string>& args, std::string* err);
136 bool ParseCritical(const std::vector<std::string>& args, std::string* err);
137 bool ParseDisabled(const std::vector<std::string>& args, std::string* err);
138 bool ParseGroup(const std::vector<std::string>& args, std::string* err);
139 bool ParsePriority(const std::vector<std::string>& args, std::string* err);
140 bool ParseIoprio(const std::vector<std::string>& args, std::string* err);
141 bool ParseKeycodes(const std::vector<std::string>& args, std::string* err);
142 bool ParseOneshot(const std::vector<std::string>& args, std::string* err);
143 bool ParseOnrestart(const std::vector<std::string>& args, std::string* err);
Marco Nelissen310f6702016-07-22 12:07:06 -0700144 bool ParseOomScoreAdjust(const std::vector<std::string>& args, std::string* err);
Robert Benead4852262017-07-16 19:38:11 -0700145 bool ParseMemcgLimitInBytes(const std::vector<std::string>& args, std::string* err);
146 bool ParseMemcgSoftLimitInBytes(const std::vector<std::string>& args, std::string* err);
147 bool ParseMemcgSwappiness(const std::vector<std::string>& args, std::string* err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400148 bool ParseNamespace(const std::vector<std::string>& args, std::string* err);
149 bool ParseSeclabel(const std::vector<std::string>& args, std::string* err);
150 bool ParseSetenv(const std::vector<std::string>& args, std::string* err);
Keun-young Parkcccb34f2017-07-05 11:38:44 -0700151 bool ParseShutdown(const std::vector<std::string>& args, std::string* err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400152 bool ParseSocket(const std::vector<std::string>& args, std::string* err);
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700153 bool ParseFile(const std::vector<std::string>& args, std::string* err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400154 bool ParseUser(const std::vector<std::string>& args, std::string* err);
155 bool ParseWritepid(const std::vector<std::string>& args, std::string* err);
Tom Cherryb7349902015-08-26 11:43:36 -0700156
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700157 template <typename T>
158 bool AddDescriptor(const std::vector<std::string>& args, std::string* err);
159
Tom Cherry59383792017-07-26 16:09:09 -0700160 static unsigned long next_start_order_;
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700161 static bool is_exec_service_running_;
Tom Cherry59383792017-07-26 16:09:09 -0700162
Tom Cherrybac32992015-07-31 12:45:25 -0700163 std::string name_;
Wei Wang641ff0a2017-03-27 10:59:11 -0700164 std::set<std::string> classnames_;
Viorel Suman70daa672016-03-21 10:08:07 +0200165 std::string console_;
Tom Cherrybac32992015-07-31 12:45:25 -0700166
167 unsigned flags_;
168 pid_t pid_;
James Hawkinse78ea772017-03-24 11:43:02 -0700169 android::base::boot_clock::time_point time_started_; // time of last start
170 android::base::boot_clock::time_point time_crashed_; // first crash within inspection window
Elliott Hughes9605a942016-11-10 17:43:47 -0800171 int crash_count_; // number of times crashed within window
Tom Cherrybac32992015-07-31 12:45:25 -0700172
173 uid_t uid_;
174 gid_t gid_;
175 std::vector<gid_t> supp_gids_;
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -0400176 CapSet capabilities_;
Jorge Lucangeli Obes1b3fa3d2016-04-21 15:35:09 -0700177 unsigned namespace_flags_;
Tom Cherrybac32992015-07-31 12:45:25 -0700178
179 std::string seclabel_;
180
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700181 std::vector<std::unique_ptr<DescriptorInfo>> descriptors_;
Tom Cherrybac32992015-07-31 12:45:25 -0700182 std::vector<ServiceEnvironmentInfo> envvars_;
183
184 Action onrestart_; // Commands to execute on restart.
185
186 std::vector<std::string> writepid_files_;
187
188 // keycodes for triggering this service via /dev/keychord
189 std::vector<int> keycodes_;
190 int keychord_id_;
191
192 IoSchedClass ioprio_class_;
193 int ioprio_pri_;
Vitalii Tomkiv081705c2016-05-18 17:36:30 -0700194 int priority_;
Tom Cherrybac32992015-07-31 12:45:25 -0700195
Marco Nelissen310f6702016-07-22 12:07:06 -0700196 int oom_score_adjust_;
197
Robert Benead4852262017-07-16 19:38:11 -0700198 int swappiness_;
199 int soft_limit_in_bytes_;
200 int limit_in_bytes_;
201
Tom Cherry33838b12017-05-04 11:32:36 -0700202 bool process_cgroup_empty_ = false;
203
Tom Cherry59383792017-07-26 16:09:09 -0700204 unsigned long start_order_;
205
Tom Cherrybac32992015-07-31 12:45:25 -0700206 std::vector<std::string> args_;
207};
208
Tom Cherry911b9b12017-07-27 16:20:58 -0700209class ServiceList {
Wei Wangeeab4912017-06-27 22:08:45 -0700210 public:
Tom Cherry911b9b12017-07-27 16:20:58 -0700211 static ServiceList& GetInstance();
Tom Cherrybac32992015-07-31 12:45:25 -0700212
Tom Cherry7da54852017-05-01 14:16:41 -0700213 // Exposed for testing
Tom Cherry911b9b12017-07-27 16:20:58 -0700214 ServiceList();
Tom Cherry7da54852017-05-01 14:16:41 -0700215
Tom Cherryb7349902015-08-26 11:43:36 -0700216 void AddService(std::unique_ptr<Service> service);
Tom Cherrybac32992015-07-31 12:45:25 -0700217 void RemoveService(const Service& svc);
Tom Cherry911b9b12017-07-27 16:20:58 -0700218
219 template <typename T, typename F = decltype(&Service::name)>
220 Service* FindService(T value, F function = &Service::name) const {
221 auto svc = std::find_if(services_.begin(), services_.end(),
222 [&function, &value](const std::unique_ptr<Service>& s) {
223 return std::invoke(function, s) == value;
224 });
225 if (svc != services_.end()) {
226 return svc->get();
227 }
228 return nullptr;
229 }
230
Tom Cherrybac32992015-07-31 12:45:25 -0700231 void DumpState() const;
Tom Cherryb7349902015-08-26 11:43:36 -0700232
Tom Cherry911b9b12017-07-27 16:20:58 -0700233 auto begin() const { return services_.begin(); }
234 auto end() const { return services_.end(); }
235 const std::vector<std::unique_ptr<Service>>& services() const { return services_; }
236 const std::vector<Service*> services_in_shutdown_order() const;
237
Wei Wangeeab4912017-06-27 22:08:45 -0700238 private:
Tom Cherrybac32992015-07-31 12:45:25 -0700239 std::vector<std::unique_ptr<Service>> services_;
240};
241
Tom Cherryb7349902015-08-26 11:43:36 -0700242class ServiceParser : public SectionParser {
Tom Cherry012c5732017-04-18 13:21:54 -0700243 public:
Tom Cherry911b9b12017-07-27 16:20:58 -0700244 ServiceParser(ServiceList* service_list) : service_list_(service_list), service_(nullptr) {}
Tom Cherry30a6f272017-04-19 15:31:58 -0700245 bool ParseSection(std::vector<std::string>&& args, const std::string& filename, int line,
Tom Cherryb7349902015-08-26 11:43:36 -0700246 std::string* err) override;
Tom Cherry30a6f272017-04-19 15:31:58 -0700247 bool ParseLineSection(std::vector<std::string>&& args, int line, std::string* err) override;
Tom Cherryb7349902015-08-26 11:43:36 -0700248 void EndSection() override;
Tom Cherry012c5732017-04-18 13:21:54 -0700249
250 private:
Tom Cherryb7349902015-08-26 11:43:36 -0700251 bool IsValidName(const std::string& name) const;
252
Tom Cherry911b9b12017-07-27 16:20:58 -0700253 ServiceList* service_list_;
Tom Cherryb7349902015-08-26 11:43:36 -0700254 std::unique_ptr<Service> service_;
255};
256
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700257} // namespace init
258} // namespace android
259
Tom Cherrybac32992015-07-31 12:45:25 -0700260#endif