2 Copyright (C) 2019-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "lib/compose.hpp"
23 #include "lib/cross.h"
24 #include "lib/dcpomatic_log.h"
25 #include "lib/digester.h"
26 #include "lib/disk_writer_messages.h"
27 #include "lib/exceptions.h"
29 #include "lib/file_log.h"
30 #include "lib/nanomsg.h"
31 #include "lib/version.h"
32 #include "lib/warnings.h"
34 #ifdef DCPOMATIC_POSIX
35 #include <sys/ioctl.h>
36 #include <sys/types.h>
41 #include "lib/stdout_log.h"
44 #include <lwext4/file_dev.h>
49 #ifdef DCPOMATIC_LINUX
50 #include <polkit/polkit.h>
54 #ifdef DCPOMATIC_WINDOWS
56 #include <lwext4/file_windows.h>
60 DCPOMATIC_DISABLE_WARNINGS
62 DCPOMATIC_ENABLE_WARNINGS
65 #include <sys/types.h>
66 #include <boost/filesystem.hpp>
67 #include <boost/algorithm/string.hpp>
74 using std::runtime_error;
77 using boost::optional;
80 #define SHORT_TIMEOUT 100
81 #define LONG_TIMEOUT 2000
84 #ifdef DCPOMATIC_LINUX
85 static PolkitAuthority* polkit_authority = nullptr;
87 static Nanomsg* nanomsg = nullptr;
92 boost::filesystem::path dcp_path;
94 std::string posix_partition;
98 #ifdef DCPOMATIC_LINUX
101 polkit_callback (GObject *, GAsyncResult* res, gpointer data)
103 auto parameters = reinterpret_cast<Parameters*> (data);
104 GError* error = nullptr;
105 auto result = polkit_authority_check_authorization_finish (polkit_authority, res, &error);
109 LOG_DISK("polkit authority check failed (check_authorization_finish failed with %1)", error->message);
112 if (polkit_authorization_result_get_is_authorized(result)) {
113 dcpomatic::write (parameters->dcp_path, parameters->device, parameters->posix_partition, nanomsg);
116 if (polkit_authorization_result_get_is_challenge(result)) {
117 LOG_DISK_NC("polkit authority check failed (challenge)");
119 LOG_DISK_NC("polkit authority check failed (not authorized)");
124 if (failed && nanomsg) {
125 nanomsg->send(DISK_WRITER_ERROR "\nCould not obtain authorization to write to the drive\n", LONG_TIMEOUT);
131 g_object_unref (result);
141 using namespace boost::algorithm;
143 auto s = nanomsg->receive (0);
148 LOG_DISK("Writer receives command: %1", *s);
150 if (*s == DISK_WRITER_QUIT) {
152 } else if (*s == DISK_WRITER_PING) {
153 nanomsg->send(DISK_WRITER_PONG "\n", LONG_TIMEOUT);
154 } else if (*s == DISK_WRITER_UNMOUNT) {
155 /* XXX: should do Linux polkit stuff here */
156 auto xml_head = nanomsg->receive (LONG_TIMEOUT);
157 auto xml_body = nanomsg->receive (LONG_TIMEOUT);
158 if (!xml_head || !xml_body) {
159 LOG_DISK_NC("Failed to receive unmount request");
160 throw CommunicationFailedError ();
162 bool const success = Drive(*xml_head + *xml_body).unmount();
163 if (!nanomsg->send (success ? (DISK_WRITER_OK "\n") : (DISK_WRITER_ERROR "\n"), LONG_TIMEOUT)) {
164 LOG_DISK_NC("CommunicationFailedError in unmount_finished");
165 throw CommunicationFailedError ();
167 } else if (*s == DISK_WRITER_WRITE) {
168 auto dcp_path = nanomsg->receive (LONG_TIMEOUT);
169 auto device = nanomsg->receive (LONG_TIMEOUT);
170 if (!dcp_path || !device) {
171 LOG_DISK_NC("Failed to receive write request");
172 throw CommunicationFailedError();
175 /* Do some basic sanity checks; this is a bit belt-and-braces but it can't hurt... */
178 if (!starts_with(*device, "/dev/disk")) {
179 LOG_DISK ("Will not write to %1", *device);
180 nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT);
184 #ifdef DCPOMATIC_LINUX
185 if (!starts_with(*device, "/dev/sd") && !starts_with(*device, "/dev/hd")) {
186 LOG_DISK ("Will not write to %1", *device);
187 nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT);
191 #ifdef DCPOMATIC_WINDOWS
192 if (!starts_with(*device, "\\\\.\\PHYSICALDRIVE")) {
193 LOG_DISK ("Will not write to %1", *device);
194 nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT);
199 bool on_drive_list = false;
200 bool mounted = false;
201 for (auto const& i: Drive::get()) {
202 if (i.device() == *device) {
203 on_drive_list = true;
204 mounted = i.mounted();
208 if (!on_drive_list) {
209 LOG_DISK ("Will not write to %1 as it's not recognised as a drive", *device);
210 nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT);
214 LOG_DISK ("Will not write to %1 as it's mounted", *device);
215 nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT);
219 LOG_DISK ("Here we go writing %1 to %2", *dcp_path, *device);
221 #if defined(DCPOMATIC_LINUX)
222 polkit_authority = polkit_authority_get_sync (0, 0);
223 auto subject = polkit_unix_process_new_for_owner (getppid(), 0, -1);
224 auto parameters = new Parameters;
225 parameters->dcp_path = *dcp_path;
226 parameters->device = *device;
227 parameters->posix_partition = *device;
228 /* XXX: don't know if this logic is sensible */
229 if (parameters->posix_partition.size() > 0 && isdigit(parameters->posix_partition[parameters->posix_partition.length() - 1])) {
230 parameters->posix_partition += "p1";
232 parameters->posix_partition += "1";
234 polkit_authority_check_authorization (
235 polkit_authority, subject, "com.dcpomatic.write-drive", 0, POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, 0, polkit_callback, parameters
237 #elif defined(DCPOMATIC_OSX)
238 auto fast_device = boost::algorithm::replace_first_copy (*device, "/dev/disk", "/dev/rdisk");
239 dcpomatic::write (*dcp_path, fast_device, fast_device + "s1", nanomsg);
240 #elif defined(DCPOMATIC_WINDOWS)
241 dcpomatic::write (*dcp_path, *device, "", nanomsg);
246 } catch (exception& e) {
247 LOG_DISK("Exception (from idle): %1", e.what());
255 /* On macOS this is running as root, so config_path() will be somewhere in root's
256 * home. Instead, just write to stdout as the macOS process control stuff will
257 * redirect this to a file in /var/log
259 dcpomatic_log.reset(new StdoutLog(LogEntry::TYPE_DISK));
260 LOG_DISK("dcpomatic_disk_writer %1 started", dcpomatic_git_commit);
262 /* XXX: this is a hack, but I expect we'll need logs and I'm not sure if there's
263 * a better place to put them.
265 dcpomatic_log.reset(new FileLog(config_path() / "disk_writer.log", LogEntry::TYPE_DISK));
266 LOG_DISK_NC("dcpomatic_disk_writer started");
270 /* I *think* this consumes the notifyd event that we used to start the process, so we only
271 * get started once per notification.
273 xpc_set_event_stream_handler("com.apple.notifyd.matching", DISPATCH_TARGET_QUEUE_DEFAULT, ^(xpc_object_t) {});
277 nanomsg = new Nanomsg (false);
278 } catch (runtime_error& e) {
279 LOG_DISK_NC("Could not set up nanomsg socket");
283 auto ml = Glib::MainLoop::create ();
284 Glib::signal_timeout().connect(sigc::ptr_fun(&idle), 500);