|
Sierra Toolkit
Version of the Day
|
00001 /*------------------------------------------------------------------------*/ 00002 /* Copyright 2010 Sandia Corporation. */ 00003 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */ 00004 /* license for use of this work by or on behalf of the U.S. Government. */ 00005 /* Export of this program may require a license from the */ 00006 /* United States Government. */ 00007 /*------------------------------------------------------------------------*/ 00008 00009 #include <stk_util/environment/platform/OperatingSystem.hpp> 00010 00011 #include <pwd.h> 00012 #include <unistd.h> 00013 00014 #include <iostream> 00015 #include <ostream> 00016 #include <fstream> 00017 #include <sstream> 00018 #include <cstring> 00019 #include <cstdlib> 00020 #include <stdexcept> 00021 #include <iomanip> 00022 #include <algorithm> 00023 #include <locale> 00024 00025 #include <fcntl.h> 00026 00027 #if defined(__GNUC__) 00028 #include <fstream> 00029 #include <malloc.h> 00030 #include <cstdlib> 00031 #include <sys/time.h> 00032 #include <sys/resource.h> 00033 #if __GNUC__ == 3 || __GNUC__ == 4 00034 #include <cxxabi.h> 00035 #endif 00036 00037 #elif defined(__PGI) 00038 #include <fstream> 00039 #include <malloc.h> 00040 #include <cstdlib> 00041 #include <sys/time.h> 00042 #include <sys/resource.h> 00043 00044 #elif defined(__sun) 00045 #include <fstream> 00046 #include <procfs.h> 00047 #include <sys/resource.h> 00048 #include <sys/systeminfo.h> 00049 #include <sys/utsname.h> 00050 #include <sys/time.h> 00051 00052 #elif defined(__SUNPRO_CC) 00053 #include <sys/resource.h> 00054 #include <sys/time.h> 00055 #include <sys/utsname.h> 00056 #include <netdb.h> 00057 #endif 00058 00059 #if defined(__PUMAGON__) 00060 extern "C" { 00061 #include <util.h> 00062 #include <sys/param.h> 00063 } 00064 00065 #elif defined(__sgi) 00066 #include <sys/time.h> 00067 #include <sys/resource.h> 00068 00069 #elif defined(REDS) 00070 #include <sys/param.h> 00071 #include <sys/utsname.h> 00072 #include <sys/time.h> 00073 #include <sys/resource.h> 00074 #include <unistd.h> 00075 #include <catamount/catmalloc.h> 00076 extern void *__heap_start; 00077 extern void *__heap_end; 00078 00079 #elif defined(__JVN) 00080 #include <sys/param.h> 00081 #include <sys/utsname.h> 00082 #include <sys/time.h> 00083 #include <sys/resource.h> 00084 #include <sys/wait.h> 00085 #include <unistd.h> 00086 00087 #else 00088 #include <sys/utsname.h> 00089 #include <sys/time.h> 00090 #include <netdb.h> 00091 #endif 00092 00093 namespace stk { 00094 00095 std::string 00096 hostname() 00097 { 00098 char buf[255]; 00099 ::gethostname(buf, sizeof(buf)); 00100 return std::string(buf); 00101 } 00102 00103 00104 std::string 00105 domainname() 00106 { 00107 #if defined(__PUMAGON__) || defined(REDS) 00108 return std::string(".sandia.gov"); 00109 00110 #elif defined(__sun) 00111 std::string domain("."); 00112 char buf[255]; 00113 00114 ::sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf)); 00115 if (std::strlen(buf)) { 00116 domain += buf; 00117 } 00118 return domain; 00119 00120 #else //#elif defined(__linux) || defined(__sgi) || defined(_AIX) 00121 std::string domain("."); 00122 char buf[255]; 00123 00124 ::getdomainname(buf, sizeof(buf)); 00125 if (::strlen(buf)) { 00126 domain += buf; 00127 } 00128 return domain; 00129 00130 #endif 00131 } 00132 00133 00134 std::string 00135 username() 00136 { 00137 #if defined(REDS) 00138 return "unknown"; 00139 #else 00140 struct passwd *user_info = ::getpwuid(::geteuid()); 00141 00142 return (user_info ? user_info->pw_name : "unknown"); 00143 #endif 00144 } 00145 00146 00147 std::string 00148 hardware() 00149 { 00150 #ifndef __sgi 00151 struct utsname uts_name; 00152 00153 uname(&uts_name); 00154 00155 return uts_name.machine; 00156 #else 00157 std::string s; 00158 return s; 00159 #endif 00160 } 00161 00162 00163 std::string 00164 osname() 00165 { 00166 #ifndef __sgi 00167 struct utsname uts_name; 00168 00169 uname(&uts_name); 00170 00171 return uts_name.sysname; 00172 #else 00173 std::string s; 00174 return s; 00175 #endif 00176 } 00177 00178 00179 std::string 00180 osversion() 00181 { 00182 #ifndef __sgi 00183 struct utsname uts_name; 00184 00185 uname(&uts_name); 00186 00187 return uts_name.release; 00188 #else 00189 std::string s; 00190 return s; 00191 #endif 00192 } 00193 00194 00195 int 00196 pid() 00197 { 00198 return ::getpid(); 00199 } 00200 00201 00202 int 00203 pgrp() 00204 { 00205 #if defined(__PUMAGON__) || defined(REDS) 00206 return 0; 00207 #else 00208 return ::getpgrp(); 00209 #endif 00210 } 00211 00212 00213 bool 00214 path_access( 00215 const std::string & name, 00216 int mode) 00217 00218 { 00219 return !name.empty() && ::access(name.c_str(), mode) == 0; 00220 } 00221 00222 00223 bool 00224 path_exists( 00225 const std::string & name) 00226 { 00227 return path_access(name, F_OK); 00228 } 00229 00230 00231 bool 00232 path_read_access( 00233 const std::string & name) 00234 { 00235 return path_access(name, R_OK); 00236 } 00237 00238 00239 bool 00240 path_write_access( 00241 const std::string & name) 00242 { 00243 return path_access(name, W_OK); 00244 } 00245 00246 00247 namespace { 00248 00249 struct flock * 00250 file_lock( 00251 short type, 00252 short whence) 00253 { 00254 // /* %TRACE[SPEC]% */ Tracespec trace__("sierra::Fmwk::<empty>::file_lock( short type, short whence)"); /* %TRACE% */ 00255 static struct flock ret; 00256 ret.l_type = type; 00257 ret.l_start = 0; 00258 ret.l_whence = whence; 00259 ret.l_len = 0; 00260 ret.l_pid = 0; //getpid(); 00261 return &ret; 00262 } 00263 00264 } // namespace <empty> 00265 00266 bool 00267 write_lock( 00268 int fd) 00269 { 00270 int i =::fcntl(fd, F_SETLK, file_lock(F_WRLCK, SEEK_SET)); 00271 // if (i == -1) 00272 // fmwkout << "Write lock failed " << errno << dendl; 00273 00274 return i != -1; 00275 } 00276 00277 00278 bool 00279 release_lock( 00280 int fd) 00281 { 00282 int i =::fcntl(fd, F_SETLK, file_lock(F_UNLCK, SEEK_SET)); 00283 // if (i == -1) 00284 // fmwkout << "Release lock failed " << errno << dendl; 00285 00286 return i != -1; 00287 } 00288 00289 00290 bool 00291 read_lock( 00292 int fd) 00293 { 00294 return ::fcntl(fd, F_SETLK, file_lock(F_RDLCK, SEEK_SET)) != -1; 00295 } 00296 00297 00298 bool 00299 append_lock( 00300 int fd) 00301 { 00302 return ::fcntl(fd, F_SETLK, file_lock(F_WRLCK, SEEK_END)) != -1; 00303 } 00304 00305 } // namespace stk