Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 1 | // Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 5 | #include "patchpanel/helper_process.h" |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 6 | |
| 7 | #include <signal.h> |
| 8 | #include <string.h> |
| 9 | #include <sys/socket.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | #include <utility> |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 13 | |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 14 | #include <base/check.h> |
| 15 | #include <base/check_op.h> |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 16 | #include <base/logging.h> |
| 17 | #include <base/process/launch.h> |
| 18 | #include <brillo/syslog_logging.h> |
| 19 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 20 | namespace patchpanel { |
Garrick Evans | 4c04257 | 2019-12-17 13:42:25 +0900 | [diff] [blame] | 21 | namespace { |
| 22 | constexpr int kMaxRestarts = 5; |
| 23 | } // namespace |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 24 | |
| 25 | void HelperProcess::Start(int argc, char* argv[], const std::string& fd_arg) { |
Garrick Evans | 4c04257 | 2019-12-17 13:42:25 +0900 | [diff] [blame] | 26 | CHECK_GE(argc, 1); |
| 27 | for (int i = 0; i < argc; i++) { |
| 28 | argv_.push_back(argv[i]); |
| 29 | } |
| 30 | fd_arg_ = fd_arg; |
| 31 | Launch(); |
| 32 | } |
| 33 | |
| 34 | bool HelperProcess::Restart() { |
| 35 | if (++restarts_ > kMaxRestarts) { |
| 36 | LOG(ERROR) << "Maximum number of restarts exceeded"; |
| 37 | return false; |
| 38 | } |
| 39 | LOG(INFO) << "Restarting..."; |
| 40 | Launch(); |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | void HelperProcess::Launch() { |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 45 | int control[2]; |
| 46 | |
| 47 | if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, control) != 0) { |
| 48 | PLOG(FATAL) << "socketpair failed"; |
| 49 | } |
| 50 | |
Taoyu Li | af944c9 | 2019-10-01 12:22:31 +0900 | [diff] [blame] | 51 | base::ScopedFD control_fd(control[0]); |
| 52 | msg_dispatcher_ = |
| 53 | std::make_unique<MessageDispatcher>(std::move(control_fd), false); |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 54 | const int subprocess_fd = control[1]; |
| 55 | |
Garrick Evans | 4c04257 | 2019-12-17 13:42:25 +0900 | [diff] [blame] | 56 | std::vector<std::string> child_argv = argv_; |
| 57 | child_argv.push_back(fd_arg_ + "=" + std::to_string(subprocess_fd)); |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 58 | |
| 59 | base::FileHandleMappingVector fd_mapping; |
| 60 | fd_mapping.push_back({subprocess_fd, subprocess_fd}); |
| 61 | |
| 62 | base::LaunchOptions options; |
Qijiang Fan | c98268c | 2019-10-31 20:54:42 +0900 | [diff] [blame] | 63 | options.fds_to_remap = std::move(fd_mapping); |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 64 | |
| 65 | base::Process p = base::LaunchProcess(child_argv, options); |
| 66 | CHECK(p.IsValid()); |
| 67 | pid_ = p.Pid(); |
| 68 | } |
| 69 | |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 70 | void HelperProcess::SendMessage( |
| 71 | const google::protobuf::MessageLite& proto) const { |
Taoyu Li | 77011cb | 2020-01-21 15:00:09 +0900 | [diff] [blame] | 72 | if (!msg_dispatcher_) { |
| 73 | return; |
| 74 | } |
Taoyu Li | af944c9 | 2019-10-01 12:22:31 +0900 | [diff] [blame] | 75 | msg_dispatcher_->SendMessage(proto); |
| 76 | } |
| 77 | |
| 78 | void HelperProcess::Listen() { |
Taoyu Li | 77011cb | 2020-01-21 15:00:09 +0900 | [diff] [blame] | 79 | if (!msg_dispatcher_) { |
| 80 | return; |
| 81 | } |
Taoyu Li | af944c9 | 2019-10-01 12:22:31 +0900 | [diff] [blame] | 82 | msg_dispatcher_->Start(); |
| 83 | } |
| 84 | |
Taoyu Li | a0727dc | 2020-09-24 19:54:59 +0900 | [diff] [blame] | 85 | void HelperProcess::RegisterNDProxyMessageHandler( |
| 86 | const base::Callback<void(const NDProxyMessage&)>& handler) { |
Taoyu Li | 77011cb | 2020-01-21 15:00:09 +0900 | [diff] [blame] | 87 | if (!msg_dispatcher_) { |
| 88 | return; |
| 89 | } |
Taoyu Li | a0727dc | 2020-09-24 19:54:59 +0900 | [diff] [blame] | 90 | msg_dispatcher_->RegisterNDProxyMessageHandler(handler); |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 93 | } // namespace patchpanel |