blob: d9e8f57c3c152e8d7c95a5a4a54690c7c805472b [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"
Mark Salyzyn62767fe2016-10-27 07:45:34 -070030#include "descriptors.h"
Tom Cherryb7349902015-08-26 11:43:36 -070031#include "init_parser.h"
32#include "keyword_map.h"
Tom Cherrybac32992015-07-31 12:45:25 -070033
34#define SVC_DISABLED 0x001 // do not autostart with class
35#define SVC_ONESHOT 0x002 // do not restart on exit
36#define SVC_RUNNING 0x004 // currently active
37#define SVC_RESTARTING 0x008 // waiting to restart
38#define SVC_CONSOLE 0x010 // requires console
39#define SVC_CRITICAL 0x020 // will reboot into recovery if keeps crashing
40#define SVC_RESET 0x040 // Use when stopping a process,
41 // but not disabling so it can be restarted with its class.
42#define SVC_RC_DISABLED 0x080 // Remember if the disabled flag was set in the rc script.
43#define SVC_RESTART 0x100 // Use to safely restart (stop, wait, start) a service.
44#define SVC_DISABLED_START 0x200 // A start was requested but it was disabled at the time.
45#define SVC_EXEC 0x400 // This synthetic service corresponds to an 'exec'.
46
47#define NR_SVC_SUPP_GIDS 12 // twelve supplementary groups
48
49class Action;
50class ServiceManager;
51
Tom Cherrybac32992015-07-31 12:45:25 -070052struct ServiceEnvironmentInfo {
53 ServiceEnvironmentInfo();
54 ServiceEnvironmentInfo(const std::string& name, const std::string& value);
55 std::string name;
56 std::string value;
57};
58
59class Service {
60public:
61 Service(const std::string& name, const std::string& classname,
62 const std::vector<std::string>& args);
63
64 Service(const std::string& name, const std::string& classname,
Jorge Lucangeli Obes1b3fa3d2016-04-21 15:35:09 -070065 unsigned flags, uid_t uid, gid_t gid,
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -040066 const std::vector<gid_t>& supp_gids, const CapSet& capabilities,
67 unsigned namespace_flags, const std::string& seclabel,
68 const std::vector<std::string>& args);
Tom Cherrybac32992015-07-31 12:45:25 -070069
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -040070 bool ParseLine(const std::vector<std::string>& args, std::string* err);
Tom Cherrybac32992015-07-31 12:45:25 -070071 bool Start();
72 bool StartIfNotDisabled();
73 bool Enable();
74 void Reset();
75 void Stop();
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080076 void Terminate();
Tom Cherrybac32992015-07-31 12:45:25 -070077 void Restart();
78 void RestartIfNeeded(time_t& process_needs_restart);
79 bool Reap();
80 void DumpState() const;
81
82 const std::string& name() const { return name_; }
83 const std::string& classname() const { return classname_; }
84 unsigned flags() const { return flags_; }
85 pid_t pid() const { return pid_; }
86 uid_t uid() const { return uid_; }
87 gid_t gid() const { return gid_; }
Vitalii Tomkiv081705c2016-05-18 17:36:30 -070088 int priority() const { return priority_; }
Tom Cherrybac32992015-07-31 12:45:25 -070089 const std::vector<gid_t>& supp_gids() const { return supp_gids_; }
90 const std::string& seclabel() const { return seclabel_; }
91 const std::vector<int>& keycodes() const { return keycodes_; }
92 int keychord_id() const { return keychord_id_; }
93 void set_keychord_id(int keychord_id) { keychord_id_ = keychord_id; }
94 const std::vector<std::string>& args() const { return args_; }
95
96private:
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -040097 using OptionParser = bool (Service::*) (const std::vector<std::string>& args,
98 std::string* err);
99 class OptionParserMap;
Tom Cherryb7349902015-08-26 11:43:36 -0700100
Tom Cherrybac32992015-07-31 12:45:25 -0700101 void NotifyStateChange(const std::string& new_state) const;
102 void StopOrReset(int how);
103 void ZapStdio() const;
104 void OpenConsole() const;
Elliott Hughesad8e94e2016-06-15 14:49:57 -0700105 void KillProcessGroup(int signal);
Jorge Lucangeli Obes344d01f2016-07-08 13:32:26 -0400106 void SetProcessAttributes();
Tom Cherrybac32992015-07-31 12:45:25 -0700107
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -0400108 bool ParseCapabilities(const std::vector<std::string>& args, std::string *err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400109 bool ParseClass(const std::vector<std::string>& args, std::string* err);
110 bool ParseConsole(const std::vector<std::string>& args, std::string* err);
111 bool ParseCritical(const std::vector<std::string>& args, std::string* err);
112 bool ParseDisabled(const std::vector<std::string>& args, std::string* err);
113 bool ParseGroup(const std::vector<std::string>& args, std::string* err);
114 bool ParsePriority(const std::vector<std::string>& args, std::string* err);
115 bool ParseIoprio(const std::vector<std::string>& args, std::string* err);
116 bool ParseKeycodes(const std::vector<std::string>& args, std::string* err);
117 bool ParseOneshot(const std::vector<std::string>& args, std::string* err);
118 bool ParseOnrestart(const std::vector<std::string>& args, std::string* err);
Marco Nelissen310f6702016-07-22 12:07:06 -0700119 bool ParseOomScoreAdjust(const std::vector<std::string>& args, std::string* err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400120 bool ParseNamespace(const std::vector<std::string>& args, std::string* err);
121 bool ParseSeclabel(const std::vector<std::string>& args, std::string* err);
122 bool ParseSetenv(const std::vector<std::string>& args, std::string* err);
123 bool ParseSocket(const std::vector<std::string>& args, std::string* err);
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700124 bool ParseFile(const std::vector<std::string>& args, std::string* err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400125 bool ParseUser(const std::vector<std::string>& args, std::string* err);
126 bool ParseWritepid(const std::vector<std::string>& args, std::string* err);
Tom Cherryb7349902015-08-26 11:43:36 -0700127
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700128 template <typename T>
129 bool AddDescriptor(const std::vector<std::string>& args, std::string* err);
130
Tom Cherrybac32992015-07-31 12:45:25 -0700131 std::string name_;
132 std::string classname_;
Viorel Suman70daa672016-03-21 10:08:07 +0200133 std::string console_;
Tom Cherrybac32992015-07-31 12:45:25 -0700134
135 unsigned flags_;
136 pid_t pid_;
137 time_t time_started_; // time of last start
138 time_t time_crashed_; // first crash within inspection window
139 int nr_crashed_; // number of times crashed within window
140
141 uid_t uid_;
142 gid_t gid_;
143 std::vector<gid_t> supp_gids_;
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -0400144 CapSet capabilities_;
Jorge Lucangeli Obes1b3fa3d2016-04-21 15:35:09 -0700145 unsigned namespace_flags_;
Tom Cherrybac32992015-07-31 12:45:25 -0700146
147 std::string seclabel_;
148
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700149 std::vector<std::unique_ptr<DescriptorInfo>> descriptors_;
Tom Cherrybac32992015-07-31 12:45:25 -0700150 std::vector<ServiceEnvironmentInfo> envvars_;
151
152 Action onrestart_; // Commands to execute on restart.
153
154 std::vector<std::string> writepid_files_;
155
156 // keycodes for triggering this service via /dev/keychord
157 std::vector<int> keycodes_;
158 int keychord_id_;
159
160 IoSchedClass ioprio_class_;
161 int ioprio_pri_;
Vitalii Tomkiv081705c2016-05-18 17:36:30 -0700162 int priority_;
Tom Cherrybac32992015-07-31 12:45:25 -0700163
Marco Nelissen310f6702016-07-22 12:07:06 -0700164 int oom_score_adjust_;
165
Tom Cherrybac32992015-07-31 12:45:25 -0700166 std::vector<std::string> args_;
167};
168
169class ServiceManager {
170public:
171 static ServiceManager& GetInstance();
172
Tom Cherryb7349902015-08-26 11:43:36 -0700173 void AddService(std::unique_ptr<Service> service);
Tom Cherrybac32992015-07-31 12:45:25 -0700174 Service* MakeExecOneshotService(const std::vector<std::string>& args);
175 Service* FindServiceByName(const std::string& name) const;
176 Service* FindServiceByPid(pid_t pid) const;
177 Service* FindServiceByKeychord(int keychord_id) const;
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700178 void ForEachService(const std::function<void(Service*)>& callback) const;
Tom Cherrybac32992015-07-31 12:45:25 -0700179 void ForEachServiceInClass(const std::string& classname,
180 void (*func)(Service* svc)) const;
181 void ForEachServiceWithFlags(unsigned matchflags,
182 void (*func)(Service* svc)) const;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800183 void ReapAnyOutstandingChildren();
Tom Cherrybac32992015-07-31 12:45:25 -0700184 void RemoveService(const Service& svc);
185 void DumpState() const;
Tom Cherryb7349902015-08-26 11:43:36 -0700186
Tom Cherrybac32992015-07-31 12:45:25 -0700187private:
188 ServiceManager();
189
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800190 // Cleans up a child process that exited.
191 // Returns true iff a children was cleaned up.
192 bool ReapOneProcess();
193
Tom Cherrybac32992015-07-31 12:45:25 -0700194 static int exec_count_; // Every service needs a unique name.
195 std::vector<std::unique_ptr<Service>> services_;
196};
197
Tom Cherryb7349902015-08-26 11:43:36 -0700198class ServiceParser : public SectionParser {
199public:
200 ServiceParser() : service_(nullptr) {
201 }
202 bool ParseSection(const std::vector<std::string>& args,
203 std::string* err) override;
204 bool ParseLineSection(const std::vector<std::string>& args,
205 const std::string& filename, int line,
206 std::string* err) const override;
207 void EndSection() override;
208 void EndFile(const std::string&) override {
209 }
210private:
211 bool IsValidName(const std::string& name) const;
212
213 std::unique_ptr<Service> service_;
214};
215
Tom Cherrybac32992015-07-31 12:45:25 -0700216#endif