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 GError *error = NULL;
291 SharedThreadData *ret;
293 if (g_once_init_enter (&shared_thread_data))
295 SharedThreadData *data;
297 /* Work-around for https://bugzilla.gnome.org/show_bug.cgi?id=627724 */
298 ensure_required_types ();
300 data = g_new0 (SharedThreadData, 1);
303 data->context = g_main_context_new ();
304 data->loop = g_main_loop_new (data->context, FALSE);
305 data->thread = g_thread_create (gdbus_shared_thread_func,
309 g_assert_no_error (error);
310 /* We can cast between gsize and gpointer safely */
311 g_once_init_leave (&shared_thread_data, (gsize) data);
314 ret = (SharedThreadData*) shared_thread_data;
315 g_atomic_int_inc (&ret->refcount);
320 _g_dbus_shared_thread_unref (SharedThreadData *data)
322 /* TODO: actually destroy the shared thread here */
324 g_assert (data != NULL);
325 if (g_atomic_int_dec_and_test (&data->refcount))
327 g_main_loop_quit (data->loop);
328 //g_thread_join (data->thread);
329 g_main_loop_unref (data->loop);
330 g_main_context_unref (data->context);
335 /* ---------------------------------------------------------------------------------------------------- */
339 volatile gint ref_count;
341 SharedThreadData *shared_thread_data;
343 /* really a boolean, but GLib 2.28 lacks atomic boolean ops */
344 volatile gint stopped;
346 /* TODO: frozen (e.g. G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING) currently
347 * only affects messages received from the other peer (since GDBusServer is the
348 * only user) - we might want it to affect messages sent to the other peer too?
351 GDBusCapabilityFlags capabilities;
352 GQueue *received_messages_while_frozen;
355 GCancellable *cancellable;
356 GDBusWorkerMessageReceivedCallback message_received_callback;
357 GDBusWorkerMessageAboutToBeSentCallback message_about_to_be_sent_callback;
358 GDBusWorkerDisconnectedCallback disconnected_callback;
361 /* if not NULL, stream is GSocketConnection */
364 /* used for reading */
367 gsize read_buffer_allocated_size;
368 gsize read_buffer_cur_size;
369 gsize read_buffer_bytes_wanted;
370 GUnixFDList *read_fd_list;
371 GSocketControlMessage **read_ancillary_messages;
372 gint read_num_ancillary_messages;
374 /* TRUE if an async write, flush or close is pending.
375 * Only the worker thread may change its value, and only with the write_lock.
376 * Other threads may read its value when holding the write_lock.
377 * The worker thread may read its value at any time.
379 gboolean output_pending;
380 /* used for writing */
382 /* queue of MessageToWriteData, protected by write_lock */
384 /* protected by write_lock */
385 guint64 write_num_messages_written;
386 /* list of FlushData, protected by write_lock */
387 GList *write_pending_flushes;
388 /* list of CloseData, protected by write_lock */
389 GList *pending_close_attempts;
392 static void _g_dbus_worker_unref (GDBusWorker *worker);
394 /* ---------------------------------------------------------------------------------------------------- */
400 guint64 number_to_wait_for;
404 struct _MessageToWriteData ;
405 typedef struct _MessageToWriteData MessageToWriteData;
407 static void message_to_write_data_free (MessageToWriteData *data);
409 static void read_message_print_transport_debug (gssize bytes_read,
410 GDBusWorker *worker);
412 static void write_message_print_transport_debug (gssize bytes_written,
413 MessageToWriteData *data);
417 GCancellable *cancellable;
418 GSimpleAsyncResult *result;
421 static void close_data_free (CloseData *close_data)
423 if (close_data->cancellable != NULL)
424 g_object_unref (close_data->cancellable);
426 if (close_data->result != NULL)
427 g_object_unref (close_data->result);
429 _g_dbus_worker_unref (close_data->worker);
430 g_slice_free (CloseData, close_data);
433 /* ---------------------------------------------------------------------------------------------------- */
436 _g_dbus_worker_ref (GDBusWorker *worker)
438 g_atomic_int_inc (&worker->ref_count);
443 _g_dbus_worker_unref (GDBusWorker *worker)
445 if (g_atomic_int_dec_and_test (&worker->ref_count))
447 g_assert (worker->write_pending_flushes == NULL);
449 _g_dbus_shared_thread_unref (worker->shared_thread_data);
451 g_object_unref (worker->stream);
453 g_mutex_free (worker->read_lock);
454 g_object_unref (worker->cancellable);
455 if (worker->read_fd_list != NULL)
456 g_object_unref (worker->read_fd_list);
458 g_queue_foreach (worker->received_messages_while_frozen, (GFunc) g_object_unref, NULL);
459 g_queue_free (worker->received_messages_while_frozen);
461 g_mutex_free (worker->write_lock);
462 g_queue_foreach (worker->write_queue, (GFunc) message_to_write_data_free, NULL);
463 g_queue_free (worker->write_queue);
465 g_free (worker->read_buffer);
472 _g_dbus_worker_emit_disconnected (GDBusWorker *worker,
473 gboolean remote_peer_vanished,
476 if (!g_atomic_int_get (&worker->stopped))
477 worker->disconnected_callback (worker, remote_peer_vanished, error, worker->user_data);
481 _g_dbus_worker_emit_message_received (GDBusWorker *worker,
482 GDBusMessage *message)
484 if (!g_atomic_int_get (&worker->stopped))
485 worker->message_received_callback (worker, message, worker->user_data);
488 static GDBusMessage *
489 _g_dbus_worker_emit_message_about_to_be_sent (GDBusWorker *worker,
490 GDBusMessage *message)
493 if (!g_atomic_int_get (&worker->stopped))
494 ret = worker->message_about_to_be_sent_callback (worker, message, worker->user_data);
500 /* can only be called from private thread with read-lock held - takes ownership of @message */
502 _g_dbus_worker_queue_or_deliver_received_message (GDBusWorker *worker,
503 GDBusMessage *message)
505 if (worker->frozen || g_queue_get_length (worker->received_messages_while_frozen) > 0)
508 g_queue_push_tail (worker->received_messages_while_frozen, message);
512 /* not frozen, nor anything in queue */
513 _g_dbus_worker_emit_message_received (worker, message);
514 g_object_unref (message);
518 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
520 unfreeze_in_idle_cb (gpointer user_data)
522 GDBusWorker *worker = user_data;
523 GDBusMessage *message;
525 g_mutex_lock (worker->read_lock);
528 while ((message = g_queue_pop_head (worker->received_messages_while_frozen)) != NULL)
530 _g_dbus_worker_emit_message_received (worker, message);
531 g_object_unref (message);
533 worker->frozen = FALSE;
537 g_assert (g_queue_get_length (worker->received_messages_while_frozen) == 0);
539 g_mutex_unlock (worker->read_lock);
543 /* can be called from any thread */
545 _g_dbus_worker_unfreeze (GDBusWorker *worker)
547 GSource *idle_source;
548 idle_source = g_idle_source_new ();
549 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
550 g_source_set_callback (idle_source,
552 _g_dbus_worker_ref (worker),
553 (GDestroyNotify) _g_dbus_worker_unref);
554 g_source_attach (idle_source, worker->shared_thread_data->context);
555 g_source_unref (idle_source);
558 /* ---------------------------------------------------------------------------------------------------- */
560 static void _g_dbus_worker_do_read_unlocked (GDBusWorker *worker);
562 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
564 _g_dbus_worker_do_read_cb (GInputStream *input_stream,
568 GDBusWorker *worker = user_data;
572 g_mutex_lock (worker->read_lock);
574 /* If already stopped, don't even process the reply */
575 if (g_atomic_int_get (&worker->stopped))
579 if (worker->socket == NULL)
580 bytes_read = g_input_stream_read_finish (g_io_stream_get_input_stream (worker->stream),
584 bytes_read = _g_socket_read_with_control_messages_finish (worker->socket,
587 if (worker->read_num_ancillary_messages > 0)
590 for (n = 0; n < worker->read_num_ancillary_messages; n++)
592 GSocketControlMessage *control_message = G_SOCKET_CONTROL_MESSAGE (worker->read_ancillary_messages[n]);
598 else if (G_IS_UNIX_FD_MESSAGE (control_message))
600 GUnixFDMessage *fd_message;
604 fd_message = G_UNIX_FD_MESSAGE (control_message);
605 fds = g_unix_fd_message_steal_fds (fd_message, &num_fds);
606 if (worker->read_fd_list == NULL)
608 worker->read_fd_list = g_unix_fd_list_new_from_array (fds, num_fds);
613 for (n = 0; n < num_fds; n++)
615 /* TODO: really want a append_steal() */
616 g_unix_fd_list_append (worker->read_fd_list, fds[n], NULL);
622 else if (G_IS_UNIX_CREDENTIALS_MESSAGE (control_message))
634 "Unexpected ancillary message of type %s received from peer",
635 g_type_name (G_TYPE_FROM_INSTANCE (control_message)));
636 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
637 g_error_free (error);
638 g_object_unref (control_message);
640 while (n < worker->read_num_ancillary_messages)
641 g_object_unref (worker->read_ancillary_messages[n++]);
642 g_free (worker->read_ancillary_messages);
646 g_object_unref (control_message);
648 g_free (worker->read_ancillary_messages);
651 if (bytes_read == -1)
653 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
654 g_error_free (error);
659 g_debug ("read %d bytes (is_closed=%d blocking=%d condition=0x%02x) stream %p, %p",
661 g_socket_is_closed (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream))),
662 g_socket_get_blocking (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream))),
663 g_socket_condition_check (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream)),
664 G_IO_IN | G_IO_OUT | G_IO_HUP),
669 /* TODO: hmm, hmm... */
675 "Underlying GIOStream returned 0 bytes on an async read");
676 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
677 g_error_free (error);
681 read_message_print_transport_debug (bytes_read, worker);
683 worker->read_buffer_cur_size += bytes_read;
684 if (worker->read_buffer_bytes_wanted == worker->read_buffer_cur_size)
686 /* OK, got what we asked for! */
687 if (worker->read_buffer_bytes_wanted == 16)
690 /* OK, got the header - determine how many more bytes are needed */
692 message_len = g_dbus_message_bytes_needed ((guchar *) worker->read_buffer,
695 if (message_len == -1)
697 g_warning ("_g_dbus_worker_do_read_cb: error determing bytes needed: %s", error->message);
698 _g_dbus_worker_emit_disconnected (worker, FALSE, error);
699 g_error_free (error);
703 worker->read_buffer_bytes_wanted = message_len;
704 _g_dbus_worker_do_read_unlocked (worker);
708 GDBusMessage *message;
711 /* TODO: use connection->priv->auth to decode the message */
713 message = g_dbus_message_new_from_blob ((guchar *) worker->read_buffer,
714 worker->read_buffer_cur_size,
715 worker->capabilities,
720 s = _g_dbus_hexdump (worker->read_buffer, worker->read_buffer_cur_size, 2);
721 g_warning ("Error decoding D-Bus message of %" G_GSIZE_FORMAT " bytes\n"
723 "The payload is as follows:\n"
725 worker->read_buffer_cur_size,
729 _g_dbus_worker_emit_disconnected (worker, FALSE, error);
730 g_error_free (error);
735 if (worker->read_fd_list != NULL)
737 g_dbus_message_set_unix_fd_list (message, worker->read_fd_list);
738 g_object_unref (worker->read_fd_list);
739 worker->read_fd_list = NULL;
743 if (G_UNLIKELY (_g_dbus_debug_message ()))
746 _g_dbus_debug_print_lock ();
747 g_print ("========================================================================\n"
748 "GDBus-debug:Message:\n"
749 " <<<< RECEIVED D-Bus message (%" G_GSIZE_FORMAT " bytes)\n",
750 worker->read_buffer_cur_size);
751 s = g_dbus_message_print (message, 2);
754 if (G_UNLIKELY (_g_dbus_debug_payload ()))
756 s = _g_dbus_hexdump (worker->read_buffer, worker->read_buffer_cur_size, 2);
760 _g_dbus_debug_print_unlock ();
763 /* yay, got a message, go deliver it */
764 _g_dbus_worker_queue_or_deliver_received_message (worker, message);
766 /* start reading another message! */
767 worker->read_buffer_bytes_wanted = 0;
768 worker->read_buffer_cur_size = 0;
769 _g_dbus_worker_do_read_unlocked (worker);
774 /* didn't get all the bytes we requested - so repeat the request... */
775 _g_dbus_worker_do_read_unlocked (worker);
779 g_mutex_unlock (worker->read_lock);
781 /* gives up the reference acquired when calling g_input_stream_read_async() */
782 _g_dbus_worker_unref (worker);
785 /* called in private thread shared by all GDBusConnection instances (with read-lock held) */
787 _g_dbus_worker_do_read_unlocked (GDBusWorker *worker)
789 /* if bytes_wanted is zero, it means start reading a message */
790 if (worker->read_buffer_bytes_wanted == 0)
792 worker->read_buffer_cur_size = 0;
793 worker->read_buffer_bytes_wanted = 16;
796 /* ensure we have a (big enough) buffer */
797 if (worker->read_buffer == NULL || worker->read_buffer_bytes_wanted > worker->read_buffer_allocated_size)
799 /* TODO: 4096 is randomly chosen; might want a better chosen default minimum */
800 worker->read_buffer_allocated_size = MAX (worker->read_buffer_bytes_wanted, 4096);
801 worker->read_buffer = g_realloc (worker->read_buffer, worker->read_buffer_allocated_size);
804 if (worker->socket == NULL)
805 g_input_stream_read_async (g_io_stream_get_input_stream (worker->stream),
806 worker->read_buffer + worker->read_buffer_cur_size,
807 worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size,
810 (GAsyncReadyCallback) _g_dbus_worker_do_read_cb,
811 _g_dbus_worker_ref (worker));
814 worker->read_ancillary_messages = NULL;
815 worker->read_num_ancillary_messages = 0;
816 _g_socket_read_with_control_messages (worker->socket,
817 worker->read_buffer + worker->read_buffer_cur_size,
818 worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size,
819 &worker->read_ancillary_messages,
820 &worker->read_num_ancillary_messages,
823 (GAsyncReadyCallback) _g_dbus_worker_do_read_cb,
824 _g_dbus_worker_ref (worker));
828 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
830 _g_dbus_worker_do_initial_read (gpointer data)
832 GDBusWorker *worker = data;
833 g_mutex_lock (worker->read_lock);
834 _g_dbus_worker_do_read_unlocked (worker);
835 g_mutex_unlock (worker->read_lock);
839 /* ---------------------------------------------------------------------------------------------------- */
841 struct _MessageToWriteData
844 GDBusMessage *message;
849 GSimpleAsyncResult *simple;
854 message_to_write_data_free (MessageToWriteData *data)
856 _g_dbus_worker_unref (data->worker);
858 g_object_unref (data->message);
863 /* ---------------------------------------------------------------------------------------------------- */
865 static void write_message_continue_writing (MessageToWriteData *data);
867 /* called in private thread shared by all GDBusConnection instances
869 * write-lock is not held on entry
870 * output_pending is true on entry
873 write_message_async_cb (GObject *source_object,
877 MessageToWriteData *data = user_data;
878 GSimpleAsyncResult *simple;
879 gssize bytes_written;
882 /* Note: we can't access data->simple after calling g_async_result_complete () because the
883 * callback can free @data and we're not completing in idle. So use a copy of the pointer.
885 simple = data->simple;
888 bytes_written = g_output_stream_write_finish (G_OUTPUT_STREAM (source_object),
891 if (bytes_written == -1)
893 g_simple_async_result_take_error (simple, error);
894 g_simple_async_result_complete (simple);
895 g_object_unref (simple);
898 g_assert (bytes_written > 0); /* zero is never returned */
900 write_message_print_transport_debug (bytes_written, data);
902 data->total_written += bytes_written;
903 g_assert (data->total_written <= data->blob_size);
904 if (data->total_written == data->blob_size)
906 g_simple_async_result_complete (simple);
907 g_object_unref (simple);
911 write_message_continue_writing (data);
917 /* called in private thread shared by all GDBusConnection instances
919 * write-lock is not held on entry
920 * output_pending is true on entry
923 on_socket_ready (GSocket *socket,
924 GIOCondition condition,
927 MessageToWriteData *data = user_data;
928 write_message_continue_writing (data);
929 return FALSE; /* remove source */
932 /* called in private thread shared by all GDBusConnection instances
934 * write-lock is not held on entry
935 * output_pending is true on entry
938 write_message_continue_writing (MessageToWriteData *data)
940 GOutputStream *ostream;
941 GSimpleAsyncResult *simple;
943 GUnixFDList *fd_list;
946 /* Note: we can't access data->simple after calling g_async_result_complete () because the
947 * callback can free @data and we're not completing in idle. So use a copy of the pointer.
949 simple = data->simple;
951 ostream = g_io_stream_get_output_stream (data->worker->stream);
953 fd_list = g_dbus_message_get_unix_fd_list (data->message);
956 g_assert (!g_output_stream_has_pending (ostream));
957 g_assert_cmpint (data->total_written, <, data->blob_size);
963 else if (G_IS_SOCKET_OUTPUT_STREAM (ostream) && data->total_written == 0)
965 GOutputVector vector;
966 GSocketControlMessage *control_message;
967 gssize bytes_written;
970 vector.buffer = data->blob;
971 vector.size = data->blob_size;
973 control_message = NULL;
974 if (fd_list != NULL && g_unix_fd_list_get_length (fd_list) > 0)
976 if (!(data->worker->capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING))
978 g_simple_async_result_set_error (simple,
981 "Tried sending a file descriptor but remote peer does not support this capability");
982 g_simple_async_result_complete (simple);
983 g_object_unref (simple);
986 control_message = g_unix_fd_message_new_with_fd_list (fd_list);
990 bytes_written = g_socket_send_message (data->worker->socket,
994 control_message != NULL ? &control_message : NULL,
995 control_message != NULL ? 1 : 0,
997 data->worker->cancellable,
999 if (control_message != NULL)
1000 g_object_unref (control_message);
1002 if (bytes_written == -1)
1004 /* Handle WOULD_BLOCK by waiting until there's room in the buffer */
1005 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1008 source = g_socket_create_source (data->worker->socket,
1009 G_IO_OUT | G_IO_HUP | G_IO_ERR,
1010 data->worker->cancellable);
1011 g_source_set_callback (source,
1012 (GSourceFunc) on_socket_ready,
1014 NULL); /* GDestroyNotify */
1015 g_source_attach (source, g_main_context_get_thread_default ());
1016 g_source_unref (source);
1017 g_error_free (error);
1020 g_simple_async_result_take_error (simple, error);
1021 g_simple_async_result_complete (simple);
1022 g_object_unref (simple);
1025 g_assert (bytes_written > 0); /* zero is never returned */
1027 write_message_print_transport_debug (bytes_written, data);
1029 data->total_written += bytes_written;
1030 g_assert (data->total_written <= data->blob_size);
1031 if (data->total_written == data->blob_size)
1033 g_simple_async_result_complete (simple);
1034 g_object_unref (simple);
1038 write_message_continue_writing (data);
1044 if (fd_list != NULL)
1046 g_simple_async_result_set_error (simple,
1049 "Tried sending a file descriptor on unsupported stream of type %s",
1050 g_type_name (G_TYPE_FROM_INSTANCE (ostream)));
1051 g_simple_async_result_complete (simple);
1052 g_object_unref (simple);
1057 g_output_stream_write_async (ostream,
1058 (const gchar *) data->blob + data->total_written,
1059 data->blob_size - data->total_written,
1061 data->worker->cancellable,
1062 write_message_async_cb,
1069 /* called in private thread shared by all GDBusConnection instances
1071 * write-lock is not held on entry
1072 * output_pending is true on entry
1075 write_message_async (GDBusWorker *worker,
1076 MessageToWriteData *data,
1077 GAsyncReadyCallback callback,
1080 data->simple = g_simple_async_result_new (NULL,
1083 write_message_async);
1084 data->total_written = 0;
1085 write_message_continue_writing (data);
1088 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
1090 write_message_finish (GAsyncResult *res,
1093 g_warn_if_fail (g_simple_async_result_get_source_tag (G_SIMPLE_ASYNC_RESULT (res)) == write_message_async);
1094 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error))
1099 /* ---------------------------------------------------------------------------------------------------- */
1101 static void maybe_write_next_message (GDBusWorker *worker);
1105 GDBusWorker *worker;
1110 flush_data_list_complete (const GList *flushers,
1111 const GError *error)
1115 for (l = flushers; l != NULL; l = l->next)
1117 FlushData *f = l->data;
1119 f->error = error != NULL ? g_error_copy (error) : NULL;
1121 g_mutex_lock (f->mutex);
1122 g_cond_signal (f->cond);
1123 g_mutex_unlock (f->mutex);
1127 /* called in private thread shared by all GDBusConnection instances
1129 * write-lock is not held on entry
1130 * output_pending is true on entry
1133 ostream_flush_cb (GObject *source_object,
1137 FlushAsyncData *data = user_data;
1141 g_output_stream_flush_finish (G_OUTPUT_STREAM (source_object),
1147 if (G_UNLIKELY (_g_dbus_debug_transport ()))
1149 _g_dbus_debug_print_lock ();
1150 g_print ("========================================================================\n"
1151 "GDBus-debug:Transport:\n"
1152 " ---- FLUSHED stream of type %s\n",
1153 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream))));
1154 _g_dbus_debug_print_unlock ();
1158 g_assert (data->flushers != NULL);
1159 flush_data_list_complete (data->flushers, error);
1160 g_list_free (data->flushers);
1163 g_error_free (error);
1165 /* Make sure we tell folks that we don't have additional
1167 g_mutex_lock (data->worker->write_lock);
1168 g_assert (data->worker->output_pending);
1169 data->worker->output_pending = FALSE;
1170 g_mutex_unlock (data->worker->write_lock);
1172 /* OK, cool, finally kick off the next write */
1173 maybe_write_next_message (data->worker);
1175 _g_dbus_worker_unref (data->worker);
1179 /* called in private thread shared by all GDBusConnection instances
1181 * write-lock is not held on entry
1182 * output_pending is false on entry
1185 message_written (GDBusWorker *worker,
1186 MessageToWriteData *message_data)
1192 /* first log the fact that we wrote a message */
1193 if (G_UNLIKELY (_g_dbus_debug_message ()))
1196 _g_dbus_debug_print_lock ();
1197 g_print ("========================================================================\n"
1198 "GDBus-debug:Message:\n"
1199 " >>>> SENT D-Bus message (%" G_GSIZE_FORMAT " bytes)\n",
1200 message_data->blob_size);
1201 s = g_dbus_message_print (message_data->message, 2);
1204 if (G_UNLIKELY (_g_dbus_debug_payload ()))
1206 s = _g_dbus_hexdump (message_data->blob, message_data->blob_size, 2);
1207 g_print ("%s\n", s);
1210 _g_dbus_debug_print_unlock ();
1213 /* then first wake up pending flushes and, if needed, flush the stream */
1215 g_mutex_lock (worker->write_lock);
1216 worker->write_num_messages_written += 1;
1217 for (l = worker->write_pending_flushes; l != NULL; l = ll)
1219 FlushData *f = l->data;
1222 if (f->number_to_wait_for == worker->write_num_messages_written)
1224 flushers = g_list_append (flushers, f);
1225 worker->write_pending_flushes = g_list_delete_link (worker->write_pending_flushes, l);
1228 if (flushers != NULL)
1230 g_assert (!worker->output_pending);
1231 worker->output_pending = TRUE;
1233 g_mutex_unlock (worker->write_lock);
1235 if (flushers != NULL)
1237 FlushAsyncData *data;
1238 data = g_new0 (FlushAsyncData, 1);
1239 data->worker = _g_dbus_worker_ref (worker);
1240 data->flushers = flushers;
1241 /* flush the stream before writing the next message */
1242 g_output_stream_flush_async (g_io_stream_get_output_stream (worker->stream),
1244 worker->cancellable,
1250 /* kick off the next write! */
1251 maybe_write_next_message (worker);
1255 /* called in private thread shared by all GDBusConnection instances
1257 * write-lock is not held on entry
1258 * output_pending is true on entry
1261 write_message_cb (GObject *source_object,
1265 MessageToWriteData *data = user_data;
1268 g_mutex_lock (data->worker->write_lock);
1269 g_assert (data->worker->output_pending);
1270 data->worker->output_pending = FALSE;
1271 g_mutex_unlock (data->worker->write_lock);
1274 if (!write_message_finish (res, &error))
1277 _g_dbus_worker_emit_disconnected (data->worker, TRUE, error);
1278 g_error_free (error);
1281 /* this function will also kick of the next write (it might need to
1282 * flush so writing the next message might happen much later
1285 message_written (data->worker, data);
1287 message_to_write_data_free (data);
1290 /* called in private thread shared by all GDBusConnection instances
1292 * write-lock is not held on entry
1293 * output_pending is true on entry
1296 iostream_close_cb (GObject *source_object,
1300 GDBusWorker *worker = user_data;
1301 GError *error = NULL;
1302 GList *pending_close_attempts, *pending_flush_attempts;
1305 g_io_stream_close_finish (worker->stream, res, &error);
1307 g_mutex_lock (worker->write_lock);
1309 pending_close_attempts = worker->pending_close_attempts;
1310 worker->pending_close_attempts = NULL;
1312 pending_flush_attempts = worker->write_pending_flushes;
1313 worker->write_pending_flushes = NULL;
1315 send_queue = worker->write_queue;
1316 worker->write_queue = g_queue_new ();
1318 g_assert (worker->output_pending);
1319 worker->output_pending = FALSE;
1321 g_mutex_unlock (worker->write_lock);
1323 while (pending_close_attempts != NULL)
1325 CloseData *close_data = pending_close_attempts->data;
1327 pending_close_attempts = g_list_delete_link (pending_close_attempts,
1328 pending_close_attempts);
1330 if (close_data->result != NULL)
1333 g_simple_async_result_set_from_error (close_data->result, error);
1335 /* this must be in an idle because the result is likely to be
1336 * intended for another thread
1338 g_simple_async_result_complete_in_idle (close_data->result);
1341 close_data_free (close_data);
1344 g_clear_error (&error);
1346 /* all messages queued for sending are discarded */
1347 g_queue_foreach (send_queue, (GFunc) message_to_write_data_free, NULL);
1348 g_queue_free (send_queue);
1350 /* all queued flushes fail */
1351 error = g_error_new (G_IO_ERROR, G_IO_ERROR_CANCELLED,
1352 _("Operation was cancelled"));
1353 flush_data_list_complete (pending_flush_attempts, error);
1354 g_list_free (pending_flush_attempts);
1355 g_clear_error (&error);
1357 _g_dbus_worker_unref (worker);
1360 /* called in private thread shared by all GDBusConnection instances
1362 * write-lock is not held on entry
1363 * output_pending must be false on entry
1366 maybe_write_next_message (GDBusWorker *worker)
1368 MessageToWriteData *data;
1371 /* we mustn't try to write two things at once */
1372 g_assert (!worker->output_pending);
1374 g_mutex_lock (worker->write_lock);
1376 /* if we want to close the connection, that takes precedence */
1377 if (worker->pending_close_attempts != NULL)
1379 worker->output_pending = TRUE;
1381 g_io_stream_close_async (worker->stream, G_PRIORITY_DEFAULT,
1382 NULL, iostream_close_cb,
1383 _g_dbus_worker_ref (worker));
1388 data = g_queue_pop_head (worker->write_queue);
1391 worker->output_pending = TRUE;
1394 g_mutex_unlock (worker->write_lock);
1396 /* Note that write_lock is only used for protecting the @write_queue
1397 * and @output_pending fields of the GDBusWorker struct ... which we
1398 * need to modify from arbitrary threads in _g_dbus_worker_send_message().
1400 * Therefore, it's fine to drop it here when calling back into user
1401 * code and then writing the message out onto the GIOStream since this
1402 * function only runs on the worker thread.
1406 GDBusMessage *old_message;
1408 gsize new_blob_size;
1411 old_message = data->message;
1412 data->message = _g_dbus_worker_emit_message_about_to_be_sent (worker, data->message);
1413 if (data->message == old_message)
1415 /* filters had no effect - do nothing */
1417 else if (data->message == NULL)
1419 /* filters dropped message */
1420 g_mutex_lock (worker->write_lock);
1421 worker->output_pending = FALSE;
1422 g_mutex_unlock (worker->write_lock);
1423 message_to_write_data_free (data);
1428 /* filters altered the message -> reencode */
1430 new_blob = g_dbus_message_to_blob (data->message,
1432 worker->capabilities,
1434 if (new_blob == NULL)
1436 /* if filter make the GDBusMessage unencodeable, just complain on stderr and send
1437 * the old message instead
1439 g_warning ("Error encoding GDBusMessage with serial %d altered by filter function: %s",
1440 g_dbus_message_get_serial (data->message),
1442 g_error_free (error);
1446 g_free (data->blob);
1447 data->blob = (gchar *) new_blob;
1448 data->blob_size = new_blob_size;
1452 write_message_async (worker,
1459 /* called in private thread shared by all GDBusConnection instances
1461 * write-lock is not held on entry
1462 * output_pending may be true or false
1465 write_message_in_idle_cb (gpointer user_data)
1467 GDBusWorker *worker = user_data;
1469 /* Because this is the worker thread, we can read this struct member
1470 * without holding the lock: no other thread ever modifies it.
1472 if (!worker->output_pending)
1473 maybe_write_next_message (worker);
1479 * @write_data: (transfer full) (allow-none):
1480 * @close_data: (transfer full) (allow-none):
1482 * Can be called from any thread
1484 * write_lock is not held on entry
1485 * output_pending may be true or false
1488 schedule_write_in_worker_thread (GDBusWorker *worker,
1489 MessageToWriteData *write_data,
1490 CloseData *close_data)
1492 g_mutex_lock (worker->write_lock);
1494 if (write_data != NULL)
1495 g_queue_push_tail (worker->write_queue, write_data);
1497 if (close_data != NULL)
1498 worker->pending_close_attempts = g_list_prepend (worker->pending_close_attempts,
1501 if (!worker->output_pending)
1503 GSource *idle_source;
1504 idle_source = g_idle_source_new ();
1505 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1506 g_source_set_callback (idle_source,
1507 write_message_in_idle_cb,
1508 _g_dbus_worker_ref (worker),
1509 (GDestroyNotify) _g_dbus_worker_unref);
1510 g_source_attach (idle_source, worker->shared_thread_data->context);
1511 g_source_unref (idle_source);
1514 g_mutex_unlock (worker->write_lock);
1517 /* ---------------------------------------------------------------------------------------------------- */
1519 /* can be called from any thread - steals blob
1521 * write_lock is not held on entry
1522 * output_pending may be true or false
1525 _g_dbus_worker_send_message (GDBusWorker *worker,
1526 GDBusMessage *message,
1530 MessageToWriteData *data;
1532 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1533 g_return_if_fail (blob != NULL);
1534 g_return_if_fail (blob_len > 16);
1536 data = g_new0 (MessageToWriteData, 1);
1537 data->worker = _g_dbus_worker_ref (worker);
1538 data->message = g_object_ref (message);
1539 data->blob = blob; /* steal! */
1540 data->blob_size = blob_len;
1542 schedule_write_in_worker_thread (worker, data, NULL);
1545 /* ---------------------------------------------------------------------------------------------------- */
1548 _g_dbus_worker_new (GIOStream *stream,
1549 GDBusCapabilityFlags capabilities,
1550 gboolean initially_frozen,
1551 GDBusWorkerMessageReceivedCallback message_received_callback,
1552 GDBusWorkerMessageAboutToBeSentCallback message_about_to_be_sent_callback,
1553 GDBusWorkerDisconnectedCallback disconnected_callback,
1556 GDBusWorker *worker;
1557 GSource *idle_source;
1559 g_return_val_if_fail (G_IS_IO_STREAM (stream), NULL);
1560 g_return_val_if_fail (message_received_callback != NULL, NULL);
1561 g_return_val_if_fail (message_about_to_be_sent_callback != NULL, NULL);
1562 g_return_val_if_fail (disconnected_callback != NULL, NULL);
1564 worker = g_new0 (GDBusWorker, 1);
1565 worker->ref_count = 1;
1567 worker->read_lock = g_mutex_new ();
1568 worker->message_received_callback = message_received_callback;
1569 worker->message_about_to_be_sent_callback = message_about_to_be_sent_callback;
1570 worker->disconnected_callback = disconnected_callback;
1571 worker->user_data = user_data;
1572 worker->stream = g_object_ref (stream);
1573 worker->capabilities = capabilities;
1574 worker->cancellable = g_cancellable_new ();
1575 worker->output_pending = FALSE;
1577 worker->frozen = initially_frozen;
1578 worker->received_messages_while_frozen = g_queue_new ();
1580 worker->write_lock = g_mutex_new ();
1581 worker->write_queue = g_queue_new ();
1583 if (G_IS_SOCKET_CONNECTION (worker->stream))
1584 worker->socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream));
1586 worker->shared_thread_data = _g_dbus_shared_thread_ref ();
1589 idle_source = g_idle_source_new ();
1590 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1591 g_source_set_callback (idle_source,
1592 _g_dbus_worker_do_initial_read,
1593 _g_dbus_worker_ref (worker),
1594 (GDestroyNotify) _g_dbus_worker_unref);
1595 g_source_attach (idle_source, worker->shared_thread_data->context);
1596 g_source_unref (idle_source);
1601 /* ---------------------------------------------------------------------------------------------------- */
1603 /* can be called from any thread
1605 * write_lock is not held on entry
1606 * output_pending may be true or false
1609 _g_dbus_worker_close (GDBusWorker *worker,
1610 GCancellable *cancellable,
1611 GSimpleAsyncResult *result)
1613 CloseData *close_data;
1615 close_data = g_slice_new0 (CloseData);
1616 close_data->worker = _g_dbus_worker_ref (worker);
1617 close_data->cancellable =
1618 (cancellable == NULL ? NULL : g_object_ref (cancellable));
1619 close_data->result = (result == NULL ? NULL : g_object_ref (result));
1621 g_cancellable_cancel (worker->cancellable);
1622 schedule_write_in_worker_thread (worker, NULL, close_data);
1625 /* This can be called from any thread - frees worker. Note that
1626 * callbacks might still happen if called from another thread than the
1627 * worker - use your own synchronization primitive in the callbacks.
1629 * write_lock is not held on entry
1630 * output_pending may be true or false
1633 _g_dbus_worker_stop (GDBusWorker *worker)
1635 g_atomic_int_set (&worker->stopped, TRUE);
1637 /* Cancel any pending operations and schedule a close of the underlying I/O
1638 * stream in the worker thread
1640 _g_dbus_worker_close (worker, NULL, NULL);
1642 /* _g_dbus_worker_close holds a ref until after an idle in the the worker
1643 * thread has run, so we no longer need to unref in an idle like in
1646 _g_dbus_worker_unref (worker);
1649 /* ---------------------------------------------------------------------------------------------------- */
1651 /* can be called from any thread (except the worker thread) - blocks
1652 * calling thread until all queued outgoing messages are written and
1653 * the transport has been flushed
1655 * write_lock is not held on entry
1656 * output_pending may be true or false
1659 _g_dbus_worker_flush_sync (GDBusWorker *worker,
1660 GCancellable *cancellable,
1669 /* if the queue is empty, there's nothing to wait for */
1670 g_mutex_lock (worker->write_lock);
1671 if (g_queue_get_length (worker->write_queue) > 0)
1673 data = g_new0 (FlushData, 1);
1674 data->mutex = g_mutex_new ();
1675 data->cond = g_cond_new ();
1676 data->number_to_wait_for = worker->write_num_messages_written + g_queue_get_length (worker->write_queue);
1677 g_mutex_lock (data->mutex);
1678 worker->write_pending_flushes = g_list_prepend (worker->write_pending_flushes, data);
1680 g_mutex_unlock (worker->write_lock);
1684 g_cond_wait (data->cond, data->mutex);
1685 g_mutex_unlock (data->mutex);
1687 /* note:the element is removed from worker->write_pending_flushes in flush_cb() above */
1688 g_cond_free (data->cond);
1689 g_mutex_free (data->mutex);
1690 if (data->error != NULL)
1693 g_propagate_error (error, data->error);
1701 /* ---------------------------------------------------------------------------------------------------- */
1703 #define G_DBUS_DEBUG_AUTHENTICATION (1<<0)
1704 #define G_DBUS_DEBUG_TRANSPORT (1<<1)
1705 #define G_DBUS_DEBUG_MESSAGE (1<<2)
1706 #define G_DBUS_DEBUG_PAYLOAD (1<<3)
1707 #define G_DBUS_DEBUG_CALL (1<<4)
1708 #define G_DBUS_DEBUG_SIGNAL (1<<5)
1709 #define G_DBUS_DEBUG_INCOMING (1<<6)
1710 #define G_DBUS_DEBUG_RETURN (1<<7)
1711 #define G_DBUS_DEBUG_EMISSION (1<<8)
1712 #define G_DBUS_DEBUG_ADDRESS (1<<9)
1714 static gint _gdbus_debug_flags = 0;
1717 _g_dbus_debug_authentication (void)
1719 _g_dbus_initialize ();
1720 return (_gdbus_debug_flags & G_DBUS_DEBUG_AUTHENTICATION) != 0;
1724 _g_dbus_debug_transport (void)
1726 _g_dbus_initialize ();
1727 return (_gdbus_debug_flags & G_DBUS_DEBUG_TRANSPORT) != 0;
1731 _g_dbus_debug_message (void)
1733 _g_dbus_initialize ();
1734 return (_gdbus_debug_flags & G_DBUS_DEBUG_MESSAGE) != 0;
1738 _g_dbus_debug_payload (void)
1740 _g_dbus_initialize ();
1741 return (_gdbus_debug_flags & G_DBUS_DEBUG_PAYLOAD) != 0;
1745 _g_dbus_debug_call (void)
1747 _g_dbus_initialize ();
1748 return (_gdbus_debug_flags & G_DBUS_DEBUG_CALL) != 0;
1752 _g_dbus_debug_signal (void)
1754 _g_dbus_initialize ();
1755 return (_gdbus_debug_flags & G_DBUS_DEBUG_SIGNAL) != 0;
1759 _g_dbus_debug_incoming (void)
1761 _g_dbus_initialize ();
1762 return (_gdbus_debug_flags & G_DBUS_DEBUG_INCOMING) != 0;
1766 _g_dbus_debug_return (void)
1768 _g_dbus_initialize ();
1769 return (_gdbus_debug_flags & G_DBUS_DEBUG_RETURN) != 0;
1773 _g_dbus_debug_emission (void)
1775 _g_dbus_initialize ();
1776 return (_gdbus_debug_flags & G_DBUS_DEBUG_EMISSION) != 0;
1780 _g_dbus_debug_address (void)
1782 _g_dbus_initialize ();
1783 return (_gdbus_debug_flags & G_DBUS_DEBUG_ADDRESS) != 0;
1786 G_LOCK_DEFINE_STATIC (print_lock);
1789 _g_dbus_debug_print_lock (void)
1791 G_LOCK (print_lock);
1795 _g_dbus_debug_print_unlock (void)
1797 G_UNLOCK (print_lock);
1801 * _g_dbus_initialize:
1803 * Does various one-time init things such as
1805 * - registering the G_DBUS_ERROR error domain
1806 * - parses the G_DBUS_DEBUG environment variable
1809 _g_dbus_initialize (void)
1811 static volatile gsize initialized = 0;
1813 if (g_once_init_enter (&initialized))
1815 volatile GQuark g_dbus_error_domain;
1818 g_dbus_error_domain = G_DBUS_ERROR;
1819 (g_dbus_error_domain); /* To avoid -Wunused-but-set-variable */
1821 debug = g_getenv ("G_DBUS_DEBUG");
1824 const GDebugKey keys[] = {
1825 { "authentication", G_DBUS_DEBUG_AUTHENTICATION },
1826 { "transport", G_DBUS_DEBUG_TRANSPORT },
1827 { "message", G_DBUS_DEBUG_MESSAGE },
1828 { "payload", G_DBUS_DEBUG_PAYLOAD },
1829 { "call", G_DBUS_DEBUG_CALL },
1830 { "signal", G_DBUS_DEBUG_SIGNAL },
1831 { "incoming", G_DBUS_DEBUG_INCOMING },
1832 { "return", G_DBUS_DEBUG_RETURN },
1833 { "emission", G_DBUS_DEBUG_EMISSION },
1834 { "address", G_DBUS_DEBUG_ADDRESS }
1837 _gdbus_debug_flags = g_parse_debug_string (debug, keys, G_N_ELEMENTS (keys));
1838 if (_gdbus_debug_flags & G_DBUS_DEBUG_PAYLOAD)
1839 _gdbus_debug_flags |= G_DBUS_DEBUG_MESSAGE;
1842 g_once_init_leave (&initialized, 1);
1846 /* ---------------------------------------------------------------------------------------------------- */
1849 _g_dbus_compute_complete_signature (GDBusArgInfo **args)
1851 const GVariantType *arg_types[256];
1855 for (n = 0; args[n] != NULL; n++)
1857 /* DBus places a hard limit of 255 on signature length.
1858 * therefore number of args must be less than 256.
1862 arg_types[n] = G_VARIANT_TYPE (args[n]->signature);
1864 if G_UNLIKELY (arg_types[n] == NULL)
1870 return g_variant_type_new_tuple (arg_types, n);
1873 /* ---------------------------------------------------------------------------------------------------- */
1877 extern BOOL WINAPI ConvertSidToStringSidA (PSID Sid, LPSTR *StringSid);
1880 _g_dbus_win32_get_user_sid (void)
1884 DWORD token_information_len;
1891 h = INVALID_HANDLE_VALUE;
1893 if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &h))
1895 g_warning ("OpenProcessToken failed with error code %d", (gint) GetLastError ());
1899 /* Get length of buffer */
1900 token_information_len = 0;
1901 if (!GetTokenInformation (h, TokenUser, NULL, 0, &token_information_len))
1903 if (GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1905 g_warning ("GetTokenInformation() failed with error code %d", (gint) GetLastError ());
1909 user = g_malloc (token_information_len);
1910 if (!GetTokenInformation (h, TokenUser, user, token_information_len, &token_information_len))
1912 g_warning ("GetTokenInformation() failed with error code %d", (gint) GetLastError ());
1916 psid = user->User.Sid;
1917 if (!IsValidSid (psid))
1919 g_warning ("Invalid SID");
1923 if (!ConvertSidToStringSidA (psid, &sid))
1925 g_warning ("Invalid SID");
1929 ret = g_strdup (sid);
1934 if (h != INVALID_HANDLE_VALUE)
1940 /* ---------------------------------------------------------------------------------------------------- */
1943 _g_dbus_get_machine_id (GError **error)
1946 /* TODO: use PACKAGE_LOCALSTATEDIR ? */
1948 if (!g_file_get_contents ("/var/lib/dbus/machine-id",
1953 g_prefix_error (error, _("Unable to load /var/lib/dbus/machine-id: "));
1957 /* TODO: validate value */
1963 /* ---------------------------------------------------------------------------------------------------- */
1966 _g_dbus_enum_to_string (GType enum_type, gint value)
1970 GEnumValue *enum_value;
1972 klass = g_type_class_ref (enum_type);
1973 enum_value = g_enum_get_value (klass, value);
1974 if (enum_value != NULL)
1975 ret = g_strdup (enum_value->value_nick);
1977 ret = g_strdup_printf ("unknown (value %d)", value);
1978 g_type_class_unref (klass);
1982 /* ---------------------------------------------------------------------------------------------------- */
1985 write_message_print_transport_debug (gssize bytes_written,
1986 MessageToWriteData *data)
1988 if (G_LIKELY (!_g_dbus_debug_transport ()))
1991 _g_dbus_debug_print_lock ();
1992 g_print ("========================================================================\n"
1993 "GDBus-debug:Transport:\n"
1994 " >>>> WROTE %" G_GSIZE_FORMAT " bytes of message with serial %d and\n"
1995 " size %" G_GSIZE_FORMAT " from offset %" G_GSIZE_FORMAT " on a %s\n",
1997 g_dbus_message_get_serial (data->message),
1999 data->total_written,
2000 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream))));
2001 _g_dbus_debug_print_unlock ();
2006 /* ---------------------------------------------------------------------------------------------------- */
2009 read_message_print_transport_debug (gssize bytes_read,
2010 GDBusWorker *worker)
2014 gint32 message_length;
2016 if (G_LIKELY (!_g_dbus_debug_transport ()))
2019 size = bytes_read + worker->read_buffer_cur_size;
2023 message_length = g_dbus_message_bytes_needed ((guchar *) worker->read_buffer, size, NULL);
2026 switch (worker->read_buffer[0])
2030 serial = GUINT32_FROM_LE (((guint32 *) worker->read_buffer)[2]);
2034 serial = GUINT32_FROM_BE (((guint32 *) worker->read_buffer)[2]);
2037 /* an error will be set elsewhere if this happens */
2042 _g_dbus_debug_print_lock ();
2043 g_print ("========================================================================\n"
2044 "GDBus-debug:Transport:\n"
2045 " <<<< READ %" G_GSIZE_FORMAT " bytes of message with serial %d and\n"
2046 " size %d to offset %" G_GSIZE_FORMAT " from a %s\n",
2050 worker->read_buffer_cur_size,
2051 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_input_stream (worker->stream))));
2052 _g_dbus_debug_print_unlock ();
2057 /* ---------------------------------------------------------------------------------------------------- */
2060 _g_signal_accumulator_false_handled (GSignalInvocationHint *ihint,
2061 GValue *return_accu,
2062 const GValue *handler_return,
2065 gboolean continue_emission;
2066 gboolean signal_return;
2068 signal_return = g_value_get_boolean (handler_return);
2069 g_value_set_boolean (return_accu, signal_return);
2070 continue_emission = signal_return;
2072 return continue_emission;