[219] | 1 | // Gestionnaire de lien dynamique - R. Ansari 12/98
|
---|
| 2 | // LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
|
---|
| 3 |
|
---|
[913] | 4 | #include "pdlmgr.h"
|
---|
[219] | 5 | #include <stdio.h>
|
---|
| 6 | #include <stdlib.h>
|
---|
| 7 |
|
---|
| 8 | #include <iostream.h>
|
---|
| 9 |
|
---|
| 10 |
|
---|
| 11 | // Extension de noms de fichiers Shared libs
|
---|
| 12 | static const char* sofext = ".so";
|
---|
| 13 | static const char* sofext_HPUX = ".sl";
|
---|
| 14 |
|
---|
| 15 | // Variables et methodes static
|
---|
| 16 | int PDynLinkMgr::numSO = 0;
|
---|
| 17 | string* PDynLinkMgr::tmpDir = NULL;
|
---|
| 18 |
|
---|
[895] | 19 | /*!
|
---|
| 20 | \class SOPHYA::PDynLinkMgr
|
---|
[913] | 21 | \ingroup SysTools
|
---|
[895] | 22 | This classes handles the run-time operations related to using shared
|
---|
| 23 | libraries. The present version has been adapted for different Unix
|
---|
| 24 | flavours (Linux, Compaq/Digital Unix, SGI IRIX, IBM AIX, Sun Solaris).
|
---|
[913] | 25 | The example here the linking of shared library named "mylib.so"
|
---|
| 26 | containing a function \c double \c myfunction(double x).
|
---|
| 27 | \code
|
---|
| 28 | #include "pdlmgr.h"
|
---|
| 29 | typedef double (* AFunctionOfX) (double x);
|
---|
| 30 | {
|
---|
| 31 | // ...
|
---|
| 32 | string soname = "mylib.so";
|
---|
| 33 | string funcname = "myfunction";
|
---|
| 34 | PDynLinkMgr dyl(son);
|
---|
| 35 | AFunctionOfX f = (AFunctionOfX)dyl.GetFunction(funcname);
|
---|
| 36 | double x = 3.1425;
|
---|
| 37 | if (f != NULL)
|
---|
| 38 | cout << " X= " << x << " myfunction(x)=" << f(x) << endl;
|
---|
| 39 | // ...
|
---|
| 40 | }
|
---|
| 41 | \endcode
|
---|
[895] | 42 | */
|
---|
| 43 |
|
---|
[219] | 44 | /* --Methode-Static-- */
|
---|
[895] | 45 | /*! Sets the path for a temporary space where shared libraries are copied.
|
---|
| 46 | The path is appended to \b LD_LIBRARY_PATH
|
---|
| 47 | */
|
---|
[219] | 48 | void PDynLinkMgr::SetTmpDir(string const & path)
|
---|
| 49 | {
|
---|
| 50 | if ( (path.length() > 0) && (path[path.length()] != '/') ) GetTmpDir() = path + '/';
|
---|
| 51 | else GetTmpDir() = path;
|
---|
[480] | 52 | #if defined(OSF1) || defined(Linux) || defined(SunOS)
|
---|
| 53 | string cmd = "LD_LIBRARY_PATH=";
|
---|
| 54 | char* varenv=NULL;
|
---|
| 55 | varenv=getenv("LD_LIBRARY_PATH");
|
---|
| 56 | #elif defined(IRIX64)
|
---|
| 57 | string cmd = "LD_LIBRARYN32_PATH=";
|
---|
| 58 | char* varenv=NULL;
|
---|
| 59 | varenv=getenv("LD_LIBRARYN32_PATH");
|
---|
| 60 |
|
---|
| 61 | if (varenv == NULL) {
|
---|
| 62 | cmd += '.';
|
---|
| 63 | if (path.length() > 0) cmd += ':' + path;
|
---|
| 64 | }
|
---|
| 65 | else {
|
---|
| 66 | if (varenv[0] != '.') cmd += ".:";
|
---|
| 67 | if (path.length() > 0) cmd += path + ':';
|
---|
| 68 | cmd += varenv;
|
---|
| 69 | putenv(cmd.c_str());
|
---|
| 70 | }
|
---|
| 71 | #elif defined(AIX)
|
---|
| 72 | string cmd = "LIBPATH=";
|
---|
| 73 | char* varenv=NULL;
|
---|
| 74 | varenv=getenv("LIBPATH");
|
---|
| 75 | if (varenv == NULL) {
|
---|
| 76 | cmd += '.';
|
---|
| 77 | if (path.length() > 0) cmd += ':' + path;
|
---|
| 78 | cmd += ":/usr/lib:/lib";
|
---|
| 79 | }
|
---|
| 80 | else {
|
---|
| 81 | if (varenv[0] != '.') cmd += ".:";
|
---|
| 82 | if (path.length() > 0) cmd += path + ':';
|
---|
| 83 | cmd += varenv;
|
---|
| 84 | putenv(const_cast<char *>(cmd.c_str()));
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | #endif
|
---|
[219] | 88 | return;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /* --Methode-Static-- */
|
---|
[895] | 92 | /*! Returns the temporary space path */
|
---|
[219] | 93 | string& PDynLinkMgr::GetTmpDir()
|
---|
| 94 | {
|
---|
| 95 | if (tmpDir == NULL) {
|
---|
| 96 | tmpDir = new string("");
|
---|
| 97 | char* varenv;
|
---|
[895] | 98 | if ( (varenv=getenv("SOPHYA_TMP")) != NULL ) *tmpDir = varenv;
|
---|
[219] | 99 | else if ( (varenv=getenv("TMPDIR")) != NULL ) *tmpDir = varenv;
|
---|
| 100 | }
|
---|
| 101 | return(*tmpDir);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /* --Methode-Static-- */
|
---|
[895] | 105 | /*! Compiles the C source file named \b fname and creates the
|
---|
| 106 | corresponding shared library linking against the standard
|
---|
| 107 | C library (-lc) and the math library (-lm).
|
---|
| 108 | Returns a pointer to the created PDynLinkMgr object (by new).
|
---|
| 109 | Returns the NULL pointer in case of errors.
|
---|
| 110 | */
|
---|
[219] | 111 | PDynLinkMgr* PDynLinkMgr::BuildFromCFile(string const & fname)
|
---|
| 112 | {
|
---|
| 113 | size_t l = fname.length();
|
---|
| 114 | if (l < 1) return(NULL);
|
---|
| 115 | string fnameobj = GetTmpDir()+"tmp_pdl.o";
|
---|
| 116 |
|
---|
| 117 | string cmd;
|
---|
| 118 | int rc;
|
---|
| 119 |
|
---|
| 120 | // Compilation du fichier
|
---|
[480] | 121 | #ifndef __mac__
|
---|
[1249] | 122 | #ifdef SGI_ARCH64
|
---|
| 123 | cmd = "cc -64 -c -o " + fnameobj + " " + fname;
|
---|
| 124 | #else
|
---|
[219] | 125 | cmd = "cc -c -o " + fnameobj + " " + fname;
|
---|
[1249] | 126 | #endif
|
---|
[480] | 127 | #else
|
---|
| 128 | cmd = "Il faut compiler !!!" + fnameobj + " " + fname;
|
---|
[219] | 129 | #endif
|
---|
| 130 | rc = system(cmd.c_str());
|
---|
| 131 | if (rc != 0) {
|
---|
| 132 | cerr << "PDynLinkMgr::BuildFromCFile() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
| 133 | return(NULL);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | char buff[32];
|
---|
| 137 | numSO++;
|
---|
| 138 | #ifndef HPUX
|
---|
| 139 | sprintf(buff,"pdlmgr%d%s", numSO,sofext);
|
---|
| 140 | #endif
|
---|
| 141 | string fnameso = GetTmpDir()+buff;
|
---|
| 142 |
|
---|
| 143 | // Creation du shared-lib
|
---|
| 144 | #if defined(OSF1)
|
---|
| 145 | cmd = "ld -shared -o " + fnameso + " -all " + fnameobj + " -none -lm -lc";
|
---|
[480] | 146 | #elif defined(Linux)
|
---|
[219] | 147 | cmd = "ld -shared -o " + fnameso + " " + fnameobj + " -lm -lc";
|
---|
[480] | 148 | #elif defined(SunOS)
|
---|
| 149 | cmd = "ld -G -o " + fnameso + " " + fnameobj + " -lm -lc";
|
---|
| 150 | #elif defined(IRIX64)
|
---|
[1249] | 151 | #ifdef SGI_ARCH64
|
---|
| 152 | cmd = "ld -64 -shared -o " + fnameso + " " + fnameobj + " -lm -lc";
|
---|
| 153 | #else
|
---|
[480] | 154 | cmd = "ld -shared -o " + fnameso + " " + fnameobj + " -lm -lc";
|
---|
[1249] | 155 | #endif
|
---|
[480] | 156 | #elif defined(AIX)
|
---|
| 157 | cmd = "ld -G -bnogc -bexpall -bM:1L -o " + fnameso + " " + fnameobj;
|
---|
| 158 | #elif defined(HPUX)
|
---|
| 159 | cmd = "ld -b -o " + fnameso + " " + fnameobj + " -lm -lc";
|
---|
| 160 | #else
|
---|
| 161 | cmd = "ld -o " + fnameso + " " + fnameobj + " -lm -lc";
|
---|
[219] | 162 | #endif
|
---|
| 163 | rc = system(cmd.c_str());
|
---|
| 164 | if (rc != 0) {
|
---|
| 165 | cerr << "PDynLinkMgr::BuildFromCFile() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
| 166 | return(NULL);
|
---|
| 167 | }
|
---|
[296] | 168 | PDynLinkMgr* rdyn = new PDynLinkMgr(fnameso, false);
|
---|
| 169 | rdyn->copy = true;
|
---|
| 170 | return(rdyn);
|
---|
[219] | 171 |
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | /* --Methode-- */
|
---|
[895] | 175 | /*! The constructor.
|
---|
| 176 | \param soname : Name of the shared library. ".so" is appended
|
---|
| 177 | to the name if no dot "." is found in the name.
|
---|
| 178 | \param cp : if true, copies the shared library in the temporary space.
|
---|
| 179 | */
|
---|
[219] | 180 | PDynLinkMgr::PDynLinkMgr(string& soname, bool cp)
|
---|
| 181 | {
|
---|
| 182 | dlhandle = NULL;
|
---|
| 183 | soName = "";
|
---|
| 184 |
|
---|
| 185 | if (soname.find_last_of(".") > soname.length())
|
---|
| 186 | #ifdef HPUX
|
---|
| 187 | soname += sofext_HPUX;
|
---|
| 188 | #else
|
---|
| 189 | soname += sofext;
|
---|
| 190 | #endif
|
---|
| 191 |
|
---|
| 192 | string fnameso;
|
---|
| 193 | if (cp) {
|
---|
| 194 | numSO++;
|
---|
| 195 | char buff[32];
|
---|
| 196 | #ifndef HPUX
|
---|
| 197 | sprintf(buff,"pdlmgr%d%s", numSO,sofext);
|
---|
| 198 | #else
|
---|
| 199 | sprintf(buff,"pdlmgr%d%s", numSO,sofext_HPUX);
|
---|
| 200 | #endif
|
---|
| 201 | fnameso = GetTmpDir()+buff;
|
---|
| 202 | string cmd = "cp " + soname + " " + fnameso;
|
---|
| 203 | int rc = system(cmd.c_str());
|
---|
| 204 | if (rc != 0) {
|
---|
| 205 | cerr << "PDynLinkMgr::PDynLinkMgr() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
| 206 | return;
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 | else fnameso = soname;
|
---|
| 210 | copy = cp;
|
---|
| 211 | soName = fnameso;
|
---|
| 212 |
|
---|
[480] | 213 | #if defined(HPUX)
|
---|
| 214 | dlhandle = NULL;
|
---|
| 215 | cerr << "PDynLinkMgr::PDynLinkMgr() Not yet available on HP-UX " << endl;
|
---|
| 216 | return;
|
---|
| 217 | #else
|
---|
[219] | 218 | dlhandle = dlopen(fnameso.c_str(), RTLD_NOW);
|
---|
| 219 | if (dlhandle == NULL) {
|
---|
| 220 | cerr << "PDynLinkMgr::PDynLinkMgr(): Error opening SO " << fnameso
|
---|
| 221 | << " (" << soname << ")" << endl;
|
---|
| 222 | string sn = dlerror();
|
---|
| 223 | cerr << "Loader Error (dlerror()) :" << sn << endl;
|
---|
| 224 | return;
|
---|
| 225 | }
|
---|
| 226 | #endif
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | /* --Methode-- */
|
---|
[895] | 230 | /*! Destructor. Closes the shared library. Removes the file if it had been
|
---|
| 231 | copied in the temporary space, or generated by \b BuildFromCFile */
|
---|
[219] | 232 | PDynLinkMgr::~PDynLinkMgr()
|
---|
| 233 | {
|
---|
[480] | 234 | #if defined(HPUX)
|
---|
| 235 | cerr << "PDynLinkMgr::~PDynLinkMgr() Not yet available on HP-UX " << endl;
|
---|
| 236 | return;
|
---|
| 237 | #else
|
---|
[219] | 238 | if (dlhandle) dlclose(dlhandle); dlhandle = NULL;
|
---|
| 239 | if (copy) {
|
---|
[296] | 240 | string cmd = "rm -f " + soName;
|
---|
[219] | 241 | system(cmd.c_str());
|
---|
| 242 | }
|
---|
[480] | 243 | #endif
|
---|
[219] | 244 | }
|
---|
| 245 |
|
---|
| 246 | /* --Methode-- */
|
---|
[895] | 247 | /*! Returns a handle to the function named \b funcname.
|
---|
| 248 | Returns the NULL pointer in case of error */
|
---|
[219] | 249 | DlFunction PDynLinkMgr::GetFunction(string const & funcname)
|
---|
| 250 | {
|
---|
| 251 | DlFunction f = NULL;
|
---|
[480] | 252 | #if defined(HPUX)
|
---|
| 253 | cerr << "PDynLinkMgr::GetFunction() Not yet available on HP-UX " << endl;
|
---|
| 254 | return f;
|
---|
| 255 | #else
|
---|
[219] | 256 | if (dlhandle != NULL)
|
---|
| 257 | f = (DlFunction)dlsym(dlhandle, funcname.c_str());
|
---|
[480] | 258 | if (f == NULL) cerr << "PDynLinkMgr::GetFunction(): Error linking " << funcname << endl;
|
---|
| 259 | return(f);
|
---|
[219] | 260 | #endif
|
---|
| 261 | }
|
---|
| 262 |
|
---|