1 /* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
2 
3   This program is free software; you can redistribute it and/or modify
4   it under the terms of the GNU General Public License, version 2.0,
5   as published by the Free Software Foundation.
6 
7   This program is also distributed with certain software (including
8   but not limited to OpenSSL) that is licensed under separate terms,
9   as designated in a particular file or component or in included license
10   documentation.  The authors of MySQL hereby grant you an additional
11   permission to link the program and your derivative works with the
12   separately licensed software that they have included with MySQL.
13 
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License, version 2.0, for more details.
18 
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #ifndef SQL_RESTART_MONITOR_WIN_H_
24 #define SQL_RESTART_MONITOR_WIN_H_
25 
26 #include <stdlib.h>
27 #include <string.h>
28 
29 /**
30   Type indicating the type of signals which involve communication
31   between the monitor and the mysqld.
32 */
33 
34 enum class Signal_type {
35   /**
36     Shutdown signal from monitor to mysqld. This is used
37     to relay the shutdown that comes from the SCM.
38   */
39   SIGNAL_SHUTDOWN,
40 
41   /**
42     Signal used to send the service status command to the monitor.
43   */
44 
45   SIGNAL_SERVICE_STATUS_CMD,
46 
47   /**
48     Signal to the the mysqld from monitor indicating service status command
49     has been processed.
50   */
51 
52   SIGNAL_SERVICE_STATUS_CMD_PROCESSED
53 };
54 
55 /**
56   Service status message providing an abstraction for the service message sent
57   by monitor to client.
58 */
59 
60 struct Service_status_msg {
61  private:
62   /**
63     Service status message indicating type of update to be done monitor to SCM.
64   */
65 
66   char m_service_msg[32];
67 
68  public:
69   /**
70     Constructor which initializes the service status message to a null string.
71   */
72 
Service_status_msgService_status_msg73   Service_status_msg() { m_service_msg[0] = '\0'; }
74 
75   /**
76     Constructor initializes the service status with a particular message.
77 
78     @param msg pointer to message string.
79   */
80 
Service_status_msgService_status_msg81   Service_status_msg(const char *msg) {
82     strncpy(m_service_msg, msg, sizeof(m_service_msg));
83   }
84 
85   /**
86     Get service message.
87 
88     @return pointer to the service message.
89   */
90 
service_msgService_status_msg91   const char *service_msg() const { return m_service_msg; }
92 };
93 
94 /**
95   Type of messages logged by monitor logging.
96 */
97 
98 enum class Monitor_log_msg_type {
99   /**
100     Error log.
101   */
102   MONITOR_LOG_ERROR,
103 
104   /**
105     Warning log.
106   */
107   MONITOR_LOG_WARN,
108 
109   /**
110     Information log.
111   */
112   MONITOR_LOG_INFO
113 };
114 
115 /**
116   Initialize the mysqld monitor. This method is called from
117   both the monitor and mysqld. It sets the variable that
118   distinguishes the monitor and the mysqld. It also initialize the
119   monitor logging subsystem if the process under which it is called
120   is monitor.
121 */
122 
123 bool initialize_mysqld_monitor();
124 
125 /**
126   Deinitialize the monitor.
127   This method essentially closes any handles opened during initialization.
128 */
129 
130 void deinitialize_mysqld_monitor();
131 
132 /**
133   Send service status message to the monitor. This method is used by mysqld to
134   send service status like running and setting the slow timeout value.
135 */
136 
137 bool send_service_status(const Service_status_msg &);
138 
139 /**
140   Close the service status pipe. This method is called by
141   the mysqld child process.
142 */
143 
144 void close_service_status_pipe_in_mysqld();
145 
146 /**
147   Get char representation corresponding to MYSQLD_PARENT_PID.
148 
149   @return Pointer to string representing pid of the monitor.
150 */
151 
152 const char *get_monitor_pid();
153 
154 /**
155   Signal an event of type Signal_type.
156 
157   @param signal type of the event.
158 */
159 
160 void signal_event(Signal_type signal);
161 
162 /**
163   Check if option is an early type or --gdb, --no-monitor.
164   The early type options are verbose, help, initialize, version
165   and initialize-insecure. These options print and do certain activities
166   and allow the server to exit. In addition there are options like gdb,
167   no-monitor where we do not spawn a monitor process.
168 
169   @param argc   Count of arguments.
170   @param argv   Vector of arguments.
171 
172   @return true if we are early option else false.
173 */
174 
175 bool is_early_option(int argc, char **argv);
176 
177 /**
178   Check if we are monitor process.
179 
180   @return true if the current process is monitor else false.
181 */
182 
183 bool is_mysqld_monitor();
184 
185 /**
186   Check if the monitor is started under a windows service.
187 
188   @return true if the monitor is started as a windows service.
189 */
190 
191 bool is_monitor_win_service();
192 
193 /**
194   Start the monitor if we are called in parent (monitor) context.
195   In child context, set the event names and get the monitor process
196   pid and return -1.
197 
198   @return -1 if we are in child context else exit code of the mysqld process.
199 */
200 
201 int start_monitor();
202 
203 /**
204   Setup the service status command processed handle.
205   This method is called from mysqld context. This handle ensures the
206   synchronization required between the monitor and mysqld once the
207   monitor has handled the service status sent by client.
208 */
209 
210 bool setup_service_status_cmd_processed_handle();
211 
212 /**
213   Close the Service Status Cmd Processed handle.
214 */
215 
216 void close_service_status_cmd_processed_handle();
217 
218 // extern declarations.
219 extern bool is_windows_service();
220 class NTService;
221 extern NTService *get_win_service_ptr();
222 #endif  // SQL_RESTART_MONITOR_WIN_H_
223