blob: 49cec29e2c248e8ebced7f03979b7e995dba9435 [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 <memory>
8#include <utility>
9
Honglin Yu7b6c1192020-09-16 10:07:17 +100010#include <sys/types.h>
11#include <sysexits.h>
12#include <unistd.h>
13
Andrew Moylanef116f92018-04-11 15:12:38 +100014#include <base/bind.h>
Qijiang Fan713061e2021-03-08 15:45:12 +090015#include <base/check.h>
Andrew Moylanff6be512018-07-03 11:05:01 +100016#include <base/files/file_util.h>
Andrew Moylanef116f92018-04-11 15:12:38 +100017#include <base/memory/ref_counted.h>
18#include <chromeos/dbus/service_constants.h>
19#include <dbus/bus.h>
20#include <dbus/message.h>
Qijiang Fan7da812d2019-11-26 10:48:46 +090021#include <mojo/core/embedder/embedder.h>
Qijiang Fan3b917c72019-12-09 15:24:15 +090022#include <mojo/public/cpp/system/invitation.h>
Andrew Moylanff6be512018-07-03 11:05:01 +100023
Charles Zhaod4fb7b62020-08-25 17:21:58 +100024#include "ml/dlcservice_client.h"
Andrew Moylanff6be512018-07-03 11:05:01 +100025#include "ml/machine_learning_service_impl.h"
Andrew Moylanef116f92018-04-11 15:12:38 +100026
Ken Turneredb93932018-02-14 05:27:31 +110027namespace ml {
28
Andrew Moylanef116f92018-04-11 15:12:38 +100029Daemon::Daemon() : weak_ptr_factory_(this) {}
Ken Turneredb93932018-02-14 05:27:31 +110030
31Daemon::~Daemon() {}
32
33int Daemon::OnInit() {
Andrew Moylanef116f92018-04-11 15:12:38 +100034 int exit_code = DBusDaemon::OnInit();
Honglin Yu7b6c1192020-09-16 10:07:17 +100035 if (exit_code != EX_OK) {
36 LOG(ERROR) << "DBusDaemon::OnInit() failed";
Andrew Moylanef116f92018-04-11 15:12:38 +100037 return exit_code;
Honglin Yu7b6c1192020-09-16 10:07:17 +100038 }
39 // For control process, we need to change euid back to 0. We need the control
40 // process to be root in the user namespace because it needs to spawn worker
41 // processes and sandbox them.
42 if (seteuid(0) != 0) {
43 // TODO(https://crbug.com/1202545): report this error to UMA.
44 LOG(ERROR) << "Unable to change effective uid back to 0";
45 exit(EX_OSERR);
46 }
Andrew Moylanef116f92018-04-11 15:12:38 +100047
Andrew Moylan40ee4fc2018-08-24 15:46:09 +100048 metrics_.StartCollectingProcessMetrics();
Qijiang Fan7da812d2019-11-26 10:48:46 +090049 mojo::core::Init();
50 ipc_support_ = std::make_unique<mojo::core::ScopedIPCSupport>(
51 base::ThreadTaskRunnerHandle::Get(),
52 mojo::core::ScopedIPCSupport::ShutdownPolicy::FAST);
Ken Turneredb93932018-02-14 05:27:31 +110053 InitDBus();
Andrew Moylanff6be512018-07-03 11:05:01 +100054
Ken Turneredb93932018-02-14 05:27:31 +110055 return 0;
56}
57
58void Daemon::InitDBus() {
Andrew Moylanef116f92018-04-11 15:12:38 +100059 // Get or create the ExportedObject for the ML service.
60 dbus::ExportedObject* const ml_service_exported_object =
Andrew Moylan49f50b32018-07-27 08:48:11 +100061 bus_->GetExportedObject(dbus::ObjectPath(kMachineLearningServicePath));
Andrew Moylanef116f92018-04-11 15:12:38 +100062 CHECK(ml_service_exported_object);
63
64 // Register a handler of the BootstrapMojoConnection method.
65 CHECK(ml_service_exported_object->ExportMethodAndBlock(
Andrew Moylan49f50b32018-07-27 08:48:11 +100066 kMachineLearningInterfaceName, kBootstrapMojoConnectionMethod,
Andrew Moylanef116f92018-04-11 15:12:38 +100067 base::Bind(&Daemon::BootstrapMojoConnection,
68 weak_ptr_factory_.GetWeakPtr())));
69
70 // Take ownership of the ML service.
Andrew Moylan49f50b32018-07-27 08:48:11 +100071 CHECK(bus_->RequestOwnershipAndBlock(kMachineLearningServiceName,
Andrew Moylanef116f92018-04-11 15:12:38 +100072 dbus::Bus::REQUIRE_PRIMARY));
Ken Turneredb93932018-02-14 05:27:31 +110073}
74
75void Daemon::BootstrapMojoConnection(
76 dbus::MethodCall* method_call,
77 dbus::ExportedObject::ResponseSender response_sender) {
Andrew Moylan40ee4fc2018-08-24 15:46:09 +100078 metrics_.RecordMojoConnectionEvent(
79 Metrics::MojoConnectionEvent::kBootstrapRequested);
Andrew Moylanff6be512018-07-03 11:05:01 +100080 if (machine_learning_service_) {
81 LOG(ERROR) << "MachineLearningService already instantiated";
Qijiang Fan795b81f2020-07-06 12:36:18 +090082 std::move(response_sender)
83 .Run(dbus::ErrorResponse::FromMethodCall(
84 method_call, DBUS_ERROR_FAILED, "Bootstrap already completed"));
Andrew Moylanff6be512018-07-03 11:05:01 +100085 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";
Qijiang Fan795b81f2020-07-06 12:36:18 +090093 std::move(response_sender)
94 .Run(dbus::ErrorResponse::FromMethodCall(
95 method_call, DBUS_ERROR_INVALID_ARGS, "Expected file descriptor"));
Andrew Moylanff6be512018-07-03 11:05:01 +100096 return;
97 }
98
99 if (!file_handle.is_valid()) {
100 LOG(ERROR) << "ScopedFD extracted from D-Bus call was invalid (i.e. empty)";
Qijiang Fan795b81f2020-07-06 12:36:18 +0900101 std::move(response_sender)
102 .Run(dbus::ErrorResponse::FromMethodCall(
103 method_call, DBUS_ERROR_INVALID_ARGS,
104 "Invalid (empty) file descriptor"));
Andrew Moylanff6be512018-07-03 11:05:01 +1000105 return;
106 }
107
108 if (!base::SetCloseOnExec(file_handle.get())) {
109 PLOG(ERROR) << "Failed setting FD_CLOEXEC on file descriptor";
Qijiang Fan795b81f2020-07-06 12:36:18 +0900110 std::move(response_sender)
111 .Run(dbus::ErrorResponse::FromMethodCall(
112 method_call, DBUS_ERROR_FAILED,
113 "Failed setting FD_CLOEXEC on file descriptor"));
Andrew Moylanff6be512018-07-03 11:05:01 +1000114 return;
115 }
116
117 // Connect to mojo in the requesting process.
Qijiang Fan3b917c72019-12-09 15:24:15 +0900118 mojo::IncomingInvitation invitation =
119 mojo::IncomingInvitation::Accept(mojo::PlatformChannelEndpoint(
120 mojo::PlatformHandle(std::move(file_handle))));
Andrew Moylanff6be512018-07-03 11:05:01 +1000121
122 // Bind primordial message pipe to a MachineLearningService implementation.
hscham3f45fcc2019-12-25 16:54:54 +0900123 machine_learning_service_ = std::make_unique<MachineLearningServiceImpl>(
hschamd13deea2021-03-08 09:46:15 +0900124 mojo::PendingReceiver<
125 chromeos::machine_learning::mojom::MachineLearningService>(
126 invitation.ExtractMessagePipe(kBootstrapMojoConnectionChannelToken)),
Charles Zhaod4fb7b62020-08-25 17:21:58 +1000127 base::Bind(&Daemon::OnMojoDisconnection, base::Unretained(this)),
128 bus_.get());
Andrew Moylanff6be512018-07-03 11:05:01 +1000129
Andrew Moylan40ee4fc2018-08-24 15:46:09 +1000130 metrics_.RecordMojoConnectionEvent(
131 Metrics::MojoConnectionEvent::kBootstrapSucceeded);
132
Andrew Moylanff6be512018-07-03 11:05:01 +1000133 // Send success response.
Qijiang Fan795b81f2020-07-06 12:36:18 +0900134 std::move(response_sender).Run(dbus::Response::FromMethodCall(method_call));
Andrew Moylanff6be512018-07-03 11:05:01 +1000135}
136
Andrew Moylanb481af72020-07-09 15:22:00 +1000137void Daemon::OnMojoDisconnection() {
Andrew Moylan40ee4fc2018-08-24 15:46:09 +1000138 metrics_.RecordMojoConnectionEvent(
Andrew Moylanb481af72020-07-09 15:22:00 +1000139 Metrics::MojoConnectionEvent::kConnectionClosed);
140 // Die upon disconnection . Reconnection can occur when the daemon is
141 // restarted. (A future Mojo API may enable Mojo re-bootstrap without a
142 // process restart.)
Andrew Moylanff6be512018-07-03 11:05:01 +1000143 Quit();
Ken Turneredb93932018-02-14 05:27:31 +1100144}
145
146} // namespace ml