The Gaudi Framework  master (74da3c19)
Loading...
Searching...
No Matches
SystemLinux.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2026 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 "TypeNormalization.h"
13#include <array>
14#include <cstdio>
15#include <cstdlib>
16#include <cstring>
17#include <sys/types.h>
18#include <sys/utsname.h>
19#include <unistd.h>
20
21namespace System {
22 namespace Linux {
23
24 std::vector<std::string> cmdLineArgs() {
25
26 // Open the file that we can get this information from:
27 char fname[1024];
28 ::snprintf( fname, sizeof( fname ), "/proc/%d/cmdline", ::getpid() );
29 FILE* cmdLine = ::fopen( fname, "r" );
30
31 // The result object:
32 std::vector<std::string> result;
33
34 // Read the command line arguments from there:
35 char cmd[1024];
36 if ( cmdLine ) {
37 long len = ::fread( cmd, sizeof( char ), sizeof( cmd ), cmdLine );
38 if ( len > 0 ) {
39 cmd[len] = 0;
40 for ( char* token = cmd; token - cmd < len; token += ::strlen( token ) + 1 ) { result.push_back( token ); }
41 }
42 ::fclose( cmdLine );
43 }
44
45 return result;
46 }
47
48 std::string typeinfoName( const char* class_name ) { return Detail::normalizeTypeName( class_name, true ); }
49
50 std::string hostName() {
51
52 static const size_t STRING_SIZE = 512;
53 std::array<char, STRING_SIZE> hname;
54 if ( ::gethostname( hname.data(), STRING_SIZE ) ) { return ""; }
55
56 // According to the gethostname documentation, if a host name is too long
57 // to fit into the array provided by the user, the call will truncate the
58 // name to fit into the array, without adding a terminating null
59 // character at the end, and will not signal an error to the caller.
60 // While SUSv2 guarantees that "Host names are limited to 255 bytes", and
61 // the limit to host names is in practice 64 characters on Linux, just to
62 // be safe, the last character of the returned array is set to null
63 // forecfully.
64 hname.back() = '\0';
65
66 return std::string( hname.data() );
67 }
68
69 std::string osName() {
70
71 struct utsname ut;
72 if ( ::uname( &ut ) ) { return "UNKNOWN Linux"; }
73 return std::string( ut.sysname );
74 }
75
76 std::string osVersion() {
77
78 struct utsname ut;
79 if ( ::uname( &ut ) ) { return "UNKNOWN version"; }
80 return std::string( ut.release );
81 }
82
83 std::string machineType() {
84
85 struct utsname ut;
86 if ( ::uname( &ut ) ) { return "UNKNOWN"; }
87 return std::string( ut.machine );
88 }
89
90 std::string accountName() {
91
92 const char* acct = ::getlogin();
93 if ( !acct ) acct = ::getenv( "LOGNAME" );
94 if ( !acct ) acct = ::getenv( "USER" );
95
96 return ( acct ? acct : "UNKNOWN" );
97 }
98
99 } // namespace Linux
100} // namespace System
std::string normalizeTypeName(const char *mangled_name, bool normalize_commas=false)
Normalize a demangled C++ type name for cross-platform consistency.
Namespace holding Linux specific functions.
std::string typeinfoName(const char *class_name)
Get the human readable type name from a typeinfo name.
std::string accountName()
Get the account name of the current user.
std::vector< std::string > cmdLineArgs()
Get the command line arguments of the process.
std::string machineType()
Get the runner machine's type.
std::string osVersion()
Get the operating system's version.
std::string osName()
Get the operating system's name.
std::string hostName()
Get the system's host name.
Note: OS specific details for environment resolution.
Definition Environment.h:25