blob: 26b88c9179b3a11a01a177c5df9e06fa54e4489a [file] [log] [blame]
Ken Turneredb93932018-02-14 05:27:31 +11001// Copyright 2018 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
5#include "ml/daemon.h"
6
Andrew Moylanef116f92018-04-11 15:12:38 +10007#include <sysexits.h>
8
9#include <memory>
10#include <utility>
11
12#include <base/bind.h>
Andrew Moylanff6be512018-07-03 11:05:01 +100013#include <base/files/file_util.h>
14#include <base/memory/ptr_util.h>
Andrew Moylanef116f92018-04-11 15:12:38 +100015#include <base/memory/ref_counted.h>
16#include <chromeos/dbus/service_constants.h>
17#include <dbus/bus.h>
18#include <dbus/message.h>
Qijiang Fan7da812d2019-11-26 10:48:46 +090019#include <mojo/core/embedder/embedder.h>
Qijiang Fan3b917c72019-12-09 15:24:15 +090020#include <mojo/public/cpp/system/invitation.h>
Andrew Moylanff6be512018-07-03 11:05:01 +100021
22#include "ml/machine_learning_service_impl.h"
Andrew Moylanef116f92018-04-11 15:12:38 +100023
Ken Turneredb93932018-02-14 05:27:31 +110024namespace ml {
25
Andrew Moylanef116f92018-04-11 15:12:38 +100026Daemon::Daemon() : weak_ptr_factory_(this) {}
Ken Turneredb93932018-02-14 05:27:31 +110027
28Daemon::~Daemon() {}
29
30int Daemon::OnInit() {
Andrew Moylanef116f92018-04-11 15:12:38 +100031 int exit_code = DBusDaemon::OnInit();
32 if (exit_code != EX_OK)
33 return exit_code;
34
Andrew Moylan40ee4fc2018-08-24 15:46:09 +100035 metrics_.StartCollectingProcessMetrics();
Qijiang Fan7da812d2019-11-26 10:48:46 +090036 mojo::core::Init();
37 ipc_support_ = std::make_unique<mojo::core::ScopedIPCSupport>(
38 base::ThreadTaskRunnerHandle::Get(),
39 mojo::core::ScopedIPCSupport::ShutdownPolicy::FAST);
Ken Turneredb93932018-02-14 05:27:31 +110040 InitDBus();
Andrew Moylanff6be512018-07-03 11:05:01 +100041
Ken Turneredb93932018-02-14 05:27:31 +110042 return 0;
43}
44
45void Daemon::InitDBus() {
Andrew Moylanef116f92018-04-11 15:12:38 +100046 // Get or create the ExportedObject for the ML service.
47 dbus::ExportedObject* const ml_service_exported_object =
Andrew Moylan49f50b32018-07-27 08:48:11 +100048 bus_->GetExportedObject(dbus::ObjectPath(kMachineLearningServicePath));
Andrew Moylanef116f92018-04-11 15:12:38 +100049 CHECK(ml_service_exported_object);
50
51 // Register a handler of the BootstrapMojoConnection method.
52 CHECK(ml_service_exported_object->ExportMethodAndBlock(
Andrew Moylan49f50b32018-07-27 08:48:11 +100053 kMachineLearningInterfaceName, kBootstrapMojoConnectionMethod,
Andrew Moylanef116f92018-04-11 15:12:38 +100054 base::Bind(&Daemon::BootstrapMojoConnection,
55 weak_ptr_factory_.GetWeakPtr())));
56
57 // Take ownership of the ML service.
Andrew Moylan49f50b32018-07-27 08:48:11 +100058 CHECK(bus_->RequestOwnershipAndBlock(kMachineLearningServiceName,
Andrew Moylanef116f92018-04-11 15:12:38 +100059 dbus::Bus::REQUIRE_PRIMARY));
Ken Turneredb93932018-02-14 05:27:31 +110060}
61
62void Daemon::BootstrapMojoConnection(
63 dbus::MethodCall* method_call,
64 dbus::ExportedObject::ResponseSender response_sender) {
Andrew Moylan40ee4fc2018-08-24 15:46:09 +100065 metrics_.RecordMojoConnectionEvent(
66 Metrics::MojoConnectionEvent::kBootstrapRequested);
Andrew Moylanff6be512018-07-03 11:05:01 +100067 if (machine_learning_service_) {
68 LOG(ERROR) << "MachineLearningService already instantiated";
69 response_sender.Run(dbus::ErrorResponse::FromMethodCall(
70 method_call, DBUS_ERROR_FAILED, "Bootstrap already completed"));
71 return;
72 }
Andrew Moylanef116f92018-04-11 15:12:38 +100073
Andrew Moylanff6be512018-07-03 11:05:01 +100074 base::ScopedFD file_handle;
75 dbus::MessageReader reader(method_call);
Andrew Moylanef116f92018-04-11 15:12:38 +100076
Andrew Moylanff6be512018-07-03 11:05:01 +100077 if (!reader.PopFileDescriptor(&file_handle)) {
78 LOG(ERROR) << "Couldn't extract file descriptor from D-Bus call";
79 response_sender.Run(dbus::ErrorResponse::FromMethodCall(
80 method_call, DBUS_ERROR_INVALID_ARGS, "Expected file descriptor"));
81 return;
82 }
83
84 if (!file_handle.is_valid()) {
85 LOG(ERROR) << "ScopedFD extracted from D-Bus call was invalid (i.e. empty)";
86 response_sender.Run(dbus::ErrorResponse::FromMethodCall(
87 method_call, DBUS_ERROR_INVALID_ARGS,
88 "Invalid (empty) file descriptor"));
89 return;
90 }
91
92 if (!base::SetCloseOnExec(file_handle.get())) {
93 PLOG(ERROR) << "Failed setting FD_CLOEXEC on file descriptor";
94 response_sender.Run(dbus::ErrorResponse::FromMethodCall(
95 method_call, DBUS_ERROR_FAILED,
96 "Failed setting FD_CLOEXEC on file descriptor"));
97 return;
98 }
99
100 // Connect to mojo in the requesting process.
Qijiang Fan3b917c72019-12-09 15:24:15 +0900101 mojo::IncomingInvitation invitation =
102 mojo::IncomingInvitation::Accept(mojo::PlatformChannelEndpoint(
103 mojo::PlatformHandle(std::move(file_handle))));
Andrew Moylanff6be512018-07-03 11:05:01 +1000104
105 // Bind primordial message pipe to a MachineLearningService implementation.
106 machine_learning_service_ = base::MakeUnique<MachineLearningServiceImpl>(
Qijiang Fan3b917c72019-12-09 15:24:15 +0900107 invitation.ExtractMessagePipe(kBootstrapMojoConnectionChannelToken),
Andrew Moylanff6be512018-07-03 11:05:01 +1000108 base::Bind(&Daemon::OnConnectionError, base::Unretained(this)));
109
Andrew Moylan40ee4fc2018-08-24 15:46:09 +1000110 metrics_.RecordMojoConnectionEvent(
111 Metrics::MojoConnectionEvent::kBootstrapSucceeded);
112
Andrew Moylanff6be512018-07-03 11:05:01 +1000113 // Send success response.
114 response_sender.Run(dbus::Response::FromMethodCall(method_call));
115}
116
117void Daemon::OnConnectionError() {
Andrew Moylan40ee4fc2018-08-24 15:46:09 +1000118 metrics_.RecordMojoConnectionEvent(
119 Metrics::MojoConnectionEvent::kConnectionError);
Andrew Moylanff6be512018-07-03 11:05:01 +1000120 // Die upon Mojo error. Reconnection can occur when the daemon is restarted.
121 // (A future Mojo API may enable Mojo re-bootstrap without a process restart.)
122 Quit();
Ken Turneredb93932018-02-14 05:27:31 +1100123}
124
125} // namespace ml