Browse Source

daemon: add creation timestamp to GameModeClient

Record the time a client was created, i.e. registered, in the
GameModeClient struct and add a getter for it.

(Alex Smith: Fixed up function documentation comments)
Christian Kellner 5 years ago
parent
commit
52367772c8
2 changed files with 27 additions and 0 deletions
  1. 21 0
      daemon/gamemode-context.c
  2. 6 0
      daemon/gamemode.h

+ 21 - 0
daemon/gamemode-context.c

@@ -46,6 +46,7 @@ POSSIBILITY OF SUCH DAMAGE.
 #include <pthread.h>
 #include <stdatomic.h>
 #include <stdlib.h>
+#include <sys/time.h>
 #include <systemd/sd-daemon.h> /* TODO: Move usage to gamemode-dbus.c */
 #include <unistd.h>
 
@@ -58,6 +59,7 @@ struct GameModeClient {
 	pid_t pid;                   /**< Process ID */
 	struct GameModeClient *next; /**<Next client in the list */
 	char executable[PATH_MAX];   /**<Process executable */
+	time_t timestamp;            /**<When was the client registered */
 };
 
 struct GameModeContext {
@@ -657,9 +659,18 @@ static GameModeClient *game_mode_client_new(pid_t pid, char *executable)
 	GameModeClient c = {
 		.next = NULL,
 		.pid = pid,
+		.timestamp = 0,
 	};
 	/* clang-format on */
 	GameModeClient *ret = NULL;
+	struct timeval now = {
+		0,
+	};
+	int r;
+
+	r = gettimeofday(&now, NULL);
+	if (r == 0)
+		c.timestamp = now.tv_sec;
 
 	ret = calloc(1, sizeof(struct GameModeClient));
 	if (!ret) {
@@ -668,6 +679,7 @@ static GameModeClient *game_mode_client_new(pid_t pid, char *executable)
 	*ret = c;
 	ret->refcount = ATOMIC_VAR_INIT(1);
 	strncpy(ret->executable, executable, PATH_MAX - 1);
+
 	return ret;
 }
 
@@ -714,6 +726,15 @@ const char *game_mode_client_get_executable(GameModeClient *client)
 	return client->executable;
 }
 
+/**
+ * The time that game mode was requested for the client.
+ */
+uint64_t game_mode_client_get_timestamp(GameModeClient *client)
+{
+	assert(client != NULL);
+	return (uint64_t)client->timestamp;
+}
+
 /* Internal refresh config function (assumes no contention with reaper thread) */
 static void game_mode_reload_config_internal(GameModeContext *self)
 {

+ 6 - 0
daemon/gamemode.h

@@ -32,6 +32,7 @@ POSSIBILITY OF SUCH DAMAGE.
 #pragma once
 
 #include <stdbool.h>
+#include <stdint.h>
 #include <sys/types.h>
 
 #define INVALID_PROCFD -1
@@ -69,6 +70,11 @@ pid_t game_mode_client_get_pid(GameModeClient *client);
  */
 const char *game_mode_client_get_executable(GameModeClient *client);
 
+/**
+ * The time that game mode was requested for the client.
+ */
+u_int64_t game_mode_client_get_timestamp(GameModeClient *client);
+
 /**
  * Return the singleton instance
  */