| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 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" | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 29 | #include "init_parser.h" | 
 | 30 | #include "keyword_map.h" | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 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 |  | 
 | 47 | class Action; | 
 | 48 | class ServiceManager; | 
 | 49 |  | 
 | 50 | struct 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 |  | 
 | 62 | struct ServiceEnvironmentInfo { | 
 | 63 |     ServiceEnvironmentInfo(); | 
 | 64 |     ServiceEnvironmentInfo(const std::string& name, const std::string& value); | 
 | 65 |     std::string name; | 
 | 66 |     std::string value; | 
 | 67 | }; | 
 | 68 |  | 
 | 69 | class Service { | 
 | 70 | public: | 
 | 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 Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 78 |     bool HandleLine(const std::vector<std::string>& args, std::string* err); | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 79 |     bool Start(const std::vector<std::string>& dynamic_args); | 
 | 80 |     bool Start(); | 
 | 81 |     bool StartIfNotDisabled(); | 
 | 82 |     bool Enable(); | 
 | 83 |     void Reset(); | 
 | 84 |     void Stop(); | 
| Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 85 |     void Terminate(); | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 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 |     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 |  | 
 | 104 | private: | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 105 |     using OptionHandler = bool (Service::*) (const std::vector<std::string>& args, | 
 | 106 |                                              std::string* err); | 
 | 107 |     class OptionHandlerMap; | 
 | 108 |  | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 109 |     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; | 
 | 114 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 115 |     bool HandleClass(const std::vector<std::string>& args, std::string* err); | 
 | 116 |     bool HandleConsole(const std::vector<std::string>& args, std::string* err); | 
 | 117 |     bool HandleCritical(const std::vector<std::string>& args, std::string* err); | 
 | 118 |     bool HandleDisabled(const std::vector<std::string>& args, std::string* err); | 
 | 119 |     bool HandleGroup(const std::vector<std::string>& args, std::string* err); | 
 | 120 |     bool HandleIoprio(const std::vector<std::string>& args, std::string* err); | 
 | 121 |     bool HandleKeycodes(const std::vector<std::string>& args, std::string* err); | 
 | 122 |     bool HandleOneshot(const std::vector<std::string>& args, std::string* err); | 
 | 123 |     bool HandleOnrestart(const std::vector<std::string>& args, std::string* err); | 
 | 124 |     bool HandleSeclabel(const std::vector<std::string>& args, std::string* err); | 
 | 125 |     bool HandleSetenv(const std::vector<std::string>& args, std::string* err); | 
 | 126 |     bool HandleSocket(const std::vector<std::string>& args, std::string* err); | 
 | 127 |     bool HandleUser(const std::vector<std::string>& args, std::string* err); | 
 | 128 |     bool HandleWritepid(const std::vector<std::string>& args, std::string* err); | 
 | 129 |  | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 130 |     std::string name_; | 
 | 131 |     std::string classname_; | 
 | 132 |  | 
 | 133 |     unsigned flags_; | 
 | 134 |     pid_t pid_; | 
 | 135 |     time_t time_started_;    // time of last start | 
 | 136 |     time_t time_crashed_;    // first crash within inspection window | 
 | 137 |     int nr_crashed_;         // number of times crashed within window | 
 | 138 |  | 
 | 139 |     uid_t uid_; | 
 | 140 |     gid_t gid_; | 
 | 141 |     std::vector<gid_t> supp_gids_; | 
 | 142 |  | 
 | 143 |     std::string seclabel_; | 
 | 144 |  | 
 | 145 |     std::vector<SocketInfo> sockets_; | 
 | 146 |     std::vector<ServiceEnvironmentInfo> envvars_; | 
 | 147 |  | 
 | 148 |     Action onrestart_;  // Commands to execute on restart. | 
 | 149 |  | 
 | 150 |     std::vector<std::string> writepid_files_; | 
 | 151 |  | 
 | 152 |     // keycodes for triggering this service via /dev/keychord | 
 | 153 |     std::vector<int> keycodes_; | 
 | 154 |     int keychord_id_; | 
 | 155 |  | 
 | 156 |     IoSchedClass ioprio_class_; | 
 | 157 |     int ioprio_pri_; | 
 | 158 |  | 
 | 159 |     std::vector<std::string> args_; | 
 | 160 | }; | 
 | 161 |  | 
 | 162 | class ServiceManager { | 
 | 163 | public: | 
 | 164 |     static ServiceManager& GetInstance(); | 
 | 165 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 166 |     void AddService(std::unique_ptr<Service> service); | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 167 |     Service* MakeExecOneshotService(const std::vector<std::string>& args); | 
 | 168 |     Service* FindServiceByName(const std::string& name) const; | 
 | 169 |     Service* FindServiceByPid(pid_t pid) const; | 
 | 170 |     Service* FindServiceByKeychord(int keychord_id) const; | 
| Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 171 |     void ForEachService(std::function<void(Service*)> callback) const; | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 172 |     void ForEachServiceInClass(const std::string& classname, | 
 | 173 |                                void (*func)(Service* svc)) const; | 
 | 174 |     void ForEachServiceWithFlags(unsigned matchflags, | 
 | 175 |                              void (*func)(Service* svc)) const; | 
| Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 176 |     void ReapAnyOutstandingChildren(); | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 177 |     void RemoveService(const Service& svc); | 
 | 178 |     void DumpState() const; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 179 |  | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 180 | private: | 
 | 181 |     ServiceManager(); | 
 | 182 |  | 
| Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 183 |     // Cleans up a child process that exited. | 
 | 184 |     // Returns true iff a children was cleaned up. | 
 | 185 |     bool ReapOneProcess(); | 
 | 186 |  | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 187 |     static int exec_count_; // Every service needs a unique name. | 
 | 188 |     std::vector<std::unique_ptr<Service>> services_; | 
 | 189 | }; | 
 | 190 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 191 | class ServiceParser : public SectionParser { | 
 | 192 | public: | 
 | 193 |     ServiceParser() : service_(nullptr) { | 
 | 194 |     } | 
 | 195 |     bool ParseSection(const std::vector<std::string>& args, | 
 | 196 |                       std::string* err) override; | 
 | 197 |     bool ParseLineSection(const std::vector<std::string>& args, | 
 | 198 |                           const std::string& filename, int line, | 
 | 199 |                           std::string* err) const override; | 
 | 200 |     void EndSection() override; | 
 | 201 |     void EndFile(const std::string&) override { | 
 | 202 |     } | 
 | 203 | private: | 
 | 204 |     bool IsValidName(const std::string& name) const; | 
 | 205 |  | 
 | 206 |     std::unique_ptr<Service> service_; | 
 | 207 | }; | 
 | 208 |  | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 209 | #endif |