blob: b9c270aa6b759d2088f555420c9ad3aaf115ad87 [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 "init_parser.h"
34#include "keyword_map.h"
Elliott Hughes9605a942016-11-10 17:43:47 -080035#include "util.h"
Tom Cherrybac32992015-07-31 12:45:25 -070036
Keun-young Park8d01f632017-03-13 11:54:47 -070037#define SVC_DISABLED 0x001 // do not autostart with class
38#define SVC_ONESHOT 0x002 // do not restart on exit
39#define SVC_RUNNING 0x004 // currently active
40#define SVC_RESTARTING 0x008 // waiting to restart
41#define SVC_CONSOLE 0x010 // requires console
42#define SVC_CRITICAL 0x020 // will reboot into recovery if keeps crashing
43#define SVC_RESET 0x040 // Use when stopping a process,
Tom Cherrybac32992015-07-31 12:45:25 -070044 // but not disabling so it can be restarted with its class.
Keun-young Park8d01f632017-03-13 11:54:47 -070045#define SVC_RC_DISABLED 0x080 // Remember if the disabled flag was set in the rc script.
46#define SVC_RESTART 0x100 // Use to safely restart (stop, wait, start) a service.
Tom Cherrybac32992015-07-31 12:45:25 -070047#define SVC_DISABLED_START 0x200 // A start was requested but it was disabled at the time.
Tom Cherryb27004a2017-03-27 16:27:30 -070048#define SVC_EXEC 0x400 // This service was started by either 'exec' or 'exec_start' and stops
49 // init from processing more commands until it completes
Keun-young Park8d01f632017-03-13 11:54:47 -070050
51#define SVC_SHUTDOWN_CRITICAL 0x800 // This service is critical for shutdown and
52 // should not be killed during shutdown
Tom Cherryb27004a2017-03-27 16:27:30 -070053#define SVC_TEMPORARY 0x1000 // This service was started by 'exec' and should be removed from the
54 // service list once it is reaped.
Tom Cherrybac32992015-07-31 12:45:25 -070055
56#define NR_SVC_SUPP_GIDS 12 // twelve supplementary groups
57
58class Action;
59class ServiceManager;
60
Tom Cherrybac32992015-07-31 12:45:25 -070061struct ServiceEnvironmentInfo {
62 ServiceEnvironmentInfo();
63 ServiceEnvironmentInfo(const std::string& name, const std::string& value);
64 std::string name;
65 std::string value;
66};
67
68class Service {
Wei Wang641ff0a2017-03-27 10:59:11 -070069 public:
70 Service(const std::string& name, const std::vector<std::string>& args);
Tom Cherrybac32992015-07-31 12:45:25 -070071
Wei Wang641ff0a2017-03-27 10:59:11 -070072 Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid,
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -040073 const std::vector<gid_t>& supp_gids, const CapSet& capabilities,
74 unsigned namespace_flags, const std::string& seclabel,
75 const std::vector<std::string>& args);
Tom Cherrybac32992015-07-31 12:45:25 -070076
Keun-young Park8d01f632017-03-13 11:54:47 -070077 bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; }
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -040078 bool ParseLine(const std::vector<std::string>& args, std::string* err);
Tom Cherryb27004a2017-03-27 16:27:30 -070079 bool ExecStart(std::unique_ptr<Timer>* exec_waiter);
Tom Cherrybac32992015-07-31 12:45:25 -070080 bool Start();
81 bool StartIfNotDisabled();
82 bool Enable();
83 void Reset();
84 void Stop();
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080085 void Terminate();
Tom Cherrybac32992015-07-31 12:45:25 -070086 void Restart();
Elliott Hughes9605a942016-11-10 17:43:47 -080087 void RestartIfNeeded(time_t* process_needs_restart_at);
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 Cherrybac32992015-07-31 12:45:25 -070092
93 const std::string& name() const { return name_; }
Wei Wang641ff0a2017-03-27 10:59:11 -070094 const std::set<std::string>& classnames() const { return classnames_; }
Tom Cherrybac32992015-07-31 12:45:25 -070095 unsigned flags() const { return flags_; }
96 pid_t pid() const { return pid_; }
Tom Cherry7da54852017-05-01 14:16:41 -070097 int crash_count() const { return crash_count_; }
Tom Cherrybac32992015-07-31 12:45:25 -070098 uid_t uid() const { return uid_; }
99 gid_t gid() const { return gid_; }
Tom Cherry7da54852017-05-01 14:16:41 -0700100 unsigned namespace_flags() const { return namespace_flags_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700101 const std::vector<gid_t>& supp_gids() const { return supp_gids_; }
102 const std::string& seclabel() const { return seclabel_; }
103 const std::vector<int>& keycodes() const { return keycodes_; }
104 int keychord_id() const { return keychord_id_; }
105 void set_keychord_id(int keychord_id) { keychord_id_ = keychord_id; }
Tom Cherry7da54852017-05-01 14:16:41 -0700106 IoSchedClass ioprio_class() const { return ioprio_class_; }
107 int ioprio_pri() const { return ioprio_pri_; }
108 int priority() const { return priority_; }
109 int oom_score_adjust() const { return oom_score_adjust_; }
Tom Cherry33838b12017-05-04 11:32:36 -0700110 bool process_cgroup_empty() const { return process_cgroup_empty_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700111 const std::vector<std::string>& args() const { return args_; }
112
Wei Wang641ff0a2017-03-27 10:59:11 -0700113 private:
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400114 using OptionParser = bool (Service::*) (const std::vector<std::string>& args,
115 std::string* err);
116 class OptionParserMap;
Tom Cherryb7349902015-08-26 11:43:36 -0700117
Tom Cherrybac32992015-07-31 12:45:25 -0700118 void NotifyStateChange(const std::string& new_state) const;
119 void StopOrReset(int how);
120 void ZapStdio() const;
121 void OpenConsole() const;
Elliott Hughesad8e94e2016-06-15 14:49:57 -0700122 void KillProcessGroup(int signal);
Jorge Lucangeli Obes344d01f2016-07-08 13:32:26 -0400123 void SetProcessAttributes();
Tom Cherrybac32992015-07-31 12:45:25 -0700124
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -0400125 bool ParseCapabilities(const std::vector<std::string>& args, std::string *err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400126 bool ParseClass(const std::vector<std::string>& args, std::string* err);
127 bool ParseConsole(const std::vector<std::string>& args, std::string* err);
128 bool ParseCritical(const std::vector<std::string>& args, std::string* err);
129 bool ParseDisabled(const std::vector<std::string>& args, std::string* err);
130 bool ParseGroup(const std::vector<std::string>& args, std::string* err);
131 bool ParsePriority(const std::vector<std::string>& args, std::string* err);
132 bool ParseIoprio(const std::vector<std::string>& args, std::string* err);
133 bool ParseKeycodes(const std::vector<std::string>& args, std::string* err);
134 bool ParseOneshot(const std::vector<std::string>& args, std::string* err);
135 bool ParseOnrestart(const std::vector<std::string>& args, std::string* err);
Marco Nelissen310f6702016-07-22 12:07:06 -0700136 bool ParseOomScoreAdjust(const std::vector<std::string>& args, std::string* err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400137 bool ParseNamespace(const std::vector<std::string>& args, std::string* err);
138 bool ParseSeclabel(const std::vector<std::string>& args, std::string* err);
139 bool ParseSetenv(const std::vector<std::string>& args, std::string* err);
140 bool ParseSocket(const std::vector<std::string>& args, std::string* err);
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700141 bool ParseFile(const std::vector<std::string>& args, std::string* err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400142 bool ParseUser(const std::vector<std::string>& args, std::string* err);
143 bool ParseWritepid(const std::vector<std::string>& args, std::string* err);
Tom Cherryb7349902015-08-26 11:43:36 -0700144
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700145 template <typename T>
146 bool AddDescriptor(const std::vector<std::string>& args, std::string* err);
147
Tom Cherrybac32992015-07-31 12:45:25 -0700148 std::string name_;
Wei Wang641ff0a2017-03-27 10:59:11 -0700149 std::set<std::string> classnames_;
Viorel Suman70daa672016-03-21 10:08:07 +0200150 std::string console_;
Tom Cherrybac32992015-07-31 12:45:25 -0700151
152 unsigned flags_;
153 pid_t pid_;
James Hawkinse78ea772017-03-24 11:43:02 -0700154 android::base::boot_clock::time_point time_started_; // time of last start
155 android::base::boot_clock::time_point time_crashed_; // first crash within inspection window
Elliott Hughes9605a942016-11-10 17:43:47 -0800156 int crash_count_; // number of times crashed within window
Tom Cherrybac32992015-07-31 12:45:25 -0700157
158 uid_t uid_;
159 gid_t gid_;
160 std::vector<gid_t> supp_gids_;
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -0400161 CapSet capabilities_;
Jorge Lucangeli Obes1b3fa3d2016-04-21 15:35:09 -0700162 unsigned namespace_flags_;
Tom Cherrybac32992015-07-31 12:45:25 -0700163
164 std::string seclabel_;
165
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700166 std::vector<std::unique_ptr<DescriptorInfo>> descriptors_;
Tom Cherrybac32992015-07-31 12:45:25 -0700167 std::vector<ServiceEnvironmentInfo> envvars_;
168
169 Action onrestart_; // Commands to execute on restart.
170
171 std::vector<std::string> writepid_files_;
172
173 // keycodes for triggering this service via /dev/keychord
174 std::vector<int> keycodes_;
175 int keychord_id_;
176
177 IoSchedClass ioprio_class_;
178 int ioprio_pri_;
Vitalii Tomkiv081705c2016-05-18 17:36:30 -0700179 int priority_;
Tom Cherrybac32992015-07-31 12:45:25 -0700180
Marco Nelissen310f6702016-07-22 12:07:06 -0700181 int oom_score_adjust_;
182
Tom Cherry33838b12017-05-04 11:32:36 -0700183 bool process_cgroup_empty_ = false;
184
Tom Cherrybac32992015-07-31 12:45:25 -0700185 std::vector<std::string> args_;
186};
187
188class ServiceManager {
189public:
190 static ServiceManager& GetInstance();
191
Tom Cherry7da54852017-05-01 14:16:41 -0700192 // Exposed for testing
193 ServiceManager();
194
Tom Cherryb7349902015-08-26 11:43:36 -0700195 void AddService(std::unique_ptr<Service> service);
Tom Cherrybac32992015-07-31 12:45:25 -0700196 Service* MakeExecOneshotService(const std::vector<std::string>& args);
Tom Cherryb27004a2017-03-27 16:27:30 -0700197 bool Exec(const std::vector<std::string>& args);
198 bool ExecStart(const std::string& name);
199 bool IsWaitingForExec() const;
Tom Cherrybac32992015-07-31 12:45:25 -0700200 Service* FindServiceByName(const std::string& name) const;
201 Service* FindServiceByPid(pid_t pid) const;
202 Service* FindServiceByKeychord(int keychord_id) const;
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700203 void ForEachService(const std::function<void(Service*)>& callback) const;
Tom Cherrybac32992015-07-31 12:45:25 -0700204 void ForEachServiceInClass(const std::string& classname,
205 void (*func)(Service* svc)) const;
206 void ForEachServiceWithFlags(unsigned matchflags,
207 void (*func)(Service* svc)) const;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800208 void ReapAnyOutstandingChildren();
Tom Cherrybac32992015-07-31 12:45:25 -0700209 void RemoveService(const Service& svc);
210 void DumpState() const;
Tom Cherryb7349902015-08-26 11:43:36 -0700211
Tom Cherrybac32992015-07-31 12:45:25 -0700212private:
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800213 // Cleans up a child process that exited.
214 // Returns true iff a children was cleaned up.
215 bool ReapOneProcess();
216
Tom Cherrybac32992015-07-31 12:45:25 -0700217 static int exec_count_; // Every service needs a unique name.
Tom Cherryb27004a2017-03-27 16:27:30 -0700218 std::unique_ptr<Timer> exec_waiter_;
219
Tom Cherrybac32992015-07-31 12:45:25 -0700220 std::vector<std::unique_ptr<Service>> services_;
221};
222
Tom Cherryb7349902015-08-26 11:43:36 -0700223class ServiceParser : public SectionParser {
Tom Cherry012c5732017-04-18 13:21:54 -0700224 public:
Tom Cherry30a6f272017-04-19 15:31:58 -0700225 ServiceParser(ServiceManager* service_manager)
226 : service_manager_(service_manager), service_(nullptr) {}
227 bool ParseSection(std::vector<std::string>&& args, const std::string& filename, int line,
Tom Cherryb7349902015-08-26 11:43:36 -0700228 std::string* err) override;
Tom Cherry30a6f272017-04-19 15:31:58 -0700229 bool ParseLineSection(std::vector<std::string>&& args, int line, std::string* err) override;
Tom Cherryb7349902015-08-26 11:43:36 -0700230 void EndSection() override;
Tom Cherry012c5732017-04-18 13:21:54 -0700231
232 private:
Tom Cherryb7349902015-08-26 11:43:36 -0700233 bool IsValidName(const std::string& name) const;
234
Tom Cherry30a6f272017-04-19 15:31:58 -0700235 ServiceManager* service_manager_;
Tom Cherryb7349902015-08-26 11:43:36 -0700236 std::unique_ptr<Service> service_;
237};
238
Tom Cherrybac32992015-07-31 12:45:25 -0700239#endif