Tidy up nanomsg class API; add unmounting for 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/foreach.hpp>
35 #ifdef DCPOMATIC_DISK
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 boost::shared_ptr;
60 using boost::optional;
61
62 /** @param s Number of seconds to sleep for */
63 void
64 dcpomatic_sleep_seconds (int s)
65 {
66         sleep (s);
67 }
68
69 void
70 dcpomatic_sleep_milliseconds (int ms)
71 {
72         usleep (ms * 1000);
73 }
74
75 /** @return A string of CPU information (model name etc.) */
76 string
77 cpu_info ()
78 {
79         string info;
80
81         /* This use of ifstream is ok; the filename can never
82            be non-Latin
83         */
84         ifstream f ("/proc/cpuinfo");
85         while (f.good ()) {
86                 string l;
87                 getline (f, l);
88                 if (boost::algorithm::starts_with (l, "model name")) {
89                         string::size_type const c = l.find (':');
90                         if (c != string::npos) {
91                                 info = l.substr (c + 2);
92                         }
93                 }
94         }
95
96         return info;
97 }
98
99 boost::filesystem::path
100 shared_path ()
101 {
102         char const * p = getenv ("DCPOMATIC_LINUX_SHARE_PREFIX");
103         if (p) {
104                 return p;
105         }
106         return boost::filesystem::canonical (LINUX_SHARE_PREFIX);
107 }
108
109 void
110 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
111 {
112         string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
113         LOG_GENERAL (N_("Probing with %1"), ffprobe);
114         system (ffprobe.c_str ());
115 }
116
117 list<pair<string, string> >
118 mount_info ()
119 {
120         list<pair<string, string> > m;
121
122         FILE* f = setmntent ("/etc/mtab", "r");
123         if (!f) {
124                 return m;
125         }
126
127         while (true) {
128                 struct mntent* mnt = getmntent (f);
129                 if (!mnt) {
130                         break;
131                 }
132
133                 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
134         }
135
136         endmntent (f);
137
138         return m;
139 }
140
141 boost::filesystem::path
142 openssl_path ()
143 {
144         return "dcpomatic2_openssl";
145 }
146
147 #ifdef DCPOMATIC_DISK
148 boost::filesystem::path
149 disk_writer_path ()
150 {
151         return boost::dll::program_location().parent_path() / "dcpomatic2_disk_writer";
152 }
153 #endif
154
155 /* Apparently there is no way to create an ofstream using a UTF-8
156    filename under Windows.  We are hence reduced to using fopen
157    with this wrapper.
158 */
159 FILE *
160 fopen_boost (boost::filesystem::path p, string t)
161 {
162         return fopen (p.c_str(), t.c_str ());
163 }
164
165 int
166 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
167 {
168         return fseek (stream, offset, whence);
169 }
170
171 void
172 Waker::nudge ()
173 {
174
175 }
176
177 Waker::Waker ()
178 {
179
180 }
181
182 Waker::~Waker ()
183 {
184
185 }
186
187 void
188 start_tool (boost::filesystem::path dcpomatic, string executable, string)
189 {
190         boost::filesystem::path batch = dcpomatic.parent_path() / executable;
191
192         pid_t pid = fork ();
193         if (pid == 0) {
194                 int const r = system (batch.string().c_str());
195                 exit (WEXITSTATUS (r));
196         }
197 }
198
199 void
200 start_batch_converter (boost::filesystem::path dcpomatic)
201 {
202         start_tool (dcpomatic, "dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app");
203 }
204
205 void
206 start_player (boost::filesystem::path dcpomatic)
207 {
208         start_tool (dcpomatic, "dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app");
209 }
210
211 uint64_t
212 thread_id ()
213 {
214         return (uint64_t) pthread_self ();
215 }
216
217 int
218 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
219 {
220         return avio_open (s, file.c_str(), flags);
221 }
222
223
224 boost::filesystem::path
225 home_directory ()
226 {
227                 return getenv("HOME");
228 }
229
230 string
231 command_and_read (string cmd)
232 {
233         FILE* pipe = popen (cmd.c_str(), "r");
234         if (!pipe) {
235                 throw runtime_error ("popen failed");
236         }
237
238         string result;
239         char buffer[128];
240         try {
241                 while (fgets(buffer, sizeof(buffer), pipe)) {
242                         result += buffer;
243                 }
244         } catch (...) {
245                 pclose (pipe);
246                 throw;
247         }
248
249         pclose (pipe);
250         return result;
251 }
252
253 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
254 bool
255 running_32_on_64 ()
256 {
257         /* I'm assuming nobody does this on Linux */
258         return false;
259 }
260
261 vector<pair<string, string> >
262 get_mounts (string prefix)
263 {
264         vector<pair<string, string> > mounts;
265
266         std::ifstream f("/proc/mounts");
267         string line;
268         while (f.good()) {
269                 getline(f, line);
270                 vector<string> bits;
271                 boost::algorithm::split (bits, line, boost::is_any_of(" "));
272                 if (bits.size() > 1 && boost::algorithm::starts_with(bits[0], prefix)) {
273                         mounts.push_back(make_pair(bits[0], bits[1]));
274                         LOG_DISK("Found mounted device %1 from prefix %2", bits[0], prefix);
275                 }
276         }
277
278         return mounts;
279 }
280
281 vector<Drive>
282 get_drives ()
283 {
284         vector<Drive> drives;
285
286         using namespace boost::filesystem;
287         vector<pair<string, string> > mounted_devices = get_mounts("/dev/");
288
289         for (directory_iterator i = directory_iterator("/sys/block"); i != directory_iterator(); ++i) {
290                 string const name = i->path().filename().string();
291                 path device_type_file("/sys/block/" + name + "/device/type");
292                 optional<string> device_type;
293                 if (exists(device_type_file)) {
294                         device_type = dcp::file_to_string (device_type_file);
295                         boost::trim(*device_type);
296                 }
297                 /* Device type 5 is "SCSI_TYPE_ROM" in blkdev.h; seems usually to be a CD/DVD drive */
298                 if (!boost::algorithm::starts_with(name, "loop") && (!device_type || *device_type != "5")) {
299                         uint64_t const size = dcp::raw_convert<uint64_t>(dcp::file_to_string(*i / "size")) * 512;
300                         if (size == 0) {
301                                 continue;
302                         }
303                         bool mounted = false;
304                         optional<string> vendor;
305                         try {
306                                 vendor = dcp::file_to_string("/sys/block/" + name + "/device/vendor");
307                                 boost::trim(*vendor);
308                         } catch (...) {}
309                         optional<string> model;
310                         try {
311                                 model = dcp::file_to_string("/sys/block/" + name + "/device/model");
312                                 boost::trim(*model);
313                         } catch (...) {}
314                         for (vector<pair<string, string> >::const_iterator j = mounted_devices.begin(); j != mounted_devices.end(); ++j) {
315                                 if (boost::algorithm::starts_with(j->first, "/dev/" + name)) {
316                                         mounted = true;
317                                 }
318                         }
319                         drives.push_back(Drive("/dev/" + i->path().filename().string(), size, mounted, vendor, model));
320                         LOG_DISK("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]"));
321                 }
322         }
323
324         return drives;
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 }
356
357 bool
358 unmount_drive (string drive)
359 {
360         vector<pair<string, string> > mounts = get_mounts (drive);
361         for (vector<pair<string, string> >::const_iterator i = mounts.begin(); i != mounts.end(); ++i) {
362                 int const r = umount(i->second.c_str());
363                 LOG_DISK("Tried to unmount %1 and got %2 and %3", i->second, r, errno);
364                 if (r == -1) {
365                         return false;
366                 }
367         }
368         return true;
369 }
370