1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
|
/* $%BEGINLICENSE%$
Copyright (c) 2007, 2010, 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 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
$%ENDLICENSE%$ */
/** @file
* the user-interface for the MySQL Proxy @see main()
*
* - command-line handling
* - config-file parsing
*
*
* network_mysqld_thread() is the real proxy thread
*
* @todo move the SQL based help out into a lua script
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#ifdef _WIN32
#include <process.h> /* getpid() */
#include <io.h> /* open() */
#else
#include <unistd.h>
#include <sys/wait.h>
#include <sys/resource.h> /* for rusage in wait() */
#endif
#include <glib.h>
#include <gmodule.h>
#ifdef HAVE_LUA_H
#include <lua.h>
#include <stdio.h>
#endif
#ifdef HAVE_VALGRIND_VALGRIND_H
#include <valgrind/valgrind.h>
#endif
#ifndef HAVE_VALGRIND_VALGRIND_H
#define RUNNING_ON_VALGRIND 0
#endif
#include "network-mysqld.h"
#include "network-mysqld-proto.h"
#include "sys-pedantic.h"
#include "chassis-log.h"
#include "chassis-keyfile.h"
#include "chassis-mainloop.h"
#include "chassis-path.h"
#include "chassis-limits.h"
#include "chassis-filemode.h"
#include "chassis-win32-service.h"
#include "chassis-unix-daemon.h"
#include "chassis-frontend.h"
#include "chassis-options.h"
#ifdef WIN32
#define CHASSIS_NEWLINE "\r\n"
#else
#define CHASSIS_NEWLINE "\n"
#endif
#define GETTEXT_PACKAGE "mysql-proxy"
/**
* options of the MySQL Proxy frontend
*/
typedef struct {
int print_version;
int verbose_shutdown;
int daemon_mode;
gchar *user;
gchar *base_dir;
int auto_base_dir;
gchar *default_file;
GKeyFile *keyfile;
chassis_plugin *p;
GOptionEntry *config_entries;
gchar *pid_file;
gchar *plugin_dir;
gchar **plugin_names;
guint invoke_dbg_on_crash;
#ifndef _WIN32
/* the --keepalive option isn't available on Unix */
guint auto_restart;
#endif
gint max_files_number;
gint event_thread_count;
gchar *log_level;
gchar *log_filename;
int use_syslog;
char *lua_path;
char *lua_cpath;
char **lua_subdirs;
} chassis_frontend_t;
/**
* create a new the frontend for the chassis
*/
chassis_frontend_t *chassis_frontend_new(void) {
chassis_frontend_t *frontend;
frontend = g_slice_new0(chassis_frontend_t);
frontend->event_thread_count = 1;
frontend->max_files_number = 0;
return frontend;
}
/**
* free the frontend of the chassis
*/
void chassis_frontend_free(chassis_frontend_t *frontend) {
if (!frontend) return;
if (frontend->keyfile) g_key_file_free(frontend->keyfile);
if (frontend->default_file) g_free(frontend->default_file);
if (frontend->base_dir) g_free(frontend->base_dir);
if (frontend->user) g_free(frontend->user);
if (frontend->pid_file) g_free(frontend->pid_file);
if (frontend->log_level) g_free(frontend->log_level);
if (frontend->plugin_dir) g_free(frontend->plugin_dir);
if (frontend->plugin_names) {
g_strfreev(frontend->plugin_names);
}
if (frontend->lua_path) g_free(frontend->lua_path);
if (frontend->lua_cpath) g_free(frontend->lua_cpath);
if (frontend->lua_subdirs) g_strfreev(frontend->lua_subdirs);
g_slice_free(chassis_frontend_t, frontend);
}
/**
* setup the options of the chassis
*/
int chassis_frontend_set_chassis_options(chassis_frontend_t *frontend, chassis_options_t *opts) {
chassis_options_add(opts,
"verbose-shutdown", 0, 0, G_OPTION_ARG_NONE, &(frontend->verbose_shutdown), "Always log the exit code when shutting down", NULL);
chassis_options_add(opts,
"daemon", 0, 0, G_OPTION_ARG_NONE, &(frontend->daemon_mode), "Start in daemon-mode", NULL);
#ifndef _WIN32
chassis_options_add(opts,
"user", 0, 0, G_OPTION_ARG_STRING, &(frontend->user), "Run mysql-proxy as user", "<user>");
#endif
chassis_options_add(opts,
"basedir", 0, 0, G_OPTION_ARG_STRING, &(frontend->base_dir), "Base directory to prepend to relative paths in the config", "<absolute path>");
chassis_options_add(opts,
"pid-file", 0, 0, G_OPTION_ARG_STRING, &(frontend->pid_file), "PID file in case we are started as daemon", "<file>");
chassis_options_add(opts,
"plugin-dir", 0, 0, G_OPTION_ARG_STRING, &(frontend->plugin_dir), "path to the plugins", "<path>");
chassis_options_add(opts,
"plugins", 0, 0, G_OPTION_ARG_STRING_ARRAY, &(frontend->plugin_names), "plugins to load", "<name>");
chassis_options_add(opts,
"log-level", 0, 0, G_OPTION_ARG_STRING, &(frontend->log_level), "log all messages of level ... or higher", "(error|warning|info|message|debug)");
chassis_options_add(opts,
"log-file", 0, 0, G_OPTION_ARG_STRING, &(frontend->log_filename), "log all messages in a file", "<file>");
chassis_options_add(opts,
"log-use-syslog", 0, 0, G_OPTION_ARG_NONE, &(frontend->use_syslog), "log all messages to syslog", NULL);
chassis_options_add(opts,
"log-backtrace-on-crash", 0, 0, G_OPTION_ARG_NONE, &(frontend->invoke_dbg_on_crash), "try to invoke debugger on crash", NULL);
#ifndef _WIN32
chassis_options_add(opts,
"keepalive", 0, 0, G_OPTION_ARG_NONE, &(frontend->auto_restart), "try to restart the proxy if it crashed", NULL);
#endif
chassis_options_add(opts,
"max-open-files", 0, 0, G_OPTION_ARG_INT, &(frontend->max_files_number), "maximum number of open files (ulimit -n)", NULL);
chassis_options_add(opts,
"event-threads", 0, 0, G_OPTION_ARG_INT, &(frontend->event_thread_count), "number of event-handling threads (default: 1)", NULL);
chassis_options_add(opts,
"lua-path", 0, 0, G_OPTION_ARG_STRING, &(frontend->lua_path), "set the LUA_PATH", "<...>");
chassis_options_add(opts,
"lua-cpath", 0, 0, G_OPTION_ARG_STRING, &(frontend->lua_cpath), "set the LUA_CPATH", "<...>");
return 0;
}
static void sigsegv_handler(int G_GNUC_UNUSED signum) {
g_on_error_stack_trace(g_get_prgname());
abort(); /* trigger a SIGABRT instead of just exiting */
}
/**
* This is the "real" main which is called both on Windows and UNIX platforms.
* For the Windows service case, this will also handle the notifications and set
* up the logging support appropriately.
*/
int main_cmdline(int argc, char **argv) {
chassis *srv = NULL;
#ifdef HAVE_SIGACTION
static struct sigaction sigsegv_sa;
#endif
/* read the command-line options */
GOptionContext *option_ctx = NULL;
GOptionEntry *main_entries = NULL;
chassis_frontend_t *frontend = NULL;
chassis_options_t *opts = NULL;
GError *gerr = NULL;
chassis_log *log = NULL;
/* a little helper macro to set the src-location that we stepped out at to exit */
#define GOTO_EXIT(status) \
exit_code = status; \
exit_location = G_STRLOC; \
goto exit_nicely;
int exit_code = EXIT_SUCCESS;
const gchar *exit_location = G_STRLOC;
if (chassis_frontend_init_glib()) { /* init the thread, module, ... system */
GOTO_EXIT(EXIT_FAILURE);
}
/* start the logging ... to stderr */
log = chassis_log_new();
log->min_lvl = G_LOG_LEVEL_MESSAGE; /* display messages while parsing or loading plugins */
g_log_set_default_handler(chassis_log_func, log);
#ifdef _WIN32
if (chassis_win32_is_service() && chassis_log_set_event_log(log, g_get_prgname())) {
GOTO_EXIT(EXIT_FAILURE);
}
if (chassis_frontend_init_win32()) { /* setup winsock */
GOTO_EXIT(EXIT_FAILURE);
}
#endif
/* may fail on library mismatch */
if (NULL == (srv = chassis_new())) {
GOTO_EXIT(EXIT_FAILURE);
}
srv->log = log; /* we need the log structure for the log-rotation */
frontend = chassis_frontend_new();
option_ctx = g_option_context_new("- MySQL Proxy");
/**
* parse once to get the basic options like --defaults-file and --version
*
* leave the unknown options in the list
*/
if (chassis_frontend_init_base_options(option_ctx,
&argc, &argv,
&(frontend->print_version),
&(frontend->default_file),
&gerr)) {
g_critical("%s: %s",
G_STRLOC,
gerr->message);
g_clear_error(&gerr);
GOTO_EXIT(EXIT_FAILURE);
}
if (frontend->default_file) {
if (!(frontend->keyfile = chassis_frontend_open_config_file(frontend->default_file, &gerr))) {
g_critical("%s: loading config from '%s' failed: %s",
G_STRLOC,
frontend->default_file,
gerr->message);
g_clear_error(&gerr);
GOTO_EXIT(EXIT_FAILURE);
}
}
/* print the main version number here, but don't exit
* we check for print_version again, after loading the plugins (if any)
* and print their version numbers, too. then we exit cleanly.
*/
if (frontend->print_version) {
#ifndef CHASSIS_BUILD_TAG
#define CHASSIS_BUILD_TAG PACKAGE_STRING
#endif
g_print("%s" CHASSIS_NEWLINE, CHASSIS_BUILD_TAG);
chassis_frontend_print_version();
}
/* add the other options which can also appear in the configfile */
opts = chassis_options_new();
chassis_frontend_set_chassis_options(frontend, opts);
main_entries = chassis_options_to_g_option_entries(opts);
g_option_context_add_main_entries(option_ctx, main_entries, NULL);
/**
* parse once to get the basic options
*
* leave the unknown options in the list
*/
if (FALSE == g_option_context_parse(option_ctx, &argc, &argv, &gerr)) {
g_critical("%s", gerr->message);
GOTO_EXIT(EXIT_FAILURE);
}
if (frontend->keyfile) {
if (chassis_keyfile_to_options(frontend->keyfile, "mysql-proxy", main_entries)) {
GOTO_EXIT(EXIT_FAILURE);
}
}
if (chassis_frontend_init_basedir(argv[0], &(frontend->base_dir))) {
GOTO_EXIT(EXIT_FAILURE);
}
/* basic setup is done, base-dir is known, ... */
frontend->lua_subdirs = g_new(char *, 2);
frontend->lua_subdirs[0] = g_strdup("mysql-proxy");
frontend->lua_subdirs[1] = NULL;
if (chassis_frontend_init_lua_path(frontend->lua_path, frontend->base_dir, frontend->lua_subdirs)) {
GOTO_EXIT(EXIT_FAILURE);
}
if (chassis_frontend_init_lua_cpath(frontend->lua_cpath, frontend->base_dir, frontend->lua_subdirs)) {
GOTO_EXIT(EXIT_FAILURE);
}
/* assign the mysqld part to the */
network_mysqld_init(srv); /* starts the also the lua-scope, LUA_PATH and LUA_CPATH have to be set before this being called */
#ifdef HAVE_SIGACTION
/* register the sigsegv interceptor */
memset(&sigsegv_sa, 0, sizeof(sigsegv_sa));
sigsegv_sa.sa_handler = sigsegv_handler;
sigemptyset(&sigsegv_sa.sa_mask);
if (frontend->invoke_dbg_on_crash && !(RUNNING_ON_VALGRIND)) {
sigaction(SIGSEGV, &sigsegv_sa, NULL);
}
#endif
/*
* some plugins cannot see the chassis struct from the point
* where they open files, hence we must make it available
*/
srv->base_dir = g_strdup(frontend->base_dir);
chassis_frontend_init_plugin_dir(&frontend->plugin_dir, srv->base_dir);
/*
* these are used before we gathered all the options
* from the plugins, thus we need to fix them up before
* dealing with all the rest.
*/
chassis_resolve_path(srv->base_dir, &frontend->log_filename);
chassis_resolve_path(srv->base_dir, &frontend->pid_file);
chassis_resolve_path(srv->base_dir, &frontend->plugin_dir);
/*
* start the logging
*/
if (frontend->log_filename) {
log->log_filename = g_strdup(frontend->log_filename);
}
log->use_syslog = frontend->use_syslog;
if (log->log_filename && log->use_syslog) {
g_critical("%s: log-file and log-use-syslog were given, but only one is allowed",
G_STRLOC);
GOTO_EXIT(EXIT_FAILURE);
}
if (log->log_filename && FALSE == chassis_log_open(log)) {
g_critical("can't open log-file '%s': %s", log->log_filename, g_strerror(errno));
GOTO_EXIT(EXIT_FAILURE);
}
/* handle log-level after the config-file is read, just in case it is specified in the file */
if (frontend->log_level) {
if (0 != chassis_log_set_level(log, frontend->log_level)) {
g_critical("--log-level=... failed, level '%s' is unknown ",
frontend->log_level);
GOTO_EXIT(EXIT_FAILURE);
}
} else {
/* if it is not set, use "critical" as default */
log->min_lvl = G_LOG_LEVEL_CRITICAL;
}
/*
* the MySQL Proxy should load 'admin' and 'proxy' plugins
*/
if (!frontend->plugin_names) {
frontend->plugin_names = g_new(char *, 3);
frontend->plugin_names[0] = g_strdup("admin");
frontend->plugin_names[1] = g_strdup("proxy");
frontend->plugin_names[2] = NULL;
}
if (chassis_frontend_load_plugins(srv->modules,
frontend->plugin_dir,
frontend->plugin_names)) {
GOTO_EXIT(EXIT_FAILURE);
}
if (chassis_frontend_init_plugins(srv->modules,
option_ctx,
&argc, &argv,
frontend->keyfile,
"mysql-proxy",
srv->base_dir,
&gerr)) {
g_critical("%s: %s",
G_STRLOC,
gerr->message);
g_clear_error(&gerr);
GOTO_EXIT(EXIT_FAILURE);
}
/* if we only print the version numbers, exit and don't do any more work */
if (frontend->print_version) {
chassis_frontend_print_lua_version();
chassis_frontend_print_plugin_versions(srv->modules);
GOTO_EXIT(EXIT_SUCCESS);
}
/* we know about the options now, lets parse them */
g_option_context_set_help_enabled(option_ctx, TRUE);
g_option_context_set_ignore_unknown_options(option_ctx, FALSE);
/* handle unknown options */
if (FALSE == g_option_context_parse(option_ctx, &argc, &argv, &gerr)) {
if (gerr->domain == G_OPTION_ERROR &&
gerr->code == G_OPTION_ERROR_UNKNOWN_OPTION) {
g_critical("%s: %s (use --help to show all options)",
G_STRLOC,
gerr->message);
} else {
g_critical("%s: %s (code = %d, domain = %s)",
G_STRLOC,
gerr->message,
gerr->code,
g_quark_to_string(gerr->domain)
);
}
GOTO_EXIT(EXIT_FAILURE);
}
g_option_context_free(option_ctx);
option_ctx = NULL;
/* after parsing the options we should only have the program name left */
if (argc > 1) {
g_critical("unknown option: %s", argv[1]);
GOTO_EXIT(EXIT_FAILURE);
}
/* make sure that he max-thread-count isn't negative */
if (frontend->event_thread_count < 1) {
g_critical("--event-threads has to be >= 1, is %d", frontend->event_thread_count);
GOTO_EXIT(EXIT_FAILURE);
}
srv->event_thread_count = frontend->event_thread_count;
#ifndef _WIN32
signal(SIGPIPE, SIG_IGN);
if (frontend->daemon_mode) {
chassis_unix_daemonize();
}
if (frontend->auto_restart) {
int child_exit_status = EXIT_SUCCESS; /* forward the exit-status of the child */
int ret = chassis_unix_proc_keepalive(&child_exit_status);
if (ret > 0) {
/* the agent stopped */
exit_code = child_exit_status;
goto exit_nicely;
} else if (ret < 0) {
GOTO_EXIT(EXIT_FAILURE);
} else {
/* we are the child, go on */
}
}
#endif
if (frontend->pid_file) {
if (0 != chassis_frontend_write_pidfile(frontend->pid_file, &gerr)) {
g_critical("%s", gerr->message);
g_clear_error(&gerr);
GOTO_EXIT(EXIT_FAILURE);
}
}
/* the message has to be _after_ the g_option_content_parse() to
* hide from the output if the --help is asked for
*/
g_message("%s started", PACKAGE_STRING); /* add tag to the logfile (after we opened the logfile) */
#ifdef _WIN32
if (chassis_win32_is_service()) chassis_win32_service_set_state(SERVICE_RUNNING, 0);
#endif
/*
* we have to drop root privileges in chassis_mainloop() after
* the plugins opened the ports, so we need the user there
*/
srv->user = g_strdup(frontend->user);
if (frontend->max_files_number) {
if (0 != chassis_fdlimit_set(frontend->max_files_number)) {
g_critical("%s: setting fdlimit = %d failed: %s (%d)",
G_STRLOC,
frontend->max_files_number,
g_strerror(errno),
errno);
GOTO_EXIT(EXIT_FAILURE);
}
}
g_debug("max open file-descriptors = %"G_GINT64_FORMAT,
chassis_fdlimit_get());
if (chassis_mainloop(srv)) {
/* looks like we failed */
g_critical("%s: Failure from chassis_mainloop. Shutting down.", G_STRLOC);
GOTO_EXIT(EXIT_FAILURE);
}
exit_nicely:
/* necessary to set the shutdown flag, because the monitor will continue
* to schedule timers otherwise, causing an infinite loop in cleanup
*/
if (!exit_code) {
exit_location = G_STRLOC;
}
chassis_set_shutdown_location(exit_location);
if (!frontend->print_version) {
g_log(G_LOG_DOMAIN, (frontend->verbose_shutdown ? G_LOG_LEVEL_CRITICAL : G_LOG_LEVEL_MESSAGE),
"shutting down normally, exit code is: %d", exit_code); /* add a tag to the logfile */
}
#ifdef _WIN32
if (chassis_win32_is_service()) chassis_win32_service_set_state(SERVICE_STOP_PENDING, 0);
#endif
chassis_frontend_free(frontend);
if (gerr) g_error_free(gerr);
if (option_ctx) g_option_context_free(option_ctx);
if (srv) chassis_free(srv);
if (opts) chassis_options_free(opts);
if (main_entries) g_free(main_entries);
chassis_log_free(log);
#ifdef _WIN32
if (chassis_win32_is_service()) chassis_win32_service_set_state(SERVICE_STOPPED, 0);
#endif
#ifdef HAVE_SIGACTION
/* reset the handler */
sigsegv_sa.sa_handler = SIG_DFL;
if (frontend->invoke_dbg_on_crash && !(RUNNING_ON_VALGRIND)) {
sigaction(SIGSEGV, &sigsegv_sa, NULL);
}
#endif
return exit_code;
}
/**
* On Windows we first look if we are started as a service and
* set that up if appropriate.
* We eventually fall down through to main_cmdline, even on Windows.
*/
int main(int argc, char **argv) {
#ifdef WIN32_AS_SERVICE
return main_win32(argc, argv, main_cmdline);
#else
return main_cmdline(argc, argv);
#endif
}
|