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 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
651 g_error_free (error);
656 g_debug ("read %d bytes (is_closed=%d blocking=%d condition=0x%02x) stream %p, %p",
658 g_socket_is_closed (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream))),
659 g_socket_get_blocking (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream))),
660 g_socket_condition_check (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream)),
661 G_IO_IN | G_IO_OUT | G_IO_HUP),
666 /* TODO: hmm, hmm... */
672 "Underlying GIOStream returned 0 bytes on an async read");
673 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
674 g_error_free (error);
678 read_message_print_transport_debug (bytes_read, worker);
680 worker->read_buffer_cur_size += bytes_read;
681 if (worker->read_buffer_bytes_wanted == worker->read_buffer_cur_size)
683 /* OK, got what we asked for! */
684 if (worker->read_buffer_bytes_wanted == 16)
687 /* OK, got the header - determine how many more bytes are needed */
689 message_len = g_dbus_message_bytes_needed ((guchar *) worker->read_buffer,
692 if (message_len == -1)
694 g_warning ("_g_dbus_worker_do_read_cb: error determing bytes needed: %s", error->message);
695 _g_dbus_worker_emit_disconnected (worker, FALSE, error);
696 g_error_free (error);
700 worker->read_buffer_bytes_wanted = message_len;
701 _g_dbus_worker_do_read_unlocked (worker);
705 GDBusMessage *message;
708 /* TODO: use connection->priv->auth to decode the message */
710 message = g_dbus_message_new_from_blob ((guchar *) worker->read_buffer,
711 worker->read_buffer_cur_size,
712 worker->capabilities,
717 s = _g_dbus_hexdump (worker->read_buffer, worker->read_buffer_cur_size, 2);
718 g_warning ("Error decoding D-Bus message of %" G_GSIZE_FORMAT " bytes\n"
720 "The payload is as follows:\n"
722 worker->read_buffer_cur_size,
726 _g_dbus_worker_emit_disconnected (worker, FALSE, error);
727 g_error_free (error);
732 if (worker->read_fd_list != NULL)
734 g_dbus_message_set_unix_fd_list (message, worker->read_fd_list);
735 g_object_unref (worker->read_fd_list);
736 worker->read_fd_list = NULL;
740 if (G_UNLIKELY (_g_dbus_debug_message ()))
743 _g_dbus_debug_print_lock ();
744 g_print ("========================================================================\n"
745 "GDBus-debug:Message:\n"
746 " <<<< RECEIVED D-Bus message (%" G_GSIZE_FORMAT " bytes)\n",
747 worker->read_buffer_cur_size);
748 s = g_dbus_message_print (message, 2);
751 if (G_UNLIKELY (_g_dbus_debug_payload ()))
753 s = _g_dbus_hexdump (worker->read_buffer, worker->read_buffer_cur_size, 2);
757 _g_dbus_debug_print_unlock ();
760 /* yay, got a message, go deliver it */
761 _g_dbus_worker_queue_or_deliver_received_message (worker, message);
763 /* start reading another message! */
764 worker->read_buffer_bytes_wanted = 0;
765 worker->read_buffer_cur_size = 0;
766 _g_dbus_worker_do_read_unlocked (worker);
771 /* didn't get all the bytes we requested - so repeat the request... */
772 _g_dbus_worker_do_read_unlocked (worker);
776 g_mutex_unlock (&worker->read_lock);
778 /* gives up the reference acquired when calling g_input_stream_read_async() */
779 _g_dbus_worker_unref (worker);
782 /* called in private thread shared by all GDBusConnection instances (with read-lock held) */
784 _g_dbus_worker_do_read_unlocked (GDBusWorker *worker)
786 /* if bytes_wanted is zero, it means start reading a message */
787 if (worker->read_buffer_bytes_wanted == 0)
789 worker->read_buffer_cur_size = 0;
790 worker->read_buffer_bytes_wanted = 16;
793 /* ensure we have a (big enough) buffer */
794 if (worker->read_buffer == NULL || worker->read_buffer_bytes_wanted > worker->read_buffer_allocated_size)
796 /* TODO: 4096 is randomly chosen; might want a better chosen default minimum */
797 worker->read_buffer_allocated_size = MAX (worker->read_buffer_bytes_wanted, 4096);
798 worker->read_buffer = g_realloc (worker->read_buffer, worker->read_buffer_allocated_size);
801 if (worker->socket == NULL)
802 g_input_stream_read_async (g_io_stream_get_input_stream (worker->stream),
803 worker->read_buffer + worker->read_buffer_cur_size,
804 worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size,
807 (GAsyncReadyCallback) _g_dbus_worker_do_read_cb,
808 _g_dbus_worker_ref (worker));
811 worker->read_ancillary_messages = NULL;
812 worker->read_num_ancillary_messages = 0;
813 _g_socket_read_with_control_messages (worker->socket,
814 worker->read_buffer + worker->read_buffer_cur_size,
815 worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size,
816 &worker->read_ancillary_messages,
817 &worker->read_num_ancillary_messages,
820 (GAsyncReadyCallback) _g_dbus_worker_do_read_cb,
821 _g_dbus_worker_ref (worker));
825 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
827 _g_dbus_worker_do_initial_read (gpointer data)
829 GDBusWorker *worker = data;
830 g_mutex_lock (&worker->read_lock);
831 _g_dbus_worker_do_read_unlocked (worker);
832 g_mutex_unlock (&worker->read_lock);
836 /* ---------------------------------------------------------------------------------------------------- */
838 struct _MessageToWriteData
841 GDBusMessage *message;
846 GSimpleAsyncResult *simple;
851 message_to_write_data_free (MessageToWriteData *data)
853 _g_dbus_worker_unref (data->worker);
855 g_object_unref (data->message);
860 /* ---------------------------------------------------------------------------------------------------- */
862 static void write_message_continue_writing (MessageToWriteData *data);
864 /* called in private thread shared by all GDBusConnection instances
866 * write-lock is not held on entry
867 * output_pending is true on entry
870 write_message_async_cb (GObject *source_object,
874 MessageToWriteData *data = user_data;
875 GSimpleAsyncResult *simple;
876 gssize bytes_written;
879 /* Note: we can't access data->simple after calling g_async_result_complete () because the
880 * callback can free @data and we're not completing in idle. So use a copy of the pointer.
882 simple = data->simple;
885 bytes_written = g_output_stream_write_finish (G_OUTPUT_STREAM (source_object),
888 if (bytes_written == -1)
890 g_simple_async_result_take_error (simple, error);
891 g_simple_async_result_complete (simple);
892 g_object_unref (simple);
895 g_assert (bytes_written > 0); /* zero is never returned */
897 write_message_print_transport_debug (bytes_written, data);
899 data->total_written += bytes_written;
900 g_assert (data->total_written <= data->blob_size);
901 if (data->total_written == data->blob_size)
903 g_simple_async_result_complete (simple);
904 g_object_unref (simple);
908 write_message_continue_writing (data);
914 /* called in private thread shared by all GDBusConnection instances
916 * write-lock is not held on entry
917 * output_pending is true on entry
920 on_socket_ready (GSocket *socket,
921 GIOCondition condition,
924 MessageToWriteData *data = user_data;
925 write_message_continue_writing (data);
926 return FALSE; /* remove source */
929 /* called in private thread shared by all GDBusConnection instances
931 * write-lock is not held on entry
932 * output_pending is true on entry
935 write_message_continue_writing (MessageToWriteData *data)
937 GOutputStream *ostream;
938 GSimpleAsyncResult *simple;
940 GUnixFDList *fd_list;
943 /* Note: we can't access data->simple after calling g_async_result_complete () because the
944 * callback can free @data and we're not completing in idle. So use a copy of the pointer.
946 simple = data->simple;
948 ostream = g_io_stream_get_output_stream (data->worker->stream);
950 fd_list = g_dbus_message_get_unix_fd_list (data->message);
953 g_assert (!g_output_stream_has_pending (ostream));
954 g_assert_cmpint (data->total_written, <, data->blob_size);
960 else if (G_IS_SOCKET_OUTPUT_STREAM (ostream) && data->total_written == 0)
962 GOutputVector vector;
963 GSocketControlMessage *control_message;
964 gssize bytes_written;
967 vector.buffer = data->blob;
968 vector.size = data->blob_size;
970 control_message = NULL;
971 if (fd_list != NULL && g_unix_fd_list_get_length (fd_list) > 0)
973 if (!(data->worker->capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING))
975 g_simple_async_result_set_error (simple,
978 "Tried sending a file descriptor but remote peer does not support this capability");
979 g_simple_async_result_complete (simple);
980 g_object_unref (simple);
983 control_message = g_unix_fd_message_new_with_fd_list (fd_list);
987 bytes_written = g_socket_send_message (data->worker->socket,
991 control_message != NULL ? &control_message : NULL,
992 control_message != NULL ? 1 : 0,
994 data->worker->cancellable,
996 if (control_message != NULL)
997 g_object_unref (control_message);
999 if (bytes_written == -1)
1001 /* Handle WOULD_BLOCK by waiting until there's room in the buffer */
1002 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1005 source = g_socket_create_source (data->worker->socket,
1006 G_IO_OUT | G_IO_HUP | G_IO_ERR,
1007 data->worker->cancellable);
1008 g_source_set_callback (source,
1009 (GSourceFunc) on_socket_ready,
1011 NULL); /* GDestroyNotify */
1012 g_source_attach (source, g_main_context_get_thread_default ());
1013 g_source_unref (source);
1014 g_error_free (error);
1017 g_simple_async_result_take_error (simple, error);
1018 g_simple_async_result_complete (simple);
1019 g_object_unref (simple);
1022 g_assert (bytes_written > 0); /* zero is never returned */
1024 write_message_print_transport_debug (bytes_written, data);
1026 data->total_written += bytes_written;
1027 g_assert (data->total_written <= data->blob_size);
1028 if (data->total_written == data->blob_size)
1030 g_simple_async_result_complete (simple);
1031 g_object_unref (simple);
1035 write_message_continue_writing (data);
1041 if (fd_list != NULL)
1043 g_simple_async_result_set_error (simple,
1046 "Tried sending a file descriptor on unsupported stream of type %s",
1047 g_type_name (G_TYPE_FROM_INSTANCE (ostream)));
1048 g_simple_async_result_complete (simple);
1049 g_object_unref (simple);
1054 g_output_stream_write_async (ostream,
1055 (const gchar *) data->blob + data->total_written,
1056 data->blob_size - data->total_written,
1058 data->worker->cancellable,
1059 write_message_async_cb,
1066 /* called in private thread shared by all GDBusConnection instances
1068 * write-lock is not held on entry
1069 * output_pending is true on entry
1072 write_message_async (GDBusWorker *worker,
1073 MessageToWriteData *data,
1074 GAsyncReadyCallback callback,
1077 data->simple = g_simple_async_result_new (NULL,
1080 write_message_async);
1081 data->total_written = 0;
1082 write_message_continue_writing (data);
1085 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
1087 write_message_finish (GAsyncResult *res,
1090 g_warn_if_fail (g_simple_async_result_get_source_tag (G_SIMPLE_ASYNC_RESULT (res)) == write_message_async);
1091 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error))
1096 /* ---------------------------------------------------------------------------------------------------- */
1098 static void maybe_write_next_message (GDBusWorker *worker);
1102 GDBusWorker *worker;
1107 flush_data_list_complete (const GList *flushers,
1108 const GError *error)
1112 for (l = flushers; l != NULL; l = l->next)
1114 FlushData *f = l->data;
1116 f->error = error != NULL ? g_error_copy (error) : NULL;
1118 g_mutex_lock (&f->mutex);
1119 g_cond_signal (&f->cond);
1120 g_mutex_unlock (&f->mutex);
1124 /* called in private thread shared by all GDBusConnection instances
1126 * write-lock is not held on entry
1127 * output_pending is true on entry
1130 ostream_flush_cb (GObject *source_object,
1134 FlushAsyncData *data = user_data;
1138 g_output_stream_flush_finish (G_OUTPUT_STREAM (source_object),
1144 if (G_UNLIKELY (_g_dbus_debug_transport ()))
1146 _g_dbus_debug_print_lock ();
1147 g_print ("========================================================================\n"
1148 "GDBus-debug:Transport:\n"
1149 " ---- FLUSHED stream of type %s\n",
1150 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream))));
1151 _g_dbus_debug_print_unlock ();
1155 g_assert (data->flushers != NULL);
1156 flush_data_list_complete (data->flushers, error);
1157 g_list_free (data->flushers);
1160 g_error_free (error);
1162 /* Make sure we tell folks that we don't have additional
1164 g_mutex_lock (&data->worker->write_lock);
1165 g_assert (data->worker->output_pending);
1166 data->worker->output_pending = FALSE;
1167 g_mutex_unlock (&data->worker->write_lock);
1169 /* OK, cool, finally kick off the next write */
1170 maybe_write_next_message (data->worker);
1172 _g_dbus_worker_unref (data->worker);
1176 /* called in private thread shared by all GDBusConnection instances
1178 * write-lock is not held on entry
1179 * output_pending is false on entry
1182 message_written (GDBusWorker *worker,
1183 MessageToWriteData *message_data)
1189 /* first log the fact that we wrote a message */
1190 if (G_UNLIKELY (_g_dbus_debug_message ()))
1193 _g_dbus_debug_print_lock ();
1194 g_print ("========================================================================\n"
1195 "GDBus-debug:Message:\n"
1196 " >>>> SENT D-Bus message (%" G_GSIZE_FORMAT " bytes)\n",
1197 message_data->blob_size);
1198 s = g_dbus_message_print (message_data->message, 2);
1201 if (G_UNLIKELY (_g_dbus_debug_payload ()))
1203 s = _g_dbus_hexdump (message_data->blob, message_data->blob_size, 2);
1204 g_print ("%s\n", s);
1207 _g_dbus_debug_print_unlock ();
1210 /* then first wake up pending flushes and, if needed, flush the stream */
1212 g_mutex_lock (&worker->write_lock);
1213 worker->write_num_messages_written += 1;
1214 for (l = worker->write_pending_flushes; l != NULL; l = ll)
1216 FlushData *f = l->data;
1219 if (f->number_to_wait_for == worker->write_num_messages_written)
1221 flushers = g_list_append (flushers, f);
1222 worker->write_pending_flushes = g_list_delete_link (worker->write_pending_flushes, l);
1225 if (flushers != NULL)
1227 g_assert (!worker->output_pending);
1228 worker->output_pending = TRUE;
1230 g_mutex_unlock (&worker->write_lock);
1232 if (flushers != NULL)
1234 FlushAsyncData *data;
1235 data = g_new0 (FlushAsyncData, 1);
1236 data->worker = _g_dbus_worker_ref (worker);
1237 data->flushers = flushers;
1238 /* flush the stream before writing the next message */
1239 g_output_stream_flush_async (g_io_stream_get_output_stream (worker->stream),
1241 worker->cancellable,
1247 /* kick off the next write! */
1248 maybe_write_next_message (worker);
1252 /* called in private thread shared by all GDBusConnection instances
1254 * write-lock is not held on entry
1255 * output_pending is true on entry
1258 write_message_cb (GObject *source_object,
1262 MessageToWriteData *data = user_data;
1265 g_mutex_lock (&data->worker->write_lock);
1266 g_assert (data->worker->output_pending);
1267 data->worker->output_pending = FALSE;
1268 g_mutex_unlock (&data->worker->write_lock);
1271 if (!write_message_finish (res, &error))
1274 _g_dbus_worker_emit_disconnected (data->worker, TRUE, error);
1275 g_error_free (error);
1278 /* this function will also kick of the next write (it might need to
1279 * flush so writing the next message might happen much later
1282 message_written (data->worker, data);
1284 message_to_write_data_free (data);
1287 /* called in private thread shared by all GDBusConnection instances
1289 * write-lock is not held on entry
1290 * output_pending is true on entry
1293 iostream_close_cb (GObject *source_object,
1297 GDBusWorker *worker = user_data;
1298 GError *error = NULL;
1299 GList *pending_close_attempts, *pending_flush_attempts;
1302 g_io_stream_close_finish (worker->stream, res, &error);
1304 g_mutex_lock (&worker->write_lock);
1306 pending_close_attempts = worker->pending_close_attempts;
1307 worker->pending_close_attempts = NULL;
1309 pending_flush_attempts = worker->write_pending_flushes;
1310 worker->write_pending_flushes = NULL;
1312 send_queue = worker->write_queue;
1313 worker->write_queue = g_queue_new ();
1315 g_assert (worker->output_pending);
1316 worker->output_pending = FALSE;
1318 g_mutex_unlock (&worker->write_lock);
1320 while (pending_close_attempts != NULL)
1322 CloseData *close_data = pending_close_attempts->data;
1324 pending_close_attempts = g_list_delete_link (pending_close_attempts,
1325 pending_close_attempts);
1327 if (close_data->result != NULL)
1330 g_simple_async_result_set_from_error (close_data->result, error);
1332 /* this must be in an idle because the result is likely to be
1333 * intended for another thread
1335 g_simple_async_result_complete_in_idle (close_data->result);
1338 close_data_free (close_data);
1341 g_clear_error (&error);
1343 /* all messages queued for sending are discarded */
1344 g_queue_foreach (send_queue, (GFunc) message_to_write_data_free, NULL);
1345 g_queue_free (send_queue);
1347 /* all queued flushes fail */
1348 error = g_error_new (G_IO_ERROR, G_IO_ERROR_CANCELLED,
1349 _("Operation was cancelled"));
1350 flush_data_list_complete (pending_flush_attempts, error);
1351 g_list_free (pending_flush_attempts);
1352 g_clear_error (&error);
1354 _g_dbus_worker_unref (worker);
1357 /* called in private thread shared by all GDBusConnection instances
1359 * write-lock is not held on entry
1360 * output_pending must be false on entry
1363 maybe_write_next_message (GDBusWorker *worker)
1365 MessageToWriteData *data;
1368 /* we mustn't try to write two things at once */
1369 g_assert (!worker->output_pending);
1371 g_mutex_lock (&worker->write_lock);
1373 /* if we want to close the connection, that takes precedence */
1374 if (worker->pending_close_attempts != NULL)
1376 worker->output_pending = TRUE;
1378 g_io_stream_close_async (worker->stream, G_PRIORITY_DEFAULT,
1379 NULL, iostream_close_cb,
1380 _g_dbus_worker_ref (worker));
1385 data = g_queue_pop_head (worker->write_queue);
1388 worker->output_pending = TRUE;
1391 g_mutex_unlock (&worker->write_lock);
1393 /* Note that write_lock is only used for protecting the @write_queue
1394 * and @output_pending fields of the GDBusWorker struct ... which we
1395 * need to modify from arbitrary threads in _g_dbus_worker_send_message().
1397 * Therefore, it's fine to drop it here when calling back into user
1398 * code and then writing the message out onto the GIOStream since this
1399 * function only runs on the worker thread.
1403 GDBusMessage *old_message;
1405 gsize new_blob_size;
1408 old_message = data->message;
1409 data->message = _g_dbus_worker_emit_message_about_to_be_sent (worker, data->message);
1410 if (data->message == old_message)
1412 /* filters had no effect - do nothing */
1414 else if (data->message == NULL)
1416 /* filters dropped message */
1417 g_mutex_lock (&worker->write_lock);
1418 worker->output_pending = FALSE;
1419 g_mutex_unlock (&worker->write_lock);
1420 message_to_write_data_free (data);
1425 /* filters altered the message -> reencode */
1427 new_blob = g_dbus_message_to_blob (data->message,
1429 worker->capabilities,
1431 if (new_blob == NULL)
1433 /* if filter make the GDBusMessage unencodeable, just complain on stderr and send
1434 * the old message instead
1436 g_warning ("Error encoding GDBusMessage with serial %d altered by filter function: %s",
1437 g_dbus_message_get_serial (data->message),
1439 g_error_free (error);
1443 g_free (data->blob);
1444 data->blob = (gchar *) new_blob;
1445 data->blob_size = new_blob_size;
1449 write_message_async (worker,
1456 /* called in private thread shared by all GDBusConnection instances
1458 * write-lock is not held on entry
1459 * output_pending may be true or false
1462 write_message_in_idle_cb (gpointer user_data)
1464 GDBusWorker *worker = user_data;
1466 /* Because this is the worker thread, we can read this struct member
1467 * without holding the lock: no other thread ever modifies it.
1469 if (!worker->output_pending)
1470 maybe_write_next_message (worker);
1476 * @write_data: (transfer full) (allow-none):
1477 * @close_data: (transfer full) (allow-none):
1479 * Can be called from any thread
1481 * write_lock is not held on entry
1482 * output_pending may be true or false
1485 schedule_write_in_worker_thread (GDBusWorker *worker,
1486 MessageToWriteData *write_data,
1487 CloseData *close_data)
1489 g_mutex_lock (&worker->write_lock);
1491 if (write_data != NULL)
1492 g_queue_push_tail (worker->write_queue, write_data);
1494 if (close_data != NULL)
1495 worker->pending_close_attempts = g_list_prepend (worker->pending_close_attempts,
1498 if (!worker->output_pending)
1500 GSource *idle_source;
1501 idle_source = g_idle_source_new ();
1502 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1503 g_source_set_callback (idle_source,
1504 write_message_in_idle_cb,
1505 _g_dbus_worker_ref (worker),
1506 (GDestroyNotify) _g_dbus_worker_unref);
1507 g_source_attach (idle_source, worker->shared_thread_data->context);
1508 g_source_unref (idle_source);
1511 g_mutex_unlock (&worker->write_lock);
1514 /* ---------------------------------------------------------------------------------------------------- */
1516 /* can be called from any thread - steals blob
1518 * write_lock is not held on entry
1519 * output_pending may be true or false
1522 _g_dbus_worker_send_message (GDBusWorker *worker,
1523 GDBusMessage *message,
1527 MessageToWriteData *data;
1529 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1530 g_return_if_fail (blob != NULL);
1531 g_return_if_fail (blob_len > 16);
1533 data = g_new0 (MessageToWriteData, 1);
1534 data->worker = _g_dbus_worker_ref (worker);
1535 data->message = g_object_ref (message);
1536 data->blob = blob; /* steal! */
1537 data->blob_size = blob_len;
1539 schedule_write_in_worker_thread (worker, data, NULL);
1542 /* ---------------------------------------------------------------------------------------------------- */
1545 _g_dbus_worker_new (GIOStream *stream,
1546 GDBusCapabilityFlags capabilities,
1547 gboolean initially_frozen,
1548 GDBusWorkerMessageReceivedCallback message_received_callback,
1549 GDBusWorkerMessageAboutToBeSentCallback message_about_to_be_sent_callback,
1550 GDBusWorkerDisconnectedCallback disconnected_callback,
1553 GDBusWorker *worker;
1554 GSource *idle_source;
1556 g_return_val_if_fail (G_IS_IO_STREAM (stream), NULL);
1557 g_return_val_if_fail (message_received_callback != NULL, NULL);
1558 g_return_val_if_fail (message_about_to_be_sent_callback != NULL, NULL);
1559 g_return_val_if_fail (disconnected_callback != NULL, NULL);
1561 worker = g_new0 (GDBusWorker, 1);
1562 worker->ref_count = 1;
1564 g_mutex_init (&worker->read_lock);
1565 worker->message_received_callback = message_received_callback;
1566 worker->message_about_to_be_sent_callback = message_about_to_be_sent_callback;
1567 worker->disconnected_callback = disconnected_callback;
1568 worker->user_data = user_data;
1569 worker->stream = g_object_ref (stream);
1570 worker->capabilities = capabilities;
1571 worker->cancellable = g_cancellable_new ();
1572 worker->output_pending = FALSE;
1574 worker->frozen = initially_frozen;
1575 worker->received_messages_while_frozen = g_queue_new ();
1577 g_mutex_init (&worker->write_lock);
1578 worker->write_queue = g_queue_new ();
1580 if (G_IS_SOCKET_CONNECTION (worker->stream))
1581 worker->socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream));
1583 worker->shared_thread_data = _g_dbus_shared_thread_ref ();
1586 idle_source = g_idle_source_new ();
1587 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1588 g_source_set_callback (idle_source,
1589 _g_dbus_worker_do_initial_read,
1590 _g_dbus_worker_ref (worker),
1591 (GDestroyNotify) _g_dbus_worker_unref);
1592 g_source_attach (idle_source, worker->shared_thread_data->context);
1593 g_source_unref (idle_source);
1598 /* ---------------------------------------------------------------------------------------------------- */
1600 /* can be called from any thread
1602 * write_lock is not held on entry
1603 * output_pending may be true or false
1606 _g_dbus_worker_close (GDBusWorker *worker,
1607 GCancellable *cancellable,
1608 GSimpleAsyncResult *result)
1610 CloseData *close_data;
1612 close_data = g_slice_new0 (CloseData);
1613 close_data->worker = _g_dbus_worker_ref (worker);
1614 close_data->cancellable =
1615 (cancellable == NULL ? NULL : g_object_ref (cancellable));
1616 close_data->result = (result == NULL ? NULL : g_object_ref (result));
1618 g_cancellable_cancel (worker->cancellable);
1619 schedule_write_in_worker_thread (worker, NULL, close_data);
1622 /* This can be called from any thread - frees worker. Note that
1623 * callbacks might still happen if called from another thread than the
1624 * worker - use your own synchronization primitive in the callbacks.
1626 * write_lock is not held on entry
1627 * output_pending may be true or false
1630 _g_dbus_worker_stop (GDBusWorker *worker)
1632 g_atomic_int_set (&worker->stopped, TRUE);
1634 /* Cancel any pending operations and schedule a close of the underlying I/O
1635 * stream in the worker thread
1637 _g_dbus_worker_close (worker, NULL, NULL);
1639 /* _g_dbus_worker_close holds a ref until after an idle in the the worker
1640 * thread has run, so we no longer need to unref in an idle like in
1643 _g_dbus_worker_unref (worker);
1646 /* ---------------------------------------------------------------------------------------------------- */
1648 /* can be called from any thread (except the worker thread) - blocks
1649 * calling thread until all queued outgoing messages are written and
1650 * the transport has been flushed
1652 * write_lock is not held on entry
1653 * output_pending may be true or false
1656 _g_dbus_worker_flush_sync (GDBusWorker *worker,
1657 GCancellable *cancellable,
1666 /* if the queue is empty, there's nothing to wait for */
1667 g_mutex_lock (&worker->write_lock);
1668 if (g_queue_get_length (worker->write_queue) > 0)
1670 data = g_new0 (FlushData, 1);
1671 g_mutex_init (&data->mutex);
1672 g_cond_init (&data->cond);
1673 data->number_to_wait_for = worker->write_num_messages_written + g_queue_get_length (worker->write_queue);
1674 g_mutex_lock (&data->mutex);
1675 worker->write_pending_flushes = g_list_prepend (worker->write_pending_flushes, data);
1677 g_mutex_unlock (&worker->write_lock);
1681 g_cond_wait (&data->cond, &data->mutex);
1682 g_mutex_unlock (&data->mutex);
1684 /* note:the element is removed from worker->write_pending_flushes in flush_cb() above */
1685 g_cond_clear (&data->cond);
1686 g_mutex_clear (&data->mutex);
1687 if (data->error != NULL)
1690 g_propagate_error (error, data->error);
1698 /* ---------------------------------------------------------------------------------------------------- */
1700 #define G_DBUS_DEBUG_AUTHENTICATION (1<<0)
1701 #define G_DBUS_DEBUG_TRANSPORT (1<<1)
1702 #define G_DBUS_DEBUG_MESSAGE (1<<2)
1703 #define G_DBUS_DEBUG_PAYLOAD (1<<3)
1704 #define G_DBUS_DEBUG_CALL (1<<4)
1705 #define G_DBUS_DEBUG_SIGNAL (1<<5)
1706 #define G_DBUS_DEBUG_INCOMING (1<<6)
1707 #define G_DBUS_DEBUG_RETURN (1<<7)
1708 #define G_DBUS_DEBUG_EMISSION (1<<8)
1709 #define G_DBUS_DEBUG_ADDRESS (1<<9)
1711 static gint _gdbus_debug_flags = 0;
1714 _g_dbus_debug_authentication (void)
1716 _g_dbus_initialize ();
1717 return (_gdbus_debug_flags & G_DBUS_DEBUG_AUTHENTICATION) != 0;
1721 _g_dbus_debug_transport (void)
1723 _g_dbus_initialize ();
1724 return (_gdbus_debug_flags & G_DBUS_DEBUG_TRANSPORT) != 0;
1728 _g_dbus_debug_message (void)
1730 _g_dbus_initialize ();
1731 return (_gdbus_debug_flags & G_DBUS_DEBUG_MESSAGE) != 0;
1735 _g_dbus_debug_payload (void)
1737 _g_dbus_initialize ();
1738 return (_gdbus_debug_flags & G_DBUS_DEBUG_PAYLOAD) != 0;
1742 _g_dbus_debug_call (void)
1744 _g_dbus_initialize ();
1745 return (_gdbus_debug_flags & G_DBUS_DEBUG_CALL) != 0;
1749 _g_dbus_debug_signal (void)
1751 _g_dbus_initialize ();
1752 return (_gdbus_debug_flags & G_DBUS_DEBUG_SIGNAL) != 0;
1756 _g_dbus_debug_incoming (void)
1758 _g_dbus_initialize ();
1759 return (_gdbus_debug_flags & G_DBUS_DEBUG_INCOMING) != 0;
1763 _g_dbus_debug_return (void)
1765 _g_dbus_initialize ();
1766 return (_gdbus_debug_flags & G_DBUS_DEBUG_RETURN) != 0;
1770 _g_dbus_debug_emission (void)
1772 _g_dbus_initialize ();
1773 return (_gdbus_debug_flags & G_DBUS_DEBUG_EMISSION) != 0;
1777 _g_dbus_debug_address (void)
1779 _g_dbus_initialize ();
1780 return (_gdbus_debug_flags & G_DBUS_DEBUG_ADDRESS) != 0;
1783 G_LOCK_DEFINE_STATIC (print_lock);
1786 _g_dbus_debug_print_lock (void)
1788 G_LOCK (print_lock);
1792 _g_dbus_debug_print_unlock (void)
1794 G_UNLOCK (print_lock);
1798 * _g_dbus_initialize:
1800 * Does various one-time init things such as
1802 * - registering the G_DBUS_ERROR error domain
1803 * - parses the G_DBUS_DEBUG environment variable
1806 _g_dbus_initialize (void)
1808 static volatile gsize initialized = 0;
1810 if (g_once_init_enter (&initialized))
1812 volatile GQuark g_dbus_error_domain;
1815 g_dbus_error_domain = G_DBUS_ERROR;
1816 (g_dbus_error_domain); /* To avoid -Wunused-but-set-variable */
1818 debug = g_getenv ("G_DBUS_DEBUG");
1821 const GDebugKey keys[] = {
1822 { "authentication", G_DBUS_DEBUG_AUTHENTICATION },
1823 { "transport", G_DBUS_DEBUG_TRANSPORT },
1824 { "message", G_DBUS_DEBUG_MESSAGE },
1825 { "payload", G_DBUS_DEBUG_PAYLOAD },
1826 { "call", G_DBUS_DEBUG_CALL },
1827 { "signal", G_DBUS_DEBUG_SIGNAL },
1828 { "incoming", G_DBUS_DEBUG_INCOMING },
1829 { "return", G_DBUS_DEBUG_RETURN },
1830 { "emission", G_DBUS_DEBUG_EMISSION },
1831 { "address", G_DBUS_DEBUG_ADDRESS }
1834 _gdbus_debug_flags = g_parse_debug_string (debug, keys, G_N_ELEMENTS (keys));
1835 if (_gdbus_debug_flags & G_DBUS_DEBUG_PAYLOAD)
1836 _gdbus_debug_flags |= G_DBUS_DEBUG_MESSAGE;
1839 g_once_init_leave (&initialized, 1);
1843 /* ---------------------------------------------------------------------------------------------------- */
1846 _g_dbus_compute_complete_signature (GDBusArgInfo **args)
1848 const GVariantType *arg_types[256];
1852 for (n = 0; args[n] != NULL; n++)
1854 /* DBus places a hard limit of 255 on signature length.
1855 * therefore number of args must be less than 256.
1859 arg_types[n] = G_VARIANT_TYPE (args[n]->signature);
1861 if G_UNLIKELY (arg_types[n] == NULL)
1867 return g_variant_type_new_tuple (arg_types, n);
1870 /* ---------------------------------------------------------------------------------------------------- */
1874 extern BOOL WINAPI ConvertSidToStringSidA (PSID Sid, LPSTR *StringSid);
1877 _g_dbus_win32_get_user_sid (void)
1881 DWORD token_information_len;
1888 h = INVALID_HANDLE_VALUE;
1890 if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &h))
1892 g_warning ("OpenProcessToken failed with error code %d", (gint) GetLastError ());
1896 /* Get length of buffer */
1897 token_information_len = 0;
1898 if (!GetTokenInformation (h, TokenUser, NULL, 0, &token_information_len))
1900 if (GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1902 g_warning ("GetTokenInformation() failed with error code %d", (gint) GetLastError ());
1906 user = g_malloc (token_information_len);
1907 if (!GetTokenInformation (h, TokenUser, user, token_information_len, &token_information_len))
1909 g_warning ("GetTokenInformation() failed with error code %d", (gint) GetLastError ());
1913 psid = user->User.Sid;
1914 if (!IsValidSid (psid))
1916 g_warning ("Invalid SID");
1920 if (!ConvertSidToStringSidA (psid, &sid))
1922 g_warning ("Invalid SID");
1926 ret = g_strdup (sid);
1931 if (h != INVALID_HANDLE_VALUE)
1937 /* ---------------------------------------------------------------------------------------------------- */
1940 _g_dbus_get_machine_id (GError **error)
1943 /* TODO: use PACKAGE_LOCALSTATEDIR ? */
1945 if (!g_file_get_contents ("/var/lib/dbus/machine-id",
1950 g_prefix_error (error, _("Unable to load /var/lib/dbus/machine-id: "));
1954 /* TODO: validate value */
1960 /* ---------------------------------------------------------------------------------------------------- */
1963 _g_dbus_enum_to_string (GType enum_type, gint value)
1967 GEnumValue *enum_value;
1969 klass = g_type_class_ref (enum_type);
1970 enum_value = g_enum_get_value (klass, value);
1971 if (enum_value != NULL)
1972 ret = g_strdup (enum_value->value_nick);
1974 ret = g_strdup_printf ("unknown (value %d)", value);
1975 g_type_class_unref (klass);
1979 /* ---------------------------------------------------------------------------------------------------- */
1982 write_message_print_transport_debug (gssize bytes_written,
1983 MessageToWriteData *data)
1985 if (G_LIKELY (!_g_dbus_debug_transport ()))
1988 _g_dbus_debug_print_lock ();
1989 g_print ("========================================================================\n"
1990 "GDBus-debug:Transport:\n"
1991 " >>>> WROTE %" G_GSIZE_FORMAT " bytes of message with serial %d and\n"
1992 " size %" G_GSIZE_FORMAT " from offset %" G_GSIZE_FORMAT " on a %s\n",
1994 g_dbus_message_get_serial (data->message),
1996 data->total_written,
1997 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream))));
1998 _g_dbus_debug_print_unlock ();
2003 /* ---------------------------------------------------------------------------------------------------- */
2006 read_message_print_transport_debug (gssize bytes_read,
2007 GDBusWorker *worker)
2011 gint32 message_length;
2013 if (G_LIKELY (!_g_dbus_debug_transport ()))
2016 size = bytes_read + worker->read_buffer_cur_size;
2020 message_length = g_dbus_message_bytes_needed ((guchar *) worker->read_buffer, size, NULL);
2023 switch (worker->read_buffer[0])
2027 serial = GUINT32_FROM_LE (((guint32 *) worker->read_buffer)[2]);
2031 serial = GUINT32_FROM_BE (((guint32 *) worker->read_buffer)[2]);
2034 /* an error will be set elsewhere if this happens */
2039 _g_dbus_debug_print_lock ();
2040 g_print ("========================================================================\n"
2041 "GDBus-debug:Transport:\n"
2042 " <<<< READ %" G_GSIZE_FORMAT " bytes of message with serial %d and\n"
2043 " size %d to offset %" G_GSIZE_FORMAT " from a %s\n",
2047 worker->read_buffer_cur_size,
2048 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_input_stream (worker->stream))));
2049 _g_dbus_debug_print_unlock ();
2054 /* ---------------------------------------------------------------------------------------------------- */
2057 _g_signal_accumulator_false_handled (GSignalInvocationHint *ihint,
2058 GValue *return_accu,
2059 const GValue *handler_return,
2062 gboolean continue_emission;
2063 gboolean signal_return;
2065 signal_return = g_value_get_boolean (handler_return);
2066 g_value_set_boolean (return_accu, signal_return);
2067 continue_emission = signal_return;
2069 return continue_emission;