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, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
28 #include "gdbusprivate.h"
29 #include "gdbusmessage.h"
30 #include "gdbuserror.h"
31 #include "gdbusintrospection.h"
32 #include "gasyncresult.h"
33 #include "gsimpleasyncresult.h"
34 #include "ginputstream.h"
35 #include "gmemoryinputstream.h"
36 #include "giostream.h"
37 #include "glib/gstdio.h"
38 #include "gsocketcontrolmessage.h"
39 #include "gsocketconnection.h"
40 #include "gsocketoutputstream.h"
44 #include "gkdbusconnection.h"
45 #include "gunixfdmessage.h"
46 #include "gunixconnection.h"
47 #include "gunixcredentialsmessage.h"
56 static gboolean _g_dbus_worker_do_initial_read (gpointer data);
58 /* ---------------------------------------------------------------------------------------------------- */
61 _g_dbus_hexdump (const gchar *data, gsize len, guint indent)
66 ret = g_string_new (NULL);
68 for (n = 0; n < len; n += 16)
70 g_string_append_printf (ret, "%*s%04x: ", indent, "", n);
72 for (m = n; m < n + 16; m++)
74 if (m > n && (m%4) == 0)
75 g_string_append_c (ret, ' ');
77 g_string_append_printf (ret, "%02x ", (guchar) data[m]);
79 g_string_append (ret, " ");
82 g_string_append (ret, " ");
84 for (m = n; m < len && m < n + 16; m++)
85 g_string_append_c (ret, g_ascii_isprint (data[m]) ? data[m] : '.');
87 g_string_append_c (ret, '\n');
90 return g_string_free (ret, FALSE);
93 /* ---------------------------------------------------------------------------------------------------- */
95 #if defined (G_OS_UNIX) && (KDBUS_TRANSPORT)
99 GCancellable *cancellable;
101 GSimpleAsyncResult *simple;
103 gboolean from_mainloop;
107 read_kdbus_data_free (ReadKdbusData *data)
109 g_object_unref (data->kdbus);
110 if (data->cancellable != NULL)
111 g_object_unref (data->cancellable);
112 g_object_unref (data->simple);
117 _g_kdbus_read_ready (GKdbus *kdbus,
118 GIOCondition condition,
121 ReadKdbusData *data = user_data;
122 GError *error = NULL;
125 result = _g_kdbus_receive (data->kdbus,
131 g_simple_async_result_set_op_res_gssize (data->simple, result);
135 g_assert (error != NULL);
136 g_simple_async_result_take_error (data->simple, error);
139 if (data->from_mainloop)
140 g_simple_async_result_complete (data->simple);
142 g_simple_async_result_complete_in_idle (data->simple);
148 _g_kdbus_read (GKdbus *kdbus,
149 GCancellable *cancellable,
150 GAsyncReadyCallback callback,
156 data = g_new0 (ReadKdbusData, 1);
157 data->kdbus = g_object_ref (kdbus);
158 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
160 data->simple = g_simple_async_result_new (G_OBJECT (kdbus),
164 g_simple_async_result_set_check_cancellable (data->simple, cancellable);
166 data->from_mainloop = TRUE;
167 source = _g_kdbus_create_source (data->kdbus,
170 g_source_set_callback (source,
171 (GSourceFunc) _g_kdbus_read_ready,
173 (GDestroyNotify) read_kdbus_data_free);
174 g_source_attach (source, g_main_context_get_thread_default ());
175 g_source_unref (source);
179 _g_kdbus_read_finish (GKdbus *kdbus,
180 GAsyncResult *result,
183 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
185 g_return_val_if_fail (G_IS_KDBUS (kdbus), -1);
186 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == _g_kdbus_read);
188 if (g_simple_async_result_propagate_error (simple, error))
191 return g_simple_async_result_get_op_res_gssize (simple);
194 #endif /* defined (G_OS_UNIX) && (KDBUS_TRANSPORT) */
196 /* Unfortunately ancillary messages are discarded when reading from a
197 * socket using the GSocketInputStream abstraction. So we provide a
198 * very GInputStream-ish API that uses GSocket in this case (very
199 * similar to GSocketInputStream).
205 GCancellable *cancellable;
210 GSocketControlMessage ***messages;
213 GSimpleAsyncResult *simple;
215 gboolean from_mainloop;
216 } ReadWithControlData;
219 read_with_control_data_free (ReadWithControlData *data)
221 g_object_unref (data->socket);
222 if (data->cancellable != NULL)
223 g_object_unref (data->cancellable);
224 g_object_unref (data->simple);
229 _g_socket_read_with_control_messages_ready (GSocket *socket,
230 GIOCondition condition,
233 ReadWithControlData *data = user_data;
239 vector.buffer = data->buffer;
240 vector.size = data->count;
241 result = g_socket_receive_message (data->socket,
252 g_simple_async_result_set_op_res_gssize (data->simple, result);
256 g_assert (error != NULL);
257 g_simple_async_result_take_error (data->simple, error);
260 if (data->from_mainloop)
261 g_simple_async_result_complete (data->simple);
263 g_simple_async_result_complete_in_idle (data->simple);
269 _g_socket_read_with_control_messages (GSocket *socket,
272 GSocketControlMessage ***messages,
275 GCancellable *cancellable,
276 GAsyncReadyCallback callback,
279 ReadWithControlData *data;
281 data = g_new0 (ReadWithControlData, 1);
282 data->socket = g_object_ref (socket);
283 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
284 data->buffer = buffer;
286 data->messages = messages;
287 data->num_messages = num_messages;
289 data->simple = g_simple_async_result_new (G_OBJECT (socket),
292 _g_socket_read_with_control_messages);
293 g_simple_async_result_set_check_cancellable (data->simple, cancellable);
295 if (!g_socket_condition_check (socket, G_IO_IN))
298 data->from_mainloop = TRUE;
299 source = g_socket_create_source (data->socket,
300 G_IO_IN | G_IO_HUP | G_IO_ERR,
302 g_source_set_callback (source,
303 (GSourceFunc) _g_socket_read_with_control_messages_ready,
305 (GDestroyNotify) read_with_control_data_free);
306 g_source_attach (source, g_main_context_get_thread_default ());
307 g_source_unref (source);
311 _g_socket_read_with_control_messages_ready (data->socket, G_IO_IN, data);
312 read_with_control_data_free (data);
317 _g_socket_read_with_control_messages_finish (GSocket *socket,
318 GAsyncResult *result,
321 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
323 g_return_val_if_fail (G_IS_SOCKET (socket), -1);
324 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == _g_socket_read_with_control_messages);
326 if (g_simple_async_result_propagate_error (simple, error))
329 return g_simple_async_result_get_op_res_gssize (simple);
332 /* ---------------------------------------------------------------------------------------------------- */
334 /* Work-around for https://bugzilla.gnome.org/show_bug.cgi?id=627724 */
336 static GPtrArray *ensured_classes = NULL;
339 ensure_type (GType gtype)
341 g_ptr_array_add (ensured_classes, g_type_class_ref (gtype));
345 release_required_types (void)
347 g_ptr_array_foreach (ensured_classes, (GFunc) g_type_class_unref, NULL);
348 g_ptr_array_unref (ensured_classes);
349 ensured_classes = NULL;
353 ensure_required_types (void)
355 g_assert (ensured_classes == NULL);
356 ensured_classes = g_ptr_array_new ();
357 ensure_type (G_TYPE_SIMPLE_ASYNC_RESULT);
358 ensure_type (G_TYPE_MEMORY_INPUT_STREAM);
360 /* ---------------------------------------------------------------------------------------------------- */
364 volatile gint refcount;
366 GMainContext *context;
371 gdbus_shared_thread_func (gpointer user_data)
373 SharedThreadData *data = user_data;
375 g_main_context_push_thread_default (data->context);
376 g_main_loop_run (data->loop);
377 g_main_context_pop_thread_default (data->context);
379 release_required_types ();
384 /* ---------------------------------------------------------------------------------------------------- */
386 static SharedThreadData *
387 _g_dbus_shared_thread_ref (void)
389 static gsize shared_thread_data = 0;
390 SharedThreadData *ret;
392 if (g_once_init_enter (&shared_thread_data))
394 SharedThreadData *data;
396 /* Work-around for https://bugzilla.gnome.org/show_bug.cgi?id=627724 */
397 ensure_required_types ();
399 data = g_new0 (SharedThreadData, 1);
402 data->context = g_main_context_new ();
403 data->loop = g_main_loop_new (data->context, FALSE);
404 data->thread = g_thread_new ("gdbus",
405 gdbus_shared_thread_func,
407 /* We can cast between gsize and gpointer safely */
408 g_once_init_leave (&shared_thread_data, (gsize) data);
411 ret = (SharedThreadData*) shared_thread_data;
412 g_atomic_int_inc (&ret->refcount);
417 _g_dbus_shared_thread_unref (SharedThreadData *data)
419 /* TODO: actually destroy the shared thread here */
421 g_assert (data != NULL);
422 if (g_atomic_int_dec_and_test (&data->refcount))
424 g_main_loop_quit (data->loop);
425 //g_thread_join (data->thread);
426 g_main_loop_unref (data->loop);
427 g_main_context_unref (data->context);
432 /* ---------------------------------------------------------------------------------------------------- */
443 volatile gint ref_count;
445 SharedThreadData *shared_thread_data;
447 /* really a boolean, but GLib 2.28 lacks atomic boolean ops */
448 volatile gint stopped;
450 /* TODO: frozen (e.g. G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING) currently
451 * only affects messages received from the other peer (since GDBusServer is the
452 * only user) - we might want it to affect messages sent to the other peer too?
455 GDBusCapabilityFlags capabilities;
456 GQueue *received_messages_while_frozen;
459 GCancellable *cancellable;
460 GDBusWorkerMessageReceivedCallback message_received_callback;
461 GDBusWorkerMessageAboutToBeSentCallback message_about_to_be_sent_callback;
462 GDBusWorkerDisconnectedCallback disconnected_callback;
465 /* if GSocket and GKdbus are NULL, stream is GSocketConnection */
467 #if defined (G_OS_UNIX) && (KDBUS_TRANSPORT)
471 /* used for reading */
474 gsize read_buffer_allocated_size;
475 gsize read_buffer_cur_size;
476 gsize read_buffer_bytes_wanted;
477 GUnixFDList *read_fd_list;
478 GSocketControlMessage **read_ancillary_messages;
479 gint read_num_ancillary_messages;
480 #if defined (G_OS_UNIX) && (KDBUS_TRANSPORT)
481 GSList *read_kdbus_msg_items;
485 /* Whether an async write, flush or close, or none of those, is pending.
486 * Only the worker thread may change its value, and only with the write_lock.
487 * Other threads may read its value when holding the write_lock.
488 * The worker thread may read its value at any time.
490 OutputPending output_pending;
491 /* used for writing */
493 /* queue of MessageToWriteData, protected by write_lock */
495 /* protected by write_lock */
496 guint64 write_num_messages_written;
497 /* number of messages we'd written out last time we flushed;
498 * protected by write_lock
500 guint64 write_num_messages_flushed;
501 /* list of FlushData, protected by write_lock */
502 GList *write_pending_flushes;
503 /* list of CloseData, protected by write_lock */
504 GList *pending_close_attempts;
505 /* no lock - only used from the worker thread */
506 gboolean close_expected;
509 static void _g_dbus_worker_unref (GDBusWorker *worker);
511 /* ---------------------------------------------------------------------------------------------------- */
517 guint64 number_to_wait_for;
521 struct _MessageToWriteData ;
522 typedef struct _MessageToWriteData MessageToWriteData;
524 static void message_to_write_data_free (MessageToWriteData *data);
526 static void read_message_print_transport_debug (gssize bytes_read,
527 GDBusWorker *worker);
529 static void write_message_print_transport_debug (gssize bytes_written,
530 MessageToWriteData *data);
534 GCancellable *cancellable;
535 GSimpleAsyncResult *result;
538 static void close_data_free (CloseData *close_data)
540 if (close_data->cancellable != NULL)
541 g_object_unref (close_data->cancellable);
543 if (close_data->result != NULL)
544 g_object_unref (close_data->result);
546 _g_dbus_worker_unref (close_data->worker);
547 g_slice_free (CloseData, close_data);
550 /* ---------------------------------------------------------------------------------------------------- */
553 _g_dbus_worker_ref (GDBusWorker *worker)
555 g_atomic_int_inc (&worker->ref_count);
560 _g_dbus_worker_unref (GDBusWorker *worker)
562 if (g_atomic_int_dec_and_test (&worker->ref_count))
564 g_assert (worker->write_pending_flushes == NULL);
566 _g_dbus_shared_thread_unref (worker->shared_thread_data);
568 g_object_unref (worker->stream);
570 g_mutex_clear (&worker->read_lock);
571 g_object_unref (worker->cancellable);
572 if (worker->read_fd_list != NULL)
573 g_object_unref (worker->read_fd_list);
575 g_queue_free_full (worker->received_messages_while_frozen, (GDestroyNotify) g_object_unref);
576 g_mutex_clear (&worker->write_lock);
577 g_queue_free_full (worker->write_queue, (GDestroyNotify) message_to_write_data_free);
578 g_free (worker->read_buffer);
585 _g_dbus_worker_emit_disconnected (GDBusWorker *worker,
586 gboolean remote_peer_vanished,
589 if (!g_atomic_int_get (&worker->stopped))
590 worker->disconnected_callback (worker, remote_peer_vanished, error, worker->user_data);
594 _g_dbus_worker_emit_message_received (GDBusWorker *worker,
595 GDBusMessage *message)
597 if (!g_atomic_int_get (&worker->stopped))
598 worker->message_received_callback (worker, message, worker->user_data);
601 static GDBusMessage *
602 _g_dbus_worker_emit_message_about_to_be_sent (GDBusWorker *worker,
603 GDBusMessage *message)
606 if (!g_atomic_int_get (&worker->stopped))
607 ret = worker->message_about_to_be_sent_callback (worker, message, worker->user_data);
613 /* can only be called from private thread with read-lock held - takes ownership of @message */
615 _g_dbus_worker_queue_or_deliver_received_message (GDBusWorker *worker,
616 GDBusMessage *message)
618 if (worker->frozen || g_queue_get_length (worker->received_messages_while_frozen) > 0)
621 g_queue_push_tail (worker->received_messages_while_frozen, message);
625 /* not frozen, nor anything in queue */
626 _g_dbus_worker_emit_message_received (worker, message);
627 g_object_unref (message);
631 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
633 unfreeze_in_idle_cb (gpointer user_data)
635 GDBusWorker *worker = user_data;
636 GDBusMessage *message;
638 g_mutex_lock (&worker->read_lock);
641 while ((message = g_queue_pop_head (worker->received_messages_while_frozen)) != NULL)
643 _g_dbus_worker_emit_message_received (worker, message);
644 g_object_unref (message);
646 worker->frozen = FALSE;
650 g_assert (g_queue_get_length (worker->received_messages_while_frozen) == 0);
652 g_mutex_unlock (&worker->read_lock);
656 /* can be called from any thread */
658 _g_dbus_worker_unfreeze (GDBusWorker *worker)
660 GSource *idle_source;
661 idle_source = g_idle_source_new ();
662 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
663 g_source_set_callback (idle_source,
665 _g_dbus_worker_ref (worker),
666 (GDestroyNotify) _g_dbus_worker_unref);
667 g_source_set_name (idle_source, "[gio] unfreeze_in_idle_cb");
668 g_source_attach (idle_source, worker->shared_thread_data->context);
669 g_source_unref (idle_source);
672 /* ---------------------------------------------------------------------------------------------------- */
674 static void _g_dbus_worker_do_read_unlocked (GDBusWorker *worker);
676 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
678 _g_dbus_worker_do_read_cb (GInputStream *input_stream,
682 GDBusWorker *worker = user_data;
686 g_mutex_lock (&worker->read_lock);
688 /* If already stopped, don't even process the reply */
689 if (g_atomic_int_get (&worker->stopped))
698 #if defined (G_OS_UNIX) && (KDBUS_TRANSPORT)
699 else if (G_IS_KDBUS_CONNECTION (worker->stream))
701 bytes_read = _g_kdbus_read_finish (worker->kdbus,
705 /* [KDBUS] Get all received items*/
706 worker->read_kdbus_msg_items = _g_kdbus_get_last_msg_items (worker->kdbus);
708 /* [KDBUS] Attach fds (if any) to worker->read_fd_list */
709 _g_kdbus_attach_fds_to_msg (worker->kdbus, &worker->read_fd_list);
711 /* [KDBUS] For KDBUS transport we read whole message at once*/
712 worker->read_buffer_bytes_wanted = bytes_read;
715 else if (worker->socket == NULL)
716 bytes_read = g_input_stream_read_finish (g_io_stream_get_input_stream (worker->stream),
720 bytes_read = _g_socket_read_with_control_messages_finish (worker->socket,
723 if (worker->read_num_ancillary_messages > 0)
726 for (n = 0; n < worker->read_num_ancillary_messages; n++)
728 GSocketControlMessage *control_message = G_SOCKET_CONTROL_MESSAGE (worker->read_ancillary_messages[n]);
734 else if (G_IS_UNIX_FD_MESSAGE (control_message))
736 GUnixFDMessage *fd_message;
740 fd_message = G_UNIX_FD_MESSAGE (control_message);
741 fds = g_unix_fd_message_steal_fds (fd_message, &num_fds);
742 if (worker->read_fd_list == NULL)
744 worker->read_fd_list = g_unix_fd_list_new_from_array (fds, num_fds);
749 for (n = 0; n < num_fds; n++)
751 /* TODO: really want a append_steal() */
752 g_unix_fd_list_append (worker->read_fd_list, fds[n], NULL);
753 (void) g_close (fds[n], NULL);
758 else if (G_IS_UNIX_CREDENTIALS_MESSAGE (control_message))
770 "Unexpected ancillary message of type %s received from peer",
771 g_type_name (G_TYPE_FROM_INSTANCE (control_message)));
772 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
773 g_error_free (error);
774 g_object_unref (control_message);
776 while (n < worker->read_num_ancillary_messages)
777 g_object_unref (worker->read_ancillary_messages[n++]);
778 g_free (worker->read_ancillary_messages);
782 g_object_unref (control_message);
784 g_free (worker->read_ancillary_messages);
787 if (bytes_read == -1)
789 if (G_UNLIKELY (_g_dbus_debug_transport ()))
791 _g_dbus_debug_print_lock ();
792 g_print ("========================================================================\n"
793 "GDBus-debug:Transport:\n"
794 " ---- READ ERROR:\n"
796 g_quark_to_string (error->domain), error->code,
798 _g_dbus_debug_print_unlock ();
801 /* Every async read that uses this callback uses worker->cancellable
802 * as its GCancellable. worker->cancellable gets cancelled if and only
803 * if the GDBusConnection tells us to close (either via
804 * _g_dbus_worker_stop, which is called on last-unref, or directly),
805 * so a cancelled read must mean our connection was closed locally.
807 * If we're closing, other errors are possible - notably,
808 * G_IO_ERROR_CLOSED can be seen if we close the stream with an async
809 * read in-flight. It seems sensible to treat all read errors during
810 * closing as an expected thing that doesn't trip exit-on-close.
812 * Because close_expected can't be set until we get into the worker
813 * thread, but the cancellable is signalled sooner (from another
814 * thread), we do still need to check the error.
816 if (worker->close_expected ||
817 g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
818 _g_dbus_worker_emit_disconnected (worker, FALSE, NULL);
820 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
822 g_error_free (error);
827 g_debug ("read %d bytes (is_closed=%d blocking=%d condition=0x%02x) stream %p, %p",
829 g_socket_is_closed (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream))),
830 g_socket_get_blocking (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream))),
831 g_socket_condition_check (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream)),
832 G_IO_IN | G_IO_OUT | G_IO_HUP),
837 /* TODO: hmm, hmm... */
843 "Underlying GIOStream returned 0 bytes on an async read");
844 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
845 g_error_free (error);
849 /* [KDBUS] don't print transport dbus debug for kdbus connection */
850 if (!G_IS_KDBUS_CONNECTION (worker->stream))
851 read_message_print_transport_debug (bytes_read, worker);
853 worker->read_buffer_cur_size += bytes_read;
854 if (worker->read_buffer_bytes_wanted == worker->read_buffer_cur_size)
856 /* OK, got what we asked for! */
857 if (worker->read_buffer_bytes_wanted == 16)
860 /* OK, got the header - determine how many more bytes are needed */
862 message_len = g_dbus_message_bytes_needed ((guchar *) worker->read_buffer,
865 if (message_len == -1)
867 g_warning ("_g_dbus_worker_do_read_cb: error determining bytes needed: %s", error->message);
868 _g_dbus_worker_emit_disconnected (worker, FALSE, error);
869 g_error_free (error);
873 worker->read_buffer_bytes_wanted = message_len;
874 _g_dbus_worker_do_read_unlocked (worker);
878 GDBusMessage *message;
881 /* TODO: use connection->priv->auth to decode the message */
886 #if defined (G_OS_UNIX) && (KDBUS_TRANSPORT)
887 else if (G_IS_KDBUS_CONNECTION (worker->stream))
889 GDBusMessageType message_type;
893 message = _g_dbus_message_new_from_kdbus_items (worker->read_kdbus_msg_items,
896 /* [KDBUS] override informations from the user header with kernel msg header */
897 sender = _g_kdbus_get_last_msg_sender (worker->kdbus);
898 g_dbus_message_set_sender (message, sender);
900 message_type = g_dbus_message_get_message_type (message);
901 if (message_type == G_DBUS_MESSAGE_TYPE_SIGNAL)
903 destination = _g_kdbus_get_last_msg_destination (worker->kdbus);
904 g_dbus_message_set_destination (message, destination);
909 g_warning ("Error decoding D-Bus (kdbus) message\n");
910 g_error_free (error);
917 message = g_dbus_message_new_from_blob ((guchar *) worker->read_buffer,
918 worker->read_buffer_cur_size,
919 worker->capabilities,
925 s = _g_dbus_hexdump (worker->read_buffer, worker->read_buffer_cur_size, 2);
926 g_warning ("Error decoding D-Bus message of %" G_GSIZE_FORMAT " bytes\n"
928 "The payload is as follows:\n"
930 worker->read_buffer_cur_size,
934 _g_dbus_worker_emit_disconnected (worker, FALSE, error);
935 g_error_free (error);
941 if (worker->read_fd_list != NULL)
943 g_dbus_message_set_unix_fd_list (message, worker->read_fd_list);
944 g_object_unref (worker->read_fd_list);
945 worker->read_fd_list = NULL;
949 if (G_UNLIKELY (_g_dbus_debug_message ()))
952 _g_dbus_debug_print_lock ();
953 g_print ("========================================================================\n"
954 "GDBus-debug:Message:\n"
955 " <<<< RECEIVED D-Bus message (%" G_GSIZE_FORMAT " bytes)\n",
956 worker->read_buffer_cur_size);
957 s = g_dbus_message_print (message, 2);
960 if (G_UNLIKELY (_g_dbus_debug_payload ()))
965 #if defined (G_OS_UNIX) && (KDBUS_TRANSPORT)
966 else if (G_IS_KDBUS_CONNECTION (worker->stream))
967 s = _g_kdbus_hexdump_all_items (worker->read_kdbus_msg_items);
970 s = _g_dbus_hexdump (worker->read_buffer, worker->read_buffer_cur_size, 2);
974 _g_dbus_debug_print_unlock ();
977 /* yay, got a message, go deliver it */
978 _g_dbus_worker_queue_or_deliver_received_message (worker, message);
980 /* start reading another message! */
981 worker->read_buffer_bytes_wanted = 0;
982 worker->read_buffer_cur_size = 0;
983 _g_dbus_worker_do_read_unlocked (worker);
988 /* didn't get all the bytes we requested - so repeat the request... */
989 _g_dbus_worker_do_read_unlocked (worker);
994 #if defined (G_OS_UNIX) && (KDBUS_TRANSPORT)
995 /* [KDBUS] release memory occupied by kdbus message */
996 if (G_IS_KDBUS_CONNECTION (worker->stream))
998 if (!_g_kdbus_is_closed (worker->kdbus))
1000 _g_kdbus_release_kmsg (worker->kdbus);
1001 worker->read_kdbus_msg_items = NULL;
1003 worker->read_buffer = NULL;
1007 g_mutex_unlock (&worker->read_lock);
1009 /* gives up the reference acquired when calling g_input_stream_read_async() */
1010 _g_dbus_worker_unref (worker);
1013 /* called in private thread shared by all GDBusConnection instances (with read-lock held) */
1015 _g_dbus_worker_do_read_unlocked (GDBusWorker *worker)
1017 /* Note that we do need to keep trying to read even if close_expected is
1018 * true, because only failing a read causes us to signal 'closed'.
1022 * For KDBUS transport we don't have to alloc buffer (worker->read_buffer)
1023 * instead of it we use kdbus memory pool. On connection stage KDBUS client
1024 * have to register a memory pool, large enough to carry all backlog of
1025 * data enqueued for the connection.
1028 #if defined (G_OS_UNIX) && (KDBUS_TRANSPORT)
1029 if (G_IS_KDBUS_CONNECTION (worker->stream))
1031 _g_kdbus_read(worker->kdbus,
1032 worker->cancellable,
1033 (GAsyncReadyCallback) _g_dbus_worker_do_read_cb,
1034 _g_dbus_worker_ref (worker));
1039 /* if bytes_wanted is zero, it means start reading a message */
1040 if (worker->read_buffer_bytes_wanted == 0)
1042 worker->read_buffer_cur_size = 0;
1043 worker->read_buffer_bytes_wanted = 16;
1046 /* ensure we have a (big enough) buffer */
1047 if (worker->read_buffer == NULL || worker->read_buffer_bytes_wanted > worker->read_buffer_allocated_size)
1049 /* TODO: 4096 is randomly chosen; might want a better chosen default minimum */
1050 worker->read_buffer_allocated_size = MAX (worker->read_buffer_bytes_wanted, 4096);
1051 worker->read_buffer = g_realloc (worker->read_buffer, worker->read_buffer_allocated_size);
1054 if (worker->socket == NULL)
1055 g_input_stream_read_async (g_io_stream_get_input_stream (worker->stream),
1056 worker->read_buffer + worker->read_buffer_cur_size,
1057 worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size,
1059 worker->cancellable,
1060 (GAsyncReadyCallback) _g_dbus_worker_do_read_cb,
1061 _g_dbus_worker_ref (worker));
1064 worker->read_ancillary_messages = NULL;
1065 worker->read_num_ancillary_messages = 0;
1066 _g_socket_read_with_control_messages (worker->socket,
1067 worker->read_buffer + worker->read_buffer_cur_size,
1068 worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size,
1069 &worker->read_ancillary_messages,
1070 &worker->read_num_ancillary_messages,
1072 worker->cancellable,
1073 (GAsyncReadyCallback) _g_dbus_worker_do_read_cb,
1074 _g_dbus_worker_ref (worker));
1078 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
1080 _g_dbus_worker_do_initial_read (gpointer data)
1082 GDBusWorker *worker = data;
1083 g_mutex_lock (&worker->read_lock);
1084 _g_dbus_worker_do_read_unlocked (worker);
1085 g_mutex_unlock (&worker->read_lock);
1089 /* ---------------------------------------------------------------------------------------------------- */
1091 struct _MessageToWriteData
1093 GDBusWorker *worker;
1094 GDBusMessage *message;
1098 gsize total_written;
1099 GSimpleAsyncResult *simple;
1104 message_to_write_data_free (MessageToWriteData *data)
1106 _g_dbus_worker_unref (data->worker);
1108 g_object_unref (data->message);
1109 g_free (data->blob);
1113 /* ---------------------------------------------------------------------------------------------------- */
1115 static void write_message_continue_writing (MessageToWriteData *data);
1117 /* called in private thread shared by all GDBusConnection instances
1119 * write-lock is not held on entry
1120 * output_pending is PENDING_WRITE on entry
1123 write_message_async_cb (GObject *source_object,
1127 MessageToWriteData *data = user_data;
1128 GSimpleAsyncResult *simple;
1129 gssize bytes_written;
1132 /* Note: we can't access data->simple after calling g_async_result_complete () because the
1133 * callback can free @data and we're not completing in idle. So use a copy of the pointer.
1135 simple = data->simple;
1138 bytes_written = g_output_stream_write_finish (G_OUTPUT_STREAM (source_object),
1141 if (bytes_written == -1)
1143 g_simple_async_result_take_error (simple, error);
1144 g_simple_async_result_complete (simple);
1145 g_object_unref (simple);
1148 g_assert (bytes_written > 0); /* zero is never returned */
1150 write_message_print_transport_debug (bytes_written, data);
1152 data->total_written += bytes_written;
1153 g_assert (data->total_written <= data->blob_size);
1154 if (data->total_written == data->blob_size)
1156 g_simple_async_result_complete (simple);
1157 g_object_unref (simple);
1161 write_message_continue_writing (data);
1167 /* called in private thread shared by all GDBusConnection instances
1169 * write-lock is not held on entry
1170 * output_pending is PENDING_WRITE on entry
1174 on_socket_ready (GSocket *socket,
1175 GIOCondition condition,
1178 MessageToWriteData *data = user_data;
1179 write_message_continue_writing (data);
1180 return FALSE; /* remove source */
1184 /* called in private thread shared by all GDBusConnection instances
1186 * write-lock is not held on entry
1187 * output_pending is PENDING_WRITE on entry
1190 write_message_continue_writing (MessageToWriteData *data)
1192 GOutputStream *ostream;
1195 GSimpleAsyncResult *simple;
1196 GUnixFDList *fd_list;
1198 /* Note: we can't access data->simple after calling g_async_result_complete () because the
1199 * callback can free @data and we're not completing in idle. So use a copy of the pointer.
1201 simple = data->simple;
1203 fd_list = g_dbus_message_get_unix_fd_list (data->message);
1205 #ifdef KDBUS_TRANSPORT
1206 if (G_IS_KDBUS_CONNECTION (data->worker->stream))
1210 data->total_written = _g_kdbus_send (data->worker,
1211 data->worker->kdbus,
1214 data->worker->cancellable,
1217 g_simple_async_result_complete (simple);
1218 g_object_unref (simple);
1221 #endif /* KDBUS_TRANSPORT */
1223 #endif /* G_OS_UNIX */
1225 ostream = g_io_stream_get_output_stream (data->worker->stream);
1227 g_assert (!g_output_stream_has_pending (ostream));
1228 g_assert_cmpint (data->total_written, <, data->blob_size);
1234 else if (G_IS_SOCKET_OUTPUT_STREAM (ostream) && data->total_written == 0)
1236 GOutputVector vector;
1237 GSocketControlMessage *control_message;
1238 gssize bytes_written;
1241 vector.buffer = data->blob;
1242 vector.size = data->blob_size;
1244 control_message = NULL;
1245 if (fd_list != NULL && g_unix_fd_list_get_length (fd_list) > 0)
1247 if (!(data->worker->capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING))
1249 g_simple_async_result_set_error (simple,
1252 "Tried sending a file descriptor but remote peer does not support this capability");
1253 g_simple_async_result_complete (simple);
1254 g_object_unref (simple);
1257 control_message = g_unix_fd_message_new_with_fd_list (fd_list);
1261 bytes_written = g_socket_send_message (data->worker->socket,
1265 control_message != NULL ? &control_message : NULL,
1266 control_message != NULL ? 1 : 0,
1268 data->worker->cancellable,
1270 if (control_message != NULL)
1271 g_object_unref (control_message);
1273 if (bytes_written == -1)
1275 /* Handle WOULD_BLOCK by waiting until there's room in the buffer */
1276 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1279 source = g_socket_create_source (data->worker->socket,
1280 G_IO_OUT | G_IO_HUP | G_IO_ERR,
1281 data->worker->cancellable);
1282 g_source_set_callback (source,
1283 (GSourceFunc) on_socket_ready,
1285 NULL); /* GDestroyNotify */
1286 g_source_attach (source, g_main_context_get_thread_default ());
1287 g_source_unref (source);
1288 g_error_free (error);
1291 g_simple_async_result_take_error (simple, error);
1292 g_simple_async_result_complete (simple);
1293 g_object_unref (simple);
1296 g_assert (bytes_written > 0); /* zero is never returned */
1298 write_message_print_transport_debug (bytes_written, data);
1300 data->total_written += bytes_written;
1301 g_assert (data->total_written <= data->blob_size);
1302 if (data->total_written == data->blob_size)
1304 g_simple_async_result_complete (simple);
1305 g_object_unref (simple);
1309 write_message_continue_writing (data);
1315 if (fd_list != NULL)
1317 g_simple_async_result_set_error (simple,
1320 "Tried sending a file descriptor on unsupported stream of type %s",
1321 g_type_name (G_TYPE_FROM_INSTANCE (ostream)));
1322 g_simple_async_result_complete (simple);
1323 g_object_unref (simple);
1328 g_output_stream_write_async (ostream,
1329 (const gchar *) data->blob + data->total_written,
1330 data->blob_size - data->total_written,
1332 data->worker->cancellable,
1333 write_message_async_cb,
1342 /* called in private thread shared by all GDBusConnection instances
1344 * write-lock is not held on entry
1345 * output_pending is PENDING_WRITE on entry
1348 write_message_async (GDBusWorker *worker,
1349 MessageToWriteData *data,
1350 GAsyncReadyCallback callback,
1353 data->simple = g_simple_async_result_new (NULL,
1356 write_message_async);
1357 data->total_written = 0;
1358 write_message_continue_writing (data);
1361 /* called in private thread shared by all GDBusConnection instances (with write-lock held) */
1363 write_message_finish (GAsyncResult *res,
1366 g_warn_if_fail (g_simple_async_result_get_source_tag (G_SIMPLE_ASYNC_RESULT (res)) == write_message_async);
1367 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error))
1372 /* ---------------------------------------------------------------------------------------------------- */
1374 static void continue_writing (GDBusWorker *worker);
1378 GDBusWorker *worker;
1383 flush_data_list_complete (const GList *flushers,
1384 const GError *error)
1388 for (l = flushers; l != NULL; l = l->next)
1390 FlushData *f = l->data;
1392 f->error = error != NULL ? g_error_copy (error) : NULL;
1394 g_mutex_lock (&f->mutex);
1395 g_cond_signal (&f->cond);
1396 g_mutex_unlock (&f->mutex);
1400 /* called in private thread shared by all GDBusConnection instances
1402 * write-lock is not held on entry
1403 * output_pending is PENDING_FLUSH on entry
1406 ostream_flush_cb (GObject *source_object,
1410 FlushAsyncData *data = user_data;
1414 g_output_stream_flush_finish (G_OUTPUT_STREAM (source_object),
1420 if (G_UNLIKELY (_g_dbus_debug_transport ()))
1422 _g_dbus_debug_print_lock ();
1423 g_print ("========================================================================\n"
1424 "GDBus-debug:Transport:\n"
1425 " ---- FLUSHED stream of type %s\n",
1426 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream))));
1427 _g_dbus_debug_print_unlock ();
1431 g_assert (data->flushers != NULL);
1432 flush_data_list_complete (data->flushers, error);
1433 g_list_free (data->flushers);
1436 g_error_free (error);
1438 /* Make sure we tell folks that we don't have additional
1440 g_mutex_lock (&data->worker->write_lock);
1441 data->worker->write_num_messages_flushed = data->worker->write_num_messages_written;
1442 g_assert (data->worker->output_pending == PENDING_FLUSH);
1443 data->worker->output_pending = PENDING_NONE;
1444 g_mutex_unlock (&data->worker->write_lock);
1446 /* OK, cool, finally kick off the next write */
1447 continue_writing (data->worker);
1449 _g_dbus_worker_unref (data->worker);
1453 /* called in private thread shared by all GDBusConnection instances
1455 * write-lock is not held on entry
1456 * output_pending is PENDING_FLUSH on entry
1459 start_flush (FlushAsyncData *data)
1461 /*[KDBUS]: TODO: to investigate */
1462 if (G_IS_KDBUS_CONNECTION (data->worker->stream))
1464 g_assert (data->flushers != NULL);
1465 flush_data_list_complete (data->flushers, NULL);
1466 g_list_free (data->flushers);
1468 g_mutex_lock (&data->worker->write_lock);
1469 data->worker->write_num_messages_flushed = data->worker->write_num_messages_written;
1470 g_assert (data->worker->output_pending == PENDING_FLUSH);
1471 data->worker->output_pending = PENDING_NONE;
1472 g_mutex_unlock (&data->worker->write_lock);
1474 /* OK, cool, finally kick off the next write */
1475 continue_writing (data->worker);
1477 _g_dbus_worker_unref (data->worker);
1482 g_output_stream_flush_async (g_io_stream_get_output_stream (data->worker->stream),
1484 data->worker->cancellable,
1490 /* called in private thread shared by all GDBusConnection instances
1492 * write-lock is held on entry
1493 * output_pending is PENDING_NONE on entry
1496 message_written_unlocked (GDBusWorker *worker,
1497 MessageToWriteData *message_data)
1499 if (G_UNLIKELY (_g_dbus_debug_message ()))
1502 _g_dbus_debug_print_lock ();
1503 g_print ("========================================================================\n"
1504 "GDBus-debug:Message:\n"
1505 " >>>> SENT D-Bus message (%" G_GSIZE_FORMAT " bytes)\n",
1506 message_data->blob_size);
1507 s = g_dbus_message_print (message_data->message, 2);
1510 if (G_UNLIKELY (_g_dbus_debug_payload ()))
1512 s = _g_dbus_hexdump (message_data->blob, message_data->blob_size, 2);
1513 g_print ("%s\n", s);
1516 _g_dbus_debug_print_unlock ();
1519 worker->write_num_messages_written += 1;
1522 /* called in private thread shared by all GDBusConnection instances
1524 * write-lock is held on entry
1525 * output_pending is PENDING_NONE on entry
1527 * Returns: non-%NULL, setting @output_pending, if we need to flush now
1529 static FlushAsyncData *
1530 prepare_flush_unlocked (GDBusWorker *worker)
1537 for (l = worker->write_pending_flushes; l != NULL; l = ll)
1539 FlushData *f = l->data;
1542 if (f->number_to_wait_for == worker->write_num_messages_written)
1544 flushers = g_list_append (flushers, f);
1545 worker->write_pending_flushes = g_list_delete_link (worker->write_pending_flushes, l);
1548 if (flushers != NULL)
1550 g_assert (worker->output_pending == PENDING_NONE);
1551 worker->output_pending = PENDING_FLUSH;
1554 if (flushers != NULL)
1556 FlushAsyncData *data;
1558 data = g_new0 (FlushAsyncData, 1);
1559 data->worker = _g_dbus_worker_ref (worker);
1560 data->flushers = flushers;
1567 /* called in private thread shared by all GDBusConnection instances
1569 * write-lock is not held on entry
1570 * output_pending is PENDING_WRITE on entry
1573 write_message_cb (GObject *source_object,
1577 MessageToWriteData *data = user_data;
1580 g_mutex_lock (&data->worker->write_lock);
1581 g_assert (data->worker->output_pending == PENDING_WRITE);
1582 data->worker->output_pending = PENDING_NONE;
1585 if (!write_message_finish (res, &error))
1587 g_mutex_unlock (&data->worker->write_lock);
1590 _g_dbus_worker_emit_disconnected (data->worker, TRUE, error);
1591 g_error_free (error);
1593 g_mutex_lock (&data->worker->write_lock);
1596 message_written_unlocked (data->worker, data);
1598 g_mutex_unlock (&data->worker->write_lock);
1600 continue_writing (data->worker);
1602 message_to_write_data_free (data);
1605 /* called in private thread shared by all GDBusConnection instances
1607 * write-lock is not held on entry
1608 * output_pending is PENDING_CLOSE on entry
1611 iostream_close_cb (GObject *source_object,
1615 GDBusWorker *worker = user_data;
1616 GError *error = NULL;
1617 GList *pending_close_attempts, *pending_flush_attempts;
1620 g_io_stream_close_finish (worker->stream, res, &error);
1622 g_mutex_lock (&worker->write_lock);
1624 pending_close_attempts = worker->pending_close_attempts;
1625 worker->pending_close_attempts = NULL;
1627 pending_flush_attempts = worker->write_pending_flushes;
1628 worker->write_pending_flushes = NULL;
1630 send_queue = worker->write_queue;
1631 worker->write_queue = g_queue_new ();
1633 g_assert (worker->output_pending == PENDING_CLOSE);
1634 worker->output_pending = PENDING_NONE;
1636 g_mutex_unlock (&worker->write_lock);
1638 while (pending_close_attempts != NULL)
1640 CloseData *close_data = pending_close_attempts->data;
1642 pending_close_attempts = g_list_delete_link (pending_close_attempts,
1643 pending_close_attempts);
1645 if (close_data->result != NULL)
1648 g_simple_async_result_set_from_error (close_data->result, error);
1650 /* this must be in an idle because the result is likely to be
1651 * intended for another thread
1653 g_simple_async_result_complete_in_idle (close_data->result);
1656 close_data_free (close_data);
1659 g_clear_error (&error);
1661 /* all messages queued for sending are discarded */
1662 g_queue_free_full (send_queue, (GDestroyNotify) message_to_write_data_free);
1663 /* all queued flushes fail */
1664 error = g_error_new (G_IO_ERROR, G_IO_ERROR_CANCELLED,
1665 _("Operation was cancelled"));
1666 flush_data_list_complete (pending_flush_attempts, error);
1667 g_list_free (pending_flush_attempts);
1668 g_clear_error (&error);
1670 _g_dbus_worker_unref (worker);
1673 /* called in private thread shared by all GDBusConnection instances
1675 * write-lock is not held on entry
1676 * output_pending must be PENDING_NONE on entry
1679 continue_writing (GDBusWorker *worker)
1681 MessageToWriteData *data;
1682 FlushAsyncData *flush_async_data;
1685 /* we mustn't try to write two things at once */
1686 g_assert (worker->output_pending == PENDING_NONE);
1688 g_mutex_lock (&worker->write_lock);
1691 flush_async_data = NULL;
1693 /* if we want to close the connection, that takes precedence */
1694 if (worker->pending_close_attempts != NULL)
1696 worker->close_expected = TRUE;
1697 worker->output_pending = PENDING_CLOSE;
1699 g_io_stream_close_async (worker->stream, G_PRIORITY_DEFAULT,
1700 NULL, iostream_close_cb,
1701 _g_dbus_worker_ref (worker));
1705 flush_async_data = prepare_flush_unlocked (worker);
1707 if (flush_async_data == NULL)
1709 data = g_queue_pop_head (worker->write_queue);
1712 worker->output_pending = PENDING_WRITE;
1716 g_mutex_unlock (&worker->write_lock);
1718 /* Note that write_lock is only used for protecting the @write_queue
1719 * and @output_pending fields of the GDBusWorker struct ... which we
1720 * need to modify from arbitrary threads in _g_dbus_worker_send_message().
1722 * Therefore, it's fine to drop it here when calling back into user
1723 * code and then writing the message out onto the GIOStream since this
1724 * function only runs on the worker thread.
1727 if (flush_async_data != NULL)
1729 start_flush (flush_async_data);
1730 g_assert (data == NULL);
1732 else if (data != NULL)
1734 GDBusMessage *old_message;
1736 gsize new_blob_size;
1739 old_message = data->message;
1740 data->message = _g_dbus_worker_emit_message_about_to_be_sent (worker, data->message);
1741 if (data->message == old_message)
1743 /* filters had no effect - do nothing */
1745 else if (data->message == NULL)
1747 /* filters dropped message */
1748 g_mutex_lock (&worker->write_lock);
1749 worker->output_pending = PENDING_NONE;
1750 g_mutex_unlock (&worker->write_lock);
1751 message_to_write_data_free (data);
1756 /* filters altered the message -> reencode */
1760 * Setting protocol version, before invoking g_dbus_message_to_blob() will
1761 * be removed after preparing new function only for kdbus transport purposes
1762 * (this function will be able to create blob directly/unconditionally in memfd
1763 * object, without making copy):
1765 * [1] https://code.google.com/p/d-bus/source/browse/TODO
1768 if (G_IS_KDBUS_CONNECTION (worker->stream))
1769 _g_dbus_message_set_protocol_ver (data->message,2);
1771 _g_dbus_message_set_protocol_ver (data->message,1);
1773 new_blob = g_dbus_message_to_blob (data->message,
1775 worker->capabilities,
1777 if (new_blob == NULL)
1779 /* if filter make the GDBusMessage unencodeable, just complain on stderr and send
1780 * the old message instead
1782 g_warning ("Error encoding GDBusMessage with serial %d altered by filter function: %s",
1783 g_dbus_message_get_serial (data->message),
1785 g_error_free (error);
1789 g_free (data->blob);
1790 data->blob = (gchar *) new_blob;
1791 data->blob_size = new_blob_size;
1795 write_message_async (worker,
1802 /* called in private thread shared by all GDBusConnection instances
1804 * write-lock is not held on entry
1805 * output_pending may be anything
1808 continue_writing_in_idle_cb (gpointer user_data)
1810 GDBusWorker *worker = user_data;
1812 /* Because this is the worker thread, we can read this struct member
1813 * without holding the lock: no other thread ever modifies it.
1815 if (worker->output_pending == PENDING_NONE)
1816 continue_writing (worker);
1822 * @write_data: (transfer full) (allow-none):
1823 * @flush_data: (transfer full) (allow-none):
1824 * @close_data: (transfer full) (allow-none):
1826 * Can be called from any thread
1828 * write_lock is held on entry
1829 * output_pending may be anything
1832 schedule_writing_unlocked (GDBusWorker *worker,
1833 MessageToWriteData *write_data,
1834 FlushData *flush_data,
1835 CloseData *close_data)
1837 if (write_data != NULL)
1838 g_queue_push_tail (worker->write_queue, write_data);
1840 if (flush_data != NULL)
1841 worker->write_pending_flushes = g_list_prepend (worker->write_pending_flushes, flush_data);
1843 if (close_data != NULL)
1844 worker->pending_close_attempts = g_list_prepend (worker->pending_close_attempts,
1847 /* If we had output pending, the next bit of output will happen
1848 * automatically when it finishes, so we only need to do this
1849 * if nothing was pending.
1851 * The idle callback will re-check that output_pending is still
1852 * PENDING_NONE, to guard against output starting before the idle.
1854 if (worker->output_pending == PENDING_NONE)
1856 GSource *idle_source;
1857 idle_source = g_idle_source_new ();
1858 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1859 g_source_set_callback (idle_source,
1860 continue_writing_in_idle_cb,
1861 _g_dbus_worker_ref (worker),
1862 (GDestroyNotify) _g_dbus_worker_unref);
1863 g_source_set_name (idle_source, "[gio] continue_writing_in_idle_cb");
1864 g_source_attach (idle_source, worker->shared_thread_data->context);
1865 g_source_unref (idle_source);
1869 /* ---------------------------------------------------------------------------------------------------- */
1871 /* can be called from any thread - steals blob
1873 * write_lock is not held on entry
1874 * output_pending may be anything
1877 _g_dbus_worker_send_message (GDBusWorker *worker,
1878 GDBusMessage *message,
1882 MessageToWriteData *data;
1884 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1885 g_return_if_fail (blob != NULL);
1886 g_return_if_fail (blob_len > 16);
1888 data = g_new0 (MessageToWriteData, 1);
1889 data->worker = _g_dbus_worker_ref (worker);
1890 data->message = g_object_ref (message);
1891 data->blob = blob; /* steal! */
1892 data->blob_size = blob_len;
1894 g_mutex_lock (&worker->write_lock);
1895 schedule_writing_unlocked (worker, data, NULL, NULL);
1896 g_mutex_unlock (&worker->write_lock);
1899 /* ---------------------------------------------------------------------------------------------------- */
1902 _g_dbus_worker_new (GIOStream *stream,
1903 GDBusCapabilityFlags capabilities,
1904 gboolean initially_frozen,
1905 GDBusWorkerMessageReceivedCallback message_received_callback,
1906 GDBusWorkerMessageAboutToBeSentCallback message_about_to_be_sent_callback,
1907 GDBusWorkerDisconnectedCallback disconnected_callback,
1910 GDBusWorker *worker;
1911 GSource *idle_source;
1913 g_return_val_if_fail (G_IS_IO_STREAM (stream), NULL);
1914 g_return_val_if_fail (message_received_callback != NULL, NULL);
1915 g_return_val_if_fail (message_about_to_be_sent_callback != NULL, NULL);
1916 g_return_val_if_fail (disconnected_callback != NULL, NULL);
1918 worker = g_new0 (GDBusWorker, 1);
1919 worker->ref_count = 1;
1921 g_mutex_init (&worker->read_lock);
1922 worker->message_received_callback = message_received_callback;
1923 worker->message_about_to_be_sent_callback = message_about_to_be_sent_callback;
1924 worker->disconnected_callback = disconnected_callback;
1925 worker->user_data = user_data;
1926 worker->stream = g_object_ref (stream);
1927 worker->capabilities = capabilities;
1928 worker->cancellable = g_cancellable_new ();
1929 worker->output_pending = PENDING_NONE;
1931 worker->frozen = initially_frozen;
1932 worker->received_messages_while_frozen = g_queue_new ();
1934 g_mutex_init (&worker->write_lock);
1935 worker->write_queue = g_queue_new ();
1937 if (G_IS_SOCKET_CONNECTION (worker->stream))
1938 worker->socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream));
1940 #if defined (G_OS_UNIX) && (KDBUS_TRANSPORT)
1941 if (G_IS_KDBUS_CONNECTION (worker->stream))
1942 worker->kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (worker->stream));
1945 worker->shared_thread_data = _g_dbus_shared_thread_ref ();
1948 idle_source = g_idle_source_new ();
1949 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1950 g_source_set_callback (idle_source,
1951 _g_dbus_worker_do_initial_read,
1952 _g_dbus_worker_ref (worker),
1953 (GDestroyNotify) _g_dbus_worker_unref);
1954 g_source_set_name (idle_source, "[gio] _g_dbus_worker_do_initial_read");
1955 g_source_attach (idle_source, worker->shared_thread_data->context);
1956 g_source_unref (idle_source);
1961 /* ---------------------------------------------------------------------------------------------------- */
1963 /* can be called from any thread
1965 * write_lock is not held on entry
1966 * output_pending may be anything
1969 _g_dbus_worker_close (GDBusWorker *worker,
1970 GCancellable *cancellable,
1971 GSimpleAsyncResult *result)
1973 CloseData *close_data;
1975 close_data = g_slice_new0 (CloseData);
1976 close_data->worker = _g_dbus_worker_ref (worker);
1977 close_data->cancellable =
1978 (cancellable == NULL ? NULL : g_object_ref (cancellable));
1979 close_data->result = (result == NULL ? NULL : g_object_ref (result));
1981 /* Don't set worker->close_expected here - we're in the wrong thread.
1982 * It'll be set before the actual close happens.
1984 g_cancellable_cancel (worker->cancellable);
1985 g_mutex_lock (&worker->write_lock);
1986 schedule_writing_unlocked (worker, NULL, NULL, close_data);
1987 g_mutex_unlock (&worker->write_lock);
1990 /* This can be called from any thread - frees worker. Note that
1991 * callbacks might still happen if called from another thread than the
1992 * worker - use your own synchronization primitive in the callbacks.
1994 * write_lock is not held on entry
1995 * output_pending may be anything
1998 _g_dbus_worker_stop (GDBusWorker *worker)
2000 g_atomic_int_set (&worker->stopped, TRUE);
2002 /* Cancel any pending operations and schedule a close of the underlying I/O
2003 * stream in the worker thread
2005 _g_dbus_worker_close (worker, NULL, NULL);
2007 /* _g_dbus_worker_close holds a ref until after an idle in the worker
2008 * thread has run, so we no longer need to unref in an idle like in
2011 _g_dbus_worker_unref (worker);
2014 /* ---------------------------------------------------------------------------------------------------- */
2016 /* can be called from any thread (except the worker thread) - blocks
2017 * calling thread until all queued outgoing messages are written and
2018 * the transport has been flushed
2020 * write_lock is not held on entry
2021 * output_pending may be anything
2024 _g_dbus_worker_flush_sync (GDBusWorker *worker,
2025 GCancellable *cancellable,
2030 guint64 pending_writes;
2035 g_mutex_lock (&worker->write_lock);
2037 /* if the queue is empty, no write is in-flight and we haven't written
2038 * anything since the last flush, then there's nothing to wait for
2040 pending_writes = g_queue_get_length (worker->write_queue);
2042 /* if a write is in-flight, we shouldn't be satisfied until the first
2043 * flush operation that follows it
2045 if (worker->output_pending == PENDING_WRITE)
2046 pending_writes += 1;
2048 if (pending_writes > 0 ||
2049 worker->write_num_messages_written != worker->write_num_messages_flushed)
2051 data = g_new0 (FlushData, 1);
2052 g_mutex_init (&data->mutex);
2053 g_cond_init (&data->cond);
2054 data->number_to_wait_for = worker->write_num_messages_written + pending_writes;
2055 g_mutex_lock (&data->mutex);
2057 schedule_writing_unlocked (worker, NULL, data, NULL);
2059 g_mutex_unlock (&worker->write_lock);
2063 g_cond_wait (&data->cond, &data->mutex);
2064 g_mutex_unlock (&data->mutex);
2066 /* note:the element is removed from worker->write_pending_flushes in flush_cb() above */
2067 g_cond_clear (&data->cond);
2068 g_mutex_clear (&data->mutex);
2069 if (data->error != NULL)
2072 g_propagate_error (error, data->error);
2080 /* ---------------------------------------------------------------------------------------------------- */
2082 #define G_DBUS_DEBUG_AUTHENTICATION (1<<0)
2083 #define G_DBUS_DEBUG_TRANSPORT (1<<1)
2084 #define G_DBUS_DEBUG_MESSAGE (1<<2)
2085 #define G_DBUS_DEBUG_PAYLOAD (1<<3)
2086 #define G_DBUS_DEBUG_CALL (1<<4)
2087 #define G_DBUS_DEBUG_SIGNAL (1<<5)
2088 #define G_DBUS_DEBUG_INCOMING (1<<6)
2089 #define G_DBUS_DEBUG_RETURN (1<<7)
2090 #define G_DBUS_DEBUG_EMISSION (1<<8)
2091 #define G_DBUS_DEBUG_ADDRESS (1<<9)
2093 static gint _gdbus_debug_flags = 0;
2096 _g_dbus_debug_authentication (void)
2098 _g_dbus_initialize ();
2099 return (_gdbus_debug_flags & G_DBUS_DEBUG_AUTHENTICATION) != 0;
2103 _g_dbus_debug_transport (void)
2105 _g_dbus_initialize ();
2106 return (_gdbus_debug_flags & G_DBUS_DEBUG_TRANSPORT) != 0;
2110 _g_dbus_debug_message (void)
2112 _g_dbus_initialize ();
2113 return (_gdbus_debug_flags & G_DBUS_DEBUG_MESSAGE) != 0;
2117 _g_dbus_debug_payload (void)
2119 _g_dbus_initialize ();
2120 return (_gdbus_debug_flags & G_DBUS_DEBUG_PAYLOAD) != 0;
2124 _g_dbus_debug_call (void)
2126 _g_dbus_initialize ();
2127 return (_gdbus_debug_flags & G_DBUS_DEBUG_CALL) != 0;
2131 _g_dbus_debug_signal (void)
2133 _g_dbus_initialize ();
2134 return (_gdbus_debug_flags & G_DBUS_DEBUG_SIGNAL) != 0;
2138 _g_dbus_debug_incoming (void)
2140 _g_dbus_initialize ();
2141 return (_gdbus_debug_flags & G_DBUS_DEBUG_INCOMING) != 0;
2145 _g_dbus_debug_return (void)
2147 _g_dbus_initialize ();
2148 return (_gdbus_debug_flags & G_DBUS_DEBUG_RETURN) != 0;
2152 _g_dbus_debug_emission (void)
2154 _g_dbus_initialize ();
2155 return (_gdbus_debug_flags & G_DBUS_DEBUG_EMISSION) != 0;
2159 _g_dbus_debug_address (void)
2161 _g_dbus_initialize ();
2162 return (_gdbus_debug_flags & G_DBUS_DEBUG_ADDRESS) != 0;
2165 G_LOCK_DEFINE_STATIC (print_lock);
2168 _g_dbus_debug_print_lock (void)
2170 G_LOCK (print_lock);
2174 _g_dbus_debug_print_unlock (void)
2176 G_UNLOCK (print_lock);
2180 * _g_dbus_initialize:
2182 * Does various one-time init things such as
2184 * - registering the G_DBUS_ERROR error domain
2185 * - parses the G_DBUS_DEBUG environment variable
2188 _g_dbus_initialize (void)
2190 static volatile gsize initialized = 0;
2192 if (g_once_init_enter (&initialized))
2194 volatile GQuark g_dbus_error_domain;
2197 g_dbus_error_domain = G_DBUS_ERROR;
2198 (g_dbus_error_domain); /* To avoid -Wunused-but-set-variable */
2200 debug = g_getenv ("G_DBUS_DEBUG");
2203 const GDebugKey keys[] = {
2204 { "authentication", G_DBUS_DEBUG_AUTHENTICATION },
2205 { "transport", G_DBUS_DEBUG_TRANSPORT },
2206 { "message", G_DBUS_DEBUG_MESSAGE },
2207 { "payload", G_DBUS_DEBUG_PAYLOAD },
2208 { "call", G_DBUS_DEBUG_CALL },
2209 { "signal", G_DBUS_DEBUG_SIGNAL },
2210 { "incoming", G_DBUS_DEBUG_INCOMING },
2211 { "return", G_DBUS_DEBUG_RETURN },
2212 { "emission", G_DBUS_DEBUG_EMISSION },
2213 { "address", G_DBUS_DEBUG_ADDRESS }
2216 _gdbus_debug_flags = g_parse_debug_string (debug, keys, G_N_ELEMENTS (keys));
2217 if (_gdbus_debug_flags & G_DBUS_DEBUG_PAYLOAD)
2218 _gdbus_debug_flags |= G_DBUS_DEBUG_MESSAGE;
2221 g_once_init_leave (&initialized, 1);
2225 /* ---------------------------------------------------------------------------------------------------- */
2228 _g_dbus_compute_complete_signature (GDBusArgInfo **args)
2230 const GVariantType *arg_types[256];
2234 for (n = 0; args[n] != NULL; n++)
2236 /* DBus places a hard limit of 255 on signature length.
2237 * therefore number of args must be less than 256.
2241 arg_types[n] = G_VARIANT_TYPE (args[n]->signature);
2243 if G_UNLIKELY (arg_types[n] == NULL)
2249 return g_variant_type_new_tuple (arg_types, n);
2252 /* ---------------------------------------------------------------------------------------------------- */
2256 extern BOOL WINAPI ConvertSidToStringSidA (PSID Sid, LPSTR *StringSid);
2259 _g_dbus_win32_get_user_sid (void)
2263 DWORD token_information_len;
2270 h = INVALID_HANDLE_VALUE;
2272 if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &h))
2274 g_warning ("OpenProcessToken failed with error code %d", (gint) GetLastError ());
2278 /* Get length of buffer */
2279 token_information_len = 0;
2280 if (!GetTokenInformation (h, TokenUser, NULL, 0, &token_information_len))
2282 if (GetLastError () != ERROR_INSUFFICIENT_BUFFER)
2284 g_warning ("GetTokenInformation() failed with error code %d", (gint) GetLastError ());
2288 user = g_malloc (token_information_len);
2289 if (!GetTokenInformation (h, TokenUser, user, token_information_len, &token_information_len))
2291 g_warning ("GetTokenInformation() failed with error code %d", (gint) GetLastError ());
2295 psid = user->User.Sid;
2296 if (!IsValidSid (psid))
2298 g_warning ("Invalid SID");
2302 if (!ConvertSidToStringSidA (psid, &sid))
2304 g_warning ("Invalid SID");
2308 ret = g_strdup (sid);
2313 if (h != INVALID_HANDLE_VALUE)
2319 /* ---------------------------------------------------------------------------------------------------- */
2322 _g_dbus_get_machine_id (GError **error)
2325 HW_PROFILE_INFOA info;
2326 char *src, *dest, *res;
2329 if (!GetCurrentHwProfileA (&info))
2331 char *message = g_win32_error_message (GetLastError ());
2335 _("Unable to get Hardware profile: %s"), message);
2340 /* Form: {12340001-4980-1920-6788-123456789012} */
2341 src = &info.szHwProfileGuid[0];
2343 res = g_malloc (32+1);
2347 for (i = 0; i < 8; i++)
2350 for (i = 0; i < 4; i++)
2353 for (i = 0; i < 4; i++)
2356 for (i = 0; i < 4; i++)
2359 for (i = 0; i < 12; i++)
2366 GError *first_error;
2367 /* TODO: use PACKAGE_LOCALSTATEDIR ? */
2370 if (!g_file_get_contents ("/var/lib/dbus/machine-id",
2374 !g_file_get_contents ("/etc/machine-id",
2379 g_propagate_prefixed_error (error, first_error,
2380 _("Unable to load /var/lib/dbus/machine-id or /etc/machine-id: "));
2384 /* ignore the error from the first try, if any */
2385 g_clear_error (&first_error);
2386 /* TODO: validate value */
2393 /* ---------------------------------------------------------------------------------------------------- */
2396 _g_dbus_enum_to_string (GType enum_type, gint value)
2400 GEnumValue *enum_value;
2402 klass = g_type_class_ref (enum_type);
2403 enum_value = g_enum_get_value (klass, value);
2404 if (enum_value != NULL)
2405 ret = g_strdup (enum_value->value_nick);
2407 ret = g_strdup_printf ("unknown (value %d)", value);
2408 g_type_class_unref (klass);
2412 /* ---------------------------------------------------------------------------------------------------- */
2415 write_message_print_transport_debug (gssize bytes_written,
2416 MessageToWriteData *data)
2418 if (G_LIKELY (!_g_dbus_debug_transport ()))
2421 _g_dbus_debug_print_lock ();
2422 g_print ("========================================================================\n"
2423 "GDBus-debug:Transport:\n"
2424 " >>>> WROTE %" G_GSSIZE_FORMAT " bytes of message with serial %d and\n"
2425 " size %" G_GSIZE_FORMAT " from offset %" G_GSIZE_FORMAT " on a %s\n",
2427 g_dbus_message_get_serial (data->message),
2429 data->total_written,
2430 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream))));
2431 _g_dbus_debug_print_unlock ();
2436 /* ---------------------------------------------------------------------------------------------------- */
2439 read_message_print_transport_debug (gssize bytes_read,
2440 GDBusWorker *worker)
2444 gint32 message_length;
2446 if (G_LIKELY (!_g_dbus_debug_transport ()))
2449 size = bytes_read + worker->read_buffer_cur_size;
2453 message_length = g_dbus_message_bytes_needed ((guchar *) worker->read_buffer, size, NULL);
2456 switch (worker->read_buffer[0])
2460 serial = GUINT32_FROM_LE (((guint32 *) worker->read_buffer)[2]);
2464 serial = GUINT32_FROM_BE (((guint32 *) worker->read_buffer)[2]);
2467 /* an error will be set elsewhere if this happens */
2472 _g_dbus_debug_print_lock ();
2473 g_print ("========================================================================\n"
2474 "GDBus-debug:Transport:\n"
2475 " <<<< READ %" G_GSSIZE_FORMAT " bytes of message with serial %d and\n"
2476 " size %d to offset %" G_GSIZE_FORMAT " from a %s\n",
2480 worker->read_buffer_cur_size,
2481 g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_input_stream (worker->stream))));
2482 _g_dbus_debug_print_unlock ();
2487 /* ---------------------------------------------------------------------------------------------------- */
2490 _g_signal_accumulator_false_handled (GSignalInvocationHint *ihint,
2491 GValue *return_accu,
2492 const GValue *handler_return,
2495 gboolean continue_emission;
2496 gboolean signal_return;
2498 signal_return = g_value_get_boolean (handler_return);
2499 g_value_set_boolean (return_accu, signal_return);
2500 continue_emission = signal_return;
2502 return continue_emission;