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