blob: aa73aaf84de26051f46dcfec26db8482eb0a98ea [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>
25#include <string>
26#include <vector>
27
28#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070029#include "init_parser.h"
30#include "keyword_map.h"
Tom Cherrybac32992015-07-31 12:45:25 -070031
32#define SVC_DISABLED 0x001 // do not autostart with class
33#define SVC_ONESHOT 0x002 // do not restart on exit
34#define SVC_RUNNING 0x004 // currently active
35#define SVC_RESTARTING 0x008 // waiting to restart
36#define SVC_CONSOLE 0x010 // requires console
37#define SVC_CRITICAL 0x020 // will reboot into recovery if keeps crashing
38#define SVC_RESET 0x040 // Use when stopping a process,
39 // but not disabling so it can be restarted with its class.
40#define SVC_RC_DISABLED 0x080 // Remember if the disabled flag was set in the rc script.
41#define SVC_RESTART 0x100 // Use to safely restart (stop, wait, start) a service.
42#define SVC_DISABLED_START 0x200 // A start was requested but it was disabled at the time.
43#define SVC_EXEC 0x400 // This synthetic service corresponds to an 'exec'.
44
45#define NR_SVC_SUPP_GIDS 12 // twelve supplementary groups
46
47class Action;
48class ServiceManager;
49
50struct SocketInfo {
51 SocketInfo();
52 SocketInfo(const std::string& name, const std::string& type, uid_t uid,
53 gid_t gid, int perm, const std::string& socketcon);
54 std::string name;
55 std::string type;
56 uid_t uid;
57 gid_t gid;
58 int perm;
59 std::string socketcon;
60};
61
62struct ServiceEnvironmentInfo {
63 ServiceEnvironmentInfo();
64 ServiceEnvironmentInfo(const std::string& name, const std::string& value);
65 std::string name;
66 std::string value;
67};
68
69class Service {
70public:
71 Service(const std::string& name, const std::string& classname,
72 const std::vector<std::string>& args);
73
74 Service(const std::string& name, const std::string& classname,
Jorge Lucangeli Obes1b3fa3d2016-04-21 15:35:09 -070075 unsigned flags, uid_t uid, gid_t gid,
76 const std::vector<gid_t>& supp_gids, unsigned namespace_flags,
77 const std::string& seclabel, const std::vector<std::string>& args);
Tom Cherrybac32992015-07-31 12:45:25 -070078
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -040079 bool ParseLine(const std::vector<std::string>& args, std::string* err);
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();
87 void RestartIfNeeded(time_t& process_needs_restart);
88 bool Reap();
89 void DumpState() const;
90
91 const std::string& name() const { return name_; }
92 const std::string& classname() const { return classname_; }
93 unsigned flags() const { return flags_; }
94 pid_t pid() const { return pid_; }
95 uid_t uid() const { return uid_; }
96 gid_t gid() const { return gid_; }
Vitalii Tomkiv081705c2016-05-18 17:36:30 -070097 int priority() const { return priority_; }
Tom Cherrybac32992015-07-31 12:45:25 -070098 const std::vector<gid_t>& supp_gids() const { return supp_gids_; }
99 const std::string& seclabel() const { return seclabel_; }
100 const std::vector<int>& keycodes() const { return keycodes_; }
101 int keychord_id() const { return keychord_id_; }
102 void set_keychord_id(int keychord_id) { keychord_id_ = keychord_id; }
103 const std::vector<std::string>& args() const { return args_; }
104
105private:
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400106 using OptionParser = bool (Service::*) (const std::vector<std::string>& args,
107 std::string* err);
108 class OptionParserMap;
Tom Cherryb7349902015-08-26 11:43:36 -0700109
Tom Cherrybac32992015-07-31 12:45:25 -0700110 void NotifyStateChange(const std::string& new_state) const;
111 void StopOrReset(int how);
112 void ZapStdio() const;
113 void OpenConsole() const;
114 void PublishSocket(const std::string& name, int fd) const;
Elliott Hughesad8e94e2016-06-15 14:49:57 -0700115 void KillProcessGroup(int signal);
Jorge Lucangeli Obes344d01f2016-07-08 13:32:26 -0400116 void CreateSockets(const std::string& scon);
117 void SetProcessAttributes();
Tom Cherrybac32992015-07-31 12:45:25 -0700118
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400119 bool ParseClass(const std::vector<std::string>& args, std::string* err);
120 bool ParseConsole(const std::vector<std::string>& args, std::string* err);
121 bool ParseCritical(const std::vector<std::string>& args, std::string* err);
122 bool ParseDisabled(const std::vector<std::string>& args, std::string* err);
123 bool ParseGroup(const std::vector<std::string>& args, std::string* err);
124 bool ParsePriority(const std::vector<std::string>& args, std::string* err);
125 bool ParseIoprio(const std::vector<std::string>& args, std::string* err);
126 bool ParseKeycodes(const std::vector<std::string>& args, std::string* err);
127 bool ParseOneshot(const std::vector<std::string>& args, std::string* err);
128 bool ParseOnrestart(const std::vector<std::string>& args, std::string* err);
129 bool ParseNamespace(const std::vector<std::string>& args, std::string* err);
130 bool ParseSeclabel(const std::vector<std::string>& args, std::string* err);
131 bool ParseSetenv(const std::vector<std::string>& args, std::string* err);
132 bool ParseSocket(const std::vector<std::string>& args, std::string* err);
133 bool ParseUser(const std::vector<std::string>& args, std::string* err);
134 bool ParseWritepid(const std::vector<std::string>& args, std::string* err);
Tom Cherryb7349902015-08-26 11:43:36 -0700135
Tom Cherrybac32992015-07-31 12:45:25 -0700136 std::string name_;
137 std::string classname_;
Viorel Suman70daa672016-03-21 10:08:07 +0200138 std::string console_;
Tom Cherrybac32992015-07-31 12:45:25 -0700139
140 unsigned flags_;
141 pid_t pid_;
142 time_t time_started_; // time of last start
143 time_t time_crashed_; // first crash within inspection window
144 int nr_crashed_; // number of times crashed within window
145
146 uid_t uid_;
147 gid_t gid_;
148 std::vector<gid_t> supp_gids_;
Jorge Lucangeli Obes1b3fa3d2016-04-21 15:35:09 -0700149 unsigned namespace_flags_;
Tom Cherrybac32992015-07-31 12:45:25 -0700150
151 std::string seclabel_;
152
153 std::vector<SocketInfo> sockets_;
154 std::vector<ServiceEnvironmentInfo> envvars_;
155
156 Action onrestart_; // Commands to execute on restart.
157
158 std::vector<std::string> writepid_files_;
159
160 // keycodes for triggering this service via /dev/keychord
161 std::vector<int> keycodes_;
162 int keychord_id_;
163
164 IoSchedClass ioprio_class_;
165 int ioprio_pri_;
Vitalii Tomkiv081705c2016-05-18 17:36:30 -0700166 int priority_;
Tom Cherrybac32992015-07-31 12:45:25 -0700167
168 std::vector<std::string> args_;
169};
170
171class ServiceManager {
172public:
173 static ServiceManager& GetInstance();
174
Tom Cherryb7349902015-08-26 11:43:36 -0700175 void AddService(std::unique_ptr<Service> service);
Tom Cherrybac32992015-07-31 12:45:25 -0700176 Service* MakeExecOneshotService(const std::vector<std::string>& args);
177 Service* FindServiceByName(const std::string& name) const;
178 Service* FindServiceByPid(pid_t pid) const;
179 Service* FindServiceByKeychord(int keychord_id) const;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800180 void ForEachService(std::function<void(Service*)> callback) const;
Tom Cherrybac32992015-07-31 12:45:25 -0700181 void ForEachServiceInClass(const std::string& classname,
182 void (*func)(Service* svc)) const;
183 void ForEachServiceWithFlags(unsigned matchflags,
184 void (*func)(Service* svc)) const;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800185 void ReapAnyOutstandingChildren();
Tom Cherrybac32992015-07-31 12:45:25 -0700186 void RemoveService(const Service& svc);
187 void DumpState() const;
Tom Cherryb7349902015-08-26 11:43:36 -0700188
Tom Cherrybac32992015-07-31 12:45:25 -0700189private:
190 ServiceManager();
191
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800192 // Cleans up a child process that exited.
193 // Returns true iff a children was cleaned up.
194 bool ReapOneProcess();
195
Tom Cherrybac32992015-07-31 12:45:25 -0700196 static int exec_count_; // Every service needs a unique name.
197 std::vector<std::unique_ptr<Service>> services_;
198};
199
Tom Cherryb7349902015-08-26 11:43:36 -0700200class ServiceParser : public SectionParser {
201public:
202 ServiceParser() : service_(nullptr) {
203 }
204 bool ParseSection(const std::vector<std::string>& args,
205 std::string* err) override;
206 bool ParseLineSection(const std::vector<std::string>& args,
207 const std::string& filename, int line,
208 std::string* err) const override;
209 void EndSection() override;
210 void EndFile(const std::string&) override {
211 }
212private:
213 bool IsValidName(const std::string& name) const;
214
215 std::unique_ptr<Service> service_;
216};
217
Tom Cherrybac32992015-07-31 12:45:25 -0700218#endif