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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
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"
47 #include "gunixfdmessage.h"
48 #include "gunixconnection.h"
49 #include "gunixcredentialsmessage.h"
58 static gboolean _g_dbus_worker_do_initial_read (gpointer data);
60 /* ---------------------------------------------------------------------------------------------------- */
63 _g_dbus_hexdump (const gchar *data, gsize len, guint indent)
68 ret = g_string_new (NULL);
70 for (n = 0; n < len; n += 16)
72 g_string_append_printf (ret, "%*s%04x: ", indent, "", n);
74 for (m = n; m < n + 16; m++)
76 if (m > n && (m%4) == 0)
77 g_string_append_c (ret, ' ');
79 g_string_append_printf (ret, "%02x ", (guchar) data[m]);
81 g_string_append (ret, " ");
84 g_string_append (ret, " ");
86 for (m = n; m < len && m < n + 16; m++)
87 g_string_append_c (ret, g_ascii_isprint (data[m]) ? data[m] : '.');
89 g_string_append_c (ret, '\n');
92 return g_string_free (ret, FALSE);
95 /* ---------------------------------------------------------------------------------------------------- */
97 /* Unfortunately ancillary messages are discarded when reading from a
98 * socket using the GSocketInputStream abstraction. So we provide a
99 * very GInputStream-ish API that uses GSocket in this case (very
100 * similar to GSocketInputStream).
106 GCancellable *cancellable;
111 GSocketControlMessage ***messages;
114 GSimpleAsyncResult *simple;
116 gboolean from_mainloop;
117 } ReadWithControlData;
120 read_with_control_data_free (ReadWithControlData *data)
122 g_object_unref (data->socket);
123 if (data->cancellable != NULL)
124 g_object_unref (data->cancellable);
125 g_object_unref (data->simple);
130 _g_socket_read_with_control_messages_ready (GSocket *socket,
131 GIOCondition condition,
134 ReadWithControlData *data = user_data;
140 vector.buffer = data->buffer;
141 vector.size = data->count;
142 result = g_socket_receive_message (data->socket,
153 g_simple_async_result_set_op_res_gssize (data->simple, result);
157 g_assert (error != NULL);
158 g_simple_async_result_take_error (data->simple, error);
161 if (data->from_mainloop)
162 g_simple_async_result_complete (data->simple);
164 g_simple_async_result_complete_in_idle (data->simple);
170 _g_socket_read_with_control_messages (GSocket *socket,
173 GSocketControlMessage ***messages,
176 GCancellable *cancellable,
177 GAsyncReadyCallback callback,
180 ReadWithControlData *data;
182 data = g_new0 (ReadWithControlData, 1);
183 data->socket = g_object_ref (socket);
184 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
185 data->buffer = buffer;
187 data->messages = messages;
188 data->num_messages = num_messages;
190 data->simple = g_simple_async_result_new (G_OBJECT (socket),
193 _g_socket_read_with_control_messages);
195 if (!g_socket_condition_check (socket, G_IO_IN))
198 data->from_mainloop = TRUE;
199 source = g_socket_create_source (data->socket,
200 G_IO_IN | G_IO_HUP | G_IO_ERR,
202 g_source_set_callback (source,
203 (GSourceFunc) _g_socket_read_with_control_messages_ready,
205 (GDestroyNotify) read_with_control_data_free);
206 g_source_attach (source, g_main_context_get_thread_default ());
207 g_source_unref (source);
211 _g_socket_read_with_control_messages_ready (data->socket, G_IO_IN, data);
212 read_with_control_data_free (data);
217 _g_socket_read_with_control_messages_finish (GSocket *socket,
218 GAsyncResult *result,
221 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
223 g_return_val_if_fail (G_IS_SOCKET (socket), -1);
224 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == _g_socket_read_with_control_messages);
226 if (g_simple_async_result_propagate_error (simple, error))
229 return g_simple_async_result_get_op_res_gssize (simple);
232 /* ---------------------------------------------------------------------------------------------------- */
234 /* Work-around for https://bugzilla.gnome.org/show_bug.cgi?id=627724 */
236 static GPtrArray *ensured_classes = NULL;
239 ensure_type (GType gtype)
241 g_ptr_array_add (ensured_classes, g_type_class_ref (gtype));
245 release_required_types (void)
247 g_ptr_array_foreach (ensured_classes, (GFunc) g_type_class_unref, NULL);
248 g_ptr_array_unref (ensured_classes);
249 ensured_classes = NULL;
253 ensure_required_types (void)
255 g_assert (ensured_classes == NULL);
256 ensured_classes = g_ptr_array_new ();
257 ensure_type (G_TYPE_SIMPLE_ASYNC_RESULT);
258 ensure_type (G_TYPE_MEMORY_INPUT_STREAM);
260 /* ---------------------------------------------------------------------------------------------------- */
264 volatile gint refcount;
266 GMainContext *context;
271 gdbus_shared_thread_func (gpointer user_data)
273 SharedThreadData *data = user_data;
275 g_main_context_push_thread_default (data->context);
276 g_main_loop_run (data->loop);
277 g_main_context_pop_thread_default (data->context);
279 release_required_types ();
284 /* ---------------------------------------------------------------------------------------------------- */
286 static SharedThreadData *
287 _g_dbus_shared_thread_ref (void)
289 static gsize shared_thread_data = 0;
290 SharedThreadData *ret;
292 if (g_once_init_enter (&shared_thread_data))
294 SharedThreadData *data;
296 /* Work-around for https://bugzilla.gnome.org/show_bug.cgi?id=627724 */
297 ensure_required_types ();
299 data = g_new0 (SharedThreadData, 1);
302 data->context = g_main_context_new ();
303 data->loop = g_main_loop_new (data->context, FALSE);
304 data->thread = g_thread_new ("gdbus",
305 gdbus_shared_thread_func,
307 /* We can cast between gsize and gpointer safely */
308 g_once_init_leave (&shared_thread_data, (gsize) data);
311 ret = (SharedThreadData*) shared_thread_data;
312 g_atomic_int_inc (&ret->refcount);
317 _g_dbus_shared_thread_unref (SharedThreadData *data)
319 /* TODO: actually destroy the shared thread here */
321 g_assert (data != NULL);
322 if (g_atomic_int_dec_and_test (&data->refcount))
324 g_main_loop_quit (data->loop);
325 //g_thread_join (data->thread);
326 g_main_loop_unref (data->loop);
327 g_main_context_unref (data->context);
332 /* ---------------------------------------------------------------------------------------------------- */
336 volatile gint ref_count;
338 SharedThreadData *shared_thread_data;
340 /* really a boolean, but GLib 2.28 lacks atomic boolean ops */
341 volatile gint stopped;
343 /* TODO: frozen (e.g. G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING) currently
344 * only affects messages received from the other peer (since GDBusServer is the
345 * only user) - we might want it to affect messages sent to the other peer too?
348 GDBusCapabilityFlags capabilities;
349 GQueue *received_messages_while_frozen;
352 GCancellable *cancellable;
353 GDBusWorkerMessageReceivedCallback message_received_callback;
354 GDBusWorkerMessageAboutToBeSentCallback message_about_to_be_sent_callback;
355 GDBusWorkerDisconnectedCallback disconnected_callback;
358 /* if not NULL, stream is GSocketConnection */
361 /* used for reading */
364 gsize read_buffer_allocated_size;
365 gsize read_buffer_cur_size;
366 gsize read_buffer_bytes_wanted;
367 GUnixFDList *read_fd_list;
368 GSocketControlMessage **read_ancillary_messages;
369 gint read_num_ancillary_messages;
371 /* TRUE if an async write, flush or close is pending.
372 * Only the worker thread may change its value, and only with the write_lock.
373 * Other threads may read its value when holding the write_lock.
374 * The worker thread may read its value at any time.
376 gboolean output_pending;
377 /* used for writing */
379 /* queue of MessageToWriteData, protected by write_lock */
381 /* protected by write_lock */
382 guint64 write_num_messages_written;
383 /* list of FlushData, protected by write_lock */
384 GList *write_pending_flushes;
385 /* list of CloseData, protected by write_lock */
386 GList *pending_close_attempts;
389 static void _g_dbus_worker_unref (GDBusWorker *worker);
391 /* ---------------------------------------------------------------------------------------------------- */
397 guint64 number_to_wait_for;
401 struct _MessageToWriteData ;
402 typedef struct _MessageToWriteData MessageToWriteData;
404 static void message_to_write_data_free (MessageToWriteData *data);
406 static void read_message_print_transport_debug (gssize bytes_read,
407 GDBusWorker *worker);
409 static void write_message_print_transport_debug (gssize bytes_written,
410 MessageToWriteData *data);
414 GCancellable *cancellable;
415 GSimpleAsyncResult *result;
418 static void close_data_free (CloseData *close_data)
420 if (close_data->cancellable != NULL)
421 g_object_unref (close_data->cancellable);
423 if (close_data->result != NULL)
424 g_object_unref (close_data->result);
426 _g_dbus_worker_unref (close_data->worker);
427 g_slice_free (CloseData, close_data);
430 /* ---------------------------------------------------------------------------------------------------- */
433 _g_dbus_worker_ref (GDBusWorker *worker)
435 g_atomic_int_inc (&worker->ref_count);
440 _g_dbus_worker_unref (GDBusWorker *worker)
442 if (g_atomic_int_dec_and_test (&worker->ref_count))
444 g_assert (worker->write_pending_flushes == NULL);
446 _g_dbus_shared_thread_unref (worker->shared_thread_data);
448 g_object_unref (worker->stream);
450 g_mutex_clear (&worker->read_lock);
451 g_object_unref (worker->cancellable);
452 if (worker->read_fd_list != NULL)
453 g_object_unref (worker->read_fd_list);
455 g_queue_foreach (worker->received_messages_while_frozen, (GFunc) g_object_unref, NULL);
456 g_queue_free (worker->received_messages_while_frozen);
458 g_mutex_clear (&worker->write_lock);
459 g_queue_foreach (worker->write_queue, (GFunc) message_to_write_data_free, NULL);
460 g_queue_free (worker->write_queue);
462 g_free (worker->read_buffer);
469 _g_dbus_worker_emit_disconnected (GDBusWorker *worker,
470 gboolean remote_peer_vanished,
473 if (!g_atomic_int_get (&worker->stopped))
474 worker->disconnected_callback (worker, remote_peer_vanished, error, worker->user_data);
478 _g_dbus_worker_emit_message_received (GDBusWorker *worker,
479 GDBusMessage *message)
481 if (!g_atomic_int_get (&worker->stopped))
482 worker->message_received_callback (worker, message, worker->user_data);
485 static GDBusMessage *
486 _g_dbus_worker_emit_message_about_to_be_sent (GDBusWorker *worker,
487 GDBusMessage *message)
490 if (!g_atomic_int_get (&worker->stopped))
491 ret = worker->message_about_to_be_sent_callback (worker, message, worker->user_data);
497 /* can only be called from private thread with read-lock held - takes ownership of @message */
499 _g_dbus_worker_queue_or_deliver_received_message (GDBusWorker *worker,
500 GDBusMessage *message)
502 if (worker->frozen || g_queue_get_length (worker->received_messages_while_frozen) > 0)
505 g_queue_push_tail (worker->received_messages_while_frozen, message);
509 /* not frozen, nor anything in queue */
510 _g_dbus_worker_emit_message_received (worker, message);
511 g_object_unref (message);
515 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
517 unfreeze_in_idle_cb (gpointer user_data)
519 GDBusWorker *worker = user_data;
520 GDBusMessage *message;
522 g_mutex_lock (&worker->read_lock);
525 while ((message = g_queue_pop_head (worker->received_messages_while_frozen)) != NULL)
527 _g_dbus_worker_emit_message_received (worker, message);
528 g_object_unref (message);
530 worker->frozen = FALSE;
534 g_assert (g_queue_get_length (worker->received_messages_while_frozen) == 0);
536 g_mutex_unlock (&worker->read_lock);
540 /* can be called from any thread */
542 _g_dbus_worker_unfreeze (GDBusWorker *worker)
544 GSource *idle_source;
545 idle_source = g_idle_source_new ();
546 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
547 g_source_set_callback (idle_source,
549 _g_dbus_worker_ref (worker),
550 (GDestroyNotify) _g_dbus_worker_unref);
551 g_source_attach (idle_source, worker->shared_thread_data->context);
552 g_source_unref (idle_source);
555 /* ---------------------------------------------------------------------------------------------------- */
557 static void _g_dbus_worker_do_read_unlocked (GDBusWorker *worker);
559 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
561 _g_dbus_worker_do_read_cb (GInputStream *input_stream,
565 GDBusWorker *worker = user_data;
569 g_mutex_lock (&worker->read_lock);
571 /* If already stopped, don't even process the reply */
572 if (g_atomic_int_get (&worker->stopped))
576 if (worker->socket == NULL)
577 bytes_read = g_input_stream_read_finish (g_io_stream_get_input_stream (worker->stream),
581 bytes_read = _g_socket_read_with_control_messages_finish (worker->socket,
584 if (worker->read_num_ancillary_messages > 0)
587 for (n = 0; n < worker->read_num_ancillary_messages; n++)
589 GSocketControlMessage *control_message = G_SOCKET_CONTROL_MESSAGE (worker->read_ancillary_messages[n]);
595 else if (G_IS_UNIX_FD_MESSAGE (control_message))
597 GUnixFDMessage *fd_message;
601 fd_message = G_UNIX_FD_MESSAGE (control_message);
602 fds = g_unix_fd_message_steal_fds (fd_message, &num_fds);
603 if (worker->read_fd_list == NULL)
605 worker->read_fd_list = g_unix_fd_list_new_from_array (fds, num_fds);
610 for (n = 0; n < num_fds; n++)
612 /* TODO: really want a append_steal() */
613 g_unix_fd_list_append (worker->read_fd_list, fds[n], NULL);
619 else if (G_IS_UNIX_CREDENTIALS_MESSAGE (control_message))
631 "Unexpected ancillary message of type %s received from peer",
632 g_type_name (G_TYPE_FROM_INSTANCE (control_message)));
633 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
634 g_error_free (error);
635 g_object_unref (control_message);
637 while (n < worker->read_num_ancillary_messages)
638 g_object_unref (worker->read_ancillary_messages[n++]);
639 g_free (worker->read_ancillary_messages);
643 g_object_unref (control_message);
645 g_free (worker->read_ancillary_messages);
648 if (bytes_read == -1)
650 /* Every async read that uses this callback uses worker->cancellable
651 * as its GCancellable. worker->cancellable gets cancelled if and only
652 * if the GDBusConnection tells us to close (either via
653 * _g_dbus_worker_stop, which is called on last-unref, or directly),
654 * so a cancelled read must mean our connection was closed locally.
656 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
657 _g_dbus_worker_emit_disconnected (worker, FALSE, NULL);
659 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
661 g_error_free (error);
666 g_debug ("read %d bytes (is_closed=%d blocking=%d condition=0x%02x) stream %p, %p",
668 g_socket_is_closed (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream))),
669 g_socket_get_blocking (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream))),
670 g_socket_condition_check (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream)),
671 G_IO_IN | G_IO_OUT | G_IO_HUP),
676 /* TODO: hmm, hmm... */
682 "Underlying GIOStream returned 0 bytes on an async read");
683 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
684 g_error_free (error);
688 read_message_print_transport_debug (bytes_read, worker);
690 worker->read_buffer_cur_size += bytes_read;
691 if (worker->read_buffer_bytes_wanted == worker->read_buffer_cur_size)
693 /* OK, got what we asked for! */
694 if (worker->read_buffer_bytes_wanted == 16)
697 /* OK, got the header - determine how many more bytes are needed */
699 message_len = g_dbus_message_bytes_needed ((guchar *) worker->read_buffer,
702 if (message_len == -1)
704 g_warning ("_g_dbus_worker_do_read_cb: error determing bytes needed: %s", error->message);
705 _g_dbus_worker_emit_disconnected (worker, FALSE, error);
706 g_error_free (error);
710 worker->read_buffer_bytes_wanted = message_len;
711 _g_dbus_worker_do_read_unlocked (worker);
715 GDBusMessage *message;
718 /* TODO: use connection->priv->auth to decode the message */
720 message = g_dbus_message_new_from_blob ((guchar *) worker->read_buffer,
721 worker->read_buffer_cur_size,
722 worker->capabilities,
727 s = _g_dbus_hexdump (worker->read_buffer, worker->read_buffer_cur_size, 2);
728 g_warning ("Error decoding D-Bus message of %" G_GSIZE_FORMAT " bytes\n"
730 "The payload is as follows:\n"
732 worker->read_buffer_cur_size,
736 _g_dbus_worker_emit_disconnected (worker, FALSE, error);
737 g_error_free (error);
742 if (worker->read_fd_list != NULL)
744 g_dbus_message_set_unix_fd_list (message, worker->read_fd_list);
745 g_object_unref (worker->read_fd_list);
746 worker->read_fd_list = NULL;
750 if (G_UNLIKELY (_g_dbus_debug_message ()))
753 _g_dbus_debug_print_lock ();
754 g_print ("========================================================================\n"
755 "GDBus-debug:Message:\n"
756 " <<<< RECEIVED D-Bus message (%" G_GSIZE_FORMAT " bytes)\n",
757 worker->read_buffer_cur_size);
758 s = g_dbus_message_print (message, 2);
761 if (G_UNLIKELY (_g_dbus_debug_payload ()))
763 s = _g_dbus_hexdump (worker->read_buffer, worker->read_buffer_cur_size, 2);
767 _g_dbus_debug_print_unlock ();
770 /* yay, got a message, go deliver it */
771 _g_dbus_worker_queue_or_deliver_received_message (worker, message);
773 /* start reading another message! */
774 worker->read_buffer_bytes_wanted = 0;
775 worker->read_buffer_cur_size = 0;
776 _g_dbus_worker_do_read_unlocked (worker);
781 /* didn't get all the bytes we requested - so repeat the request... */
782 _g_dbus_worker_do_read_unlocked (worker);
786 g_mutex_unlock (&worker->read_lock);
788 /* gives up the reference acquired when calling g_input_stream_read_async() */
789 _g_dbus_worker_unref (worker);
792 /* called in private thread shared by all GDBusConnection instances (with read-lock held) */
794 _g_dbus_worker_do_read_unlocked (GDBusWorker *worker)
796 /* if bytes_wanted is zero, it means start reading a message */
797 if (worker->read_buffer_bytes_wanted == 0)
799 worker->read_buffer_cur_size = 0;
800 worker->read_buffer_bytes_wanted = 16;
803 /* ensure we have a (big enough) buffer */
804 if (worker->read_buffer == NULL || worker->read_buffer_bytes_wanted > worker->read_buffer_allocated_size)
806 /* TODO: 4096 is randomly chosen; might want a better chosen default minimum */
807 worker->read_buffer_allocated_size = MAX (worker->read_buffer_bytes_wanted, 4096);
808 worker->read_buffer = g_realloc (worker->read_buffer, worker->read_buffer_allocated_size);
811 if (worker->socket == NULL)
812 g_input_stream_read_async (g_io_stream_get_input_stream (worker->stream),
813 worker->read_buffer + worker->read_buffer_cur_size,
814 worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size,
817 (GAsyncReadyCallback) _g_dbus_worker_do_read_cb,
818 _g_dbus_worker_ref (worker));
821 worker->read_ancillary_messages = NULL;
822 worker->read_num_ancillary_messages = 0;
823 _g_socket_read_with_control_messages (worker->socket,
824 worker->read_buffer + worker->read_buffer_cur_size,
825 worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size,
826 &worker->read_ancillary_messages,
827 &worker->read_num_ancillary_messages,
830 (GAsyncReadyCallback) _g_dbus_worker_do_read_cb,
831 _g_dbus_worker_ref (worker));
835 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
837 _g_dbus_worker_do_initial_read (gpointer data)
839 GDBusWorker *worker = data;
840 g_mutex_lock (&worker->read_lock);
841 _g_dbus_worker_do_read_unlocked (worker);
842 g_mutex_unlock (&worker->read_lock);
846 /* ---------------------------------------------------------------------------------------------------- */
848 struct _MessageToWriteData
851 GDBusMessage *message;
856 GSimpleAsyncResult *simple;
861 message_to_write_data_free (MessageToWriteData *data)
863 _g_dbus_worker_unref (data->worker);
865 g_object_unref (data->message);
870 /* ---------------------------------------------------------------------------------------------------- */
872 static void write_message_continue_writing (MessageToWriteData *data);
874 /* called in private thread shared by all GDBusConnection instances
876 * write-lock is not held on entry
877 * output_pending is true on entry
880 write_message_async_cb (GObject *source_object,
884 MessageToWriteData *data = user_data;
885 GSimpleAsyncResult *simple;
886 gssize bytes_written;
889 /* Note: we can't access data->simple after calling g_async_result_complete () because the
890 * callback can free @data and we're not completing in idle. So use a copy of the pointer.
892 simple = data->simple;
895 bytes_written = g_output_stream_write_finish (G_OUTPUT_STREAM (source_object),
898 if (bytes_written == -1)
900 g_simple_async_result_take_error (simple, error);
901 g_simple_async_result_complete (simple);
902 g_object_unref (simple);
905 g_assert (bytes_written > 0); /* zero is never returned */
907 write_message_print_transport_debug (bytes_written, data);
909 data->total_written += bytes_written;
910 g_assert (data->total_written <= data->blob_size);
911 if (data->total_written == data->blob_size)
913 g_simple_async_result_complete (simple);
914 g_object_unref (simple);
918 write_message_continue_writing (data);
924 /* called in private thread shared by all GDBusConnection instances
926 * write-lock is not held on entry
927 * output_pending is true on entry
930 on_socket_ready (GSocket *socket,
931 GIOCondition condition,
934 MessageToWriteData *data = user_data;
935 write_message_continue_writing (data);
936 return FALSE; /* remove source */
939 /* called in private thread shared by all GDBusConnection instances
941 * write-lock is not held on entry
942 * output_pending is true on entry
945 write_message_continue_writing (MessageToWriteData *data)
947 GOutputStream *ostream;
948 GSimpleAsyncResult *simple;
950 GUnixFDList *fd_list;
953 /* Note: we can't access data->simple after calling g_async_result_complete () because the
954 * callback can free @data and we're not completing in idle. So use a copy of the pointer.
956 simple = data->simple;
958 ostream = g_io_stream_get_output_stream (data->worker->stream);
960 fd_list = g_dbus_message_get_unix_fd_list (data->message);
963 g_assert (!g_output_stream_has_pending (ostream));
964 g_assert_cmpint (data->total_written, <, data->blob_size);
970 else if (G_IS_SOCKET_OUTPUT_STREAM (ostream) && data->total_written == 0)
972 GOutputVector vector;
973 GSocketControlMessage *control_message;
974 gssize bytes_written;
977 vector.buffer = data->blob;
978 vector.size = data->blob_size;
980 control_message = NULL;
981 if (fd_list != NULL && g_unix_fd_list_get_length (fd_list) > 0)
983 if (!(data->worker->capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING))
985 g_simple_async_result_set_error (simple,
988 "Tried sending a file descriptor but remote peer does not support this capability");
989 g_simple_async_result_complete (simple);
990 g_object_unref (simple);
993 control_message = g_unix_fd_message_new_with_fd_list (fd_list);
997 bytes_written = g_socket_send_message (data->worker->socket,
1001 control_message != NULL ? &control_message : NULL,
1002 control_message != NULL ? 1 : 0,
1004 data->worker->cancellable,
1006 if (control_message != NULL)
1007 g_object_unref (control_message);
1009 if (bytes_written == -1)
1011 /* Handle WOULD_BLOCK by waiting until there's room in the buffer */
1012 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1015 source = g_socket_create_source (data->worker->socket,
1016 G_IO_OUT | G_IO_HUP | G_IO_ERR,
1017 data->worker->cancellable);
1018 g_source_set_callback (source,
1019 (GSourceFunc) on_socket_ready,
1021 NULL); /* GDestroyNotify */
1022 g_source_attach (source, g_main_context_get_thread_default ());
1023 g_source_unref (source);
1024 g_error_free (error);
1027 g_simple_async_result_take_error (simple, error);
1028 g_simple_async_result_complete (simple);
1029 g_object_unref (simple);
1032 g_assert (bytes_written > 0); /* zero is never returned */
1034 write_message_print_transport_debug (bytes_written, data);
1036 data->total_written += bytes_written;
1037 g_assert (data->total_written <= data->blob_size);
1038 if (data->total_written == data->blob_size)
1040 g_simple_async_result_complete (simple);
1041 g_object_unref (simple);
1045 write_message_continue_writing (data);
1051 if (fd_list != NULL)
1053 g_simple_async_result_set_error (simple,
1056 "Tried sending a file descriptor on unsupported stream of type %s",
1057 g_type_name (G_TYPE_FROM_INSTANCE (ostream)));
1058 g_simple_async_result_complete (simple);
1059 g_object_unref (simple);
1064 g_output_stream_write_async (ostream,
1065 (const gchar *) data->blob + data->total_written,
1066 data->blob_size - data->total_written,
1068 data->worker->cancellable,
1069 write_message_async_cb,
1076 /* called in private thread shared by all GDBusConnection instances
1078 * write-lock is not held on entry
1079 * output_pending is true on entry
1082 write_message_async (GDBusWorker *worker,
1083 MessageToWriteData *data,
1084 GAsyncReadyCallback callback,
1087 data->simple = g_simple_async_result_new (NULL,
1090 write_message_async);
1091 data->total_written = 0;
1092 write_message_continue_writing (data);
1095 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
1097 write_message_finish (GAsyncResult *res,
1100 g_warn_if_fail (g_simple_async_result_get_source_tag (G_SIMPLE_ASYNC_RESULT (res)) == write_message_async);
1101 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error))
1106 /* ---------------------------------------------------------------------------------------------------- */
1108 static void maybe_write_next_message (GDBusWorker *worker);
1112 GDBusWorker *worker;
1117 flush_data_list_complete (const GList *flushers,
1118 const GError *error)
1122 for (l = flushers; l != NULL; l = l->next)
1124 FlushData *f = l->data;
1126 f->error = error != NULL ? g_error_copy (error) : NULL;
1128 g_mutex_lock (&f->mutex);
1129 g_cond_signal (&f->cond);
1130 g_mutex_unlock (&f->mutex);
1134 /* called in private thread shared by all GDBusConnection instances
1136 * write-lock is not held on entry
1137 * output_pending is true on entry
1140 ostream_flush_cb (GObject *source_object,
1144 FlushAsyncData *data = user_data;
1148 g_output_stream_flush_finish (G_OUTPUT_STREAM (source_object),
1154 if (G_UNLIKELY (_g_dbus_debug_transport ()))
1156 _g_dbus_debug_print_lock ();
1157 g_print ("========================================================================\n"
1158 "GDBus-debug:Transport:\n"
1159 " ---- FLUSHED stream of type %s\n",
1160 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream))));
1161 _g_dbus_debug_print_unlock ();
1165 g_assert (data->flushers != NULL);
1166 flush_data_list_complete (data->flushers, error);
1167 g_list_free (data->flushers);
1170 g_error_free (error);
1172 /* Make sure we tell folks that we don't have additional
1174 g_mutex_lock (&data->worker->write_lock);
1175 g_assert (data->worker->output_pending);
1176 data->worker->output_pending = FALSE;
1177 g_mutex_unlock (&data->worker->write_lock);
1179 /* OK, cool, finally kick off the next write */
1180 maybe_write_next_message (data->worker);
1182 _g_dbus_worker_unref (data->worker);
1186 /* called in private thread shared by all GDBusConnection instances
1188 * write-lock is not held on entry
1189 * output_pending is false on entry
1192 message_written (GDBusWorker *worker,
1193 MessageToWriteData *message_data)
1199 /* first log the fact that we wrote a message */
1200 if (G_UNLIKELY (_g_dbus_debug_message ()))
1203 _g_dbus_debug_print_lock ();
1204 g_print ("========================================================================\n"
1205 "GDBus-debug:Message:\n"
1206 " >>>> SENT D-Bus message (%" G_GSIZE_FORMAT " bytes)\n",
1207 message_data->blob_size);
1208 s = g_dbus_message_print (message_data->message, 2);
1211 if (G_UNLIKELY (_g_dbus_debug_payload ()))
1213 s = _g_dbus_hexdump (message_data->blob, message_data->blob_size, 2);
1214 g_print ("%s\n", s);
1217 _g_dbus_debug_print_unlock ();
1220 /* then first wake up pending flushes and, if needed, flush the stream */
1222 g_mutex_lock (&worker->write_lock);
1223 worker->write_num_messages_written += 1;
1224 for (l = worker->write_pending_flushes; l != NULL; l = ll)
1226 FlushData *f = l->data;
1229 if (f->number_to_wait_for == worker->write_num_messages_written)
1231 flushers = g_list_append (flushers, f);
1232 worker->write_pending_flushes = g_list_delete_link (worker->write_pending_flushes, l);
1235 if (flushers != NULL)
1237 g_assert (!worker->output_pending);
1238 worker->output_pending = TRUE;
1240 g_mutex_unlock (&worker->write_lock);
1242 if (flushers != NULL)
1244 FlushAsyncData *data;
1245 data = g_new0 (FlushAsyncData, 1);
1246 data->worker = _g_dbus_worker_ref (worker);
1247 data->flushers = flushers;
1248 /* flush the stream before writing the next message */
1249 g_output_stream_flush_async (g_io_stream_get_output_stream (worker->stream),
1251 worker->cancellable,
1257 /* kick off the next write! */
1258 maybe_write_next_message (worker);
1262 /* called in private thread shared by all GDBusConnection instances
1264 * write-lock is not held on entry
1265 * output_pending is true on entry
1268 write_message_cb (GObject *source_object,
1272 MessageToWriteData *data = user_data;
1275 g_mutex_lock (&data->worker->write_lock);
1276 g_assert (data->worker->output_pending);
1277 data->worker->output_pending = FALSE;
1278 g_mutex_unlock (&data->worker->write_lock);
1281 if (!write_message_finish (res, &error))
1284 _g_dbus_worker_emit_disconnected (data->worker, TRUE, error);
1285 g_error_free (error);
1288 /* this function will also kick of the next write (it might need to
1289 * flush so writing the next message might happen much later
1292 message_written (data->worker, data);
1294 message_to_write_data_free (data);
1297 /* called in private thread shared by all GDBusConnection instances
1299 * write-lock is not held on entry
1300 * output_pending is true on entry
1303 iostream_close_cb (GObject *source_object,
1307 GDBusWorker *worker = user_data;
1308 GError *error = NULL;
1309 GList *pending_close_attempts, *pending_flush_attempts;
1312 g_io_stream_close_finish (worker->stream, res, &error);
1314 g_mutex_lock (&worker->write_lock);
1316 pending_close_attempts = worker->pending_close_attempts;
1317 worker->pending_close_attempts = NULL;
1319 pending_flush_attempts = worker->write_pending_flushes;
1320 worker->write_pending_flushes = NULL;
1322 send_queue = worker->write_queue;
1323 worker->write_queue = g_queue_new ();
1325 g_assert (worker->output_pending);
1326 worker->output_pending = FALSE;
1328 g_mutex_unlock (&worker->write_lock);
1330 while (pending_close_attempts != NULL)
1332 CloseData *close_data = pending_close_attempts->data;
1334 pending_close_attempts = g_list_delete_link (pending_close_attempts,
1335 pending_close_attempts);
1337 if (close_data->result != NULL)
1340 g_simple_async_result_set_from_error (close_data->result, error);
1342 /* this must be in an idle because the result is likely to be
1343 * intended for another thread
1345 g_simple_async_result_complete_in_idle (close_data->result);
1348 close_data_free (close_data);
1351 g_clear_error (&error);
1353 /* all messages queued for sending are discarded */
1354 g_queue_foreach (send_queue, (GFunc) message_to_write_data_free, NULL);
1355 g_queue_free (send_queue);
1357 /* all queued flushes fail */
1358 error = g_error_new (G_IO_ERROR, G_IO_ERROR_CANCELLED,
1359 _("Operation was cancelled"));
1360 flush_data_list_complete (pending_flush_attempts, error);
1361 g_list_free (pending_flush_attempts);
1362 g_clear_error (&error);
1364 _g_dbus_worker_unref (worker);
1367 /* called in private thread shared by all GDBusConnection instances
1369 * write-lock is not held on entry
1370 * output_pending must be false on entry
1373 maybe_write_next_message (GDBusWorker *worker)
1375 MessageToWriteData *data;
1378 /* we mustn't try to write two things at once */
1379 g_assert (!worker->output_pending);
1381 g_mutex_lock (&worker->write_lock);
1383 /* if we want to close the connection, that takes precedence */
1384 if (worker->pending_close_attempts != NULL)
1386 worker->output_pending = TRUE;
1388 g_io_stream_close_async (worker->stream, G_PRIORITY_DEFAULT,
1389 NULL, iostream_close_cb,
1390 _g_dbus_worker_ref (worker));
1395 data = g_queue_pop_head (worker->write_queue);
1398 worker->output_pending = TRUE;
1401 g_mutex_unlock (&worker->write_lock);
1403 /* Note that write_lock is only used for protecting the @write_queue
1404 * and @output_pending fields of the GDBusWorker struct ... which we
1405 * need to modify from arbitrary threads in _g_dbus_worker_send_message().
1407 * Therefore, it's fine to drop it here when calling back into user
1408 * code and then writing the message out onto the GIOStream since this
1409 * function only runs on the worker thread.
1413 GDBusMessage *old_message;
1415 gsize new_blob_size;
1418 old_message = data->message;
1419 data->message = _g_dbus_worker_emit_message_about_to_be_sent (worker, data->message);
1420 if (data->message == old_message)
1422 /* filters had no effect - do nothing */
1424 else if (data->message == NULL)
1426 /* filters dropped message */
1427 g_mutex_lock (&worker->write_lock);
1428 worker->output_pending = FALSE;
1429 g_mutex_unlock (&worker->write_lock);
1430 message_to_write_data_free (data);
1435 /* filters altered the message -> reencode */
1437 new_blob = g_dbus_message_to_blob (data->message,
1439 worker->capabilities,
1441 if (new_blob == NULL)
1443 /* if filter make the GDBusMessage unencodeable, just complain on stderr and send
1444 * the old message instead
1446 g_warning ("Error encoding GDBusMessage with serial %d altered by filter function: %s",
1447 g_dbus_message_get_serial (data->message),
1449 g_error_free (error);
1453 g_free (data->blob);
1454 data->blob = (gchar *) new_blob;
1455 data->blob_size = new_blob_size;
1459 write_message_async (worker,
1466 /* called in private thread shared by all GDBusConnection instances
1468 * write-lock is not held on entry
1469 * output_pending may be true or false
1472 write_message_in_idle_cb (gpointer user_data)
1474 GDBusWorker *worker = user_data;
1476 /* Because this is the worker thread, we can read this struct member
1477 * without holding the lock: no other thread ever modifies it.
1479 if (!worker->output_pending)
1480 maybe_write_next_message (worker);
1486 * @write_data: (transfer full) (allow-none):
1487 * @close_data: (transfer full) (allow-none):
1489 * Can be called from any thread
1491 * write_lock is not held on entry
1492 * output_pending may be true or false
1495 schedule_write_in_worker_thread (GDBusWorker *worker,
1496 MessageToWriteData *write_data,
1497 CloseData *close_data)
1499 g_mutex_lock (&worker->write_lock);
1501 if (write_data != NULL)
1502 g_queue_push_tail (worker->write_queue, write_data);
1504 if (close_data != NULL)
1505 worker->pending_close_attempts = g_list_prepend (worker->pending_close_attempts,
1508 if (!worker->output_pending)
1510 GSource *idle_source;
1511 idle_source = g_idle_source_new ();
1512 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1513 g_source_set_callback (idle_source,
1514 write_message_in_idle_cb,
1515 _g_dbus_worker_ref (worker),
1516 (GDestroyNotify) _g_dbus_worker_unref);
1517 g_source_attach (idle_source, worker->shared_thread_data->context);
1518 g_source_unref (idle_source);
1521 g_mutex_unlock (&worker->write_lock);
1524 /* ---------------------------------------------------------------------------------------------------- */
1526 /* can be called from any thread - steals blob
1528 * write_lock is not held on entry
1529 * output_pending may be true or false
1532 _g_dbus_worker_send_message (GDBusWorker *worker,
1533 GDBusMessage *message,
1537 MessageToWriteData *data;
1539 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1540 g_return_if_fail (blob != NULL);
1541 g_return_if_fail (blob_len > 16);
1543 data = g_new0 (MessageToWriteData, 1);
1544 data->worker = _g_dbus_worker_ref (worker);
1545 data->message = g_object_ref (message);
1546 data->blob = blob; /* steal! */
1547 data->blob_size = blob_len;
1549 schedule_write_in_worker_thread (worker, data, NULL);
1552 /* ---------------------------------------------------------------------------------------------------- */
1555 _g_dbus_worker_new (GIOStream *stream,
1556 GDBusCapabilityFlags capabilities,
1557 gboolean initially_frozen,
1558 GDBusWorkerMessageReceivedCallback message_received_callback,
1559 GDBusWorkerMessageAboutToBeSentCallback message_about_to_be_sent_callback,
1560 GDBusWorkerDisconnectedCallback disconnected_callback,
1563 GDBusWorker *worker;
1564 GSource *idle_source;
1566 g_return_val_if_fail (G_IS_IO_STREAM (stream), NULL);
1567 g_return_val_if_fail (message_received_callback != NULL, NULL);
1568 g_return_val_if_fail (message_about_to_be_sent_callback != NULL, NULL);
1569 g_return_val_if_fail (disconnected_callback != NULL, NULL);
1571 worker = g_new0 (GDBusWorker, 1);
1572 worker->ref_count = 1;
1574 g_mutex_init (&worker->read_lock);
1575 worker->message_received_callback = message_received_callback;
1576 worker->message_about_to_be_sent_callback = message_about_to_be_sent_callback;
1577 worker->disconnected_callback = disconnected_callback;
1578 worker->user_data = user_data;
1579 worker->stream = g_object_ref (stream);
1580 worker->capabilities = capabilities;
1581 worker->cancellable = g_cancellable_new ();
1582 worker->output_pending = FALSE;
1584 worker->frozen = initially_frozen;
1585 worker->received_messages_while_frozen = g_queue_new ();
1587 g_mutex_init (&worker->write_lock);
1588 worker->write_queue = g_queue_new ();
1590 if (G_IS_SOCKET_CONNECTION (worker->stream))
1591 worker->socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream));
1593 worker->shared_thread_data = _g_dbus_shared_thread_ref ();
1596 idle_source = g_idle_source_new ();
1597 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1598 g_source_set_callback (idle_source,
1599 _g_dbus_worker_do_initial_read,
1600 _g_dbus_worker_ref (worker),
1601 (GDestroyNotify) _g_dbus_worker_unref);
1602 g_source_attach (idle_source, worker->shared_thread_data->context);
1603 g_source_unref (idle_source);
1608 /* ---------------------------------------------------------------------------------------------------- */
1610 /* can be called from any thread
1612 * write_lock is not held on entry
1613 * output_pending may be true or false
1616 _g_dbus_worker_close (GDBusWorker *worker,
1617 GCancellable *cancellable,
1618 GSimpleAsyncResult *result)
1620 CloseData *close_data;
1622 close_data = g_slice_new0 (CloseData);
1623 close_data->worker = _g_dbus_worker_ref (worker);
1624 close_data->cancellable =
1625 (cancellable == NULL ? NULL : g_object_ref (cancellable));
1626 close_data->result = (result == NULL ? NULL : g_object_ref (result));
1628 g_cancellable_cancel (worker->cancellable);
1629 schedule_write_in_worker_thread (worker, NULL, close_data);
1632 /* This can be called from any thread - frees worker. Note that
1633 * callbacks might still happen if called from another thread than the
1634 * worker - use your own synchronization primitive in the callbacks.
1636 * write_lock is not held on entry
1637 * output_pending may be true or false
1640 _g_dbus_worker_stop (GDBusWorker *worker)
1642 g_atomic_int_set (&worker->stopped, TRUE);
1644 /* Cancel any pending operations and schedule a close of the underlying I/O
1645 * stream in the worker thread
1647 _g_dbus_worker_close (worker, NULL, NULL);
1649 /* _g_dbus_worker_close holds a ref until after an idle in the the worker
1650 * thread has run, so we no longer need to unref in an idle like in
1653 _g_dbus_worker_unref (worker);
1656 /* ---------------------------------------------------------------------------------------------------- */
1658 /* can be called from any thread (except the worker thread) - blocks
1659 * calling thread until all queued outgoing messages are written and
1660 * the transport has been flushed
1662 * write_lock is not held on entry
1663 * output_pending may be true or false
1666 _g_dbus_worker_flush_sync (GDBusWorker *worker,
1667 GCancellable *cancellable,
1676 /* if the queue is empty, there's nothing to wait for */
1677 g_mutex_lock (&worker->write_lock);
1678 if (g_queue_get_length (worker->write_queue) > 0)
1680 data = g_new0 (FlushData, 1);
1681 g_mutex_init (&data->mutex);
1682 g_cond_init (&data->cond);
1683 data->number_to_wait_for = worker->write_num_messages_written + g_queue_get_length (worker->write_queue);
1684 g_mutex_lock (&data->mutex);
1685 worker->write_pending_flushes = g_list_prepend (worker->write_pending_flushes, data);
1687 g_mutex_unlock (&worker->write_lock);
1691 g_cond_wait (&data->cond, &data->mutex);
1692 g_mutex_unlock (&data->mutex);
1694 /* note:the element is removed from worker->write_pending_flushes in flush_cb() above */
1695 g_cond_clear (&data->cond);
1696 g_mutex_clear (&data->mutex);
1697 if (data->error != NULL)
1700 g_propagate_error (error, data->error);
1708 /* ---------------------------------------------------------------------------------------------------- */
1710 #define G_DBUS_DEBUG_AUTHENTICATION (1<<0)
1711 #define G_DBUS_DEBUG_TRANSPORT (1<<1)
1712 #define G_DBUS_DEBUG_MESSAGE (1<<2)
1713 #define G_DBUS_DEBUG_PAYLOAD (1<<3)
1714 #define G_DBUS_DEBUG_CALL (1<<4)
1715 #define G_DBUS_DEBUG_SIGNAL (1<<5)
1716 #define G_DBUS_DEBUG_INCOMING (1<<6)
1717 #define G_DBUS_DEBUG_RETURN (1<<7)
1718 #define G_DBUS_DEBUG_EMISSION (1<<8)
1719 #define G_DBUS_DEBUG_ADDRESS (1<<9)
1721 static gint _gdbus_debug_flags = 0;
1724 _g_dbus_debug_authentication (void)
1726 _g_dbus_initialize ();
1727 return (_gdbus_debug_flags & G_DBUS_DEBUG_AUTHENTICATION) != 0;
1731 _g_dbus_debug_transport (void)
1733 _g_dbus_initialize ();
1734 return (_gdbus_debug_flags & G_DBUS_DEBUG_TRANSPORT) != 0;
1738 _g_dbus_debug_message (void)
1740 _g_dbus_initialize ();
1741 return (_gdbus_debug_flags & G_DBUS_DEBUG_MESSAGE) != 0;
1745 _g_dbus_debug_payload (void)
1747 _g_dbus_initialize ();
1748 return (_gdbus_debug_flags & G_DBUS_DEBUG_PAYLOAD) != 0;
1752 _g_dbus_debug_call (void)
1754 _g_dbus_initialize ();
1755 return (_gdbus_debug_flags & G_DBUS_DEBUG_CALL) != 0;
1759 _g_dbus_debug_signal (void)
1761 _g_dbus_initialize ();
1762 return (_gdbus_debug_flags & G_DBUS_DEBUG_SIGNAL) != 0;
1766 _g_dbus_debug_incoming (void)
1768 _g_dbus_initialize ();
1769 return (_gdbus_debug_flags & G_DBUS_DEBUG_INCOMING) != 0;
1773 _g_dbus_debug_return (void)
1775 _g_dbus_initialize ();
1776 return (_gdbus_debug_flags & G_DBUS_DEBUG_RETURN) != 0;
1780 _g_dbus_debug_emission (void)
1782 _g_dbus_initialize ();
1783 return (_gdbus_debug_flags & G_DBUS_DEBUG_EMISSION) != 0;
1787 _g_dbus_debug_address (void)
1789 _g_dbus_initialize ();
1790 return (_gdbus_debug_flags & G_DBUS_DEBUG_ADDRESS) != 0;
1793 G_LOCK_DEFINE_STATIC (print_lock);
1796 _g_dbus_debug_print_lock (void)
1798 G_LOCK (print_lock);
1802 _g_dbus_debug_print_unlock (void)
1804 G_UNLOCK (print_lock);
1808 * _g_dbus_initialize:
1810 * Does various one-time init things such as
1812 * - registering the G_DBUS_ERROR error domain
1813 * - parses the G_DBUS_DEBUG environment variable
1816 _g_dbus_initialize (void)
1818 static volatile gsize initialized = 0;
1820 if (g_once_init_enter (&initialized))
1822 volatile GQuark g_dbus_error_domain;
1825 g_dbus_error_domain = G_DBUS_ERROR;
1826 (g_dbus_error_domain); /* To avoid -Wunused-but-set-variable */
1828 debug = g_getenv ("G_DBUS_DEBUG");
1831 const GDebugKey keys[] = {
1832 { "authentication", G_DBUS_DEBUG_AUTHENTICATION },
1833 { "transport", G_DBUS_DEBUG_TRANSPORT },
1834 { "message", G_DBUS_DEBUG_MESSAGE },
1835 { "payload", G_DBUS_DEBUG_PAYLOAD },
1836 { "call", G_DBUS_DEBUG_CALL },
1837 { "signal", G_DBUS_DEBUG_SIGNAL },
1838 { "incoming", G_DBUS_DEBUG_INCOMING },
1839 { "return", G_DBUS_DEBUG_RETURN },
1840 { "emission", G_DBUS_DEBUG_EMISSION },
1841 { "address", G_DBUS_DEBUG_ADDRESS }
1844 _gdbus_debug_flags = g_parse_debug_string (debug, keys, G_N_ELEMENTS (keys));
1845 if (_gdbus_debug_flags & G_DBUS_DEBUG_PAYLOAD)
1846 _gdbus_debug_flags |= G_DBUS_DEBUG_MESSAGE;
1849 g_once_init_leave (&initialized, 1);
1853 /* ---------------------------------------------------------------------------------------------------- */
1856 _g_dbus_compute_complete_signature (GDBusArgInfo **args)
1858 const GVariantType *arg_types[256];
1862 for (n = 0; args[n] != NULL; n++)
1864 /* DBus places a hard limit of 255 on signature length.
1865 * therefore number of args must be less than 256.
1869 arg_types[n] = G_VARIANT_TYPE (args[n]->signature);
1871 if G_UNLIKELY (arg_types[n] == NULL)
1877 return g_variant_type_new_tuple (arg_types, n);
1880 /* ---------------------------------------------------------------------------------------------------- */
1884 extern BOOL WINAPI ConvertSidToStringSidA (PSID Sid, LPSTR *StringSid);
1887 _g_dbus_win32_get_user_sid (void)
1891 DWORD token_information_len;
1898 h = INVALID_HANDLE_VALUE;
1900 if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &h))
1902 g_warning ("OpenProcessToken failed with error code %d", (gint) GetLastError ());
1906 /* Get length of buffer */
1907 token_information_len = 0;
1908 if (!GetTokenInformation (h, TokenUser, NULL, 0, &token_information_len))
1910 if (GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1912 g_warning ("GetTokenInformation() failed with error code %d", (gint) GetLastError ());
1916 user = g_malloc (token_information_len);
1917 if (!GetTokenInformation (h, TokenUser, user, token_information_len, &token_information_len))
1919 g_warning ("GetTokenInformation() failed with error code %d", (gint) GetLastError ());
1923 psid = user->User.Sid;
1924 if (!IsValidSid (psid))
1926 g_warning ("Invalid SID");
1930 if (!ConvertSidToStringSidA (psid, &sid))
1932 g_warning ("Invalid SID");
1936 ret = g_strdup (sid);
1941 if (h != INVALID_HANDLE_VALUE)
1947 /* ---------------------------------------------------------------------------------------------------- */
1950 _g_dbus_get_machine_id (GError **error)
1953 /* TODO: use PACKAGE_LOCALSTATEDIR ? */
1955 if (!g_file_get_contents ("/var/lib/dbus/machine-id",
1960 g_prefix_error (error, _("Unable to load /var/lib/dbus/machine-id: "));
1964 /* TODO: validate value */
1970 /* ---------------------------------------------------------------------------------------------------- */
1973 _g_dbus_enum_to_string (GType enum_type, gint value)
1977 GEnumValue *enum_value;
1979 klass = g_type_class_ref (enum_type);
1980 enum_value = g_enum_get_value (klass, value);
1981 if (enum_value != NULL)
1982 ret = g_strdup (enum_value->value_nick);
1984 ret = g_strdup_printf ("unknown (value %d)", value);
1985 g_type_class_unref (klass);
1989 /* ---------------------------------------------------------------------------------------------------- */
1992 write_message_print_transport_debug (gssize bytes_written,
1993 MessageToWriteData *data)
1995 if (G_LIKELY (!_g_dbus_debug_transport ()))
1998 _g_dbus_debug_print_lock ();
1999 g_print ("========================================================================\n"
2000 "GDBus-debug:Transport:\n"
2001 " >>>> WROTE %" G_GSIZE_FORMAT " bytes of message with serial %d and\n"
2002 " size %" G_GSIZE_FORMAT " from offset %" G_GSIZE_FORMAT " on a %s\n",
2004 g_dbus_message_get_serial (data->message),
2006 data->total_written,
2007 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream))));
2008 _g_dbus_debug_print_unlock ();
2013 /* ---------------------------------------------------------------------------------------------------- */
2016 read_message_print_transport_debug (gssize bytes_read,
2017 GDBusWorker *worker)
2021 gint32 message_length;
2023 if (G_LIKELY (!_g_dbus_debug_transport ()))
2026 size = bytes_read + worker->read_buffer_cur_size;
2030 message_length = g_dbus_message_bytes_needed ((guchar *) worker->read_buffer, size, NULL);
2033 switch (worker->read_buffer[0])
2037 serial = GUINT32_FROM_LE (((guint32 *) worker->read_buffer)[2]);
2041 serial = GUINT32_FROM_BE (((guint32 *) worker->read_buffer)[2]);
2044 /* an error will be set elsewhere if this happens */
2049 _g_dbus_debug_print_lock ();
2050 g_print ("========================================================================\n"
2051 "GDBus-debug:Transport:\n"
2052 " <<<< READ %" G_GSIZE_FORMAT " bytes of message with serial %d and\n"
2053 " size %d to offset %" G_GSIZE_FORMAT " from a %s\n",
2057 worker->read_buffer_cur_size,
2058 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_input_stream (worker->stream))));
2059 _g_dbus_debug_print_unlock ();
2064 /* ---------------------------------------------------------------------------------------------------- */
2067 _g_signal_accumulator_false_handled (GSignalInvocationHint *ihint,
2068 GValue *return_accu,
2069 const GValue *handler_return,
2072 gboolean continue_emission;
2073 gboolean signal_return;
2075 signal_return = g_value_get_boolean (handler_return);
2076 g_value_set_boolean (return_accu, signal_return);
2077 continue_emission = signal_return;
2079 return continue_emission;