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 */
382 guint64 write_num_messages_written;
383 GList *write_pending_flushes;
384 /* list of CloseData */
385 GList *pending_close_attempts;
388 static void _g_dbus_worker_unref (GDBusWorker *worker);
390 /* ---------------------------------------------------------------------------------------------------- */
396 guint64 number_to_wait_for;
400 struct _MessageToWriteData ;
401 typedef struct _MessageToWriteData MessageToWriteData;
403 static void message_to_write_data_free (MessageToWriteData *data);
405 static void read_message_print_transport_debug (gssize bytes_read,
406 GDBusWorker *worker);
408 static void write_message_print_transport_debug (gssize bytes_written,
409 MessageToWriteData *data);
413 GCancellable *cancellable;
414 GSimpleAsyncResult *result;
417 static void close_data_free (CloseData *close_data)
419 if (close_data->cancellable != NULL)
420 g_object_unref (close_data->cancellable);
422 if (close_data->result != NULL)
423 g_object_unref (close_data->result);
425 _g_dbus_worker_unref (close_data->worker);
426 g_slice_free (CloseData, close_data);
429 /* ---------------------------------------------------------------------------------------------------- */
432 _g_dbus_worker_ref (GDBusWorker *worker)
434 g_atomic_int_inc (&worker->ref_count);
439 _g_dbus_worker_unref (GDBusWorker *worker)
441 if (g_atomic_int_dec_and_test (&worker->ref_count))
443 g_assert (worker->write_pending_flushes == NULL);
445 _g_dbus_shared_thread_unref (worker->shared_thread_data);
447 g_object_unref (worker->stream);
449 g_mutex_free (worker->read_lock);
450 g_object_unref (worker->cancellable);
451 if (worker->read_fd_list != NULL)
452 g_object_unref (worker->read_fd_list);
454 g_queue_foreach (worker->received_messages_while_frozen, (GFunc) g_object_unref, NULL);
455 g_queue_free (worker->received_messages_while_frozen);
457 g_mutex_free (worker->write_lock);
458 g_queue_foreach (worker->write_queue, (GFunc) message_to_write_data_free, NULL);
459 g_queue_free (worker->write_queue);
461 g_free (worker->read_buffer);
468 _g_dbus_worker_emit_disconnected (GDBusWorker *worker,
469 gboolean remote_peer_vanished,
472 if (!worker->stopped)
473 worker->disconnected_callback (worker, remote_peer_vanished, error, worker->user_data);
477 _g_dbus_worker_emit_message_received (GDBusWorker *worker,
478 GDBusMessage *message)
480 if (!worker->stopped)
481 worker->message_received_callback (worker, message, worker->user_data);
484 static GDBusMessage *
485 _g_dbus_worker_emit_message_about_to_be_sent (GDBusWorker *worker,
486 GDBusMessage *message)
489 if (!worker->stopped)
490 ret = worker->message_about_to_be_sent_callback (worker, message, worker->user_data);
496 /* can only be called from private thread with read-lock held - takes ownership of @message */
498 _g_dbus_worker_queue_or_deliver_received_message (GDBusWorker *worker,
499 GDBusMessage *message)
501 if (worker->frozen || g_queue_get_length (worker->received_messages_while_frozen) > 0)
504 g_queue_push_tail (worker->received_messages_while_frozen, message);
508 /* not frozen, nor anything in queue */
509 _g_dbus_worker_emit_message_received (worker, message);
510 g_object_unref (message);
514 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
516 unfreeze_in_idle_cb (gpointer user_data)
518 GDBusWorker *worker = user_data;
519 GDBusMessage *message;
521 g_mutex_lock (worker->read_lock);
524 while ((message = g_queue_pop_head (worker->received_messages_while_frozen)) != NULL)
526 _g_dbus_worker_emit_message_received (worker, message);
527 g_object_unref (message);
529 worker->frozen = FALSE;
533 g_assert (g_queue_get_length (worker->received_messages_while_frozen) == 0);
535 g_mutex_unlock (worker->read_lock);
539 /* can be called from any thread */
541 _g_dbus_worker_unfreeze (GDBusWorker *worker)
543 GSource *idle_source;
544 idle_source = g_idle_source_new ();
545 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
546 g_source_set_callback (idle_source,
548 _g_dbus_worker_ref (worker),
549 (GDestroyNotify) _g_dbus_worker_unref);
550 g_source_attach (idle_source, worker->shared_thread_data->context);
551 g_source_unref (idle_source);
554 /* ---------------------------------------------------------------------------------------------------- */
556 static void _g_dbus_worker_do_read_unlocked (GDBusWorker *worker);
558 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
560 _g_dbus_worker_do_read_cb (GInputStream *input_stream,
564 GDBusWorker *worker = user_data;
568 g_mutex_lock (worker->read_lock);
570 /* If already stopped, don't even process the reply */
575 if (worker->socket == NULL)
576 bytes_read = g_input_stream_read_finish (g_io_stream_get_input_stream (worker->stream),
580 bytes_read = _g_socket_read_with_control_messages_finish (worker->socket,
583 if (worker->read_num_ancillary_messages > 0)
586 for (n = 0; n < worker->read_num_ancillary_messages; n++)
588 GSocketControlMessage *control_message = G_SOCKET_CONTROL_MESSAGE (worker->read_ancillary_messages[n]);
594 else if (G_IS_UNIX_FD_MESSAGE (control_message))
596 GUnixFDMessage *fd_message;
600 fd_message = G_UNIX_FD_MESSAGE (control_message);
601 fds = g_unix_fd_message_steal_fds (fd_message, &num_fds);
602 if (worker->read_fd_list == NULL)
604 worker->read_fd_list = g_unix_fd_list_new_from_array (fds, num_fds);
609 for (n = 0; n < num_fds; n++)
611 /* TODO: really want a append_steal() */
612 g_unix_fd_list_append (worker->read_fd_list, fds[n], NULL);
618 else if (G_IS_UNIX_CREDENTIALS_MESSAGE (control_message))
630 "Unexpected ancillary message of type %s received from peer",
631 g_type_name (G_TYPE_FROM_INSTANCE (control_message)));
632 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
633 g_error_free (error);
634 g_object_unref (control_message);
636 while (n < worker->read_num_ancillary_messages)
637 g_object_unref (worker->read_ancillary_messages[n++]);
638 g_free (worker->read_ancillary_messages);
642 g_object_unref (control_message);
644 g_free (worker->read_ancillary_messages);
647 if (bytes_read == -1)
649 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
650 g_error_free (error);
655 g_debug ("read %d bytes (is_closed=%d blocking=%d condition=0x%02x) stream %p, %p",
657 g_socket_is_closed (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream))),
658 g_socket_get_blocking (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream))),
659 g_socket_condition_check (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream)),
660 G_IO_IN | G_IO_OUT | G_IO_HUP),
665 /* TODO: hmm, hmm... */
671 "Underlying GIOStream returned 0 bytes on an async read");
672 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
673 g_error_free (error);
677 read_message_print_transport_debug (bytes_read, worker);
679 worker->read_buffer_cur_size += bytes_read;
680 if (worker->read_buffer_bytes_wanted == worker->read_buffer_cur_size)
682 /* OK, got what we asked for! */
683 if (worker->read_buffer_bytes_wanted == 16)
686 /* OK, got the header - determine how many more bytes are needed */
688 message_len = g_dbus_message_bytes_needed ((guchar *) worker->read_buffer,
691 if (message_len == -1)
693 g_warning ("_g_dbus_worker_do_read_cb: error determing bytes needed: %s", error->message);
694 _g_dbus_worker_emit_disconnected (worker, FALSE, error);
695 g_error_free (error);
699 worker->read_buffer_bytes_wanted = message_len;
700 _g_dbus_worker_do_read_unlocked (worker);
704 GDBusMessage *message;
707 /* TODO: use connection->priv->auth to decode the message */
709 message = g_dbus_message_new_from_blob ((guchar *) worker->read_buffer,
710 worker->read_buffer_cur_size,
711 worker->capabilities,
716 s = _g_dbus_hexdump (worker->read_buffer, worker->read_buffer_cur_size, 2);
717 g_warning ("Error decoding D-Bus message of %" G_GSIZE_FORMAT " bytes\n"
719 "The payload is as follows:\n"
721 worker->read_buffer_cur_size,
725 _g_dbus_worker_emit_disconnected (worker, FALSE, error);
726 g_error_free (error);
731 if (worker->read_fd_list != NULL)
733 g_dbus_message_set_unix_fd_list (message, worker->read_fd_list);
734 g_object_unref (worker->read_fd_list);
735 worker->read_fd_list = NULL;
739 if (G_UNLIKELY (_g_dbus_debug_message ()))
742 _g_dbus_debug_print_lock ();
743 g_print ("========================================================================\n"
744 "GDBus-debug:Message:\n"
745 " <<<< RECEIVED D-Bus message (%" G_GSIZE_FORMAT " bytes)\n",
746 worker->read_buffer_cur_size);
747 s = g_dbus_message_print (message, 2);
750 if (G_UNLIKELY (_g_dbus_debug_payload ()))
752 s = _g_dbus_hexdump (worker->read_buffer, worker->read_buffer_cur_size, 2);
756 _g_dbus_debug_print_unlock ();
759 /* yay, got a message, go deliver it */
760 _g_dbus_worker_queue_or_deliver_received_message (worker, message);
762 /* start reading another message! */
763 worker->read_buffer_bytes_wanted = 0;
764 worker->read_buffer_cur_size = 0;
765 _g_dbus_worker_do_read_unlocked (worker);
770 /* didn't get all the bytes we requested - so repeat the request... */
771 _g_dbus_worker_do_read_unlocked (worker);
775 g_mutex_unlock (worker->read_lock);
777 /* gives up the reference acquired when calling g_input_stream_read_async() */
778 _g_dbus_worker_unref (worker);
781 /* called in private thread shared by all GDBusConnection instances (with read-lock held) */
783 _g_dbus_worker_do_read_unlocked (GDBusWorker *worker)
785 /* if bytes_wanted is zero, it means start reading a message */
786 if (worker->read_buffer_bytes_wanted == 0)
788 worker->read_buffer_cur_size = 0;
789 worker->read_buffer_bytes_wanted = 16;
792 /* ensure we have a (big enough) buffer */
793 if (worker->read_buffer == NULL || worker->read_buffer_bytes_wanted > worker->read_buffer_allocated_size)
795 /* TODO: 4096 is randomly chosen; might want a better chosen default minimum */
796 worker->read_buffer_allocated_size = MAX (worker->read_buffer_bytes_wanted, 4096);
797 worker->read_buffer = g_realloc (worker->read_buffer, worker->read_buffer_allocated_size);
800 if (worker->socket == NULL)
801 g_input_stream_read_async (g_io_stream_get_input_stream (worker->stream),
802 worker->read_buffer + worker->read_buffer_cur_size,
803 worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size,
806 (GAsyncReadyCallback) _g_dbus_worker_do_read_cb,
807 _g_dbus_worker_ref (worker));
810 worker->read_ancillary_messages = NULL;
811 worker->read_num_ancillary_messages = 0;
812 _g_socket_read_with_control_messages (worker->socket,
813 worker->read_buffer + worker->read_buffer_cur_size,
814 worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size,
815 &worker->read_ancillary_messages,
816 &worker->read_num_ancillary_messages,
819 (GAsyncReadyCallback) _g_dbus_worker_do_read_cb,
820 _g_dbus_worker_ref (worker));
824 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
826 _g_dbus_worker_do_initial_read (gpointer data)
828 GDBusWorker *worker = data;
829 g_mutex_lock (worker->read_lock);
830 _g_dbus_worker_do_read_unlocked (worker);
831 g_mutex_unlock (worker->read_lock);
835 /* ---------------------------------------------------------------------------------------------------- */
837 struct _MessageToWriteData
840 GDBusMessage *message;
845 GSimpleAsyncResult *simple;
850 message_to_write_data_free (MessageToWriteData *data)
852 _g_dbus_worker_unref (data->worker);
854 g_object_unref (data->message);
859 /* ---------------------------------------------------------------------------------------------------- */
861 static void write_message_continue_writing (MessageToWriteData *data);
863 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
865 write_message_async_cb (GObject *source_object,
869 MessageToWriteData *data = user_data;
870 GSimpleAsyncResult *simple;
871 gssize bytes_written;
874 /* Note: we can't access data->simple after calling g_async_result_complete () because the
875 * callback can free @data and we're not completing in idle. So use a copy of the pointer.
877 simple = data->simple;
880 bytes_written = g_output_stream_write_finish (G_OUTPUT_STREAM (source_object),
883 if (bytes_written == -1)
885 g_simple_async_result_take_error (simple, error);
886 g_simple_async_result_complete (simple);
887 g_object_unref (simple);
890 g_assert (bytes_written > 0); /* zero is never returned */
892 write_message_print_transport_debug (bytes_written, data);
894 data->total_written += bytes_written;
895 g_assert (data->total_written <= data->blob_size);
896 if (data->total_written == data->blob_size)
898 g_simple_async_result_complete (simple);
899 g_object_unref (simple);
903 write_message_continue_writing (data);
909 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
911 on_socket_ready (GSocket *socket,
912 GIOCondition condition,
915 MessageToWriteData *data = user_data;
916 write_message_continue_writing (data);
917 return FALSE; /* remove source */
920 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
922 write_message_continue_writing (MessageToWriteData *data)
924 GOutputStream *ostream;
925 GSimpleAsyncResult *simple;
927 GUnixFDList *fd_list;
930 /* Note: we can't access data->simple after calling g_async_result_complete () because the
931 * callback can free @data and we're not completing in idle. So use a copy of the pointer.
933 simple = data->simple;
935 ostream = g_io_stream_get_output_stream (data->worker->stream);
937 fd_list = g_dbus_message_get_unix_fd_list (data->message);
940 g_assert (!g_output_stream_has_pending (ostream));
941 g_assert_cmpint (data->total_written, <, data->blob_size);
947 else if (G_IS_SOCKET_OUTPUT_STREAM (ostream) && data->total_written == 0)
949 GOutputVector vector;
950 GSocketControlMessage *control_message;
951 gssize bytes_written;
954 vector.buffer = data->blob;
955 vector.size = data->blob_size;
957 control_message = NULL;
958 if (fd_list != NULL && g_unix_fd_list_get_length (fd_list) > 0)
960 if (!(data->worker->capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING))
962 g_simple_async_result_set_error (simple,
965 "Tried sending a file descriptor but remote peer does not support this capability");
966 g_simple_async_result_complete (simple);
967 g_object_unref (simple);
970 control_message = g_unix_fd_message_new_with_fd_list (fd_list);
974 bytes_written = g_socket_send_message (data->worker->socket,
978 control_message != NULL ? &control_message : NULL,
979 control_message != NULL ? 1 : 0,
981 data->worker->cancellable,
983 if (control_message != NULL)
984 g_object_unref (control_message);
986 if (bytes_written == -1)
988 /* Handle WOULD_BLOCK by waiting until there's room in the buffer */
989 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
992 source = g_socket_create_source (data->worker->socket,
993 G_IO_OUT | G_IO_HUP | G_IO_ERR,
994 data->worker->cancellable);
995 g_source_set_callback (source,
996 (GSourceFunc) on_socket_ready,
998 NULL); /* GDestroyNotify */
999 g_source_attach (source, g_main_context_get_thread_default ());
1000 g_source_unref (source);
1001 g_error_free (error);
1004 g_simple_async_result_take_error (simple, error);
1005 g_simple_async_result_complete (simple);
1006 g_object_unref (simple);
1009 g_assert (bytes_written > 0); /* zero is never returned */
1011 write_message_print_transport_debug (bytes_written, data);
1013 data->total_written += bytes_written;
1014 g_assert (data->total_written <= data->blob_size);
1015 if (data->total_written == data->blob_size)
1017 g_simple_async_result_complete (simple);
1018 g_object_unref (simple);
1022 write_message_continue_writing (data);
1028 if (fd_list != NULL)
1030 g_simple_async_result_set_error (simple,
1033 "Tried sending a file descriptor on unsupported stream of type %s",
1034 g_type_name (G_TYPE_FROM_INSTANCE (ostream)));
1035 g_simple_async_result_complete (simple);
1036 g_object_unref (simple);
1041 g_output_stream_write_async (ostream,
1042 (const gchar *) data->blob + data->total_written,
1043 data->blob_size - data->total_written,
1045 data->worker->cancellable,
1046 write_message_async_cb,
1053 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
1055 write_message_async (GDBusWorker *worker,
1056 MessageToWriteData *data,
1057 GAsyncReadyCallback callback,
1060 data->simple = g_simple_async_result_new (NULL,
1063 write_message_async);
1064 data->total_written = 0;
1065 write_message_continue_writing (data);
1068 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
1070 write_message_finish (GAsyncResult *res,
1073 g_warn_if_fail (g_simple_async_result_get_source_tag (G_SIMPLE_ASYNC_RESULT (res)) == write_message_async);
1074 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error))
1079 /* ---------------------------------------------------------------------------------------------------- */
1081 static void maybe_write_next_message (GDBusWorker *worker);
1085 GDBusWorker *worker;
1090 flush_data_list_complete (const GList *flushers,
1091 const GError *error)
1095 for (l = flushers; l != NULL; l = l->next)
1097 FlushData *f = l->data;
1099 f->error = error != NULL ? g_error_copy (error) : NULL;
1101 g_mutex_lock (f->mutex);
1102 g_cond_signal (f->cond);
1103 g_mutex_unlock (f->mutex);
1107 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
1109 ostream_flush_cb (GObject *source_object,
1113 FlushAsyncData *data = user_data;
1117 g_output_stream_flush_finish (G_OUTPUT_STREAM (source_object),
1123 if (G_UNLIKELY (_g_dbus_debug_transport ()))
1125 _g_dbus_debug_print_lock ();
1126 g_print ("========================================================================\n"
1127 "GDBus-debug:Transport:\n"
1128 " ---- FLUSHED stream of type %s\n",
1129 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream))));
1130 _g_dbus_debug_print_unlock ();
1134 g_assert (data->flushers != NULL);
1135 flush_data_list_complete (data->flushers, error);
1136 g_list_free (data->flushers);
1139 g_error_free (error);
1141 /* Make sure we tell folks that we don't have additional
1143 g_mutex_lock (data->worker->write_lock);
1144 g_assert (data->worker->output_pending);
1145 data->worker->output_pending = FALSE;
1146 g_mutex_unlock (data->worker->write_lock);
1148 /* OK, cool, finally kick off the next write */
1149 maybe_write_next_message (data->worker);
1151 _g_dbus_worker_unref (data->worker);
1155 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
1157 message_written (GDBusWorker *worker,
1158 MessageToWriteData *message_data)
1164 /* first log the fact that we wrote a message */
1165 if (G_UNLIKELY (_g_dbus_debug_message ()))
1168 _g_dbus_debug_print_lock ();
1169 g_print ("========================================================================\n"
1170 "GDBus-debug:Message:\n"
1171 " >>>> SENT D-Bus message (%" G_GSIZE_FORMAT " bytes)\n",
1172 message_data->blob_size);
1173 s = g_dbus_message_print (message_data->message, 2);
1176 if (G_UNLIKELY (_g_dbus_debug_payload ()))
1178 s = _g_dbus_hexdump (message_data->blob, message_data->blob_size, 2);
1179 g_print ("%s\n", s);
1182 _g_dbus_debug_print_unlock ();
1185 /* then first wake up pending flushes and, if needed, flush the stream */
1187 g_mutex_lock (worker->write_lock);
1188 worker->write_num_messages_written += 1;
1189 for (l = worker->write_pending_flushes; l != NULL; l = ll)
1191 FlushData *f = l->data;
1194 if (f->number_to_wait_for == worker->write_num_messages_written)
1196 flushers = g_list_append (flushers, f);
1197 worker->write_pending_flushes = g_list_delete_link (worker->write_pending_flushes, l);
1200 if (flushers != NULL)
1202 g_assert (!worker->output_pending);
1203 worker->output_pending = TRUE;
1205 g_mutex_unlock (worker->write_lock);
1207 if (flushers != NULL)
1209 FlushAsyncData *data;
1210 data = g_new0 (FlushAsyncData, 1);
1211 data->worker = _g_dbus_worker_ref (worker);
1212 data->flushers = flushers;
1213 /* flush the stream before writing the next message */
1214 g_output_stream_flush_async (g_io_stream_get_output_stream (worker->stream),
1216 worker->cancellable,
1222 /* kick off the next write! */
1223 maybe_write_next_message (worker);
1227 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
1229 write_message_cb (GObject *source_object,
1233 MessageToWriteData *data = user_data;
1236 g_mutex_lock (data->worker->write_lock);
1237 g_assert (data->worker->output_pending);
1238 data->worker->output_pending = FALSE;
1239 g_mutex_unlock (data->worker->write_lock);
1242 if (!write_message_finish (res, &error))
1245 _g_dbus_worker_emit_disconnected (data->worker, TRUE, error);
1246 g_error_free (error);
1249 /* this function will also kick of the next write (it might need to
1250 * flush so writing the next message might happen much later
1253 message_written (data->worker, data);
1255 message_to_write_data_free (data);
1258 /* called in private thread shared by all GDBusConnection instances
1260 * write-lock is not held on entry
1261 * output_pending is true on entry
1264 iostream_close_cb (GObject *source_object,
1268 GDBusWorker *worker = user_data;
1269 GError *error = NULL;
1270 GList *pending_close_attempts, *pending_flush_attempts;
1273 g_io_stream_close_finish (worker->stream, res, &error);
1275 g_mutex_lock (worker->write_lock);
1277 pending_close_attempts = worker->pending_close_attempts;
1278 worker->pending_close_attempts = NULL;
1280 pending_flush_attempts = worker->write_pending_flushes;
1281 worker->write_pending_flushes = NULL;
1283 send_queue = worker->write_queue;
1284 worker->write_queue = g_queue_new ();
1286 g_assert (worker->output_pending);
1287 worker->output_pending = FALSE;
1289 g_mutex_unlock (worker->write_lock);
1291 while (pending_close_attempts != NULL)
1293 CloseData *close_data = pending_close_attempts->data;
1295 pending_close_attempts = g_list_delete_link (pending_close_attempts,
1296 pending_close_attempts);
1298 if (close_data->result != NULL)
1301 g_simple_async_result_set_from_error (close_data->result, error);
1303 /* this must be in an idle because the result is likely to be
1304 * intended for another thread
1306 g_simple_async_result_complete_in_idle (close_data->result);
1309 close_data_free (close_data);
1312 g_clear_error (&error);
1314 /* all messages queued for sending are discarded */
1315 g_queue_foreach (send_queue, (GFunc) message_to_write_data_free, NULL);
1316 g_queue_free (send_queue);
1318 /* all queued flushes fail */
1319 error = g_error_new (G_IO_ERROR, G_IO_ERROR_CANCELLED,
1320 _("Operation was cancelled"));
1321 flush_data_list_complete (pending_flush_attempts, error);
1322 g_list_free (pending_flush_attempts);
1323 g_clear_error (&error);
1325 _g_dbus_worker_unref (worker);
1328 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
1330 maybe_write_next_message (GDBusWorker *worker)
1332 MessageToWriteData *data;
1335 /* we mustn't try to write two things at once */
1336 g_assert (!worker->output_pending);
1338 g_mutex_lock (worker->write_lock);
1340 /* if we want to close the connection, that takes precedence */
1341 if (worker->pending_close_attempts != NULL)
1343 worker->output_pending = TRUE;
1345 g_io_stream_close_async (worker->stream, G_PRIORITY_DEFAULT,
1346 NULL, iostream_close_cb,
1347 _g_dbus_worker_ref (worker));
1352 data = g_queue_pop_head (worker->write_queue);
1355 worker->output_pending = TRUE;
1358 g_mutex_unlock (worker->write_lock);
1360 /* Note that write_lock is only used for protecting the @write_queue
1361 * and @output_pending fields of the GDBusWorker struct ... which we
1362 * need to modify from arbitrary threads in _g_dbus_worker_send_message().
1364 * Therefore, it's fine to drop it here when calling back into user
1365 * code and then writing the message out onto the GIOStream since this
1366 * function only runs on the worker thread.
1370 GDBusMessage *old_message;
1372 gsize new_blob_size;
1375 old_message = data->message;
1376 data->message = _g_dbus_worker_emit_message_about_to_be_sent (worker, data->message);
1377 if (data->message == old_message)
1379 /* filters had no effect - do nothing */
1381 else if (data->message == NULL)
1383 /* filters dropped message */
1384 g_mutex_lock (worker->write_lock);
1385 worker->output_pending = FALSE;
1386 g_mutex_unlock (worker->write_lock);
1387 message_to_write_data_free (data);
1392 /* filters altered the message -> reencode */
1394 new_blob = g_dbus_message_to_blob (data->message,
1396 worker->capabilities,
1398 if (new_blob == NULL)
1400 /* if filter make the GDBusMessage unencodeable, just complain on stderr and send
1401 * the old message instead
1403 g_warning ("Error encoding GDBusMessage with serial %d altered by filter function: %s",
1404 g_dbus_message_get_serial (data->message),
1406 g_error_free (error);
1410 g_free (data->blob);
1411 data->blob = (gchar *) new_blob;
1412 data->blob_size = new_blob_size;
1416 write_message_async (worker,
1423 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
1425 write_message_in_idle_cb (gpointer user_data)
1427 GDBusWorker *worker = user_data;
1429 /* Because this is the worker thread, we can read this struct member
1430 * without holding the lock: no other thread ever modifies it.
1432 if (!worker->output_pending)
1433 maybe_write_next_message (worker);
1439 * @write_data: (transfer full) (allow-none):
1440 * @close_data: (transfer full) (allow-none):
1442 * Can be called from any thread
1444 * write_lock is not held on entry
1445 * output_pending may be true or false
1448 schedule_write_in_worker_thread (GDBusWorker *worker,
1449 MessageToWriteData *write_data,
1450 CloseData *close_data)
1452 g_mutex_lock (worker->write_lock);
1454 if (write_data != NULL)
1455 g_queue_push_tail (worker->write_queue, write_data);
1457 if (close_data != NULL)
1458 worker->pending_close_attempts = g_list_prepend (worker->pending_close_attempts,
1461 if (!worker->output_pending)
1463 GSource *idle_source;
1464 idle_source = g_idle_source_new ();
1465 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1466 g_source_set_callback (idle_source,
1467 write_message_in_idle_cb,
1468 _g_dbus_worker_ref (worker),
1469 (GDestroyNotify) _g_dbus_worker_unref);
1470 g_source_attach (idle_source, worker->shared_thread_data->context);
1471 g_source_unref (idle_source);
1474 g_mutex_unlock (worker->write_lock);
1477 /* ---------------------------------------------------------------------------------------------------- */
1479 /* can be called from any thread - steals blob */
1481 _g_dbus_worker_send_message (GDBusWorker *worker,
1482 GDBusMessage *message,
1486 MessageToWriteData *data;
1488 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1489 g_return_if_fail (blob != NULL);
1490 g_return_if_fail (blob_len > 16);
1492 data = g_new0 (MessageToWriteData, 1);
1493 data->worker = _g_dbus_worker_ref (worker);
1494 data->message = g_object_ref (message);
1495 data->blob = blob; /* steal! */
1496 data->blob_size = blob_len;
1498 schedule_write_in_worker_thread (worker, data, NULL);
1501 /* ---------------------------------------------------------------------------------------------------- */
1504 _g_dbus_worker_new (GIOStream *stream,
1505 GDBusCapabilityFlags capabilities,
1506 gboolean initially_frozen,
1507 GDBusWorkerMessageReceivedCallback message_received_callback,
1508 GDBusWorkerMessageAboutToBeSentCallback message_about_to_be_sent_callback,
1509 GDBusWorkerDisconnectedCallback disconnected_callback,
1512 GDBusWorker *worker;
1513 GSource *idle_source;
1515 g_return_val_if_fail (G_IS_IO_STREAM (stream), NULL);
1516 g_return_val_if_fail (message_received_callback != NULL, NULL);
1517 g_return_val_if_fail (message_about_to_be_sent_callback != NULL, NULL);
1518 g_return_val_if_fail (disconnected_callback != NULL, NULL);
1520 worker = g_new0 (GDBusWorker, 1);
1521 worker->ref_count = 1;
1523 worker->read_lock = g_mutex_new ();
1524 worker->message_received_callback = message_received_callback;
1525 worker->message_about_to_be_sent_callback = message_about_to_be_sent_callback;
1526 worker->disconnected_callback = disconnected_callback;
1527 worker->user_data = user_data;
1528 worker->stream = g_object_ref (stream);
1529 worker->capabilities = capabilities;
1530 worker->cancellable = g_cancellable_new ();
1531 worker->output_pending = FALSE;
1533 worker->frozen = initially_frozen;
1534 worker->received_messages_while_frozen = g_queue_new ();
1536 worker->write_lock = g_mutex_new ();
1537 worker->write_queue = g_queue_new ();
1539 if (G_IS_SOCKET_CONNECTION (worker->stream))
1540 worker->socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream));
1542 worker->shared_thread_data = _g_dbus_shared_thread_ref ();
1545 idle_source = g_idle_source_new ();
1546 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1547 g_source_set_callback (idle_source,
1548 _g_dbus_worker_do_initial_read,
1551 g_source_attach (idle_source, worker->shared_thread_data->context);
1552 g_source_unref (idle_source);
1557 /* ---------------------------------------------------------------------------------------------------- */
1559 /* can be called from any thread
1561 * write_lock is not held on entry
1562 * output_pending may be true or false
1565 _g_dbus_worker_close (GDBusWorker *worker,
1566 GCancellable *cancellable,
1567 GSimpleAsyncResult *result)
1569 CloseData *close_data;
1571 close_data = g_slice_new0 (CloseData);
1572 close_data->worker = _g_dbus_worker_ref (worker);
1573 close_data->cancellable =
1574 (cancellable == NULL ? NULL : g_object_ref (cancellable));
1575 close_data->result = (result == NULL ? NULL : g_object_ref (result));
1577 g_cancellable_cancel (worker->cancellable);
1578 schedule_write_in_worker_thread (worker, NULL, close_data);
1581 /* This can be called from any thread - frees worker. Note that
1582 * callbacks might still happen if called from another thread than the
1583 * worker - use your own synchronization primitive in the callbacks.
1586 _g_dbus_worker_stop (GDBusWorker *worker)
1588 worker->stopped = TRUE;
1590 /* Cancel any pending operations and schedule a close of the underlying I/O
1591 * stream in the worker thread
1593 _g_dbus_worker_close (worker, NULL, NULL);
1595 /* _g_dbus_worker_close holds a ref until after an idle in the the worker
1596 * thread has run, so we no longer need to unref in an idle like in
1599 _g_dbus_worker_unref (worker);
1602 /* ---------------------------------------------------------------------------------------------------- */
1604 /* can be called from any thread (except the worker thread) - blocks
1605 * calling thread until all queued outgoing messages are written and
1606 * the transport has been flushed
1609 _g_dbus_worker_flush_sync (GDBusWorker *worker,
1610 GCancellable *cancellable,
1619 /* if the queue is empty, there's nothing to wait for */
1620 g_mutex_lock (worker->write_lock);
1621 if (g_queue_get_length (worker->write_queue) > 0)
1623 data = g_new0 (FlushData, 1);
1624 data->mutex = g_mutex_new ();
1625 data->cond = g_cond_new ();
1626 data->number_to_wait_for = worker->write_num_messages_written + g_queue_get_length (worker->write_queue);
1627 g_mutex_lock (data->mutex);
1628 worker->write_pending_flushes = g_list_prepend (worker->write_pending_flushes, data);
1630 g_mutex_unlock (worker->write_lock);
1634 g_cond_wait (data->cond, data->mutex);
1635 g_mutex_unlock (data->mutex);
1637 /* note:the element is removed from worker->write_pending_flushes in flush_cb() above */
1638 g_cond_free (data->cond);
1639 g_mutex_free (data->mutex);
1640 if (data->error != NULL)
1643 g_propagate_error (error, data->error);
1651 /* ---------------------------------------------------------------------------------------------------- */
1653 #define G_DBUS_DEBUG_AUTHENTICATION (1<<0)
1654 #define G_DBUS_DEBUG_TRANSPORT (1<<1)
1655 #define G_DBUS_DEBUG_MESSAGE (1<<2)
1656 #define G_DBUS_DEBUG_PAYLOAD (1<<3)
1657 #define G_DBUS_DEBUG_CALL (1<<4)
1658 #define G_DBUS_DEBUG_SIGNAL (1<<5)
1659 #define G_DBUS_DEBUG_INCOMING (1<<6)
1660 #define G_DBUS_DEBUG_RETURN (1<<7)
1661 #define G_DBUS_DEBUG_EMISSION (1<<8)
1662 #define G_DBUS_DEBUG_ADDRESS (1<<9)
1664 static gint _gdbus_debug_flags = 0;
1667 _g_dbus_debug_authentication (void)
1669 _g_dbus_initialize ();
1670 return (_gdbus_debug_flags & G_DBUS_DEBUG_AUTHENTICATION) != 0;
1674 _g_dbus_debug_transport (void)
1676 _g_dbus_initialize ();
1677 return (_gdbus_debug_flags & G_DBUS_DEBUG_TRANSPORT) != 0;
1681 _g_dbus_debug_message (void)
1683 _g_dbus_initialize ();
1684 return (_gdbus_debug_flags & G_DBUS_DEBUG_MESSAGE) != 0;
1688 _g_dbus_debug_payload (void)
1690 _g_dbus_initialize ();
1691 return (_gdbus_debug_flags & G_DBUS_DEBUG_PAYLOAD) != 0;
1695 _g_dbus_debug_call (void)
1697 _g_dbus_initialize ();
1698 return (_gdbus_debug_flags & G_DBUS_DEBUG_CALL) != 0;
1702 _g_dbus_debug_signal (void)
1704 _g_dbus_initialize ();
1705 return (_gdbus_debug_flags & G_DBUS_DEBUG_SIGNAL) != 0;
1709 _g_dbus_debug_incoming (void)
1711 _g_dbus_initialize ();
1712 return (_gdbus_debug_flags & G_DBUS_DEBUG_INCOMING) != 0;
1716 _g_dbus_debug_return (void)
1718 _g_dbus_initialize ();
1719 return (_gdbus_debug_flags & G_DBUS_DEBUG_RETURN) != 0;
1723 _g_dbus_debug_emission (void)
1725 _g_dbus_initialize ();
1726 return (_gdbus_debug_flags & G_DBUS_DEBUG_EMISSION) != 0;
1730 _g_dbus_debug_address (void)
1732 _g_dbus_initialize ();
1733 return (_gdbus_debug_flags & G_DBUS_DEBUG_ADDRESS) != 0;
1736 G_LOCK_DEFINE_STATIC (print_lock);
1739 _g_dbus_debug_print_lock (void)
1741 G_LOCK (print_lock);
1745 _g_dbus_debug_print_unlock (void)
1747 G_UNLOCK (print_lock);
1751 * _g_dbus_initialize:
1753 * Does various one-time init things such as
1755 * - registering the G_DBUS_ERROR error domain
1756 * - parses the G_DBUS_DEBUG environment variable
1759 _g_dbus_initialize (void)
1761 static volatile gsize initialized = 0;
1763 if (g_once_init_enter (&initialized))
1765 volatile GQuark g_dbus_error_domain;
1768 g_dbus_error_domain = G_DBUS_ERROR;
1769 (g_dbus_error_domain); /* To avoid -Wunused-but-set-variable */
1771 debug = g_getenv ("G_DBUS_DEBUG");
1774 const GDebugKey keys[] = {
1775 { "authentication", G_DBUS_DEBUG_AUTHENTICATION },
1776 { "transport", G_DBUS_DEBUG_TRANSPORT },
1777 { "message", G_DBUS_DEBUG_MESSAGE },
1778 { "payload", G_DBUS_DEBUG_PAYLOAD },
1779 { "call", G_DBUS_DEBUG_CALL },
1780 { "signal", G_DBUS_DEBUG_SIGNAL },
1781 { "incoming", G_DBUS_DEBUG_INCOMING },
1782 { "return", G_DBUS_DEBUG_RETURN },
1783 { "emission", G_DBUS_DEBUG_EMISSION },
1784 { "address", G_DBUS_DEBUG_ADDRESS }
1787 _gdbus_debug_flags = g_parse_debug_string (debug, keys, G_N_ELEMENTS (keys));
1788 if (_gdbus_debug_flags & G_DBUS_DEBUG_PAYLOAD)
1789 _gdbus_debug_flags |= G_DBUS_DEBUG_MESSAGE;
1792 g_once_init_leave (&initialized, 1);
1796 /* ---------------------------------------------------------------------------------------------------- */
1799 _g_dbus_compute_complete_signature (GDBusArgInfo **args)
1801 const GVariantType *arg_types[256];
1805 for (n = 0; args[n] != NULL; n++)
1807 /* DBus places a hard limit of 255 on signature length.
1808 * therefore number of args must be less than 256.
1812 arg_types[n] = G_VARIANT_TYPE (args[n]->signature);
1814 if G_UNLIKELY (arg_types[n] == NULL)
1820 return g_variant_type_new_tuple (arg_types, n);
1823 /* ---------------------------------------------------------------------------------------------------- */
1827 extern BOOL WINAPI ConvertSidToStringSidA (PSID Sid, LPSTR *StringSid);
1830 _g_dbus_win32_get_user_sid (void)
1834 DWORD token_information_len;
1841 h = INVALID_HANDLE_VALUE;
1843 if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &h))
1845 g_warning ("OpenProcessToken failed with error code %d", (gint) GetLastError ());
1849 /* Get length of buffer */
1850 token_information_len = 0;
1851 if (!GetTokenInformation (h, TokenUser, NULL, 0, &token_information_len))
1853 if (GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1855 g_warning ("GetTokenInformation() failed with error code %d", (gint) GetLastError ());
1859 user = g_malloc (token_information_len);
1860 if (!GetTokenInformation (h, TokenUser, user, token_information_len, &token_information_len))
1862 g_warning ("GetTokenInformation() failed with error code %d", (gint) GetLastError ());
1866 psid = user->User.Sid;
1867 if (!IsValidSid (psid))
1869 g_warning ("Invalid SID");
1873 if (!ConvertSidToStringSidA (psid, &sid))
1875 g_warning ("Invalid SID");
1879 ret = g_strdup (sid);
1884 if (h != INVALID_HANDLE_VALUE)
1890 /* ---------------------------------------------------------------------------------------------------- */
1893 _g_dbus_get_machine_id (GError **error)
1896 /* TODO: use PACKAGE_LOCALSTATEDIR ? */
1898 if (!g_file_get_contents ("/var/lib/dbus/machine-id",
1903 g_prefix_error (error, _("Unable to load /var/lib/dbus/machine-id: "));
1907 /* TODO: validate value */
1913 /* ---------------------------------------------------------------------------------------------------- */
1916 _g_dbus_enum_to_string (GType enum_type, gint value)
1920 GEnumValue *enum_value;
1922 klass = g_type_class_ref (enum_type);
1923 enum_value = g_enum_get_value (klass, value);
1924 if (enum_value != NULL)
1925 ret = g_strdup (enum_value->value_nick);
1927 ret = g_strdup_printf ("unknown (value %d)", value);
1928 g_type_class_unref (klass);
1932 /* ---------------------------------------------------------------------------------------------------- */
1935 write_message_print_transport_debug (gssize bytes_written,
1936 MessageToWriteData *data)
1938 if (G_LIKELY (!_g_dbus_debug_transport ()))
1941 _g_dbus_debug_print_lock ();
1942 g_print ("========================================================================\n"
1943 "GDBus-debug:Transport:\n"
1944 " >>>> WROTE %" G_GSIZE_FORMAT " bytes of message with serial %d and\n"
1945 " size %" G_GSIZE_FORMAT " from offset %" G_GSIZE_FORMAT " on a %s\n",
1947 g_dbus_message_get_serial (data->message),
1949 data->total_written,
1950 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream))));
1951 _g_dbus_debug_print_unlock ();
1956 /* ---------------------------------------------------------------------------------------------------- */
1959 read_message_print_transport_debug (gssize bytes_read,
1960 GDBusWorker *worker)
1964 gint32 message_length;
1966 if (G_LIKELY (!_g_dbus_debug_transport ()))
1969 size = bytes_read + worker->read_buffer_cur_size;
1973 message_length = g_dbus_message_bytes_needed ((guchar *) worker->read_buffer, size, NULL);
1976 switch (worker->read_buffer[0])
1980 serial = GUINT32_FROM_LE (((guint32 *) worker->read_buffer)[2]);
1984 serial = GUINT32_FROM_BE (((guint32 *) worker->read_buffer)[2]);
1987 /* an error will be set elsewhere if this happens */
1992 _g_dbus_debug_print_lock ();
1993 g_print ("========================================================================\n"
1994 "GDBus-debug:Transport:\n"
1995 " <<<< READ %" G_GSIZE_FORMAT " bytes of message with serial %d and\n"
1996 " size %d to offset %" G_GSIZE_FORMAT " from a %s\n",
2000 worker->read_buffer_cur_size,
2001 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_input_stream (worker->stream))));
2002 _g_dbus_debug_print_unlock ();
2007 /* ---------------------------------------------------------------------------------------------------- */
2010 _g_signal_accumulator_false_handled (GSignalInvocationHint *ihint,
2011 GValue *return_accu,
2012 const GValue *handler_return,
2015 gboolean continue_emission;
2016 gboolean signal_return;
2018 signal_return = g_value_get_boolean (handler_return);
2019 g_value_set_boolean (return_accu, signal_return);
2020 continue_emission = signal_return;
2022 return continue_emission;