]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Unload shared libraries when a database connection closes. (CVS 3208)
authordrh <drh@noemail.net>
Thu, 8 Jun 2006 15:48:00 +0000 (15:48 +0000)
committerdrh <drh@noemail.net>
Thu, 8 Jun 2006 15:48:00 +0000 (15:48 +0000)
FossilOrigin-Name: 327e6909c9d35b651ab6f3a1a270022b354538c6

manifest
manifest.uuid
src/loadext.c
src/main.c
src/sqliteInt.h

index 1d607c64819ba1532d60f84bdaec8db104f6680e..2faa123ee1a9d7ad4115789d7f393eee232db879 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C New\sshell\scommand\s".load"\sand\sthe\ssqlite3_load_extension()\sAPI\sallow\nnew\sSQL\sfunctions\sand\scollating\ssequences\sto\sbe\sloaded\sat\srun-time\sfrom\na\sDLL\sor\sshared\slibrary.\s(CVS\s3207)
-D 2006-06-08T15:28:44
+C Unload\sshared\slibraries\swhen\sa\sdatabase\sconnection\scloses.\s(CVS\s3208)
+D 2006-06-08T15:48:01
 F Makefile.in 50d948a8c4eda30ebb5799b661bd4c2de11824d0
 F Makefile.linux-gcc 74ba0eadf88748a9ce3fd03d2a3ede2e6715baec
 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@@ -48,8 +48,8 @@ F src/hash.c 449f3d6620193aa557f5d86cbc5cc6b87702b185
 F src/hash.h 1b3f7e2609141fd571f62199fc38687d262e9564
 F src/insert.c 1ae4b8ff5549497808e1b57b9243abcb599fd02f
 F src/legacy.c fa15d505dd4e45044177ee4d1c6aeaf8c836d390
-F src/loadext.c 71405a8f9fedc0c21b63bbe24a0fb52081caf4a5
-F src/main.c 928d93cfd5d72be3a619ee908182c9432151a99e
+F src/loadext.c 528a3c130ca32b83609593605ebeec235de4e55b
+F src/main.c 0147dbf7ba04563749aef77ef709b78dd86d6771
 F src/md5.c c5fdfa5c2593eaee2e32a5ce6c6927c986eaf217
 F src/os.c 59f05de8c5777c34876607114a2fbe55ae578235
 F src/os.h 46fad85c707ad8643622bab9d894a642940850aa
@@ -74,7 +74,7 @@ F src/server.c 087b92a39d883e3fa113cae259d64e4c7438bc96
 F src/shell.c 4f1e4a4d3e7aadd1369604a30293fc3e1726c78c
 F src/sqlite.h.in d33c4688ba292af5f84fea49b2e3946b9129673a
 F src/sqlite3ext.h 127bd394c8eea481f2ac9b754bf399dbfc818b75
-F src/sqliteInt.h 98b3d7e9c4c48fd128d03d5788ca8fae0994280a
+F src/sqliteInt.h 029ae294180139e3ead077b548dd9e08f99010aa
 F src/table.c f64ec4fbfe333f8df925bc6ba494f55e05b0e75e
 F src/tclsqlite.c 5ae9f08f7af7fe80d38fbccc4f5359f272643af1
 F src/test1.c becd9202b733debc607b5aec43002769730e1f71
@@ -360,7 +360,7 @@ F www/tclsqlite.tcl bb0d1357328a42b1993d78573e587c6dcbc964b9
 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
 F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b
 F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513
-P c11cb07e4b3f0b815a7099c8d201b3473869cba2
-R 46a5b7e0c262c56ce770de14a1643ea8
+P 4ca932d3ae9bb97b819b5baf6fd3e1cebda9e0e2
+R 0158de9b8b4c84dbf29d65b8d8634e42
 U drh
-Z edb9ada8988bcadffc5de5550d7a256a
+Z 1d4147161dab15313c86f0f2346a379f
index e243a6b8dc5f88055d6035b85a52b1a3b2dd214f..78ac6191220397859cf2a9d3a9a8f1fa00d4462a 100644 (file)
@@ -1 +1 @@
-4ca932d3ae9bb97b819b5baf6fd3e1cebda9e0e2
\ No newline at end of file
+327e6909c9d35b651ab6f3a1a270022b354538c6
\ No newline at end of file
index 799b878d3aa894b91c2f79e9a97522fe2d1b2c5b..b0199b5c34a588fa7ac9f357a10e76756dbf2295 100644 (file)
@@ -16,6 +16,7 @@
 
 #define SQLITE_CORE 1  /* Disable the API redefinition in sqlite3ext.h */
 #include "sqlite3ext.h"
+#include "sqliteInt.h"
 #include <string.h>
 #include <ctype.h>
 
@@ -147,8 +148,7 @@ const sqlite3_api_routines sqlite3_api = {
 # define SQLITE_LIBRARY_TYPE     HANDLE
 # define SQLITE_OPEN_LIBRARY(A)  LoadLibrary(A)
 # define SQLITE_FIND_SYMBOL(A,B) GetProcAddress(A,B)
-# define SQLITE_LIBRARY_ERROR(A) FreeLibrary(A)
-# define SQLITE_CLOSE_LIBRARY(A) 
+# define SQLITE_CLOSE_LIBRARY(A) FreeLibrary(A)
 #endif /* windows */
 
 /*
@@ -159,8 +159,7 @@ const sqlite3_api_routines sqlite3_api = {
 # define SQLITE_LIBRARY_TYPE     void*
 # define SQLITE_OPEN_LIBRARY(A)  dlopen(A, RTLD_NOW | RTLD_GLOBAL)
 # define SQLITE_FIND_SYMBOL(A,B) dlsym(A,B)
-# define SQLITE_LIBRARY_ERROR(A) dlclose(A)
-# define SQLITE_CLOSE_LIBRARY(A)
+# define SQLITE_CLOSE_LIBRARY(A) dlclose(A)
 #endif
 
 /*
@@ -202,7 +201,18 @@ int sqlite3_load_extension(
   SQLITE_LIBRARY_TYPE handle;
   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
   char *zErrmsg = 0;
+  SQLITE_LIBRARY_TYPE *aHandle;
 
+  db->nExtension++;
+  aHandle = sqliteMalloc(sizeof(handle)*db->nExtension);
+  if( aHandle==0 ){
+    return SQLITE_NOMEM;
+  }
+  if( db->nExtension>0 ){
+    memcpy(aHandle, db->aExtension, sizeof(handle)*(db->nExtension-1));
+  }
+  sqliteFree(db->aExtension);
+  db->aExtension = aHandle;
   if( zProc==0 ){
     int i, j, n;
     char *z;
@@ -244,17 +254,17 @@ int sqlite3_load_extension(
        *pzErrMsg = sqlite3_mprintf("no entry point [%s] in shared library [%s]",
                                    zProc, zFile);
     }
-    SQLITE_LIBRARY_ERROR(handle);
+    SQLITE_CLOSE_LIBRARY(handle);
     return SQLITE_ERROR;
   }else if( xInit(db, &zErrmsg, &sqlite3_api) ){
     if( pzErrMsg ){
       *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
     }
     sqlite3_free(zErrmsg);
-    SQLITE_LIBRARY_ERROR(handle);
+    SQLITE_CLOSE_LIBRARY(handle);
     return SQLITE_ERROR;
   }
-  SQLITE_CLOSE_LIBRARY(handle);
+  ((SQLITE_LIBRARY_TYPE*)db->aExtension)[db->nExtension-1] = handle;
   return SQLITE_OK;
 #else
   if( pzErrMsg ){
@@ -264,4 +274,17 @@ int sqlite3_load_extension(
   return SQLITE_ERROR;
 #endif
 }
+
+/*
+** Call this routine when the database connection is closing in order
+** to clean up loaded extensions
+*/
+void sqlite3CloseExtensions(sqlite3 *db){
+  int i;
+  for(i=0; i<db->nExtension; i++){
+    SQLITE_CLOSE_LIBRARY(((SQLITE_LIBRARY_TYPE*)db->aExtension)[i]);
+  }
+  sqliteFree(db->aExtension);
+}
+
 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
index 4a13cbb0e11771a459226a6a11812b10570219da..9d6cbd9713e923c5b1e0c7b0e929f63e24920d76 100644 (file)
@@ -14,7 +14,7 @@
 ** other files are for internal use by SQLite and should not be
 ** accessed by users of the library.
 **
-** $Id: main.c,v 1.340 2006/05/24 12:43:27 drh Exp $
+** $Id: main.c,v 1.341 2006/06/08 15:48:01 drh Exp $
 */
 #include "sqliteInt.h"
 #include "os.h"
@@ -165,6 +165,7 @@ int sqlite3_close(sqlite3 *db){
   if( db->pErr ){
     sqlite3ValueFree(db->pErr);
   }
+  sqlite3CloseExtensions(db);
 
   db->magic = SQLITE_MAGIC_ERROR;
 
index 80d4a19d6c664d2bca53d14ff3169e1f38cc2c11..44210e3604bcb4ba6c96824595e14f00d9457f59 100644 (file)
@@ -11,7 +11,7 @@
 *************************************************************************
 ** Internal interface definitions for SQLite.
 **
-** @(#) $Id: sqliteInt.h,v 1.494 2006/05/25 12:17:31 drh Exp $
+** @(#) $Id: sqliteInt.h,v 1.495 2006/06/08 15:48:01 drh Exp $
 */
 #ifndef _SQLITEINT_H_
 #define _SQLITEINT_H_
@@ -460,6 +460,8 @@ struct sqlite3 {
     int newTnum;                /* Rootpage of table being initialized */
     u8 busy;                    /* TRUE if currently initializing */
   } init;
+  int nExtension;               /* Number of loaded extensions */
+  void *aExtension;             /* Array of shared libraray handles */
   struct Vdbe *pVdbe;           /* List of active virtual machines */
   int activeVdbeCnt;            /* Number of vdbes currently executing */
   void (*xTrace)(void*,const char*);        /* Trace function */
@@ -1740,6 +1742,7 @@ int sqlite3MallocFailed(void);
 void sqlite3FailedMalloc(void);
 void sqlite3AbortOtherActiveVdbes(sqlite3 *, Vdbe *);
 int sqlite3OpenTempDatabase(Parse *);
+void sqlite3CloseExtensions(sqlite3*);
 
 #ifndef SQLITE_OMIT_SHARED_CACHE
   void sqlite3TableLock(Parse *, int, int, u8, const char *);