mirror of
https://github.com/FeralInteractive/gamemode.git
synced 2025-06-06 07:37:21 +02:00
Implement PID support for --request and --status
This commit is contained in:
parent
baff9c0363
commit
717777e6c2
142
daemon/main.c
142
daemon/main.c
@ -65,8 +65,10 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||||||
|
|
||||||
#define USAGE_TEXT \
|
#define USAGE_TEXT \
|
||||||
"Usage: %s [-d] [-l] [-r] [-t] [-h] [-v]\n\n" \
|
"Usage: %s [-d] [-l] [-r] [-t] [-h] [-v]\n\n" \
|
||||||
" -r, --request Request gamemode and pause\n" \
|
" -r[PID], --request=[PID] Toggle gamemode for process\n" \
|
||||||
" -s, --status Query the status of gamemode\n" \
|
" When no PID given, requests gamemode and pauses\n" \
|
||||||
|
" -s[PID], --status=[PID] Query the status of gamemode for process\n" \
|
||||||
|
" When no PID given, queries the status globally\n" \
|
||||||
" -d, --daemonize Daemonize self after launch\n" \
|
" -d, --daemonize Daemonize self after launch\n" \
|
||||||
" -l, --log-to-syslog Log to syslog\n" \
|
" -l, --log-to-syslog Log to syslog\n" \
|
||||||
" -r, --test Run tests\n" \
|
" -r, --test Run tests\n" \
|
||||||
@ -107,9 +109,9 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
/* Options struct for getopt_long */
|
/* Options struct for getopt_long */
|
||||||
static struct option long_options[] = {
|
static struct option long_options[] = {
|
||||||
{ "daemonize", no_argument, 0, 'd' }, { "log-to-syslog", no_argument, 0, 'l' },
|
{ "daemonize", no_argument, 0, 'd' }, { "log-to-syslog", no_argument, 0, 'l' },
|
||||||
{ "request", no_argument, 0, 'r' }, { "test", no_argument, 0, 't' },
|
{ "request", optional_argument, 0, 'r' }, { "test", no_argument, 0, 't' },
|
||||||
{ "status", no_argument, 0, 's' }, { "help", no_argument, 0, 'h' },
|
{ "status", optional_argument, 0, 's' }, { "help", no_argument, 0, 'h' },
|
||||||
{ "version", no_argument, 0, 'v' }
|
{ "version", no_argument, 0, 'v' }
|
||||||
};
|
};
|
||||||
static const char *short_options = "dls::r::tvh";
|
static const char *short_options = "dls::r::tvh";
|
||||||
@ -124,53 +126,107 @@ int main(int argc, char *argv[])
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 's':
|
case 's':
|
||||||
|
if (optarg != NULL) {
|
||||||
switch (gamemode_query_status()) {
|
pid_t pid = atoi(optarg);
|
||||||
case 0:
|
switch (gamemode_query_status_for(pid)) {
|
||||||
LOG_MSG("gamemode is inactive\n");
|
case 0: /* inactive */
|
||||||
break;
|
LOG_MSG("gamemode is inactive\n");
|
||||||
case 1:
|
break;
|
||||||
LOG_MSG("gamemode is active\n");
|
case 1: /* active not not registered */
|
||||||
break;
|
LOG_MSG("gamemode is active but [%d] not registered\n", pid);
|
||||||
case -1:
|
break;
|
||||||
LOG_ERROR("gamemode status request failed: %s\n", gamemode_error_string());
|
case 2: /* active for client */
|
||||||
exit(EXIT_FAILURE);
|
LOG_MSG("gamemode is active and [%d] registered\n", pid);
|
||||||
default:
|
break;
|
||||||
LOG_ERROR("gamemode_query_status returned unexpected value 2\n");
|
case -1:
|
||||||
exit(EXIT_FAILURE);
|
LOG_ERROR("gamemode_query_status_for(%d) failed: %s\n",
|
||||||
|
pid,
|
||||||
|
gamemode_error_string());
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
default:
|
||||||
|
LOG_ERROR("gamemode_query_status returned unexpected value 2\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
switch (gamemode_query_status()) {
|
||||||
|
case 0:
|
||||||
|
LOG_MSG("gamemode is inactive\n");
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
LOG_MSG("gamemode is active\n");
|
||||||
|
break;
|
||||||
|
case -1:
|
||||||
|
LOG_ERROR("gamemode status request failed: %s\n", gamemode_error_string());
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
default:
|
||||||
|
LOG_ERROR("gamemode_query_status returned unexpected value 2\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
|
|
||||||
case 'r':
|
case 'r':
|
||||||
|
|
||||||
if (gamemode_request_start() < 0) {
|
if (optarg != NULL) {
|
||||||
LOG_ERROR("gamemode request failed: %s\n", gamemode_error_string());
|
pid_t pid = atoi(optarg);
|
||||||
exit(EXIT_FAILURE);
|
switch (gamemode_query_status_for(pid)) {
|
||||||
}
|
case 0: /* inactive */
|
||||||
|
case 1: /* active not not registered */
|
||||||
|
LOG_MSG("gamemode not active for client, requesting start for %d...\n", pid);
|
||||||
|
if (gamemode_request_start_for(pid) < 0) {
|
||||||
|
LOG_ERROR("gamemode_request_start_for(%d) failed: %s\n",
|
||||||
|
pid,
|
||||||
|
gamemode_error_string());
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
LOG_MSG("request succeeded\n");
|
||||||
|
break;
|
||||||
|
case 2: /* active for client */
|
||||||
|
LOG_MSG("gamemode active for client, requesting end for %d...\n", pid);
|
||||||
|
if (gamemode_request_end_for(pid) < 0) {
|
||||||
|
LOG_ERROR("gamemode_request_end_for(%d) failed: %s\n",
|
||||||
|
pid,
|
||||||
|
gamemode_error_string());
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
LOG_MSG("request succeeded\n");
|
||||||
|
break;
|
||||||
|
case -1:
|
||||||
|
LOG_ERROR("gamemode_query_status_for(%d) failed: %s\n",
|
||||||
|
pid,
|
||||||
|
gamemode_error_string());
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (gamemode_request_start() < 0) {
|
||||||
|
LOG_ERROR("gamemode request failed: %s\n", gamemode_error_string());
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
switch (gamemode_query_status()) {
|
switch (gamemode_query_status()) {
|
||||||
case 2:
|
case 2:
|
||||||
LOG_MSG("gamemode request succeeded and is active\n");
|
LOG_MSG("gamemode request succeeded and is active\n");
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
LOG_ERROR("gamemode request succeeded and is active but registration failed\n");
|
LOG_ERROR("gamemode request succeeded and is active but registration failed\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
case 0:
|
case 0:
|
||||||
LOG_ERROR("gamemode request succeeded but is not active\n");
|
LOG_ERROR("gamemode request succeeded but is not active\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simply pause and wait a SIGINT
|
// Simply pause and wait a SIGINT
|
||||||
if (signal(SIGINT, sigint_handler_noexit) == SIG_ERR) {
|
if (signal(SIGINT, sigint_handler_noexit) == SIG_ERR) {
|
||||||
FATAL_ERRORNO("Could not catch SIGINT");
|
FATAL_ERRORNO("Could not catch SIGINT");
|
||||||
}
|
}
|
||||||
pause();
|
pause();
|
||||||
|
|
||||||
// Explicitly clean up
|
// Explicitly clean up
|
||||||
if (gamemode_request_end() < 0) {
|
if (gamemode_request_end() < 0) {
|
||||||
LOG_ERROR("gamemode request failed: %s\n", gamemode_error_string());
|
LOG_ERROR("gamemode request failed: %s\n", gamemode_error_string());
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
|
@ -14,11 +14,13 @@ The design has a clear cut abstraction between the host daemon and library (\fBg
|
|||||||
.SH OPTIONS
|
.SH OPTIONS
|
||||||
|
|
||||||
.TP 8
|
.TP 8
|
||||||
.B \-r, \-\-request
|
.B \-r[PID], \-\-request=[PID]
|
||||||
Request gamemode and pause
|
Toggle gamemode for process.
|
||||||
|
When no PID given, requests gamemode and pauses
|
||||||
.TP 8
|
.TP 8
|
||||||
.B \-s, \-\-status
|
.B \-s[PID], \-\-status=[PID]
|
||||||
Query the status of gamemode
|
Query the status of gamemode for process
|
||||||
|
When no PID given, queries the status globally
|
||||||
.TP 8
|
.TP 8
|
||||||
.B \-d, \-\-daemonize
|
.B \-d, \-\-daemonize
|
||||||
Run the daemon as a separate process (daemonize it)
|
Run the daemon as a separate process (daemonize it)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user