Back to index

nordugrid-arc-nox  1.1.0~rc6
Utils.cpp
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #ifndef WIN32
00008 #include <signal.h>
00009 #endif
00010 
00011 #ifdef HAVE_GLIBMM_GETENV
00012 #include <glibmm/miscutils.h>
00013 #else
00014 #include <stdlib.h>
00015 #endif
00016 
00017 #ifdef HAVE_GLIBMM_SETENV
00018 #include <glibmm/miscutils.h>
00019 #else
00020 #include <stdlib.h>
00021 #endif
00022 
00023 #ifdef HAVE_GLIBMM_UNSETENV
00024 #include <glibmm/miscutils.h>
00025 #else
00026 #include <stdlib.h>
00027 #endif
00028 
00029 #include <glibmm/module.h>
00030 #include <glibmm/fileutils.h>
00031 
00032 #include <string.h>
00033 
00034 #include <arc/ArcLocation.h>
00035 #include <arc/StringConv.h>
00036 #include <arc/Thread.h>
00037 
00038 #ifndef BUFLEN
00039 #define BUFLEN 1024
00040 #endif
00041 
00042 namespace Arc {
00043 
00044 #ifndef WIN32
00045   class SIGPIPEIngore {
00046   private:
00047     void (*sighandler_)(int);
00048   public:
00049     SIGPIPEIngore(void);
00050     ~SIGPIPEIngore(void);
00051   };
00052 
00053   SIGPIPEIngore::SIGPIPEIngore(void) {
00054     sighandler_ = signal(SIGPIPE,SIG_IGN);
00055   }
00056 
00057   SIGPIPEIngore::~SIGPIPEIngore(void) {
00058     signal(SIGPIPE,sighandler_);
00059   }
00060 
00061   static SIGPIPEIngore sigpipe_ignore;
00062 #endif
00063 
00064   std::string GetEnv(const std::string& var) {
00065 #ifdef HAVE_GLIBMM_GETENV
00066     return Glib::getenv(var);
00067 #else
00068     char val* = getenv(var.c_str());
00069     return val ? val : "";
00070 #endif
00071   }
00072 
00073   std::string GetEnv(const std::string& var, bool &found) {
00074 #ifdef HAVE_GLIBMM_GETENV
00075     return Glib::getenv(var, found);
00076 #else
00077     char val* = getenv(var.c_str());
00078     found = (val != NULL)
00079     return val ? val : "";
00080 #endif
00081   }
00082 
00083   bool SetEnv(const std::string& var, const std::string& value, bool overwrite) {
00084 #ifdef HAVE_GLIBMM_SETENV
00085     return Glib::setenv(var, value, overwrite);
00086 #else
00087 #ifdef HAVE_SETENV
00088     return (setenv(var.c_str(), value.c_str(), overwrite) == 0);
00089 #else
00090     return (putenv(strdup((var + "=" + value).c_str())) == 0);
00091 #endif
00092 #endif
00093   }
00094 
00095   void UnsetEnv(const std::string& var) {
00096 #ifdef HAVE_GLIBMM_UNSETENV
00097     Glib::unsetenv(var);
00098 #else
00099 #ifdef HAVE_UNSETENV
00100     unsetenv(var.c_str());
00101 #else
00102     putenv(strdup(var.c_str()));
00103 #endif
00104 #endif
00105   }
00106 
00107   std::string StrError(int errnum) {
00108 #ifdef HAVE_STRERROR_R
00109     char errbuf[BUFLEN];
00110 #ifdef STRERROR_R_CHAR_P
00111     return strerror_r(errnum, errbuf, sizeof(errbuf));
00112 #else
00113     if (strerror_r(errnum, errbuf, sizeof(errbuf)) == 0)
00114       return errbuf;
00115     else
00116       return "Unknown error " + tostring(errnum);
00117 #endif
00118 #else
00119     return strerror(errnum);
00120 #endif
00121   }
00122   
00123   static Glib::Mutex persistent_libraries_lock;
00124   static std::list<std::string> persistent_libraries_list;
00125 
00126   bool PersistentLibraryInit(const std::string& name) {
00127     // Library is made persistent by loading intermediate
00128     // module which depends on that library. So passed name
00129     // is name of that module. Modules usually reside in 
00130     // ARC_LOCATION/lib/arc. This approach is needed because
00131     // on some platforms shared libraries can't be dlopen'ed.
00132     std::string arc_lib_path = ArcLocation::Get();
00133     if(!arc_lib_path.empty()) 
00134       arc_lib_path = arc_lib_path + G_DIR_SEPARATOR_S + PKGLIBSUBDIR;
00135     std::string libpath = Glib::build_filename(arc_lib_path,"lib"+name+"."+G_MODULE_SUFFIX);
00136 
00137     persistent_libraries_lock.lock();
00138     for(std::list<std::string>::iterator l = persistent_libraries_list.begin();
00139             l != persistent_libraries_list.end();++l) {
00140       if(*l == libpath) {
00141         persistent_libraries_lock.unlock();
00142         return true;
00143       };
00144     };
00145     persistent_libraries_lock.unlock();
00146     Glib::Module *module = new Glib::Module(libpath);
00147     if(module && (*module)) {
00148       persistent_libraries_lock.lock();
00149       persistent_libraries_list.push_back(libpath);
00150       persistent_libraries_lock.unlock();
00151       return true;
00152     };
00153     if(module) delete module;
00154     return false;
00155   }
00156 
00157 } // namespace Arc