Skip to content

Instantly share code, notes, and snippets.

@chergert
Created September 12, 2016 07:39
Show Gist options
  • Save chergert/2f11ab7f48c56ab9195c3bb669e4c39f to your computer and use it in GitHub Desktop.
Save chergert/2f11ab7f48c56ab9195c3bb669e4c39f to your computer and use it in GitHub Desktop.
#include <gio/gio.h>
#include <stdlib.h>
#include <glib-unix.h>
#include <gio/gunixfdlist.h>
static GMainLoop *main_loop;
static guint exited_subscription;
static void
host_command_exited_cb (GDBusConnection *connection,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface_name,
const gchar *signal_name,
GVariant *parameters,
gpointer user_data)
{
g_print ("Exited\n");
g_dbus_connection_signal_unsubscribe (connection, exited_subscription);
exited_subscription = 0;
}
static gboolean
make_call (gpointer user_data)
{
GDBusConnection *conn = user_data;
GError *error = NULL;
GVariant *reply;
GVariant *params;
g_autoptr(GUnixFDList) fd_list = g_unix_fd_list_new ();
GVariantBuilder *fd_builder = g_variant_builder_new (G_VARIANT_TYPE ("a{uh}"));
GVariantBuilder *env_builder = g_variant_builder_new (G_VARIANT_TYPE ("a{ss}"));
static const gchar *argv[] = { "ls", NULL };
gint stdin_handle;
gint stdout_handle;
gint stderr_handle;
stdin_handle = g_unix_fd_list_append (fd_list, STDIN_FILENO, &error);
g_assert_no_error (error);
stdout_handle = g_unix_fd_list_append (fd_list, STDOUT_FILENO, &error);
g_assert_no_error (error);
stderr_handle = g_unix_fd_list_append (fd_list, STDERR_FILENO, &error);
g_assert_no_error (error);
g_variant_builder_add (fd_builder, "{uh}", 0, stdin_handle);
g_variant_builder_add (fd_builder, "{uh}", 1, stdout_handle);
g_variant_builder_add (fd_builder, "{uh}", 2, stderr_handle);
params = g_variant_new ("(^ay^aay@a{uh}@a{ss}u)",
g_get_home_dir (),
argv,
g_variant_builder_end (fd_builder),
g_variant_builder_end (env_builder),
0);
exited_subscription = g_dbus_connection_signal_subscribe (conn,
NULL,
"org.freedesktop.Flatpak.Development",
"HostCommandExited",
"/org/freedesktop/Flatpak/Development",
NULL,
G_DBUS_SIGNAL_FLAGS_NONE,
host_command_exited_cb,
NULL,
NULL);
reply = g_dbus_connection_call_with_unix_fd_list_sync (conn,
"org.freedesktop.Flatpak",
"/org/freedesktop/Flatpak/Development",
"org.freedesktop.Flatpak.Development",
"HostCommand",
params,
G_VARIANT_TYPE ("(u)"),
G_DBUS_CALL_FLAGS_NONE,
-1,
fd_list,
NULL,
NULL,
&error);
if (reply == NULL)
{
g_printerr ("%s\n", error->message);
g_clear_error (&error);
}
{
g_autofree gchar *str = g_variant_print (reply, TRUE);
g_print ("%s\n", str);
}
g_variant_unref (reply);
return G_SOURCE_CONTINUE;
}
gint
main (gint argc,
gchar *argv[])
{
GDBusConnection *conn;
gint timeout;
if (argc < 2)
{
g_printerr ("usage: %s TIMEOUT_IN_SECONDS\n", argv[0]);
return 1;
}
timeout = atoi (argv[1]);
conn = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
g_assert (conn != NULL);
main_loop = g_main_loop_new (NULL, FALSE);
g_timeout_add (timeout * 1000, make_call, conn);
g_main_loop_run (main_loop);
g_main_loop_unref (main_loop);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment