openwrt/package/utils/ucode/patches/111-uloop-add-optional-setup-callback-to-process.patch
Paul Donald a82738d83f ucode: update to Git 85922056ef7 (2026-01-16)
45bf891e1d8d rtnl: add module documentation
8bbf01215ce3 nl80211: add module documentation
40a6aeb698e1 nl80211: add new attribute "mlo_links" for per link information
376e8733609e ubus: complete pending requests when disconnecting locally
32449bfb69d6 io: add ptsname, tcgetattr, tcsetattr, grantpt, unlockpt
76bf32679d86 digest: fix jsdoc to reveal sha512 functions
58b4597fa2ae build: drop remnant
6ef0b3ada3fd build: respect bin/lib paths
28132276a426 build: adjust flags and definitions
cb1b1c1a097b zlib: make chunk size configurable
6f80655c88c3 debian: refresh packaging
9fc4889c6e8e debian: adjust LTO build
a503a49f5cef debian: build "digest" and "zlib" modules
ffc48a2a4db9 socket: fix off-by-one in uv_to_sockaddr()

Fixes: https://github.com/jow-/ucode/issues/366

dropped patch 130 - integrated at source.
refreshed patches

https://github.com/openwrt/openwrt/pull/21585
Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: https://github.com/openwrt/openwrt/pull/21585
Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
2026-01-21 23:50:52 +01:00

85 lines
3.1 KiB
Diff

From: Felix Fietkau <nbd@nbd.name>
Date: Wed, 8 Oct 2025 22:06:46 +0200
Subject: [PATCH] uloop: add optional setup callback to process()
Add optional setup callback as 5th argument to uloop.process() that is
invoked in the child process after fork() but before exec().
Signed-off-by: Felix Fietkau <nbd@nbd.name>
---
--- a/lib/uloop.c
+++ b/lib/uloop.c
@@ -968,8 +968,9 @@ uc_uloop_process_cb(struct uloop_process
*
* This function creates a process instance for executing external programs.
* It takes the executable path string, an optional string array as the argument
- * vector, an optional dictionary describing environment variables, and a
- * callback function to be invoked when the invoked process ends.
+ * vector, an optional dictionary describing environment variables, a
+ * callback function to be invoked when the invoked process ends, and an optional
+ * setup callback to be invoked in the child process after fork().
*
* @function module:uloop#process
*
@@ -986,6 +987,11 @@ uc_uloop_process_cb(struct uloop_process
* @param {Function} callback
* The callback function to be invoked when the invoked process ends.
*
+ * @param {Function} [setup]
+ * Optional. A callback function to be invoked in the child process after fork()
+ * but before exec(). This can be used to set up file descriptors, change working
+ * directory, or perform other initialization.
+ *
* @returns {?module:uloop.process}
* Returns a process instance for executing external programs.
* Returns `null` on error, e.g. due to `exec()` failure or invalid arguments.
@@ -995,6 +1001,16 @@ uc_uloop_process_cb(struct uloop_process
* const myProcess = uloop.process("/bin/ls", ["-l", "/tmp"], null, (code) => {
* printf(`Process exited with code ${code}\n`);
* });
+ *
+ * // With setup callback to redirect stderr
+ * const myProcess = uloop.process("/bin/ls", ["-l", "/tmp"], null, (code) => {
+ * printf(`Process exited with code ${code}\n`);
+ * }, () => {
+ * const fs = require('fs');
+ * const errlog = fs.open('/tmp/error.log', 'w');
+ * fs.dup2(errlog.fileno(), 2);
+ * errlog.close();
+ * });
*/
static uc_value_t *
uc_uloop_process(uc_vm_t *vm, size_t nargs)
@@ -1003,6 +1019,7 @@ uc_uloop_process(uc_vm_t *vm, size_t nar
uc_value_t *arguments = uc_fn_arg(1);
uc_value_t *env_arg = uc_fn_arg(2);
uc_value_t *callback = uc_fn_arg(3);
+ uc_value_t *setup_cb = uc_fn_arg(4);
uc_uloop_process_t *process;
uc_stringbuf_t *buf;
char **argp, **envp;
@@ -1012,7 +1029,8 @@ uc_uloop_process(uc_vm_t *vm, size_t nar
if (ucv_type(executable) != UC_STRING ||
(arguments && ucv_type(arguments) != UC_ARRAY) ||
(env_arg && ucv_type(env_arg) != UC_OBJECT) ||
- !ucv_is_callable(callback)) {
+ !ucv_is_callable(callback) ||
+ (setup_cb && !ucv_is_callable(setup_cb))) {
err_return(EINVAL);
}
@@ -1022,6 +1040,13 @@ uc_uloop_process(uc_vm_t *vm, size_t nar
err_return(errno);
if (pid == 0) {
+ if (setup_cb) {
+ uc_vm_stack_push(vm, ucv_get(setup_cb));
+
+ if (uc_uloop_vm_call(vm, false, 0))
+ ucv_put(uc_vm_stack_pop(vm));
+ }
+
argp = calloc(ucv_array_length(arguments) + 2, sizeof(char *));
envp = environ;