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 | |
| 20 | #include <sys/types.h> |
| 21 | |
| 22 | #include <cutils/iosched_policy.h> |
| 23 | |
| 24 | #include <memory> |
| 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
| 28 | #include "action.h" |
| 29 | |
| 30 | #define SVC_DISABLED 0x001 // do not autostart with class |
| 31 | #define SVC_ONESHOT 0x002 // do not restart on exit |
| 32 | #define SVC_RUNNING 0x004 // currently active |
| 33 | #define SVC_RESTARTING 0x008 // waiting to restart |
| 34 | #define SVC_CONSOLE 0x010 // requires console |
| 35 | #define SVC_CRITICAL 0x020 // will reboot into recovery if keeps crashing |
| 36 | #define SVC_RESET 0x040 // Use when stopping a process, |
| 37 | // but not disabling so it can be restarted with its class. |
| 38 | #define SVC_RC_DISABLED 0x080 // Remember if the disabled flag was set in the rc script. |
| 39 | #define SVC_RESTART 0x100 // Use to safely restart (stop, wait, start) a service. |
| 40 | #define SVC_DISABLED_START 0x200 // A start was requested but it was disabled at the time. |
| 41 | #define SVC_EXEC 0x400 // This synthetic service corresponds to an 'exec'. |
| 42 | |
| 43 | #define NR_SVC_SUPP_GIDS 12 // twelve supplementary groups |
| 44 | |
| 45 | class Action; |
| 46 | class ServiceManager; |
| 47 | |
| 48 | struct SocketInfo { |
| 49 | SocketInfo(); |
| 50 | SocketInfo(const std::string& name, const std::string& type, uid_t uid, |
| 51 | gid_t gid, int perm, const std::string& socketcon); |
| 52 | std::string name; |
| 53 | std::string type; |
| 54 | uid_t uid; |
| 55 | gid_t gid; |
| 56 | int perm; |
| 57 | std::string socketcon; |
| 58 | }; |
| 59 | |
| 60 | struct ServiceEnvironmentInfo { |
| 61 | ServiceEnvironmentInfo(); |
| 62 | ServiceEnvironmentInfo(const std::string& name, const std::string& value); |
| 63 | std::string name; |
| 64 | std::string value; |
| 65 | }; |
| 66 | |
| 67 | class Service { |
| 68 | public: |
| 69 | Service(const std::string& name, const std::string& classname, |
| 70 | const std::vector<std::string>& args); |
| 71 | |
| 72 | Service(const std::string& name, const std::string& classname, |
| 73 | unsigned flags, uid_t uid, gid_t gid, const std::vector<gid_t>& supp_gids, |
| 74 | const std::string& seclabel, const std::vector<std::string>& args); |
| 75 | |
| 76 | bool HandleLine(int kw, const std::vector<std::string>& args, std::string* err); |
| 77 | bool Start(const std::vector<std::string>& dynamic_args); |
| 78 | bool Start(); |
| 79 | bool StartIfNotDisabled(); |
| 80 | bool Enable(); |
| 81 | void Reset(); |
| 82 | void Stop(); |
| 83 | void Restart(); |
| 84 | void RestartIfNeeded(time_t& process_needs_restart); |
| 85 | bool Reap(); |
| 86 | void DumpState() const; |
| 87 | |
| 88 | const std::string& name() const { return name_; } |
| 89 | const std::string& classname() const { return classname_; } |
| 90 | unsigned flags() const { return flags_; } |
| 91 | pid_t pid() const { return pid_; } |
| 92 | uid_t uid() const { return uid_; } |
| 93 | gid_t gid() const { return gid_; } |
| 94 | const std::vector<gid_t>& supp_gids() const { return supp_gids_; } |
| 95 | const std::string& seclabel() const { return seclabel_; } |
| 96 | const std::vector<int>& keycodes() const { return keycodes_; } |
| 97 | int keychord_id() const { return keychord_id_; } |
| 98 | void set_keychord_id(int keychord_id) { keychord_id_ = keychord_id; } |
| 99 | const std::vector<std::string>& args() const { return args_; } |
| 100 | |
| 101 | private: |
| 102 | void NotifyStateChange(const std::string& new_state) const; |
| 103 | void StopOrReset(int how); |
| 104 | void ZapStdio() const; |
| 105 | void OpenConsole() const; |
| 106 | void PublishSocket(const std::string& name, int fd) const; |
| 107 | |
| 108 | std::string name_; |
| 109 | std::string classname_; |
| 110 | |
| 111 | unsigned flags_; |
| 112 | pid_t pid_; |
| 113 | time_t time_started_; // time of last start |
| 114 | time_t time_crashed_; // first crash within inspection window |
| 115 | int nr_crashed_; // number of times crashed within window |
| 116 | |
| 117 | uid_t uid_; |
| 118 | gid_t gid_; |
| 119 | std::vector<gid_t> supp_gids_; |
| 120 | |
| 121 | std::string seclabel_; |
| 122 | |
| 123 | std::vector<SocketInfo> sockets_; |
| 124 | std::vector<ServiceEnvironmentInfo> envvars_; |
| 125 | |
| 126 | Action onrestart_; // Commands to execute on restart. |
| 127 | |
| 128 | std::vector<std::string> writepid_files_; |
| 129 | |
| 130 | // keycodes for triggering this service via /dev/keychord |
| 131 | std::vector<int> keycodes_; |
| 132 | int keychord_id_; |
| 133 | |
| 134 | IoSchedClass ioprio_class_; |
| 135 | int ioprio_pri_; |
| 136 | |
| 137 | std::vector<std::string> args_; |
| 138 | }; |
| 139 | |
| 140 | class ServiceManager { |
| 141 | public: |
| 142 | static ServiceManager& GetInstance(); |
| 143 | |
| 144 | Service* AddNewService(const std::string& name, const std::string& classname, |
| 145 | const std::vector<std::string>& args, |
| 146 | std::string* err); |
| 147 | Service* MakeExecOneshotService(const std::vector<std::string>& args); |
| 148 | Service* FindServiceByName(const std::string& name) const; |
| 149 | Service* FindServiceByPid(pid_t pid) const; |
| 150 | Service* FindServiceByKeychord(int keychord_id) const; |
| 151 | void ForEachService(void (*func)(Service* svc)) const; |
| 152 | void ForEachServiceInClass(const std::string& classname, |
| 153 | void (*func)(Service* svc)) const; |
| 154 | void ForEachServiceWithFlags(unsigned matchflags, |
| 155 | void (*func)(Service* svc)) const; |
| 156 | void RemoveService(const Service& svc); |
| 157 | void DumpState() const; |
| 158 | private: |
| 159 | ServiceManager(); |
| 160 | |
| 161 | bool IsValidName(const std::string& name) const; |
| 162 | |
| 163 | static int exec_count_; // Every service needs a unique name. |
| 164 | std::vector<std::unique_ptr<Service>> services_; |
| 165 | }; |
| 166 | |
| 167 | #endif |