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