loadext.c

Go to the documentation of this file.
00001 /*
00002 ** 2006 June 7
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 *************************************************************************
00012 ** This file contains code used to dynamically load extensions into
00013 ** the SQLite library.
00014 **
00015 ** $Id: loadext.c,v 1.56 2008/10/12 00:27:53 shane Exp $
00016 */
00017 
00018 #ifndef SQLITE_CORE
00019   #define SQLITE_CORE 1  /* Disable the API redefinition in sqlite3ext.h */
00020 #endif
00021 #include "sqlite3ext.h"
00022 #include "sqliteInt.h"
00023 #include <string.h>
00024 #include <ctype.h>
00025 
00026 #ifndef SQLITE_OMIT_LOAD_EXTENSION
00027 
00028 /*
00029 ** Some API routines are omitted when various features are
00030 ** excluded from a build of SQLite.  Substitute a NULL pointer
00031 ** for any missing APIs.
00032 */
00033 #ifndef SQLITE_ENABLE_COLUMN_METADATA
00034 # define sqlite3_column_database_name   0
00035 # define sqlite3_column_database_name16 0
00036 # define sqlite3_column_table_name      0
00037 # define sqlite3_column_table_name16    0
00038 # define sqlite3_column_origin_name     0
00039 # define sqlite3_column_origin_name16   0
00040 # define sqlite3_table_column_metadata  0
00041 #endif
00042 
00043 #ifdef SQLITE_OMIT_AUTHORIZATION
00044 # define sqlite3_set_authorizer         0
00045 #endif
00046 
00047 #ifdef SQLITE_OMIT_UTF16
00048 # define sqlite3_bind_text16            0
00049 # define sqlite3_collation_needed16     0
00050 # define sqlite3_column_decltype16      0
00051 # define sqlite3_column_name16          0
00052 # define sqlite3_column_text16          0
00053 # define sqlite3_complete16             0
00054 # define sqlite3_create_collation16     0
00055 # define sqlite3_create_function16      0
00056 # define sqlite3_errmsg16               0
00057 # define sqlite3_open16                 0
00058 # define sqlite3_prepare16              0
00059 # define sqlite3_prepare16_v2           0
00060 # define sqlite3_result_error16         0
00061 # define sqlite3_result_text16          0
00062 # define sqlite3_result_text16be        0
00063 # define sqlite3_result_text16le        0
00064 # define sqlite3_value_text16           0
00065 # define sqlite3_value_text16be         0
00066 # define sqlite3_value_text16le         0
00067 # define sqlite3_column_database_name16 0
00068 # define sqlite3_column_table_name16    0
00069 # define sqlite3_column_origin_name16   0
00070 #endif
00071 
00072 #ifdef SQLITE_OMIT_COMPLETE
00073 # define sqlite3_complete 0
00074 # define sqlite3_complete16 0
00075 #endif
00076 
00077 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
00078 # define sqlite3_progress_handler 0
00079 #endif
00080 
00081 #ifdef SQLITE_OMIT_VIRTUALTABLE
00082 # define sqlite3_create_module 0
00083 # define sqlite3_create_module_v2 0
00084 # define sqlite3_declare_vtab 0
00085 #endif
00086 
00087 #ifdef SQLITE_OMIT_SHARED_CACHE
00088 # define sqlite3_enable_shared_cache 0
00089 #endif
00090 
00091 #ifdef SQLITE_OMIT_TRACE
00092 # define sqlite3_profile       0
00093 # define sqlite3_trace         0
00094 #endif
00095 
00096 #ifdef SQLITE_OMIT_GET_TABLE
00097 # define sqlite3_free_table    0
00098 # define sqlite3_get_table     0
00099 #endif
00100 
00101 #ifdef SQLITE_OMIT_INCRBLOB
00102 #define sqlite3_bind_zeroblob  0
00103 #define sqlite3_blob_bytes     0
00104 #define sqlite3_blob_close     0
00105 #define sqlite3_blob_open      0
00106 #define sqlite3_blob_read      0
00107 #define sqlite3_blob_write     0
00108 #endif
00109 
00110 /*
00111 ** The following structure contains pointers to all SQLite API routines.
00112 ** A pointer to this structure is passed into extensions when they are
00113 ** loaded so that the extension can make calls back into the SQLite
00114 ** library.
00115 **
00116 ** When adding new APIs, add them to the bottom of this structure
00117 ** in order to preserve backwards compatibility.
00118 **
00119 ** Extensions that use newer APIs should first call the
00120 ** sqlite3_libversion_number() to make sure that the API they
00121 ** intend to use is supported by the library.  Extensions should
00122 ** also check to make sure that the pointer to the function is
00123 ** not NULL before calling it.
00124 */
00125 static const sqlite3_api_routines sqlite3Apis = {
00126   sqlite3_aggregate_context,
00127 #ifndef SQLITE_OMIT_DEPRECATED
00128   sqlite3_aggregate_count,
00129 #else
00130   0,
00131 #endif
00132   sqlite3_bind_blob,
00133   sqlite3_bind_double,
00134   sqlite3_bind_int,
00135   sqlite3_bind_int64,
00136   sqlite3_bind_null,
00137   sqlite3_bind_parameter_count,
00138   sqlite3_bind_parameter_index,
00139   sqlite3_bind_parameter_name,
00140   sqlite3_bind_text,
00141   sqlite3_bind_text16,
00142   sqlite3_bind_value,
00143   sqlite3_busy_handler,
00144   sqlite3_busy_timeout,
00145   sqlite3_changes,
00146   sqlite3_close,
00147   sqlite3_collation_needed,
00148   sqlite3_collation_needed16,
00149   sqlite3_column_blob,
00150   sqlite3_column_bytes,
00151   sqlite3_column_bytes16,
00152   sqlite3_column_count,
00153   sqlite3_column_database_name,
00154   sqlite3_column_database_name16,
00155   sqlite3_column_decltype,
00156   sqlite3_column_decltype16,
00157   sqlite3_column_double,
00158   sqlite3_column_int,
00159   sqlite3_column_int64,
00160   sqlite3_column_name,
00161   sqlite3_column_name16,
00162   sqlite3_column_origin_name,
00163   sqlite3_column_origin_name16,
00164   sqlite3_column_table_name,
00165   sqlite3_column_table_name16,
00166   sqlite3_column_text,
00167   sqlite3_column_text16,
00168   sqlite3_column_type,
00169   sqlite3_column_value,
00170   sqlite3_commit_hook,
00171   sqlite3_complete,
00172   sqlite3_complete16,
00173   sqlite3_create_collation,
00174   sqlite3_create_collation16,
00175   sqlite3_create_function,
00176   sqlite3_create_function16,
00177   sqlite3_create_module,
00178   sqlite3_data_count,
00179   sqlite3_db_handle,
00180   sqlite3_declare_vtab,
00181   sqlite3_enable_shared_cache,
00182   sqlite3_errcode,
00183   sqlite3_errmsg,
00184   sqlite3_errmsg16,
00185   sqlite3_exec,
00186 #ifndef SQLITE_OMIT_DEPRECATED
00187   sqlite3_expired,
00188 #else
00189   0,
00190 #endif
00191   sqlite3_finalize,
00192   sqlite3_free,
00193   sqlite3_free_table,
00194   sqlite3_get_autocommit,
00195   sqlite3_get_auxdata,
00196   sqlite3_get_table,
00197   0,     /* Was sqlite3_global_recover(), but that function is deprecated */
00198   sqlite3_interrupt,
00199   sqlite3_last_insert_rowid,
00200   sqlite3_libversion,
00201   sqlite3_libversion_number,
00202   sqlite3_malloc,
00203   sqlite3_mprintf,
00204   sqlite3_open,
00205   sqlite3_open16,
00206   sqlite3_prepare,
00207   sqlite3_prepare16,
00208   sqlite3_profile,
00209   sqlite3_progress_handler,
00210   sqlite3_realloc,
00211   sqlite3_reset,
00212   sqlite3_result_blob,
00213   sqlite3_result_double,
00214   sqlite3_result_error,
00215   sqlite3_result_error16,
00216   sqlite3_result_int,
00217   sqlite3_result_int64,
00218   sqlite3_result_null,
00219   sqlite3_result_text,
00220   sqlite3_result_text16,
00221   sqlite3_result_text16be,
00222   sqlite3_result_text16le,
00223   sqlite3_result_value,
00224   sqlite3_rollback_hook,
00225   sqlite3_set_authorizer,
00226   sqlite3_set_auxdata,
00227   sqlite3_snprintf,
00228   sqlite3_step,
00229   sqlite3_table_column_metadata,
00230 #ifndef SQLITE_OMIT_DEPRECATED
00231   sqlite3_thread_cleanup,
00232 #else
00233   0,
00234 #endif
00235   sqlite3_total_changes,
00236   sqlite3_trace,
00237 #ifndef SQLITE_OMIT_DEPRECATED
00238   sqlite3_transfer_bindings,
00239 #else
00240   0,
00241 #endif
00242   sqlite3_update_hook,
00243   sqlite3_user_data,
00244   sqlite3_value_blob,
00245   sqlite3_value_bytes,
00246   sqlite3_value_bytes16,
00247   sqlite3_value_double,
00248   sqlite3_value_int,
00249   sqlite3_value_int64,
00250   sqlite3_value_numeric_type,
00251   sqlite3_value_text,
00252   sqlite3_value_text16,
00253   sqlite3_value_text16be,
00254   sqlite3_value_text16le,
00255   sqlite3_value_type,
00256   sqlite3_vmprintf,
00257   /*
00258   ** The original API set ends here.  All extensions can call any
00259   ** of the APIs above provided that the pointer is not NULL.  But
00260   ** before calling APIs that follow, extension should check the
00261   ** sqlite3_libversion_number() to make sure they are dealing with
00262   ** a library that is new enough to support that API.
00263   *************************************************************************
00264   */
00265   sqlite3_overload_function,
00266 
00267   /*
00268   ** Added after 3.3.13
00269   */
00270   sqlite3_prepare_v2,
00271   sqlite3_prepare16_v2,
00272   sqlite3_clear_bindings,
00273 
00274   /*
00275   ** Added for 3.4.1
00276   */
00277   sqlite3_create_module_v2,
00278 
00279   /*
00280   ** Added for 3.5.0
00281   */
00282   sqlite3_bind_zeroblob,
00283   sqlite3_blob_bytes,
00284   sqlite3_blob_close,
00285   sqlite3_blob_open,
00286   sqlite3_blob_read,
00287   sqlite3_blob_write,
00288   sqlite3_create_collation_v2,
00289   sqlite3_file_control,
00290   sqlite3_memory_highwater,
00291   sqlite3_memory_used,
00292 #ifdef SQLITE_MUTEX_OMIT
00293   0, 
00294   0, 
00295   0,
00296   0,
00297   0,
00298 #else
00299   sqlite3_mutex_alloc,
00300   sqlite3_mutex_enter,
00301   sqlite3_mutex_free,
00302   sqlite3_mutex_leave,
00303   sqlite3_mutex_try,
00304 #endif
00305   sqlite3_open_v2,
00306   sqlite3_release_memory,
00307   sqlite3_result_error_nomem,
00308   sqlite3_result_error_toobig,
00309   sqlite3_sleep,
00310   sqlite3_soft_heap_limit,
00311   sqlite3_vfs_find,
00312   sqlite3_vfs_register,
00313   sqlite3_vfs_unregister,
00314 
00315   /*
00316   ** Added for 3.5.8
00317   */
00318   sqlite3_threadsafe,
00319   sqlite3_result_zeroblob,
00320   sqlite3_result_error_code,
00321   sqlite3_test_control,
00322   sqlite3_randomness,
00323   sqlite3_context_db_handle,
00324 
00325   /*
00326   ** Added for 3.6.0
00327   */
00328   sqlite3_extended_result_codes,
00329   sqlite3_limit,
00330   sqlite3_next_stmt,
00331   sqlite3_sql,
00332   sqlite3_status,
00333 };
00334 
00335 /*
00336 ** Attempt to load an SQLite extension library contained in the file
00337 ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
00338 ** default entry point name (sqlite3_extension_init) is used.  Use
00339 ** of the default name is recommended.
00340 **
00341 ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
00342 **
00343 ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with 
00344 ** error message text.  The calling function should free this memory
00345 ** by calling sqlite3DbFree(db, ).
00346 */
00347 static int sqlite3LoadExtension(
00348   sqlite3 *db,          /* Load the extension into this database connection */
00349   const char *zFile,    /* Name of the shared library containing extension */
00350   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
00351   char **pzErrMsg       /* Put error message here if not 0 */
00352 ){
00353   sqlite3_vfs *pVfs = db->pVfs;
00354   void *handle;
00355   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
00356   char *zErrmsg = 0;
00357   void **aHandle;
00358 
00359   /* Ticket #1863.  To avoid a creating security problems for older
00360   ** applications that relink against newer versions of SQLite, the
00361   ** ability to run load_extension is turned off by default.  One
00362   ** must call sqlite3_enable_load_extension() to turn on extension
00363   ** loading.  Otherwise you get the following error.
00364   */
00365   if( (db->flags & SQLITE_LoadExtension)==0 ){
00366     if( pzErrMsg ){
00367       *pzErrMsg = sqlite3_mprintf("not authorized");
00368     }
00369     return SQLITE_ERROR;
00370   }
00371 
00372   if( zProc==0 ){
00373     zProc = "sqlite3_extension_init";
00374   }
00375 
00376   handle = sqlite3OsDlOpen(pVfs, zFile);
00377   if( handle==0 ){
00378     if( pzErrMsg ){
00379       char zErr[256];
00380       zErr[sizeof(zErr)-1] = '\0';
00381       sqlite3_snprintf(sizeof(zErr)-1, zErr, 
00382           "unable to open shared library [%s]", zFile);
00383       sqlite3OsDlError(pVfs, sizeof(zErr)-1, zErr);
00384       *pzErrMsg = sqlite3DbStrDup(0, zErr);
00385     }
00386     return SQLITE_ERROR;
00387   }
00388   xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
00389                    sqlite3OsDlSym(pVfs, handle, zProc);
00390   if( xInit==0 ){
00391     if( pzErrMsg ){
00392       char zErr[256];
00393       zErr[sizeof(zErr)-1] = '\0';
00394       sqlite3_snprintf(sizeof(zErr)-1, zErr,
00395           "no entry point [%s] in shared library [%s]", zProc,zFile);
00396       sqlite3OsDlError(pVfs, sizeof(zErr)-1, zErr);
00397       *pzErrMsg = sqlite3DbStrDup(0, zErr);
00398       sqlite3OsDlClose(pVfs, handle);
00399     }
00400     return SQLITE_ERROR;
00401   }else if( xInit(db, &zErrmsg, &sqlite3Apis) ){
00402     if( pzErrMsg ){
00403       *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
00404     }
00405     sqlite3_free(zErrmsg);
00406     sqlite3OsDlClose(pVfs, handle);
00407     return SQLITE_ERROR;
00408   }
00409 
00410   /* Append the new shared library handle to the db->aExtension array. */
00411   aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
00412   if( aHandle==0 ){
00413     return SQLITE_NOMEM;
00414   }
00415   if( db->nExtension>0 ){
00416     memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
00417   }
00418   sqlite3DbFree(db, db->aExtension);
00419   db->aExtension = aHandle;
00420 
00421   db->aExtension[db->nExtension++] = handle;
00422   return SQLITE_OK;
00423 }
00424 int sqlite3_load_extension(
00425   sqlite3 *db,          /* Load the extension into this database connection */
00426   const char *zFile,    /* Name of the shared library containing extension */
00427   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
00428   char **pzErrMsg       /* Put error message here if not 0 */
00429 ){
00430   int rc;
00431   sqlite3_mutex_enter(db->mutex);
00432   rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
00433   sqlite3_mutex_leave(db->mutex);
00434   return rc;
00435 }
00436 
00437 /*
00438 ** Call this routine when the database connection is closing in order
00439 ** to clean up loaded extensions
00440 */
00441 void sqlite3CloseExtensions(sqlite3 *db){
00442   int i;
00443   assert( sqlite3_mutex_held(db->mutex) );
00444   for(i=0; i<db->nExtension; i++){
00445     sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
00446   }
00447   sqlite3DbFree(db, db->aExtension);
00448 }
00449 
00450 /*
00451 ** Enable or disable extension loading.  Extension loading is disabled by
00452 ** default so as not to open security holes in older applications.
00453 */
00454 int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
00455   sqlite3_mutex_enter(db->mutex);
00456   if( onoff ){
00457     db->flags |= SQLITE_LoadExtension;
00458   }else{
00459     db->flags &= ~SQLITE_LoadExtension;
00460   }
00461   sqlite3_mutex_leave(db->mutex);
00462   return SQLITE_OK;
00463 }
00464 
00465 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
00466 
00467 /*
00468 ** The auto-extension code added regardless of whether or not extension
00469 ** loading is supported.  We need a dummy sqlite3Apis pointer for that
00470 ** code if regular extension loading is not available.  This is that
00471 ** dummy pointer.
00472 */
00473 #ifdef SQLITE_OMIT_LOAD_EXTENSION
00474 static const sqlite3_api_routines sqlite3Apis = { 0 };
00475 #endif
00476 
00477 
00478 /*
00479 ** The following object holds the list of automatically loaded
00480 ** extensions.
00481 **
00482 ** This list is shared across threads.  The SQLITE_MUTEX_STATIC_MASTER
00483 ** mutex must be held while accessing this list.
00484 */
00485 typedef struct sqlite3ExtType sqlite3ExtType;
00486 static SQLITE_WSD struct sqlite3ExtType {
00487   int nExt;        /* Number of entries in aExt[] */          
00488   void **aExt;     /* Pointers to the extension init functions */
00489 } sqlite3Autoext = { 0, 0 };
00490 
00491 /* The "wsdAutoext" macro will resolve to the autoextension
00492 ** state vector.  If writable static data is unsupported on the target,
00493 ** we have to locate the state vector at run-time.  In the more common
00494 ** case where writable static data is supported, wsdStat can refer directly
00495 ** to the "sqlite3Autoext" state vector declared above.
00496 */
00497 #ifdef SQLITE_OMIT_WSD
00498 # define wsdAutoextInit \
00499   sqlite3ExtType *x = &GLOBAL(sqlite3ExtType,sqlite3Autoext)
00500 # define wsdAutoext x[0]
00501 #else
00502 # define wsdAutoextInit
00503 # define wsdAutoext sqlite3Autoext
00504 #endif
00505 
00506 
00507 /*
00508 ** Register a statically linked extension that is automatically
00509 ** loaded by every new database connection.
00510 */
00511 int sqlite3_auto_extension(void *xInit){
00512   int rc = SQLITE_OK;
00513 #ifndef SQLITE_OMIT_AUTOINIT
00514   rc = sqlite3_initialize();
00515   if( rc ){
00516     return rc;
00517   }else
00518 #endif
00519   {
00520     int i;
00521 #if SQLITE_THREADSAFE
00522     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
00523 #endif
00524     wsdAutoextInit;
00525     sqlite3_mutex_enter(mutex);
00526     for(i=0; i<wsdAutoext.nExt; i++){
00527       if( wsdAutoext.aExt[i]==xInit ) break;
00528     }
00529     if( i==wsdAutoext.nExt ){
00530       int nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
00531       void **aNew;
00532       aNew = sqlite3_realloc(wsdAutoext.aExt, nByte);
00533       if( aNew==0 ){
00534         rc = SQLITE_NOMEM;
00535       }else{
00536         wsdAutoext.aExt = aNew;
00537         wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
00538         wsdAutoext.nExt++;
00539       }
00540     }
00541     sqlite3_mutex_leave(mutex);
00542     assert( (rc&0xff)==rc );
00543     return rc;
00544   }
00545 }
00546 
00547 /*
00548 ** Reset the automatic extension loading mechanism.
00549 */
00550 void sqlite3_reset_auto_extension(void){
00551 #ifndef SQLITE_OMIT_AUTOINIT
00552   if( sqlite3_initialize()==SQLITE_OK )
00553 #endif
00554   {
00555 #if SQLITE_THREADSAFE
00556     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
00557 #endif
00558     wsdAutoextInit;
00559     sqlite3_mutex_enter(mutex);
00560     sqlite3_free(wsdAutoext.aExt);
00561     wsdAutoext.aExt = 0;
00562     wsdAutoext.nExt = 0;
00563     sqlite3_mutex_leave(mutex);
00564   }
00565 }
00566 
00567 /*
00568 ** Load all automatic extensions.
00569 */
00570 int sqlite3AutoLoadExtensions(sqlite3 *db){
00571   int i;
00572   int go = 1;
00573   int rc = SQLITE_OK;
00574   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
00575 
00576   wsdAutoextInit;
00577   if( wsdAutoext.nExt==0 ){
00578     /* Common case: early out without every having to acquire a mutex */
00579     return SQLITE_OK;
00580   }
00581   for(i=0; go; i++){
00582     char *zErrmsg = 0;
00583 #if SQLITE_THREADSAFE
00584     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
00585 #endif
00586     sqlite3_mutex_enter(mutex);
00587     if( i>=wsdAutoext.nExt ){
00588       xInit = 0;
00589       go = 0;
00590     }else{
00591       xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
00592               wsdAutoext.aExt[i];
00593     }
00594     sqlite3_mutex_leave(mutex);
00595     if( xInit && xInit(db, &zErrmsg, &sqlite3Apis) ){
00596       sqlite3Error(db, SQLITE_ERROR,
00597             "automatic extension loading failed: %s", zErrmsg);
00598       go = 0;
00599       rc = SQLITE_ERROR;
00600       sqlite3_free(zErrmsg);
00601     }
00602   }
00603   return rc;
00604 }

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:54 2011 by Doxygen 1.6.1