Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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  namespace Linux {
19 
21 
22  // Open the file that we can get this information from:
23  char fname[1024];
24  ::sprintf( fname, "/proc/%d/cmdline", ::getpid() );
25  FILE* cmdLine = ::fopen( fname, "r" );
26 
27  // The result object:
29 
30  // Read the command line arguments from there:
31  char cmd[1024];
32  if ( cmdLine ) {
33  long len = ::fread( cmd, sizeof( char ), sizeof( cmd ), cmdLine );
34  if ( len > 0 ) {
35  cmd[len] = 0;
36  for ( char* token = cmd; token - cmd < len; token += ::strlen( token ) + 1 ) { result.push_back( token ); }
37  }
38  ::fclose( cmdLine );
39  }
40 
41  return result;
42  }
43 
44  std::string typeinfoName( const char* class_name ) {
45 
46  // Demangle the name:
47  int status;
49  abi::__cxa_demangle( class_name, nullptr, nullptr, &status ), std::free );
50  if ( !realname ) return class_name;
51 
52  // Substitute the full type of std::string with "std::string"
53  static const std::regex cxx11_string{
54  "std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >( (?=>))?"};
55  std::string result = std::regex_replace( realname.get(), cxx11_string, "std::string" );
56 
57  // Substitute ', ' with ','
58  static const std::regex comma_space{", "};
59  result = std::regex_replace( result, comma_space, "," );
60 
61  return result;
62  }
63 
65 
66  static const size_t STRING_SIZE = 512;
68  if ( ::gethostname( hname.data(), STRING_SIZE ) ) { return ""; }
69 
70  // According to the gethostname documentation, if a host name is too long
71  // to fit into the array provided by the user, the call will truncate the
72  // name to fit into the array, without adding a terminating null
73  // character at the end, and will not signal an error to the caller.
74  // While SUSv2 guarantees that "Host names are limited to 255 bytes", and
75  // the limit to host names is in practice 64 characters on Linux, just to
76  // be safe, the last character of the returned array is set to null
77  // forecfully.
78  hname.back() = '\0';
79 
80  return std::string( hname.data() );
81  }
82 
84 
85  struct utsname ut;
86  if ( ::uname( &ut ) ) { return "UNKNOWN Linux"; }
87  return std::string( ut.sysname );
88  }
89 
91 
92  struct utsname ut;
93  if ( ::uname( &ut ) ) { return "UNKNOWN version"; }
94  return std::string( ut.release );
95  }
96 
98 
99  struct utsname ut;
100  if ( ::uname( &ut ) ) { return "UNKNOWN"; }
101  return std::string( ut.machine );
102  }
103 
105 
106  const char* acct = ::getlogin();
107  if ( !acct ) acct = ::getenv( "LOGNAME" );
108  if ( !acct ) acct = ::getenv( "USER" );
109 
110  return ( acct ? acct : "UNKNOWN" );
111  }
112 
113  } // namespace Linux
114 } // namespace System
std::string osVersion()
Get the operating system&#39;s version.
Definition: SystemLinux.cpp:90
std::string accountName()
Get the account name of the current user.
std::string machineType()
Get the runner machine&#39;s type.
Definition: SystemLinux.cpp:97
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:44
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:20
std::string hostName()
Get the system&#39;s host name.
Definition: SystemLinux.cpp:64
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:83