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