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