Child processes send trace information back to the parent. Also fixed weird double-activation of children.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3729 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2022-01-02 18:17:58 +00:00
parent d550092bd3
commit 23b15a8dc5
7 changed files with 71 additions and 32 deletions

View File

@ -71,6 +71,7 @@ typedef struct _tf_task_t {
script_export_t* _scriptExports;
bool _activated;
bool _trusted;
bool _killed;
int32_t _exitCode;
@ -398,7 +399,7 @@ JSValue _task_invokeExport_internal(tf_taskstub_t* from, tf_task_t* to, exportid
export_record_t* export = _task_get_export(to, exportId);
if (export)
{
JSValue arguments = tf_serialize_load((tf_task_t*)to, from, buffer, size);
JSValue arguments = tf_serialize_load(to, from, buffer, size);
JSValue* argument_array = NULL;
JSValue length_val = JS_GetPropertyStr(to->_context, arguments, "length");
int length;
@ -678,7 +679,7 @@ void tf_task_on_receive_packet(int packetType, const char* begin, size_t length,
memcpy(&promise, begin, sizeof(promiseid_t));
if (length > sizeof(promiseid_t))
{
arg = tf_serialize_load((tf_task_t*)to, from, begin + sizeof(promiseid_t), length - sizeof(promiseid_t));
arg = tf_serialize_load(to, from, begin + sizeof(promiseid_t), length - sizeof(promiseid_t));
}
if (packetType == kResolvePromise)
{
@ -731,11 +732,11 @@ void tf_task_on_receive_packet(int packetType, const char* begin, size_t length,
}
break;
case kSetRequires:
to->_requires = tf_serialize_load((tf_task_t*)to, from, begin, length);
to->_requires = tf_serialize_load(to, from, begin, length);
break;
case kLoadFile:
{
JSValue args = tf_serialize_load((tf_task_t*)to, from, begin, length);
JSValue args = tf_serialize_load(to, from, begin, length);
JSValue key = JS_GetPropertyUint32(to->_context, args, 0);
JSValue content = JS_GetPropertyUint32(to->_context, args, 1);
const char* name = JS_ToCString(to->_context, key);
@ -754,7 +755,7 @@ void tf_task_on_receive_packet(int packetType, const char* begin, size_t length,
assert(length >= sizeof(promiseid_t));
promiseid_t promise;
memcpy(&promise, begin, sizeof(promiseid_t));
JSValue value = tf_serialize_load((tf_task_t*)to, from, begin + sizeof(promiseid_t), length - sizeof(promiseid_t));
JSValue value = tf_serialize_load(to, from, begin + sizeof(promiseid_t), length - sizeof(promiseid_t));
JSValue source = JS_GetPropertyStr(to->_context, value, "source");
JSValue name_val = JS_GetPropertyStr(to->_context, value, "name");
const char* name = JS_ToCString(to->_context, name_val);
@ -776,7 +777,7 @@ void tf_task_on_receive_packet(int packetType, const char* begin, size_t length,
case kSetImports:
{
JSValue global = JS_GetGlobalObject(to->_context);
JSValue imports = tf_serialize_load((tf_task_t*)to, from, begin, length);
JSValue imports = tf_serialize_load(to, from, begin, length);
JSPropertyEnum* ptab;
uint32_t plen;
@ -810,11 +811,14 @@ void tf_task_on_receive_packet(int packetType, const char* begin, size_t length,
break;
case kTaskError:
{
JSValue error = tf_serialize_load((tf_task_t*)to, from, begin, length);
JSValue error = tf_serialize_load(to, from, begin, length);
tf_taskstub_on_error(from, error);
JS_FreeValue(to->_context, error);
}
break;
case kTaskTrace:
tf_trace_raw(to->_trace, begin, length);
break;
}
tf_trace_end(to->_trace);
}
@ -1225,8 +1229,17 @@ void tf_task_configure_from_stdin(tf_task_t* task)
task->_parent = tf_taskstub_create_parent(task, STDIN_FILENO);
}
static void _tf_task_trace_to_parent(tf_trace_t* trace, const char* buffer, size_t size, void* user_data)
{
tf_task_t* task = user_data;
tf_packetstream_send(tf_taskstub_get_stream(task->_parent), kTaskTrace, buffer, size);
}
void tf_task_activate(tf_task_t* task)
{
assert(!task->_activated);
task->_activated = true;
JSContext* context = task->_context;
JSValue global = JS_GetGlobalObject(context);
JS_SetPropertyStr(context, global, "exports", JS_NewObject(context));
@ -1270,6 +1283,7 @@ void tf_task_activate(tf_task_t* task)
}
JS_SetPropertyStr(context, global, "tildefriends", tildefriends);
task->_trace = tf_trace_create();
if (task->_trusted)
{
sqlite3_open(*task->_db_path ? task->_db_path : "db.sqlite", &task->_db);
@ -1280,8 +1294,6 @@ void tf_task_activate(tf_task_t* task)
JS_SetPropertyStr(context, global, "TlsContext", tf_tls_context_register(context));
tf_file_register(context);
task->_trace = tf_trace_create();
task->_ssb = tf_ssb_create(&task->_loop, task->_context, task->_db, *task->_secrets_path ? task->_secrets_path : NULL);
tf_ssb_set_trace(task->_ssb, task->_trace);
tf_ssb_register(context, task->_ssb);
@ -1297,6 +1309,7 @@ void tf_task_activate(tf_task_t* task)
else
{
JS_SetPropertyStr(context, global, "require", JS_NewCFunction(context, _tf_task_sandbox_require, "sandboxRequire", 0));
tf_trace_set_write_callback(task->_trace, _tf_task_trace_to_parent, task);
}
tf_bcrypt_register(context);