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; |
| 28 | virtual ~HelperProcess() = default; |
| 29 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 30 | // 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] | 31 | // side of |control_fd|. This tells the subprocess to start up a different |
| 32 | // mainloop. |
| 33 | void Start(int argc, char* argv[], const std::string& fd_arg); |
| 34 | |
Garrick Evans | 4c04257 | 2019-12-17 13:42:25 +0900 | [diff] [blame] | 35 | // Attempts to restart the process with the original arguments. |
| 36 | // Returns false if the maximum number of restarts has been exceeded. |
| 37 | bool Restart(); |
| 38 | |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 39 | // Serializes a protobuf and sends it to the helper process. |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 40 | void SendMessage(const google::protobuf::MessageLite& proto) const; |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 41 | |
Taoyu Li | af944c9 | 2019-10-01 12:22:31 +0900 | [diff] [blame] | 42 | // Start listening on messages from subprocess and dispatching them to |
| 43 | // handlers. This function can only be called after that the message loop of |
| 44 | // main process is initialized. |
| 45 | void Listen(); |
| 46 | |
| 47 | void RegisterDeviceMessageHandler( |
| 48 | const base::Callback<void(const DeviceMessage&)>& handler); |
| 49 | |
Garrick Evans | 4c04257 | 2019-12-17 13:42:25 +0900 | [diff] [blame] | 50 | pid_t pid() const { return pid_; } |
| 51 | uint8_t restarts() const { return restarts_; } |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 52 | |
| 53 | private: |
Garrick Evans | 4c04257 | 2019-12-17 13:42:25 +0900 | [diff] [blame] | 54 | void Launch(); |
| 55 | |
| 56 | pid_t pid_{0}; |
| 57 | uint8_t restarts_{0}; |
| 58 | std::vector<std::string> argv_; |
| 59 | std::string fd_arg_; |
Taoyu Li | af944c9 | 2019-10-01 12:22:31 +0900 | [diff] [blame] | 60 | std::unique_ptr<MessageDispatcher> msg_dispatcher_; |
Garrick Evans | 4c04257 | 2019-12-17 13:42:25 +0900 | [diff] [blame] | 61 | |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 62 | DISALLOW_COPY_AND_ASSIGN(HelperProcess); |
| 63 | }; |
| 64 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 65 | } // namespace patchpanel |
Kevin Cernekee | 27bcaa6 | 2016-12-03 11:16:26 -0800 | [diff] [blame] | 66 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 67 | #endif // PATCHPANEL_HELPER_PROCESS_H_ |