Mercurial > hg > orthanc-stone
annotate Resources/Orthanc/Core/SystemToolbox.cpp @ 22:b01d46e5a2b3
support for sandboxed environments
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 23 Nov 2016 12:08:15 +0100 |
parents | ff1e935768e7 |
children | a4a1e4033403 |
rev | line source |
---|---|
15 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include "PrecompiledHeaders.h" | |
34 #include "SystemToolbox.h" | |
35 | |
36 | |
37 #if BOOST_HAS_DATE_TIME == 1 | |
38 #include <boost/date_time/posix_time/posix_time.hpp> | |
39 #endif | |
40 | |
41 | |
42 #if defined(_WIN32) | |
43 #include <windows.h> | |
44 #include <process.h> // For "_spawnvp()" and "_getpid()" | |
45 #else | |
46 #include <unistd.h> // For "execvp()" | |
47 #include <sys/wait.h> // For "waitpid()" | |
48 #endif | |
49 | |
50 #if defined(__APPLE__) && defined(__MACH__) | |
51 #include <mach-o/dyld.h> /* _NSGetExecutablePath */ | |
52 #include <limits.h> /* PATH_MAX */ | |
53 #endif | |
54 | |
55 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__) | |
56 #include <limits.h> /* PATH_MAX */ | |
57 #include <signal.h> | |
58 #include <unistd.h> | |
59 #endif | |
60 | |
61 | |
22
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
62 // Inclusions for UUID |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
63 // http://stackoverflow.com/a/1626302 |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
64 |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
65 extern "C" |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
66 { |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
67 #ifdef WIN32 |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
68 # include <rpc.h> |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
69 #else |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
70 # include <uuid/uuid.h> |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
71 #endif |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
72 } |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
73 |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
74 |
15 | 75 #include "Logging.h" |
76 #include "OrthancException.h" | |
77 #include "Toolbox.h" | |
78 | |
79 #include <boost/filesystem.hpp> | |
80 #include <boost/filesystem/fstream.hpp> | |
81 | |
82 | |
83 namespace Orthanc | |
84 { | |
85 static bool finish_; | |
86 static ServerBarrierEvent barrierEvent_; | |
87 | |
88 #if defined(_WIN32) | |
89 static BOOL WINAPI ConsoleControlHandler(DWORD dwCtrlType) | |
90 { | |
91 // http://msdn.microsoft.com/en-us/library/ms683242(v=vs.85).aspx | |
92 finish_ = true; | |
93 return true; | |
94 } | |
95 #else | |
96 static void SignalHandler(int signal) | |
97 { | |
98 if (signal == SIGHUP) | |
99 { | |
100 barrierEvent_ = ServerBarrierEvent_Reload; | |
101 } | |
102 | |
103 finish_ = true; | |
104 } | |
105 #endif | |
106 | |
107 | |
108 static ServerBarrierEvent ServerBarrierInternal(const bool* stopFlag) | |
109 { | |
110 #if defined(_WIN32) | |
111 SetConsoleCtrlHandler(ConsoleControlHandler, true); | |
112 #else | |
113 signal(SIGINT, SignalHandler); | |
114 signal(SIGQUIT, SignalHandler); | |
115 signal(SIGTERM, SignalHandler); | |
116 signal(SIGHUP, SignalHandler); | |
117 #endif | |
118 | |
119 // Active loop that awakens every 100ms | |
120 finish_ = false; | |
121 barrierEvent_ = ServerBarrierEvent_Stop; | |
122 while (!(*stopFlag || finish_)) | |
123 { | |
124 Toolbox::USleep(100 * 1000); | |
125 } | |
126 | |
127 #if defined(_WIN32) | |
128 SetConsoleCtrlHandler(ConsoleControlHandler, false); | |
129 #else | |
130 signal(SIGINT, NULL); | |
131 signal(SIGQUIT, NULL); | |
132 signal(SIGTERM, NULL); | |
133 signal(SIGHUP, NULL); | |
134 #endif | |
135 | |
136 return barrierEvent_; | |
137 } | |
138 | |
139 | |
140 ServerBarrierEvent SystemToolbox::ServerBarrier(const bool& stopFlag) | |
141 { | |
142 return ServerBarrierInternal(&stopFlag); | |
143 } | |
144 | |
145 | |
146 ServerBarrierEvent SystemToolbox::ServerBarrier() | |
147 { | |
148 const bool stopFlag = false; | |
149 return ServerBarrierInternal(&stopFlag); | |
150 } | |
151 | |
152 | |
153 static std::streamsize GetStreamSize(std::istream& f) | |
154 { | |
155 // http://www.cplusplus.com/reference/iostream/istream/tellg/ | |
156 f.seekg(0, std::ios::end); | |
157 std::streamsize size = f.tellg(); | |
158 f.seekg(0, std::ios::beg); | |
159 | |
160 return size; | |
161 } | |
162 | |
163 | |
164 void SystemToolbox::ReadFile(std::string& content, | |
165 const std::string& path) | |
166 { | |
167 if (!IsRegularFile(path)) | |
168 { | |
169 LOG(ERROR) << std::string("The path does not point to a regular file: ") << path; | |
170 throw OrthancException(ErrorCode_RegularFileExpected); | |
171 } | |
172 | |
173 boost::filesystem::ifstream f; | |
174 f.open(path, std::ifstream::in | std::ifstream::binary); | |
175 if (!f.good()) | |
176 { | |
177 throw OrthancException(ErrorCode_InexistentFile); | |
178 } | |
179 | |
180 std::streamsize size = GetStreamSize(f); | |
181 content.resize(size); | |
182 if (size != 0) | |
183 { | |
184 f.read(reinterpret_cast<char*>(&content[0]), size); | |
185 } | |
186 | |
187 f.close(); | |
188 } | |
189 | |
190 | |
191 bool SystemToolbox::ReadHeader(std::string& header, | |
192 const std::string& path, | |
193 size_t headerSize) | |
194 { | |
195 if (!IsRegularFile(path)) | |
196 { | |
197 LOG(ERROR) << std::string("The path does not point to a regular file: ") << path; | |
198 throw OrthancException(ErrorCode_RegularFileExpected); | |
199 } | |
200 | |
201 boost::filesystem::ifstream f; | |
202 f.open(path, std::ifstream::in | std::ifstream::binary); | |
203 if (!f.good()) | |
204 { | |
205 throw OrthancException(ErrorCode_InexistentFile); | |
206 } | |
207 | |
208 bool full = true; | |
209 | |
210 { | |
211 std::streamsize size = GetStreamSize(f); | |
212 if (size <= 0) | |
213 { | |
214 headerSize = 0; | |
215 full = false; | |
216 } | |
217 else if (static_cast<size_t>(size) < headerSize) | |
218 { | |
219 headerSize = size; // Truncate to the size of the file | |
220 full = false; | |
221 } | |
222 } | |
223 | |
224 header.resize(headerSize); | |
225 if (headerSize != 0) | |
226 { | |
227 f.read(reinterpret_cast<char*>(&header[0]), headerSize); | |
228 } | |
229 | |
230 f.close(); | |
231 | |
232 return full; | |
233 } | |
234 | |
235 | |
236 void SystemToolbox::WriteFile(const void* content, | |
237 size_t size, | |
238 const std::string& path) | |
239 { | |
240 boost::filesystem::ofstream f; | |
241 f.open(path, std::ofstream::out | std::ofstream::binary); | |
242 if (!f.good()) | |
243 { | |
244 throw OrthancException(ErrorCode_CannotWriteFile); | |
245 } | |
246 | |
247 if (size != 0) | |
248 { | |
249 f.write(reinterpret_cast<const char*>(content), size); | |
250 | |
251 if (!f.good()) | |
252 { | |
253 f.close(); | |
254 throw OrthancException(ErrorCode_FileStorageCannotWrite); | |
255 } | |
256 } | |
257 | |
258 f.close(); | |
259 } | |
260 | |
261 | |
262 void SystemToolbox::WriteFile(const std::string& content, | |
263 const std::string& path) | |
264 { | |
265 WriteFile(content.size() > 0 ? content.c_str() : NULL, | |
266 content.size(), path); | |
267 } | |
268 | |
269 | |
270 void SystemToolbox::RemoveFile(const std::string& path) | |
271 { | |
272 if (boost::filesystem::exists(path)) | |
273 { | |
274 if (IsRegularFile(path)) | |
275 { | |
276 boost::filesystem::remove(path); | |
277 } | |
278 else | |
279 { | |
280 throw OrthancException(ErrorCode_RegularFileExpected); | |
281 } | |
282 } | |
283 } | |
284 | |
285 | |
286 uint64_t SystemToolbox::GetFileSize(const std::string& path) | |
287 { | |
288 try | |
289 { | |
290 return static_cast<uint64_t>(boost::filesystem::file_size(path)); | |
291 } | |
292 catch (boost::filesystem::filesystem_error&) | |
293 { | |
294 throw OrthancException(ErrorCode_InexistentFile); | |
295 } | |
296 } | |
297 | |
298 | |
299 void SystemToolbox::MakeDirectory(const std::string& path) | |
300 { | |
301 if (boost::filesystem::exists(path)) | |
302 { | |
303 if (!boost::filesystem::is_directory(path)) | |
304 { | |
305 throw OrthancException(ErrorCode_DirectoryOverFile); | |
306 } | |
307 } | |
308 else | |
309 { | |
310 if (!boost::filesystem::create_directories(path)) | |
311 { | |
312 throw OrthancException(ErrorCode_MakeDirectory); | |
313 } | |
314 } | |
315 } | |
316 | |
317 | |
318 bool SystemToolbox::IsExistingFile(const std::string& path) | |
319 { | |
320 return boost::filesystem::exists(path); | |
321 } | |
322 | |
323 | |
324 #if defined(_WIN32) | |
325 static std::string GetPathToExecutableInternal() | |
326 { | |
327 // Yes, this is ugly, but there is no simple way to get the | |
328 // required buffer size, so we use a big constant | |
329 std::vector<char> buffer(32768); | |
330 /*int bytes =*/ GetModuleFileNameA(NULL, &buffer[0], static_cast<DWORD>(buffer.size() - 1)); | |
331 return std::string(&buffer[0]); | |
332 } | |
333 | |
334 #elif defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__) | |
335 static std::string GetPathToExecutableInternal() | |
336 { | |
337 std::vector<char> buffer(PATH_MAX + 1); | |
338 ssize_t bytes = readlink("/proc/self/exe", &buffer[0], buffer.size() - 1); | |
339 if (bytes == 0) | |
340 { | |
341 throw OrthancException(ErrorCode_PathToExecutable); | |
342 } | |
343 | |
344 return std::string(&buffer[0]); | |
345 } | |
346 | |
347 #elif defined(__APPLE__) && defined(__MACH__) | |
348 static std::string GetPathToExecutableInternal() | |
349 { | |
350 char pathbuf[PATH_MAX + 1]; | |
351 unsigned int bufsize = static_cast<int>(sizeof(pathbuf)); | |
352 | |
353 _NSGetExecutablePath( pathbuf, &bufsize); | |
354 | |
355 return std::string(pathbuf); | |
356 } | |
357 | |
358 #else | |
359 #error Support your platform here | |
360 #endif | |
361 | |
362 | |
363 std::string SystemToolbox::GetPathToExecutable() | |
364 { | |
365 boost::filesystem::path p(GetPathToExecutableInternal()); | |
366 return boost::filesystem::absolute(p).string(); | |
367 } | |
368 | |
369 | |
370 std::string SystemToolbox::GetDirectoryOfExecutable() | |
371 { | |
372 boost::filesystem::path p(GetPathToExecutableInternal()); | |
373 return boost::filesystem::absolute(p.parent_path()).string(); | |
374 } | |
375 | |
376 | |
377 void SystemToolbox::ExecuteSystemCommand(const std::string& command, | |
378 const std::vector<std::string>& arguments) | |
379 { | |
380 // Convert the arguments as a C array | |
381 std::vector<char*> args(arguments.size() + 2); | |
382 | |
383 args.front() = const_cast<char*>(command.c_str()); | |
384 | |
385 for (size_t i = 0; i < arguments.size(); i++) | |
386 { | |
387 args[i + 1] = const_cast<char*>(arguments[i].c_str()); | |
388 } | |
389 | |
390 args.back() = NULL; | |
391 | |
392 int status; | |
393 | |
394 #if defined(_WIN32) | |
395 // http://msdn.microsoft.com/en-us/library/275khfab.aspx | |
396 status = static_cast<int>(_spawnvp(_P_OVERLAY, command.c_str(), &args[0])); | |
397 | |
398 #else | |
399 int pid = fork(); | |
400 | |
401 if (pid == -1) | |
402 { | |
403 // Error in fork() | |
404 #if ORTHANC_ENABLE_LOGGING == 1 | |
405 LOG(ERROR) << "Cannot fork a child process"; | |
406 #endif | |
407 | |
408 throw OrthancException(ErrorCode_SystemCommand); | |
409 } | |
410 else if (pid == 0) | |
411 { | |
412 // Execute the system command in the child process | |
413 execvp(command.c_str(), &args[0]); | |
414 | |
415 // We should never get here | |
416 _exit(1); | |
417 } | |
418 else | |
419 { | |
420 // Wait for the system command to exit | |
421 waitpid(pid, &status, 0); | |
422 } | |
423 #endif | |
424 | |
425 if (status != 0) | |
426 { | |
427 #if ORTHANC_ENABLE_LOGGING == 1 | |
428 LOG(ERROR) << "System command failed with status code " << status; | |
429 #endif | |
430 | |
431 throw OrthancException(ErrorCode_SystemCommand); | |
432 } | |
433 } | |
434 | |
435 | |
436 int SystemToolbox::GetProcessId() | |
437 { | |
438 #if defined(_WIN32) | |
439 return static_cast<int>(_getpid()); | |
440 #else | |
441 return static_cast<int>(getpid()); | |
442 #endif | |
443 } | |
444 | |
445 | |
446 bool SystemToolbox::IsRegularFile(const std::string& path) | |
447 { | |
448 namespace fs = boost::filesystem; | |
449 | |
450 try | |
451 { | |
452 if (fs::exists(path)) | |
453 { | |
454 fs::file_status status = fs::status(path); | |
455 return (status.type() == boost::filesystem::regular_file || | |
456 status.type() == boost::filesystem::reparse_file); // Fix BitBucket issue #11 | |
457 } | |
458 } | |
459 catch (fs::filesystem_error&) | |
460 { | |
461 } | |
462 | |
463 return false; | |
464 } | |
465 | |
466 | |
467 FILE* SystemToolbox::OpenFile(const std::string& path, | |
468 FileMode mode) | |
469 { | |
470 #if defined(_WIN32) | |
471 // TODO Deal with special characters by converting to the current locale | |
472 #endif | |
473 | |
474 const char* m; | |
475 switch (mode) | |
476 { | |
477 case FileMode_ReadBinary: | |
478 m = "rb"; | |
479 break; | |
480 | |
481 case FileMode_WriteBinary: | |
482 m = "wb"; | |
483 break; | |
484 | |
485 default: | |
486 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
487 } | |
488 | |
489 return fopen(path.c_str(), m); | |
490 } | |
491 | |
492 | |
22
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
493 std::string SystemToolbox::GenerateUuid() |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
494 { |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
495 #ifdef WIN32 |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
496 UUID uuid; |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
497 UuidCreate ( &uuid ); |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
498 |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
499 unsigned char * str; |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
500 UuidToStringA ( &uuid, &str ); |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
501 |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
502 std::string s( ( char* ) str ); |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
503 |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
504 RpcStringFreeA ( &str ); |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
505 #else |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
506 uuid_t uuid; |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
507 uuid_generate_random ( uuid ); |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
508 char s[37]; |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
509 uuid_unparse ( uuid, s ); |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
510 #endif |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
511 return s; |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
512 } |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
513 |
b01d46e5a2b3
support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
514 |
15 | 515 #if BOOST_HAS_DATE_TIME == 1 |
516 std::string SystemToolbox::GetNowIsoString() | |
517 { | |
518 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); | |
519 return boost::posix_time::to_iso_string(now); | |
520 } | |
521 | |
522 void SystemToolbox::GetNowDicom(std::string& date, | |
523 std::string& time) | |
524 { | |
525 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); | |
526 tm tm = boost::posix_time::to_tm(now); | |
527 | |
528 char s[32]; | |
529 sprintf(s, "%04d%02d%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); | |
530 date.assign(s); | |
531 | |
532 // TODO milliseconds | |
533 sprintf(s, "%02d%02d%02d.%06d", tm.tm_hour, tm.tm_min, tm.tm_sec, 0); | |
534 time.assign(s); | |
535 } | |
536 #endif | |
537 } |