diff options
author | unknown <reggie@fedora.(none)> | 2005-07-20 10:55:40 -0500 |
---|---|---|
committer | unknown <reggie@fedora.(none)> | 2005-07-20 10:55:40 -0500 |
commit | 875b0e6322df380b6fcd4bd4f66d0d3e0142039f (patch) | |
tree | fd59532b923e8ee6c5bb48afac93f8d93e48fec6 /server-tools/instance-manager/instance.cc | |
parent | 6ae060305541019882bcb60bf086458b010faa45 (diff) | |
download | mariadb-git-875b0e6322df380b6fcd4bd4f66d0d3e0142039f.tar.gz |
initial import of Windows port of IM.
server-tools/instance-manager/commands.cc:
type cleanups for compiling on Windows
now using Options::config_file for the location of the single
my.cnf file we are using
server-tools/instance-manager/guardian.cc:
pthread_mutex_lock and unlock do not return a value on Windows
so we return 0 in all cases
server-tools/instance-manager/instance.cc:
big changes here.
Had to implement Windows versions of launch_and_wait and kill()
server-tools/instance-manager/instance.h:
added some function defs
server-tools/instance-manager/instance_map.cc:
pthread_mutex_lock and unlock do not return a value on Windows
Also, now using only the file named as Options::config_file
server-tools/instance-manager/instance_options.h:
added reference to port.h
server-tools/instance-manager/listener.cc:
reworked and simplified the socket handling code.
Added windows versions of the code that sets the sockets to be
non-blocking and non-inheritable
server-tools/instance-manager/listener.h:
change Options to always be a struct. Really surprised GCC was
letting this go. Options was declared to be struct in some places
and class in other places.
server-tools/instance-manager/log.cc:
added reference to port.h
server-tools/instance-manager/manager.cc:
moved all the signal code inside some #ifndef __WIN__ blocks
server-tools/instance-manager/manager.h:
change Options to always be a struct. Really surprised GCC was
letting this go. Options was declared to be struct in some places
and class in other places.
server-tools/instance-manager/mysqlmanager.cc:
added in the Windows service code.
server-tools/instance-manager/options.cc:
Added in the windows options for running as a service and the code
for loading settings only from a single file
server-tools/instance-manager/options.h:
added definitions for the new Windows service vars and routines
server-tools/instance-manager/parse_output.cc:
added reference to port.h
server-tools/instance-manager/priv.cc:
added reference to port.h
server-tools/instance-manager/priv.h:
linuxthreads should not be visible on Windows
server-tools/instance-manager/thread_registry.cc:
more __WIN__ blocking
server-tools/instance-manager/user_map.cc:
fixed passwd file code to handle files with \r\n line endings
server-tools/instance-manager/IMService.cpp:
New BitKeeper file ``server-tools/instance-manager/IMService.cpp''
server-tools/instance-manager/IMService.h:
New BitKeeper file ``server-tools/instance-manager/IMService.h''
server-tools/instance-manager/WindowsService.cpp:
New BitKeeper file ``server-tools/instance-manager/WindowsService.cpp''
server-tools/instance-manager/WindowsService.h:
New BitKeeper file ``server-tools/instance-manager/WindowsService.h''
server-tools/instance-manager/mysqlmanager.vcproj:
New BitKeeper file ``server-tools/instance-manager/mysqlmanager.vcproj''
server-tools/instance-manager/port.h:
New BitKeeper file ``server-tools/instance-manager/port.h''
Diffstat (limited to 'server-tools/instance-manager/instance.cc')
-rw-r--r-- | server-tools/instance-manager/instance.cc | 212 |
1 files changed, 175 insertions, 37 deletions
diff --git a/server-tools/instance-manager/instance.cc b/server-tools/instance-manager/instance.cc index 2b25c74439e..5e3c07b9b31 100644 --- a/server-tools/instance-manager/instance.cc +++ b/server-tools/instance-manager/instance.cc @@ -18,14 +18,19 @@ #pragma implementation #endif +#ifdef __WIN__ +#include <process.h> +#endif #include "instance.h" #include "mysql_manager_error.h" #include "log.h" #include "instance_map.h" #include "priv.h" - +#include "port.h" +#ifndef __WIN__ #include <sys/wait.h> +#endif #include <my_sys.h> #include <signal.h> #include <m_string.h> @@ -50,6 +55,16 @@ pthread_handler_decl(proxy, arg) 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); +} + /* The method starts an instance. @@ -65,8 +80,6 @@ C_MODE_END int Instance::start() { - pid_t pid; - /* clear crash flag */ pthread_mutex_lock(&LOCK_instance); crashed= 0; @@ -75,11 +88,7 @@ int Instance::start() if (!is_running()) { - 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); + remove_pid(); /* No need to monitor this thread in the Thread_registry, as all @@ -107,20 +116,21 @@ int Instance::start() return ER_INSTANCE_ALREADY_STARTED; } - -void Instance::fork_and_monitor() +#ifndef __WIN__ +int Instance::launch_and_wait() { - pid_t pid; - log_info("starting instance %s", options.instance_name); - switch (pid= fork()) { - 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; - default: + 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: /* Here we wait for the child created. This process differs for systems running LinuxThreads and POSIX Threads compliant systems. This is because @@ -141,22 +151,89 @@ void Instance::fork_and_monitor() wait(NULL); /* LinuxThreads were detected */ else waitpid(pid, NULL, 0); - /* set instance state to crashed */ - pthread_mutex_lock(&LOCK_instance); - crashed= 1; - pthread_mutex_unlock(&LOCK_instance); - - /* - Wake connection threads waiting for an instance to stop. This - is needed if a user issued command to stop an instance via - mysql connection. This is not the case if Guardian stop the thread. - */ - pthread_cond_signal(&COND_instance_stopped); - /* wake guardian */ - pthread_cond_signal(&instance_map->guardian->COND_guardian); - /* thread exits */ - return; } + return 0; +} +#else +int Instance::launch_and_wait() +{ + STARTUPINFO si; + PROCESS_INFORMATION pi; + + ZeroMemory( &si, sizeof(si) ); + si.cb = sizeof(si); + ZeroMemory( &pi, sizeof(pi) ); + + 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++) + { + 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; + + // Wait until child process exits. + WaitForSingleObject(pi.hProcess, INFINITE); + + DWORD exitcode; + ::GetExitCodeProcess(pi.hProcess, &exitcode); + + // Close process and thread handles. + CloseHandle( pi.hProcess ); + CloseHandle( pi.hThread ); + + return exitcode; +} +#endif + + +void Instance::fork_and_monitor() +{ + log_info("starting instance %s", options.instance_name); + + int result = launch_and_wait(); + if (result == -1) return; + + /* set instance state to crashed */ + pthread_mutex_lock(&LOCK_instance); + crashed= 1; + pthread_mutex_unlock(&LOCK_instance); + + /* + Wake connection threads waiting for an instance to stop. This + is needed if a user issued command to stop an instance via + mysql connection. This is not the case if Guardian stop the thread. + */ + pthread_cond_signal(&COND_instance_stopped); + /* wake guardian */ + pthread_cond_signal(&instance_map->guardian->COND_guardian); + /* thread exits */ + return; + /* we should never end up here */ DBUG_ASSERT(0); } @@ -253,7 +330,6 @@ bool Instance::is_running() int Instance::stop() { - pid_t pid; struct timespec timeout; uint waitchild= (uint) DEFAULT_SHUTDOWN_DELAY; @@ -290,6 +366,68 @@ err: return ER_STOP_INSTANCE; } +#ifdef __WIN__ + +BOOL SafeTerminateProcess(HANDLE hProcess, UINT uExitCode) +{ + DWORD dwTID, dwCode, dwErr = 0; + HANDLE hProcessDup = INVALID_HANDLE_VALUE; + HANDLE hRT = NULL; + HINSTANCE hKernel = GetModuleHandle("Kernel32"); + BOOL bSuccess = FALSE; + + BOOL bDup = DuplicateHandle(GetCurrentProcess(), + hProcess, GetCurrentProcess(), &hProcessDup, PROCESS_ALL_ACCESS, FALSE, 0); + + // Detect the special case where the process is + // already dead... + if ( GetExitCodeProcess((bDup) ? hProcessDup : hProcess, &dwCode) && + (dwCode == STILL_ACTIVE) ) + { + FARPROC pfnExitProc; + + pfnExitProc = GetProcAddress(hKernel, "ExitProcess"); + + hRT = CreateRemoteThread((bDup) ? hProcessDup : hProcess, NULL, 0, + (LPTHREAD_START_ROUTINE)pfnExitProc, (PVOID)uExitCode, 0, &dwTID); + + if ( hRT == NULL ) + dwErr = GetLastError(); + } + else + { + dwErr = ERROR_PROCESS_ABORTED; + } + + if ( hRT ) + { + // Must wait process to terminate to + // guarantee that it has exited... + WaitForSingleObject((bDup) ? hProcessDup : hProcess, INFINITE); + + CloseHandle(hRT); + bSuccess = TRUE; + } + + if ( bDup ) + CloseHandle(hProcessDup); + + if ( !bSuccess ) + SetLastError(dwErr); + + return bSuccess; +} + +int kill(pid_t pid, int signum) +{ + HANDLE processhandle = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); + if (signum == SIGTERM) + ::SafeTerminateProcess(processhandle, 0); + else + ::TerminateProcess(processhandle, -1); + return 0; +} +#endif void Instance::kill_instance(int signum) { |