33 #include "sql_parse.h"
34 #include "sql_table.h"
36 #include <my_pthread.h>
46 static bool initialized = 0;
53 char *dl, Item_udftype typ);
55 static void *find_udf_dl(
const char *dl);
57 static char *init_syms(
udf_func *tmp,
char *nm)
61 if (!((tmp->func= (Udf_func_any) dlsym(tmp->dlhandle, tmp->name.str))))
64 end=strmov(nm,tmp->name.str);
66 if (tmp->type == UDFTYPE_AGGREGATE)
68 (void)strmov(end,
"_clear");
69 if (!((tmp->func_clear= (Udf_func_clear) dlsym(tmp->dlhandle, nm))))
71 (void)strmov(end,
"_add");
72 if (!((tmp->func_add= (Udf_func_add) dlsym(tmp->dlhandle, nm))))
76 (void) strmov(end,
"_deinit");
77 tmp->func_deinit= (Udf_func_deinit) dlsym(tmp->dlhandle, nm);
79 (void) strmov(end,
"_init");
80 tmp->func_init= (Udf_func_init) dlsym(tmp->dlhandle, nm);
86 if (!tmp->func_init && !tmp->func_deinit && tmp->type != UDFTYPE_AGGREGATE)
88 if (!opt_allow_suspicious_udfs)
90 sql_print_warning(ER(ER_CANT_FIND_DL_ENTRY), nm);
96 extern "C" uchar* get_hash_key(
const uchar *buff,
size_t *length,
97 my_bool not_used __attribute__((unused)))
100 *length=(uint) udf->name.length;
101 return (uchar*) udf->name.str;
104 #ifdef HAVE_PSI_INTERFACE
105 static PSI_rwlock_key key_rwlock_THR_LOCK_udf;
107 static PSI_rwlock_info all_udf_rwlocks[]=
109 { &key_rwlock_THR_LOCK_udf,
"THR_LOCK_udf", PSI_FLAG_GLOBAL}
112 static void init_udf_psi_keys(
void)
114 const char* category=
"sql";
117 count= array_elements(all_udf_rwlocks);
134 DBUG_ENTER(
"ufd_init");
140 #ifdef HAVE_PSI_INTERFACE
146 init_sql_alloc(&mem, UDF_ALLOC_BLOCK_SIZE, 0);
147 THD *new_thd =
new THD;
149 my_hash_init(&udf_hash,system_charset_info,32,0,0,get_hash_key, NULL, 0))
151 sql_print_error(
"Can't allocate memory for udf structures");
152 my_hash_free(&udf_hash);
153 free_root(&mem,MYF(0));
158 new_thd->thread_stack= (
char*) &new_thd;
159 new_thd->store_globals();
160 new_thd->set_db(db,
sizeof(db)-1);
162 tables.
init_one_table(db,
sizeof(db)-1,
"func", 4,
"func", TL_READ);
166 DBUG_PRINT(
"error",(
"Can't open udf table"));
167 sql_print_error(
"Can't open the mysql.func table. Please "
168 "run mysql_upgrade to create it.");
173 if (init_read_record(&read_record_info, new_thd, table, NULL, 1, 1, FALSE))
175 table->use_all_columns();
176 while (!(error= read_record_info.read_record(&read_record_info)))
178 DBUG_PRINT(
"info",(
"init udf record"));
180 name.str=get_field(&mem, table->field[0]);
181 name.length = (uint) strlen(name.str);
182 char *dl_name= get_field(&mem, table->field[2]);
184 Item_udftype udftype=UDFTYPE_FUNCTION;
185 if (table->s->fields >= 4)
186 udftype=(Item_udftype) table->field[3]->val_int();
195 if (check_valid_path(dl_name, strlen(dl_name)) ||
196 check_string_char_length(&name,
"", NAME_CHAR_LEN,
197 system_charset_info, 1))
199 sql_print_error(
"Invalid row in mysql.func table for function '%.64s'",
204 if (!(tmp= add_udf(&name,(Item_result) table->field[1]->val_int(),
207 sql_print_error(
"Can't alloc memory for udf function: '%.64s'", name.str);
211 void *dl = find_udf_dl(tmp->dl);
214 char dlpath[FN_REFLEN];
215 strxnmov(dlpath,
sizeof(dlpath) - 1, opt_plugin_dir,
"/", tmp->dl,
217 (void) unpack_filename(dlpath, dlpath);
218 if (!(dl= dlopen(dlpath, RTLD_NOW)))
221 int error_number= dlopen_errno;
222 DLERROR_GENERATE(errmsg, error_number);
225 sql_print_error(ER(ER_CANT_OPEN_LIBRARY), tmp->dl, error_number, errmsg);
233 char buf[NAME_LEN+16], *missing;
234 if ((missing= init_syms(tmp, buf)))
236 sql_print_error(ER(ER_CANT_FIND_DL_ENTRY), missing);
244 sql_print_error(
"Got unknown error: %d", my_errno);
245 end_read_record(&read_record_info);
246 table->m_needs_reopen= TRUE;
252 my_pthread_setspecific_ptr(THR_THD, 0);
260 DBUG_ENTER(
"udf_free");
261 for (uint idx=0 ; idx < udf_hash.records ; idx++)
267 for (uint j=idx+1 ; j < udf_hash.records ; j++)
270 if (udf->dlhandle == tmp->dlhandle)
273 dlclose(udf->dlhandle);
276 my_hash_free(&udf_hash);
277 free_root(&mem,MYF(0));
289 DBUG_ENTER(
"del_udf");
290 if (!--udf->usage_count)
292 my_hash_delete(&udf_hash,(uchar*) udf);
293 using_udf_functions=udf_hash.records != 0;
302 char *name= udf->name.str;
303 uint name_length=udf->name.length;
304 udf->name.str=(
char*)
"*";
306 my_hash_update(&udf_hash,(uchar*) udf,(uchar*) name,name_length);
314 DBUG_ENTER(
"free_udf");
320 if (!--udf->usage_count)
326 my_hash_delete(&udf_hash,(uchar*) udf);
327 using_udf_functions=udf_hash.records != 0;
328 if (!find_udf_dl(udf->dl))
329 dlclose(udf->dlhandle);
338 udf_func *find_udf(
const char *name,uint length,
bool mark_used)
341 DBUG_ENTER(
"find_udf");
352 if ((udf=(
udf_func*) my_hash_search(&udf_hash,(uchar*) name,
353 length ? length : (uint) strlen(name))))
365 static void *find_udf_dl(
const char *dl)
367 DBUG_ENTER(
"find_udf_dl");
373 for (uint idx=0 ; idx < udf_hash.records ; idx++)
376 if (!strcmp(dl, udf->dl) && udf->dlhandle != NULL)
377 DBUG_RETURN(udf->dlhandle);
388 if (!name || !dl || !(uint) type || (uint) type > (uint) UDFTYPE_AGGREGATE)
393 memset(tmp, 0,
sizeof(*tmp));
399 if (my_hash_insert(&udf_hash,(uchar*) tmp))
401 using_udf_functions=1;
414 int mysql_create_function(THD *thd,
udf_func *udf)
422 bool save_binlog_row_based;
423 DBUG_ENTER(
"mysql_create_function");
428 my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
430 "UDFs are unavailable with the --skip-grant-tables option");
432 my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
441 if (check_valid_path(udf->dl, strlen(udf->dl)))
443 my_message(ER_UDF_NO_PATHS, ER(ER_UDF_NO_PATHS), MYF(0));
446 if (check_string_char_length(&udf->name,
"", NAME_CHAR_LEN,
447 system_charset_info, 1))
449 my_error(ER_TOO_LONG_IDENT, MYF(0), udf->name.str);
454 if (!(table= open_ltable(thd, &tables, TL_WRITE, MYSQL_LOCK_IGNORE_TIMEOUT)))
461 if ((save_binlog_row_based= thd->is_current_stmt_binlog_format_row()))
462 thd->clear_current_stmt_binlog_format_row();
465 if ((my_hash_search(&udf_hash,(uchar*) udf->name.str, udf->name.length)))
467 my_error(ER_UDF_EXISTS, MYF(0), udf->name.str);
470 if (!(dl = find_udf_dl(udf->dl)))
472 char dlpath[FN_REFLEN];
473 strxnmov(dlpath,
sizeof(dlpath) - 1, opt_plugin_dir,
"/", udf->dl, NullS);
474 (void) unpack_filename(dlpath, dlpath);
476 if (!(dl = dlopen(dlpath, RTLD_NOW)))
479 int error_number= dlopen_errno;
480 DLERROR_GENERATE(errmsg, error_number);
482 DBUG_PRINT(
"error",(
"dlopen of %s failed, error: %d (%s)",
483 udf->dl, error_number, errmsg));
484 my_error(ER_CANT_OPEN_LIBRARY, MYF(0),
485 udf->dl, error_number, errmsg);
492 char buf[NAME_LEN+16], *missing;
493 if ((missing= init_syms(udf, buf)))
495 my_error(ER_CANT_FIND_DL_ENTRY, MYF(0), missing);
499 udf->name.str=strdup_root(&mem,udf->name.str);
500 udf->dl=strdup_root(&mem,udf->dl);
501 if (!(u_d=add_udf(&udf->name,udf->returns,udf->dl,udf->type)))
505 u_d->func_init=udf->func_init;
506 u_d->func_deinit=udf->func_deinit;
507 u_d->func_clear=udf->func_clear;
508 u_d->func_add=udf->func_add;
512 table->use_all_columns();
513 restore_record(table, s->default_values);
514 table->field[0]->store(u_d->name.str, u_d->name.length, system_charset_info);
515 table->field[1]->store((longlong) u_d->returns, TRUE);
516 table->field[2]->store(u_d->dl,(uint) strlen(u_d->dl), system_charset_info);
517 if (table->s->fields >= 4)
518 table->field[3]->store((longlong) u_d->type, TRUE);
519 error = table->file->ha_write_row(table->record[0]);
523 char errbuf[MYSYS_STRERROR_SIZE];
524 my_error(ER_ERROR_ON_WRITE, MYF(0),
"mysql.func", error,
525 my_strerror(errbuf,
sizeof(errbuf), error));
532 if (write_bin_log(thd, TRUE, thd->query(), thd->query_length()))
535 DBUG_ASSERT(!thd->is_current_stmt_binlog_format_row());
536 if (save_binlog_row_based)
537 thd->set_current_stmt_binlog_format_row();
541 DBUG_ASSERT(!thd->is_current_stmt_binlog_format_row());
542 if (save_binlog_row_based)
543 thd->set_current_stmt_binlog_format_row();
551 DBUG_ASSERT(!thd->is_current_stmt_binlog_format_row());
552 if (save_binlog_row_based)
553 thd->set_current_stmt_binlog_format_row();
558 int mysql_drop_function(THD *thd,
const LEX_STRING *udf_name)
563 char *exact_name_str;
565 bool save_binlog_row_based;
567 DBUG_ENTER(
"mysql_drop_function");
572 my_error(ER_FUNCTION_NOT_DEFINED, MYF(0), udf_name->str);
574 my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
579 if (!(table= open_ltable(thd, &tables, TL_WRITE, MYSQL_LOCK_IGNORE_TIMEOUT)))
586 if ((save_binlog_row_based= thd->is_current_stmt_binlog_format_row()))
587 thd->clear_current_stmt_binlog_format_row();
590 if (!(udf=(
udf_func*) my_hash_search(&udf_hash,(uchar*) udf_name->str,
591 (uint) udf_name->length)))
593 my_error(ER_FUNCTION_NOT_DEFINED, MYF(0), udf_name->str);
597 exact_name_str= udf->name.str;
598 exact_name_len= udf->name.length;
604 if (udf->dlhandle && !find_udf_dl(udf->dl))
605 dlclose(udf->dlhandle);
608 table->use_all_columns();
609 table->field[0]->store(exact_name_str, exact_name_len, &my_charset_bin);
611 (uchar*) table->field[0]->ptr,
616 if ((delete_err = table->file->ha_delete_row(table->record[0])))
624 if (!write_bin_log(thd, TRUE, thd->query(), thd->query_length()))
628 DBUG_ASSERT(!thd->is_current_stmt_binlog_format_row());
629 if (save_binlog_row_based)
630 thd->set_current_stmt_binlog_format_row();