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