Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
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 * (c) Copyright 1998-2025 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 #include "SystemLinux.h"
12 #include <array>
13 #include <cstdio>
14 #include <cstdlib>
15 #include <cstring>
16 #include <cxxabi.h>
17 #include <memory>
18 #include <regex>
19 #include <sys/types.h>
20 #include <sys/utsname.h>
21 #include <unistd.h>
22 
23 namespace System {
24  namespace Linux {
25 
26  std::vector<std::string> cmdLineArgs() {
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  }
49 
50  std::string typeinfoName( const char* class_name ) {
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  }
69 
70  std::string hostName() {
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  }
88 
89  std::string osName() {
90 
91  struct utsname ut;
92  if ( ::uname( &ut ) ) { return "UNKNOWN Linux"; }
93  return std::string( ut.sysname );
94  }
95 
96  std::string osVersion() {
97 
98  struct utsname ut;
99  if ( ::uname( &ut ) ) { return "UNKNOWN version"; }
100  return std::string( ut.release );
101  }
102 
103  std::string machineType() {
104 
105  struct utsname ut;
106  if ( ::uname( &ut ) ) { return "UNKNOWN"; }
107  return std::string( ut.machine );
108  }
109 
110  std::string accountName() {
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  }
118 
119  } // namespace Linux
120 } // namespace System
System::Linux::accountName
std::string accountName()
Get the account name of the current user.
Definition: SystemLinux.cpp:110
System::Linux::osName
std::string osName()
Get the operating system's name.
Definition: SystemLinux.cpp:89
System::Linux::machineType
std::string machineType()
Get the runner machine's type.
Definition: SystemLinux.cpp:103
System::Linux::hostName
std::string hostName()
Get the system's host name.
Definition: SystemLinux.cpp:70
SystemLinux.h
System::Linux::cmdLineArgs
std::vector< std::string > cmdLineArgs()
Get the command line arguments of the process.
Definition: SystemLinux.cpp:26
System
Note: OS specific details for environment resolution.
Definition: Environment.h:25
System::Linux::osVersion
std::string osVersion()
Get the operating system's version.
Definition: SystemLinux.cpp:96
System::Linux::typeinfoName
std::string typeinfoName(const char *class_name)
Get the human readable type name from a typeinfo name.
Definition: SystemLinux.cpp:50