sysprofd: add API to get a FD for a file in proc

This is useful for optimized parsing of proc files such as necessary by
the memory source to seek() back to the start (instead of re-opening a
new file).
This commit is contained in:
Christian Hergert
2019-05-12 16:06:02 -07:00
parent 16bc6f970e
commit 9d72203687
6 changed files with 142 additions and 1 deletions

View File

@ -22,6 +22,7 @@
#include "config.h"
#include <fcntl.h>
#include <gio/gio.h>
#include <errno.h>
#ifdef __linux__
@ -281,6 +282,25 @@ helpers_get_proc_file (const gchar *path,
g_file_get_contents (canon, contents, len, NULL);
}
gboolean
helpers_get_proc_fd (const gchar *path,
gint *out_fd)
{
g_autofree gchar *canon = NULL;
g_autoptr(GFile) file = NULL;
g_assert (path != NULL);
g_assert (out_fd != NULL);
/* Canonicalize filename */
file = g_file_new_for_path (path);
canon = g_file_get_path (file);
return g_file_is_native (file) &&
(g_str_has_prefix (canon, "/proc/") || g_str_has_prefix (canon, "/sys/")) &&
-1 != (*out_fd = open (canon, O_RDONLY | O_CLOEXEC));
}
gboolean
helpers_can_see_pids (void)
{