The Gaudi Framework  master (37c0b60a)
SystemLinux.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 
12 // System include(s):
13 #include <array>
14 #include <cstdio>
15 #include <cstdlib>
16 #include <cstring>
17 #include <cxxabi.h>
18 #include <memory>
19 #include <regex>
20 #include <sys/types.h>
21 #include <sys/utsname.h>
22 #include <unistd.h>
23 
24 // Local include(s):
25 #include "SystemLinux.h"
26 
27 namespace System {
28  namespace Linux {
29 
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  }
53 
54  std::string typeinfoName( const char* class_name ) {
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  }
73 
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  }
92 
94 
95  struct utsname ut;
96  if ( ::uname( &ut ) ) { return "UNKNOWN Linux"; }
97  return std::string( ut.sysname );
98  }
99 
101 
102  struct utsname ut;
103  if ( ::uname( &ut ) ) { return "UNKNOWN version"; }
104  return std::string( ut.release );
105  }
106 
108 
109  struct utsname ut;
110  if ( ::uname( &ut ) ) { return "UNKNOWN"; }
111  return std::string( ut.machine );
112  }
113 
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  }
122 
123  } // namespace Linux
124 } // namespace System
std::string
STL class.
std::vector< std::string >
System::Linux::accountName
std::string accountName()
Get the account name of the current user.
Definition: SystemLinux.cpp:114
std::array::back
T back(T... args)
System::Linux::osName
std::string osName()
Get the operating system's name.
Definition: SystemLinux.cpp:93
System::Linux::machineType
std::string machineType()
Get the runner machine's type.
Definition: SystemLinux.cpp:107
System::Linux::hostName
std::string hostName()
Get the system's host name.
Definition: SystemLinux.cpp:74
std::vector::push_back
T push_back(T... args)
SystemLinux.h
std::array
STL class.
System::Linux::cmdLineArgs
std::vector< std::string > cmdLineArgs()
Get the command line arguments of the process.
Definition: SystemLinux.cpp:30
std::regex
System
Note: OS specific details for environment resolution.
Definition: Debugger.h:29
std::free
T free(T... args)
System::Linux::osVersion
std::string osVersion()
Get the operating system's version.
Definition: SystemLinux.cpp:100
std::unique_ptr
STL class.
std::regex_replace
T regex_replace(T... args)
std::array::data
T data(T... args)
System::Linux::typeinfoName
std::string typeinfoName(const char *class_name)
Get the human readable type name from a typeinfo name.
Definition: SystemLinux.cpp:54