| 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 |  | 
| Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 17 | #pragma once | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 18 |  | 
| Paul Crowley | c73b215 | 2018-04-13 17:38:57 +0000 | [diff] [blame] | 19 | #include <signal.h> | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 20 | #include <sys/types.h> | 
 | 21 |  | 
| Tom Cherry | 73f535e | 2018-09-27 16:10:46 -0700 | [diff] [blame] | 22 | #include <chrono> | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 23 | #include <memory> | 
| Tom Cherry | 73f535e | 2018-09-27 16:10:46 -0700 | [diff] [blame] | 24 | #include <optional> | 
| 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 |  | 
| James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 29 | #include <android-base/chrono_utils.h> | 
| Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 30 | #include <cutils/iosched_policy.h> | 
| James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 31 |  | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 32 | #include "action.h" | 
| Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 33 | #include "capabilities.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" | 
| Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 36 | #include "service_utils.h" | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 37 | #include "subcontext.h" | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 38 |  | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 39 | #define SVC_DISABLED 0x001        // do not autostart with class | 
 | 40 | #define SVC_ONESHOT 0x002         // do not restart on exit | 
 | 41 | #define SVC_RUNNING 0x004         // currently active | 
 | 42 | #define SVC_RESTARTING 0x008      // waiting to restart | 
 | 43 | #define SVC_CONSOLE 0x010         // requires console | 
| Tom Cherry | ad9e7ea | 2018-10-15 17:21:48 -0700 | [diff] [blame] | 44 | #define SVC_CRITICAL 0x020        // will reboot into bootloader if keeps crashing | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 45 | #define SVC_RESET 0x040           // Use when stopping a process, | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 46 |                                   // but not disabling so it can be restarted with its class. | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 47 | #define SVC_RC_DISABLED 0x080     // Remember if the disabled flag was set in the rc script. | 
 | 48 | #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] | 49 | #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] | 50 | #define SVC_EXEC 0x400  // This service was started by either 'exec' or 'exec_start' and stops | 
 | 51 |                         // init from processing more commands until it completes | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 52 |  | 
 | 53 | #define SVC_SHUTDOWN_CRITICAL 0x800  // This service is critical for shutdown and | 
 | 54 |                                      // should not be killed during shutdown | 
| Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 55 | #define SVC_TEMPORARY 0x1000  // This service was started by 'exec' and should be removed from the | 
 | 56 |                               // service list once it is reaped. | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 57 |  | 
 | 58 | #define NR_SVC_SUPP_GIDS 12    // twelve supplementary groups | 
 | 59 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 60 | namespace android { | 
 | 61 | namespace init { | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 62 |  | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 63 | class Service { | 
| Tom Cherry | b1ffb1d | 2019-06-26 11:22:52 -0700 | [diff] [blame] | 64 |     friend class ServiceParser; | 
 | 65 |  | 
| 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, | 
| Tom Cherry | 247ffbf | 2019-07-08 15:09:36 -0700 | [diff] [blame] | 71 |             const std::vector<gid_t>& supp_gids, int namespace_flags, const std::string& seclabel, | 
 | 72 |             Subcontext* subcontext_for_restart_commands, const std::vector<std::string>& args); | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 73 |  | 
| Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 74 |     static Result<std::unique_ptr<Service>> MakeTemporaryOneshotService( | 
 | 75 |             const std::vector<std::string>& args); | 
| Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 76 |  | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 77 |     bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; } | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 78 |     Result<void> ExecStart(); | 
 | 79 |     Result<void> Start(); | 
 | 80 |     Result<void> StartIfNotDisabled(); | 
 | 81 |     Result<void> StartIfPostData(); | 
 | 82 |     Result<void> Enable(); | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 83 |     void Reset(); | 
| Martijn Coenen | 70788f9 | 2019-04-23 16:26:01 +0200 | [diff] [blame] | 84 |     void ResetIfPostData(); | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 85 |     void Stop(); | 
| Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 86 |     void Terminate(); | 
| Tom Cherry | 73f535e | 2018-09-27 16:10:46 -0700 | [diff] [blame] | 87 |     void Timeout(); | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 88 |     void Restart(); | 
| Paul Crowley | c73b215 | 2018-04-13 17:38:57 +0000 | [diff] [blame] | 89 |     void Reap(const siginfo_t& siginfo); | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 90 |     void DumpState() const; | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 91 |     void SetShutdownCritical() { flags_ |= SVC_SHUTDOWN_CRITICAL; } | 
| Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 92 |     bool IsShutdownCritical() const { return (flags_ & SVC_SHUTDOWN_CRITICAL) != 0; } | 
| Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 93 |     void UnSetExec() { | 
 | 94 |         is_exec_service_running_ = false; | 
 | 95 |         flags_ &= ~SVC_EXEC; | 
 | 96 |     } | 
| Paul Crowley | c73b215 | 2018-04-13 17:38:57 +0000 | [diff] [blame] | 97 |     void AddReapCallback(std::function<void(const siginfo_t& siginfo)> callback) { | 
 | 98 |         reap_callbacks_.emplace_back(std::move(callback)); | 
 | 99 |     } | 
| Tom Cherry | 6737a6b | 2019-08-05 15:03:58 -0700 | [diff] [blame] | 100 |     size_t CheckAllCommands() const { return onrestart_.CheckAllCommands(); } | 
| Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 101 |  | 
 | 102 |     static bool is_exec_service_running() { return is_exec_service_running_; } | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 103 |  | 
 | 104 |     const std::string& name() const { return name_; } | 
| Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 105 |     const std::set<std::string>& classnames() const { return classnames_; } | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 106 |     unsigned flags() const { return flags_; } | 
 | 107 |     pid_t pid() const { return pid_; } | 
| Tom Cherry | d269e3a | 2017-07-31 13:23:18 -0700 | [diff] [blame] | 108 |     android::base::boot_clock::time_point time_started() const { return time_started_; } | 
| Tom Cherry | 7da5485 | 2017-05-01 14:16:41 -0700 | [diff] [blame] | 109 |     int crash_count() const { return crash_count_; } | 
| Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 110 |     uid_t uid() const { return proc_attr_.uid; } | 
 | 111 |     gid_t gid() const { return proc_attr_.gid; } | 
| Tom Cherry | 247ffbf | 2019-07-08 15:09:36 -0700 | [diff] [blame] | 112 |     int namespace_flags() const { return namespaces_.flags; } | 
| Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 113 |     const std::vector<gid_t>& supp_gids() const { return proc_attr_.supp_gids; } | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 114 |     const std::string& seclabel() const { return seclabel_; } | 
 | 115 |     const std::vector<int>& keycodes() const { return keycodes_; } | 
| Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 116 |     IoSchedClass ioprio_class() const { return proc_attr_.ioprio_class; } | 
 | 117 |     int ioprio_pri() const { return proc_attr_.ioprio_pri; } | 
| Steven Moreland | e055d73 | 2017-10-05 18:50:22 -0700 | [diff] [blame] | 118 |     const std::set<std::string>& interfaces() const { return interfaces_; } | 
| Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 119 |     int priority() const { return proc_attr_.priority; } | 
| Tom Cherry | 7da5485 | 2017-05-01 14:16:41 -0700 | [diff] [blame] | 120 |     int oom_score_adjust() const { return oom_score_adjust_; } | 
| Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 121 |     bool is_override() const { return override_; } | 
| Tom Cherry | 33838b1 | 2017-05-04 11:32:36 -0700 | [diff] [blame] | 122 |     bool process_cgroup_empty() const { return process_cgroup_empty_; } | 
| Tom Cherry | 5938379 | 2017-07-26 16:09:09 -0700 | [diff] [blame] | 123 |     unsigned long start_order() const { return start_order_; } | 
| Tom Cherry | 8f38048 | 2018-04-17 14:48:44 -0700 | [diff] [blame] | 124 |     void set_sigstop(bool value) { sigstop_ = value; } | 
| Tom Cherry | 73f535e | 2018-09-27 16:10:46 -0700 | [diff] [blame] | 125 |     std::chrono::seconds restart_period() const { return restart_period_; } | 
 | 126 |     std::optional<std::chrono::seconds> timeout_period() const { return timeout_period_; } | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 127 |     const std::vector<std::string>& args() const { return args_; } | 
| Jiyong Park | 80aa447 | 2018-11-12 12:08:41 +0900 | [diff] [blame] | 128 |     bool is_updatable() const { return updatable_; } | 
| Martijn Coenen | 70788f9 | 2019-04-23 16:26:01 +0200 | [diff] [blame] | 129 |     bool is_post_data() const { return post_data_; } | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 130 |  | 
| Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 131 |   private: | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 132 |     void NotifyStateChange(const std::string& new_state) const; | 
 | 133 |     void StopOrReset(int how); | 
| Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 134 |     void KillProcessGroup(int signal); | 
| Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 135 |     void SetProcessAttributesAndCaps(); | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 136 |  | 
| Tom Cherry | 5938379 | 2017-07-26 16:09:09 -0700 | [diff] [blame] | 137 |     static unsigned long next_start_order_; | 
| Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 138 |     static bool is_exec_service_running_; | 
| Tom Cherry | 5938379 | 2017-07-26 16:09:09 -0700 | [diff] [blame] | 139 |  | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 140 |     std::string name_; | 
| Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 141 |     std::set<std::string> classnames_; | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 142 |  | 
 | 143 |     unsigned flags_; | 
 | 144 |     pid_t pid_; | 
| James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 145 |     android::base::boot_clock::time_point time_started_;  // time of last start | 
 | 146 |     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] | 147 |     int crash_count_;                     // number of times crashed within window | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 148 |  | 
| Tom Cherry | 1cd082d | 2019-02-06 10:45:56 -0800 | [diff] [blame] | 149 |     std::optional<CapSet> capabilities_; | 
| Vic Yang | e01ca4d | 2019-05-29 15:58:32 -0700 | [diff] [blame] | 150 |     ProcessAttributes proc_attr_; | 
 | 151 |     NamespaceInfo namespaces_; | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 152 |  | 
 | 153 |     std::string seclabel_; | 
 | 154 |  | 
| Tom Cherry | 2e4c85f | 2019-07-09 13:33:36 -0700 | [diff] [blame] | 155 |     std::vector<SocketDescriptor> sockets_; | 
 | 156 |     std::vector<FileDescriptor> files_; | 
| Tom Cherry | 6de21f1 | 2017-08-22 15:41:03 -0700 | [diff] [blame] | 157 |     std::vector<std::pair<std::string, std::string>> environment_vars_; | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 158 |  | 
 | 159 |     Action onrestart_;  // Commands to execute on restart. | 
 | 160 |  | 
 | 161 |     std::vector<std::string> writepid_files_; | 
 | 162 |  | 
| Steven Moreland | e055d73 | 2017-10-05 18:50:22 -0700 | [diff] [blame] | 163 |     std::set<std::string> interfaces_;  // e.g. some.package.foo@1.0::IBaz/instance-name | 
 | 164 |  | 
| Mark Salyzyn | 1385725 | 2018-05-18 15:25:15 -0700 | [diff] [blame] | 165 |     // keycodes for triggering this service via /dev/input/input* | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 166 |     std::vector<int> keycodes_; | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 167 |  | 
| Marco Nelissen | 310f670 | 2016-07-22 12:07:06 -0700 | [diff] [blame] | 168 |     int oom_score_adjust_; | 
 | 169 |  | 
| Peter Collingbourne | d7157c2 | 2018-10-30 15:49:33 -0700 | [diff] [blame] | 170 |     int swappiness_ = -1; | 
 | 171 |     int soft_limit_in_bytes_ = -1; | 
 | 172 |  | 
 | 173 |     int limit_in_bytes_ = -1; | 
 | 174 |     int limit_percent_ = -1; | 
 | 175 |     std::string limit_property_; | 
| Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 176 |  | 
| Tom Cherry | 33838b1 | 2017-05-04 11:32:36 -0700 | [diff] [blame] | 177 |     bool process_cgroup_empty_ = false; | 
 | 178 |  | 
| Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 179 |     bool override_ = false; | 
 | 180 |  | 
| Tom Cherry | 5938379 | 2017-07-26 16:09:09 -0700 | [diff] [blame] | 181 |     unsigned long start_order_; | 
 | 182 |  | 
| Tom Cherry | 8f38048 | 2018-04-17 14:48:44 -0700 | [diff] [blame] | 183 |     bool sigstop_ = false; | 
 | 184 |  | 
| Tom Cherry | 73f535e | 2018-09-27 16:10:46 -0700 | [diff] [blame] | 185 |     std::chrono::seconds restart_period_ = 5s; | 
 | 186 |     std::optional<std::chrono::seconds> timeout_period_; | 
 | 187 |  | 
| Jiyong Park | 80aa447 | 2018-11-12 12:08:41 +0900 | [diff] [blame] | 188 |     bool updatable_ = false; | 
 | 189 |  | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 190 |     std::vector<std::string> args_; | 
| Paul Crowley | c73b215 | 2018-04-13 17:38:57 +0000 | [diff] [blame] | 191 |  | 
 | 192 |     std::vector<std::function<void(const siginfo_t& siginfo)>> reap_callbacks_; | 
| Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 193 |  | 
 | 194 |     bool pre_apexd_ = false; | 
| Martijn Coenen | 70788f9 | 2019-04-23 16:26:01 +0200 | [diff] [blame] | 195 |  | 
 | 196 |     bool post_data_ = false; | 
| Martijn Coenen | acc45aa | 2019-05-15 22:04:13 +0200 | [diff] [blame] | 197 |  | 
 | 198 |     bool running_at_post_data_reset_ = false; | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 199 | }; | 
 | 200 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 201 | }  // namespace init | 
 | 202 | }  // namespace android |