xref: /petsc/src/sys/dll/dl.c (revision 2b8d69ca7ea5fe9190df62c1dce3bbd66fce84dd)
1 /*
2       Routines for opening dynamic link libraries (DLLs), keeping a searchable
3    path of DLLs, obtaining remote DLLs via a URL and opening them locally.
4 */
5 
6 #include <petsc/private/petscimpl.h>
7 
8 /* ------------------------------------------------------------------------------*/
9 /*
10       Code to maintain a list of opened dynamic libraries and load symbols
11 */
12 struct _n_PetscDLLibrary {
13   PetscDLLibrary next;
14   PetscDLHandle  handle;
15   char           libname[PETSC_MAX_PATH_LEN];
16 };
17 
18 #undef __FUNCT__
19 #define __FUNCT__ "PetscDLLibraryPrintPath"
20 PetscErrorCode  PetscDLLibraryPrintPath(PetscDLLibrary libs)
21 {
22   PetscFunctionBegin;
23   while (libs) {
24     PetscErrorPrintf("  %s\n",libs->libname);
25     libs = libs->next;
26   }
27   PetscFunctionReturn(0);
28 }
29 
30 #undef __FUNCT__
31 #define __FUNCT__ "PetscDLLibraryRetrieve"
32 /*@C
33    PetscDLLibraryRetrieve - Copies a PETSc dynamic library from a remote location
34      (if it is remote), indicates if it exits and its local name.
35 
36      Collective on MPI_Comm
37 
38    Input Parameters:
39 +   comm - processors that are opening the library
40 -   libname - name of the library, can be relative or absolute
41 
42    Output Parameter:
43 +   name - actual name of file on local filesystem if found
44 .   llen - length of the name buffer
45 -   found - true if the file exists
46 
47    Level: developer
48 
49    Notes:
50    [[<http,ftp>://hostname]/directoryname/]filename[.so.1.0]
51 
52    ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LIB_DIR}, or ${any environmental variable}
53    occuring in directoryname and filename will be replaced with appropriate values.
54 @*/
55 PetscErrorCode  PetscDLLibraryRetrieve(MPI_Comm comm,const char libname[],char *lname,size_t llen,PetscBool  *found)
56 {
57   char           *buf,*par2,suffix[16],*gz,*so;
58   size_t         len;
59   PetscErrorCode ierr;
60 
61   PetscFunctionBegin;
62   /*
63      make copy of library name and replace $PETSC_ARCH etc
64      so we can add to the end of it to look for something like .so.1.0 etc.
65   */
66   ierr = PetscStrlen(libname,&len);CHKERRQ(ierr);
67   len  = PetscMax(4*len,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
68   ierr = PetscMalloc1(len,&buf);CHKERRQ(ierr);
69   par2 = buf;
70   ierr = PetscStrreplace(comm,libname,par2,len);CHKERRQ(ierr);
71 
72   /* temporarily remove .gz if it ends library name */
73   ierr = PetscStrrstr(par2,".gz",&gz);CHKERRQ(ierr);
74   if (gz) {
75     ierr = PetscStrlen(gz,&len);CHKERRQ(ierr);
76     if (len != 3) gz  = 0; /* do not end (exactly) with .gz */
77     else          *gz = 0; /* ends with .gz, so remove it   */
78   }
79   /* strip out .a from it if user put it in by mistake */
80   ierr = PetscStrlen(par2,&len);CHKERRQ(ierr);
81   if (par2[len-1] == 'a' && par2[len-2] == '.') par2[len-2] = 0;
82 
83   ierr = PetscFileRetrieve(comm,par2,lname,llen,found);CHKERRQ(ierr);
84   if (!(*found)) {
85     /* see if library name does already not have suffix attached */
86     ierr = PetscStrcpy(suffix,".");CHKERRQ(ierr);
87     ierr = PetscStrcat(suffix,PETSC_SLSUFFIX);CHKERRQ(ierr);
88     ierr = PetscStrrstr(par2,suffix,&so);CHKERRQ(ierr);
89     /* and attach the suffix if it is not there */
90     if (!so) { ierr = PetscStrcat(par2,suffix);CHKERRQ(ierr); }
91 
92     /* restore the .gz suffix if it was there */
93     if (gz) { ierr = PetscStrcat(par2,".gz");CHKERRQ(ierr); }
94 
95     /* and finally retrieve the file */
96     ierr = PetscFileRetrieve(comm,par2,lname,llen,found);CHKERRQ(ierr);
97   }
98 
99   ierr = PetscFree(buf);CHKERRQ(ierr);
100   PetscFunctionReturn(0);
101 }
102 
103 
104 #undef __FUNCT__
105 #define __FUNCT__ "PetscDLLibraryOpen"
106 /*@C
107    PetscDLLibraryOpen - Opens a PETSc dynamic link library
108 
109      Collective on MPI_Comm
110 
111    Input Parameters:
112 +   comm - processors that are opening the library
113 -   path - name of the library, can be relative or absolute
114 
115    Output Parameter:
116 .   entry - a PETSc dynamic link library entry
117 
118    Level: developer
119 
120    Notes:
121    [[<http,ftp>://hostname]/directoryname/]libbasename[.so.1.0]
122 
123    If the library has the symbol PetscDLLibraryRegister_basename() in it then that function is automatically run
124    when the library is opened.
125 
126    ${PETSC_ARCH} occuring in directoryname and filename
127    will be replaced with the appropriate value.
128 
129 .seealso: PetscLoadDynamicLibrary(), PetscDLLibraryAppend()
130 @*/
131 PetscErrorCode  PetscDLLibraryOpen(MPI_Comm comm,const char path[],PetscDLLibrary *entry)
132 {
133   PetscErrorCode ierr;
134   PetscBool      foundlibrary,match;
135   char           libname[PETSC_MAX_PATH_LEN],par2[PETSC_MAX_PATH_LEN],suffix[16],*s;
136   char           *basename,registername[128];
137   PetscDLHandle  handle;
138   PetscErrorCode (*func)(void) = NULL;
139   size_t         len;
140 
141   PetscFunctionBegin;
142   PetscValidCharPointer(path,2);
143   PetscValidPointer(entry,3);
144 
145   *entry = NULL;
146 
147   /* retrieve the library */
148   ierr = PetscInfo1(0,"Retrieving %s\n",path);CHKERRQ(ierr);
149   ierr = PetscDLLibraryRetrieve(comm,path,par2,PETSC_MAX_PATH_LEN,&foundlibrary);CHKERRQ(ierr);
150   if (!foundlibrary) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate dynamic library:\n  %s\n",path);
151   /* Eventually ./configure should determine if the system needs an executable dynamic library */
152 #define PETSC_USE_NONEXECUTABLE_SO
153 #if !defined(PETSC_USE_NONEXECUTABLE_SO)
154   ierr = PetscTestFile(par2,'x',&foundlibrary);CHKERRQ(ierr);
155   if (!foundlibrary) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Dynamic library is not executable:\n  %s\n  %s\n",path,par2);
156 #endif
157 
158   /* copy path and setup shared library suffix  */
159   ierr = PetscStrncpy(libname,path,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
160   ierr = PetscStrcpy(suffix,".");CHKERRQ(ierr);
161   ierr = PetscStrcat(suffix,PETSC_SLSUFFIX);CHKERRQ(ierr);
162   /* remove wrong suffixes from libname */
163   ierr = PetscStrrstr(libname,".gz",&s);CHKERRQ(ierr);
164   if (s && s[3] == 0) s[0] = 0;
165   ierr = PetscStrrstr(libname,".a",&s);CHKERRQ(ierr);
166   if (s && s[2] == 0) s[0] = 0;
167   /* remove shared suffix from libname */
168   ierr = PetscStrrstr(libname,suffix,&s);CHKERRQ(ierr);
169   if (s) s[0] = 0;
170 
171   /* open the dynamic library */
172   ierr = PetscInfo1(0,"Opening dynamic library %s\n",libname);CHKERRQ(ierr);
173   ierr = PetscDLOpen(par2,PETSC_DL_DECIDE,&handle);CHKERRQ(ierr);
174 
175   /* look for [path/]libXXXXX.YYY and extract out the XXXXXX */
176   ierr = PetscStrrchr(libname,'/',&basename);CHKERRQ(ierr); /* XXX Windows ??? */
177   if (!basename) basename = libname;
178   ierr = PetscStrncmp(basename,"lib",3,&match);CHKERRQ(ierr);
179   if (match) basename = basename + 3;
180   else {
181     ierr = PetscInfo1(0,"Dynamic library %s does not have lib prefix\n",libname);CHKERRQ(ierr);
182   }
183   for (s=basename; *s; s++) if (*s == '-') *s = '_';
184   ierr = PetscStrlen(basename,&len);CHKERRQ(ierr);
185   ierr = PetscStrcpy(registername,"PetscDLLibraryRegister_");CHKERRQ(ierr);
186   ierr = PetscStrncat(registername,basename,len);CHKERRQ(ierr);
187   ierr = PetscDLSym(handle,registername,(void**)&func);CHKERRQ(ierr);
188   if (func) {
189     ierr = PetscInfo1(0,"Loading registered routines from %s\n",libname);CHKERRQ(ierr);
190     ierr = (*func)();CHKERRQ(ierr);
191   } else {
192     ierr = PetscInfo2(0,"Dynamic library %s does not have symbol %s\n",libname,registername);CHKERRQ(ierr);
193   }
194 
195   ierr = PetscNew(entry);CHKERRQ(ierr);
196   (*entry)->next   = 0;
197   (*entry)->handle = handle;
198   ierr = PetscStrcpy((*entry)->libname,libname);CHKERRQ(ierr);
199   PetscFunctionReturn(0);
200 }
201 
202 #undef __FUNCT__
203 #define __FUNCT__ "PetscDLLibrarySym"
204 /*@C
205    PetscDLLibrarySym - Load a symbol from the dynamic link libraries.
206 
207    Collective on MPI_Comm
208 
209    Input Parameter:
210 +  comm - communicator that will open the library
211 .  outlist - list of already open libraries that may contain symbol (can be NULL and only the executable is searched for the function)
212 .  path     - optional complete library name (if provided checks here before checking outlist)
213 -  insymbol - name of symbol
214 
215    Output Parameter:
216 .  value - if symbol not found then this value is set to NULL
217 
218    Level: developer
219 
220    Notes: Symbol can be of the form
221         [/path/libname[.so.1.0]:]functionname[()] where items in [] denote optional
222 
223         Will attempt to (retrieve and) open the library if it is not yet been opened.
224 
225 @*/
226 PetscErrorCode  PetscDLLibrarySym(MPI_Comm comm,PetscDLLibrary *outlist,const char path[],const char insymbol[],void **value)
227 {
228   char           libname[PETSC_MAX_PATH_LEN],suffix[16],*symbol,*s;
229   PetscDLLibrary nlist,prev,list = NULL;
230   PetscErrorCode ierr;
231 
232   PetscFunctionBegin;
233   if (outlist) PetscValidPointer(outlist,2);
234   if (path) PetscValidCharPointer(path,3);
235   PetscValidCharPointer(insymbol,4);
236   PetscValidPointer(value,5);
237 
238   if (outlist) list = *outlist;
239   *value = 0;
240 
241 
242   ierr = PetscStrchr(insymbol,'(',&s);CHKERRQ(ierr);
243   if (s) {
244     /* make copy of symbol so we can edit it in place */
245     ierr = PetscStrallocpy(insymbol,&symbol);CHKERRQ(ierr);
246     /* If symbol contains () then replace with a NULL, to support functionname() */
247     ierr = PetscStrchr(symbol,'(',&s);CHKERRQ(ierr);
248     s[0] = 0;
249   } else symbol = (char*)insymbol;
250 
251   /*
252        Function name does include library
253        -------------------------------------
254   */
255   if (path && path[0] != '\0') {
256     /* copy path and remove suffix from libname */
257     ierr = PetscStrncpy(libname,path,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
258     ierr = PetscStrcpy(suffix,".");CHKERRQ(ierr);
259     ierr = PetscStrcat(suffix,PETSC_SLSUFFIX);CHKERRQ(ierr);
260     ierr = PetscStrrstr(libname,suffix,&s);CHKERRQ(ierr);
261     if (s) s[0] = 0;
262     /* Look if library is already opened and in path */
263     prev  = 0;
264     nlist = list;
265     while (nlist) {
266       PetscBool match;
267       ierr = PetscStrcmp(nlist->libname,libname,&match);CHKERRQ(ierr);
268       if (match) goto done;
269       prev  = nlist;
270       nlist = nlist->next;
271     }
272     /* open the library and append it to path */
273     ierr = PetscDLLibraryOpen(comm,path,&nlist);CHKERRQ(ierr);
274     ierr = PetscInfo1(0,"Appending %s to dynamic library search path\n",path);CHKERRQ(ierr);
275     if (prev) prev->next = nlist;
276     else      *outlist   = nlist;
277 
278 done:;
279     ierr = PetscDLSym(nlist->handle,symbol,value);CHKERRQ(ierr);
280     if (*value) {
281       ierr = PetscInfo2(0,"Loading function %s from dynamic library %s\n",insymbol,path);CHKERRQ(ierr);
282     }
283 
284     /*
285          Function name does not include library so search path
286          -----------------------------------------------------
287     */
288   } else {
289     while (list) {
290       ierr = PetscDLSym(list->handle,symbol,value);CHKERRQ(ierr);
291       if (*value) {
292         ierr = PetscInfo2(0,"Loading symbol %s from dynamic library %s\n",symbol,list->libname);CHKERRQ(ierr);
293         break;
294       }
295       list = list->next;
296     }
297     if (!*value) {
298       ierr = PetscDLSym(NULL,symbol,value);CHKERRQ(ierr);
299       if (*value) {
300         ierr = PetscInfo1(0,"Loading symbol %s from object code\n",symbol);CHKERRQ(ierr);
301       }
302     }
303   }
304 
305   if (symbol != insymbol) {
306     ierr = PetscFree(symbol);CHKERRQ(ierr);
307   }
308   PetscFunctionReturn(0);
309 }
310 
311 #undef __FUNCT__
312 #define __FUNCT__ "PetscDLLibraryAppend"
313 /*@C
314      PetscDLLibraryAppend - Appends another dynamic link library to the seach list, to the end
315                 of the search path.
316 
317      Collective on MPI_Comm
318 
319      Input Parameters:
320 +     comm - MPI communicator
321 -     path - name of the library
322 
323      Output Parameter:
324 .     outlist - list of libraries
325 
326      Level: developer
327 
328      Notes: if library is already in path will not add it.
329 
330   If the library has the symbol PetscDLLibraryRegister_basename() in it then that function is automatically run
331       when the library is opened.
332 
333 .seealso: PetscDLLibraryOpen()
334 @*/
335 PetscErrorCode  PetscDLLibraryAppend(MPI_Comm comm,PetscDLLibrary *outlist,const char path[])
336 {
337   PetscDLLibrary list,prev;
338   PetscErrorCode ierr;
339   size_t         len;
340   PetscBool      match,dir;
341   char           program[PETSC_MAX_PATH_LEN],found[8*PETSC_MAX_PATH_LEN];
342   char           *libname,suffix[16],*s;
343   PetscToken     token;
344 
345   PetscFunctionBegin;
346   PetscValidPointer(outlist,2);
347 
348   /* is path a directory? */
349   ierr = PetscTestDirectory(path,'r',&dir);CHKERRQ(ierr);
350   if (dir) {
351     ierr = PetscInfo1(0,"Checking directory %s for dynamic libraries\n",path);CHKERRQ(ierr);
352     ierr = PetscStrcpy(program,path);CHKERRQ(ierr);
353     ierr = PetscStrlen(program,&len);CHKERRQ(ierr);
354     if (program[len-1] == '/') {
355       ierr = PetscStrcat(program,"*.");CHKERRQ(ierr);
356     } else {
357       ierr = PetscStrcat(program,"/*.");CHKERRQ(ierr);
358     }
359     ierr = PetscStrcat(program,PETSC_SLSUFFIX);CHKERRQ(ierr);
360 
361     ierr = PetscLs(comm,program,found,8*PETSC_MAX_PATH_LEN,&dir);CHKERRQ(ierr);
362     if (!dir) PetscFunctionReturn(0);
363   } else {
364     ierr = PetscStrncpy(found,path,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
365   }
366   ierr = PetscStrcpy(suffix,".");CHKERRQ(ierr);
367   ierr = PetscStrcat(suffix,PETSC_SLSUFFIX);CHKERRQ(ierr);
368 
369   ierr = PetscTokenCreate(found,'\n',&token);CHKERRQ(ierr);
370   ierr = PetscTokenFind(token,&libname);CHKERRQ(ierr);
371   while (libname) {
372     /* remove suffix from libname */
373     ierr = PetscStrrstr(libname,suffix,&s);CHKERRQ(ierr);
374     if (s) s[0] = 0;
375     /* see if library was already open then we are done */
376     list  = prev = *outlist;
377     match = PETSC_FALSE;
378     while (list) {
379       ierr = PetscStrcmp(list->libname,libname,&match);CHKERRQ(ierr);
380       if (match) break;
381       prev = list;
382       list = list->next;
383     }
384     /* restore suffix from libname */
385     if (s) s[0] = '.';
386     if (!match) {
387       /* open the library and add to end of list */
388       ierr = PetscDLLibraryOpen(comm,libname,&list);CHKERRQ(ierr);
389       ierr = PetscInfo1(0,"Appending %s to dynamic library search path\n",libname);CHKERRQ(ierr);
390       if (!*outlist) *outlist   = list;
391       else           prev->next = list;
392     }
393     ierr = PetscTokenFind(token,&libname);CHKERRQ(ierr);
394   }
395   ierr = PetscTokenDestroy(&token);CHKERRQ(ierr);
396   PetscFunctionReturn(0);
397 }
398 
399 #undef __FUNCT__
400 #define __FUNCT__ "PetscDLLibraryPrepend"
401 /*@C
402      PetscDLLibraryPrepend - Add another dynamic library to search for symbols to the beginning of
403                  the search path.
404 
405      Collective on MPI_Comm
406 
407      Input Parameters:
408 +     comm - MPI communicator
409 -     path - name of the library
410 
411      Output Parameter:
412 .     outlist - list of libraries
413 
414      Level: developer
415 
416      Notes: If library is already in path will remove old reference.
417 
418 @*/
419 PetscErrorCode  PetscDLLibraryPrepend(MPI_Comm comm,PetscDLLibrary *outlist,const char path[])
420 {
421   PetscDLLibrary list,prev;
422   PetscErrorCode ierr;
423   size_t         len;
424   PetscBool      match,dir;
425   char           program[PETSC_MAX_PATH_LEN],found[8*PETSC_MAX_PATH_LEN];
426   char           *libname,suffix[16],*s;
427   PetscToken     token;
428 
429   PetscFunctionBegin;
430   PetscValidPointer(outlist,2);
431 
432   /* is path a directory? */
433   ierr = PetscTestDirectory(path,'r',&dir);CHKERRQ(ierr);
434   if (dir) {
435     ierr = PetscInfo1(0,"Checking directory %s for dynamic libraries\n",path);CHKERRQ(ierr);
436     ierr = PetscStrcpy(program,path);CHKERRQ(ierr);
437     ierr = PetscStrlen(program,&len);CHKERRQ(ierr);
438     if (program[len-1] == '/') {
439       ierr = PetscStrcat(program,"*.");CHKERRQ(ierr);
440     } else {
441       ierr = PetscStrcat(program,"/*.");CHKERRQ(ierr);
442     }
443     ierr = PetscStrcat(program,PETSC_SLSUFFIX);CHKERRQ(ierr);
444 
445     ierr = PetscLs(comm,program,found,8*PETSC_MAX_PATH_LEN,&dir);CHKERRQ(ierr);
446     if (!dir) PetscFunctionReturn(0);
447   } else {
448     ierr = PetscStrncpy(found,path,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
449   }
450 
451   ierr = PetscStrcpy(suffix,".");CHKERRQ(ierr);
452   ierr = PetscStrcat(suffix,PETSC_SLSUFFIX);CHKERRQ(ierr);
453 
454   ierr = PetscTokenCreate(found,'\n',&token);CHKERRQ(ierr);
455   ierr = PetscTokenFind(token,&libname);CHKERRQ(ierr);
456   while (libname) {
457     /* remove suffix from libname */
458     ierr = PetscStrstr(libname,suffix,&s);CHKERRQ(ierr);
459     if (s) s[0] = 0;
460     /* see if library was already open and move it to the front */
461     prev  = 0;
462     list  = *outlist;
463     match = PETSC_FALSE;
464     while (list) {
465       ierr = PetscStrcmp(list->libname,libname,&match);CHKERRQ(ierr);
466       if (match) {
467         ierr = PetscInfo1(0,"Moving %s to begin of dynamic library search path\n",libname);CHKERRQ(ierr);
468         if (prev) prev->next = list->next;
469         if (prev) list->next = *outlist;
470         *outlist = list;
471         break;
472       }
473       prev = list;
474       list = list->next;
475     }
476     /* restore suffix from libname */
477     if (s) s[0] = '.';
478     if (!match) {
479       /* open the library and add to front of list */
480       ierr       = PetscDLLibraryOpen(comm,libname,&list);CHKERRQ(ierr);
481       ierr       = PetscInfo1(0,"Prepending %s to dynamic library search path\n",libname);CHKERRQ(ierr);
482       list->next = *outlist;
483       *outlist   = list;
484     }
485     ierr = PetscTokenFind(token,&libname);CHKERRQ(ierr);
486   }
487   ierr = PetscTokenDestroy(&token);CHKERRQ(ierr);
488   PetscFunctionReturn(0);
489 }
490 
491 #undef __FUNCT__
492 #define __FUNCT__ "PetscDLLibraryClose"
493 /*@C
494      PetscDLLibraryClose - Destroys the search path of dynamic libraries and closes the libraries.
495 
496     Collective on PetscDLLibrary
497 
498     Input Parameter:
499 .     head - library list
500 
501      Level: developer
502 
503 @*/
504 PetscErrorCode  PetscDLLibraryClose(PetscDLLibrary list)
505 {
506   PetscBool      done = PETSC_FALSE;
507   PetscDLLibrary prev,tail;
508   PetscErrorCode ierr;
509 
510   PetscFunctionBegin;
511   if (!list) PetscFunctionReturn(0);
512   /* traverse the list in reverse order */
513   while (!done) {
514     if (!list->next) done = PETSC_TRUE;
515     prev = tail = list;
516     while (tail->next) {
517       prev = tail;
518       tail = tail->next;
519     }
520     prev->next = 0;
521     /* close the dynamic library and free the space in entry data-structure*/
522     ierr = PetscInfo1(0,"Closing dynamic library %s\n",tail->libname);CHKERRQ(ierr);
523     ierr = PetscDLClose(&tail->handle);CHKERRQ(ierr);
524     ierr = PetscFree(tail);CHKERRQ(ierr);
525   }
526   PetscFunctionReturn(0);
527 }
528 
529