blob: 57f1a538ecf602d461a3eb73c386c7f50acffd22 [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>
Andrew Moylanff6be512018-07-03 11:05:01 +100019#include <mojo/edk/embedder/embedder.h>
20#include <mojo/edk/embedder/process_delegate.h>
21
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 Moylanff6be512018-07-03 11:05:01 +100026namespace {
27
28// A ProcessDelegate that does nothing upon IPC system shutdown.
29class DoNothingProcessDelegate : public mojo::edk::ProcessDelegate {
30 public:
31 void OnShutdownComplete() override {}
32};
33
34void InitMojo() {
35 mojo::edk::Init();
36 static DoNothingProcessDelegate process_delegate;
37 mojo::edk::InitIPCSupport(&process_delegate,
38 base::ThreadTaskRunnerHandle::Get());
39}
40
41} // namespace
42
Andrew Moylanef116f92018-04-11 15:12:38 +100043Daemon::Daemon() : weak_ptr_factory_(this) {}
Ken Turneredb93932018-02-14 05:27:31 +110044
45Daemon::~Daemon() {}
46
47int Daemon::OnInit() {
Andrew Moylanef116f92018-04-11 15:12:38 +100048 int exit_code = DBusDaemon::OnInit();
49 if (exit_code != EX_OK)
50 return exit_code;
51
Andrew Moylan40ee4fc2018-08-24 15:46:09 +100052 metrics_.StartCollectingProcessMetrics();
Andrew Moylanff6be512018-07-03 11:05:01 +100053 InitMojo();
Ken Turneredb93932018-02-14 05:27:31 +110054 InitDBus();
Andrew Moylanff6be512018-07-03 11:05:01 +100055
Ken Turneredb93932018-02-14 05:27:31 +110056 return 0;
57}
58
59void Daemon::InitDBus() {
Andrew Moylanef116f92018-04-11 15:12:38 +100060 // Get or create the ExportedObject for the ML service.
61 dbus::ExportedObject* const ml_service_exported_object =
Andrew Moylan49f50b32018-07-27 08:48:11 +100062 bus_->GetExportedObject(dbus::ObjectPath(kMachineLearningServicePath));
Andrew Moylanef116f92018-04-11 15:12:38 +100063 CHECK(ml_service_exported_object);
64
65 // Register a handler of the BootstrapMojoConnection method.
66 CHECK(ml_service_exported_object->ExportMethodAndBlock(
Andrew Moylan49f50b32018-07-27 08:48:11 +100067 kMachineLearningInterfaceName, kBootstrapMojoConnectionMethod,
Andrew Moylanef116f92018-04-11 15:12:38 +100068 base::Bind(&Daemon::BootstrapMojoConnection,
69 weak_ptr_factory_.GetWeakPtr())));
70
71 // Take ownership of the ML service.
Andrew Moylan49f50b32018-07-27 08:48:11 +100072 CHECK(bus_->RequestOwnershipAndBlock(kMachineLearningServiceName,
Andrew Moylanef116f92018-04-11 15:12:38 +100073 dbus::Bus::REQUIRE_PRIMARY));
Ken Turneredb93932018-02-14 05:27:31 +110074}
75
76void Daemon::BootstrapMojoConnection(
77 dbus::MethodCall* method_call,
78 dbus::ExportedObject::ResponseSender response_sender) {
Andrew Moylan40ee4fc2018-08-24 15:46:09 +100079 metrics_.RecordMojoConnectionEvent(
80 Metrics::MojoConnectionEvent::kBootstrapRequested);
Andrew Moylanff6be512018-07-03 11:05:01 +100081 if (machine_learning_service_) {
82 LOG(ERROR) << "MachineLearningService already instantiated";
83 response_sender.Run(dbus::ErrorResponse::FromMethodCall(
84 method_call, DBUS_ERROR_FAILED, "Bootstrap already completed"));
85 return;
86 }
Andrew Moylanef116f92018-04-11 15:12:38 +100087
Andrew Moylanff6be512018-07-03 11:05:01 +100088 base::ScopedFD file_handle;
89 dbus::MessageReader reader(method_call);
Andrew Moylanef116f92018-04-11 15:12:38 +100090
Andrew Moylanff6be512018-07-03 11:05:01 +100091 if (!reader.PopFileDescriptor(&file_handle)) {
92 LOG(ERROR) << "Couldn't extract file descriptor from D-Bus call";
93 response_sender.Run(dbus::ErrorResponse::FromMethodCall(
94 method_call, DBUS_ERROR_INVALID_ARGS, "Expected file descriptor"));
95 return;
96 }
97
98 if (!file_handle.is_valid()) {
99 LOG(ERROR) << "ScopedFD extracted from D-Bus call was invalid (i.e. empty)";
100 response_sender.Run(dbus::ErrorResponse::FromMethodCall(
101 method_call, DBUS_ERROR_INVALID_ARGS,
102 "Invalid (empty) file descriptor"));
103 return;
104 }
105
106 if (!base::SetCloseOnExec(file_handle.get())) {
107 PLOG(ERROR) << "Failed setting FD_CLOEXEC on file descriptor";
108 response_sender.Run(dbus::ErrorResponse::FromMethodCall(
109 method_call, DBUS_ERROR_FAILED,
110 "Failed setting FD_CLOEXEC on file descriptor"));
111 return;
112 }
113
114 // Connect to mojo in the requesting process.
115 mojo::edk::SetParentPipeHandle(mojo::edk::ScopedPlatformHandle(
116 mojo::edk::PlatformHandle(file_handle.release())));
117
118 // Bind primordial message pipe to a MachineLearningService implementation.
119 machine_learning_service_ = base::MakeUnique<MachineLearningServiceImpl>(
120 mojo::edk::CreateChildMessagePipe(kBootstrapMojoConnectionChannelToken),
121 base::Bind(&Daemon::OnConnectionError, base::Unretained(this)));
122
Andrew Moylan40ee4fc2018-08-24 15:46:09 +1000123 metrics_.RecordMojoConnectionEvent(
124 Metrics::MojoConnectionEvent::kBootstrapSucceeded);
125
Andrew Moylanff6be512018-07-03 11:05:01 +1000126 // Send success response.
127 response_sender.Run(dbus::Response::FromMethodCall(method_call));
128}
129
130void Daemon::OnConnectionError() {
Andrew Moylan40ee4fc2018-08-24 15:46:09 +1000131 metrics_.RecordMojoConnectionEvent(
132 Metrics::MojoConnectionEvent::kConnectionError);
Andrew Moylanff6be512018-07-03 11:05:01 +1000133 // Die upon Mojo error. Reconnection can occur when the daemon is restarted.
134 // (A future Mojo API may enable Mojo re-bootstrap without a process restart.)
135 Quit();
Ken Turneredb93932018-02-14 05:27:31 +1100136}
137
138} // namespace ml