From b97182141fec6d8ac23d4ee19aa6b945d018a86d Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sat, 26 Jan 2019 12:08:40 +0000 Subject: [PATCH] Properly handle quitting by request, and use that in the tests --- daemon/gamemode-tests.c | 35 +++++++++++++++++++++++++++-------- daemon/main.c | 16 +++++++++++++++- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/daemon/gamemode-tests.c b/daemon/gamemode-tests.c index 398441b..4962ff0 100644 --- a/daemon/gamemode-tests.c +++ b/daemon/gamemode-tests.c @@ -36,6 +36,8 @@ POSSIBILITY OF SUCH DAMAGE. #include "logging.h" #include +#include +#include #include #include "gamemode_client.h" @@ -155,7 +157,9 @@ static int run_basic_client_tests(void) return 0; } -/* Run some dual client tests */ +/* Run some dual client tests + * This also tests that the "-r" argument works correctly and cleans up correctly + */ static int run_dual_client_tests(void) { int status = 0; @@ -200,21 +204,36 @@ static int run_dual_client_tests(void) if (verify_active_and_registered() != 0) status = -1; - /* Send SIGCONT to child */ - if (kill(child, SIGCONT) == -1) { + /* Request end of gamemode (de-register ourselves) */ + if (gamemode_request_end() != 0) { + fprintf(stderr, "gamemode_request_end failed: %s!\n", gamemode_error_string()); + status = -1; + } + + /* Check that when we request gamemode, it replies that the other client is connected */ + if (verify_other_client_connected() != 0) + status = -1; + + /* Send SIGINT to child to wake it up*/ + if (kill(child, SIGINT) == -1) { fprintf(stderr, "failed to send continue signal to other client: %s\n", strerror(errno)); status = -1; } /* Give the child a chance to finish */ - usleep(1000); + usleep(10000); - /* clean up the child */ - if (kill(child, SIGKILL) == -1) { - fprintf(stderr, "failed to kill the child: %s\n", strerror(errno)); - status = -1; + // Wait for the child to finish up + int wstatus; + while (waitpid(child, &wstatus, WNOHANG) == 0) { + fprintf(stderr, "Waiting for child to quit...\n"); + usleep(10000); } + /* Verify that gamemode is now innactive */ + if (verify_deactivated() != 0) + return -1; + if (status == 0) fprintf(stdout, "dual client tests passed.\n"); diff --git a/daemon/main.c b/daemon/main.c index 2867838..48317de 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -86,6 +86,11 @@ static void sigint_handler(__attribute__((unused)) int signo) _Exit(EXIT_SUCCESS); } +static void sigint_handler_noexit(__attribute__((unused)) int signo) +{ + LOG_MSG("Quitting by request...\n"); +} + /** * Main bootstrap entry into gamemoded */ @@ -136,9 +141,18 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - // Simply pause and wait for any signal + // Simply pause and wait a SIGINT + if (signal(SIGINT, sigint_handler_noexit) == SIG_ERR) { + FATAL_ERRORNO("Could not catch SIGINT"); + } pause(); + // Explicitly clean up + if (gamemode_request_end() < 0) { + fprintf(stderr, "gamemode request failed: %s\n", gamemode_error_string()); + exit(EXIT_FAILURE); + } + exit(EXIT_SUCCESS); break; case 't':