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 <dcp/raw_convert.h>
30 #include <libavformat/avio.h>
32 #include <boost/algorithm/string.hpp>
33 #include <boost/foreach.hpp>
36 #include <sys/types.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
53 using std::runtime_error;
54 using boost::shared_ptr;
55 using boost::optional;
57 /** @param s Number of seconds to sleep for */
59 dcpomatic_sleep_seconds (int s)
65 dcpomatic_sleep_milliseconds (int ms)
70 /** @return A string of CPU information (model name etc.) */
76 /* This use of ifstream is ok; the filename can never
79 ifstream f ("/proc/cpuinfo");
83 if (boost::algorithm::starts_with (l, "model name")) {
84 string::size_type const c = l.find (':');
85 if (c != string::npos) {
86 info = l.substr (c + 2);
94 boost::filesystem::path
97 char const * p = getenv ("DCPOMATIC_LINUX_SHARE_PREFIX");
101 return boost::filesystem::canonical (LINUX_SHARE_PREFIX);
105 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
107 string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
108 LOG_GENERAL (N_("Probing with %1"), ffprobe);
109 system (ffprobe.c_str ());
112 list<pair<string, string> >
115 list<pair<string, string> > m;
117 FILE* f = setmntent ("/etc/mtab", "r");
123 struct mntent* mnt = getmntent (f);
128 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
136 boost::filesystem::path
139 return "dcpomatic2_openssl";
142 boost::filesystem::path
145 return "dcpomatic2_dist_writer";
148 /* Apparently there is no way to create an ofstream using a UTF-8
149 filename under Windows. We are hence reduced to using fopen
153 fopen_boost (boost::filesystem::path p, string t)
155 return fopen (p.c_str(), t.c_str ());
159 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
161 return fseek (stream, offset, whence);
181 start_tool (boost::filesystem::path dcpomatic, string executable, string)
183 boost::filesystem::path batch = dcpomatic.parent_path() / executable;
187 int const r = system (batch.string().c_str());
188 exit (WEXITSTATUS (r));
193 start_batch_converter (boost::filesystem::path dcpomatic)
195 start_tool (dcpomatic, "dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app");
199 start_player (boost::filesystem::path dcpomatic)
201 start_tool (dcpomatic, "dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app");
207 return (uint64_t) pthread_self ();
211 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
213 return avio_open (s, file.c_str(), flags);
217 boost::filesystem::path
220 return getenv("HOME");
224 command_and_read (string cmd)
226 FILE* pipe = popen (cmd.c_str(), "r");
228 throw runtime_error ("popen failed");
234 while (fgets(buffer, sizeof(buffer), pipe)) {
246 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
250 /* I'm assuming nobody does this on Linux */
257 vector<Drive> drives;
259 using namespace boost::filesystem;
260 list<string> mounted_devices;
261 std::ifstream f("/proc/mounts");
266 boost::algorithm::split (bits, line, boost::is_any_of(" "));
267 if (bits.size() > 0 && boost::algorithm::starts_with(bits[0], "/dev/")) {
268 mounted_devices.push_back(bits[0]);
269 LOG_DIST("Mounted device %1", bits[0]);
273 for (directory_iterator i = directory_iterator("/sys/block"); i != directory_iterator(); ++i) {
274 string const name = i->path().filename().string();
275 path device_type_file("/sys/block/" + name + "/device/type");
276 optional<string> device_type;
277 if (exists(device_type_file)) {
278 device_type = dcp::file_to_string (device_type_file);
279 boost::trim(*device_type);
281 /* Device type 5 is "SCSI_TYPE_ROM" in blkdev.h; seems usually to be a CD/DVD drive */
282 if (!boost::algorithm::starts_with(name, "loop") && (!device_type || *device_type != "5")) {
283 uint64_t const size = dcp::raw_convert<uint64_t>(dcp::file_to_string(*i / "size")) * 512;
287 bool mounted = false;
288 optional<string> vendor;
290 vendor = dcp::file_to_string("/sys/block/" + name + "/device/vendor");
291 boost::trim(*vendor);
293 optional<string> model;
295 model = dcp::file_to_string("/sys/block/" + name + "/device/model");
298 BOOST_FOREACH (string j, mounted_devices) {
299 if (boost::algorithm::starts_with(j, "/dev/" + name)) {
303 drives.push_back(Drive(i->path().filename().string(), size, mounted, vendor, model));
304 LOG_DIST("Block device %1 size %2 %3 vendor %4 model %5", name, size, mounted ? "mounted" : "not mounted", vendor.get_value_or("[none]"), model.get_value_or("[none]"));
312 Drive::description () const
315 snprintf(gb, 64, "%.1f", _size / 1000000000.0);
322 if (name.size() > 0) {
323 name += " " + *_model;
326 if (name.size() == 0) {
330 return String::compose("%1 (%2 GB) [%3]", name, gb, _internal_name);
336 uid_t ruid, euid, suid;
337 if (getresuid(&ruid, &euid, &suid) == -1) {
338 cerr << "getresuid() failed.\n";
344 PrivilegeEscalator::~PrivilegeEscalator ()
349 PrivilegeEscalator::PrivilegeEscalator ()
354 boost::filesystem::path
357 boost::filesystem::path p;
358 p /= g_get_user_config_dir ();