blob: 7f035eff264db3151da97e3db70eb4933a60982a [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"
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -040029#include "capabilities.h"
Tom Cherryb7349902015-08-26 11:43:36 -070030#include "init_parser.h"
31#include "keyword_map.h"
Tom Cherrybac32992015-07-31 12:45:25 -070032
33#define SVC_DISABLED 0x001 // do not autostart with class
34#define SVC_ONESHOT 0x002 // do not restart on exit
35#define SVC_RUNNING 0x004 // currently active
36#define SVC_RESTARTING 0x008 // waiting to restart
37#define SVC_CONSOLE 0x010 // requires console
38#define SVC_CRITICAL 0x020 // will reboot into recovery if keeps crashing
39#define SVC_RESET 0x040 // Use when stopping a process,
40 // but not disabling so it can be restarted with its class.
41#define SVC_RC_DISABLED 0x080 // Remember if the disabled flag was set in the rc script.
42#define SVC_RESTART 0x100 // Use to safely restart (stop, wait, start) a service.
43#define SVC_DISABLED_START 0x200 // A start was requested but it was disabled at the time.
44#define SVC_EXEC 0x400 // This synthetic service corresponds to an 'exec'.
45
46#define NR_SVC_SUPP_GIDS 12 // twelve supplementary groups
47
48class Action;
49class ServiceManager;
50
51struct SocketInfo {
52 SocketInfo();
53 SocketInfo(const std::string& name, const std::string& type, uid_t uid,
54 gid_t gid, int perm, const std::string& socketcon);
55 std::string name;
56 std::string type;
57 uid_t uid;
58 gid_t gid;
59 int perm;
60 std::string socketcon;
61};
62
63struct ServiceEnvironmentInfo {
64 ServiceEnvironmentInfo();
65 ServiceEnvironmentInfo(const std::string& name, const std::string& value);
66 std::string name;
67 std::string value;
68};
69
70class Service {
71public:
72 Service(const std::string& name, const std::string& classname,
73 const std::vector<std::string>& args);
74
75 Service(const std::string& name, const std::string& classname,
Jorge Lucangeli Obes1b3fa3d2016-04-21 15:35:09 -070076 unsigned flags, uid_t uid, gid_t gid,
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -040077 const std::vector<gid_t>& supp_gids, const CapSet& capabilities,
78 unsigned namespace_flags, const std::string& seclabel,
79 const std::vector<std::string>& args);
Tom Cherrybac32992015-07-31 12:45:25 -070080
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -040081 bool ParseLine(const std::vector<std::string>& args, std::string* err);
Tom Cherrybac32992015-07-31 12:45:25 -070082 bool Start();
83 bool StartIfNotDisabled();
84 bool Enable();
85 void Reset();
86 void Stop();
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080087 void Terminate();
Tom Cherrybac32992015-07-31 12:45:25 -070088 void Restart();
89 void RestartIfNeeded(time_t& process_needs_restart);
90 bool Reap();
91 void DumpState() const;
92
93 const std::string& name() const { return name_; }
94 const std::string& classname() const { return classname_; }
95 unsigned flags() const { return flags_; }
96 pid_t pid() const { return pid_; }
97 uid_t uid() const { return uid_; }
98 gid_t gid() const { return gid_; }
Vitalii Tomkiv081705c2016-05-18 17:36:30 -070099 int priority() const { return priority_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700100 const std::vector<gid_t>& supp_gids() const { return supp_gids_; }
101 const std::string& seclabel() const { return seclabel_; }
102 const std::vector<int>& keycodes() const { return keycodes_; }
103 int keychord_id() const { return keychord_id_; }
104 void set_keychord_id(int keychord_id) { keychord_id_ = keychord_id; }
105 const std::vector<std::string>& args() const { return args_; }
106
107private:
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400108 using OptionParser = bool (Service::*) (const std::vector<std::string>& args,
109 std::string* err);
110 class OptionParserMap;
Tom Cherryb7349902015-08-26 11:43:36 -0700111
Tom Cherrybac32992015-07-31 12:45:25 -0700112 void NotifyStateChange(const std::string& new_state) const;
113 void StopOrReset(int how);
114 void ZapStdio() const;
115 void OpenConsole() const;
116 void PublishSocket(const std::string& name, int fd) const;
Elliott Hughesad8e94e2016-06-15 14:49:57 -0700117 void KillProcessGroup(int signal);
Jorge Lucangeli Obes344d01f2016-07-08 13:32:26 -0400118 void CreateSockets(const std::string& scon);
119 void SetProcessAttributes();
Tom Cherrybac32992015-07-31 12:45:25 -0700120
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -0400121 bool ParseCapabilities(const std::vector<std::string>& args, std::string *err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400122 bool ParseClass(const std::vector<std::string>& args, std::string* err);
123 bool ParseConsole(const std::vector<std::string>& args, std::string* err);
124 bool ParseCritical(const std::vector<std::string>& args, std::string* err);
125 bool ParseDisabled(const std::vector<std::string>& args, std::string* err);
126 bool ParseGroup(const std::vector<std::string>& args, std::string* err);
127 bool ParsePriority(const std::vector<std::string>& args, std::string* err);
128 bool ParseIoprio(const std::vector<std::string>& args, std::string* err);
129 bool ParseKeycodes(const std::vector<std::string>& args, std::string* err);
130 bool ParseOneshot(const std::vector<std::string>& args, std::string* err);
131 bool ParseOnrestart(const std::vector<std::string>& args, std::string* err);
Marco Nelissen310f6702016-07-22 12:07:06 -0700132 bool ParseOomScoreAdjust(const std::vector<std::string>& args, std::string* err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400133 bool ParseNamespace(const std::vector<std::string>& args, std::string* err);
134 bool ParseSeclabel(const std::vector<std::string>& args, std::string* err);
135 bool ParseSetenv(const std::vector<std::string>& args, std::string* err);
136 bool ParseSocket(const std::vector<std::string>& args, std::string* err);
137 bool ParseUser(const std::vector<std::string>& args, std::string* err);
138 bool ParseWritepid(const std::vector<std::string>& args, std::string* err);
Tom Cherryb7349902015-08-26 11:43:36 -0700139
Tom Cherrybac32992015-07-31 12:45:25 -0700140 std::string name_;
141 std::string classname_;
Viorel Suman70daa672016-03-21 10:08:07 +0200142 std::string console_;
Tom Cherrybac32992015-07-31 12:45:25 -0700143
144 unsigned flags_;
145 pid_t pid_;
146 time_t time_started_; // time of last start
147 time_t time_crashed_; // first crash within inspection window
148 int nr_crashed_; // number of times crashed within window
149
150 uid_t uid_;
151 gid_t gid_;
152 std::vector<gid_t> supp_gids_;
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -0400153 CapSet capabilities_;
Jorge Lucangeli Obes1b3fa3d2016-04-21 15:35:09 -0700154 unsigned namespace_flags_;
Tom Cherrybac32992015-07-31 12:45:25 -0700155
156 std::string seclabel_;
157
158 std::vector<SocketInfo> sockets_;
159 std::vector<ServiceEnvironmentInfo> envvars_;
160
161 Action onrestart_; // Commands to execute on restart.
162
163 std::vector<std::string> writepid_files_;
164
165 // keycodes for triggering this service via /dev/keychord
166 std::vector<int> keycodes_;
167 int keychord_id_;
168
169 IoSchedClass ioprio_class_;
170 int ioprio_pri_;
Vitalii Tomkiv081705c2016-05-18 17:36:30 -0700171 int priority_;
Tom Cherrybac32992015-07-31 12:45:25 -0700172
Marco Nelissen310f6702016-07-22 12:07:06 -0700173 int oom_score_adjust_;
174
Tom Cherrybac32992015-07-31 12:45:25 -0700175 std::vector<std::string> args_;
176};
177
178class ServiceManager {
179public:
180 static ServiceManager& GetInstance();
181
Tom Cherryb7349902015-08-26 11:43:36 -0700182 void AddService(std::unique_ptr<Service> service);
Tom Cherrybac32992015-07-31 12:45:25 -0700183 Service* MakeExecOneshotService(const std::vector<std::string>& args);
184 Service* FindServiceByName(const std::string& name) const;
185 Service* FindServiceByPid(pid_t pid) const;
186 Service* FindServiceByKeychord(int keychord_id) const;
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700187 void ForEachService(const std::function<void(Service*)>& callback) const;
Tom Cherrybac32992015-07-31 12:45:25 -0700188 void ForEachServiceInClass(const std::string& classname,
189 void (*func)(Service* svc)) const;
190 void ForEachServiceWithFlags(unsigned matchflags,
191 void (*func)(Service* svc)) const;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800192 void ReapAnyOutstandingChildren();
Tom Cherrybac32992015-07-31 12:45:25 -0700193 void RemoveService(const Service& svc);
194 void DumpState() const;
Tom Cherryb7349902015-08-26 11:43:36 -0700195
Tom Cherrybac32992015-07-31 12:45:25 -0700196private:
197 ServiceManager();
198
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800199 // Cleans up a child process that exited.
200 // Returns true iff a children was cleaned up.
201 bool ReapOneProcess();
202
Tom Cherrybac32992015-07-31 12:45:25 -0700203 static int exec_count_; // Every service needs a unique name.
204 std::vector<std::unique_ptr<Service>> services_;
205};
206
Tom Cherryb7349902015-08-26 11:43:36 -0700207class ServiceParser : public SectionParser {
208public:
209 ServiceParser() : service_(nullptr) {
210 }
211 bool ParseSection(const std::vector<std::string>& args,
212 std::string* err) override;
213 bool ParseLineSection(const std::vector<std::string>& args,
214 const std::string& filename, int line,
215 std::string* err) const override;
216 void EndSection() override;
217 void EndFile(const std::string&) override {
218 }
219private:
220 bool IsValidName(const std::string& name) const;
221
222 std::unique_ptr<Service> service_;
223};
224
Tom Cherrybac32992015-07-31 12:45:25 -0700225#endif