diff options
Diffstat (limited to 'plugin')
-rw-r--r-- | plugin/auth_examples/dialog_examples.c | 2 | ||||
-rw-r--r-- | plugin/auth_examples/qa_auth_interface.c | 2 | ||||
-rw-r--r-- | plugin/auth_examples/qa_auth_server.c | 2 | ||||
-rw-r--r-- | plugin/daemon_example/CMakeLists.txt | 2 | ||||
-rw-r--r-- | plugin/daemon_example/daemon_example.cc | 2 | ||||
-rw-r--r-- | plugin/handler_socket/handlersocket/hstcpsvr_worker.cpp | 1 | ||||
-rw-r--r-- | plugin/handler_socket/libhsclient/fatal.cpp | 1 | ||||
-rw-r--r-- | plugin/semisync/semisync_master.cc | 5 | ||||
-rw-r--r-- | plugin/sql_errlog/CMakeLists.txt | 2 | ||||
-rw-r--r-- | plugin/sql_errlog/logger_service.c | 21 | ||||
-rw-r--r-- | plugin/sql_errlog/service_logger.h | 100 | ||||
-rw-r--r-- | plugin/sql_errlog/sql_errlog.c | 27 | ||||
-rw-r--r-- | plugin/sql_errlog/sql_logger.cc | 194 |
13 files changed, 351 insertions, 10 deletions
diff --git a/plugin/auth_examples/dialog_examples.c b/plugin/auth_examples/dialog_examples.c index 9c4728b7886..067244d6f7d 100644 --- a/plugin/auth_examples/dialog_examples.c +++ b/plugin/auth_examples/dialog_examples.c @@ -1,5 +1,5 @@ /* Copyright (C) 2010 Sergei Golubchik and Monty Program Ab - Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. + Copyright (c) 2010, 2011, Oracle and/or its affiliates. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as diff --git a/plugin/auth_examples/qa_auth_interface.c b/plugin/auth_examples/qa_auth_interface.c index 303978f7b1f..ddf51a3dd19 100644 --- a/plugin/auth_examples/qa_auth_interface.c +++ b/plugin/auth_examples/qa_auth_interface.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. +/* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as diff --git a/plugin/auth_examples/qa_auth_server.c b/plugin/auth_examples/qa_auth_server.c index a6a06cfa289..59b926b63dc 100644 --- a/plugin/auth_examples/qa_auth_server.c +++ b/plugin/auth_examples/qa_auth_server.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. +/* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as diff --git a/plugin/daemon_example/CMakeLists.txt b/plugin/daemon_example/CMakeLists.txt index 06c39af4bdd..3d674c4ef3e 100644 --- a/plugin/daemon_example/CMakeLists.txt +++ b/plugin/daemon_example/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved. # # 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 diff --git a/plugin/daemon_example/daemon_example.cc b/plugin/daemon_example/daemon_example.cc index ab374a4fb3f..ec2979de3c3 100644 --- a/plugin/daemon_example/daemon_example.cc +++ b/plugin/daemon_example/daemon_example.cc @@ -1,4 +1,4 @@ -/* Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. +/* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved. 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 diff --git a/plugin/handler_socket/handlersocket/hstcpsvr_worker.cpp b/plugin/handler_socket/handlersocket/hstcpsvr_worker.cpp index 85887d1d55c..c254d17dff5 100644 --- a/plugin/handler_socket/handlersocket/hstcpsvr_worker.cpp +++ b/plugin/handler_socket/handlersocket/hstcpsvr_worker.cpp @@ -7,6 +7,7 @@ */ #include <my_config.h> +#include <sys/types.h> #include <netinet/in.h> #include <errno.h> #include <poll.h> diff --git a/plugin/handler_socket/libhsclient/fatal.cpp b/plugin/handler_socket/libhsclient/fatal.cpp index 8f8751da382..5cdd8879ab1 100644 --- a/plugin/handler_socket/libhsclient/fatal.cpp +++ b/plugin/handler_socket/libhsclient/fatal.cpp @@ -9,6 +9,7 @@ #include <stdlib.h> #include <stdio.h> #include <syslog.h> +#include <unistd.h> #include "fatal.hpp" diff --git a/plugin/semisync/semisync_master.cc b/plugin/semisync/semisync_master.cc index 7504697a21f..9e857f83d39 100644 --- a/plugin/semisync/semisync_master.cc +++ b/plugin/semisync/semisync_master.cc @@ -626,7 +626,7 @@ int ReplSemiSyncMaster::commitTrx(const char* trx_wait_binlog_name, (int)is_on()); } - while (is_on()) + while (is_on() && !thd_killed(NULL)) { if (reply_file_name_inited_) { @@ -743,7 +743,8 @@ int ReplSemiSyncMaster::commitTrx(const char* trx_wait_binlog_name, At this point, the binlog file and position of this transaction must have been removed from ActiveTranx. */ - assert(!active_tranxs_->is_tranx_end_pos(trx_wait_binlog_name, + assert(thd_killed(NULL) || + !active_tranxs_->is_tranx_end_pos(trx_wait_binlog_name, trx_wait_binlog_pos)); /* Update the status counter. */ diff --git a/plugin/sql_errlog/CMakeLists.txt b/plugin/sql_errlog/CMakeLists.txt index 18fb9f5421d..50c4b7d806d 100644 --- a/plugin/sql_errlog/CMakeLists.txt +++ b/plugin/sql_errlog/CMakeLists.txt @@ -13,4 +13,4 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -MYSQL_ADD_PLUGIN(sql_errlog sql_errlog.c MODULE_ONLY) +MYSQL_ADD_PLUGIN(sql_errlog sql_errlog.c sql_logger.cc service_logger.h MODULE_ONLY) diff --git a/plugin/sql_errlog/logger_service.c b/plugin/sql_errlog/logger_service.c new file mode 100644 index 00000000000..c45e978413c --- /dev/null +++ b/plugin/sql_errlog/logger_service.c @@ -0,0 +1,21 @@ +/* Copyright (C) 2012 Monty Program 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; version 2 of the License. + + 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 */ + +#include <service_versions.h> + + +/* file reserved for the future use */ +SERVICE_VERSION *logger_service= (void *) VERSION_logger; + diff --git a/plugin/sql_errlog/service_logger.h b/plugin/sql_errlog/service_logger.h new file mode 100644 index 00000000000..2aa4d94f010 --- /dev/null +++ b/plugin/sql_errlog/service_logger.h @@ -0,0 +1,100 @@ +/* Copyright (C) 2012 Monty Program 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; version 2 of the License. + + 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 */ + +#ifndef MYSQL_SERVICE_LOGGER_INCLUDED +#define MYSQL_SERVICE_LOGGER_INCLUDED + +#ifndef MYSQL_ABI_CHECK +#include <stdarg.h> +#endif + +/** + @file + logger service + + Log file with rotation implementation. + + This service implements logging with possible rotation + of the log files. Interface intentionally tries to be similar to FILE* + related functions. + + So that one can open the log with logger_open(), specifying + the limit on the logfile size and the rotations number. + + Then it's possible to write messages to the log with + logger_printf or logger_vprintf functions. + + As the size of the logfile grows over the specified limit, + it is renamed to 'logfile.1'. The former 'logfile.1' becomes + 'logfile.2', etc. The file 'logfile.rotations' is removed. + That's how the rotation works. + + The rotation can be forced with the logger_rotate() call. + + Finally the log should be closed with logger_close(). + +@notes: + Implementation checks the size of the log file before it starts new + printf into it. So the size of the file gets over the limit when it rotates. + + The access is secured with the mutex, so the log is threadsafe. +*/ + + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct logger_handle_st LOGGER_HANDLE; + +extern struct logger_service_st { + LOGGER_HANDLE* (*open)(const char *path, + unsigned long long size_limit, + unsigned int rotations); + int (*close)(LOGGER_HANDLE *log); + int (*vprintf)(LOGGER_HANDLE *log, const char *fmt, va_list argptr); + int (*printf)(LOGGER_HANDLE *log, const char *fmt, ...); + int (*rotate)(LOGGER_HANDLE *log); +} *logger_service; + +#if 0 /*MYSQL_DYNAMIC_PLUGIN*/ + +#define logger_open(path, size_limit, rotations) \ + (logger_service->open(path, size_limit, rotations)) +#define logger_close(log) (logger_service->close(log)) +#define logger_rotate(log) (logger_service->rotate(log)) +#define logger_vprintf(log, fmt, argptr) (logger_service->\ + vprintf(log, fmt, argptr)) +#define logger_printf logger_service->printf +#else + + LOGGER_HANDLE *logger_open(const char *path, + unsigned long long size_limit, + unsigned int rotations); + int logger_close(LOGGER_HANDLE *log); + int logger_vprintf(LOGGER_HANDLE *log, const char *fmt, va_list argptr); + int logger_rotate(LOGGER_HANDLE *log); + int logger_printf(LOGGER_HANDLE *log, const char *fmt, ...); + + void init_logger_mutexes(); +#endif + + +#ifdef __cplusplus +} +#endif + +#endif /*MYSQL_SERVICE_LOGGER_INCLUDED*/ + diff --git a/plugin/sql_errlog/sql_errlog.c b/plugin/sql_errlog/sql_errlog.c index ee2765a7143..ce383916621 100644 --- a/plugin/sql_errlog/sql_errlog.c +++ b/plugin/sql_errlog/sql_errlog.c @@ -16,6 +16,7 @@ #include <mysql/plugin_audit.h> #include <stdio.h> #include <time.h> +#include "service_logger.h" /* Disable __attribute__() on non-gcc compilers. @@ -32,6 +33,7 @@ rate 0 means the logging was disabled. */ + static char *filename; static unsigned int rate; static unsigned long long size_limit; @@ -50,7 +52,7 @@ static MYSQL_SYSVAR_UINT(rate, rate, PLUGIN_VAR_RQCMDARG, static MYSQL_SYSVAR_ULONGLONG(size_limit, size_limit, PLUGIN_VAR_READONLY, "Log file size limit", NULL, NULL, - 1000000, 100, 0, 1); + 1000000, 100, ((long long) 0x7FFFFFFFFFFFFFFFLL), 1); static MYSQL_SYSVAR_UINT(rotations, rotations, PLUGIN_VAR_READONLY, "Number of rotations before log is removed.", @@ -104,6 +106,8 @@ static void log_sql_errors(MYSQL_THD thd __attribute__((unused)), static int sql_error_log_init(void *p __attribute__((unused))) { + init_logger_mutexes(); + logfile= logger_open(filename, size_limit, rotations); if (logfile == NULL) { fprintf(stderr, "Could not create file '%s'\n", @@ -117,7 +121,8 @@ static int sql_error_log_init(void *p __attribute__((unused))) static int sql_error_log_deinit(void *p __attribute__((unused))) { - logger_close(logfile); + if (logfile) + logger_close(logfile); return 0; } @@ -139,6 +144,24 @@ static struct st_mysql_audit descriptor = { MYSQL_AUDIT_GENERAL_CLASSMASK } }; +mysql_declare_plugin(sql_errlog) +{ + MYSQL_AUDIT_PLUGIN, + &descriptor, + "SQL_ERROR_LOG", + "Alexey Botchkov", + "Log SQL level errors to a file with rotation", + PLUGIN_LICENSE_GPL, + sql_error_log_init, + sql_error_log_deinit, + 0x0100, + NULL, + vars, + NULL, + 0 +} +mysql_declare_plugin_end; + maria_declare_plugin(sql_errlog) { MYSQL_AUDIT_PLUGIN, diff --git a/plugin/sql_errlog/sql_logger.cc b/plugin/sql_errlog/sql_logger.cc new file mode 100644 index 00000000000..8bf757f2fd7 --- /dev/null +++ b/plugin/sql_errlog/sql_logger.cc @@ -0,0 +1,194 @@ +/* Copyright (C) 2012 Monty Program 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; version 2 of the License. + + 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 */ + + +#include "my_global.h" +#include <my_sys.h> +#include "service_logger.h" +#include <my_pthread.h> + +extern MYSQL_PLUGIN_IMPORT char *mysql_data_home; + +/* These belong to the service initialization */ +static PSI_mutex_key key_LOCK_logger_service; +static PSI_mutex_info mutex_list[]= +{{ &key_LOCK_logger_service, "logger_service_file_st::lock", PSI_FLAG_GLOBAL}}; + +typedef struct logger_handle_st { + File file; + char path[FN_REFLEN]; + unsigned long long size_limit; + unsigned int rotations; + size_t path_len; + mysql_mutex_t lock; +} LSFS; + + +#define LOG_FLAGS (O_APPEND | O_CREAT | O_WRONLY) + +static unsigned int n_dig(unsigned int i) +{ + return (i == 0) ? 0 : ((i < 10) ? 1 : ((i < 100) ? 2 : 3)); +} + + +LOGGER_HANDLE *logger_open(const char *path, + unsigned long long size_limit, + unsigned int rotations) +{ + LOGGER_HANDLE new_log, *l_perm; + + /* + I don't think we ever need more rotations, + but if it's so, the rotation procedure should be adapted to it. + */ + if (rotations > 999) + return 0; + + new_log.rotations= rotations; + new_log.size_limit= size_limit; + new_log.path_len= strlen(fn_format(new_log.path, path, + mysql_data_home, "", MY_UNPACK_FILENAME)); + + if (new_log.path_len+n_dig(rotations)+1 > FN_REFLEN) + { + errno= ENAMETOOLONG; + /* File path too long */ + return 0; + } + if ((new_log.file= my_open(new_log.path, LOG_FLAGS, MYF(0))) < 0) + { + errno= my_errno; + /* Check errno for the cause */ + return 0; + } + + if (!(l_perm= (LOGGER_HANDLE *) my_malloc(sizeof(LOGGER_HANDLE), MYF(0)))) + { + my_close(new_log.file, MYF(0)); + new_log.file= -1; + return 0; /* End of memory */ + } + *l_perm= new_log; + mysql_mutex_init(key_LOCK_logger_service, &l_perm->lock, MY_MUTEX_INIT_FAST); + return l_perm; +} + +int logger_close(LOGGER_HANDLE *log) +{ + int result; + File file= log->file; + mysql_mutex_destroy(&log->lock); + my_free(log); + if ((result= my_close(file, MYF(0)))) + errno= my_errno; + return result; +} + + +static char *logname(LOGGER_HANDLE *log, char *buf, unsigned int n_log) +{ + sprintf(buf+log->path_len, ".%0*u", n_dig(log->rotations), n_log); + return buf; +} + + +static int do_rotate(LOGGER_HANDLE *log) +{ + char namebuf[FN_REFLEN]; + int result; + unsigned int i; + char *buf_old, *buf_new, *tmp; + + memcpy(namebuf, log->path, log->path_len); + + buf_new= logname(log, namebuf, log->rotations); + buf_old= log->path; + for (i=log->rotations-1; i>0; i--) + { + logname(log, buf_old, i); + if (!access(buf_old, F_OK) && + (result= my_rename(buf_old, buf_new, MYF(0)))) + goto exit; + tmp= buf_old; + buf_old= buf_new; + buf_new= tmp; + } + if ((result= my_close(log->file, MYF(0)))) + goto exit; + namebuf[log->path_len]= 0; + result= my_rename(namebuf, logname(log, log->path, 1), MYF(0)); + log->file= my_open(namebuf, LOG_FLAGS, MYF(0)); +exit: + errno= my_errno; + return log->file < 0 || result; +} + + +int logger_vprintf(LOGGER_HANDLE *log, const char* fmt, va_list ap) +{ + int result; + my_off_t filesize; + char cvtbuf[1024]; + size_t n_bytes; + + mysql_mutex_lock(&log->lock); + if ((filesize= my_tell(log->file, MYF(0))) == (my_off_t) -1 || + ((unsigned long long)filesize >= log->size_limit && + do_rotate(log))) + { + result= -1; + errno= my_errno; + goto exit; /* Log rotation needed but failed */ + } + + n_bytes= my_vsnprintf(cvtbuf, sizeof(cvtbuf), fmt, ap); + if (n_bytes >= sizeof(cvtbuf)) + n_bytes= sizeof(cvtbuf) - 1; + + result= my_write(log->file, (uchar *) cvtbuf, n_bytes, MYF(0)); + +exit: + mysql_mutex_unlock(&log->lock); + return result; +} + + +int logger_rotate(LOGGER_HANDLE *log) +{ + int result; + mysql_mutex_lock(&log->lock); + result= do_rotate(log); + mysql_mutex_unlock(&log->lock); + return result; +} + + +int logger_printf(LOGGER_HANDLE *log, const char *fmt, ...) +{ + int result; + va_list args; + va_start(args,fmt); + result= logger_vprintf(log, fmt, args); + va_end(args); + return result; +} + +void init_logger_mutexes() +{ + if (PSI_server) + PSI_server->register_mutex("sql_logger", mutex_list, 1); +} + |