~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to plugin/daemon_example/daemon_example.cc

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2006 MySQL AB
 
2
 
 
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.
 
6
 
 
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.
 
11
 
 
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 */
 
15
 
 
16
#include <mysql_priv.h>
 
17
#include <stdlib.h>
 
18
#include <ctype.h>
 
19
#include <mysql_version.h>
 
20
#include <mysql/plugin.h>
 
21
#include <my_global.h>
 
22
#include <my_dir.h>
 
23
 
 
24
/*
 
25
  Disable __attribute__() on non-gcc compilers.
 
26
*/
 
27
#if !defined(__attribute__) && !defined(__GNUC__)
 
28
#define __attribute__(A)
 
29
#endif
 
30
 
 
31
 
 
32
#define HEART_STRING_BUFFER 100
 
33
  
 
34
struct mysql_heartbeat_context
 
35
{
 
36
  pthread_t heartbeat_thread;
 
37
  File heartbeat_file;
 
38
};
 
39
 
 
40
pthread_handler_t mysql_heartbeat(void *p)
 
41
{
 
42
  DBUG_ENTER("mysql_heartbeat");
 
43
  struct mysql_heartbeat_context *con= (struct mysql_heartbeat_context *)p;
 
44
  char buffer[HEART_STRING_BUFFER];
 
45
  unsigned int x= 0;
 
46
  time_t result;
 
47
  struct tm tm_tmp;
 
48
 
 
49
  while(1)
 
50
  {
 
51
    sleep(5);
 
52
 
 
53
    result= time(NULL);
 
54
    localtime_r(&result, &tm_tmp);
 
55
    my_snprintf(buffer, sizeof(buffer),
 
56
                "Heartbeat at %02d%02d%02d %2d:%02d:%02d\n",
 
57
                tm_tmp.tm_year % 100,
 
58
                tm_tmp.tm_mon+1,
 
59
                tm_tmp.tm_mday,
 
60
                tm_tmp.tm_hour,
 
61
                tm_tmp.tm_min,
 
62
                tm_tmp.tm_sec);
 
63
    my_write(con->heartbeat_file, (uchar*) buffer, strlen(buffer), MYF(0));
 
64
    x++;
 
65
  }
 
66
 
 
67
  DBUG_RETURN(0);
 
68
}
 
69
 
 
70
/*
 
71
  Initialize the daemon example at server start or plugin installation.
 
72
 
 
73
  SYNOPSIS
 
74
    daemon_example_plugin_init()
 
75
 
 
76
  DESCRIPTION
 
77
    Starts up heartbeatbeat thread
 
78
 
 
79
  RETURN VALUE
 
80
    0                    success
 
81
    1                    failure (cannot happen)
 
82
*/
 
83
 
 
84
static int daemon_example_plugin_init(void *p)
 
85
{
 
86
 
 
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);
 
93
  struct tm tm_tmp;
 
94
 
 
95
  struct st_plugin_int *plugin= (struct st_plugin_int *)p;
 
96
 
 
97
  con= (struct mysql_heartbeat_context *)
 
98
    my_malloc(sizeof(struct mysql_heartbeat_context), MYF(0)); 
 
99
 
 
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));
 
104
 
 
105
  /*
 
106
    No threads exist at this point in time, so this is thread safe.
 
107
  */
 
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,
 
112
              tm_tmp.tm_mon+1,
 
113
              tm_tmp.tm_mday,
 
114
              tm_tmp.tm_hour,
 
115
              tm_tmp.tm_min,
 
116
              tm_tmp.tm_sec);
 
117
  my_write(con->heartbeat_file, (uchar*) buffer, strlen(buffer), MYF(0));
 
118
 
 
119
  pthread_attr_init(&attr);
 
120
  pthread_attr_setdetachstate(&attr,
 
121
                              PTHREAD_CREATE_JOINABLE);
 
122
 
 
123
 
 
124
  /* now create the thread */
 
125
  if (pthread_create(&con->heartbeat_thread, &attr, mysql_heartbeat,
 
126
                     (void *)con) != 0)
 
127
  {
 
128
    fprintf(stderr,"Could not create heartbeat thread!\n");
 
129
    exit(0);
 
130
  }
 
131
  plugin->data= (void *)con;
 
132
 
 
133
  DBUG_RETURN(0);
 
134
}
 
135
 
 
136
 
 
137
/*
 
138
  Terminate the daemon example at server shutdown or plugin deinstallation.
 
139
 
 
140
  SYNOPSIS
 
141
    daemon_example_plugin_deinit()
 
142
    Does nothing.
 
143
 
 
144
  RETURN VALUE
 
145
    0                    success
 
146
    1                    failure (cannot happen)
 
147
 
 
148
*/
 
149
 
 
150
static int daemon_example_plugin_deinit(void *p)
 
151
{
 
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);
 
158
  struct tm tm_tmp;
 
159
 
 
160
  pthread_cancel(con->heartbeat_thread);
 
161
 
 
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,
 
166
              tm_tmp.tm_mon+1,
 
167
              tm_tmp.tm_mday,
 
168
              tm_tmp.tm_hour,
 
169
              tm_tmp.tm_min,
 
170
              tm_tmp.tm_sec);
 
171
  my_write(con->heartbeat_file, (uchar*) buffer, strlen(buffer), MYF(0));
 
172
  my_close(con->heartbeat_file, MYF(0));
 
173
 
 
174
  my_free((char *)con, MYF(0));
 
175
 
 
176
  DBUG_RETURN(0);
 
177
}
 
178
 
 
179
 
 
180
struct st_mysql_daemon daemon_example_plugin=
 
181
{ MYSQL_DAEMON_INTERFACE_VERSION  };
 
182
 
 
183
/*
 
184
  Plugin library descriptor
 
185
*/
 
186
 
 
187
mysql_declare_plugin(daemon_example)
 
188
{
 
189
  MYSQL_DAEMON_PLUGIN,
 
190
  &daemon_example_plugin,
 
191
  "daemon_example",
 
192
  "Brian Aker",
 
193
  "Daemon example, creates a heartbeat beat file in mysql-heartbeat.log",
 
194
  PLUGIN_LICENSE_GPL,
 
195
  daemon_example_plugin_init, /* Plugin Init */
 
196
  daemon_example_plugin_deinit, /* Plugin Deinit */
 
197
  0x0100 /* 1.0 */,
 
198
  NULL,                       /* status variables                */
 
199
  NULL,                       /* system variables                */
 
200
  NULL                        /* config options                  */
 
201
}
 
202
mysql_declare_plugin_end;