1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-29 05:21:33 +03:00

IM port cleanup

This commit is contained in:
petr@mysql.com
2005-08-05 17:02:06 +04:00
parent 07431723c0
commit 22ccfb4d67
8 changed files with 205 additions and 219 deletions

View File

@ -5,8 +5,8 @@
IMService::IMService(void)
{
serviceName = "MySqlManager";
displayName = "MySQL Manager";
serviceName= "MySqlManager";
displayName= "MySQL Manager";
}
IMService::~IMService(void)
@ -69,4 +69,3 @@ int HandleServiceOptions(Options options)
return (int)winService.Init();
return ret_val;
}

View File

@ -36,10 +36,10 @@ BOOL WindowsService::Install()
return false;
newService= CreateService(scm, serviceName, displayName,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
szFilePath, NULL, NULL, NULL, username,
password);
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
szFilePath, NULL, NULL, NULL, username,
password);
if (newService)
{
@ -68,7 +68,7 @@ BOOL WindowsService::Init()
BOOL WindowsService::Remove()
{
bool ret_val= false;
bool ret_val= false;
if (! IsInstalled())
return true;
@ -118,9 +118,9 @@ BOOL WindowsService::ReportStatus(DWORD currentState, DWORD waitHint,
if(debugging) return TRUE;
if(currentState == SERVICE_START_PENDING)
status.dwControlsAccepted= 0;
status.dwControlsAccepted= 0;
else
status.dwControlsAccepted= dwAcceptedControls;
status.dwControlsAccepted= dwAcceptedControls;
status.dwCurrentState= currentState;
status.dwWin32ExitCode= dwError != 0 ?
@ -155,35 +155,34 @@ void WindowsService::RegisterAndRun(DWORD argc, LPTSTR *argv)
void WindowsService::HandleControlCode(DWORD opcode)
{
// Handle the requested control code.
switch(opcode)
{
case SERVICE_CONTROL_STOP:
// Stop the service.
status.dwCurrentState= SERVICE_STOP_PENDING;
Stop();
break;
switch(opcode) {
case SERVICE_CONTROL_STOP:
// Stop the service.
status.dwCurrentState= SERVICE_STOP_PENDING;
Stop();
break;
case SERVICE_CONTROL_PAUSE:
status.dwCurrentState= SERVICE_PAUSE_PENDING;
Pause();
break;
case SERVICE_CONTROL_PAUSE:
status.dwCurrentState= SERVICE_PAUSE_PENDING;
Pause();
break;
case SERVICE_CONTROL_CONTINUE:
status.dwCurrentState= SERVICE_CONTINUE_PENDING;
Continue();
break;
case SERVICE_CONTROL_CONTINUE:
status.dwCurrentState= SERVICE_CONTINUE_PENDING;
Continue();
break;
case SERVICE_CONTROL_SHUTDOWN:
Shutdown();
break;
case SERVICE_CONTROL_SHUTDOWN:
Shutdown();
break;
case SERVICE_CONTROL_INTERROGATE:
ReportStatus(status.dwCurrentState);
break;
case SERVICE_CONTROL_INTERROGATE:
ReportStatus(status.dwCurrentState);
break;
default:
// invalid control code
break;
default:
// invalid control code
break;
}
}
@ -201,5 +200,3 @@ void WINAPI WindowsService::ControlHandler(DWORD opcode)
return gService->HandleControlCode(opcode);
}

View File

@ -3,7 +3,7 @@
class WindowsService
{
protected:
bool inited;
bool inited;
const char *serviceName;
const char *displayName;
const char *username;
@ -23,11 +23,11 @@ public:
BOOL Init();
BOOL IsInstalled();
void SetAcceptedControls(DWORD acceptedControls);
void Debug(bool debugFlag) { debugging = debugFlag; }
void Debug(bool debugFlag) { debugging= debugFlag; }
public:
static void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
static void WINAPI ControlHandler(DWORD CtrlType);
static void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
static void WINAPI ControlHandler(DWORD CtrlType);
protected:
virtual void Run()= 0;
@ -41,4 +41,3 @@ protected:
void HandleControlCode(DWORD opcode);
void RegisterAndRun(DWORD argc, LPTSTR *argv);
};

View File

@ -57,12 +57,12 @@ C_MODE_END
void Instance::remove_pid()
{
int pid;
if ((pid= options.get_pid()) != 0) /* check the pidfile */
if (options.unlink_pidfile()) /* remove stalled pidfile */
log_error("cannot remove pidfile for instance %i, this might be \
since IM lacks permmissions or hasn't found the pidifle",
options.instance_name);
int pid;
if ((pid= options.get_pid()) != 0) /* check the pidfile */
if (options.unlink_pidfile()) /* remove stalled pidfile */
log_error("cannot remove pidfile for instance %i, this might be \
since IM lacks permmissions or hasn't found the pidifle",
options.instance_name);
}
/*
@ -121,16 +121,15 @@ int Instance::launch_and_wait()
{
pid_t pid= fork();
switch (pid)
{
case 0:
execv(options.mysqld_path, options.argv);
/* exec never returns */
exit(1);
case -1:
log_info("cannot fork() to start instance %s", options.instance_name);
return -1;
default:
switch (pid) {
case 0:
execv(options.mysqld_path, options.argv);
/* exec never returns */
exit(1);
case -1:
log_info("cannot fork() to start instance %s", options.instance_name);
return -1;
default:
/*
Here we wait for the child created. This process differs for systems
running LinuxThreads and POSIX Threads compliant systems. This is because
@ -157,56 +156,56 @@ int Instance::launch_and_wait()
#else
int Instance::launch_and_wait()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
ZeroMemory(&si, sizeof(si));
si.cb= sizeof(si);
ZeroMemory(&pi, sizeof(pi));
int cmdlen= 0;
int cmdlen= 0;
for (int i= 1; options.argv[i] != 0; i++)
cmdlen+= strlen(options.argv[i]) + 1;
cmdlen++; // we have to add a single space for CreateProcess (read the docs)
char *cmdline= NULL;
if (cmdlen > 0)
{
cmdline= new char[cmdlen];
cmdline[0]= 0;
for (int i= 1; options.argv[i] != 0; i++)
cmdlen+= strlen(options.argv[i]) + 1;
cmdlen++; // we have to add a single space for CreateProcess (read the docs)
char *cmdline= NULL;
if (cmdlen > 0)
{
cmdline= new char[cmdlen];
cmdline[0]= 0;
for (int i= 1; options.argv[i] != 0; i++)
{
strcat(cmdline, " ");
strcat(cmdline, options.argv[i]);
}
strcat(cmdline, " ");
strcat(cmdline, options.argv[i]);
}
}
// Start the child process.
BOOL result= CreateProcess(options.mysqld_path, // file to execute
cmdline, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ); // Pointer to PROCESS_INFORMATION structure.
delete cmdline;
if (! result)
return -1;
// Start the child process.
BOOL result= CreateProcess(options.mysqld_path, // file to execute
cmdline, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ); // Pointer to PROCESS_INFORMATION structure.
delete cmdline;
if (! result)
return -1;
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD exitcode;
::GetExitCodeProcess(pi.hProcess, &exitcode);
DWORD exitcode;
::GetExitCodeProcess(pi.hProcess, &exitcode);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return exitcode;
return exitcode;
}
#endif
@ -215,8 +214,8 @@ void Instance::fork_and_monitor()
{
log_info("starting instance %s", options.instance_name);
int result= launch_and_wait();
if (result == -1) return;
if (launch_and_wait())
return; /* error is logged */
/* set instance state to crashed */
pthread_mutex_lock(&LOCK_instance);
@ -233,9 +232,6 @@ void Instance::fork_and_monitor()
pthread_cond_signal(&instance_map->guardian->COND_guardian);
/* thread exits */
return;
/* we should never end up here */
DBUG_ASSERT(0);
}
@ -268,9 +264,9 @@ bool Instance::is_running()
MYSQL mysql;
uint port= 0;
const char *socket= NULL;
const char *password= "check_connection";
const char *username= "MySQL_Instance_Manager";
const char *access_denied_message= "Access denied for user";
static const char *password= "check_connection";
static const char *username= "MySQL_Instance_Manager";
static const char *access_denied_message= "Access denied for user";
bool return_val;
if (options.mysqld_port)
@ -299,15 +295,8 @@ bool Instance::is_running()
return_val= TRUE; /* server is alive */
}
else
{
if (!strncmp(access_denied_message, mysql_error(&mysql),
sizeof(access_denied_message)-1))
{
return_val= TRUE;
}
else
return_val= FALSE;
}
return_val= test(!strncmp(access_denied_message, mysql_error(&mysql),
sizeof(access_denied_message) - 1));
mysql_close(&mysql);
pthread_mutex_unlock(&LOCK_instance);
@ -370,7 +359,7 @@ err:
BOOL SafeTerminateProcess(HANDLE hProcess, UINT uExitCode)
{
DWORD dwTID, dwCode, dwErr = 0;
DWORD dwTID, dwCode, dwErr= 0;
HANDLE hProcessDup= INVALID_HANDLE_VALUE;
HANDLE hRT= NULL;
HINSTANCE hKernel= GetModuleHandle("Kernel32");
@ -383,37 +372,37 @@ BOOL SafeTerminateProcess(HANDLE hProcess, UINT uExitCode)
// Detect the special case where the process is
// already dead...
if (GetExitCodeProcess((bDup) ? hProcessDup : hProcess, &dwCode) &&
(dwCode == STILL_ACTIVE))
(dwCode == STILL_ACTIVE))
{
FARPROC pfnExitProc;
FARPROC pfnExitProc;
pfnExitProc= GetProcAddress(hKernel, "ExitProcess");
pfnExitProc= GetProcAddress(hKernel, "ExitProcess");
hRT= CreateRemoteThread((bDup) ? hProcessDup : hProcess, NULL, 0,
(LPTHREAD_START_ROUTINE)pfnExitProc,
(PVOID)uExitCode, 0, &dwTID);
hRT= CreateRemoteThread((bDup) ? hProcessDup : hProcess, NULL, 0,
(LPTHREAD_START_ROUTINE)pfnExitProc,
(PVOID)uExitCode, 0, &dwTID);
if (hRT == NULL)
dwErr= GetLastError();
if (hRT == NULL)
dwErr= GetLastError();
}
else
dwErr= ERROR_PROCESS_ABORTED;
dwErr= ERROR_PROCESS_ABORTED;
if (hRT)
{
// Must wait process to terminate to
// guarantee that it has exited...
WaitForSingleObject((bDup) ? hProcessDup : hProcess, INFINITE);
// Must wait process to terminate to
// guarantee that it has exited...
WaitForSingleObject((bDup) ? hProcessDup : hProcess, INFINITE);
CloseHandle(hRT);
bSuccess= TRUE;
CloseHandle(hRT);
bSuccess= TRUE;
}
if (bDup)
CloseHandle(hProcessDup);
CloseHandle(hProcessDup);
if (!bSuccess)
SetLastError(dwErr);
SetLastError(dwErr);
return bSuccess;
}

View File

@ -133,12 +133,12 @@ void Listener_thread::run()
int rc= select(n, &read_fds_arg, 0, 0, 0);
if (rc == -1 && errno != EINTR)
{
log_error("Listener_thread::run(): select() failed, %s",
strerror(errno));
continue;
}
if (rc == -1 && errno != EINTR)
{
log_error("Listener_thread::run(): select() failed, %s",
strerror(errno));
continue;
}
for (int socket_index= 0; socket_index < num_sockets; socket_index++)
@ -150,8 +150,8 @@ void Listener_thread::run()
/* accept may return -1 (failure or spurious wakeup) */
if (client_fd >= 0) // connection established
{
Vio *vio = vio_new(client_fd, socket_index==0?VIO_TYPE_SOCKET:
VIO_TYPE_TCPIP, socket_index==0?1:0);
Vio *vio= vio_new(client_fd, socket_index==0?VIO_TYPE_SOCKET:
VIO_TYPE_TCPIP, socket_index==0?1:0);
if (vio != 0)
handle_new_mysql_connection(vio);
else
@ -236,11 +236,11 @@ int Listener_thread::create_tcp_socket()
uint im_port= options.port_number;
ip_socket_address.sin_family= AF_INET;
ip_socket_address.sin_addr.s_addr = im_bind_addr;
ip_socket_address.sin_addr.s_addr= im_bind_addr;
ip_socket_address.sin_port= (unsigned short)
htons((unsigned short) im_port);
htons((unsigned short) im_port);
setsockopt(ip_socket, SOL_SOCKET, SO_REUSEADDR, (char*) &arg, sizeof(arg));
if (bind(ip_socket, (struct sockaddr *) &ip_socket_address,
@ -273,8 +273,8 @@ int Listener_thread::create_tcp_socket()
}
#ifndef __WIN__
int Listener_thread::create_unix_socket(
struct sockaddr_un &unix_socket_address)
int Listener_thread::create_unix_socket(struct sockaddr_un
&unix_socket_address)
{
int unix_socket= socket(AF_UNIX, SOCK_STREAM, 0);
if (unix_socket == INVALID_SOCKET)
@ -297,11 +297,11 @@ int Listener_thread::create_unix_socket(
*/
mode_t old_mask= umask(0);
if (bind(unix_socket, (struct sockaddr *) &unix_socket_address,
sizeof(unix_socket_address)))
sizeof(unix_socket_address)))
{
log_error("Listener_thread::run(): bind(unix socket) failed, "
"socket file name is '%s', error '%s'",
unix_socket_address.sun_path, strerror(errno));
"socket file name is '%s', error '%s'",
unix_socket_address.sun_path, strerror(errno));
close(unix_socket);
return -1;
}
@ -322,8 +322,9 @@ int Listener_thread::create_unix_socket(
/* make sure that instances won't be listening our sockets */
set_no_inherit(unix_socket);
log_info("accepting connections on unix socket %s", unix_socket_address.sun_path);
sockets[num_sockets++] = unix_socket;
log_info("accepting connections on unix socket %s",
unix_socket_address.sun_path);
sockets[num_sockets++]= unix_socket;
FD_SET(unix_socket, &read_fds);
return 0;
}

View File

@ -238,20 +238,20 @@ void manager(const Options &options)
process_alarm(signo);
else
#endif
{
if (!guardian_thread.is_stopped())
{
if (!guardian_thread.is_stopped())
{
bool stop_instances= true;
guardian_thread.request_shutdown(stop_instances);
pthread_cond_signal(&guardian_thread.COND_guardian);
}
else
{
thread_registry.deliver_shutdown();
shutdown_complete= TRUE;
}
bool stop_instances= true;
guardian_thread.request_shutdown(stop_instances);
pthread_cond_signal(&guardian_thread.COND_guardian);
}
else
{
thread_registry.deliver_shutdown();
shutdown_complete= TRUE;
}
}
}
err:
/* delete the pid file */

View File

@ -111,22 +111,23 @@ static struct my_option my_long_options[] =
0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "default-mysqld-path", OPT_MYSQLD_PATH, "Where to look for MySQL"
" Server binary.",
(gptr *) &Options::default_mysqld_path, (gptr *) &Options::default_mysqld_path,
" Server binary.",
(gptr *) &Options::default_mysqld_path,
(gptr *) &Options::default_mysqld_path,
0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "monitoring-interval", OPT_MONITORING_INTERVAL, "Interval to monitor instances"
" in seconds.",
(gptr *) &Options::monitoring_interval,
(gptr *) &Options::monitoring_interval,
0, GET_UINT, REQUIRED_ARG, DEFAULT_MONITORING_INTERVAL,
0, 0, 0, 0, 0 },
{ "monitoring-interval", OPT_MONITORING_INTERVAL, "Interval to monitor"
" instances in seconds.",
(gptr *) &Options::monitoring_interval,
(gptr *) &Options::monitoring_interval,
0, GET_UINT, REQUIRED_ARG, DEFAULT_MONITORING_INTERVAL,
0, 0, 0, 0, 0 },
#ifdef __WIN__
{ "install", OPT_INSTALL_SERVICE, "Install as system service.",
(gptr *) &Options::install_as_service, (gptr*) &Options::install_as_service,
0, GET_BOOL, NO_ARG, 0, 0, 1, 0, 0, 0 },
{ "remove", OPT_REMOVE_SERVICE, "Remove system service.",
(gptr *)&Options::remove_service, (gptr*) &Options::remove_service,
(gptr *)&Options::remove_service, (gptr*) &Options::remove_service,
0, GET_BOOL, NO_ARG, 0, 0, 1, 0, 0, 0},
#else
{ "run-as-service", OPT_RUN_AS_SERVICE,
@ -134,12 +135,12 @@ static struct my_option my_long_options[] =
0, 0, GET_BOOL, NO_ARG, 0, 0, 1, 0, 0, 0 },
{ "user", OPT_USER, "Username to start mysqlmanager",
(gptr *) &Options::user,
(gptr *) &Options::user,
0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
(gptr *) &Options::user,
(gptr *) &Options::user,
0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
#endif
{ "version", 'V', "Output version information and exit.", 0, 0, 0,
GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0 },
GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0 }
};
@ -254,8 +255,8 @@ int Options::load(int argc, char **argv)
{
/* the log is not enabled yet */
fprintf(stderr, "The --defaults-extra-file and --no-defaults options"
" are not supported by\n"
"Instance Manager. Program aborted.\n");
" are not supported by\n"
"Instance Manager. Program aborted.\n");
goto err;
}
}
@ -264,9 +265,9 @@ int Options::load(int argc, char **argv)
setup_windows_defaults(*argv);
/*
On Windows, there are two possibilities. Either we are given
a defaults file on the command line or we use the my.ini file
that is in our app dir
On Windows, there are two possibilities. Either we are given
a defaults file on the command line or we use the my.ini file
that is in our app dir
*/
if (Options::config_file == NULL)
{
@ -274,7 +275,7 @@ int Options::load(int argc, char **argv)
static const char default_win_config_file_name[]= "\\my.ini";
if (!GetModuleFileName(NULL, windows_config_file,
sizeof(windows_config_file)))
sizeof(windows_config_file)))
goto err;
filename= strrchr(windows_config_file, "\\");
@ -327,9 +328,9 @@ char* change_extension(const char *src, const char *newext)
void Options::setup_windows_defaults(const char *progname)
{
Options::password_file_name= default_password_file_name =
Options::password_file_name= default_password_file_name=
change_extension(progname, "passwd");
Options::log_file_name= default_log_file_name =
Options::log_file_name= default_log_file_name=
change_extension(progname, "log");
}

View File

@ -25,6 +25,12 @@
#include "log.h"
#ifdef __WIN__
#define NEWLINE_LEN 2
#else
#define NEWLINE_LEN 1
#endif
struct User
{
char user[USERNAME_LENGTH + 1];
@ -36,8 +42,7 @@ struct User
int User::init(const char *line)
{
const char *name_begin, *name_end;
char *password;
const char *name_begin, *name_end, *password;
if (line[0] == '\'' || line[0] == '"')
{
@ -45,7 +50,7 @@ int User::init(const char *line)
name_end= strchr(name_begin, line[0]);
if (name_end == 0 || name_end[1] != ':')
goto err;
password= (char*)(name_end + 2);
password= name_end + 2;
}
else
{
@ -53,19 +58,14 @@ int User::init(const char *line)
name_end= strchr(name_begin, ':');
if (name_end == 0)
goto err;
password= (char*)(name_end + 1);
password= name_end + 1;
}
user_length= name_end - name_begin;
if (user_length > USERNAME_LENGTH)
goto err;
/* assume that newline characater is present */
if (password[strlen(password)-2] == '\r')
{
password[strlen(password)-2]= '\n';
password[strlen(password)-1]= 0;
}
if (strlen(password) != SCRAMBLED_PASSWORD_CHAR_LENGTH + 1)
if (strlen(password) != SCRAMBLED_PASSWORD_CHAR_LENGTH + NEWLINE_LEN)
goto err;
memcpy(user, name_begin, user_length);
@ -101,7 +101,7 @@ C_MODE_END
int User_map::init()
{
enum { START_HASH_SIZE = 16 };
enum { START_HASH_SIZE= 16 };
if (hash_init(&hash, default_charset_info, START_HASH_SIZE, 0, 0,
get_user_key, delete_user, 0))
return 1;