| 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" | 
| Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 29 | #include "capabilities.h" | 
| Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 30 | #include "descriptors.h" | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 31 | #include "init_parser.h" | 
|  | 32 | #include "keyword_map.h" | 
| Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 33 | #include "util.h" | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 34 |  | 
|  | 35 | #define SVC_DISABLED       0x001  // do not autostart with class | 
|  | 36 | #define SVC_ONESHOT        0x002  // do not restart on exit | 
|  | 37 | #define SVC_RUNNING        0x004  // currently active | 
|  | 38 | #define SVC_RESTARTING     0x008  // waiting to restart | 
|  | 39 | #define SVC_CONSOLE        0x010  // requires console | 
|  | 40 | #define SVC_CRITICAL       0x020  // will reboot into recovery if keeps crashing | 
|  | 41 | #define SVC_RESET          0x040  // Use when stopping a process, | 
|  | 42 | // but not disabling so it can be restarted with its class. | 
|  | 43 | #define SVC_RC_DISABLED    0x080  // Remember if the disabled flag was set in the rc script. | 
|  | 44 | #define SVC_RESTART        0x100  // Use to safely restart (stop, wait, start) a service. | 
|  | 45 | #define SVC_DISABLED_START 0x200  // A start was requested but it was disabled at the time. | 
|  | 46 | #define SVC_EXEC           0x400  // This synthetic service corresponds to an 'exec'. | 
|  | 47 |  | 
|  | 48 | #define NR_SVC_SUPP_GIDS 12    // twelve supplementary groups | 
|  | 49 |  | 
|  | 50 | class Action; | 
|  | 51 | class ServiceManager; | 
|  | 52 |  | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 53 | struct ServiceEnvironmentInfo { | 
|  | 54 | ServiceEnvironmentInfo(); | 
|  | 55 | ServiceEnvironmentInfo(const std::string& name, const std::string& value); | 
|  | 56 | std::string name; | 
|  | 57 | std::string value; | 
|  | 58 | }; | 
|  | 59 |  | 
|  | 60 | class Service { | 
|  | 61 | public: | 
|  | 62 | Service(const std::string& name, const std::string& classname, | 
|  | 63 | const std::vector<std::string>& args); | 
|  | 64 |  | 
|  | 65 | Service(const std::string& name, const std::string& classname, | 
| Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 66 | unsigned flags, uid_t uid, gid_t gid, | 
| Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 67 | const std::vector<gid_t>& supp_gids, const CapSet& capabilities, | 
|  | 68 | unsigned namespace_flags, const std::string& seclabel, | 
|  | 69 | const std::vector<std::string>& args); | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 70 |  | 
| Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 71 | bool ParseLine(const std::vector<std::string>& args, std::string* err); | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 72 | bool Start(); | 
|  | 73 | bool StartIfNotDisabled(); | 
|  | 74 | bool Enable(); | 
|  | 75 | void Reset(); | 
|  | 76 | void Stop(); | 
| Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 77 | void Terminate(); | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 78 | void Restart(); | 
| Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 79 | void RestartIfNeeded(time_t* process_needs_restart_at); | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 80 | bool Reap(); | 
|  | 81 | void DumpState() const; | 
|  | 82 |  | 
|  | 83 | const std::string& name() const { return name_; } | 
|  | 84 | const std::string& classname() const { return classname_; } | 
|  | 85 | unsigned flags() const { return flags_; } | 
|  | 86 | pid_t pid() const { return pid_; } | 
|  | 87 | uid_t uid() const { return uid_; } | 
|  | 88 | gid_t gid() const { return gid_; } | 
| Vitalii Tomkiv | 081705c | 2016-05-18 17:36:30 -0700 | [diff] [blame] | 89 | int priority() const { return priority_; } | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 90 | const std::vector<gid_t>& supp_gids() const { return supp_gids_; } | 
|  | 91 | const std::string& seclabel() const { return seclabel_; } | 
|  | 92 | const std::vector<int>& keycodes() const { return keycodes_; } | 
|  | 93 | int keychord_id() const { return keychord_id_; } | 
|  | 94 | void set_keychord_id(int keychord_id) { keychord_id_ = keychord_id; } | 
|  | 95 | const std::vector<std::string>& args() const { return args_; } | 
|  | 96 |  | 
|  | 97 | private: | 
| Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 98 | using OptionParser = bool (Service::*) (const std::vector<std::string>& args, | 
|  | 99 | std::string* err); | 
|  | 100 | class OptionParserMap; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 101 |  | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 102 | void NotifyStateChange(const std::string& new_state) const; | 
|  | 103 | void StopOrReset(int how); | 
|  | 104 | void ZapStdio() const; | 
|  | 105 | void OpenConsole() const; | 
| Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 106 | void KillProcessGroup(int signal); | 
| Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 107 | void SetProcessAttributes(); | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 108 |  | 
| Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 109 | bool ParseCapabilities(const std::vector<std::string>& args, std::string *err); | 
| Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 110 | bool ParseClass(const std::vector<std::string>& args, std::string* err); | 
|  | 111 | bool ParseConsole(const std::vector<std::string>& args, std::string* err); | 
|  | 112 | bool ParseCritical(const std::vector<std::string>& args, std::string* err); | 
|  | 113 | bool ParseDisabled(const std::vector<std::string>& args, std::string* err); | 
|  | 114 | bool ParseGroup(const std::vector<std::string>& args, std::string* err); | 
|  | 115 | bool ParsePriority(const std::vector<std::string>& args, std::string* err); | 
|  | 116 | bool ParseIoprio(const std::vector<std::string>& args, std::string* err); | 
|  | 117 | bool ParseKeycodes(const std::vector<std::string>& args, std::string* err); | 
|  | 118 | bool ParseOneshot(const std::vector<std::string>& args, std::string* err); | 
|  | 119 | bool ParseOnrestart(const std::vector<std::string>& args, std::string* err); | 
| Marco Nelissen | 310f670 | 2016-07-22 12:07:06 -0700 | [diff] [blame] | 120 | bool ParseOomScoreAdjust(const std::vector<std::string>& args, std::string* err); | 
| Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 121 | bool ParseNamespace(const std::vector<std::string>& args, std::string* err); | 
|  | 122 | bool ParseSeclabel(const std::vector<std::string>& args, std::string* err); | 
|  | 123 | bool ParseSetenv(const std::vector<std::string>& args, std::string* err); | 
|  | 124 | bool ParseSocket(const std::vector<std::string>& args, std::string* err); | 
| Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 125 | bool ParseFile(const std::vector<std::string>& args, std::string* err); | 
| Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 126 | bool ParseUser(const std::vector<std::string>& args, std::string* err); | 
|  | 127 | bool ParseWritepid(const std::vector<std::string>& args, std::string* err); | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 128 |  | 
| Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 129 | template <typename T> | 
|  | 130 | bool AddDescriptor(const std::vector<std::string>& args, std::string* err); | 
|  | 131 |  | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 132 | std::string name_; | 
|  | 133 | std::string classname_; | 
| Viorel Suman | 70daa67 | 2016-03-21 10:08:07 +0200 | [diff] [blame] | 134 | std::string console_; | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 135 |  | 
|  | 136 | unsigned flags_; | 
|  | 137 | pid_t pid_; | 
| Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 138 | boot_clock::time_point time_started_; // time of last start | 
|  | 139 | boot_clock::time_point time_crashed_; // first crash within inspection window | 
|  | 140 | int crash_count_;                     // number of times crashed within window | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 141 |  | 
|  | 142 | uid_t uid_; | 
|  | 143 | gid_t gid_; | 
|  | 144 | std::vector<gid_t> supp_gids_; | 
| Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 145 | CapSet capabilities_; | 
| Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 146 | unsigned namespace_flags_; | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 147 |  | 
|  | 148 | std::string seclabel_; | 
|  | 149 |  | 
| Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 150 | std::vector<std::unique_ptr<DescriptorInfo>> descriptors_; | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 151 | std::vector<ServiceEnvironmentInfo> envvars_; | 
|  | 152 |  | 
|  | 153 | Action onrestart_;  // Commands to execute on restart. | 
|  | 154 |  | 
|  | 155 | std::vector<std::string> writepid_files_; | 
|  | 156 |  | 
|  | 157 | // keycodes for triggering this service via /dev/keychord | 
|  | 158 | std::vector<int> keycodes_; | 
|  | 159 | int keychord_id_; | 
|  | 160 |  | 
|  | 161 | IoSchedClass ioprio_class_; | 
|  | 162 | int ioprio_pri_; | 
| Vitalii Tomkiv | 081705c | 2016-05-18 17:36:30 -0700 | [diff] [blame] | 163 | int priority_; | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 164 |  | 
| Marco Nelissen | 310f670 | 2016-07-22 12:07:06 -0700 | [diff] [blame] | 165 | int oom_score_adjust_; | 
|  | 166 |  | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 167 | std::vector<std::string> args_; | 
|  | 168 | }; | 
|  | 169 |  | 
|  | 170 | class ServiceManager { | 
|  | 171 | public: | 
|  | 172 | static ServiceManager& GetInstance(); | 
|  | 173 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 174 | void AddService(std::unique_ptr<Service> service); | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 175 | Service* MakeExecOneshotService(const std::vector<std::string>& args); | 
|  | 176 | Service* FindServiceByName(const std::string& name) const; | 
|  | 177 | Service* FindServiceByPid(pid_t pid) const; | 
|  | 178 | Service* FindServiceByKeychord(int keychord_id) const; | 
| Chih-Hung Hsieh | 8f7b9e3 | 2016-07-27 16:25:51 -0700 | [diff] [blame] | 179 | void ForEachService(const std::function<void(Service*)>& callback) const; | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 180 | void ForEachServiceInClass(const std::string& classname, | 
|  | 181 | void (*func)(Service* svc)) const; | 
|  | 182 | void ForEachServiceWithFlags(unsigned matchflags, | 
|  | 183 | void (*func)(Service* svc)) const; | 
| Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 184 | void ReapAnyOutstandingChildren(); | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 185 | void RemoveService(const Service& svc); | 
|  | 186 | void DumpState() const; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 187 |  | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 188 | private: | 
|  | 189 | ServiceManager(); | 
|  | 190 |  | 
| Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 191 | // Cleans up a child process that exited. | 
|  | 192 | // Returns true iff a children was cleaned up. | 
|  | 193 | bool ReapOneProcess(); | 
|  | 194 |  | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 195 | static int exec_count_; // Every service needs a unique name. | 
|  | 196 | std::vector<std::unique_ptr<Service>> services_; | 
|  | 197 | }; | 
|  | 198 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 199 | class ServiceParser : public SectionParser { | 
|  | 200 | public: | 
|  | 201 | ServiceParser() : service_(nullptr) { | 
|  | 202 | } | 
|  | 203 | bool ParseSection(const std::vector<std::string>& args, | 
|  | 204 | std::string* err) override; | 
|  | 205 | bool ParseLineSection(const std::vector<std::string>& args, | 
|  | 206 | const std::string& filename, int line, | 
|  | 207 | std::string* err) const override; | 
|  | 208 | void EndSection() override; | 
|  | 209 | void EndFile(const std::string&) override { | 
|  | 210 | } | 
|  | 211 | private: | 
|  | 212 | bool IsValidName(const std::string& name) const; | 
|  | 213 |  | 
|  | 214 | std::unique_ptr<Service> service_; | 
|  | 215 | }; | 
|  | 216 |  | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 217 | #endif |