Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ab744a1

Browse files
committed
an approach to embedding main() into sapi/embed from php code
1 parent 1ac91f5 commit ab744a1

File tree

2 files changed

+216
-3
lines changed

2 files changed

+216
-3
lines changed

‎sapi/embed/config.m4

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
11
PHP_ARG_ENABLE([embed],,
22
[AS_HELP_STRING([[--enable-embed[=TYPE]]],
33
[Enable building of embedded SAPI library TYPE is either
4-
'shared' or 'static'. [TYPE=shared]])],
4+
'shared' or 'static' or 'main'. [TYPE=shared]])],
5+
[no],
6+
[no])
7+
8+
PHP_ARG_WITH([embed-main],,
9+
[AS_HELP_STRING([[--enable-embed-main[=FILE]]],
10+
[Enable building of embedded SAPI as a program with embedded main
11+
[TYPE=no]])],
12+
[no],
13+
[no])
14+
15+
PHP_ARG_WITH([embed-link],,
16+
[AS_HELP_STRING([[--enable-embed-link[=FILE]]],
17+
[Enable creation of symlink to embedded code
18+
[TYPE=no]])],
519
[no],
620
[no])
721

822
AC_MSG_CHECKING([for embedded SAPI library support])
923

10-
if test "$PHP_EMBED" != "no"; then
24+
if test "$PHP_EMBED" != "no" || test "$PHP_EMBED_MAIN" != "no"; then
25+
26+
if test "$PHP_EMBED" == "main"; then
27+
if test "$PHP_EMBED_MAIN" == "no"; then
28+
AC_MSG_ERROR([--with-embed-main is required for --enable-embed=main])
29+
fi
30+
elif test "$PHP_EMBED_MAIN" != "no"; then
31+
PHP_EMBED=main
32+
fi
33+
1134
AS_CASE([$PHP_EMBED],
1235
[yes|shared], [
1336
LIBPHP_CFLAGS="-shared"
@@ -22,6 +45,46 @@ if test "$PHP_EMBED" != "no"; then
2245
PHP_EMBED_TYPE=static
2346
INSTALL_IT="\$(mkinstalldirs) \$(INSTALL_ROOT)\$(orig_libdir); \$(INSTALL) -m 0644 $SAPI_STATIC \$(INSTALL_ROOT)\$(orig_libdir)"
2447
],
48+
[main], [
49+
AC_MSG_RESULT([main])
50+
AC_PATH_PROG([XXD], [xxd])
51+
if test ! -x "$XXD"; then
52+
AC_MSG_ERROR([xxd not found, required for --with-embed-main])
53+
fi
54+
AC_MSG_CHECKING([for access to $PHP_EMBED_MAIN])
55+
if test ! -f "$PHP_EMBED_MAIN"; then
56+
AC_MSG_ERROR([--with-embed-main argument "$PHP_EMBED_MAIN" not found])
57+
else
58+
AC_MSG_RESULT([ok])
59+
fi
60+
61+
AC_MSG_CHECKING([generating sapi/embed/php_embed_main.h])
62+
$XXD -i -n php_embed_main "$PHP_EMBED_MAIN" sapi/embed/php_embed_main.h
63+
if test $? -ne 0; then
64+
AC_MSG_ERROR([failed])
65+
else
66+
AC_MSG_RESULT([ok])
67+
fi
68+
69+
AC_MSG_CHECKING([proceeding to final configure of embed])
70+
LIBPHP_CFLAGS="-static"
71+
PHP_EMBED_TYPE=static
72+
INSTALL_IT="\
73+
\$(mkinstalldirs) \$(INSTALL_ROOT)\$(orig_libdir); \
74+
\$(INSTALL) -m 0644 \$(SAPI_STATIC) \$(INSTALL_ROOT)\$(orig_libdir); \
75+
\$(mkinstalldirs) \$(INSTALL_ROOT)\$(bindir); \
76+
\$(LIBTOOL) --tag=CC --mode=compile \$(CC) \
77+
\$(CFLAGS_CLEAN) \$(EXTRA_CFLAGS) \$(INCLUDES) -DHAVE_EMBED_MAIN \
78+
-c sapi/embed/php_embed.c -o sapi/embed/php_embed_main.lo; \
79+
\$(LIBTOOL) --tag=CC --mode=link \$(CC) \
80+
-export-dynamic \$(EXTRA_LDFLAGS_PROGRAM) \$(LDFLAGS) \$(PHP_RPATHS) \
81+
sapi/embed/php_embed_main.lo \
82+
\$(PHP_GLOBAL_OBJS) \$(PHP_SAPI_OBJS) \$(EXTRA_LIBS) \$(ZEND_EXTRA_LIBS) \
83+
-o \$(INSTALL_ROOT)\$(bindir)/php-embed-main"
84+
if test "$PHP_EMBED_LINK" != "no"; then
85+
AC_DEFINE_UNQUOTED([PHP_EMBED_LINK], ["$PHP_EMBED_LINK"], [PHP_EMBED_LINK])
86+
fi
87+
],
2588
[PHP_EMBED_TYPE=])
2689

2790
AS_VAR_IF([PHP_EMBED_TYPE],, [AC_MSG_RESULT([no])], [

‎sapi/embed/php_embed.c

Lines changed: 151 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <fcntl.h>
2424
#endif
2525

26+
#ifndef HAVE_EMBED_MAIN
2627
static const char HARDCODED_INI[] =
2728
"html_errors=0\n"
2829
"register_argc_argv=1\n"
@@ -114,6 +115,8 @@ static void php_embed_log_message(const char *message, int syslog_type_int)
114115
static void php_embed_register_variables(zval *track_vars_array)
115116
{
116117
php_import_environment_variables(track_vars_array);
118+
119+
php_register_variable("PHP_SELF", "-", track_vars_array);
117120
}
118121

119122
/* Module initialization (MINIT) */
@@ -244,7 +247,6 @@ EMBED_SAPI_API int php_embed_init(int argc, char **argv)
244247

245248
SG(headers_sent) = 1;
246249
SG(request_info).no_headers = 1;
247-
php_register_variable("PHP_SELF", "-", NULL);
248250

249251
return SUCCESS;
250252
}
@@ -264,3 +266,151 @@ EMBED_SAPI_API void php_embed_shutdown(void)
264266
tsrm_shutdown();
265267
#endif
266268
}
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /