summaryrefslogtreecommitdiff
path: root/server-tools/instance-manager/mysqlmanager.cc
diff options
context:
space:
mode:
authorunknown <petr@mysql.com>2004-12-09 04:22:28 +0300
committerunknown <petr@mysql.com>2004-12-09 04:22:28 +0300
commitf7ef17dda1a062d74028ca2967b3d8506354d803 (patch)
tree6b81e45da104e71d355f27b8abaf834a2f10eeab /server-tools/instance-manager/mysqlmanager.cc
parentd11d6d697113297de7f2533703f3db89d1b7835d (diff)
parentb5536bb962da6751bbc62ef9b7aba720bb04d018 (diff)
downloadmariadb-git-f7ef17dda1a062d74028ca2967b3d8506354d803.tar.gz
merge
BitKeeper/etc/ignore: auto-union server-tools/instance-manager/mysqlmanager.cc: Auto merged sql/net_serv.cc: Auto merged
Diffstat (limited to 'server-tools/instance-manager/mysqlmanager.cc')
-rw-r--r--server-tools/instance-manager/mysqlmanager.cc69
1 files changed, 38 insertions, 31 deletions
diff --git a/server-tools/instance-manager/mysqlmanager.cc b/server-tools/instance-manager/mysqlmanager.cc
index cb2d2e1c5d5..9ad8ce2c858 100644
--- a/server-tools/instance-manager/mysqlmanager.cc
+++ b/server-tools/instance-manager/mysqlmanager.cc
@@ -1,4 +1,4 @@
-/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
+/* Copyright (C) 2003 MySQL AB & MySQL Finland AB & TCX DataKonsult 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
@@ -14,20 +14,21 @@
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+#include "manager.h"
+#include "options.h"
+#include "log.h"
+
#include <my_global.h>
#include <my_sys.h>
#include <string.h>
#include <signal.h>
#include <sys/wait.h>
-#include <sys/stat.h>
#include <sys/types.h>
+#include <sys/stat.h>
-#include "manager.h"
-#include "options.h"
-#include "log.h"
/*
- Few notes about the Instance Manager architecture:
+ Few notes about Instance Manager architecture:
Instance Manager consisits of two processes: the angel process, and the
instance manager process. Responsibilities of the angel process is to
monitor the instance manager process, and restart it in case of
@@ -35,19 +36,19 @@
'--run-as-service' is provided.
The Instance Manager process consists of several
subsystems (thread sets):
- - the signal handling thread: it's responsibilities are to handle
+ - the signal handling thread: it's responsibilities are to handle
user signals and propogate them to the other threads. All other threads
- are accounted in the signal handler thread Thread Repository.
- - the listener: listens all sockets. There is a listening
+ are accounted in the signal handler thread Thread Registry.
+ - the listener: listens all sockets. There is a listening
socket for each (mysql, http, snmp, rendezvous (?)) subsystem.
- mysql subsystem: Instance Manager acts like an ordinary MySQL Server,
- but with very restricted command set. Each MySQL client connection is
+ but with very restricted command set. Each MySQL client connection is
handled in a separate thread. All MySQL client connections threads
constitute mysql subsystem.
- - http subsystem: it is also possible to talk with Instance Manager via
+ - http subsystem: it is also possible to talk with Instance Manager via
http. One thread per http connection is used. Threads are pooled.
- 'snmp' connections (FIXME: I know nothing about it yet)
- - rendezvous threads
+ - rendezvous threads
*/
static void init_environment(char *progname);
@@ -70,11 +71,12 @@ int main(int argc, char *argv[])
options.load(argc, argv);
if (options.run_as_service)
{
+ /* forks, and returns only in child */
daemonize(options.log_file_name);
+ /* forks again, and returns only in child: parent becomes angel */
angel(options);
}
- else
- manager(options.log_file_name);
+ manager(options);
return 0;
}
@@ -90,6 +92,7 @@ static void init_environment(char *progname)
MY_INIT(progname);
log_init();
umask(0117);
+ srand(time(0));
}
@@ -109,8 +112,8 @@ static void daemonize(const char *log_file_name)
int fd;
/*
Become a session leader: setsid must succeed because child is
- guaranteed not to be a process group leader (it belongs to the
- process group of the parent.)
+ guaranteed not to be a process group leader (it belongs to the
+ process group of the parent.)
The goal is not to have a controlling terminal.
*/
setsid();
@@ -121,7 +124,7 @@ static void daemonize(const char *log_file_name)
close(STDIN_FILENO);
- fd= open(log_file_name, O_WRONLY | O_CREAT | O_APPEND | O_NOCTTY,
+ fd= open(log_file_name, O_WRONLY | O_CREAT | O_APPEND | O_NOCTTY,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
if (fd < 0)
die("daemonize(): failed to open log file %s, %s", log_file_name,
@@ -133,7 +136,7 @@ static void daemonize(const char *log_file_name)
/* TODO: chroot() and/or chdir() here */
break;
- default:
+ default:
/* successfully exit from parent */
exit(0);
}
@@ -144,13 +147,13 @@ enum { CHILD_OK= 0, CHILD_NEED_RESPAWN, CHILD_EXIT_ANGEL };
static volatile sig_atomic_t child_status= CHILD_OK;
-/*
+/*
Signal handler for SIGCHLD: reap child, analyze child exit status, and set
child_status appropriately.
*/
void reap_child(int __attribute__((unused)) signo)
-{
+{
int child_exit_status;
/* As we have only one child, no need to cycle waitpid */
if (waitpid(0, &child_exit_status, WNOHANG) > 0)
@@ -159,16 +162,14 @@ void reap_child(int __attribute__((unused)) signo)
child_status= CHILD_NEED_RESPAWN;
else
/*
- As we reap_child is not called for SIGSTOP, we should be here only
+ As reap_child is not called for SIGSTOP, we should be here only
if the child exited normally.
*/
child_status= CHILD_EXIT_ANGEL;
}
}
-/* Not static to reuse it in childs */
-
-volatile sig_atomic_t is_terminated= 0;
+static volatile sig_atomic_t is_terminated= 0;
/*
Signal handler for terminate signals - SIGTERM, SIGHUP, SIGINT.
@@ -178,7 +179,7 @@ volatile sig_atomic_t is_terminated= 0;
void terminate(int signo)
{
- is_terminated= signo;
+ is_terminated= signo;
}
@@ -215,18 +216,18 @@ spawn:
pid_t pid= fork();
switch (pid) {
case -1:
- die("angel(): fork failed, %s", strerror(errno));
+ die("angel(): fork failed, %s", strerror(errno));
case 0: // child, success
/*
restore default actions for signals to let the manager work with
signals as he wishes
- */
+ */
sigaction(SIGCHLD, &sa_chld_out, 0);
sigaction(SIGTERM, &sa_term_out, 0);
sigaction(SIGINT, &sa_int_out, 0);
sigaction(SIGHUP, &sa_hup_out, 0);
-
- manager(options.socket_file_name);
+ /* Here we return to main, and fall into manager */
+ break;
default: // parent, success
while (child_status == CHILD_OK && is_terminated == 0)
sigsuspend(&zeromask);
@@ -234,12 +235,18 @@ spawn:
if (is_terminated)
log_info("angel got signal %d, exiting", is_terminated);
else if (child_status == CHILD_NEED_RESPAWN)
- {
+ {
child_status= CHILD_OK;
log_error("angel(): mysqlmanager exited abnormally: respawning...");
sleep(1); /* don't respawn too fast */
goto spawn;
}
- /* mysqlmanager successfully exited, let's silently evaporate */
+ /*
+ mysqlmanager successfully exited, let's silently evaporate
+ If we return to main we fall into the manager() function, so let's
+ simply exit().
+ */
+ exit(0);
}
}
+