# SAPI EMBED Problems

- Thread: https://externals.io/message/25605
- Original archive: https://news-web.php.net/php.internals/25605
- Started by Brian Fertig on 2006-09-11T12:46:02+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #25605 — Brian Fertig — 2006-09-11T12:46:02+00:00

URL: https://externals.io/message/25605#25605

I wrote an embedded application that can execute php scripts.
When the script exits and I call the php_embed_shutdown() function
it crashes.  Can someone explain to me how I can keep this from happening?


Brian

---

## #25606 — Johannes Schlueter — 2006-09-11T13:09:04+00:00

URL: https://externals.io/message/25605#25606 · In reply to: #25605

Not without code, backtrace, etc.

johannes

Brian Fertig wrote:
> I wrote an embedded application that can execute php scripts.
> When the script exits and I call the php_embed_shutdown() function
> it crashes.  Can someone explain to me how I can keep this from happening?
>
>
> Brian

---

## #25607 — Brian Fertig — 2006-09-11T13:22:34+00:00

URL: https://externals.io/message/25605#25607 · In reply to: #25606

Johannes Schlueter wrote:
> Not without code, backtrace, etc.
> 
> johannes
> 
> Brian Fertig wrote:
> > […]


this is the code
----------------
> static void php_function(switch_core_session_t *session, char *data)
> {
>         char *uuid = switch_core_session_get_uuid(session);
>         uint32_t ulen = strlen(uuid);
>         uint32_t len = strlen((char *) data) + ulen + 2;
>         char *mydata = switch_core_session_alloc(session, len);
>         int argc, retval;
>         char *argv[5];
>         char php_code[1024];
>         void*** tsrm_ls = NULL;
> 
>         snprintf(mydata, len, "%s %s", uuid, data);
> 
>         argc = switch_separate_string(mydata, ' ',argv,(sizeof(argv) / sizeof(argv[0])));
> 
>         sprintf(php_code, "uuid=\"%s\"; include(\"%s\");\n", argv[0], argv[1]);
>         //sprintf(php_code, "include('%s');", argv[1]);
> 
>         sprintf(php_code, "%s %s", data, uuid);
> 
>         zend_file_handle script;
>         script.type = ZEND_HANDLE_FP;
>         script.filename = data;
>         script.opened_path = NULL;
>         script.free_filename = 0;
>         script.handle.fp = fopen(script.filename, "rb");
> 
>         // Initialize PHPs CORE
>         php_embed_init(argc, argv, &tsrm_ls);
> 
>         // Return All of the DEBUG crap to the console and/or a log file
>         php_embed_module.ub_write = sapi_mod_php_ub_write;
>         php_embed_module.log_message = mod_php_log_message;
>         php_embed_module.sapi_error = (sapi_error_function_t)mod_php_error_handler;
> 
>         // Let the nice people know we are about to start their script
>         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Starting Script %s\n",data);
> 
>         // Force $uuid and $session to exist in PHPs memory space
>         zval *php_uuid;
>         MAKE_STD_ZVAL(php_uuid);
>         //MAKE_STD_ZVAL(php_session);
>         //php_uuid->type = IS_STRING;
>         //php_uuid->value.str.len = strlen(uuid);
>         //php_uuid->value.str.val = estrdup(uuid);
>         ZVAL_STRING(php_uuid, uuid , 1);
>         //ZVAL_STRING(php_session, session , 1);
>         ZEND_SET_SYMBOL(&EG(symbol_table), "uuid", php_uuid);
>         //ZEND_SET_SYMBOL(&EG(active_symbol_table), "session", php_session);
> 
>         // Force Some INI entries weather the user likes it or not
>         zend_alter_ini_entry("register_globals",sizeof("register_globals"),"1", sizeof("1") - 1, PHP_INI_SYSTEM, PHP_INI_STAGE_RUNTIME);
> 
>         // Execute the bloody script
>         retval = php_execute_script(&script TSRMLS_CC);
> 
>         // Clean up after PHP and such
>         php_embed_shutdown(tsrm_ls);  It freaks out down here when it calls tsrm_shutdown() it just stops the whole server.
> 
> 
> 
>         // Return back to the Dialplan
> 
> // Buh bye now!
> }

---

## #25610 — Steph Fox — 2006-09-11T16:10:23+00:00

URL: https://externals.io/message/25605#25610 · In reply to: #25607

Is this crash under Windows perchance?

> Johannes Schlueter wrote:
> > […]
>
>
> this is the code
> ----------------
> > […]

---

## #25611 — Stanislav Malyshev — 2006-09-11T16:31:16+00:00

URL: https://externals.io/message/25605#25611 · In reply to: #25607

> > […]

Without looking into the code, crash on shutdown frequently hints on one 
of the two:
1. Memory corruption
2. applying efree() to a memory block that was result of malloc() or was 
not allocated. This includes putting malloc'ed or non-allocated strings 
into zvals, etc.
I would suggest to build PHP with --enable-debug and see if memory 
manager has something to say. Another idea would be to use valgrind if 
you are running on supported system or analogous tool if you run on 
system where valgrind does not work.

---

## #25612 — Steph Fox — 2006-09-11T16:35:54+00:00

URL: https://externals.io/message/25605#25612 · In reply to: #25610

Scratch that, not enough caffeine in the bloodstream :(

Yes, of course it's under doze.... a more intelligent question would be 
'which version of PHP are you using?'

- Steph

> Is this crash under Windows perchance?
>
> > […]
