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