1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
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.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
28 #include "gdbusprivate.h"
29 #include "gdbusmessage.h"
30 #include "gdbuserror.h"
31 #include "gdbusintrospection.h"
32 #include "gasyncresult.h"
33 #include "gsimpleasyncresult.h"
34 #include "ginputstream.h"
35 #include "gmemoryinputstream.h"
36 #include "giostream.h"
37 #include "glib/gstdio.h"
38 #include "gsocketcontrolmessage.h"
39 #include "gsocketconnection.h"
40 #include "gsocketoutputstream.h"
43 #include "gunixfdmessage.h"
44 #include "gunixconnection.h"
45 #include "gunixcredentialsmessage.h"
54 static gboolean _g_dbus_worker_do_initial_read (gpointer data);
56 /* ---------------------------------------------------------------------------------------------------- */
59 _g_dbus_hexdump (const gchar *data, gsize len, guint indent)
64 ret = g_string_new (NULL);
66 for (n = 0; n < len; n += 16)
68 g_string_append_printf (ret, "%*s%04x: ", indent, "", n);
70 for (m = n; m < n + 16; m++)
72 if (m > n && (m%4) == 0)
73 g_string_append_c (ret, ' ');
75 g_string_append_printf (ret, "%02x ", (guchar) data[m]);
77 g_string_append (ret, " ");
80 g_string_append (ret, " ");
82 for (m = n; m < len && m < n + 16; m++)
83 g_string_append_c (ret, g_ascii_isprint (data[m]) ? data[m] : '.');
85 g_string_append_c (ret, '\n');
88 return g_string_free (ret, FALSE);
91 /* ---------------------------------------------------------------------------------------------------- */
93 /* Unfortunately ancillary messages are discarded when reading from a
94 * socket using the GSocketInputStream abstraction. So we provide a
95 * very GInputStream-ish API that uses GSocket in this case (very
96 * similar to GSocketInputStream).
102 GCancellable *cancellable;
107 GSocketControlMessage ***messages;
110 GSimpleAsyncResult *simple;
112 gboolean from_mainloop;
113 } ReadWithControlData;
116 read_with_control_data_free (ReadWithControlData *data)
118 g_object_unref (data->socket);
119 if (data->cancellable != NULL)
120 g_object_unref (data->cancellable);
121 g_object_unref (data->simple);
126 _g_socket_read_with_control_messages_ready (GSocket *socket,
127 GIOCondition condition,
130 ReadWithControlData *data = user_data;
136 vector.buffer = data->buffer;
137 vector.size = data->count;
138 result = g_socket_receive_message (data->socket,
149 g_simple_async_result_set_op_res_gssize (data->simple, result);
153 g_assert (error != NULL);
154 g_simple_async_result_take_error (data->simple, error);
157 if (data->from_mainloop)
158 g_simple_async_result_complete (data->simple);
160 g_simple_async_result_complete_in_idle (data->simple);
166 _g_socket_read_with_control_messages (GSocket *socket,
169 GSocketControlMessage ***messages,
172 GCancellable *cancellable,
173 GAsyncReadyCallback callback,
176 ReadWithControlData *data;
178 data = g_new0 (ReadWithControlData, 1);
179 data->socket = g_object_ref (socket);
180 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
181 data->buffer = buffer;
183 data->messages = messages;
184 data->num_messages = num_messages;
186 data->simple = g_simple_async_result_new (G_OBJECT (socket),
189 _g_socket_read_with_control_messages);
190 g_simple_async_result_set_check_cancellable (data->simple, cancellable);
192 if (!g_socket_condition_check (socket, G_IO_IN))
195 data->from_mainloop = TRUE;
196 source = g_socket_create_source (data->socket,
197 G_IO_IN | G_IO_HUP | G_IO_ERR,
199 g_source_set_callback (source,
200 (GSourceFunc) _g_socket_read_with_control_messages_ready,
202 (GDestroyNotify) read_with_control_data_free);
203 g_source_attach (source, g_main_context_get_thread_default ());
204 g_source_unref (source);
208 _g_socket_read_with_control_messages_ready (data->socket, G_IO_IN, data);
209 read_with_control_data_free (data);
214 _g_socket_read_with_control_messages_finish (GSocket *socket,
215 GAsyncResult *result,
218 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
220 g_return_val_if_fail (G_IS_SOCKET (socket), -1);
221 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == _g_socket_read_with_control_messages);
223 if (g_simple_async_result_propagate_error (simple, error))
226 return g_simple_async_result_get_op_res_gssize (simple);
229 /* ---------------------------------------------------------------------------------------------------- */
231 /* Work-around for https://bugzilla.gnome.org/show_bug.cgi?id=627724 */
233 static GPtrArray *ensured_classes = NULL;
236 ensure_type (GType gtype)
238 g_ptr_array_add (ensured_classes, g_type_class_ref (gtype));
242 release_required_types (void)
244 g_ptr_array_foreach (ensured_classes, (GFunc) g_type_class_unref, NULL);
245 g_ptr_array_unref (ensured_classes);
246 ensured_classes = NULL;
250 ensure_required_types (void)
252 g_assert (ensured_classes == NULL);
253 ensured_classes = g_ptr_array_new ();
254 ensure_type (G_TYPE_SIMPLE_ASYNC_RESULT);
255 ensure_type (G_TYPE_MEMORY_INPUT_STREAM);
257 /* ---------------------------------------------------------------------------------------------------- */
261 volatile gint refcount;
263 GMainContext *context;
268 gdbus_shared_thread_func (gpointer user_data)
270 SharedThreadData *data = user_data;
272 g_main_context_push_thread_default (data->context);
273 g_main_loop_run (data->loop);
274 g_main_context_pop_thread_default (data->context);
276 release_required_types ();
281 /* ---------------------------------------------------------------------------------------------------- */
283 static SharedThreadData *
284 _g_dbus_shared_thread_ref (void)
286 static gsize shared_thread_data = 0;
287 SharedThreadData *ret;
289 if (g_once_init_enter (&shared_thread_data))
291 SharedThreadData *data;
293 /* Work-around for https://bugzilla.gnome.org/show_bug.cgi?id=627724 */
294 ensure_required_types ();
296 data = g_new0 (SharedThreadData, 1);
299 data->context = g_main_context_new ();
300 data->loop = g_main_loop_new (data->context, FALSE);
301 data->thread = g_thread_new ("gdbus",
302 gdbus_shared_thread_func,
304 /* We can cast between gsize and gpointer safely */
305 g_once_init_leave (&shared_thread_data, (gsize) data);
308 ret = (SharedThreadData*) shared_thread_data;
309 g_atomic_int_inc (&ret->refcount);
314 _g_dbus_shared_thread_unref (SharedThreadData *data)
316 /* TODO: actually destroy the shared thread here */
318 g_assert (data != NULL);
319 if (g_atomic_int_dec_and_test (&data->refcount))
321 g_main_loop_quit (data->loop);
322 //g_thread_join (data->thread);
323 g_main_loop_unref (data->loop);
324 g_main_context_unref (data->context);
329 /* ---------------------------------------------------------------------------------------------------- */
340 volatile gint ref_count;
342 SharedThreadData *shared_thread_data;
344 /* really a boolean, but GLib 2.28 lacks atomic boolean ops */
345 volatile gint stopped;
347 /* TODO: frozen (e.g. G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING) currently
348 * only affects messages received from the other peer (since GDBusServer is the
349 * only user) - we might want it to affect messages sent to the other peer too?
352 GDBusCapabilityFlags capabilities;
353 GQueue *received_messages_while_frozen;
356 GCancellable *cancellable;
357 GDBusWorkerMessageReceivedCallback message_received_callback;
358 GDBusWorkerMessageAboutToBeSentCallback message_about_to_be_sent_callback;
359 GDBusWorkerDisconnectedCallback disconnected_callback;
362 /* if not NULL, stream is GSocketConnection */
365 /* used for reading */
368 gsize read_buffer_allocated_size;
369 gsize read_buffer_cur_size;
370 gsize read_buffer_bytes_wanted;
371 GUnixFDList *read_fd_list;
372 GSocketControlMessage **read_ancillary_messages;
373 gint read_num_ancillary_messages;
375 /* Whether an async write, flush or close, or none of those, is pending.
376 * Only the worker thread may change its value, and only with the write_lock.
377 * Other threads may read its value when holding the write_lock.
378 * The worker thread may read its value at any time.
380 OutputPending output_pending;
381 /* used for writing */
383 /* queue of MessageToWriteData, protected by write_lock */
385 /* protected by write_lock */
386 guint64 write_num_messages_written;
387 /* number of messages we'd written out last time we flushed;
388 * protected by write_lock
390 guint64 write_num_messages_flushed;
391 /* list of FlushData, protected by write_lock */
392 GList *write_pending_flushes;
393 /* list of CloseData, protected by write_lock */
394 GList *pending_close_attempts;
395 /* no lock - only used from the worker thread */
396 gboolean close_expected;
399 static void _g_dbus_worker_unref (GDBusWorker *worker);
401 /* ---------------------------------------------------------------------------------------------------- */
407 guint64 number_to_wait_for;
411 struct _MessageToWriteData ;
412 typedef struct _MessageToWriteData MessageToWriteData;
414 static void message_to_write_data_free (MessageToWriteData *data);
416 static void read_message_print_transport_debug (gssize bytes_read,
417 GDBusWorker *worker);
419 static void write_message_print_transport_debug (gssize bytes_written,
420 MessageToWriteData *data);
424 GCancellable *cancellable;
425 GSimpleAsyncResult *result;
428 static void close_data_free (CloseData *close_data)
430 if (close_data->cancellable != NULL)
431 g_object_unref (close_data->cancellable);
433 if (close_data->result != NULL)
434 g_object_unref (close_data->result);
436 _g_dbus_worker_unref (close_data->worker);
437 g_slice_free (CloseData, close_data);
440 /* ---------------------------------------------------------------------------------------------------- */
443 _g_dbus_worker_ref (GDBusWorker *worker)
445 g_atomic_int_inc (&worker->ref_count);
450 _g_dbus_worker_unref (GDBusWorker *worker)
452 if (g_atomic_int_dec_and_test (&worker->ref_count))
454 g_assert (worker->write_pending_flushes == NULL);
456 _g_dbus_shared_thread_unref (worker->shared_thread_data);
458 g_object_unref (worker->stream);
460 g_mutex_clear (&worker->read_lock);
461 g_object_unref (worker->cancellable);
462 if (worker->read_fd_list != NULL)
463 g_object_unref (worker->read_fd_list);
465 g_queue_free_full (worker->received_messages_while_frozen, (GDestroyNotify) g_object_unref);
466 g_mutex_clear (&worker->write_lock);
467 g_queue_free_full (worker->write_queue, (GDestroyNotify) message_to_write_data_free);
468 g_free (worker->read_buffer);
475 _g_dbus_worker_emit_disconnected (GDBusWorker *worker,
476 gboolean remote_peer_vanished,
479 if (!g_atomic_int_get (&worker->stopped))
480 worker->disconnected_callback (worker, remote_peer_vanished, error, worker->user_data);
484 _g_dbus_worker_emit_message_received (GDBusWorker *worker,
485 GDBusMessage *message)
487 if (!g_atomic_int_get (&worker->stopped))
488 worker->message_received_callback (worker, message, worker->user_data);
491 static GDBusMessage *
492 _g_dbus_worker_emit_message_about_to_be_sent (GDBusWorker *worker,
493 GDBusMessage *message)
496 if (!g_atomic_int_get (&worker->stopped))
497 ret = worker->message_about_to_be_sent_callback (worker, message, worker->user_data);
503 /* can only be called from private thread with read-lock held - takes ownership of @message */
505 _g_dbus_worker_queue_or_deliver_received_message (GDBusWorker *worker,
506 GDBusMessage *message)
508 if (worker->frozen || g_queue_get_length (worker->received_messages_while_frozen) > 0)
511 g_queue_push_tail (worker->received_messages_while_frozen, message);
515 /* not frozen, nor anything in queue */
516 _g_dbus_worker_emit_message_received (worker, message);
517 g_object_unref (message);
521 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
523 unfreeze_in_idle_cb (gpointer user_data)
525 GDBusWorker *worker = user_data;
526 GDBusMessage *message;
528 g_mutex_lock (&worker->read_lock);
531 while ((message = g_queue_pop_head (worker->received_messages_while_frozen)) != NULL)
533 _g_dbus_worker_emit_message_received (worker, message);
534 g_object_unref (message);
536 worker->frozen = FALSE;
540 g_assert (g_queue_get_length (worker->received_messages_while_frozen) == 0);
542 g_mutex_unlock (&worker->read_lock);
546 /* can be called from any thread */
548 _g_dbus_worker_unfreeze (GDBusWorker *worker)
550 GSource *idle_source;
551 idle_source = g_idle_source_new ();
552 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
553 g_source_set_callback (idle_source,
555 _g_dbus_worker_ref (worker),
556 (GDestroyNotify) _g_dbus_worker_unref);
557 g_source_set_name (idle_source, "[gio] unfreeze_in_idle_cb");
558 g_source_attach (idle_source, worker->shared_thread_data->context);
559 g_source_unref (idle_source);
562 /* ---------------------------------------------------------------------------------------------------- */
564 static void _g_dbus_worker_do_read_unlocked (GDBusWorker *worker);
566 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
568 _g_dbus_worker_do_read_cb (GInputStream *input_stream,
572 GDBusWorker *worker = user_data;
576 g_mutex_lock (&worker->read_lock);
578 /* If already stopped, don't even process the reply */
579 if (g_atomic_int_get (&worker->stopped))
583 if (worker->socket == NULL)
584 bytes_read = g_input_stream_read_finish (g_io_stream_get_input_stream (worker->stream),
588 bytes_read = _g_socket_read_with_control_messages_finish (worker->socket,
591 if (worker->read_num_ancillary_messages > 0)
594 for (n = 0; n < worker->read_num_ancillary_messages; n++)
596 GSocketControlMessage *control_message = G_SOCKET_CONTROL_MESSAGE (worker->read_ancillary_messages[n]);
602 else if (G_IS_UNIX_FD_MESSAGE (control_message))
604 GUnixFDMessage *fd_message;
608 fd_message = G_UNIX_FD_MESSAGE (control_message);
609 fds = g_unix_fd_message_steal_fds (fd_message, &num_fds);
610 if (worker->read_fd_list == NULL)
612 worker->read_fd_list = g_unix_fd_list_new_from_array (fds, num_fds);
617 for (n = 0; n < num_fds; n++)
619 /* TODO: really want a append_steal() */
620 g_unix_fd_list_append (worker->read_fd_list, fds[n], NULL);
621 (void) g_close (fds[n], NULL);
626 else if (G_IS_UNIX_CREDENTIALS_MESSAGE (control_message))
638 "Unexpected ancillary message of type %s received from peer",
639 g_type_name (G_TYPE_FROM_INSTANCE (control_message)));
640 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
641 g_error_free (error);
642 g_object_unref (control_message);
644 while (n < worker->read_num_ancillary_messages)
645 g_object_unref (worker->read_ancillary_messages[n++]);
646 g_free (worker->read_ancillary_messages);
650 g_object_unref (control_message);
652 g_free (worker->read_ancillary_messages);
655 if (bytes_read == -1)
657 if (G_UNLIKELY (_g_dbus_debug_transport ()))
659 _g_dbus_debug_print_lock ();
660 g_print ("========================================================================\n"
661 "GDBus-debug:Transport:\n"
662 " ---- READ ERROR on stream of type %s:\n"
664 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_input_stream (worker->stream))),
665 g_quark_to_string (error->domain), error->code,
667 _g_dbus_debug_print_unlock ();
670 /* Every async read that uses this callback uses worker->cancellable
671 * as its GCancellable. worker->cancellable gets cancelled if and only
672 * if the GDBusConnection tells us to close (either via
673 * _g_dbus_worker_stop, which is called on last-unref, or directly),
674 * so a cancelled read must mean our connection was closed locally.
676 * If we're closing, other errors are possible - notably,
677 * G_IO_ERROR_CLOSED can be seen if we close the stream with an async
678 * read in-flight. It seems sensible to treat all read errors during
679 * closing as an expected thing that doesn't trip exit-on-close.
681 * Because close_expected can't be set until we get into the worker
682 * thread, but the cancellable is signalled sooner (from another
683 * thread), we do still need to check the error.
685 if (worker->close_expected ||
686 g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
687 _g_dbus_worker_emit_disconnected (worker, FALSE, NULL);
689 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
691 g_error_free (error);
696 g_debug ("read %d bytes (is_closed=%d blocking=%d condition=0x%02x) stream %p, %p",
698 g_socket_is_closed (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream))),
699 g_socket_get_blocking (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream))),
700 g_socket_condition_check (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream)),
701 G_IO_IN | G_IO_OUT | G_IO_HUP),
706 /* TODO: hmm, hmm... */
712 "Underlying GIOStream returned 0 bytes on an async read");
713 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
714 g_error_free (error);
718 read_message_print_transport_debug (bytes_read, worker);
720 worker->read_buffer_cur_size += bytes_read;
721 if (worker->read_buffer_bytes_wanted == worker->read_buffer_cur_size)
723 /* OK, got what we asked for! */
724 if (worker->read_buffer_bytes_wanted == 16)
727 /* OK, got the header - determine how many more bytes are needed */
729 message_len = g_dbus_message_bytes_needed ((guchar *) worker->read_buffer,
732 if (message_len == -1)
734 g_warning ("_g_dbus_worker_do_read_cb: error determining bytes needed: %s", error->message);
735 _g_dbus_worker_emit_disconnected (worker, FALSE, error);
736 g_error_free (error);
740 worker->read_buffer_bytes_wanted = message_len;
741 _g_dbus_worker_do_read_unlocked (worker);
745 GDBusMessage *message;
748 /* TODO: use connection->priv->auth to decode the message */
750 message = g_dbus_message_new_from_blob ((guchar *) worker->read_buffer,
751 worker->read_buffer_cur_size,
752 worker->capabilities,
757 s = _g_dbus_hexdump (worker->read_buffer, worker->read_buffer_cur_size, 2);
758 g_warning ("Error decoding D-Bus message of %" G_GSIZE_FORMAT " bytes\n"
760 "The payload is as follows:\n"
762 worker->read_buffer_cur_size,
766 _g_dbus_worker_emit_disconnected (worker, FALSE, error);
767 g_error_free (error);
772 if (worker->read_fd_list != NULL)
774 g_dbus_message_set_unix_fd_list (message, worker->read_fd_list);
775 g_object_unref (worker->read_fd_list);
776 worker->read_fd_list = NULL;
780 if (G_UNLIKELY (_g_dbus_debug_message ()))
783 _g_dbus_debug_print_lock ();
784 g_print ("========================================================================\n"
785 "GDBus-debug:Message:\n"
786 " <<<< RECEIVED D-Bus message (%" G_GSIZE_FORMAT " bytes)\n",
787 worker->read_buffer_cur_size);
788 s = g_dbus_message_print (message, 2);
791 if (G_UNLIKELY (_g_dbus_debug_payload ()))
793 s = _g_dbus_hexdump (worker->read_buffer, worker->read_buffer_cur_size, 2);
797 _g_dbus_debug_print_unlock ();
800 /* yay, got a message, go deliver it */
801 _g_dbus_worker_queue_or_deliver_received_message (worker, message);
803 /* start reading another message! */
804 worker->read_buffer_bytes_wanted = 0;
805 worker->read_buffer_cur_size = 0;
806 _g_dbus_worker_do_read_unlocked (worker);
811 /* didn't get all the bytes we requested - so repeat the request... */
812 _g_dbus_worker_do_read_unlocked (worker);
816 g_mutex_unlock (&worker->read_lock);
818 /* gives up the reference acquired when calling g_input_stream_read_async() */
819 _g_dbus_worker_unref (worker);
822 /* called in private thread shared by all GDBusConnection instances (with read-lock held) */
824 _g_dbus_worker_do_read_unlocked (GDBusWorker *worker)
826 /* Note that we do need to keep trying to read even if close_expected is
827 * true, because only failing a read causes us to signal 'closed'.
830 /* if bytes_wanted is zero, it means start reading a message */
831 if (worker->read_buffer_bytes_wanted == 0)
833 worker->read_buffer_cur_size = 0;
834 worker->read_buffer_bytes_wanted = 16;
837 /* ensure we have a (big enough) buffer */
838 if (worker->read_buffer == NULL || worker->read_buffer_bytes_wanted > worker->read_buffer_allocated_size)
840 /* TODO: 4096 is randomly chosen; might want a better chosen default minimum */
841 worker->read_buffer_allocated_size = MAX (worker->read_buffer_bytes_wanted, 4096);
842 worker->read_buffer = g_realloc (worker->read_buffer, worker->read_buffer_allocated_size);
845 if (worker->socket == NULL)
846 g_input_stream_read_async (g_io_stream_get_input_stream (worker->stream),
847 worker->read_buffer + worker->read_buffer_cur_size,
848 worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size,
851 (GAsyncReadyCallback) _g_dbus_worker_do_read_cb,
852 _g_dbus_worker_ref (worker));
855 worker->read_ancillary_messages = NULL;
856 worker->read_num_ancillary_messages = 0;
857 _g_socket_read_with_control_messages (worker->socket,
858 worker->read_buffer + worker->read_buffer_cur_size,
859 worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size,
860 &worker->read_ancillary_messages,
861 &worker->read_num_ancillary_messages,
864 (GAsyncReadyCallback) _g_dbus_worker_do_read_cb,
865 _g_dbus_worker_ref (worker));
869 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
871 _g_dbus_worker_do_initial_read (gpointer data)
873 GDBusWorker *worker = data;
874 g_mutex_lock (&worker->read_lock);
875 _g_dbus_worker_do_read_unlocked (worker);
876 g_mutex_unlock (&worker->read_lock);
880 /* ---------------------------------------------------------------------------------------------------- */
882 struct _MessageToWriteData
885 GDBusMessage *message;
890 GSimpleAsyncResult *simple;
895 message_to_write_data_free (MessageToWriteData *data)
897 _g_dbus_worker_unref (data->worker);
899 g_object_unref (data->message);
904 /* ---------------------------------------------------------------------------------------------------- */
906 static void write_message_continue_writing (MessageToWriteData *data);
908 /* called in private thread shared by all GDBusConnection instances
910 * write-lock is not held on entry
911 * output_pending is PENDING_WRITE on entry
914 write_message_async_cb (GObject *source_object,
918 MessageToWriteData *data = user_data;
919 GSimpleAsyncResult *simple;
920 gssize bytes_written;
923 /* Note: we can't access data->simple after calling g_async_result_complete () because the
924 * callback can free @data and we're not completing in idle. So use a copy of the pointer.
926 simple = data->simple;
929 bytes_written = g_output_stream_write_finish (G_OUTPUT_STREAM (source_object),
932 if (bytes_written == -1)
934 g_simple_async_result_take_error (simple, error);
935 g_simple_async_result_complete (simple);
936 g_object_unref (simple);
939 g_assert (bytes_written > 0); /* zero is never returned */
941 write_message_print_transport_debug (bytes_written, data);
943 data->total_written += bytes_written;
944 g_assert (data->total_written <= data->blob_size);
945 if (data->total_written == data->blob_size)
947 g_simple_async_result_complete (simple);
948 g_object_unref (simple);
952 write_message_continue_writing (data);
958 /* called in private thread shared by all GDBusConnection instances
960 * write-lock is not held on entry
961 * output_pending is PENDING_WRITE on entry
965 on_socket_ready (GSocket *socket,
966 GIOCondition condition,
969 MessageToWriteData *data = user_data;
970 write_message_continue_writing (data);
971 return FALSE; /* remove source */
975 /* called in private thread shared by all GDBusConnection instances
977 * write-lock is not held on entry
978 * output_pending is PENDING_WRITE on entry
981 write_message_continue_writing (MessageToWriteData *data)
983 GOutputStream *ostream;
985 GSimpleAsyncResult *simple;
986 GUnixFDList *fd_list;
990 /* Note: we can't access data->simple after calling g_async_result_complete () because the
991 * callback can free @data and we're not completing in idle. So use a copy of the pointer.
993 simple = data->simple;
996 ostream = g_io_stream_get_output_stream (data->worker->stream);
998 fd_list = g_dbus_message_get_unix_fd_list (data->message);
1001 g_assert (!g_output_stream_has_pending (ostream));
1002 g_assert_cmpint (data->total_written, <, data->blob_size);
1008 else if (G_IS_SOCKET_OUTPUT_STREAM (ostream) && data->total_written == 0)
1010 GOutputVector vector;
1011 GSocketControlMessage *control_message;
1012 gssize bytes_written;
1015 vector.buffer = data->blob;
1016 vector.size = data->blob_size;
1018 control_message = NULL;
1019 if (fd_list != NULL && g_unix_fd_list_get_length (fd_list) > 0)
1021 if (!(data->worker->capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING))
1023 g_simple_async_result_set_error (simple,
1026 "Tried sending a file descriptor but remote peer does not support this capability");
1027 g_simple_async_result_complete (simple);
1028 g_object_unref (simple);
1031 control_message = g_unix_fd_message_new_with_fd_list (fd_list);
1035 bytes_written = g_socket_send_message (data->worker->socket,
1039 control_message != NULL ? &control_message : NULL,
1040 control_message != NULL ? 1 : 0,
1042 data->worker->cancellable,
1044 if (control_message != NULL)
1045 g_object_unref (control_message);
1047 if (bytes_written == -1)
1049 /* Handle WOULD_BLOCK by waiting until there's room in the buffer */
1050 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1053 source = g_socket_create_source (data->worker->socket,
1054 G_IO_OUT | G_IO_HUP | G_IO_ERR,
1055 data->worker->cancellable);
1056 g_source_set_callback (source,
1057 (GSourceFunc) on_socket_ready,
1059 NULL); /* GDestroyNotify */
1060 g_source_attach (source, g_main_context_get_thread_default ());
1061 g_source_unref (source);
1062 g_error_free (error);
1065 g_simple_async_result_take_error (simple, error);
1066 g_simple_async_result_complete (simple);
1067 g_object_unref (simple);
1070 g_assert (bytes_written > 0); /* zero is never returned */
1072 write_message_print_transport_debug (bytes_written, data);
1074 data->total_written += bytes_written;
1075 g_assert (data->total_written <= data->blob_size);
1076 if (data->total_written == data->blob_size)
1078 g_simple_async_result_complete (simple);
1079 g_object_unref (simple);
1083 write_message_continue_writing (data);
1089 if (fd_list != NULL)
1091 g_simple_async_result_set_error (simple,
1094 "Tried sending a file descriptor on unsupported stream of type %s",
1095 g_type_name (G_TYPE_FROM_INSTANCE (ostream)));
1096 g_simple_async_result_complete (simple);
1097 g_object_unref (simple);
1102 g_output_stream_write_async (ostream,
1103 (const gchar *) data->blob + data->total_written,
1104 data->blob_size - data->total_written,
1106 data->worker->cancellable,
1107 write_message_async_cb,
1116 /* called in private thread shared by all GDBusConnection instances
1118 * write-lock is not held on entry
1119 * output_pending is PENDING_WRITE on entry
1122 write_message_async (GDBusWorker *worker,
1123 MessageToWriteData *data,
1124 GAsyncReadyCallback callback,
1127 data->simple = g_simple_async_result_new (NULL,
1130 write_message_async);
1131 data->total_written = 0;
1132 write_message_continue_writing (data);
1135 /* called in private thread shared by all GDBusConnection instances (with write-lock held) */
1137 write_message_finish (GAsyncResult *res,
1140 g_warn_if_fail (g_simple_async_result_get_source_tag (G_SIMPLE_ASYNC_RESULT (res)) == write_message_async);
1141 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error))
1146 /* ---------------------------------------------------------------------------------------------------- */
1148 static void continue_writing (GDBusWorker *worker);
1152 GDBusWorker *worker;
1157 flush_data_list_complete (const GList *flushers,
1158 const GError *error)
1162 for (l = flushers; l != NULL; l = l->next)
1164 FlushData *f = l->data;
1166 f->error = error != NULL ? g_error_copy (error) : NULL;
1168 g_mutex_lock (&f->mutex);
1169 g_cond_signal (&f->cond);
1170 g_mutex_unlock (&f->mutex);
1174 /* called in private thread shared by all GDBusConnection instances
1176 * write-lock is not held on entry
1177 * output_pending is PENDING_FLUSH on entry
1180 ostream_flush_cb (GObject *source_object,
1184 FlushAsyncData *data = user_data;
1188 g_output_stream_flush_finish (G_OUTPUT_STREAM (source_object),
1194 if (G_UNLIKELY (_g_dbus_debug_transport ()))
1196 _g_dbus_debug_print_lock ();
1197 g_print ("========================================================================\n"
1198 "GDBus-debug:Transport:\n"
1199 " ---- FLUSHED stream of type %s\n",
1200 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream))));
1201 _g_dbus_debug_print_unlock ();
1205 g_assert (data->flushers != NULL);
1206 flush_data_list_complete (data->flushers, error);
1207 g_list_free (data->flushers);
1210 g_error_free (error);
1212 /* Make sure we tell folks that we don't have additional
1214 g_mutex_lock (&data->worker->write_lock);
1215 data->worker->write_num_messages_flushed = data->worker->write_num_messages_written;
1216 g_assert (data->worker->output_pending == PENDING_FLUSH);
1217 data->worker->output_pending = PENDING_NONE;
1218 g_mutex_unlock (&data->worker->write_lock);
1220 /* OK, cool, finally kick off the next write */
1221 continue_writing (data->worker);
1223 _g_dbus_worker_unref (data->worker);
1227 /* called in private thread shared by all GDBusConnection instances
1229 * write-lock is not held on entry
1230 * output_pending is PENDING_FLUSH on entry
1233 start_flush (FlushAsyncData *data)
1235 g_output_stream_flush_async (g_io_stream_get_output_stream (data->worker->stream),
1237 data->worker->cancellable,
1242 /* called in private thread shared by all GDBusConnection instances
1244 * write-lock is held on entry
1245 * output_pending is PENDING_NONE on entry
1248 message_written_unlocked (GDBusWorker *worker,
1249 MessageToWriteData *message_data)
1251 if (G_UNLIKELY (_g_dbus_debug_message ()))
1254 _g_dbus_debug_print_lock ();
1255 g_print ("========================================================================\n"
1256 "GDBus-debug:Message:\n"
1257 " >>>> SENT D-Bus message (%" G_GSIZE_FORMAT " bytes)\n",
1258 message_data->blob_size);
1259 s = g_dbus_message_print (message_data->message, 2);
1262 if (G_UNLIKELY (_g_dbus_debug_payload ()))
1264 s = _g_dbus_hexdump (message_data->blob, message_data->blob_size, 2);
1265 g_print ("%s\n", s);
1268 _g_dbus_debug_print_unlock ();
1271 worker->write_num_messages_written += 1;
1274 /* called in private thread shared by all GDBusConnection instances
1276 * write-lock is held on entry
1277 * output_pending is PENDING_NONE on entry
1279 * Returns: non-%NULL, setting @output_pending, if we need to flush now
1281 static FlushAsyncData *
1282 prepare_flush_unlocked (GDBusWorker *worker)
1289 for (l = worker->write_pending_flushes; l != NULL; l = ll)
1291 FlushData *f = l->data;
1294 if (f->number_to_wait_for == worker->write_num_messages_written)
1296 flushers = g_list_append (flushers, f);
1297 worker->write_pending_flushes = g_list_delete_link (worker->write_pending_flushes, l);
1300 if (flushers != NULL)
1302 g_assert (worker->output_pending == PENDING_NONE);
1303 worker->output_pending = PENDING_FLUSH;
1306 if (flushers != NULL)
1308 FlushAsyncData *data;
1310 data = g_new0 (FlushAsyncData, 1);
1311 data->worker = _g_dbus_worker_ref (worker);
1312 data->flushers = flushers;
1319 /* called in private thread shared by all GDBusConnection instances
1321 * write-lock is not held on entry
1322 * output_pending is PENDING_WRITE on entry
1325 write_message_cb (GObject *source_object,
1329 MessageToWriteData *data = user_data;
1332 g_mutex_lock (&data->worker->write_lock);
1333 g_assert (data->worker->output_pending == PENDING_WRITE);
1334 data->worker->output_pending = PENDING_NONE;
1337 if (!write_message_finish (res, &error))
1339 g_mutex_unlock (&data->worker->write_lock);
1342 _g_dbus_worker_emit_disconnected (data->worker, TRUE, error);
1343 g_error_free (error);
1345 g_mutex_lock (&data->worker->write_lock);
1348 message_written_unlocked (data->worker, data);
1350 g_mutex_unlock (&data->worker->write_lock);
1352 continue_writing (data->worker);
1354 message_to_write_data_free (data);
1357 /* called in private thread shared by all GDBusConnection instances
1359 * write-lock is not held on entry
1360 * output_pending is PENDING_CLOSE on entry
1363 iostream_close_cb (GObject *source_object,
1367 GDBusWorker *worker = user_data;
1368 GError *error = NULL;
1369 GList *pending_close_attempts, *pending_flush_attempts;
1372 g_io_stream_close_finish (worker->stream, res, &error);
1374 g_mutex_lock (&worker->write_lock);
1376 pending_close_attempts = worker->pending_close_attempts;
1377 worker->pending_close_attempts = NULL;
1379 pending_flush_attempts = worker->write_pending_flushes;
1380 worker->write_pending_flushes = NULL;
1382 send_queue = worker->write_queue;
1383 worker->write_queue = g_queue_new ();
1385 g_assert (worker->output_pending == PENDING_CLOSE);
1386 worker->output_pending = PENDING_NONE;
1388 g_mutex_unlock (&worker->write_lock);
1390 while (pending_close_attempts != NULL)
1392 CloseData *close_data = pending_close_attempts->data;
1394 pending_close_attempts = g_list_delete_link (pending_close_attempts,
1395 pending_close_attempts);
1397 if (close_data->result != NULL)
1400 g_simple_async_result_set_from_error (close_data->result, error);
1402 /* this must be in an idle because the result is likely to be
1403 * intended for another thread
1405 g_simple_async_result_complete_in_idle (close_data->result);
1408 close_data_free (close_data);
1411 g_clear_error (&error);
1413 /* all messages queued for sending are discarded */
1414 g_queue_free_full (send_queue, (GDestroyNotify) message_to_write_data_free);
1415 /* all queued flushes fail */
1416 error = g_error_new (G_IO_ERROR, G_IO_ERROR_CANCELLED,
1417 _("Operation was cancelled"));
1418 flush_data_list_complete (pending_flush_attempts, error);
1419 g_list_free (pending_flush_attempts);
1420 g_clear_error (&error);
1422 _g_dbus_worker_unref (worker);
1425 /* called in private thread shared by all GDBusConnection instances
1427 * write-lock is not held on entry
1428 * output_pending must be PENDING_NONE on entry
1431 continue_writing (GDBusWorker *worker)
1433 MessageToWriteData *data;
1434 FlushAsyncData *flush_async_data;
1437 /* we mustn't try to write two things at once */
1438 g_assert (worker->output_pending == PENDING_NONE);
1440 g_mutex_lock (&worker->write_lock);
1443 flush_async_data = NULL;
1445 /* if we want to close the connection, that takes precedence */
1446 if (worker->pending_close_attempts != NULL)
1448 worker->close_expected = TRUE;
1449 worker->output_pending = PENDING_CLOSE;
1451 g_io_stream_close_async (worker->stream, G_PRIORITY_DEFAULT,
1452 NULL, iostream_close_cb,
1453 _g_dbus_worker_ref (worker));
1457 flush_async_data = prepare_flush_unlocked (worker);
1459 if (flush_async_data == NULL)
1461 data = g_queue_pop_head (worker->write_queue);
1464 worker->output_pending = PENDING_WRITE;
1468 g_mutex_unlock (&worker->write_lock);
1470 /* Note that write_lock is only used for protecting the @write_queue
1471 * and @output_pending fields of the GDBusWorker struct ... which we
1472 * need to modify from arbitrary threads in _g_dbus_worker_send_message().
1474 * Therefore, it's fine to drop it here when calling back into user
1475 * code and then writing the message out onto the GIOStream since this
1476 * function only runs on the worker thread.
1479 if (flush_async_data != NULL)
1481 start_flush (flush_async_data);
1482 g_assert (data == NULL);
1484 else if (data != NULL)
1486 GDBusMessage *old_message;
1488 gsize new_blob_size;
1491 old_message = data->message;
1492 data->message = _g_dbus_worker_emit_message_about_to_be_sent (worker, data->message);
1493 if (data->message == old_message)
1495 /* filters had no effect - do nothing */
1497 else if (data->message == NULL)
1499 /* filters dropped message */
1500 g_mutex_lock (&worker->write_lock);
1501 worker->output_pending = PENDING_NONE;
1502 g_mutex_unlock (&worker->write_lock);
1503 message_to_write_data_free (data);
1508 /* filters altered the message -> reencode */
1510 new_blob = g_dbus_message_to_blob (data->message,
1512 worker->capabilities,
1514 if (new_blob == NULL)
1516 /* if filter make the GDBusMessage unencodeable, just complain on stderr and send
1517 * the old message instead
1519 g_warning ("Error encoding GDBusMessage with serial %d altered by filter function: %s",
1520 g_dbus_message_get_serial (data->message),
1522 g_error_free (error);
1526 g_free (data->blob);
1527 data->blob = (gchar *) new_blob;
1528 data->blob_size = new_blob_size;
1532 write_message_async (worker,
1539 /* called in private thread shared by all GDBusConnection instances
1541 * write-lock is not held on entry
1542 * output_pending may be anything
1545 continue_writing_in_idle_cb (gpointer user_data)
1547 GDBusWorker *worker = user_data;
1549 /* Because this is the worker thread, we can read this struct member
1550 * without holding the lock: no other thread ever modifies it.
1552 if (worker->output_pending == PENDING_NONE)
1553 continue_writing (worker);
1559 * @write_data: (transfer full) (allow-none):
1560 * @flush_data: (transfer full) (allow-none):
1561 * @close_data: (transfer full) (allow-none):
1563 * Can be called from any thread
1565 * write_lock is held on entry
1566 * output_pending may be anything
1569 schedule_writing_unlocked (GDBusWorker *worker,
1570 MessageToWriteData *write_data,
1571 FlushData *flush_data,
1572 CloseData *close_data)
1574 if (write_data != NULL)
1575 g_queue_push_tail (worker->write_queue, write_data);
1577 if (flush_data != NULL)
1578 worker->write_pending_flushes = g_list_prepend (worker->write_pending_flushes, flush_data);
1580 if (close_data != NULL)
1581 worker->pending_close_attempts = g_list_prepend (worker->pending_close_attempts,
1584 /* If we had output pending, the next bit of output will happen
1585 * automatically when it finishes, so we only need to do this
1586 * if nothing was pending.
1588 * The idle callback will re-check that output_pending is still
1589 * PENDING_NONE, to guard against output starting before the idle.
1591 if (worker->output_pending == PENDING_NONE)
1593 GSource *idle_source;
1594 idle_source = g_idle_source_new ();
1595 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1596 g_source_set_callback (idle_source,
1597 continue_writing_in_idle_cb,
1598 _g_dbus_worker_ref (worker),
1599 (GDestroyNotify) _g_dbus_worker_unref);
1600 g_source_set_name (idle_source, "[gio] continue_writing_in_idle_cb");
1601 g_source_attach (idle_source, worker->shared_thread_data->context);
1602 g_source_unref (idle_source);
1606 /* ---------------------------------------------------------------------------------------------------- */
1608 /* can be called from any thread - steals blob
1610 * write_lock is not held on entry
1611 * output_pending may be anything
1614 _g_dbus_worker_send_message (GDBusWorker *worker,
1615 GDBusMessage *message,
1619 MessageToWriteData *data;
1621 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1622 g_return_if_fail (blob != NULL);
1623 g_return_if_fail (blob_len > 16);
1625 data = g_new0 (MessageToWriteData, 1);
1626 data->worker = _g_dbus_worker_ref (worker);
1627 data->message = g_object_ref (message);
1628 data->blob = blob; /* steal! */
1629 data->blob_size = blob_len;
1631 g_mutex_lock (&worker->write_lock);
1632 schedule_writing_unlocked (worker, data, NULL, NULL);
1633 g_mutex_unlock (&worker->write_lock);
1636 /* ---------------------------------------------------------------------------------------------------- */
1639 _g_dbus_worker_new (GIOStream *stream,
1640 GDBusCapabilityFlags capabilities,
1641 gboolean initially_frozen,
1642 GDBusWorkerMessageReceivedCallback message_received_callback,
1643 GDBusWorkerMessageAboutToBeSentCallback message_about_to_be_sent_callback,
1644 GDBusWorkerDisconnectedCallback disconnected_callback,
1647 GDBusWorker *worker;
1648 GSource *idle_source;
1650 g_return_val_if_fail (G_IS_IO_STREAM (stream), NULL);
1651 g_return_val_if_fail (message_received_callback != NULL, NULL);
1652 g_return_val_if_fail (message_about_to_be_sent_callback != NULL, NULL);
1653 g_return_val_if_fail (disconnected_callback != NULL, NULL);
1655 worker = g_new0 (GDBusWorker, 1);
1656 worker->ref_count = 1;
1658 g_mutex_init (&worker->read_lock);
1659 worker->message_received_callback = message_received_callback;
1660 worker->message_about_to_be_sent_callback = message_about_to_be_sent_callback;
1661 worker->disconnected_callback = disconnected_callback;
1662 worker->user_data = user_data;
1663 worker->stream = g_object_ref (stream);
1664 worker->capabilities = capabilities;
1665 worker->cancellable = g_cancellable_new ();
1666 worker->output_pending = PENDING_NONE;
1668 worker->frozen = initially_frozen;
1669 worker->received_messages_while_frozen = g_queue_new ();
1671 g_mutex_init (&worker->write_lock);
1672 worker->write_queue = g_queue_new ();
1674 if (G_IS_SOCKET_CONNECTION (worker->stream))
1675 worker->socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream));
1677 worker->shared_thread_data = _g_dbus_shared_thread_ref ();
1680 idle_source = g_idle_source_new ();
1681 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1682 g_source_set_callback (idle_source,
1683 _g_dbus_worker_do_initial_read,
1684 _g_dbus_worker_ref (worker),
1685 (GDestroyNotify) _g_dbus_worker_unref);
1686 g_source_set_name (idle_source, "[gio] _g_dbus_worker_do_initial_read");
1687 g_source_attach (idle_source, worker->shared_thread_data->context);
1688 g_source_unref (idle_source);
1693 /* ---------------------------------------------------------------------------------------------------- */
1695 /* can be called from any thread
1697 * write_lock is not held on entry
1698 * output_pending may be anything
1701 _g_dbus_worker_close (GDBusWorker *worker,
1702 GCancellable *cancellable,
1703 GSimpleAsyncResult *result)
1705 CloseData *close_data;
1707 close_data = g_slice_new0 (CloseData);
1708 close_data->worker = _g_dbus_worker_ref (worker);
1709 close_data->cancellable =
1710 (cancellable == NULL ? NULL : g_object_ref (cancellable));
1711 close_data->result = (result == NULL ? NULL : g_object_ref (result));
1713 /* Don't set worker->close_expected here - we're in the wrong thread.
1714 * It'll be set before the actual close happens.
1716 g_cancellable_cancel (worker->cancellable);
1717 g_mutex_lock (&worker->write_lock);
1718 schedule_writing_unlocked (worker, NULL, NULL, close_data);
1719 g_mutex_unlock (&worker->write_lock);
1722 /* This can be called from any thread - frees worker. Note that
1723 * callbacks might still happen if called from another thread than the
1724 * worker - use your own synchronization primitive in the callbacks.
1726 * write_lock is not held on entry
1727 * output_pending may be anything
1730 _g_dbus_worker_stop (GDBusWorker *worker)
1732 g_atomic_int_set (&worker->stopped, TRUE);
1734 /* Cancel any pending operations and schedule a close of the underlying I/O
1735 * stream in the worker thread
1737 _g_dbus_worker_close (worker, NULL, NULL);
1739 /* _g_dbus_worker_close holds a ref until after an idle in the worker
1740 * thread has run, so we no longer need to unref in an idle like in
1743 _g_dbus_worker_unref (worker);
1746 /* ---------------------------------------------------------------------------------------------------- */
1748 /* can be called from any thread (except the worker thread) - blocks
1749 * calling thread until all queued outgoing messages are written and
1750 * the transport has been flushed
1752 * write_lock is not held on entry
1753 * output_pending may be anything
1756 _g_dbus_worker_flush_sync (GDBusWorker *worker,
1757 GCancellable *cancellable,
1762 guint64 pending_writes;
1767 g_mutex_lock (&worker->write_lock);
1769 /* if the queue is empty, no write is in-flight and we haven't written
1770 * anything since the last flush, then there's nothing to wait for
1772 pending_writes = g_queue_get_length (worker->write_queue);
1774 /* if a write is in-flight, we shouldn't be satisfied until the first
1775 * flush operation that follows it
1777 if (worker->output_pending == PENDING_WRITE)
1778 pending_writes += 1;
1780 if (pending_writes > 0 ||
1781 worker->write_num_messages_written != worker->write_num_messages_flushed)
1783 data = g_new0 (FlushData, 1);
1784 g_mutex_init (&data->mutex);
1785 g_cond_init (&data->cond);
1786 data->number_to_wait_for = worker->write_num_messages_written + pending_writes;
1787 g_mutex_lock (&data->mutex);
1789 schedule_writing_unlocked (worker, NULL, data, NULL);
1791 g_mutex_unlock (&worker->write_lock);
1795 g_cond_wait (&data->cond, &data->mutex);
1796 g_mutex_unlock (&data->mutex);
1798 /* note:the element is removed from worker->write_pending_flushes in flush_cb() above */
1799 g_cond_clear (&data->cond);
1800 g_mutex_clear (&data->mutex);
1801 if (data->error != NULL)
1804 g_propagate_error (error, data->error);
1812 /* ---------------------------------------------------------------------------------------------------- */
1814 #define G_DBUS_DEBUG_AUTHENTICATION (1<<0)
1815 #define G_DBUS_DEBUG_TRANSPORT (1<<1)
1816 #define G_DBUS_DEBUG_MESSAGE (1<<2)
1817 #define G_DBUS_DEBUG_PAYLOAD (1<<3)
1818 #define G_DBUS_DEBUG_CALL (1<<4)
1819 #define G_DBUS_DEBUG_SIGNAL (1<<5)
1820 #define G_DBUS_DEBUG_INCOMING (1<<6)
1821 #define G_DBUS_DEBUG_RETURN (1<<7)
1822 #define G_DBUS_DEBUG_EMISSION (1<<8)
1823 #define G_DBUS_DEBUG_ADDRESS (1<<9)
1825 static gint _gdbus_debug_flags = 0;
1828 _g_dbus_debug_authentication (void)
1830 _g_dbus_initialize ();
1831 return (_gdbus_debug_flags & G_DBUS_DEBUG_AUTHENTICATION) != 0;
1835 _g_dbus_debug_transport (void)
1837 _g_dbus_initialize ();
1838 return (_gdbus_debug_flags & G_DBUS_DEBUG_TRANSPORT) != 0;
1842 _g_dbus_debug_message (void)
1844 _g_dbus_initialize ();
1845 return (_gdbus_debug_flags & G_DBUS_DEBUG_MESSAGE) != 0;
1849 _g_dbus_debug_payload (void)
1851 _g_dbus_initialize ();
1852 return (_gdbus_debug_flags & G_DBUS_DEBUG_PAYLOAD) != 0;
1856 _g_dbus_debug_call (void)
1858 _g_dbus_initialize ();
1859 return (_gdbus_debug_flags & G_DBUS_DEBUG_CALL) != 0;
1863 _g_dbus_debug_signal (void)
1865 _g_dbus_initialize ();
1866 return (_gdbus_debug_flags & G_DBUS_DEBUG_SIGNAL) != 0;
1870 _g_dbus_debug_incoming (void)
1872 _g_dbus_initialize ();
1873 return (_gdbus_debug_flags & G_DBUS_DEBUG_INCOMING) != 0;
1877 _g_dbus_debug_return (void)
1879 _g_dbus_initialize ();
1880 return (_gdbus_debug_flags & G_DBUS_DEBUG_RETURN) != 0;
1884 _g_dbus_debug_emission (void)
1886 _g_dbus_initialize ();
1887 return (_gdbus_debug_flags & G_DBUS_DEBUG_EMISSION) != 0;
1891 _g_dbus_debug_address (void)
1893 _g_dbus_initialize ();
1894 return (_gdbus_debug_flags & G_DBUS_DEBUG_ADDRESS) != 0;
1897 G_LOCK_DEFINE_STATIC (print_lock);
1900 _g_dbus_debug_print_lock (void)
1902 G_LOCK (print_lock);
1906 _g_dbus_debug_print_unlock (void)
1908 G_UNLOCK (print_lock);
1912 * _g_dbus_initialize:
1914 * Does various one-time init things such as
1916 * - registering the G_DBUS_ERROR error domain
1917 * - parses the G_DBUS_DEBUG environment variable
1920 _g_dbus_initialize (void)
1922 static volatile gsize initialized = 0;
1924 if (g_once_init_enter (&initialized))
1926 volatile GQuark g_dbus_error_domain;
1929 g_dbus_error_domain = G_DBUS_ERROR;
1930 (g_dbus_error_domain); /* To avoid -Wunused-but-set-variable */
1932 debug = g_getenv ("G_DBUS_DEBUG");
1935 const GDebugKey keys[] = {
1936 { "authentication", G_DBUS_DEBUG_AUTHENTICATION },
1937 { "transport", G_DBUS_DEBUG_TRANSPORT },
1938 { "message", G_DBUS_DEBUG_MESSAGE },
1939 { "payload", G_DBUS_DEBUG_PAYLOAD },
1940 { "call", G_DBUS_DEBUG_CALL },
1941 { "signal", G_DBUS_DEBUG_SIGNAL },
1942 { "incoming", G_DBUS_DEBUG_INCOMING },
1943 { "return", G_DBUS_DEBUG_RETURN },
1944 { "emission", G_DBUS_DEBUG_EMISSION },
1945 { "address", G_DBUS_DEBUG_ADDRESS }
1948 _gdbus_debug_flags = g_parse_debug_string (debug, keys, G_N_ELEMENTS (keys));
1949 if (_gdbus_debug_flags & G_DBUS_DEBUG_PAYLOAD)
1950 _gdbus_debug_flags |= G_DBUS_DEBUG_MESSAGE;
1953 g_once_init_leave (&initialized, 1);
1957 /* ---------------------------------------------------------------------------------------------------- */
1960 _g_dbus_compute_complete_signature (GDBusArgInfo **args)
1962 const GVariantType *arg_types[256];
1966 for (n = 0; args[n] != NULL; n++)
1968 /* DBus places a hard limit of 255 on signature length.
1969 * therefore number of args must be less than 256.
1973 arg_types[n] = G_VARIANT_TYPE (args[n]->signature);
1975 if G_UNLIKELY (arg_types[n] == NULL)
1981 return g_variant_type_new_tuple (arg_types, n);
1984 /* ---------------------------------------------------------------------------------------------------- */
1988 extern BOOL WINAPI ConvertSidToStringSidA (PSID Sid, LPSTR *StringSid);
1991 _g_dbus_win32_get_user_sid (void)
1995 DWORD token_information_len;
2002 h = INVALID_HANDLE_VALUE;
2004 if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &h))
2006 g_warning ("OpenProcessToken failed with error code %d", (gint) GetLastError ());
2010 /* Get length of buffer */
2011 token_information_len = 0;
2012 if (!GetTokenInformation (h, TokenUser, NULL, 0, &token_information_len))
2014 if (GetLastError () != ERROR_INSUFFICIENT_BUFFER)
2016 g_warning ("GetTokenInformation() failed with error code %d", (gint) GetLastError ());
2020 user = g_malloc (token_information_len);
2021 if (!GetTokenInformation (h, TokenUser, user, token_information_len, &token_information_len))
2023 g_warning ("GetTokenInformation() failed with error code %d", (gint) GetLastError ());
2027 psid = user->User.Sid;
2028 if (!IsValidSid (psid))
2030 g_warning ("Invalid SID");
2034 if (!ConvertSidToStringSidA (psid, &sid))
2036 g_warning ("Invalid SID");
2040 ret = g_strdup (sid);
2045 if (h != INVALID_HANDLE_VALUE)
2051 /* ---------------------------------------------------------------------------------------------------- */
2054 _g_dbus_get_machine_id (GError **error)
2057 HW_PROFILE_INFOA info;
2058 char *src, *dest, *res;
2061 if (!GetCurrentHwProfileA (&info))
2063 char *message = g_win32_error_message (GetLastError ());
2067 _("Unable to get Hardware profile: %s"), message);
2072 /* Form: {12340001-4980-1920-6788-123456789012} */
2073 src = &info.szHwProfileGuid[0];
2075 res = g_malloc (32+1);
2079 for (i = 0; i < 8; i++)
2082 for (i = 0; i < 4; i++)
2085 for (i = 0; i < 4; i++)
2088 for (i = 0; i < 4; i++)
2091 for (i = 0; i < 12; i++)
2098 GError *first_error;
2099 /* TODO: use PACKAGE_LOCALSTATEDIR ? */
2102 if (!g_file_get_contents ("/var/lib/dbus/machine-id",
2106 !g_file_get_contents ("/etc/machine-id",
2111 g_propagate_prefixed_error (error, first_error,
2112 _("Unable to load /var/lib/dbus/machine-id or /etc/machine-id: "));
2116 /* ignore the error from the first try, if any */
2117 g_clear_error (&first_error);
2118 /* TODO: validate value */
2125 /* ---------------------------------------------------------------------------------------------------- */
2128 _g_dbus_enum_to_string (GType enum_type, gint value)
2132 GEnumValue *enum_value;
2134 klass = g_type_class_ref (enum_type);
2135 enum_value = g_enum_get_value (klass, value);
2136 if (enum_value != NULL)
2137 ret = g_strdup (enum_value->value_nick);
2139 ret = g_strdup_printf ("unknown (value %d)", value);
2140 g_type_class_unref (klass);
2144 /* ---------------------------------------------------------------------------------------------------- */
2147 write_message_print_transport_debug (gssize bytes_written,
2148 MessageToWriteData *data)
2150 if (G_LIKELY (!_g_dbus_debug_transport ()))
2153 _g_dbus_debug_print_lock ();
2154 g_print ("========================================================================\n"
2155 "GDBus-debug:Transport:\n"
2156 " >>>> WROTE %" G_GSSIZE_FORMAT " bytes of message with serial %d and\n"
2157 " size %" G_GSIZE_FORMAT " from offset %" G_GSIZE_FORMAT " on a %s\n",
2159 g_dbus_message_get_serial (data->message),
2161 data->total_written,
2162 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream))));
2163 _g_dbus_debug_print_unlock ();
2168 /* ---------------------------------------------------------------------------------------------------- */
2171 read_message_print_transport_debug (gssize bytes_read,
2172 GDBusWorker *worker)
2176 gint32 message_length;
2178 if (G_LIKELY (!_g_dbus_debug_transport ()))
2181 size = bytes_read + worker->read_buffer_cur_size;
2185 message_length = g_dbus_message_bytes_needed ((guchar *) worker->read_buffer, size, NULL);
2188 switch (worker->read_buffer[0])
2192 serial = GUINT32_FROM_LE (((guint32 *) worker->read_buffer)[2]);
2196 serial = GUINT32_FROM_BE (((guint32 *) worker->read_buffer)[2]);
2199 /* an error will be set elsewhere if this happens */
2204 _g_dbus_debug_print_lock ();
2205 g_print ("========================================================================\n"
2206 "GDBus-debug:Transport:\n"
2207 " <<<< READ %" G_GSSIZE_FORMAT " bytes of message with serial %d and\n"
2208 " size %d to offset %" G_GSIZE_FORMAT " from a %s\n",
2212 worker->read_buffer_cur_size,
2213 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_input_stream (worker->stream))));
2214 _g_dbus_debug_print_unlock ();
2219 /* ---------------------------------------------------------------------------------------------------- */
2222 _g_signal_accumulator_false_handled (GSignalInvocationHint *ihint,
2223 GValue *return_accu,
2224 const GValue *handler_return,
2227 gboolean continue_emission;
2228 gboolean signal_return;
2230 signal_return = g_value_get_boolean (handler_return);
2231 g_value_set_boolean (return_accu, signal_return);
2232 continue_emission = signal_return;
2234 return continue_emission;