blob: d84ce028124c243185dada105f867ae38066843d [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
James Hawkinse78ea772017-03-24 11:43:02 -070029#include <android-base/chrono_utils.h>
30
Tom Cherrybac32992015-07-31 12:45:25 -070031#include "action.h"
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -040032#include "capabilities.h"
Mark Salyzyn62767fe2016-10-27 07:45:34 -070033#include "descriptors.h"
Tom Cherryb7349902015-08-26 11:43:36 -070034#include "init_parser.h"
35#include "keyword_map.h"
Elliott Hughes9605a942016-11-10 17:43:47 -080036#include "util.h"
Tom Cherrybac32992015-07-31 12:45:25 -070037
Keun-young Park8d01f632017-03-13 11:54:47 -070038#define SVC_DISABLED 0x001 // do not autostart with class
39#define SVC_ONESHOT 0x002 // do not restart on exit
40#define SVC_RUNNING 0x004 // currently active
41#define SVC_RESTARTING 0x008 // waiting to restart
42#define SVC_CONSOLE 0x010 // requires console
43#define SVC_CRITICAL 0x020 // will reboot into recovery if keeps crashing
44#define SVC_RESET 0x040 // Use when stopping a process,
Tom Cherrybac32992015-07-31 12:45:25 -070045 // but not disabling so it can be restarted with its class.
Keun-young Park8d01f632017-03-13 11:54:47 -070046#define SVC_RC_DISABLED 0x080 // Remember if the disabled flag was set in the rc script.
47#define SVC_RESTART 0x100 // Use to safely restart (stop, wait, start) a service.
Tom Cherrybac32992015-07-31 12:45:25 -070048#define SVC_DISABLED_START 0x200 // A start was requested but it was disabled at the time.
Tom Cherryb27004a2017-03-27 16:27:30 -070049#define SVC_EXEC 0x400 // This service was started by either 'exec' or 'exec_start' and stops
50 // init from processing more commands until it completes
Keun-young Park8d01f632017-03-13 11:54:47 -070051
52#define SVC_SHUTDOWN_CRITICAL 0x800 // This service is critical for shutdown and
53 // should not be killed during shutdown
Tom Cherryb27004a2017-03-27 16:27:30 -070054#define SVC_TEMPORARY 0x1000 // This service was started by 'exec' and should be removed from the
55 // service list once it is reaped.
Tom Cherrybac32992015-07-31 12:45:25 -070056
57#define NR_SVC_SUPP_GIDS 12 // twelve supplementary groups
58
59class Action;
60class ServiceManager;
61
Tom Cherrybac32992015-07-31 12:45:25 -070062struct 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 {
Wei Wang641ff0a2017-03-27 10:59:11 -070070 public:
71 Service(const std::string& name, const std::vector<std::string>& args);
Tom Cherrybac32992015-07-31 12:45:25 -070072
Wei Wang641ff0a2017-03-27 10:59:11 -070073 Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid,
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -040074 const std::vector<gid_t>& supp_gids, const CapSet& capabilities,
75 unsigned namespace_flags, const std::string& seclabel,
76 const std::vector<std::string>& args);
Tom Cherrybac32992015-07-31 12:45:25 -070077
Keun-young Park8d01f632017-03-13 11:54:47 -070078 bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; }
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -040079 bool ParseLine(const std::vector<std::string>& args, std::string* err);
Tom Cherryb27004a2017-03-27 16:27:30 -070080 bool ExecStart(std::unique_ptr<Timer>* exec_waiter);
Tom Cherrybac32992015-07-31 12:45:25 -070081 bool Start();
82 bool StartIfNotDisabled();
83 bool Enable();
84 void Reset();
85 void Stop();
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080086 void Terminate();
Tom Cherrybac32992015-07-31 12:45:25 -070087 void Restart();
Elliott Hughes9605a942016-11-10 17:43:47 -080088 void RestartIfNeeded(time_t* process_needs_restart_at);
Tom Cherryb27004a2017-03-27 16:27:30 -070089 void Reap();
Tom Cherrybac32992015-07-31 12:45:25 -070090 void DumpState() const;
Keun-young Park8d01f632017-03-13 11:54:47 -070091 void SetShutdownCritical() { flags_ |= SVC_SHUTDOWN_CRITICAL; }
Wei Wang641ff0a2017-03-27 10:59:11 -070092 bool IsShutdownCritical() const { return (flags_ & SVC_SHUTDOWN_CRITICAL) != 0; }
Tom Cherrybac32992015-07-31 12:45:25 -070093
94 const std::string& name() const { return name_; }
Wei Wang641ff0a2017-03-27 10:59:11 -070095 const std::set<std::string>& classnames() const { return classnames_; }
Tom Cherrybac32992015-07-31 12:45:25 -070096 unsigned flags() const { return flags_; }
97 pid_t pid() const { return pid_; }
98 uid_t uid() const { return uid_; }
99 gid_t gid() const { return gid_; }
Vitalii Tomkiv081705c2016-05-18 17:36:30 -0700100 int priority() const { return priority_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700101 const std::vector<gid_t>& supp_gids() const { return supp_gids_; }
102 const std::string& seclabel() const { return seclabel_; }
103 const std::vector<int>& keycodes() const { return keycodes_; }
104 int keychord_id() const { return keychord_id_; }
105 void set_keychord_id(int keychord_id) { keychord_id_ = keychord_id; }
106 const std::vector<std::string>& args() const { return args_; }
107
Wei Wang641ff0a2017-03-27 10:59:11 -0700108 private:
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400109 using OptionParser = bool (Service::*) (const std::vector<std::string>& args,
110 std::string* err);
111 class OptionParserMap;
Tom Cherryb7349902015-08-26 11:43:36 -0700112
Tom Cherrybac32992015-07-31 12:45:25 -0700113 void NotifyStateChange(const std::string& new_state) const;
114 void StopOrReset(int how);
115 void ZapStdio() const;
116 void OpenConsole() const;
Elliott Hughesad8e94e2016-06-15 14:49:57 -0700117 void KillProcessGroup(int signal);
Jorge Lucangeli Obes344d01f2016-07-08 13:32:26 -0400118 void SetProcessAttributes();
Tom Cherrybac32992015-07-31 12:45:25 -0700119
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -0400120 bool ParseCapabilities(const std::vector<std::string>& args, std::string *err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400121 bool ParseClass(const std::vector<std::string>& args, std::string* err);
122 bool ParseConsole(const std::vector<std::string>& args, std::string* err);
123 bool ParseCritical(const std::vector<std::string>& args, std::string* err);
124 bool ParseDisabled(const std::vector<std::string>& args, std::string* err);
125 bool ParseGroup(const std::vector<std::string>& args, std::string* err);
126 bool ParsePriority(const std::vector<std::string>& args, std::string* err);
127 bool ParseIoprio(const std::vector<std::string>& args, std::string* err);
128 bool ParseKeycodes(const std::vector<std::string>& args, std::string* err);
129 bool ParseOneshot(const std::vector<std::string>& args, std::string* err);
130 bool ParseOnrestart(const std::vector<std::string>& args, std::string* err);
Marco Nelissen310f6702016-07-22 12:07:06 -0700131 bool ParseOomScoreAdjust(const std::vector<std::string>& args, std::string* err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400132 bool ParseNamespace(const std::vector<std::string>& args, std::string* err);
133 bool ParseSeclabel(const std::vector<std::string>& args, std::string* err);
134 bool ParseSetenv(const std::vector<std::string>& args, std::string* err);
135 bool ParseSocket(const std::vector<std::string>& args, std::string* err);
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700136 bool ParseFile(const std::vector<std::string>& args, std::string* err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400137 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
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700140 template <typename T>
141 bool AddDescriptor(const std::vector<std::string>& args, std::string* err);
142
Tom Cherrybac32992015-07-31 12:45:25 -0700143 std::string name_;
Wei Wang641ff0a2017-03-27 10:59:11 -0700144 std::set<std::string> classnames_;
Viorel Suman70daa672016-03-21 10:08:07 +0200145 std::string console_;
Tom Cherrybac32992015-07-31 12:45:25 -0700146
147 unsigned flags_;
148 pid_t pid_;
James Hawkinse78ea772017-03-24 11:43:02 -0700149 android::base::boot_clock::time_point time_started_; // time of last start
150 android::base::boot_clock::time_point time_crashed_; // first crash within inspection window
Elliott Hughes9605a942016-11-10 17:43:47 -0800151 int crash_count_; // number of times crashed within window
Tom Cherrybac32992015-07-31 12:45:25 -0700152
153 uid_t uid_;
154 gid_t gid_;
155 std::vector<gid_t> supp_gids_;
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -0400156 CapSet capabilities_;
Jorge Lucangeli Obes1b3fa3d2016-04-21 15:35:09 -0700157 unsigned namespace_flags_;
Tom Cherrybac32992015-07-31 12:45:25 -0700158
159 std::string seclabel_;
160
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700161 std::vector<std::unique_ptr<DescriptorInfo>> descriptors_;
Tom Cherrybac32992015-07-31 12:45:25 -0700162 std::vector<ServiceEnvironmentInfo> envvars_;
163
164 Action onrestart_; // Commands to execute on restart.
165
166 std::vector<std::string> writepid_files_;
167
168 // keycodes for triggering this service via /dev/keychord
169 std::vector<int> keycodes_;
170 int keychord_id_;
171
172 IoSchedClass ioprio_class_;
173 int ioprio_pri_;
Vitalii Tomkiv081705c2016-05-18 17:36:30 -0700174 int priority_;
Tom Cherrybac32992015-07-31 12:45:25 -0700175
Marco Nelissen310f6702016-07-22 12:07:06 -0700176 int oom_score_adjust_;
177
Tom Cherrybac32992015-07-31 12:45:25 -0700178 std::vector<std::string> args_;
179};
180
181class ServiceManager {
182public:
183 static ServiceManager& GetInstance();
184
Tom Cherryb7349902015-08-26 11:43:36 -0700185 void AddService(std::unique_ptr<Service> service);
Tom Cherrybac32992015-07-31 12:45:25 -0700186 Service* MakeExecOneshotService(const std::vector<std::string>& args);
Tom Cherryb27004a2017-03-27 16:27:30 -0700187 bool Exec(const std::vector<std::string>& args);
188 bool ExecStart(const std::string& name);
189 bool IsWaitingForExec() const;
Tom Cherrybac32992015-07-31 12:45:25 -0700190 Service* FindServiceByName(const std::string& name) const;
191 Service* FindServiceByPid(pid_t pid) const;
192 Service* FindServiceByKeychord(int keychord_id) const;
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700193 void ForEachService(const std::function<void(Service*)>& callback) const;
Tom Cherrybac32992015-07-31 12:45:25 -0700194 void ForEachServiceInClass(const std::string& classname,
195 void (*func)(Service* svc)) const;
196 void ForEachServiceWithFlags(unsigned matchflags,
197 void (*func)(Service* svc)) const;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800198 void ReapAnyOutstandingChildren();
Tom Cherrybac32992015-07-31 12:45:25 -0700199 void RemoveService(const Service& svc);
200 void DumpState() const;
Tom Cherryb7349902015-08-26 11:43:36 -0700201
Tom Cherrybac32992015-07-31 12:45:25 -0700202private:
203 ServiceManager();
204
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800205 // Cleans up a child process that exited.
206 // Returns true iff a children was cleaned up.
207 bool ReapOneProcess();
208
Tom Cherrybac32992015-07-31 12:45:25 -0700209 static int exec_count_; // Every service needs a unique name.
Tom Cherryb27004a2017-03-27 16:27:30 -0700210 std::unique_ptr<Timer> exec_waiter_;
211
Tom Cherrybac32992015-07-31 12:45:25 -0700212 std::vector<std::unique_ptr<Service>> services_;
213};
214
Tom Cherryb7349902015-08-26 11:43:36 -0700215class ServiceParser : public SectionParser {
216public:
217 ServiceParser() : service_(nullptr) {
218 }
219 bool ParseSection(const std::vector<std::string>& args,
220 std::string* err) override;
221 bool ParseLineSection(const std::vector<std::string>& args,
222 const std::string& filename, int line,
223 std::string* err) const override;
224 void EndSection() override;
225 void EndFile(const std::string&) override {
226 }
227private:
228 bool IsValidName(const std::string& name) const;
229
230 std::unique_ptr<Service> service_;
231};
232
Tom Cherrybac32992015-07-31 12:45:25 -0700233#endif