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 | #ifndef PATCHPANEL_HELPER_PROCESS_H_ |
| 6 | #define PATCHPANEL_HELPER_PROCESS_H_ |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 7 | |
| 8 | #include <sys/types.h> |
| 9 | |
Taoyu Li | af944c9 | 2019-10-01 12:22:31 +0900 | [diff] [blame] | 10 | #include <memory> |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 11 | #include <string> |
Garrick Evans | 4c04257 | 2019-12-17 13:42:25 +0900 | [diff] [blame] | 12 | #include <vector> |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 13 | |
| 14 | #include <base/files/scoped_file.h> |
| 15 | #include <base/macros.h> |
| 16 | #include <google/protobuf/message_lite.h> |
| 17 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 18 | #include "patchpanel/message_dispatcher.h" |
Taoyu Li | af944c9 | 2019-10-01 12:22:31 +0900 | [diff] [blame] | 19 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 20 | namespace patchpanel { |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 21 | |
| 22 | // Tracks a helper subprocess. Handles forking, cleaning up on termination, |
| 23 | // and IPC. |
| 24 | // This object is used by the main Manager process. |
| 25 | class HelperProcess { |
| 26 | public: |
| 27 | HelperProcess() = default; |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 28 | HelperProcess(const HelperProcess&) = delete; |
| 29 | HelperProcess& operator=(const HelperProcess&) = delete; |
| 30 | |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 31 | virtual ~HelperProcess() = default; |
| 32 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 33 | // Re-execs patchpanel with a new argument: "|fd_arg|=N", where N is the |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 34 | // side of |control_fd|. This tells the subprocess to start up a different |
| 35 | // mainloop. |
| 36 | void Start(int argc, char* argv[], const std::string& fd_arg); |
| 37 | |
Garrick Evans | 4c04257 | 2019-12-17 13:42:25 +0900 | [diff] [blame] | 38 | // Attempts to restart the process with the original arguments. |
| 39 | // Returns false if the maximum number of restarts has been exceeded. |
| 40 | bool Restart(); |
| 41 | |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 42 | // Serializes a protobuf and sends it to the helper process. |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 43 | void SendMessage(const google::protobuf::MessageLite& proto) const; |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 44 | |
Taoyu Li | af944c9 | 2019-10-01 12:22:31 +0900 | [diff] [blame] | 45 | // Start listening on messages from subprocess and dispatching them to |
| 46 | // handlers. This function can only be called after that the message loop of |
| 47 | // main process is initialized. |
| 48 | void Listen(); |
| 49 | |
Taoyu Li | a0727dc | 2020-09-24 19:54:59 +0900 | [diff] [blame] | 50 | void RegisterNDProxyMessageHandler( |
| 51 | const base::Callback<void(const NDProxyMessage&)>& handler); |
Taoyu Li | af944c9 | 2019-10-01 12:22:31 +0900 | [diff] [blame] | 52 | |
Garrick Evans | 4c04257 | 2019-12-17 13:42:25 +0900 | [diff] [blame] | 53 | pid_t pid() const { return pid_; } |
| 54 | uint8_t restarts() const { return restarts_; } |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 55 | |
| 56 | private: |
Garrick Evans | 4c04257 | 2019-12-17 13:42:25 +0900 | [diff] [blame] | 57 | void Launch(); |
| 58 | |
| 59 | pid_t pid_{0}; |
| 60 | uint8_t restarts_{0}; |
| 61 | std::vector<std::string> argv_; |
| 62 | std::string fd_arg_; |
Taoyu Li | af944c9 | 2019-10-01 12:22:31 +0900 | [diff] [blame] | 63 | std::unique_ptr<MessageDispatcher> msg_dispatcher_; |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 66 | } // namespace patchpanel |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 67 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 68 | #endif // PATCHPANEL_HELPER_PROCESS_H_ |