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