1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available at through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt. |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: George Wang <gwang@litespeedtech.com> |
16 +----------------------------------------------------------------------+
17 */
18
19 #include "php.h"
20 #include "SAPI.h"
21 #include "php_main.h"
22 #include "php_ini.h"
23 #include "php_variables.h"
24 #include "zend_highlight.h"
25 #include "zend.h"
26 #include "ext/standard/basic_functions.h"
27 #include "ext/standard/info.h"
28 #include "lsapilib.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #if HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36
37 #include <sys/wait.h>
38 #include <sys/stat.h>
39
40 #if HAVE_SYS_TYPES_H
41
42 #include <sys/types.h>
43
44 #endif
45
46 #include <signal.h>
47 #include <sys/socket.h>
48 #include <arpa/inet.h>
49 #include <netinet/in.h>
50 #include <sys/time.h>
51
52 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
53 #include "lscriu.c"
54 #endif
55
56 #define SAPI_LSAPI_MAX_HEADER_LENGTH LSAPI_RESP_HTTP_HEADER_MAX
57
58 /* Key for each cache entry is dirname(PATH_TRANSLATED).
59 *
60 * NOTE: Each cache entry config_hash contains the combination from all user ini files found in
61 * the path starting from doc_root through to dirname(PATH_TRANSLATED). There is no point
62 * storing per-file entries as it would not be possible to detect added / deleted entries
63 * between separate files.
64 */
65 typedef struct _user_config_cache_entry {
66 time_t expires;
67 HashTable user_config;
68 } user_config_cache_entry;
69 static HashTable user_config_cache;
70
71 static int lsapi_mode = 0;
72 static char *php_self = "";
73 static char *script_filename = "";
74 static int source_highlight = 0;
75 static int ignore_php_ini = 0;
76 static char * argv0 = NULL;
77 static int engine = 1;
78 static int parse_user_ini = 0;
79
80 #ifdef ZTS
81 zend_compiler_globals *compiler_globals;
82 zend_executor_globals *executor_globals;
83 php_core_globals *core_globals;
84 sapi_globals_struct *sapi_globals;
85 #endif
86
87 zend_module_entry litespeed_module_entry;
88
init_sapi_from_env(sapi_module_struct * sapi_module)89 static void init_sapi_from_env(sapi_module_struct *sapi_module)
90 {
91 char *p;
92 p = getenv("LSPHPRC");
93 if (p)
94 sapi_module->php_ini_path_override = p;
95 }
96
97 /* {{{ php_lsapi_startup
98 */
php_lsapi_startup(sapi_module_struct * sapi_module)99 static int php_lsapi_startup(sapi_module_struct *sapi_module)
100 {
101 if (php_module_startup(sapi_module, NULL, 0)==FAILURE) {
102 return FAILURE;
103 }
104 argv0 = sapi_module->executable_location;
105 return SUCCESS;
106 }
107 /* }}} */
108
109 /* {{{ sapi_lsapi_ini_defaults */
110
111 /* overwriteable ini defaults must be set in sapi_cli_ini_defaults() */
112 #define INI_DEFAULT(name,value)\
113 ZVAL_STRING(tmp, value, 0);\
114 zend_hash_update(configuration_hash, name, sizeof(name), tmp, sizeof(zval), (void**)&entry);\
115 Z_STRVAL_P(entry) = zend_strndup(Z_STRVAL_P(entry), Z_STRLEN_P(entry))
116
sapi_lsapi_ini_defaults(HashTable * configuration_hash)117 static void sapi_lsapi_ini_defaults(HashTable *configuration_hash)
118 {
119 #if PHP_MAJOR_VERSION > 4
120 /*
121 zval *tmp, *entry;
122
123 MAKE_STD_ZVAL(tmp);
124
125 INI_DEFAULT("register_long_arrays", "0");
126
127 FREE_ZVAL(tmp);
128 */
129 #endif
130
131 }
132 /* }}} */
133
134
135 /* {{{ sapi_lsapi_ub_write
136 */
sapi_lsapi_ub_write(const char * str,size_t str_length)137 static size_t sapi_lsapi_ub_write(const char *str, size_t str_length)
138 {
139 int ret;
140 int remain;
141 if ( lsapi_mode ) {
142 ret = LSAPI_Write( str, str_length );
143 if ( ret < str_length ) {
144 php_handle_aborted_connection();
145 return str_length - ret;
146 }
147 } else {
148 remain = str_length;
149 while( remain > 0 ) {
150 ret = write( 1, str, remain );
151 if ( ret <= 0 ) {
152 php_handle_aborted_connection();
153 return str_length - remain;
154 }
155 str += ret;
156 remain -= ret;
157 }
158 }
159 return str_length;
160 }
161 /* }}} */
162
163
164 /* {{{ sapi_lsapi_flush
165 */
sapi_lsapi_flush(void * server_context)166 static void sapi_lsapi_flush(void * server_context)
167 {
168 if ( lsapi_mode ) {
169 if ( LSAPI_Flush() == -1) {
170 php_handle_aborted_connection();
171 }
172 }
173 }
174 /* }}} */
175
176
177 /* {{{ sapi_lsapi_deactivate
178 */
sapi_lsapi_deactivate(void)179 static int sapi_lsapi_deactivate(void)
180 {
181 if ( SG(request_info).path_translated ) {
182 efree( SG(request_info).path_translated );
183 SG(request_info).path_translated = NULL;
184 }
185
186 return SUCCESS;
187 }
188 /* }}} */
189
190
191
192
193 /* {{{ sapi_lsapi_getenv
194 */
sapi_lsapi_getenv(char * name,size_t name_len)195 static char *sapi_lsapi_getenv( char * name, size_t name_len )
196 {
197 if ( lsapi_mode ) {
198 return LSAPI_GetEnv( name );
199 } else {
200 return getenv( name );
201 }
202 }
203 /* }}} */
204
205
add_variable(const char * pKey,int keyLen,const char * pValue,int valLen,void * arg)206 static int add_variable( const char * pKey, int keyLen, const char * pValue, int valLen,
207 void * arg )
208 {
209 int filter_arg = (Z_ARR_P((zval *)arg) == Z_ARR(PG(http_globals)[TRACK_VARS_ENV]))
210 ? PARSE_ENV : PARSE_SERVER;
211 char * new_val = (char *) pValue;
212 size_t new_val_len;
213
214 if (sapi_module.input_filter(filter_arg, (char *)pKey, &new_val, valLen, &new_val_len)) {
215 php_register_variable_safe((char *)pKey, new_val, new_val_len, (zval *)arg );
216 }
217 return 1;
218 }
219
litespeed_php_import_environment_variables(zval * array_ptr)220 static void litespeed_php_import_environment_variables(zval *array_ptr)
221 {
222 char buf[128];
223 char **env, *p, *t = buf;
224 size_t alloc_size = sizeof(buf);
225 unsigned long nlen; /* ptrdiff_t is not portable */
226
227 if (Z_TYPE(PG(http_globals)[TRACK_VARS_ENV]) == IS_ARRAY &&
228 Z_ARR_P(array_ptr) != Z_ARR(PG(http_globals)[TRACK_VARS_ENV]) &&
229 zend_hash_num_elements(Z_ARRVAL(PG(http_globals)[TRACK_VARS_ENV])) > 0
230 ) {
231 zval_ptr_dtor_nogc(array_ptr);
232 ZVAL_DUP(array_ptr, &PG(http_globals)[TRACK_VARS_ENV]);
233 return;
234 } else if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY &&
235 Z_ARR_P(array_ptr) != Z_ARR(PG(http_globals)[TRACK_VARS_SERVER]) &&
236 zend_hash_num_elements(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER])) > 0
237 ) {
238 zval_ptr_dtor_nogc(array_ptr);
239 ZVAL_DUP(array_ptr, &PG(http_globals)[TRACK_VARS_SERVER]);
240 return;
241 }
242
243 tsrm_env_lock();
244 for (env = environ; env != NULL && *env != NULL; env++) {
245 p = strchr(*env, '=');
246 if (!p) { /* malformed entry? */
247 continue;
248 }
249 nlen = p - *env;
250 if (nlen >= alloc_size) {
251 alloc_size = nlen + 64;
252 t = (t == buf ? emalloc(alloc_size): erealloc(t, alloc_size));
253 }
254 memcpy(t, *env, nlen);
255 t[nlen] = '\0';
256 add_variable(t, nlen, p + 1, strlen( p + 1 ), array_ptr);
257 }
258 tsrm_env_unlock();
259 if (t != buf && t != NULL) {
260 efree(t);
261 }
262 }
263
264 /* {{{ sapi_lsapi_register_variables
265 */
sapi_lsapi_register_variables(zval * track_vars_array)266 static void sapi_lsapi_register_variables(zval *track_vars_array)
267 {
268 char * php_self = "";
269 if ( lsapi_mode ) {
270 if ( (SG(request_info).request_uri ) )
271 php_self = (SG(request_info).request_uri );
272
273 litespeed_php_import_environment_variables(track_vars_array);
274
275 LSAPI_ForeachHeader( add_variable, track_vars_array );
276 LSAPI_ForeachEnv( add_variable, track_vars_array );
277 add_variable("PHP_SELF", 8, php_self, strlen( php_self ), track_vars_array );
278 } else {
279 php_import_environment_variables(track_vars_array);
280
281 php_register_variable("PHP_SELF", php_self, track_vars_array);
282 php_register_variable("SCRIPT_NAME", php_self, track_vars_array);
283 php_register_variable("SCRIPT_FILENAME", script_filename, track_vars_array);
284 php_register_variable("PATH_TRANSLATED", script_filename, track_vars_array);
285 php_register_variable("DOCUMENT_ROOT", "", track_vars_array);
286
287 }
288 }
289 /* }}} */
290
291
292 /* {{{ sapi_lsapi_read_post
293 */
sapi_lsapi_read_post(char * buffer,size_t count_bytes)294 static size_t sapi_lsapi_read_post(char *buffer, size_t count_bytes)
295 {
296 if ( lsapi_mode ) {
297 ssize_t rv = LSAPI_ReadReqBody(buffer, (unsigned long long)count_bytes);
298 return (rv >= 0) ? (size_t)rv : 0;
299 } else {
300 return 0;
301 }
302 }
303 /* }}} */
304
305
306
307
308 /* {{{ sapi_lsapi_read_cookies
309 */
sapi_lsapi_read_cookies(void)310 static char *sapi_lsapi_read_cookies(void)
311 {
312 if ( lsapi_mode ) {
313 return LSAPI_GetHeader( H_COOKIE );
314 } else {
315 return NULL;
316 }
317 }
318 /* }}} */
319
320
321 typedef struct _http_error {
322 int code;
323 const char* msg;
324 } http_error;
325
326 static const http_error http_error_codes[] = {
327 {100, "Continue"},
328 {101, "Switching Protocols"},
329 {200, "OK"},
330 {201, "Created"},
331 {202, "Accepted"},
332 {203, "Non-Authoritative Information"},
333 {204, "No Content"},
334 {205, "Reset Content"},
335 {206, "Partial Content"},
336 {300, "Multiple Choices"},
337 {301, "Moved Permanently"},
338 {302, "Moved Temporarily"},
339 {303, "See Other"},
340 {304, "Not Modified"},
341 {305, "Use Proxy"},
342 {400, "Bad Request"},
343 {401, "Unauthorized"},
344 {402, "Payment Required"},
345 {403, "Forbidden"},
346 {404, "Not Found"},
347 {405, "Method Not Allowed"},
348 {406, "Not Acceptable"},
349 {407, "Proxy Authentication Required"},
350 {408, "Request Time-out"},
351 {409, "Conflict"},
352 {410, "Gone"},
353 {411, "Length Required"},
354 {412, "Precondition Failed"},
355 {413, "Request Entity Too Large"},
356 {414, "Request-URI Too Large"},
357 {415, "Unsupported Media Type"},
358 {428, "Precondition Required"},
359 {429, "Too Many Requests"},
360 {431, "Request Header Fields Too Large"},
361 {451, "Unavailable For Legal Reasons"},
362 {500, "Internal Server Error"},
363 {501, "Not Implemented"},
364 {502, "Bad Gateway"},
365 {503, "Service Unavailable"},
366 {504, "Gateway Time-out"},
367 {505, "HTTP Version not supported"},
368 {511, "Network Authentication Required"},
369 {0, NULL}
370 };
371
372
sapi_lsapi_send_headers_like_cgi(sapi_headers_struct * sapi_headers)373 static int sapi_lsapi_send_headers_like_cgi(sapi_headers_struct *sapi_headers)
374 {
375 char buf[SAPI_LSAPI_MAX_HEADER_LENGTH];
376 sapi_header_struct *h;
377 zend_llist_position pos;
378 zend_bool ignore_status = 0;
379 int response_status = SG(sapi_headers).http_response_code;
380
381 if (SG(request_info).no_headers == 1) {
382 LSAPI_FinalizeRespHeaders();
383 return SAPI_HEADER_SENT_SUCCESSFULLY;
384 }
385
386 if (SG(sapi_headers).http_response_code != 200)
387 {
388 int len;
389 zend_bool has_status = 0;
390
391 char *s;
392
393 if (SG(sapi_headers).http_status_line &&
394 (s = strchr(SG(sapi_headers).http_status_line, ' ')) != 0 &&
395 (s - SG(sapi_headers).http_status_line) >= 5 &&
396 strncasecmp(SG(sapi_headers).http_status_line, "HTTP/", 5) == 0
397 ) {
398 len = slprintf(buf, sizeof(buf), "Status:%s", s);
399 response_status = atoi((s + 1));
400 } else {
401 h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos);
402 while (h) {
403 if (h->header_len > sizeof("Status:")-1 &&
404 strncasecmp(h->header, "Status:", sizeof("Status:")-1) == 0
405 ) {
406 has_status = 1;
407 break;
408 }
409 h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
410 }
411 if (!has_status) {
412 http_error *err = (http_error*)http_error_codes;
413
414 while (err->code != 0) {
415 if (err->code == SG(sapi_headers).http_response_code) {
416 break;
417 }
418 err++;
419 }
420 if (err->msg) {
421 len = slprintf(buf, sizeof(buf), "Status: %d %s", SG(sapi_headers).http_response_code, err->msg);
422 } else {
423 len = slprintf(buf, sizeof(buf), "Status: %d", SG(sapi_headers).http_response_code);
424 }
425 }
426 }
427
428 if (!has_status) {
429 LSAPI_AppendRespHeader( buf, len );
430 ignore_status = 1;
431 }
432 }
433
434 h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos);
435 while (h) {
436 /* prevent CRLFCRLF */
437 if (h->header_len) {
438 if (h->header_len > sizeof("Status:")-1 &&
439 strncasecmp(h->header, "Status:", sizeof("Status:")-1) == 0
440 ) {
441 if (!ignore_status) {
442 ignore_status = 1;
443 LSAPI_AppendRespHeader(h->header, h->header_len);
444 }
445 } else if (response_status == 304 && h->header_len > sizeof("Content-Type:")-1 &&
446 strncasecmp(h->header, "Content-Type:", sizeof("Content-Type:")-1) == 0
447 ) {
448 h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
449 continue;
450 } else {
451 LSAPI_AppendRespHeader(h->header, h->header_len);
452 }
453 }
454 h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
455 }
456
457 LSAPI_FinalizeRespHeaders();
458 return SAPI_HEADER_SENT_SUCCESSFULLY;
459 }
460
461 /*
462 mod_lsapi mode or legacy LS mode
463 */
464 static int mod_lsapi_mode = 0;
465
466
467 /* {{{ sapi_lsapi_send_headers
468 */
sapi_lsapi_send_headers(sapi_headers_struct * sapi_headers)469 static int sapi_lsapi_send_headers(sapi_headers_struct *sapi_headers)
470 {
471 sapi_header_struct *h;
472 zend_llist_position pos;
473
474 if ( mod_lsapi_mode ) {
475 /* mod_lsapi mode */
476 return sapi_lsapi_send_headers_like_cgi(sapi_headers);
477 }
478
479 /* Legacy mode */
480 if ( lsapi_mode ) {
481 LSAPI_SetRespStatus( SG(sapi_headers).http_response_code );
482
483 h = zend_llist_get_first_ex(&sapi_headers->headers, &pos);
484 while (h) {
485 if ( h->header_len > 0 ) {
486 LSAPI_AppendRespHeader(h->header, h->header_len);
487 }
488 h = zend_llist_get_next_ex(&sapi_headers->headers, &pos);
489 }
490 if (SG(sapi_headers).send_default_content_type) {
491 char *hd;
492 int len;
493 char headerBuf[SAPI_LSAPI_MAX_HEADER_LENGTH];
494
495 hd = sapi_get_default_content_type();
496 len = snprintf( headerBuf, SAPI_LSAPI_MAX_HEADER_LENGTH - 1,
497 "Content-type: %s", hd );
498 efree(hd);
499
500 LSAPI_AppendRespHeader( headerBuf, len );
501 }
502 }
503 LSAPI_FinalizeRespHeaders();
504 return SAPI_HEADER_SENT_SUCCESSFULLY;
505
506
507 }
508 /* }}} */
509
510
511 /* {{{ sapi_lsapi_send_headers
512 */
sapi_lsapi_log_message(char * message,int syslog_type_int)513 static void sapi_lsapi_log_message(char *message, int syslog_type_int)
514 {
515 char buf[8192];
516 int len = strlen( message );
517 if ( *(message + len - 1 ) != '\n' )
518 {
519 snprintf( buf, 8191, "%s\n", message );
520 message = buf;
521 if (len > 8191)
522 len = 8191;
523 ++len;
524 }
525 LSAPI_Write_Stderr( message, len);
526 }
527 /* }}} */
528
529 /* Set to 1 to turn on log messages useful during development:
530 */
531 #if 0
532 static void log_message (const char *fmt, ...)
533 {
534 va_list ap;
535 va_start(ap, fmt);
536 char buf[0x100];
537 vsnprintf(buf, sizeof(buf), fmt, ap);
538 va_end(ap);
539 sapi_lsapi_log_message(buf
540 #if PHP_MAJOR_VERSION > 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION >= 1)
541 , 0
542 #endif
543 );
544 }
545 #define DEBUG_MESSAGE(fmt, ...) log_message("LS:%d " fmt "\n", __LINE__, ##__VA_ARGS__)
546 #else
547 #define DEBUG_MESSAGE(fmt, ...)
548 #endif
549
550 static int lsapi_activate_user_ini();
551
sapi_lsapi_activate()552 static int sapi_lsapi_activate()
553 {
554 char *path, *server_name;
555 size_t path_len, server_name_len;
556
557 /* PATH_TRANSLATED should be defined at this stage but better safe than sorry :) */
558 if (!SG(request_info).path_translated) {
559 return FAILURE;
560 }
561
562 if (php_ini_has_per_host_config()) {
563 server_name = sapi_lsapi_getenv("SERVER_NAME", 0);
564 /* SERVER_NAME should also be defined at this stage..but better check it anyway */
565 if (server_name) {
566 server_name_len = strlen(server_name);
567 server_name = estrndup(server_name, server_name_len);
568 zend_str_tolower(server_name, server_name_len);
569 php_ini_activate_per_host_config(server_name, server_name_len);
570 efree(server_name);
571 }
572 }
573
574 if (php_ini_has_per_dir_config()) {
575 /* Prepare search path */
576 path_len = strlen(SG(request_info).path_translated);
577
578 /* Make sure we have trailing slash! */
579 if (!IS_SLASH(SG(request_info).path_translated[path_len])) {
580 path = emalloc(path_len + 2);
581 memcpy(path, SG(request_info).path_translated, path_len + 1);
582 path_len = zend_dirname(path, path_len);
583 path[path_len++] = DEFAULT_SLASH;
584 } else {
585 path = estrndup(SG(request_info).path_translated, path_len);
586 path_len = zend_dirname(path, path_len);
587 }
588 path[path_len] = 0;
589
590 /* Activate per-dir-system-configuration defined in php.ini and stored into configuration_hash during startup */
591 php_ini_activate_per_dir_config(path, path_len); /* Note: for global settings sake we check from root to path */
592
593 efree(path);
594 }
595
596 if (parse_user_ini && lsapi_activate_user_ini() == FAILURE) {
597 return FAILURE;
598 }
599 return SUCCESS;
600 }
601 /* {{{ sapi_module_struct cgi_sapi_module
602 */
603 static sapi_module_struct lsapi_sapi_module =
604 {
605 "litespeed",
606 "LiteSpeed V7.9",
607
608 php_lsapi_startup, /* startup */
609 php_module_shutdown_wrapper, /* shutdown */
610
611 sapi_lsapi_activate, /* activate */
612 sapi_lsapi_deactivate, /* deactivate */
613
614 sapi_lsapi_ub_write, /* unbuffered write */
615 sapi_lsapi_flush, /* flush */
616 NULL, /* get uid */
617 sapi_lsapi_getenv, /* getenv */
618
619 php_error, /* error handler */
620
621 NULL, /* header handler */
622 sapi_lsapi_send_headers, /* send headers handler */
623 NULL, /* send header handler */
624
625 sapi_lsapi_read_post, /* read POST data */
626 sapi_lsapi_read_cookies, /* read Cookies */
627
628 sapi_lsapi_register_variables, /* register server variables */
629 sapi_lsapi_log_message, /* Log message */
630 NULL, /* Get request time */
631 NULL, /* Child terminate */
632
633 STANDARD_SAPI_MODULE_PROPERTIES
634
635 };
636 /* }}} */
637
init_request_info(void)638 static void init_request_info( void )
639 {
640 char * pContentType = LSAPI_GetHeader( H_CONTENT_TYPE );
641 char * pAuth;
642
643 SG(request_info).content_type = pContentType ? pContentType : "";
644 SG(request_info).request_method = LSAPI_GetRequestMethod();
645 SG(request_info).query_string = LSAPI_GetQueryString();
646 SG(request_info).request_uri = LSAPI_GetScriptName();
647 SG(request_info).content_length = LSAPI_GetReqBodyLen();
648 SG(request_info).path_translated = estrdup( LSAPI_GetScriptFileName());
649
650 /* It is not reset by zend engine, set it to 200. */
651 SG(sapi_headers).http_response_code = 200;
652
653 pAuth = LSAPI_GetHeader( H_AUTHORIZATION );
654 php_handle_auth_data(pAuth);
655 }
656
lsapi_execute_script(zend_file_handle * file_handle)657 static int lsapi_execute_script( zend_file_handle * file_handle)
658 {
659 char *p;
660 int len;
661 zend_stream_init_filename(file_handle, SG(request_info).path_translated);
662
663 p = argv0;
664 *p++ = ':';
665 len = strlen( SG(request_info).path_translated );
666 if ( len > 45 )
667 len = len - 45;
668 else
669 len = 0;
670 memccpy( p, SG(request_info).path_translated + len, 0, 46 );
671
672 php_execute_script(file_handle);
673 return 0;
674
675 }
676
lsapi_sigsegv(int signal)677 static void lsapi_sigsegv( int signal )
678 {
679 //fprintf(stderr, "lsapi_sigsegv: %d: Segmentation violation signal is caught during request shutdown\n", getpid());
680 _exit(1);
681 }
682
683 static int do_clean_shutdown = 1;
684
685 static int clean_onexit = 1;
686
687
lsapi_clean_shutdown()688 static void lsapi_clean_shutdown()
689 {
690 struct sigaction act;
691 int sa_rc;
692 struct itimerval tmv;
693 #if PHP_MAJOR_VERSION >= 7
694 zend_string * key;
695 #endif
696 clean_onexit = 1;
697 sigemptyset(&act.sa_mask);
698 act.sa_flags = 0;
699 act.sa_handler = lsapi_sigsegv;
700 sa_rc = sigaction(SIGINT, &act, NULL);
701 sa_rc = sigaction(SIGQUIT, &act, NULL);
702 sa_rc = sigaction(SIGILL, &act, NULL);
703 sa_rc = sigaction(SIGABRT, &act, NULL);
704 sa_rc = sigaction(SIGBUS, &act, NULL);
705 sa_rc = sigaction(SIGSEGV, &act, NULL);
706 sa_rc = sigaction(SIGTERM, &act, NULL);
707
708 sa_rc = sigaction(SIGPROF, &act, NULL);
709 memset(&tmv, 0, sizeof(struct itimerval));
710 tmv.it_value.tv_sec = 0;
711 tmv.it_value.tv_usec = 100000;
712 setitimer(ITIMER_PROF, &tmv, NULL);
713
714 #if PHP_MAJOR_VERSION >= 7
715 key = zend_string_init("error_reporting", 15, 1);
716 zend_alter_ini_entry_chars_ex(key, "0", 1,
717 PHP_INI_SYSTEM, PHP_INI_STAGE_SHUTDOWN, 1);
718 zend_string_release(key);
719 #else
720 zend_alter_ini_entry("error_reporting", 16, "0", 1,
721 PHP_INI_SYSTEM, PHP_INI_STAGE_SHUTDOWN);
722 #endif
723
724 zend_try {
725 php_request_shutdown(NULL);
726 } zend_end_try();
727 }
728
lsapi_sigterm(int signal)729 static void lsapi_sigterm(int signal)
730 {
731
732 // fprintf(stderr, "lsapi_sigterm: %d: clean_onexit %d\n", getpid(), clean_onexit );
733 if(!clean_onexit)
734 {
735 lsapi_clean_shutdown();
736 }
737 exit(1);
738 }
739
lsapi_atexit(void)740 static void lsapi_atexit(void)
741 {
742 //fprintf(stderr, "lsapi_atexit: %d: clean_onexit %d\n", getpid(), clean_onexit );
743 if(!clean_onexit)
744 {
745 lsapi_clean_shutdown();
746 }
747 }
748
749
lsapi_module_main(int show_source)750 static int lsapi_module_main(int show_source)
751 {
752 struct sigaction act;
753 int sa_rc;
754 zend_file_handle file_handle;
755 memset(&file_handle, 0, sizeof(file_handle));
756 if (php_request_startup() == FAILURE ) {
757 return -1;
758 }
759
760 if (do_clean_shutdown) {
761 sigemptyset(&act.sa_mask);
762 act.sa_flags = SA_NODEFER;
763 act.sa_handler = lsapi_sigterm;
764 sa_rc = sigaction( SIGINT, &act, NULL);
765 sa_rc = sigaction( SIGQUIT, &act, NULL);
766 sa_rc = sigaction( SIGILL, &act, NULL);
767 sa_rc = sigaction( SIGABRT, &act, NULL);
768 sa_rc = sigaction( SIGBUS, &act, NULL);
769 sa_rc = sigaction( SIGSEGV, &act, NULL);
770 sa_rc = sigaction( SIGTERM, &act, NULL);
771
772 clean_onexit = 0;
773 }
774
775 if (show_source) {
776 zend_syntax_highlighter_ini syntax_highlighter_ini;
777
778 php_get_highlight_struct(&syntax_highlighter_ini);
779 highlight_file(SG(request_info).path_translated, &syntax_highlighter_ini);
780 } else {
781 lsapi_execute_script( &file_handle);
782 }
783 zend_try {
784 php_request_shutdown(NULL);
785
786 clean_onexit = 1;
787
788 memset( argv0, 0, 46 );
789 } zend_end_try();
790 return 0;
791 }
792
793
alter_ini(const char * pKey,int keyLen,const char * pValue,int valLen,void * arg)794 static int alter_ini( const char * pKey, int keyLen, const char * pValue, int valLen,
795 void * arg )
796 {
797 zend_string * psKey;
798
799 int type = ZEND_INI_PERDIR;
800 int stage = PHP_INI_STAGE_RUNTIME;
801 if ( '\001' == *pKey ) {
802 ++pKey;
803 if ( *pKey == 4 ) {
804 type = ZEND_INI_SYSTEM;
805 /*
806 Use ACTIVATE stage in legacy mode only.
807
808 RUNTIME stage should be used here,
809 as with ACTIVATE it's impossible to change the option from script with ini_set
810 */
811 if(!mod_lsapi_mode)
812 {
813 stage = PHP_INI_STAGE_ACTIVATE;
814 }
815 }
816 else
817 {
818 stage = PHP_INI_STAGE_HTACCESS;
819 }
820 ++pKey;
821 --keyLen;
822 if (( keyLen == 7 )&&( strncasecmp( pKey, "engine", 6 )== 0 ))
823 {
824 if ( *pValue == '0' )
825 engine = 0;
826 }
827 else
828 {
829 --keyLen;
830 psKey = zend_string_init(pKey, keyLen, 1);
831 zend_alter_ini_entry_chars(psKey,
832 (char *)pValue, valLen,
833 type, stage);
834 zend_string_release_ex(psKey, 1);
835 }
836 }
837 return 1;
838 }
839
user_config_cache_entry_dtor(zval * el)840 static void user_config_cache_entry_dtor(zval *el)
841 {
842 user_config_cache_entry *entry = (user_config_cache_entry *)Z_PTR_P(el);
843 zend_hash_destroy(&entry->user_config);
844 free(entry);
845 }
846
user_config_cache_init()847 static void user_config_cache_init()
848 {
849 zend_hash_init(&user_config_cache, 0, NULL, user_config_cache_entry_dtor, 1);
850 }
851
pathlen_without_trailing_slash(char * path)852 static int pathlen_without_trailing_slash(char *path)
853 {
854 int len = (int)strlen(path);
855 while (len > 1 && /* mind "/" as root dir */
856 path[len-1] == DEFAULT_SLASH)
857 {
858 --len;
859 }
860 return len;
861 }
862
skip_slash(char * s)863 static inline char* skip_slash(char *s)
864 {
865 while (*s == DEFAULT_SLASH) {
866 ++s;
867 }
868 return s;
869 }
870
871 /**
872 * Walk down the path_stop starting at path_start.
873 *
874 * If path_start = "/path1" and path_stop = "/path1/path2/path3"
875 * the callback will be called 3 times with the next args:
876 *
877 * 1. "/path1/path2/path3"
878 * ^ end
879 * ^ start
880 * 2. "/path1/path2/path3"
881 * ^ end
882 * ^ start
883 * 3. "/path1/path2/path3"
884 * ^ end
885 * ^ start
886 *
887 * path_stop has to be a subdir of path_start
888 * or to be path_start itself.
889 *
890 * Both path args have to be absolute.
891 * Trailing slashes are allowed.
892 * NULL or empty string args are not allowed.
893 */
walk_down_the_path(char * path_start,char * path_stop,void (* cb)(char * begin,char * end,void * data),void * data)894 static void walk_down_the_path(char* path_start,
895 char* path_stop,
896 void (*cb)(char* begin,
897 char* end,
898 void* data),
899 void* data)
900 {
901 char *pos = path_stop + pathlen_without_trailing_slash(path_start);
902 cb(path_stop, pos, data);
903
904 while ((pos = skip_slash(pos))[0]) {
905 pos = strchr(pos, DEFAULT_SLASH);
906 if (!pos) {
907 /* The last token without trailing slash
908 */
909 cb(path_stop, path_stop + strlen(path_stop), data);
910 return;
911 }
912 cb(path_stop, pos, data);
913 }
914 }
915
916
917 typedef struct {
918 char *path;
919 uint32_t path_len;
920 char *doc_root;
921 user_config_cache_entry *entry;
922 } _lsapi_activate_user_ini_ctx;
923
924 typedef int (*fn_activate_user_ini_chain_t)
925 (_lsapi_activate_user_ini_ctx *ctx, void* next);
926
lsapi_activate_user_ini_basic_checks(_lsapi_activate_user_ini_ctx * ctx,void * next)927 static int lsapi_activate_user_ini_basic_checks(_lsapi_activate_user_ini_ctx *ctx,
928 void* next)
929 {
930 int rc = SUCCESS;
931 fn_activate_user_ini_chain_t *fn_next = next;
932
933 if (!PG(user_ini_filename) || !*PG(user_ini_filename)) {
934 return SUCCESS;
935 }
936
937 /* PATH_TRANSLATED should be defined at this stage */
938 ctx->path = SG(request_info).path_translated;
939 if (!ctx->path || !*ctx->path) {
940 return FAILURE;
941 }
942
943 ctx->doc_root = sapi_lsapi_getenv("DOCUMENT_ROOT", 0);
944 DEBUG_MESSAGE("doc_root: %s", ctx->doc_root);
945
946 if (*fn_next) {
947 rc = (*fn_next)(ctx, fn_next + 1);
948 }
949
950 return rc;
951 }
952
lsapi_activate_user_ini_mk_path(_lsapi_activate_user_ini_ctx * ctx,void * next)953 static int lsapi_activate_user_ini_mk_path(_lsapi_activate_user_ini_ctx *ctx,
954 void* next)
955 {
956 char *path;
957 int rc = SUCCESS;
958 fn_activate_user_ini_chain_t *fn_next = next;
959
960 /* Extract dir name from path_translated * and store it in 'path' */
961 ctx->path_len = strlen(ctx->path);
962 path = ctx->path = estrndup(SG(request_info).path_translated, ctx->path_len);
963 ctx->path_len = zend_dirname(path, ctx->path_len);
964
965 if (*fn_next) {
966 rc = (*fn_next)(ctx, fn_next + 1);
967 }
968
969 efree(path);
970 return rc;
971 }
972
lsapi_activate_user_ini_mk_realpath(_lsapi_activate_user_ini_ctx * ctx,void * next)973 static int lsapi_activate_user_ini_mk_realpath(_lsapi_activate_user_ini_ctx *ctx,
974 void* next)
975 {
976 char *real_path;
977 int rc = SUCCESS;
978 fn_activate_user_ini_chain_t *fn_next = next;
979
980 if (!IS_ABSOLUTE_PATH(ctx->path, ctx->path_len)) {
981 real_path = tsrm_realpath(ctx->path, NULL);
982 if (!real_path) {
983 return SUCCESS;
984 }
985 ctx->path = real_path;
986 ctx->path_len = strlen(ctx->path);
987 } else {
988 real_path = NULL;
989 }
990
991 if (*fn_next) {
992 rc = (*fn_next)(ctx, fn_next + 1);
993 }
994
995 if (real_path)
996 efree(real_path);
997 return rc;
998 }
999
lsapi_activate_user_ini_mk_user_config(_lsapi_activate_user_ini_ctx * ctx,void * next)1000 static int lsapi_activate_user_ini_mk_user_config(_lsapi_activate_user_ini_ctx *ctx,
1001 void* next)
1002 {
1003 fn_activate_user_ini_chain_t *fn_next = next;
1004
1005 /* Find cached config entry: If not found, create one */
1006 ctx->entry = zend_hash_str_find_ptr(&user_config_cache, ctx->path, ctx->path_len);
1007
1008 if (!ctx->entry)
1009 {
1010 ctx->entry = pemalloc(sizeof(user_config_cache_entry), 1);
1011 ctx->entry->expires = 0;
1012 zend_hash_init(&ctx->entry->user_config, 0, NULL,
1013 config_zval_dtor, 1);
1014 zend_hash_str_update_ptr(&user_config_cache, ctx->path,
1015 ctx->path_len, ctx->entry);
1016 }
1017
1018 if (*fn_next) {
1019 return (*fn_next)(ctx, fn_next + 1);
1020 } else {
1021 return SUCCESS;
1022 }
1023 }
1024
walk_down_the_path_callback(char * begin,char * end,void * data)1025 static void walk_down_the_path_callback(char* begin,
1026 char* end,
1027 void* data)
1028 {
1029 _lsapi_activate_user_ini_ctx *ctx = data;
1030 char tmp = end[0];
1031 end[0] = 0;
1032 php_parse_user_ini_file(begin, PG(user_ini_filename), &ctx->entry->user_config);
1033 end[0] = tmp;
1034 }
1035
lsapi_activate_user_ini_walk_down_the_path(_lsapi_activate_user_ini_ctx * ctx,void * next)1036 static int lsapi_activate_user_ini_walk_down_the_path(_lsapi_activate_user_ini_ctx *ctx,
1037 void* next)
1038 {
1039 time_t request_time = sapi_get_request_time();
1040 uint32_t docroot_len;
1041 int rc = SUCCESS;
1042 fn_activate_user_ini_chain_t *fn_next = next;
1043
1044 if (!ctx->entry->expires || request_time > ctx->entry->expires)
1045 {
1046 docroot_len = ctx->doc_root && ctx->doc_root[0]
1047 ? pathlen_without_trailing_slash(ctx->doc_root)
1048 : 0;
1049
1050 int is_outside_of_docroot = !docroot_len ||
1051 ctx->path_len < docroot_len ||
1052 strncmp(ctx->path, ctx->doc_root, docroot_len) != 0;
1053
1054 if (is_outside_of_docroot) {
1055 php_parse_user_ini_file(ctx->path, PG(user_ini_filename),
1056 &ctx->entry->user_config);
1057 } else {
1058 walk_down_the_path(ctx->doc_root, ctx->path,
1059 &walk_down_the_path_callback, ctx);
1060 }
1061
1062 ctx->entry->expires = request_time + PG(user_ini_cache_ttl);
1063 }
1064
1065 if (*fn_next) {
1066 rc = (*fn_next)(ctx, fn_next + 1);
1067 }
1068
1069 return rc;
1070 }
1071
lsapi_activate_user_ini_finally(_lsapi_activate_user_ini_ctx * ctx,void * next)1072 static int lsapi_activate_user_ini_finally(_lsapi_activate_user_ini_ctx *ctx,
1073 void* next)
1074 {
1075 int rc = SUCCESS;
1076 fn_activate_user_ini_chain_t *fn_next = next;
1077
1078 php_ini_activate_config(&ctx->entry->user_config, PHP_INI_PERDIR,
1079 PHP_INI_STAGE_HTACCESS);
1080
1081 if (*fn_next) {
1082 rc = (*fn_next)(ctx, fn_next + 1);
1083 }
1084
1085 return rc;
1086 }
1087
lsapi_activate_user_ini(void)1088 static int lsapi_activate_user_ini( void )
1089 {
1090 _lsapi_activate_user_ini_ctx ctx;
1091 /**
1092 * The reason to have this function list stacked
1093 * is each function now can define a scoped destructor.
1094 *
1095 * Passing control via function pointer is a sign of low coupling,
1096 * which means dependencies between these functions are to be
1097 * controlled from a single place
1098 * (here below, by modifying this function list order)
1099 */
1100 static const fn_activate_user_ini_chain_t fn_chain[] = {
1101 &lsapi_activate_user_ini_basic_checks,
1102 &lsapi_activate_user_ini_mk_path,
1103 &lsapi_activate_user_ini_mk_realpath,
1104 &lsapi_activate_user_ini_mk_user_config,
1105 &lsapi_activate_user_ini_walk_down_the_path,
1106 &lsapi_activate_user_ini_finally,
1107 NULL
1108 };
1109
1110 return fn_chain[0](&ctx, (fn_activate_user_ini_chain_t*)(fn_chain + 1));
1111 }
1112
1113
override_ini()1114 static void override_ini()
1115 {
1116
1117 LSAPI_ForeachSpecialEnv( alter_ini, NULL );
1118
1119 }
1120
1121
processReq(void)1122 static int processReq(void)
1123 {
1124 int ret = 0;
1125 zend_first_try {
1126
1127 /* avoid server_context==NULL checks */
1128 SG(server_context) = (void *) 1;
1129
1130 engine = 1;
1131 override_ini();
1132
1133 if ( engine ) {
1134 init_request_info();
1135
1136 if ( lsapi_module_main( source_highlight ) == -1 ) {
1137 ret = -1;
1138 }
1139 } else {
1140 LSAPI_AppendRespHeader( "status: 403", 11 );
1141 LSAPI_AppendRespHeader( "content-type: text/html", 23 );
1142 LSAPI_Write( "Forbidden: PHP engine is disable.\n", 34 );
1143 }
1144 } zend_end_try();
1145 return ret;
1146 }
1147
cli_usage(void)1148 static void cli_usage(void)
1149 {
1150 static const char * usage =
1151 "Usage: php\n"
1152 " php -[b|c|n|h|i|q|s|v|?] [<file>] [args...]\n"
1153 " Run in LSAPI mode, only '-b', '-s' and '-c' are effective\n"
1154 " Run in Command Line Interpreter mode when parameters are specified\n"
1155 "\n"
1156 " -b <address:port>|<port> Bind Path for external LSAPI Server mode\n"
1157 " -c <path>|<file> Look for php.ini file in this directory\n"
1158 " -n No php.ini file will be used\n"
1159 " -h This help\n"
1160 " -i PHP information\n"
1161 " -l Syntax check\n"
1162 " -q Quiet-mode. Suppress HTTP Header output.\n"
1163 " -s Display colour syntax highlighted source.\n"
1164 " -v Version number\n"
1165 " -? This help\n"
1166 "\n"
1167 " args... Arguments passed to script.\n";
1168 php_output_startup();
1169 php_output_activate();
1170 php_printf( "%s", usage );
1171 #ifdef PHP_OUTPUT_NEWAPI
1172 php_output_end_all();
1173 #else
1174 php_end_ob_buffers(1);
1175 #endif
1176 }
1177
parse_opt(int argc,char * argv[],int * climode,char ** php_ini_path,char ** php_bind)1178 static int parse_opt( int argc, char * argv[], int *climode,
1179 char **php_ini_path, char ** php_bind )
1180 {
1181 char ** p = &argv[1];
1182 char ** argend= &argv[argc];
1183 int c;
1184 while (( p < argend )&&(**p == '-' )) {
1185 c = *((*p)+1);
1186 ++p;
1187 switch( c ) {
1188 case 'b':
1189 if ( p >= argend ) {
1190 fprintf( stderr, "TCP or socket address must be specified following '-b' option.\n");
1191 return -1;
1192 }
1193 *php_bind = strdup(*p++);
1194 break;
1195
1196 case 'c':
1197 if ( p >= argend ) {
1198 fprintf( stderr, "<path> or <file> must be specified following '-c' option.\n");
1199
1200 return -1;
1201 }
1202 *php_ini_path = strdup( *p++ );
1203 break;
1204 case 's':
1205 source_highlight = 1;
1206 break;
1207 case 'n':
1208 ignore_php_ini = 1;
1209 break;
1210 case '?':
1211 if ( *((*(p-1))+2) == 's' )
1212 exit( 99 );
1213 case 'h':
1214 case 'i':
1215 case 'l':
1216 case 'q':
1217 case 'v':
1218 default:
1219 *climode = 1;
1220 break;
1221 }
1222 }
1223 if ( p - argv < argc ) {
1224 *climode = 1;
1225 }
1226 return 0;
1227 }
1228
cli_main(int argc,char * argv[])1229 static int cli_main( int argc, char * argv[] )
1230 {
1231
1232 static const char * ini_defaults[] = {
1233 "report_zend_debug", "0",
1234 "display_errors", "1",
1235 "register_argc_argv", "1",
1236 "html_errors", "0",
1237 "implicit_flush", "1",
1238 "output_buffering", "0",
1239 "max_execution_time", "0",
1240 "max_input_time", "-1",
1241 NULL
1242 };
1243
1244 const char ** ini;
1245 char ** p = &argv[1];
1246 char ** argend= &argv[argc];
1247 int ret = -1;
1248 int c;
1249 zend_string *psKey;
1250 lsapi_mode = 0; /* enter CLI mode */
1251
1252 zend_first_try {
1253 SG(server_context) = (void *) 1;
1254
1255 zend_uv.html_errors = 0; /* tell the engine we're in non-html mode */
1256 CG(in_compilation) = 0; /* not initialized but needed for several options */
1257 SG(options) |= SAPI_OPTION_NO_CHDIR;
1258
1259 #if PHP_MAJOR_VERSION < 7
1260 EG(uninitialized_zval_ptr) = NULL;
1261 #endif
1262 for( ini = ini_defaults; *ini; ini+=2 ) {
1263 psKey = zend_string_init(*ini, strlen( *ini ), 1);
1264 zend_alter_ini_entry_chars(psKey,
1265 (char *)*(ini+1), strlen( *(ini+1) ),
1266 PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
1267 zend_string_release_ex(psKey, 1);
1268 }
1269
1270 while (( p < argend )&&(**p == '-' )) {
1271 c = *((*p)+1);
1272 ++p;
1273 switch( c ) {
1274 case 'q':
1275 break;
1276 case 'i':
1277 if (php_request_startup() != FAILURE) {
1278 php_print_info(0xFFFFFFFF);
1279 #ifdef PHP_OUTPUT_NEWAPI
1280 php_output_end_all();
1281 #else
1282 php_end_ob_buffers(1);
1283 #endif
1284 php_request_shutdown( NULL );
1285 ret = 0;
1286 }
1287 break;
1288 case 'v':
1289 if (php_request_startup() != FAILURE) {
1290 #if ZEND_DEBUG
1291 php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
1292 #else
1293 php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
1294 #endif
1295 #ifdef PHP_OUTPUT_NEWAPI
1296 php_output_end_all();
1297 #else
1298 php_end_ob_buffers(1);
1299 #endif
1300 php_request_shutdown( NULL );
1301 ret = 0;
1302 }
1303 break;
1304 case 'c':
1305 ++p;
1306 /* fall through */
1307 case 's':
1308 break;
1309 case 'l':
1310 source_highlight = 2;
1311 break;
1312 case 'h':
1313 case '?':
1314 default:
1315 cli_usage();
1316 ret = 0;
1317 break;
1318
1319 }
1320 }
1321 if ( ret == -1 ) {
1322 if ( *p ) {
1323 zend_file_handle file_handle;
1324 zend_stream_init_fp(&file_handle, VCWD_FOPEN(*p, "rb"), NULL);
1325
1326 if ( file_handle.handle.fp ) {
1327 script_filename = *p;
1328 php_self = *p;
1329
1330 SG(request_info).path_translated = estrdup(*p);
1331 SG(request_info).argc = argc - (p - argv);
1332 SG(request_info).argv = p;
1333
1334 if (php_request_startup() == FAILURE ) {
1335 fclose( file_handle.handle.fp );
1336 ret = 2;
1337 } else {
1338 if (source_highlight == 1) {
1339 zend_syntax_highlighter_ini syntax_highlighter_ini;
1340
1341 php_get_highlight_struct(&syntax_highlighter_ini);
1342 highlight_file(SG(request_info).path_translated, &syntax_highlighter_ini);
1343 } else if (source_highlight == 2) {
1344 file_handle.filename = *p;
1345 file_handle.free_filename = 0;
1346 file_handle.opened_path = NULL;
1347 ret = php_lint_script(&file_handle);
1348 if (ret==SUCCESS) {
1349 zend_printf("No syntax errors detected in %s\n", file_handle.filename);
1350 } else {
1351 zend_printf("Errors parsing %s\n", file_handle.filename);
1352 }
1353
1354 } else {
1355 file_handle.filename = *p;
1356 file_handle.free_filename = 0;
1357 file_handle.opened_path = NULL;
1358
1359 php_execute_script(&file_handle);
1360 ret = EG(exit_status);
1361 }
1362
1363 php_request_shutdown( NULL );
1364 }
1365 } else {
1366 php_printf("Could not open input file: %s.\n", *p);
1367 }
1368 } else {
1369 cli_usage();
1370 }
1371 }
1372
1373 }zend_end_try();
1374
1375 php_module_shutdown();
1376
1377 #ifdef ZTS
1378 tsrm_shutdown();
1379 #endif
1380 return ret;
1381 }
1382
1383 static int s_stop;
litespeed_cleanup(int signal)1384 void litespeed_cleanup(int signal)
1385 {
1386 s_stop = signal;
1387 }
1388
1389
start_children(int children)1390 void start_children( int children )
1391 {
1392 struct sigaction act, old_term, old_quit, old_int, old_usr1;
1393 int running = 0;
1394 int status;
1395 pid_t pid;
1396
1397 /* Create a process group */
1398 setsid();
1399
1400 /* Set up handler to kill children upon exit */
1401 sigemptyset(&act.sa_mask);
1402 act.sa_flags = 0;
1403 act.sa_handler = litespeed_cleanup;
1404 if( sigaction( SIGTERM, &act, &old_term ) ||
1405 sigaction( SIGINT, &act, &old_int ) ||
1406 sigaction( SIGUSR1, &act, &old_usr1 ) ||
1407 sigaction( SIGQUIT, &act, &old_quit )) {
1408 perror( "Can't set signals" );
1409 exit( 1 );
1410 }
1411 s_stop = 0;
1412 while( 1 ) {
1413 while((!s_stop )&&( running < children )) {
1414 pid = fork();
1415 switch( pid ) {
1416 case 0: /* children process */
1417
1418 /* don't catch our signals */
1419 sigaction( SIGTERM, &old_term, 0 );
1420 sigaction( SIGQUIT, &old_quit, 0 );
1421 sigaction( SIGINT, &old_int, 0 );
1422 sigaction( SIGUSR1, &old_usr1, 0 );
1423 return ;
1424 case -1:
1425 perror( "php (pre-forking)" );
1426 exit( 1 );
1427 break;
1428 default: /* parent process */
1429 running++;
1430 break;
1431 }
1432 }
1433 if ( s_stop ) {
1434 break;
1435 }
1436 pid = wait( &status );
1437 running--;
1438 }
1439 kill( -getpgrp(), SIGUSR1 );
1440 exit( 0 );
1441 }
1442
setArgv0(int argc,char * argv[])1443 void setArgv0( int argc, char * argv[] )
1444 {
1445 char * p;
1446 int i;
1447 argv0 = argv[0] + strlen( argv[0] );
1448 p = argv0;
1449 while(( p > argv[0] )&&( p[-1] != '/'))
1450 --p;
1451 if ( p > argv[0] )
1452 {
1453 memmove( argv[0], p, argv0 - p );
1454 memset( argv[0] + ( argv0 - p ), 0, p - argv[0] );
1455 argv0 = argv[0] + (argv0 - p);
1456 }
1457 for( i = 1; i < argc; ++i )
1458 {
1459 memset( argv[i], 0, strlen( argv[i] ) );
1460 }
1461 }
1462
1463 #include <fcntl.h>
main(int argc,char * argv[])1464 int main( int argc, char * argv[] )
1465 {
1466 int ret;
1467 int bindFd;
1468
1469 char * php_ini_path = NULL;
1470 char * php_bind = NULL;
1471 int n;
1472 int climode = 0;
1473 struct timeval tv_req_begin;
1474 struct timeval tv_req_end;
1475 int slow_script_msec = 0;
1476 char time_buf[40];
1477
1478
1479 #if defined(SIGPIPE) && defined(SIG_IGN)
1480 signal(SIGPIPE, SIG_IGN);
1481 #endif
1482
1483 #ifdef ZTS
1484 php_tsrm_startup();
1485 #endif
1486
1487 #if PHP_MAJOR_VERSION >= 7
1488 #if defined(ZEND_SIGNALS) || PHP_MINOR_VERSION > 0
1489 zend_signal_startup();
1490 #endif
1491 #endif
1492
1493 if (argc > 1 ) {
1494 if ( parse_opt( argc, argv, &climode,
1495 &php_ini_path, &php_bind ) == -1 ) {
1496 return 1;
1497 }
1498 }
1499 if ( climode ) {
1500 lsapi_sapi_module.phpinfo_as_text = 1;
1501 } else {
1502 setArgv0(argc, argv );
1503 }
1504
1505 sapi_startup(&lsapi_sapi_module);
1506
1507 #ifdef ZTS
1508 compiler_globals = ts_resource(compiler_globals_id);
1509 executor_globals = ts_resource(executor_globals_id);
1510 core_globals = ts_resource(core_globals_id);
1511 sapi_globals = ts_resource(sapi_globals_id);
1512
1513 SG(request_info).path_translated = NULL;
1514 #endif
1515
1516 lsapi_sapi_module.executable_location = argv[0];
1517
1518 /* Initialize from environment variables before processing command-line
1519 * options: the latter override the former.
1520 */
1521 init_sapi_from_env(&lsapi_sapi_module);
1522
1523 if ( ignore_php_ini )
1524 lsapi_sapi_module.php_ini_ignore = 1;
1525
1526 if ( php_ini_path ) {
1527 lsapi_sapi_module.php_ini_path_override = php_ini_path;
1528 }
1529
1530
1531 lsapi_sapi_module.ini_defaults = sapi_lsapi_ini_defaults;
1532
1533 if (php_module_startup(&lsapi_sapi_module, &litespeed_module_entry, 1) == FAILURE) {
1534 #ifdef ZTS
1535 tsrm_shutdown();
1536 #endif
1537 return FAILURE;
1538 }
1539
1540 if ( climode ) {
1541 return cli_main(argc, argv);
1542 }
1543
1544 if ( php_bind ) {
1545 bindFd = LSAPI_CreateListenSock( php_bind, 10 );
1546 if ( bindFd == -1 ) {
1547 fprintf( stderr,
1548 "Failed to bind socket [%s]: %s\n", php_bind, strerror( errno ) );
1549 exit( 2 );
1550 }
1551 if ( bindFd != 0 ) {
1552 dup2( bindFd, 0 );
1553 close( bindFd );
1554 }
1555 }
1556
1557 LSAPI_Init();
1558
1559 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
1560 int is_criu = LSCRIU_Init(); // Must be called before the regular init as it unsets the parameters.
1561 #endif
1562
1563 LSAPI_Init_Env_Parameters( NULL );
1564 lsapi_mode = 1;
1565
1566 slow_script_msec = LSAPI_Get_Slow_Req_Msecs();
1567
1568 if ( php_bind ) {
1569 LSAPI_No_Check_ppid();
1570 free( php_bind );
1571 php_bind = NULL;
1572 }
1573
1574 int result;
1575
1576 if (do_clean_shutdown)
1577 atexit(lsapi_atexit);
1578
1579 while( ( result = LSAPI_Prefork_Accept_r( &g_req )) >= 0 ) {
1580 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
1581 if (is_criu && !result) {
1582 LSCRIU_inc_req_procssed();
1583 }
1584 #endif
1585 if ( slow_script_msec ) {
1586 gettimeofday( &tv_req_begin, NULL );
1587 }
1588 ret = processReq();
1589 if ( slow_script_msec ) {
1590 gettimeofday( &tv_req_end, NULL );
1591 n = ((long) tv_req_end.tv_sec - tv_req_begin.tv_sec ) * 1000
1592 + (tv_req_end.tv_usec - tv_req_begin.tv_usec) / 1000;
1593 if ( n > slow_script_msec )
1594 {
1595 strftime( time_buf, 30, "%d/%b/%Y:%H:%M:%S", localtime( &tv_req_end.tv_sec ) );
1596 fprintf( stderr, "[%s] Slow PHP script: %d ms\n URL: %s %s\n Query String: %s\n Script: %s\n",
1597 time_buf, n, LSAPI_GetRequestMethod(),
1598 LSAPI_GetScriptName(), LSAPI_GetQueryString(),
1599 LSAPI_GetScriptFileName() );
1600
1601 }
1602 }
1603 LSAPI_Finish();
1604 if ( ret ) {
1605 break;
1606 }
1607 }
1608
1609 php_module_shutdown();
1610
1611 #ifdef ZTS
1612 tsrm_shutdown();
1613 #endif
1614 return ret;
1615 }
1616
1617
1618 /* LiteSpeed PHP module starts here */
1619
1620 /* {{{ arginfo */
1621 ZEND_BEGIN_ARG_INFO(arginfo_litespeed__void, 0)
1622 ZEND_END_ARG_INFO()
1623 /* }}} */
1624
1625 PHP_FUNCTION(litespeed_request_headers);
1626 PHP_FUNCTION(litespeed_response_headers);
1627 PHP_FUNCTION(apache_get_modules);
1628 PHP_FUNCTION(litespeed_finish_request);
1629
1630 PHP_MINFO_FUNCTION(litespeed);
1631
1632 static const zend_function_entry litespeed_functions[] = {
1633 PHP_FE(litespeed_request_headers, arginfo_litespeed__void)
1634 PHP_FE(litespeed_response_headers, arginfo_litespeed__void)
1635 PHP_FE(apache_get_modules, arginfo_litespeed__void)
1636 PHP_FE(litespeed_finish_request, arginfo_litespeed__void)
1637 PHP_FALIAS(getallheaders, litespeed_request_headers, arginfo_litespeed__void)
1638 PHP_FALIAS(apache_request_headers, litespeed_request_headers, arginfo_litespeed__void)
1639 PHP_FALIAS(apache_response_headers, litespeed_response_headers, arginfo_litespeed__void)
1640 {NULL, NULL, NULL}
1641 };
1642
PHP_MINIT_FUNCTION(litespeed)1643 static PHP_MINIT_FUNCTION(litespeed)
1644 {
1645 user_config_cache_init();
1646
1647 const char *p = getenv("LSPHP_ENABLE_USER_INI");
1648 if (p && 0 == strcasecmp(p, "on"))
1649 parse_user_ini = 1;
1650
1651 p = getenv("LSAPI_CLEAN_SHUTDOWN");
1652 if (p) {
1653 if (*p == '1' || 0 == strcasecmp(p, "on"))
1654 do_clean_shutdown = 1;
1655 else if (*p == '0' || 0 == strcasecmp(p, "off"))
1656 do_clean_shutdown = 0;
1657 }
1658 /*
1659 * mod_lsapi always sets this env var,
1660 * so we can detect mod_lsapi mode with its presense.
1661 */
1662 mod_lsapi_mode = ( getenv("LSAPI_DISABLE_CPAN_BEHAV") != NULL );
1663
1664 /* REGISTER_INI_ENTRIES(); */
1665 return SUCCESS;
1666 }
1667
1668
PHP_MSHUTDOWN_FUNCTION(litespeed)1669 static PHP_MSHUTDOWN_FUNCTION(litespeed)
1670 {
1671 zend_hash_destroy(&user_config_cache);
1672
1673 /* UNREGISTER_INI_ENTRIES(); */
1674 return SUCCESS;
1675 }
1676
1677 zend_module_entry litespeed_module_entry = {
1678 STANDARD_MODULE_HEADER,
1679 "litespeed",
1680 litespeed_functions,
1681 PHP_MINIT(litespeed),
1682 PHP_MSHUTDOWN(litespeed),
1683 NULL,
1684 NULL,
1685 NULL,
1686 PHP_VERSION,
1687 STANDARD_MODULE_PROPERTIES
1688 };
1689
add_associate_array(const char * pKey,int keyLen,const char * pValue,int valLen,void * arg)1690 static int add_associate_array( const char * pKey, int keyLen, const char * pValue, int valLen,
1691 void * arg )
1692 {
1693 add_assoc_string_ex((zval *)arg, (char *)pKey, keyLen, (char *)pValue);
1694 return 1;
1695 }
1696
1697
1698 /* {{{ proto array litespeed_request_headers(void)
1699 Fetch all HTTP request headers */
PHP_FUNCTION(litespeed_request_headers)1700 PHP_FUNCTION(litespeed_request_headers)
1701 {
1702 /* TODO: */
1703 if (ZEND_NUM_ARGS() > 0) {
1704 WRONG_PARAM_COUNT;
1705 }
1706 array_init(return_value);
1707
1708 LSAPI_ForeachOrgHeader( add_associate_array, return_value );
1709
1710 }
1711 /* }}} */
1712
1713
1714
1715 /* {{{ proto array litespeed_response_headers(void)
1716 Fetch all HTTP response headers */
PHP_FUNCTION(litespeed_response_headers)1717 PHP_FUNCTION(litespeed_response_headers)
1718 {
1719 sapi_header_struct *h;
1720 zend_llist_position pos;
1721 char * p;
1722 int len;
1723 char headerBuf[SAPI_LSAPI_MAX_HEADER_LENGTH];
1724
1725 if (ZEND_NUM_ARGS() > 0) {
1726 WRONG_PARAM_COUNT;
1727 }
1728
1729 if (!&SG(sapi_headers).headers) {
1730 RETURN_FALSE;
1731 }
1732 array_init(return_value);
1733
1734 h = zend_llist_get_first_ex(&SG(sapi_headers).headers, &pos);
1735 while (h) {
1736 if ( h->header_len > 0 ) {
1737 p = strchr( h->header, ':' );
1738 len = p - h->header;
1739 if (p && len > 0 && len < LSAPI_RESP_HTTP_HEADER_MAX) {
1740 memmove( headerBuf, h->header, len );
1741 while( len > 0 && (isspace( headerBuf[len-1])) ) {
1742 --len;
1743 }
1744 headerBuf[len] = 0;
1745 if ( len ) {
1746 while( isspace(*++p));
1747 add_assoc_string_ex(return_value, headerBuf, len, p);
1748 }
1749 }
1750 }
1751 h = zend_llist_get_next_ex(&SG(sapi_headers).headers, &pos);
1752 }
1753 }
1754
1755 /* }}} */
1756
1757
1758 /* {{{ proto array apache_get_modules(void)
1759 Fetch all loaded module names */
PHP_FUNCTION(apache_get_modules)1760 PHP_FUNCTION(apache_get_modules)
1761 {
1762 static const char * mod_names[] =
1763 {
1764 "mod_rewrite", "mod_mime", "mod_headers", "mod_expires", "mod_auth_basic", NULL
1765 };
1766 const char **name = mod_names;
1767 /* TODO: */
1768 if (ZEND_NUM_ARGS() > 0) {
1769 WRONG_PARAM_COUNT;
1770 }
1771 array_init(return_value);
1772 while( *name )
1773 {
1774 add_next_index_string(return_value, *name);
1775 ++name;
1776 }
1777 }
1778 /* }}} */
1779
1780
1781 /* {{{ proto array litespeed_finish_request(void)
1782 Flushes all response data to the client */
PHP_FUNCTION(litespeed_finish_request)1783 PHP_FUNCTION(litespeed_finish_request)
1784 {
1785 if (ZEND_NUM_ARGS() > 0) {
1786 WRONG_PARAM_COUNT;
1787 }
1788
1789 php_output_end_all();
1790 php_header();
1791
1792 if (LSAPI_End_Response() != -1) {
1793 RETURN_TRUE;
1794 }
1795 RETURN_FALSE;
1796 }
1797 /* }}} */
1798