Cat
System.cpp
Go to the documentation of this file.
1 // $Id: System.cpp,v 1.20.2.1 2006/10/06 11:28:02 hmd Exp $
2 //====================================================================
3 // System.cpp
4 //--------------------------------------------------------------------
5 //
6 // Package : System (The LHCb System service)
7 //
8 // Description: Implementation of Systems internals
9 //
10 // Author : M.Frank
11 // Created : 13/1/99
12 // Changes :
13 //====================================================================
14 #define SYSTEM_CPP
15 #include <ctime>
16 #include <cstring>
17 #include <cstdlib>
18 #include <iostream>
19 #include <typeinfo>
20 
21 #include "System.h"
22  const char* SHLIB_SUFFIX = ".so";
23  #include <errno.h>
24  #include <string.h>
25  #include "sys/times.h"
26  #include "unistd.h"
27  #include "libgen.h"
28  #include <cstdio>
29  #include <cxxabi.h>
30  #include "dlfcn.h"
31  #include <sys/utsname.h>
32  #include <unistd.h>
33  extern char** environ;
34 
35 static std::vector<std::string> s_argvStrings;
36 static std::vector<const char*> s_argvChars;
37 
39 const std::string& System::hostName() {
40  static std::string host = "";
41  if ( host == "" ) {
42  char buffer[512];
43  memset(buffer,0,sizeof(buffer));
44  ::gethostname(buffer, sizeof(buffer));
45  host = buffer;
46  }
47  return host;
48 }
49 
51 const std::string& System::osName() {
52  static std::string osname = "";
53  struct utsname ut;
54  if (uname(&ut) == 0) {
55  osname = ut.sysname;
56  } else {
57  osname = "UNKNOWN";
58  }
59  return osname;
60 }
61 
62 
64 const std::string& System::osVersion() {
65  static std::string osver = "";
66  struct utsname ut;
67  if (uname(&ut) == 0) {
68  osver = ut.release;
69  } else {
70  osver = "UNKNOWN";
71  }
72  return osver;
73 }
74 
76 const std::string& System::machineType() {
77  static std::string mach = "";
78  struct utsname ut;
79  if (uname(&ut) == 0) {
80  mach = ut.machine;
81  } else {
82  mach = "UNKNOWN";
83  }
84  return mach;
85 }
86 
88 const std::string& System::accountName() {
89  static std::string account = "";
90  if ( account == "" ) {
91  const char* acct = ::getlogin();
92  if ( 0 == acct ) acct = ::getenv("LOGNAME");
93  if ( 0 == acct ) acct = ::getenv("USER");
94  account = (acct) ? acct : "Unknown";
95  }
96  return account;
97 }
98 
101  return cmdLineArgs().size();
102 }
103 
105 long System::argc() {
106  return cmdLineArgs().size();
107 }
108 
110 const std::vector<std::string> System::cmdLineArgs() {
111  if ( s_argvChars.size() == 0 ) {
112  char exe[1024];
113  sprintf(exe, "/proc/%d/cmdline", ::getpid());
114  FILE *cmdLine = ::fopen(exe,"r");
115  char cmd[1024];
116  if ( cmdLine ) {
117  long len = fread(cmd, sizeof(char), sizeof(cmd), cmdLine);
118  if ( len > 0 ) {
119  cmd[len] = 0;
120  for ( char* token = cmd; token-cmd < len; token += strlen(token)+1 ) {
121  s_argvStrings.push_back(token);
122  s_argvChars.push_back( s_argvStrings.back().c_str());
123  }
124  s_argvStrings[0] = exeName();
125  s_argvChars[0] = s_argvStrings[0].c_str();
126  }
127  }
128  ::fclose(cmdLine);
129  }
130  return s_argvStrings;
131 }
132 
134 char** System::argv() {
136  if( s_argvChars.empty() ) { cmdLineArgs(); }
137  // We rely here on the fact that a vector's allocation table is contiguous
139  return (char**)&s_argvChars[0];
140 }
141 
143 const std::string System::getEnv(const char* var) {
144  char* env;
145  if ( (env = getenv(var)) != 0 ) {
146  return env;
147  } else {
148  return "UNKNOWN";
149  }
150 }
151 
153 const std::vector<std::string> System::getEnv() {
154  std::vector<std::string> vars;
155  for (int i=0; environ[i] != 0; ++i) {
156  vars.push_back(environ[i]);
157  }
158  return vars;
159 }
160 
162 int System::setEnv(const std::string &name, const std::string &value, int overwrite)
163 {
164  // UNIX version
165  return value.empty() ?
166  // remove if set to nothing (and return success)
167  ::unsetenv(name.c_str()) , 0 :
168  // set the value
169  ::setenv(name.c_str(),value.c_str(), overwrite);
170 }
171 
Definition: var.h:19
char ** environ
const std::string & machineType()
Machine type.
Definition: System.cpp:76
const std::vector< std::string > cmdLineArgs()
Command line arguments including executable name as arg[0] as vector of strings.
Definition: System.cpp:110
const std::string & osName()
OS name.
Definition: System.cpp:51
char ** argv()
char** command line arguments including executable name as arg[0]; You may not modify them! ...
Definition: System.cpp:134
const std::string & osVersion()
OS version.
Definition: System.cpp:64
static std::vector< const char * > s_argvChars
Definition: System.cpp:36
int setEnv(const std::string &name, const std::string &value, int overwrite=1)
set an environment variables.
Definition: System.cpp:162
const char * SHLIB_SUFFIX
Definition: System.cpp:22
long numCmdLineArgs()
Number of arguments passed to the commandline.
Definition: System.cpp:100
std::vector< var > vars
Definition: var.h:73
const std::string & exeName()
Name of the executable file running.
Definition: ModuleInfo.cpp:187
long argc()
Number of arguments passed to the commandline (==numCmdLineArgs()); just to match argv call...
Definition: System.cpp:105
static std::vector< std::string > s_argvStrings
Definition: System.cpp:35
const std::string & accountName()
User login name.
Definition: System.cpp:88
const std::string getEnv(const char *var)
get a particular environment variable
Definition: System.cpp:143
const std::string & hostName()
Host name.
Definition: System.cpp:39