|
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 00077 #elif defined(__JVN) 00078 #include <sys/param.h> 00079 #include <sys/utsname.h> 00080 #include <sys/time.h> 00081 #include <sys/resource.h> 00082 #include <sys/wait.h> 00083 #include <unistd.h> 00084 00085 #else 00086 #include <sys/utsname.h> 00087 #include <sys/time.h> 00088 #include <netdb.h> 00089 #endif 00090 00091 namespace stk { 00092 bool 00093 path_access( 00094 const std::string & name, 00095 int mode) 00096 00097 { 00098 return !name.empty() && ::access(name.c_str(), mode) == 0; 00099 } 00100 00101 00102 bool 00103 path_exists( 00104 const std::string & name) 00105 { 00106 return path_access(name, F_OK); 00107 } 00108 00109 00110 bool 00111 path_read_access( 00112 const std::string & name) 00113 { 00114 return path_access(name, R_OK); 00115 } 00116 00117 00118 bool 00119 path_write_access( 00120 const std::string & name) 00121 { 00122 return path_access(name, W_OK); 00123 } 00124 00125 00126 namespace { 00127 00128 struct flock * 00129 file_lock( 00130 short type, 00131 short whence) 00132 { 00133 // /* %TRACE[SPEC]% */ Tracespec trace__("sierra::Fmwk::<empty>::file_lock( short type, short whence)"); /* %TRACE% */ 00134 static struct flock ret; 00135 ret.l_type = type; 00136 ret.l_start = 0; 00137 ret.l_whence = whence; 00138 ret.l_len = 0; 00139 ret.l_pid = 0; //getpid(); 00140 return &ret; 00141 } 00142 00143 } // namespace <empty> 00144 00145 bool 00146 write_lock( 00147 int fd) 00148 { 00149 int i =::fcntl(fd, F_SETLK, file_lock(F_WRLCK, SEEK_SET)); 00150 // if (i == -1) 00151 // fmwkout << "Write lock failed " << errno << dendl; 00152 00153 return i != -1; 00154 } 00155 00156 00157 bool 00158 release_lock( 00159 int fd) 00160 { 00161 int i =::fcntl(fd, F_SETLK, file_lock(F_UNLCK, SEEK_SET)); 00162 // if (i == -1) 00163 // fmwkout << "Release lock failed " << errno << dendl; 00164 00165 return i != -1; 00166 } 00167 00168 00169 bool 00170 read_lock( 00171 int fd) 00172 { 00173 return ::fcntl(fd, F_SETLK, file_lock(F_RDLCK, SEEK_SET)) != -1; 00174 } 00175 00176 00177 bool 00178 append_lock( 00179 int fd) 00180 { 00181 return ::fcntl(fd, F_SETLK, file_lock(F_WRLCK, SEEK_END)) != -1; 00182 } 00183 00184 } // namespace stk