mysql_test_run.c and associated files, with my initial changes and WAX's changes for Windows
This commit is contained in:
parent
19efafcc13
commit
6f60424629
@ -22,6 +22,7 @@ carsten@tsort.bitbybit.dk
|
||||
davida@isil.mysql.com
|
||||
gluh@gluh.(none)
|
||||
gluh@gluh.mysql.r18.ru
|
||||
greg@gcw.ath.cx
|
||||
greg@mysql.com
|
||||
guilhem@mysql.com
|
||||
gweir@work.mysql.com
|
||||
|
@ -1,4 +1,4 @@
|
||||
# Copyright (c) 2002 Novell, Inc. All Rights Reserved.
|
||||
# Copyright (c) 2002 Novell, Inc. 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
|
||||
@ -11,37 +11,37 @@
|
||||
# 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
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
INCLUDES= -I$(srcdir)/../include -I../include -I..
|
||||
bin_PROGRAMS= mysqld_safe mysql_install_db mysql_test_run libmysql
|
||||
mysqld_safe_SOURCES= mysqld_safe.c my_manage.c
|
||||
mysql_install_db_SOURCES= mysql_install_db.c my_manage.c
|
||||
mysql_test_run_SOURCES= mysql_test_run.c my_manage.c
|
||||
libmysql_SOURCES= libmysqlmain.c
|
||||
libmysql_LDADD= ../libmysql/.libs/libmysqlclient.a
|
||||
INCLUDES= -I$(srcdir)/../include -I../include -I..
|
||||
bin_PROGRAMS= mysqld_safe mysql_install_db mysql_test_run libmysql
|
||||
mysqld_safe_SOURCES= mysqld_safe.c my_manage.c
|
||||
mysql_install_db_SOURCES= mysql_install_db.c my_manage.c
|
||||
mysql_test_run_SOURCES= mysql_test_run.c my_manage.c
|
||||
libmysql_SOURCES= libmysqlmain.c
|
||||
libmysql_LDADD= ../libmysql/.libs/libmysqlclient.a
|
||||
|
||||
netware_build_files = client/mysql.def client/mysqladmin.def \
|
||||
client/mysqlbinlog.def client/mysqlcheck.def \
|
||||
client/mysqldump.def client/mysqlimport.def \
|
||||
client/mysqlshow.def client/mysqltest.def \
|
||||
extra/mysql_install.def extra/my_print_defaults.def \
|
||||
extra/perror.def extra/replace.def \
|
||||
extra/resolveip.def extra/comp_err.def \
|
||||
isam/isamchk.def \
|
||||
isam/isamlog.def isam/pack_isam.def \
|
||||
libmysqld/libmysqld.def myisam/myisamchk.def \
|
||||
myisam/myisamlog.def myisam/myisampack.def \
|
||||
sql/mysqld.def
|
||||
|
||||
netware_build_files = client/mysql.def client/mysqladmin.def \
|
||||
client/mysqlbinlog.def client/mysqlcheck.def \
|
||||
client/mysqldump.def client/mysqlimport.def \
|
||||
client/mysqlshow.def client/mysqltest.def \
|
||||
extra/mysql_install.def extra/my_print_defaults.def \
|
||||
extra/perror.def extra/replace.def \
|
||||
extra/resolveip.def extra/comp_err.def \
|
||||
isam/isamchk.def \
|
||||
isam/isamlog.def isam/pack_isam.def \
|
||||
libmysqld/libmysqld.def myisam/myisamchk.def \
|
||||
myisam/myisamlog.def myisam/myisampack.def \
|
||||
sql/mysqld.def
|
||||
|
||||
link_sources:
|
||||
set -x; \
|
||||
for f in $(netware_build_files); do \
|
||||
rm -f $(srcdir)/../$$f; \
|
||||
org=`echo $$f | sed -e 's/.*\/\(.*\)/\1/g'`; \
|
||||
@LN_CP_F@ $(srcdir)/$$org $(srcdir)/../$$f; \
|
||||
done;
|
||||
set -x; \
|
||||
for f in $(netware_build_files); do \
|
||||
rm -f $(srcdir)/../$$f; \
|
||||
org=`echo $$f | sed -e 's/.*\/\(.*\)/\1/g'`; \
|
||||
@LN_CP_F@ $(srcdir)/$$org $(srcdir)/../$$f; \
|
||||
done;
|
||||
|
||||
# Don't update the files from bitkeeper
|
||||
%::SCCS/s.%
|
||||
|
@ -1,90 +1,104 @@
|
||||
/*
|
||||
Copyright (c) 2003 Novell, Inc. All Rights Reserved.
|
||||
Copyright (c) 2003 MySQL AB
|
||||
Copyright (c) 2003 Novell, Inc.
|
||||
|
||||
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; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
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; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
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.
|
||||
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
|
||||
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <dirent.h>
|
||||
#include <string.h>
|
||||
#include <screen.h>
|
||||
#include <proc.h>
|
||||
#include <ctype.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <assert.h>
|
||||
/*****************************************************************************
|
||||
** Utility functions for support programs
|
||||
*****************************************************************************/
|
||||
|
||||
/* MySQL library headers */
|
||||
#include <my_global.h>
|
||||
#include <my_sys.h>
|
||||
#include <my_dir.h>
|
||||
#include <m_string.h>
|
||||
|
||||
/* These 'should' be POSIX or ANSI */
|
||||
#include <assert.h> /* ASSERT */
|
||||
#include <stdarg.h> /* vsprintf, va_* */
|
||||
#include <sys/types.h> /* pid_t */
|
||||
#ifndef __WIN__
|
||||
#include <unistd.h> /* fork, rmdir, execve */
|
||||
#endif
|
||||
#include <stdio.h> /* freopen */
|
||||
#include <stdlib.h> /* FILE */
|
||||
#ifndef __WIN__
|
||||
#include <dirent.h> /* opendir, readdir */
|
||||
#endif
|
||||
|
||||
#if !defined(__NETWARE__) && !defined(__WIN__)
|
||||
#include <sys/wait.h>
|
||||
#endif
|
||||
|
||||
#if !defined(__NETWARE__)
|
||||
#include <signal.h>
|
||||
#endif
|
||||
|
||||
/* For ASSERT -- Not totally sure about this one: */
|
||||
#if !defined(ASSERT)
|
||||
#define ASSERT(A) assert(A)
|
||||
#endif
|
||||
|
||||
#include "my_manage.h"
|
||||
|
||||
#define __STDC__ 1
|
||||
#include "process.h"
|
||||
/******************************************************************************
|
||||
|
||||
macros
|
||||
|
||||
******************************************************************************/
|
||||
init_args()
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
global variables
|
||||
|
||||
******************************************************************************/
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
functions
|
||||
|
||||
******************************************************************************/
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
init_args()
|
||||
|
||||
Init an argument list.
|
||||
Init an argument list.
|
||||
|
||||
******************************************************************************/
|
||||
void init_args(arg_list_t *al)
|
||||
{
|
||||
#ifndef __WIN__
|
||||
ASSERT(al != NULL);
|
||||
|
||||
|
||||
al->argc = 0;
|
||||
al->size = ARG_BUF;
|
||||
al->argv = malloc(al->size * sizeof(char *));
|
||||
al->argv = (char **)my_malloc(al->size * sizeof(char *), MYF(MY_WME));
|
||||
ASSERT(al->argv != NULL);
|
||||
|
||||
#else
|
||||
win_args[0]= '\0';
|
||||
skip_first_param= TRUE;
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
add_arg()
|
||||
|
||||
Add an argument to a list.
|
||||
add_arg()
|
||||
|
||||
Add an argument to a list.
|
||||
|
||||
******************************************************************************/
|
||||
void add_arg(arg_list_t *al, char *format, ...)
|
||||
void add_arg(arg_list_t *al, const char *format, ...)
|
||||
{
|
||||
#ifndef __WIN__
|
||||
va_list ap;
|
||||
char temp[PATH_MAX];
|
||||
|
||||
ASSERT(al != NULL);
|
||||
|
||||
// increase size
|
||||
if (al->argc >= al->size)
|
||||
/* increase size */
|
||||
if (al->argc >= (int)al->size)
|
||||
{
|
||||
al->size += ARG_BUF;
|
||||
al->argv = realloc(al->argv, al->size * sizeof(char *));
|
||||
al->argv = (char **)my_realloc((char *)al->argv, al->size * sizeof(char *), MYF(MY_WME));
|
||||
ASSERT(al->argv != NULL);
|
||||
}
|
||||
|
||||
@ -94,7 +108,7 @@ void add_arg(arg_list_t *al, char *format, ...)
|
||||
vsprintf(temp, format, ap);
|
||||
va_end(ap);
|
||||
|
||||
al->argv[al->argc] = malloc(strlen(temp)+1);
|
||||
al->argv[al->argc] = my_malloc(strlen(temp)+1, MYF(MY_WME));
|
||||
ASSERT(al->argv[al->argc] != NULL);
|
||||
strcpy(al->argv[al->argc], temp);
|
||||
|
||||
@ -104,19 +118,36 @@ void add_arg(arg_list_t *al, char *format, ...)
|
||||
{
|
||||
al->argv[al->argc] = NULL;
|
||||
}
|
||||
#else
|
||||
va_list ap;
|
||||
char param[PATH_MAX];
|
||||
|
||||
if (!skip_first_param)
|
||||
{
|
||||
va_start(ap, format);
|
||||
vsprintf(¶m, format, ap);
|
||||
va_end(ap);
|
||||
strcat(win_args," ");
|
||||
strcat(win_args,param);
|
||||
}
|
||||
else
|
||||
{
|
||||
skip_first_param= FALSE;
|
||||
}
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
free_args()
|
||||
|
||||
Free an argument list.
|
||||
free_args()
|
||||
|
||||
Free an argument list.
|
||||
|
||||
******************************************************************************/
|
||||
void free_args(arg_list_t *al)
|
||||
{
|
||||
#ifndef __WIN__
|
||||
int i;
|
||||
|
||||
ASSERT(al != NULL);
|
||||
@ -124,74 +155,110 @@ void free_args(arg_list_t *al)
|
||||
for(i = 0; i < al->argc; i++)
|
||||
{
|
||||
ASSERT(al->argv[i] != NULL);
|
||||
free(al->argv[i]);
|
||||
my_free(al->argv[i], MYF(MY_WME));
|
||||
al->argv[i] = NULL;
|
||||
}
|
||||
|
||||
free(al->argv);
|
||||
my_free((char *)al->argv, MYF(MY_WME));
|
||||
al->argc = 0;
|
||||
al->argv = NULL;
|
||||
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
sleep_until_file_deleted()
|
||||
|
||||
Sleep until the given file is no longer found.
|
||||
sleep_until_file_deleted()
|
||||
|
||||
Sleep until the given file is no longer found.
|
||||
|
||||
******************************************************************************/
|
||||
int sleep_until_file_deleted(char *pid_file)
|
||||
{
|
||||
struct stat buf;
|
||||
int i, err;
|
||||
|
||||
for(i = 0; (i < TRY_MAX) && (err = !stat(pid_file, &buf)); i++) sleep(1);
|
||||
|
||||
if (err != 0) err = errno;
|
||||
|
||||
return err;
|
||||
MY_STAT stat_info;
|
||||
int i, err = 0;
|
||||
#ifndef __WIN__
|
||||
for(i = 0; i < TRY_MAX; i++)
|
||||
{
|
||||
if (my_stat(pid_file, &stat_info, MYF(0)) == (MY_STAT *) NULL)
|
||||
{
|
||||
err = errno;
|
||||
break;
|
||||
}
|
||||
my_sleep(1);
|
||||
}
|
||||
#else
|
||||
switch (pid_mode)
|
||||
{
|
||||
case MASTER_PID:
|
||||
err= (WaitForSingleObject(master_server, TRY_MAX*1000) == WAIT_TIMEOUT);
|
||||
pid_mode= 0;
|
||||
break;
|
||||
case SLAVE_PID:
|
||||
err= (WaitForSingleObject(slave_server, TRY_MAX*1000) == WAIT_TIMEOUT);
|
||||
pid_mode= 0;
|
||||
break;
|
||||
};
|
||||
#endif
|
||||
return err;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
sleep_until_file_exists()
|
||||
|
||||
Sleep until the given file exists.
|
||||
sleep_until_file_exists()
|
||||
|
||||
Sleep until the given file exists.
|
||||
|
||||
******************************************************************************/
|
||||
int sleep_until_file_exists(char *pid_file)
|
||||
{
|
||||
struct stat buf;
|
||||
int i, err;
|
||||
|
||||
for(i = 0; (i < TRY_MAX) && (err = stat(pid_file, &buf)); i++) sleep(1);
|
||||
|
||||
if (err != 0) err = errno;
|
||||
|
||||
return err;
|
||||
MY_STAT stat_info;
|
||||
int i, err = 0;
|
||||
|
||||
#ifndef __WIN__
|
||||
for(i = 0; i < TRY_MAX; i++)
|
||||
{
|
||||
if (my_stat(pid_file, &stat_info, MYF(0)) == (MY_STAT *) NULL)
|
||||
{
|
||||
err = errno;
|
||||
break;
|
||||
}
|
||||
my_sleep(1);
|
||||
}
|
||||
#else
|
||||
switch (pid_mode)
|
||||
{
|
||||
case MASTER_PID:
|
||||
WaitForSingleObject(master_server, TRY_MAX*1000);
|
||||
pid_mode= 0;
|
||||
break;
|
||||
case SLAVE_PID:
|
||||
WaitForSingleObject(slave_server, TRY_MAX*1000);
|
||||
pid_mode= 0;
|
||||
break;
|
||||
};
|
||||
#endif
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
wait_for_server_start()
|
||||
|
||||
Wait for the server on the given port to start.
|
||||
wait_for_server_start()
|
||||
|
||||
Wait for the server on the given port to start.
|
||||
|
||||
******************************************************************************/
|
||||
int wait_for_server_start(char *bin_dir, char *user, char *password, int port)
|
||||
{
|
||||
arg_list_t al;
|
||||
int err, i;
|
||||
char mysqladmin_file[PATH_MAX];
|
||||
int err = 0, i;
|
||||
char trash[PATH_MAX];
|
||||
|
||||
// mysqladmin file
|
||||
snprintf(mysqladmin_file, PATH_MAX, "%s/mysqladmin", bin_dir);
|
||||
snprintf(trash, PATH_MAX, "/tmp/trash.out");
|
||||
|
||||
// args
|
||||
/* mysqladmin file */
|
||||
my_snprintf(trash, PATH_MAX, "/tmp/trash.out");
|
||||
|
||||
/* args */
|
||||
init_args(&al);
|
||||
add_arg(&al, "%s", mysqladmin_file);
|
||||
add_arg(&al, "--no-defaults");
|
||||
@ -205,13 +272,13 @@ int wait_for_server_start(char *bin_dir, char *user, char *password, int port)
|
||||
add_arg(&al, "--host=localhost");
|
||||
add_arg(&al, "ping");
|
||||
|
||||
// NetWare does not support the connect timeout in the TCP/IP stack
|
||||
// -- we will try the ping multiple times
|
||||
for(i = 0; (i < TRY_MAX)
|
||||
&& (err = spawn(mysqladmin_file, &al, TRUE, NULL,
|
||||
trash, NULL)); i++) sleep(1);
|
||||
|
||||
// free args
|
||||
/* NetWare does not support the connect timeout in the TCP/IP stack
|
||||
-- we will try the ping multiple times */
|
||||
for(i = 0; (i < TRY_MAX)
|
||||
&& (err = spawn(mysqladmin_file, &al, TRUE, NULL,
|
||||
trash, NULL, NOT_NEED_PID)); i++) sleep(1);
|
||||
|
||||
/* free args */
|
||||
free_args(&al);
|
||||
|
||||
return err;
|
||||
@ -219,20 +286,23 @@ int wait_for_server_start(char *bin_dir, char *user, char *password, int port)
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
spawn()
|
||||
|
||||
Spawn the given path with the given arguments.
|
||||
spawn()
|
||||
|
||||
Spawn the executable at the given path with the given arguments.
|
||||
|
||||
******************************************************************************/
|
||||
|
||||
#ifdef __NETWARE__
|
||||
|
||||
int spawn(char *path, arg_list_t *al, int join, char *input,
|
||||
char *output, char *error)
|
||||
{
|
||||
pid_t pid;
|
||||
pid_t pid;
|
||||
int result = 0;
|
||||
wiring_t wiring = { FD_UNUSED, FD_UNUSED, FD_UNUSED };
|
||||
unsigned long flags = PROC_CURRENT_SPACE | PROC_INHERIT_CWD;
|
||||
|
||||
// open wiring
|
||||
/* open wiring */
|
||||
if (input)
|
||||
wiring.infd = open(input, O_RDONLY);
|
||||
|
||||
@ -242,14 +312,14 @@ int spawn(char *path, arg_list_t *al, int join, char *input,
|
||||
if (error)
|
||||
wiring.errfd = open(error, O_WRONLY | O_CREAT | O_TRUNC);
|
||||
|
||||
// procve requires a NULL
|
||||
/* procve requires a NULL */
|
||||
add_arg(al, NULL);
|
||||
|
||||
// go
|
||||
/* go */
|
||||
pid = procve(path, flags, NULL, &wiring, NULL, NULL, 0,
|
||||
NULL, (const char **)al->argv);
|
||||
|
||||
if (pid == -1)
|
||||
if (pid == -1)
|
||||
{
|
||||
result = -1;
|
||||
}
|
||||
@ -257,8 +327,8 @@ int spawn(char *path, arg_list_t *al, int join, char *input,
|
||||
{
|
||||
waitpid(pid, &result, 0);
|
||||
}
|
||||
|
||||
// close wiring
|
||||
|
||||
/* close wiring */
|
||||
if (wiring.infd != -1)
|
||||
close(wiring.infd);
|
||||
|
||||
@ -268,207 +338,411 @@ int spawn(char *path, arg_list_t *al, int join, char *input,
|
||||
if (wiring.errfd != -1)
|
||||
close(wiring.errfd);
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
#else /* NOT __NETWARE__ */
|
||||
|
||||
#ifdef __WIN__
|
||||
|
||||
int my_vsnprintf_(char *to, size_t n, const char* value, ...)
|
||||
{
|
||||
char *start=to, *end=to+n-1;
|
||||
uint length, num_state, pre_zero;
|
||||
reg2 char *par;// = value;
|
||||
va_list args;
|
||||
va_start(args,value);
|
||||
|
||||
par = va_arg(args, char *);
|
||||
while (par != NULL)
|
||||
{
|
||||
uint plen,left_len = (uint)(end-to)+1;
|
||||
if (!par) par = (char*)"(null)";
|
||||
plen = (uint) strlen(par);
|
||||
if (left_len <= plen)
|
||||
plen = left_len - 1;
|
||||
to=strnmov(to+strlen(to),par,plen);
|
||||
par = va_arg(args, char *);
|
||||
}
|
||||
va_end(args);
|
||||
DBUG_ASSERT(to <= end);
|
||||
*to='\0';
|
||||
return (uint) (to - start);
|
||||
}
|
||||
|
||||
int spawn(char *path, arg_list_t *al, int join, char *input,
|
||||
char *output, char *error)
|
||||
{
|
||||
char *cl;
|
||||
char *arg;
|
||||
intptr_t result;
|
||||
int j;
|
||||
int err;
|
||||
STARTUPINFO startup_info;
|
||||
PROCESS_INFORMATION process_information;
|
||||
ULONG dosretval;
|
||||
int retval;
|
||||
DWORD exit_code;
|
||||
SECURITY_ATTRIBUTES process_attributes, thread_attributes;
|
||||
char command_line[1024]= "";
|
||||
|
||||
|
||||
memset(&startup_info,0,sizeof(STARTUPINFO));
|
||||
startup_info.cb = sizeof(STARTUPINFO);
|
||||
|
||||
if (input)
|
||||
freopen(input, "rb", stdin);
|
||||
|
||||
if (output)
|
||||
freopen(output, "wb", stdout);
|
||||
|
||||
if (error)
|
||||
freopen(error, "wb", stderr);
|
||||
|
||||
result= CreateProcess(
|
||||
path,
|
||||
&win_args,
|
||||
NULL,
|
||||
NULL,
|
||||
TRUE,
|
||||
0,
|
||||
NULL,
|
||||
NULL,
|
||||
&startup_info,
|
||||
&process_information
|
||||
);
|
||||
|
||||
if (process_information.hProcess)
|
||||
{
|
||||
if (join)
|
||||
{
|
||||
if (WaitForSingleObject(process_information.hProcess, mysqld_timeout) == WAIT_TIMEOUT)
|
||||
{
|
||||
exit_code= -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
GetExitCodeProcess(process_information.hProcess, &exit_code);
|
||||
}
|
||||
CloseHandle(process_information.hProcess);
|
||||
}
|
||||
else
|
||||
{
|
||||
exit_code= 0;
|
||||
}
|
||||
if (run_server)
|
||||
{
|
||||
switch (pid_mode)
|
||||
{
|
||||
case MASTER_PID:
|
||||
master_server= process_information.hProcess;
|
||||
break;
|
||||
case SLAVE_PID:
|
||||
slave_server= process_information.hProcess;
|
||||
break;
|
||||
};
|
||||
pid_mode= 0;
|
||||
run_server= FALSE;
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
exit_code= -1;
|
||||
}
|
||||
if (input)
|
||||
freopen("CONIN$","rb",stdin);
|
||||
if (output)
|
||||
freopen("CONOUT$","wb",stdout);
|
||||
if (error)
|
||||
freopen("CONOUT$","wb",stderr);
|
||||
|
||||
return exit_code;
|
||||
}
|
||||
|
||||
#else /* NOT __NETWARE__, NOT __WIN__ */
|
||||
|
||||
/* This assumes full POSIX.1 compliance */
|
||||
int spawn(char *path, arg_list_t *al, int join, char *input,
|
||||
char *output, char *error)
|
||||
{
|
||||
int result = 0;
|
||||
pid_t pid;
|
||||
|
||||
if ((pid = fork()))
|
||||
{
|
||||
/* Remains in parent process */
|
||||
if (join && (pid != -1))
|
||||
waitpid(pid, &result, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Child process */
|
||||
|
||||
/* Reassign streams */
|
||||
if (input)
|
||||
freopen(input, "r", stdin);
|
||||
|
||||
if (output)
|
||||
freopen(output, "w", stdout);
|
||||
|
||||
if (error)
|
||||
freopen(error, "w", stderr);
|
||||
|
||||
/* Spawn the process */
|
||||
execve(path, al->argv, environ);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif /* __WIN__ */
|
||||
|
||||
#endif /* __NETWARE__ */
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
stop_server()
|
||||
|
||||
Stop the server with the given port and pid file.
|
||||
stop_server()
|
||||
|
||||
Stop the server with the given port and pid file.
|
||||
|
||||
******************************************************************************/
|
||||
int stop_server(char *bin_dir, char *user, char *password, int port,
|
||||
char *pid_file)
|
||||
{
|
||||
arg_list_t al;
|
||||
int err, i, argc = 0;
|
||||
char mysqladmin_file[PATH_MAX];
|
||||
arg_list_t al;
|
||||
int err;
|
||||
char trash[PATH_MAX];
|
||||
|
||||
// mysqladmin file
|
||||
snprintf(mysqladmin_file, PATH_MAX, "%s/mysqladmin", bin_dir);
|
||||
snprintf(trash, PATH_MAX, "/tmp/trash.out");
|
||||
|
||||
// args
|
||||
init_args(&al);
|
||||
add_arg(&al, "%s", mysqladmin_file);
|
||||
add_arg(&al, "--no-defaults");
|
||||
add_arg(&al, "--port=%u", port);
|
||||
add_arg(&al, "--user=%s", user);
|
||||
add_arg(&al, "--password=%s", password);
|
||||
add_arg(&al, "-O");
|
||||
add_arg(&al, "shutdown_timeout=20");
|
||||
add_arg(&al, "shutdown");
|
||||
my_snprintf(trash, PATH_MAX, "/tmp/trash.out");
|
||||
|
||||
// spawn
|
||||
if ((err = spawn(mysqladmin_file, &al, TRUE, NULL,
|
||||
trash, NULL)) == 0)
|
||||
{
|
||||
sleep_until_file_deleted(pid_file);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* args */
|
||||
init_args(&al);
|
||||
add_arg(&al, "%s", mysqladmin_file);
|
||||
add_arg(&al, "--no-defaults");
|
||||
add_arg(&al, "--port=%u", port);
|
||||
add_arg(&al, "--user=%s", user);
|
||||
add_arg(&al, "--password=%s", password);
|
||||
add_arg(&al, "-O");
|
||||
add_arg(&al, "shutdown_timeout=20");
|
||||
add_arg(&al, "shutdown");
|
||||
|
||||
/* spawn */
|
||||
if ((err = spawn(mysqladmin_file, &al, TRUE, NULL,
|
||||
trash, NULL)) == 0)
|
||||
{
|
||||
sleep_until_file_deleted(pid_file);
|
||||
}
|
||||
else
|
||||
{
|
||||
pid_t pid = get_server_pid(pid_file);
|
||||
|
||||
// shutdown failed - kill server
|
||||
kill_server(pid);
|
||||
|
||||
sleep(TRY_MAX);
|
||||
|
||||
// remove pid file if possible
|
||||
err = remove(pid_file);
|
||||
/* shutdown failed - kill server */
|
||||
kill_server(pid);
|
||||
|
||||
sleep(TRY_MAX);
|
||||
|
||||
/* remove pid file if possible */
|
||||
err = my_delete(pid_file, MYF(MY_WME));
|
||||
}
|
||||
|
||||
// free args
|
||||
/* free args */
|
||||
free_args(&al);
|
||||
|
||||
return err;
|
||||
return err;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
get_server_pid()
|
||||
|
||||
Get the VM id with the given pid file.
|
||||
get_server_pid()
|
||||
|
||||
Get the VM id with the given pid file.
|
||||
|
||||
******************************************************************************/
|
||||
pid_t get_server_pid(char *pid_file)
|
||||
{
|
||||
char buf[PATH_MAX];
|
||||
int fd, err;
|
||||
char *p;
|
||||
pid_t id;
|
||||
|
||||
// discover id
|
||||
fd = open(pid_file, O_RDONLY);
|
||||
|
||||
err = read(fd, buf, PATH_MAX);
|
||||
|
||||
close(fd);
|
||||
|
||||
if (err > 0)
|
||||
{
|
||||
// terminate string
|
||||
if ((p = strchr(buf, '\n')) != NULL)
|
||||
{
|
||||
*p = NULL;
|
||||
|
||||
// check for a '\r'
|
||||
if ((p = strchr(buf, '\r')) != NULL)
|
||||
{
|
||||
*p = NULL;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
buf[err] = NULL;
|
||||
}
|
||||
|
||||
id = strtol(buf, NULL, 0);
|
||||
}
|
||||
char buf[PATH_MAX];
|
||||
int err;
|
||||
File fd;
|
||||
char *p;
|
||||
pid_t id = 0;
|
||||
|
||||
/* discover id */
|
||||
fd = my_open(pid_file, O_RDONLY, MYF(MY_WME));
|
||||
|
||||
err = my_read(fd, buf, PATH_MAX, MYF(MY_WME));
|
||||
|
||||
my_close(fd, MYF(MY_WME));
|
||||
|
||||
if (err > 0)
|
||||
{
|
||||
/* terminate string */
|
||||
if ((p = strchr(buf, '\n')) != NULL)
|
||||
{
|
||||
*p = '\0';
|
||||
|
||||
/* check for a '\r' */
|
||||
if ((p = strchr(buf, '\r')) != NULL)
|
||||
{
|
||||
*p = '\0';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
buf[err] = '\0';
|
||||
}
|
||||
|
||||
id = strtol(buf, NULL, 0);
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
kill_server()
|
||||
|
||||
Force a kill of the server with the given pid.
|
||||
kill_server()
|
||||
|
||||
Force a kill of the server with the given pid.
|
||||
|
||||
******************************************************************************/
|
||||
void kill_server(pid_t pid)
|
||||
{
|
||||
if (pid > 0)
|
||||
{
|
||||
// destroy vm
|
||||
|
||||
#if !defined(__NETWARE__)
|
||||
/* Send SIGTERM to pid */
|
||||
kill(pid, SIGTERM);
|
||||
#else /* __NETWARE__ */
|
||||
/* destroy vm */
|
||||
NXVmDestroy(pid);
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
del_tree()
|
||||
|
||||
Delete the directory and subdirectories.
|
||||
del_tree()
|
||||
|
||||
Delete the directory and subdirectories.
|
||||
|
||||
******************************************************************************/
|
||||
void del_tree(char *dir)
|
||||
{
|
||||
DIR *parent = opendir(dir);
|
||||
DIR *entry;
|
||||
char temp[PATH_MAX];
|
||||
|
||||
if (parent == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
while((entry = readdir(parent)) != NULL)
|
||||
{
|
||||
// create long name
|
||||
snprintf(temp, PATH_MAX, "%s/%s", dir, entry->d_name);
|
||||
|
||||
if (entry->d_name[0] == '.')
|
||||
{
|
||||
// Skip
|
||||
}
|
||||
else if (S_ISDIR(entry->d_type))
|
||||
{
|
||||
// delete subdirectory
|
||||
del_tree(temp);
|
||||
}
|
||||
else
|
||||
{
|
||||
// remove file
|
||||
remove(temp);
|
||||
}
|
||||
}
|
||||
|
||||
// remove directory
|
||||
rmdir(dir);
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
removef()
|
||||
|
||||
******************************************************************************/
|
||||
int removef(char *format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
char path[PATH_MAX];
|
||||
|
||||
va_start(ap, format);
|
||||
|
||||
vsnprintf(path, PATH_MAX, format, ap);
|
||||
|
||||
va_end(ap);
|
||||
MY_DIR *current;
|
||||
uint i;
|
||||
char temp[PATH_MAX];
|
||||
|
||||
return remove(path);
|
||||
current = my_dir(dir, MYF(MY_WME | MY_WANT_STAT));
|
||||
|
||||
/* current is NULL if dir does not exist */
|
||||
if (current == NULL)
|
||||
return;
|
||||
|
||||
for (i = 0; i < current->number_off_files; i++)
|
||||
{
|
||||
/* create long name */
|
||||
my_snprintf(temp, PATH_MAX, "%s/%s", dir, current->dir_entry[i].name);
|
||||
|
||||
if (current->dir_entry[i].name[0] == '.')
|
||||
{
|
||||
/* Skip */
|
||||
}
|
||||
else if (MY_S_ISDIR(current->dir_entry[i].mystat.st_mode))
|
||||
{
|
||||
/* delete subdirectory */
|
||||
del_tree(temp);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* remove file */
|
||||
my_delete(temp, MYF(MY_WME));
|
||||
}
|
||||
}
|
||||
|
||||
my_dirend(current);
|
||||
|
||||
/* remove directory */
|
||||
rmdir(dir);
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
get_basedir()
|
||||
|
||||
removef()
|
||||
|
||||
******************************************************************************/
|
||||
int removef(const char *format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
char path[PATH_MAX];
|
||||
|
||||
va_start(ap, format);
|
||||
|
||||
my_vsnprintf(path, PATH_MAX, format, ap);
|
||||
|
||||
va_end(ap);
|
||||
#ifdef __WIN__
|
||||
{
|
||||
MY_DIR *current;
|
||||
uint i;
|
||||
struct _finddata_t find;
|
||||
char temp[PATH_MAX];
|
||||
#ifdef _WIN64
|
||||
__int64 handle;
|
||||
#else
|
||||
long handle;
|
||||
#endif
|
||||
char *p;
|
||||
|
||||
p= strrchr(path,'\\');
|
||||
if (p == NULL)
|
||||
{
|
||||
p= strrchr(path,'/');
|
||||
if (p == NULL)
|
||||
p= &path;
|
||||
else
|
||||
p++;
|
||||
}
|
||||
else
|
||||
p++;
|
||||
|
||||
if ((handle=_findfirst(path,&find)) == -1L)
|
||||
return 0;
|
||||
do
|
||||
{
|
||||
strcpy(p,find.name);
|
||||
my_delete(path, MYF(MY_WME));
|
||||
} while (!_findnext(handle,&find));
|
||||
_findclose(handle);
|
||||
}
|
||||
#else
|
||||
return my_delete(path, MYF(MY_WME));
|
||||
#endif
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
get_basedir()
|
||||
|
||||
******************************************************************************/
|
||||
void get_basedir(char *argv0, char *basedir)
|
||||
{
|
||||
char temp[PATH_MAX];
|
||||
char *p;
|
||||
|
||||
ASSERT(argv0 != NULL);
|
||||
char temp[PATH_MAX];
|
||||
char *p;
|
||||
|
||||
ASSERT(argv0 != NULL);
|
||||
ASSERT(basedir != NULL);
|
||||
|
||||
strcpy(temp, strlwr(argv0));
|
||||
while((p = strchr(temp, '\\')) != NULL) *p = '/';
|
||||
|
||||
if ((p = strindex(temp, "/bin/")) != NULL)
|
||||
{
|
||||
*p = NULL;
|
||||
strcpy(basedir, temp);
|
||||
}
|
||||
strcpy(temp, argv0);
|
||||
#ifndef __WIN__
|
||||
casedn_str(temp);
|
||||
#endif
|
||||
while((p = strchr(temp, '\\')) != NULL) *p = '/';
|
||||
|
||||
if ((p = strstr(temp, "/bin/")) != NullS)
|
||||
{
|
||||
*p = '\0';
|
||||
strcpy(basedir, temp);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -26,17 +26,41 @@
|
||||
******************************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
#ifndef __WIN__
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
macros
|
||||
|
||||
******************************************************************************/
|
||||
#ifdef __WIN__
|
||||
#define PATH_MAX _MAX_PATH
|
||||
#define NAME_MAX _MAX_FNAME
|
||||
#define kill(A,B) TerminateProcess((HANDLE)A,0)
|
||||
#define NOT_NEED_PID 0
|
||||
#define MASTER_PID 1
|
||||
#define SLAVE_PID 2
|
||||
#define mysqld_timeout 60000
|
||||
|
||||
intptr_t master_server;
|
||||
intptr_t slave_server;
|
||||
int pid_mode;
|
||||
bool run_server;
|
||||
char win_args[1024];
|
||||
bool skip_first_param;
|
||||
#endif
|
||||
|
||||
|
||||
#define ARG_BUF 10
|
||||
#define TRY_MAX 5
|
||||
|
||||
#ifdef __NETWARE__
|
||||
#define strstr(A,B) strindex(A,B)
|
||||
#endif
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
structures
|
||||
@ -53,6 +77,8 @@ typedef struct
|
||||
|
||||
} arg_list_t;
|
||||
|
||||
|
||||
typedef int pid_t;
|
||||
/******************************************************************************
|
||||
|
||||
global variables
|
||||
@ -66,7 +92,7 @@ typedef struct
|
||||
******************************************************************************/
|
||||
|
||||
void init_args(arg_list_t *);
|
||||
void add_arg(arg_list_t *, char *, ...);
|
||||
void add_arg(arg_list_t *, const char *, ...);
|
||||
void free_args(arg_list_t *);
|
||||
|
||||
int sleep_until_file_exists(char *);
|
||||
@ -80,8 +106,12 @@ pid_t get_server_pid(char *);
|
||||
void kill_server(pid_t pid);
|
||||
|
||||
void del_tree(char *);
|
||||
int removef(char *, ...);
|
||||
int removef(const char *, ...);
|
||||
|
||||
void get_basedir(char *, char *);
|
||||
|
||||
char mysqladmin_file[PATH_MAX];
|
||||
|
||||
#endif /* _MY_MANAGE */
|
||||
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user