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 "giostream.h"
41 #include "gsocketcontrolmessage.h"
42 #include "gsocketconnection.h"
45 #include "gunixfdmessage.h"
46 #include "gunixconnection.h"
47 #include "gunixcredentialsmessage.h"
57 /* ---------------------------------------------------------------------------------------------------- */
60 _g_dbus_hexdump (const gchar *data, gsize len, guint indent)
65 ret = g_string_new (NULL);
67 for (n = 0; n < len; n += 16)
69 g_string_append_printf (ret, "%*s%04x: ", indent, "", n);
71 for (m = n; m < n + 16; m++)
73 if (m > n && (m%4) == 0)
74 g_string_append_c (ret, ' ');
76 g_string_append_printf (ret, "%02x ", (guchar) data[m]);
78 g_string_append (ret, " ");
81 g_string_append (ret, " ");
83 for (m = n; m < len && m < n + 16; m++)
84 g_string_append_c (ret, g_ascii_isprint (data[m]) ? data[m] : '.');
86 g_string_append_c (ret, '\n');
89 return g_string_free (ret, FALSE);
92 /* ---------------------------------------------------------------------------------------------------- */
94 /* Unfortunately ancillary messages are discarded when reading from a
95 * socket using the GSocketInputStream abstraction. So we provide a
96 * very GInputStream-ish API that uses GSocket in this case (very
97 * similar to GSocketInputStream).
103 GCancellable *cancellable;
108 GSocketControlMessage ***messages;
111 GSimpleAsyncResult *simple;
113 gboolean from_mainloop;
114 } ReadWithControlData;
117 read_with_control_data_free (ReadWithControlData *data)
119 g_object_unref (data->socket);
120 if (data->cancellable != NULL)
121 g_object_unref (data->cancellable);
126 _g_socket_read_with_control_messages_ready (GSocket *socket,
127 GIOCondition condition,
130 ReadWithControlData *data = user_data;
136 vector.buffer = data->buffer;
137 vector.size = data->count;
138 result = g_socket_receive_message (data->socket,
149 g_simple_async_result_set_op_res_gssize (data->simple, result);
153 g_assert (error != NULL);
154 g_simple_async_result_set_from_error (data->simple, error);
155 g_error_free (error);
158 if (data->from_mainloop)
159 g_simple_async_result_complete (data->simple);
161 g_simple_async_result_complete_in_idle (data->simple);
167 _g_socket_read_with_control_messages (GSocket *socket,
170 GSocketControlMessage ***messages,
173 GCancellable *cancellable,
174 GAsyncReadyCallback callback,
177 ReadWithControlData *data;
179 data = g_new0 (ReadWithControlData, 1);
180 data->socket = g_object_ref (socket);
181 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
182 data->buffer = buffer;
184 data->messages = messages;
185 data->num_messages = num_messages;
187 data->simple = g_simple_async_result_new (G_OBJECT (socket),
190 _g_socket_read_with_control_messages);
192 if (!g_socket_condition_check (socket, G_IO_IN))
195 data->from_mainloop = TRUE;
196 source = g_socket_create_source (data->socket,
197 G_IO_IN | G_IO_HUP | G_IO_ERR,
199 g_source_set_callback (source,
200 (GSourceFunc) _g_socket_read_with_control_messages_ready,
202 (GDestroyNotify) read_with_control_data_free);
203 g_source_attach (source, g_main_context_get_thread_default ());
204 g_source_unref (source);
208 _g_socket_read_with_control_messages_ready (data->socket, G_IO_IN, data);
209 read_with_control_data_free (data);
214 _g_socket_read_with_control_messages_finish (GSocket *socket,
215 GAsyncResult *result,
218 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
220 g_return_val_if_fail (G_IS_SOCKET (socket), -1);
221 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == _g_socket_read_with_control_messages);
223 if (g_simple_async_result_propagate_error (simple, error))
226 return g_simple_async_result_get_op_res_gssize (simple);
229 /* ---------------------------------------------------------------------------------------------------- */
231 G_LOCK_DEFINE_STATIC (shared_thread_lock);
237 GMainContext *context;
241 static SharedThreadData *shared_thread_data = NULL;
244 shared_thread_func (gpointer data)
246 g_main_context_push_thread_default (shared_thread_data->context);
247 g_main_loop_run (shared_thread_data->loop);
248 g_main_context_pop_thread_default (shared_thread_data->context);
252 typedef void (*GDBusSharedThreadFunc) (gpointer user_data);
256 GDBusSharedThreadFunc func;
262 invoke_caller (gpointer user_data)
264 CallerData *data = user_data;
265 data->func (data->user_data);
271 _g_dbus_shared_thread_ref (GDBusSharedThreadFunc func,
275 GSource *idle_source;
278 G_LOCK (shared_thread_lock);
280 if (shared_thread_data != NULL)
282 shared_thread_data->num_users += 1;
286 shared_thread_data = g_new0 (SharedThreadData, 1);
287 shared_thread_data->num_users = 1;
290 shared_thread_data->context = g_main_context_new ();
291 shared_thread_data->loop = g_main_loop_new (shared_thread_data->context, FALSE);
292 shared_thread_data->thread = g_thread_create (shared_thread_func,
296 g_assert_no_error (error);
300 data = g_new0 (CallerData, 1);
302 data->user_data = user_data;
305 idle_source = g_idle_source_new ();
306 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
307 g_source_set_callback (idle_source,
311 g_source_attach (idle_source, shared_thread_data->context);
312 g_source_unref (idle_source);
314 /* wait for the user code to run.. hmm.. probably use a condition variable instead */
320 G_UNLOCK (shared_thread_lock);
324 _g_dbus_shared_thread_unref (void)
326 /* TODO: actually destroy the shared thread here */
328 G_LOCK (shared_thread_lock);
329 g_assert (shared_thread_data != NULL);
330 shared_thread_data->num_users -= 1;
331 if (shared_thread_data->num_users == 0)
333 g_main_loop_quit (shared_thread_data->loop);
334 //g_thread_join (shared_thread_data->thread);
335 g_main_loop_unref (shared_thread_data->loop);
336 g_main_context_unref (shared_thread_data->context);
337 g_free (shared_thread_data);
338 shared_thread_data = NULL;
339 G_UNLOCK (shared_thread_lock);
343 G_UNLOCK (shared_thread_lock);
348 /* ---------------------------------------------------------------------------------------------------- */
352 volatile gint ref_count;
355 GDBusCapabilityFlags capabilities;
356 GCancellable *cancellable;
357 GDBusWorkerMessageReceivedCallback message_received_callback;
358 GDBusWorkerMessageAboutToBeSentCallback message_about_to_be_sent_callback;
359 GDBusWorkerDisconnectedCallback disconnected_callback;
364 /* if not NULL, stream is GSocketConnection */
367 /* used for reading */
370 gsize read_buffer_allocated_size;
371 gsize read_buffer_cur_size;
372 gsize read_buffer_bytes_wanted;
373 GUnixFDList *read_fd_list;
374 GSocketControlMessage **read_ancillary_messages;
375 gint read_num_ancillary_messages;
377 /* used for writing */
380 gboolean write_is_pending;
383 struct _MessageToWriteData ;
384 typedef struct _MessageToWriteData MessageToWriteData;
386 static void message_to_write_data_free (MessageToWriteData *data);
389 _g_dbus_worker_ref (GDBusWorker *worker)
391 g_atomic_int_inc (&worker->ref_count);
396 _g_dbus_worker_unref (GDBusWorker *worker)
398 if (g_atomic_int_dec_and_test (&worker->ref_count))
400 _g_dbus_shared_thread_unref ();
402 g_object_unref (worker->stream);
404 g_mutex_free (worker->read_lock);
405 g_object_unref (worker->cancellable);
406 if (worker->read_fd_list != NULL)
407 g_object_unref (worker->read_fd_list);
409 g_mutex_free (worker->write_lock);
410 g_queue_foreach (worker->write_queue,
411 (GFunc) message_to_write_data_free,
413 g_queue_free (worker->write_queue);
419 _g_dbus_worker_emit_disconnected (GDBusWorker *worker,
420 gboolean remote_peer_vanished,
423 if (!worker->stopped)
424 worker->disconnected_callback (worker, remote_peer_vanished, error, worker->user_data);
428 _g_dbus_worker_emit_message_received (GDBusWorker *worker,
429 GDBusMessage *message)
431 if (!worker->stopped)
432 worker->message_received_callback (worker, message, worker->user_data);
436 _g_dbus_worker_emit_message_about_to_be_sent (GDBusWorker *worker,
437 GDBusMessage *message)
441 if (!worker->stopped)
442 ret = worker->message_about_to_be_sent_callback (worker, message, worker->user_data);
446 static void _g_dbus_worker_do_read_unlocked (GDBusWorker *worker);
448 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
450 _g_dbus_worker_do_read_cb (GInputStream *input_stream,
454 GDBusWorker *worker = user_data;
458 g_mutex_lock (worker->read_lock);
460 /* If already stopped, don't even process the reply */
465 if (worker->socket == NULL)
466 bytes_read = g_input_stream_read_finish (g_io_stream_get_input_stream (worker->stream),
470 bytes_read = _g_socket_read_with_control_messages_finish (worker->socket,
473 if (worker->read_num_ancillary_messages > 0)
476 for (n = 0; n < worker->read_num_ancillary_messages; n++)
478 GSocketControlMessage *control_message = G_SOCKET_CONTROL_MESSAGE (worker->read_ancillary_messages[n]);
484 else if (G_IS_UNIX_FD_MESSAGE (control_message))
486 GUnixFDMessage *fd_message;
490 fd_message = G_UNIX_FD_MESSAGE (control_message);
491 fds = g_unix_fd_message_steal_fds (fd_message, &num_fds);
492 if (worker->read_fd_list == NULL)
494 worker->read_fd_list = g_unix_fd_list_new_from_array (fds, num_fds);
499 for (n = 0; n < num_fds; n++)
501 /* TODO: really want a append_steal() */
502 g_unix_fd_list_append (worker->read_fd_list, fds[n], NULL);
508 else if (G_IS_UNIX_CREDENTIALS_MESSAGE (control_message))
520 "Unexpected ancillary message of type %s received from peer",
521 g_type_name (G_TYPE_FROM_INSTANCE (control_message)));
522 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
523 g_error_free (error);
524 g_object_unref (control_message);
526 while (n < worker->read_num_ancillary_messages)
527 g_object_unref (worker->read_ancillary_messages[n++]);
528 g_free (worker->read_ancillary_messages);
532 g_object_unref (control_message);
534 g_free (worker->read_ancillary_messages);
537 if (bytes_read == -1)
539 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
540 g_error_free (error);
545 g_debug ("read %d bytes (is_closed=%d blocking=%d condition=0x%02x) stream %p, %p",
547 g_socket_is_closed (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream))),
548 g_socket_get_blocking (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream))),
549 g_socket_condition_check (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream)),
550 G_IO_IN | G_IO_OUT | G_IO_HUP),
555 /* TODO: hmm, hmm... */
561 "Underlying GIOStream returned 0 bytes on an async read");
562 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
563 g_error_free (error);
567 worker->read_buffer_cur_size += bytes_read;
568 if (worker->read_buffer_bytes_wanted == worker->read_buffer_cur_size)
570 /* OK, got what we asked for! */
571 if (worker->read_buffer_bytes_wanted == 16)
574 /* OK, got the header - determine how many more bytes are needed */
576 message_len = g_dbus_message_bytes_needed ((guchar *) worker->read_buffer,
579 if (message_len == -1)
581 g_warning ("_g_dbus_worker_do_read_cb: error determing bytes needed: %s", error->message);
582 _g_dbus_worker_emit_disconnected (worker, FALSE, error);
583 g_error_free (error);
587 worker->read_buffer_bytes_wanted = message_len;
588 _g_dbus_worker_do_read_unlocked (worker);
592 GDBusMessage *message;
595 /* TODO: use connection->priv->auth to decode the message */
597 message = g_dbus_message_new_from_blob ((guchar *) worker->read_buffer,
598 worker->read_buffer_cur_size,
599 worker->capabilities,
604 s = _g_dbus_hexdump (worker->read_buffer, worker->read_buffer_cur_size, 2);
605 g_warning ("Error decoding D-Bus message of %" G_GSIZE_FORMAT " bytes\n"
607 "The payload is as follows:\n"
609 worker->read_buffer_cur_size,
613 _g_dbus_worker_emit_disconnected (worker, FALSE, error);
614 g_error_free (error);
619 if (worker->read_fd_list != NULL)
621 g_dbus_message_set_unix_fd_list (message, worker->read_fd_list);
622 worker->read_fd_list = NULL;
626 if (G_UNLIKELY (_g_dbus_debug_message ()))
629 g_print ("========================================================================\n"
630 "GDBus-debug:Message:\n"
631 " <<<< RECEIVED D-Bus message (%" G_GSIZE_FORMAT " bytes)\n",
632 worker->read_buffer_cur_size);
633 s = g_dbus_message_print (message, 2);
636 s = _g_dbus_hexdump (worker->read_buffer, worker->read_buffer_cur_size, 2);
641 /* yay, got a message, go deliver it */
642 _g_dbus_worker_emit_message_received (worker, message);
643 g_object_unref (message);
645 /* start reading another message! */
646 worker->read_buffer_bytes_wanted = 0;
647 worker->read_buffer_cur_size = 0;
648 _g_dbus_worker_do_read_unlocked (worker);
653 /* didn't get all the bytes we requested - so repeat the request... */
654 _g_dbus_worker_do_read_unlocked (worker);
658 g_mutex_unlock (worker->read_lock);
660 /* gives up the reference acquired when calling g_input_stream_read_async() */
661 _g_dbus_worker_unref (worker);
664 /* called in private thread shared by all GDBusConnection instances (with read-lock held) */
666 _g_dbus_worker_do_read_unlocked (GDBusWorker *worker)
668 /* if bytes_wanted is zero, it means start reading a message */
669 if (worker->read_buffer_bytes_wanted == 0)
671 worker->read_buffer_cur_size = 0;
672 worker->read_buffer_bytes_wanted = 16;
675 /* ensure we have a (big enough) buffer */
676 if (worker->read_buffer == NULL || worker->read_buffer_bytes_wanted > worker->read_buffer_allocated_size)
678 /* TODO: 4096 is randomly chosen; might want a better chosen default minimum */
679 worker->read_buffer_allocated_size = MAX (worker->read_buffer_bytes_wanted, 4096);
680 worker->read_buffer = g_realloc (worker->read_buffer, worker->read_buffer_allocated_size);
683 if (worker->socket == NULL)
684 g_input_stream_read_async (g_io_stream_get_input_stream (worker->stream),
685 worker->read_buffer + worker->read_buffer_cur_size,
686 worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size,
689 (GAsyncReadyCallback) _g_dbus_worker_do_read_cb,
690 _g_dbus_worker_ref (worker));
693 worker->read_ancillary_messages = NULL;
694 worker->read_num_ancillary_messages = 0;
695 _g_socket_read_with_control_messages (worker->socket,
696 worker->read_buffer + worker->read_buffer_cur_size,
697 worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size,
698 &worker->read_ancillary_messages,
699 &worker->read_num_ancillary_messages,
702 (GAsyncReadyCallback) _g_dbus_worker_do_read_cb,
703 _g_dbus_worker_ref (worker));
707 /* called in private thread shared by all GDBusConnection instances (without read-lock held) */
709 _g_dbus_worker_do_read (GDBusWorker *worker)
711 g_mutex_lock (worker->read_lock);
712 _g_dbus_worker_do_read_unlocked (worker);
713 g_mutex_unlock (worker->read_lock);
716 /* ---------------------------------------------------------------------------------------------------- */
718 struct _MessageToWriteData
720 GDBusMessage *message;
726 message_to_write_data_free (MessageToWriteData *data)
728 g_object_unref (data->message);
733 /* ---------------------------------------------------------------------------------------------------- */
735 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
737 write_message (GDBusWorker *worker,
738 MessageToWriteData *data,
743 g_return_val_if_fail (data->blob_size > 16, FALSE);
747 /* First, the initial 16 bytes - special case UNIX sockets here
748 * since it may involve writing an ancillary message with file
753 GOutputVector vector;
754 GSocketControlMessage *message;
755 GUnixFDList *fd_list;
756 gssize bytes_written;
758 fd_list = g_dbus_message_get_unix_fd_list (data->message);
763 if (!G_IS_UNIX_CONNECTION (worker->stream))
767 G_IO_ERROR_INVALID_ARGUMENT,
768 "Tried sending a file descriptor on unsupported stream of type %s",
769 g_type_name (G_TYPE_FROM_INSTANCE (worker->stream)));
772 else if (!(worker->capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING))
774 g_set_error_literal (error,
776 G_IO_ERROR_INVALID_ARGUMENT,
777 "Tried sending a file descriptor but remote peer does not support this capability");
780 message = g_unix_fd_message_new_with_fd_list (fd_list);
783 vector.buffer = data->blob;
786 bytes_written = g_socket_send_message (worker->socket,
790 message != NULL ? &message : NULL,
791 message != NULL ? 1 : 0,
795 if (bytes_written == -1)
797 g_prefix_error (error, _("Error writing first 16 bytes of message to socket: "));
799 g_object_unref (message);
803 g_object_unref (message);
805 if (bytes_written < 16)
807 /* TODO: I think this needs to be handled ... are we guaranteed that the ancillary
810 g_assert_not_reached ();
814 /* write the first 16 bytes (guaranteed to return an error if everything can't be written) */
815 if (!g_output_stream_write_all (g_io_stream_get_output_stream (worker->stream),
816 (const gchar *) data->blob,
818 NULL, /* bytes_written */
819 worker->cancellable, /* cancellable */
824 /* Then write the rest of the message (guaranteed to return an error if everything can't be written) */
825 if (!g_output_stream_write_all (g_io_stream_get_output_stream (worker->stream),
826 (const gchar *) data->blob + 16,
827 data->blob_size - 16,
828 NULL, /* bytes_written */
829 worker->cancellable, /* cancellable */
835 if (G_UNLIKELY (_g_dbus_debug_message ()))
838 g_print ("========================================================================\n"
839 "GDBus-debug:Message:\n"
840 " >>>> SENT D-Bus message (%" G_GSIZE_FORMAT " bytes)\n",
842 s = g_dbus_message_print (data->message, 2);
845 s = _g_dbus_hexdump (data->blob, data->blob_size, 2);
854 /* ---------------------------------------------------------------------------------------------------- */
856 /* called in private thread shared by all GDBusConnection instances (without write-lock held) */
858 write_message_in_idle_cb (gpointer user_data)
860 GDBusWorker *worker = user_data;
861 gboolean more_writes_are_pending;
862 MessageToWriteData *data;
863 gboolean message_was_dropped;
866 g_mutex_lock (worker->write_lock);
867 data = g_queue_pop_head (worker->write_queue);
868 g_assert (data != NULL);
869 more_writes_are_pending = (g_queue_get_length (worker->write_queue) > 0);
870 worker->write_is_pending = more_writes_are_pending;
871 g_mutex_unlock (worker->write_lock);
873 /* Note that write_lock is only used for protecting the @write_queue
874 * and @write_is_pending fields of the GDBusWorker struct ... which we
875 * need to modify from arbitrary threads in _g_dbus_worker_send_message().
877 * Therefore, it's fine to drop it here when calling back into user
878 * code and then writing the message out onto the GIOStream since this
879 * function only runs on the worker thread.
881 message_was_dropped = _g_dbus_worker_emit_message_about_to_be_sent (worker, data->message);
882 if (G_LIKELY (!message_was_dropped))
885 if (!write_message (worker,
890 _g_dbus_worker_emit_disconnected (worker, TRUE, error);
891 g_error_free (error);
894 message_to_write_data_free (data);
896 return more_writes_are_pending;
899 /* ---------------------------------------------------------------------------------------------------- */
901 /* can be called from any thread - steals blob */
903 _g_dbus_worker_send_message (GDBusWorker *worker,
904 GDBusMessage *message,
908 MessageToWriteData *data;
910 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
911 g_return_if_fail (blob != NULL);
912 g_return_if_fail (blob_len > 16);
914 data = g_new0 (MessageToWriteData, 1);
915 data->message = g_object_ref (message);
916 data->blob = blob; /* steal! */
917 data->blob_size = blob_len;
919 g_mutex_lock (worker->write_lock);
920 g_queue_push_tail (worker->write_queue, data);
921 if (!worker->write_is_pending)
923 GSource *idle_source;
925 worker->write_is_pending = TRUE;
927 idle_source = g_idle_source_new ();
928 g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
929 g_source_set_callback (idle_source,
930 write_message_in_idle_cb,
931 _g_dbus_worker_ref (worker),
932 (GDestroyNotify) _g_dbus_worker_unref);
933 g_source_attach (idle_source, shared_thread_data->context);
934 g_source_unref (idle_source);
936 g_mutex_unlock (worker->write_lock);
939 /* ---------------------------------------------------------------------------------------------------- */
942 _g_dbus_worker_thread_begin_func (gpointer user_data)
944 GDBusWorker *worker = user_data;
946 worker->thread = g_thread_self ();
949 _g_dbus_worker_do_read (worker);
953 _g_dbus_worker_new (GIOStream *stream,
954 GDBusCapabilityFlags capabilities,
955 GDBusWorkerMessageReceivedCallback message_received_callback,
956 GDBusWorkerMessageAboutToBeSentCallback message_about_to_be_sent_callback,
957 GDBusWorkerDisconnectedCallback disconnected_callback,
962 g_return_val_if_fail (G_IS_IO_STREAM (stream), NULL);
963 g_return_val_if_fail (message_received_callback != NULL, NULL);
964 g_return_val_if_fail (message_about_to_be_sent_callback != NULL, NULL);
965 g_return_val_if_fail (disconnected_callback != NULL, NULL);
967 worker = g_new0 (GDBusWorker, 1);
968 worker->ref_count = 1;
970 worker->read_lock = g_mutex_new ();
971 worker->message_received_callback = message_received_callback;
972 worker->message_about_to_be_sent_callback = message_about_to_be_sent_callback;
973 worker->disconnected_callback = disconnected_callback;
974 worker->user_data = user_data;
975 worker->stream = g_object_ref (stream);
976 worker->capabilities = capabilities;
977 worker->cancellable = g_cancellable_new ();
979 worker->write_lock = g_mutex_new ();
980 worker->write_queue = g_queue_new ();
982 if (G_IS_SOCKET_CONNECTION (worker->stream))
983 worker->socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream));
985 _g_dbus_shared_thread_ref (_g_dbus_worker_thread_begin_func, worker);
990 /* This can be called from any thread - frees worker - guarantees no callbacks
991 * will ever be issued again
994 _g_dbus_worker_stop (GDBusWorker *worker)
996 /* If we're called in the worker thread it means we are called from
997 * a worker callback. This is fine, we just can't lock in that case since
998 * we're already holding the lock...
1000 if (g_thread_self () != worker->thread)
1001 g_mutex_lock (worker->read_lock);
1002 worker->stopped = TRUE;
1003 if (g_thread_self () != worker->thread)
1004 g_mutex_unlock (worker->read_lock);
1006 g_cancellable_cancel (worker->cancellable);
1007 _g_dbus_worker_unref (worker);
1010 #define G_DBUS_DEBUG_AUTHENTICATION (1<<0)
1011 #define G_DBUS_DEBUG_MESSAGE (1<<1)
1012 #define G_DBUS_DEBUG_ALL 0xffffffff
1013 static gint _gdbus_debug_flags = 0;
1016 _g_dbus_debug_authentication (void)
1018 _g_dbus_initialize ();
1019 return (_gdbus_debug_flags & G_DBUS_DEBUG_AUTHENTICATION) != 0;
1023 _g_dbus_debug_message (void)
1025 _g_dbus_initialize ();
1026 return (_gdbus_debug_flags & G_DBUS_DEBUG_MESSAGE) != 0;
1030 * _g_dbus_initialize:
1032 * Does various one-time init things such as
1034 * - registering the G_DBUS_ERROR error domain
1035 * - parses the G_DBUS_DEBUG environment variable
1038 _g_dbus_initialize (void)
1040 static volatile gsize initialized = 0;
1042 if (g_once_init_enter (&initialized))
1044 volatile GQuark g_dbus_error_domain;
1047 g_dbus_error_domain = G_DBUS_ERROR;
1049 debug = g_getenv ("G_DBUS_DEBUG");
1054 tokens = g_strsplit (debug, ",", 0);
1055 for (n = 0; tokens[n] != NULL; n++)
1057 if (g_strcmp0 (tokens[n], "authentication") == 0)
1058 _gdbus_debug_flags |= G_DBUS_DEBUG_AUTHENTICATION;
1059 else if (g_strcmp0 (tokens[n], "message") == 0)
1060 _gdbus_debug_flags |= G_DBUS_DEBUG_MESSAGE;
1061 else if (g_strcmp0 (tokens[n], "all") == 0)
1062 _gdbus_debug_flags |= G_DBUS_DEBUG_ALL;
1064 g_strfreev (tokens);
1067 g_once_init_leave (&initialized, 1);
1071 /* ---------------------------------------------------------------------------------------------------- */
1074 _g_dbus_compute_complete_signature (GDBusArgInfo **args)
1076 const GVariantType *arg_types[256];
1080 for (n = 0; args[n] != NULL; n++)
1082 /* DBus places a hard limit of 255 on signature length.
1083 * therefore number of args must be less than 256.
1087 arg_types[n] = G_VARIANT_TYPE (args[n]->signature);
1089 if G_UNLIKELY (arg_types[n] == NULL)
1095 return g_variant_type_new_tuple (arg_types, n);
1098 /* ---------------------------------------------------------------------------------------------------- */
1102 extern BOOL WINAPI ConvertSidToStringSidA (PSID Sid, LPSTR *StringSid);
1105 _g_dbus_win32_get_user_sid (void)
1109 DWORD token_information_len;
1116 h = INVALID_HANDLE_VALUE;
1118 if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &h))
1120 g_warning ("OpenProcessToken failed with error code %d", (gint) GetLastError ());
1124 /* Get length of buffer */
1125 token_information_len = 0;
1126 if (!GetTokenInformation (h, TokenUser, NULL, 0, &token_information_len))
1128 if (GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1130 g_warning ("GetTokenInformation() failed with error code %d", (gint) GetLastError ());
1134 user = g_malloc (token_information_len);
1135 if (!GetTokenInformation (h, TokenUser, user, token_information_len, &token_information_len))
1137 g_warning ("GetTokenInformation() failed with error code %d", (gint) GetLastError ());
1141 psid = user->User.Sid;
1142 if (!IsValidSid (psid))
1144 g_warning ("Invalid SID");
1148 if (!ConvertSidToStringSidA (psid, &sid))
1150 g_warning ("Invalid SID");
1154 ret = g_strdup (sid);
1159 if (h != INVALID_HANDLE_VALUE)
1165 /* ---------------------------------------------------------------------------------------------------- */
1167 #define __G_DBUS_PRIVATE_C__
1168 #include "gioaliasdef.c"