/src/glib/gio/gdbusprivate.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* GDBus - GLib D-Bus Library |
2 | | * |
3 | | * Copyright (C) 2008-2010 Red Hat, Inc. |
4 | | * |
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.1 of the License, or (at your option) any later version. |
9 | | * |
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. |
14 | | * |
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/>. |
17 | | * |
18 | | * Author: David Zeuthen <davidz@redhat.com> |
19 | | */ |
20 | | |
21 | | #include "config.h" |
22 | | |
23 | | #include <stdlib.h> |
24 | | #include <string.h> |
25 | | |
26 | | #include "giotypes.h" |
27 | | #include "gioenumtypes.h" |
28 | | #include "gsocket.h" |
29 | | #include "gdbusauthobserver.h" |
30 | | #include "gdbusprivate.h" |
31 | | #include "gdbusmessage.h" |
32 | | #include "gdbusconnection.h" |
33 | | #include "gdbusproxy.h" |
34 | | #include "gdbuserror.h" |
35 | | #include "gdbusintrospection.h" |
36 | | #include "gdbusdaemon.h" |
37 | | #include "giomodule-priv.h" |
38 | | #include "gtask.h" |
39 | | #include "ginputstream.h" |
40 | | #include "gmemoryinputstream.h" |
41 | | #include "giostream.h" |
42 | | #include "glib/gstdio.h" |
43 | | #include "gsocketaddress.h" |
44 | | #include "gsocketcontrolmessage.h" |
45 | | #include "gsocketconnection.h" |
46 | | #include "gsocketoutputstream.h" |
47 | | |
48 | | #ifdef G_OS_UNIX |
49 | | #include "gunixfdmessage.h" |
50 | | #include "gunixconnection.h" |
51 | | #include "gunixcredentialsmessage.h" |
52 | | #endif |
53 | | |
54 | | #ifdef G_OS_WIN32 |
55 | | #include <windows.h> |
56 | | #include <io.h> |
57 | | #include <conio.h> |
58 | | #endif |
59 | | |
60 | | #include "glibintl.h" |
61 | | |
62 | | static gboolean _g_dbus_worker_do_initial_read (gpointer data); |
63 | | static void schedule_pending_close (GDBusWorker *worker); |
64 | | |
65 | | /* ---------------------------------------------------------------------------------------------------- */ |
66 | | |
67 | | gchar * |
68 | | _g_dbus_hexdump (const gchar *data, gsize len, guint indent) |
69 | 0 | { |
70 | 0 | guint n, m; |
71 | 0 | GString *ret; |
72 | |
|
73 | 0 | ret = g_string_new (NULL); |
74 | |
|
75 | 0 | for (n = 0; n < len; n += 16) |
76 | 0 | { |
77 | 0 | g_string_append_printf (ret, "%*s%04x: ", indent, "", n); |
78 | |
|
79 | 0 | for (m = n; m < n + 16; m++) |
80 | 0 | { |
81 | 0 | if (m > n && (m%4) == 0) |
82 | 0 | g_string_append_c (ret, ' '); |
83 | 0 | if (m < len) |
84 | 0 | g_string_append_printf (ret, "%02x ", (guchar) data[m]); |
85 | 0 | else |
86 | 0 | g_string_append (ret, " "); |
87 | 0 | } |
88 | |
|
89 | 0 | g_string_append (ret, " "); |
90 | |
|
91 | 0 | for (m = n; m < len && m < n + 16; m++) |
92 | 0 | g_string_append_c (ret, g_ascii_isprint (data[m]) ? data[m] : '.'); |
93 | |
|
94 | 0 | g_string_append_c (ret, '\n'); |
95 | 0 | } |
96 | |
|
97 | 0 | return g_string_free (ret, FALSE); |
98 | 0 | } |
99 | | |
100 | | /* ---------------------------------------------------------------------------------------------------- */ |
101 | | |
102 | | /* Unfortunately ancillary messages are discarded when reading from a |
103 | | * socket using the GSocketInputStream abstraction. So we provide a |
104 | | * very GInputStream-ish API that uses GSocket in this case (very |
105 | | * similar to GSocketInputStream). |
106 | | */ |
107 | | |
108 | | typedef struct |
109 | | { |
110 | | void *buffer; |
111 | | gsize count; |
112 | | |
113 | | GSocketControlMessage ***messages; |
114 | | gint *num_messages; |
115 | | } ReadWithControlData; |
116 | | |
117 | | static void |
118 | | read_with_control_data_free (ReadWithControlData *data) |
119 | 0 | { |
120 | 0 | g_slice_free (ReadWithControlData, data); |
121 | 0 | } |
122 | | |
123 | | static gboolean |
124 | | _g_socket_read_with_control_messages_ready (GSocket *socket, |
125 | | GIOCondition condition, |
126 | | gpointer user_data) |
127 | 0 | { |
128 | 0 | GTask *task = user_data; |
129 | 0 | ReadWithControlData *data = g_task_get_task_data (task); |
130 | 0 | GError *error; |
131 | 0 | gssize result; |
132 | 0 | GInputVector vector; |
133 | |
|
134 | 0 | error = NULL; |
135 | 0 | vector.buffer = data->buffer; |
136 | 0 | vector.size = data->count; |
137 | 0 | result = g_socket_receive_message (socket, |
138 | 0 | NULL, /* address */ |
139 | 0 | &vector, |
140 | 0 | 1, |
141 | 0 | data->messages, |
142 | 0 | data->num_messages, |
143 | 0 | NULL, |
144 | 0 | g_task_get_cancellable (task), |
145 | 0 | &error); |
146 | |
|
147 | 0 | if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) |
148 | 0 | { |
149 | 0 | g_error_free (error); |
150 | 0 | return TRUE; |
151 | 0 | } |
152 | | |
153 | 0 | g_assert (result >= 0 || error != NULL); |
154 | 0 | if (result >= 0) |
155 | 0 | g_task_return_int (task, result); |
156 | 0 | else |
157 | 0 | g_task_return_error (task, error); |
158 | 0 | g_object_unref (task); |
159 | |
|
160 | 0 | return FALSE; |
161 | 0 | } |
162 | | |
163 | | static void |
164 | | _g_socket_read_with_control_messages (GSocket *socket, |
165 | | void *buffer, |
166 | | gsize count, |
167 | | GSocketControlMessage ***messages, |
168 | | gint *num_messages, |
169 | | gint io_priority, |
170 | | GCancellable *cancellable, |
171 | | GAsyncReadyCallback callback, |
172 | | gpointer user_data) |
173 | 0 | { |
174 | 0 | GTask *task; |
175 | 0 | ReadWithControlData *data; |
176 | 0 | GSource *source; |
177 | |
|
178 | 0 | data = g_slice_new0 (ReadWithControlData); |
179 | 0 | data->buffer = buffer; |
180 | 0 | data->count = count; |
181 | 0 | data->messages = messages; |
182 | 0 | data->num_messages = num_messages; |
183 | |
|
184 | 0 | task = g_task_new (socket, cancellable, callback, user_data); |
185 | 0 | g_task_set_source_tag (task, _g_socket_read_with_control_messages); |
186 | 0 | g_task_set_name (task, "[gio] D-Bus read"); |
187 | 0 | g_task_set_task_data (task, data, (GDestroyNotify) read_with_control_data_free); |
188 | |
|
189 | 0 | if (g_socket_condition_check (socket, G_IO_IN)) |
190 | 0 | { |
191 | 0 | if (!_g_socket_read_with_control_messages_ready (socket, G_IO_IN, task)) |
192 | 0 | return; |
193 | 0 | } |
194 | | |
195 | 0 | source = g_socket_create_source (socket, |
196 | 0 | G_IO_IN | G_IO_HUP | G_IO_ERR, |
197 | 0 | cancellable); |
198 | 0 | g_task_attach_source (task, source, (GSourceFunc) _g_socket_read_with_control_messages_ready); |
199 | 0 | g_source_unref (source); |
200 | 0 | } |
201 | | |
202 | | static gssize |
203 | | _g_socket_read_with_control_messages_finish (GSocket *socket, |
204 | | GAsyncResult *result, |
205 | | GError **error) |
206 | 0 | { |
207 | 0 | g_return_val_if_fail (G_IS_SOCKET (socket), -1); |
208 | 0 | g_return_val_if_fail (g_task_is_valid (result, socket), -1); |
209 | | |
210 | 0 | return g_task_propagate_int (G_TASK (result), error); |
211 | 0 | } |
212 | | |
213 | | /* ---------------------------------------------------------------------------------------------------- */ |
214 | | |
215 | | /* Work-around for https://bugzilla.gnome.org/show_bug.cgi?id=674885 |
216 | | and see also the original https://bugzilla.gnome.org/show_bug.cgi?id=627724 */ |
217 | | |
218 | | static GPtrArray *ensured_classes = NULL; |
219 | | |
220 | | static void |
221 | | ensure_type (GType gtype) |
222 | 0 | { |
223 | 0 | g_ptr_array_add (ensured_classes, g_type_class_ref (gtype)); |
224 | 0 | } |
225 | | |
226 | | static void |
227 | | release_required_types (void) |
228 | 0 | { |
229 | 0 | g_ptr_array_foreach (ensured_classes, (GFunc) g_type_class_unref, NULL); |
230 | 0 | g_ptr_array_unref (ensured_classes); |
231 | 0 | ensured_classes = NULL; |
232 | 0 | } |
233 | | |
234 | | static void |
235 | | ensure_required_types (void) |
236 | 0 | { |
237 | 0 | g_assert (ensured_classes == NULL); |
238 | 0 | ensured_classes = g_ptr_array_new (); |
239 | | /* Generally in this list, you should initialize types which are used as |
240 | | * properties first, then the class which has them. For example, GDBusProxy |
241 | | * has a type of GDBusConnection, so we initialize GDBusConnection first. |
242 | | * And because GDBusConnection has a property of type GDBusConnectionFlags, |
243 | | * we initialize that first. |
244 | | * |
245 | | * Similarly, GSocket has a type of GSocketAddress. |
246 | | * |
247 | | * We don't fill out the whole dependency tree right now because in practice |
248 | | * it tends to be just types that GDBus use that cause pain, and there |
249 | | * is work on a more general approach in https://bugzilla.gnome.org/show_bug.cgi?id=674885 |
250 | | */ |
251 | 0 | ensure_type (G_TYPE_TASK); |
252 | 0 | ensure_type (G_TYPE_MEMORY_INPUT_STREAM); |
253 | 0 | ensure_type (G_TYPE_DBUS_CONNECTION_FLAGS); |
254 | 0 | ensure_type (G_TYPE_DBUS_CAPABILITY_FLAGS); |
255 | 0 | ensure_type (G_TYPE_DBUS_AUTH_OBSERVER); |
256 | 0 | ensure_type (G_TYPE_DBUS_CONNECTION); |
257 | 0 | ensure_type (G_TYPE_DBUS_PROXY); |
258 | 0 | ensure_type (G_TYPE_SOCKET_FAMILY); |
259 | 0 | ensure_type (G_TYPE_SOCKET_TYPE); |
260 | 0 | ensure_type (G_TYPE_SOCKET_PROTOCOL); |
261 | 0 | ensure_type (G_TYPE_SOCKET_ADDRESS); |
262 | 0 | ensure_type (G_TYPE_SOCKET); |
263 | 0 | } |
264 | | /* ---------------------------------------------------------------------------------------------------- */ |
265 | | |
266 | | typedef struct |
267 | | { |
268 | | gint refcount; /* (atomic) */ |
269 | | GThread *thread; |
270 | | GMainContext *context; |
271 | | GMainLoop *loop; |
272 | | } SharedThreadData; |
273 | | |
274 | | static gpointer |
275 | | gdbus_shared_thread_func (gpointer user_data) |
276 | 0 | { |
277 | 0 | SharedThreadData *data = user_data; |
278 | |
|
279 | 0 | g_main_context_push_thread_default (data->context); |
280 | 0 | g_main_loop_run (data->loop); |
281 | 0 | g_main_context_pop_thread_default (data->context); |
282 | |
|
283 | 0 | release_required_types (); |
284 | |
|
285 | 0 | return NULL; |
286 | 0 | } |
287 | | |
288 | | /* ---------------------------------------------------------------------------------------------------- */ |
289 | | |
290 | | static SharedThreadData * |
291 | | _g_dbus_shared_thread_ref (void) |
292 | 0 | { |
293 | 0 | static gsize shared_thread_data = 0; |
294 | 0 | SharedThreadData *ret; |
295 | |
|
296 | 0 | if (g_once_init_enter (&shared_thread_data)) |
297 | 0 | { |
298 | 0 | SharedThreadData *data; |
299 | |
|
300 | 0 | data = g_new0 (SharedThreadData, 1); |
301 | 0 | data->refcount = 0; |
302 | | |
303 | 0 | data->context = g_main_context_new (); |
304 | 0 | data->loop = g_main_loop_new (data->context, FALSE); |
305 | 0 | data->thread = g_thread_new ("gdbus", |
306 | 0 | gdbus_shared_thread_func, |
307 | 0 | data); |
308 | | /* We can cast between gsize and gpointer safely */ |
309 | 0 | g_once_init_leave (&shared_thread_data, (gsize) data); |
310 | 0 | } |
311 | |
|
312 | 0 | ret = (SharedThreadData*) shared_thread_data; |
313 | 0 | g_atomic_int_inc (&ret->refcount); |
314 | 0 | return ret; |
315 | 0 | } |
316 | | |
317 | | static void |
318 | | _g_dbus_shared_thread_unref (SharedThreadData *data) |
319 | 0 | { |
320 | | /* TODO: actually destroy the shared thread here */ |
321 | | #if 0 |
322 | | g_assert (data != NULL); |
323 | | if (g_atomic_int_dec_and_test (&data->refcount)) |
324 | | { |
325 | | g_main_loop_quit (data->loop); |
326 | | //g_thread_join (data->thread); |
327 | | g_main_loop_unref (data->loop); |
328 | | g_main_context_unref (data->context); |
329 | | } |
330 | | #endif |
331 | 0 | } |
332 | | |
333 | | /* ---------------------------------------------------------------------------------------------------- */ |
334 | | |
335 | | typedef enum { |
336 | | PENDING_NONE = 0, |
337 | | PENDING_WRITE, |
338 | | PENDING_FLUSH, |
339 | | PENDING_CLOSE |
340 | | } OutputPending; |
341 | | |
342 | | struct GDBusWorker |
343 | | { |
344 | | gint ref_count; /* (atomic) */ |
345 | | |
346 | | SharedThreadData *shared_thread_data; |
347 | | |
348 | | /* really a boolean, but GLib 2.28 lacks atomic boolean ops */ |
349 | | gint stopped; /* (atomic) */ |
350 | | |
351 | | /* TODO: frozen (e.g. G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING) currently |
352 | | * only affects messages received from the other peer (since GDBusServer is the |
353 | | * only user) - we might want it to affect messages sent to the other peer too? |
354 | | */ |
355 | | gboolean frozen; |
356 | | GDBusCapabilityFlags capabilities; |
357 | | GQueue *received_messages_while_frozen; |
358 | | |
359 | | GIOStream *stream; |
360 | | GCancellable *cancellable; |
361 | | GDBusWorkerMessageReceivedCallback message_received_callback; |
362 | | GDBusWorkerMessageAboutToBeSentCallback message_about_to_be_sent_callback; |
363 | | GDBusWorkerDisconnectedCallback disconnected_callback; |
364 | | gpointer user_data; |
365 | | |
366 | | /* if not NULL, stream is GSocketConnection */ |
367 | | GSocket *socket; |
368 | | |
369 | | /* used for reading */ |
370 | | GMutex read_lock; |
371 | | gchar *read_buffer; |
372 | | gsize read_buffer_allocated_size; |
373 | | gsize read_buffer_cur_size; |
374 | | gsize read_buffer_bytes_wanted; |
375 | | GUnixFDList *read_fd_list; |
376 | | GSocketControlMessage **read_ancillary_messages; |
377 | | gint read_num_ancillary_messages; |
378 | | |
379 | | /* Whether an async write, flush or close, or none of those, is pending. |
380 | | * Only the worker thread may change its value, and only with the write_lock. |
381 | | * Other threads may read its value when holding the write_lock. |
382 | | * The worker thread may read its value at any time. |
383 | | */ |
384 | | OutputPending output_pending; |
385 | | /* used for writing */ |
386 | | GMutex write_lock; |
387 | | /* queue of MessageToWriteData, protected by write_lock */ |
388 | | GQueue *write_queue; |
389 | | /* protected by write_lock */ |
390 | | guint64 write_num_messages_written; |
391 | | /* number of messages we'd written out last time we flushed; |
392 | | * protected by write_lock |
393 | | */ |
394 | | guint64 write_num_messages_flushed; |
395 | | /* list of FlushData, protected by write_lock */ |
396 | | GList *write_pending_flushes; |
397 | | /* list of CloseData, protected by write_lock */ |
398 | | GList *pending_close_attempts; |
399 | | /* no lock - only used from the worker thread */ |
400 | | gboolean close_expected; |
401 | | }; |
402 | | |
403 | | static void _g_dbus_worker_unref (GDBusWorker *worker); |
404 | | |
405 | | /* ---------------------------------------------------------------------------------------------------- */ |
406 | | |
407 | | typedef struct |
408 | | { |
409 | | GMutex mutex; |
410 | | GCond cond; |
411 | | guint64 number_to_wait_for; |
412 | | gboolean finished; |
413 | | GError *error; |
414 | | } FlushData; |
415 | | |
416 | | struct _MessageToWriteData ; |
417 | | typedef struct _MessageToWriteData MessageToWriteData; |
418 | | |
419 | | static void message_to_write_data_free (MessageToWriteData *data); |
420 | | |
421 | | static void read_message_print_transport_debug (gssize bytes_read, |
422 | | GDBusWorker *worker); |
423 | | |
424 | | static void write_message_print_transport_debug (gssize bytes_written, |
425 | | MessageToWriteData *data); |
426 | | |
427 | | typedef struct { |
428 | | GDBusWorker *worker; |
429 | | GTask *task; |
430 | | } CloseData; |
431 | | |
432 | | static void close_data_free (CloseData *close_data) |
433 | 0 | { |
434 | 0 | g_clear_object (&close_data->task); |
435 | |
|
436 | 0 | _g_dbus_worker_unref (close_data->worker); |
437 | 0 | g_slice_free (CloseData, close_data); |
438 | 0 | } |
439 | | |
440 | | /* ---------------------------------------------------------------------------------------------------- */ |
441 | | |
442 | | static GDBusWorker * |
443 | | _g_dbus_worker_ref (GDBusWorker *worker) |
444 | 0 | { |
445 | 0 | g_atomic_int_inc (&worker->ref_count); |
446 | 0 | return worker; |
447 | 0 | } |
448 | | |
449 | | static void |
450 | | _g_dbus_worker_unref (GDBusWorker *worker) |
451 | 0 | { |
452 | 0 | if (g_atomic_int_dec_and_test (&worker->ref_count)) |
453 | 0 | { |
454 | 0 | g_assert (worker->write_pending_flushes == NULL); |
455 | | |
456 | 0 | _g_dbus_shared_thread_unref (worker->shared_thread_data); |
457 | |
|
458 | 0 | g_object_unref (worker->stream); |
459 | |
|
460 | 0 | g_mutex_clear (&worker->read_lock); |
461 | 0 | g_object_unref (worker->cancellable); |
462 | 0 | if (worker->read_fd_list != NULL) |
463 | 0 | g_object_unref (worker->read_fd_list); |
464 | |
|
465 | 0 | g_queue_free_full (worker->received_messages_while_frozen, (GDestroyNotify) g_object_unref); |
466 | 0 | g_mutex_clear (&worker->write_lock); |
467 | 0 | g_queue_free_full (worker->write_queue, (GDestroyNotify) message_to_write_data_free); |
468 | 0 | g_free (worker->read_buffer); |
469 | |
|
470 | 0 | g_free (worker); |
471 | 0 | } |
472 | 0 | } |
473 | | |
474 | | static void |
475 | | _g_dbus_worker_emit_disconnected (GDBusWorker *worker, |
476 | | gboolean remote_peer_vanished, |
477 | | GError *error) |
478 | 0 | { |
479 | 0 | if (!g_atomic_int_get (&worker->stopped)) |
480 | 0 | worker->disconnected_callback (worker, remote_peer_vanished, error, worker->user_data); |
481 | 0 | } |
482 | | |
483 | | static void |
484 | | _g_dbus_worker_emit_message_received (GDBusWorker *worker, |
485 | | GDBusMessage *message) |
486 | 0 | { |
487 | 0 | if (!g_atomic_int_get (&worker->stopped)) |
488 | 0 | worker->message_received_callback (worker, message, worker->user_data); |
489 | 0 | } |
490 | | |
491 | | static GDBusMessage * |
492 | | _g_dbus_worker_emit_message_about_to_be_sent (GDBusWorker *worker, |
493 | | GDBusMessage *message) |
494 | 0 | { |
495 | 0 | GDBusMessage *ret; |
496 | 0 | if (!g_atomic_int_get (&worker->stopped)) |
497 | 0 | ret = worker->message_about_to_be_sent_callback (worker, g_steal_pointer (&message), worker->user_data); |
498 | 0 | else |
499 | 0 | ret = g_steal_pointer (&message); |
500 | 0 | return ret; |
501 | 0 | } |
502 | | |
503 | | /* can only be called from private thread with read-lock held - takes ownership of @message */ |
504 | | static void |
505 | | _g_dbus_worker_queue_or_deliver_received_message (GDBusWorker *worker, |
506 | | GDBusMessage *message) |
507 | 0 | { |
508 | 0 | if (worker->frozen || g_queue_get_length (worker->received_messages_while_frozen) > 0) |
509 | 0 | { |
510 | | /* queue up */ |
511 | 0 | g_queue_push_tail (worker->received_messages_while_frozen, g_steal_pointer (&message)); |
512 | 0 | } |
513 | 0 | else |
514 | 0 | { |
515 | | /* not frozen, nor anything in queue */ |
516 | 0 | _g_dbus_worker_emit_message_received (worker, message); |
517 | 0 | g_clear_object (&message); |
518 | 0 | } |
519 | 0 | } |
520 | | |
521 | | /* called in private thread shared by all GDBusConnection instances (without read-lock held) */ |
522 | | static gboolean |
523 | | unfreeze_in_idle_cb (gpointer user_data) |
524 | 0 | { |
525 | 0 | GDBusWorker *worker = user_data; |
526 | 0 | GDBusMessage *message; |
527 | |
|
528 | 0 | g_mutex_lock (&worker->read_lock); |
529 | 0 | if (worker->frozen) |
530 | 0 | { |
531 | 0 | while ((message = g_queue_pop_head (worker->received_messages_while_frozen)) != NULL) |
532 | 0 | { |
533 | 0 | _g_dbus_worker_emit_message_received (worker, message); |
534 | 0 | g_clear_object (&message); |
535 | 0 | } |
536 | 0 | worker->frozen = FALSE; |
537 | 0 | } |
538 | 0 | else |
539 | 0 | { |
540 | 0 | g_assert (g_queue_get_length (worker->received_messages_while_frozen) == 0); |
541 | 0 | } |
542 | 0 | g_mutex_unlock (&worker->read_lock); |
543 | 0 | return FALSE; |
544 | 0 | } |
545 | | |
546 | | /* can be called from any thread */ |
547 | | void |
548 | | _g_dbus_worker_unfreeze (GDBusWorker *worker) |
549 | 0 | { |
550 | 0 | GSource *idle_source; |
551 | 0 | idle_source = g_idle_source_new (); |
552 | 0 | g_source_set_priority (idle_source, G_PRIORITY_DEFAULT); |
553 | 0 | g_source_set_callback (idle_source, |
554 | 0 | unfreeze_in_idle_cb, |
555 | 0 | _g_dbus_worker_ref (worker), |
556 | 0 | (GDestroyNotify) _g_dbus_worker_unref); |
557 | 0 | g_source_set_name (idle_source, "[gio] unfreeze_in_idle_cb"); |
558 | 0 | g_source_attach (idle_source, worker->shared_thread_data->context); |
559 | 0 | g_source_unref (idle_source); |
560 | 0 | } |
561 | | |
562 | | /* ---------------------------------------------------------------------------------------------------- */ |
563 | | |
564 | | static void _g_dbus_worker_do_read_unlocked (GDBusWorker *worker); |
565 | | |
566 | | /* called in private thread shared by all GDBusConnection instances (without read-lock held) */ |
567 | | static void |
568 | | _g_dbus_worker_do_read_cb (GInputStream *input_stream, |
569 | | GAsyncResult *res, |
570 | | gpointer user_data) |
571 | 0 | { |
572 | 0 | GDBusWorker *worker = user_data; |
573 | 0 | GError *error; |
574 | 0 | gssize bytes_read; |
575 | |
|
576 | 0 | g_mutex_lock (&worker->read_lock); |
577 | | |
578 | | /* If already stopped, don't even process the reply */ |
579 | 0 | if (g_atomic_int_get (&worker->stopped)) |
580 | 0 | goto out; |
581 | | |
582 | 0 | error = NULL; |
583 | 0 | if (worker->socket == NULL) |
584 | 0 | bytes_read = g_input_stream_read_finish (g_io_stream_get_input_stream (worker->stream), |
585 | 0 | res, |
586 | 0 | &error); |
587 | 0 | else |
588 | 0 | bytes_read = _g_socket_read_with_control_messages_finish (worker->socket, |
589 | 0 | res, |
590 | 0 | &error); |
591 | 0 | if (worker->read_num_ancillary_messages > 0) |
592 | 0 | { |
593 | 0 | gint n; |
594 | 0 | for (n = 0; n < worker->read_num_ancillary_messages; n++) |
595 | 0 | { |
596 | 0 | GSocketControlMessage *control_message = G_SOCKET_CONTROL_MESSAGE (worker->read_ancillary_messages[n]); |
597 | |
|
598 | 0 | if (FALSE) |
599 | 0 | { |
600 | 0 | } |
601 | 0 | #ifdef G_OS_UNIX |
602 | 0 | else if (G_IS_UNIX_FD_MESSAGE (control_message)) |
603 | 0 | { |
604 | 0 | GUnixFDMessage *fd_message; |
605 | 0 | gint *fds; |
606 | 0 | gint num_fds; |
607 | |
|
608 | 0 | fd_message = G_UNIX_FD_MESSAGE (control_message); |
609 | 0 | fds = g_unix_fd_message_steal_fds (fd_message, &num_fds); |
610 | 0 | if (worker->read_fd_list == NULL) |
611 | 0 | { |
612 | 0 | worker->read_fd_list = g_unix_fd_list_new_from_array (fds, num_fds); |
613 | 0 | } |
614 | 0 | else |
615 | 0 | { |
616 | 0 | gint n; |
617 | 0 | for (n = 0; n < num_fds; n++) |
618 | 0 | { |
619 | | /* TODO: really want a append_steal() */ |
620 | 0 | g_unix_fd_list_append (worker->read_fd_list, fds[n], NULL); |
621 | 0 | (void) g_close (fds[n], NULL); |
622 | 0 | } |
623 | 0 | } |
624 | 0 | g_free (fds); |
625 | 0 | } |
626 | 0 | else if (G_IS_UNIX_CREDENTIALS_MESSAGE (control_message)) |
627 | 0 | { |
628 | | /* do nothing */ |
629 | 0 | } |
630 | 0 | #endif |
631 | 0 | else |
632 | 0 | { |
633 | 0 | if (error == NULL) |
634 | 0 | { |
635 | 0 | g_set_error (&error, |
636 | 0 | G_IO_ERROR, |
637 | 0 | G_IO_ERROR_FAILED, |
638 | 0 | "Unexpected ancillary message of type %s received from peer", |
639 | 0 | g_type_name (G_TYPE_FROM_INSTANCE (control_message))); |
640 | 0 | _g_dbus_worker_emit_disconnected (worker, TRUE, error); |
641 | 0 | g_error_free (error); |
642 | 0 | g_object_unref (control_message); |
643 | 0 | n++; |
644 | 0 | while (n < worker->read_num_ancillary_messages) |
645 | 0 | g_object_unref (worker->read_ancillary_messages[n++]); |
646 | 0 | g_free (worker->read_ancillary_messages); |
647 | 0 | goto out; |
648 | 0 | } |
649 | 0 | } |
650 | 0 | g_object_unref (control_message); |
651 | 0 | } |
652 | 0 | g_free (worker->read_ancillary_messages); |
653 | 0 | } |
654 | | |
655 | 0 | if (bytes_read == -1) |
656 | 0 | { |
657 | 0 | if (G_UNLIKELY (_g_dbus_debug_transport ())) |
658 | 0 | { |
659 | 0 | _g_dbus_debug_print_lock (); |
660 | 0 | g_print ("========================================================================\n" |
661 | 0 | "GDBus-debug:Transport:\n" |
662 | 0 | " ---- READ ERROR on stream of type %s:\n" |
663 | 0 | " ---- %s %d: %s\n", |
664 | 0 | g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_input_stream (worker->stream))), |
665 | 0 | g_quark_to_string (error->domain), error->code, |
666 | 0 | error->message); |
667 | 0 | _g_dbus_debug_print_unlock (); |
668 | 0 | } |
669 | | |
670 | | /* Every async read that uses this callback uses worker->cancellable |
671 | | * as its GCancellable. worker->cancellable gets cancelled if and only |
672 | | * if the GDBusConnection tells us to close (either via |
673 | | * _g_dbus_worker_stop, which is called on last-unref, or directly), |
674 | | * so a cancelled read must mean our connection was closed locally. |
675 | | * |
676 | | * If we're closing, other errors are possible - notably, |
677 | | * G_IO_ERROR_CLOSED can be seen if we close the stream with an async |
678 | | * read in-flight. It seems sensible to treat all read errors during |
679 | | * closing as an expected thing that doesn't trip exit-on-close. |
680 | | * |
681 | | * Because close_expected can't be set until we get into the worker |
682 | | * thread, but the cancellable is signalled sooner (from another |
683 | | * thread), we do still need to check the error. |
684 | | */ |
685 | 0 | if (worker->close_expected || |
686 | 0 | g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) |
687 | 0 | _g_dbus_worker_emit_disconnected (worker, FALSE, NULL); |
688 | 0 | else |
689 | 0 | _g_dbus_worker_emit_disconnected (worker, TRUE, error); |
690 | |
|
691 | 0 | g_error_free (error); |
692 | 0 | goto out; |
693 | 0 | } |
694 | | |
695 | | #if 0 |
696 | | g_debug ("read %d bytes (is_closed=%d blocking=%d condition=0x%02x) stream %p, %p", |
697 | | (gint) bytes_read, |
698 | | g_socket_is_closed (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream))), |
699 | | g_socket_get_blocking (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream))), |
700 | | g_socket_condition_check (g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream)), |
701 | | G_IO_IN | G_IO_OUT | G_IO_HUP), |
702 | | worker->stream, |
703 | | worker); |
704 | | #endif |
705 | | |
706 | | /* The read failed, which could mean the dbus-daemon was sent SIGTERM. */ |
707 | 0 | if (bytes_read == 0) |
708 | 0 | { |
709 | 0 | g_set_error (&error, |
710 | 0 | G_IO_ERROR, |
711 | 0 | G_IO_ERROR_FAILED, |
712 | 0 | "Underlying GIOStream returned 0 bytes on an async read"); |
713 | 0 | _g_dbus_worker_emit_disconnected (worker, TRUE, error); |
714 | 0 | g_error_free (error); |
715 | 0 | goto out; |
716 | 0 | } |
717 | | |
718 | 0 | read_message_print_transport_debug (bytes_read, worker); |
719 | |
|
720 | 0 | worker->read_buffer_cur_size += bytes_read; |
721 | 0 | if (worker->read_buffer_bytes_wanted == worker->read_buffer_cur_size) |
722 | 0 | { |
723 | | /* OK, got what we asked for! */ |
724 | 0 | if (worker->read_buffer_bytes_wanted == 16) |
725 | 0 | { |
726 | 0 | gssize message_len; |
727 | | /* OK, got the header - determine how many more bytes are needed */ |
728 | 0 | error = NULL; |
729 | 0 | message_len = g_dbus_message_bytes_needed ((guchar *) worker->read_buffer, |
730 | 0 | 16, |
731 | 0 | &error); |
732 | 0 | if (message_len == -1) |
733 | 0 | { |
734 | 0 | g_warning ("_g_dbus_worker_do_read_cb: error determining bytes needed: %s", error->message); |
735 | 0 | _g_dbus_worker_emit_disconnected (worker, FALSE, error); |
736 | 0 | g_error_free (error); |
737 | 0 | goto out; |
738 | 0 | } |
739 | | |
740 | 0 | worker->read_buffer_bytes_wanted = message_len; |
741 | 0 | _g_dbus_worker_do_read_unlocked (worker); |
742 | 0 | } |
743 | 0 | else |
744 | 0 | { |
745 | 0 | GDBusMessage *message; |
746 | 0 | error = NULL; |
747 | | |
748 | | /* TODO: use connection->priv->auth to decode the message */ |
749 | |
|
750 | 0 | message = g_dbus_message_new_from_blob ((guchar *) worker->read_buffer, |
751 | 0 | worker->read_buffer_cur_size, |
752 | 0 | worker->capabilities, |
753 | 0 | &error); |
754 | 0 | if (message == NULL) |
755 | 0 | { |
756 | 0 | gchar *s; |
757 | 0 | s = _g_dbus_hexdump (worker->read_buffer, worker->read_buffer_cur_size, 2); |
758 | 0 | g_warning ("Error decoding D-Bus message of %" G_GSIZE_FORMAT " bytes\n" |
759 | 0 | "The error is: %s\n" |
760 | 0 | "The payload is as follows:\n" |
761 | 0 | "%s", |
762 | 0 | worker->read_buffer_cur_size, |
763 | 0 | error->message, |
764 | 0 | s); |
765 | 0 | g_free (s); |
766 | 0 | _g_dbus_worker_emit_disconnected (worker, FALSE, error); |
767 | 0 | g_error_free (error); |
768 | 0 | goto out; |
769 | 0 | } |
770 | | |
771 | 0 | #ifdef G_OS_UNIX |
772 | 0 | if (worker->read_fd_list != NULL) |
773 | 0 | { |
774 | 0 | g_dbus_message_set_unix_fd_list (message, worker->read_fd_list); |
775 | 0 | g_object_unref (worker->read_fd_list); |
776 | 0 | worker->read_fd_list = NULL; |
777 | 0 | } |
778 | 0 | #endif |
779 | |
|
780 | 0 | if (G_UNLIKELY (_g_dbus_debug_message ())) |
781 | 0 | { |
782 | 0 | gchar *s; |
783 | 0 | _g_dbus_debug_print_lock (); |
784 | 0 | g_print ("========================================================================\n" |
785 | 0 | "GDBus-debug:Message:\n" |
786 | 0 | " <<<< RECEIVED D-Bus message (%" G_GSIZE_FORMAT " bytes)\n", |
787 | 0 | worker->read_buffer_cur_size); |
788 | 0 | s = g_dbus_message_print (message, 2); |
789 | 0 | g_print ("%s", s); |
790 | 0 | g_free (s); |
791 | 0 | if (G_UNLIKELY (_g_dbus_debug_payload ())) |
792 | 0 | { |
793 | 0 | s = _g_dbus_hexdump (worker->read_buffer, worker->read_buffer_cur_size, 2); |
794 | 0 | g_print ("%s\n", s); |
795 | 0 | g_free (s); |
796 | 0 | } |
797 | 0 | _g_dbus_debug_print_unlock (); |
798 | 0 | } |
799 | | |
800 | | /* yay, got a message, go deliver it */ |
801 | 0 | _g_dbus_worker_queue_or_deliver_received_message (worker, g_steal_pointer (&message)); |
802 | | |
803 | | /* start reading another message! */ |
804 | 0 | worker->read_buffer_bytes_wanted = 0; |
805 | 0 | worker->read_buffer_cur_size = 0; |
806 | 0 | _g_dbus_worker_do_read_unlocked (worker); |
807 | 0 | } |
808 | 0 | } |
809 | 0 | else |
810 | 0 | { |
811 | | /* didn't get all the bytes we requested - so repeat the request... */ |
812 | 0 | _g_dbus_worker_do_read_unlocked (worker); |
813 | 0 | } |
814 | | |
815 | 0 | out: |
816 | 0 | g_mutex_unlock (&worker->read_lock); |
817 | | |
818 | | /* check if there is any pending close */ |
819 | 0 | schedule_pending_close (worker); |
820 | | |
821 | | /* gives up the reference acquired when calling g_input_stream_read_async() */ |
822 | 0 | _g_dbus_worker_unref (worker); |
823 | 0 | } |
824 | | |
825 | | /* called in private thread shared by all GDBusConnection instances (with read-lock held) */ |
826 | | static void |
827 | | _g_dbus_worker_do_read_unlocked (GDBusWorker *worker) |
828 | 0 | { |
829 | | /* Note that we do need to keep trying to read even if close_expected is |
830 | | * true, because only failing a read causes us to signal 'closed'. |
831 | | */ |
832 | | |
833 | | /* if bytes_wanted is zero, it means start reading a message */ |
834 | 0 | if (worker->read_buffer_bytes_wanted == 0) |
835 | 0 | { |
836 | 0 | worker->read_buffer_cur_size = 0; |
837 | 0 | worker->read_buffer_bytes_wanted = 16; |
838 | 0 | } |
839 | | |
840 | | /* ensure we have a (big enough) buffer */ |
841 | 0 | if (worker->read_buffer == NULL || worker->read_buffer_bytes_wanted > worker->read_buffer_allocated_size) |
842 | 0 | { |
843 | | /* TODO: 4096 is randomly chosen; might want a better chosen default minimum */ |
844 | 0 | worker->read_buffer_allocated_size = MAX (worker->read_buffer_bytes_wanted, 4096); |
845 | 0 | worker->read_buffer = g_realloc (worker->read_buffer, worker->read_buffer_allocated_size); |
846 | 0 | } |
847 | |
|
848 | 0 | if (worker->socket == NULL) |
849 | 0 | g_input_stream_read_async (g_io_stream_get_input_stream (worker->stream), |
850 | 0 | worker->read_buffer + worker->read_buffer_cur_size, |
851 | 0 | worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size, |
852 | 0 | G_PRIORITY_DEFAULT, |
853 | 0 | worker->cancellable, |
854 | 0 | (GAsyncReadyCallback) _g_dbus_worker_do_read_cb, |
855 | 0 | _g_dbus_worker_ref (worker)); |
856 | 0 | else |
857 | 0 | { |
858 | 0 | worker->read_ancillary_messages = NULL; |
859 | 0 | worker->read_num_ancillary_messages = 0; |
860 | 0 | _g_socket_read_with_control_messages (worker->socket, |
861 | 0 | worker->read_buffer + worker->read_buffer_cur_size, |
862 | 0 | worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size, |
863 | 0 | &worker->read_ancillary_messages, |
864 | 0 | &worker->read_num_ancillary_messages, |
865 | 0 | G_PRIORITY_DEFAULT, |
866 | 0 | worker->cancellable, |
867 | 0 | (GAsyncReadyCallback) _g_dbus_worker_do_read_cb, |
868 | 0 | _g_dbus_worker_ref (worker)); |
869 | 0 | } |
870 | 0 | } |
871 | | |
872 | | /* called in private thread shared by all GDBusConnection instances (without read-lock held) */ |
873 | | static gboolean |
874 | | _g_dbus_worker_do_initial_read (gpointer data) |
875 | 0 | { |
876 | 0 | GDBusWorker *worker = data; |
877 | 0 | g_mutex_lock (&worker->read_lock); |
878 | 0 | _g_dbus_worker_do_read_unlocked (worker); |
879 | 0 | g_mutex_unlock (&worker->read_lock); |
880 | 0 | return FALSE; |
881 | 0 | } |
882 | | |
883 | | /* ---------------------------------------------------------------------------------------------------- */ |
884 | | |
885 | | struct _MessageToWriteData |
886 | | { |
887 | | GDBusWorker *worker; |
888 | | GDBusMessage *message; |
889 | | gchar *blob; |
890 | | gsize blob_size; |
891 | | |
892 | | gsize total_written; |
893 | | GTask *task; |
894 | | }; |
895 | | |
896 | | static void |
897 | | message_to_write_data_free (MessageToWriteData *data) |
898 | 0 | { |
899 | 0 | _g_dbus_worker_unref (data->worker); |
900 | 0 | if (data->message) |
901 | 0 | g_object_unref (data->message); |
902 | 0 | g_free (data->blob); |
903 | 0 | g_slice_free (MessageToWriteData, data); |
904 | 0 | } |
905 | | |
906 | | /* ---------------------------------------------------------------------------------------------------- */ |
907 | | |
908 | | static void write_message_continue_writing (MessageToWriteData *data); |
909 | | |
910 | | /* called in private thread shared by all GDBusConnection instances |
911 | | * |
912 | | * write-lock is not held on entry |
913 | | * output_pending is PENDING_WRITE on entry |
914 | | */ |
915 | | static void |
916 | | write_message_async_cb (GObject *source_object, |
917 | | GAsyncResult *res, |
918 | | gpointer user_data) |
919 | 0 | { |
920 | 0 | MessageToWriteData *data = user_data; |
921 | 0 | GTask *task; |
922 | 0 | gssize bytes_written; |
923 | 0 | GError *error; |
924 | | |
925 | | /* Note: we can't access data->task after calling g_task_return_* () because the |
926 | | * callback can free @data and we're not completing in idle. So use a copy of the pointer. |
927 | | */ |
928 | 0 | task = data->task; |
929 | |
|
930 | 0 | error = NULL; |
931 | 0 | bytes_written = g_output_stream_write_finish (G_OUTPUT_STREAM (source_object), |
932 | 0 | res, |
933 | 0 | &error); |
934 | 0 | if (bytes_written == -1) |
935 | 0 | { |
936 | 0 | g_task_return_error (task, error); |
937 | 0 | g_object_unref (task); |
938 | 0 | goto out; |
939 | 0 | } |
940 | 0 | g_assert (bytes_written > 0); /* zero is never returned */ |
941 | | |
942 | 0 | write_message_print_transport_debug (bytes_written, data); |
943 | |
|
944 | 0 | data->total_written += bytes_written; |
945 | 0 | g_assert (data->total_written <= data->blob_size); |
946 | 0 | if (data->total_written == data->blob_size) |
947 | 0 | { |
948 | 0 | g_task_return_boolean (task, TRUE); |
949 | 0 | g_object_unref (task); |
950 | 0 | goto out; |
951 | 0 | } |
952 | | |
953 | 0 | write_message_continue_writing (data); |
954 | |
|
955 | 0 | out: |
956 | 0 | ; |
957 | 0 | } |
958 | | |
959 | | /* called in private thread shared by all GDBusConnection instances |
960 | | * |
961 | | * write-lock is not held on entry |
962 | | * output_pending is PENDING_WRITE on entry |
963 | | */ |
964 | | #ifdef G_OS_UNIX |
965 | | static gboolean |
966 | | on_socket_ready (GSocket *socket, |
967 | | GIOCondition condition, |
968 | | gpointer user_data) |
969 | 0 | { |
970 | 0 | MessageToWriteData *data = user_data; |
971 | 0 | write_message_continue_writing (data); |
972 | 0 | return FALSE; /* remove source */ |
973 | 0 | } |
974 | | #endif |
975 | | |
976 | | /* called in private thread shared by all GDBusConnection instances |
977 | | * |
978 | | * write-lock is not held on entry |
979 | | * output_pending is PENDING_WRITE on entry |
980 | | */ |
981 | | static void |
982 | | write_message_continue_writing (MessageToWriteData *data) |
983 | 0 | { |
984 | 0 | GOutputStream *ostream; |
985 | 0 | #ifdef G_OS_UNIX |
986 | 0 | GTask *task; |
987 | 0 | GUnixFDList *fd_list; |
988 | 0 | #endif |
989 | |
|
990 | 0 | #ifdef G_OS_UNIX |
991 | | /* Note: we can't access data->task after calling g_task_return_* () because the |
992 | | * callback can free @data and we're not completing in idle. So use a copy of the pointer. |
993 | | */ |
994 | 0 | task = data->task; |
995 | 0 | #endif |
996 | |
|
997 | 0 | ostream = g_io_stream_get_output_stream (data->worker->stream); |
998 | 0 | #ifdef G_OS_UNIX |
999 | 0 | fd_list = g_dbus_message_get_unix_fd_list (data->message); |
1000 | 0 | #endif |
1001 | |
|
1002 | 0 | g_assert (!g_output_stream_has_pending (ostream)); |
1003 | 0 | g_assert_cmpint (data->total_written, <, data->blob_size); |
1004 | |
|
1005 | 0 | if (FALSE) |
1006 | 0 | { |
1007 | 0 | } |
1008 | 0 | #ifdef G_OS_UNIX |
1009 | 0 | else if (G_IS_SOCKET_OUTPUT_STREAM (ostream) && data->total_written == 0) |
1010 | 0 | { |
1011 | 0 | GOutputVector vector; |
1012 | 0 | GSocketControlMessage *control_message; |
1013 | 0 | gssize bytes_written; |
1014 | 0 | GError *error; |
1015 | |
|
1016 | 0 | vector.buffer = data->blob; |
1017 | 0 | vector.size = data->blob_size; |
1018 | |
|
1019 | 0 | control_message = NULL; |
1020 | 0 | if (fd_list != NULL && g_unix_fd_list_get_length (fd_list) > 0) |
1021 | 0 | { |
1022 | 0 | if (!(data->worker->capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING)) |
1023 | 0 | { |
1024 | 0 | g_task_return_new_error (task, |
1025 | 0 | G_IO_ERROR, |
1026 | 0 | G_IO_ERROR_FAILED, |
1027 | 0 | "Tried sending a file descriptor but remote peer does not support this capability"); |
1028 | 0 | g_object_unref (task); |
1029 | 0 | goto out; |
1030 | 0 | } |
1031 | 0 | control_message = g_unix_fd_message_new_with_fd_list (fd_list); |
1032 | 0 | } |
1033 | | |
1034 | 0 | error = NULL; |
1035 | 0 | bytes_written = g_socket_send_message (data->worker->socket, |
1036 | 0 | NULL, /* address */ |
1037 | 0 | &vector, |
1038 | 0 | 1, |
1039 | 0 | control_message != NULL ? &control_message : NULL, |
1040 | 0 | control_message != NULL ? 1 : 0, |
1041 | 0 | G_SOCKET_MSG_NONE, |
1042 | 0 | data->worker->cancellable, |
1043 | 0 | &error); |
1044 | 0 | if (control_message != NULL) |
1045 | 0 | g_object_unref (control_message); |
1046 | |
|
1047 | 0 | if (bytes_written == -1) |
1048 | 0 | { |
1049 | | /* Handle WOULD_BLOCK by waiting until there's room in the buffer */ |
1050 | 0 | if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) |
1051 | 0 | { |
1052 | 0 | GSource *source; |
1053 | 0 | source = g_socket_create_source (data->worker->socket, |
1054 | 0 | G_IO_OUT | G_IO_HUP | G_IO_ERR, |
1055 | 0 | data->worker->cancellable); |
1056 | 0 | g_source_set_callback (source, |
1057 | 0 | (GSourceFunc) on_socket_ready, |
1058 | 0 | data, |
1059 | 0 | NULL); /* GDestroyNotify */ |
1060 | 0 | g_source_attach (source, g_main_context_get_thread_default ()); |
1061 | 0 | g_source_unref (source); |
1062 | 0 | g_error_free (error); |
1063 | 0 | goto out; |
1064 | 0 | } |
1065 | 0 | g_task_return_error (task, error); |
1066 | 0 | g_object_unref (task); |
1067 | 0 | goto out; |
1068 | 0 | } |
1069 | 0 | g_assert (bytes_written > 0); /* zero is never returned */ |
1070 | | |
1071 | 0 | write_message_print_transport_debug (bytes_written, data); |
1072 | |
|
1073 | 0 | data->total_written += bytes_written; |
1074 | 0 | g_assert (data->total_written <= data->blob_size); |
1075 | 0 | if (data->total_written == data->blob_size) |
1076 | 0 | { |
1077 | 0 | g_task_return_boolean (task, TRUE); |
1078 | 0 | g_object_unref (task); |
1079 | 0 | goto out; |
1080 | 0 | } |
1081 | | |
1082 | 0 | write_message_continue_writing (data); |
1083 | 0 | } |
1084 | 0 | #endif |
1085 | 0 | else |
1086 | 0 | { |
1087 | 0 | #ifdef G_OS_UNIX |
1088 | 0 | if (data->total_written == 0 && fd_list != NULL) |
1089 | 0 | { |
1090 | | /* We were trying to write byte 0 of the message, which needs |
1091 | | * the fd list to be attached to it, but this connection doesn't |
1092 | | * support doing that. */ |
1093 | 0 | g_task_return_new_error (task, |
1094 | 0 | G_IO_ERROR, |
1095 | 0 | G_IO_ERROR_FAILED, |
1096 | 0 | "Tried sending a file descriptor on unsupported stream of type %s", |
1097 | 0 | g_type_name (G_TYPE_FROM_INSTANCE (ostream))); |
1098 | 0 | g_object_unref (task); |
1099 | 0 | goto out; |
1100 | 0 | } |
1101 | 0 | #endif |
1102 | | |
1103 | 0 | g_output_stream_write_async (ostream, |
1104 | 0 | (const gchar *) data->blob + data->total_written, |
1105 | 0 | data->blob_size - data->total_written, |
1106 | 0 | G_PRIORITY_DEFAULT, |
1107 | 0 | data->worker->cancellable, |
1108 | 0 | write_message_async_cb, |
1109 | 0 | data); |
1110 | 0 | } |
1111 | 0 | #ifdef G_OS_UNIX |
1112 | 0 | out: |
1113 | 0 | #endif |
1114 | 0 | ; |
1115 | 0 | } |
1116 | | |
1117 | | /* called in private thread shared by all GDBusConnection instances |
1118 | | * |
1119 | | * write-lock is not held on entry |
1120 | | * output_pending is PENDING_WRITE on entry |
1121 | | */ |
1122 | | static void |
1123 | | write_message_async (GDBusWorker *worker, |
1124 | | MessageToWriteData *data, |
1125 | | GAsyncReadyCallback callback, |
1126 | | gpointer user_data) |
1127 | 0 | { |
1128 | 0 | data->task = g_task_new (NULL, NULL, callback, user_data); |
1129 | 0 | g_task_set_source_tag (data->task, write_message_async); |
1130 | 0 | g_task_set_name (data->task, "[gio] D-Bus write message"); |
1131 | 0 | data->total_written = 0; |
1132 | 0 | write_message_continue_writing (data); |
1133 | 0 | } |
1134 | | |
1135 | | /* called in private thread shared by all GDBusConnection instances (with write-lock held) */ |
1136 | | static gboolean |
1137 | | write_message_finish (GAsyncResult *res, |
1138 | | GError **error) |
1139 | 0 | { |
1140 | 0 | g_return_val_if_fail (g_task_is_valid (res, NULL), FALSE); |
1141 | | |
1142 | 0 | return g_task_propagate_boolean (G_TASK (res), error); |
1143 | 0 | } |
1144 | | /* ---------------------------------------------------------------------------------------------------- */ |
1145 | | |
1146 | | static void continue_writing (GDBusWorker *worker); |
1147 | | |
1148 | | typedef struct |
1149 | | { |
1150 | | GDBusWorker *worker; |
1151 | | GList *flushers; |
1152 | | } FlushAsyncData; |
1153 | | |
1154 | | static void |
1155 | | flush_data_list_complete (const GList *flushers, |
1156 | | const GError *error) |
1157 | 0 | { |
1158 | 0 | const GList *l; |
1159 | |
|
1160 | 0 | for (l = flushers; l != NULL; l = l->next) |
1161 | 0 | { |
1162 | 0 | FlushData *f = l->data; |
1163 | |
|
1164 | 0 | f->error = error != NULL ? g_error_copy (error) : NULL; |
1165 | |
|
1166 | 0 | g_mutex_lock (&f->mutex); |
1167 | 0 | f->finished = TRUE; |
1168 | 0 | g_cond_signal (&f->cond); |
1169 | 0 | g_mutex_unlock (&f->mutex); |
1170 | 0 | } |
1171 | 0 | } |
1172 | | |
1173 | | /* called in private thread shared by all GDBusConnection instances |
1174 | | * |
1175 | | * write-lock is not held on entry |
1176 | | * output_pending is PENDING_FLUSH on entry |
1177 | | */ |
1178 | | static void |
1179 | | ostream_flush_cb (GObject *source_object, |
1180 | | GAsyncResult *res, |
1181 | | gpointer user_data) |
1182 | 0 | { |
1183 | 0 | FlushAsyncData *data = user_data; |
1184 | 0 | GError *error; |
1185 | |
|
1186 | 0 | error = NULL; |
1187 | 0 | g_output_stream_flush_finish (G_OUTPUT_STREAM (source_object), |
1188 | 0 | res, |
1189 | 0 | &error); |
1190 | |
|
1191 | 0 | if (error == NULL) |
1192 | 0 | { |
1193 | 0 | if (G_UNLIKELY (_g_dbus_debug_transport ())) |
1194 | 0 | { |
1195 | 0 | _g_dbus_debug_print_lock (); |
1196 | 0 | g_print ("========================================================================\n" |
1197 | 0 | "GDBus-debug:Transport:\n" |
1198 | 0 | " ---- FLUSHED stream of type %s\n", |
1199 | 0 | g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream)))); |
1200 | 0 | _g_dbus_debug_print_unlock (); |
1201 | 0 | } |
1202 | 0 | } |
1203 | | |
1204 | | /* Make sure we tell folks that we don't have additional |
1205 | | flushes pending */ |
1206 | 0 | g_mutex_lock (&data->worker->write_lock); |
1207 | 0 | data->worker->write_num_messages_flushed = data->worker->write_num_messages_written; |
1208 | 0 | g_assert (data->worker->output_pending == PENDING_FLUSH); |
1209 | 0 | data->worker->output_pending = PENDING_NONE; |
1210 | 0 | g_mutex_unlock (&data->worker->write_lock); |
1211 | |
|
1212 | 0 | g_assert (data->flushers != NULL); |
1213 | 0 | flush_data_list_complete (data->flushers, error); |
1214 | 0 | g_list_free (data->flushers); |
1215 | 0 | if (error != NULL) |
1216 | 0 | g_error_free (error); |
1217 | | |
1218 | | /* OK, cool, finally kick off the next write */ |
1219 | 0 | continue_writing (data->worker); |
1220 | |
|
1221 | 0 | _g_dbus_worker_unref (data->worker); |
1222 | 0 | g_free (data); |
1223 | 0 | } |
1224 | | |
1225 | | /* called in private thread shared by all GDBusConnection instances |
1226 | | * |
1227 | | * write-lock is not held on entry |
1228 | | * output_pending is PENDING_FLUSH on entry |
1229 | | */ |
1230 | | static void |
1231 | | start_flush (FlushAsyncData *data) |
1232 | 0 | { |
1233 | 0 | g_output_stream_flush_async (g_io_stream_get_output_stream (data->worker->stream), |
1234 | 0 | G_PRIORITY_DEFAULT, |
1235 | 0 | data->worker->cancellable, |
1236 | 0 | ostream_flush_cb, |
1237 | 0 | data); |
1238 | 0 | } |
1239 | | |
1240 | | /* called in private thread shared by all GDBusConnection instances |
1241 | | * |
1242 | | * write-lock is held on entry |
1243 | | * output_pending is PENDING_NONE on entry |
1244 | | */ |
1245 | | static void |
1246 | | message_written_unlocked (GDBusWorker *worker, |
1247 | | MessageToWriteData *message_data) |
1248 | 0 | { |
1249 | 0 | if (G_UNLIKELY (_g_dbus_debug_message ())) |
1250 | 0 | { |
1251 | 0 | gchar *s; |
1252 | 0 | _g_dbus_debug_print_lock (); |
1253 | 0 | g_print ("========================================================================\n" |
1254 | 0 | "GDBus-debug:Message:\n" |
1255 | 0 | " >>>> SENT D-Bus message (%" G_GSIZE_FORMAT " bytes)\n", |
1256 | 0 | message_data->blob_size); |
1257 | 0 | s = g_dbus_message_print (message_data->message, 2); |
1258 | 0 | g_print ("%s", s); |
1259 | 0 | g_free (s); |
1260 | 0 | if (G_UNLIKELY (_g_dbus_debug_payload ())) |
1261 | 0 | { |
1262 | 0 | s = _g_dbus_hexdump (message_data->blob, message_data->blob_size, 2); |
1263 | 0 | g_print ("%s\n", s); |
1264 | 0 | g_free (s); |
1265 | 0 | } |
1266 | 0 | _g_dbus_debug_print_unlock (); |
1267 | 0 | } |
1268 | |
|
1269 | 0 | worker->write_num_messages_written += 1; |
1270 | 0 | } |
1271 | | |
1272 | | /* called in private thread shared by all GDBusConnection instances |
1273 | | * |
1274 | | * write-lock is held on entry |
1275 | | * output_pending is PENDING_NONE on entry |
1276 | | * |
1277 | | * Returns: non-%NULL, setting @output_pending, if we need to flush now |
1278 | | */ |
1279 | | static FlushAsyncData * |
1280 | | prepare_flush_unlocked (GDBusWorker *worker) |
1281 | 0 | { |
1282 | 0 | GList *l; |
1283 | 0 | GList *ll; |
1284 | 0 | GList *flushers; |
1285 | |
|
1286 | 0 | flushers = NULL; |
1287 | 0 | for (l = worker->write_pending_flushes; l != NULL; l = ll) |
1288 | 0 | { |
1289 | 0 | FlushData *f = l->data; |
1290 | 0 | ll = l->next; |
1291 | |
|
1292 | 0 | if (f->number_to_wait_for == worker->write_num_messages_written) |
1293 | 0 | { |
1294 | 0 | flushers = g_list_append (flushers, f); |
1295 | 0 | worker->write_pending_flushes = g_list_delete_link (worker->write_pending_flushes, l); |
1296 | 0 | } |
1297 | 0 | } |
1298 | 0 | if (flushers != NULL) |
1299 | 0 | { |
1300 | 0 | g_assert (worker->output_pending == PENDING_NONE); |
1301 | 0 | worker->output_pending = PENDING_FLUSH; |
1302 | 0 | } |
1303 | | |
1304 | 0 | if (flushers != NULL) |
1305 | 0 | { |
1306 | 0 | FlushAsyncData *data; |
1307 | |
|
1308 | 0 | data = g_new0 (FlushAsyncData, 1); |
1309 | 0 | data->worker = _g_dbus_worker_ref (worker); |
1310 | 0 | data->flushers = flushers; |
1311 | 0 | return data; |
1312 | 0 | } |
1313 | | |
1314 | 0 | return NULL; |
1315 | 0 | } |
1316 | | |
1317 | | /* called in private thread shared by all GDBusConnection instances |
1318 | | * |
1319 | | * write-lock is not held on entry |
1320 | | * output_pending is PENDING_WRITE on entry |
1321 | | */ |
1322 | | static void |
1323 | | write_message_cb (GObject *source_object, |
1324 | | GAsyncResult *res, |
1325 | | gpointer user_data) |
1326 | 0 | { |
1327 | 0 | MessageToWriteData *data = user_data; |
1328 | 0 | GError *error; |
1329 | |
|
1330 | 0 | g_mutex_lock (&data->worker->write_lock); |
1331 | 0 | g_assert (data->worker->output_pending == PENDING_WRITE); |
1332 | 0 | data->worker->output_pending = PENDING_NONE; |
1333 | |
|
1334 | 0 | error = NULL; |
1335 | 0 | if (!write_message_finish (res, &error)) |
1336 | 0 | { |
1337 | 0 | g_mutex_unlock (&data->worker->write_lock); |
1338 | | |
1339 | | /* TODO: handle */ |
1340 | 0 | _g_dbus_worker_emit_disconnected (data->worker, TRUE, error); |
1341 | 0 | g_error_free (error); |
1342 | |
|
1343 | 0 | g_mutex_lock (&data->worker->write_lock); |
1344 | 0 | } |
1345 | |
|
1346 | 0 | message_written_unlocked (data->worker, data); |
1347 | |
|
1348 | 0 | g_mutex_unlock (&data->worker->write_lock); |
1349 | |
|
1350 | 0 | continue_writing (data->worker); |
1351 | |
|
1352 | 0 | message_to_write_data_free (data); |
1353 | 0 | } |
1354 | | |
1355 | | /* called in private thread shared by all GDBusConnection instances |
1356 | | * |
1357 | | * write-lock is not held on entry |
1358 | | * output_pending is PENDING_CLOSE on entry |
1359 | | */ |
1360 | | static void |
1361 | | iostream_close_cb (GObject *source_object, |
1362 | | GAsyncResult *res, |
1363 | | gpointer user_data) |
1364 | 0 | { |
1365 | 0 | GDBusWorker *worker = user_data; |
1366 | 0 | GError *error = NULL; |
1367 | 0 | GList *pending_close_attempts, *pending_flush_attempts; |
1368 | 0 | GQueue *send_queue; |
1369 | |
|
1370 | 0 | g_io_stream_close_finish (worker->stream, res, &error); |
1371 | |
|
1372 | 0 | g_mutex_lock (&worker->write_lock); |
1373 | |
|
1374 | 0 | pending_close_attempts = worker->pending_close_attempts; |
1375 | 0 | worker->pending_close_attempts = NULL; |
1376 | |
|
1377 | 0 | pending_flush_attempts = worker->write_pending_flushes; |
1378 | 0 | worker->write_pending_flushes = NULL; |
1379 | |
|
1380 | 0 | send_queue = worker->write_queue; |
1381 | 0 | worker->write_queue = g_queue_new (); |
1382 | |
|
1383 | 0 | g_assert (worker->output_pending == PENDING_CLOSE); |
1384 | 0 | worker->output_pending = PENDING_NONE; |
1385 | | |
1386 | | /* Ensure threads waiting for pending flushes to finish will be unblocked. */ |
1387 | 0 | worker->write_num_messages_flushed = |
1388 | 0 | worker->write_num_messages_written + g_list_length(pending_flush_attempts); |
1389 | |
|
1390 | 0 | g_mutex_unlock (&worker->write_lock); |
1391 | |
|
1392 | 0 | while (pending_close_attempts != NULL) |
1393 | 0 | { |
1394 | 0 | CloseData *close_data = pending_close_attempts->data; |
1395 | |
|
1396 | 0 | pending_close_attempts = g_list_delete_link (pending_close_attempts, |
1397 | 0 | pending_close_attempts); |
1398 | |
|
1399 | 0 | if (close_data->task != NULL) |
1400 | 0 | { |
1401 | 0 | if (error != NULL) |
1402 | 0 | g_task_return_error (close_data->task, g_error_copy (error)); |
1403 | 0 | else |
1404 | 0 | g_task_return_boolean (close_data->task, TRUE); |
1405 | 0 | } |
1406 | |
|
1407 | 0 | close_data_free (close_data); |
1408 | 0 | } |
1409 | |
|
1410 | 0 | g_clear_error (&error); |
1411 | | |
1412 | | /* all messages queued for sending are discarded */ |
1413 | 0 | g_queue_free_full (send_queue, (GDestroyNotify) message_to_write_data_free); |
1414 | | /* all queued flushes fail */ |
1415 | 0 | error = g_error_new (G_IO_ERROR, G_IO_ERROR_CANCELLED, |
1416 | 0 | _("Operation was cancelled")); |
1417 | 0 | flush_data_list_complete (pending_flush_attempts, error); |
1418 | 0 | g_list_free (pending_flush_attempts); |
1419 | 0 | g_clear_error (&error); |
1420 | |
|
1421 | 0 | _g_dbus_worker_unref (worker); |
1422 | 0 | } |
1423 | | |
1424 | | /* called in private thread shared by all GDBusConnection instances |
1425 | | * |
1426 | | * write-lock is not held on entry |
1427 | | * output_pending must be PENDING_NONE on entry |
1428 | | */ |
1429 | | static void |
1430 | | continue_writing (GDBusWorker *worker) |
1431 | 0 | { |
1432 | 0 | MessageToWriteData *data; |
1433 | 0 | FlushAsyncData *flush_async_data; |
1434 | |
|
1435 | 0 | write_next: |
1436 | | /* we mustn't try to write two things at once */ |
1437 | 0 | g_assert (worker->output_pending == PENDING_NONE); |
1438 | | |
1439 | 0 | g_mutex_lock (&worker->write_lock); |
1440 | |
|
1441 | 0 | data = NULL; |
1442 | 0 | flush_async_data = NULL; |
1443 | | |
1444 | | /* if we want to close the connection, that takes precedence */ |
1445 | 0 | if (worker->pending_close_attempts != NULL) |
1446 | 0 | { |
1447 | 0 | GInputStream *input = g_io_stream_get_input_stream (worker->stream); |
1448 | |
|
1449 | 0 | if (!g_input_stream_has_pending (input)) |
1450 | 0 | { |
1451 | 0 | worker->close_expected = TRUE; |
1452 | 0 | worker->output_pending = PENDING_CLOSE; |
1453 | |
|
1454 | 0 | g_io_stream_close_async (worker->stream, G_PRIORITY_DEFAULT, |
1455 | 0 | NULL, iostream_close_cb, |
1456 | 0 | _g_dbus_worker_ref (worker)); |
1457 | 0 | } |
1458 | 0 | } |
1459 | 0 | else |
1460 | 0 | { |
1461 | 0 | flush_async_data = prepare_flush_unlocked (worker); |
1462 | |
|
1463 | 0 | if (flush_async_data == NULL) |
1464 | 0 | { |
1465 | 0 | data = g_queue_pop_head (worker->write_queue); |
1466 | |
|
1467 | 0 | if (data != NULL) |
1468 | 0 | worker->output_pending = PENDING_WRITE; |
1469 | 0 | } |
1470 | 0 | } |
1471 | |
|
1472 | 0 | g_mutex_unlock (&worker->write_lock); |
1473 | | |
1474 | | /* Note that write_lock is only used for protecting the @write_queue |
1475 | | * and @output_pending fields of the GDBusWorker struct ... which we |
1476 | | * need to modify from arbitrary threads in _g_dbus_worker_send_message(). |
1477 | | * |
1478 | | * Therefore, it's fine to drop it here when calling back into user |
1479 | | * code and then writing the message out onto the GIOStream since this |
1480 | | * function only runs on the worker thread. |
1481 | | */ |
1482 | |
|
1483 | 0 | if (flush_async_data != NULL) |
1484 | 0 | { |
1485 | 0 | start_flush (flush_async_data); |
1486 | 0 | g_assert (data == NULL); |
1487 | 0 | } |
1488 | 0 | else if (data != NULL) |
1489 | 0 | { |
1490 | 0 | GDBusMessage *old_message; |
1491 | 0 | guchar *new_blob; |
1492 | 0 | gsize new_blob_size; |
1493 | 0 | GError *error; |
1494 | |
|
1495 | 0 | old_message = data->message; |
1496 | 0 | data->message = _g_dbus_worker_emit_message_about_to_be_sent (worker, data->message); |
1497 | 0 | if (data->message == old_message) |
1498 | 0 | { |
1499 | | /* filters had no effect - do nothing */ |
1500 | 0 | } |
1501 | 0 | else if (data->message == NULL) |
1502 | 0 | { |
1503 | | /* filters dropped message */ |
1504 | 0 | g_mutex_lock (&worker->write_lock); |
1505 | 0 | worker->output_pending = PENDING_NONE; |
1506 | 0 | g_mutex_unlock (&worker->write_lock); |
1507 | 0 | message_to_write_data_free (data); |
1508 | 0 | goto write_next; |
1509 | 0 | } |
1510 | 0 | else |
1511 | 0 | { |
1512 | | /* filters altered the message -> re-encode */ |
1513 | 0 | error = NULL; |
1514 | 0 | new_blob = g_dbus_message_to_blob (data->message, |
1515 | 0 | &new_blob_size, |
1516 | 0 | worker->capabilities, |
1517 | 0 | &error); |
1518 | 0 | if (new_blob == NULL) |
1519 | 0 | { |
1520 | | /* if filter make the GDBusMessage unencodeable, just complain on stderr and send |
1521 | | * the old message instead |
1522 | | */ |
1523 | 0 | g_warning ("Error encoding GDBusMessage with serial %d altered by filter function: %s", |
1524 | 0 | g_dbus_message_get_serial (data->message), |
1525 | 0 | error->message); |
1526 | 0 | g_error_free (error); |
1527 | 0 | } |
1528 | 0 | else |
1529 | 0 | { |
1530 | 0 | g_free (data->blob); |
1531 | 0 | data->blob = (gchar *) new_blob; |
1532 | 0 | data->blob_size = new_blob_size; |
1533 | 0 | } |
1534 | 0 | } |
1535 | | |
1536 | 0 | write_message_async (worker, |
1537 | 0 | data, |
1538 | 0 | write_message_cb, |
1539 | 0 | data); |
1540 | 0 | } |
1541 | 0 | } |
1542 | | |
1543 | | /* called in private thread shared by all GDBusConnection instances |
1544 | | * |
1545 | | * write-lock is not held on entry |
1546 | | * output_pending may be anything |
1547 | | */ |
1548 | | static gboolean |
1549 | | continue_writing_in_idle_cb (gpointer user_data) |
1550 | 0 | { |
1551 | 0 | GDBusWorker *worker = user_data; |
1552 | | |
1553 | | /* Because this is the worker thread, we can read this struct member |
1554 | | * without holding the lock: no other thread ever modifies it. |
1555 | | */ |
1556 | 0 | if (worker->output_pending == PENDING_NONE) |
1557 | 0 | continue_writing (worker); |
1558 | |
|
1559 | 0 | return FALSE; |
1560 | 0 | } |
1561 | | |
1562 | | /* |
1563 | | * @write_data: (transfer full) (nullable): |
1564 | | * @flush_data: (transfer full) (nullable): |
1565 | | * @close_data: (transfer full) (nullable): |
1566 | | * |
1567 | | * Can be called from any thread |
1568 | | * |
1569 | | * write_lock is held on entry |
1570 | | * output_pending may be anything |
1571 | | */ |
1572 | | static void |
1573 | | schedule_writing_unlocked (GDBusWorker *worker, |
1574 | | MessageToWriteData *write_data, |
1575 | | FlushData *flush_data, |
1576 | | CloseData *close_data) |
1577 | 0 | { |
1578 | 0 | if (write_data != NULL) |
1579 | 0 | g_queue_push_tail (worker->write_queue, write_data); |
1580 | |
|
1581 | 0 | if (flush_data != NULL) |
1582 | 0 | worker->write_pending_flushes = g_list_prepend (worker->write_pending_flushes, flush_data); |
1583 | |
|
1584 | 0 | if (close_data != NULL) |
1585 | 0 | worker->pending_close_attempts = g_list_prepend (worker->pending_close_attempts, |
1586 | 0 | close_data); |
1587 | | |
1588 | | /* If we had output pending, the next bit of output will happen |
1589 | | * automatically when it finishes, so we only need to do this |
1590 | | * if nothing was pending. |
1591 | | * |
1592 | | * The idle callback will re-check that output_pending is still |
1593 | | * PENDING_NONE, to guard against output starting before the idle. |
1594 | | */ |
1595 | 0 | if (worker->output_pending == PENDING_NONE) |
1596 | 0 | { |
1597 | 0 | GSource *idle_source; |
1598 | 0 | idle_source = g_idle_source_new (); |
1599 | 0 | g_source_set_priority (idle_source, G_PRIORITY_DEFAULT); |
1600 | 0 | g_source_set_callback (idle_source, |
1601 | 0 | continue_writing_in_idle_cb, |
1602 | 0 | _g_dbus_worker_ref (worker), |
1603 | 0 | (GDestroyNotify) _g_dbus_worker_unref); |
1604 | 0 | g_source_set_name (idle_source, "[gio] continue_writing_in_idle_cb"); |
1605 | 0 | g_source_attach (idle_source, worker->shared_thread_data->context); |
1606 | 0 | g_source_unref (idle_source); |
1607 | 0 | } |
1608 | 0 | } |
1609 | | |
1610 | | static void |
1611 | | schedule_pending_close (GDBusWorker *worker) |
1612 | 0 | { |
1613 | 0 | g_mutex_lock (&worker->write_lock); |
1614 | 0 | if (worker->pending_close_attempts) |
1615 | 0 | schedule_writing_unlocked (worker, NULL, NULL, NULL); |
1616 | 0 | g_mutex_unlock (&worker->write_lock); |
1617 | 0 | } |
1618 | | |
1619 | | /* ---------------------------------------------------------------------------------------------------- */ |
1620 | | |
1621 | | /* can be called from any thread - steals blob |
1622 | | * |
1623 | | * write_lock is not held on entry |
1624 | | * output_pending may be anything |
1625 | | */ |
1626 | | void |
1627 | | _g_dbus_worker_send_message (GDBusWorker *worker, |
1628 | | GDBusMessage *message, |
1629 | | gchar *blob, |
1630 | | gsize blob_len) |
1631 | 0 | { |
1632 | 0 | MessageToWriteData *data; |
1633 | |
|
1634 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
1635 | 0 | g_return_if_fail (blob != NULL); |
1636 | 0 | g_return_if_fail (blob_len > 16); |
1637 | | |
1638 | 0 | data = g_slice_new0 (MessageToWriteData); |
1639 | 0 | data->worker = _g_dbus_worker_ref (worker); |
1640 | 0 | data->message = g_object_ref (message); |
1641 | 0 | data->blob = blob; /* steal! */ |
1642 | 0 | data->blob_size = blob_len; |
1643 | |
|
1644 | 0 | g_mutex_lock (&worker->write_lock); |
1645 | 0 | schedule_writing_unlocked (worker, data, NULL, NULL); |
1646 | 0 | g_mutex_unlock (&worker->write_lock); |
1647 | 0 | } |
1648 | | |
1649 | | /* ---------------------------------------------------------------------------------------------------- */ |
1650 | | |
1651 | | GDBusWorker * |
1652 | | _g_dbus_worker_new (GIOStream *stream, |
1653 | | GDBusCapabilityFlags capabilities, |
1654 | | gboolean initially_frozen, |
1655 | | GDBusWorkerMessageReceivedCallback message_received_callback, |
1656 | | GDBusWorkerMessageAboutToBeSentCallback message_about_to_be_sent_callback, |
1657 | | GDBusWorkerDisconnectedCallback disconnected_callback, |
1658 | | gpointer user_data) |
1659 | 0 | { |
1660 | 0 | GDBusWorker *worker; |
1661 | 0 | GSource *idle_source; |
1662 | |
|
1663 | 0 | g_return_val_if_fail (G_IS_IO_STREAM (stream), NULL); |
1664 | 0 | g_return_val_if_fail (message_received_callback != NULL, NULL); |
1665 | 0 | g_return_val_if_fail (message_about_to_be_sent_callback != NULL, NULL); |
1666 | 0 | g_return_val_if_fail (disconnected_callback != NULL, NULL); |
1667 | | |
1668 | 0 | worker = g_new0 (GDBusWorker, 1); |
1669 | 0 | worker->ref_count = 1; |
1670 | |
|
1671 | 0 | g_mutex_init (&worker->read_lock); |
1672 | 0 | worker->message_received_callback = message_received_callback; |
1673 | 0 | worker->message_about_to_be_sent_callback = message_about_to_be_sent_callback; |
1674 | 0 | worker->disconnected_callback = disconnected_callback; |
1675 | 0 | worker->user_data = user_data; |
1676 | 0 | worker->stream = g_object_ref (stream); |
1677 | 0 | worker->capabilities = capabilities; |
1678 | 0 | worker->cancellable = g_cancellable_new (); |
1679 | 0 | worker->output_pending = PENDING_NONE; |
1680 | |
|
1681 | 0 | worker->frozen = initially_frozen; |
1682 | 0 | worker->received_messages_while_frozen = g_queue_new (); |
1683 | |
|
1684 | 0 | g_mutex_init (&worker->write_lock); |
1685 | 0 | worker->write_queue = g_queue_new (); |
1686 | |
|
1687 | 0 | if (G_IS_SOCKET_CONNECTION (worker->stream)) |
1688 | 0 | worker->socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream)); |
1689 | |
|
1690 | 0 | worker->shared_thread_data = _g_dbus_shared_thread_ref (); |
1691 | | |
1692 | | /* begin reading */ |
1693 | 0 | idle_source = g_idle_source_new (); |
1694 | 0 | g_source_set_priority (idle_source, G_PRIORITY_DEFAULT); |
1695 | 0 | g_source_set_callback (idle_source, |
1696 | 0 | _g_dbus_worker_do_initial_read, |
1697 | 0 | _g_dbus_worker_ref (worker), |
1698 | 0 | (GDestroyNotify) _g_dbus_worker_unref); |
1699 | 0 | g_source_set_name (idle_source, "[gio] _g_dbus_worker_do_initial_read"); |
1700 | 0 | g_source_attach (idle_source, worker->shared_thread_data->context); |
1701 | 0 | g_source_unref (idle_source); |
1702 | |
|
1703 | 0 | return worker; |
1704 | 0 | } |
1705 | | |
1706 | | /* ---------------------------------------------------------------------------------------------------- */ |
1707 | | |
1708 | | /* can be called from any thread |
1709 | | * |
1710 | | * write_lock is not held on entry |
1711 | | * output_pending may be anything |
1712 | | */ |
1713 | | void |
1714 | | _g_dbus_worker_close (GDBusWorker *worker, |
1715 | | GTask *task) |
1716 | 0 | { |
1717 | 0 | CloseData *close_data; |
1718 | |
|
1719 | 0 | close_data = g_slice_new0 (CloseData); |
1720 | 0 | close_data->worker = _g_dbus_worker_ref (worker); |
1721 | 0 | close_data->task = (task == NULL ? NULL : g_object_ref (task)); |
1722 | | |
1723 | | /* Don't set worker->close_expected here - we're in the wrong thread. |
1724 | | * It'll be set before the actual close happens. |
1725 | | */ |
1726 | 0 | g_cancellable_cancel (worker->cancellable); |
1727 | 0 | g_mutex_lock (&worker->write_lock); |
1728 | 0 | schedule_writing_unlocked (worker, NULL, NULL, close_data); |
1729 | 0 | g_mutex_unlock (&worker->write_lock); |
1730 | 0 | } |
1731 | | |
1732 | | /* This can be called from any thread - frees worker. Note that |
1733 | | * callbacks might still happen if called from another thread than the |
1734 | | * worker - use your own synchronization primitive in the callbacks. |
1735 | | * |
1736 | | * write_lock is not held on entry |
1737 | | * output_pending may be anything |
1738 | | */ |
1739 | | void |
1740 | | _g_dbus_worker_stop (GDBusWorker *worker) |
1741 | 0 | { |
1742 | 0 | g_atomic_int_set (&worker->stopped, TRUE); |
1743 | | |
1744 | | /* Cancel any pending operations and schedule a close of the underlying I/O |
1745 | | * stream in the worker thread |
1746 | | */ |
1747 | 0 | _g_dbus_worker_close (worker, NULL); |
1748 | | |
1749 | | /* _g_dbus_worker_close holds a ref until after an idle in the worker |
1750 | | * thread has run, so we no longer need to unref in an idle like in |
1751 | | * commit 322e25b535 |
1752 | | */ |
1753 | 0 | _g_dbus_worker_unref (worker); |
1754 | 0 | } |
1755 | | |
1756 | | /* ---------------------------------------------------------------------------------------------------- */ |
1757 | | |
1758 | | /* can be called from any thread (except the worker thread) - blocks |
1759 | | * calling thread until all queued outgoing messages are written and |
1760 | | * the transport has been flushed |
1761 | | * |
1762 | | * write_lock is not held on entry |
1763 | | * output_pending may be anything |
1764 | | */ |
1765 | | gboolean |
1766 | | _g_dbus_worker_flush_sync (GDBusWorker *worker, |
1767 | | GCancellable *cancellable, |
1768 | | GError **error) |
1769 | 0 | { |
1770 | 0 | gboolean ret; |
1771 | 0 | FlushData *data; |
1772 | 0 | guint64 pending_writes; |
1773 | |
|
1774 | 0 | data = NULL; |
1775 | 0 | ret = TRUE; |
1776 | |
|
1777 | 0 | g_mutex_lock (&worker->write_lock); |
1778 | | |
1779 | | /* if the queue is empty, no write is in-flight and we haven't written |
1780 | | * anything since the last flush, then there's nothing to wait for |
1781 | | */ |
1782 | 0 | pending_writes = g_queue_get_length (worker->write_queue); |
1783 | | |
1784 | | /* if a write is in-flight, we shouldn't be satisfied until the first |
1785 | | * flush operation that follows it |
1786 | | */ |
1787 | 0 | if (worker->output_pending == PENDING_WRITE) |
1788 | 0 | pending_writes += 1; |
1789 | |
|
1790 | 0 | if (pending_writes > 0 || |
1791 | 0 | worker->write_num_messages_written != worker->write_num_messages_flushed) |
1792 | 0 | { |
1793 | 0 | data = g_new0 (FlushData, 1); |
1794 | 0 | g_mutex_init (&data->mutex); |
1795 | 0 | g_cond_init (&data->cond); |
1796 | 0 | data->number_to_wait_for = worker->write_num_messages_written + pending_writes; |
1797 | 0 | data->finished = FALSE; |
1798 | 0 | g_mutex_lock (&data->mutex); |
1799 | |
|
1800 | 0 | schedule_writing_unlocked (worker, NULL, data, NULL); |
1801 | 0 | } |
1802 | 0 | g_mutex_unlock (&worker->write_lock); |
1803 | |
|
1804 | 0 | if (data != NULL) |
1805 | 0 | { |
1806 | | /* Wait for flush operations to finish. */ |
1807 | 0 | while (!data->finished) |
1808 | 0 | { |
1809 | 0 | g_cond_wait (&data->cond, &data->mutex); |
1810 | 0 | } |
1811 | |
|
1812 | 0 | g_mutex_unlock (&data->mutex); |
1813 | 0 | g_cond_clear (&data->cond); |
1814 | 0 | g_mutex_clear (&data->mutex); |
1815 | 0 | if (data->error != NULL) |
1816 | 0 | { |
1817 | 0 | ret = FALSE; |
1818 | 0 | g_propagate_error (error, data->error); |
1819 | 0 | } |
1820 | 0 | g_free (data); |
1821 | 0 | } |
1822 | |
|
1823 | 0 | return ret; |
1824 | 0 | } |
1825 | | |
1826 | | /* ---------------------------------------------------------------------------------------------------- */ |
1827 | | |
1828 | 0 | #define G_DBUS_DEBUG_AUTHENTICATION (1<<0) |
1829 | 0 | #define G_DBUS_DEBUG_TRANSPORT (1<<1) |
1830 | 0 | #define G_DBUS_DEBUG_MESSAGE (1<<2) |
1831 | 0 | #define G_DBUS_DEBUG_PAYLOAD (1<<3) |
1832 | 0 | #define G_DBUS_DEBUG_CALL (1<<4) |
1833 | 0 | #define G_DBUS_DEBUG_SIGNAL (1<<5) |
1834 | 0 | #define G_DBUS_DEBUG_INCOMING (1<<6) |
1835 | 0 | #define G_DBUS_DEBUG_RETURN (1<<7) |
1836 | 0 | #define G_DBUS_DEBUG_EMISSION (1<<8) |
1837 | 0 | #define G_DBUS_DEBUG_ADDRESS (1<<9) |
1838 | 0 | #define G_DBUS_DEBUG_PROXY (1<<10) |
1839 | | |
1840 | | static gint _gdbus_debug_flags = 0; |
1841 | | |
1842 | | gboolean |
1843 | | _g_dbus_debug_authentication (void) |
1844 | 0 | { |
1845 | 0 | _g_dbus_initialize (); |
1846 | 0 | return (_gdbus_debug_flags & G_DBUS_DEBUG_AUTHENTICATION) != 0; |
1847 | 0 | } |
1848 | | |
1849 | | gboolean |
1850 | | _g_dbus_debug_transport (void) |
1851 | 0 | { |
1852 | 0 | _g_dbus_initialize (); |
1853 | 0 | return (_gdbus_debug_flags & G_DBUS_DEBUG_TRANSPORT) != 0; |
1854 | 0 | } |
1855 | | |
1856 | | gboolean |
1857 | | _g_dbus_debug_message (void) |
1858 | 0 | { |
1859 | 0 | _g_dbus_initialize (); |
1860 | 0 | return (_gdbus_debug_flags & G_DBUS_DEBUG_MESSAGE) != 0; |
1861 | 0 | } |
1862 | | |
1863 | | gboolean |
1864 | | _g_dbus_debug_payload (void) |
1865 | 0 | { |
1866 | 0 | _g_dbus_initialize (); |
1867 | 0 | return (_gdbus_debug_flags & G_DBUS_DEBUG_PAYLOAD) != 0; |
1868 | 0 | } |
1869 | | |
1870 | | gboolean |
1871 | | _g_dbus_debug_call (void) |
1872 | 0 | { |
1873 | 0 | _g_dbus_initialize (); |
1874 | 0 | return (_gdbus_debug_flags & G_DBUS_DEBUG_CALL) != 0; |
1875 | 0 | } |
1876 | | |
1877 | | gboolean |
1878 | | _g_dbus_debug_signal (void) |
1879 | 0 | { |
1880 | 0 | _g_dbus_initialize (); |
1881 | 0 | return (_gdbus_debug_flags & G_DBUS_DEBUG_SIGNAL) != 0; |
1882 | 0 | } |
1883 | | |
1884 | | gboolean |
1885 | | _g_dbus_debug_incoming (void) |
1886 | 0 | { |
1887 | 0 | _g_dbus_initialize (); |
1888 | 0 | return (_gdbus_debug_flags & G_DBUS_DEBUG_INCOMING) != 0; |
1889 | 0 | } |
1890 | | |
1891 | | gboolean |
1892 | | _g_dbus_debug_return (void) |
1893 | 0 | { |
1894 | 0 | _g_dbus_initialize (); |
1895 | 0 | return (_gdbus_debug_flags & G_DBUS_DEBUG_RETURN) != 0; |
1896 | 0 | } |
1897 | | |
1898 | | gboolean |
1899 | | _g_dbus_debug_emission (void) |
1900 | 0 | { |
1901 | 0 | _g_dbus_initialize (); |
1902 | 0 | return (_gdbus_debug_flags & G_DBUS_DEBUG_EMISSION) != 0; |
1903 | 0 | } |
1904 | | |
1905 | | gboolean |
1906 | | _g_dbus_debug_address (void) |
1907 | 0 | { |
1908 | 0 | _g_dbus_initialize (); |
1909 | 0 | return (_gdbus_debug_flags & G_DBUS_DEBUG_ADDRESS) != 0; |
1910 | 0 | } |
1911 | | |
1912 | | gboolean |
1913 | | _g_dbus_debug_proxy (void) |
1914 | 0 | { |
1915 | 0 | _g_dbus_initialize (); |
1916 | 0 | return (_gdbus_debug_flags & G_DBUS_DEBUG_PROXY) != 0; |
1917 | 0 | } |
1918 | | |
1919 | | G_LOCK_DEFINE_STATIC (print_lock); |
1920 | | |
1921 | | void |
1922 | | _g_dbus_debug_print_lock (void) |
1923 | 0 | { |
1924 | 0 | G_LOCK (print_lock); |
1925 | 0 | } |
1926 | | |
1927 | | void |
1928 | | _g_dbus_debug_print_unlock (void) |
1929 | 0 | { |
1930 | 0 | G_UNLOCK (print_lock); |
1931 | 0 | } |
1932 | | |
1933 | | /** |
1934 | | * _g_dbus_initialize: |
1935 | | * |
1936 | | * Does various one-time init things such as |
1937 | | * |
1938 | | * - registering the G_DBUS_ERROR error domain |
1939 | | * - parses the G_DBUS_DEBUG environment variable |
1940 | | */ |
1941 | | void |
1942 | | _g_dbus_initialize (void) |
1943 | 0 | { |
1944 | 0 | static gsize initialized = 0; |
1945 | |
|
1946 | 0 | if (g_once_init_enter (&initialized)) |
1947 | 0 | { |
1948 | 0 | const gchar *debug; |
1949 | | |
1950 | | /* Ensure the domain is registered. */ |
1951 | 0 | g_dbus_error_quark (); |
1952 | |
|
1953 | 0 | debug = g_getenv ("G_DBUS_DEBUG"); |
1954 | 0 | if (debug != NULL) |
1955 | 0 | { |
1956 | 0 | const GDebugKey keys[] = { |
1957 | 0 | { "authentication", G_DBUS_DEBUG_AUTHENTICATION }, |
1958 | 0 | { "transport", G_DBUS_DEBUG_TRANSPORT }, |
1959 | 0 | { "message", G_DBUS_DEBUG_MESSAGE }, |
1960 | 0 | { "payload", G_DBUS_DEBUG_PAYLOAD }, |
1961 | 0 | { "call", G_DBUS_DEBUG_CALL }, |
1962 | 0 | { "signal", G_DBUS_DEBUG_SIGNAL }, |
1963 | 0 | { "incoming", G_DBUS_DEBUG_INCOMING }, |
1964 | 0 | { "return", G_DBUS_DEBUG_RETURN }, |
1965 | 0 | { "emission", G_DBUS_DEBUG_EMISSION }, |
1966 | 0 | { "address", G_DBUS_DEBUG_ADDRESS }, |
1967 | 0 | { "proxy", G_DBUS_DEBUG_PROXY } |
1968 | 0 | }; |
1969 | |
|
1970 | 0 | _gdbus_debug_flags = g_parse_debug_string (debug, keys, G_N_ELEMENTS (keys)); |
1971 | 0 | if (_gdbus_debug_flags & G_DBUS_DEBUG_PAYLOAD) |
1972 | 0 | _gdbus_debug_flags |= G_DBUS_DEBUG_MESSAGE; |
1973 | 0 | } |
1974 | | |
1975 | | /* Work-around for https://bugzilla.gnome.org/show_bug.cgi?id=627724 */ |
1976 | 0 | ensure_required_types (); |
1977 | |
|
1978 | 0 | g_once_init_leave (&initialized, 1); |
1979 | 0 | } |
1980 | 0 | } |
1981 | | |
1982 | | /* ---------------------------------------------------------------------------------------------------- */ |
1983 | | |
1984 | | GVariantType * |
1985 | | _g_dbus_compute_complete_signature (GDBusArgInfo **args) |
1986 | 0 | { |
1987 | 0 | const GVariantType *arg_types[256]; |
1988 | 0 | guint n; |
1989 | |
|
1990 | 0 | if (args) |
1991 | 0 | for (n = 0; args[n] != NULL; n++) |
1992 | 0 | { |
1993 | | /* DBus places a hard limit of 255 on signature length. |
1994 | | * therefore number of args must be less than 256. |
1995 | | */ |
1996 | 0 | g_assert (n < 256); |
1997 | | |
1998 | 0 | arg_types[n] = G_VARIANT_TYPE (args[n]->signature); |
1999 | |
|
2000 | 0 | if G_UNLIKELY (arg_types[n] == NULL) |
2001 | 0 | return NULL; |
2002 | 0 | } |
2003 | 0 | else |
2004 | 0 | n = 0; |
2005 | | |
2006 | 0 | return g_variant_type_new_tuple (arg_types, n); |
2007 | 0 | } |
2008 | | |
2009 | | /* ---------------------------------------------------------------------------------------------------- */ |
2010 | | |
2011 | | #ifdef G_OS_WIN32 |
2012 | | |
2013 | | extern BOOL WINAPI ConvertSidToStringSidA (PSID Sid, LPSTR *StringSid); |
2014 | | |
2015 | | gchar * |
2016 | | _g_dbus_win32_get_user_sid (void) |
2017 | | { |
2018 | | HANDLE h; |
2019 | | TOKEN_USER *user; |
2020 | | DWORD token_information_len; |
2021 | | PSID psid; |
2022 | | gchar *sid; |
2023 | | gchar *ret; |
2024 | | |
2025 | | ret = NULL; |
2026 | | user = NULL; |
2027 | | h = INVALID_HANDLE_VALUE; |
2028 | | |
2029 | | if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &h)) |
2030 | | { |
2031 | | g_warning ("OpenProcessToken failed with error code %d", (gint) GetLastError ()); |
2032 | | goto out; |
2033 | | } |
2034 | | |
2035 | | /* Get length of buffer */ |
2036 | | token_information_len = 0; |
2037 | | if (!GetTokenInformation (h, TokenUser, NULL, 0, &token_information_len)) |
2038 | | { |
2039 | | if (GetLastError () != ERROR_INSUFFICIENT_BUFFER) |
2040 | | { |
2041 | | g_warning ("GetTokenInformation() failed with error code %d", (gint) GetLastError ()); |
2042 | | goto out; |
2043 | | } |
2044 | | } |
2045 | | user = g_malloc (token_information_len); |
2046 | | if (!GetTokenInformation (h, TokenUser, user, token_information_len, &token_information_len)) |
2047 | | { |
2048 | | g_warning ("GetTokenInformation() failed with error code %d", (gint) GetLastError ()); |
2049 | | goto out; |
2050 | | } |
2051 | | |
2052 | | psid = user->User.Sid; |
2053 | | if (!IsValidSid (psid)) |
2054 | | { |
2055 | | g_warning ("Invalid SID"); |
2056 | | goto out; |
2057 | | } |
2058 | | |
2059 | | if (!ConvertSidToStringSidA (psid, &sid)) |
2060 | | { |
2061 | | g_warning ("Invalid SID"); |
2062 | | goto out; |
2063 | | } |
2064 | | |
2065 | | ret = g_strdup (sid); |
2066 | | LocalFree (sid); |
2067 | | |
2068 | | out: |
2069 | | g_free (user); |
2070 | | if (h != INVALID_HANDLE_VALUE) |
2071 | | CloseHandle (h); |
2072 | | return ret; |
2073 | | } |
2074 | | |
2075 | | |
2076 | | #define DBUS_DAEMON_ADDRESS_INFO "DBusDaemonAddressInfo" |
2077 | | #define DBUS_DAEMON_MUTEX "DBusDaemonMutex" |
2078 | | #define UNIQUE_DBUS_INIT_MUTEX "UniqueDBusInitMutex" |
2079 | | #define DBUS_AUTOLAUNCH_MUTEX "DBusAutolaunchMutex" |
2080 | | |
2081 | | static void |
2082 | | release_mutex (HANDLE mutex) |
2083 | | { |
2084 | | ReleaseMutex (mutex); |
2085 | | CloseHandle (mutex); |
2086 | | } |
2087 | | |
2088 | | static HANDLE |
2089 | | acquire_mutex (const char *mutexname) |
2090 | | { |
2091 | | HANDLE mutex; |
2092 | | DWORD res; |
2093 | | |
2094 | | mutex = CreateMutexA (NULL, FALSE, mutexname); |
2095 | | if (!mutex) |
2096 | | return 0; |
2097 | | |
2098 | | res = WaitForSingleObject (mutex, INFINITE); |
2099 | | switch (res) |
2100 | | { |
2101 | | case WAIT_ABANDONED: |
2102 | | release_mutex (mutex); |
2103 | | return 0; |
2104 | | case WAIT_FAILED: |
2105 | | case WAIT_TIMEOUT: |
2106 | | return 0; |
2107 | | } |
2108 | | |
2109 | | return mutex; |
2110 | | } |
2111 | | |
2112 | | static gboolean |
2113 | | is_mutex_owned (const char *mutexname) |
2114 | | { |
2115 | | HANDLE mutex; |
2116 | | gboolean res = FALSE; |
2117 | | |
2118 | | mutex = CreateMutexA (NULL, FALSE, mutexname); |
2119 | | if (WaitForSingleObject (mutex, 10) == WAIT_TIMEOUT) |
2120 | | res = TRUE; |
2121 | | else |
2122 | | ReleaseMutex (mutex); |
2123 | | CloseHandle (mutex); |
2124 | | |
2125 | | return res; |
2126 | | } |
2127 | | |
2128 | | static char * |
2129 | | read_shm (const char *shm_name) |
2130 | | { |
2131 | | HANDLE shared_mem; |
2132 | | char *shared_data; |
2133 | | char *res; |
2134 | | int i; |
2135 | | |
2136 | | res = NULL; |
2137 | | |
2138 | | for (i = 0; i < 20; i++) |
2139 | | { |
2140 | | shared_mem = OpenFileMappingA (FILE_MAP_READ, FALSE, shm_name); |
2141 | | if (shared_mem != 0) |
2142 | | break; |
2143 | | Sleep (100); |
2144 | | } |
2145 | | |
2146 | | if (shared_mem != 0) |
2147 | | { |
2148 | | shared_data = MapViewOfFile (shared_mem, FILE_MAP_READ, 0, 0, 0); |
2149 | | /* It looks that a race is possible here: |
2150 | | * if the dbus process already created mapping but didn't fill it |
2151 | | * the code below may read incorrect address. |
2152 | | * Also this is a bit complicated by the fact that |
2153 | | * any change in the "synchronization contract" between processes |
2154 | | * should be accompanied with renaming all of used win32 named objects: |
2155 | | * otherwise libgio-2.0-0.dll of different versions shipped with |
2156 | | * different apps may break each other due to protocol difference. |
2157 | | */ |
2158 | | if (shared_data != NULL) |
2159 | | { |
2160 | | res = g_strdup (shared_data); |
2161 | | UnmapViewOfFile (shared_data); |
2162 | | } |
2163 | | CloseHandle (shared_mem); |
2164 | | } |
2165 | | |
2166 | | return res; |
2167 | | } |
2168 | | |
2169 | | static HANDLE |
2170 | | set_shm (const char *shm_name, const char *value) |
2171 | | { |
2172 | | HANDLE shared_mem; |
2173 | | char *shared_data; |
2174 | | |
2175 | | shared_mem = CreateFileMappingA (INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, |
2176 | | 0, strlen (value) + 1, shm_name); |
2177 | | if (shared_mem == 0) |
2178 | | return 0; |
2179 | | |
2180 | | shared_data = MapViewOfFile (shared_mem, FILE_MAP_WRITE, 0, 0, 0 ); |
2181 | | if (shared_data == NULL) |
2182 | | return 0; |
2183 | | |
2184 | | strcpy (shared_data, value); |
2185 | | |
2186 | | UnmapViewOfFile (shared_data); |
2187 | | |
2188 | | return shared_mem; |
2189 | | } |
2190 | | |
2191 | | /* These keep state between publish_session_bus and unpublish_session_bus */ |
2192 | | static HANDLE published_daemon_mutex; |
2193 | | static HANDLE published_shared_mem; |
2194 | | |
2195 | | static gboolean |
2196 | | publish_session_bus (const char *address) |
2197 | | { |
2198 | | HANDLE init_mutex; |
2199 | | |
2200 | | init_mutex = acquire_mutex (UNIQUE_DBUS_INIT_MUTEX); |
2201 | | |
2202 | | published_daemon_mutex = CreateMutexA (NULL, FALSE, DBUS_DAEMON_MUTEX); |
2203 | | if (WaitForSingleObject (published_daemon_mutex, 10 ) != WAIT_OBJECT_0) |
2204 | | { |
2205 | | release_mutex (init_mutex); |
2206 | | CloseHandle (published_daemon_mutex); |
2207 | | published_daemon_mutex = NULL; |
2208 | | return FALSE; |
2209 | | } |
2210 | | |
2211 | | published_shared_mem = set_shm (DBUS_DAEMON_ADDRESS_INFO, address); |
2212 | | if (!published_shared_mem) |
2213 | | { |
2214 | | release_mutex (init_mutex); |
2215 | | CloseHandle (published_daemon_mutex); |
2216 | | published_daemon_mutex = NULL; |
2217 | | return FALSE; |
2218 | | } |
2219 | | |
2220 | | release_mutex (init_mutex); |
2221 | | return TRUE; |
2222 | | } |
2223 | | |
2224 | | static void |
2225 | | unpublish_session_bus (void) |
2226 | | { |
2227 | | HANDLE init_mutex; |
2228 | | |
2229 | | init_mutex = acquire_mutex (UNIQUE_DBUS_INIT_MUTEX); |
2230 | | |
2231 | | CloseHandle (published_shared_mem); |
2232 | | published_shared_mem = NULL; |
2233 | | |
2234 | | release_mutex (published_daemon_mutex); |
2235 | | published_daemon_mutex = NULL; |
2236 | | |
2237 | | release_mutex (init_mutex); |
2238 | | } |
2239 | | |
2240 | | static void |
2241 | | wait_console_window (void) |
2242 | | { |
2243 | | FILE *console = fopen ("CONOUT$", "w"); |
2244 | | |
2245 | | SetConsoleTitleW (L"gdbus-daemon output. Type any character to close this window."); |
2246 | | fprintf (console, _("(Type any character to close this window)\n")); |
2247 | | fflush (console); |
2248 | | _getch (); |
2249 | | } |
2250 | | |
2251 | | static void |
2252 | | open_console_window (void) |
2253 | | { |
2254 | | if (((HANDLE) _get_osfhandle (fileno (stdout)) == INVALID_HANDLE_VALUE || |
2255 | | (HANDLE) _get_osfhandle (fileno (stderr)) == INVALID_HANDLE_VALUE) && AllocConsole ()) |
2256 | | { |
2257 | | if ((HANDLE) _get_osfhandle (fileno (stdout)) == INVALID_HANDLE_VALUE) |
2258 | | freopen ("CONOUT$", "w", stdout); |
2259 | | |
2260 | | if ((HANDLE) _get_osfhandle (fileno (stderr)) == INVALID_HANDLE_VALUE) |
2261 | | freopen ("CONOUT$", "w", stderr); |
2262 | | |
2263 | | SetConsoleTitleW (L"gdbus-daemon debug output."); |
2264 | | |
2265 | | atexit (wait_console_window); |
2266 | | } |
2267 | | } |
2268 | | |
2269 | | static void |
2270 | | idle_timeout_cb (GDBusDaemon *daemon, gpointer user_data) |
2271 | | { |
2272 | | GMainLoop *loop = user_data; |
2273 | | g_main_loop_quit (loop); |
2274 | | } |
2275 | | |
2276 | | /* Satisfies STARTF_FORCEONFEEDBACK */ |
2277 | | static void |
2278 | | turn_off_the_starting_cursor (void) |
2279 | | { |
2280 | | MSG msg; |
2281 | | BOOL bRet; |
2282 | | |
2283 | | PostQuitMessage (0); |
2284 | | |
2285 | | while ((bRet = GetMessage (&msg, 0, 0, 0)) != 0) |
2286 | | { |
2287 | | if (bRet == -1) |
2288 | | continue; |
2289 | | |
2290 | | TranslateMessage (&msg); |
2291 | | DispatchMessage (&msg); |
2292 | | } |
2293 | | } |
2294 | | |
2295 | | __declspec(dllexport) void __stdcall |
2296 | | g_win32_run_session_bus (void* hwnd, void* hinst, const char* cmdline, int cmdshow) |
2297 | | { |
2298 | | GDBusDaemon *daemon; |
2299 | | GMainLoop *loop; |
2300 | | const char *address; |
2301 | | GError *error = NULL; |
2302 | | |
2303 | | turn_off_the_starting_cursor (); |
2304 | | |
2305 | | if (g_getenv ("GDBUS_DAEMON_DEBUG") != NULL) |
2306 | | open_console_window (); |
2307 | | |
2308 | | address = "nonce-tcp:"; |
2309 | | daemon = _g_dbus_daemon_new (address, NULL, &error); |
2310 | | if (daemon == NULL) |
2311 | | { |
2312 | | g_printerr ("Can't init bus: %s\n", error->message); |
2313 | | g_error_free (error); |
2314 | | return; |
2315 | | } |
2316 | | |
2317 | | loop = g_main_loop_new (NULL, FALSE); |
2318 | | |
2319 | | /* There is a subtle detail with "idle-timeout" signal of dbus daemon: |
2320 | | * It is fired on idle after last client disconnection, |
2321 | | * but (at least with glib 2.59.1) it is NEVER fired |
2322 | | * if no clients connect to daemon at all. |
2323 | | * This may lead to infinite run of this daemon process. |
2324 | | */ |
2325 | | g_signal_connect (daemon, "idle-timeout", G_CALLBACK (idle_timeout_cb), loop); |
2326 | | |
2327 | | if (publish_session_bus (_g_dbus_daemon_get_address (daemon))) |
2328 | | { |
2329 | | g_main_loop_run (loop); |
2330 | | |
2331 | | unpublish_session_bus (); |
2332 | | } |
2333 | | |
2334 | | g_main_loop_unref (loop); |
2335 | | g_object_unref (daemon); |
2336 | | } |
2337 | | |
2338 | | static gboolean autolaunch_binary_absent = FALSE; |
2339 | | |
2340 | | gchar * |
2341 | | _g_dbus_win32_get_session_address_dbus_launch (GError **error) |
2342 | | { |
2343 | | HANDLE autolaunch_mutex, init_mutex; |
2344 | | char *address = NULL; |
2345 | | |
2346 | | autolaunch_mutex = acquire_mutex (DBUS_AUTOLAUNCH_MUTEX); |
2347 | | |
2348 | | init_mutex = acquire_mutex (UNIQUE_DBUS_INIT_MUTEX); |
2349 | | |
2350 | | if (is_mutex_owned (DBUS_DAEMON_MUTEX)) |
2351 | | address = read_shm (DBUS_DAEMON_ADDRESS_INFO); |
2352 | | |
2353 | | release_mutex (init_mutex); |
2354 | | |
2355 | | if (address == NULL && !autolaunch_binary_absent) |
2356 | | { |
2357 | | wchar_t gio_path[MAX_PATH + 2] = { 0 }; |
2358 | | int gio_path_len = GetModuleFileNameW (_g_io_win32_get_module (), gio_path, MAX_PATH + 1); |
2359 | | |
2360 | | /* The <= MAX_PATH check prevents truncated path usage */ |
2361 | | if (gio_path_len > 0 && gio_path_len <= MAX_PATH) |
2362 | | { |
2363 | | PROCESS_INFORMATION pi = { 0 }; |
2364 | | STARTUPINFOW si = { 0 }; |
2365 | | BOOL res = FALSE; |
2366 | | wchar_t exe_path[MAX_PATH + 100] = { 0 }; |
2367 | | /* calculate index of first char of dll file name inside full path */ |
2368 | | int gio_name_index = gio_path_len; |
2369 | | for (; gio_name_index > 0; --gio_name_index) |
2370 | | { |
2371 | | wchar_t prev_char = gio_path[gio_name_index - 1]; |
2372 | | if (prev_char == L'\\' || prev_char == L'/') |
2373 | | break; |
2374 | | } |
2375 | | gio_path[gio_name_index] = L'\0'; |
2376 | | wcscpy (exe_path, gio_path); |
2377 | | wcscat (exe_path, L"\\gdbus.exe"); |
2378 | | |
2379 | | if (GetFileAttributesW (exe_path) == INVALID_FILE_ATTRIBUTES) |
2380 | | { |
2381 | | /* warning won't be raised another time |
2382 | | * since autolaunch_binary_absent would be already set. |
2383 | | */ |
2384 | | autolaunch_binary_absent = TRUE; |
2385 | | g_warning ("win32 session dbus binary not found: %S", exe_path ); |
2386 | | } |
2387 | | else |
2388 | | { |
2389 | | wchar_t args[MAX_PATH*2 + 100] = { 0 }; |
2390 | | wcscpy (args, L"\""); |
2391 | | wcscat (args, exe_path); |
2392 | | wcscat (args, L"\" "); |
2393 | | #define _L_PREFIX_FOR_EXPANDED(arg) L##arg |
2394 | | #define _L_PREFIX(arg) _L_PREFIX_FOR_EXPANDED (arg) |
2395 | | wcscat (args, _L_PREFIX (_GDBUS_ARG_WIN32_RUN_SESSION_BUS)); |
2396 | | #undef _L_PREFIX |
2397 | | #undef _L_PREFIX_FOR_EXPANDED |
2398 | | |
2399 | | res = CreateProcessW (exe_path, args, |
2400 | | 0, 0, FALSE, |
2401 | | NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW | DETACHED_PROCESS, |
2402 | | 0, gio_path, |
2403 | | &si, &pi); |
2404 | | } |
2405 | | if (res) |
2406 | | { |
2407 | | address = read_shm (DBUS_DAEMON_ADDRESS_INFO); |
2408 | | if (address == NULL) |
2409 | | g_warning ("%S dbus binary failed to launch bus, maybe incompatible version", exe_path ); |
2410 | | } |
2411 | | } |
2412 | | } |
2413 | | |
2414 | | release_mutex (autolaunch_mutex); |
2415 | | |
2416 | | if (address == NULL) |
2417 | | g_set_error (error, |
2418 | | G_IO_ERROR, |
2419 | | G_IO_ERROR_FAILED, |
2420 | | _("Session dbus not running, and autolaunch failed")); |
2421 | | |
2422 | | return address; |
2423 | | } |
2424 | | |
2425 | | #endif |
2426 | | |
2427 | | /* ---------------------------------------------------------------------------------------------------- */ |
2428 | | |
2429 | | gchar * |
2430 | | _g_dbus_get_machine_id (GError **error) |
2431 | 0 | { |
2432 | | #ifdef G_OS_WIN32 |
2433 | | HW_PROFILE_INFOA info; |
2434 | | char *src, *dest, *res; |
2435 | | int i; |
2436 | | |
2437 | | if (!GetCurrentHwProfileA (&info)) |
2438 | | { |
2439 | | char *message = g_win32_error_message (GetLastError ()); |
2440 | | g_set_error (error, |
2441 | | G_IO_ERROR, |
2442 | | G_IO_ERROR_FAILED, |
2443 | | _("Unable to get Hardware profile: %s"), message); |
2444 | | g_free (message); |
2445 | | return NULL; |
2446 | | } |
2447 | | |
2448 | | /* Form: {12340001-4980-1920-6788-123456789012} */ |
2449 | | src = &info.szHwProfileGuid[0]; |
2450 | | |
2451 | | res = g_malloc (32+1); |
2452 | | dest = res; |
2453 | | |
2454 | | src++; /* Skip { */ |
2455 | | for (i = 0; i < 8; i++) |
2456 | | *dest++ = *src++; |
2457 | | src++; /* Skip - */ |
2458 | | for (i = 0; i < 4; i++) |
2459 | | *dest++ = *src++; |
2460 | | src++; /* Skip - */ |
2461 | | for (i = 0; i < 4; i++) |
2462 | | *dest++ = *src++; |
2463 | | src++; /* Skip - */ |
2464 | | for (i = 0; i < 4; i++) |
2465 | | *dest++ = *src++; |
2466 | | src++; /* Skip - */ |
2467 | | for (i = 0; i < 12; i++) |
2468 | | *dest++ = *src++; |
2469 | | *dest = 0; |
2470 | | |
2471 | | return res; |
2472 | | #else |
2473 | 0 | gchar *ret = NULL; |
2474 | 0 | GError *first_error = NULL; |
2475 | 0 | gsize i; |
2476 | 0 | gboolean non_zero = FALSE; |
2477 | | |
2478 | | /* Copy what dbus.git does: allow the /var/lib path to be configurable at |
2479 | | * build time, but hard-code the system-wide machine ID path in /etc. */ |
2480 | 0 | const gchar *var_lib_path = LOCALSTATEDIR "/lib/dbus/machine-id"; |
2481 | 0 | const gchar *etc_path = "/etc/machine-id"; |
2482 | |
|
2483 | 0 | if (!g_file_get_contents (var_lib_path, |
2484 | 0 | &ret, |
2485 | 0 | NULL, |
2486 | 0 | &first_error) && |
2487 | 0 | !g_file_get_contents (etc_path, |
2488 | 0 | &ret, |
2489 | 0 | NULL, |
2490 | 0 | NULL)) |
2491 | 0 | { |
2492 | 0 | g_propagate_prefixed_error (error, g_steal_pointer (&first_error), |
2493 | | /* Translators: Both placeholders are file paths */ |
2494 | 0 | _("Unable to load %s or %s: "), |
2495 | 0 | var_lib_path, etc_path); |
2496 | 0 | return NULL; |
2497 | 0 | } |
2498 | | |
2499 | | /* ignore the error from the first try, if any */ |
2500 | 0 | g_clear_error (&first_error); |
2501 | | |
2502 | | /* Validate the machine ID. From `man 5 machine-id`: |
2503 | | * > The machine ID is a single newline-terminated, hexadecimal, 32-character, |
2504 | | * > lowercase ID. When decoded from hexadecimal, this corresponds to a |
2505 | | * > 16-byte/128-bit value. This ID may not be all zeros. |
2506 | | */ |
2507 | 0 | for (i = 0; ret[i] != '\0' && ret[i] != '\n'; i++) |
2508 | 0 | { |
2509 | | /* Break early if it’s invalid. */ |
2510 | 0 | if (!g_ascii_isxdigit (ret[i]) || g_ascii_isupper (ret[i])) |
2511 | 0 | break; |
2512 | | |
2513 | 0 | if (ret[i] != '0') |
2514 | 0 | non_zero = TRUE; |
2515 | 0 | } |
2516 | |
|
2517 | 0 | if (i != 32 || ret[i] != '\n' || ret[i + 1] != '\0' || !non_zero) |
2518 | 0 | { |
2519 | 0 | g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
2520 | 0 | "Invalid machine ID in %s or %s", |
2521 | 0 | var_lib_path, etc_path); |
2522 | 0 | g_free (ret); |
2523 | 0 | return NULL; |
2524 | 0 | } |
2525 | | |
2526 | | /* Strip trailing newline. */ |
2527 | 0 | ret[32] = '\0'; |
2528 | |
|
2529 | 0 | return g_steal_pointer (&ret); |
2530 | 0 | #endif |
2531 | 0 | } |
2532 | | |
2533 | | /* ---------------------------------------------------------------------------------------------------- */ |
2534 | | |
2535 | | gchar * |
2536 | | _g_dbus_enum_to_string (GType enum_type, gint value) |
2537 | 0 | { |
2538 | 0 | gchar *ret; |
2539 | 0 | GEnumClass *klass; |
2540 | 0 | GEnumValue *enum_value; |
2541 | |
|
2542 | 0 | klass = g_type_class_ref (enum_type); |
2543 | 0 | enum_value = g_enum_get_value (klass, value); |
2544 | 0 | if (enum_value != NULL) |
2545 | 0 | ret = g_strdup (enum_value->value_nick); |
2546 | 0 | else |
2547 | 0 | ret = g_strdup_printf ("unknown (value %d)", value); |
2548 | 0 | g_type_class_unref (klass); |
2549 | 0 | return ret; |
2550 | 0 | } |
2551 | | |
2552 | | /* ---------------------------------------------------------------------------------------------------- */ |
2553 | | |
2554 | | static void |
2555 | | write_message_print_transport_debug (gssize bytes_written, |
2556 | | MessageToWriteData *data) |
2557 | 0 | { |
2558 | 0 | if (G_LIKELY (!_g_dbus_debug_transport ())) |
2559 | 0 | goto out; |
2560 | | |
2561 | 0 | _g_dbus_debug_print_lock (); |
2562 | 0 | g_print ("========================================================================\n" |
2563 | 0 | "GDBus-debug:Transport:\n" |
2564 | 0 | " >>>> WROTE %" G_GSSIZE_FORMAT " bytes of message with serial %d and\n" |
2565 | 0 | " size %" G_GSIZE_FORMAT " from offset %" G_GSIZE_FORMAT " on a %s\n", |
2566 | 0 | bytes_written, |
2567 | 0 | g_dbus_message_get_serial (data->message), |
2568 | 0 | data->blob_size, |
2569 | 0 | data->total_written, |
2570 | 0 | g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream)))); |
2571 | 0 | _g_dbus_debug_print_unlock (); |
2572 | 0 | out: |
2573 | 0 | ; |
2574 | 0 | } |
2575 | | |
2576 | | /* ---------------------------------------------------------------------------------------------------- */ |
2577 | | |
2578 | | static void |
2579 | | read_message_print_transport_debug (gssize bytes_read, |
2580 | | GDBusWorker *worker) |
2581 | 0 | { |
2582 | 0 | gsize size; |
2583 | 0 | gint32 serial; |
2584 | 0 | gint32 message_length; |
2585 | |
|
2586 | 0 | if (G_LIKELY (!_g_dbus_debug_transport ())) |
2587 | 0 | goto out; |
2588 | | |
2589 | 0 | size = bytes_read + worker->read_buffer_cur_size; |
2590 | 0 | serial = 0; |
2591 | 0 | message_length = 0; |
2592 | 0 | if (size >= 16) |
2593 | 0 | message_length = g_dbus_message_bytes_needed ((guchar *) worker->read_buffer, size, NULL); |
2594 | 0 | if (size >= 1) |
2595 | 0 | { |
2596 | 0 | switch (worker->read_buffer[0]) |
2597 | 0 | { |
2598 | 0 | case 'l': |
2599 | 0 | if (size >= 12) |
2600 | 0 | serial = GUINT32_FROM_LE (((guint32 *) worker->read_buffer)[2]); |
2601 | 0 | break; |
2602 | 0 | case 'B': |
2603 | 0 | if (size >= 12) |
2604 | 0 | serial = GUINT32_FROM_BE (((guint32 *) worker->read_buffer)[2]); |
2605 | 0 | break; |
2606 | 0 | default: |
2607 | | /* an error will be set elsewhere if this happens */ |
2608 | 0 | goto out; |
2609 | 0 | } |
2610 | 0 | } |
2611 | | |
2612 | 0 | _g_dbus_debug_print_lock (); |
2613 | 0 | g_print ("========================================================================\n" |
2614 | 0 | "GDBus-debug:Transport:\n" |
2615 | 0 | " <<<< READ %" G_GSSIZE_FORMAT " bytes of message with serial %d and\n" |
2616 | 0 | " size %d to offset %" G_GSIZE_FORMAT " from a %s\n", |
2617 | 0 | bytes_read, |
2618 | 0 | serial, |
2619 | 0 | message_length, |
2620 | 0 | worker->read_buffer_cur_size, |
2621 | 0 | g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_input_stream (worker->stream)))); |
2622 | 0 | _g_dbus_debug_print_unlock (); |
2623 | 0 | out: |
2624 | 0 | ; |
2625 | 0 | } |
2626 | | |
2627 | | /* ---------------------------------------------------------------------------------------------------- */ |
2628 | | |
2629 | | gboolean |
2630 | | _g_signal_accumulator_false_handled (GSignalInvocationHint *ihint, |
2631 | | GValue *return_accu, |
2632 | | const GValue *handler_return, |
2633 | | gpointer dummy) |
2634 | 0 | { |
2635 | 0 | gboolean continue_emission; |
2636 | 0 | gboolean signal_return; |
2637 | |
|
2638 | 0 | signal_return = g_value_get_boolean (handler_return); |
2639 | 0 | g_value_set_boolean (return_accu, signal_return); |
2640 | 0 | continue_emission = signal_return; |
2641 | |
|
2642 | 0 | return continue_emission; |
2643 | 0 | } |
2644 | | |
2645 | | /* ---------------------------------------------------------------------------------------------------- */ |
2646 | | |
2647 | | static void |
2648 | | append_nibble (GString *s, gint val) |
2649 | 0 | { |
2650 | 0 | g_string_append_c (s, val >= 10 ? ('a' + val - 10) : ('0' + val)); |
2651 | 0 | } |
2652 | | |
2653 | | /* ---------------------------------------------------------------------------------------------------- */ |
2654 | | |
2655 | | gchar * |
2656 | | _g_dbus_hexencode (const gchar *str, |
2657 | | gsize str_len) |
2658 | 0 | { |
2659 | 0 | gsize n; |
2660 | 0 | GString *s; |
2661 | |
|
2662 | 0 | s = g_string_new (NULL); |
2663 | 0 | for (n = 0; n < str_len; n++) |
2664 | 0 | { |
2665 | 0 | gint val; |
2666 | 0 | gint upper_nibble; |
2667 | 0 | gint lower_nibble; |
2668 | |
|
2669 | 0 | val = ((const guchar *) str)[n]; |
2670 | 0 | upper_nibble = val >> 4; |
2671 | 0 | lower_nibble = val & 0x0f; |
2672 | |
|
2673 | 0 | append_nibble (s, upper_nibble); |
2674 | 0 | append_nibble (s, lower_nibble); |
2675 | 0 | } |
2676 | |
|
2677 | 0 | return g_string_free (s, FALSE); |
2678 | 0 | } |