blob: 9a9046b9e7fbe7ba270b5a504d402712d3fe630d [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
22#include <cutils/iosched_policy.h>
23
24#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
29#include "action.h"
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -040030#include "capabilities.h"
Mark Salyzyn62767fe2016-10-27 07:45:34 -070031#include "descriptors.h"
Tom Cherryb7349902015-08-26 11:43:36 -070032#include "init_parser.h"
33#include "keyword_map.h"
Elliott Hughes9605a942016-11-10 17:43:47 -080034#include "util.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.
Keun-young Park8d01f632017-03-13 11:54:47 -070047#define SVC_EXEC 0x400 // This synthetic service corresponds to an 'exec'.
48
49#define SVC_SHUTDOWN_CRITICAL 0x800 // This service is critical for shutdown and
50 // should not be killed during shutdown
Tom Cherrybac32992015-07-31 12:45:25 -070051
52#define NR_SVC_SUPP_GIDS 12 // twelve supplementary groups
53
54class Action;
55class ServiceManager;
56
Tom Cherrybac32992015-07-31 12:45:25 -070057struct ServiceEnvironmentInfo {
58 ServiceEnvironmentInfo();
59 ServiceEnvironmentInfo(const std::string& name, const std::string& value);
60 std::string name;
61 std::string value;
62};
63
64class Service {
Wei Wang641ff0a2017-03-27 10:59:11 -070065 public:
66 Service(const std::string& name, 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,
71 const std::vector<std::string>& args);
Tom Cherrybac32992015-07-31 12:45:25 -070072
Keun-young Park8d01f632017-03-13 11:54:47 -070073 bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; }
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -040074 bool ParseLine(const std::vector<std::string>& args, std::string* err);
Tom Cherrybac32992015-07-31 12:45:25 -070075 bool Start();
76 bool StartIfNotDisabled();
77 bool Enable();
78 void Reset();
79 void Stop();
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080080 void Terminate();
Tom Cherrybac32992015-07-31 12:45:25 -070081 void Restart();
Elliott Hughes9605a942016-11-10 17:43:47 -080082 void RestartIfNeeded(time_t* process_needs_restart_at);
Tom Cherrybac32992015-07-31 12:45:25 -070083 bool Reap();
84 void DumpState() const;
Keun-young Park8d01f632017-03-13 11:54:47 -070085 void SetShutdownCritical() { flags_ |= SVC_SHUTDOWN_CRITICAL; }
Wei Wang641ff0a2017-03-27 10:59:11 -070086 bool IsShutdownCritical() const { return (flags_ & SVC_SHUTDOWN_CRITICAL) != 0; }
Tom Cherrybac32992015-07-31 12:45:25 -070087
88 const std::string& name() const { return name_; }
Wei Wang641ff0a2017-03-27 10:59:11 -070089 const std::set<std::string>& classnames() const { return classnames_; }
Tom Cherrybac32992015-07-31 12:45:25 -070090 unsigned flags() const { return flags_; }
91 pid_t pid() const { return pid_; }
92 uid_t uid() const { return uid_; }
93 gid_t gid() const { return gid_; }
Vitalii Tomkiv081705c2016-05-18 17:36:30 -070094 int priority() const { return priority_; }
Tom Cherrybac32992015-07-31 12:45:25 -070095 const std::vector<gid_t>& supp_gids() const { return supp_gids_; }
96 const std::string& seclabel() const { return seclabel_; }
97 const std::vector<int>& keycodes() const { return keycodes_; }
98 int keychord_id() const { return keychord_id_; }
99 void set_keychord_id(int keychord_id) { keychord_id_ = keychord_id; }
100 const std::vector<std::string>& args() const { return args_; }
101
Wei Wang641ff0a2017-03-27 10:59:11 -0700102 private:
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400103 using OptionParser = bool (Service::*) (const std::vector<std::string>& args,
104 std::string* err);
105 class OptionParserMap;
Tom Cherryb7349902015-08-26 11:43:36 -0700106
Tom Cherrybac32992015-07-31 12:45:25 -0700107 void NotifyStateChange(const std::string& new_state) const;
108 void StopOrReset(int how);
109 void ZapStdio() const;
110 void OpenConsole() const;
Elliott Hughesad8e94e2016-06-15 14:49:57 -0700111 void KillProcessGroup(int signal);
Jorge Lucangeli Obes344d01f2016-07-08 13:32:26 -0400112 void SetProcessAttributes();
Tom Cherrybac32992015-07-31 12:45:25 -0700113
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -0400114 bool ParseCapabilities(const std::vector<std::string>& args, std::string *err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400115 bool ParseClass(const std::vector<std::string>& args, std::string* err);
116 bool ParseConsole(const std::vector<std::string>& args, std::string* err);
117 bool ParseCritical(const std::vector<std::string>& args, std::string* err);
118 bool ParseDisabled(const std::vector<std::string>& args, std::string* err);
119 bool ParseGroup(const std::vector<std::string>& args, std::string* err);
120 bool ParsePriority(const std::vector<std::string>& args, std::string* err);
121 bool ParseIoprio(const std::vector<std::string>& args, std::string* err);
122 bool ParseKeycodes(const std::vector<std::string>& args, std::string* err);
123 bool ParseOneshot(const std::vector<std::string>& args, std::string* err);
124 bool ParseOnrestart(const std::vector<std::string>& args, std::string* err);
Marco Nelissen310f6702016-07-22 12:07:06 -0700125 bool ParseOomScoreAdjust(const std::vector<std::string>& args, std::string* err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400126 bool ParseNamespace(const std::vector<std::string>& args, std::string* err);
127 bool ParseSeclabel(const std::vector<std::string>& args, std::string* err);
128 bool ParseSetenv(const std::vector<std::string>& args, std::string* err);
129 bool ParseSocket(const std::vector<std::string>& args, std::string* err);
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700130 bool ParseFile(const std::vector<std::string>& args, std::string* err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400131 bool ParseUser(const std::vector<std::string>& args, std::string* err);
132 bool ParseWritepid(const std::vector<std::string>& args, std::string* err);
Tom Cherryb7349902015-08-26 11:43:36 -0700133
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700134 template <typename T>
135 bool AddDescriptor(const std::vector<std::string>& args, std::string* err);
136
Tom Cherrybac32992015-07-31 12:45:25 -0700137 std::string name_;
Wei Wang641ff0a2017-03-27 10:59:11 -0700138 std::set<std::string> classnames_;
Viorel Suman70daa672016-03-21 10:08:07 +0200139 std::string console_;
Tom Cherrybac32992015-07-31 12:45:25 -0700140
141 unsigned flags_;
142 pid_t pid_;
James Hawkinsc8ac0672017-02-14 19:20:20 +0000143 boot_clock::time_point time_started_; // time of last start
144 boot_clock::time_point time_crashed_; // first crash within inspection window
Elliott Hughes9605a942016-11-10 17:43:47 -0800145 int crash_count_; // number of times crashed within window
Tom Cherrybac32992015-07-31 12:45:25 -0700146
147 uid_t uid_;
148 gid_t gid_;
149 std::vector<gid_t> supp_gids_;
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -0400150 CapSet capabilities_;
Jorge Lucangeli Obes1b3fa3d2016-04-21 15:35:09 -0700151 unsigned namespace_flags_;
Tom Cherrybac32992015-07-31 12:45:25 -0700152
153 std::string seclabel_;
154
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700155 std::vector<std::unique_ptr<DescriptorInfo>> descriptors_;
Tom Cherrybac32992015-07-31 12:45:25 -0700156 std::vector<ServiceEnvironmentInfo> envvars_;
157
158 Action onrestart_; // Commands to execute on restart.
159
160 std::vector<std::string> writepid_files_;
161
162 // keycodes for triggering this service via /dev/keychord
163 std::vector<int> keycodes_;
164 int keychord_id_;
165
166 IoSchedClass ioprio_class_;
167 int ioprio_pri_;
Vitalii Tomkiv081705c2016-05-18 17:36:30 -0700168 int priority_;
Tom Cherrybac32992015-07-31 12:45:25 -0700169
Marco Nelissen310f6702016-07-22 12:07:06 -0700170 int oom_score_adjust_;
171
Tom Cherrybac32992015-07-31 12:45:25 -0700172 std::vector<std::string> args_;
173};
174
175class ServiceManager {
176public:
177 static ServiceManager& GetInstance();
178
Tom Cherryb7349902015-08-26 11:43:36 -0700179 void AddService(std::unique_ptr<Service> service);
Tom Cherrybac32992015-07-31 12:45:25 -0700180 Service* MakeExecOneshotService(const std::vector<std::string>& args);
181 Service* FindServiceByName(const std::string& name) const;
182 Service* FindServiceByPid(pid_t pid) const;
183 Service* FindServiceByKeychord(int keychord_id) const;
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700184 void ForEachService(const std::function<void(Service*)>& callback) const;
Tom Cherrybac32992015-07-31 12:45:25 -0700185 void ForEachServiceInClass(const std::string& classname,
186 void (*func)(Service* svc)) const;
187 void ForEachServiceWithFlags(unsigned matchflags,
188 void (*func)(Service* svc)) const;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800189 void ReapAnyOutstandingChildren();
Tom Cherrybac32992015-07-31 12:45:25 -0700190 void RemoveService(const Service& svc);
191 void DumpState() const;
Tom Cherryb7349902015-08-26 11:43:36 -0700192
Tom Cherrybac32992015-07-31 12:45:25 -0700193private:
194 ServiceManager();
195
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800196 // Cleans up a child process that exited.
197 // Returns true iff a children was cleaned up.
198 bool ReapOneProcess();
199
Tom Cherrybac32992015-07-31 12:45:25 -0700200 static int exec_count_; // Every service needs a unique name.
201 std::vector<std::unique_ptr<Service>> services_;
202};
203
Tom Cherryb7349902015-08-26 11:43:36 -0700204class ServiceParser : public SectionParser {
205public:
206 ServiceParser() : service_(nullptr) {
207 }
208 bool ParseSection(const std::vector<std::string>& args,
209 std::string* err) override;
210 bool ParseLineSection(const std::vector<std::string>& args,
211 const std::string& filename, int line,
212 std::string* err) const override;
213 void EndSection() override;
214 void EndFile(const std::string&) override {
215 }
216private:
217 bool IsValidName(const std::string& name) const;
218
219 std::unique_ptr<Service> service_;
220};
221
Tom Cherrybac32992015-07-31 12:45:25 -0700222#endif