core: error out in nm_utils_kill_child_{sync,async}() if first waitpid() gives ECHILD

It is wrong trying to send the signal still. Just error out.

Note that ECHILD indicates that the process is either not a child
or was already reaped. In both cases, that is a bug of the caller
who must keep accurate track of the child's process ID.
This commit is contained in:
Thomas Haller 2023-03-29 13:58:17 +02:00
parent a65e80e8b6
commit f58a69656a
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 17 additions and 25 deletions

View file

@ -507,16 +507,13 @@ nm_utils_kill_child_async(pid_t pid,
return; return;
} else if (ret != 0) { } else if (ret != 0) {
errsv = errno; errsv = errno;
/* ECHILD means, the process is not a child/does not exist or it has SIGCHILD blocked. */ nm_log_err(LOGD_CORE | log_domain,
if (errsv != ECHILD) { LOG_NAME_FMT ": unexpected error while waitpid: %s (%d)",
nm_log_err(LOGD_CORE | log_domain, LOG_NAME_ARGS,
LOG_NAME_FMT ": unexpected error while waitpid: %s (%d)", nm_strerror_native(errsv),
LOG_NAME_ARGS, errsv);
nm_strerror_native(errsv), _kc_invoke_callback(pid, log_domain, log_name, callback, user_data, FALSE, -1);
errsv); return;
_kc_invoke_callback(pid, log_domain, log_name, callback, user_data, FALSE, -1);
return;
}
} }
/* send the first signal. */ /* send the first signal. */
@ -647,15 +644,12 @@ nm_utils_kill_child_sync(pid_t pid,
goto out; goto out;
} else if (ret != 0) { } else if (ret != 0) {
errsv = errno; errsv = errno;
/* ECHILD means, the process is not a child/does not exist or it has SIGCHILD blocked. */ nm_log_err(LOGD_CORE | log_domain,
if (errsv != ECHILD) { LOG_NAME_FMT ": unexpected error while waitpid: %s (%d)",
nm_log_err(LOGD_CORE | log_domain, LOG_NAME_ARGS,
LOG_NAME_FMT ": unexpected error while waitpid: %s (%d)", nm_strerror_native(errsv),
LOG_NAME_ARGS, errsv);
nm_strerror_native(errsv), goto out;
errsv);
goto out;
}
} }
/* send first signal @sig */ /* send first signal @sig */

View file

@ -333,9 +333,8 @@ do_test_nm_utils_kill_child(void)
/* pid3s should not be a valid process, hence the call should fail. Note, that there /* pid3s should not be a valid process, hence the call should fail. Note, that there
* is a race here. */ * is a race here. */
NMTST_EXPECT_NM_ERROR( NMTST_EXPECT_NM_ERROR("kill child process 'test-s-3-2' (*): unexpected error while waitpid: No "
"kill child process 'test-s-3-2' (*): failed due to unexpected return value -1 by waitpid " "child process* (10)");
"(No child process*, 10) after sending no signal (0)");
test_nm_utils_kill_child_sync_do("test-s-3-2", pid3s, 0, 0, FALSE, NULL); test_nm_utils_kill_child_sync_do("test-s-3-2", pid3s, 0, 0, FALSE, NULL);
NMTST_EXPECT_NM_DEBUG("kill child process 'test-s-4' (*): waiting up to 50 milliseconds for " NMTST_EXPECT_NM_DEBUG("kill child process 'test-s-4' (*): waiting up to 50 milliseconds for "
@ -396,9 +395,8 @@ do_test_nm_utils_kill_child(void)
/* pid3a should not be a valid process, hence the call should fail. Note, that there /* pid3a should not be a valid process, hence the call should fail. Note, that there
* is a race here. */ * is a race here. */
NMTST_EXPECT_NM_ERROR( NMTST_EXPECT_NM_ERROR("kill child process 'test-a-3-2' (*): unexpected error while "
"kill child process 'test-a-3-2' (*): failed due to unexpected return value -1 by waitpid " "waitpid: No child process* (10)");
"(No child process*, 10) after sending no signal (0)");
NMTST_EXPECT_NM_DEBUG( NMTST_EXPECT_NM_DEBUG(
"kill child process 'test-a-3-2' (*): invoke callback: killing child failed"); "kill child process 'test-a-3-2' (*): invoke callback: killing child failed");
test_nm_utils_kill_child_async_do("test-a-3-2", pid3a, 0, 0, FALSE, NULL); test_nm_utils_kill_child_async_do("test-a-3-2", pid3a, 0, 0, FALSE, NULL);