1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
2 | // Gestionnaire de lien dynamique - R. Ansari 12/98
|
---|
3 | // LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
|
---|
4 |
|
---|
5 | #include <stdio.h>
|
---|
6 | #include <stdlib.h>
|
---|
7 |
|
---|
8 | #include <iostream.h>
|
---|
9 |
|
---|
10 | #include "pdlmgr.h"
|
---|
11 |
|
---|
12 | // Extension de noms de fichiers Shared libs
|
---|
13 | static const char* sofext = ".so";
|
---|
14 | static const char* sofext_HPUX = ".sl";
|
---|
15 |
|
---|
16 | // Variables et methodes static
|
---|
17 | int PDynLinkMgr::numSO = 0;
|
---|
18 | string* PDynLinkMgr::tmpDir = NULL;
|
---|
19 |
|
---|
20 | /* --Methode-Static-- */
|
---|
21 | void PDynLinkMgr::SetTmpDir(string const & path)
|
---|
22 | {
|
---|
23 | if ( (path.length() > 0) && (path[path.length()] != '/') ) GetTmpDir() = path + '/';
|
---|
24 | else GetTmpDir() = path;
|
---|
25 | return;
|
---|
26 | }
|
---|
27 |
|
---|
28 | /* --Methode-Static-- */
|
---|
29 | string& PDynLinkMgr::GetTmpDir()
|
---|
30 | {
|
---|
31 | if (tmpDir == NULL) {
|
---|
32 | tmpDir = new string("");
|
---|
33 | char* varenv;
|
---|
34 | if ( (varenv=getenv("PEIDA_TMP")) != NULL ) *tmpDir = varenv;
|
---|
35 | else if ( (varenv=getenv("TMPDIR")) != NULL ) *tmpDir = varenv;
|
---|
36 | }
|
---|
37 | return(*tmpDir);
|
---|
38 | }
|
---|
39 |
|
---|
40 | /* --Methode-Static-- */
|
---|
41 | PDynLinkMgr* PDynLinkMgr::BuildFromCFile(string const & fname)
|
---|
42 | {
|
---|
43 | size_t l = fname.length();
|
---|
44 | if (l < 1) return(NULL);
|
---|
45 | string fnameobj = GetTmpDir()+"tmp_pdl.o";
|
---|
46 |
|
---|
47 | string cmd;
|
---|
48 | int rc;
|
---|
49 |
|
---|
50 | // Compilation du fichier
|
---|
51 | #if defined(OSF1) || defined(Linux)
|
---|
52 | cmd = "cc -c -o " + fnameobj + " " + fname;
|
---|
53 | #endif
|
---|
54 | rc = system(cmd.c_str());
|
---|
55 | if (rc != 0) {
|
---|
56 | cerr << "PDynLinkMgr::BuildFromCFile() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
57 | return(NULL);
|
---|
58 | }
|
---|
59 |
|
---|
60 | char buff[32];
|
---|
61 | numSO++;
|
---|
62 | #ifndef HPUX
|
---|
63 | sprintf(buff,"pdlmgr%d%s", numSO,sofext);
|
---|
64 | #endif
|
---|
65 | string fnameso = GetTmpDir()+buff;
|
---|
66 |
|
---|
67 | // Creation du shared-lib
|
---|
68 | #if defined(OSF1)
|
---|
69 | cmd = "ld -shared -o " + fnameso + " -all " + fnameobj + " -none -lm -lc";
|
---|
70 | #else
|
---|
71 | cmd = "ld -shared -o " + fnameso + " " + fnameobj + " -lm -lc";
|
---|
72 | #endif
|
---|
73 | rc = system(cmd.c_str());
|
---|
74 | if (rc != 0) {
|
---|
75 | cerr << "PDynLinkMgr::BuildFromCFile() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
76 | return(NULL);
|
---|
77 | }
|
---|
78 |
|
---|
79 | PDynLinkMgr* rdyn = new PDynLinkMgr(fnameso, false);
|
---|
80 | rdyn->copy = true;
|
---|
81 | return(rdyn);
|
---|
82 |
|
---|
83 | }
|
---|
84 |
|
---|
85 | /* --Methode-- */
|
---|
86 | PDynLinkMgr::PDynLinkMgr(string& soname, bool cp)
|
---|
87 | {
|
---|
88 | dlhandle = NULL;
|
---|
89 | soName = "";
|
---|
90 |
|
---|
91 | if (soname.find_last_of(".") > soname.length())
|
---|
92 | #ifdef HPUX
|
---|
93 | soname += sofext_HPUX;
|
---|
94 | #else
|
---|
95 | soname += sofext;
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | string fnameso;
|
---|
99 | if (cp) {
|
---|
100 | numSO++;
|
---|
101 | char buff[32];
|
---|
102 | #ifndef HPUX
|
---|
103 | sprintf(buff,"pdlmgr%d%s", numSO,sofext);
|
---|
104 | #else
|
---|
105 | sprintf(buff,"pdlmgr%d%s", numSO,sofext_HPUX);
|
---|
106 | #endif
|
---|
107 | fnameso = GetTmpDir()+buff;
|
---|
108 | string cmd = "cp " + soname + " " + fnameso;
|
---|
109 | int rc = system(cmd.c_str());
|
---|
110 | if (rc != 0) {
|
---|
111 | cerr << "PDynLinkMgr::PDynLinkMgr() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
112 | return;
|
---|
113 | }
|
---|
114 | }
|
---|
115 | else fnameso = soname;
|
---|
116 | copy = cp;
|
---|
117 | soName = fnameso;
|
---|
118 |
|
---|
119 | #if defined(OSF1) || defined(Linux)
|
---|
120 | dlhandle = dlopen(fnameso.c_str(), RTLD_NOW);
|
---|
121 | if (dlhandle == NULL) {
|
---|
122 | cerr << "PDynLinkMgr::PDynLinkMgr(): Error opening SO " << fnameso
|
---|
123 | << " (" << soname << ")" << endl;
|
---|
124 | string sn = dlerror();
|
---|
125 | cerr << "Loader Error (dlerror()) :" << sn << endl;
|
---|
126 | return;
|
---|
127 | }
|
---|
128 | #endif
|
---|
129 | }
|
---|
130 |
|
---|
131 | /* --Methode-- */
|
---|
132 | PDynLinkMgr::~PDynLinkMgr()
|
---|
133 | {
|
---|
134 | #if defined(OSF1) || defined(Linux)
|
---|
135 | if (dlhandle) dlclose(dlhandle); dlhandle = NULL;
|
---|
136 | #endif
|
---|
137 | if (copy) {
|
---|
138 | string cmd = "rm -f " + soName;
|
---|
139 | system(cmd.c_str());
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | /* --Methode-- */
|
---|
144 | DlFunction PDynLinkMgr::GetFunction(string const & funcname)
|
---|
145 | {
|
---|
146 | DlFunction f = NULL;
|
---|
147 | #if defined(OSF1) || defined(Linux)
|
---|
148 | if (dlhandle != NULL)
|
---|
149 | f = (DlFunction)dlsym(dlhandle, funcname.c_str());
|
---|
150 | #endif
|
---|
151 | if (f == NULL) cerr << "PDynLinkMgr::PDynLinkMgr(): Error linking " << funcname << endl;
|
---|
152 | return(f);
|
---|
153 | }
|
---|
154 |
|
---|