BOOST_FOREACH.
[dcpomatic.git] / src / tools / dcpomatic_disk_writer.cc
1 /*
2     Copyright (C) 2019-2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
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"
27 #include "lib/ext.h"
28 #include "lib/file_log.h"
29 #include "lib/nanomsg.h"
30 #include "lib/version.h"
31 #include "lib/warnings.h"
32
33 #ifdef DCPOMATIC_POSIX
34 #include <sys/ioctl.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #endif
38
39 #ifdef DCPOMATIC_OSX
40 #include "lib/stdout_log.h"
41 #undef nil
42 extern "C" {
43 #include <lwext4/file_dev.h>
44 }
45 #include <xpc/xpc.h>
46 #endif
47
48 #ifdef DCPOMATIC_LINUX
49 #include <polkit/polkit.h>
50 #include <poll.h>
51 #endif
52
53 #ifdef DCPOMATIC_WINDOWS
54 extern "C" {
55 #include <lwext4/file_windows.h>
56 }
57 #endif
58
59 DCPOMATIC_DISABLE_WARNINGS
60 #include <glibmm.h>
61 DCPOMATIC_ENABLE_WARNINGS
62
63 #include <unistd.h>
64 #include <sys/types.h>
65 #include <boost/filesystem.hpp>
66 #include <boost/algorithm/string.hpp>
67 #include <iostream>
68
69 using std::cin;
70 using std::min;
71 using std::string;
72 using std::runtime_error;
73 using std::exception;
74 using std::vector;
75 using boost::optional;
76
77
78 #define SHORT_TIMEOUT 100
79 #define LONG_TIMEOUT 2000
80
81
82 #ifdef DCPOMATIC_LINUX
83 static PolkitAuthority* polkit_authority = 0;
84 #endif
85 static Nanomsg* nanomsg = 0;
86
87 struct Parameters
88 {
89         boost::filesystem::path dcp_path;
90         std::string device;
91         std::string posix_partition;
92 };
93
94 #ifdef DCPOMATIC_LINUX
95 static
96 void
97 polkit_callback (GObject *, GAsyncResult* res, gpointer data)
98 {
99         Parameters* parameters = reinterpret_cast<Parameters*> (data);
100         PolkitAuthorizationResult* result = polkit_authority_check_authorization_finish (polkit_authority, res, 0);
101         if (result && polkit_authorization_result_get_is_authorized(result)) {
102                 dcpomatic::write (parameters->dcp_path, parameters->device, parameters->posix_partition, nanomsg);
103         }
104         delete parameters;
105         if (result) {
106                 g_object_unref (result);
107         }
108 }
109 #endif
110
111
112 bool
113 idle ()
114 try
115 {
116         using namespace boost::algorithm;
117
118         optional<string> s = nanomsg->receive (0);
119         if (!s) {
120                 return true;
121         }
122
123         LOG_DISK("Writer receives command: %1", *s);
124
125         if (*s == DISK_WRITER_QUIT) {
126                 exit (EXIT_SUCCESS);
127         } else if (*s == DISK_WRITER_PING) {
128                 nanomsg->send(DISK_WRITER_PONG "\n", LONG_TIMEOUT);
129         } else if (*s == DISK_WRITER_UNMOUNT) {
130                 /* XXX: should do Linux polkit stuff here */
131                 optional<string> xml_head = nanomsg->receive (LONG_TIMEOUT);
132                 optional<string> xml_body = nanomsg->receive (LONG_TIMEOUT);
133                 if (!xml_head || !xml_body) {
134                         LOG_DISK_NC("Failed to receive unmount request");
135                         throw CommunicationFailedError ();
136                 }
137                 bool const success = Drive(*xml_head + *xml_body).unmount();
138                 if (!nanomsg->send (success ? (DISK_WRITER_OK "\n") : (DISK_WRITER_ERROR "\n"), LONG_TIMEOUT)) {
139                         LOG_DISK_NC("CommunicationFailedError in unmount_finished");
140                         throw CommunicationFailedError ();
141                 }
142         } else if (*s == DISK_WRITER_WRITE) {
143                 optional<string> dcp_path = nanomsg->receive (LONG_TIMEOUT);
144                 optional<string> device = nanomsg->receive (LONG_TIMEOUT);
145                 if (!dcp_path || !device) {
146                         LOG_DISK_NC("Failed to receive write request");
147                         throw CommunicationFailedError();
148                 }
149
150                 /* Do some basic sanity checks; this is a bit belt-and-braces but it can't hurt... */
151
152 #ifdef DCPOMATIC_OSX
153                 if (!starts_with(*device, "/dev/disk")) {
154                         LOG_DISK ("Will not write to %1", *device);
155                         nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT);
156                         return true;
157                 }
158 #endif
159 #ifdef DCPOMATIC_LINUX
160                 if (!starts_with(*device, "/dev/sd") && !starts_with(*device, "/dev/hd")) {
161                         LOG_DISK ("Will not write to %1", *device);
162                         nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT);
163                         return true;
164                 }
165 #endif
166 #ifdef DCPOMATIC_WINDOWS
167                 if (!starts_with(*device, "\\\\.\\PHYSICALDRIVE")) {
168                         LOG_DISK ("Will not write to %1", *device);
169                         nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT);
170                         return true;
171                 }
172 #endif
173
174                 bool on_drive_list = false;
175                 bool mounted = false;
176                 for (auto const& i: Drive::get()) {
177                         if (i.device() == *device) {
178                                 on_drive_list = true;
179                                 mounted = i.mounted();
180                         }
181                 }
182
183                 if (!on_drive_list) {
184                         LOG_DISK ("Will not write to %1 as it's not recognised as a drive", *device);
185                         nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT);
186                         return true;
187                 }
188                 if (mounted) {
189                         LOG_DISK ("Will not write to %1 as it's mounted", *device);
190                         nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT);
191                         return true;
192                 }
193
194                 LOG_DISK ("Here we go writing %1 to %2", *dcp_path, *device);
195
196 #ifdef DCPOMATIC_LINUX
197                 polkit_authority = polkit_authority_get_sync (0, 0);
198                 PolkitSubject* subject = polkit_unix_process_new_for_owner (getppid(), 0, -1);
199                 Parameters* parameters = new Parameters;
200                 parameters->dcp_path = *dcp_path;
201                 parameters->device = *device;
202                 parameters->posix_partition = *device;
203                 /* XXX: don't know if this logic is sensible */
204                 if (parameters->posix_partition.size() > 0 && isdigit(parameters->posix_partition[parameters->posix_partition.length() - 1])) {
205                         parameters->posix_partition += "p1";
206                 } else {
207                         parameters->posix_partition += "1";
208                 }
209                 polkit_authority_check_authorization (
210                                 polkit_authority, subject, "com.dcpomatic.write-drive", 0, POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, 0, polkit_callback, parameters
211                                 );
212 #else
213                 string posix_partition = "";
214 #ifdef DCPOMATIC_OSX
215                 posix_partition = *device + "s1";
216 #endif
217                 dcpomatic::write (*dcp_path, *device, posix_partition, nanomsg);
218 #endif
219         }
220
221         return true;
222 } catch (exception& e) {
223         LOG_DISK("Exception (from idle): %1", e.what());
224         return true;
225 }
226
227 int
228 main ()
229 {
230 #ifdef DCPOMATIC_OSX
231         /* On macOS this is running as root, so config_path() will be somewhere in root's
232          * home.  Instead, just write to stdout as the macOS process control stuff will
233          * redirect this to a file in /var/log
234          */
235         dcpomatic_log.reset(new StdoutLog(LogEntry::TYPE_DISK));
236         LOG_DISK("dcpomatic_disk_writer %1 started", dcpomatic_git_commit);
237 #else
238         /* XXX: this is a hack, but I expect we'll need logs and I'm not sure if there's
239          * a better place to put them.
240          */
241         dcpomatic_log.reset(new FileLog(config_path() / "disk_writer.log", LogEntry::TYPE_DISK));
242         LOG_DISK_NC("dcpomatic_disk_writer started");
243 #endif
244
245 #ifdef DCPOMATIC_OSX
246         /* I *think* this consumes the notifyd event that we used to start the process, so we only
247          * get started once per notification.
248          */
249         xpc_set_event_stream_handler("com.apple.notifyd.matching", DISPATCH_TARGET_QUEUE_DEFAULT, ^(xpc_object_t) {});
250 #endif
251
252         try {
253                 nanomsg = new Nanomsg (false);
254         } catch (runtime_error& e) {
255                 LOG_DISK_NC("Could not set up nanomsg socket");
256                 exit (EXIT_FAILURE);
257         }
258
259         Glib::RefPtr<Glib::MainLoop> ml = Glib::MainLoop::create ();
260         Glib::signal_timeout().connect(sigc::ptr_fun(&idle), 500);
261         ml->run ();
262 }