Repository navigation
Expand file tree
/
Copy pathemulate_php_cli.c
More file actions
235 lines (201 loc) · 6.93 KB
/
Copy pathemulate_php_cli.c
File metadata and controls
235 lines (201 loc) · 6.93 KB
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
#include "emulate_php_cli.h"
#include "frankenphp.h"
#include <SAPI.h>
#include <Zend/zend_alloc.h>
#include <Zend/zend_exceptions.h>
#include <Zend/zend_interfaces.h>
#include <Zend/zend_types.h>
#include <errno.h>
#include <ext/spl/spl_exceptions.h>
#include <ext/standard/head.h>
#include <inttypes.h>
#include <php.h>
#ifdef PHP_WIN32
#include <config.w32.h>
#else
#include <php_config.h>
#endif
#include <php_ini.h>
#include <php_main.h>
#include <php_network.h>
#include <php_output.h>
#include <php_variables.h>
#include <php_version.h>
#include <pthread.h>
#include <sapi/embed/php_embed.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef ZEND_WIN32
#include <unistd.h>
#endif
#if defined(__linux__)
#include <sys/prctl.h>
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
#include <pthread_np.h>
#endif
cli_exec_args_t *cli_args;
static int (*previous_embed_startup)(sapi_module_struct *sapi_module) = NULL;
static int cli_embed_startup(sapi_module_struct *sapi_module) {
sapi_module->executable_location = cli_args->argv[0];
return previous_embed_startup(sapi_module);
}
static void register_server_variable_filtered(const char *key, char **val,
size_t *val_len,
zval *track_vars_array) {
if (sapi_module.input_filter(PARSE_SERVER, key, val, *val_len, val_len)) {
php_register_variable_safe(key, *val, *val_len, track_vars_array);
}
}
static php_stream *cli_open_standard_stream(const char *path, const char *mode,
FILE *file) {
php_stream *stream = php_stream_open_wrapper(path, mode, 0, NULL);
php_socket_t fd;
/* PHP uses the process's stdin/stdout/stderr on the first open and duplicates
* them on later opens. Keep the originals open for the next CLI execution,
* but let PHP close the duplicates. */
if (stream &&
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void **)&fd, 0) ==
SUCCESS &&
fd == (php_socket_t)fileno(file)) {
stream->flags |= PHP_STREAM_FLAG_NO_CLOSE;
}
return stream;
}
/*
* CLI code is adapted from
* https://github.com/php/php-src/blob/master/sapi/cli/php_cli.c Copyright (c)
* The PHP Group Licensed under The PHP License Original uthors: Edin Kadribasic
* <edink@php.net>, Marcus Boerger <helly@php.net> and Johannes Schlueter
* <johannes@php.net> Parts based on CGI SAPI Module by Rasmus Lerdorf, Stig
* Bakken and Zeev Suraski
*/
static void cli_register_file_handles(void) /* {{{ */
{
php_stream *s_in, *s_out, *s_err;
zend_constant ic, oc, ec;
s_in = cli_open_standard_stream("php://stdin", "rb", stdin);
s_out = cli_open_standard_stream("php://stdout", "wb", stdout);
s_err = cli_open_standard_stream("php://stderr", "wb", stderr);
if (s_in == NULL || s_out == NULL || s_err == NULL) {
if (s_in)
php_stream_close(s_in);
if (s_out)
php_stream_close(s_out);
if (s_err)
php_stream_close(s_err);
return;
}
php_stream_to_zval(s_in, &ic.value);
php_stream_to_zval(s_out, &oc.value);
php_stream_to_zval(s_err, &ec.value);
ZEND_CONSTANT_SET_FLAGS(&ic, CONST_CS, 0);
ic.name = zend_string_init_interned("STDIN", sizeof("STDIN") - 1, 0);
zend_register_constant(&ic);
ZEND_CONSTANT_SET_FLAGS(&oc, CONST_CS, 0);
oc.name = zend_string_init_interned("STDOUT", sizeof("STDOUT") - 1, 0);
zend_register_constant(&oc);
ZEND_CONSTANT_SET_FLAGS(&ec, CONST_CS, 0);
ec.name = zend_string_init_interned("STDERR", sizeof("STDERR") - 1, 0);
zend_register_constant(&ec);
}
/* }}} */
static void sapi_cli_register_variables(zval *track_vars_array) /* {{{ */
{
size_t len = strlen(cli_args->script);
char *docroot = "";
/*
* In CGI mode, we consider the environment to be a part of the server
* variables
*/
php_import_environment_variables(track_vars_array);
/* Build the special-case PHP_SELF variable for the CLI version */
register_server_variable_filtered("PHP_SELF", &cli_args->script, &len,
track_vars_array);
register_server_variable_filtered("SCRIPT_NAME", &cli_args->script, &len,
track_vars_array);
/* filenames are empty for stdin */
register_server_variable_filtered("SCRIPT_FILENAME", &cli_args->script, &len,
track_vars_array);
register_server_variable_filtered("PATH_TRANSLATED", &cli_args->script, &len,
track_vars_array);
/* just make it available */
len = 0U;
register_server_variable_filtered("DOCUMENT_ROOT", &docroot, &len,
track_vars_array);
}
/* }}} */
void *emulate_script_cli(void *arg) {
void *exit_status;
cli_exec_args_t *args = arg;
cli_args = args;
/* Parse argv to detect -r (eval mode) and find the script path */
bool eval = false;
char *script = NULL;
int script_index = 0;
for (int i = 1; i < args->argc; i++) {
if (strcmp(args->argv[i], "-r") == 0 && i + 1 < args->argc) {
eval = true;
script = args->argv[i + 1];
script_index = i + 1;
break;
} else if (args->argv[i][0] != '-') {
script = args->argv[i];
script_index = i;
break;
}
}
if (script == NULL) {
fprintf(stderr, "this functionality is not available in frankenphp php-cli "
"with a php version lower than 8.6\n");
return (void *)(intptr_t)1;
}
/* Update cli_args->script so sapi_cli_register_variables uses the right path
*/
cli_args->script = script;
/*
* Like the CLI SAPI's do_cli(): script path must become $argv[0].
* In -r mode the CLI SAPI reports "Standard input code" as $argv[0].
*/
int argc = args->argc - script_index;
char **argv = args->argv + script_index;
char *code = NULL;
if (eval) {
code = argv[0];
argv[0] = "Standard input code";
}
/*
* The SAPI name "cli" is hardcoded into too many programs... let's usurp it.
*/
php_embed_module.name = "cli";
php_embed_module.pretty_name = "PHP CLI embedded in FrankenPHP";
php_embed_module.register_server_variables = sapi_cli_register_variables;
/* the CLI SAPI prints phpinfo() as plain text, not as HTML */
php_embed_module.phpinfo_as_text = 1;
if (php_embed_module.startup != cli_embed_startup) {
previous_embed_startup = php_embed_module.startup;
php_embed_module.startup = cli_embed_startup;
}
php_embed_init(argc, argv);
cli_register_file_handles();
zend_first_try {
if (eval) {
/* evaluate script as literal PHP code (php-cli -r "...") */
zend_eval_string_ex(script, NULL, "Command line code", 1);
} else {
zend_file_handle file_handle;
zend_stream_init_filename(&file_handle, script);
CG(skip_shebang) = 1;
php_execute_script(&file_handle);
}
}
zend_end_try();
exit_status = (void *)(intptr_t)EG(exit_status);
php_embed_shutdown();
if (eval) {
argv[0] = code;
}
return exit_status;
}