mirror of
https://github.com/MariaDB/server.git
synced 2025-08-08 11:22:35 +03:00
Made the example actually do something :)
It now demonstrates creating its own thread and shows off how to clean up after itself (creates a really simple heartbeat file)
This commit is contained in:
@@ -19,18 +19,20 @@ MYSQLSHAREdir = $(pkgdatadir)
|
||||
MYSQLBASEdir= $(prefix)
|
||||
MYSQLLIBdir= $(pkglibdir)
|
||||
INCLUDES = -I$(top_srcdir)/include -I$(top_builddir)/include \
|
||||
-I$(srcdir)
|
||||
-I$(top_srcdir)/regex \
|
||||
-I$(top_srcdir)/sql \
|
||||
-I$(srcdir) @ZLIB_INCLUDES@
|
||||
|
||||
EXTRA_LTLIBRARIES = libdaemon_example.la
|
||||
pkglib_LTLIBRARIES = @plugin_daemon_example_shared_target@
|
||||
libdaemon_example_la_LDFLAGS = -module -rpath $(MYSQLLIBdir)
|
||||
libdaemon_example_la_CXXFLAGS= $(AM_CFLAGS) -DMYSQL_DYNAMIC_PLUGIN
|
||||
libdaemon_example_la_CFLAGS = $(AM_CFLAGS) -DMYSQL_DYNAMIC_PLUGIN
|
||||
libdaemon_example_la_SOURCES = daemon_example.c
|
||||
libdaemon_example_la_SOURCES = daemon_example.cc
|
||||
|
||||
|
||||
EXTRA_LIBRARIES = libdaemon_example.a
|
||||
noinst_LIBRARIES = @plugin_daemon_example_static_target@
|
||||
libdaemon_example_a_CXXFLAGS = $(AM_CFLAGS)
|
||||
libdaemon_example_a_CFLAGS = $(AM_CFLAGS)
|
||||
libdaemon_example_a_SOURCES= daemon_example.c
|
||||
libdaemon_example_a_SOURCES= daemon_example.cc
|
||||
|
@@ -1,89 +0,0 @@
|
||||
/* Copyright (C) 2006 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; 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <mysql_version.h>
|
||||
#include <mysql/plugin.h>
|
||||
|
||||
/*
|
||||
#if !defined(__attribute__) && (defined(__cplusplus) || !defined(__GNUC__) || __GNUC__ == 2 && __GNUC_MINOR__ < 8)
|
||||
#define __attribute__(A)
|
||||
#endif
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/*
|
||||
Initialize the daemon example at server start or plugin installation.
|
||||
|
||||
SYNOPSIS
|
||||
daemon_example_plugin_init()
|
||||
|
||||
DESCRIPTION
|
||||
Does nothing.
|
||||
|
||||
RETURN VALUE
|
||||
0 success
|
||||
1 failure (cannot happen)
|
||||
*/
|
||||
|
||||
static int daemon_example_plugin_init(void *p)
|
||||
{
|
||||
return(0);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Terminate the daemon example at server shutdown or plugin deinstallation.
|
||||
|
||||
SYNOPSIS
|
||||
daemon_example_plugin_deinit()
|
||||
Does nothing.
|
||||
|
||||
RETURN VALUE
|
||||
0 success
|
||||
1 failure (cannot happen)
|
||||
|
||||
*/
|
||||
|
||||
static int daemon_example_plugin_deinit(void *p)
|
||||
{
|
||||
return(0);
|
||||
}
|
||||
|
||||
struct st_mysql_daemon daemon_example_plugin=
|
||||
{ MYSQL_DAEMON_INTERFACE_VERSION };
|
||||
|
||||
/*
|
||||
Plugin library descriptor
|
||||
*/
|
||||
|
||||
mysql_declare_plugin(daemon_example)
|
||||
{
|
||||
MYSQL_DAEMON_PLUGIN,
|
||||
&daemon_example_plugin,
|
||||
"daemon_example",
|
||||
"Brian Aker",
|
||||
"Daemon example that tests init and deinit of a plugin",
|
||||
PLUGIN_LICENSE_GPL,
|
||||
daemon_example_plugin_init, /* Plugin Init */
|
||||
daemon_example_plugin_deinit, /* Plugin Deinit */
|
||||
0x0100 /* 1.0 */,
|
||||
NULL, /* status variables */
|
||||
NULL, /* system variables */
|
||||
NULL /* config options */
|
||||
}
|
||||
mysql_declare_plugin_end;
|
196
plugin/daemon_example/daemon_example.cc
Normal file
196
plugin/daemon_example/daemon_example.cc
Normal file
@@ -0,0 +1,196 @@
|
||||
/* Copyright (C) 2006 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; 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
|
||||
|
||||
#include <mysql_priv.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <mysql_version.h>
|
||||
#include <mysql/plugin.h>
|
||||
#include <my_global.h>
|
||||
#include <my_dir.h>
|
||||
|
||||
/*
|
||||
#if !defined(__attribute__) && (defined(__cplusplus) || !defined(__GNUC__) || __GNUC__ == 2 && __GNUC_MINOR__ < 8)
|
||||
#define __attribute__(A)
|
||||
#endif
|
||||
*/
|
||||
|
||||
#define HEART_STRING_BUFFER 100
|
||||
|
||||
struct mysql_heartbeat_context
|
||||
{
|
||||
pthread_t heartbeat_thread;
|
||||
File heartbeat_file;
|
||||
};
|
||||
|
||||
pthread_handler_t mysql_heartbeat(void *p)
|
||||
{
|
||||
DBUG_ENTER("mysql_heartbeat");
|
||||
struct mysql_heartbeat_context *con= (struct mysql_heartbeat_context *)p;
|
||||
char buffer[HEART_STRING_BUFFER];
|
||||
unsigned int x= 0;
|
||||
time_t result;
|
||||
struct tm tm_tmp;
|
||||
|
||||
while(1)
|
||||
{
|
||||
sleep(5);
|
||||
|
||||
result= time(NULL);
|
||||
localtime_r(&result, &tm_tmp);
|
||||
my_snprintf(buffer, sizeof(buffer),
|
||||
"Heartbeat at %02d%02d%02d %2d:%02d:%02d\n",
|
||||
tm_tmp.tm_year % 100,
|
||||
tm_tmp.tm_mon+1,
|
||||
tm_tmp.tm_mday,
|
||||
tm_tmp.tm_hour,
|
||||
tm_tmp.tm_min,
|
||||
tm_tmp.tm_sec);
|
||||
my_write(con->heartbeat_file, buffer, strlen(buffer), MYF(0));
|
||||
x++;
|
||||
}
|
||||
|
||||
|
||||
DBUG_RETURN(0);
|
||||
}
|
||||
|
||||
/*
|
||||
Initialize the daemon example at server start or plugin installation.
|
||||
|
||||
SYNOPSIS
|
||||
daemon_example_plugin_init()
|
||||
|
||||
DESCRIPTION
|
||||
Starts up heartbeatbeat thread
|
||||
|
||||
RETURN VALUE
|
||||
0 success
|
||||
1 failure (cannot happen)
|
||||
*/
|
||||
|
||||
static int daemon_example_plugin_init(void *p)
|
||||
{
|
||||
DBUG_ENTER("daemon_example_plugin_init");
|
||||
struct mysql_heartbeat_context *con;
|
||||
pthread_attr_t attr; /* Thread attributes */
|
||||
char heartbeat_filename[FN_REFLEN];
|
||||
char buffer[HEART_STRING_BUFFER];
|
||||
time_t result= time(NULL);
|
||||
struct tm tm_tmp;
|
||||
|
||||
struct st_plugin_int *plugin= (struct st_plugin_int *)p;
|
||||
|
||||
con= (struct mysql_heartbeat_context *)my_malloc(sizeof(struct mysql_heartbeat_context), MYF(0));
|
||||
|
||||
fn_format(heartbeat_filename, "mysql-heartbeat", "", ".log", MY_REPLACE_EXT | MY_UNPACK_FILENAME);
|
||||
unlink(heartbeat_filename);
|
||||
con->heartbeat_file= my_open(heartbeat_filename, O_CREAT|O_RDWR, MYF(0));
|
||||
|
||||
/*
|
||||
No threads exist at this point in time, so this is thread safe.
|
||||
*/
|
||||
localtime_r(&result, &tm_tmp);
|
||||
my_snprintf(buffer, sizeof(buffer),
|
||||
"Starting up at %02d%02d%02d %2d:%02d:%02d\n",
|
||||
tm_tmp.tm_year % 100,
|
||||
tm_tmp.tm_mon+1,
|
||||
tm_tmp.tm_mday,
|
||||
tm_tmp.tm_hour,
|
||||
tm_tmp.tm_min,
|
||||
tm_tmp.tm_sec);
|
||||
my_write(con->heartbeat_file, buffer, strlen(buffer), MYF(0));
|
||||
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setdetachstate(&attr,
|
||||
PTHREAD_CREATE_JOINABLE);
|
||||
|
||||
|
||||
/* now create the thread */
|
||||
if (pthread_create(&con->heartbeat_thread, &attr, mysql_heartbeat, (void *)con) != 0)
|
||||
{
|
||||
fprintf(stderr,"Could not create heartbeat thread!\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
plugin->data= (void *)con;
|
||||
|
||||
DBUG_RETURN(0);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Terminate the daemon example at server shutdown or plugin deinstallation.
|
||||
|
||||
SYNOPSIS
|
||||
daemon_example_plugin_deinit()
|
||||
Does nothing.
|
||||
|
||||
RETURN VALUE
|
||||
0 success
|
||||
1 failure (cannot happen)
|
||||
|
||||
*/
|
||||
static int daemon_example_plugin_deinit(void *p)
|
||||
{
|
||||
DBUG_ENTER("daemon_example_plugin_deinit");
|
||||
char buffer[HEART_STRING_BUFFER];
|
||||
struct st_plugin_int *plugin= (struct st_plugin_int *)p;
|
||||
struct mysql_heartbeat_context *con= (struct mysql_heartbeat_context *)plugin->data;
|
||||
time_t result= time(NULL);
|
||||
struct tm tm_tmp;
|
||||
|
||||
pthread_cancel(con->heartbeat_thread);
|
||||
|
||||
localtime_r(&result, &tm_tmp);
|
||||
my_snprintf(buffer, sizeof(buffer),
|
||||
"Shutting down at %02d%02d%02d %2d:%02d:%02d\n",
|
||||
tm_tmp.tm_year % 100,
|
||||
tm_tmp.tm_mon+1,
|
||||
tm_tmp.tm_mday,
|
||||
tm_tmp.tm_hour,
|
||||
tm_tmp.tm_min,
|
||||
tm_tmp.tm_sec);
|
||||
my_write(con->heartbeat_file, buffer, strlen(buffer), MYF(0));
|
||||
my_close(con->heartbeat_file, MYF(0));
|
||||
|
||||
|
||||
my_free((char *)con, MYF(0));
|
||||
|
||||
DBUG_RETURN(0);
|
||||
}
|
||||
|
||||
struct st_mysql_daemon daemon_example_plugin=
|
||||
{ MYSQL_DAEMON_INTERFACE_VERSION };
|
||||
|
||||
/*
|
||||
Plugin library descriptor
|
||||
*/
|
||||
|
||||
mysql_declare_plugin(daemon_example)
|
||||
{
|
||||
MYSQL_DAEMON_PLUGIN,
|
||||
&daemon_example_plugin,
|
||||
"daemon_example",
|
||||
"Brian Aker",
|
||||
"Daemon example, creates a heartbeat beat file in mysql-heartbeat.log",
|
||||
PLUGIN_LICENSE_GPL,
|
||||
daemon_example_plugin_init, /* Plugin Init */
|
||||
daemon_example_plugin_deinit, /* Plugin Deinit */
|
||||
0x0100 /* 1.0 */,
|
||||
NULL, /* status variables */
|
||||
NULL, /* system variables */
|
||||
NULL /* config options */
|
||||
}
|
||||
mysql_declare_plugin_end;
|
Reference in New Issue
Block a user