2 Copyright (C) 2019-2020 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/>.
21 #include "lib/compose.hpp"
22 #include "lib/cross.h"
23 #include "lib/dcpomatic_log.h"
24 #include "lib/digester.h"
25 #include "lib/disk_writer_messages.h"
26 #include "lib/exceptions.h"
28 #include "lib/file_log.h"
29 #include "lib/nanomsg.h"
30 #include "lib/version.h"
31 #include "lib/warnings.h"
33 #ifdef DCPOMATIC_POSIX
34 #include <sys/ioctl.h>
35 #include <sys/types.h>
40 #include "lib/stdout_log.h"
43 #include <lwext4/file_dev.h>
48 #ifdef DCPOMATIC_LINUX
49 #include <polkit/polkit.h>
53 #ifdef DCPOMATIC_WINDOWS
55 #include <lwext4/file_windows.h>
59 DCPOMATIC_DISABLE_WARNINGS
61 DCPOMATIC_ENABLE_WARNINGS
64 #include <sys/types.h>
65 #include <boost/filesystem.hpp>
66 #include <boost/algorithm/string.hpp>
67 #include <boost/foreach.hpp>
73 using std::runtime_error;
76 using boost::optional;
79 #define SHORT_TIMEOUT 100
80 #define LONG_TIMEOUT 2000
83 #ifdef DCPOMATIC_LINUX
84 static PolkitAuthority* polkit_authority = 0;
86 static Nanomsg* nanomsg = 0;
90 boost::filesystem::path dcp_path;
92 std::string posix_partition;
95 #ifdef DCPOMATIC_LINUX
98 polkit_callback (GObject *, GAsyncResult* res, gpointer data)
100 Parameters* parameters = reinterpret_cast<Parameters*> (data);
101 PolkitAuthorizationResult* result = polkit_authority_check_authorization_finish (polkit_authority, res, 0);
102 if (result && polkit_authorization_result_get_is_authorized(result)) {
103 dcpomatic::write (parameters->dcp_path, parameters->device, parameters->posix_partition, nanomsg);
107 g_object_unref (result);
117 using namespace boost::algorithm;
119 optional<string> s = nanomsg->receive (0);
124 LOG_DISK("Writer receives command: %1", *s);
126 if (*s == DISK_WRITER_QUIT) {
128 } else if (*s == DISK_WRITER_PING) {
129 nanomsg->send(DISK_WRITER_PONG "\n", LONG_TIMEOUT);
130 } else if (*s == DISK_WRITER_UNMOUNT) {
131 /* XXX: should do Linux polkit stuff here */
132 optional<string> xml_head = nanomsg->receive (LONG_TIMEOUT);
133 optional<string> xml_body = nanomsg->receive (LONG_TIMEOUT);
134 if (!xml_head || !xml_body) {
135 LOG_DISK_NC("Failed to receive unmount request");
136 throw CommunicationFailedError ();
138 bool const success = Drive(*xml_head + *xml_body).unmount();
139 if (!nanomsg->send (success ? (DISK_WRITER_OK "\n") : (DISK_WRITER_ERROR "\n"), LONG_TIMEOUT)) {
140 LOG_DISK_NC("CommunicationFailedError in unmount_finished");
141 throw CommunicationFailedError ();
143 } else if (*s == DISK_WRITER_WRITE) {
144 optional<string> dcp_path = nanomsg->receive (LONG_TIMEOUT);
145 optional<string> device = nanomsg->receive (LONG_TIMEOUT);
146 if (!dcp_path || !device) {
147 LOG_DISK_NC("Failed to receive write request");
148 throw CommunicationFailedError();
151 /* Do some basic sanity checks; this is a bit belt-and-braces but it can't hurt... */
154 if (!starts_with(*device, "/dev/disk")) {
155 LOG_DISK ("Will not write to %1", *device);
156 nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT);
160 #ifdef DCPOMATIC_LINUX
161 if (!starts_with(*device, "/dev/sd") && !starts_with(*device, "/dev/hd")) {
162 LOG_DISK ("Will not write to %1", *device);
163 nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT);
167 #ifdef DCPOMATIC_WINDOWS
168 if (!starts_with(*device, "\\\\.\\PHYSICALDRIVE")) {
169 LOG_DISK ("Will not write to %1", *device);
170 nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT);
175 bool on_drive_list = false;
176 bool mounted = false;
177 for (auto const& i: Drive::get()) {
178 if (i.device() == *device) {
179 on_drive_list = true;
180 mounted = i.mounted();
184 if (!on_drive_list) {
185 LOG_DISK ("Will not write to %1 as it's not recognised as a drive", *device);
186 nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT);
190 LOG_DISK ("Will not write to %1 as it's mounted", *device);
191 nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT);
195 LOG_DISK ("Here we go writing %1 to %2", *dcp_path, *device);
197 #ifdef DCPOMATIC_LINUX
198 polkit_authority = polkit_authority_get_sync (0, 0);
199 PolkitSubject* subject = polkit_unix_process_new_for_owner (getppid(), 0, -1);
200 Parameters* parameters = new Parameters;
201 parameters->dcp_path = *dcp_path;
202 parameters->device = *device;
203 parameters->posix_partition = *device;
204 /* XXX: don't know if this logic is sensible */
205 if (parameters->posix_partition.size() > 0 && isdigit(parameters->posix_partition[parameters->posix_partition.length() - 1])) {
206 parameters->posix_partition += "p1";
208 parameters->posix_partition += "1";
210 polkit_authority_check_authorization (
211 polkit_authority, subject, "com.dcpomatic.write-drive", 0, POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, 0, polkit_callback, parameters
214 string posix_partition = "";
216 posix_partition = *device + "s1";
218 dcpomatic::write (*dcp_path, *device, posix_partition, nanomsg);
223 } catch (exception& e) {
224 LOG_DISK("Exception (from idle): %1", e.what());
232 /* On macOS this is running as root, so config_path() will be somewhere in root's
233 * home. Instead, just write to stdout as the macOS process control stuff will
234 * redirect this to a file in /var/log
236 dcpomatic_log.reset(new StdoutLog(LogEntry::TYPE_DISK));
237 LOG_DISK("dcpomatic_disk_writer %1 started", dcpomatic_git_commit);
239 /* XXX: this is a hack, but I expect we'll need logs and I'm not sure if there's
240 * a better place to put them.
242 dcpomatic_log.reset(new FileLog(config_path() / "disk_writer.log", LogEntry::TYPE_DISK));
243 LOG_DISK_NC("dcpomatic_disk_writer started");
247 /* I *think* this consumes the notifyd event that we used to start the process, so we only
248 * get started once per notification.
250 xpc_set_event_stream_handler("com.apple.notifyd.matching", DISPATCH_TARGET_QUEUE_DEFAULT, ^(xpc_object_t) {});
254 nanomsg = new Nanomsg (false);
255 } catch (runtime_error& e) {
256 LOG_DISK_NC("Could not set up nanomsg socket");
260 Glib::RefPtr<Glib::MainLoop> ml = Glib::MainLoop::create ();
261 Glib::signal_timeout().connect(sigc::ptr_fun(&idle), 500);