Simplify process_wait()

The function process_wait() returned a bool (true if the process
terminated successfully) and provided the exit code via an output
parameter exit_code.

But the returned value was always equivalent to exit_code == 0, so just
return the exit code instead.
This commit is contained in:
Romain Vimont 2021-01-22 18:29:21 +01:00
parent 94eff0a4bb
commit b8edcf52b0
6 changed files with 30 additions and 36 deletions

View file

@ -178,7 +178,7 @@ file_handler_stop(struct file_handler *file_handler) {
if (!process_terminate(file_handler->current_process)) { if (!process_terminate(file_handler->current_process)) {
LOGW("Could not terminate install process"); LOGW("Could not terminate install process");
} }
process_wait(file_handler->current_process, NULL); process_wait(file_handler->current_process); // ignore exit code
file_handler->current_process = PROCESS_NONE; file_handler->current_process = PROCESS_NONE;
} }
mutex_unlock(file_handler->mutex); mutex_unlock(file_handler->mutex);

View file

@ -391,7 +391,7 @@ server_init(struct server *server) {
static int static int
run_wait_server(void *data) { run_wait_server(void *data) {
struct server *server = data; struct server *server = data;
process_wait_noclose(server->process, NULL); // ignore exit code process_wait_noclose(server->process); // ignore exit code
mutex_lock(server->mutex); mutex_lock(server->mutex);
server->process_terminated = true; server->process_terminated = true;
@ -447,7 +447,7 @@ server_start(struct server *server, const char *serial,
SDL_CreateThread(run_wait_server, "wait-server", server); SDL_CreateThread(run_wait_server, "wait-server", server);
if (!server->wait_server_thread) { if (!server->wait_server_thread) {
process_terminate(server->process); process_terminate(server->process);
process_wait(server->process, NULL); // ignore exit code process_wait(server->process); // ignore exit code
goto error2; goto error2;
} }

View file

@ -121,8 +121,8 @@ process_terminate(pid_t pid) {
return kill(pid, SIGTERM) != -1; return kill(pid, SIGTERM) != -1;
} }
static bool static exit_code_t
process_wait_internal(pid_t pid, int *exit_code, bool close) { process_wait_internal(pid_t pid, bool close) {
int code; int code;
int options = WEXITED; int options = WEXITED;
if (!close) { if (!close) {
@ -133,29 +133,26 @@ process_wait_internal(pid_t pid, int *exit_code, bool close) {
int r = waitid(P_PID, pid, &info, options); int r = waitid(P_PID, pid, &info, options);
if (r == -1 || info.si_code != CLD_EXITED) { if (r == -1 || info.si_code != CLD_EXITED) {
// could not wait, or exited unexpectedly, probably by a signal // could not wait, or exited unexpectedly, probably by a signal
code = -1; code = NO_EXIT_CODE;
} else { } else {
code = info.si_status; code = info.si_status;
} }
if (exit_code) { return code;
*exit_code = code;
}
return !code;
} }
bool exit_code_t
process_wait(pid_t pid, int *exit_code) { process_wait(pid_t pid) {
return process_wait_internal(pid, exit_code, true); return process_wait_internal(pid, true);
} }
bool exit_code_t
process_wait_noclose(pid_t pid, int *exit_code) { process_wait_noclose(pid_t pid) {
return process_wait_internal(pid, exit_code, false); return process_wait_internal(pid, false);
} }
void void
process_close(pid_t pid) { process_close(pid_t pid) {
process_wait_internal(pid, NULL, true); process_wait_internal(pid, true); // ignore exit code
} }
char * char *

View file

@ -59,31 +59,28 @@ process_terminate(HANDLE handle) {
return TerminateProcess(handle, 1); return TerminateProcess(handle, 1);
} }
static bool static exit_code_t
process_wait_internal(HANDLE handle, DWORD *exit_code, bool close) { process_wait_internal(HANDLE handle, bool close) {
DWORD code; DWORD code;
if (WaitForSingleObject(handle, INFINITE) != WAIT_OBJECT_0 if (WaitForSingleObject(handle, INFINITE) != WAIT_OBJECT_0
|| !GetExitCodeProcess(handle, &code)) { || !GetExitCodeProcess(handle, &code)) {
// could not wait or retrieve the exit code // could not wait or retrieve the exit code
code = -1; // max value, it's unsigned code = NO_EXIT_CODE; // max value, it's unsigned
}
if (exit_code) {
*exit_code = code;
} }
if (close) { if (close) {
CloseHandle(handle); CloseHandle(handle);
} }
return !code; return code;
} }
bool exit_code_t
process_wait(HANDLE handle, DWORD *exit_code) { process_wait(HANDLE handle) {
return process_wait_internal(handle, exit_code, true); return process_wait_internal(handle, true);
} }
bool exit_code_t
process_wait_noclose(HANDLE handle, DWORD *exit_code) { process_wait_noclose(HANDLE handle) {
return process_wait_internal(handle, exit_code, false); return process_wait_internal(handle, false);
} }
void void

View file

@ -8,8 +8,8 @@ process_check_success(process_t proc, const char *name) {
LOGE("Could not execute \"%s\"", name); LOGE("Could not execute \"%s\"", name);
return false; return false;
} }
exit_code_t exit_code; exit_code_t exit_code = process_wait(proc);
if (!process_wait(proc, &exit_code)) { if (exit_code) {
if (exit_code != NO_EXIT_CODE) { if (exit_code != NO_EXIT_CODE) {
LOGE("\"%s\" returned with value %" PRIexitcode, name, exit_code); LOGE("\"%s\" returned with value %" PRIexitcode, name, exit_code);
} else { } else {

View file

@ -47,12 +47,12 @@ bool
process_terminate(process_t pid); process_terminate(process_t pid);
// wait and close the process (like waitpid()) // wait and close the process (like waitpid())
bool exit_code_t
process_wait(process_t pid, exit_code_t *exit_code); process_wait(process_t pid);
// wait (but does not close) the process (waitid() with WNOWAIT) // wait (but does not close) the process (waitid() with WNOWAIT)
bool exit_code_t
process_wait_noclose(process_t pid, exit_code_t *exit_code); process_wait_noclose(process_t pid);
// close the process // close the process
// //