1
/* Copyright (C) 2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16
#include <mysql_priv.h>
19
#include <mysql_version.h>
20
#include <mysql/plugin.h>
21
#include <my_global.h>
25
Disable __attribute__() on non-gcc compilers.
27
#if !defined(__attribute__) && !defined(__GNUC__)
28
#define __attribute__(A)
32
#define HEART_STRING_BUFFER 100
34
struct mysql_heartbeat_context
36
pthread_t heartbeat_thread;
40
pthread_handler_t mysql_heartbeat(void *p)
42
DBUG_ENTER("mysql_heartbeat");
43
struct mysql_heartbeat_context *con= (struct mysql_heartbeat_context *)p;
44
char buffer[HEART_STRING_BUFFER];
54
localtime_r(&result, &tm_tmp);
55
my_snprintf(buffer, sizeof(buffer),
56
"Heartbeat at %02d%02d%02d %2d:%02d:%02d\n",
63
my_write(con->heartbeat_file, (uchar*) buffer, strlen(buffer), MYF(0));
71
Initialize the daemon example at server start or plugin installation.
74
daemon_example_plugin_init()
77
Starts up heartbeatbeat thread
81
1 failure (cannot happen)
84
static int daemon_example_plugin_init(void *p)
87
DBUG_ENTER("daemon_example_plugin_init");
88
struct mysql_heartbeat_context *con;
89
pthread_attr_t attr; /* Thread attributes */
90
char heartbeat_filename[FN_REFLEN];
91
char buffer[HEART_STRING_BUFFER];
92
time_t result= time(NULL);
95
struct st_plugin_int *plugin= (struct st_plugin_int *)p;
97
con= (struct mysql_heartbeat_context *)
98
my_malloc(sizeof(struct mysql_heartbeat_context), MYF(0));
100
fn_format(heartbeat_filename, "mysql-heartbeat", "", ".log",
101
MY_REPLACE_EXT | MY_UNPACK_FILENAME);
102
unlink(heartbeat_filename);
103
con->heartbeat_file= my_open(heartbeat_filename, O_CREAT|O_RDWR, MYF(0));
106
No threads exist at this point in time, so this is thread safe.
108
localtime_r(&result, &tm_tmp);
109
my_snprintf(buffer, sizeof(buffer),
110
"Starting up at %02d%02d%02d %2d:%02d:%02d\n",
111
tm_tmp.tm_year % 100,
117
my_write(con->heartbeat_file, (uchar*) buffer, strlen(buffer), MYF(0));
119
pthread_attr_init(&attr);
120
pthread_attr_setdetachstate(&attr,
121
PTHREAD_CREATE_JOINABLE);
124
/* now create the thread */
125
if (pthread_create(&con->heartbeat_thread, &attr, mysql_heartbeat,
128
fprintf(stderr,"Could not create heartbeat thread!\n");
131
plugin->data= (void *)con;
138
Terminate the daemon example at server shutdown or plugin deinstallation.
141
daemon_example_plugin_deinit()
146
1 failure (cannot happen)
150
static int daemon_example_plugin_deinit(void *p)
152
DBUG_ENTER("daemon_example_plugin_deinit");
153
char buffer[HEART_STRING_BUFFER];
154
struct st_plugin_int *plugin= (struct st_plugin_int *)p;
155
struct mysql_heartbeat_context *con=
156
(struct mysql_heartbeat_context *)plugin->data;
157
time_t result= time(NULL);
160
pthread_cancel(con->heartbeat_thread);
162
localtime_r(&result, &tm_tmp);
163
my_snprintf(buffer, sizeof(buffer),
164
"Shutting down at %02d%02d%02d %2d:%02d:%02d\n",
165
tm_tmp.tm_year % 100,
171
my_write(con->heartbeat_file, (uchar*) buffer, strlen(buffer), MYF(0));
172
my_close(con->heartbeat_file, MYF(0));
174
my_free((char *)con, MYF(0));
180
struct st_mysql_daemon daemon_example_plugin=
181
{ MYSQL_DAEMON_INTERFACE_VERSION };
184
Plugin library descriptor
187
mysql_declare_plugin(daemon_example)
190
&daemon_example_plugin,
193
"Daemon example, creates a heartbeat beat file in mysql-heartbeat.log",
195
daemon_example_plugin_init, /* Plugin Init */
196
daemon_example_plugin_deinit, /* Plugin Deinit */
198
NULL, /* status variables */
199
NULL, /* system variables */
200
NULL /* config options */
202
mysql_declare_plugin_end;