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;
345 /* TODO: frozen (e.g. G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING) currently
346 * only affects messages received from the other peer (since GDBusServer is the
347 * only user) - we might want it to affect messages sent to the other peer too?
350 GDBusCapabilityFlags capabilities;
351 GQueue *received_messages_while_frozen;
354 GCancellable *cancellable;
355 GDBusWorkerMessageReceivedCallback message_received_callback;
356 GDBusWorkerMessageAboutToBeSentCallback message_about_to_be_sent_callback;
357 GDBusWorkerDisconnectedCallback disconnected_callback;
360 /* if not NULL, stream is GSocketConnection */
363 /* used for reading */
366 gsize read_buffer_allocated_size;
367 gsize read_buffer_cur_size;
368 gsize read_buffer_bytes_wanted;
369 GUnixFDList *read_fd_list;
370 GSocketControlMessage **read_ancillary_messages;
371 gint read_num_ancillary_messages;
373 /* TRUE if an async write, flush or close is pending.
374 * Only the worker thread may change its value, and only with the write_lock.
375 * Other threads may read its value when holding the write_lock.
376 * The worker thread may read its value at any time.
378 gboolean output_pending;
379 /* used for writing */
381 /* queue of MessageToWriteData, protected by write_lock */
383 /* protected by write_lock */
384 guint64 write_num_messages_written;
385 /* list of FlushData, protected by write_lock */
386 GList *write_pending_flushes;
387 /* list of CloseData, protected by write_lock */
388 GList *pending_close_attempts;
391 static void _g_dbus_worker_unref (GDBusWorker *worker);
393 /* ---------------------------------------------------------------------------------------------------- */
399 guint64 number_to_wait_for;
403 struct _MessageToWriteData ;
404 typedef struct _MessageToWriteData MessageToWriteData;
406 static void message_to_write_data_free (MessageToWriteData *data);
408 static void read_message_print_transport_debug (gssize bytes_read,
409 GDBusWorker *worker);
411 static void write_message_print_transport_debug (gssize bytes_written,
412 MessageToWriteData *data);
416 GCancellable *cancellable;
417 GSimpleAsyncResult *result;
420 static void close_data_free (CloseData *close_data)
422 if (close_data->cancellable != NULL)
423 g_object_unref (close_data->cancellable);
425 if (close_data->result != NULL)
426 g_object_unref (close_data->result);
428 _g_dbus_worker_unref (close_data->worker);
429 g_slice_free (CloseData, close_data);
432 /* ---------------------------------------------------------------------------------------------------- */
435 _g_dbus_worker_ref (GDBusWorker *worker)
437 g_atomic_int_inc (&worker->ref_count);
442 _g_dbus_worker_unref (GDBusWorker *worker)
444 if (g_atomic_int_dec_and_test (&worker->ref_count))
446 g_assert (worker->write_pending_flushes == NULL);
448 _g_dbus_shared_thread_unref (worker->shared_thread_data);
450 g_object_unref (worker->stream);
452 g_mutex_free (worker->read_lock);
453 g_object_unref (worker->cancellable);
454 if (worker->read_fd_list != NULL)
455 g_object_unref (worker->read_fd_list);
457 g_queue_foreach (worker->received_messages_while_frozen, (GFunc) g_object_unref, NULL);
458 g_queue_free (worker->received_messages_while_frozen);
460 g_mutex_free (worker->write_lock);
461 g_queue_foreach (worker->write_queue, (GFunc) message_to_write_data_free, NULL);
462 g_queue_free (worker->write_queue);
464 g_free (worker->read_buffer);
471 _g_dbus_worker_emit_disconnected (GDBusWorker *worker,
472 gboolean remote_peer_vanished,
475 if (!worker->stopped)
476 worker->disconnected_callback (worker, remote_peer_vanished, error, worker->user_data);
480 _g_dbus_worker_emit_message_received (GDBusWorker *worker,
481 GDBusMessage *message)
483 if (!worker->stopped)
484 worker->message_received_callback (worker, message, worker->user_data);
487 static GDBusMessage *
488 _g_dbus_worker_emit_message_about_to_be_sent (GDBusWorker *worker,
489 GDBusMessage *message)
492 if (!worker->stopped)
493 ret = worker->message_about_to_be_sent_callback (worker, message, worker->user_data);
499 /* can only be called from private thread with read-lock held - takes ownership of @message */
501 _g_dbus_worker_queue_or_deliver_received_message (GDBusWorker *worker,
502 GDBusMessage *message)
504 if (worker->frozen || g_queue_get_length (worker->received_messages_while_frozen) > 0)
507 g_queue_push_tail (worker->received_messages_while_frozen, message);
511 /* not frozen, nor anything in queue */
512 _g_dbus_worker_emit_message_received (worker, message);
513 g_object_unref (message);
517 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
519 unfreeze_in_idle_cb (gpointer user_data)
521 GDBusWorker *worker = user_data;
522 GDBusMessage *message;
524 g_mutex_lock (worker->read_lock);
527 while ((message = g_queue_pop_head (worker->received_messages_while_frozen)) != NULL)
529 _g_dbus_worker_emit_message_received (worker, message);
530 g_object_unref (message);
532 worker->frozen = FALSE;
536 g_assert (g_queue_get_length (worker->received_messages_while_frozen) == 0);
538 g_mutex_unlock (worker->read_lock);
542 /* can be called from any thread */
544 _g_dbus_worker_unfreeze (GDBusWorker *worker)
546 GSource *idle_source;
547 idle_source = g_idle_source_new ();
548 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
549 g_source_set_callback (idle_source,
551 _g_dbus_worker_ref (worker),
552 (GDestroyNotify) _g_dbus_worker_unref);
553 g_source_attach (idle_source, worker->shared_thread_data->context);
554 g_source_unref (idle_source);
557 /* ---------------------------------------------------------------------------------------------------- */
559 static void _g_dbus_worker_do_read_unlocked (GDBusWorker *worker);
561 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
563 _g_dbus_worker_do_read_cb (GInputStream *input_stream,
567 GDBusWorker *worker = user_data;
571 g_mutex_lock (worker->read_lock);
573 /* If already stopped, don't even process the reply */
578 if (worker->socket == NULL)
579 bytes_read = g_input_stream_read_finish (g_io_stream_get_input_stream (worker->stream),
583 bytes_read = _g_socket_read_with_control_messages_finish (worker->socket,
586 if (worker->read_num_ancillary_messages > 0)
589 for (n = 0; n < worker->read_num_ancillary_messages; n++)
591 GSocketControlMessage *control_message = G_SOCKET_CONTROL_MESSAGE (worker->read_ancillary_messages[n]);
597 else if (G_IS_UNIX_FD_MESSAGE (control_message))
599 GUnixFDMessage *fd_message;
603 fd_message = G_UNIX_FD_MESSAGE (control_message);
604 fds = g_unix_fd_message_steal_fds (fd_message, &num_fds);
605 if (worker->read_fd_list == NULL)
607 worker->read_fd_list = g_unix_fd_list_new_from_array (fds, num_fds);
612 for (n = 0; n < num_fds; n++)
614 /* TODO: really want a append_steal() */
615 g_unix_fd_list_append (worker->read_fd_list, fds[n], NULL);
621 else if (G_IS_UNIX_CREDENTIALS_MESSAGE (control_message))
633 "Unexpected ancillary message of type %s received from peer",
634 g_type_name (G_TYPE_FROM_INSTANCE (control_message)));
635 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
636 g_error_free (error);
637 g_object_unref (control_message);
639 while (n < worker->read_num_ancillary_messages)
640 g_object_unref (worker->read_ancillary_messages[n++]);
641 g_free (worker->read_ancillary_messages);
645 g_object_unref (control_message);
647 g_free (worker->read_ancillary_messages);
650 if (bytes_read == -1)
652 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
653 g_error_free (error);
658 g_debug ("read %d bytes (is_closed=%d blocking=%d condition=0x%02x) stream %p, %p",
660 g_socket_is_closed (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream))),
661 g_socket_get_blocking (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream))),
662 g_socket_condition_check (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream)),
663 G_IO_IN | G_IO_OUT | G_IO_HUP),
668 /* TODO: hmm, hmm... */
674 "Underlying GIOStream returned 0 bytes on an async read");
675 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
676 g_error_free (error);
680 read_message_print_transport_debug (bytes_read, worker);
682 worker->read_buffer_cur_size += bytes_read;
683 if (worker->read_buffer_bytes_wanted == worker->read_buffer_cur_size)
685 /* OK, got what we asked for! */
686 if (worker->read_buffer_bytes_wanted == 16)
689 /* OK, got the header - determine how many more bytes are needed */
691 message_len = g_dbus_message_bytes_needed ((guchar *) worker->read_buffer,
694 if (message_len == -1)
696 g_warning ("_g_dbus_worker_do_read_cb: error determing bytes needed: %s", error->message);
697 _g_dbus_worker_emit_disconnected (worker, FALSE, error);
698 g_error_free (error);
702 worker->read_buffer_bytes_wanted = message_len;
703 _g_dbus_worker_do_read_unlocked (worker);
707 GDBusMessage *message;
710 /* TODO: use connection->priv->auth to decode the message */
712 message = g_dbus_message_new_from_blob ((guchar *) worker->read_buffer,
713 worker->read_buffer_cur_size,
714 worker->capabilities,
719 s = _g_dbus_hexdump (worker->read_buffer, worker->read_buffer_cur_size, 2);
720 g_warning ("Error decoding D-Bus message of %" G_GSIZE_FORMAT " bytes\n"
722 "The payload is as follows:\n"
724 worker->read_buffer_cur_size,
728 _g_dbus_worker_emit_disconnected (worker, FALSE, error);
729 g_error_free (error);
734 if (worker->read_fd_list != NULL)
736 g_dbus_message_set_unix_fd_list (message, worker->read_fd_list);
737 g_object_unref (worker->read_fd_list);
738 worker->read_fd_list = NULL;
742 if (G_UNLIKELY (_g_dbus_debug_message ()))
745 _g_dbus_debug_print_lock ();
746 g_print ("========================================================================\n"
747 "GDBus-debug:Message:\n"
748 " <<<< RECEIVED D-Bus message (%" G_GSIZE_FORMAT " bytes)\n",
749 worker->read_buffer_cur_size);
750 s = g_dbus_message_print (message, 2);
753 if (G_UNLIKELY (_g_dbus_debug_payload ()))
755 s = _g_dbus_hexdump (worker->read_buffer, worker->read_buffer_cur_size, 2);
759 _g_dbus_debug_print_unlock ();
762 /* yay, got a message, go deliver it */
763 _g_dbus_worker_queue_or_deliver_received_message (worker, message);
765 /* start reading another message! */
766 worker->read_buffer_bytes_wanted = 0;
767 worker->read_buffer_cur_size = 0;
768 _g_dbus_worker_do_read_unlocked (worker);
773 /* didn't get all the bytes we requested - so repeat the request... */
774 _g_dbus_worker_do_read_unlocked (worker);
778 g_mutex_unlock (worker->read_lock);
780 /* gives up the reference acquired when calling g_input_stream_read_async() */
781 _g_dbus_worker_unref (worker);
784 /* called in private thread shared by all GDBusConnection instances (with read-lock held) */
786 _g_dbus_worker_do_read_unlocked (GDBusWorker *worker)
788 /* if bytes_wanted is zero, it means start reading a message */
789 if (worker->read_buffer_bytes_wanted == 0)
791 worker->read_buffer_cur_size = 0;
792 worker->read_buffer_bytes_wanted = 16;
795 /* ensure we have a (big enough) buffer */
796 if (worker->read_buffer == NULL || worker->read_buffer_bytes_wanted > worker->read_buffer_allocated_size)
798 /* TODO: 4096 is randomly chosen; might want a better chosen default minimum */
799 worker->read_buffer_allocated_size = MAX (worker->read_buffer_bytes_wanted, 4096);
800 worker->read_buffer = g_realloc (worker->read_buffer, worker->read_buffer_allocated_size);
803 if (worker->socket == NULL)
804 g_input_stream_read_async (g_io_stream_get_input_stream (worker->stream),
805 worker->read_buffer + worker->read_buffer_cur_size,
806 worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size,
809 (GAsyncReadyCallback) _g_dbus_worker_do_read_cb,
810 _g_dbus_worker_ref (worker));
813 worker->read_ancillary_messages = NULL;
814 worker->read_num_ancillary_messages = 0;
815 _g_socket_read_with_control_messages (worker->socket,
816 worker->read_buffer + worker->read_buffer_cur_size,
817 worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size,
818 &worker->read_ancillary_messages,
819 &worker->read_num_ancillary_messages,
822 (GAsyncReadyCallback) _g_dbus_worker_do_read_cb,
823 _g_dbus_worker_ref (worker));
827 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
829 _g_dbus_worker_do_initial_read (gpointer data)
831 GDBusWorker *worker = data;
832 g_mutex_lock (worker->read_lock);
833 _g_dbus_worker_do_read_unlocked (worker);
834 g_mutex_unlock (worker->read_lock);
838 /* ---------------------------------------------------------------------------------------------------- */
840 struct _MessageToWriteData
843 GDBusMessage *message;
848 GSimpleAsyncResult *simple;
853 message_to_write_data_free (MessageToWriteData *data)
855 _g_dbus_worker_unref (data->worker);
857 g_object_unref (data->message);
862 /* ---------------------------------------------------------------------------------------------------- */
864 static void write_message_continue_writing (MessageToWriteData *data);
866 /* called in private thread shared by all GDBusConnection instances
868 * write-lock is not held on entry
869 * output_pending is true on entry
872 write_message_async_cb (GObject *source_object,
876 MessageToWriteData *data = user_data;
877 GSimpleAsyncResult *simple;
878 gssize bytes_written;
881 /* Note: we can't access data->simple after calling g_async_result_complete () because the
882 * callback can free @data and we're not completing in idle. So use a copy of the pointer.
884 simple = data->simple;
887 bytes_written = g_output_stream_write_finish (G_OUTPUT_STREAM (source_object),
890 if (bytes_written == -1)
892 g_simple_async_result_take_error (simple, error);
893 g_simple_async_result_complete (simple);
894 g_object_unref (simple);
897 g_assert (bytes_written > 0); /* zero is never returned */
899 write_message_print_transport_debug (bytes_written, data);
901 data->total_written += bytes_written;
902 g_assert (data->total_written <= data->blob_size);
903 if (data->total_written == data->blob_size)
905 g_simple_async_result_complete (simple);
906 g_object_unref (simple);
910 write_message_continue_writing (data);
916 /* called in private thread shared by all GDBusConnection instances
918 * write-lock is not held on entry
919 * output_pending is true on entry
922 on_socket_ready (GSocket *socket,
923 GIOCondition condition,
926 MessageToWriteData *data = user_data;
927 write_message_continue_writing (data);
928 return FALSE; /* remove source */
931 /* called in private thread shared by all GDBusConnection instances
933 * write-lock is not held on entry
934 * output_pending is true on entry
937 write_message_continue_writing (MessageToWriteData *data)
939 GOutputStream *ostream;
940 GSimpleAsyncResult *simple;
942 GUnixFDList *fd_list;
945 /* Note: we can't access data->simple after calling g_async_result_complete () because the
946 * callback can free @data and we're not completing in idle. So use a copy of the pointer.
948 simple = data->simple;
950 ostream = g_io_stream_get_output_stream (data->worker->stream);
952 fd_list = g_dbus_message_get_unix_fd_list (data->message);
955 g_assert (!g_output_stream_has_pending (ostream));
956 g_assert_cmpint (data->total_written, <, data->blob_size);
962 else if (G_IS_SOCKET_OUTPUT_STREAM (ostream) && data->total_written == 0)
964 GOutputVector vector;
965 GSocketControlMessage *control_message;
966 gssize bytes_written;
969 vector.buffer = data->blob;
970 vector.size = data->blob_size;
972 control_message = NULL;
973 if (fd_list != NULL && g_unix_fd_list_get_length (fd_list) > 0)
975 if (!(data->worker->capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING))
977 g_simple_async_result_set_error (simple,
980 "Tried sending a file descriptor but remote peer does not support this capability");
981 g_simple_async_result_complete (simple);
982 g_object_unref (simple);
985 control_message = g_unix_fd_message_new_with_fd_list (fd_list);
989 bytes_written = g_socket_send_message (data->worker->socket,
993 control_message != NULL ? &control_message : NULL,
994 control_message != NULL ? 1 : 0,
996 data->worker->cancellable,
998 if (control_message != NULL)
999 g_object_unref (control_message);
1001 if (bytes_written == -1)
1003 /* Handle WOULD_BLOCK by waiting until there's room in the buffer */
1004 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1007 source = g_socket_create_source (data->worker->socket,
1008 G_IO_OUT | G_IO_HUP | G_IO_ERR,
1009 data->worker->cancellable);
1010 g_source_set_callback (source,
1011 (GSourceFunc) on_socket_ready,
1013 NULL); /* GDestroyNotify */
1014 g_source_attach (source, g_main_context_get_thread_default ());
1015 g_source_unref (source);
1016 g_error_free (error);
1019 g_simple_async_result_take_error (simple, error);
1020 g_simple_async_result_complete (simple);
1021 g_object_unref (simple);
1024 g_assert (bytes_written > 0); /* zero is never returned */
1026 write_message_print_transport_debug (bytes_written, data);
1028 data->total_written += bytes_written;
1029 g_assert (data->total_written <= data->blob_size);
1030 if (data->total_written == data->blob_size)
1032 g_simple_async_result_complete (simple);
1033 g_object_unref (simple);
1037 write_message_continue_writing (data);
1043 if (fd_list != NULL)
1045 g_simple_async_result_set_error (simple,
1048 "Tried sending a file descriptor on unsupported stream of type %s",
1049 g_type_name (G_TYPE_FROM_INSTANCE (ostream)));
1050 g_simple_async_result_complete (simple);
1051 g_object_unref (simple);
1056 g_output_stream_write_async (ostream,
1057 (const gchar *) data->blob + data->total_written,
1058 data->blob_size - data->total_written,
1060 data->worker->cancellable,
1061 write_message_async_cb,
1068 /* called in private thread shared by all GDBusConnection instances
1070 * write-lock is not held on entry
1071 * output_pending is true on entry
1074 write_message_async (GDBusWorker *worker,
1075 MessageToWriteData *data,
1076 GAsyncReadyCallback callback,
1079 data->simple = g_simple_async_result_new (NULL,
1082 write_message_async);
1083 data->total_written = 0;
1084 write_message_continue_writing (data);
1087 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
1089 write_message_finish (GAsyncResult *res,
1092 g_warn_if_fail (g_simple_async_result_get_source_tag (G_SIMPLE_ASYNC_RESULT (res)) == write_message_async);
1093 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error))
1098 /* ---------------------------------------------------------------------------------------------------- */
1100 static void maybe_write_next_message (GDBusWorker *worker);
1104 GDBusWorker *worker;
1109 flush_data_list_complete (const GList *flushers,
1110 const GError *error)
1114 for (l = flushers; l != NULL; l = l->next)
1116 FlushData *f = l->data;
1118 f->error = error != NULL ? g_error_copy (error) : NULL;
1120 g_mutex_lock (f->mutex);
1121 g_cond_signal (f->cond);
1122 g_mutex_unlock (f->mutex);
1126 /* called in private thread shared by all GDBusConnection instances
1128 * write-lock is not held on entry
1129 * output_pending is true on entry
1132 ostream_flush_cb (GObject *source_object,
1136 FlushAsyncData *data = user_data;
1140 g_output_stream_flush_finish (G_OUTPUT_STREAM (source_object),
1146 if (G_UNLIKELY (_g_dbus_debug_transport ()))
1148 _g_dbus_debug_print_lock ();
1149 g_print ("========================================================================\n"
1150 "GDBus-debug:Transport:\n"
1151 " ---- FLUSHED stream of type %s\n",
1152 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream))));
1153 _g_dbus_debug_print_unlock ();
1157 g_assert (data->flushers != NULL);
1158 flush_data_list_complete (data->flushers, error);
1159 g_list_free (data->flushers);
1162 g_error_free (error);
1164 /* Make sure we tell folks that we don't have additional
1166 g_mutex_lock (data->worker->write_lock);
1167 g_assert (data->worker->output_pending);
1168 data->worker->output_pending = FALSE;
1169 g_mutex_unlock (data->worker->write_lock);
1171 /* OK, cool, finally kick off the next write */
1172 maybe_write_next_message (data->worker);
1174 _g_dbus_worker_unref (data->worker);
1178 /* called in private thread shared by all GDBusConnection instances
1180 * write-lock is not held on entry
1181 * output_pending is false on entry
1184 message_written (GDBusWorker *worker,
1185 MessageToWriteData *message_data)
1191 /* first log the fact that we wrote a message */
1192 if (G_UNLIKELY (_g_dbus_debug_message ()))
1195 _g_dbus_debug_print_lock ();
1196 g_print ("========================================================================\n"
1197 "GDBus-debug:Message:\n"
1198 " >>>> SENT D-Bus message (%" G_GSIZE_FORMAT " bytes)\n",
1199 message_data->blob_size);
1200 s = g_dbus_message_print (message_data->message, 2);
1203 if (G_UNLIKELY (_g_dbus_debug_payload ()))
1205 s = _g_dbus_hexdump (message_data->blob, message_data->blob_size, 2);
1206 g_print ("%s\n", s);
1209 _g_dbus_debug_print_unlock ();
1212 /* then first wake up pending flushes and, if needed, flush the stream */
1214 g_mutex_lock (worker->write_lock);
1215 worker->write_num_messages_written += 1;
1216 for (l = worker->write_pending_flushes; l != NULL; l = ll)
1218 FlushData *f = l->data;
1221 if (f->number_to_wait_for == worker->write_num_messages_written)
1223 flushers = g_list_append (flushers, f);
1224 worker->write_pending_flushes = g_list_delete_link (worker->write_pending_flushes, l);
1227 if (flushers != NULL)
1229 g_assert (!worker->output_pending);
1230 worker->output_pending = TRUE;
1232 g_mutex_unlock (worker->write_lock);
1234 if (flushers != NULL)
1236 FlushAsyncData *data;
1237 data = g_new0 (FlushAsyncData, 1);
1238 data->worker = _g_dbus_worker_ref (worker);
1239 data->flushers = flushers;
1240 /* flush the stream before writing the next message */
1241 g_output_stream_flush_async (g_io_stream_get_output_stream (worker->stream),
1243 worker->cancellable,
1249 /* kick off the next write! */
1250 maybe_write_next_message (worker);
1254 /* called in private thread shared by all GDBusConnection instances
1256 * write-lock is not held on entry
1257 * output_pending is true on entry
1260 write_message_cb (GObject *source_object,
1264 MessageToWriteData *data = user_data;
1267 g_mutex_lock (data->worker->write_lock);
1268 g_assert (data->worker->output_pending);
1269 data->worker->output_pending = FALSE;
1270 g_mutex_unlock (data->worker->write_lock);
1273 if (!write_message_finish (res, &error))
1276 _g_dbus_worker_emit_disconnected (data->worker, TRUE, error);
1277 g_error_free (error);
1280 /* this function will also kick of the next write (it might need to
1281 * flush so writing the next message might happen much later
1284 message_written (data->worker, data);
1286 message_to_write_data_free (data);
1289 /* called in private thread shared by all GDBusConnection instances
1291 * write-lock is not held on entry
1292 * output_pending is true on entry
1295 iostream_close_cb (GObject *source_object,
1299 GDBusWorker *worker = user_data;
1300 GError *error = NULL;
1301 GList *pending_close_attempts, *pending_flush_attempts;
1304 g_io_stream_close_finish (worker->stream, res, &error);
1306 g_mutex_lock (worker->write_lock);
1308 pending_close_attempts = worker->pending_close_attempts;
1309 worker->pending_close_attempts = NULL;
1311 pending_flush_attempts = worker->write_pending_flushes;
1312 worker->write_pending_flushes = NULL;
1314 send_queue = worker->write_queue;
1315 worker->write_queue = g_queue_new ();
1317 g_assert (worker->output_pending);
1318 worker->output_pending = FALSE;
1320 g_mutex_unlock (worker->write_lock);
1322 while (pending_close_attempts != NULL)
1324 CloseData *close_data = pending_close_attempts->data;
1326 pending_close_attempts = g_list_delete_link (pending_close_attempts,
1327 pending_close_attempts);
1329 if (close_data->result != NULL)
1332 g_simple_async_result_set_from_error (close_data->result, error);
1334 /* this must be in an idle because the result is likely to be
1335 * intended for another thread
1337 g_simple_async_result_complete_in_idle (close_data->result);
1340 close_data_free (close_data);
1343 g_clear_error (&error);
1345 /* all messages queued for sending are discarded */
1346 g_queue_foreach (send_queue, (GFunc) message_to_write_data_free, NULL);
1347 g_queue_free (send_queue);
1349 /* all queued flushes fail */
1350 error = g_error_new (G_IO_ERROR, G_IO_ERROR_CANCELLED,
1351 _("Operation was cancelled"));
1352 flush_data_list_complete (pending_flush_attempts, error);
1353 g_list_free (pending_flush_attempts);
1354 g_clear_error (&error);
1356 _g_dbus_worker_unref (worker);
1359 /* called in private thread shared by all GDBusConnection instances
1361 * write-lock is not held on entry
1362 * output_pending must be false on entry
1365 maybe_write_next_message (GDBusWorker *worker)
1367 MessageToWriteData *data;
1370 /* we mustn't try to write two things at once */
1371 g_assert (!worker->output_pending);
1373 g_mutex_lock (worker->write_lock);
1375 /* if we want to close the connection, that takes precedence */
1376 if (worker->pending_close_attempts != NULL)
1378 worker->output_pending = TRUE;
1380 g_io_stream_close_async (worker->stream, G_PRIORITY_DEFAULT,
1381 NULL, iostream_close_cb,
1382 _g_dbus_worker_ref (worker));
1387 data = g_queue_pop_head (worker->write_queue);
1390 worker->output_pending = TRUE;
1393 g_mutex_unlock (worker->write_lock);
1395 /* Note that write_lock is only used for protecting the @write_queue
1396 * and @output_pending fields of the GDBusWorker struct ... which we
1397 * need to modify from arbitrary threads in _g_dbus_worker_send_message().
1399 * Therefore, it's fine to drop it here when calling back into user
1400 * code and then writing the message out onto the GIOStream since this
1401 * function only runs on the worker thread.
1405 GDBusMessage *old_message;
1407 gsize new_blob_size;
1410 old_message = data->message;
1411 data->message = _g_dbus_worker_emit_message_about_to_be_sent (worker, data->message);
1412 if (data->message == old_message)
1414 /* filters had no effect - do nothing */
1416 else if (data->message == NULL)
1418 /* filters dropped message */
1419 g_mutex_lock (worker->write_lock);
1420 worker->output_pending = FALSE;
1421 g_mutex_unlock (worker->write_lock);
1422 message_to_write_data_free (data);
1427 /* filters altered the message -> reencode */
1429 new_blob = g_dbus_message_to_blob (data->message,
1431 worker->capabilities,
1433 if (new_blob == NULL)
1435 /* if filter make the GDBusMessage unencodeable, just complain on stderr and send
1436 * the old message instead
1438 g_warning ("Error encoding GDBusMessage with serial %d altered by filter function: %s",
1439 g_dbus_message_get_serial (data->message),
1441 g_error_free (error);
1445 g_free (data->blob);
1446 data->blob = (gchar *) new_blob;
1447 data->blob_size = new_blob_size;
1451 write_message_async (worker,
1458 /* called in private thread shared by all GDBusConnection instances
1460 * write-lock is not held on entry
1461 * output_pending may be true or false
1464 write_message_in_idle_cb (gpointer user_data)
1466 GDBusWorker *worker = user_data;
1468 /* Because this is the worker thread, we can read this struct member
1469 * without holding the lock: no other thread ever modifies it.
1471 if (!worker->output_pending)
1472 maybe_write_next_message (worker);
1478 * @write_data: (transfer full) (allow-none):
1479 * @close_data: (transfer full) (allow-none):
1481 * Can be called from any thread
1483 * write_lock is not held on entry
1484 * output_pending may be true or false
1487 schedule_write_in_worker_thread (GDBusWorker *worker,
1488 MessageToWriteData *write_data,
1489 CloseData *close_data)
1491 g_mutex_lock (worker->write_lock);
1493 if (write_data != NULL)
1494 g_queue_push_tail (worker->write_queue, write_data);
1496 if (close_data != NULL)
1497 worker->pending_close_attempts = g_list_prepend (worker->pending_close_attempts,
1500 if (!worker->output_pending)
1502 GSource *idle_source;
1503 idle_source = g_idle_source_new ();
1504 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1505 g_source_set_callback (idle_source,
1506 write_message_in_idle_cb,
1507 _g_dbus_worker_ref (worker),
1508 (GDestroyNotify) _g_dbus_worker_unref);
1509 g_source_attach (idle_source, worker->shared_thread_data->context);
1510 g_source_unref (idle_source);
1513 g_mutex_unlock (worker->write_lock);
1516 /* ---------------------------------------------------------------------------------------------------- */
1518 /* can be called from any thread - steals blob
1520 * write_lock is not held on entry
1521 * output_pending may be true or false
1524 _g_dbus_worker_send_message (GDBusWorker *worker,
1525 GDBusMessage *message,
1529 MessageToWriteData *data;
1531 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1532 g_return_if_fail (blob != NULL);
1533 g_return_if_fail (blob_len > 16);
1535 data = g_new0 (MessageToWriteData, 1);
1536 data->worker = _g_dbus_worker_ref (worker);
1537 data->message = g_object_ref (message);
1538 data->blob = blob; /* steal! */
1539 data->blob_size = blob_len;
1541 schedule_write_in_worker_thread (worker, data, NULL);
1544 /* ---------------------------------------------------------------------------------------------------- */
1547 _g_dbus_worker_new (GIOStream *stream,
1548 GDBusCapabilityFlags capabilities,
1549 gboolean initially_frozen,
1550 GDBusWorkerMessageReceivedCallback message_received_callback,
1551 GDBusWorkerMessageAboutToBeSentCallback message_about_to_be_sent_callback,
1552 GDBusWorkerDisconnectedCallback disconnected_callback,
1555 GDBusWorker *worker;
1556 GSource *idle_source;
1558 g_return_val_if_fail (G_IS_IO_STREAM (stream), NULL);
1559 g_return_val_if_fail (message_received_callback != NULL, NULL);
1560 g_return_val_if_fail (message_about_to_be_sent_callback != NULL, NULL);
1561 g_return_val_if_fail (disconnected_callback != NULL, NULL);
1563 worker = g_new0 (GDBusWorker, 1);
1564 worker->ref_count = 1;
1566 worker->read_lock = g_mutex_new ();
1567 worker->message_received_callback = message_received_callback;
1568 worker->message_about_to_be_sent_callback = message_about_to_be_sent_callback;
1569 worker->disconnected_callback = disconnected_callback;
1570 worker->user_data = user_data;
1571 worker->stream = g_object_ref (stream);
1572 worker->capabilities = capabilities;
1573 worker->cancellable = g_cancellable_new ();
1574 worker->output_pending = FALSE;
1576 worker->frozen = initially_frozen;
1577 worker->received_messages_while_frozen = g_queue_new ();
1579 worker->write_lock = g_mutex_new ();
1580 worker->write_queue = g_queue_new ();
1582 if (G_IS_SOCKET_CONNECTION (worker->stream))
1583 worker->socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream));
1585 worker->shared_thread_data = _g_dbus_shared_thread_ref ();
1588 idle_source = g_idle_source_new ();
1589 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1590 g_source_set_callback (idle_source,
1591 _g_dbus_worker_do_initial_read,
1594 g_source_attach (idle_source, worker->shared_thread_data->context);
1595 g_source_unref (idle_source);
1600 /* ---------------------------------------------------------------------------------------------------- */
1602 /* can be called from any thread
1604 * write_lock is not held on entry
1605 * output_pending may be true or false
1608 _g_dbus_worker_close (GDBusWorker *worker,
1609 GCancellable *cancellable,
1610 GSimpleAsyncResult *result)
1612 CloseData *close_data;
1614 close_data = g_slice_new0 (CloseData);
1615 close_data->worker = _g_dbus_worker_ref (worker);
1616 close_data->cancellable =
1617 (cancellable == NULL ? NULL : g_object_ref (cancellable));
1618 close_data->result = (result == NULL ? NULL : g_object_ref (result));
1620 g_cancellable_cancel (worker->cancellable);
1621 schedule_write_in_worker_thread (worker, NULL, close_data);
1624 /* This can be called from any thread - frees worker. Note that
1625 * callbacks might still happen if called from another thread than the
1626 * worker - use your own synchronization primitive in the callbacks.
1628 * write_lock is not held on entry
1629 * output_pending may be true or false
1632 _g_dbus_worker_stop (GDBusWorker *worker)
1634 worker->stopped = TRUE;
1636 /* Cancel any pending operations and schedule a close of the underlying I/O
1637 * stream in the worker thread
1639 _g_dbus_worker_close (worker, NULL, NULL);
1641 /* _g_dbus_worker_close holds a ref until after an idle in the the worker
1642 * thread has run, so we no longer need to unref in an idle like in
1645 _g_dbus_worker_unref (worker);
1648 /* ---------------------------------------------------------------------------------------------------- */
1650 /* can be called from any thread (except the worker thread) - blocks
1651 * calling thread until all queued outgoing messages are written and
1652 * the transport has been flushed
1654 * write_lock is not held on entry
1655 * output_pending may be true or false
1658 _g_dbus_worker_flush_sync (GDBusWorker *worker,
1659 GCancellable *cancellable,
1668 /* if the queue is empty, there's nothing to wait for */
1669 g_mutex_lock (worker->write_lock);
1670 if (g_queue_get_length (worker->write_queue) > 0)
1672 data = g_new0 (FlushData, 1);
1673 data->mutex = g_mutex_new ();
1674 data->cond = g_cond_new ();
1675 data->number_to_wait_for = worker->write_num_messages_written + g_queue_get_length (worker->write_queue);
1676 g_mutex_lock (data->mutex);
1677 worker->write_pending_flushes = g_list_prepend (worker->write_pending_flushes, data);
1679 g_mutex_unlock (worker->write_lock);
1683 g_cond_wait (data->cond, data->mutex);
1684 g_mutex_unlock (data->mutex);
1686 /* note:the element is removed from worker->write_pending_flushes in flush_cb() above */
1687 g_cond_free (data->cond);
1688 g_mutex_free (data->mutex);
1689 if (data->error != NULL)
1692 g_propagate_error (error, data->error);
1700 /* ---------------------------------------------------------------------------------------------------- */
1702 #define G_DBUS_DEBUG_AUTHENTICATION (1<<0)
1703 #define G_DBUS_DEBUG_TRANSPORT (1<<1)
1704 #define G_DBUS_DEBUG_MESSAGE (1<<2)
1705 #define G_DBUS_DEBUG_PAYLOAD (1<<3)
1706 #define G_DBUS_DEBUG_CALL (1<<4)
1707 #define G_DBUS_DEBUG_SIGNAL (1<<5)
1708 #define G_DBUS_DEBUG_INCOMING (1<<6)
1709 #define G_DBUS_DEBUG_RETURN (1<<7)
1710 #define G_DBUS_DEBUG_EMISSION (1<<8)
1711 #define G_DBUS_DEBUG_ADDRESS (1<<9)
1713 static gint _gdbus_debug_flags = 0;
1716 _g_dbus_debug_authentication (void)
1718 _g_dbus_initialize ();
1719 return (_gdbus_debug_flags & G_DBUS_DEBUG_AUTHENTICATION) != 0;
1723 _g_dbus_debug_transport (void)
1725 _g_dbus_initialize ();
1726 return (_gdbus_debug_flags & G_DBUS_DEBUG_TRANSPORT) != 0;
1730 _g_dbus_debug_message (void)
1732 _g_dbus_initialize ();
1733 return (_gdbus_debug_flags & G_DBUS_DEBUG_MESSAGE) != 0;
1737 _g_dbus_debug_payload (void)
1739 _g_dbus_initialize ();
1740 return (_gdbus_debug_flags & G_DBUS_DEBUG_PAYLOAD) != 0;
1744 _g_dbus_debug_call (void)
1746 _g_dbus_initialize ();
1747 return (_gdbus_debug_flags & G_DBUS_DEBUG_CALL) != 0;
1751 _g_dbus_debug_signal (void)
1753 _g_dbus_initialize ();
1754 return (_gdbus_debug_flags & G_DBUS_DEBUG_SIGNAL) != 0;
1758 _g_dbus_debug_incoming (void)
1760 _g_dbus_initialize ();
1761 return (_gdbus_debug_flags & G_DBUS_DEBUG_INCOMING) != 0;
1765 _g_dbus_debug_return (void)
1767 _g_dbus_initialize ();
1768 return (_gdbus_debug_flags & G_DBUS_DEBUG_RETURN) != 0;
1772 _g_dbus_debug_emission (void)
1774 _g_dbus_initialize ();
1775 return (_gdbus_debug_flags & G_DBUS_DEBUG_EMISSION) != 0;
1779 _g_dbus_debug_address (void)
1781 _g_dbus_initialize ();
1782 return (_gdbus_debug_flags & G_DBUS_DEBUG_ADDRESS) != 0;
1785 G_LOCK_DEFINE_STATIC (print_lock);
1788 _g_dbus_debug_print_lock (void)
1790 G_LOCK (print_lock);
1794 _g_dbus_debug_print_unlock (void)
1796 G_UNLOCK (print_lock);
1800 * _g_dbus_initialize:
1802 * Does various one-time init things such as
1804 * - registering the G_DBUS_ERROR error domain
1805 * - parses the G_DBUS_DEBUG environment variable
1808 _g_dbus_initialize (void)
1810 static volatile gsize initialized = 0;
1812 if (g_once_init_enter (&initialized))
1814 volatile GQuark g_dbus_error_domain;
1817 g_dbus_error_domain = G_DBUS_ERROR;
1818 (g_dbus_error_domain); /* To avoid -Wunused-but-set-variable */
1820 debug = g_getenv ("G_DBUS_DEBUG");
1823 const GDebugKey keys[] = {
1824 { "authentication", G_DBUS_DEBUG_AUTHENTICATION },
1825 { "transport", G_DBUS_DEBUG_TRANSPORT },
1826 { "message", G_DBUS_DEBUG_MESSAGE },
1827 { "payload", G_DBUS_DEBUG_PAYLOAD },
1828 { "call", G_DBUS_DEBUG_CALL },
1829 { "signal", G_DBUS_DEBUG_SIGNAL },
1830 { "incoming", G_DBUS_DEBUG_INCOMING },
1831 { "return", G_DBUS_DEBUG_RETURN },
1832 { "emission", G_DBUS_DEBUG_EMISSION },
1833 { "address", G_DBUS_DEBUG_ADDRESS }
1836 _gdbus_debug_flags = g_parse_debug_string (debug, keys, G_N_ELEMENTS (keys));
1837 if (_gdbus_debug_flags & G_DBUS_DEBUG_PAYLOAD)
1838 _gdbus_debug_flags |= G_DBUS_DEBUG_MESSAGE;
1841 g_once_init_leave (&initialized, 1);
1845 /* ---------------------------------------------------------------------------------------------------- */
1848 _g_dbus_compute_complete_signature (GDBusArgInfo **args)
1850 const GVariantType *arg_types[256];
1854 for (n = 0; args[n] != NULL; n++)
1856 /* DBus places a hard limit of 255 on signature length.
1857 * therefore number of args must be less than 256.
1861 arg_types[n] = G_VARIANT_TYPE (args[n]->signature);
1863 if G_UNLIKELY (arg_types[n] == NULL)
1869 return g_variant_type_new_tuple (arg_types, n);
1872 /* ---------------------------------------------------------------------------------------------------- */
1876 extern BOOL WINAPI ConvertSidToStringSidA (PSID Sid, LPSTR *StringSid);
1879 _g_dbus_win32_get_user_sid (void)
1883 DWORD token_information_len;
1890 h = INVALID_HANDLE_VALUE;
1892 if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &h))
1894 g_warning ("OpenProcessToken failed with error code %d", (gint) GetLastError ());
1898 /* Get length of buffer */
1899 token_information_len = 0;
1900 if (!GetTokenInformation (h, TokenUser, NULL, 0, &token_information_len))
1902 if (GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1904 g_warning ("GetTokenInformation() failed with error code %d", (gint) GetLastError ());
1908 user = g_malloc (token_information_len);
1909 if (!GetTokenInformation (h, TokenUser, user, token_information_len, &token_information_len))
1911 g_warning ("GetTokenInformation() failed with error code %d", (gint) GetLastError ());
1915 psid = user->User.Sid;
1916 if (!IsValidSid (psid))
1918 g_warning ("Invalid SID");
1922 if (!ConvertSidToStringSidA (psid, &sid))
1924 g_warning ("Invalid SID");
1928 ret = g_strdup (sid);
1933 if (h != INVALID_HANDLE_VALUE)
1939 /* ---------------------------------------------------------------------------------------------------- */
1942 _g_dbus_get_machine_id (GError **error)
1945 /* TODO: use PACKAGE_LOCALSTATEDIR ? */
1947 if (!g_file_get_contents ("/var/lib/dbus/machine-id",
1952 g_prefix_error (error, _("Unable to load /var/lib/dbus/machine-id: "));
1956 /* TODO: validate value */
1962 /* ---------------------------------------------------------------------------------------------------- */
1965 _g_dbus_enum_to_string (GType enum_type, gint value)
1969 GEnumValue *enum_value;
1971 klass = g_type_class_ref (enum_type);
1972 enum_value = g_enum_get_value (klass, value);
1973 if (enum_value != NULL)
1974 ret = g_strdup (enum_value->value_nick);
1976 ret = g_strdup_printf ("unknown (value %d)", value);
1977 g_type_class_unref (klass);
1981 /* ---------------------------------------------------------------------------------------------------- */
1984 write_message_print_transport_debug (gssize bytes_written,
1985 MessageToWriteData *data)
1987 if (G_LIKELY (!_g_dbus_debug_transport ()))
1990 _g_dbus_debug_print_lock ();
1991 g_print ("========================================================================\n"
1992 "GDBus-debug:Transport:\n"
1993 " >>>> WROTE %" G_GSIZE_FORMAT " bytes of message with serial %d and\n"
1994 " size %" G_GSIZE_FORMAT " from offset %" G_GSIZE_FORMAT " on a %s\n",
1996 g_dbus_message_get_serial (data->message),
1998 data->total_written,
1999 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream))));
2000 _g_dbus_debug_print_unlock ();
2005 /* ---------------------------------------------------------------------------------------------------- */
2008 read_message_print_transport_debug (gssize bytes_read,
2009 GDBusWorker *worker)
2013 gint32 message_length;
2015 if (G_LIKELY (!_g_dbus_debug_transport ()))
2018 size = bytes_read + worker->read_buffer_cur_size;
2022 message_length = g_dbus_message_bytes_needed ((guchar *) worker->read_buffer, size, NULL);
2025 switch (worker->read_buffer[0])
2029 serial = GUINT32_FROM_LE (((guint32 *) worker->read_buffer)[2]);
2033 serial = GUINT32_FROM_BE (((guint32 *) worker->read_buffer)[2]);
2036 /* an error will be set elsewhere if this happens */
2041 _g_dbus_debug_print_lock ();
2042 g_print ("========================================================================\n"
2043 "GDBus-debug:Transport:\n"
2044 " <<<< READ %" G_GSIZE_FORMAT " bytes of message with serial %d and\n"
2045 " size %d to offset %" G_GSIZE_FORMAT " from a %s\n",
2049 worker->read_buffer_cur_size,
2050 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_input_stream (worker->stream))));
2051 _g_dbus_debug_print_unlock ();
2056 /* ---------------------------------------------------------------------------------------------------- */
2059 _g_signal_accumulator_false_handled (GSignalInvocationHint *ihint,
2060 GValue *return_accu,
2061 const GValue *handler_return,
2064 gboolean continue_emission;
2065 gboolean signal_return;
2067 signal_return = g_value_get_boolean (handler_return);
2068 g_value_set_boolean (return_accu, signal_return);
2069 continue_emission = signal_return;
2071 return continue_emission;