BioDynaMo  v1.03.58-27764645
proc.cc
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #include "core/util/proc.h"
16 #include <unistd.h>
17 #include <experimental/filesystem>
18 #include "core/util/log.h"
19 
20 namespace fs = std::experimental::filesystem;
21 
22 #ifdef LINUX
23 
24 namespace bdm {
25 
26 std::string GetExecutablePath() {
27  char buffer[1024];
28  int path_size = readlink("/proc/self/exe", buffer, sizeof(buffer));
29  if (path_size <= 0) {
30  Log::Fatal("GetExecutablePath",
31  "readlink(\"/proc/self/exe\", ...) failed");
32  }
33  // return value of readlink is not null terminated
34  buffer[path_size] = '\0';
35  return std::string(buffer);
36 }
37 
38 } // namespace bdm
39 
40 #else // APPLE
41 
42 #include <libproc.h>
43 #include <cstdlib>
44 
45 namespace bdm {
46 
47 std::string GetExecutablePath() {
48  pid_t pid = getpid();
49  char buffer[1024];
50  int path_size = proc_pidpath(pid, buffer, sizeof(buffer));
51  if (path_size <= 0) {
52  Log::Fatal("GetExecutablePath",
53  "readlink(\"/proc/self/exe\", ...) failed");
54  }
55  return std::string(buffer);
56 }
57 
58 } // namespace bdm
59 
60 #endif // LINUX
61 
62 namespace bdm {
63 
64 std::string GetExecutableDirectory() {
65  fs::path bin_path = GetExecutablePath();
66  return bin_path.remove_filename().string();
67 }
68 
69 std::string GetExecutableName() {
70  fs::path bin_path = GetExecutablePath();
71  return bin_path.filename().string();
72 }
73 
74 } // namespace bdm
bdm
Definition: agent.cc:39
bdm::GetExecutablePath
std::string GetExecutablePath()
This function returns the path of the currently running executable.
Definition: proc.cc:47
bdm::GetExecutableName
std::string GetExecutableName()
Definition: proc.cc:69
bdm::Log::Fatal
static void Fatal(const std::string &location, const Args &... parts)
Prints fatal error message.
Definition: log.h:115
log.h
proc.h
bdm::GetExecutableDirectory
std::string GetExecutableDirectory()
Definition: proc.cc:64