mirror of
https://gitlab.freedesktop.org/libinput/libei.git
synced 2026-02-03 14:50:27 +01:00
util: add a helper for fetching the cmdline.
Better than duplicating this three times. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
parent
a59474b5ca
commit
c15fcdc140
6 changed files with 42 additions and 47 deletions
15
src/libei.c
15
src/libei.c
|
|
@ -113,19 +113,8 @@ DEFINE_UNREF_CLEANUP_FUNC(ei_region);
|
|||
static void
|
||||
set_prop_cmdline(struct ei *ei)
|
||||
{
|
||||
char path[PATH_MAX];
|
||||
|
||||
xsnprintf(path, sizeof(path), "/proc/%d/cmdline", getpid());
|
||||
_cleanup_close_ int fd = open(path, O_RDONLY);
|
||||
if (fd < 0)
|
||||
return;
|
||||
|
||||
int len;
|
||||
if ((len = read(fd, path, sizeof(path) - 1)) < 0)
|
||||
return;
|
||||
path[len] = '\0';
|
||||
|
||||
ei_property_update(ei, "ei.application.cmdline", path, EI_PROPERTY_PERM_NONE);
|
||||
_cleanup_free_ char *cmdline = cmdline_as_str();
|
||||
ei_property_update(ei, "ei.application.cmdline", cmdline, EI_PROPERTY_PERM_NONE);
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
|
|||
|
|
@ -471,4 +471,22 @@ MUNIT_TEST(test_strreplace)
|
|||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
MUNIT_TEST(test_cmdline_as_str)
|
||||
{
|
||||
_cleanup_free_ char *from_function = cmdline_as_str();
|
||||
char cmdline[PATH_MAX];
|
||||
|
||||
xsnprintf(cmdline, sizeof(cmdline), "/proc/%i/cmdline", getpid());
|
||||
|
||||
int fd = open(cmdline, O_RDONLY);
|
||||
munit_assert_int(fd, >=, 0);
|
||||
int len = read(fd, cmdline, sizeof(cmdline) - 1);
|
||||
munit_assert_int(len, >=, 0);
|
||||
cmdline[len] = '\0';
|
||||
|
||||
munit_assert_string_equal(cmdline, from_function);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@
|
|||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
|
@ -380,3 +381,19 @@ strstartswith(const char *str, const char *prefix)
|
|||
|
||||
return prefixlen > 0 ? strneq(str, prefix, strlen(prefix)) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the content of /proc/$pid/cmdline as newly allocated string.
|
||||
*/
|
||||
static inline char *
|
||||
cmdline_as_str(void)
|
||||
{
|
||||
int fd = open("/proc/self/cmdline", O_RDONLY);
|
||||
if (fd > 0) {
|
||||
char buffer[1024] = {0};
|
||||
int len = read(fd, buffer, sizeof(buffer) - 1);
|
||||
close(fd);
|
||||
return len > 0 ? xstrdup(buffer) : NULL;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -54,16 +54,9 @@ MUNIT_TEST(test_ei_initial_properties)
|
|||
{
|
||||
_unref_(peck) *peck = peck_new();
|
||||
char pid[64];
|
||||
char cmdline[PATH_MAX];
|
||||
_cleanup_free_ char *cmdline = cmdline_as_str();
|
||||
|
||||
xsnprintf(pid, sizeof(pid), "%i", getpid());
|
||||
xsnprintf(cmdline, sizeof(cmdline), "/proc/%i/cmdline", getpid());
|
||||
|
||||
int fd = open(cmdline, O_RDONLY);
|
||||
munit_assert_int(fd, >=, 0);
|
||||
int len = read(fd, cmdline, sizeof(cmdline) - 1);
|
||||
munit_assert_int(len, >=, 0);
|
||||
cmdline[len] = '\0';
|
||||
|
||||
peck_enable_eis_behavior(peck, PECK_EIS_BEHAVIOR_NONE);
|
||||
peck_dispatch_until_stable(peck);
|
||||
|
|
|
|||
|
|
@ -189,19 +189,8 @@ portal_create_session(sd_bus_message *m, void *userdata,
|
|||
static void
|
||||
set_prop_cmdline(struct reis *reis)
|
||||
{
|
||||
char path[PATH_MAX];
|
||||
|
||||
xsnprintf(path, sizeof(path), "/proc/%i/cmdline", getpid());
|
||||
_cleanup_close_ int fd = open(path, O_RDONLY);
|
||||
if (fd < 0)
|
||||
return;
|
||||
|
||||
int len;
|
||||
if ((len = read(fd, path, sizeof(path) - 1)) < 0)
|
||||
return;
|
||||
path[len] = '\0';
|
||||
|
||||
reis_set_property_with_permissions(reis, "ei.application.cmdline", path, REIS_PROPERTY_PERM_NONE);
|
||||
_cleanup_free_ char *cmdline = cmdline_as_str();
|
||||
reis_set_property_with_permissions(reis, "ei.application.cmdline", cmdline, REIS_PROPERTY_PERM_NONE);
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
|
|||
|
|
@ -230,19 +230,8 @@ portal_create_session(sd_bus_message *m, void *userdata,
|
|||
static void
|
||||
set_prop_cmdline(struct reis *reis)
|
||||
{
|
||||
char path[PATH_MAX];
|
||||
|
||||
xsnprintf(path, sizeof(path), "/proc/%i/cmdline", getpid());
|
||||
_cleanup_close_ int fd = open(path, O_RDONLY);
|
||||
if (fd < 0)
|
||||
return;
|
||||
|
||||
int len;
|
||||
if ((len = read(fd, path, sizeof(path) - 1)) < 0)
|
||||
return;
|
||||
path[len] = '\0';
|
||||
|
||||
reis_set_property_with_permissions(reis, "ei.application.cmdline", path, REIS_PROPERTY_PERM_NONE);
|
||||
_cleanup_free_ char *cmdline = cmdline_as_str();
|
||||
reis_set_property_with_permissions(reis, "ei.application.cmdline", cmdline, REIS_PROPERTY_PERM_NONE);
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue