diff options
Diffstat (limited to 'sql/sql_udf.cc')
-rw-r--r-- | sql/sql_udf.cc | 87 |
1 files changed, 47 insertions, 40 deletions
diff --git a/sql/sql_udf.cc b/sql/sql_udf.cc index 5705dccd8d0..daf02b1c6d6 100644 --- a/sql/sql_udf.cc +++ b/sql/sql_udf.cc @@ -1,21 +1,19 @@ -/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB - +/* Copyright (C) 2000 MySQL AB + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ - - /* This implements 'user defined functions' */ /* @@ -52,11 +50,11 @@ extern "C" { FreeLibrary((HMODULE)lib); } - + #elif !defined(OS2) #include <dlfcn.h> #endif - + #include <stdarg.h> #include <hash.h> } @@ -134,14 +132,13 @@ void udf_init() sql_print_error("Can't allocate memory for udf structures"); hash_free(&udf_hash); free_root(&mem,MYF(0)); + delete new_thd; DBUG_VOID_RETURN; } initialized = 1; - new_thd->mysys_var=my_thread_var; - new_thd->version = refresh_version; //current_thd->version; - new_thd->current_tablenr = 0; - new_thd->open_tables = 0; - new_thd->db = my_strdup("mysql", MYF(0)); + new_thd->store_globals(); + new_thd->db= my_strdup("mysql", MYF(0)); + new_thd->db_length=5; bzero((gptr) &tables,sizeof(tables)); tables.alias= tables.real_name= (char*) "func"; @@ -181,9 +178,10 @@ void udf_init() { if (!(dl = dlopen(tmp->dl, RTLD_NOW))) { + /* Print warning to log */ sql_print_error(ER(ER_CANT_OPEN_LIBRARY), tmp->dl,errno,dlerror()); - del_udf(tmp); + /* Keep the udf in the hash so that we can remove it later */ continue; } new_dl=1; @@ -215,16 +213,17 @@ void udf_free() for (uint idx=0 ; idx < udf_hash.records ; idx++) { udf_func *udf=(udf_func*) hash_element(&udf_hash,idx); - if (udf->dl) + if (udf->dlhandle) // Not closed before { + /* Mark all versions using the same handler as closed */ for (uint j=idx+1 ; j < udf_hash.records ; j++) { udf_func *tmp=(udf_func*) hash_element(&udf_hash,j); - if (tmp->dl && !strcmp(udf->dl,tmp->dl)) - tmp->dl=0; + if (udf->dlhandle == tmp->dlhandle) + tmp->dlhandle=0; // Already closed } + dlclose(udf->dlhandle); } - dlclose(udf->dlhandle); } hash_free(&udf_hash); free_root(&mem,MYF(0)); @@ -243,9 +242,9 @@ static void del_udf(udf_func *udf) else { /* - ** The functions is in use ; Rename the functions instead of removing it. - ** The functions will be automaticly removed when the least threads - ** doesn't use it anymore + The functions is in use ; Rename the functions instead of removing it. + The functions will be automaticly removed when the least threads + doesn't use it anymore */ char *name= udf->name; uint name_length=udf->name_length; @@ -263,6 +262,10 @@ void free_udf(udf_func *udf) pthread_mutex_lock(&THR_LOCK_udf); if (!--udf->usage_count) { + /* + We come here when someone has deleted the udf function + while another thread still was using the udf + */ hash_delete(&udf_hash,(byte*) udf); using_udf_functions=udf_hash.records != 0; if (!find_udf_dl(udf->dl)) @@ -272,6 +275,7 @@ void free_udf(udf_func *udf) DBUG_VOID_RETURN; } + /* This is only called if using_udf_functions != 0 */ udf_func *find_udf(const char *name,uint length,bool mark_used) @@ -281,20 +285,26 @@ udf_func *find_udf(const char *name,uint length,bool mark_used) /* TODO: This should be changed to reader locks someday! */ pthread_mutex_lock(&THR_LOCK_udf); - udf=(udf_func*) hash_search(&udf_hash,(byte*) name, - length ? length : (uint) strlen(name)); - if (mark_used) - udf->usage_count++; + if ((udf=(udf_func*) hash_search(&udf_hash,(byte*) name, + length ? length : (uint) strlen(name)))) + { + if (!udf->dlhandle) + udf=0; // Could not be opened + else if (mark_used) + udf->usage_count++; + } pthread_mutex_unlock(&THR_LOCK_udf); DBUG_RETURN(udf); } + static void *find_udf_dl(const char *dl) { DBUG_ENTER("find_udf_dl"); - /* because only the function name is hashed, we have to search trough - ** all rows to find the dl. + /* + Because only the function name is hashed, we have to search trough + all rows to find the dl. */ for (uint idx=0 ; idx < udf_hash.records ; idx++) { @@ -311,7 +321,7 @@ static void *find_udf_dl(const char *dl) static udf_func *add_udf(char *name, Item_result ret, char *dl, Item_udftype type) { - if (!name || !dl) + if (!name || !dl || !(uint) type || (uint) type > (uint) UDFTYPE_AGGREGATE) return 0; udf_func *tmp= (udf_func*) alloc_root(&mem, sizeof(udf_func)); if (!tmp) @@ -363,7 +373,7 @@ int mysql_create_function(THD *thd,udf_func *udf) } pthread_mutex_lock(&THR_LOCK_udf); - if (hash_search(&udf_hash,(byte*) udf->name, udf->name_length)) + if ((hash_search(&udf_hash,(byte*) udf->name, udf->name_length))) { net_printf(&thd->net, ER_UDF_EXISTS, udf->name); goto err; @@ -389,8 +399,7 @@ int mysql_create_function(THD *thd,udf_func *udf) } udf->name=strdup_root(&mem,udf->name); udf->dl=strdup_root(&mem,udf->dl); - if (!udf->name || !udf->dl || - !(u_d=add_udf(udf->name,udf->returns,udf->dl,udf->type))) + if (!(u_d=add_udf(udf->name,udf->returns,udf->dl,udf->type))) { send_error(&thd->net,0); // End of memory goto err; @@ -449,13 +458,18 @@ int mysql_drop_function(THD *thd,const char *udf_name) DBUG_RETURN(1); } pthread_mutex_lock(&THR_LOCK_udf); - if (!(udf=(udf_func*) hash_search(&udf_hash,(byte*) udf_name, (uint) strlen(udf_name)))) + if (!(udf=(udf_func*) hash_search(&udf_hash,(byte*) udf_name, + (uint) strlen(udf_name)))) { net_printf(&thd->net, ER_FUNCTION_NOT_DEFINED, udf_name); goto err; } del_udf(udf); - if (!find_udf_dl(udf->dl)) + /* + Close the handle if this was function that was found during boot or + CREATE FUNCTION and it's not in use by any other udf function + */ + if (udf->dlhandle && !find_udf_dl(udf->dl)) dlclose(udf->dlhandle); bzero((char*) &tables,sizeof(tables)); @@ -481,10 +495,3 @@ int mysql_drop_function(THD *thd,const char *udf_name) } #endif /* HAVE_DLOPEN */ - -/* -** Local variables: -** tab-width: 8 -** c-basic-offset: 2 -** End: -*/ |