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