The Gaudi Framework  v31r0 (aeb156f0)
System::Linux Namespace Reference

Namespace holding Linux specific functions. More...

Functions

std::vector< std::stringcmdLineArgs ()
 Get the command line arguments of the process. More...
 
std::string typeinfoName (const char *name)
 Get the human readable type name from a typeinfo name. More...
 
std::string hostName ()
 Get the system's host name. More...
 
std::string osName ()
 Get the operating system's name. More...
 
std::string osVersion ()
 Get the operating system's version. More...
 
std::string machineType ()
 Get the runner machine's type. More...
 
std::string accountName ()
 Get the account name of the current user. More...
 

Detailed Description

Namespace holding Linux specific functions.

Function Documentation

std::string System::Linux::accountName ( )

Get the account name of the current user.

User login name.

Definition at line 104 of file SystemLinux.cpp.

104  {
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  }
std::vector< std::string > System::Linux::cmdLineArgs ( )

Get the command line arguments of the process.

Command line arguments including executable name as arg[0] as vector of strings.

Definition at line 20 of file SystemLinux.cpp.

20  {
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  }
T push_back(T...args)
std::string System::Linux::hostName ( )

Get the system's host name.

Host name.

Definition at line 64 of file SystemLinux.cpp.

64  {
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  }
STL class.
T data(T...args)
T back(T...args)
STL class.
std::string System::Linux::machineType ( )

Get the runner machine's type.

Machine type.

Definition at line 97 of file SystemLinux.cpp.

97  {
98 
99  struct utsname ut;
100  if ( ::uname( &ut ) ) { return "UNKNOWN"; }
101  return std::string( ut.machine );
102  }
STL class.
std::string System::Linux::osName ( )

Get the operating system's name.

OS name.

Definition at line 83 of file SystemLinux.cpp.

83  {
84 
85  struct utsname ut;
86  if ( ::uname( &ut ) ) { return "UNKNOWN Linux"; }
87  return std::string( ut.sysname );
88  }
STL class.
std::string System::Linux::osVersion ( )

Get the operating system's version.

OS version.

Definition at line 90 of file SystemLinux.cpp.

90  {
91 
92  struct utsname ut;
93  if ( ::uname( &ut ) ) { return "UNKNOWN version"; }
94  return std::string( ut.release );
95  }
STL class.
std::string System::Linux::typeinfoName ( const char *  class_name)

Get the human readable type name from a typeinfo name.

Definition at line 44 of file SystemLinux.cpp.

44  {
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  }
T free(T...args)
STL class.
T regex_replace(T...args)
STL class.