blob: 8b3a0ad7cad0953432ec5150baee85587a9c2480 [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,
75 unsigned flags, uid_t uid, gid_t gid, const std::vector<gid_t>& supp_gids,
76 const std::string& seclabel, const std::vector<std::string>& args);
77
Tom Cherryb7349902015-08-26 11:43:36 -070078 bool HandleLine(const std::vector<std::string>& args, std::string* err);
Tom Cherrybac32992015-07-31 12:45:25 -070079 bool Start();
80 bool StartIfNotDisabled();
81 bool Enable();
82 void Reset();
83 void Stop();
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080084 void Terminate();
Tom Cherrybac32992015-07-31 12:45:25 -070085 void Restart();
86 void RestartIfNeeded(time_t& process_needs_restart);
87 bool Reap();
88 void DumpState() const;
89
90 const std::string& name() const { return name_; }
91 const std::string& classname() const { return classname_; }
92 unsigned flags() const { return flags_; }
93 pid_t pid() const { return pid_; }
94 uid_t uid() const { return uid_; }
95 gid_t gid() const { return gid_; }
Vitalii Tomkiv081705c2016-05-18 17:36:30 -070096 int priority() const { return priority_; }
Tom Cherrybac32992015-07-31 12:45:25 -070097 const std::vector<gid_t>& supp_gids() const { return supp_gids_; }
98 const std::string& seclabel() const { return seclabel_; }
99 const std::vector<int>& keycodes() const { return keycodes_; }
100 int keychord_id() const { return keychord_id_; }
101 void set_keychord_id(int keychord_id) { keychord_id_ = keychord_id; }
102 const std::vector<std::string>& args() const { return args_; }
103
104private:
Tom Cherryb7349902015-08-26 11:43:36 -0700105 using OptionHandler = bool (Service::*) (const std::vector<std::string>& args,
106 std::string* err);
107 class OptionHandlerMap;
108
Tom Cherrybac32992015-07-31 12:45:25 -0700109 void NotifyStateChange(const std::string& new_state) const;
110 void StopOrReset(int how);
111 void ZapStdio() const;
112 void OpenConsole() const;
113 void PublishSocket(const std::string& name, int fd) const;
Elliott Hughesad8e94e2016-06-15 14:49:57 -0700114 void KillProcessGroup(int signal);
Tom Cherrybac32992015-07-31 12:45:25 -0700115
Tom Cherryb7349902015-08-26 11:43:36 -0700116 bool HandleClass(const std::vector<std::string>& args, std::string* err);
117 bool HandleConsole(const std::vector<std::string>& args, std::string* err);
118 bool HandleCritical(const std::vector<std::string>& args, std::string* err);
119 bool HandleDisabled(const std::vector<std::string>& args, std::string* err);
120 bool HandleGroup(const std::vector<std::string>& args, std::string* err);
Vitalii Tomkiv081705c2016-05-18 17:36:30 -0700121 bool HandlePriority(const std::vector<std::string>& args, std::string* err);
Tom Cherryb7349902015-08-26 11:43:36 -0700122 bool HandleIoprio(const std::vector<std::string>& args, std::string* err);
123 bool HandleKeycodes(const std::vector<std::string>& args, std::string* err);
124 bool HandleOneshot(const std::vector<std::string>& args, std::string* err);
125 bool HandleOnrestart(const std::vector<std::string>& args, std::string* err);
126 bool HandleSeclabel(const std::vector<std::string>& args, std::string* err);
127 bool HandleSetenv(const std::vector<std::string>& args, std::string* err);
128 bool HandleSocket(const std::vector<std::string>& args, std::string* err);
129 bool HandleUser(const std::vector<std::string>& args, std::string* err);
130 bool HandleWritepid(const std::vector<std::string>& args, std::string* err);
131
Tom Cherrybac32992015-07-31 12:45:25 -0700132 std::string name_;
133 std::string classname_;
Viorel Suman70daa672016-03-21 10:08:07 +0200134 std::string console_;
Tom Cherrybac32992015-07-31 12:45:25 -0700135
136 unsigned flags_;
137 pid_t pid_;
138 time_t time_started_; // time of last start
139 time_t time_crashed_; // first crash within inspection window
140 int nr_crashed_; // number of times crashed within window
141
142 uid_t uid_;
143 gid_t gid_;
144 std::vector<gid_t> supp_gids_;
145
146 std::string seclabel_;
147
148 std::vector<SocketInfo> sockets_;
149 std::vector<ServiceEnvironmentInfo> envvars_;
150
151 Action onrestart_; // Commands to execute on restart.
152
153 std::vector<std::string> writepid_files_;
154
155 // keycodes for triggering this service via /dev/keychord
156 std::vector<int> keycodes_;
157 int keychord_id_;
158
159 IoSchedClass ioprio_class_;
160 int ioprio_pri_;
Vitalii Tomkiv081705c2016-05-18 17:36:30 -0700161 int priority_;
Tom Cherrybac32992015-07-31 12:45:25 -0700162
163 std::vector<std::string> args_;
164};
165
166class ServiceManager {
167public:
168 static ServiceManager& GetInstance();
169
Tom Cherryb7349902015-08-26 11:43:36 -0700170 void AddService(std::unique_ptr<Service> service);
Tom Cherrybac32992015-07-31 12:45:25 -0700171 Service* MakeExecOneshotService(const std::vector<std::string>& args);
172 Service* FindServiceByName(const std::string& name) const;
173 Service* FindServiceByPid(pid_t pid) const;
174 Service* FindServiceByKeychord(int keychord_id) const;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800175 void ForEachService(std::function<void(Service*)> callback) const;
Tom Cherrybac32992015-07-31 12:45:25 -0700176 void ForEachServiceInClass(const std::string& classname,
177 void (*func)(Service* svc)) const;
178 void ForEachServiceWithFlags(unsigned matchflags,
179 void (*func)(Service* svc)) const;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800180 void ReapAnyOutstandingChildren();
Tom Cherrybac32992015-07-31 12:45:25 -0700181 void RemoveService(const Service& svc);
182 void DumpState() const;
Tom Cherryb7349902015-08-26 11:43:36 -0700183
Tom Cherrybac32992015-07-31 12:45:25 -0700184private:
185 ServiceManager();
186
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800187 // Cleans up a child process that exited.
188 // Returns true iff a children was cleaned up.
189 bool ReapOneProcess();
190
Tom Cherrybac32992015-07-31 12:45:25 -0700191 static int exec_count_; // Every service needs a unique name.
192 std::vector<std::unique_ptr<Service>> services_;
193};
194
Tom Cherryb7349902015-08-26 11:43:36 -0700195class ServiceParser : public SectionParser {
196public:
197 ServiceParser() : service_(nullptr) {
198 }
199 bool ParseSection(const std::vector<std::string>& args,
200 std::string* err) override;
201 bool ParseLineSection(const std::vector<std::string>& args,
202 const std::string& filename, int line,
203 std::string* err) const override;
204 void EndSection() override;
205 void EndFile(const std::string&) override {
206 }
207private:
208 bool IsValidName(const std::string& name) const;
209
210 std::unique_ptr<Service> service_;
211};
212
Tom Cherrybac32992015-07-31 12:45:25 -0700213#endif