1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
/* Copyright (C) 2003 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#ifndef CPCD_HPP
#define CPCD_HPP
#include <Vector.hpp>
#include <Properties.hpp>
#include <NdbOut.hpp>
#include <NdbThread.h>
#include <NdbCondition.h>
#include <BaseString.hpp>
/* XXX Need to figure out how to do this for non-Unix systems */
#define CPCD_DEFAULT_WORK_DIR "/var/run/ndb_cpcd"
#define CPCD_DEFAULT_PROC_FILE "ndb_cpcd.conf"
#define CPCD_DEFAULT_TCP_PORT 1234
#define CPCD_DEFAULT_POLLING_INTERVAL 5 /* seconds */
#define CPCD_DEFAULT_CONFIG_FILE "/etc/ndb_cpcd.conf"
enum ProcessStatus {
STOPPED = 0,
STARTING = 1,
RUNNING = 2,
STOPPING = 3
};
enum ProcessType {
PERMANENT = 0,
TEMPORARY = 1
};
struct CPCEvent {
enum EventType {
ET_USER_CONNECT,
ET_USER_DISCONNECT,
ET_PROC_USER_DEFINE, // Defined proc
ET_PROC_USER_UNDEFINE, // Undefined proc
ET_PROC_USER_START, // Proc ordered to start
ET_PROC_USER_STOP, // Proc ordered to stop
ET_PROC_STATE_RUNNING, // exec returned(?) ok
ET_PROC_STATE_STOPPED // detected that proc is ! running
};
int m_proc;
time_t m_time;
EventType m_type;
};
struct EventSubscriber {
virtual void report(const CPCEvent &) = 0;
};
/**
* @brief Error codes for CPCD requests
*/
enum RequestStatusCode {
OK = 0, ///< Everything OK
Error = 1, ///< Generic error
AlreadyExists = 2, ///< Entry already exists in list
NotExists = 3, ///< Entry does not exist in list
AlreadyStopped = 4
};
/**
* @class CPCD
* @brief Manages processes, letting them be controlled with a TCP connection.
*
* The class implementing the Cluster Process Control Daemon
*/
class CPCD {
public:
/** @brief Describes the status of a client request */
class RequestStatus {
public:
/** @brief Constructs an empty RequestStatus */
RequestStatus() { m_status = OK; m_errorstring[0] = '\0'; };
/** @brief Sets an errorcode and a printable message */
void err(enum RequestStatusCode, const char *);
/** @brief Returns the error message */
char *getErrMsg() { return m_errorstring; };
/** @brief Returns the error code */
enum RequestStatusCode getStatus() { return m_status; };
private:
enum RequestStatusCode m_status;
char m_errorstring[256];
};
/**
* @brief Manages a process
*/
class Process {
int m_pid;
public:
/**
* @brief Constructs and empty Process
*/
Process(const Properties & props, class CPCD *cpcd);
/**
* @brief Monitors the process
*
* The process is started or stopped as needed.
*/
void monitor();
/**
* @brief Checks if the process is running or not
*
* @return
* - true if the process is running,
* - false if the process is not running
*/
bool isRunning();
/** @brief Starts the process */
int start();
/** @brief Stops the process */
void stop();
/**
* @brief Reads the pid from stable storage
*
* @return The pid number
*/
int readPid();
/**
* @brief Writes the pid from stable storage
*
* @return
* - 0 if successful
- -1 and sets errno if an error occured
*/
int writePid(int pid);
/**
* @brief Prints a textual description of the process on a file
*/
void print(FILE *);
/** Id number of the Process.
*
* @note This is not the same as a pid. This number is used in the
* protocol, and will not be changed if a processes is restarted.
*/
int m_id;
/** @brief The name shown to the user */
BaseString m_name;
/** @brief Used to group a number of processes */
BaseString m_group;
/** @brief Environment variables
*
* Environmentvariables to add for the process.
*
* @note
* - The environment cpcd started with is preserved
* - There is no way to delete variables
*/
BaseString m_env;
/** @brief Path to the binary to run */
BaseString m_path;
/** @brief Arguments to the process.
*
* @note
* - This includes argv[0].
* - If no argv[0] is given, argv[0] will be set to m_path.
*/
BaseString m_args;
/**
* @brief Type of process
*
* Either set to "interactive" or "permanent".
*/
BaseString m_type;
ProcessType m_processType;
/**
* @brief Working directory
*
* Working directory the process will start in.
*/
BaseString m_cwd;
/**
* @brief Owner of the process.
*
* @note This will not affect the process' uid or gid;
* it is only used for managemental purposes.
* @see m_runas
*/
BaseString m_owner;
/**
* @bried Run as
* @note This affects uid
* @see m_owner
*/
BaseString m_runas;
/**
* @brief redirection for stdin
*/
BaseString m_stdin;
/**
* @brief redirection for stdout
*/
BaseString m_stdout;
/**
* @brief redirection for stderr
*/
BaseString m_stderr;
/** @brief Status of the process */
enum ProcessStatus m_status;
/**
* @brief ulimits for process
* @desc Format c:unlimited d:0 ...
*/
BaseString m_ulimit;
private:
class CPCD *m_cpcd;
void do_exec();
};
/**
* @brief Starts and stops processes as needed
*
* At a specified interval (default 5 seconds) calls the monitor function
* of all the processes in the CPCDs list, causing the to start or
* stop, depending on the configuration.
*/
class Monitor {
public:
/** Creates a new CPCD::Monitor object, connected to the specified
* CPCD.
* A new thread will be created, which will poll the processes of
* the CPCD at the specifed interval.
*/
Monitor(CPCD *cpcd, int poll = CPCD_DEFAULT_POLLING_INTERVAL);
/** Stops the monitor, but does not stop the processes */
~Monitor();
/** Runs the monitor thread. */
void run();
/** Signals configuration changes to the monitor thread, causing it to
* do the check without waiting for the timeout */
void signal();
private:
class CPCD *m_cpcd;
struct NdbThread *m_monitorThread;
bool m_monitorThreadQuitFlag;
struct NdbCondition *m_changeCondition;
NdbMutex *m_changeMutex;
int m_pollingInterval; /* seconds */
};
/** @brief Constructs a CPCD object */
CPCD();
/**
* @brief Destroys a CPCD object,
* but does not stop the processes it manages
*/
~CPCD();
/** Adds a Process to the CPCDs list of managed Processes.
*
* @note The process will not be started until it is explicitly
* marked as running with CPCD::startProcess().
*
* @return
* - true if the addition was successful,
* - false if not
* - RequestStatus will be filled in with a suitable error
* if an error occured.
*/
bool defineProcess(RequestStatus *rs, Process * arg);
/** Removes a Process from the CPCD.
*
* @note A Process that is running cannot be removed.
*
* @return
* - true if the removal was successful,
* - false if not
* - The RequestStatus will be filled in with a suitable error
* if an error occured.
*/
bool undefineProcess(RequestStatus *rs, int id);
/** Marks a Process for starting.
*
* @note The fact that a process has started does not mean it will actually
* start properly. This command only makes sure the CPCD will
* try to start it.
*
* @return
* - true if the marking was successful
* - false if not
* - RequestStatus will be filled in with a suitable error
* if an error occured.
*/
bool startProcess(RequestStatus *rs, int id);
/** Marks a Process for stopping.
*
* @return
* - true if the marking was successful
* - false if not
* - The RequestStatus will be filled in with a suitable error
* if an error occured.
*/
bool stopProcess(RequestStatus *rs, int id);
/** Generates a list of processes, and sends them to the CPCD client */
bool listProcesses(RequestStatus *rs, MutexVector<const char *> &);
/** Set to true while the CPCD is reading the configuration file */
bool loadingProcessList;
/** Saves the list of Processes and their status to the configuration file.
* Called whenever the configuration is changed.
*/
bool saveProcessList();
/** Loads the list of Processes and their status from the configuration
* file.
* @note This function should only be called when the CPCD is starting,
* calling it at other times will cause unspecified behaviour.
*/
bool loadProcessList();
/** Returns the list of processes */
MutexVector<Process *> *getProcessList();
/** The list of processes. Should not be used directly */
MutexVector<Process *> m_processes;
/** Register event subscriber */
void do_register(EventSubscriber * sub);
EventSubscriber* do_unregister(EventSubscriber * sub);
private:
friend class Process;
bool notifyChanges();
int findUniqueId();
BaseString m_procfile;
Monitor *m_monitor;
void report(int id, CPCEvent::EventType);
MutexVector<EventSubscriber *> m_subscribers;
};
#endif
|