ソースを参照

Merge pull request #148 from mdiluz/the-big-cleanup

Spring cleaning
Alex Smith 5 年 前
コミット
fc46ffc463

+ 19 - 22
.clang-format

@@ -2,8 +2,7 @@
 AccessModifierOffset: 0
 AlignAfterOpenBracket: true
 AlignConsecutiveAssignments: false
-#uncomment for clang 3.9
-#AlignConsecutiveDeclarations: false
+AlignConsecutiveDeclarations: false
 AlignEscapedNewlinesLeft: false
 AlignOperands: true
 AlignTrailingComments: true
@@ -14,21 +13,18 @@ AllowShortFunctionsOnASingleLine: None
 AllowShortIfStatementsOnASingleLine: false
 AllowShortLoopsOnASingleLine: false
 # AlwaysBreakAfterDefinitionReturnType: None
-#uncomment for clang 3.9
-#AlwaysBreakAfterReturnType: None
+AlwaysBreakAfterReturnType: None
 AlwaysBreakBeforeMultilineStrings: true
 AlwaysBreakTemplateDeclarations: false
 BinPackArguments: false
 BinPackParameters: true
-# BraceWrapping: (not set since BreakBeforeBraces is not Custom)
 BreakBeforeBinaryOperators: None
-# BreakAfterJavaFieldAnnotations: (not java)
 BreakBeforeBinaryOperators: None
 BreakBeforeBraces: Linux
 BreakBeforeTernaryOperators: true
 BreakConstructorInitializersBeforeComma: false
-#uncomment for clang 3.9
-#BreakStringLiterals: false
+# Too new for travis clang-format version
+# BreakStringLiterals: false
 ColumnLimit: 100
 CommentPragmas:  '\*\<'
 ConstructorInitializerAllOnOneLineOrOnePerLine: false
@@ -39,35 +35,37 @@ DerivePointerAlignment: false
 DisableFormat: false
 ExperimentalAutoDetectBinPacking: false
 ForEachMacros: [ ]
-#Uncomment for clang 3.9
-#IncludeCategories:
-#  - Regex: '^"'
-#    Priority: 1
+SortIncludes: true
+# IncludeBlocksStyle changed to IncludeBlocks, between xenial and disco, so we can't use it for consistency
+# IncludeBlocks: Regroup
+IncludeCategories:
+  - Regex: '^"gamemode.h"'
+    Priority: 0
+  - Regex: '^"build-'
+    Priority: 1
+  - Regex: '^"common-'
+    Priority: 2
+  - Regex: '^"gamemode-'
+    Priority: 3
+  - Regex: '^<'
+    Priority: 4
 # IncludeIsMainRegex: (project doesn't use a main includes that can add other includes via regex)
 IndentCaseLabels: false
 IndentWidth: 4
 IndentWrappedFunctionNames: false
-# JavaScriptQuotes: (not javascript)
 KeepEmptyLinesAtTheStartOfBlocks: false
 Language: Cpp
 MacroBlockBegin: ''
 MacroBlockEnd:   ''
 MaxEmptyLinesToKeep: 1
 NamespaceIndentation: None
-# ObjCBlockIndentWidth: (not objc)
-# ObjCSpaceAfterProperty: (not objc)
-# ObjCSpaceBeforeProtocolList: (not objc)
 PenaltyBreakBeforeFirstCallParameter: 400
 PenaltyBreakComment: 0
-# PenaltyBreakFirstLessLess: (not cpp)
 PenaltyBreakString: 500
 PenaltyExcessCharacter: 10000
 PenaltyReturnTypeOnItsOwnLine: 600
 PointerAlignment: Right
-#uncomment for clang 3.9
-#ReflowComments: true
-#uncomment for clang 3.9
-#SortIncludes: true
+ReflowComments: true
 SpaceAfterCStyleCast: false
 SpaceBeforeAssignmentOperators: true
 SpaceBeforeParens: ControlStatements
@@ -75,7 +73,6 @@ SpaceInEmptyParentheses: false
 SpacesBeforeTrailingComments: 1
 SpacesInAngles: false
 SpacesInCStyleCastParentheses: false
-# SpacesInContainerLiterals: (not objc or javascript)
 SpacesInParentheses: false
 SpacesInSquareBrackets: false
 Standard:        Cpp11

+ 43 - 125
README.md

@@ -6,59 +6,15 @@ GameMode was designed primarily as a stop-gap solution to problems with the Inte
 Currently GameMode includes support for optimisations including:
 * CPU governor
 * I/O priority
+* Process niceness
 * Kernel scheduler (`SCHED_ISO`)
 * Screensaver inhibiting
 * GPU performance mode (NVIDIA and AMD), GPU overclocking (NVIDIA)
 * Custom scripts
 
-Issues with GameMode should be reported here in the issues section, and not reported to Feral directly.
-
----
-## Building and installing [![Build Status](https://travis-ci.org/FeralInteractive/gamemode.svg?branch=master)](https://travis-ci.org/FeralInteractive/gamemode)
-
-*It is preferable to install GameMode with your package manager of choice, if available*. There are Ubuntu (Cosmic), Debian, Solus, AUR, Gentoo, Fedora and openSUSE packages available at the time of writing.
-
-### Install Dependencies
-GameMode depends on `meson` for building and `systemd` for internal communication. This repo contains a `bootstrap.sh` script to allow for quick install to the user bus, but check `meson_options.txt` for custom settings.
-
-#### Ubuntu/Debian (you may also need `dbus-user-session`)
-```bash
-apt install meson libsystemd-dev pkg-config ninja-build git libdbus1-dev
-```
-#### Arch
-```bash
-pacman -S meson systemd git dbus
-```
-#### Fedora
-```bash
-dnf install meson systemd-devel pkg-config git dbus-devel
-```
-#### Gentoo
-Gentoo has an ebuild which builds a stable release from sources. It will also pull in all the dependencies so you can work on the source code.
-```bash
-emerge --ask games-util/gamemode
-```
-You can also install using the latest sources from git:
-```bash
-ACCEPT_KEYWORDS="**" emerge --ask ~games-util/gamemode-9999
-```
-
-### Build and Install GameMode
-Then clone, build and install a release version of GameMode at 1.3.1:
-
-```bash
-git clone https://github.com/FeralInteractive/gamemode.git
-cd gamemode
-git checkout 1.3.1 # omit to build the master branch
-./bootstrap.sh
-```
+GameMode packages are available for Ubuntu, Debian, Solus, the AUR, Gentoo, Fedora, OpenSUSE and possibly more.
 
-To uninstall:
-```bash
-systemctl --user stop gamemoded.service
-cd build/
-ninja uninstall
-```
+Issues with GameMode should be reported here in the issues section, and not reported to Feral directly.
 
 ---
 ## Requesting GameMode
@@ -78,12 +34,12 @@ Note: for older versions of GameMode (before 1.3) use this string in place of `g
 ```
 LD_PRELOAD="$LD_PRELOAD:/usr/\$LIB/libgamemodeauto.so.0"
 ```
-Please note the backslash here in `\$LIB` is required.
+**Please note the backslash here in `\$LIB` is required.**
 
 ---
 ## Configuration
 
-The daemon can currently be configured using a `gamemode.ini` file. [gamemode.ini](https://github.com/FeralInteractive/gamemode/blob/master/example/gamemode.ini) is an example of what this file would look like, with explanations for all the variables.
+The daemon is configured with a `gamemode.ini` file. [example/gamemode.ini](https://github.com/FeralInteractive/gamemode/blob/master/example/gamemode.ini) is an example of what this file would look like, with explanations for all the variables.
 
 Config files are loaded and merged from the following directories, in order:
 1. `/usr/share/gamemode/`
@@ -91,38 +47,6 @@ Config files are loaded and merged from the following directories, in order:
 3. `$XDG_CONFIG_HOME` or `$HOME/.config/`
 4. `$PWD`
 
-The file parsing uses [inih](https://github.com/benhoyt/inih).
-
----
-## Features
-
-### Scheduling
-GameMode can leverage support for soft real time mode if the running kernel supports `SCHED_ISO` (not currently supported in upstream kernels), controlled by the `softrealtime` option. This adjusts the scheduling of the game to real time without sacrificing system stability by starving other processes.
-
-GameMode can adjust the nice priority of games to give them a slight IO and CPU priority over other background processes, controlled by the `renice` option. This only works if your user is permitted to adjust priorities within the limits configured by PAM. GameMode can be configured to take care of it by passing `with-pam-group=group` to the build options where `group` is a group your user needs to be part of.
-For more information, see `/etc/security/limits.conf`.
-
-Please take note that some games may actually run seemingly slower with `SCHED_ISO` if the game makes use of busy looping while interacting with the graphic driver. The same may happen if you apply too strong nice values. This effect is called priority inversion: Due to the high priority given to busy loops, there may be too few resources left for the graphics driver. Thus, sane defaults were chosen to not expose this effect on most systems. Part of this default is a heuristic which automatically turns off `SCHED_ISO` if GameMode detects three or less CPU cores. Your experience may change based on using GL threaded optimizations, CPU core binding (taskset), the graphic driver, or different CPU architectures. If you experience bad input latency or inconsistent FPS, try switching these configurations on or off first and report back. `SCHED_ISO` comes with a protection against this effect by falling back to normal scheduling as soon as the `SCHED_ISO` process uses more than 70% avarage across all CPU cores. This default value can be adjusted outside of the scope of GameMode (it's in `/proc/sys/kernel/iso_cpu`). This value also protects against compromising system stability, do not set it to 100% as this would turn the game into a full real time process, thus potentially starving all other OS components from CPU resources.
-
-### IO priority
-GameMode can adjust the I/O priority of games to benefit from reduced lag and latency when a game has to load assets on demand. This is done by default.
-
-### For those with overclocked CPUs
-If you have an AMD CPU and have disabled Cool'n'Quiet, or you have an Intel CPU and have disabled SpeedStep, then GameMode's governor settings will not work, as your CPU is not running with a governor. You are already getting maximum performance.
-
-If you are unsure, `bootstrap.sh` will warn you if your system lacks CPU governor control.
-
-Scripts and other features will still work.
-
-### GPU optimisations
-GameMode is able to automatically apply GPU performance mode changes on AMD and NVIDIA, and overclocking on NVIDIA, when activated. AMD support currently requires the `amdgpu` kernel module, and NVIDIA requires the `coolbits` extension to be enabled in the NVIDIA settings.
-
-It is very much encouraged for users to find out their own overclocking limits manually before venturing into configuring them in GameMode, and activating this feature in GameMode assumes you take responsibility for the effects of said overclocks.
-
-More information can be found in the `example/gamemode.ini` file.
-
-Note that both NVIDIA (GPUBoost) and AMD (Overdrive) devices and drivers already attempt to internally overclock if possible, but it is still common for enthusiasts to want to manually push the upper threshold.
-
 ---
 ## Apps with GameMode integration
 
@@ -138,57 +62,53 @@ Other apps which can integrate with GameMode include:
 * GNOME Shell ([via extension](https://github.com/gicmo/gamemode-extension)) - indicates when GameMode is active in the top panel.
 
 ---
-## Developers
+## Development [![Build Status](https://travis-ci.org/FeralInteractive/gamemode.svg?branch=master)](https://travis-ci.org/FeralInteractive/gamemode)
 
 The design of GameMode has a clear-cut abstraction between the host daemon and library (`gamemoded` and `libgamemode`), and the client loaders (`libgamemodeauto` and `gamemode_client.h`) that allows for safe use without worrying about whether the daemon is installed or running. This design also means that while the host library currently relies on `systemd` for exchanging messages with the daemon, it's entirely possible to implement other internals that still work with the same clients.
 
-### Components
-**gamemoded** runs in the background, activates game mode on request, refcounts and also checks caller PID lifetime. Run `man gamemoded` for command line options.
-
-**libgamemode** is an internal library used to dispatch requests to the daemon. Note: `libgamemode` should never be linked with directly.
+See repository subdirectories for information on each component.
 
-**libgamemodeauto** is a simple dynamic library that automatically requests game mode when loaded. Useful to `LD_PRELOAD` into any game as needed.
-
-**gamemode\_client.h** is as single header lib that lets a game request game mode and handle errors.
-
-### Integration
-Developers can integrate the request directly into an app. Note that none of these client methods force your users to have the daemon installed or running - they will safely no-op if the host is missing.
-
-```C
-// Manually with error checking
-#include "gamemode_client.h"
-
-	if( gamemode_request_start() < 0 ) {
-		fprintf( stderr, "gamemode request failed: %s\n", gamemode_error_string() );
-	}
-
-	/* run game... */
+### Install Dependencies
+GameMode depends on `meson` for building and `systemd` for internal communication. This repo contains a `bootstrap.sh` script to allow for quick install to the user bus, but check `meson_options.txt` for custom settings.
 
-	gamemode_request_end(); // Not required, gamemoded can clean up after game exits
+#### Ubuntu/Debian (you may also need `dbus-user-session`)
+```bash
+apt install meson libsystemd-dev pkg-config ninja-build git libdbus1-dev
 ```
-
-```C
-// Automatically on program start and finish
-#define GAMEMODE_AUTO
-#include "gamemode_client.h"
+#### Arch
+```bash
+pacman -S meson systemd git dbus
+```
+#### Fedora
+```bash
+dnf install meson systemd-devel pkg-config git dbus-devel
+```
+#### Gentoo
+Gentoo has an ebuild which builds a stable release from sources. It will also pull in all the dependencies so you can work on the source code.
+```bash
+emerge --ask games-util/gamemode
+```
+You can also install using the latest sources from git:
+```bash
+ACCEPT_KEYWORDS="**" emerge --ask ~games-util/gamemode-9999
 ```
 
-Or, distribute `libgamemodeauto.so` and either add `-lgamemodeauto` to your linker arguments, or add it to an LD\_PRELOAD in a launch script.
-
-### Supervisor support
-Developers can also create apps that manage GameMode on the system, for other processes:
-
-```C
-#include "gamemode_client.h"
+### Build and Install GameMode
+Then clone, build and install a release version of GameMode at 1.3.1:
 
-	gamemode_request_start_for(gamePID);
-	gamemode_request_end_for(gamePID);
+```bash
+git clone https://github.com/FeralInteractive/gamemode.git
+cd gamemode
+git checkout 1.3.1 # omit to build the master branch
+./bootstrap.sh
 ```
 
-This functionality can also be controlled in the config file in the `supervisor` section.
-
----
-## Contributions
+To uninstall:
+```bash
+systemctl --user stop gamemoded.service
+cd build/
+ninja uninstall
+```
 
 ### Pull Requests
 Pull requests must match with the coding style found in the `.clang-format` file, please run this before committing:
@@ -196,10 +116,6 @@ Pull requests must match with the coding style found in the `.clang-format` file
 clang-format -i $(find . -name '*.[ch]' -not -path "*subprojects/*")
 ```
 
-### Planned Features
-* Additional mode-switch plugins
-* Improved client state tracking (PID is unreliable)
-
 ### Maintained by
 Feral Interactive
 
@@ -211,3 +127,5 @@ See the [contributors](https://github.com/FeralInteractive/gamemode/graphs/contr
 Copyright © 2017-2019 Feral Interactive
 
 GameMode is available under the terms of the BSD 3-Clause License (Revised)
+
+The "inih" library is distributed under the New BSD license

+ 2 - 6
daemon/external-helper.c → common/common-external.c

@@ -31,13 +31,9 @@ POSSIBILITY OF SUCH DAMAGE.
 
 #define _GNU_SOURCE
 
-#include "external-helper.h"
-#include "logging.h"
+#include "common-external.h"
+#include "common-logging.h"
 
-#include <linux/limits.h>
-#include <stdio.h>
-#include <sys/time.h>
-#include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>
 

+ 0 - 0
daemon/external-helper.h → common/common-external.h


+ 2 - 4
daemon/governors-query.c → common/common-governors.c

@@ -31,13 +31,11 @@ POSSIBILITY OF SUCH DAMAGE.
 
 #define _GNU_SOURCE
 
-#include "governors-query.h"
-#include "logging.h"
+#include "common-governors.h"
+#include "common-logging.h"
 
 #include <assert.h>
 #include <glob.h>
-#include <stdio.h>
-#include <string.h>
 
 /**
  * Discover all governers on the system.

+ 0 - 0
daemon/governors-query.h → common/common-governors.h


+ 2 - 4
daemon/gpu-control.c → common/common-gpu.c

@@ -28,10 +28,8 @@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.
 
  */
-#include "gpu-control.h"
-#include "logging.h"
-
-#include <stdio.h>
+#include "common-gpu.h"
+#include "common-logging.h"
 
 /* Get the vendor for a device */
 enum GPUVendor gamemode_get_gpu_vendor(long device)

+ 2 - 2
daemon/gpu-control.h → common/common-gpu.h

@@ -30,7 +30,7 @@ POSSIBILITY OF SUCH DAMAGE.
  */
 
 #pragma once
-#include "daemon_config.h"
+#define GPU_VALUE_MAX 256
 
 /* Enums for GPU vendors */
 enum GPUVendor {
@@ -52,7 +52,7 @@ struct GameModeGPUInfo {
 	long nv_mem;             /* Nvidia mem clock */
 	long nv_powermizer_mode; /* NV Powermizer Mode */
 
-	char amd_performance_level[CONFIG_VALUE_MAX]; /* The AMD performance level set to */
+	char amd_performance_level[GPU_VALUE_MAX]; /* The AMD performance level set to */
 };
 
 /* Get the vendor for a device */

+ 1 - 1
daemon/helpers.c → common/common-helpers.c

@@ -31,7 +31,7 @@ POSSIBILITY OF SUCH DAMAGE.
  */
 
 #define _GNU_SOURCE
-#include "helpers.h"
+#include "common-helpers.h"
 
 /* Starting with C99 we can use "inline" without "static" and thus avoid
  * having multiple (local) definitions of the same inline function. One

+ 0 - 1
daemon/helpers.h → common/common-helpers.h

@@ -31,7 +31,6 @@ POSSIBILITY OF SUCH DAMAGE.
 
 #pragma once
 
-#include <stdio.h>
 #include <string.h>
 #include <sys/param.h>
 #include <unistd.h>

+ 2 - 1
daemon/logging.c → common/common-logging.c

@@ -29,7 +29,8 @@ POSSIBILITY OF SUCH DAMAGE.
 
  */
 
-#include "logging.h"
+#include "common-logging.h"
+
 #include "syslog.h"
 
 static bool use_syslog = false;

+ 0 - 1
daemon/logging.h → common/common-logging.h

@@ -37,7 +37,6 @@ POSSIBILITY OF SUCH DAMAGE.
 #include <stdlib.h>
 #include <string.h>
 #include <syslog.h>
-#include <unistd.h>
 
 /* Macros to help with basic logging */
 #define PLOG_MSG(...) printf(__VA_ARGS__)

+ 20 - 0
common/meson.build

@@ -0,0 +1,20 @@
+# Convenience library for the duplicated logging functionality
+common_sources = [
+    'common-logging.c',
+    'common-governors.c',
+    'common-external.c',
+    'common-helpers.c',
+    'common-gpu.c',
+]
+
+daemon_common = static_library(
+    'daemon-common',
+    sources: common_sources,
+    install: false,
+)
+
+link_daemon_common = declare_dependency(
+    link_with: daemon_common,
+)
+
+include_daemon_common = include_directories('.')

+ 49 - 0
daemon/README.md

@@ -0,0 +1,49 @@
+### gamemoded
+**gamemoded** is a daemon that runs in the background, activates system and program optimisations on request, refcounts and also checks caller lifetime.
+
+**gamemoded** currently supports the current arguments:
+```
+Usage: gamemoded [-d] [-l] [-r] [-t] [-h] [-v]
+
+  -r[PID], --request=[PID] Toggle gamemode for process
+                           When no PID given, requests gamemode and pauses
+  -s[PID], --status=[PID]  Query the status of gamemode for process
+                           When no PID given, queries the status globally
+  -d, --daemonize          Daemonize self after launch
+  -l, --log-to-syslog      Log to syslog
+  -r, --test               Run tests
+  -h, --help               Print this help
+  -v, --version            Print version
+```
+
+Run `man gamemoded` for information and options.
+
+---
+## Daemon Features
+
+### Scheduling
+GameMode can leverage support for soft real time mode if the running kernel supports `SCHED_ISO` (not currently supported in upstream kernels), controlled by the `softrealtime` option. This adjusts the scheduling of the game to real time without sacrificing system stability by starving other processes.
+
+GameMode can adjust the nice priority of games to give them a slight IO and CPU priority over other background processes, controlled by the `renice` option. This only works if your user is permitted to adjust priorities within the limits configured by PAM. GameMode can be configured to take care of it by passing `with-pam-group=group` to the build options where `group` is a group your user needs to be part of.
+For more information, see `/etc/security/limits.conf`.
+
+Please take note that some games may actually run seemingly slower with `SCHED_ISO` if the game makes use of busy looping while interacting with the graphic driver. The same may happen if you apply too strong nice values. This effect is called priority inversion: Due to the high priority given to busy loops, there may be too few resources left for the graphics driver. Thus, sane defaults were chosen to not expose this effect on most systems. Part of this default is a heuristic which automatically turns off `SCHED_ISO` if GameMode detects three or less CPU cores. Your experience may change based on using GL threaded optimizations, CPU core binding (taskset), the graphic driver, or different CPU architectures. If you experience bad input latency or inconsistent FPS, try switching these configurations on or off first and report back. `SCHED_ISO` comes with a protection against this effect by falling back to normal scheduling as soon as the `SCHED_ISO` process uses more than 70% avarage across all CPU cores. This default value can be adjusted outside of the scope of GameMode (it's in `/proc/sys/kernel/iso_cpu`). This value also protects against compromising system stability, do not set it to 100% as this would turn the game into a full real time process, thus potentially starving all other OS components from CPU resources.
+
+### IO priority
+GameMode can adjust the I/O priority of games to benefit from reduced lag and latency when a game has to load assets on demand. This is done by default.
+
+### For those with overclocked CPUs
+If you have an AMD CPU and have disabled Cool'n'Quiet, or you have an Intel CPU and have disabled SpeedStep, then GameMode's governor settings will not work, as your CPU is not running with a governor. You are already getting maximum performance.
+
+If you are unsure, `bootstrap.sh` will warn you if your system lacks CPU governor control.
+
+Scripts and other features will still work.
+
+### GPU optimisations
+GameMode is able to automatically apply GPU performance mode changes on AMD and NVIDIA, and overclocking on NVIDIA, when activated. AMD support currently requires the `amdgpu` kernel module, and NVIDIA requires the `coolbits` extension to be enabled in the NVIDIA settings.
+
+It is very much encouraged for users to find out their own overclocking limits manually before venturing into configuring them in GameMode, and activating this feature in GameMode assumes you take responsibility for the effects of said overclocks.
+
+More information can be found in the `example/gamemode.ini` file.
+
+Note that both NVIDIA (GPUBoost) and AMD (Overdrive) devices and drivers already attempt to internally overclock if possible, but it is still common for enthusiasts to want to manually push the upper threshold.

+ 0 - 81
daemon/daemonize.c

@@ -1,81 +0,0 @@
-/*
-
-Copyright (c) 2017-2019, 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.
-
- */
-#include "daemonize.h"
-#include "logging.h"
-
-#include <fcntl.h>
-#include <stdlib.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-/**
- * Helper to perform standard UNIX daemonization
- */
-void daemonize(const char *name)
-{
-	/* Initial fork */
-	pid_t pid = fork();
-	if (pid < 0) {
-		FATAL_ERRORNO("Failed to fork");
-	}
-
-	if (pid != 0) {
-		LOG_MSG("Daemon launched as %s...\n", name);
-		exit(EXIT_SUCCESS);
-	}
-
-	/* Fork a second time */
-	pid = fork();
-	if (pid < 0) {
-		FATAL_ERRORNO("Failed to fork");
-	} else if (pid > 0) {
-		exit(EXIT_SUCCESS);
-	}
-
-	/* Now continue execution */
-	umask(0022);
-	if (setsid() < 0) {
-		FATAL_ERRORNO("Failed to create process group\n");
-	}
-	if (chdir("/") < 0) {
-		FATAL_ERRORNO("Failed to change to root directory\n");
-	}
-
-	/* replace standard file descriptors by /dev/null */
-	int devnull_r = open("/dev/null", O_RDONLY);
-	int devnull_w = open("/dev/null", O_WRONLY);
-	dup2(devnull_r, STDIN_FILENO);
-	dup2(devnull_w, STDOUT_FILENO);
-	dup2(devnull_w, STDERR_FILENO);
-	close(devnull_r);
-	close(devnull_w);
-}

+ 0 - 38
daemon/daemonize.h

@@ -1,38 +0,0 @@
-/*
-
-Copyright (c) 2017-2019, 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.
-
- */
-
-#pragma once
-
-/**
- * Attempt daemonization of the process.
- * If this fails, the process will exit
- */
-void daemonize(const char *name);

+ 0 - 51
daemon/dbus_messaging.h

@@ -1,51 +0,0 @@
-/*
-
-Copyright (c) 2017-2019, 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.
-
- */
-
-#pragma once
-
-#include <stdbool.h>
-
-#include "gamemode.h"
-
-/**
- * Run the main D-BUS loop "forever"
- */
-void game_mode_context_loop(GameModeContext *context) __attribute__((noreturn));
-
-/**
- * Inhibit the screensaver
- */
-int game_mode_inhibit_screensaver(bool inhibit);
-
-/**
- * Signal the ClientCount property has changed
- */
-void game_mode_client_count_changed(void);

+ 5 - 8
daemon/daemon_config.c → daemon/gamemode-config.c

@@ -30,22 +30,19 @@ POSSIBILITY OF SUCH DAMAGE.
  */
 #define _GNU_SOURCE
 
-#include "daemon_config.h"
-#include "helpers.h"
-#include "logging.h"
+#include "gamemode-config.h"
+
+#include "common-helpers.h"
+#include "common-logging.h"
 
 /* Ben Hoyt's inih library */
 #include "ini.h"
 
 #include <dirent.h>
-#include <linux/limits.h>
 #include <pthread.h>
 #include <pwd.h>
-#include <stdio.h>
-#include <string.h>
 #include <sys/inotify.h>
 #include <sys/stat.h>
-#include <sys/types.h>
 
 /* Name and possible location of the config file */
 #define CONFIG_NAME "gamemode.ini"
@@ -437,7 +434,7 @@ void config_init(GameModeConfig *self)
 	self->inotfd = inotify_init1(IN_NONBLOCK);
 	if (self->inotfd == -1)
 		LOG_ERROR(
-		    "inotify_init failed: %s, gamemode will be able to watch config files for edits!\n",
+		    "inotify_init failed: %s, gamemode will not be able to watch config files for edits!\n",
 		    strerror(errno));
 
 	for (unsigned int i = 0; i < CONFIG_NUM_LOCATIONS; i++) {

+ 6 - 41
daemon/daemon_config.h → daemon/gamemode-config.h

@@ -80,65 +80,30 @@ bool config_needs_reload(GameModeConfig *self);
 void config_destroy(GameModeConfig *self);
 
 /*
- * Get if the client is in the whitelist
- * returns false for an empty whitelist
+ * Get if the client is in the whitelist or blacklist
+ * config_get_client_whitelisted returns false for an empty whitelist
  */
 bool config_get_client_whitelisted(GameModeConfig *self, const char *client);
-
-/*
- * Get if the client is in the blacklist
- */
 bool config_get_client_blacklisted(GameModeConfig *self, const char *client);
 
 /*
- * Get the frequency (in seconds) for the reaper thread
- */
-long config_get_reaper_frequency(GameModeConfig *self);
-
-/*
- * Get whether we want to inhibit the screensaver (defaults to true)
- */
-bool config_get_inhibit_screensaver(GameModeConfig *self);
-
-/*
- * Get a set of scripts to call when gamemode starts
+ * Get the script sets to run at the start or end
  */
 void config_get_gamemode_start_scripts(GameModeConfig *self,
                                        char scripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX]);
-/*
- * Get a set of scripts to call when gamemode ends
- */
 void config_get_gamemode_end_scripts(GameModeConfig *self,
                                      char scripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX]);
 
 /*
- * Get the script timout value
+ * Various get methods for config values
  */
+long config_get_reaper_frequency(GameModeConfig *self);
+bool config_get_inhibit_screensaver(GameModeConfig *self);
 long config_get_script_timeout(GameModeConfig *self);
-
-/*
- * Get the chosen default governor
- */
 void config_get_default_governor(GameModeConfig *self, char governor[CONFIG_VALUE_MAX]);
-
-/*
- * Get the chosen desired governor
- */
 void config_get_desired_governor(GameModeConfig *self, char governor[CONFIG_VALUE_MAX]);
-
-/*
- * Get the chosen soft realtime behavior
- */
 void config_get_soft_realtime(GameModeConfig *self, char softrealtime[CONFIG_VALUE_MAX]);
-
-/*
- * Get the renice value
- */
 long config_get_renice_value(GameModeConfig *self);
-
-/*
- * Get the ioprio value
- */
 long config_get_ioprio_value(GameModeConfig *self);
 
 /*

+ 16 - 42
daemon/gamemode.c → daemon/gamemode-context.c

@@ -31,22 +31,20 @@ POSSIBILITY OF SUCH DAMAGE.
 
 #define _GNU_SOURCE
 
+#include "common-external.h"
+#include "common-governors.h"
+#include "common-helpers.h"
+#include "common-logging.h"
+
 #include "gamemode.h"
-#include "config.h"
-#include "daemon_config.h"
-#include "dbus_messaging.h"
-#include "external-helper.h"
-#include "governors-query.h"
-#include "helpers.h"
-#include "logging.h"
+#include "gamemode-config.h"
+
+#include "build-config.h"
 
 #include <fcntl.h>
 #include <pthread.h>
-#include <signal.h>
 #include <stdatomic.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <systemd/sd-daemon.h>
+#include <systemd/sd-daemon.h> /* TODO: Move usage to gamemode-dbus.c */
 #include <unistd.h>
 
 /**
@@ -82,9 +80,6 @@ struct GameModeContext {
 
 static GameModeContext instance = { 0 };
 
-/* Maximum number of concurrent processes we'll sanely support */
-#define MAX_GAMES 256
-
 /**
  * Protect against signals
  */
@@ -387,12 +382,6 @@ int game_mode_context_register(GameModeContext *self, pid_t client, pid_t reques
 		goto error_cleanup;
 	}
 
-	/* Cap the total number of active clients */
-	if (game_mode_context_num_clients(self) + 1 > MAX_GAMES) {
-		LOG_ERROR("Max games (%d) reached, not registering %d\n", MAX_GAMES, client);
-		goto error_cleanup;
-	}
-
 	/* Check the PID first to spare a potentially expensive lookup for the exe */
 	pthread_rwlock_rdlock(&self->rwlock); // ensure our pointer is sane
 	const GameModeClient *existing = game_mode_context_has_client(self, client);
@@ -613,10 +602,13 @@ int game_mode_context_query_status(GameModeContext *self, pid_t client, pid_t re
  */
 static GameModeClient *game_mode_client_new(pid_t pid, char *executable)
 {
+	/* This bit seems to be formatted differently by different clang-format versions */
+	/* clang-format off */
 	GameModeClient c = {
 		.next = NULL,
 		.pid = pid,
 	};
+	/* clang-format on */
 	GameModeClient *ret = NULL;
 
 	ret = calloc(1, sizeof(struct GameModeClient));
@@ -757,31 +749,13 @@ static char *game_mode_context_find_exe(pid_t pid)
 
 	char *exe = strdup(buffer);
 
-	/* Detect if the process is a wine loader process */
-	if (game_mode_detect_wine_preloader(exe)) {
-		LOG_MSG("Detected wine preloader for client %d [%s].\n", pid, exe);
-		goto wine_preloader;
-	}
-	if (game_mode_detect_wine_loader(exe)) {
-		LOG_MSG("Detected wine loader for client %d [%s].\n", pid, exe);
-		goto wine_preloader;
-	}
-
-	return exe;
-
-wine_preloader:
-
-	wine_exe = game_mode_resolve_wine_preloader(pid);
-	if (wine_exe) {
+	/* Resolve for wine if appropriate */
+	if ((wine_exe = game_mode_resolve_wine_preloader(exe, pid))) {
 		free(exe);
 		exe = wine_exe;
-		return exe;
 	}
 
-	/* We have to ignore this because the wine process is in some sort
-	 * of respawn mode
-	 */
-	free(exe);
+	return exe;
 
 fail:
 	if (errno != 0) // otherwise a proper message was logged before
@@ -823,4 +797,4 @@ int game_mode_reload_config(GameModeContext *self)
 	start_reaper_thread(self);
 
 	return 0;
-}
+}

+ 20 - 20
daemon/dbus_messaging.c → daemon/gamemode-dbus.c

@@ -31,12 +31,8 @@ POSSIBILITY OF SUCH DAMAGE.
 
 #define _GNU_SOURCE
 
-#include "dbus_messaging.h"
-#include "daemonize.h"
 #include "gamemode.h"
-#include "logging.h"
-
-#include <stdlib.h>
+#include "common-logging.h"
 
 #include <systemd/sd-bus.h>
 #include <systemd/sd-daemon.h>
@@ -220,21 +216,25 @@ static int method_refresh_config(sd_bus_message *m, void *userdata,
 /**
  * D-BUS vtable to dispatch virtual methods
  */
-static const sd_bus_vtable gamemode_vtable[] =
-    { SD_BUS_VTABLE_START(0),
-	  SD_BUS_PROPERTY("ClientCount", "i", property_get_client_count, 0,
-	                  SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
-	  SD_BUS_METHOD("RegisterGame", "i", "i", method_register_game, SD_BUS_VTABLE_UNPRIVILEGED),
-	  SD_BUS_METHOD("UnregisterGame", "i", "i", method_unregister_game, SD_BUS_VTABLE_UNPRIVILEGED),
-	  SD_BUS_METHOD("QueryStatus", "i", "i", method_query_status, SD_BUS_VTABLE_UNPRIVILEGED),
-	  SD_BUS_METHOD("RegisterGameByPID", "ii", "i", method_register_game_by_pid,
-	                SD_BUS_VTABLE_UNPRIVILEGED),
-	  SD_BUS_METHOD("UnregisterGameByPID", "ii", "i", method_unregister_game_by_pid,
-	                SD_BUS_VTABLE_UNPRIVILEGED),
-	  SD_BUS_METHOD("QueryStatusByPID", "ii", "i", method_query_status_by_pid,
-	                SD_BUS_VTABLE_UNPRIVILEGED),
-	  SD_BUS_METHOD("RefreshConfig", "", "i", method_refresh_config, SD_BUS_VTABLE_UNPRIVILEGED),
-	  SD_BUS_VTABLE_END };
+/* This bit seems to be formatted differently by different clang-format versions */
+/* clang-format off */
+static const sd_bus_vtable gamemode_vtable[] = {
+	SD_BUS_VTABLE_START(0),
+	SD_BUS_PROPERTY("ClientCount", "i", property_get_client_count, 0,
+	                SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+	SD_BUS_METHOD("RegisterGame", "i", "i", method_register_game, SD_BUS_VTABLE_UNPRIVILEGED),
+	SD_BUS_METHOD("UnregisterGame", "i", "i", method_unregister_game, SD_BUS_VTABLE_UNPRIVILEGED),
+	SD_BUS_METHOD("QueryStatus", "i", "i", method_query_status, SD_BUS_VTABLE_UNPRIVILEGED),
+	SD_BUS_METHOD("RegisterGameByPID", "ii", "i", method_register_game_by_pid,
+	              SD_BUS_VTABLE_UNPRIVILEGED),
+	SD_BUS_METHOD("UnregisterGameByPID", "ii", "i", method_unregister_game_by_pid,
+	              SD_BUS_VTABLE_UNPRIVILEGED),
+	SD_BUS_METHOD("QueryStatusByPID", "ii", "i", method_query_status_by_pid,
+	              SD_BUS_VTABLE_UNPRIVILEGED),
+	SD_BUS_METHOD("RefreshConfig", "", "i", method_refresh_config, SD_BUS_VTABLE_UNPRIVILEGED),
+	SD_BUS_VTABLE_END
+};
+/* clang-format on */
 
 /**
  * Main process loop for the daemon. Run until quitting has been requested.

+ 0 - 96
daemon/gamemode-env.c

@@ -1,96 +0,0 @@
-/*
-
-Copyright (c) 2017-2019, 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 <fcntl.h>
-#include <pwd.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-/**
- * Lookup the process environment for a specific variable or return NULL.
- * Requires an open directory FD from /proc/PID.
- */
-char *game_mode_lookup_proc_env(const procfd_t proc_fd, const char *var)
-{
-	char *environ = NULL;
-
-	int fd = openat(proc_fd, "environ", O_RDONLY | O_CLOEXEC);
-	if (fd != -1) {
-		FILE *stream = fdopen(fd, "r");
-		if (stream) {
-			/* Read every \0 terminated line from the environment */
-			char *line = NULL;
-			size_t len = 0;
-			size_t pos = strlen(var) + 1;
-			while (!environ && (getdelim(&line, &len, 0, stream) != -1)) {
-				/* Find a match including the "=" suffix */
-				if ((len > pos) && (strncmp(line, var, strlen(var)) == 0) && (line[pos - 1] == '='))
-					environ = strndup(line + pos, len - pos);
-			}
-			free(line);
-			fclose(stream);
-		} else
-			close(fd);
-	}
-
-	/* If found variable is empty, skip it */
-	if (environ && !strlen(environ)) {
-		free(environ);
-		environ = NULL;
-	}
-
-	return environ;
-}
-
-/**
- * Lookup the home directory of the user in a safe way.
- */
-char *game_mode_lookup_user_home(void)
-{
-	/* Try loading env HOME first */
-	const char *home = secure_getenv("HOME");
-	if (!home) {
-		/* If HOME is not defined (or out of context), fall back to passwd */
-		struct passwd *pw = getpwuid(getuid());
-		if (!pw)
-			return NULL;
-		home = pw->pw_dir;
-	}
-
-	/* Try to allocate into our heap */
-	return home ? strdup(home) : NULL;
-}

+ 8 - 11
daemon/gamemode-gpu.c

@@ -32,15 +32,17 @@ POSSIBILITY OF SUCH DAMAGE.
 
 #define _GNU_SOURCE
 
-#include "config.h"
-#include "external-helper.h"
-#include "helpers.h"
-#include "logging.h"
+#include "common-external.h"
+#include "common-gpu.h"
+#include "common-helpers.h"
+#include "common-logging.h"
 
 #include "gamemode.h"
+#include "gamemode-config.h"
 
-#include "daemon_config.h"
-#include "gpu-control.h"
+#include "build-config.h"
+
+_Static_assert(CONFIG_VALUE_MAX == GPU_VALUE_MAX, "Config max value and GPU value out of sync!");
 
 /**
  * Attempts to identify the current in use GPU information
@@ -141,11 +143,6 @@ void game_mode_free_gpu(GameModeGPUInfo **info)
 	*info = NULL;
 }
 
-//#include <linux/limits.h>
-//#include <stdio.h>
-//#include <sys/wait.h>
-//#include <unistd.h>
-
 /**
  * Applies GPU optimisations when gamemode is active and removes them after
  */

+ 3 - 6
daemon/gamemode-ioprio.c

@@ -31,16 +31,13 @@ POSSIBILITY OF SUCH DAMAGE.
 
 #define _GNU_SOURCE
 
-#include "daemon_config.h"
 #include "gamemode.h"
-#include "helpers.h"
-#include "logging.h"
+#include "common-helpers.h"
+#include "common-logging.h"
+#include "gamemode-config.h"
 
 #include <dirent.h>
-#include <errno.h>
-#include <stdio.h>
 #include <sys/syscall.h>
-#include <unistd.h>
 
 /**
  * Define the syscall interface in Linux because it is missing from glibc

+ 0 - 61
daemon/gamemode-proc.c

@@ -1,61 +0,0 @@
-/*
-
-Copyright (c) 2017-2019, 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 <fcntl.h>
-#include <linux/limits.h>
-#include <unistd.h>
-
-/**
- * Opens the process environment for a specific PID and returns
- * a file descriptor to the directory /proc/PID. Doing it that way prevents
- * the directory going MIA when a process exits while we are looking at it
- * and allows us to handle fewer error cases.
- */
-procfd_t game_mode_open_proc(const pid_t pid)
-{
-	char buffer[PATH_MAX];
-	const char *proc_path = buffered_snprintf(buffer, "/proc/%d", pid);
-
-	return proc_path ? open(proc_path, O_RDONLY | O_CLOEXEC) : INVALID_PROCFD;
-}
-
-/**
- * Closes the process environment.
- */
-int game_mode_close_proc(const procfd_t procfd)
-{
-	return close(procfd);
-}

+ 2 - 5
daemon/gamemode-sched.c

@@ -31,15 +31,12 @@ POSSIBILITY OF SUCH DAMAGE.
 
 #define _GNU_SOURCE
 
-#include "daemon_config.h"
 #include "gamemode.h"
-#include "logging.h"
+#include "common-logging.h"
+#include "gamemode-config.h"
 
 #include <dirent.h>
-#include <errno.h>
 #include <sched.h>
-#include <stdio.h>
-#include <string.h>
 #include <sys/resource.h>
 #include <sys/sysinfo.h>
 

+ 9 - 10
daemon/gamemode-tests.c

@@ -31,22 +31,21 @@ POSSIBILITY OF SUCH DAMAGE.
 
 #define _GNU_SOURCE
 
+#include "common-external.h"
+#include "common-governors.h"
+#include "common-gpu.h"
+#include "common-helpers.h"
+#include "common-logging.h"
+
 #include "gamemode.h"
-#include "helpers.h"
-#include "logging.h"
+#include "gamemode-config.h"
+#include "gamemode_client.h"
 
-#include <libgen.h>
 #include <pthread.h>
 #include <sys/syscall.h>
-#include <sys/types.h>
 #include <sys/wait.h>
-#include <unistd.h>
 
-#include "daemon_config.h"
-#include "external-helper.h"
-#include "gamemode_client.h"
-#include "governors-query.h"
-#include "gpu-control.h"
+struct GameModeConfig;
 
 /* Initial verify step to ensure gamemode isn't already active */
 static int verify_gamemode_initial(struct GameModeConfig *config)

+ 90 - 7
daemon/gamemode-wine.c

@@ -32,18 +32,17 @@ POSSIBILITY OF SUCH DAMAGE.
 #define _GNU_SOURCE
 
 #include "gamemode.h"
-#include "helpers.h"
-#include "logging.h"
+#include "common-helpers.h"
+#include "common-logging.h"
 
 #include <ctype.h>
 #include <fcntl.h>
-#include <stdlib.h>
-#include <unistd.h>
+#include <pwd.h>
 
 /**
  * Detect if the process is a wine preloader process
  */
-bool game_mode_detect_wine_preloader(const char *exe)
+static bool game_mode_detect_wine_preloader(const char *exe)
 {
 	return (strtail(exe, "/wine-preloader") || strtail(exe, "/wine64-preloader"));
 }
@@ -51,18 +50,102 @@ bool game_mode_detect_wine_preloader(const char *exe)
 /**
  * Detect if the process is a wine loader process
  */
-bool game_mode_detect_wine_loader(const char *exe)
+static bool game_mode_detect_wine_loader(const char *exe)
 {
 	return (strtail(exe, "/wine") || strtail(exe, "/wine64"));
 }
 
+/**
+ * Opens the process environment for a specific PID and returns
+ * a file descriptor to the directory /proc/PID. Doing it that way prevents
+ * the directory going MIA when a process exits while we are looking at it
+ * and allows us to handle fewer error cases.
+ */
+static procfd_t game_mode_open_proc(const pid_t pid)
+{
+	char buffer[PATH_MAX];
+	const char *proc_path = buffered_snprintf(buffer, "/proc/%d", pid);
+
+	return proc_path ? open(proc_path, O_RDONLY | O_CLOEXEC) : INVALID_PROCFD;
+}
+
+/**
+ * Closes the process environment.
+ */
+static int game_mode_close_proc(const procfd_t procfd)
+{
+	return close(procfd);
+}
+
+/**
+ * Lookup the process environment for a specific variable or return NULL.
+ * Requires an open directory FD from /proc/PID.
+ */
+static char *game_mode_lookup_proc_env(const procfd_t proc_fd, const char *var)
+{
+	char *environ = NULL;
+
+	int fd = openat(proc_fd, "environ", O_RDONLY | O_CLOEXEC);
+	if (fd != -1) {
+		FILE *stream = fdopen(fd, "r");
+		if (stream) {
+			/* Read every \0 terminated line from the environment */
+			char *line = NULL;
+			size_t len = 0;
+			size_t pos = strlen(var) + 1;
+			while (!environ && (getdelim(&line, &len, 0, stream) != -1)) {
+				/* Find a match including the "=" suffix */
+				if ((len > pos) && (strncmp(line, var, strlen(var)) == 0) && (line[pos - 1] == '='))
+					environ = strndup(line + pos, len - pos);
+			}
+			free(line);
+			fclose(stream);
+		} else
+			close(fd);
+	}
+
+	/* If found variable is empty, skip it */
+	if (environ && !strlen(environ)) {
+		free(environ);
+		environ = NULL;
+	}
+
+	return environ;
+}
+
+/**
+ * Lookup the home directory of the user in a safe way.
+ */
+static char *game_mode_lookup_user_home(void)
+{
+	/* Try loading env HOME first */
+	const char *home = secure_getenv("HOME");
+	if (!home) {
+		/* If HOME is not defined (or out of context), fall back to passwd */
+		struct passwd *pw = getpwuid(getuid());
+		if (!pw)
+			return NULL;
+		home = pw->pw_dir;
+	}
+
+	/* Try to allocate into our heap */
+	return home ? strdup(home) : NULL;
+}
+
 /**
  * Attempt to resolve the exe for wine-preloader.
  * This function is used if game_mode_context_find_exe() identified the
  * process as wine-preloader. Returns NULL when resolve fails.
  */
-char *game_mode_resolve_wine_preloader(const pid_t pid)
+char *game_mode_resolve_wine_preloader(const char *exe, const pid_t pid)
 {
+	/* Detect if the process is a wine loader process */
+	if (game_mode_detect_wine_preloader(exe) || game_mode_detect_wine_loader(exe)) {
+		LOG_MSG("Detected wine for client %d [%s].\n", pid, exe);
+	} else {
+		return NULL;
+	}
+
 	char buffer[PATH_MAX];
 	char *wine_exe = NULL, *wineprefix = NULL;
 

+ 8 - 17
daemon/gamemode.h

@@ -116,13 +116,6 @@ GameModeConfig *game_mode_config_from_context(const GameModeContext *context);
  */
 int game_mode_reload_config(GameModeContext *context);
 
-/** gamemode-env.c
- * Provides internal API functions specific to working environment
- * variables.
- */
-char *game_mode_lookup_proc_env(const procfd_t proc_fd, const char *var);
-char *game_mode_lookup_user_home(void);
-
 /** gamemode-ioprio.c
  * Provides internal API functions specific to adjusting process
  * IO priorities.
@@ -130,13 +123,6 @@ char *game_mode_lookup_user_home(void);
 int game_mode_get_ioprio(const pid_t client);
 void game_mode_apply_ioprio(const GameModeContext *self, const pid_t client, int expected);
 
-/** gamemode-proc.c
- * Provides internal API functions specific to working with process
- * environments.
- */
-procfd_t game_mode_open_proc(const pid_t pid);
-int game_mode_close_proc(const procfd_t procfd);
-
 /** gamemode-sched.c
  * Provides internal API functions specific to adjusting process
  * scheduling.
@@ -149,9 +135,7 @@ void game_mode_apply_scheduling(const GameModeContext *self, const pid_t client)
  * Provides internal API functions specific to handling wine
  * prefixes.
  */
-bool game_mode_detect_wine_loader(const char *exe);
-bool game_mode_detect_wine_preloader(const char *exe);
-char *game_mode_resolve_wine_preloader(const pid_t pid);
+char *game_mode_resolve_wine_preloader(const char *exe, const pid_t pid);
 
 /** gamemode-tests.c
  * Provides a test suite to verify gamemode behaviour
@@ -166,3 +150,10 @@ int game_mode_initialise_gpu(GameModeConfig *config, GameModeGPUInfo **info);
 void game_mode_free_gpu(GameModeGPUInfo **info);
 int game_mode_apply_gpu(const GameModeGPUInfo *info);
 int game_mode_get_gpu(GameModeGPUInfo *info);
+
+/** gamemode-dbus.c
+ * Provides an API interface for using dbus
+ */
+void game_mode_context_loop(GameModeContext *context) __attribute__((noreturn));
+int game_mode_inhibit_screensaver(bool inhibit);
+void game_mode_client_count_changed(void);

+ 51 - 7
daemon/main.c → daemon/gamemoded.c

@@ -49,18 +49,19 @@ POSSIBILITY OF SUCH DAMAGE.
 
 #define _GNU_SOURCE
 
-#include "config.h"
-#include "daemonize.h"
-#include "dbus_messaging.h"
 #include "gamemode.h"
+#include "common-logging.h"
+#include "gamemode-config.h"
+
 #include "gamemode_client.h"
-#include "logging.h"
 
+#include "build-config.h"
+
+#include <fcntl.h>
 #include <getopt.h>
 #include <signal.h>
-#include <stdlib.h>
-#include <string.h>
-#include <systemd/sd-daemon.h>
+#include <sys/stat.h>
+#include <systemd/sd-daemon.h> /* TODO: Move usage to gamemode-dbus.c */
 #include <unistd.h>
 
 #define USAGE_TEXT                                                                                 \
@@ -95,6 +96,49 @@ static void sigint_handler_noexit(__attribute__((unused)) int signo)
 	LOG_MSG("Quitting by request...\n");
 }
 
+/**
+ * Helper to perform standard UNIX daemonization
+ */
+static void daemonize(const char *name)
+{
+	/* Initial fork */
+	pid_t pid = fork();
+	if (pid < 0) {
+		FATAL_ERRORNO("Failed to fork");
+	}
+
+	if (pid != 0) {
+		LOG_MSG("Daemon launched as %s...\n", name);
+		exit(EXIT_SUCCESS);
+	}
+
+	/* Fork a second time */
+	pid = fork();
+	if (pid < 0) {
+		FATAL_ERRORNO("Failed to fork");
+	} else if (pid > 0) {
+		exit(EXIT_SUCCESS);
+	}
+
+	/* Now continue execution */
+	umask(0022);
+	if (setsid() < 0) {
+		FATAL_ERRORNO("Failed to create process group\n");
+	}
+	if (chdir("/") < 0) {
+		FATAL_ERRORNO("Failed to change to root directory\n");
+	}
+
+	/* replace standard file descriptors by /dev/null */
+	int devnull_r = open("/dev/null", O_RDONLY);
+	int devnull_w = open("/dev/null", O_WRONLY);
+	dup2(devnull_r, STDIN_FILENO);
+	dup2(devnull_w, STDOUT_FILENO);
+	dup2(devnull_w, STDERR_FILENO);
+	close(devnull_r);
+	close(devnull_w);
+}
+
 /**
  * Main bootstrap entry into gamemoded
  */

+ 8 - 57
daemon/meson.build

@@ -1,36 +1,14 @@
-# Convenience library for the duplicated logging functionality
-common_sources = [
-    'logging.c',
-    'governors-query.c',
-    'external-helper.c',
-    'gpu-control.c',
-]
-
-daemon_common = static_library(
-    'daemon-common',
-    sources: common_sources,
-    install: false,
-)
-
-link_daemon_common = declare_dependency(
-    link_with: daemon_common,
-)
-
 # Main daemon
 daemon_sources = [
-    'main.c',
-    'gamemode.c',
-    'gamemode-env.c',
+    'gamemoded.c',
+    'gamemode-context.c',
     'gamemode-ioprio.c',
-    'gamemode-proc.c',
     'gamemode-sched.c',
     'gamemode-wine.c',
     'gamemode-tests.c',
     'gamemode-gpu.c',
-    'daemonize.c',
-    'dbus_messaging.c',
-    'daemon_config.c',
-    'helpers.c',
+    'gamemode-dbus.c',
+    'gamemode-config.c',
 ]
 
 gamemoded_includes = libgamemode_includes
@@ -46,36 +24,9 @@ executable(
         inih_dependency,
         libdl,
     ],
-    include_directories: gamemoded_includes,
-    install: true,
-)
-
-# Small target util to get and set cpu governors
-cpugovctl_sources = [
-    'cpugovctl.c',
-]
-
-cpugovctl = executable(
-    'cpugovctl',
-    sources: cpugovctl_sources,
-    dependencies: [
-        link_daemon_common,
-    ],
-    install: true,
-    install_dir: path_libexecdir,
-)
-
-# Small target util to get and set gpu clocks values
-gpuclockctl_sources = [
-    'gpuclockctl.c',
-]
-
-gpuclockctl = executable(
-    'gpuclockctl',
-    sources: gpuclockctl_sources,
-    dependencies: [
-        link_daemon_common,
+    include_directories: [
+        gamemoded_includes,
+        include_daemon_common,
     ],
     install: true,
-    install_dir: path_libexecdir,
-)
+)

+ 43 - 0
lib/README.md

@@ -0,0 +1,43 @@
+## libgamemode
+
+**libgamemode** is an internal library used to dispatch requests to the daemon. Note: `libgamemode` should never be linked with directly.
+
+**libgamemodeauto** is a simple dynamic library that automatically requests game mode when loaded. Useful to `LD_PRELOAD` into any game as needed.
+
+**gamemode\_client.h** is as single header lib that lets a game request game mode and handle errors.
+
+### Integration
+Developers can integrate the request directly into an app. Note that none of these client methods force your users to have the daemon installed or running - they will safely no-op if the host is missing.
+
+```C
+// Manually with error checking
+#include "gamemode_client.h"
+
+	if( gamemode_request_start() < 0 ) {
+		fprintf( stderr, "gamemode request failed: %s\n", gamemode_error_string() );
+	}
+
+	/* run game... */
+
+	gamemode_request_end(); // Not required, gamemoded can clean up after game exits
+```
+
+```C
+// Automatically on program start and finish
+#define GAMEMODE_AUTO
+#include "gamemode_client.h"
+```
+
+Or, distribute `libgamemodeauto.so` and either add `-lgamemodeauto` to your linker arguments, or add it to an LD\_PRELOAD in a launch script.
+
+### Supervisor support
+Developers can also create apps that manage GameMode on the system, for other processes:
+
+```C
+#include "gamemode_client.h"
+
+	gamemode_request_start_for(gamePID);
+	gamemode_request_end_for(gamePID);
+```
+
+This functionality can also be controlled in the config file in the `supervisor` section.

+ 0 - 4
lib/client_impl.c

@@ -32,11 +32,7 @@ POSSIBILITY OF SUCH DAMAGE.
 #define _GNU_SOURCE
 
 #include <dbus/dbus.h>
-#include <dlfcn.h>
-#include <errno.h>
 #include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>

+ 7 - 1
meson.build

@@ -132,13 +132,19 @@ cdata.set_quoted('LIBEXECDIR', path_libexecdir)
 cdata.set_quoted('GAMEMODE_VERSION', meson.project_version())
 config_h = configure_file(
     configuration: cdata,
-    output: 'config.h',
+    output: 'build-config.h',
 )
 config_h_dir = include_directories('.')
 
 # Library is always required
 subdir('lib')
 
+# common lib is always required
+subdir('common')
+
+# Utilities are always required
+subdir('util')
+
 # The daemon can be disabled if necessary, allowing multilib builds of the
 # main library
 if with_daemon == true

+ 3 - 5
daemon/cpugovctl.c → util/cpugovctl.c

@@ -31,12 +31,10 @@ POSSIBILITY OF SUCH DAMAGE.
 
 #define _GNU_SOURCE
 
-#include "governors-query.h"
-#include "logging.h"
+#include "common-governors.h"
+#include "common-logging.h"
 
-#include <ctype.h>
-#include <errno.h>
-#include <sys/types.h>
+#include <unistd.h>
 
 /**
  * Sets all governors to a value

+ 9 - 9
daemon/gpuclockctl.c → util/gpuclockctl.c

@@ -31,12 +31,12 @@ POSSIBILITY OF SUCH DAMAGE.
 
 #define _GNU_SOURCE
 
-#include "logging.h"
-
-#include "external-helper.h"
-#include "gpu-control.h"
+#include "common-external.h"
+#include "common-gpu.h"
+#include "common-logging.h"
 
 #include <limits.h>
+#include <unistd.h>
 
 /* NV constants */
 #define NV_CORE_OFFSET_ATTRIBUTE "GPUGraphicsClockOffset"
@@ -304,8 +304,8 @@ static int get_gpu_state_amd(struct GameModeGPUInfo *info)
 
 	int ret = 0;
 
-	char buff[CONFIG_VALUE_MAX] = { 0 };
-	if (!fgets(buff, CONFIG_VALUE_MAX, file)) {
+	char buff[GPU_VALUE_MAX] = { 0 };
+	if (!fgets(buff, GPU_VALUE_MAX, file)) {
 		LOG_ERROR("Could not read file %s (%s)!\n", path, strerror(errno));
 		ret = -1;
 	}
@@ -317,8 +317,8 @@ static int get_gpu_state_amd(struct GameModeGPUInfo *info)
 
 	if (ret == 0) {
 		/* Copy in the value from the file */
-		strncpy(info->amd_performance_level, buff, CONFIG_VALUE_MAX - 1);
-		info->amd_performance_level[CONFIG_VALUE_MAX - 1] = '\0';
+		strncpy(info->amd_performance_level, buff, GPU_VALUE_MAX - 1);
+		info->amd_performance_level[GPU_VALUE_MAX - 1] = '\0';
 	}
 
 	return ret;
@@ -468,7 +468,7 @@ int main(int argc, char *argv[])
 				LOG_ERROR("Must pass performance level for AMD gpu!\n");
 				print_usage_and_exit();
 			}
-			strncpy(info.amd_performance_level, argv[3], CONFIG_VALUE_MAX - 1);
+			strncpy(info.amd_performance_level, argv[3], GPU_VALUE_MAX - 1);
 			return set_gpu_state_amd(&info);
 			break;
 		default:

+ 32 - 0
util/meson.build

@@ -0,0 +1,32 @@
+
+# Small target util to get and set cpu governors
+cpugovctl_sources = [
+    'cpugovctl.c',
+]
+
+cpugovctl = executable(
+    'cpugovctl',
+    sources: cpugovctl_sources,
+    dependencies: [
+        link_daemon_common,
+    ],
+    install: true,
+    include_directories: include_daemon_common,
+    install_dir: path_libexecdir,
+)
+
+# Small target util to get and set gpu clocks values
+gpuclockctl_sources = [
+    'gpuclockctl.c',
+]
+
+gpuclockctl = executable(
+    'gpuclockctl',
+    sources: gpuclockctl_sources,
+    dependencies: [
+        link_daemon_common,
+    ],
+    install: true,
+    include_directories: include_daemon_common,
+    install_dir: path_libexecdir,
+)