blob: 79609cd0f9ffa2d79097dce106f3c52bf9445ece [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;
28 virtual ~HelperProcess() = default;
29
Garrick Evans3388a032020-03-24 11:25:55 +090030 // Re-execs patchpanel with a new argument: "|fd_arg|=N", where N is the
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080031 // 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 Evans4c042572019-12-17 13:42:25 +090035 // 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 Cernekee27bcaa62016-12-03 11:16:26 -080039 // Serializes a protobuf and sends it to the helper process.
Garrick Evans49879532018-12-03 13:15:36 +090040 void SendMessage(const google::protobuf::MessageLite& proto) const;
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080041
Taoyu Liaf944c92019-10-01 12:22:31 +090042 // 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 Evans4c042572019-12-17 13:42:25 +090050 pid_t pid() const { return pid_; }
51 uint8_t restarts() const { return restarts_; }
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080052
53 private:
Garrick Evans4c042572019-12-17 13:42:25 +090054 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 Liaf944c92019-10-01 12:22:31 +090060 std::unique_ptr<MessageDispatcher> msg_dispatcher_;
Garrick Evans4c042572019-12-17 13:42:25 +090061
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080062 DISALLOW_COPY_AND_ASSIGN(HelperProcess);
63};
64
Garrick Evans3388a032020-03-24 11:25:55 +090065} // namespace patchpanel
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080066
Garrick Evans3388a032020-03-24 11:25:55 +090067#endif // PATCHPANEL_HELPER_PROCESS_H_