Explorar o código

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

Marc Di Luzio %!s(int64=6) %!d(string=hai) anos
pai
achega
b97182141f
Modificáronse 2 ficheiros con 42 adicións e 9 borrados
  1. 27 8
      daemon/gamemode-tests.c
  2. 15 1
      daemon/main.c

+ 27 - 8
daemon/gamemode-tests.c

@@ -36,6 +36,8 @@ POSSIBILITY OF SUCH DAMAGE.
 #include "logging.h"
 
 #include <libgen.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 #include <unistd.h>
 
 #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");
 

+ 15 - 1
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':