5c1bb7d9090772d2968c95b45f77ae0bfdac7897
[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 resources_path ()
103 {
104         return directory_containing_executable().parent_path() / "share" / "dcpomatic2";
105 }
106
107
108 boost::filesystem::path
109 xsd_path ()
110 {
111         return directory_containing_executable().parent_path() / "share" / "libdcp" / "xsd";
112 }
113
114
115 boost::filesystem::path
116 tags_path ()
117 {
118         return directory_containing_executable().parent_path() / "share" / "libdcp" / "tags";
119 }
120
121
122 void
123 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
124 {
125         string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
126         LOG_GENERAL (N_("Probing with %1"), ffprobe);
127         int const r = system (ffprobe.c_str());
128         if (r == -1 || (WIFEXITED(r) && WEXITSTATUS(r) != 0)) {
129                 LOG_GENERAL (N_("Could not run ffprobe (system returned %1"), r);
130         }
131 }
132
133 list<pair<string, string> >
134 mount_info ()
135 {
136         list<pair<string, string> > m;
137
138         FILE* f = setmntent ("/etc/mtab", "r");
139         if (!f) {
140                 return m;
141         }
142
143         while (true) {
144                 struct mntent* mnt = getmntent (f);
145                 if (!mnt) {
146                         break;
147                 }
148
149                 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
150         }
151
152         endmntent (f);
153
154         return m;
155 }
156
157
158 boost::filesystem::path
159 directory_containing_executable ()
160 {
161 #if BOOST_VERSION >= 106100
162         return boost::dll::program_location().parent_path();
163 #else
164         char buffer[PATH_MAX];
165         ssize_t N = readlink ("/proc/self/exe", buffer, PATH_MAX);
166         return boost::filesystem::path(string(buffer, N)).parent_path();
167 #endif
168 }
169
170
171 boost::filesystem::path
172 openssl_path ()
173 {
174         boost::filesystem::path p = directory_containing_executable() / "dcpomatic2_openssl";
175         if (boost::filesystem::is_regular_file(p)) {
176                 return p;
177         }
178
179         return "dcpomatic2_openssl";
180 }
181
182
183 #ifdef DCPOMATIC_DISK
184 boost::filesystem::path
185 disk_writer_path ()
186 {
187         return directory_containing_executable() / "dcpomatic2_disk_writer";
188 }
189 #endif
190
191
192 /* Apparently there is no way to create an ofstream using a UTF-8
193    filename under Windows.  We are hence reduced to using fopen
194    with this wrapper.
195 */
196 FILE *
197 fopen_boost (boost::filesystem::path p, string t)
198 {
199         return fopen (p.c_str(), t.c_str ());
200 }
201
202 int
203 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
204 {
205         return fseek (stream, offset, whence);
206 }
207
208 void
209 Waker::nudge ()
210 {
211
212 }
213
214 Waker::Waker ()
215 {
216
217 }
218
219 Waker::~Waker ()
220 {
221
222 }
223
224
225 void
226 start_tool (string executable)
227 {
228         boost::filesystem::path batch = directory_containing_executable() / executable;
229
230         pid_t pid = fork ();
231         if (pid == 0) {
232                 int const r = system (batch.string().c_str());
233                 exit (WEXITSTATUS (r));
234         }
235 }
236
237
238 void
239 start_batch_converter ()
240 {
241         start_tool ("dcpomatic2_batch");
242 }
243
244
245 void
246 start_player ()
247 {
248         start_tool ("dcpomatic2_player");
249 }
250
251
252 uint64_t
253 thread_id ()
254 {
255         return (uint64_t) pthread_self ();
256 }
257
258 int
259 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
260 {
261         return avio_open (s, file.c_str(), flags);
262 }
263
264
265 boost::filesystem::path
266 home_directory ()
267 {
268         return getenv("HOME");
269 }
270
271 string
272 command_and_read (string cmd)
273 {
274         FILE* pipe = popen (cmd.c_str(), "r");
275         if (!pipe) {
276                 throw runtime_error ("popen failed");
277         }
278
279         string result;
280         char buffer[128];
281         try {
282                 while (fgets(buffer, sizeof(buffer), pipe)) {
283                         result += buffer;
284                 }
285         } catch (...) {
286                 pclose (pipe);
287                 throw;
288         }
289
290         pclose (pipe);
291         return result;
292 }
293
294 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
295 bool
296 running_32_on_64 ()
297 {
298         /* I'm assuming nobody does this on Linux */
299         return false;
300 }
301
302
303 static
304 vector<pair<string, string> >
305 get_mounts (string prefix)
306 {
307         vector<pair<string, string> > mounts;
308
309         std::ifstream f("/proc/mounts");
310         string line;
311         while (f.good()) {
312                 getline(f, line);
313                 vector<string> bits;
314                 boost::algorithm::split (bits, line, boost::is_any_of(" "));
315                 if (bits.size() > 1 && boost::algorithm::starts_with(bits[0], prefix)) {
316                         boost::algorithm::replace_all (bits[1], "\\040", " ");
317                         mounts.push_back(make_pair(bits[0], bits[1]));
318                         LOG_DISK("Found mounted device %1 from prefix %2", bits[0], prefix);
319                 }
320         }
321
322         return mounts;
323 }
324
325
326 vector<Drive>
327 Drive::get ()
328 {
329         vector<Drive> drives;
330
331         using namespace boost::filesystem;
332         vector<pair<string, string> > mounted_devices = get_mounts("/dev/");
333
334         for (directory_iterator i = directory_iterator("/sys/block"); i != directory_iterator(); ++i) {
335                 string const name = i->path().filename().string();
336                 path device_type_file("/sys/block/" + name + "/device/type");
337                 optional<string> device_type;
338                 if (exists(device_type_file)) {
339                         device_type = dcp::file_to_string (device_type_file);
340                         boost::trim(*device_type);
341                 }
342                 /* Device type 5 is "SCSI_TYPE_ROM" in blkdev.h; seems usually to be a CD/DVD drive */
343                 if (!boost::algorithm::starts_with(name, "loop") && (!device_type || *device_type != "5")) {
344                         uint64_t const size = dcp::raw_convert<uint64_t>(dcp::file_to_string(*i / "size")) * 512;
345                         if (size == 0) {
346                                 continue;
347                         }
348                         optional<string> vendor;
349                         try {
350                                 vendor = dcp::file_to_string("/sys/block/" + name + "/device/vendor");
351                                 boost::trim(*vendor);
352                         } catch (...) {}
353                         optional<string> model;
354                         try {
355                                 model = dcp::file_to_string("/sys/block/" + name + "/device/model");
356                                 boost::trim(*model);
357                         } catch (...) {}
358                         vector<boost::filesystem::path> mount_points;
359                         for (vector<pair<string, string> >::const_iterator j = mounted_devices.begin(); j != mounted_devices.end(); ++j) {
360                                 if (boost::algorithm::starts_with(j->first, "/dev/" + name)) {
361                                         mount_points.push_back (j->second);
362                                 }
363                         }
364                         drives.push_back(Drive("/dev/" + name, mount_points, size, vendor, model));
365                         LOG_DISK_NC(drives.back().log_summary());
366                 }
367         }
368
369         return drives;
370 }
371
372
373 bool
374 Drive::unmount ()
375 {
376         BOOST_FOREACH (boost::filesystem::path i, _mount_points) {
377                 int const r = umount(i.string().c_str());
378                 LOG_DISK("Tried to unmount %1 and got %2 and %3", i.string(), r, errno);
379                 if (r == -1) {
380                         return false;
381                 }
382         }
383         return true;
384 }
385
386
387 void
388 unprivileged ()
389 {
390         uid_t ruid, euid, suid;
391         if (getresuid(&ruid, &euid, &suid) == -1) {
392                 cerr << "getresuid() failed.\n";
393         }
394         if (seteuid(ruid) == -1) {
395                 cerr << "seteuid() failed.\n";
396         }
397 }
398
399 PrivilegeEscalator::~PrivilegeEscalator ()
400 {
401         unprivileged ();
402 }
403
404 PrivilegeEscalator::PrivilegeEscalator ()
405 {
406         int const r = seteuid(0);
407         if (r < 0) {
408                 throw PrivilegeError (String::compose("seteuid() call failed with %1", errno));
409         }
410 }
411
412 boost::filesystem::path
413 config_path ()
414 {
415         boost::filesystem::path p;
416         p /= g_get_user_config_dir ();
417         p /= "dcpomatic2";
418         return p;
419 }
420
421
422 void
423 disk_write_finished ()
424 {
425
426 }
427