blob: 14101cc23428a6f5213f981079ae8f5be3e13541 [file] [log] [blame]
Kevin Cernekee27bcaa62016-12-03 11:16:26 -08001// 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 Evans3388a032020-03-24 11:25:55 +09005#ifndef PATCHPANEL_HELPER_PROCESS_H_
6#define PATCHPANEL_HELPER_PROCESS_H_
Kevin Cernekee27bcaa62016-12-03 11:16:26 -08007
8#include <sys/types.h>
9
Taoyu Liaf944c92019-10-01 12:22:31 +090010#include <memory>
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080011#include <string>
Garrick Evans4c042572019-12-17 13:42:25 +090012#include <vector>
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080013
14#include <base/files/scoped_file.h>
15#include <base/macros.h>
16#include <google/protobuf/message_lite.h>
17
Garrick Evans3388a032020-03-24 11:25:55 +090018#include "patchpanel/message_dispatcher.h"
Taoyu Liaf944c92019-10-01 12:22:31 +090019
Garrick Evans3388a032020-03-24 11:25:55 +090020namespace patchpanel {
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080021
22// Tracks a helper subprocess. Handles forking, cleaning up on termination,
23// and IPC.
24// This object is used by the main Manager process.
25class HelperProcess {
26 public:
27 HelperProcess() = default;
Qijiang Fan6bc59e12020-11-11 02:51:06 +090028 HelperProcess(const HelperProcess&) = delete;
29 HelperProcess& operator=(const HelperProcess&) = delete;
30
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080031 virtual ~HelperProcess() = default;
32
Garrick Evans3388a032020-03-24 11:25:55 +090033 // Re-execs patchpanel with a new argument: "|fd_arg|=N", where N is the
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080034 // 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 Evans4c042572019-12-17 13:42:25 +090038 // 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 Cernekee27bcaa62016-12-03 11:16:26 -080042 // Serializes a protobuf and sends it to the helper process.
Garrick Evans49879532018-12-03 13:15:36 +090043 void SendMessage(const google::protobuf::MessageLite& proto) const;
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080044
Taoyu Liaf944c92019-10-01 12:22:31 +090045 // 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 Lia0727dc2020-09-24 19:54:59 +090050 void RegisterNDProxyMessageHandler(
51 const base::Callback<void(const NDProxyMessage&)>& handler);
Taoyu Liaf944c92019-10-01 12:22:31 +090052
Garrick Evans4c042572019-12-17 13:42:25 +090053 pid_t pid() const { return pid_; }
54 uint8_t restarts() const { return restarts_; }
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080055
56 private:
Garrick Evans4c042572019-12-17 13:42:25 +090057 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 Liaf944c92019-10-01 12:22:31 +090063 std::unique_ptr<MessageDispatcher> msg_dispatcher_;
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080064};
65
Garrick Evans3388a032020-03-24 11:25:55 +090066} // namespace patchpanel
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080067
Garrick Evans3388a032020-03-24 11:25:55 +090068#endif // PATCHPANEL_HELPER_PROCESS_H_