MySQL 8.4.3
Source Code Documentation
restart_monitor_win.cc File Reference
#include "restart_monitor_win.h"
#include <assert.h>
#include <windows.h>
#include <memory>
#include <vector>
#include <shellapi.h>
#include "my_sys.h"
#include "mysqld.h"
#include "sql/log.h"
#include "sql/message.h"
#include "sql/sql_const.h"

Functions

static int monitor_mysqld (LPSTR cmdline)
 Spawn mysqld process. More...
 
static bool initialize_events (void)
 Initialize event handles in monitor process. More...
 
bool initialize_monitor_log ()
 Initialize the monitor log. More...
 
void deinitialize_monitor_log ()
 Deinitialize the monitor log. More...
 
static WORD get_event_type (Monitor_log_msg_type type)
 Map monitor msg type to corresponding event log type. More...
 
static void log_in_eventlog (WORD type, LPCSTR msg)
 Log a message to the event log. More...
 
void monitor_log_msg (Monitor_log_msg_type type, LPCTSTR format,...)
 Log an msg. More...
 
static void signal_thr_open_event (const char *event_name)
 Signal an named event by the event name. More...
 
const char * get_monitor_pid ()
 Get char representation corresponding to MYSQLD_PARENT_PID. More...
 
void signal_event (Signal_type signal_type)
 Signal an event of type Signal_type. More...
 
static HANDLE get_service_status_pipe_in_mysqld ()
 Get handle to service status pipe. More...
 
void close_service_status_pipe_in_mysqld ()
 Close mysqld service status pipe. More...
 
static bool setup_service_status_pipe_in_monitor ()
 Setup service status pipe. More...
 
static void close_service_status_pipe_in_monitor ()
 Close service status pipe. More...
 
bool send_service_status (const Service_status_msg &msg)
 Send service status message to the monitor. More...
 
static bool get_and_set_service_status ()
 Get the service status command from the mysqld process and process it and set the status in the SCM. More...
 
static void quote_arg (const std::string &arg, std::string *cmd_line)
 Add an argument to a command line adding quotes and backlashes for paths as necessary. More...
 
static std::string construct_cmd_line (const std::vector< std::string > &args)
 Construct the command line that is to be passed to the spawned mysqld process. More...
 
static bool is_early_short_option (const char ch)
 Check if the char representing is an early option. More...
 
static bool is_early_option (const char *cur_arg)
 Check if the option specified is an early option. More...
 
bool is_early_option (int argc, char **argv)
 Check if option is an early type or –gdb, –no-monitor. More...
 
bool initialize_mysqld_monitor ()
 Initialize the mysqld monitor. More...
 
bool is_monitor_win_service ()
 Check if the monitor is started under a windows service. More...
 
void deinitialize_mysqld_monitor ()
 Deinitialize the monitor. More...
 
bool is_mysqld_monitor ()
 Check if we are monitor process. More...
 
static void set_event_names ()
 This function assigns names to the shutdown event, service status command event, service status command processed event and the name of service status pipe. More...
 
bool setup_service_status_cmd_processed_handle ()
 Create an event handle to indicate service status command has been processed from the mysqld monitor parent to the mysqld child. More...
 
void close_service_status_cmd_processed_handle ()
 Close the Service Status Cmd Processed handle. More...
 
int start_monitor ()
 Start the monitor if we are called in parent (monitor) context. More...
 

Variables

static DWORD my_monitor_pid
 
static bool mysqld_monitor_process
 
static HANDLE service_status_cmd_event
 
static HANDLE service_status_pipe
 
static HANDLE client_service_status_pipe = nullptr
 
static HANDLE service_status_cmd_processed_handle = nullptr
 
static HANDLE event_log_handle = nullptr
 
static const int NUM_WAIT_HANDLES = 2
 
static const int MYSQLD_HANDLE = 0
 
static const int SERVICE_STATUS_CMD_HANDLE = 1
 
static const int EVENT_NAME_LEN = 32
 
static char service_status_cmd_processed_event_name [EVENT_NAME_LEN]
 
static char shutdown_event_name [EVENT_NAME_LEN]
 
static char service_status_pipe_name [EVENT_NAME_LEN]
 
static char service_status_cmd_event_name [EVENT_NAME_LEN]
 

Function Documentation

◆ close_service_status_cmd_processed_handle()

void close_service_status_cmd_processed_handle ( )

Close the Service Status Cmd Processed handle.

◆ close_service_status_pipe_in_monitor()

static void close_service_status_pipe_in_monitor ( )
static

Close service status pipe.

This handle is closed in context of the monitor process.

◆ close_service_status_pipe_in_mysqld()

void close_service_status_pipe_in_mysqld ( )

Close mysqld service status pipe.

Close the service status pipe.

This call shall made in context of the mysqld process.

◆ construct_cmd_line()

static std::string construct_cmd_line ( const std::vector< std::string > &  args)
static

Construct the command line that is to be passed to the spawned mysqld process.

Parameters
argsreference to vector of string arguments.
Returns
string representing the cmd line.

◆ deinitialize_monitor_log()

void deinitialize_monitor_log ( )

Deinitialize the monitor log.

This method deregisters the event log.

◆ deinitialize_mysqld_monitor()

void deinitialize_mysqld_monitor ( )

Deinitialize the monitor.

This method essentially closes any handles opened during initialization.

◆ get_and_set_service_status()

static bool get_and_set_service_status ( )
static

Get the service status command from the mysqld process and process it and set the status in the SCM.

Returns
bool true if the call is successful else false.

◆ get_event_type()

static WORD get_event_type ( Monitor_log_msg_type  type)
static

Map monitor msg type to corresponding event log type.

Parameters
typeType of the message.
Returns
a DWORD indicating event log type.

◆ get_monitor_pid()

const char * get_monitor_pid ( )

Get char representation corresponding to MYSQLD_PARENT_PID.

Returns
Pointer to string representing pid of the monitor.

◆ get_service_status_pipe_in_mysqld()

static HANDLE get_service_status_pipe_in_mysqld ( )
static

Get handle to service status pipe.

This call will be in the context of mysqld process.

Returns
handle to the service status pipe.

◆ initialize_events()

static bool initialize_events ( void  )
static

Initialize event handles in monitor process.

◆ initialize_monitor_log()

bool initialize_monitor_log ( )

Initialize the monitor log.

In case mysqld is spawned from windows service, we use event log for logging error messages. (stderr/stdout is not available when started as as a service.)

◆ initialize_mysqld_monitor()

bool initialize_mysqld_monitor ( )

Initialize the mysqld monitor.

This method is called from both the monitor and mysqld. It sets the variable that distinguishes the monitor and the mysqld. It also initialize the monitor logging subsystem if the process under which it is called is monitor.

◆ is_early_option() [1/2]

static bool is_early_option ( const char *  cur_arg)
inlinestatic

Check if the option specified is an early option.

Parameters
cur_argstring representing an early option.
Returns
true if string is an early option else false.

◆ is_early_option() [2/2]

bool is_early_option ( int  argc,
char **  argv 
)

Check if option is an early type or –gdb, –no-monitor.

The early type options are verbose, help, initialize, version and initialize-insecure. These options print and do certain activities and allow the server to exit. In addition there are options like gdb, no-monitor where we do not spawn a monitor process.

Parameters
argcCount of arguments.
argvVector of arguments.
Returns
true if we are early option else false.

◆ is_early_short_option()

static bool is_early_short_option ( const char  ch)
inlinestatic

Check if the char representing is an early option.

Parameters
chcharactering representing an short option.
Returns
true if the option char is an early short option else false.

◆ is_monitor_win_service()

bool is_monitor_win_service ( )

Check if the monitor is started under a windows service.

Returns
true if the monitor is started as a windows service.

◆ is_mysqld_monitor()

bool is_mysqld_monitor ( )

Check if we are monitor process.

Returns
true if the current process is monitor else false.

◆ log_in_eventlog()

static void log_in_eventlog ( WORD  type,
LPCSTR  msg 
)
inlinestatic

Log a message to the event log.

Parameters
typeEvent log type.
msgPointer to the message string.

◆ monitor_log_msg()

void monitor_log_msg ( Monitor_log_msg_type  type,
LPCTSTR  format,
  ... 
)

Log an msg.

If the monitor is started as a windows service, then log it to the event log using apporirate type.

Parameters
typeType of message indicating whether it is information, warning, error.
formatformat string.

◆ monitor_mysqld()

static int monitor_mysqld ( LPSTR  cmdline)
static

Spawn mysqld process.

Wait on spawned process handle for mysqld exit. On exit, retrieve exit code to check for restart and shutdown or abort exit. If we exit using MYSQLD_RESTART_EXIT code then we respawn this child process. In addition, if the mysqld process is instantiated as a windows service, then we create a named pipe. The pipe allows for communication from mysqld to send service status code. This service status code can then be relayed to the Service Control Manager using the service status handle. This is used to relay the service running status and set slow status timeout value.

◆ quote_arg()

static void quote_arg ( const std::string &  arg,
std::string *  cmd_line 
)
static

Add an argument to a command line adding quotes and backlashes for paths as necessary.

Parameters
argstring representing the argument.
[in,out]cmd_linecommand line to which the string should be appended with necessary quoting.

◆ send_service_status()

bool send_service_status ( const Service_status_msg msg)

Send service status message to the monitor.

This method is used by mysqld to send service status like running and setting the slow timeout value.

◆ set_event_names()

static void set_event_names ( )
static

This function assigns names to the shutdown event, service status command event, service status command processed event and the name of service status pipe.

◆ setup_service_status_cmd_processed_handle()

bool setup_service_status_cmd_processed_handle ( )

Create an event handle to indicate service status command has been processed from the mysqld monitor parent to the mysqld child.

Setup the service status command processed handle.

Returns
true if service status command processed handle could not be setup else false.

◆ setup_service_status_pipe_in_monitor()

static bool setup_service_status_pipe_in_monitor ( )
static

Setup service status pipe.

Returns
true if service status pipe is setup successfully else false.

◆ signal_event()

void signal_event ( Signal_type  signal)

Signal an event of type Signal_type.

Parameters
signaltype of the event.

◆ signal_thr_open_event()

static void signal_thr_open_event ( const char *  event_name)
static

Signal an named event by the event name.

Parameters
event_namepointer to char string representing the event.

◆ start_monitor()

int start_monitor ( )

Start the monitor if we are called in parent (monitor) context.

In child context, set the event names and get the monitor process pid and return -1.

Returns
-1 if we are in child context else exit code of the mysqld process.

Variable Documentation

◆ client_service_status_pipe

HANDLE client_service_status_pipe = nullptr
static

◆ event_log_handle

HANDLE event_log_handle = nullptr
static

◆ EVENT_NAME_LEN

const int EVENT_NAME_LEN = 32
static

◆ my_monitor_pid

DWORD my_monitor_pid
static

◆ MYSQLD_HANDLE

const int MYSQLD_HANDLE = 0
static

◆ mysqld_monitor_process

bool mysqld_monitor_process
static

◆ NUM_WAIT_HANDLES

const int NUM_WAIT_HANDLES = 2
static

◆ service_status_cmd_event

HANDLE service_status_cmd_event
static

◆ service_status_cmd_event_name

char service_status_cmd_event_name[EVENT_NAME_LEN]
static

◆ SERVICE_STATUS_CMD_HANDLE

const int SERVICE_STATUS_CMD_HANDLE = 1
static

◆ service_status_cmd_processed_event_name

char service_status_cmd_processed_event_name[EVENT_NAME_LEN]
static

◆ service_status_cmd_processed_handle

HANDLE service_status_cmd_processed_handle = nullptr
static

◆ service_status_pipe

HANDLE service_status_pipe
static

◆ service_status_pipe_name

char service_status_pipe_name[EVENT_NAME_LEN]
static

◆ shutdown_event_name

char shutdown_event_name[EVENT_NAME_LEN]
static