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