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