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 | |
Hidehiko Abe | 3a7e513 | 2018-02-15 13:07:50 +0900 | [diff] [blame] | 5 | #ifndef ARC_NETWORK_HELPER_PROCESS_H_ |
| 6 | #define ARC_NETWORK_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> |
| 12 | |
| 13 | #include <base/files/scoped_file.h> |
| 14 | #include <base/macros.h> |
| 15 | #include <google/protobuf/message_lite.h> |
| 16 | |
Taoyu Li | af944c9 | 2019-10-01 12:22:31 +0900 | [diff] [blame^] | 17 | #include "arc/network/message_dispatcher.h" |
| 18 | |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 19 | namespace arc_networkd { |
| 20 | |
| 21 | // Tracks a helper subprocess. Handles forking, cleaning up on termination, |
| 22 | // and IPC. |
| 23 | // This object is used by the main Manager process. |
| 24 | class HelperProcess { |
| 25 | public: |
| 26 | HelperProcess() = default; |
| 27 | virtual ~HelperProcess() = default; |
| 28 | |
| 29 | // Re-execs arc-networkd with a new argument: "|fd_arg|=N", where N is the |
| 30 | // side of |control_fd|. This tells the subprocess to start up a different |
| 31 | // mainloop. |
| 32 | void Start(int argc, char* argv[], const std::string& fd_arg); |
| 33 | |
| 34 | // Serializes a protobuf and sends it to the helper process. |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 35 | void SendMessage(const google::protobuf::MessageLite& proto) const; |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 36 | |
Taoyu Li | af944c9 | 2019-10-01 12:22:31 +0900 | [diff] [blame^] | 37 | // Start listening on messages from subprocess and dispatching them to |
| 38 | // handlers. This function can only be called after that the message loop of |
| 39 | // main process is initialized. |
| 40 | void Listen(); |
| 41 | |
| 42 | void RegisterDeviceMessageHandler( |
| 43 | const base::Callback<void(const DeviceMessage&)>& handler); |
| 44 | |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 45 | const pid_t pid() { return pid_; } |
| 46 | |
| 47 | protected: |
| 48 | pid_t pid_{0}; |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 49 | |
| 50 | private: |
Taoyu Li | af944c9 | 2019-10-01 12:22:31 +0900 | [diff] [blame^] | 51 | std::unique_ptr<MessageDispatcher> msg_dispatcher_; |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 52 | DISALLOW_COPY_AND_ASSIGN(HelperProcess); |
| 53 | }; |
| 54 | |
| 55 | } // namespace arc_networkd |
| 56 | |
Hidehiko Abe | 3a7e513 | 2018-02-15 13:07:50 +0900 | [diff] [blame] | 57 | #endif // ARC_NETWORK_HELPER_PROCESS_H_ |