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