The Gaudi Framework  v30r3 (a5ef0a68)
SystemLinux.cpp
Go to the documentation of this file.
1 
2 // System include(s):
3 #include <array>
4 #include <cstdio>
5 #include <cstdlib>
6 #include <cstring>
7 #include <cxxabi.h>
8 #include <memory>
9 #include <regex>
10 #include <sys/types.h>
11 #include <sys/utsname.h>
12 #include <unistd.h>
13 
14 // Local include(s):
15 #include "SystemLinux.h"
16 
17 namespace System
18 {
19  namespace Linux
20  {
21 
23  {
24 
25  // Open the file that we can get this information from:
26  char fname[1024];
27  ::sprintf( fname, "/proc/%d/cmdline", ::getpid() );
28  FILE* cmdLine = ::fopen( fname, "r" );
29 
30  // The result object:
32 
33  // Read the command line arguments from there:
34  char cmd[1024];
35  if ( cmdLine ) {
36  long len = ::fread( cmd, sizeof( char ), sizeof( cmd ), cmdLine );
37  if ( len > 0 ) {
38  cmd[len] = 0;
39  for ( char* token = cmd; token - cmd < len; token += ::strlen( token ) + 1 ) {
40  result.push_back( token );
41  }
42  }
43  ::fclose( cmdLine );
44  }
45 
46  return result;
47  }
48 
49  std::string typeinfoName( const char* class_name )
50  {
51 
52  // Demangle the name:
53  int status;
55  abi::__cxa_demangle( class_name, nullptr, nullptr, &status ), std::free );
56  if ( !realname ) return class_name;
57 
58  // Substitute the full type of std::string with "std::string"
59  static const std::regex cxx11_string{
60  "std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >( (?=>))?"};
61  std::string result = std::regex_replace( realname.get(), cxx11_string, "std::string" );
62 
63  // Substitute ', ' with ','
64  static const std::regex comma_space{", "};
65  result = std::regex_replace( result, comma_space, "," );
66 
67  return result;
68  }
69 
71  {
72 
73  static const size_t STRING_SIZE = 512;
75  if (::gethostname( hname.data(), STRING_SIZE ) ) {
76  return "";
77  }
78 
79  // According to the gethostname documentation, if a host name is too long
80  // to fit into the array provided by the user, the call will truncate the
81  // name to fit into the array, without adding a terminating null
82  // character at the end, and will not signal an error to the caller.
83  // While SUSv2 guarantees that "Host names are limited to 255 bytes", and
84  // the limit to host names is in practice 64 characters on Linux, just to
85  // be safe, the last character of the returned array is set to null
86  // forecfully.
87  hname.back() = '\0';
88 
89  return std::string( hname.data() );
90  }
91 
93  {
94 
95  struct utsname ut;
96  if (::uname( &ut ) ) {
97  return "UNKNOWN Linux";
98  }
99  return std::string( ut.sysname );
100  }
101 
103  {
104 
105  struct utsname ut;
106  if (::uname( &ut ) ) {
107  return "UNKNOWN version";
108  }
109  return std::string( ut.release );
110  }
111 
113  {
114 
115  struct utsname ut;
116  if (::uname( &ut ) ) {
117  return "UNKNOWN";
118  }
119  return std::string( ut.machine );
120  }
121 
123  {
124 
125  const char* acct = ::getlogin();
126  if ( !acct ) acct = ::getenv( "LOGNAME" );
127  if ( !acct ) acct = ::getenv( "USER" );
128 
129  return ( acct ? acct : "UNKNOWN" );
130  }
131 
132  } // namespace Linux
133 } // namespace System
std::string osVersion()
Get the operating system&#39;s version.
std::string accountName()
Get the account name of the current user.
std::string machineType()
Get the runner machine&#39;s type.
Note: OS specific details for environment resolution.
Definition: Debugger.h:19
T free(T...args)
STL class.
std::string typeinfoName(const char *class_name)
Get the human readable type name from a typeinfo name.
Definition: SystemLinux.cpp:49
T push_back(T...args)
T data(T...args)
std::vector< std::string > cmdLineArgs()
Get the command line arguments of the process.
Definition: SystemLinux.cpp:22
std::string hostName()
Get the system&#39;s host name.
Definition: SystemLinux.cpp:70
T regex_replace(T...args)
STL class.
T back(T...args)
STL class.
std::string osName()
Get the operating system&#39;s name.
Definition: SystemLinux.cpp:92