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 <petscsys.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 = PetscMalloc(len*sizeof(char),&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)(const char*) = NULL; 139 size_t len; 140 141 PetscFunctionBegin; 142 PetscValidCharPointer(path,2); 143 PetscValidPointer(entry,3); 144 145 *entry = PETSC_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) { 180 basename = basename + 3; 181 } else { 182 ierr = PetscInfo1(0,"Dynamic library %s does not have lib prefix\n",libname);CHKERRQ(ierr); 183 } 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)(libname);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(struct _n_PetscDLLibrary,entry);CHKERRQ(ierr); 196 (*entry)->next = 0; 197 (*entry)->handle = handle; 198 ierr = PetscStrcpy((*entry)->libname,libname);CHKERRQ(ierr); 199 200 PetscFunctionReturn(0); 201 } 202 203 #undef __FUNCT__ 204 #define __FUNCT__ "PetscDLLibrarySym" 205 /*@C 206 PetscDLLibrarySym - Load a symbol from the dynamic link libraries. 207 208 Collective on MPI_Comm 209 210 Input Parameter: 211 + comm - communicator that will open the library 212 . outlist - list of already open libraries that may contain symbol (can be PETSC_NULL and only the executable is searched for the function) 213 . path - optional complete library name (if provided checks here before checking outlist) 214 - insymbol - name of symbol 215 216 Output Parameter: 217 . value - if symbol not found then this value is set to PETSC_NULL 218 219 Level: developer 220 221 Notes: Symbol can be of the form 222 [/path/libname[.so.1.0]:]functionname[()] where items in [] denote optional 223 224 Will attempt to (retrieve and) open the library if it is not yet been opened. 225 226 @*/ 227 PetscErrorCode PetscDLLibrarySym(MPI_Comm comm,PetscDLLibrary *outlist,const char path[],const char insymbol[],void **value) 228 { 229 char libname[PETSC_MAX_PATH_LEN],suffix[16],*symbol,*s; 230 PetscDLLibrary nlist,prev,list = PETSC_NULL; 231 PetscErrorCode ierr; 232 233 PetscFunctionBegin; 234 if (outlist) PetscValidPointer(outlist,2); 235 if (path) PetscValidCharPointer(path,3); 236 PetscValidCharPointer(insymbol,4); 237 PetscValidPointer(value,5); 238 239 if (outlist) list = *outlist; 240 *value = 0; 241 242 243 ierr = PetscStrchr(insymbol,'(',&s);CHKERRQ(ierr); 244 if (s) { 245 /* make copy of symbol so we can edit it in place */ 246 ierr = PetscStrallocpy(insymbol,&symbol);CHKERRQ(ierr); 247 /* If symbol contains () then replace with a NULL, to support functionname() */ 248 ierr = PetscStrchr(symbol,'(',&s);CHKERRQ(ierr); 249 s[0] = 0; 250 } else { 251 symbol = (char *)insymbol; 252 } 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 = PetscStrcpy(suffix,".");CHKERRQ(ierr); 262 ierr = PetscStrcat(suffix,PETSC_SLSUFFIX);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 = 0; 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(0,"Appending %s to dynamic library search path\n",path);CHKERRQ(ierr); 278 if (prev) { prev->next = nlist; } 279 else { *outlist = nlist; } 280 281 done:; 282 ierr = PetscDLSym(nlist->handle,symbol,value);CHKERRQ(ierr); 283 if (*value) { 284 ierr = PetscInfo2(0,"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(0,"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(PETSC_NULL,symbol,value);CHKERRQ(ierr); 302 if (*value) { 303 ierr = PetscInfo1(0,"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 #undef __FUNCT__ 315 #define __FUNCT__ "PetscDLLibraryAppend" 316 /*@C 317 PetscDLLibraryAppend - Appends another dynamic link library to the seach list, to the end 318 of the search path. 319 320 Collective on MPI_Comm 321 322 Input Parameters: 323 + comm - MPI communicator 324 - path - name of the library 325 326 Output Parameter: 327 . outlist - list of libraries 328 329 Level: developer 330 331 Notes: if library is already in path will not add it. 332 333 If the library has the symbol PetscDLLibraryRegister_basename() in it then that function is automatically run 334 when the library is opened. 335 336 .seealso: PetscDLLibraryOpen() 337 @*/ 338 PetscErrorCode PetscDLLibraryAppend(MPI_Comm comm,PetscDLLibrary *outlist,const char path[]) 339 { 340 PetscDLLibrary list,prev; 341 PetscErrorCode ierr; 342 size_t len; 343 PetscBool match,dir; 344 char program[PETSC_MAX_PATH_LEN],found[8*PETSC_MAX_PATH_LEN]; 345 char *libname,suffix[16],*s; 346 PetscToken token; 347 348 PetscFunctionBegin; 349 PetscValidPointer(outlist,2); 350 351 /* is path a directory? */ 352 ierr = PetscTestDirectory(path,'r',&dir);CHKERRQ(ierr); 353 if (dir) { 354 ierr = PetscInfo1(0,"Checking directory %s for dynamic libraries\n",path);CHKERRQ(ierr); 355 ierr = PetscStrcpy(program,path);CHKERRQ(ierr); 356 ierr = PetscStrlen(program,&len);CHKERRQ(ierr); 357 if (program[len-1] == '/') { 358 ierr = PetscStrcat(program,"*.");CHKERRQ(ierr); 359 } else { 360 ierr = PetscStrcat(program,"/*.");CHKERRQ(ierr); 361 } 362 ierr = PetscStrcat(program,PETSC_SLSUFFIX);CHKERRQ(ierr); 363 364 ierr = PetscLs(comm,program,found,8*PETSC_MAX_PATH_LEN,&dir);CHKERRQ(ierr); 365 if (!dir) PetscFunctionReturn(0); 366 } else { 367 ierr = PetscStrncpy(found,path,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 368 } 369 ierr = PetscStrcpy(suffix,".");CHKERRQ(ierr); 370 ierr = PetscStrcat(suffix,PETSC_SLSUFFIX);CHKERRQ(ierr); 371 372 ierr = PetscTokenCreate(found,'\n',&token);CHKERRQ(ierr); 373 ierr = PetscTokenFind(token,&libname);CHKERRQ(ierr); 374 while (libname) { 375 /* remove suffix from libname */ 376 ierr = PetscStrrstr(libname,suffix,&s);CHKERRQ(ierr); 377 if (s) s[0] = 0; 378 /* see if library was already open then we are done */ 379 list = prev = *outlist; 380 match = PETSC_FALSE; 381 while (list) { 382 ierr = PetscStrcmp(list->libname,libname,&match);CHKERRQ(ierr); 383 if (match) break; 384 prev = list; 385 list = list->next; 386 } 387 /* restore suffix from libname */ 388 if (s) s[0] = '.'; 389 if (!match) { 390 /* open the library and add to end of list */ 391 ierr = PetscDLLibraryOpen(comm,libname,&list);CHKERRQ(ierr); 392 ierr = PetscInfo1(0,"Appending %s to dynamic library search path\n",libname);CHKERRQ(ierr); 393 if (!*outlist) { 394 *outlist = list; 395 } else { 396 prev->next = list; 397 } 398 } 399 ierr = PetscTokenFind(token,&libname);CHKERRQ(ierr); 400 } 401 ierr = PetscTokenDestroy(&token);CHKERRQ(ierr); 402 PetscFunctionReturn(0); 403 } 404 405 #undef __FUNCT__ 406 #define __FUNCT__ "PetscDLLibraryPrepend" 407 /*@C 408 PetscDLLibraryPrepend - Add another dynamic library to search for symbols to the beginning of 409 the search path. 410 411 Collective on MPI_Comm 412 413 Input Parameters: 414 + comm - MPI communicator 415 - path - name of the library 416 417 Output Parameter: 418 . outlist - list of libraries 419 420 Level: developer 421 422 Notes: If library is already in path will remove old reference. 423 424 @*/ 425 PetscErrorCode PetscDLLibraryPrepend(MPI_Comm comm,PetscDLLibrary *outlist,const char path[]) 426 { 427 PetscDLLibrary list,prev; 428 PetscErrorCode ierr; 429 size_t len; 430 PetscBool match,dir; 431 char program[PETSC_MAX_PATH_LEN],found[8*PETSC_MAX_PATH_LEN]; 432 char *libname,suffix[16],*s; 433 PetscToken token; 434 435 PetscFunctionBegin; 436 PetscValidPointer(outlist,2); 437 438 /* is path a directory? */ 439 ierr = PetscTestDirectory(path,'r',&dir);CHKERRQ(ierr); 440 if (dir) { 441 ierr = PetscInfo1(0,"Checking directory %s for dynamic libraries\n",path);CHKERRQ(ierr); 442 ierr = PetscStrcpy(program,path);CHKERRQ(ierr); 443 ierr = PetscStrlen(program,&len);CHKERRQ(ierr); 444 if (program[len-1] == '/') { 445 ierr = PetscStrcat(program,"*.");CHKERRQ(ierr); 446 } else { 447 ierr = PetscStrcat(program,"/*.");CHKERRQ(ierr); 448 } 449 ierr = PetscStrcat(program,PETSC_SLSUFFIX);CHKERRQ(ierr); 450 451 ierr = PetscLs(comm,program,found,8*PETSC_MAX_PATH_LEN,&dir);CHKERRQ(ierr); 452 if (!dir) PetscFunctionReturn(0); 453 } else { 454 ierr = PetscStrncpy(found,path,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 455 } 456 457 ierr = PetscStrcpy(suffix,".");CHKERRQ(ierr); 458 ierr = PetscStrcat(suffix,PETSC_SLSUFFIX);CHKERRQ(ierr); 459 460 ierr = PetscTokenCreate(found,'\n',&token);CHKERRQ(ierr); 461 ierr = PetscTokenFind(token,&libname);CHKERRQ(ierr); 462 while (libname) { 463 /* remove suffix from libname */ 464 ierr = PetscStrstr(libname,suffix,&s);CHKERRQ(ierr); 465 if (s) s[0] = 0; 466 /* see if library was already open and move it to the front */ 467 prev = 0; 468 list = *outlist; 469 match = PETSC_FALSE; 470 while (list) { 471 ierr = PetscStrcmp(list->libname,libname,&match);CHKERRQ(ierr); 472 if (match) { 473 ierr = PetscInfo1(0,"Moving %s to begin of dynamic library search path\n",libname);CHKERRQ(ierr); 474 if (prev) prev->next = list->next; 475 if (prev) list->next = *outlist; 476 *outlist = list; 477 break; 478 } 479 prev = list; 480 list = list->next; 481 } 482 /* restore suffix from libname */ 483 if (s) s[0] = '.'; 484 if (!match) { 485 /* open the library and add to front of list */ 486 ierr = PetscDLLibraryOpen(comm,libname,&list);CHKERRQ(ierr); 487 ierr = PetscInfo1(0,"Prepending %s to dynamic library search path\n",libname);CHKERRQ(ierr); 488 list->next = *outlist; 489 *outlist = list; 490 } 491 ierr = PetscTokenFind(token,&libname);CHKERRQ(ierr); 492 } 493 ierr = PetscTokenDestroy(&token);CHKERRQ(ierr); 494 PetscFunctionReturn(0); 495 } 496 497 #undef __FUNCT__ 498 #define __FUNCT__ "PetscDLLibraryClose" 499 /*@C 500 PetscDLLibraryClose - Destroys the search path of dynamic libraries and closes the libraries. 501 502 Collective on PetscDLLibrary 503 504 Input Parameter: 505 . head - library list 506 507 Level: developer 508 509 @*/ 510 PetscErrorCode PetscDLLibraryClose(PetscDLLibrary list) 511 { 512 PetscBool done = PETSC_FALSE; 513 PetscDLLibrary prev,tail; 514 PetscErrorCode ierr; 515 516 PetscFunctionBegin; 517 if (!list) PetscFunctionReturn(0); 518 /* traverse the list in reverse order */ 519 while (!done) { 520 if (!list->next) done = PETSC_TRUE; 521 prev = tail = list; 522 while (tail->next) { 523 prev = tail; 524 tail = tail->next; 525 } 526 prev->next = 0; 527 /* close the dynamic library and free the space in entry data-structure*/ 528 ierr = PetscInfo1(0,"Closing dynamic library %s\n",tail->libname);CHKERRQ(ierr); 529 ierr = PetscDLClose(&tail->handle);CHKERRQ(ierr); 530 ierr = PetscFree(tail);CHKERRQ(ierr); 531 }; 532 PetscFunctionReturn(0); 533 } 534 535