MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CPCD.hpp
1 /*
2  Copyright (C) 2003-2007 MySQL AB, 2009, 2010 Sun Microsystems, Inc.
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #ifndef CPCD_HPP
20 #define CPCD_HPP
21 
22 #include <Vector.hpp>
23 #include <Properties.hpp>
24 #include <NdbOut.hpp>
25 #include <NdbThread.h>
26 #include <NdbCondition.h>
27 #include <BaseString.hpp>
28 
29 #ifdef _WIN32
30 typedef DWORD pid_t;
31 #endif
32 const pid_t bad_pid = -1;
33 
34 inline bool is_bad_pid(pid_t pid)
35 {
36 #ifdef _WIN32
37  return pid == bad_pid;
38 #else
39  return pid <= 1;
40 #endif
41 }
42 
43 #define CPCD_DEFAULT_PROC_FILE "ndb_cpcd.conf"
44 #define CPCD_DEFAULT_TCP_PORT 1234
45 #define CPCD_DEFAULT_POLLING_INTERVAL 5 /* seconds */
46 #ifndef _WIN32
47 #define CPCD_DEFAULT_WORK_DIR "/var/run/ndb_cpcd"
48 #define CPCD_DEFAULT_CONFIG_FILE "/etc/ndb_cpcd.conf"
49 
50 #else
51 #define CPCD_DEFAULT_WORK_DIR "c:\\ndb_cpcd"
52 #define CPCD_DEFAULT_CONFIG_FILE "c:\\ndb_cpcd\\ndb_cpcd.conf"
53 #endif
54 enum ProcessStatus {
55  STOPPED = 0,
56  STARTING = 1,
57  RUNNING = 2,
58  STOPPING = 3
59 };
60 
61 enum ProcessType {
62  PERMANENT = 0,
63  TEMPORARY = 1
64 };
65 
69 enum RequestStatusCode {
70  OK = 0,
71  Error = 1,
72  AlreadyExists = 2,
73  NotExists = 3,
74  AlreadyStopped = 4
75 };
76 
83 class CPCD {
84 public:
86  class RequestStatus {
87  public:
89  RequestStatus() { m_status = OK; m_errorstring[0] = '\0'; };
90 
92  void err(enum RequestStatusCode, const char *);
93 
95  char *getErrMsg() { return m_errorstring; };
96 
98  enum RequestStatusCode getStatus() { return m_status; };
99  private:
100  enum RequestStatusCode m_status;
101  char m_errorstring[256];
102  };
106  class Process {
107  pid_t m_pid;
108 #ifdef _WIN32
109  HANDLE m_job;
110 #endif
111  public:
115  Process(const Properties & props, class CPCD *cpcd);
121  void monitor();
122 
130  bool isRunning();
131 
133  int start();
134 
136  void stop();
137 
143  int readPid();
144 
152  int writePid(int pid);
153 
157  void print(FILE *);
158 
164  int m_id;
165 
168 
171 
181 
184 
192 
199  ProcessType m_processType;
200 
207 
216 
223 
228 
233 
238 
240  enum ProcessStatus m_status;
241 
247 
252 
253  private:
254  class CPCD *m_cpcd;
255  void do_exec();
256  };
257 
265  class Monitor {
266  public:
272  Monitor(CPCD *cpcd, int poll = CPCD_DEFAULT_POLLING_INTERVAL);
273 
275  ~Monitor();
276 
278  void run();
279 
282  void signal();
283  private:
284  class CPCD *m_cpcd;
285  struct NdbThread *m_monitorThread;
286  bool m_monitorThreadQuitFlag;
287  struct NdbCondition *m_changeCondition;
288  NdbMutex *m_changeMutex;
289  int m_pollingInterval; /* seconds */
290  };
291 
293  CPCD();
294 
299  ~CPCD();
300 
312  bool defineProcess(RequestStatus *rs, Process * arg);
313 
324  bool undefineProcess(RequestStatus *rs, int id);
325 
338  bool startProcess(RequestStatus *rs, int id);
339 
348  bool stopProcess(RequestStatus *rs, int id);
349 
352 
355 
359  bool saveProcessList();
360 
366  bool loadProcessList();
367 
370 
373 
374 private:
375  friend class Process;
376  bool notifyChanges();
377  int findUniqueId();
378  BaseString m_procfile;
379  Monitor *m_monitor;
380 };
381 
382 #endif