rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | diff --git a/Makefile.am b/Makefile.am |
| 2 | new file mode 100644 |
| 3 | index 0000000..7010370 |
| 4 | --- /dev/null |
| 5 | +++ b/Makefile.am |
| 6 | @@ -0,0 +1,2 @@ |
| 7 | +SUBDIRS = \ |
| 8 | + system/core/init |
| 9 | diff --git a/configure.ac b/configure.ac |
| 10 | new file mode 100644 |
| 11 | index 0000000..83b1988 |
| 12 | --- /dev/null |
| 13 | +++ b/configure.ac |
| 14 | @@ -0,0 +1,8 @@ |
| 15 | +AC_INIT([service-launcher], [1.0]) |
| 16 | +AM_INIT_AUTOMAKE([foreign]) |
| 17 | +AC_PROG_CXX |
| 18 | +AC_CONFIG_FILES([ |
| 19 | + Makefile |
| 20 | + system/core/init/Makefile |
| 21 | +]) |
| 22 | +AC_OUTPUT |
| 23 | diff --git a/system/core/init/Makefile.am b/system/core/init/Makefile.am |
| 24 | new file mode 100644 |
| 25 | index 0000000..adf9298 |
| 26 | --- /dev/null |
| 27 | +++ b/system/core/init/Makefile.am |
| 28 | @@ -0,0 +1,14 @@ |
| 29 | +bin_PROGRAMS = service-launcher |
| 30 | + |
| 31 | +service_launcher_CPPFLAGS = \ |
| 32 | + -I$(top_srcdir)/system/core/base/include |
| 33 | + |
| 34 | +service_launcher_CXXFLAGS = -std=c++14 |
| 35 | + |
| 36 | +service_launcher_SOURCES = \ |
| 37 | + $(top_srcdir)/system/core/base/stringprintf.cpp \ |
| 38 | + init_parser.cpp \ |
| 39 | + parser.cpp \ |
| 40 | + service.cpp \ |
| 41 | + service-launcher.cpp \ |
| 42 | + util.cpp |
| 43 | diff --git a/system/core/init/init_parser.cpp b/system/core/init/init_parser.cpp |
| 44 | index b44ca59..cc50f73 100644 |
| 45 | --- a/system/core/init/init_parser.cpp |
| 46 | +++ b/system/core/init/init_parser.cpp |
| 47 | @@ -18,7 +18,6 @@ |
| 48 | #include <errno.h> |
| 49 | #include <fcntl.h> |
| 50 | |
| 51 | -#include "action.h" |
| 52 | #include "init_parser.h" |
| 53 | #include "log.h" |
| 54 | #include "parser.h" |
| 55 | @@ -142,7 +141,9 @@ bool Parser::ParseConfig(const std::string& path) { |
| 56 | return ParseConfigFile(path); |
| 57 | } |
| 58 | |
| 59 | +#if 0 |
| 60 | void Parser::DumpState() const { |
| 61 | ServiceManager::GetInstance().DumpState(); |
| 62 | ActionManager::GetInstance().DumpState(); |
| 63 | } |
| 64 | +#endif |
| 65 | diff --git a/system/core/init/init_parser.h b/system/core/init/init_parser.h |
| 66 | index 5ed30ad..7310c35 100644 |
| 67 | --- a/system/core/init/init_parser.h |
| 68 | +++ b/system/core/init/init_parser.h |
| 69 | @@ -18,6 +18,7 @@ |
| 70 | #define _INIT_INIT_PARSER_H_ |
| 71 | |
| 72 | #include <map> |
| 73 | +#include <memory> |
| 74 | #include <string> |
| 75 | #include <vector> |
| 76 | |
| 77 | diff --git a/system/core/init/log.h b/system/core/init/log.h |
| 78 | index c5c30af..97776ed 100644 |
| 79 | --- a/system/core/init/log.h |
| 80 | +++ b/system/core/init/log.h |
| 81 | @@ -17,8 +17,6 @@ |
| 82 | #ifndef _INIT_LOG_H_ |
| 83 | #define _INIT_LOG_H_ |
| 84 | |
| 85 | -#include <cutils/klog.h> |
| 86 | - |
| 87 | #define ERROR(x...) init_klog_write(KLOG_ERROR_LEVEL, x) |
| 88 | #define WARNING(x...) init_klog_write(KLOG_WARNING_LEVEL, x) |
| 89 | #define NOTICE(x...) init_klog_write(KLOG_NOTICE_LEVEL, x) |
| 90 | @@ -26,7 +24,6 @@ |
| 91 | #define DEBUG(x...) init_klog_write(KLOG_DEBUG_LEVEL, x) |
| 92 | #define VERBOSE(x...) init_klog_write(KLOG_DEBUG_LEVEL, x) |
| 93 | |
| 94 | -void init_klog_write(int level, const char* fmt, ...) __printflike(2, 3); |
| 95 | -int selinux_klog_callback(int level, const char* fmt, ...) __printflike(2, 3); |
| 96 | +#define init_klog_write(level, x...) printf(x) |
| 97 | |
| 98 | #endif |
| 99 | diff --git a/system/core/init/service-launcher.cpp b/system/core/init/service-launcher.cpp |
| 100 | new file mode 100644 |
| 101 | index 0000000..9493384 |
| 102 | --- /dev/null |
| 103 | +++ b/system/core/init/service-launcher.cpp |
| 104 | @@ -0,0 +1,17 @@ |
| 105 | +#include <memory> |
| 106 | + |
| 107 | +#include "init_parser.h" |
| 108 | +#include "service.h" |
| 109 | + |
| 110 | +int main(int argc, char** argv) { |
| 111 | + |
| 112 | + if (argc != 2) { |
| 113 | + return 0; |
| 114 | + } |
| 115 | + |
| 116 | + Parser& parser = Parser::GetInstance(); |
| 117 | + parser.AddSectionParser("service",std::make_unique<ServiceParser>()); |
| 118 | + parser.ParseConfig(argv[1]); |
| 119 | + |
| 120 | + return 0; |
| 121 | +} |
| 122 | diff --git a/system/core/init/service.cpp b/system/core/init/service.cpp |
| 123 | index f5b8b00..20c2c9a 100644 |
| 124 | --- a/system/core/init/service.cpp |
| 125 | +++ b/system/core/init/service.cpp |
| 126 | @@ -23,22 +23,16 @@ |
| 127 | #include <termios.h> |
| 128 | #include <unistd.h> |
| 129 | |
| 130 | -#include <selinux/selinux.h> |
| 131 | - |
| 132 | -#include <android-base/file.h> |
| 133 | #include <android-base/stringprintf.h> |
| 134 | -#include <cutils/android_reboot.h> |
| 135 | #include <cutils/sockets.h> |
| 136 | |
| 137 | -#include "action.h" |
| 138 | -#include "init.h" |
| 139 | #include "init_parser.h" |
| 140 | #include "log.h" |
| 141 | -#include "property_service.h" |
| 142 | #include "util.h" |
| 143 | |
| 144 | using android::base::StringPrintf; |
| 145 | -using android::base::WriteStringToFile; |
| 146 | + |
| 147 | +#define add_environment(k, v) setenv(k, v, 1) |
| 148 | |
| 149 | #define CRITICAL_CRASH_THRESHOLD 4 // if we crash >4 times ... |
| 150 | #define CRITICAL_CRASH_WINDOW (4*60) // ... in 4 minutes, goto recovery |
| 151 | @@ -63,8 +57,7 @@ Service::Service(const std::string& name, const std::string& classname, |
| 152 | const std::vector<std::string>& args) |
| 153 | : name_(name), classname_(classname), flags_(0), pid_(0), time_started_(0), |
| 154 | time_crashed_(0), nr_crashed_(0), uid_(0), gid_(0), seclabel_(""), |
| 155 | - ioprio_class_(IoSchedClass_NONE), ioprio_pri_(0), args_(args) { |
| 156 | - onrestart_.InitSingleTrigger("onrestart"); |
| 157 | + args_(args) { |
| 158 | } |
| 159 | |
| 160 | Service::Service(const std::string& name, const std::string& classname, |
| 161 | @@ -72,10 +65,10 @@ Service::Service(const std::string& name, const std::string& classname, |
| 162 | const std::string& seclabel, const std::vector<std::string>& args) |
| 163 | : name_(name), classname_(classname), flags_(flags), pid_(0), time_started_(0), |
| 164 | time_crashed_(0), nr_crashed_(0), uid_(uid), gid_(gid), supp_gids_(supp_gids), |
| 165 | - seclabel_(seclabel), ioprio_class_(IoSchedClass_NONE), ioprio_pri_(0), args_(args) { |
| 166 | - onrestart_.InitSingleTrigger("onrestart"); |
| 167 | + seclabel_(seclabel), args_(args) { |
| 168 | } |
| 169 | |
| 170 | +#if 0 |
| 171 | void Service::NotifyStateChange(const std::string& new_state) const { |
| 172 | if ((flags_ & SVC_EXEC) != 0) { |
| 173 | // 'exec' commands don't have properties tracking their state. |
| 174 | @@ -151,6 +144,7 @@ bool Service::Reap() { |
| 175 | NotifyStateChange("restarting"); |
| 176 | return false; |
| 177 | } |
| 178 | +#endif |
| 179 | |
| 180 | void Service::DumpState() const { |
| 181 | INFO("service %s\n", name_.c_str()); |
| 182 | @@ -196,6 +190,7 @@ bool Service::HandleGroup(const std::vector<std::string>& args, std::string* err |
| 183 | } |
| 184 | |
| 185 | bool Service::HandleIoprio(const std::vector<std::string>& args, std::string* err) { |
| 186 | +#if 0 |
| 187 | ioprio_pri_ = std::stoul(args[2], 0, 8); |
| 188 | |
| 189 | if (ioprio_pri_ < 0 || ioprio_pri_ > 7) { |
| 190 | @@ -213,6 +208,7 @@ bool Service::HandleIoprio(const std::vector<std::string>& args, std::string* er |
| 191 | *err = "ioprio option usage: ioprio <rt|be|idle> <0-7>"; |
| 192 | return false; |
| 193 | } |
| 194 | +#endif |
| 195 | |
| 196 | return true; |
| 197 | } |
| 198 | @@ -230,8 +226,10 @@ bool Service::HandleOneshot(const std::vector<std::string>& args, std::string* e |
| 199 | } |
| 200 | |
| 201 | bool Service::HandleOnrestart(const std::vector<std::string>& args, std::string* err) { |
| 202 | +#if 0 |
| 203 | std::vector<std::string> str_args(args.begin() + 1, args.end()); |
| 204 | onrestart_.AddCommand(str_args, "", 0, err); |
| 205 | +#endif |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | @@ -329,6 +327,7 @@ bool Service::Start() { |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | +#if 0 |
| 214 | bool needs_console = (flags_ & SVC_CONSOLE); |
| 215 | if (needs_console) { |
| 216 | if (console_.empty()) { |
| 217 | @@ -343,6 +342,7 @@ bool Service::Start() { |
| 218 | return false; |
| 219 | } |
| 220 | } |
| 221 | +#endif |
| 222 | |
| 223 | struct stat sb; |
| 224 | if (stat(args_[0].c_str(), &sb) == -1) { |
| 225 | @@ -352,6 +352,7 @@ bool Service::Start() { |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | +#if 0 |
| 230 | std::string scon; |
| 231 | if (!seclabel_.empty()) { |
| 232 | scon = seclabel_; |
| 233 | @@ -393,11 +394,14 @@ bool Service::Start() { |
| 234 | return false; |
| 235 | } |
| 236 | } |
| 237 | +#endif |
| 238 | |
| 239 | NOTICE("Starting service '%s'...\n", name_.c_str()); |
| 240 | |
| 241 | +#if 0 |
| 242 | pid_t pid = fork(); |
| 243 | if (pid == 0) { |
| 244 | +#endif |
| 245 | umask(077); |
| 246 | |
| 247 | for (const auto& ei : envvars_) { |
| 248 | @@ -408,8 +412,7 @@ bool Service::Start() { |
| 249 | int socket_type = ((si.type == "stream" ? SOCK_STREAM : |
| 250 | (si.type == "dgram" ? SOCK_DGRAM : |
| 251 | SOCK_SEQPACKET))); |
| 252 | - const char* socketcon = |
| 253 | - !si.socketcon.empty() ? si.socketcon.c_str() : scon.c_str(); |
| 254 | + const char* socketcon = nullptr; |
| 255 | |
| 256 | int s = create_socket(si.name.c_str(), socket_type, si.perm, |
| 257 | si.uid, si.gid, socketcon); |
| 258 | @@ -418,6 +421,7 @@ bool Service::Start() { |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | +#if 0 |
| 263 | std::string pid_str = StringPrintf("%d", getpid()); |
| 264 | for (const auto& file : writepid_files_) { |
| 265 | if (!WriteStringToFile(pid_str, file)) { |
| 266 | @@ -468,17 +472,19 @@ bool Service::Start() { |
| 267 | _exit(127); |
| 268 | } |
| 269 | } |
| 270 | +#endif |
| 271 | |
| 272 | std::vector<char*> strs; |
| 273 | for (const auto& s : args_) { |
| 274 | strs.push_back(const_cast<char*>(s.c_str())); |
| 275 | } |
| 276 | strs.push_back(nullptr); |
| 277 | - if (execve(args_[0].c_str(), (char**) &strs[0], (char**) ENV) < 0) { |
| 278 | + if (execv(args_[0].c_str(), (char**) &strs[0]) < 0) { |
| 279 | ERROR("cannot execve('%s'): %s\n", args_[0].c_str(), strerror(errno)); |
| 280 | } |
| 281 | |
| 282 | _exit(127); |
| 283 | +#if 0 |
| 284 | } |
| 285 | |
| 286 | if (pid < 0) { |
| 287 | @@ -498,9 +504,11 @@ bool Service::Start() { |
| 288 | } |
| 289 | |
| 290 | NotifyStateChange("running"); |
| 291 | +#endif |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | +#if 0 |
| 296 | bool Service::StartIfNotDisabled() { |
| 297 | if (!(flags_ & SVC_DISABLED)) { |
| 298 | return Start(); |
| 299 | @@ -608,6 +616,7 @@ void Service::OpenConsole() const { |
| 300 | dup2(fd, 2); |
| 301 | close(fd); |
| 302 | } |
| 303 | +#endif |
| 304 | |
| 305 | void Service::PublishSocket(const std::string& name, int fd) const { |
| 306 | std::string key = StringPrintf(ANDROID_SOCKET_ENV_PREFIX "%s", name.c_str()); |
| 307 | @@ -618,6 +627,7 @@ void Service::PublishSocket(const std::string& name, int fd) const { |
| 308 | fcntl(fd, F_SETFD, 0); |
| 309 | } |
| 310 | |
| 311 | +#if 0 |
| 312 | int ServiceManager::exec_count_ = 0; |
| 313 | |
| 314 | ServiceManager::ServiceManager() { |
| 315 | @@ -817,6 +827,7 @@ void ServiceManager::ReapAnyOutstandingChildren() { |
| 316 | while (ReapOneProcess()) { |
| 317 | } |
| 318 | } |
| 319 | +#endif |
| 320 | |
| 321 | bool ServiceParser::ParseSection(const std::vector<std::string>& args, |
| 322 | std::string* err) { |
| 323 | @@ -844,7 +855,7 @@ bool ServiceParser::ParseLineSection(const std::vector<std::string>& args, |
| 324 | |
| 325 | void ServiceParser::EndSection() { |
| 326 | if (service_) { |
| 327 | - ServiceManager::GetInstance().AddService(std::move(service_)); |
| 328 | + service_->Start(); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | diff --git a/system/core/init/service.h b/system/core/init/service.h |
| 333 | index d6ce664..1e36d3f 100644 |
| 334 | --- a/system/core/init/service.h |
| 335 | +++ b/system/core/init/service.h |
| 336 | @@ -19,13 +19,11 @@ |
| 337 | |
| 338 | #include <sys/types.h> |
| 339 | |
| 340 | -#include <cutils/iosched_policy.h> |
| 341 | - |
| 342 | +#include <limits> |
| 343 | #include <memory> |
| 344 | #include <string> |
| 345 | #include <vector> |
| 346 | |
| 347 | -#include "action.h" |
| 348 | #include "init_parser.h" |
| 349 | #include "keyword_map.h" |
| 350 | |
| 351 | @@ -145,17 +143,12 @@ private: |
| 352 | std::vector<SocketInfo> sockets_; |
| 353 | std::vector<ServiceEnvironmentInfo> envvars_; |
| 354 | |
| 355 | - Action onrestart_; // Commands to execute on restart. |
| 356 | - |
| 357 | std::vector<std::string> writepid_files_; |
| 358 | |
| 359 | // keycodes for triggering this service via /dev/keychord |
| 360 | std::vector<int> keycodes_; |
| 361 | int keychord_id_; |
| 362 | |
| 363 | - IoSchedClass ioprio_class_; |
| 364 | - int ioprio_pri_; |
| 365 | - |
| 366 | std::vector<std::string> args_; |
| 367 | }; |
| 368 | |
| 369 | diff --git a/system/core/init/util.cpp b/system/core/init/util.cpp |
| 370 | index 750e040..85e647f 100644 |
| 371 | --- a/system/core/init/util.cpp |
| 372 | +++ b/system/core/init/util.cpp |
| 373 | @@ -21,28 +21,22 @@ |
| 374 | #include <fcntl.h> |
| 375 | #include <ctype.h> |
| 376 | #include <errno.h> |
| 377 | +#include <limits.h> |
| 378 | #include <time.h> |
| 379 | #include <ftw.h> |
| 380 | #include <pwd.h> |
| 381 | - |
| 382 | -#include <selinux/label.h> |
| 383 | -#include <selinux/android.h> |
| 384 | +#include <unistd.h> |
| 385 | |
| 386 | #include <sys/stat.h> |
| 387 | #include <sys/types.h> |
| 388 | #include <sys/socket.h> |
| 389 | #include <sys/un.h> |
| 390 | |
| 391 | -#include <android-base/file.h> |
| 392 | -#include <android-base/strings.h> |
| 393 | - |
| 394 | /* for ANDROID_SOCKET_* */ |
| 395 | #include <cutils/sockets.h> |
| 396 | #include <android-base/stringprintf.h> |
| 397 | |
| 398 | -#include "init.h" |
| 399 | #include "log.h" |
| 400 | -#include "property_service.h" |
| 401 | #include "util.h" |
| 402 | |
| 403 | static unsigned int do_decode_uid(const char *s) |
| 404 | @@ -90,6 +84,7 @@ int create_socket(const char *name, int type, mode_t perm, uid_t uid, |
| 405 | { |
| 406 | struct sockaddr_un addr; |
| 407 | int fd, ret, savederrno; |
| 408 | +#if 0 |
| 409 | char *filecon; |
| 410 | |
| 411 | if (socketcon) { |
| 412 | @@ -98,6 +93,7 @@ int create_socket(const char *name, int type, mode_t perm, uid_t uid, |
| 413 | return -1; |
| 414 | } |
| 415 | } |
| 416 | +#endif |
| 417 | |
| 418 | fd = socket(PF_UNIX, type, 0); |
| 419 | if (fd < 0) { |
| 420 | @@ -105,8 +101,10 @@ int create_socket(const char *name, int type, mode_t perm, uid_t uid, |
| 421 | return -1; |
| 422 | } |
| 423 | |
| 424 | +#if 0 |
| 425 | if (socketcon) |
| 426 | setsockcreatecon(NULL); |
| 427 | +#endif |
| 428 | |
| 429 | memset(&addr, 0 , sizeof(addr)); |
| 430 | addr.sun_family = AF_UNIX; |
| 431 | @@ -119,18 +117,22 @@ int create_socket(const char *name, int type, mode_t perm, uid_t uid, |
| 432 | goto out_close; |
| 433 | } |
| 434 | |
| 435 | +#if 0 |
| 436 | filecon = NULL; |
| 437 | if (sehandle) { |
| 438 | ret = selabel_lookup(sehandle, &filecon, addr.sun_path, S_IFSOCK); |
| 439 | if (ret == 0) |
| 440 | setfscreatecon(filecon); |
| 441 | } |
| 442 | +#endif |
| 443 | |
| 444 | ret = bind(fd, (struct sockaddr *) &addr, sizeof (addr)); |
| 445 | savederrno = errno; |
| 446 | |
| 447 | +#if 0 |
| 448 | setfscreatecon(NULL); |
| 449 | freecon(filecon); |
| 450 | +#endif |
| 451 | |
| 452 | if (ret) { |
| 453 | ERROR("Failed to bind socket '%s': %s\n", name, strerror(savederrno)); |
| 454 | @@ -142,11 +144,13 @@ int create_socket(const char *name, int type, mode_t perm, uid_t uid, |
| 455 | ERROR("Failed to lchown socket '%s': %s\n", addr.sun_path, strerror(errno)); |
| 456 | goto out_unlink; |
| 457 | } |
| 458 | +#if 0 |
| 459 | ret = fchmodat(AT_FDCWD, addr.sun_path, perm, AT_SYMLINK_NOFOLLOW); |
| 460 | if (ret) { |
| 461 | ERROR("Failed to fchmodat socket '%s': %s\n", addr.sun_path, strerror(errno)); |
| 462 | goto out_unlink; |
| 463 | } |
| 464 | +#endif |
| 465 | |
| 466 | INFO("Created socket '%s' with mode '%o', user '%d', group '%d'\n", |
| 467 | addr.sun_path, perm, uid, gid); |
| 468 | @@ -180,11 +184,17 @@ bool read_file(const char* path, std::string* content) { |
| 469 | return false; |
| 470 | } |
| 471 | |
| 472 | - bool okay = android::base::ReadFdToString(fd, content); |
| 473 | + char buf[BUFSIZ]; |
| 474 | + ssize_t n; |
| 475 | + while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) { |
| 476 | + content->append(buf, n); |
| 477 | + } |
| 478 | + bool okay = n == 0; |
| 479 | close(fd); |
| 480 | return okay; |
| 481 | } |
| 482 | |
| 483 | +#if 0 |
| 484 | int write_file(const char* path, const char* content) { |
| 485 | int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY|O_CREAT|O_NOFOLLOW|O_CLOEXEC, 0600)); |
| 486 | if (fd == -1) { |
| 487 | @@ -276,6 +286,7 @@ time_t gettime() { |
| 488 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 489 | return now.tv_sec; |
| 490 | } |
| 491 | +#endif |
| 492 | |
| 493 | uint64_t gettime_ns() { |
| 494 | timespec now; |
| 495 | @@ -283,6 +294,7 @@ uint64_t gettime_ns() { |
| 496 | return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec; |
| 497 | } |
| 498 | |
| 499 | +#if 0 |
| 500 | int mkdir_recursive(const char *pathname, mode_t mode) |
| 501 | { |
| 502 | char buf[128]; |
| 503 | @@ -465,6 +477,7 @@ std::string bytes_to_hex(const uint8_t* bytes, size_t bytes_len) { |
| 504 | android::base::StringAppendF(&hex, "%02x", bytes[i]); |
| 505 | return hex; |
| 506 | } |
| 507 | +#endif |
| 508 | |
| 509 | /* |
| 510 | * Returns true is pathname is a directory |
| 511 | @@ -477,6 +490,7 @@ bool is_dir(const char* pathname) { |
| 512 | return S_ISDIR(info.st_mode); |
| 513 | } |
| 514 | |
| 515 | +#if 0 |
| 516 | bool expand_props(const std::string& src, std::string* dst) { |
| 517 | const char* src_ptr = src.c_str(); |
| 518 | |
| 519 | @@ -546,3 +560,4 @@ bool expand_props(const std::string& src, std::string* dst) { |
| 520 | |
| 521 | return true; |
| 522 | } |
| 523 | +#endif |