• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
18 
19 #include <signal.h>
20 #include <sys/types.h>
21 
22 #include <chrono>
23 #include <memory>
24 #include <optional>
25 #include <set>
26 #include <string>
27 #include <vector>
28 
29 #include <android-base/chrono_utils.h>
30 #include <cutils/iosched_policy.h>
31 
32 #include "action.h"
33 #include "capabilities.h"
34 #include "interprocess_fifo.h"
35 #include "keyword_map.h"
36 #include "mount_namespace.h"
37 #include "parser.h"
38 #include "service_utils.h"
39 #include "subcontext.h"
40 
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
46 #define SVC_CRITICAL 0x020        // will reboot into bootloader if keeps crashing
47 #define SVC_RESET 0x040           // Use when stopping a process,
48                                   // but not disabling so it can be restarted with its class.
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.
51 #define SVC_DISABLED_START 0x200  // A start was requested but it was disabled at the time.
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
54 
55 #define SVC_SHUTDOWN_CRITICAL 0x800  // This service is critical for shutdown and
56                                      // should not be killed during shutdown
57 #define SVC_TEMPORARY 0x1000  // This service was started by 'exec' and should be removed from the
58                               // service list once it is reaped.
59 #define SVC_GENTLE_KILL 0x2000  // This service should be stopped with SIGTERM instead of SIGKILL
60                                 // Will still be SIGKILLed after timeout period of 200 ms
61 
62 #define NR_SVC_SUPP_GIDS 12    // twelve supplementary groups
63 
64 namespace android {
65 namespace init {
66 
67 class Service {
68     friend class ServiceParser;
69 
70   public:
71     Service(const std::string& name, Subcontext* subcontext_for_restart_commands,
72             const std::string& filename, const std::vector<std::string>& args);
73 
74     Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid,
75             const std::vector<gid_t>& supp_gids, int namespace_flags, const std::string& seclabel,
76             Subcontext* subcontext_for_restart_commands, const std::string& filename,
77             const std::vector<std::string>& args);
78     Service(const Service&) = delete;
79     void operator=(const Service&) = delete;
80 
81     static Result<std::unique_ptr<Service>> MakeTemporaryOneshotService(
82             const std::vector<std::string>& args);
83 
IsRunning()84     bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; }
IsEnabled()85     bool IsEnabled() { return (flags_ & SVC_DISABLED) == 0; }
86     Result<void> ExecStart();
87     Result<void> Start();
88     Result<void> StartIfNotDisabled();
89     Result<void> Enable();
90     void Reset();
91     void Stop();
92     void Terminate();
93     void Timeout();
94     void Restart();
95     void Reap(const siginfo_t& siginfo);
96     void DumpState() const;
SetShutdownCritical()97     void SetShutdownCritical() { flags_ |= SVC_SHUTDOWN_CRITICAL; }
IsShutdownCritical()98     bool IsShutdownCritical() const { return (flags_ & SVC_SHUTDOWN_CRITICAL) != 0; }
UnSetExec()99     void UnSetExec() {
100         is_exec_service_running_ = false;
101         flags_ &= ~SVC_EXEC;
102     }
AddReapCallback(std::function<void (const siginfo_t & siginfo)> callback)103     void AddReapCallback(std::function<void(const siginfo_t& siginfo)> callback) {
104         reap_callbacks_.emplace_back(std::move(callback));
105     }
106     void SetStartedInFirstStage(pid_t pid);
107     bool MarkSocketPersistent(const std::string& socket_name);
CheckAllCommands()108     size_t CheckAllCommands() const { return onrestart_.CheckAllCommands(); }
109 
is_exec_service_running()110     static bool is_exec_service_running() { return is_exec_service_running_; }
111 
name()112     const std::string& name() const { return name_; }
classnames()113     const std::set<std::string>& classnames() const { return classnames_; }
flags()114     unsigned flags() const { return flags_; }
pid()115     pid_t pid() const { return pid_; }
time_started()116     android::base::boot_clock::time_point time_started() const { return time_started_; }
crash_count()117     int crash_count() const { return crash_count_; }
uid()118     uid_t uid() const { return proc_attr_.uid; }
gid()119     gid_t gid() const { return proc_attr_.gid; }
namespace_flags()120     int namespace_flags() const { return namespaces_.flags; }
supp_gids()121     const std::vector<gid_t>& supp_gids() const { return proc_attr_.supp_gids; }
seclabel()122     const std::string& seclabel() const { return seclabel_; }
keycodes()123     const std::vector<int>& keycodes() const { return keycodes_; }
ioprio_class()124     IoSchedClass ioprio_class() const { return proc_attr_.ioprio_class; }
ioprio_pri()125     int ioprio_pri() const { return proc_attr_.ioprio_pri; }
interfaces()126     const std::set<std::string>& interfaces() const { return interfaces_; }
priority()127     int priority() const { return proc_attr_.priority; }
oom_score_adjust()128     int oom_score_adjust() const { return oom_score_adjust_; }
is_override()129     bool is_override() const { return override_; }
process_cgroup_empty()130     bool process_cgroup_empty() const { return process_cgroup_empty_; }
start_order()131     unsigned long start_order() const { return start_order_; }
set_sigstop(bool value)132     void set_sigstop(bool value) { sigstop_ = value; }
restart_period()133     std::chrono::seconds restart_period() const { return restart_period_; }
timeout_period()134     std::optional<std::chrono::seconds> timeout_period() const { return timeout_period_; }
args()135     const std::vector<std::string>& args() const { return args_; }
is_updatable()136     bool is_updatable() const { return updatable_; }
is_post_data()137     bool is_post_data() const { return post_data_; }
is_from_apex()138     bool is_from_apex() const { return base::StartsWith(filename_, "/apex/"); }
set_oneshot(bool value)139     void set_oneshot(bool value) {
140         if (value) {
141             flags_ |= SVC_ONESHOT;
142         } else {
143             flags_ &= ~SVC_ONESHOT;
144         }
145     }
subcontext()146     const Subcontext* subcontext() const { return subcontext_; }
filename()147     const std::string& filename() const { return filename_; }
set_filename(const std::string & name)148     void set_filename(const std::string& name) { filename_ = name; }
149 
150   private:
151     void NotifyStateChange(const std::string& new_state) const;
152     void StopOrReset(int how);
153     void KillProcessGroup(int signal, bool report_oneshot = false);
154     void SetProcessAttributesAndCaps(InterprocessFifo setsid_finished);
155     void ResetFlagsForStart();
156     Result<void> CheckConsole();
157     void ConfigureMemcg();
158     void RunService(const std::vector<Descriptor>& descriptors, InterprocessFifo cgroups_activated,
159                     InterprocessFifo setsid_finished);
160     void SetMountNamespace();
161     static unsigned long next_start_order_;
162     static bool is_exec_service_running_;
163 
164     const std::string name_;
165     std::set<std::string> classnames_;
166 
167     unsigned flags_;
168     pid_t pid_;
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
171     int crash_count_;                     // number of times crashed within window
172     bool upgraded_mte_ = false;           // whether we upgraded async MTE -> sync MTE before
173     std::chrono::minutes fatal_crash_window_ = 4min;  // fatal() when more than 4 crashes in it
174     std::optional<std::string> fatal_reboot_target_;  // reboot target of fatal handler
175 
176     std::optional<CapSet> capabilities_;
177     ProcessAttributes proc_attr_;
178     NamespaceInfo namespaces_;
179 
180     std::string seclabel_;
181 
182     std::vector<SocketDescriptor> sockets_;
183     std::vector<FileDescriptor> files_;
184     std::vector<std::pair<std::string, std::string>> environment_vars_;
185     // Environment variables that only get applied to the next run.
186     std::vector<std::pair<std::string, std::string>> once_environment_vars_;
187 
188     const Subcontext* const subcontext_;
189     Action onrestart_;  // Commands to execute on restart.
190 
191     std::vector<std::string> writepid_files_;
192 
193     std::vector<std::string> task_profiles_;
194 
195     std::set<std::string> interfaces_;  // e.g. some.package.foo@1.0::IBaz/instance-name
196 
197     // keycodes for triggering this service via /dev/input/input*
198     std::vector<int> keycodes_;
199 
200     int oom_score_adjust_;
201 
202     int swappiness_ = -1;
203     int soft_limit_in_bytes_ = -1;
204 
205     int limit_in_bytes_ = -1;
206     int limit_percent_ = -1;
207     std::string limit_property_;
208 
209     bool process_cgroup_empty_ = false;
210 
211     bool override_ = false;
212 
213     unsigned long start_order_;
214 
215     bool sigstop_ = false;
216 
217     std::chrono::seconds restart_period_ = 5s;
218     std::optional<std::chrono::seconds> timeout_period_;
219 
220     bool updatable_ = false;
221 
222     const std::vector<std::string> args_;
223 
224     std::vector<std::function<void(const siginfo_t& siginfo)>> reap_callbacks_;
225 
226     std::optional<MountNamespace> mount_namespace_;
227 
228     bool post_data_ = false;
229 
230     std::optional<std::string> on_failure_reboot_target_;
231 
232     std::string filename_;
233 };
234 
235 }  // namespace init
236 }  // namespace android
237