Tidy up Drive and unmounting a little.
[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
262 static
263 vector<pair<string, string> >
264 get_mounts (string prefix)
265 {
266         vector<pair<string, string> > mounts;
267
268         std::ifstream f("/proc/mounts");
269         string line;
270         while (f.good()) {
271                 getline(f, line);
272                 vector<string> bits;
273                 boost::algorithm::split (bits, line, boost::is_any_of(" "));
274                 if (bits.size() > 1 && boost::algorithm::starts_with(bits[0], prefix)) {
275                         mounts.push_back(make_pair(bits[0], bits[1]));
276                         LOG_DISK("Found mounted device %1 from prefix %2", bits[0], prefix);
277                 }
278         }
279
280         return mounts;
281 }
282
283
284 vector<Drive>
285 Drive::get ()
286 {
287         vector<Drive> drives;
288
289         using namespace boost::filesystem;
290         vector<pair<string, string> > mounted_devices = get_mounts("/dev/");
291
292         for (directory_iterator i = directory_iterator("/sys/block"); i != directory_iterator(); ++i) {
293                 string const name = i->path().filename().string();
294                 path device_type_file("/sys/block/" + name + "/device/type");
295                 optional<string> device_type;
296                 if (exists(device_type_file)) {
297                         device_type = dcp::file_to_string (device_type_file);
298                         boost::trim(*device_type);
299                 }
300                 /* Device type 5 is "SCSI_TYPE_ROM" in blkdev.h; seems usually to be a CD/DVD drive */
301                 if (!boost::algorithm::starts_with(name, "loop") && (!device_type || *device_type != "5")) {
302                         uint64_t const size = dcp::raw_convert<uint64_t>(dcp::file_to_string(*i / "size")) * 512;
303                         if (size == 0) {
304                                 continue;
305                         }
306                         optional<string> vendor;
307                         try {
308                                 vendor = dcp::file_to_string("/sys/block/" + name + "/device/vendor");
309                                 boost::trim(*vendor);
310                         } catch (...) {}
311                         optional<string> model;
312                         try {
313                                 model = dcp::file_to_string("/sys/block/" + name + "/device/model");
314                                 boost::trim(*model);
315                         } catch (...) {}
316                         vector<boost::filesystem::path> mount_points;
317                         for (vector<pair<string, string> >::const_iterator j = mounted_devices.begin(); j != mounted_devices.end(); ++j) {
318                                 if (boost::algorithm::starts_with(j->first, "/dev/" + name)) {
319                                         mount_points.push_back (j->second);
320                                 }
321                         }
322                         drives.push_back(Drive("/dev/" + name, mount_points, size, vendor, model));
323                         LOG_DISK_NC(drives.back().log_summary());
324                 }
325         }
326
327         return drives;
328 }
329
330
331 bool
332 Drive::unmount () 
333 {
334         BOOST_FOREACH (boost::filesystem::path i, _mount_points) {
335                 int const r = umount(i.string().c_str());
336                 LOG_DISK("Tried to unmount %1 and got %2 and %3", i.string(), r, errno);
337                 if (r == -1) {
338                         return false;
339                 }
340         }
341         return true;
342 }
343
344
345 void
346 unprivileged ()
347 {
348         uid_t ruid, euid, suid;
349         if (getresuid(&ruid, &euid, &suid) == -1) {
350                 cerr << "getresuid() failed.\n";
351                 exit (EXIT_FAILURE);
352         }
353         seteuid (ruid);
354 }
355
356 PrivilegeEscalator::~PrivilegeEscalator ()
357 {
358         unprivileged ();
359 }
360
361 PrivilegeEscalator::PrivilegeEscalator ()
362 {
363         seteuid (0);
364 }
365
366 boost::filesystem::path
367 config_path ()
368 {
369         boost::filesystem::path p;
370         p /= g_get_user_config_dir ();
371         p /= "dcpomatic2";
372         return p;
373 }
374