123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- /*
- Copyright (c) 2017-2018, Feral Interactive
- All rights reserved.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
- * Redistributions of source code must retain the above copyright notice,
- this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- * Neither the name of Feral Interactive nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- POSSIBILITY OF SUCH DAMAGE.
- */
- #define _GNU_SOURCE
- #include "gamemode.h"
- #include "helpers.h"
- #include "logging.h"
- #include <libgen.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include "daemon_config.h"
- #include "gamemode_client.h"
- #include "governors-query.h"
- /* Initial verify step to ensure gamemode isn't already active */
- static int verify_gamemode_initial(void)
- {
- int status = 0;
- if ((status = gamemode_query_status()) != 0 && status != -1) {
- LOG_ERROR("gamemode is currently active, tests require gamemode to start deactivated!\n");
- status = -1;
- } else if (status == -1) {
- LOG_ERROR("gamemode_query_status failed: %s!\n", gamemode_error_string());
- LOG_ERROR("is gamemode installed correctly?\n");
- status = -1;
- } else {
- status = 0;
- }
- return status;
- }
- /* Check if gamemode is active and this client is registered */
- static int verify_active_and_registered(void)
- {
- int status = gamemode_query_status();
- if (status != 2) {
- if (status == -1) {
- LOG_ERROR("gamemode_query_status failed: %s\n", gamemode_error_string());
- } else if (status == 1) {
- LOG_ERROR("gamemode was active but did not have this process registered\n");
- }
- LOG_ERROR("gamemode failed to activate correctly when requested (expected 2)!\n");
- status = -1;
- } else {
- status = 0;
- }
- return status;
- }
- /* Ensure gamemode is deactivated when it should be */
- static int verify_deactivated(void)
- {
- int status = gamemode_query_status();
- if (status != 0) {
- if (status == -1) {
- LOG_ERROR("gamemode_query_status failed: %s\n", gamemode_error_string());
- }
- LOG_ERROR("gamemode failed to deactivate when requested (expected 0)!\n");
- status = -1;
- } else {
- status = 0;
- }
- return status;
- }
- /* Ensure another client is connected */
- static int verify_other_client_connected(void)
- {
- int status = gamemode_query_status();
- if (status != 1) {
- if (status == -1) {
- LOG_ERROR("gamemode_query_status failed: %s\n", gamemode_error_string());
- }
- LOG_ERROR("gamemode_query_status failed to return other client connected (expected 1)!\n");
- status = -1;
- } else {
- status = 0;
- }
- return status;
- }
- /* Run basic client tests
- * Tests a simple request_start and request_end works
- */
- static int run_basic_client_tests(void)
- {
- LOG_MSG(":: Basic client tests\n");
- /* First verify that gamemode is not currently active on the system
- * As well as it being currently installed and queryable
- */
- if (verify_gamemode_initial() != 0)
- return -1;
- /* Verify that gamemode_request_start correctly start gamemode */
- if (gamemode_request_start() != 0) {
- LOG_ERROR("gamemode_request_start failed: %s\n", gamemode_error_string());
- return -1;
- }
- /* Verify that gamemode is now active and this client is registered*/
- if (verify_active_and_registered() != 0)
- return -1;
- /* Verify that gamemode_request_end corrently de-registers gamemode */
- if (gamemode_request_end() != 0) {
- LOG_ERROR("gamemode_request_end failed: %s!\n", gamemode_error_string());
- return -1;
- }
- /* Verify that gamemode is now innactive */
- if (verify_deactivated() != 0)
- return -1;
- LOG_MSG(":: Passed\n\n");
- return 0;
- }
- /* 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;
- /* Try running some process interop tests */
- LOG_MSG(":: Dual client tests\n");
- /* Get the current path to this binary */
- char mypath[PATH_MAX];
- memset(mypath, 0, sizeof(mypath));
- if (readlink("/proc/self/exe", mypath, PATH_MAX) == -1) {
- LOG_ERROR("could not read current exe path: %s\n", strerror(errno));
- return -1;
- }
- /* Fork so that the child can request gamemode */
- int child = fork();
- if (child == 0) {
- /* Relaunch self with -r (request and wait for signal) */
- if (execl(mypath, mypath, "-r", (char *)NULL) == -1) {
- LOG_ERROR("failed to re-launch self (%s) with execl: %s\n", mypath, strerror(errno));
- return -1;
- }
- }
- /* Parent process */
- /* None of these should early-out as we need to clean up the child */
- /* Give the child a chance to reqeust gamemode */
- usleep(1000);
- /* Check that when we request gamemode, it replies that the other client is connected */
- if (verify_other_client_connected() != 0)
- status = -1;
- /* Verify that gamemode_request_start correctly start gamemode */
- if (gamemode_request_start() != 0) {
- LOG_ERROR("gamemode_request_start failed: %s\n", gamemode_error_string());
- status = -1;
- }
- /* Verify that gamemode is now active and this client is registered*/
- if (verify_active_and_registered() != 0)
- status = -1;
- /* Request end of gamemode (de-register ourselves) */
- if (gamemode_request_end() != 0) {
- LOG_ERROR("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) {
- LOG_ERROR("failed to send continue signal to other client: %s\n", strerror(errno));
- status = -1;
- }
- /* Give the child a chance to finish */
- usleep(10000);
- // Wait for the child to finish up
- int wstatus;
- while (waitpid(child, &wstatus, WNOHANG) == 0) {
- LOG_MSG("...Waiting for child to quit...\n");
- usleep(10000);
- }
- /* Verify that gamemode is now innactive */
- if (verify_deactivated() != 0)
- return -1;
- if (status == 0)
- LOG_MSG(":: Passed\n\n");
- return status;
- }
- /**
- * game_mode_run_feature_tests runs a set of tests for each current feature (based on the current
- * config) returns 0 for success, -1 for failure
- */
- static int game_mode_run_feature_tests(void)
- {
- int status = 0;
- LOG_MSG(":: Feature tests\n");
- /* If we reach here, we should assume the basic requests and register functions are working */
- /* Grab the config */
- /* Note: this config may pick up a local gamemode.ini, or the daemon may have one, we may need
- * to cope with that */
- GameModeConfig *config = config_create();
- config_init(config);
- /* Does the CPU governor get set properly? */
- int cpustatus = 0;
- {
- LOG_MSG("::: Verifying CPU governor setting\n");
- /* get the two config parameters we care about */
- char desiredgov[CONFIG_VALUE_MAX] = { 0 };
- config_get_desired_governor(config, desiredgov);
- if (desiredgov[0] == '\0')
- strcpy(desiredgov, "performance");
- char defaultgov[CONFIG_VALUE_MAX] = { 0 };
- config_get_default_governor(config, defaultgov);
- if (desiredgov[0] == '\0') {
- const char *currentgov = get_gov_state();
- if (currentgov) {
- strncpy(desiredgov, currentgov, CONFIG_VALUE_MAX);
- } else {
- LOG_ERROR(
- "Could not get current CPU governor state, this indicates an error! See rest "
- "of log.\n");
- cpustatus = -1;
- }
- }
- /* Only continue if we had no error before */
- if (cpustatus == 0) {
- /* Start gamemode */
- gamemode_request_start();
- /* Verify the governor is the desired one */
- const char *currentgov = get_gov_state();
- if (strncmp(currentgov, desiredgov, CONFIG_VALUE_MAX) != 0) {
- LOG_ERROR("Govenor was not set to %s (was actually %s)!", desiredgov, currentgov);
- cpustatus = -1;
- }
- /* End gamemode */
- gamemode_request_end();
- /* Verify the governor has been set back */
- currentgov = get_gov_state();
- if (strncmp(currentgov, defaultgov, CONFIG_VALUE_MAX) != 0) {
- LOG_ERROR("Govenor was not set back to %s (was actually %s)!",
- defaultgov,
- currentgov);
- cpustatus = -1;
- }
- }
- }
- /* Do custom scripts run? */
- int scriptstatus = 0;
- {
- LOG_MSG("::: Verifying Scripts\n");
- /* Grab the scripts */
- char scripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX];
- memset(scripts, 0, sizeof(scripts));
- config_get_gamemode_start_scripts(config, scripts);
- if (scripts[0][0] != '\0') {
- /* Print out the scripts to run */
- LOG_MSG("Custom scripts:\n");
- int i = 0;
- while (*scripts[i] != '\0' && i < CONFIG_LIST_MAX)
- LOG_MSG("%s\n", scripts[i]);
- }
- /* TODO: Somehow verify these get run
- * Possibly by watching if the the binary part gets run?
- */
- }
- /* Does the screensaver get inhibited? */
- /* TODO */
- /* Do GPU optimisations get applied? */
- /* TODO */
- /* Was the process reniced? */
- /* TODO */
- /* Was the scheduling applied? */
- /* TODO */
- /* Were io priorities changed? */
- /* TODO */
- if (status != -1)
- LOG_MSG(":: Passed%s\n\n", status > 0 ? " (with optional failures)" : "");
- else
- LOG_ERROR(":: Failed!\n");
- return status;
- }
- /**
- * game_mode_run_client_tests runs a set of tests of the client code
- * we simply verify that the client can request the status and recieves the correct results
- *
- * returns 0 for success, -1 for failure
- */
- int game_mode_run_client_tests()
- {
- int status = 0;
- LOG_MSG(": Running tests\n\n");
- /* Run the basic tests */
- if (run_basic_client_tests() != 0)
- status = -1;
- /* Run the dual client tests */
- if (run_dual_client_tests() != 0)
- status = -1;
- if (status != 0) {
- LOG_MSG(": Client tests failed, skipping feature tests\n");
- } else {
- /* Run the feature tests */
- status = game_mode_run_feature_tests();
- }
- if (status >= 0)
- LOG_MSG(": All Tests Passed%s!\n", status > 0 ? " (with optional failures)" : "");
- else
- LOG_MSG(": Tests Failed!\n");
- return status;
- }
|