/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef _INIT_SERVICE_H #define _INIT_SERVICE_H #include #include #include #include #include #include #include #include #include "action.h" #include "capabilities.h" #include "descriptors.h" #include "keyword_map.h" #include "parser.h" #include "subcontext.h" #define SVC_DISABLED 0x001 // do not autostart with class #define SVC_ONESHOT 0x002 // do not restart on exit #define SVC_RUNNING 0x004 // currently active #define SVC_RESTARTING 0x008 // waiting to restart #define SVC_CONSOLE 0x010 // requires console #define SVC_CRITICAL 0x020 // will reboot into recovery if keeps crashing #define SVC_RESET 0x040 // Use when stopping a process, // but not disabling so it can be restarted with its class. #define SVC_RC_DISABLED 0x080 // Remember if the disabled flag was set in the rc script. #define SVC_RESTART 0x100 // Use to safely restart (stop, wait, start) a service. #define SVC_DISABLED_START 0x200 // A start was requested but it was disabled at the time. #define SVC_EXEC 0x400 // This service was started by either 'exec' or 'exec_start' and stops // init from processing more commands until it completes #define SVC_SHUTDOWN_CRITICAL 0x800 // This service is critical for shutdown and // should not be killed during shutdown #define SVC_TEMPORARY 0x1000 // This service was started by 'exec' and should be removed from the // service list once it is reaped. #define NR_SVC_SUPP_GIDS 12 // twelve supplementary groups namespace android { namespace init { class Service { public: Service(const std::string& name, Subcontext* subcontext_for_restart_commands, const std::vector& args); Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid, const std::vector& supp_gids, const CapSet& capabilities, unsigned namespace_flags, const std::string& seclabel, Subcontext* subcontext_for_restart_commands, const std::vector& args); static std::unique_ptr MakeTemporaryOneshotService(const std::vector& args); bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; } Result ParseLine(const std::vector& args); Result ExecStart(); Result Start(); Result StartIfNotDisabled(); Result Enable(); void Reset(); void Stop(); void Terminate(); void Restart(); void Reap(); void DumpState() const; void SetShutdownCritical() { flags_ |= SVC_SHUTDOWN_CRITICAL; } bool IsShutdownCritical() const { return (flags_ & SVC_SHUTDOWN_CRITICAL) != 0; } void UnSetExec() { is_exec_service_running_ = false; flags_ &= ~SVC_EXEC; } static bool is_exec_service_running() { return is_exec_service_running_; } const std::string& name() const { return name_; } const std::set& classnames() const { return classnames_; } unsigned flags() const { return flags_; } pid_t pid() const { return pid_; } android::base::boot_clock::time_point time_started() const { return time_started_; } int crash_count() const { return crash_count_; } uid_t uid() const { return uid_; } gid_t gid() const { return gid_; } unsigned namespace_flags() const { return namespace_flags_; } const std::vector& supp_gids() const { return supp_gids_; } const std::string& seclabel() const { return seclabel_; } const std::vector& keycodes() const { return keycodes_; } int keychord_id() const { return keychord_id_; } void set_keychord_id(int keychord_id) { keychord_id_ = keychord_id; } IoSchedClass ioprio_class() const { return ioprio_class_; } int ioprio_pri() const { return ioprio_pri_; } const std::set& interfaces() const { return interfaces_; } int priority() const { return priority_; } int oom_score_adjust() const { return oom_score_adjust_; } bool is_override() const { return override_; } bool process_cgroup_empty() const { return process_cgroup_empty_; } unsigned long start_order() const { return start_order_; } const std::vector& args() const { return args_; } private: using OptionParser = Result (Service::*)(const std::vector& args); class OptionParserMap; void NotifyStateChange(const std::string& new_state) const; void StopOrReset(int how); void ZapStdio() const; void OpenConsole() const; void KillProcessGroup(int signal); void SetProcessAttributes(); Result ParseCapabilities(const std::vector& args); Result ParseClass(const std::vector& args); Result ParseConsole(const std::vector& args); Result ParseCritical(const std::vector& args); Result ParseDisabled(const std::vector& args); Result ParseGroup(const std::vector& args); Result ParsePriority(const std::vector& args); Result ParseInterface(const std::vector& args); Result ParseIoprio(const std::vector& args); Result ParseKeycodes(const std::vector& args); Result ParseOneshot(const std::vector& args); Result ParseOnrestart(const std::vector& args); Result ParseOomScoreAdjust(const std::vector& args); Result ParseOverride(const std::vector& args); Result ParseMemcgLimitInBytes(const std::vector& args); Result ParseMemcgSoftLimitInBytes(const std::vector& args); Result ParseMemcgSwappiness(const std::vector& args); Result ParseNamespace(const std::vector& args); Result ParseProcessRlimit(const std::vector& args); Result ParseSeclabel(const std::vector& args); Result ParseSetenv(const std::vector& args); Result ParseShutdown(const std::vector& args); Result ParseSocket(const std::vector& args); Result ParseFile(const std::vector& args); Result ParseUser(const std::vector& args); Result ParseWritepid(const std::vector& args); template Result AddDescriptor(const std::vector& args); static unsigned long next_start_order_; static bool is_exec_service_running_; std::string name_; std::set classnames_; std::string console_; unsigned flags_; pid_t pid_; android::base::boot_clock::time_point time_started_; // time of last start android::base::boot_clock::time_point time_crashed_; // first crash within inspection window int crash_count_; // number of times crashed within window uid_t uid_; gid_t gid_; std::vector supp_gids_; CapSet capabilities_; unsigned namespace_flags_; std::string seclabel_; std::vector> descriptors_; std::vector> environment_vars_; Action onrestart_; // Commands to execute on restart. std::vector writepid_files_; std::set interfaces_; // e.g. some.package.foo@1.0::IBaz/instance-name // keycodes for triggering this service via /dev/keychord std::vector keycodes_; int keychord_id_; IoSchedClass ioprio_class_; int ioprio_pri_; int priority_; int oom_score_adjust_; int swappiness_; int soft_limit_in_bytes_; int limit_in_bytes_; bool process_cgroup_empty_ = false; bool override_ = false; unsigned long start_order_; std::vector> rlimits_; std::vector args_; }; class ServiceList { public: static ServiceList& GetInstance(); // Exposed for testing ServiceList(); void AddService(std::unique_ptr service); void RemoveService(const Service& svc); template Service* FindService(T value, F function = &Service::name) const { auto svc = std::find_if(services_.begin(), services_.end(), [&function, &value](const std::unique_ptr& s) { return std::invoke(function, s) == value; }); if (svc != services_.end()) { return svc->get(); } return nullptr; } void DumpState() const; auto begin() const { return services_.begin(); } auto end() const { return services_.end(); } const std::vector>& services() const { return services_; } const std::vector services_in_shutdown_order() const; private: std::vector> services_; }; class ServiceParser : public SectionParser { public: ServiceParser(ServiceList* service_list, std::vector* subcontexts) : service_list_(service_list), subcontexts_(subcontexts), service_(nullptr) {} Result ParseSection(std::vector&& args, const std::string& filename, int line) override; Result ParseLineSection(std::vector&& args, int line) override; Result EndSection() override; private: bool IsValidName(const std::string& name) const; ServiceList* service_list_; std::vector* subcontexts_; std::unique_ptr service_; }; } // namespace init } // namespace android #endif