Back to index

nordugrid-arc-nox  1.1.0~rc6
environment.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include <iostream>
00006 #include <sys/types.h>
00007 #include <sys/stat.h>
00008 #include <unistd.h>
00009 #include <arc/ArcLocation.h>
00010 #include <arc/Utils.h>
00011 #include <arc/Thread.h>
00012 #define olog std::cerr
00013 #include "environment.h"
00014 
00015 class prstring {
00016  private:
00017   Glib::Mutex lock_;
00018   std::string val_;
00019  public:
00020   prstring(void);
00021   prstring(const char*);
00022   prstring(const prstring&);
00023   void operator=(const char*);
00024   void operator=(const std::string&);
00025   void operator=(const prstring&);
00026   void operator+=(const char*);
00027   void operator+=(const std::string&);
00028   std::string operator+(const char*) const;
00029   std::string operator+(const std::string&) const;
00030   operator std::string(void) const;
00031   std::string str(void) const;
00032   bool empty() const;
00033 };
00034 
00035 std::string operator+(const char*,const prstring&);
00036 std::string operator+(const std::string&,const prstring&);
00037 
00038 prstring::prstring(void) {
00039 }
00040 
00041 prstring::prstring(const char* val):val_(val) {
00042 }
00043 
00044 prstring::prstring(const prstring& val):val_(val.str()) {
00045 }
00046 
00047 void prstring::operator=(const char* val) {
00048   Glib::Mutex::Lock lock(lock_);
00049   val_=val;
00050 }
00051 
00052 void prstring::operator=(const std::string& val) {
00053   Glib::Mutex::Lock lock(lock_);
00054   val_=val;
00055 }
00056 
00057 void prstring::operator=(const prstring& val) {
00058   if(&val == this) return;
00059   Glib::Mutex::Lock lock(lock_);
00060   val_=val.str();
00061 }
00062 
00063 void prstring::operator+=(const char* val) {
00064   Glib::Mutex::Lock lock(lock_);
00065   val_+=val;
00066 }
00067 
00068 void prstring::operator+=(const std::string& val) {
00069   Glib::Mutex::Lock lock(lock_);
00070   val_+=val;
00071 }
00072 
00073 std::string prstring::operator+(const char* val) const {
00074   const_cast<Glib::Mutex&>(lock_).lock();
00075   std::string r = val_ + val;
00076   const_cast<Glib::Mutex&>(lock_).unlock();
00077   return r;
00078 }
00079 
00080 std::string prstring::operator+(const std::string& val) const {
00081   const_cast<Glib::Mutex&>(lock_).lock();
00082   std::string r = val_ + val;
00083   const_cast<Glib::Mutex&>(lock_).unlock();
00084   return r;
00085 }
00086 
00087 prstring::operator std::string(void) const {
00088   const_cast<Glib::Mutex&>(lock_).lock();
00089   std::string r = val_;
00090   const_cast<Glib::Mutex&>(lock_).unlock();
00091   return r;
00092 }
00093 
00094 bool prstring::empty() const {
00095   const_cast<Glib::Mutex&>(lock_).lock();
00096   bool r = val_.empty();
00097   const_cast<Glib::Mutex&>(lock_).unlock();
00098   return r;
00099 }
00100 
00101 std::string prstring::str(void) const {
00102   return operator std::string();
00103 }
00104 
00105 std::string operator+(const char* val1,const prstring& val2) {
00106   return (val1 + val2.str());
00107 }
00108 
00109 std::string operator+(const std::string& val1,const prstring& val2) {
00110   return (val1 + val2.str());
00111 }
00112 
00113 // Globus installation path - $GLOBUS_LOCATION
00114 static prstring globus_loc_; 
00115 // Various Globus scripts - $GLOBUS_LOCATION/libexec
00116 static prstring globus_scripts_loc_;
00117 // ARC installation path - $ARC_LOCATION, executable path
00118 static prstring nordugrid_loc_;
00119 // ARC system tools
00120 static prstring nordugrid_libexec_loc_;
00121 // ARC libraries and plugins
00122 static prstring nordugrid_lib_loc_;
00123 // ARC administrator tools
00124 static prstring nordugrid_sbin_loc_;
00125 // ARC configuration file
00126 static prstring nordugrid_config_loc_;
00127 // Certificates directory 
00128 static prstring cert_dir_loc_;
00129 // RTE setup scripts
00130 static prstring runtime_config_dir_;
00131 // Email address of person responsible for this ARC installation
00132 static prstring support_mail_address_;
00133 // Global gridmap files with welcomed users' DNs and UNIX names
00134 static prstring globus_gridmap_;
00135 
00136 std::string globus_loc(void) {
00137   return globus_loc_.str();
00138 }
00139 
00140 std::string globus_scripts_loc(void) {
00141   return globus_scripts_loc_.str();
00142 }
00143 
00144 std::string nordugrid_loc(void) {
00145   return nordugrid_loc_.str();
00146 }
00147 
00148 std::string nordugrid_libexec_loc(void) {
00149   return nordugrid_libexec_loc_.str();
00150 }
00151 
00152 std::string nordugrid_lib_loc(void) {
00153   return nordugrid_lib_loc_.str();
00154 }
00155 
00156 std::string nordugrid_sbin_loc(void) {
00157   return nordugrid_sbin_loc_.str();
00158 }
00159 
00160 std::string nordugrid_config_loc(void) {
00161   return nordugrid_config_loc_.str();
00162 }
00163 
00164 std::string cert_dir_loc(void) {
00165   return cert_dir_loc_.str();
00166 }
00167 
00168 void nordugrid_config_loc(const std::string& val) {
00169   nordugrid_config_loc_=val;
00170 }
00171 
00172 std::string runtime_config_dir(void) {
00173   return runtime_config_dir_.str();
00174 }
00175 
00176 void runtime_config_dir(const std::string& val) {
00177   runtime_config_dir_=val;
00178 }
00179 
00180 std::string support_mail_address(void) {
00181   return support_mail_address_.str();
00182 }
00183 
00184 void support_mail_address(const std::string& val) {
00185   support_mail_address_=val;
00186 }
00187 
00188 std::string globus_gridmap(void) {
00189   return globus_gridmap_.str();
00190 }
00191 
00192 
00193 static bool file_exists(const char* name) {
00194   struct stat st;
00195   if(lstat(name,&st) != 0) return false;
00196   if(!S_ISREG(st.st_mode)) return false;
00197   return true;
00198 }
00199 
00200 static bool dir_exists(const char* name) {
00201   struct stat st;
00202   if(lstat(name,&st) != 0) return false;
00203   if(!S_ISDIR(st.st_mode)) return false;
00204   return true;
00205 }
00206 
00207 bool read_env_vars(bool guess) {
00208   if(globus_loc_.empty()) {
00209     globus_loc_=Arc::GetEnv("GLOBUS_LOCATION");
00210     if(globus_loc_.empty()) {
00211       if(!guess) {
00212         olog<<"Warning: GLOBUS_LOCATION environment variable not defined"<<std::endl;
00213         //return false;
00214       }
00215       else {
00216         globus_loc_="/opt/globus";
00217       };
00218     };
00219     Arc::SetEnv("GLOBUS_LOCATION",globus_loc_.str());
00220   };
00221   globus_scripts_loc_=globus_loc_+"/libexec";
00222 
00223   if(nordugrid_loc_.empty()) {
00224     nordugrid_loc_=Arc::GetEnv("ARC_LOCATION");
00225     if(nordugrid_loc_.empty()) {
00226       nordugrid_loc_=Arc::ArcLocation::Get();
00227     };
00228     nordugrid_lib_loc_=nordugrid_loc_+"/"+PKGLIBSUBDIR;
00229     nordugrid_libexec_loc_=nordugrid_loc_+"/"+PKGLIBEXECSUBDIR;
00230   };
00231 
00232   if(nordugrid_config_loc_.empty()) {
00233     std::string tmp = Arc::GetEnv("ARC_CONFIG");
00234     if(tmp.empty()) {
00235       tmp=Arc::GetEnv("NORDUGRID_CONFIG");
00236       if(tmp.empty()) {
00237         tmp="/etc/arc.conf";
00238         nordugrid_config_loc_=tmp;
00239         if(!file_exists(tmp.c_str())) {
00240           olog<<"Central configuration file is missing at guessed location:\n"
00241               <<"  /etc/arc.conf\n"
00242               <<"Use ARC_CONFIG variable for non-standard location"
00243               <<std::endl;
00244           return false;
00245         };
00246       };
00247     };
00248     if(!tmp.empty()) nordugrid_config_loc_=tmp;
00249   };
00250   
00251   if(cert_dir_loc_.empty()) {
00252     cert_dir_loc_=Arc::GetEnv("X509_CERT_DIR");
00253   };
00254 
00255   // Set all environement variables for other tools
00256   Arc::SetEnv("ARC_CONFIG",nordugrid_config_loc_);
00257   if(support_mail_address_.empty()) {
00258     char hn[100];
00259     support_mail_address_="grid.manager@";
00260     if(gethostname(hn,99) == 0) {
00261       support_mail_address_+=hn;
00262     }
00263     else {
00264       support_mail_address_+="localhost";
00265     };
00266   };
00267   std::string tmp=Arc::GetEnv("GRIDMAP");
00268   if(tmp.empty()) { globus_gridmap_="/etc/grid-security/grid-mapfile"; }
00269   else { globus_gridmap_=tmp; };
00270   return true;
00271 }
00272