service.h revision 177b27d4f5bfa498cc46aad24d9375d65630bea0
1/*
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"
29#include "init_parser.h"
30#include "keyword_map.h"
31
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,
76            const std::vector<gid_t>& supp_gids, unsigned namespace_flags,
77            const std::string& seclabel, const std::vector<std::string>& args);
78
79    bool ParseLine(const std::vector<std::string>& args, std::string* err);
80    bool Start();
81    bool StartIfNotDisabled();
82    bool Enable();
83    void Reset();
84    void Stop();
85    void Terminate();
86    void Restart();
87    void RestartIfNeeded(time_t& process_needs_restart);
88    bool Reap();
89    void DumpState() const;
90
91    const std::string& name() const { return name_; }
92    const std::string& classname() const { return classname_; }
93    unsigned flags() const { return flags_; }
94    pid_t pid() const { return pid_; }
95    uid_t uid() const { return uid_; }
96    gid_t gid() const { return gid_; }
97    int priority() const { return priority_; }
98    const std::vector<gid_t>& supp_gids() const { return supp_gids_; }
99    const std::string& seclabel() const { return seclabel_; }
100    const std::vector<int>& keycodes() const { return keycodes_; }
101    int keychord_id() const { return keychord_id_; }
102    void set_keychord_id(int keychord_id) { keychord_id_ = keychord_id; }
103    const std::vector<std::string>& args() const { return args_; }
104
105private:
106    using OptionParser = bool (Service::*) (const std::vector<std::string>& args,
107                                            std::string* err);
108    class OptionParserMap;
109
110    void NotifyStateChange(const std::string& new_state) const;
111    void StopOrReset(int how);
112    void ZapStdio() const;
113    void OpenConsole() const;
114    void PublishSocket(const std::string& name, int fd) const;
115    void KillProcessGroup(int signal);
116
117    bool ParseClass(const std::vector<std::string>& args, std::string* err);
118    bool ParseConsole(const std::vector<std::string>& args, std::string* err);
119    bool ParseCritical(const std::vector<std::string>& args, std::string* err);
120    bool ParseDisabled(const std::vector<std::string>& args, std::string* err);
121    bool ParseGroup(const std::vector<std::string>& args, std::string* err);
122    bool ParsePriority(const std::vector<std::string>& args, std::string* err);
123    bool ParseIoprio(const std::vector<std::string>& args, std::string* err);
124    bool ParseKeycodes(const std::vector<std::string>& args, std::string* err);
125    bool ParseOneshot(const std::vector<std::string>& args, std::string* err);
126    bool ParseOnrestart(const std::vector<std::string>& args, std::string* err);
127    bool ParseNamespace(const std::vector<std::string>& args, std::string* err);
128    bool ParseSeclabel(const std::vector<std::string>& args, std::string* err);
129    bool ParseSetenv(const std::vector<std::string>& args, std::string* err);
130    bool ParseSocket(const std::vector<std::string>& args, std::string* err);
131    bool ParseUser(const std::vector<std::string>& args, std::string* err);
132    bool ParseWritepid(const std::vector<std::string>& args, std::string* err);
133
134    std::string name_;
135    std::string classname_;
136    std::string console_;
137
138    unsigned flags_;
139    pid_t pid_;
140    time_t time_started_;    // time of last start
141    time_t time_crashed_;    // first crash within inspection window
142    int nr_crashed_;         // number of times crashed within window
143
144    uid_t uid_;
145    gid_t gid_;
146    std::vector<gid_t> supp_gids_;
147    unsigned namespace_flags_;
148
149    std::string seclabel_;
150
151    std::vector<SocketInfo> sockets_;
152    std::vector<ServiceEnvironmentInfo> envvars_;
153
154    Action onrestart_;  // Commands to execute on restart.
155
156    std::vector<std::string> writepid_files_;
157
158    // keycodes for triggering this service via /dev/keychord
159    std::vector<int> keycodes_;
160    int keychord_id_;
161
162    IoSchedClass ioprio_class_;
163    int ioprio_pri_;
164    int priority_;
165
166    std::vector<std::string> args_;
167};
168
169class ServiceManager {
170public:
171    static ServiceManager& GetInstance();
172
173    void AddService(std::unique_ptr<Service> service);
174    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;
178    void ForEachService(std::function<void(Service*)> callback) const;
179    void ForEachServiceInClass(const std::string& classname,
180                               void (*func)(Service* svc)) const;
181    void ForEachServiceWithFlags(unsigned matchflags,
182                             void (*func)(Service* svc)) const;
183    void ReapAnyOutstandingChildren();
184    void RemoveService(const Service& svc);
185    void DumpState() const;
186
187private:
188    ServiceManager();
189
190    // Cleans up a child process that exited.
191    // Returns true iff a children was cleaned up.
192    bool ReapOneProcess();
193
194    static int exec_count_; // Every service needs a unique name.
195    std::vector<std::unique_ptr<Service>> services_;
196};
197
198class 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
216#endif
217