Actual source code: dl.c

  1: #define PETSC_DLL
  2: /*
  3:       Routines for opening dynamic link libraries (DLLs), keeping a searchable
  4:    path of DLLs, obtaining remote DLLs via a URL and opening them locally.
  5: */

 7:  #include petsc.h
 8:  #include petscsys.h
  9: #include "petscfix.h"

 11: #if defined(PETSC_USE_DYNAMIC_LIBRARIES)

 13: #if defined(PETSC_HAVE_PWD_H)
 14: #include <pwd.h>
 15: #endif
 16: #include <ctype.h>
 17: #include <sys/types.h>
 18: #include <sys/stat.h>
 19: #if defined(PETSC_HAVE_UNISTD_H)
 20: #include <unistd.h>
 21: #endif
 22: #if defined(PETSC_HAVE_STDLIB_H)
 23: #include <stdlib.h>
 24: #endif
 25: #if defined(PETSC_HAVE_SYS_UTSNAME_H)
 26: #include <sys/utsname.h>
 27: #endif
 28: #if defined(PETSC_HAVE_WINDOWS_H)
 29: #include <windows.h>
 30: #endif
 31: #include <fcntl.h>
 32: #include <time.h>  
 33: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
 34: #include <sys/systeminfo.h>
 35: #endif
 36: #if defined(PETSC_HAVE_DLFCN_H)
 37: #include <dlfcn.h>
 38: #endif

 40: #endif


 43: /*
 44:    Contains the list of registered CCA components
 45: */
 46: PetscFList CCAList = 0;


 49: /* ------------------------------------------------------------------------------*/
 50: /*
 51:       Code to maintain a list of opened dynamic libraries and load symbols
 52: */
 53: #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
 54: struct _n_PetscDLLibrary {
 55:   PetscDLLibrary next;
 56:   void           *handle;
 57:   char           libname[PETSC_MAX_PATH_LEN];
 58: };

 61: EXTERN PetscErrorCode Petsc_DelTag(MPI_Comm,int,void*,void*);

 66: PetscErrorCode  PetscDLLibraryPrintPath(void)
 67: {
 68:   PetscDLLibrary libs;

 71:   libs = DLLibrariesLoaded;
 72:   while (libs) {
 73:     PetscErrorPrintf("  %s\n",libs->libname);
 74:     libs = libs->next;
 75:   }
 76:   return(0);
 77: }

 81: /*@C
 82:    PetscDLLibraryRetrieve - Copies a PETSc dynamic library from a remote location
 83:      (if it is remote), indicates if it exits and its local name.

 85:      Collective on MPI_Comm

 87:    Input Parameters:
 88: +   comm - processors that are opening the library
 89: -   libname - name of the library, can be relative or absolute

 91:    Output Parameter:
 92: .   handle - library handle 

 94:    Level: developer

 96:    Notes:
 97:    [[<http,ftp>://hostname]/directoryname/]filename[.so.1.0]

 99:    ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LIB_DIR}, or ${any environmental variable}
100:    occuring in directoryname and filename will be replaced with appropriate values.
101: @*/
102: PetscErrorCode  PetscDLLibraryRetrieve(MPI_Comm comm,const char libname[],char *lname,int llen,PetscTruth *found)
103: {
104:   char           *par2,buff[10],*en,*gz;
106:   size_t         len1,len2,len;
107:   PetscTruth     tflg,flg;

110:   /* 
111:      make copy of library name and replace $PETSC_ARCH etc
112:      so we can add to the end of it to look for something like .so.1.0 etc.
113:   */
114:   PetscStrlen(libname,&len);
115:   len  = PetscMax(4*len,PETSC_MAX_PATH_LEN);
116:   PetscMalloc(len*sizeof(char),&par2);
117:   PetscStrreplace(comm,libname,par2,len);

119:   /* 
120:      Remove any file: header
121:   */
122:   PetscStrncmp(par2,"file:",5,&tflg);
123:   if (tflg) {
124:     PetscStrcpy(par2,par2+5);
125:   }

127:   /* strip out .a from it if user put it in by mistake */
128:   PetscStrlen(par2,&len);
129:   if (par2[len-1] == 'a' && par2[len-2] == '.') par2[len-2] = 0;

131:   /* remove .gz if it ends library name */
132:   PetscStrstr(par2,".gz",&gz);
133:   if (gz) {
134:     PetscStrlen(gz,&len);
135:     if (len == 3) {
136:       *gz = 0;
137:     }
138:   }

140:   /* see if library name does already not have suffix attached */
141:   PetscStrcpy(buff,".");
142:   PetscStrcat(buff,PETSC_SLSUFFIX);
143:   PetscStrstr(par2,buff,&en);
144:   if (en) {
145:     PetscStrlen(en,&len1);
146:     PetscStrlen(PETSC_SLSUFFIX,&len2);
147:     flg = (PetscTruth) (len1 != 1 + len2);
148:   } else {
149:     flg = PETSC_TRUE;
150:   }
151:   if (flg) {
152:     PetscStrcat(par2,".");
153:     PetscStrcat(par2,PETSC_SLSUFFIX);
154:   }

156:   /* put the .gz back on if it was there */
157:   if (gz) {
158:     PetscStrcat(par2,".gz");
159:   }

161:   PetscFileRetrieve(comm,par2,lname,llen,found);
162:   PetscFree(par2);
163:   return(0);
164: }


169: /*@C
170:    PetscDLLibraryOpen - Opens a dynamic link library

172:      Collective on MPI_Comm

174:    Input Parameters:
175: +   comm - processors that are opening the library
176: -   libname - name of the library, can be relative or absolute

178:    Output Parameter:
179: .   handle - library handle 

181:    Level: developer

183:    Notes:
184:    [[<http,ftp>://hostname]/directoryname/]filename[.so.1.0]

186:    ${PETSC_ARCH} occuring in directoryname and filename 
187:    will be replaced with the appropriate value.
188: @*/
189: PetscErrorCode  PetscDLLibraryOpen(MPI_Comm comm,const char libname[],void **handle)
190: {
192:   char           *par2,registername[128],*ptr,*ptrp;
193:   PetscTruth     foundlibrary;
194:   PetscErrorCode (*func)(const char*) = NULL;
195:   size_t         len;

198:   *handle = NULL;
199:   PetscMalloc(PETSC_MAX_PATH_LEN*sizeof(char),&par2);
200:   PetscDLLibraryRetrieve(comm,libname,par2,PETSC_MAX_PATH_LEN,&foundlibrary);
201:   if (!foundlibrary) SETERRQ1(PETSC_ERR_FILE_OPEN,"Unable to locate dynamic library:\n  %s\n",libname);

203:   /* Eventually config/configure.py should determine if the system needs an executable dynamic library */
204: #define PETSC_USE_NONEXECUTABLE_SO
205: #if !defined(PETSC_USE_NONEXECUTABLE_SO)
206:   PetscTestFile(par2,'x',&foundlibrary);
207:   if (!foundlibrary) SETERRQ2(PETSC_ERR_FILE_OPEN,"Dynamic library is not executable:\n  %s\n  %s\n",libname,par2);
208: #endif

210:   /*
211:       Mode indicates symbols required by symbol loaded with dlsym() 
212:      are only loaded when required (not all together) also indicates
213:      symbols required can be contained in other libraries also opened
214:      with dlopen()
215:   */
216:   PetscInfo1(0,"Opening %s\n",libname);
217: #if defined(PETSC_HAVE_LOADLIBRARY)
218:   *handle = LoadLibrary(par2);
219: #elif defined(PETSC_HAVE_RTLD_GLOBAL)
220:   *handle = dlopen(par2,RTLD_LAZY | RTLD_GLOBAL);
221: #else
222:   *handle = dlopen(par2,RTLD_LAZY);
223: #endif

225:   if (!*handle) {
226: #if defined(PETSC_HAVE_DLERROR)
227:     SETERRQ3(PETSC_ERR_FILE_OPEN,"Unable to open dynamic library:\n  %s\n  %s\n  Error message from dlopen() %s\n",libname,par2,dlerror());
228: #elif defined(PETSC_HAVE_GETLASTERROR)
229:     {
230:       DWORD erc;
231:       char  *buff;
232:       erc   = GetLastError();
233:       FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
234:                     NULL,erc,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPSTR)&buff,0,NULL);
235:       PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,PETSC_ERR_FILE_OPEN,1,
236:                         "Unable to open dynamic library:\n  %s\n  %s\n  Error message from LoadLibrary() %s\n",libname,par2,buff);
237:       LocalFree(buff);
238:       return(ierr);
239:     }
240: #endif
241:   }

243:   /* build name of symbol to look for based on libname */
244:   PetscStrcpy(registername,"PetscDLLibraryRegister_");
245:   /* look for libXXXXX.YYY and extract out the XXXXXX */
246:   PetscStrrstr(libname,"lib",&ptr);
247:   if (!ptr) SETERRQ1(PETSC_ERR_ARG_WRONG,"Dynamic library name must have lib prefix:%s",libname);
248:   PetscStrchr(ptr+3,'.',&ptrp);
249:   if (ptrp) {
250:     len = ptrp - ptr - 3;
251:   } else {
252:     PetscStrlen(ptr+3,&len);
253:   }
254:   PetscStrncat(registername,ptr+3,len);

256: #if defined(PETSC_HAVE_GETPROCADDRESS)
257:   func = (PetscErrorCode (*)(const char *)) GetProcAddress((HMODULE)*handle,registername);
258: #else
259:   func = (PetscErrorCode (*)(const char *)) dlsym(*handle,registername);
260: #endif
261:   if (func) {
262:     (*func)(libname);
263:     PetscInfo1(0,"Loading registered routines from %s\n",libname);
264:   } else {
265:     SETERRQ2(PETSC_ERR_FILE_UNEXPECTED,"Able to locate dynamic library %s, but cannot load symbol  %s\n",libname,registername);
266:   }
267:   PetscFree(par2);
268:   return(0);
269: }

273: /*@C
274:    PetscDLLibrarySym - Load a symbol from the dynamic link libraries.

276:    Collective on MPI_Comm

278:    Input Parameter:
279: +  comm - communicator that will open the library
280: .  inlist - list of already open libraries that may contain symbol (checks here before path)
281: .  path     - optional complete library name
282: -  insymbol - name of symbol

284:    Output Parameter:
285: .  value 

287:    Level: developer

289:    Notes: Symbol can be of the form
290:         [/path/libname[.so.1.0]:]functionname[()] where items in [] denote optional 

292:         Will attempt to (retrieve and) open the library if it is not yet been opened.

294: @*/
295: PetscErrorCode  PetscDLLibrarySym(MPI_Comm comm,PetscDLLibrary *inlist,const char path[],const char insymbol[],void **value)
296: {
297:   char           *par1,*symbol;
299:   size_t         len;
300:   PetscDLLibrary nlist,prev,list;

303:   if (inlist) list = *inlist; else list = PETSC_NULL;
304:   *value = 0;

306:   /* make copy of symbol so we can edit it in place */
307:   PetscStrlen(insymbol,&len);
308:   PetscMalloc((len+1)*sizeof(char),&symbol);
309:   PetscStrcpy(symbol,insymbol);

311:   /* 
312:       If symbol contains () then replace with a NULL, to support functionname() 
313:   */
314:   PetscStrchr(symbol,'(',&par1);
315:   if (par1) *par1 = 0;


318:   /*
319:        Function name does include library 
320:        -------------------------------------
321:   */
322:   if (path && path[0] != '\0') {
323:     void *handle;
324: 
325:     /*   
326:         Look if library is already opened and in path
327:     */
328:     nlist = list;
329:     prev  = 0;
330:     while (nlist) {
331:       PetscTruth match;

333:       PetscStrcmp(nlist->libname,path,&match);
334:       if (match) {
335:         handle = nlist->handle;
336:         goto done;
337:       }
338:       prev  = nlist;
339:       nlist = nlist->next;
340:     }
341:     PetscDLLibraryOpen(comm,path,&handle);

343:     PetscNew(struct _n_PetscDLLibrary,&nlist);
344:     nlist->next   = 0;
345:     nlist->handle = handle;
346:     PetscStrcpy(nlist->libname,path);

348:     if (prev) {
349:       prev->next = nlist;
350:     } else {
351:       if (inlist) *inlist = nlist;
352:       else {PetscDLLibraryClose(nlist);}
353:     }
354:     PetscInfo1(0,"Appending %s to dynamic library search path\n",path);

356:     done:;
357: #if defined(PETSC_HAVE_GETPROCADDRESS)
358:     *value   = GetProcAddress((HMODULE)handle,symbol);
359: #else
360:     *value   = dlsym(handle,symbol);
361: #endif
362:     if (!*value) {
363:       SETERRQ2(PETSC_ERR_PLIB,"Unable to locate function %s in dynamic library %s",insymbol,path);
364:     }
365:     PetscInfo2(0,"Loading function %s from dynamic library %s\n",insymbol,path);

367:   /*
368:        Function name does not include library so search path
369:        -----------------------------------------------------
370:   */
371:   } else {
372:     while (list) {
373: #if defined(PETSC_HAVE_GETPROCADDRESS)
374:       *value = GetProcAddress((HMODULE)list->handle,symbol);
375: #else
376:       *value =  dlsym(list->handle,symbol);
377: #endif
378:       if (*value) {
379:         PetscInfo2(0,"Loading function %s from dynamic library %s\n",symbol,list->libname);
380:         break;
381:       }
382:       list = list->next;
383:     }
384:     if (!*value) {
385: #if defined(PETSC_HAVE_GETPROCADDRESS)
386:       *value = GetProcAddress(GetCurrentProcess(),symbol);
387: #else
388:       *value = dlsym(0,symbol);
389: #endif
390:       if (*value) {
391:         PetscInfo1(0,"Loading function %s from object code\n",symbol);
392:       }
393:     }
394:   }

396:   PetscFree(symbol);
397:   return(0);
398: }

402: /*@C
403:      PetscDLLibraryAppend - Appends another dynamic link library to the seach list, to the end
404:                 of the search path.

406:      Collective on MPI_Comm

408:      Input Parameters:
409: +     comm - MPI communicator
410: -     libname - name of the library

412:      Output Parameter:
413: .     outlist - list of libraries

415:      Level: developer

417:      Notes: if library is already in path will not add it.
418: @*/
419: PetscErrorCode  PetscDLLibraryAppend(MPI_Comm comm,PetscDLLibrary *outlist,const char libname[])
420: {
421:   PetscDLLibrary list,prev;
422:   void*          handle;
424:   size_t         len;
425:   PetscTruth     match,dir;
426:   char           program[PETSC_MAX_PATH_LEN],buf[8*PETSC_MAX_PATH_LEN],*found,*libname1,suffix[16],*s;
427:   PetscToken     token;


431:   /* is libname a directory? */
432:   PetscTestDirectory(libname,'r',&dir);
433:   if (dir) {
434:     PetscInfo1(0,"Checking directory %s for dynamic libraries\n",libname);
435:     PetscStrcpy(program,libname);
436:     PetscStrlen(program,&len);
437:     if (program[len-1] == '/') {
438:       PetscStrcat(program,"*.");
439:     } else {
440:       PetscStrcat(program,"/*.");
441:     }
442:     PetscStrcat(program,PETSC_SLSUFFIX);

444:     PetscLs(comm,program,buf,8*PETSC_MAX_PATH_LEN,&dir);
445:     if (!dir) return(0);
446:     found = buf;
447:   } else {
448:     found = (char*)libname;
449:   }
450:   PetscStrcpy(suffix,".");
451:   PetscStrcat(suffix,PETSC_SLSUFFIX);

453:   PetscTokenCreate(found,'\n',&token);
454:   PetscTokenFind(token,&libname1);
455:   PetscStrstr(libname1,suffix,&s);
456:   if (s) s[0] = 0;
457:   while (libname1) {

459:     /* see if library was already open then we are done */
460:     list  = prev = *outlist;
461:     match = PETSC_FALSE;
462:     while (list) {

464:       PetscStrcmp(list->libname,libname1,&match);
465:       if (match) break;
466:       prev = list;
467:       list = list->next;
468:     }
469:     if (!match) {

471:       PetscDLLibraryOpen(comm,libname1,&handle);

473:       PetscNew(struct _n_PetscDLLibrary,&list);
474:       list->next   = 0;
475:       list->handle = handle;
476:       PetscStrcpy(list->libname,libname1);

478:       if (!*outlist) {
479:         *outlist   = list;
480:       } else {
481:         prev->next = list;
482:       }
483:       PetscInfo1(0,"Appending %s to dynamic library search path\n",libname1);
484:     }
485:     PetscTokenFind(token,&libname1);
486:     if (libname1) {
487:       PetscStrstr(libname1,suffix,&s);
488:       if (s) s[0] = 0;
489:     }
490:   }
491:   PetscTokenDestroy(token);
492:   return(0);
493: }

497: /*@C
498:      PetscDLLibraryPrepend - Add another dynamic library to search for symbols to the beginning of
499:                  the search path.

501:      Collective on MPI_Comm

503:      Input Parameters:
504: +     comm - MPI communicator
505: -     libname - name of the library

507:      Output Parameter:
508: .     outlist - list of libraries

510:      Level: developer

512:      Notes: If library is already in path will remove old reference.

514: @*/
515: PetscErrorCode  PetscDLLibraryPrepend(MPI_Comm comm,PetscDLLibrary *outlist,const char libname[])
516: {
517:   PetscDLLibrary list,prev;
518:   void*          handle;
520:   size_t         len;
521:   PetscTruth     match,dir;
522:   char           program[PETSC_MAX_PATH_LEN],buf[8*PETSC_MAX_PATH_LEN],*found,*libname1,suffix[16],*s;
523:   PetscToken     token;

526: 
527:   /* is libname a directory? */
528:   PetscTestDirectory(libname,'r',&dir);
529:   if (dir) {
530:     PetscInfo1(0,"Checking directory %s for dynamic libraries\n",libname);
531:     PetscStrcpy(program,libname);
532:     PetscStrlen(program,&len);
533:     if (program[len-1] == '/') {
534:       PetscStrcat(program,"*.");
535:     } else {
536:       PetscStrcat(program,"/*.");
537:     }
538:     PetscStrcat(program,PETSC_SLSUFFIX);

540:     PetscLs(comm,program,buf,8*PETSC_MAX_PATH_LEN,&dir);
541:     if (!dir) return(0);
542:     found = buf;
543:   } else {
544:     found = (char*)libname;
545:   }

547:   PetscStrcpy(suffix,".");
548:   PetscStrcat(suffix,PETSC_SLSUFFIX);

550:   PetscTokenCreate(found,'\n',&token);
551:   PetscTokenFind(token,&libname1);
552:   PetscStrstr(libname1,suffix,&s);
553:   if (s) s[0] = 0;
554:   while (libname1) {
555:     /* see if library was already open and move it to the front */
556:     list  = *outlist;
557:     prev  = 0;
558:     match = PETSC_FALSE;
559:     while (list) {

561:       PetscStrcmp(list->libname,libname1,&match);
562:       if (match) {
563:         if (prev) prev->next = list->next;
564:         list->next = *outlist;
565:         *outlist   = list;
566:         break;
567:       }
568:       prev = list;
569:       list = list->next;
570:     }
571:     if (!match) {
572:       /* open the library and add to front of list */
573:       PetscDLLibraryOpen(comm,libname1,&handle);
574: 
575:       PetscInfo1(0,"Prepending %s to dynamic library search path\n",libname1);

577:       PetscNew(struct _n_PetscDLLibrary,&list);
578:       list->handle = handle;
579:       list->next   = *outlist;
580:       PetscStrcpy(list->libname,libname1);
581:       *outlist     = list;
582:     }
583:     PetscTokenFind(token,&libname1);
584:     if (libname1) {
585:       PetscStrstr(libname1,suffix,&s);
586:       if (s) s[0] = 0;
587:     }
588:   }
589:   PetscTokenDestroy(token);
590:   return(0);
591: }

595: /*@C
596:      PetscDLLibraryClose - Destroys the search path of dynamic libraries and closes the libraries.

598:     Collective on PetscDLLibrary

600:     Input Parameter:
601: .     next - library list

603:      Level: developer

605: @*/
606: PetscErrorCode  PetscDLLibraryClose(PetscDLLibrary next)
607: {
608:   PetscDLLibrary prev;

612:   while (next) {
613:     prev = next;
614:     next = next->next;
615:     /* free the space in the prev data-structure */
616:     PetscFree(prev);
617:   }
618:   return(0);
619: }

623: /*@C
624:      PetscDLLibraryCCAAppend - Appends another CCA dynamic link library to the seach list, to the end
625:                 of the search path.

627:      Collective on MPI_Comm

629:      Input Parameters:
630: +     comm - MPI communicator
631: -     libname - name of directory to check

633:      Output Parameter:
634: .     outlist - list of libraries

636:      Level: developer

638:      Notes: if library is already in path will not add it.
639: @*/
640: PetscErrorCode  PetscDLLibraryCCAAppend(MPI_Comm comm,PetscDLLibrary *outlist,const char dirname[])
641: {
643:   size_t         l;
644:   PetscTruth     dir;
645:   char           program[PETSC_MAX_PATH_LEN],buf[8*PETSC_MAX_PATH_LEN],*libname1,fbuf[PETSC_MAX_PATH_LEN],*found,suffix[16],*f2;
646:   char           *func,*funcname,libname[PETSC_MAX_PATH_LEN],*lib;
647:   FILE           *fp;
648:   PetscToken     token1, token2;
649:   int            err;

652:   /* is dirname a directory? */
653:   PetscTestDirectory(dirname,'r',&dir);
654:   if (!dir) return(0);

656:   PetscInfo1(0,"Checking directory %s for CCA components\n",dirname);
657:   PetscStrcpy(program,dirname);
658:   PetscStrcat(program,"/*.cca");

660:   PetscLs(comm,program,buf,8*PETSC_MAX_PATH_LEN,&dir);
661:   if (!dir) return(0);

663:   PetscStrcpy(suffix,".");
664:   PetscStrcat(suffix,PETSC_SLSUFFIX);
665:   PetscTokenCreate(buf,'\n',&token1);
666:   PetscTokenFind(token1,&libname1);
667:   while (libname1) {
668:     fp    = fopen(libname1,"r"); if (!fp) continue;
669:     while ((found = fgets(fbuf,PETSC_MAX_PATH_LEN,fp))) {
670:       if (found[0] == '!') continue;
671:       PetscStrstr(found,suffix,&f2);
672:       if (f2) { /* found library name */
673:         if (found[0] == '/') {
674:           lib = found;
675:         } else {
676:           PetscStrcpy(libname,dirname);
677:           PetscStrlen(libname,&l);
678:           if (libname[l-1] != '/') {PetscStrcat(libname,"/");}
679:           PetscStrcat(libname,found);
680:           lib  = libname;
681:         }
682:         PetscDLLibraryAppend(comm,outlist,lib);
683:       } else {
684:         PetscInfo2(0,"CCA Component function and name: %s from %s\n",found,libname1);
685:         PetscTokenCreate(found,' ',&token2);
686:         PetscTokenFind(token2,&func);
687:         PetscTokenFind(token2,&funcname);
688:         PetscFListAdd(&CCAList,funcname,func,PETSC_NULL);
689:         PetscTokenDestroy(token2);
690:       }
691:     }
692:     err = fclose(fp);
693:     if (err) SETERRQ(PETSC_ERR_SYS,"fclose() failed on file");
694:     PetscTokenFind(token1,&libname1);
695:   }
696:   PetscTokenDestroy(token1);
697:   return(0);
698: }


701: #endif