Properly handle quitting by request, and use that in the tests

This commit is contained in:
Marc Di Luzio 2019-01-26 12:08:40 +00:00
parent c99e06ed9e
commit b97182141f
2 changed files with 42 additions and 9 deletions

View File

@ -36,6 +36,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include "logging.h" #include "logging.h"
#include <libgen.h> #include <libgen.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
#include "gamemode_client.h" #include "gamemode_client.h"
@ -155,7 +157,9 @@ static int run_basic_client_tests(void)
return 0; 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) static int run_dual_client_tests(void)
{ {
int status = 0; int status = 0;
@ -200,21 +204,36 @@ static int run_dual_client_tests(void)
if (verify_active_and_registered() != 0) if (verify_active_and_registered() != 0)
status = -1; status = -1;
/* Send SIGCONT to child */ /* Request end of gamemode (de-register ourselves) */
if (kill(child, SIGCONT) == -1) { 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)); fprintf(stderr, "failed to send continue signal to other client: %s\n", strerror(errno));
status = -1; status = -1;
} }
/* Give the child a chance to finish */ /* Give the child a chance to finish */
usleep(1000); usleep(10000);
/* clean up the child */ // Wait for the child to finish up
if (kill(child, SIGKILL) == -1) { int wstatus;
fprintf(stderr, "failed to kill the child: %s\n", strerror(errno)); while (waitpid(child, &wstatus, WNOHANG) == 0) {
status = -1; 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) if (status == 0)
fprintf(stdout, "dual client tests passed.\n"); fprintf(stdout, "dual client tests passed.\n");

View File

@ -86,6 +86,11 @@ static void sigint_handler(__attribute__((unused)) int signo)
_Exit(EXIT_SUCCESS); _Exit(EXIT_SUCCESS);
} }
static void sigint_handler_noexit(__attribute__((unused)) int signo)
{
LOG_MSG("Quitting by request...\n");
}
/** /**
* Main bootstrap entry into gamemoded * Main bootstrap entry into gamemoded
*/ */
@ -136,9 +141,18 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE); 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(); 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); exit(EXIT_SUCCESS);
break; break;
case 't': case 't':