a609e19f0d096c406954b3a4e8cf708e47db2a73
[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 <dcp/raw_convert.h>
28 #include <glib.h>
29 extern "C" {
30 #include <libavformat/avio.h>
31 }
32 #include <boost/algorithm/string.hpp>
33 #include <boost/foreach.hpp>
34 #include <unistd.h>
35 #include <mntent.h>
36 #include <sys/types.h>
37 #include <ifaddrs.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 #include <fstream>
41
42 #include "i18n.h"
43
44 using std::pair;
45 using std::list;
46 using std::ifstream;
47 using std::string;
48 using std::wstring;
49 using std::make_pair;
50 using std::vector;
51 using std::cerr;
52 using std::cout;
53 using std::runtime_error;
54 using boost::shared_ptr;
55 using boost::optional;
56
57 /** @param s Number of seconds to sleep for */
58 void
59 dcpomatic_sleep_seconds (int s)
60 {
61         sleep (s);
62 }
63
64 void
65 dcpomatic_sleep_milliseconds (int ms)
66 {
67         usleep (ms * 1000);
68 }
69
70 /** @return A string of CPU information (model name etc.) */
71 string
72 cpu_info ()
73 {
74         string info;
75
76         /* This use of ifstream is ok; the filename can never
77            be non-Latin
78         */
79         ifstream f ("/proc/cpuinfo");
80         while (f.good ()) {
81                 string l;
82                 getline (f, l);
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);
87                         }
88                 }
89         }
90
91         return info;
92 }
93
94 boost::filesystem::path
95 shared_path ()
96 {
97         char const * p = getenv ("DCPOMATIC_LINUX_SHARE_PREFIX");
98         if (p) {
99                 return p;
100         }
101         return boost::filesystem::canonical (LINUX_SHARE_PREFIX);
102 }
103
104 void
105 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
106 {
107         string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
108         LOG_GENERAL (N_("Probing with %1"), ffprobe);
109         system (ffprobe.c_str ());
110 }
111
112 list<pair<string, string> >
113 mount_info ()
114 {
115         list<pair<string, string> > m;
116
117         FILE* f = setmntent ("/etc/mtab", "r");
118         if (!f) {
119                 return m;
120         }
121
122         while (true) {
123                 struct mntent* mnt = getmntent (f);
124                 if (!mnt) {
125                         break;
126                 }
127
128                 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
129         }
130
131         endmntent (f);
132
133         return m;
134 }
135
136 boost::filesystem::path
137 openssl_path ()
138 {
139         return "dcpomatic2_openssl";
140 }
141
142 /* Apparently there is no way to create an ofstream using a UTF-8
143    filename under Windows.  We are hence reduced to using fopen
144    with this wrapper.
145 */
146 FILE *
147 fopen_boost (boost::filesystem::path p, string t)
148 {
149         return fopen (p.c_str(), t.c_str ());
150 }
151
152 int
153 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
154 {
155         return fseek (stream, offset, whence);
156 }
157
158 void
159 Waker::nudge ()
160 {
161
162 }
163
164 Waker::Waker ()
165 {
166
167 }
168
169 Waker::~Waker ()
170 {
171
172 }
173
174 void
175 start_tool (boost::filesystem::path dcpomatic, string executable, string)
176 {
177         boost::filesystem::path batch = dcpomatic.parent_path() / executable;
178
179         pid_t pid = fork ();
180         if (pid == 0) {
181                 int const r = system (batch.string().c_str());
182                 exit (WEXITSTATUS (r));
183         }
184 }
185
186 void
187 start_batch_converter (boost::filesystem::path dcpomatic)
188 {
189         start_tool (dcpomatic, "dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app");
190 }
191
192 void
193 start_player (boost::filesystem::path dcpomatic)
194 {
195         start_tool (dcpomatic, "dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app");
196 }
197
198 uint64_t
199 thread_id ()
200 {
201         return (uint64_t) pthread_self ();
202 }
203
204 int
205 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
206 {
207         return avio_open (s, file.c_str(), flags);
208 }
209
210
211 boost::filesystem::path
212 home_directory ()
213 {
214                 return getenv("HOME");
215 }
216
217 string
218 command_and_read (string cmd)
219 {
220         FILE* pipe = popen (cmd.c_str(), "r");
221         if (!pipe) {
222                 throw runtime_error ("popen failed");
223         }
224
225         string result;
226         char buffer[128];
227         try {
228                 while (fgets(buffer, sizeof(buffer), pipe)) {
229                         result += buffer;
230                 }
231         } catch (...) {
232                 pclose (pipe);
233                 throw;
234         }
235
236         pclose (pipe);
237         return result;
238 }
239
240 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
241 bool
242 running_32_on_64 ()
243 {
244         /* I'm assuming nobody does this on Linux */
245         return false;
246 }
247
248 vector<Drive>
249 get_drives ()
250 {
251         vector<Drive> drives;
252
253         using namespace boost::filesystem;
254         list<string> mounted_devices;
255         std::ifstream f("/proc/mounts");
256         string line;
257         while (f.good()) {
258                 getline(f, line);
259                 vector<string> bits;
260                 boost::algorithm::split (bits, line, boost::is_any_of(" "));
261                 if (bits.size() > 0 && boost::algorithm::starts_with(bits[0], "/dev/")) {
262                         mounted_devices.push_back(bits[0]);
263                         LOG_DIST("Mounted device %1", bits[0]);
264                 }
265         }
266
267         for (directory_iterator i = directory_iterator("/sys/block"); i != directory_iterator(); ++i) {
268                 string const name = i->path().filename().string();
269                 path device_type_file("/sys/block/" + name + "/device/type");
270                 optional<string> device_type;
271                 if (exists(device_type_file)) {
272                         device_type = dcp::file_to_string (device_type_file);
273                         boost::trim(*device_type);
274                 }
275                 /* Device type 5 is "SCSI_TYPE_ROM" in blkdev.h; seems usually to be a CD/DVD drive */
276                 if (!boost::algorithm::starts_with(name, "loop") && (!device_type || *device_type != "5")) {
277                         uint64_t const size = dcp::raw_convert<uint64_t>(dcp::file_to_string(*i / "size")) * 512;
278                         if (size == 0) {
279                                 continue;
280                         }
281                         bool mounted = false;
282                         optional<string> vendor;
283                         try {
284                                 vendor = dcp::file_to_string("/sys/block/" + name + "/device/vendor");
285                                 boost::trim(*vendor);
286                         } catch (...) {}
287                         optional<string> model;
288                         try {
289                                 model = dcp::file_to_string("/sys/block/" + name + "/device/model");
290                                 boost::trim(*model);
291                         } catch (...) {}
292                         BOOST_FOREACH (string j, mounted_devices) {
293                                 if (boost::algorithm::starts_with(j, "/dev/" + name)) {
294                                         mounted = true;
295                                 }
296                         }
297                         drives.push_back(Drive(i->path().filename().string(), size, mounted, vendor, model));
298                         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]"));
299                 }
300         }
301
302         return drives;
303 }
304
305 string
306 Drive::description () const
307 {
308         char gb[64];
309         snprintf(gb, 64, "%.1f", _size / 1000000000.0);
310
311         string name;
312         if (_vendor) {
313                 name += *_vendor;
314         }
315         if (_model) {
316                 if (name.size() > 0) {
317                         name += " " + *_model;
318                 }
319         }
320         if (name.size() == 0) {
321                 name = _("Unknown");
322         }
323
324         return String::compose("%1 (%2 GB) [%3]", name, gb, _internal_name);
325 }
326
327 void
328 unprivileged ()
329 {
330         uid_t ruid, euid, suid;
331         if (getresuid(&ruid, &euid, &suid) == -1) {
332                 cerr << "getresuid() failed.\n";
333                 exit (EXIT_FAILURE);
334         }
335         seteuid (ruid);
336 }
337
338 PrivilegeEscalator::~PrivilegeEscalator ()
339 {
340         unprivileged ();
341 }
342
343 PrivilegeEscalator::PrivilegeEscalator ()
344 {
345         seteuid (0);
346 }
347
348 boost::filesystem::path
349 config_path ()
350 {
351         boost::filesystem::path p;
352         p /= g_get_user_config_dir ();
353         p /= "dcpomatic2";
354         return p;
355 }