2 Copyright (C) 2012-2020 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "compose.hpp"
24 #include "dcpomatic_log.h"
26 #include "exceptions.h"
27 #include "dcpomatic_log.h"
28 #include <dcp/raw_convert.h>
31 #include <libavformat/avio.h>
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>
41 #include <sys/types.h>
42 #include <sys/mount.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
59 using std::runtime_error;
60 using boost::shared_ptr;
61 using boost::optional;
62 using boost::function;
64 /** @param s Number of seconds to sleep for */
66 dcpomatic_sleep_seconds (int s)
72 dcpomatic_sleep_milliseconds (int ms)
77 /** @return A string of CPU information (model name etc.) */
83 /* This use of ifstream is ok; the filename can never
86 ifstream f ("/proc/cpuinfo");
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);
101 boost::filesystem::path
104 char* prefix = getenv ("DCPOMATIC_SHARE_PREFIX");
106 return boost::filesystem::path(prefix) / "dcpomatic2";
109 return directory_containing_executable().parent_path() / "share" / "dcpomatic2";
113 boost::filesystem::path
116 return boost::filesystem::canonical(LINUX_SHARE_PREFIX) / "libdcp" / "xsd";
120 boost::filesystem::path
123 return boost::filesystem::canonical(LINUX_SHARE_PREFIX) / "libdcp" / "tags";
128 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
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);
138 list<pair<string, string> >
141 list<pair<string, string> > m;
143 FILE* f = setmntent ("/etc/mtab", "r");
149 struct mntent* mnt = getmntent (f);
154 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
163 boost::filesystem::path
164 directory_containing_executable ()
166 #if BOOST_VERSION >= 106100
167 return boost::dll::program_location().parent_path();
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();
176 boost::filesystem::path
179 boost::filesystem::path p = directory_containing_executable() / "dcpomatic2_openssl";
180 if (boost::filesystem::is_regular_file(p)) {
184 return "dcpomatic2_openssl";
188 #ifdef DCPOMATIC_DISK
189 boost::filesystem::path
192 return directory_containing_executable() / "dcpomatic2_disk_writer";
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
202 fopen_boost (boost::filesystem::path p, string t)
204 return fopen (p.c_str(), t.c_str ());
208 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
210 return fseek (stream, offset, whence);
231 start_tool (string executable)
233 boost::filesystem::path batch = directory_containing_executable() / executable;
237 int const r = system (batch.string().c_str());
238 exit (WEXITSTATUS (r));
244 start_batch_converter ()
246 start_tool ("dcpomatic2_batch");
253 start_tool ("dcpomatic2_player");
260 return (uint64_t) pthread_self ();
264 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
266 return avio_open (s, file.c_str(), flags);
270 boost::filesystem::path
273 return getenv("HOME");
277 command_and_read (string cmd)
279 FILE* pipe = popen (cmd.c_str(), "r");
281 throw runtime_error ("popen failed");
287 while (fgets(buffer, sizeof(buffer), pipe)) {
299 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
303 /* I'm assuming nobody does this on Linux */
309 vector<pair<string, string> >
310 get_mounts (string prefix)
312 vector<pair<string, string> > mounts;
314 std::ifstream f("/proc/mounts");
319 boost::algorithm::split (bits, line, boost::is_any_of(" "));
320 if (bits.size() > 1 && boost::algorithm::starts_with(bits[0], prefix)) {
321 boost::algorithm::replace_all (bits[1], "\\040", " ");
322 mounts.push_back(make_pair(bits[0], bits[1]));
323 LOG_DISK("Found mounted device %1 from prefix %2", bits[0], prefix);
334 vector<Drive> drives;
336 using namespace boost::filesystem;
337 vector<pair<string, string> > mounted_devices = get_mounts("/dev/");
339 for (directory_iterator i = directory_iterator("/sys/block"); i != directory_iterator(); ++i) {
340 string const name = i->path().filename().string();
341 path device_type_file("/sys/block/" + name + "/device/type");
342 optional<string> device_type;
343 if (exists(device_type_file)) {
344 device_type = dcp::file_to_string (device_type_file);
345 boost::trim(*device_type);
347 /* Device type 5 is "SCSI_TYPE_ROM" in blkdev.h; seems usually to be a CD/DVD drive */
348 if (!boost::algorithm::starts_with(name, "loop") && (!device_type || *device_type != "5")) {
349 uint64_t const size = dcp::raw_convert<uint64_t>(dcp::file_to_string(*i / "size")) * 512;
353 optional<string> vendor;
355 vendor = dcp::file_to_string("/sys/block/" + name + "/device/vendor");
356 boost::trim(*vendor);
358 optional<string> model;
360 model = dcp::file_to_string("/sys/block/" + name + "/device/model");
363 vector<boost::filesystem::path> mount_points;
364 for (vector<pair<string, string> >::const_iterator j = mounted_devices.begin(); j != mounted_devices.end(); ++j) {
365 if (boost::algorithm::starts_with(j->first, "/dev/" + name)) {
366 mount_points.push_back (j->second);
369 drives.push_back(Drive("/dev/" + name, mount_points, size, vendor, model));
370 LOG_DISK_NC(drives.back().log_summary());
381 BOOST_FOREACH (boost::filesystem::path i, _mount_points) {
382 int const r = umount(i.string().c_str());
383 LOG_DISK("Tried to unmount %1 and got %2 and %3", i.string(), r, errno);
395 uid_t ruid, euid, suid;
396 if (getresuid(&ruid, &euid, &suid) == -1) {
397 cerr << "getresuid() failed.\n";
399 if (seteuid(ruid) == -1) {
400 cerr << "seteuid() failed.\n";
404 PrivilegeEscalator::~PrivilegeEscalator ()
409 PrivilegeEscalator::PrivilegeEscalator ()
411 int const r = seteuid(0);
413 throw PrivilegeError (String::compose("seteuid() call failed with %1", errno));
417 boost::filesystem::path
420 boost::filesystem::path p;
421 p /= g_get_user_config_dir ();
428 disk_write_finished ()