-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
359 lines (316 loc) · 7.99 KB
/
main.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
/*
+----------------------------------------------------------------------+
| Copyright (c) 2009-2015, Johannes Schlüter <johannes@schlueters.de> |
| All rights reserved. |
+----------------------------------------------------------------------+
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the conditions which are |
| bundled with this package in the file LICENSE. |
| This product includes PHP software, freely available from |
| <http://www.php.net/software/> |
+----------------------------------------------------------------------+
*/
#include <stdio.h>
#ifdef PHP_WIN32
#else
#include <unistd.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include "TSRM/TSRM.h"
#include "pconnect.h"
#include "main/php_version.h"
#include "main/php_getopt.h"
#include "main/php_streams.h"
#include "pconnect-sapi.h"
#include "pconnect-phptparser.h"
#define MAX_THREADS 255
typedef struct {
int iterations;
char *phpt_skipif_script_data;
char *filename;
char *mmapped;
size_t mmapped_len;
struct phpt phpt;
char *startup_script;
char *shutdown_script;
} req_data;
const opt_struct OPTIONS[] = {
{'v', 0, "version"},
{'h', 0, "help"},
{'i', 0, "info"},
{'c', 1, "php-ini"},
#if ZTS
{'t', 1, "threads"},
#endif
{'n', 1, "iterations"},
{'p', 0, "progress"},
{'a', 1, "init-script"},
{'z', 1, "shutdown-script"},
{10, 0, "phpt"},
{'-', 0, NULL}
};
int pconn_report_progress = 0;
static void run_php(req_data *data TSRMLS_DC)
{
unsigned char *user_data = NULL;
size_t user_data_len = 0;
int i = data->iterations;
if (data->startup_script) {
pconn_do_request_f(data->startup_script, &user_data, &user_data_len TSRMLS_CC);
}
while (i--) {
int retval;
if (data->phpt.file.begin) {
retval = pconn_do_request_d(data->filename, data->phpt.file.begin, data->phpt.file.end - data->phpt.file.begin, &user_data, &user_data_len TSRMLS_CC);
} else {
retval = pconn_do_request_d(data->filename, data->mmapped, data->mmapped_len, &user_data, &user_data_len TSRMLS_CC);
}
if (retval == FAILURE) {
#ifdef FIX_ME
/* On Windows I always reach this code path, dunno why */
break;
#endif
}
if (pconn_report_progress) {
printf("(%d/%d done)\r", data->iterations - i, data->iterations);
}
}
if (pconn_report_progress) {
printf("\n");
}
if (data->shutdown_script) {
pconn_do_request_f(data->shutdown_script, &user_data, &user_data_len TSRMLS_CC);
}
if (user_data) {
free(user_data);
}
}
#ifdef ZTS
#ifdef PHP_WIN32
DWORD WINAPI php_thread(LPVOID arg)
#else
static void *php_thread(void *arg)
#endif
{
req_data *data = (req_data *)arg;
TSRMLS_FETCH();
run_php(data TSRMLS_CC);
#ifdef PHP_WIN32
return 0;
#else
return NULL;
#endif
}
#ifdef PHP_WIN32
static void run_threads(req_data *data, int concurrency)
{
int i;
HANDLE threads[MAX_THREADS];
for (i=0; i < concurrency && i < MAX_THREADS; i++) {
threads[i] = CreateThread(NULL, 0, php_thread, data, 0, NULL);
}
WaitForMultipleObjects(concurrency, threads, TRUE, INFINITE);
for (i=0; i < concurrency && i < MAX_THREADS; i++) {
CloseHandle(threads[i]);
}
}
#else /* PHP_WIN32 */
static void run_threads(req_data *data, int concurrency)
{
int i;
pthread_t threads[MAX_THREADS];
for (i=0; i < concurrency && i < MAX_THREADS; i++) {
pthread_create(&threads[i], NULL, php_thread, data);
}
for (i=0; i < concurrency && i < MAX_THREADS; i++) {
pthread_join(threads[i], NULL);
}
}
#endif /* PHP_WIN32 */
#endif /* ZTS */
static void usage(const char *name, const int status)
{
fprintf(stderr, "Usage: %s [-p] [-n iterations]"
#ifdef ZTS
" [-t <threads>]"
#endif
" [-a <startup>] [-z <shutdown>] [--phpt] <script>\n"
" %s -v\n"
" %s -i\n\n"
" -v Print version information\n"
" -i Print phpinfo();\n"
" -c <file> Look for php.ini file in this directory\n"
" -n <iterations> Set number of iterations (default=2)\n"
#ifdef ZTS
" -t <threads> Set the number of concurrent threads (default=1, max=%i)\n"
#endif
" -p Report progress (hides regular script output"
#ifdef ZTS
"\n limited use with multiple threads"
#endif
")\n"
" -a <startup> Startup script, executed once on start\n"
" -z <shutdown> Shutdown script, executed once on end\n"
" --phpt Treat <script> as phpt\n"
" <script> Main script to be executed multiple times\n\n"
, name
, name
, name
#ifdef ZTS
, MAX_THREADS
#endif
);
exit(status);
}
static void pconn_version()
{
printf("pconn test %s for PHP %s (built: %s %s) %s %s\n"
"Copyright (c) 2009-2015, Johannes Schlueter\n"
"This product includes PHP software, freely available from <http://www.php.net/software/>.\n",
PCONN_VERSION, PHP_VERSION, __DATE__, __TIME__,
#if ZEND_DEBUG && defined(HAVE_GCOV)
"(DEBUG GCOV)",
#elif ZEND_DEBUG
"(DEBUG)",
#elif defined(HAVE_GCOV)
"(GCOV)",
#else
"",
#endif
#if ZTS
"(TSRM)"
#else
""
#endif
);
}
static int process_phpt(req_data *data)
{
if (data->mmapped_len < 100) {
printf("Too small, can't be a test!\n");
return 1;
}
if (memcmp("--TEST--", data->mmapped, sizeof("--TEST--")-1)) {
printf("Not a phpt test file\n");
return 1;
}
parse_phpt(&data->phpt, data->mmapped, data->mmapped + data->mmapped_len);
return 0;
}
static int init_script_data(req_data *data, int is_phpt)
{
int fd;
struct stat ssb;
if ((fd = open(data->filename, O_RDONLY)) == -1) {
perror("Failed opening file");
return 1;
}
if (fstat(fd, &ssb)) {
perror("Failed to stat the opened file");
close(fd);
return 1;
}
data->mmapped_len = ssb.st_size;
data->mmapped = mmap(NULL, data->mmapped_len, PROT_READ, MAP_PRIVATE, fd, 0);
if (!data->mmapped) {
perror("Error while mapping file content");
close(fd);
return 1;
}
close(fd);
if (is_phpt) {
if (process_phpt(data)) {
return 1;
}
printf("Running test: ");
fflush(stdout);
write(STDOUT_FILENO, data->phpt.test.begin, data->phpt.test.end - data->phpt.test.begin);
}
return 0;
}
int main(int argc, char *argv[])
{
#ifdef ZTS
int threads = 1;
#endif
int opt;
char *php_optarg = NULL;
int php_optind = 1;
req_data data = { 2, NULL, NULL, NULL, 0, {}, NULL, NULL };
int is_phpt = 0;
while ((opt = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
switch (opt) {
case 'v':
pconn_version();
return 0;
case 'i':
pconn_phpinfo();
return 0;
case 'c':
pconn_set_ini_file(php_optarg);
break;
case 'a':
data.startup_script = php_optarg;
break;
case 'z':
data.shutdown_script = php_optarg;
break;
#ifdef ZTS
case 't':
threads = atoi(php_optarg);
if (threads < 1 || threads > MAX_THREADS) {
usage(argv[0], 1); /*terminates */
}
break;
#endif
case 'n':
data.iterations = atoi(php_optarg);
if (data.iterations <= 0) {
usage(argv[0], 1); /*terminates */
}
break;
case 'p':
pconn_report_progress = 1;
break;
case 10:
is_phpt = 1;
break;
default:
usage(argv[0], 1); /* terminates */
case 'h':
usage(argv[0], 0); /* terminates */
}
}
if (php_optind >= argc) {
usage(argv[0], 1); /* terminates */
}
pconn_init_php();
data.filename = argv[php_optind];
if (init_script_data(&data, is_phpt)) {
pconn_shutdown_php();
munmap(data.mmapped, data.mmapped_len);
return 1;
}
#ifdef ZTS
run_threads(&data, threads);
#else
run_php(&data);
#endif
pconn_shutdown_php();
munmap(data.mmapped, data.mmapped_len);
return 0;
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/