Add xsd_path() and use it to fix access to .xsd files on Linux (#1830).
[dcpomatic.git] / src / lib / cross_linux.cc
1 /*
2     Copyright (C) 2012-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 "cross.h"
22 #include "compose.hpp"
23 #include "log.h"
24 #include "dcpomatic_log.h"
25 #include "config.h"
26 #include "exceptions.h"
27 #include "dcpomatic_log.h"
28 #include <dcp/raw_convert.h>
29 #include <glib.h>
30 extern "C" {
31 #include <libavformat/avio.h>
32 }
33 #include <boost/algorithm/string.hpp>
34 #include <boost/foreach.hpp>
35 #include <boost/function.hpp>
36 #if BOOST_VERSION >= 106100
37 #include <boost/dll/runtime_symbol_info.hpp>
38 #endif
39 #include <unistd.h>
40 #include <mntent.h>
41 #include <sys/types.h>
42 #include <sys/mount.h>
43 #include <ifaddrs.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
46 #include <fstream>
47
48 #include "i18n.h"
49
50 using std::pair;
51 using std::list;
52 using std::ifstream;
53 using std::string;
54 using std::wstring;
55 using std::make_pair;
56 using std::vector;
57 using std::cerr;
58 using std::cout;
59 using std::runtime_error;
60 using boost::shared_ptr;
61 using boost::optional;
62 using boost::function;
63
64 /** @param s Number of seconds to sleep for */
65 void
66 dcpomatic_sleep_seconds (int s)
67 {
68         sleep (s);
69 }
70
71 void
72 dcpomatic_sleep_milliseconds (int ms)
73 {
74         usleep (ms * 1000);
75 }
76
77 /** @return A string of CPU information (model name etc.) */
78 string
79 cpu_info ()
80 {
81         string info;
82
83         /* This use of ifstream is ok; the filename can never
84            be non-Latin
85         */
86         ifstream f ("/proc/cpuinfo");
87         while (f.good ()) {
88                 string l;
89                 getline (f, l);
90                 if (boost::algorithm::starts_with (l, "model name")) {
91                         string::size_type const c = l.find (':');
92                         if (c != string::npos) {
93                                 info = l.substr (c + 2);
94                         }
95                 }
96         }
97
98         return info;
99 }
100
101 boost::filesystem::path
102 shared_path ()
103 {
104         char const * p = getenv ("DCPOMATIC_LINUX_SHARE_PREFIX");
105         if (p) {
106                 return boost::filesystem::path(p) / "dcpomatic2";
107         }
108         return boost::filesystem::canonical(LINUX_SHARE_PREFIX) / "dcpomatic2";
109 }
110
111
112 boost::filesystem::path
113 xsd_path ()
114 {
115         char const * p = getenv ("DCPOMATIC_LINUX_SHARE_PREFIX");
116         if (p) {
117                 return boost::filesystem::path(p) / "libdcp" / "xsd";
118         }
119         return boost::filesystem::canonical(LINUX_SHARE_PREFIX) / "libdcp" / "xsd";
120 }
121
122
123 void
124 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
125 {
126         string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
127         LOG_GENERAL (N_("Probing with %1"), ffprobe);
128         int const r = system (ffprobe.c_str());
129         if (r == -1 || (WIFEXITED(r) && WEXITSTATUS(r) != 0)) {
130                 LOG_GENERAL (N_("Could not run ffprobe (system returned %1"), r);
131         }
132 }
133
134 list<pair<string, string> >
135 mount_info ()
136 {
137         list<pair<string, string> > m;
138
139         FILE* f = setmntent ("/etc/mtab", "r");
140         if (!f) {
141                 return m;
142         }
143
144         while (true) {
145                 struct mntent* mnt = getmntent (f);
146                 if (!mnt) {
147                         break;
148                 }
149
150                 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
151         }
152
153         endmntent (f);
154
155         return m;
156 }
157
158
159 boost::filesystem::path
160 directory_containing_executable ()
161 {
162 #if BOOST_VERSION >= 106100
163         return boost::dll::program_location().parent_path();
164 #else
165         char buffer[PATH_MAX];
166         ssize_t N = readlink ("/proc/self/exe", buffer, PATH_MAX);
167         return boost::filesystem::path(string(buffer, N)).parent_path();
168 #endif
169 }
170
171
172 boost::filesystem::path
173 openssl_path ()
174 {
175         boost::filesystem::path p = directory_containing_executable() / "dcpomatic2_openssl";
176         if (boost::filesystem::is_regular_file(p)) {
177                 return p;
178         }
179
180         return "dcpomatic2_openssl";
181 }
182
183
184 #ifdef DCPOMATIC_DISK
185 boost::filesystem::path
186 disk_writer_path ()
187 {
188         return directory_containing_executable() / "dcpomatic2_disk_writer";
189 }
190 #endif
191
192
193 /* Apparently there is no way to create an ofstream using a UTF-8
194    filename under Windows.  We are hence reduced to using fopen
195    with this wrapper.
196 */
197 FILE *
198 fopen_boost (boost::filesystem::path p, string t)
199 {
200         return fopen (p.c_str(), t.c_str ());
201 }
202
203 int
204 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
205 {
206         return fseek (stream, offset, whence);
207 }
208
209 void
210 Waker::nudge ()
211 {
212
213 }
214
215 Waker::Waker ()
216 {
217
218 }
219
220 Waker::~Waker ()
221 {
222
223 }
224
225
226 void
227 start_tool (string executable)
228 {
229         boost::filesystem::path batch = directory_containing_executable() / executable;
230
231         pid_t pid = fork ();
232         if (pid == 0) {
233                 int const r = system (batch.string().c_str());
234                 exit (WEXITSTATUS (r));
235         }
236 }
237
238
239 void
240 start_batch_converter ()
241 {
242         start_tool ("dcpomatic2_batch");
243 }
244
245
246 void
247 start_player ()
248 {
249         start_tool ("dcpomatic2_player");
250 }
251
252
253 uint64_t
254 thread_id ()
255 {
256         return (uint64_t) pthread_self ();
257 }
258
259 int
260 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
261 {
262         return avio_open (s, file.c_str(), flags);
263 }
264
265
266 boost::filesystem::path
267 home_directory ()
268 {
269         return getenv("HOME");
270 }
271
272 string
273 command_and_read (string cmd)
274 {
275         FILE* pipe = popen (cmd.c_str(), "r");
276         if (!pipe) {
277                 throw runtime_error ("popen failed");
278         }
279
280         string result;
281         char buffer[128];
282         try {
283                 while (fgets(buffer, sizeof(buffer), pipe)) {
284                         result += buffer;
285                 }
286         } catch (...) {
287                 pclose (pipe);
288                 throw;
289         }
290
291         pclose (pipe);
292         return result;
293 }
294
295 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
296 bool
297 running_32_on_64 ()
298 {
299         /* I'm assuming nobody does this on Linux */
300         return false;
301 }
302
303
304 static
305 vector<pair<string, string> >
306 get_mounts (string prefix)
307 {
308         vector<pair<string, string> > mounts;
309
310         std::ifstream f("/proc/mounts");
311         string line;
312         while (f.good()) {
313                 getline(f, line);
314                 vector<string> bits;
315                 boost::algorithm::split (bits, line, boost::is_any_of(" "));
316                 if (bits.size() > 1 && boost::algorithm::starts_with(bits[0], prefix)) {
317                         boost::algorithm::replace_all (bits[1], "\\040", " ");
318                         mounts.push_back(make_pair(bits[0], bits[1]));
319                         LOG_DISK("Found mounted device %1 from prefix %2", bits[0], prefix);
320                 }
321         }
322
323         return mounts;
324 }
325
326
327 vector<Drive>
328 Drive::get ()
329 {
330         vector<Drive> drives;
331
332         using namespace boost::filesystem;
333         vector<pair<string, string> > mounted_devices = get_mounts("/dev/");
334
335         for (directory_iterator i = directory_iterator("/sys/block"); i != directory_iterator(); ++i) {
336                 string const name = i->path().filename().string();
337                 path device_type_file("/sys/block/" + name + "/device/type");
338                 optional<string> device_type;
339                 if (exists(device_type_file)) {
340                         device_type = dcp::file_to_string (device_type_file);
341                         boost::trim(*device_type);
342                 }
343                 /* Device type 5 is "SCSI_TYPE_ROM" in blkdev.h; seems usually to be a CD/DVD drive */
344                 if (!boost::algorithm::starts_with(name, "loop") && (!device_type || *device_type != "5")) {
345                         uint64_t const size = dcp::raw_convert<uint64_t>(dcp::file_to_string(*i / "size")) * 512;
346                         if (size == 0) {
347                                 continue;
348                         }
349                         optional<string> vendor;
350                         try {
351                                 vendor = dcp::file_to_string("/sys/block/" + name + "/device/vendor");
352                                 boost::trim(*vendor);
353                         } catch (...) {}
354                         optional<string> model;
355                         try {
356                                 model = dcp::file_to_string("/sys/block/" + name + "/device/model");
357                                 boost::trim(*model);
358                         } catch (...) {}
359                         vector<boost::filesystem::path> mount_points;
360                         for (vector<pair<string, string> >::const_iterator j = mounted_devices.begin(); j != mounted_devices.end(); ++j) {
361                                 if (boost::algorithm::starts_with(j->first, "/dev/" + name)) {
362                                         mount_points.push_back (j->second);
363                                 }
364                         }
365                         drives.push_back(Drive("/dev/" + name, mount_points, size, vendor, model));
366                         LOG_DISK_NC(drives.back().log_summary());
367                 }
368         }
369
370         return drives;
371 }
372
373
374 bool
375 Drive::unmount ()
376 {
377         BOOST_FOREACH (boost::filesystem::path i, _mount_points) {
378                 int const r = umount(i.string().c_str());
379                 LOG_DISK("Tried to unmount %1 and got %2 and %3", i.string(), r, errno);
380                 if (r == -1) {
381                         return false;
382                 }
383         }
384         return true;
385 }
386
387
388 void
389 unprivileged ()
390 {
391         uid_t ruid, euid, suid;
392         if (getresuid(&ruid, &euid, &suid) == -1) {
393                 cerr << "getresuid() failed.\n";
394         }
395         if (seteuid(ruid) == -1) {
396                 cerr << "seteuid() failed.\n";
397         }
398 }
399
400 PrivilegeEscalator::~PrivilegeEscalator ()
401 {
402         unprivileged ();
403 }
404
405 PrivilegeEscalator::PrivilegeEscalator ()
406 {
407         int const r = seteuid(0);
408         if (r < 0) {
409                 throw PrivilegeError (String::compose("seteuid() call failed with %1", errno));
410         }
411 }
412
413 boost::filesystem::path
414 config_path ()
415 {
416         boost::filesystem::path p;
417         p /= g_get_user_config_dir ();
418         p /= "dcpomatic2";
419         return p;
420 }
421
422
423 void
424 disk_write_finished ()
425 {
426
427 }
428