blob: 43e35b650c3b57485380e917f446f41257868b3e [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
Hidehiko Abe3a7e5132018-02-15 13:07:50 +09005#ifndef ARC_NETWORK_HELPER_PROCESS_H_
6#define ARC_NETWORK_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>
12
13#include <base/files/scoped_file.h>
14#include <base/macros.h>
15#include <google/protobuf/message_lite.h>
16
Taoyu Liaf944c92019-10-01 12:22:31 +090017#include "arc/network/message_dispatcher.h"
18
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080019namespace 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.
24class 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 Evans49879532018-12-03 13:15:36 +090035 void SendMessage(const google::protobuf::MessageLite& proto) const;
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080036
Taoyu Liaf944c92019-10-01 12:22:31 +090037 // 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 Cernekee27bcaa62016-12-03 11:16:26 -080045 const pid_t pid() { return pid_; }
46
47 protected:
48 pid_t pid_{0};
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080049
50 private:
Taoyu Liaf944c92019-10-01 12:22:31 +090051 std::unique_ptr<MessageDispatcher> msg_dispatcher_;
Kevin Cernekee27bcaa62016-12-03 11:16:26 -080052 DISALLOW_COPY_AND_ASSIGN(HelperProcess);
53};
54
55} // namespace arc_networkd
56
Hidehiko Abe3a7e5132018-02-15 13:07:50 +090057#endif // ARC_NETWORK_HELPER_PROCESS_H_