From 299da6ace3c06393dff505ed277ead8de276a419 Mon Sep 17 00:00:00 2001 From: shodanshok Date: Wed, 4 Dec 2024 11:36:10 +0100 Subject: [PATCH] Fix race in libzfs_run_process_impl When replacing a disk, a child process is forked to run a script called zfs_prepare_disk (which can be useful for disk firmware update or health check). The parent than calls waitpid and checks the child error/status code. However, the _reap_children thread (created from zed_exec_process to manage zedlets) also waits for all children with the same PGID and can stole the signal, causing the replace operation to be aborted. As waitpid returns -1, the parent incorrectly assume that the child process had an error or was killed. This, in turn, leaves the newly added disk in REMOVED or UNAVAIL status rather than completing the replace process. This patch changes the PGID of the child process execuing the prepare script, shielding it from the _reap_children thread. Reviewed-by: Brian Behlendorf Reviewed-by: Alexander Motin Reviewed-by: Tony Hutter Signed-off-by: Gionatan Danti Closes #16801 --- lib/libzfs/libzfs_util.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/libzfs/libzfs_util.c b/lib/libzfs/libzfs_util.c index 1db79fb170e4..89fea053d598 100644 --- a/lib/libzfs/libzfs_util.c +++ b/lib/libzfs/libzfs_util.c @@ -926,6 +926,7 @@ libzfs_run_process_impl(const char *path, char *argv[], char *env[], int flags, pid = fork(); if (pid == 0) { /* Child process */ + setpgid(0, 0); devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC); if (devnull_fd < 0)