23
23
#include <fcntl.h>
24
24
#endif
25
25
26
+ #ifndef HAVE_EMBED_MAIN
26
27
static const char HARDCODED_INI [] =
27
28
"html_errors=0\n"
28
29
"register_argc_argv=1\n"
@@ -114,6 +115,8 @@ static void php_embed_log_message(const char *message, int syslog_type_int)
114
115
static void php_embed_register_variables (zval * track_vars_array )
115
116
{
116
117
php_import_environment_variables (track_vars_array );
118
+
119
+ php_register_variable ("PHP_SELF" , "-" , track_vars_array );
117
120
}
118
121
119
122
/* Module initialization (MINIT) */
@@ -244,7 +247,6 @@ EMBED_SAPI_API int php_embed_init(int argc, char **argv)
244
247
245
248
SG (headers_sent ) = 1 ;
246
249
SG (request_info ).no_headers = 1 ;
247
- php_register_variable ("PHP_SELF" , "-" , NULL );
248
250
249
251
return SUCCESS ;
250
252
}
@@ -264,3 +266,151 @@ EMBED_SAPI_API void php_embed_shutdown(void)
264
266
tsrm_shutdown ();
265
267
#endif
266
268
}
269
+ #endif
270
+
271
+ #ifdef HAVE_EMBED_MAIN
272
+ # include <sys/mman.h>
273
+ # include "php_embed_main.h"
274
+
275
+ static char _php_embed_main_path_ [MAXPATHLEN ];
276
+
277
+ static php_stream * s_in_process = NULL ;
278
+
279
+ static int php_embed_main_streams (void ) {
280
+ php_stream * s_in , * s_out , * s_err ;
281
+ php_stream_context * sc_in = NULL , * sc_out = NULL , * sc_err = NULL ;
282
+ zend_constant ic , oc , ec ;
283
+
284
+ s_in = php_stream_open_wrapper_ex ("php://stdin" , "rb" , 0 , NULL , sc_in );
285
+ s_out = php_stream_open_wrapper_ex ("php://stdout" , "wb" , 0 , NULL , sc_out );
286
+ s_err = php_stream_open_wrapper_ex ("php://stderr" , "wb" , 0 , NULL , sc_err );
287
+
288
+ if (s_in ) s_in -> flags |= PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE ;
289
+ if (s_out ) s_out -> flags |= PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE ;
290
+ if (s_err ) s_err -> flags |= PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE ;
291
+
292
+ if (s_in == NULL || s_out == NULL || s_err == NULL ) {
293
+ if (s_in ) php_stream_close (s_in );
294
+ if (s_out ) php_stream_close (s_out );
295
+ if (s_err ) php_stream_close (s_err );
296
+ return FAILURE ;
297
+ }
298
+
299
+ s_in_process = s_in ;
300
+
301
+ php_stream_to_zval (s_in , & ic .value );
302
+ php_stream_to_zval (s_out , & oc .value );
303
+ php_stream_to_zval (s_err , & ec .value );
304
+
305
+ Z_CONSTANT_FLAGS (ic .value ) = 0 ;
306
+ ic .name = zend_string_init_interned ("STDIN" , sizeof ("STDIN" )- 1 , 0 );
307
+ zend_register_constant (& ic );
308
+
309
+ Z_CONSTANT_FLAGS (oc .value ) = 0 ;
310
+ oc .name = zend_string_init_interned ("STDOUT" , sizeof ("STDOUT" )- 1 , 0 );
311
+ zend_register_constant (& oc );
312
+
313
+ Z_CONSTANT_FLAGS (ec .value ) = 0 ;
314
+ ec .name = zend_string_init_interned ("STDERR" , sizeof ("STDERR" )- 1 , 0 );
315
+ zend_register_constant (& ec );
316
+
317
+ return SUCCESS ;
318
+ }
319
+
320
+ static void php_embed_main_variables (zval * track_vars_array )
321
+ {
322
+ php_import_environment_variables (track_vars_array );
323
+
324
+ php_register_variable ("PHP_SELF" ,
325
+ _php_embed_main_path_ , track_vars_array );
326
+ }
327
+
328
+ static int php_embed_main_enter (void ) {
329
+ int fd ;
330
+ snprintf (_php_embed_main_path_ ,
331
+ MAXPATHLEN , "/tmp/php_embed_main.XXXXXX" );
332
+ fd = mkstemp (_php_embed_main_path_ );
333
+ if (fd == FAILURE ) {
334
+ fprintf (stderr ,
335
+ "php_embed_main can not be initialized "
336
+ "at /tmp/php_embed_main.*: %d %s\n" ,
337
+ errno , strerror (errno ));
338
+ return FAILURE ;
339
+ }
340
+
341
+ if (write (fd , php_embed_main , php_embed_main_len ) == FAILURE ) {
342
+ fprintf (stderr ,
343
+ "php_embed_main can not be written "
344
+ "at %s: %d %s\n" ,
345
+ _php_embed_main_path_ , errno , strerror (errno ));
346
+ return FAILURE ;
347
+ }
348
+ lseek (fd , 0 , SEEK_SET );
349
+ close (fd );
350
+
351
+ #ifdef PHP_EMBED_LINK
352
+ if (symlink (_php_embed_main_path_ , PHP_EMBED_LINK ) != SUCCESS ) {
353
+ fprintf (stderr ,
354
+ "php_embed_main link can not be initialized at %s: %d %s\n" ,
355
+ PHP_EMBED_LINK ,
356
+ errno , strerror (errno ));
357
+ return FAILURE ;
358
+ }
359
+ strcpy (_php_embed_main_path_ , PHP_EMBED_LINK );
360
+ #endif
361
+
362
+ php_embed_module .register_server_variables = php_embed_main_variables ;
363
+
364
+ return SUCCESS ;
365
+ }
366
+
367
+ static int php_embed_main_execute (void ) {
368
+ int status = php_embed_main_streams ();
369
+
370
+ if (status != SUCCESS ) {
371
+ return 1 ;
372
+ }
373
+
374
+ zend_file_handle fh ;
375
+ zend_stream_init_filename (& fh ,
376
+ _php_embed_main_path_ );
377
+ if (!php_execute_script (& fh )) {
378
+ if (EG (exit_status ) == SUCCESS ) {
379
+ status = 1 ;
380
+ }
381
+ }
382
+ zend_destroy_file_handle (& fh );
383
+
384
+ if (status == FAILURE ) {
385
+ status = EG (exit_status );
386
+ }
387
+
388
+ return status ;
389
+ }
390
+
391
+ static void php_embed_main_leave (void ) {
392
+ unlink (_php_embed_main_path_ );
393
+ #ifdef PHP_EMBED_LINK
394
+ unlink (PHP_EMBED_LINK );
395
+ #endif
396
+ }
397
+
398
+ int main (int argc , char * argv [])
399
+ {
400
+ int status = FAILURE ;
401
+
402
+ if (php_embed_main_enter () != SUCCESS ) {
403
+ return 1 ;
404
+ }
405
+
406
+ PHP_EMBED_START_BLOCK (argc , argv )
407
+
408
+ status = php_embed_main_execute ();
409
+
410
+ PHP_EMBED_END_BLOCK ()
411
+
412
+ php_embed_main_leave ();
413
+
414
+ return status ;
415
+ }
416
+ #endif
0 commit comments