The Gaudi Framework  master (01b473db)
System::Linux Namespace Reference

Namespace holding Linux specific functions. More...

Functions

std::vector< std::string > cmdLineArgs ()
 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

◆ accountName()

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

Get the account name of the current user.

Definition at line 110 of file SystemLinux.cpp.

110  {
111 
112  const char* acct = ::getlogin();
113  if ( !acct ) acct = ::getenv( "LOGNAME" );
114  if ( !acct ) acct = ::getenv( "USER" );
115 
116  return ( acct ? acct : "UNKNOWN" );
117  }

◆ cmdLineArgs()

std::vector< std::string > System::Linux::cmdLineArgs ( )

Get the command line arguments of the process.

Definition at line 26 of file SystemLinux.cpp.

26  {
27 
28  // Open the file that we can get this information from:
29  char fname[1024];
30  ::sprintf( fname, "/proc/%d/cmdline", ::getpid() );
31  FILE* cmdLine = ::fopen( fname, "r" );
32 
33  // The result object:
34  std::vector<std::string> result;
35 
36  // Read the command line arguments from there:
37  char cmd[1024];
38  if ( cmdLine ) {
39  long len = ::fread( cmd, sizeof( char ), sizeof( cmd ), cmdLine );
40  if ( len > 0 ) {
41  cmd[len] = 0;
42  for ( char* token = cmd; token - cmd < len; token += ::strlen( token ) + 1 ) { result.push_back( token ); }
43  }
44  ::fclose( cmdLine );
45  }
46 
47  return result;
48  }

◆ hostName()

std::string System::Linux::hostName ( )

Get the system's host name.

Definition at line 70 of file SystemLinux.cpp.

70  {
71 
72  static const size_t STRING_SIZE = 512;
73  std::array<char, STRING_SIZE> hname;
74  if ( ::gethostname( hname.data(), STRING_SIZE ) ) { return ""; }
75 
76  // According to the gethostname documentation, if a host name is too long
77  // to fit into the array provided by the user, the call will truncate the
78  // name to fit into the array, without adding a terminating null
79  // character at the end, and will not signal an error to the caller.
80  // While SUSv2 guarantees that "Host names are limited to 255 bytes", and
81  // the limit to host names is in practice 64 characters on Linux, just to
82  // be safe, the last character of the returned array is set to null
83  // forecfully.
84  hname.back() = '\0';
85 
86  return std::string( hname.data() );
87  }

◆ machineType()

std::string System::Linux::machineType ( )

Get the runner machine's type.

Definition at line 103 of file SystemLinux.cpp.

103  {
104 
105  struct utsname ut;
106  if ( ::uname( &ut ) ) { return "UNKNOWN"; }
107  return std::string( ut.machine );
108  }

◆ osName()

std::string System::Linux::osName ( )

Get the operating system's name.

Definition at line 89 of file SystemLinux.cpp.

89  {
90 
91  struct utsname ut;
92  if ( ::uname( &ut ) ) { return "UNKNOWN Linux"; }
93  return std::string( ut.sysname );
94  }

◆ osVersion()

std::string System::Linux::osVersion ( )

Get the operating system's version.

Definition at line 96 of file SystemLinux.cpp.

96  {
97 
98  struct utsname ut;
99  if ( ::uname( &ut ) ) { return "UNKNOWN version"; }
100  return std::string( ut.release );
101  }

◆ typeinfoName()

std::string System::Linux::typeinfoName ( const char *  class_name)

Get the human readable type name from a typeinfo name.

Definition at line 50 of file SystemLinux.cpp.

50  {
51 
52  // Demangle the name:
53  int status;
54  auto realname = std::unique_ptr<char, decltype( free )*>(
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  }