Merge branch 'MXS-35' into develop

This commit is contained in:
Markus Makela 2015-09-15 11:50:36 +03:00
commit 8a996029bc
3 changed files with 70 additions and 4 deletions

View File

@ -218,6 +218,7 @@ static bool resolve_maxscale_conf_fname(
static char* check_dir_access(char* dirname,bool,bool);
static int set_user();
bool pid_file_exists();
void write_child_exit_code(int fd, int code);
/** SSL multi-threading functions and structures */
static SPINLOCK* ssl_locks;
@ -1073,6 +1074,9 @@ int main(int argc, char **argv)
int n_services;
int eno = 0; /*< local variable for errno */
int opt;
int daemon_pipe[2];
bool parent_process;
int child_status;
void** threads = NULL; /*< thread list */
char mysql_home[PATH_MAX+1];
char datadir_arg[10+PATH_MAX+1]; /*< '--datadir=' + PATH_MAX */
@ -1326,6 +1330,13 @@ int main(int argc, char **argv)
}
else
{
if(pipe(daemon_pipe) == -1)
{
fprintf(stderr,"Error: Failed to create pipe for inter-process communication: %d %s",errno,strerror(errno));
rc = MAXSCALE_INTERNALERROR;
goto return_main;
}
/*<
* Maxscale must be daemonized before opening files, initializing
* embedded MariaDB and in general, as early as possible.
@ -1458,7 +1469,37 @@ int main(int argc, char **argv)
rc = MAXSCALE_INTERNALERROR;
goto return_main;
}
gw_daemonize();
/** Daemonize the process and wait for the child process to notify
* the parent process of its exit status. */
parent_process = gw_daemonize();
if(parent_process)
{
close(daemon_pipe[1]);
int nread = read(daemon_pipe[0],(void*)&child_status,sizeof(int));
close(daemon_pipe[0]);
if(nread == -1)
{
char* logerr = "Failed to read data from child process pipe.";
print_log_n_stderr(true, true, logerr, logerr, errno);
exit(MAXSCALE_INTERNALERROR);
}
else if(nread == 0)
{
/** Child process has exited or closed write pipe */
char* logerr = "No data read from child process pipe.";
print_log_n_stderr(true, true, logerr, logerr, 0);
exit(MAXSCALE_INTERNALERROR);
}
exit(child_status);
}
/** This is the child process and we can close the read end of
* the pipe. */
close(daemon_pipe[0]);
}
/*<
* Set signal handlers for SIGHUP, SIGTERM, SIGINT and critical signals like SIGSEGV.
@ -1955,6 +1996,12 @@ int main(int argc, char **argv)
CRYPTO_set_id_callback(pthread_self);
#endif
/**
* Successful start, notify the parent process that it can exit.
*/
ss_dassert(rc == MAXSCALE_SHUTDOWN);
write_child_exit_code(daemon_pipe[1], rc);
MaxScaleStarted = time(0);
/*<
* Serve clients.
@ -1993,6 +2040,13 @@ int main(int argc, char **argv)
unlink_pidfile();
return_main:
if(daemon_mode && rc != MAXSCALE_SHUTDOWN)
{
/** Notify the parent process that an error has occurred */
write_child_exit_code(daemon_pipe[1], rc);
}
if (threads)
free(threads);
if (cnf_file_path)
@ -2505,3 +2559,14 @@ static int set_user(char* user)
return rval;
}
/**
* Write the exit status of the child process to the parent process.
* @param fd File descriptor to write to
* @param code Exit status of the child process
*/
void write_child_exit_code(int fd, int code)
{
/** Notify the parent process that an error has occurred */
write(fd, &code, sizeof (int));
close(fd);
}

View File

@ -141,7 +141,7 @@ setipaddress(struct in_addr *a, char *p) {
* Daemonize the process by forking and putting the process into the
* background.
*/
void gw_daemonize(void) {
bool gw_daemonize(void) {
pid_t pid;
pid = fork();
@ -154,7 +154,7 @@ void gw_daemonize(void) {
if (pid != 0) {
/* exit from main */
exit(0);
return true;
}
if (setsid() < 0) {
@ -162,6 +162,7 @@ void gw_daemonize(void) {
fprintf(stderr, "setsid() error %s\n", strerror_r(errno, errbuf, sizeof(errbuf)));
exit(1);
}
return false;
}
/**

View File

@ -74,7 +74,7 @@
#include "dcb.h"
void gw_daemonize(void);
bool gw_daemonize(void);
int do_read_dcb(DCB *dcb);
void MySQLListener(int epfd, char *config_bind);
int MySQLAccept(DCB *listener);