blob: f93ac381e1d93789040376ce50a9c8b14e4bb541 [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
29#include "action.h"
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -040030#include "capabilities.h"
Mark Salyzyn62767fe2016-10-27 07:45:34 -070031#include "descriptors.h"
Tom Cherryb7349902015-08-26 11:43:36 -070032#include "init_parser.h"
33#include "keyword_map.h"
Elliott Hughes9605a942016-11-10 17:43:47 -080034#include "util.h"
Tom Cherrybac32992015-07-31 12:45:25 -070035
Keun-young Park8d01f632017-03-13 11:54:47 -070036#define SVC_DISABLED 0x001 // do not autostart with class
37#define SVC_ONESHOT 0x002 // do not restart on exit
38#define SVC_RUNNING 0x004 // currently active
39#define SVC_RESTARTING 0x008 // waiting to restart
40#define SVC_CONSOLE 0x010 // requires console
41#define SVC_CRITICAL 0x020 // will reboot into recovery if keeps crashing
42#define SVC_RESET 0x040 // Use when stopping a process,
Tom Cherrybac32992015-07-31 12:45:25 -070043 // but not disabling so it can be restarted with its class.
Keun-young Park8d01f632017-03-13 11:54:47 -070044#define SVC_RC_DISABLED 0x080 // Remember if the disabled flag was set in the rc script.
45#define SVC_RESTART 0x100 // Use to safely restart (stop, wait, start) a service.
Tom Cherrybac32992015-07-31 12:45:25 -070046#define SVC_DISABLED_START 0x200 // A start was requested but it was disabled at the time.
Tom Cherry2d804672017-03-27 16:27:30 -070047#define SVC_EXEC 0x400 // This service was started by either 'exec' or 'exec_start' and stops
48 // init from processing more commands until it completes
Keun-young Park8d01f632017-03-13 11:54:47 -070049
50#define SVC_SHUTDOWN_CRITICAL 0x800 // This service is critical for shutdown and
51 // should not be killed during shutdown
Tom Cherry2d804672017-03-27 16:27:30 -070052#define SVC_TEMPORARY 0x1000 // This service was started by 'exec' and should be removed from the
53 // service list once it is reaped.
Tom Cherrybac32992015-07-31 12:45:25 -070054
55#define NR_SVC_SUPP_GIDS 12 // twelve supplementary groups
56
57class Action;
58class ServiceManager;
59
Tom Cherrybac32992015-07-31 12:45:25 -070060struct ServiceEnvironmentInfo {
61 ServiceEnvironmentInfo();
62 ServiceEnvironmentInfo(const std::string& name, const std::string& value);
63 std::string name;
64 std::string value;
65};
66
67class Service {
Wei Wang641ff0a2017-03-27 10:59:11 -070068 public:
69 Service(const std::string& name, const std::vector<std::string>& args);
Tom Cherrybac32992015-07-31 12:45:25 -070070
Wei Wang641ff0a2017-03-27 10:59:11 -070071 Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid,
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -040072 const std::vector<gid_t>& supp_gids, const CapSet& capabilities,
73 unsigned namespace_flags, const std::string& seclabel,
74 const std::vector<std::string>& args);
Tom Cherrybac32992015-07-31 12:45:25 -070075
Keun-young Park8d01f632017-03-13 11:54:47 -070076 bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; }
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -040077 bool ParseLine(const std::vector<std::string>& args, std::string* err);
Tom Cherry2d804672017-03-27 16:27:30 -070078 bool ExecStart(std::unique_ptr<Timer>* exec_waiter);
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();
Elliott Hughes9605a942016-11-10 17:43:47 -080086 void RestartIfNeeded(time_t* process_needs_restart_at);
Tom Cherry2d804672017-03-27 16:27:30 -070087 void Reap();
Tom Cherrybac32992015-07-31 12:45:25 -070088 void DumpState() const;
Keun-young Park8d01f632017-03-13 11:54:47 -070089 void SetShutdownCritical() { flags_ |= SVC_SHUTDOWN_CRITICAL; }
Wei Wang641ff0a2017-03-27 10:59:11 -070090 bool IsShutdownCritical() const { return (flags_ & SVC_SHUTDOWN_CRITICAL) != 0; }
Tom Cherrybac32992015-07-31 12:45:25 -070091
92 const std::string& name() const { return name_; }
Wei Wang641ff0a2017-03-27 10:59:11 -070093 const std::set<std::string>& classnames() const { return classnames_; }
Tom Cherrybac32992015-07-31 12:45:25 -070094 unsigned flags() const { return flags_; }
95 pid_t pid() const { return pid_; }
Tom Cherry3ac3c022017-05-01 14:16:41 -070096 int crash_count() const { return crash_count_; }
Tom Cherrybac32992015-07-31 12:45:25 -070097 uid_t uid() const { return uid_; }
98 gid_t gid() const { return gid_; }
Tom Cherry3ac3c022017-05-01 14:16:41 -070099 unsigned namespace_flags() const { return namespace_flags_; }
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; }
Tom Cherry3ac3c022017-05-01 14:16:41 -0700105 IoSchedClass ioprio_class() const { return ioprio_class_; }
106 int ioprio_pri() const { return ioprio_pri_; }
107 int priority() const { return priority_; }
108 int oom_score_adjust() const { return oom_score_adjust_; }
Tom Cherrybac32992015-07-31 12:45:25 -0700109 const std::vector<std::string>& args() const { return args_; }
110
Wei Wang641ff0a2017-03-27 10:59:11 -0700111 private:
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400112 using OptionParser = bool (Service::*) (const std::vector<std::string>& args,
113 std::string* err);
114 class OptionParserMap;
Tom Cherryb7349902015-08-26 11:43:36 -0700115
Tom Cherrybac32992015-07-31 12:45:25 -0700116 void NotifyStateChange(const std::string& new_state) const;
117 void StopOrReset(int how);
118 void ZapStdio() const;
119 void OpenConsole() const;
Elliott Hughesad8e94e2016-06-15 14:49:57 -0700120 void KillProcessGroup(int signal);
Jorge Lucangeli Obes344d01f2016-07-08 13:32:26 -0400121 void SetProcessAttributes();
Tom Cherrybac32992015-07-31 12:45:25 -0700122
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -0400123 bool ParseCapabilities(const std::vector<std::string>& args, std::string *err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400124 bool ParseClass(const std::vector<std::string>& args, std::string* err);
125 bool ParseConsole(const std::vector<std::string>& args, std::string* err);
126 bool ParseCritical(const std::vector<std::string>& args, std::string* err);
127 bool ParseDisabled(const std::vector<std::string>& args, std::string* err);
128 bool ParseGroup(const std::vector<std::string>& args, std::string* err);
129 bool ParsePriority(const std::vector<std::string>& args, std::string* err);
130 bool ParseIoprio(const std::vector<std::string>& args, std::string* err);
131 bool ParseKeycodes(const std::vector<std::string>& args, std::string* err);
132 bool ParseOneshot(const std::vector<std::string>& args, std::string* err);
133 bool ParseOnrestart(const std::vector<std::string>& args, std::string* err);
Marco Nelissen310f6702016-07-22 12:07:06 -0700134 bool ParseOomScoreAdjust(const std::vector<std::string>& args, std::string* err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400135 bool ParseNamespace(const std::vector<std::string>& args, std::string* err);
136 bool ParseSeclabel(const std::vector<std::string>& args, std::string* err);
137 bool ParseSetenv(const std::vector<std::string>& args, std::string* err);
138 bool ParseSocket(const std::vector<std::string>& args, std::string* err);
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700139 bool ParseFile(const std::vector<std::string>& args, std::string* err);
Jorge Lucangeli Obes177b27d2016-06-29 14:32:49 -0400140 bool ParseUser(const std::vector<std::string>& args, std::string* err);
141 bool ParseWritepid(const std::vector<std::string>& args, std::string* err);
Tom Cherryb7349902015-08-26 11:43:36 -0700142
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700143 template <typename T>
144 bool AddDescriptor(const std::vector<std::string>& args, std::string* err);
145
Tom Cherrybac32992015-07-31 12:45:25 -0700146 std::string name_;
Wei Wang641ff0a2017-03-27 10:59:11 -0700147 std::set<std::string> classnames_;
Viorel Suman70daa672016-03-21 10:08:07 +0200148 std::string console_;
Tom Cherrybac32992015-07-31 12:45:25 -0700149
150 unsigned flags_;
151 pid_t pid_;
James Hawkinsc8ac0672017-02-14 19:20:20 +0000152 boot_clock::time_point time_started_; // time of last start
153 boot_clock::time_point time_crashed_; // first crash within inspection window
Elliott Hughes9605a942016-11-10 17:43:47 -0800154 int crash_count_; // number of times crashed within window
Tom Cherrybac32992015-07-31 12:45:25 -0700155
156 uid_t uid_;
157 gid_t gid_;
158 std::vector<gid_t> supp_gids_;
Jorge Lucangeli Obes24b29132016-10-27 10:33:03 -0400159 CapSet capabilities_;
Jorge Lucangeli Obes1b3fa3d2016-04-21 15:35:09 -0700160 unsigned namespace_flags_;
Tom Cherrybac32992015-07-31 12:45:25 -0700161
162 std::string seclabel_;
163
Mark Salyzyn62767fe2016-10-27 07:45:34 -0700164 std::vector<std::unique_ptr<DescriptorInfo>> descriptors_;
Tom Cherrybac32992015-07-31 12:45:25 -0700165 std::vector<ServiceEnvironmentInfo> envvars_;
166
167 Action onrestart_; // Commands to execute on restart.
168
169 std::vector<std::string> writepid_files_;
170
171 // keycodes for triggering this service via /dev/keychord
172 std::vector<int> keycodes_;
173 int keychord_id_;
174
175 IoSchedClass ioprio_class_;
176 int ioprio_pri_;
Vitalii Tomkiv081705c2016-05-18 17:36:30 -0700177 int priority_;
Tom Cherrybac32992015-07-31 12:45:25 -0700178
Marco Nelissen310f6702016-07-22 12:07:06 -0700179 int oom_score_adjust_;
180
Tom Cherrybac32992015-07-31 12:45:25 -0700181 std::vector<std::string> args_;
182};
183
184class ServiceManager {
185public:
186 static ServiceManager& GetInstance();
187
Tom Cherry3ac3c022017-05-01 14:16:41 -0700188 // Exposed for testing
189 ServiceManager();
190
Tom Cherryb7349902015-08-26 11:43:36 -0700191 void AddService(std::unique_ptr<Service> service);
Tom Cherrybac32992015-07-31 12:45:25 -0700192 Service* MakeExecOneshotService(const std::vector<std::string>& args);
Tom Cherry2d804672017-03-27 16:27:30 -0700193 bool Exec(const std::vector<std::string>& args);
194 bool ExecStart(const std::string& name);
195 bool IsWaitingForExec() const;
Tom Cherrybac32992015-07-31 12:45:25 -0700196 Service* FindServiceByName(const std::string& name) const;
197 Service* FindServiceByPid(pid_t pid) const;
198 Service* FindServiceByKeychord(int keychord_id) const;
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700199 void ForEachService(const std::function<void(Service*)>& callback) const;
Tom Cherrybac32992015-07-31 12:45:25 -0700200 void ForEachServiceInClass(const std::string& classname,
201 void (*func)(Service* svc)) const;
202 void ForEachServiceWithFlags(unsigned matchflags,
203 void (*func)(Service* svc)) const;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800204 void ReapAnyOutstandingChildren();
Tom Cherrybac32992015-07-31 12:45:25 -0700205 void RemoveService(const Service& svc);
206 void DumpState() const;
Tom Cherryb7349902015-08-26 11:43:36 -0700207
Tom Cherrybac32992015-07-31 12:45:25 -0700208private:
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800209 // Cleans up a child process that exited.
210 // Returns true iff a children was cleaned up.
211 bool ReapOneProcess();
212
Tom Cherrybac32992015-07-31 12:45:25 -0700213 static int exec_count_; // Every service needs a unique name.
Tom Cherry2d804672017-03-27 16:27:30 -0700214 std::unique_ptr<Timer> exec_waiter_;
215
Tom Cherrybac32992015-07-31 12:45:25 -0700216 std::vector<std::unique_ptr<Service>> services_;
217};
218
Tom Cherryb7349902015-08-26 11:43:36 -0700219class ServiceParser : public SectionParser {
220public:
221 ServiceParser() : service_(nullptr) {
222 }
223 bool ParseSection(const std::vector<std::string>& args,
224 std::string* err) override;
225 bool ParseLineSection(const std::vector<std::string>& args,
226 const std::string& filename, int line,
227 std::string* err) const override;
228 void EndSection() override;
229 void EndFile(const std::string&) override {
230 }
231private:
232 bool IsValidName(const std::string& name) const;
233
234 std::unique_ptr<Service> service_;
235};
236
Tom Cherrybac32992015-07-31 12:45:25 -0700237#endif