MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
daemon_example.cc
1 /* Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
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 <my_global.h>
17 #include <sql_priv.h>
18 #include <stdlib.h>
19 #include <ctype.h>
20 #include <mysql_version.h>
21 #include <mysql/plugin.h>
22 #include <my_dir.h>
23 #include "my_pthread.h" // pthread_handler_t
24 #include "my_sys.h" // my_write, my_malloc
25 #include "m_string.h" // strlen
26 #include "sql_plugin.h" // st_plugin_int
27 
28 /*
29  Disable __attribute__() on non-gcc compilers.
30 */
31 #if !defined(__attribute__) && !defined(__GNUC__)
32 #define __attribute__(A)
33 #endif
34 
35 
36 #define HEART_STRING_BUFFER 100
37 
39 {
40  pthread_t heartbeat_thread;
41  File heartbeat_file;
42 };
43 
44 pthread_handler_t mysql_heartbeat(void *p)
45 {
46  DBUG_ENTER("mysql_heartbeat");
47  struct mysql_heartbeat_context *con= (struct mysql_heartbeat_context *)p;
48  char buffer[HEART_STRING_BUFFER];
49  time_t result;
50  struct tm tm_tmp;
51 
52  while(1)
53  {
54  sleep(5);
55 
56  result= time(NULL);
57  localtime_r(&result, &tm_tmp);
58  my_snprintf(buffer, sizeof(buffer),
59  "Heartbeat at %02d%02d%02d %2d:%02d:%02d\n",
60  tm_tmp.tm_year % 100,
61  tm_tmp.tm_mon+1,
62  tm_tmp.tm_mday,
63  tm_tmp.tm_hour,
64  tm_tmp.tm_min,
65  tm_tmp.tm_sec);
66  my_write(con->heartbeat_file, (uchar*) buffer, strlen(buffer), MYF(0));
67  }
68 
69  DBUG_RETURN(0);
70 }
71 
72 /*
73  Initialize the daemon example at server start or plugin installation.
74 
75  SYNOPSIS
76  daemon_example_plugin_init()
77 
78  DESCRIPTION
79  Starts up heartbeatbeat thread
80 
81  RETURN VALUE
82  0 success
83  1 failure (cannot happen)
84 */
85 
86 static int daemon_example_plugin_init(void *p)
87 {
88 
89  DBUG_ENTER("daemon_example_plugin_init");
90  struct mysql_heartbeat_context *con;
91  pthread_attr_t attr; /* Thread attributes */
92  char heartbeat_filename[FN_REFLEN];
93  char buffer[HEART_STRING_BUFFER];
94  time_t result= time(NULL);
95  struct tm tm_tmp;
96 
97  struct st_plugin_int *plugin= (struct st_plugin_int *)p;
98 
99  con= (struct mysql_heartbeat_context *)
100  my_malloc(sizeof(struct mysql_heartbeat_context), MYF(0));
101 
102  fn_format(heartbeat_filename, "mysql-heartbeat", "", ".log",
103  MY_REPLACE_EXT | MY_UNPACK_FILENAME);
104  unlink(heartbeat_filename);
105  con->heartbeat_file= my_open(heartbeat_filename, O_CREAT|O_RDWR, MYF(0));
106 
107  /*
108  No threads exist at this point in time, so this is thread safe.
109  */
110  localtime_r(&result, &tm_tmp);
111  my_snprintf(buffer, sizeof(buffer),
112  "Starting up at %02d%02d%02d %2d:%02d:%02d\n",
113  tm_tmp.tm_year % 100,
114  tm_tmp.tm_mon+1,
115  tm_tmp.tm_mday,
116  tm_tmp.tm_hour,
117  tm_tmp.tm_min,
118  tm_tmp.tm_sec);
119  my_write(con->heartbeat_file, (uchar*) buffer, strlen(buffer), MYF(0));
120 
121  pthread_attr_init(&attr);
122  pthread_attr_setdetachstate(&attr,
123  PTHREAD_CREATE_JOINABLE);
124 
125 
126  /* now create the thread */
127  if (pthread_create(&con->heartbeat_thread, &attr, mysql_heartbeat,
128  (void *)con) != 0)
129  {
130  fprintf(stderr,"Could not create heartbeat thread!\n");
131  exit(0);
132  }
133  plugin->data= (void *)con;
134 
135  DBUG_RETURN(0);
136 }
137 
138 
139 /*
140  Terminate the daemon example at server shutdown or plugin deinstallation.
141 
142  SYNOPSIS
143  daemon_example_plugin_deinit()
144  Does nothing.
145 
146  RETURN VALUE
147  0 success
148  1 failure (cannot happen)
149 
150 */
151 
152 static int daemon_example_plugin_deinit(void *p)
153 {
154  DBUG_ENTER("daemon_example_plugin_deinit");
155  char buffer[HEART_STRING_BUFFER];
156  struct st_plugin_int *plugin= (struct st_plugin_int *)p;
157  struct mysql_heartbeat_context *con=
158  (struct mysql_heartbeat_context *)plugin->data;
159  time_t result= time(NULL);
160  struct tm tm_tmp;
161  void *dummy_retval;
162 
163  pthread_cancel(con->heartbeat_thread);
164 
165  localtime_r(&result, &tm_tmp);
166  my_snprintf(buffer, sizeof(buffer),
167  "Shutting down at %02d%02d%02d %2d:%02d:%02d\n",
168  tm_tmp.tm_year % 100,
169  tm_tmp.tm_mon+1,
170  tm_tmp.tm_mday,
171  tm_tmp.tm_hour,
172  tm_tmp.tm_min,
173  tm_tmp.tm_sec);
174  my_write(con->heartbeat_file, (uchar*) buffer, strlen(buffer), MYF(0));
175 
176  /*
177  Need to wait for the hearbeat thread to terminate before closing
178  the file it writes to and freeing the memory it uses
179  */
180  pthread_join(con->heartbeat_thread, &dummy_retval);
181 
182  my_close(con->heartbeat_file, MYF(0));
183 
184  my_free(con);
185 
186  DBUG_RETURN(0);
187 }
188 
189 
190 struct st_mysql_daemon daemon_example_plugin=
191 { MYSQL_DAEMON_INTERFACE_VERSION };
192 
193 /*
194  Plugin library descriptor
195 */
196 
197 mysql_declare_plugin(daemon_example)
198 {
199  MYSQL_DAEMON_PLUGIN,
200  &daemon_example_plugin,
201  "daemon_example",
202  "Brian Aker",
203  "Daemon example, creates a heartbeat beat file in mysql-heartbeat.log",
204  PLUGIN_LICENSE_GPL,
205  daemon_example_plugin_init, /* Plugin Init */
206  daemon_example_plugin_deinit, /* Plugin Deinit */
207  0x0100 /* 1.0 */,
208  NULL, /* status variables */
209  NULL, /* system variables */
210  NULL, /* config options */
211  0, /* flags */
212 }
213 mysql_declare_plugin_end;