Skip to content

Commit 2f49aee

Browse files
committed
Add error logging
1 parent 1d6f67a commit 2f49aee

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

main.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,21 @@ var libraryPID int = -1
204204

205205
func getDynamicLibrary() unsafe.Pointer {
206206
if libraryHandle == nil || libraryPID == -1 || libraryPID != os.Getpid() {
207+
fmt.Println("Fork detected. Reloading submodule.")
207208
if libraryHandle != nil {
209+
C.dlerror()
208210
C.dlclose(libraryHandle)
211+
dlerr := C.dlerror()
212+
if dlerr != nil {
213+
fmt.Printf("Error when closing dynamic library.\n", dlerr)
214+
}
215+
}
216+
C.dlerror()
217+
libraryHandle = C.dlmopen(C.LM_ID_NEWLM, C.CString(os.Getenv("PKCS11_SUBMODULE")), C.RTLD_NOW|C.RTLD_LOCAL|C.RTLD_DEEPBIND)
218+
dlerr := C.dlerror()
219+
if dlerr != nil {
220+
fmt.Printf("Error when opening dynamic library.\n", dlerr)
209221
}
210-
libraryHandle = C.dlmopen(C.LM_ID_NEWLM, C.CString(os.Getenv("PKCS11_SUBMODULE")), C.RTLD_LAZY)
211222
if libraryHandle == nil {
212223
return nil
213224
}
@@ -221,7 +232,12 @@ func getDynamicLibrarySymbol(functionName string) uintptr {
221232
if lh == nil {
222233
return 0
223234
}
224-
return uintptr(C.dlsym(lh, C.CString(functionName)))
235+
C.dlerror()
236+
symbol := C.dlsym(lh, C.CString(functionName))
237+
if dlerr != nil {
238+
fmt.Printf("Error when getting symbol from dynamic library.\n", dlerr)
239+
}
240+
return uintptr(symbol)
225241
}
226242

227243
//export C_CancelFunction

0 commit comments

Comments
 (0)