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 | |
Paul Crowley | c73b215 | 2018-04-13 17:38:57 +0000 | [diff] [blame] | 20 | #include <signal.h> |
Tom Cherry | 7ac013d | 2017-08-25 10:39:25 -0700 | [diff] [blame] | 21 | #include <sys/resource.h> |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | |
Tom Cherry | 73f535e | 2018-09-27 16:10:46 -0700 | [diff] [blame] | 24 | #include <chrono> |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 25 | #include <memory> |
Tom Cherry | 73f535e | 2018-09-27 16:10:46 -0700 | [diff] [blame] | 26 | #include <optional> |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 27 | #include <set> |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 28 | #include <string> |
| 29 | #include <vector> |
| 30 | |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 31 | #include <android-base/chrono_utils.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 32 | #include <cutils/iosched_policy.h> |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 33 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 34 | #include "action.h" |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 35 | #include "capabilities.h" |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 36 | #include "descriptors.h" |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 37 | #include "keyword_map.h" |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 38 | #include "parser.h" |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 39 | #include "subcontext.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 40 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 41 | #define SVC_DISABLED 0x001 // do not autostart with class |
| 42 | #define SVC_ONESHOT 0x002 // do not restart on exit |
| 43 | #define SVC_RUNNING 0x004 // currently active |
| 44 | #define SVC_RESTARTING 0x008 // waiting to restart |
| 45 | #define SVC_CONSOLE 0x010 // requires console |
Tom Cherry | ad9e7ea | 2018-10-15 17:21:48 -0700 | [diff] [blame] | 46 | #define SVC_CRITICAL 0x020 // will reboot into bootloader if keeps crashing |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 47 | #define SVC_RESET 0x040 // Use when stopping a process, |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 48 | // but not disabling so it can be restarted with its class. |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 49 | #define SVC_RC_DISABLED 0x080 // Remember if the disabled flag was set in the rc script. |
| 50 | #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] | 51 | #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] | 52 | #define SVC_EXEC 0x400 // This service was started by either 'exec' or 'exec_start' and stops |
| 53 | // init from processing more commands until it completes |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 54 | |
| 55 | #define SVC_SHUTDOWN_CRITICAL 0x800 // This service is critical for shutdown and |
| 56 | // should not be killed during shutdown |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 57 | #define SVC_TEMPORARY 0x1000 // This service was started by 'exec' and should be removed from the |
| 58 | // service list once it is reaped. |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 59 | |
| 60 | #define NR_SVC_SUPP_GIDS 12 // twelve supplementary groups |
| 61 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 62 | namespace android { |
| 63 | namespace init { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 64 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 65 | class Service { |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 66 | public: |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 67 | Service(const std::string& name, Subcontext* subcontext_for_restart_commands, |
| 68 | const std::vector<std::string>& args); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 69 | |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 70 | 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] | 71 | const std::vector<gid_t>& supp_gids, const CapSet& capabilities, |
| 72 | unsigned namespace_flags, const std::string& seclabel, |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 73 | Subcontext* subcontext_for_restart_commands, const std::vector<std::string>& args); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 74 | |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 75 | static std::unique_ptr<Service> MakeTemporaryOneshotService(const std::vector<std::string>& args); |
| 76 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 77 | bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; } |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 78 | Result<Success> ParseLine(std::vector<std::string>&& args); |
Tom Cherry | 76af7e6 | 2017-08-22 16:13:59 -0700 | [diff] [blame] | 79 | Result<Success> ExecStart(); |
| 80 | Result<Success> Start(); |
| 81 | Result<Success> StartIfNotDisabled(); |
| 82 | Result<Success> Enable(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 83 | void Reset(); |
| 84 | void Stop(); |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 85 | void Terminate(); |
Tom Cherry | 73f535e | 2018-09-27 16:10:46 -0700 | [diff] [blame] | 86 | void Timeout(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 87 | void Restart(); |
Paul Crowley | c73b215 | 2018-04-13 17:38:57 +0000 | [diff] [blame] | 88 | void Reap(const siginfo_t& siginfo); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 89 | void DumpState() const; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 90 | void SetShutdownCritical() { flags_ |= SVC_SHUTDOWN_CRITICAL; } |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 91 | bool IsShutdownCritical() const { return (flags_ & SVC_SHUTDOWN_CRITICAL) != 0; } |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 92 | void UnSetExec() { |
| 93 | is_exec_service_running_ = false; |
| 94 | flags_ &= ~SVC_EXEC; |
| 95 | } |
Paul Crowley | c73b215 | 2018-04-13 17:38:57 +0000 | [diff] [blame] | 96 | void AddReapCallback(std::function<void(const siginfo_t& siginfo)> callback) { |
| 97 | reap_callbacks_.emplace_back(std::move(callback)); |
| 98 | } |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 99 | |
| 100 | static bool is_exec_service_running() { return is_exec_service_running_; } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 101 | |
| 102 | const std::string& name() const { return name_; } |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 103 | const std::set<std::string>& classnames() const { return classnames_; } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 104 | unsigned flags() const { return flags_; } |
| 105 | pid_t pid() const { return pid_; } |
Tom Cherry | d269e3a | 2017-07-31 13:23:18 -0700 | [diff] [blame] | 106 | android::base::boot_clock::time_point time_started() const { return time_started_; } |
Tom Cherry | 7da5485 | 2017-05-01 14:16:41 -0700 | [diff] [blame] | 107 | int crash_count() const { return crash_count_; } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 108 | uid_t uid() const { return uid_; } |
| 109 | gid_t gid() const { return gid_; } |
Tom Cherry | 7da5485 | 2017-05-01 14:16:41 -0700 | [diff] [blame] | 110 | unsigned namespace_flags() const { return namespace_flags_; } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 111 | const std::vector<gid_t>& supp_gids() const { return supp_gids_; } |
| 112 | const std::string& seclabel() const { return seclabel_; } |
| 113 | const std::vector<int>& keycodes() const { return keycodes_; } |
Tom Cherry | 7da5485 | 2017-05-01 14:16:41 -0700 | [diff] [blame] | 114 | IoSchedClass ioprio_class() const { return ioprio_class_; } |
| 115 | int ioprio_pri() const { return ioprio_pri_; } |
Steven Moreland | e055d73 | 2017-10-05 18:50:22 -0700 | [diff] [blame] | 116 | const std::set<std::string>& interfaces() const { return interfaces_; } |
Tom Cherry | 7da5485 | 2017-05-01 14:16:41 -0700 | [diff] [blame] | 117 | int priority() const { return priority_; } |
| 118 | int oom_score_adjust() const { return oom_score_adjust_; } |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 119 | bool is_override() const { return override_; } |
Tom Cherry | 33838b1 | 2017-05-04 11:32:36 -0700 | [diff] [blame] | 120 | bool process_cgroup_empty() const { return process_cgroup_empty_; } |
Tom Cherry | 5938379 | 2017-07-26 16:09:09 -0700 | [diff] [blame] | 121 | unsigned long start_order() const { return start_order_; } |
Tom Cherry | 8f38048 | 2018-04-17 14:48:44 -0700 | [diff] [blame] | 122 | void set_sigstop(bool value) { sigstop_ = value; } |
Tom Cherry | 73f535e | 2018-09-27 16:10:46 -0700 | [diff] [blame] | 123 | std::chrono::seconds restart_period() const { return restart_period_; } |
| 124 | std::optional<std::chrono::seconds> timeout_period() const { return timeout_period_; } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 125 | const std::vector<std::string>& args() const { return args_; } |
Jiyong Park | 80aa447 | 2018-11-12 12:08:41 +0900 | [diff] [blame] | 126 | bool is_updatable() const { return updatable_; } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 127 | |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 128 | private: |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 129 | using OptionParser = Result<Success> (Service::*)(std::vector<std::string>&& args); |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 130 | class OptionParserMap; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 131 | |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 132 | Result<Success> SetUpMountNamespace() const; |
| 133 | Result<Success> SetUpPidNamespace() const; |
| 134 | Result<Success> EnterNamespaces() const; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 135 | void NotifyStateChange(const std::string& new_state) const; |
| 136 | void StopOrReset(int how); |
| 137 | void ZapStdio() const; |
| 138 | void OpenConsole() const; |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 139 | void KillProcessGroup(int signal); |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 140 | void SetProcessAttributes(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 141 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 142 | Result<Success> ParseCapabilities(std::vector<std::string>&& args); |
| 143 | Result<Success> ParseClass(std::vector<std::string>&& args); |
| 144 | Result<Success> ParseConsole(std::vector<std::string>&& args); |
| 145 | Result<Success> ParseCritical(std::vector<std::string>&& args); |
| 146 | Result<Success> ParseDisabled(std::vector<std::string>&& args); |
| 147 | Result<Success> ParseEnterNamespace(std::vector<std::string>&& args); |
| 148 | Result<Success> ParseGroup(std::vector<std::string>&& args); |
| 149 | Result<Success> ParsePriority(std::vector<std::string>&& args); |
| 150 | Result<Success> ParseInterface(std::vector<std::string>&& args); |
| 151 | Result<Success> ParseIoprio(std::vector<std::string>&& args); |
| 152 | Result<Success> ParseKeycodes(std::vector<std::string>&& args); |
| 153 | Result<Success> ParseOneshot(std::vector<std::string>&& args); |
| 154 | Result<Success> ParseOnrestart(std::vector<std::string>&& args); |
| 155 | Result<Success> ParseOomScoreAdjust(std::vector<std::string>&& args); |
| 156 | Result<Success> ParseOverride(std::vector<std::string>&& args); |
| 157 | Result<Success> ParseMemcgLimitInBytes(std::vector<std::string>&& args); |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 158 | Result<Success> ParseMemcgLimitPercent(std::vector<std::string>&& args); |
| 159 | Result<Success> ParseMemcgLimitProperty(std::vector<std::string>&& args); |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 160 | Result<Success> ParseMemcgSoftLimitInBytes(std::vector<std::string>&& args); |
| 161 | Result<Success> ParseMemcgSwappiness(std::vector<std::string>&& args); |
| 162 | Result<Success> ParseNamespace(std::vector<std::string>&& args); |
| 163 | Result<Success> ParseProcessRlimit(std::vector<std::string>&& args); |
| 164 | Result<Success> ParseRestartPeriod(std::vector<std::string>&& args); |
| 165 | Result<Success> ParseSeclabel(std::vector<std::string>&& args); |
| 166 | Result<Success> ParseSetenv(std::vector<std::string>&& args); |
| 167 | Result<Success> ParseShutdown(std::vector<std::string>&& args); |
| 168 | Result<Success> ParseSigstop(std::vector<std::string>&& args); |
| 169 | Result<Success> ParseSocket(std::vector<std::string>&& args); |
| 170 | Result<Success> ParseTimeoutPeriod(std::vector<std::string>&& args); |
| 171 | Result<Success> ParseFile(std::vector<std::string>&& args); |
| 172 | Result<Success> ParseUser(std::vector<std::string>&& args); |
| 173 | Result<Success> ParseWritepid(std::vector<std::string>&& args); |
Jiyong Park | 80aa447 | 2018-11-12 12:08:41 +0900 | [diff] [blame] | 174 | Result<Success> ParseUpdatable(std::vector<std::string>&& args); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 175 | |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 176 | template <typename T> |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 177 | Result<Success> AddDescriptor(std::vector<std::string>&& args); |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 178 | |
Tom Cherry | 5938379 | 2017-07-26 16:09:09 -0700 | [diff] [blame] | 179 | static unsigned long next_start_order_; |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 180 | static bool is_exec_service_running_; |
Tom Cherry | 5938379 | 2017-07-26 16:09:09 -0700 | [diff] [blame] | 181 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 182 | std::string name_; |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 183 | std::set<std::string> classnames_; |
Viorel Suman | 70daa67 | 2016-03-21 10:08:07 +0200 | [diff] [blame] | 184 | std::string console_; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 185 | |
| 186 | unsigned flags_; |
| 187 | pid_t pid_; |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 188 | android::base::boot_clock::time_point time_started_; // time of last start |
| 189 | android::base::boot_clock::time_point time_crashed_; // first crash within inspection window |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 190 | int crash_count_; // number of times crashed within window |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 191 | |
| 192 | uid_t uid_; |
| 193 | gid_t gid_; |
| 194 | std::vector<gid_t> supp_gids_; |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 195 | CapSet capabilities_; |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 196 | unsigned namespace_flags_; |
Tom Cherry | aead51b | 2018-04-20 16:18:12 -0700 | [diff] [blame] | 197 | // Pair of namespace type, path to namespace. |
| 198 | std::vector<std::pair<int, std::string>> namespaces_to_enter_; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 199 | |
| 200 | std::string seclabel_; |
| 201 | |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 202 | std::vector<std::unique_ptr<DescriptorInfo>> descriptors_; |
Tom Cherry | 6de21f1 | 2017-08-22 15:41:03 -0700 | [diff] [blame] | 203 | std::vector<std::pair<std::string, std::string>> environment_vars_; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 204 | |
| 205 | Action onrestart_; // Commands to execute on restart. |
| 206 | |
| 207 | std::vector<std::string> writepid_files_; |
| 208 | |
Steven Moreland | e055d73 | 2017-10-05 18:50:22 -0700 | [diff] [blame] | 209 | std::set<std::string> interfaces_; // e.g. some.package.foo@1.0::IBaz/instance-name |
| 210 | |
Mark Salyzyn | 1385725 | 2018-05-18 15:25:15 -0700 | [diff] [blame] | 211 | // keycodes for triggering this service via /dev/input/input* |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 212 | std::vector<int> keycodes_; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 213 | |
| 214 | IoSchedClass ioprio_class_; |
| 215 | int ioprio_pri_; |
Vitalii Tomkiv | 081705c | 2016-05-18 17:36:30 -0700 | [diff] [blame] | 216 | int priority_; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 217 | |
Marco Nelissen | 310f670 | 2016-07-22 12:07:06 -0700 | [diff] [blame] | 218 | int oom_score_adjust_; |
| 219 | |
Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 220 | int swappiness_ = -1; |
| 221 | int soft_limit_in_bytes_ = -1; |
| 222 | |
| 223 | int limit_in_bytes_ = -1; |
| 224 | int limit_percent_ = -1; |
| 225 | std::string limit_property_; |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 226 | |
Tom Cherry | 33838b1 | 2017-05-04 11:32:36 -0700 | [diff] [blame] | 227 | bool process_cgroup_empty_ = false; |
| 228 | |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 229 | bool override_ = false; |
| 230 | |
Tom Cherry | 5938379 | 2017-07-26 16:09:09 -0700 | [diff] [blame] | 231 | unsigned long start_order_; |
| 232 | |
Tom Cherry | 7ac013d | 2017-08-25 10:39:25 -0700 | [diff] [blame] | 233 | std::vector<std::pair<int, rlimit>> rlimits_; |
| 234 | |
Tom Cherry | 8f38048 | 2018-04-17 14:48:44 -0700 | [diff] [blame] | 235 | bool sigstop_ = false; |
| 236 | |
Tom Cherry | 73f535e | 2018-09-27 16:10:46 -0700 | [diff] [blame] | 237 | std::chrono::seconds restart_period_ = 5s; |
| 238 | std::optional<std::chrono::seconds> timeout_period_; |
| 239 | |
Jiyong Park | 80aa447 | 2018-11-12 12:08:41 +0900 | [diff] [blame] | 240 | bool updatable_ = false; |
| 241 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 242 | std::vector<std::string> args_; |
Paul Crowley | c73b215 | 2018-04-13 17:38:57 +0000 | [diff] [blame] | 243 | |
| 244 | std::vector<std::function<void(const siginfo_t& siginfo)>> reap_callbacks_; |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame^] | 245 | |
| 246 | bool pre_apexd_ = false; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 247 | }; |
| 248 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 249 | class ServiceList { |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 250 | public: |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 251 | static ServiceList& GetInstance(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 252 | |
Tom Cherry | 7da5485 | 2017-05-01 14:16:41 -0700 | [diff] [blame] | 253 | // Exposed for testing |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 254 | ServiceList(); |
Tom Cherry | 7da5485 | 2017-05-01 14:16:41 -0700 | [diff] [blame] | 255 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 256 | void AddService(std::unique_ptr<Service> service); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 257 | void RemoveService(const Service& svc); |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 258 | |
| 259 | template <typename T, typename F = decltype(&Service::name)> |
| 260 | Service* FindService(T value, F function = &Service::name) const { |
| 261 | auto svc = std::find_if(services_.begin(), services_.end(), |
| 262 | [&function, &value](const std::unique_ptr<Service>& s) { |
| 263 | return std::invoke(function, s) == value; |
| 264 | }); |
| 265 | if (svc != services_.end()) { |
| 266 | return svc->get(); |
| 267 | } |
| 268 | return nullptr; |
| 269 | } |
| 270 | |
Steven Moreland | 6227e34 | 2018-05-08 13:46:39 -0700 | [diff] [blame] | 271 | Service* FindInterface(const std::string& interface_name) { |
| 272 | for (const auto& svc : services_) { |
| 273 | if (svc->interfaces().count(interface_name) > 0) { |
| 274 | return svc.get(); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | return nullptr; |
| 279 | } |
| 280 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 281 | void DumpState() const; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 282 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 283 | auto begin() const { return services_.begin(); } |
| 284 | auto end() const { return services_.end(); } |
| 285 | const std::vector<std::unique_ptr<Service>>& services() const { return services_; } |
| 286 | const std::vector<Service*> services_in_shutdown_order() const; |
| 287 | |
Jiyong Park | 80aa447 | 2018-11-12 12:08:41 +0900 | [diff] [blame] | 288 | void MarkServicesUpdate(); |
| 289 | bool IsServicesUpdated() const { return services_update_finished_; } |
| 290 | void DelayService(const Service& service); |
| 291 | |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 292 | private: |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 293 | std::vector<std::unique_ptr<Service>> services_; |
Jiyong Park | 80aa447 | 2018-11-12 12:08:41 +0900 | [diff] [blame] | 294 | |
| 295 | bool services_update_finished_ = false; |
| 296 | std::vector<std::string> delayed_service_names_; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 297 | }; |
| 298 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 299 | class ServiceParser : public SectionParser { |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 300 | public: |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 301 | ServiceParser(ServiceList* service_list, std::vector<Subcontext>* subcontexts) |
| 302 | : service_list_(service_list), subcontexts_(subcontexts), service_(nullptr) {} |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 303 | Result<Success> ParseSection(std::vector<std::string>&& args, const std::string& filename, |
| 304 | int line) override; |
| 305 | Result<Success> ParseLineSection(std::vector<std::string>&& args, int line) override; |
Steven Moreland | 7d0a5c3 | 2017-11-10 14:20:47 -0800 | [diff] [blame] | 306 | Result<Success> EndSection() override; |
Jiyong Park | 80aa447 | 2018-11-12 12:08:41 +0900 | [diff] [blame] | 307 | void EndFile() override { filename_ = ""; } |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 308 | |
| 309 | private: |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 310 | bool IsValidName(const std::string& name) const; |
| 311 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 312 | ServiceList* service_list_; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 313 | std::vector<Subcontext>* subcontexts_; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 314 | std::unique_ptr<Service> service_; |
Jiyong Park | 80aa447 | 2018-11-12 12:08:41 +0900 | [diff] [blame] | 315 | std::string filename_; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 316 | }; |
| 317 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 318 | } // namespace init |
| 319 | } // namespace android |
| 320 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 321 | #endif |