blob: 67a638adc311db6d16a07ed6e6580a4b07a5f9ef [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>
Andrew Moylanef116f92018-04-11 15:12:38 +100014#include <base/memory/ref_counted.h>
15#include <chromeos/dbus/service_constants.h>
16#include <dbus/bus.h>
17#include <dbus/message.h>
Qijiang Fan7da812d2019-11-26 10:48:46 +090018#include <mojo/core/embedder/embedder.h>
Qijiang Fan3b917c72019-12-09 15:24:15 +090019#include <mojo/public/cpp/system/invitation.h>
Andrew Moylanff6be512018-07-03 11:05:01 +100020
21#include "ml/machine_learning_service_impl.h"
Andrew Moylanef116f92018-04-11 15:12:38 +100022
Ken Turneredb93932018-02-14 05:27:31 +110023namespace ml {
24
Andrew Moylanef116f92018-04-11 15:12:38 +100025Daemon::Daemon() : weak_ptr_factory_(this) {}
Ken Turneredb93932018-02-14 05:27:31 +110026
27Daemon::~Daemon() {}
28
29int Daemon::OnInit() {
Andrew Moylanef116f92018-04-11 15:12:38 +100030 int exit_code = DBusDaemon::OnInit();
31 if (exit_code != EX_OK)
32 return exit_code;
33
Andrew Moylan40ee4fc2018-08-24 15:46:09 +100034 metrics_.StartCollectingProcessMetrics();
Qijiang Fan7da812d2019-11-26 10:48:46 +090035 mojo::core::Init();
36 ipc_support_ = std::make_unique<mojo::core::ScopedIPCSupport>(
37 base::ThreadTaskRunnerHandle::Get(),
38 mojo::core::ScopedIPCSupport::ShutdownPolicy::FAST);
Ken Turneredb93932018-02-14 05:27:31 +110039 InitDBus();
Andrew Moylanff6be512018-07-03 11:05:01 +100040
Ken Turneredb93932018-02-14 05:27:31 +110041 return 0;
42}
43
44void Daemon::InitDBus() {
Andrew Moylanef116f92018-04-11 15:12:38 +100045 // Get or create the ExportedObject for the ML service.
46 dbus::ExportedObject* const ml_service_exported_object =
Andrew Moylan49f50b32018-07-27 08:48:11 +100047 bus_->GetExportedObject(dbus::ObjectPath(kMachineLearningServicePath));
Andrew Moylanef116f92018-04-11 15:12:38 +100048 CHECK(ml_service_exported_object);
49
50 // Register a handler of the BootstrapMojoConnection method.
51 CHECK(ml_service_exported_object->ExportMethodAndBlock(
Andrew Moylan49f50b32018-07-27 08:48:11 +100052 kMachineLearningInterfaceName, kBootstrapMojoConnectionMethod,
Andrew Moylanef116f92018-04-11 15:12:38 +100053 base::Bind(&Daemon::BootstrapMojoConnection,
54 weak_ptr_factory_.GetWeakPtr())));
55
56 // Take ownership of the ML service.
Andrew Moylan49f50b32018-07-27 08:48:11 +100057 CHECK(bus_->RequestOwnershipAndBlock(kMachineLearningServiceName,
Andrew Moylanef116f92018-04-11 15:12:38 +100058 dbus::Bus::REQUIRE_PRIMARY));
Ken Turneredb93932018-02-14 05:27:31 +110059}
60
61void Daemon::BootstrapMojoConnection(
62 dbus::MethodCall* method_call,
63 dbus::ExportedObject::ResponseSender response_sender) {
Andrew Moylan40ee4fc2018-08-24 15:46:09 +100064 metrics_.RecordMojoConnectionEvent(
65 Metrics::MojoConnectionEvent::kBootstrapRequested);
Andrew Moylanff6be512018-07-03 11:05:01 +100066 if (machine_learning_service_) {
67 LOG(ERROR) << "MachineLearningService already instantiated";
Qijiang Fan795b81f2020-07-06 12:36:18 +090068 std::move(response_sender)
69 .Run(dbus::ErrorResponse::FromMethodCall(
70 method_call, DBUS_ERROR_FAILED, "Bootstrap already completed"));
Andrew Moylanff6be512018-07-03 11:05:01 +100071 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";
Qijiang Fan795b81f2020-07-06 12:36:18 +090079 std::move(response_sender)
80 .Run(dbus::ErrorResponse::FromMethodCall(
81 method_call, DBUS_ERROR_INVALID_ARGS, "Expected file descriptor"));
Andrew Moylanff6be512018-07-03 11:05:01 +100082 return;
83 }
84
85 if (!file_handle.is_valid()) {
86 LOG(ERROR) << "ScopedFD extracted from D-Bus call was invalid (i.e. empty)";
Qijiang Fan795b81f2020-07-06 12:36:18 +090087 std::move(response_sender)
88 .Run(dbus::ErrorResponse::FromMethodCall(
89 method_call, DBUS_ERROR_INVALID_ARGS,
90 "Invalid (empty) file descriptor"));
Andrew Moylanff6be512018-07-03 11:05:01 +100091 return;
92 }
93
94 if (!base::SetCloseOnExec(file_handle.get())) {
95 PLOG(ERROR) << "Failed setting FD_CLOEXEC on file descriptor";
Qijiang Fan795b81f2020-07-06 12:36:18 +090096 std::move(response_sender)
97 .Run(dbus::ErrorResponse::FromMethodCall(
98 method_call, DBUS_ERROR_FAILED,
99 "Failed setting FD_CLOEXEC on file descriptor"));
Andrew Moylanff6be512018-07-03 11:05:01 +1000100 return;
101 }
102
103 // Connect to mojo in the requesting process.
Qijiang Fan3b917c72019-12-09 15:24:15 +0900104 mojo::IncomingInvitation invitation =
105 mojo::IncomingInvitation::Accept(mojo::PlatformChannelEndpoint(
106 mojo::PlatformHandle(std::move(file_handle))));
Andrew Moylanff6be512018-07-03 11:05:01 +1000107
108 // Bind primordial message pipe to a MachineLearningService implementation.
hscham3f45fcc2019-12-25 16:54:54 +0900109 machine_learning_service_ = std::make_unique<MachineLearningServiceImpl>(
Qijiang Fan3b917c72019-12-09 15:24:15 +0900110 invitation.ExtractMessagePipe(kBootstrapMojoConnectionChannelToken),
Andrew Moylanb481af72020-07-09 15:22:00 +1000111 base::Bind(&Daemon::OnMojoDisconnection, base::Unretained(this)));
Andrew Moylanff6be512018-07-03 11:05:01 +1000112
Andrew Moylan40ee4fc2018-08-24 15:46:09 +1000113 metrics_.RecordMojoConnectionEvent(
114 Metrics::MojoConnectionEvent::kBootstrapSucceeded);
115
Andrew Moylanff6be512018-07-03 11:05:01 +1000116 // Send success response.
Qijiang Fan795b81f2020-07-06 12:36:18 +0900117 std::move(response_sender).Run(dbus::Response::FromMethodCall(method_call));
Andrew Moylanff6be512018-07-03 11:05:01 +1000118}
119
Andrew Moylanb481af72020-07-09 15:22:00 +1000120void Daemon::OnMojoDisconnection() {
Andrew Moylan40ee4fc2018-08-24 15:46:09 +1000121 metrics_.RecordMojoConnectionEvent(
Andrew Moylanb481af72020-07-09 15:22:00 +1000122 Metrics::MojoConnectionEvent::kConnectionClosed);
123 // Die upon disconnection . Reconnection can occur when the daemon is
124 // restarted. (A future Mojo API may enable Mojo re-bootstrap without a
125 // process restart.)
Andrew Moylanff6be512018-07-03 11:05:01 +1000126 Quit();
Ken Turneredb93932018-02-14 05:27:31 +1100127}
128
129} // namespace ml