-
Notifications
You must be signed in to change notification settings - Fork 45
/
php.c
567 lines (486 loc) · 18 KB
/
php.c
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
ex: set tabstop=4 shiftwidth=4 autoindent:
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2024 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU Lesser General Public |
| License as published by the Free Software Foundation; either |
| version 2.1 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 Lesser General Public License for more details. |
| |
| You should have received a copy of the GNU Lesser General Public |
| License along with this library; if not, write to the Free Software |
| Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 02110-1301, USA |
| |
+-------------------------------------------------------------------------+
| spine: a backend data gatherer for cacti |
+-------------------------------------------------------------------------+
| This poller would not have been possible without: |
| - Larry Adams (current development and enhancements) |
| - Rivo Nurges (rrd support, mysql poller cache, misc functions) |
| - RTG (core poller code, pthreads, snmp, autoconf examples) |
| - Brady Alleman/Doug Warner (threading ideas, implimentation details) |
+-------------------------------------------------------------------------+
| - Cacti - http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
#include "common.h"
#include "spine.h"
/*! \fn char *php_cmd(const char *php_command, int php_process)
* \brief calls the script server and executes a script command
* \param php_command the formatted php script server command
* \param php_process the php script server process to call
*
* This function is called directly by the spine poller when a script server
* request has been initiated for a host. It will place the PHP Script Server
* command on it's output pipe and then wait the pre-defined timeout period for
* a response on the PHP Script Servers output pipe.
*
* \return pointer to the string results. Must be freed by the parent.
*
*/
char *php_cmd(const char *php_command, int php_process) {
char *result_string;
char command[BUFSIZE];
ssize_t bytes;
int retries = 0;
assert(php_command != 0);
/* pad command with CR-LF */
snprintf(command, BUFSIZE, "%s\r\n", php_command);
/* place lock around mutex */
switch (php_process) {
case 0: thread_mutex_lock(LOCK_PHP_PROC_0); break;
case 1: thread_mutex_lock(LOCK_PHP_PROC_1); break;
case 2: thread_mutex_lock(LOCK_PHP_PROC_2); break;
case 3: thread_mutex_lock(LOCK_PHP_PROC_3); break;
case 4: thread_mutex_lock(LOCK_PHP_PROC_4); break;
case 5: thread_mutex_lock(LOCK_PHP_PROC_5); break;
case 6: thread_mutex_lock(LOCK_PHP_PROC_6); break;
case 7: thread_mutex_lock(LOCK_PHP_PROC_7); break;
case 8: thread_mutex_lock(LOCK_PHP_PROC_8); break;
case 9: thread_mutex_lock(LOCK_PHP_PROC_9); break;
case 10: thread_mutex_lock(LOCK_PHP_PROC_10); break;
case 11: thread_mutex_lock(LOCK_PHP_PROC_11); break;
case 12: thread_mutex_lock(LOCK_PHP_PROC_12); break;
case 13: thread_mutex_lock(LOCK_PHP_PROC_13); break;
case 14: thread_mutex_lock(LOCK_PHP_PROC_14); break;
}
/* send command to the script server */
retry:
bytes = write(php_processes[php_process].php_write_fd, command, strlen(command));
/* if write status is <= 0 then the script server may be hung */
if (bytes <= 0) {
result_string = strdup("U");
SPINE_LOG(("ERROR: SS[%i] PHP Script Server communications lost sending Command[%s]. Restarting PHP Script Server", php_process, command));
php_close(php_process);
php_init(php_process);
/* increment and retry a few times on the next item */
retries++;
if (retries < 3) {
goto retry;
}
} else {
/* read the result from the php_command */
result_string = php_readpipe(php_process, command);
/* check for a null */
if (!strlen(result_string)) {
SET_UNDEFINED(result_string);
}
}
/* unlock around php process */
switch (php_process) {
case 0: thread_mutex_unlock(LOCK_PHP_PROC_0); break;
case 1: thread_mutex_unlock(LOCK_PHP_PROC_1); break;
case 2: thread_mutex_unlock(LOCK_PHP_PROC_2); break;
case 3: thread_mutex_unlock(LOCK_PHP_PROC_3); break;
case 4: thread_mutex_unlock(LOCK_PHP_PROC_4); break;
case 5: thread_mutex_unlock(LOCK_PHP_PROC_5); break;
case 6: thread_mutex_unlock(LOCK_PHP_PROC_6); break;
case 7: thread_mutex_unlock(LOCK_PHP_PROC_7); break;
case 8: thread_mutex_unlock(LOCK_PHP_PROC_8); break;
case 9: thread_mutex_unlock(LOCK_PHP_PROC_9); break;
case 10: thread_mutex_unlock(LOCK_PHP_PROC_10); break;
case 11: thread_mutex_unlock(LOCK_PHP_PROC_11); break;
case 12: thread_mutex_unlock(LOCK_PHP_PROC_12); break;
case 13: thread_mutex_unlock(LOCK_PHP_PROC_13); break;
case 14: thread_mutex_unlock(LOCK_PHP_PROC_14); break;
}
return result_string;
}
/*! \fn in php_get_process()
* \brief returns the next php script server process to utilize
*
* This very simple function simply returns the next PHP Script Server
* process id to poll using a round robin algorithm.
*
* \return the integer number of the next script server to use
*
*/
int php_get_process(void) {
int i;
thread_mutex_lock(LOCK_PHP);
if (set.php_current_server >= set.php_servers) {
set.php_current_server = 0;
}
i = set.php_current_server;
set.php_current_server++;
thread_mutex_unlock(LOCK_PHP);
return i;
}
/*! \fn char *php_readpipe(int php_process, char *command)
* \brief read a line from a PHP Script Server process
* \param php_process the PHP Script Server process to obtain output from
*
* This function will read the output pipe from the PHP Script Server process
* and return that string to the Spine thread requesting the output. If for
* some reason the PHP Script Server process does not respond in time, it will
* be closed using the php_close function, then restarted.
*
* \return a string pointer to the PHP Script Server response
*/
char *php_readpipe(int php_process, char *command) {
fd_set fds;
struct timeval timeout;
double begin_time = 0;
double end_time = 0;
double remaining_usec = 0;
char *result_string;
int i;
char *cp;
char *bptr;
if (!(result_string = (char *)malloc(RESULTS_BUFFER))) {
die("ERROR: Fatal malloc error: php.c php_readpipe!");
}
result_string[0] = '\0';
/* record start time */
begin_time = get_time_as_double();
/* establish timeout value for the PHP script server to respond */
timeout.tv_sec = set.script_timeout;
timeout.tv_usec = 0;
/* check to see which pipe talked and take action
* should only be the READ pipe */
retry:
/* initialize file descriptors to review for input/output */
FD_ZERO(&fds);
FD_SET(php_processes[php_process].php_read_fd,&fds);
switch (select(php_processes[php_process].php_read_fd+1, &fds, NULL, NULL, &timeout)) {
case -1:
switch (errno) {
case EBADF:
SPINE_LOG(("ERROR: SS[%i] An invalid file descriptor was given in one of the sets.", php_process));
break;
case EINTR:
#ifndef SOLAR_THREAD
/* take a moment */
usleep(2000);
#endif
/* record end time */
end_time = get_time_as_double();
/* re-establish new timeout value */
timeout.tv_sec = rint(floor(set.script_timeout-(end_time-begin_time)));
remaining_usec = set.script_timeout - timeout.tv_sec - (end_time - begin_time);
if (remaining_usec > 0) {
timeout.tv_usec = rint(remaining_usec * 1000000);
} else {
timeout.tv_usec = 0;
}
if (timeout.tv_sec + timeout.tv_usec > 0) {
goto retry;
} else {
SPINE_LOG(("WARNING: SS[%i] The Script Server script timed out while processing EINTR's.", php_process));
}
break;
case EINVAL:
SPINE_LOG(("ERROR: SS[%i] N is negative or the value contained within timeout is invalid.", php_process));
break;
case ENOMEM:
SPINE_LOG(("ERROR: SS[%i] Select was unable to allocate memory for internal tables.", php_process));
break;
default:
SPINE_LOG(("ERROR: SS[%i] Unknown fatal select() error", php_process));
break;
}
SET_UNDEFINED(result_string);
/* kill script server because it is misbehaving */
php_close(php_process);
php_init(php_process);
break;
case 0:
/* record end time */
end_time = get_time_as_double();
SPINE_LOG(("WARNING: SS[%i] The PHP Script Server did not respond in time for Timeout[%0.2f], Command[%s] and will therefore be restarted", php_process, end_time - start_time, command));
SET_UNDEFINED(result_string);
/* kill script server because it is misbehaving */
php_close(php_process);
php_init(php_process);
break;
default:
if (FD_ISSET(php_processes[php_process].php_read_fd, &fds)) {
bptr = result_string;
while (1) {
i = read(php_processes[php_process].php_read_fd, bptr, RESULTS_BUFFER-(bptr-result_string));
if (i <= 0) {
SET_UNDEFINED(result_string);
break;
}
bptr += i;
*bptr = '\0'; /* make what we've got into a string */
if ((cp = strstr(result_string,"\n")) != 0) {
break;
}
if (bptr >= result_string+BUFSIZE) {
SPINE_LOG(("ERROR: SS[%i] The Script Server result was longer than the acceptable range", php_process));
SET_UNDEFINED(result_string);
}
}
} else {
SPINE_LOG(("ERROR: SS[%i] The FD was not set as expected", php_process));
SET_UNDEFINED(result_string);
}
php_processes[php_process].php_state = PHP_READY;
}
return result_string;
}
/*! \fn int php_init(int php_process)
* \brief initialize either a specific PHP Script Server or all of them.
* \param php_process the process number to start or PHP_INIT
*
* This function will either start an individual PHP Script Server process
* or all of them if the input parameter is the PHP_INIT constant. The function
* will check the status of the process to verify that it is ready to process
* scripts as well.
*
* \return TRUE if the PHP Script Server is know running or FALSE otherwise
*/
int php_init(int php_process) {
int cacti2php_pdes[2];
int php2cacti_pdes[2];
pid_t pid;
char poller_id[TINY_BUFSIZE];
char mode[TINY_BUFSIZE];
char *argv[7];
int cancel_state;
char *result_string = 0;
int num_processes;
int i;
int retry_count = 0;
char *command = strdup("INIT");
/* special code to start all PHP Servers */
if (php_process == PHP_INIT) {
num_processes = set.php_servers;
} else {
num_processes = 1;
}
for (i=0; i < num_processes; i++) {
SPINE_LOG_DEBUG(("DEBUG: SS[%i] PHP Script Server Routine Starting", i));
/* create the output pipes from Spine to php*/
if (pipe(cacti2php_pdes) < 0) {
SPINE_LOG(("ERROR: SS[%i] Could not allocate php server pipes", i));
return FALSE;
}
/* create the input pipes from php to Spine */
if (pipe(php2cacti_pdes) < 0) {
SPINE_LOG(("ERROR: SS[%i] Could not allocate php server pipes", i));
return FALSE;
}
/* disable thread cancellation from this point forward. */
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
/* establish arguments for script server execution */
if (set.cacti_version <= 1222) {
argv[0] = set.path_php;
argv[1] = "-q";
argv[2] = set.path_php_server;
argv[3] = "spine";
snprintf(poller_id, TINY_BUFSIZE, "%d", set.poller_id);
argv[4] = poller_id;
argv[5] = NULL;
} else if (set.poller_id > 1) {
argv[0] = set.path_php;
argv[1] = "-q";
argv[2] = set.path_php_server;
argv[3] = "--environ=spine";
snprintf(poller_id, TINY_BUFSIZE, "--poller=%d", set.poller_id);
argv[4] = poller_id;
if (set.mode == REMOTE_ONLINE) {
snprintf(mode, TINY_BUFSIZE, "--mode=online");
} else {
snprintf(mode, TINY_BUFSIZE, "--mode=offline");
}
argv[5] = mode;
argv[6] = NULL;
} else {
argv[0] = set.path_php;
argv[1] = "-q";
argv[2] = set.path_php_server;
argv[3] = "--environ=spine";
snprintf(poller_id, TINY_BUFSIZE, "--poller=%d", set.poller_id);
argv[4] = poller_id;
argv[5] = NULL;
}
/* fork a child process */
SPINE_LOG_DEBUG(("DEBUG: SS[%i] PHP Script Server About to FORK Child Process", i));
retry:
pid = vfork();
/* check the pid status and process as required */
switch (pid) {
case -1: /* ERROR: Could not fork() */
switch (errno) {
case EAGAIN:
if (retry_count < 3) {
retry_count++;
#ifndef SOLAR_THREAD
/* take a moment */
usleep(50000);
#endif
goto retry;
} else {
SPINE_LOG(("ERROR: SS[%i] Could not fork PHP Script Server Out of Resources", i));
}
case ENOMEM:
if (retry_count < 3) {
retry_count++;
#ifndef SOLAR_THREAD
/* take a moment */
usleep(50000);
#endif
goto retry;
} else {
SPINE_LOG(("ERROR: SS[%i] Could not fork PHP Script Server Out of Memory", i));
}
default:
SPINE_LOG(("ERROR: SS[%i] Could not fork PHP Script Server Unknown Reason", i));
}
close(php2cacti_pdes[0]);
close(php2cacti_pdes[1]);
close(cacti2php_pdes[0]);
close(cacti2php_pdes[1]);
SPINE_LOG(("ERROR: SS[%i] Could not fork PHP Script Server", i));
pthread_setcancelstate(cancel_state, NULL);
return FALSE;
/* NOTREACHED */
case 0: /* SUCCESS: I am now the child */
/* set the standard input/output channels of the new process. */
dup2(cacti2php_pdes[0], STDIN_FILENO);
dup2(php2cacti_pdes[1], STDOUT_FILENO);
/* close unneeded Pipes */
(void)close(php2cacti_pdes[0]);
(void)close(php2cacti_pdes[1]);
(void)close(cacti2php_pdes[0]);
(void)close(cacti2php_pdes[1]);
/* start the php script server process */
execv(argv[0], argv);
_exit(127);
/* NOTREACHED */
default: /* I am the parent process */
SPINE_LOG_DEBUG(("DEBUG: SS[%i] PHP Script Server Child FORK Success", i));
}
/* Parent */
/* close unneeded pipes */
close(cacti2php_pdes[0]);
close(php2cacti_pdes[1]);
if (php_process == PHP_INIT) {
php_processes[i].php_pid = pid;
php_processes[i].php_write_fd = cacti2php_pdes[1];
php_processes[i].php_read_fd = php2cacti_pdes[0];
} else {
php_processes[php_process].php_pid = pid;
php_processes[php_process].php_write_fd = cacti2php_pdes[1];
php_processes[php_process].php_read_fd = php2cacti_pdes[0];
}
/* restore caller's cancellation state. */
pthread_setcancelstate(cancel_state, NULL);
/* check pipe to insure startup took place */
if (php_process == PHP_INIT) {
result_string = php_readpipe(i, command);
} else {
result_string = php_readpipe(php_process, command);
}
if (strstr(result_string, "Started")) {
if (php_process == PHP_INIT) {
SPINE_LOG_DEBUG(("DEBUG: SS[%i] Confirmed PHP Script Server running using readfd[%i], writefd[%i]", i, php2cacti_pdes[0], cacti2php_pdes[1]));
php_processes[i].php_state = PHP_READY;
} else {
SPINE_LOG_DEBUG(("DEBUG: SS[%i] Confirmed PHP Script Server running using readfd[%i], writefd[%i]", php_process, php2cacti_pdes[0], cacti2php_pdes[1]));
php_processes[php_process].php_state = PHP_READY;
}
} else {
if (php_process == PHP_INIT) {
SPINE_LOG(("ERROR: SS[%i] Script Server did not start properly return message was: '%s'", i, result_string));
php_processes[i].php_state = PHP_BUSY;
} else {
SPINE_LOG(("ERROR: SS[%i] Script Server did not start properly return message was: '%s'", php_process, result_string));
php_processes[php_process].php_state = PHP_BUSY;
}
}
free(result_string);
}
return TRUE;
}
/*! \fn void php_close(int php_process)
* \brief close the php script server process
* \param php_process the process to close or PHP_INIT
*
* This function will take an input parameter of either a specially coded
* PHP_INIT parameter or an integer stating the process number. With that
* information is will close and/or terminate the child PHP Script Server
* process and then return to the calling function.
*
* TODO: Make ending of the child process not be reliant on SIG_TERM in cases
* where the child process is hung for one reason or another.
*
*/
void php_close(int php_process) {
int i;
int num_processes;
ssize_t bytes;
if (php_process == PHP_INIT) {
num_processes = set.php_servers;
} else {
num_processes = 1;
}
for(i = 0; i < num_processes; i++) {
php_t *phpp;
SPINE_LOG_DEBUG(("DEBUG: SS[%i] Script Server Shutdown Started", i));
/* tell the script server to close */
if (php_process == PHP_INIT) {
phpp = &php_processes[i];
} else {
phpp = &php_processes[php_process];
}
/* If we still have a valid write pipe, tell PHP to close down
* by sending a "quit" message, then closing the input channel
* so it gets an EOF.
*
* Then we wait a moment before actually killing it to allow for
* a clean shutdown.
*/
if (phpp->php_write_fd >= 0) {
static const char quit[] = "quit\r\n";
bytes = write(phpp->php_write_fd, quit, strlen(quit));
close(phpp->php_write_fd);
phpp->php_write_fd = -1;
/* wait before killing php */
#ifndef SOLAR_THREAD
usleep(50000); /* 50 msec */
#endif
}
/* only try to kill the process if the PID looks valid.
* Trying to kill a negative number is bad news (it's
* a process group leader), and PID 1 is "init".
*/
if (phpp->php_pid > 1) {
/* end the php script server process */
kill(phpp->php_pid, SIGTERM);
/* reset this PID variable? */
}
/* close file descriptors */
close(phpp->php_read_fd);
phpp->php_read_fd = -1;
}
}