1 /*
2    +----------------------------------------------------------------------+
3    | Zend OPcache                                                         |
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 through the world-wide-web at the following url:           |
10    | https://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    | Authors: Andi Gutmans <andi@php.net>                                 |
16    |          Zeev Suraski <zeev@php.net>                                 |
17    |          Stanislav Malyshev <stas@zend.com>                          |
18    |          Dmitry Stogov <dmitry@php.net>                              |
19    +----------------------------------------------------------------------+
20 */
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <time.h>
26 #ifdef ZEND_WIN32
27 # include <process.h>
28 #endif
29 #include "ZendAccelerator.h"
30 
zend_accel_error_va_args(int type,const char * format,va_list args)31 static void zend_accel_error_va_args(int type, const char *format, va_list args)
32 {
33 	time_t timestamp;
34 	char *time_string;
35 	FILE * fLog = NULL;
36 
37 	if (type <= ZCG(accel_directives).log_verbosity_level) {
38 
39 	timestamp = time(NULL);
40 	time_string = asctime(localtime(&timestamp));
41 	time_string[24] = 0;
42 
43 	if (!ZCG(accel_directives).error_log ||
44 		!*ZCG(accel_directives).error_log ||
45 		strcmp(ZCG(accel_directives).error_log, "stderr") == 0) {
46 
47 		fLog = stderr;
48 	} else {
49 		fLog = fopen(ZCG(accel_directives).error_log, "a");
50 		if (!fLog) {
51 			fLog = stderr;
52 		}
53 	}
54 
55 #ifdef ZTS
56 		fprintf(fLog, "%s (" ZEND_ULONG_FMT "): ", time_string, (zend_ulong)tsrm_thread_id());
57 #else
58 		fprintf(fLog, "%s (%d): ", time_string, getpid());
59 #endif
60 
61 		switch (type) {
62 			case ACCEL_LOG_FATAL:
63 				fprintf(fLog, "Fatal Error ");
64 				break;
65 			case ACCEL_LOG_ERROR:
66 				fprintf(fLog, "Error ");
67 				break;
68 			case ACCEL_LOG_WARNING:
69 				fprintf(fLog, "Warning ");
70 				break;
71 			case ACCEL_LOG_INFO:
72 				fprintf(fLog, "Message ");
73 				break;
74 			case ACCEL_LOG_DEBUG:
75 				fprintf(fLog, "Debug ");
76 				break;
77 		}
78 
79 		vfprintf(fLog, format, args);
80 		fprintf(fLog, "\n");
81 
82 		fflush(fLog);
83 		if (fLog != stderr) {
84 			fclose(fLog);
85 		}
86 	}
87 	/* perform error handling even without logging the error */
88 	switch (type) {
89 		case ACCEL_LOG_ERROR:
90 			zend_bailout();
91 			break;
92 		case ACCEL_LOG_FATAL:
93 			exit(-2);
94 			break;
95 	}
96 
97 }
98 
zend_accel_error(int type,const char * format,...)99 void zend_accel_error(int type, const char *format, ...)
100 {
101 	va_list args;
102 	va_start(args, format);
103 	zend_accel_error_va_args(type, format, args);
104 	va_end(args);
105 }
106 
zend_accel_error_noreturn(int type,const char * format,...)107 ZEND_NORETURN void zend_accel_error_noreturn(int type, const char *format, ...)
108 {
109 	va_list args;
110 	va_start(args, format);
111 	ZEND_ASSERT(type == ACCEL_LOG_FATAL || type == ACCEL_LOG_ERROR);
112 	zend_accel_error_va_args(type, format, args);
113 	va_end(args);
114 	/* Should never reach this. */
115 	abort();
116 }
117