BioDynaMo  v1.05.119-a4ff3934
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 <filesystem>
18 #include "core/util/log.h"
19 
20 #ifdef LINUX
21 
22 namespace bdm {
23 
24 std::string GetExecutablePath() {
25  char buffer[1024];
26  int path_size = readlink("/proc/self/exe", buffer, sizeof(buffer));
27  if (path_size <= 0) {
28  Log::Fatal("GetExecutablePath",
29  "readlink(\"/proc/self/exe\", ...) failed");
30  }
31  // return value of readlink is not null terminated
32  buffer[path_size] = '\0';
33  return std::string(buffer);
34 }
35 
36 } // namespace bdm
37 
38 #else // APPLE
39 
40 #include <libproc.h>
41 #include <cstdlib>
42 
43 namespace bdm {
44 
45 std::string GetExecutablePath() {
46  pid_t pid = getpid();
47  char buffer[1024];
48  int path_size = proc_pidpath(pid, buffer, sizeof(buffer));
49  if (path_size <= 0) {
50  Log::Fatal("GetExecutablePath",
51  "readlink(\"/proc/self/exe\", ...) failed");
52  }
53  return std::string(buffer);
54 }
55 
56 } // namespace bdm
57 
58 #endif // LINUX
59 
60 namespace bdm {
61 
62 std::string GetExecutableDirectory() {
63  std::filesystem::path bin_path = GetExecutablePath();
64  return bin_path.remove_filename().string();
65 }
66 
67 std::string GetExecutableName() {
68  std::filesystem::path bin_path = GetExecutablePath();
69  return bin_path.filename().string();
70 }
71 
72 } // 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:45
bdm::GetExecutableName
std::string GetExecutableName()
Definition: proc.cc:67
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:62