1#include <string.h>
2#include <stdlib.h>
3
4#include <sys/types.h>
5#include <unistd.h>
6
7#include <time.h>
8
9#include <gio/gio.h>
10#include <gio/gunixfdlist.h>
11
12/* see gdbus-example-server.c for the server implementation */
13static gint
14get_server_stdout (GDBusConnection *connection,
15 const gchar *name_owner,
16 GError **error)
17{
18 GDBusMessage *method_call_message;
19 GDBusMessage *method_reply_message;
20 GUnixFDList *fd_list;
21 gint fd;
22
23 fd = -1;
24 method_call_message = NULL;
25 method_reply_message = NULL;
26
27 method_call_message = g_dbus_message_new_method_call (name: name_owner,
28 path: "/org/gtk/GDBus/TestObject",
29 interface_: "org.gtk.GDBus.TestInterface",
30 method: "GimmeStdout");
31 method_reply_message = g_dbus_connection_send_message_with_reply_sync (connection,
32 message: method_call_message,
33 flags: G_DBUS_SEND_MESSAGE_FLAGS_NONE,
34 timeout_msec: -1,
35 NULL, /* out_serial */
36 NULL, /* cancellable */
37 error);
38 if (method_reply_message == NULL)
39 goto out;
40
41 if (g_dbus_message_get_message_type (message: method_reply_message) == G_DBUS_MESSAGE_TYPE_ERROR)
42 {
43 g_dbus_message_to_gerror (message: method_reply_message, error);
44 goto out;
45 }
46
47 fd_list = g_dbus_message_get_unix_fd_list (message: method_reply_message);
48 fd = g_unix_fd_list_get (list: fd_list, index_: 0, error);
49
50 out:
51 g_object_unref (object: method_call_message);
52 g_object_unref (object: method_reply_message);
53
54 return fd;
55}
56
57static void
58on_name_appeared (GDBusConnection *connection,
59 const gchar *name,
60 const gchar *name_owner,
61 gpointer user_data)
62{
63 gint fd;
64 GError *error;
65
66 error = NULL;
67 fd = get_server_stdout (connection, name_owner, error: &error);
68 if (fd == -1)
69 {
70 g_printerr (format: "Error invoking GimmeStdout(): %s\n",
71 error->message);
72 g_error_free (error);
73 exit (status: 1);
74 }
75 else
76 {
77 gchar *now_buf = NULL;
78 gssize len;
79 gchar *str;
80 GDateTime *now = g_date_time_new_now_local ();
81
82 g_assert_nonnull (now);
83 now_buf = g_date_time_format (datetime: now, format: "%Y-%m-%d %H:%M:%S");
84 g_date_time_unref (datetime: now);
85
86 str = g_strdup_printf (format: "On %s, gdbus-example-unix-fd-client with pid %d was here!\n",
87 now_buf,
88 (gint) getpid ());
89 len = strlen (s: str);
90 g_warn_if_fail (write (fd, str, len) == len);
91 close (fd: fd);
92
93 g_print (format: "Wrote the following on server's stdout:\n%s", str);
94
95 g_free (mem: str);
96 g_free (mem: now_buf);
97 exit (status: 0);
98 }
99}
100
101static void
102on_name_vanished (GDBusConnection *connection,
103 const gchar *name,
104 gpointer user_data)
105{
106 g_printerr (format: "Failed to get name owner for %s\n"
107 "Is ./gdbus-example-server running?\n",
108 name);
109 exit (status: 1);
110}
111
112int
113main (int argc, char *argv[])
114{
115 guint watcher_id;
116 GMainLoop *loop;
117
118 watcher_id = g_bus_watch_name (bus_type: G_BUS_TYPE_SESSION,
119 name: "org.gtk.GDBus.TestServer",
120 flags: G_BUS_NAME_WATCHER_FLAGS_NONE,
121 name_appeared_handler: on_name_appeared,
122 name_vanished_handler: on_name_vanished,
123 NULL,
124 NULL);
125
126 loop = g_main_loop_new (NULL, FALSE);
127 g_main_loop_run (loop);
128
129 g_bus_unwatch_name (watcher_id);
130 return 0;
131}
132

source code of gtk/subprojects/glib/gio/tests/gdbus-example-unix-fd-client.c