Use g_simple_async_result_{new_,}take_error
[platform/upstream/glib.git] / gio / gdbusprivate.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30
31 #include "giotypes.h"
32 #include "gsocket.h"
33 #include "gdbusprivate.h"
34 #include "gdbusmessage.h"
35 #include "gdbuserror.h"
36 #include "gdbusintrospection.h"
37 #include "gasyncresult.h"
38 #include "gsimpleasyncresult.h"
39 #include "ginputstream.h"
40 #include "gmemoryinputstream.h"
41 #include "giostream.h"
42 #include "gsocketcontrolmessage.h"
43 #include "gsocketconnection.h"
44 #include "gsocketoutputstream.h"
45
46 #ifdef G_OS_UNIX
47 #include "gunixfdmessage.h"
48 #include "gunixconnection.h"
49 #include "gunixcredentialsmessage.h"
50 #endif
51
52 #ifdef G_OS_WIN32
53 #include <windows.h>
54 #endif
55
56 #include "glibintl.h"
57
58 /* ---------------------------------------------------------------------------------------------------- */
59
60 gchar *
61 _g_dbus_hexdump (const gchar *data, gsize len, guint indent)
62 {
63  guint n, m;
64  GString *ret;
65
66  ret = g_string_new (NULL);
67
68  for (n = 0; n < len; n += 16)
69    {
70      g_string_append_printf (ret, "%*s%04x: ", indent, "", n);
71
72      for (m = n; m < n + 16; m++)
73        {
74          if (m > n && (m%4) == 0)
75            g_string_append_c (ret, ' ');
76          if (m < len)
77            g_string_append_printf (ret, "%02x ", (guchar) data[m]);
78          else
79            g_string_append (ret, "   ");
80        }
81
82      g_string_append (ret, "   ");
83
84      for (m = n; m < len && m < n + 16; m++)
85        g_string_append_c (ret, g_ascii_isprint (data[m]) ? data[m] : '.');
86
87      g_string_append_c (ret, '\n');
88    }
89
90  return g_string_free (ret, FALSE);
91 }
92
93 /* ---------------------------------------------------------------------------------------------------- */
94
95 /* Unfortunately ancillary messages are discarded when reading from a
96  * socket using the GSocketInputStream abstraction. So we provide a
97  * very GInputStream-ish API that uses GSocket in this case (very
98  * similar to GSocketInputStream).
99  */
100
101 typedef struct
102 {
103   GSocket *socket;
104   GCancellable *cancellable;
105
106   void *buffer;
107   gsize count;
108
109   GSocketControlMessage ***messages;
110   gint *num_messages;
111
112   GSimpleAsyncResult *simple;
113
114   gboolean from_mainloop;
115 } ReadWithControlData;
116
117 static void
118 read_with_control_data_free (ReadWithControlData *data)
119 {
120   g_object_unref (data->socket);
121   if (data->cancellable != NULL)
122     g_object_unref (data->cancellable);
123   g_object_unref (data->simple);
124   g_free (data);
125 }
126
127 static gboolean
128 _g_socket_read_with_control_messages_ready (GSocket      *socket,
129                                             GIOCondition  condition,
130                                             gpointer      user_data)
131 {
132   ReadWithControlData *data = user_data;
133   GError *error;
134   gssize result;
135   GInputVector vector;
136
137   error = NULL;
138   vector.buffer = data->buffer;
139   vector.size = data->count;
140   result = g_socket_receive_message (data->socket,
141                                      NULL, /* address */
142                                      &vector,
143                                      1,
144                                      data->messages,
145                                      data->num_messages,
146                                      NULL,
147                                      data->cancellable,
148                                      &error);
149   if (result >= 0)
150     {
151       g_simple_async_result_set_op_res_gssize (data->simple, result);
152     }
153   else
154     {
155       g_assert (error != NULL);
156       g_simple_async_result_take_error (data->simple, error);
157     }
158
159   if (data->from_mainloop)
160     g_simple_async_result_complete (data->simple);
161   else
162     g_simple_async_result_complete_in_idle (data->simple);
163
164   return FALSE;
165 }
166
167 static void
168 _g_socket_read_with_control_messages (GSocket                 *socket,
169                                       void                    *buffer,
170                                       gsize                    count,
171                                       GSocketControlMessage ***messages,
172                                       gint                    *num_messages,
173                                       gint                     io_priority,
174                                       GCancellable            *cancellable,
175                                       GAsyncReadyCallback      callback,
176                                       gpointer                 user_data)
177 {
178   ReadWithControlData *data;
179
180   data = g_new0 (ReadWithControlData, 1);
181   data->socket = g_object_ref (socket);
182   data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
183   data->buffer = buffer;
184   data->count = count;
185   data->messages = messages;
186   data->num_messages = num_messages;
187
188   data->simple = g_simple_async_result_new (G_OBJECT (socket),
189                                             callback,
190                                             user_data,
191                                             _g_socket_read_with_control_messages);
192
193   if (!g_socket_condition_check (socket, G_IO_IN))
194     {
195       GSource *source;
196       data->from_mainloop = TRUE;
197       source = g_socket_create_source (data->socket,
198                                        G_IO_IN | G_IO_HUP | G_IO_ERR,
199                                        cancellable);
200       g_source_set_callback (source,
201                              (GSourceFunc) _g_socket_read_with_control_messages_ready,
202                              data,
203                              (GDestroyNotify) read_with_control_data_free);
204       g_source_attach (source, g_main_context_get_thread_default ());
205       g_source_unref (source);
206     }
207   else
208     {
209       _g_socket_read_with_control_messages_ready (data->socket, G_IO_IN, data);
210       read_with_control_data_free (data);
211     }
212 }
213
214 static gssize
215 _g_socket_read_with_control_messages_finish (GSocket       *socket,
216                                              GAsyncResult  *result,
217                                              GError       **error)
218 {
219   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
220
221   g_return_val_if_fail (G_IS_SOCKET (socket), -1);
222   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == _g_socket_read_with_control_messages);
223
224   if (g_simple_async_result_propagate_error (simple, error))
225       return -1;
226   else
227     return g_simple_async_result_get_op_res_gssize (simple);
228 }
229
230 /* ---------------------------------------------------------------------------------------------------- */
231
232 /* Work-around for https://bugzilla.gnome.org/show_bug.cgi?id=627724 */
233
234 static GPtrArray *ensured_classes = NULL;
235
236 static void
237 ensure_type (GType gtype)
238 {
239   g_ptr_array_add (ensured_classes, g_type_class_ref (gtype));
240 }
241
242 static void
243 released_required_types (void)
244 {
245   g_ptr_array_foreach (ensured_classes, (GFunc) g_type_class_unref, NULL);
246   g_ptr_array_unref (ensured_classes);
247   ensured_classes = NULL;
248 }
249
250 static void
251 ensure_required_types (void)
252 {
253   g_assert (ensured_classes == NULL);
254   ensured_classes = g_ptr_array_new ();
255   ensure_type (G_TYPE_SIMPLE_ASYNC_RESULT);
256   ensure_type (G_TYPE_MEMORY_INPUT_STREAM);
257 }
258 /* ---------------------------------------------------------------------------------------------------- */
259
260 G_LOCK_DEFINE_STATIC (shared_thread_lock);
261
262 typedef struct
263 {
264   gint num_users;
265   GThread *thread;
266   GMainContext *context;
267   GMainLoop *loop;
268 } SharedThreadData;
269
270 static SharedThreadData *shared_thread_data = NULL;
271
272 static gpointer
273 gdbus_shared_thread_func (gpointer data)
274 {
275   g_main_context_push_thread_default (shared_thread_data->context);
276   g_main_loop_run (shared_thread_data->loop);
277   g_main_context_pop_thread_default (shared_thread_data->context);
278   return NULL;
279 }
280
281 typedef void (*GDBusSharedThreadFunc) (gpointer user_data);
282
283 typedef struct
284 {
285   GDBusSharedThreadFunc func;
286   gpointer              user_data;
287   gboolean              done;
288 } CallerData;
289
290 static gboolean
291 invoke_caller (gpointer user_data)
292 {
293   CallerData *data = user_data;
294   data->func (data->user_data);
295   data->done = TRUE;
296   return FALSE;
297 }
298
299 /* ---------------------------------------------------------------------------------------------------- */
300
301 static void
302 _g_dbus_shared_thread_ref (GDBusSharedThreadFunc func,
303                            gpointer              user_data)
304 {
305   GError *error;
306   GSource *idle_source;
307   CallerData *data;
308   gboolean release_types;
309
310   G_LOCK (shared_thread_lock);
311
312   release_types = FALSE;
313
314   if (shared_thread_data != NULL)
315     {
316       shared_thread_data->num_users += 1;
317       goto have_thread;
318     }
319
320   shared_thread_data = g_new0 (SharedThreadData, 1);
321   shared_thread_data->num_users = 1;
322
323   /* Work-around for https://bugzilla.gnome.org/show_bug.cgi?id=627724 */
324   ensure_required_types ();
325   release_types = TRUE;
326
327   error = NULL;
328   shared_thread_data->context = g_main_context_new ();
329   shared_thread_data->loop = g_main_loop_new (shared_thread_data->context, FALSE);
330   shared_thread_data->thread = g_thread_create (gdbus_shared_thread_func,
331                                                 NULL,
332                                                 TRUE,
333                                                 &error);
334   g_assert_no_error (error);
335
336  have_thread:
337
338   data = g_new0 (CallerData, 1);
339   data->func = func;
340   data->user_data = user_data;
341   data->done = FALSE;
342
343   idle_source = g_idle_source_new ();
344   g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
345   g_source_set_callback (idle_source,
346                          invoke_caller,
347                          data,
348                          NULL);
349   g_source_attach (idle_source, shared_thread_data->context);
350   g_source_unref (idle_source);
351
352   /* wait for the user code to run.. hmm.. probably use a condition variable instead */
353   while (!data->done)
354     g_thread_yield ();
355
356   if (release_types)
357     released_required_types ();
358
359   g_free (data);
360
361   G_UNLOCK (shared_thread_lock);
362 }
363
364 static void
365 _g_dbus_shared_thread_unref (void)
366 {
367   /* TODO: actually destroy the shared thread here */
368 #if 0
369   G_LOCK (shared_thread_lock);
370   g_assert (shared_thread_data != NULL);
371   shared_thread_data->num_users -= 1;
372   if (shared_thread_data->num_users == 0)
373     {
374       g_main_loop_quit (shared_thread_data->loop);
375       //g_thread_join (shared_thread_data->thread);
376       g_main_loop_unref (shared_thread_data->loop);
377       g_main_context_unref (shared_thread_data->context);
378       g_free (shared_thread_data);
379       shared_thread_data = NULL;
380       G_UNLOCK (shared_thread_lock);
381     }
382   else
383     {
384       G_UNLOCK (shared_thread_lock);
385     }
386 #endif
387 }
388
389 /* ---------------------------------------------------------------------------------------------------- */
390
391 struct GDBusWorker
392 {
393   volatile gint                       ref_count;
394
395   gboolean                            stopped;
396
397   /* TODO: frozen (e.g. G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING) currently
398    * only affects messages received from the other peer (since GDBusServer is the
399    * only user) - we might want it to affect messages sent to the other peer too?
400    */
401   gboolean                            frozen;
402   GQueue                             *received_messages_while_frozen;
403
404   GIOStream                          *stream;
405   GDBusCapabilityFlags                capabilities;
406   GCancellable                       *cancellable;
407   GDBusWorkerMessageReceivedCallback  message_received_callback;
408   GDBusWorkerMessageAboutToBeSentCallback message_about_to_be_sent_callback;
409   GDBusWorkerDisconnectedCallback     disconnected_callback;
410   gpointer                            user_data;
411
412   GThread                            *thread;
413
414   /* if not NULL, stream is GSocketConnection */
415   GSocket *socket;
416
417   /* used for reading */
418   GMutex                             *read_lock;
419   gchar                              *read_buffer;
420   gsize                               read_buffer_allocated_size;
421   gsize                               read_buffer_cur_size;
422   gsize                               read_buffer_bytes_wanted;
423   GUnixFDList                        *read_fd_list;
424   GSocketControlMessage             **read_ancillary_messages;
425   gint                                read_num_ancillary_messages;
426
427   /* used for writing */
428   GMutex                             *write_lock;
429   GQueue                             *write_queue;
430   gint                                num_writes_pending;
431   guint64                             write_num_messages_written;
432   GList                              *write_pending_flushes;
433 };
434
435 /* ---------------------------------------------------------------------------------------------------- */
436
437 typedef struct
438 {
439   GMutex *mutex;
440   GCond *cond;
441   guint64 number_to_wait_for;
442   GError *error;
443 } FlushData;
444
445 struct _MessageToWriteData ;
446 typedef struct _MessageToWriteData MessageToWriteData;
447
448 static void message_to_write_data_free (MessageToWriteData *data);
449
450 static void read_message_print_transport_debug (gssize bytes_read,
451                                                 GDBusWorker *worker);
452
453 static void write_message_print_transport_debug (gssize bytes_written,
454                                                  MessageToWriteData *data);
455
456 /* ---------------------------------------------------------------------------------------------------- */
457
458 static GDBusWorker *
459 _g_dbus_worker_ref (GDBusWorker *worker)
460 {
461   g_atomic_int_inc (&worker->ref_count);
462   return worker;
463 }
464
465 static void
466 _g_dbus_worker_unref (GDBusWorker *worker)
467 {
468   if (g_atomic_int_dec_and_test (&worker->ref_count))
469     {
470       g_assert (worker->write_pending_flushes == NULL);
471
472       _g_dbus_shared_thread_unref ();
473
474       g_object_unref (worker->stream);
475
476       g_mutex_free (worker->read_lock);
477       g_object_unref (worker->cancellable);
478       if (worker->read_fd_list != NULL)
479         g_object_unref (worker->read_fd_list);
480
481       g_queue_foreach (worker->received_messages_while_frozen, (GFunc) g_object_unref, NULL);
482       g_queue_free (worker->received_messages_while_frozen);
483
484       g_mutex_free (worker->write_lock);
485       g_queue_foreach (worker->write_queue, (GFunc) message_to_write_data_free, NULL);
486       g_queue_free (worker->write_queue);
487
488       g_free (worker->read_buffer);
489
490       g_free (worker);
491     }
492 }
493
494 static void
495 _g_dbus_worker_emit_disconnected (GDBusWorker  *worker,
496                                   gboolean      remote_peer_vanished,
497                                   GError       *error)
498 {
499   if (!worker->stopped)
500     worker->disconnected_callback (worker, remote_peer_vanished, error, worker->user_data);
501 }
502
503 static void
504 _g_dbus_worker_emit_message_received (GDBusWorker  *worker,
505                                       GDBusMessage *message)
506 {
507   if (!worker->stopped)
508     worker->message_received_callback (worker, message, worker->user_data);
509 }
510
511 static GDBusMessage *
512 _g_dbus_worker_emit_message_about_to_be_sent (GDBusWorker  *worker,
513                                               GDBusMessage *message)
514 {
515   GDBusMessage *ret;
516   if (!worker->stopped)
517     ret = worker->message_about_to_be_sent_callback (worker, message, worker->user_data);
518   else
519     ret = message;
520   return ret;
521 }
522
523 /* can only be called from private thread with read-lock held - takes ownership of @message */
524 static void
525 _g_dbus_worker_queue_or_deliver_received_message (GDBusWorker  *worker,
526                                                   GDBusMessage *message)
527 {
528   if (worker->frozen || g_queue_get_length (worker->received_messages_while_frozen) > 0)
529     {
530       /* queue up */
531       g_queue_push_tail (worker->received_messages_while_frozen, message);
532     }
533   else
534     {
535       /* not frozen, nor anything in queue */
536       _g_dbus_worker_emit_message_received (worker, message);
537       g_object_unref (message);
538     }
539 }
540
541 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
542 static gboolean
543 unfreeze_in_idle_cb (gpointer user_data)
544 {
545   GDBusWorker *worker = user_data;
546   GDBusMessage *message;
547
548   g_mutex_lock (worker->read_lock);
549   if (worker->frozen)
550     {
551       while ((message = g_queue_pop_head (worker->received_messages_while_frozen)) != NULL)
552         {
553           _g_dbus_worker_emit_message_received (worker, message);
554           g_object_unref (message);
555         }
556       worker->frozen = FALSE;
557     }
558   else
559     {
560       g_assert (g_queue_get_length (worker->received_messages_while_frozen) == 0);
561     }
562   g_mutex_unlock (worker->read_lock);
563   return FALSE;
564 }
565
566 /* can be called from any thread */
567 void
568 _g_dbus_worker_unfreeze (GDBusWorker *worker)
569 {
570   GSource *idle_source;
571   idle_source = g_idle_source_new ();
572   g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
573   g_source_set_callback (idle_source,
574                          unfreeze_in_idle_cb,
575                          _g_dbus_worker_ref (worker),
576                          (GDestroyNotify) _g_dbus_worker_unref);
577   g_source_attach (idle_source, shared_thread_data->context);
578   g_source_unref (idle_source);
579 }
580
581 /* ---------------------------------------------------------------------------------------------------- */
582
583 static void _g_dbus_worker_do_read_unlocked (GDBusWorker *worker);
584
585 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
586 static void
587 _g_dbus_worker_do_read_cb (GInputStream  *input_stream,
588                            GAsyncResult  *res,
589                            gpointer       user_data)
590 {
591   GDBusWorker *worker = user_data;
592   GError *error;
593   gssize bytes_read;
594
595   g_mutex_lock (worker->read_lock);
596
597   /* If already stopped, don't even process the reply */
598   if (worker->stopped)
599     goto out;
600
601   error = NULL;
602   if (worker->socket == NULL)
603     bytes_read = g_input_stream_read_finish (g_io_stream_get_input_stream (worker->stream),
604                                              res,
605                                              &error);
606   else
607     bytes_read = _g_socket_read_with_control_messages_finish (worker->socket,
608                                                               res,
609                                                               &error);
610   if (worker->read_num_ancillary_messages > 0)
611     {
612       gint n;
613       for (n = 0; n < worker->read_num_ancillary_messages; n++)
614         {
615           GSocketControlMessage *control_message = G_SOCKET_CONTROL_MESSAGE (worker->read_ancillary_messages[n]);
616
617           if (FALSE)
618             {
619             }
620 #ifdef G_OS_UNIX
621           else if (G_IS_UNIX_FD_MESSAGE (control_message))
622             {
623               GUnixFDMessage *fd_message;
624               gint *fds;
625               gint num_fds;
626
627               fd_message = G_UNIX_FD_MESSAGE (control_message);
628               fds = g_unix_fd_message_steal_fds (fd_message, &num_fds);
629               if (worker->read_fd_list == NULL)
630                 {
631                   worker->read_fd_list = g_unix_fd_list_new_from_array (fds, num_fds);
632                 }
633               else
634                 {
635                   gint n;
636                   for (n = 0; n < num_fds; n++)
637                     {
638                       /* TODO: really want a append_steal() */
639                       g_unix_fd_list_append (worker->read_fd_list, fds[n], NULL);
640                       close (fds[n]);
641                     }
642                 }
643               g_free (fds);
644             }
645           else if (G_IS_UNIX_CREDENTIALS_MESSAGE (control_message))
646             {
647               /* do nothing */
648             }
649 #endif
650           else
651             {
652               if (error == NULL)
653                 {
654                   g_set_error (&error,
655                                G_IO_ERROR,
656                                G_IO_ERROR_FAILED,
657                                "Unexpected ancillary message of type %s received from peer",
658                                g_type_name (G_TYPE_FROM_INSTANCE (control_message)));
659                   _g_dbus_worker_emit_disconnected (worker, TRUE, error);
660                   g_error_free (error);
661                   g_object_unref (control_message);
662                   n++;
663                   while (n < worker->read_num_ancillary_messages)
664                     g_object_unref (worker->read_ancillary_messages[n++]);
665                   g_free (worker->read_ancillary_messages);
666                   goto out;
667                 }
668             }
669           g_object_unref (control_message);
670         }
671       g_free (worker->read_ancillary_messages);
672     }
673
674   if (bytes_read == -1)
675     {
676       _g_dbus_worker_emit_disconnected (worker, TRUE, error);
677       g_error_free (error);
678       goto out;
679     }
680
681 #if 0
682   g_debug ("read %d bytes (is_closed=%d blocking=%d condition=0x%02x) stream %p, %p",
683            (gint) bytes_read,
684            g_socket_is_closed (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream))),
685            g_socket_get_blocking (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream))),
686            g_socket_condition_check (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream)),
687                                      G_IO_IN | G_IO_OUT | G_IO_HUP),
688            worker->stream,
689            worker);
690 #endif
691
692   /* TODO: hmm, hmm... */
693   if (bytes_read == 0)
694     {
695       g_set_error (&error,
696                    G_IO_ERROR,
697                    G_IO_ERROR_FAILED,
698                    "Underlying GIOStream returned 0 bytes on an async read");
699       _g_dbus_worker_emit_disconnected (worker, TRUE, error);
700       g_error_free (error);
701       goto out;
702     }
703
704   read_message_print_transport_debug (bytes_read, worker);
705
706   worker->read_buffer_cur_size += bytes_read;
707   if (worker->read_buffer_bytes_wanted == worker->read_buffer_cur_size)
708     {
709       /* OK, got what we asked for! */
710       if (worker->read_buffer_bytes_wanted == 16)
711         {
712           gssize message_len;
713           /* OK, got the header - determine how many more bytes are needed */
714           error = NULL;
715           message_len = g_dbus_message_bytes_needed ((guchar *) worker->read_buffer,
716                                                      16,
717                                                      &error);
718           if (message_len == -1)
719             {
720               g_warning ("_g_dbus_worker_do_read_cb: error determing bytes needed: %s", error->message);
721               _g_dbus_worker_emit_disconnected (worker, FALSE, error);
722               g_error_free (error);
723               goto out;
724             }
725
726           worker->read_buffer_bytes_wanted = message_len;
727           _g_dbus_worker_do_read_unlocked (worker);
728         }
729       else
730         {
731           GDBusMessage *message;
732           error = NULL;
733
734           /* TODO: use connection->priv->auth to decode the message */
735
736           message = g_dbus_message_new_from_blob ((guchar *) worker->read_buffer,
737                                                   worker->read_buffer_cur_size,
738                                                   worker->capabilities,
739                                                   &error);
740           if (message == NULL)
741             {
742               gchar *s;
743               s = _g_dbus_hexdump (worker->read_buffer, worker->read_buffer_cur_size, 2);
744               g_warning ("Error decoding D-Bus message of %" G_GSIZE_FORMAT " bytes\n"
745                          "The error is: %s\n"
746                          "The payload is as follows:\n"
747                          "%s\n",
748                          worker->read_buffer_cur_size,
749                          error->message,
750                          s);
751               g_free (s);
752               _g_dbus_worker_emit_disconnected (worker, FALSE, error);
753               g_error_free (error);
754               goto out;
755             }
756
757 #ifdef G_OS_UNIX
758           if (worker->read_fd_list != NULL)
759             {
760               g_dbus_message_set_unix_fd_list (message, worker->read_fd_list);
761               g_object_unref (worker->read_fd_list);
762               worker->read_fd_list = NULL;
763             }
764 #endif
765
766           if (G_UNLIKELY (_g_dbus_debug_message ()))
767             {
768               gchar *s;
769               _g_dbus_debug_print_lock ();
770               g_print ("========================================================================\n"
771                        "GDBus-debug:Message:\n"
772                        "  <<<< RECEIVED D-Bus message (%" G_GSIZE_FORMAT " bytes)\n",
773                        worker->read_buffer_cur_size);
774               s = g_dbus_message_print (message, 2);
775               g_print ("%s", s);
776               g_free (s);
777               if (G_UNLIKELY (_g_dbus_debug_payload ()))
778                 {
779                   s = _g_dbus_hexdump (worker->read_buffer, worker->read_buffer_cur_size, 2);
780                   g_print ("%s\n", s);
781                   g_free (s);
782                 }
783               _g_dbus_debug_print_unlock ();
784             }
785
786           /* yay, got a message, go deliver it */
787           _g_dbus_worker_queue_or_deliver_received_message (worker, message);
788
789           /* start reading another message! */
790           worker->read_buffer_bytes_wanted = 0;
791           worker->read_buffer_cur_size = 0;
792           _g_dbus_worker_do_read_unlocked (worker);
793         }
794     }
795   else
796     {
797       /* didn't get all the bytes we requested - so repeat the request... */
798       _g_dbus_worker_do_read_unlocked (worker);
799     }
800
801  out:
802   g_mutex_unlock (worker->read_lock);
803
804   /* gives up the reference acquired when calling g_input_stream_read_async() */
805   _g_dbus_worker_unref (worker);
806 }
807
808 /* called in private thread shared by all GDBusConnection instances (with read-lock held) */
809 static void
810 _g_dbus_worker_do_read_unlocked (GDBusWorker *worker)
811 {
812   /* if bytes_wanted is zero, it means start reading a message */
813   if (worker->read_buffer_bytes_wanted == 0)
814     {
815       worker->read_buffer_cur_size = 0;
816       worker->read_buffer_bytes_wanted = 16;
817     }
818
819   /* ensure we have a (big enough) buffer */
820   if (worker->read_buffer == NULL || worker->read_buffer_bytes_wanted > worker->read_buffer_allocated_size)
821     {
822       /* TODO: 4096 is randomly chosen; might want a better chosen default minimum */
823       worker->read_buffer_allocated_size = MAX (worker->read_buffer_bytes_wanted, 4096);
824       worker->read_buffer = g_realloc (worker->read_buffer, worker->read_buffer_allocated_size);
825     }
826
827   if (worker->socket == NULL)
828     g_input_stream_read_async (g_io_stream_get_input_stream (worker->stream),
829                                worker->read_buffer + worker->read_buffer_cur_size,
830                                worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size,
831                                G_PRIORITY_DEFAULT,
832                                worker->cancellable,
833                                (GAsyncReadyCallback) _g_dbus_worker_do_read_cb,
834                                _g_dbus_worker_ref (worker));
835   else
836     {
837       worker->read_ancillary_messages = NULL;
838       worker->read_num_ancillary_messages = 0;
839       _g_socket_read_with_control_messages (worker->socket,
840                                             worker->read_buffer + worker->read_buffer_cur_size,
841                                             worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size,
842                                             &worker->read_ancillary_messages,
843                                             &worker->read_num_ancillary_messages,
844                                             G_PRIORITY_DEFAULT,
845                                             worker->cancellable,
846                                             (GAsyncReadyCallback) _g_dbus_worker_do_read_cb,
847                                             _g_dbus_worker_ref (worker));
848     }
849 }
850
851 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
852 static void
853 _g_dbus_worker_do_read (GDBusWorker *worker)
854 {
855   g_mutex_lock (worker->read_lock);
856   _g_dbus_worker_do_read_unlocked (worker);
857   g_mutex_unlock (worker->read_lock);
858 }
859
860 /* ---------------------------------------------------------------------------------------------------- */
861
862 struct _MessageToWriteData
863 {
864   GDBusWorker  *worker;
865   GDBusMessage *message;
866   gchar        *blob;
867   gsize         blob_size;
868
869   gsize               total_written;
870   GSimpleAsyncResult *simple;
871
872 };
873
874 static void
875 message_to_write_data_free (MessageToWriteData *data)
876 {
877   _g_dbus_worker_unref (data->worker);
878   if (data->message)
879     g_object_unref (data->message);
880   g_free (data->blob);
881   g_free (data);
882 }
883
884 /* ---------------------------------------------------------------------------------------------------- */
885
886 static void write_message_continue_writing (MessageToWriteData *data);
887
888 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
889 static void
890 write_message_async_cb (GObject      *source_object,
891                         GAsyncResult *res,
892                         gpointer      user_data)
893 {
894   MessageToWriteData *data = user_data;
895   GSimpleAsyncResult *simple;
896   gssize bytes_written;
897   GError *error;
898
899   /* Note: we can't access data->simple after calling g_async_result_complete () because the
900    * callback can free @data and we're not completing in idle. So use a copy of the pointer.
901    */
902   simple = data->simple;
903
904   error = NULL;
905   bytes_written = g_output_stream_write_finish (G_OUTPUT_STREAM (source_object),
906                                                 res,
907                                                 &error);
908   if (bytes_written == -1)
909     {
910       g_simple_async_result_take_error (simple, error);
911       g_simple_async_result_complete (simple);
912       g_object_unref (simple);
913       goto out;
914     }
915   g_assert (bytes_written > 0); /* zero is never returned */
916
917   write_message_print_transport_debug (bytes_written, data);
918
919   data->total_written += bytes_written;
920   g_assert (data->total_written <= data->blob_size);
921   if (data->total_written == data->blob_size)
922     {
923       g_simple_async_result_complete (simple);
924       g_object_unref (simple);
925       goto out;
926     }
927
928   write_message_continue_writing (data);
929
930  out:
931   ;
932 }
933
934 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
935 static gboolean
936 on_socket_ready (GSocket      *socket,
937                  GIOCondition  condition,
938                  gpointer      user_data)
939 {
940   MessageToWriteData *data = user_data;
941   write_message_continue_writing (data);
942   return FALSE; /* remove source */
943 }
944
945 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
946 static void
947 write_message_continue_writing (MessageToWriteData *data)
948 {
949   GOutputStream *ostream;
950   GSimpleAsyncResult *simple;
951 #ifdef G_OS_UNIX
952   GUnixFDList *fd_list;
953 #endif
954
955   /* Note: we can't access data->simple after calling g_async_result_complete () because the
956    * callback can free @data and we're not completing in idle. So use a copy of the pointer.
957    */
958   simple = data->simple;
959
960   ostream = g_io_stream_get_output_stream (data->worker->stream);
961 #ifdef G_OS_UNIX
962   fd_list = g_dbus_message_get_unix_fd_list (data->message);
963 #endif
964
965   g_assert (!g_output_stream_has_pending (ostream));
966   g_assert_cmpint (data->total_written, <, data->blob_size);
967
968   if (FALSE)
969     {
970     }
971 #ifdef G_OS_UNIX
972   else if (G_IS_SOCKET_OUTPUT_STREAM (ostream) && data->total_written == 0)
973     {
974       GOutputVector vector;
975       GSocketControlMessage *control_message;
976       gssize bytes_written;
977       GError *error;
978
979       vector.buffer = data->blob;
980       vector.size = data->blob_size;
981
982       control_message = NULL;
983       if (fd_list != NULL && g_unix_fd_list_get_length (fd_list) > 0)
984         {
985           if (!(data->worker->capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING))
986             {
987               g_simple_async_result_set_error (simple,
988                                                G_IO_ERROR,
989                                                G_IO_ERROR_FAILED,
990                                                "Tried sending a file descriptor but remote peer does not support this capability");
991               g_simple_async_result_complete (simple);
992               g_object_unref (simple);
993               goto out;
994             }
995           control_message = g_unix_fd_message_new_with_fd_list (fd_list);
996         }
997
998       error = NULL;
999       bytes_written = g_socket_send_message (data->worker->socket,
1000                                              NULL, /* address */
1001                                              &vector,
1002                                              1,
1003                                              control_message != NULL ? &control_message : NULL,
1004                                              control_message != NULL ? 1 : 0,
1005                                              G_SOCKET_MSG_NONE,
1006                                              data->worker->cancellable,
1007                                              &error);
1008       if (control_message != NULL)
1009         g_object_unref (control_message);
1010
1011       if (bytes_written == -1)
1012         {
1013           /* Handle WOULD_BLOCK by waiting until there's room in the buffer */
1014           if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1015             {
1016               GSource *source;
1017               source = g_socket_create_source (data->worker->socket,
1018                                                G_IO_OUT | G_IO_HUP | G_IO_ERR,
1019                                                data->worker->cancellable);
1020               g_source_set_callback (source,
1021                                      (GSourceFunc) on_socket_ready,
1022                                      data,
1023                                      NULL); /* GDestroyNotify */
1024               g_source_attach (source, g_main_context_get_thread_default ());
1025               g_source_unref (source);
1026               g_error_free (error);
1027               goto out;
1028             }
1029           g_simple_async_result_take_error (simple, error);
1030           g_simple_async_result_complete (simple);
1031           g_object_unref (simple);
1032           goto out;
1033         }
1034       g_assert (bytes_written > 0); /* zero is never returned */
1035
1036       write_message_print_transport_debug (bytes_written, data);
1037
1038       data->total_written += bytes_written;
1039       g_assert (data->total_written <= data->blob_size);
1040       if (data->total_written == data->blob_size)
1041         {
1042           g_simple_async_result_complete (simple);
1043           g_object_unref (simple);
1044           goto out;
1045         }
1046
1047       write_message_continue_writing (data);
1048     }
1049 #endif
1050   else
1051     {
1052 #ifdef G_OS_UNIX
1053       if (fd_list != NULL)
1054         {
1055           g_simple_async_result_set_error (simple,
1056                                            G_IO_ERROR,
1057                                            G_IO_ERROR_FAILED,
1058                                            "Tried sending a file descriptor on unsupported stream of type %s",
1059                                            g_type_name (G_TYPE_FROM_INSTANCE (ostream)));
1060           g_simple_async_result_complete (simple);
1061           g_object_unref (simple);
1062           goto out;
1063         }
1064 #endif
1065
1066       g_output_stream_write_async (ostream,
1067                                    (const gchar *) data->blob + data->total_written,
1068                                    data->blob_size - data->total_written,
1069                                    G_PRIORITY_DEFAULT,
1070                                    data->worker->cancellable,
1071                                    write_message_async_cb,
1072                                    data);
1073     }
1074  out:
1075   ;
1076 }
1077
1078 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
1079 static void
1080 write_message_async (GDBusWorker         *worker,
1081                      MessageToWriteData  *data,
1082                      GAsyncReadyCallback  callback,
1083                      gpointer             user_data)
1084 {
1085   data->simple = g_simple_async_result_new (NULL,
1086                                             callback,
1087                                             user_data,
1088                                             write_message_async);
1089   data->total_written = 0;
1090   write_message_continue_writing (data);
1091 }
1092
1093 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
1094 static gboolean
1095 write_message_finish (GAsyncResult   *res,
1096                       GError        **error)
1097 {
1098   g_warn_if_fail (g_simple_async_result_get_source_tag (G_SIMPLE_ASYNC_RESULT (res)) == write_message_async);
1099   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error))
1100     return FALSE;
1101   else
1102     return TRUE;
1103 }
1104 /* ---------------------------------------------------------------------------------------------------- */
1105
1106 static void maybe_write_next_message (GDBusWorker *worker);
1107
1108 typedef struct
1109 {
1110   GDBusWorker *worker;
1111   GList *flushers;
1112 } FlushAsyncData;
1113
1114 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
1115 static void
1116 ostream_flush_cb (GObject      *source_object,
1117                   GAsyncResult *res,
1118                   gpointer      user_data)
1119 {
1120   FlushAsyncData *data = user_data;
1121   GError *error;
1122   GList *l;
1123
1124   error = NULL;
1125   g_output_stream_flush_finish (G_OUTPUT_STREAM (source_object),
1126                                 res,
1127                                 &error);
1128
1129   if (error == NULL)
1130     {
1131       if (G_UNLIKELY (_g_dbus_debug_transport ()))
1132         {
1133           _g_dbus_debug_print_lock ();
1134           g_print ("========================================================================\n"
1135                    "GDBus-debug:Transport:\n"
1136                    "  ---- FLUSHED stream of type %s\n",
1137                    g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream))));
1138           _g_dbus_debug_print_unlock ();
1139         }
1140     }
1141
1142   g_assert (data->flushers != NULL);
1143   for (l = data->flushers; l != NULL; l = l->next)
1144     {
1145       FlushData *f = l->data;
1146
1147       f->error = error != NULL ? g_error_copy (error) : NULL;
1148
1149       g_mutex_lock (f->mutex);
1150       g_cond_signal (f->cond);
1151       g_mutex_unlock (f->mutex);
1152     }
1153   g_list_free (data->flushers);
1154
1155   if (error != NULL)
1156     g_error_free (error);
1157
1158   /* OK, cool, finally kick off the next write */
1159   maybe_write_next_message (data->worker);
1160
1161   _g_dbus_worker_unref (data->worker);
1162   g_free (data);
1163 }
1164
1165 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
1166 static void
1167 message_written (GDBusWorker *worker,
1168                  MessageToWriteData *message_data)
1169 {
1170   GList *l;
1171   GList *ll;
1172   GList *flushers;
1173
1174   /* first log the fact that we wrote a message */
1175   if (G_UNLIKELY (_g_dbus_debug_message ()))
1176     {
1177       gchar *s;
1178       _g_dbus_debug_print_lock ();
1179       g_print ("========================================================================\n"
1180                "GDBus-debug:Message:\n"
1181                "  >>>> SENT D-Bus message (%" G_GSIZE_FORMAT " bytes)\n",
1182                message_data->blob_size);
1183       s = g_dbus_message_print (message_data->message, 2);
1184       g_print ("%s", s);
1185       g_free (s);
1186       if (G_UNLIKELY (_g_dbus_debug_payload ()))
1187         {
1188           s = _g_dbus_hexdump (message_data->blob, message_data->blob_size, 2);
1189           g_print ("%s\n", s);
1190           g_free (s);
1191         }
1192       _g_dbus_debug_print_unlock ();
1193     }
1194
1195   /* then first wake up pending flushes and, if needed, flush the stream */
1196   flushers = NULL;
1197   g_mutex_lock (worker->write_lock);
1198   worker->write_num_messages_written += 1;
1199   for (l = worker->write_pending_flushes; l != NULL; l = ll)
1200     {
1201       FlushData *f = l->data;
1202       ll = l->next;
1203
1204       if (f->number_to_wait_for == worker->write_num_messages_written)
1205         {
1206           flushers = g_list_append (flushers, f);
1207           worker->write_pending_flushes = g_list_delete_link (worker->write_pending_flushes, l);
1208         }
1209     }
1210   g_mutex_unlock (worker->write_lock);
1211
1212   if (flushers != NULL)
1213     {
1214       FlushAsyncData *data;
1215       data = g_new0 (FlushAsyncData, 1);
1216       data->worker = _g_dbus_worker_ref (worker);
1217       data->flushers = flushers;
1218       /* flush the stream before writing the next message */
1219       g_output_stream_flush_async (g_io_stream_get_output_stream (worker->stream),
1220                                    G_PRIORITY_DEFAULT,
1221                                    worker->cancellable,
1222                                    ostream_flush_cb,
1223                                    data);
1224     }
1225   else
1226     {
1227       /* kick off the next write! */
1228       maybe_write_next_message (worker);
1229     }
1230 }
1231
1232 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
1233 static void
1234 write_message_cb (GObject       *source_object,
1235                   GAsyncResult  *res,
1236                   gpointer       user_data)
1237 {
1238   MessageToWriteData *data = user_data;
1239   GError *error;
1240
1241   g_mutex_lock (data->worker->write_lock);
1242   data->worker->num_writes_pending -= 1;
1243   g_mutex_unlock (data->worker->write_lock);
1244
1245   error = NULL;
1246   if (!write_message_finish (res, &error))
1247     {
1248       /* TODO: handle */
1249       _g_dbus_worker_emit_disconnected (data->worker, TRUE, error);
1250       g_error_free (error);
1251     }
1252
1253   /* this function will also kick of the next write (it might need to
1254    * flush so writing the next message might happen much later
1255    * e.g. async)
1256    */
1257   message_written (data->worker, data);
1258
1259   message_to_write_data_free (data);
1260 }
1261
1262 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
1263 static void
1264 maybe_write_next_message (GDBusWorker *worker)
1265 {
1266   MessageToWriteData *data;
1267
1268  write_next:
1269
1270   g_mutex_lock (worker->write_lock);
1271   data = g_queue_pop_head (worker->write_queue);
1272   if (data != NULL)
1273     worker->num_writes_pending += 1;
1274   g_mutex_unlock (worker->write_lock);
1275
1276   /* Note that write_lock is only used for protecting the @write_queue
1277    * and @num_writes_pending fields of the GDBusWorker struct ... which we
1278    * need to modify from arbitrary threads in _g_dbus_worker_send_message().
1279    *
1280    * Therefore, it's fine to drop it here when calling back into user
1281    * code and then writing the message out onto the GIOStream since this
1282    * function only runs on the worker thread.
1283    */
1284   if (data != NULL)
1285     {
1286       GDBusMessage *old_message;
1287       guchar *new_blob;
1288       gsize new_blob_size;
1289       GError *error;
1290
1291       old_message = data->message;
1292       data->message = _g_dbus_worker_emit_message_about_to_be_sent (worker, data->message);
1293       if (data->message == old_message)
1294         {
1295           /* filters had no effect - do nothing */
1296         }
1297       else if (data->message == NULL)
1298         {
1299           /* filters dropped message */
1300           g_mutex_lock (worker->write_lock);
1301           worker->num_writes_pending -= 1;
1302           g_mutex_unlock (worker->write_lock);
1303           message_to_write_data_free (data);
1304           goto write_next;
1305         }
1306       else
1307         {
1308           /* filters altered the message -> reencode */
1309           error = NULL;
1310           new_blob = g_dbus_message_to_blob (data->message,
1311                                              &new_blob_size,
1312                                              worker->capabilities,
1313                                              &error);
1314           if (new_blob == NULL)
1315             {
1316               /* if filter make the GDBusMessage unencodeable, just complain on stderr and send
1317                * the old message instead
1318                */
1319               g_warning ("Error encoding GDBusMessage with serial %d altered by filter function: %s",
1320                          g_dbus_message_get_serial (data->message),
1321                          error->message);
1322               g_error_free (error);
1323             }
1324           else
1325             {
1326               g_free (data->blob);
1327               data->blob = (gchar *) new_blob;
1328               data->blob_size = new_blob_size;
1329             }
1330         }
1331
1332       write_message_async (worker,
1333                            data,
1334                            write_message_cb,
1335                            data);
1336     }
1337 }
1338
1339 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
1340 static gboolean
1341 write_message_in_idle_cb (gpointer user_data)
1342 {
1343   GDBusWorker *worker = user_data;
1344   if (worker->num_writes_pending == 0)
1345     maybe_write_next_message (worker);
1346   return FALSE;
1347 }
1348
1349 /* ---------------------------------------------------------------------------------------------------- */
1350
1351 /* can be called from any thread - steals blob */
1352 void
1353 _g_dbus_worker_send_message (GDBusWorker    *worker,
1354                              GDBusMessage   *message,
1355                              gchar          *blob,
1356                              gsize           blob_len)
1357 {
1358   MessageToWriteData *data;
1359
1360   g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1361   g_return_if_fail (blob != NULL);
1362   g_return_if_fail (blob_len > 16);
1363
1364   data = g_new0 (MessageToWriteData, 1);
1365   data->worker = _g_dbus_worker_ref (worker);
1366   data->message = g_object_ref (message);
1367   data->blob = blob; /* steal! */
1368   data->blob_size = blob_len;
1369
1370   g_mutex_lock (worker->write_lock);
1371   g_queue_push_tail (worker->write_queue, data);
1372   if (worker->num_writes_pending == 0)
1373     {
1374       GSource *idle_source;
1375       idle_source = g_idle_source_new ();
1376       g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1377       g_source_set_callback (idle_source,
1378                              write_message_in_idle_cb,
1379                              _g_dbus_worker_ref (worker),
1380                              (GDestroyNotify) _g_dbus_worker_unref);
1381       g_source_attach (idle_source, shared_thread_data->context);
1382       g_source_unref (idle_source);
1383     }
1384   g_mutex_unlock (worker->write_lock);
1385 }
1386
1387 /* ---------------------------------------------------------------------------------------------------- */
1388
1389 static void
1390 _g_dbus_worker_thread_begin_func (gpointer user_data)
1391 {
1392   GDBusWorker *worker = user_data;
1393
1394   worker->thread = g_thread_self ();
1395
1396   /* begin reading */
1397   _g_dbus_worker_do_read (worker);
1398 }
1399
1400 GDBusWorker *
1401 _g_dbus_worker_new (GIOStream                              *stream,
1402                     GDBusCapabilityFlags                    capabilities,
1403                     gboolean                                initially_frozen,
1404                     GDBusWorkerMessageReceivedCallback      message_received_callback,
1405                     GDBusWorkerMessageAboutToBeSentCallback message_about_to_be_sent_callback,
1406                     GDBusWorkerDisconnectedCallback         disconnected_callback,
1407                     gpointer                                user_data)
1408 {
1409   GDBusWorker *worker;
1410
1411   g_return_val_if_fail (G_IS_IO_STREAM (stream), NULL);
1412   g_return_val_if_fail (message_received_callback != NULL, NULL);
1413   g_return_val_if_fail (message_about_to_be_sent_callback != NULL, NULL);
1414   g_return_val_if_fail (disconnected_callback != NULL, NULL);
1415
1416   worker = g_new0 (GDBusWorker, 1);
1417   worker->ref_count = 1;
1418
1419   worker->read_lock = g_mutex_new ();
1420   worker->message_received_callback = message_received_callback;
1421   worker->message_about_to_be_sent_callback = message_about_to_be_sent_callback;
1422   worker->disconnected_callback = disconnected_callback;
1423   worker->user_data = user_data;
1424   worker->stream = g_object_ref (stream);
1425   worker->capabilities = capabilities;
1426   worker->cancellable = g_cancellable_new ();
1427
1428   worker->frozen = initially_frozen;
1429   worker->received_messages_while_frozen = g_queue_new ();
1430
1431   worker->write_lock = g_mutex_new ();
1432   worker->write_queue = g_queue_new ();
1433
1434   if (G_IS_SOCKET_CONNECTION (worker->stream))
1435     worker->socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream));
1436
1437   _g_dbus_shared_thread_ref (_g_dbus_worker_thread_begin_func, worker);
1438
1439   return worker;
1440 }
1441
1442 /* ---------------------------------------------------------------------------------------------------- */
1443
1444 /* This can be called from any thread - frees worker. Note that
1445  * callbacks might still happen if called from another thread than the
1446  * worker - use your own synchronization primitive in the callbacks.
1447  */
1448 void
1449 _g_dbus_worker_stop (GDBusWorker *worker)
1450 {
1451   worker->stopped = TRUE;
1452   g_cancellable_cancel (worker->cancellable);
1453   _g_dbus_worker_unref (worker);
1454 }
1455
1456 /* ---------------------------------------------------------------------------------------------------- */
1457
1458 /* can be called from any thread (except the worker thread) - blocks
1459  * calling thread until all queued outgoing messages are written and
1460  * the transport has been flushed
1461  */
1462 gboolean
1463 _g_dbus_worker_flush_sync (GDBusWorker    *worker,
1464                            GCancellable   *cancellable,
1465                            GError        **error)
1466 {
1467   gboolean ret;
1468   FlushData *data;
1469
1470   data = NULL;
1471   ret = TRUE;
1472
1473   /* if the queue is empty, there's nothing to wait for */
1474   g_mutex_lock (worker->write_lock);
1475   if (g_queue_get_length (worker->write_queue) > 0)
1476     {
1477       data = g_new0 (FlushData, 1);
1478       data->mutex = g_mutex_new ();
1479       data->cond = g_cond_new ();
1480       data->number_to_wait_for = worker->write_num_messages_written + g_queue_get_length (worker->write_queue);
1481       g_mutex_lock (data->mutex);
1482       worker->write_pending_flushes = g_list_prepend (worker->write_pending_flushes, data);
1483     }
1484   g_mutex_unlock (worker->write_lock);
1485
1486   if (data != NULL)
1487     {
1488       g_cond_wait (data->cond, data->mutex);
1489       g_mutex_unlock (data->mutex);
1490
1491       /* note:the element is removed from worker->write_pending_flushes in flush_cb() above */
1492       g_cond_free (data->cond);
1493       g_mutex_free (data->mutex);
1494       if (data->error != NULL)
1495         {
1496           ret = FALSE;
1497           g_propagate_error (error, data->error);
1498         }
1499       g_free (data);
1500     }
1501
1502   return ret;
1503 }
1504
1505 /* ---------------------------------------------------------------------------------------------------- */
1506
1507 #define G_DBUS_DEBUG_AUTHENTICATION (1<<0)
1508 #define G_DBUS_DEBUG_TRANSPORT      (1<<1)
1509 #define G_DBUS_DEBUG_MESSAGE        (1<<2)
1510 #define G_DBUS_DEBUG_PAYLOAD        (1<<3)
1511 #define G_DBUS_DEBUG_CALL           (1<<4)
1512 #define G_DBUS_DEBUG_SIGNAL         (1<<5)
1513 #define G_DBUS_DEBUG_INCOMING       (1<<6)
1514 #define G_DBUS_DEBUG_RETURN         (1<<7)
1515 #define G_DBUS_DEBUG_EMISSION       (1<<8)
1516 #define G_DBUS_DEBUG_ADDRESS        (1<<9)
1517
1518 static gint _gdbus_debug_flags = 0;
1519
1520 gboolean
1521 _g_dbus_debug_authentication (void)
1522 {
1523   _g_dbus_initialize ();
1524   return (_gdbus_debug_flags & G_DBUS_DEBUG_AUTHENTICATION) != 0;
1525 }
1526
1527 gboolean
1528 _g_dbus_debug_transport (void)
1529 {
1530   _g_dbus_initialize ();
1531   return (_gdbus_debug_flags & G_DBUS_DEBUG_TRANSPORT) != 0;
1532 }
1533
1534 gboolean
1535 _g_dbus_debug_message (void)
1536 {
1537   _g_dbus_initialize ();
1538   return (_gdbus_debug_flags & G_DBUS_DEBUG_MESSAGE) != 0;
1539 }
1540
1541 gboolean
1542 _g_dbus_debug_payload (void)
1543 {
1544   _g_dbus_initialize ();
1545   return (_gdbus_debug_flags & G_DBUS_DEBUG_PAYLOAD) != 0;
1546 }
1547
1548 gboolean
1549 _g_dbus_debug_call (void)
1550 {
1551   _g_dbus_initialize ();
1552   return (_gdbus_debug_flags & G_DBUS_DEBUG_CALL) != 0;
1553 }
1554
1555 gboolean
1556 _g_dbus_debug_signal (void)
1557 {
1558   _g_dbus_initialize ();
1559   return (_gdbus_debug_flags & G_DBUS_DEBUG_SIGNAL) != 0;
1560 }
1561
1562 gboolean
1563 _g_dbus_debug_incoming (void)
1564 {
1565   _g_dbus_initialize ();
1566   return (_gdbus_debug_flags & G_DBUS_DEBUG_INCOMING) != 0;
1567 }
1568
1569 gboolean
1570 _g_dbus_debug_return (void)
1571 {
1572   _g_dbus_initialize ();
1573   return (_gdbus_debug_flags & G_DBUS_DEBUG_RETURN) != 0;
1574 }
1575
1576 gboolean
1577 _g_dbus_debug_emission (void)
1578 {
1579   _g_dbus_initialize ();
1580   return (_gdbus_debug_flags & G_DBUS_DEBUG_EMISSION) != 0;
1581 }
1582
1583 gboolean
1584 _g_dbus_debug_address (void)
1585 {
1586   _g_dbus_initialize ();
1587   return (_gdbus_debug_flags & G_DBUS_DEBUG_ADDRESS) != 0;
1588 }
1589
1590 G_LOCK_DEFINE_STATIC (print_lock);
1591
1592 void
1593 _g_dbus_debug_print_lock (void)
1594 {
1595   G_LOCK (print_lock);
1596 }
1597
1598 void
1599 _g_dbus_debug_print_unlock (void)
1600 {
1601   G_UNLOCK (print_lock);
1602 }
1603
1604 /*
1605  * _g_dbus_initialize:
1606  *
1607  * Does various one-time init things such as
1608  *
1609  *  - registering the G_DBUS_ERROR error domain
1610  *  - parses the G_DBUS_DEBUG environment variable
1611  */
1612 void
1613 _g_dbus_initialize (void)
1614 {
1615   static volatile gsize initialized = 0;
1616
1617   if (g_once_init_enter (&initialized))
1618     {
1619       volatile GQuark g_dbus_error_domain;
1620       const gchar *debug;
1621
1622       g_dbus_error_domain = G_DBUS_ERROR;
1623
1624       debug = g_getenv ("G_DBUS_DEBUG");
1625       if (debug != NULL)
1626         {
1627           const GDebugKey keys[] = {
1628             { "authentication", G_DBUS_DEBUG_AUTHENTICATION },
1629             { "transport",      G_DBUS_DEBUG_TRANSPORT      },
1630             { "message",        G_DBUS_DEBUG_MESSAGE        },
1631             { "payload",        G_DBUS_DEBUG_PAYLOAD        },
1632             { "call",           G_DBUS_DEBUG_CALL           },
1633             { "signal",         G_DBUS_DEBUG_SIGNAL         },
1634             { "incoming",       G_DBUS_DEBUG_INCOMING       },
1635             { "return",         G_DBUS_DEBUG_RETURN         },
1636             { "emission",       G_DBUS_DEBUG_EMISSION       },
1637             { "address",        G_DBUS_DEBUG_ADDRESS        }
1638           };
1639
1640           _gdbus_debug_flags = g_parse_debug_string (debug, keys, G_N_ELEMENTS (keys));
1641           if (_gdbus_debug_flags & G_DBUS_DEBUG_PAYLOAD)
1642             _gdbus_debug_flags |= G_DBUS_DEBUG_MESSAGE;
1643         }
1644
1645       g_once_init_leave (&initialized, 1);
1646     }
1647 }
1648
1649 /* ---------------------------------------------------------------------------------------------------- */
1650
1651 GVariantType *
1652 _g_dbus_compute_complete_signature (GDBusArgInfo **args)
1653 {
1654   const GVariantType *arg_types[256];
1655   guint n;
1656
1657   if (args)
1658     for (n = 0; args[n] != NULL; n++)
1659       {
1660         /* DBus places a hard limit of 255 on signature length.
1661          * therefore number of args must be less than 256.
1662          */
1663         g_assert (n < 256);
1664
1665         arg_types[n] = G_VARIANT_TYPE (args[n]->signature);
1666
1667         if G_UNLIKELY (arg_types[n] == NULL)
1668           return NULL;
1669       }
1670   else
1671     n = 0;
1672
1673   return g_variant_type_new_tuple (arg_types, n);
1674 }
1675
1676 /* ---------------------------------------------------------------------------------------------------- */
1677
1678 #ifdef G_OS_WIN32
1679
1680 extern BOOL WINAPI ConvertSidToStringSidA (PSID Sid, LPSTR *StringSid);
1681
1682 gchar *
1683 _g_dbus_win32_get_user_sid (void)
1684 {
1685   HANDLE h;
1686   TOKEN_USER *user;
1687   DWORD token_information_len;
1688   PSID psid;
1689   gchar *sid;
1690   gchar *ret;
1691
1692   ret = NULL;
1693   user = NULL;
1694   h = INVALID_HANDLE_VALUE;
1695
1696   if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &h))
1697     {
1698       g_warning ("OpenProcessToken failed with error code %d", (gint) GetLastError ());
1699       goto out;
1700     }
1701
1702   /* Get length of buffer */
1703   token_information_len = 0;
1704   if (!GetTokenInformation (h, TokenUser, NULL, 0, &token_information_len))
1705     {
1706       if (GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1707         {
1708           g_warning ("GetTokenInformation() failed with error code %d", (gint) GetLastError ());
1709           goto out;
1710         }
1711     }
1712   user = g_malloc (token_information_len);
1713   if (!GetTokenInformation (h, TokenUser, user, token_information_len, &token_information_len))
1714     {
1715       g_warning ("GetTokenInformation() failed with error code %d", (gint) GetLastError ());
1716       goto out;
1717     }
1718
1719   psid = user->User.Sid;
1720   if (!IsValidSid (psid))
1721     {
1722       g_warning ("Invalid SID");
1723       goto out;
1724     }
1725
1726   if (!ConvertSidToStringSidA (psid, &sid))
1727     {
1728       g_warning ("Invalid SID");
1729       goto out;
1730     }
1731
1732   ret = g_strdup (sid);
1733   LocalFree (sid);
1734
1735 out:
1736   g_free (user);
1737   if (h != INVALID_HANDLE_VALUE)
1738     CloseHandle (h);
1739   return ret;
1740 }
1741 #endif
1742
1743 /* ---------------------------------------------------------------------------------------------------- */
1744
1745 gchar *
1746 _g_dbus_get_machine_id (GError **error)
1747 {
1748   gchar *ret;
1749   /* TODO: use PACKAGE_LOCALSTATEDIR ? */
1750   ret = NULL;
1751   if (!g_file_get_contents ("/var/lib/dbus/machine-id",
1752                             &ret,
1753                             NULL,
1754                             error))
1755     {
1756       g_prefix_error (error, _("Unable to load /var/lib/dbus/machine-id: "));
1757     }
1758   else
1759     {
1760       /* TODO: validate value */
1761       g_strstrip (ret);
1762     }
1763   return ret;
1764 }
1765
1766 /* ---------------------------------------------------------------------------------------------------- */
1767
1768 gchar *
1769 _g_dbus_enum_to_string (GType enum_type, gint value)
1770 {
1771   gchar *ret;
1772   GEnumClass *klass;
1773   GEnumValue *enum_value;
1774
1775   klass = g_type_class_ref (enum_type);
1776   enum_value = g_enum_get_value (klass, value);
1777   if (enum_value != NULL)
1778     ret = g_strdup (enum_value->value_nick);
1779   else
1780     ret = g_strdup_printf ("unknown (value %d)", value);
1781   g_type_class_unref (klass);
1782   return ret;
1783 }
1784
1785 /* ---------------------------------------------------------------------------------------------------- */
1786
1787 static void
1788 write_message_print_transport_debug (gssize bytes_written,
1789                                      MessageToWriteData *data)
1790 {
1791   if (G_LIKELY (!_g_dbus_debug_transport ()))
1792     goto out;
1793
1794   _g_dbus_debug_print_lock ();
1795   g_print ("========================================================================\n"
1796            "GDBus-debug:Transport:\n"
1797            "  >>>> WROTE %" G_GSIZE_FORMAT " bytes of message with serial %d and\n"
1798            "       size %" G_GSIZE_FORMAT " from offset %" G_GSIZE_FORMAT " on a %s\n",
1799            bytes_written,
1800            g_dbus_message_get_serial (data->message),
1801            data->blob_size,
1802            data->total_written,
1803            g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream))));
1804   _g_dbus_debug_print_unlock ();
1805  out:
1806   ;
1807 }
1808
1809 /* ---------------------------------------------------------------------------------------------------- */
1810
1811 static void
1812 read_message_print_transport_debug (gssize bytes_read,
1813                                     GDBusWorker *worker)
1814 {
1815   gsize size;
1816   gint32 serial;
1817   gint32 message_length;
1818
1819   if (G_LIKELY (!_g_dbus_debug_transport ()))
1820     goto out;
1821
1822   size = bytes_read + worker->read_buffer_cur_size;
1823   serial = 0;
1824   message_length = 0;
1825   if (size >= 16)
1826     message_length = g_dbus_message_bytes_needed ((guchar *) worker->read_buffer, size, NULL);
1827   if (size >= 1)
1828     {
1829       switch (worker->read_buffer[0])
1830         {
1831         case 'l':
1832           if (size >= 12)
1833             serial = GUINT32_FROM_LE (((guint32 *) worker->read_buffer)[2]);
1834           break;
1835         case 'B':
1836           if (size >= 12)
1837             serial = GUINT32_FROM_BE (((guint32 *) worker->read_buffer)[2]);
1838           break;
1839         default:
1840           /* an error will be set elsewhere if this happens */
1841           goto out;
1842         }
1843     }
1844
1845     _g_dbus_debug_print_lock ();
1846   g_print ("========================================================================\n"
1847            "GDBus-debug:Transport:\n"
1848            "  <<<< READ %" G_GSIZE_FORMAT " bytes of message with serial %d and\n"
1849            "       size %d to offset %" G_GSIZE_FORMAT " from a %s\n",
1850            bytes_read,
1851            serial,
1852            message_length,
1853            worker->read_buffer_cur_size,
1854            g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_input_stream (worker->stream))));
1855   _g_dbus_debug_print_unlock ();
1856  out:
1857   ;
1858 }