The Gaudi Framework  master (37c0b60a)
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

◆ accountName()

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

Get the account name of the current user.

Definition at line 114 of file SystemLinux.cpp.

114  {
115 
116  const char* acct = ::getlogin();
117  if ( !acct ) acct = ::getenv( "LOGNAME" );
118  if ( !acct ) acct = ::getenv( "USER" );
119 
120  return ( acct ? acct : "UNKNOWN" );
121  }

◆ cmdLineArgs()

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

Get the command line arguments of the process.

Definition at line 30 of file SystemLinux.cpp.

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

◆ hostName()

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

Get the system's host name.

Definition at line 74 of file SystemLinux.cpp.

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

◆ machineType()

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

Get the runner machine's type.

Definition at line 107 of file SystemLinux.cpp.

107  {
108 
109  struct utsname ut;
110  if ( ::uname( &ut ) ) { return "UNKNOWN"; }
111  return std::string( ut.machine );
112  }

◆ osName()

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

Get the operating system's name.

Definition at line 93 of file SystemLinux.cpp.

93  {
94 
95  struct utsname ut;
96  if ( ::uname( &ut ) ) { return "UNKNOWN Linux"; }
97  return std::string( ut.sysname );
98  }

◆ osVersion()

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

Get the operating system's version.

Definition at line 100 of file SystemLinux.cpp.

100  {
101 
102  struct utsname ut;
103  if ( ::uname( &ut ) ) { return "UNKNOWN version"; }
104  return std::string( ut.release );
105  }

◆ typeinfoName()

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

Get the human readable type name from a typeinfo name.

Definition at line 54 of file SystemLinux.cpp.

54  {
55 
56  // Demangle the name:
57  int status;
59  abi::__cxa_demangle( class_name, nullptr, nullptr, &status ), std::free );
60  if ( !realname ) return class_name;
61 
62  // Substitute the full type of std::string with "std::string"
63  static const std::regex cxx11_string{
64  "std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >( (?=>))?" };
65  std::string result = std::regex_replace( realname.get(), cxx11_string, "std::string" );
66 
67  // Substitute ', ' with ','
68  static const std::regex comma_space{ ", " };
69  result = std::regex_replace( result, comma_space, "," );
70 
71  return result;
72  }
std::string
STL class.
std::vector< std::string >
std::array::back
T back(T... args)
std::vector::push_back
T push_back(T... args)
std::array
STL class.
std::regex
std::free
T free(T... args)
std::unique_ptr
STL class.
std::regex_replace
T regex_replace(T... args)
std::array::data
T data(T... args)