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 | { |
70 | guint n, m; |
71 | GString *ret; |
72 | |
73 | ret = g_string_new (NULL); |
74 | |
75 | for (n = 0; n < len; n += 16) |
76 | { |
77 | g_string_append_printf (string: ret, format: "%*s%04x: " , indent, "" , n); |
78 | |
79 | for (m = n; m < n + 16; m++) |
80 | { |
81 | if (m > n && (m%4) == 0) |
82 | g_string_append_c (ret, ' '); |
83 | if (m < len) |
84 | g_string_append_printf (string: ret, format: "%02x " , (guchar) data[m]); |
85 | else |
86 | g_string_append (string: ret, val: " " ); |
87 | } |
88 | |
89 | g_string_append (string: ret, val: " " ); |
90 | |
91 | for (m = n; m < len && m < n + 16; m++) |
92 | g_string_append_c (ret, g_ascii_isprint (data[m]) ? data[m] : '.'); |
93 | |
94 | g_string_append_c (ret, '\n'); |
95 | } |
96 | |
97 | return g_string_free (string: ret, FALSE); |
98 | } |
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 | { |
120 | g_slice_free (ReadWithControlData, data); |
121 | } |
122 | |
123 | static gboolean |
124 | _g_socket_read_with_control_messages_ready (GSocket *socket, |
125 | GIOCondition condition, |
126 | gpointer user_data) |
127 | { |
128 | GTask *task = user_data; |
129 | ReadWithControlData *data = g_task_get_task_data (task); |
130 | GError *error; |
131 | gssize result; |
132 | GInputVector vector; |
133 | |
134 | error = NULL; |
135 | vector.buffer = data->buffer; |
136 | vector.size = data->count; |
137 | result = g_socket_receive_message (socket, |
138 | NULL, /* address */ |
139 | vectors: &vector, |
140 | num_vectors: 1, |
141 | messages: data->messages, |
142 | num_messages: data->num_messages, |
143 | NULL, |
144 | cancellable: g_task_get_cancellable (task), |
145 | error: &error); |
146 | |
147 | if (g_error_matches (error, G_IO_ERROR, code: G_IO_ERROR_WOULD_BLOCK)) |
148 | { |
149 | g_error_free (error); |
150 | return TRUE; |
151 | } |
152 | |
153 | g_assert (result >= 0 || error != NULL); |
154 | if (result >= 0) |
155 | g_task_return_int (task, result); |
156 | else |
157 | g_task_return_error (task, error); |
158 | g_object_unref (object: task); |
159 | |
160 | return FALSE; |
161 | } |
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 | { |
174 | GTask *task; |
175 | ReadWithControlData *data; |
176 | GSource *source; |
177 | |
178 | data = g_slice_new0 (ReadWithControlData); |
179 | data->buffer = buffer; |
180 | data->count = count; |
181 | data->messages = messages; |
182 | data->num_messages = num_messages; |
183 | |
184 | task = g_task_new (source_object: socket, cancellable, callback, callback_data: user_data); |
185 | g_task_set_source_tag (task, _g_socket_read_with_control_messages); |
186 | g_task_set_name (task, name: "[gio] D-Bus read" ); |
187 | g_task_set_task_data (task, task_data: data, task_data_destroy: (GDestroyNotify) read_with_control_data_free); |
188 | |
189 | if (g_socket_condition_check (socket, condition: G_IO_IN)) |
190 | { |
191 | if (!_g_socket_read_with_control_messages_ready (socket, condition: G_IO_IN, user_data: task)) |
192 | return; |
193 | } |
194 | |
195 | source = g_socket_create_source (socket, |
196 | condition: G_IO_IN | G_IO_HUP | G_IO_ERR, |
197 | cancellable); |
198 | g_task_attach_source (task, source, callback: (GSourceFunc) _g_socket_read_with_control_messages_ready); |
199 | g_source_unref (source); |
200 | } |
201 | |
202 | static gssize |
203 | _g_socket_read_with_control_messages_finish (GSocket *socket, |
204 | GAsyncResult *result, |
205 | GError **error) |
206 | { |
207 | g_return_val_if_fail (G_IS_SOCKET (socket), -1); |
208 | g_return_val_if_fail (g_task_is_valid (result, socket), -1); |
209 | |
210 | return g_task_propagate_int (G_TASK (result), error); |
211 | } |
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 | { |
223 | g_ptr_array_add (array: ensured_classes, data: g_type_class_ref (type: gtype)); |
224 | } |
225 | |
226 | static void |
227 | release_required_types (void) |
228 | { |
229 | g_ptr_array_foreach (array: ensured_classes, func: (GFunc) g_type_class_unref, NULL); |
230 | g_ptr_array_unref (array: ensured_classes); |
231 | ensured_classes = NULL; |
232 | } |
233 | |
234 | static void |
235 | ensure_required_types (void) |
236 | { |
237 | g_assert (ensured_classes == NULL); |
238 | 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 | ensure_type (G_TYPE_TASK); |
252 | ensure_type (G_TYPE_MEMORY_INPUT_STREAM); |
253 | ensure_type (gtype: G_TYPE_DBUS_CONNECTION_FLAGS); |
254 | ensure_type (gtype: G_TYPE_DBUS_CAPABILITY_FLAGS); |
255 | ensure_type (G_TYPE_DBUS_AUTH_OBSERVER); |
256 | ensure_type (G_TYPE_DBUS_CONNECTION); |
257 | ensure_type (G_TYPE_DBUS_PROXY); |
258 | ensure_type (gtype: G_TYPE_SOCKET_FAMILY); |
259 | ensure_type (gtype: G_TYPE_SOCKET_TYPE); |
260 | ensure_type (gtype: G_TYPE_SOCKET_PROTOCOL); |
261 | ensure_type (G_TYPE_SOCKET_ADDRESS); |
262 | ensure_type (G_TYPE_SOCKET); |
263 | } |
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 | { |
277 | SharedThreadData *data = user_data; |
278 | |
279 | g_main_context_push_thread_default (context: data->context); |
280 | g_main_loop_run (loop: data->loop); |
281 | g_main_context_pop_thread_default (context: data->context); |
282 | |
283 | release_required_types (); |
284 | |
285 | return NULL; |
286 | } |
287 | |
288 | /* ---------------------------------------------------------------------------------------------------- */ |
289 | |
290 | static SharedThreadData * |
291 | _g_dbus_shared_thread_ref (void) |
292 | { |
293 | static gsize shared_thread_data = 0; |
294 | SharedThreadData *ret; |
295 | |
296 | if (g_once_init_enter (&shared_thread_data)) |
297 | { |
298 | SharedThreadData *data; |
299 | |
300 | data = g_new0 (SharedThreadData, 1); |
301 | data->refcount = 0; |
302 | |
303 | data->context = g_main_context_new (); |
304 | data->loop = g_main_loop_new (context: data->context, FALSE); |
305 | data->thread = g_thread_new (name: "gdbus" , |
306 | func: gdbus_shared_thread_func, |
307 | data); |
308 | /* We can cast between gsize and gpointer safely */ |
309 | g_once_init_leave (&shared_thread_data, (gsize) data); |
310 | } |
311 | |
312 | ret = (SharedThreadData*) shared_thread_data; |
313 | g_atomic_int_inc (&ret->refcount); |
314 | return ret; |
315 | } |
316 | |
317 | static void |
318 | _g_dbus_shared_thread_unref (SharedThreadData *data) |
319 | { |
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 | } |
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 | { |
434 | g_clear_object (&close_data->task); |
435 | |
436 | _g_dbus_worker_unref (worker: close_data->worker); |
437 | g_slice_free (CloseData, close_data); |
438 | } |
439 | |
440 | /* ---------------------------------------------------------------------------------------------------- */ |
441 | |
442 | static GDBusWorker * |
443 | _g_dbus_worker_ref (GDBusWorker *worker) |
444 | { |
445 | g_atomic_int_inc (&worker->ref_count); |
446 | return worker; |
447 | } |
448 | |
449 | static void |
450 | _g_dbus_worker_unref (GDBusWorker *worker) |
451 | { |
452 | if (g_atomic_int_dec_and_test (&worker->ref_count)) |
453 | { |
454 | g_assert (worker->write_pending_flushes == NULL); |
455 | |
456 | _g_dbus_shared_thread_unref (data: worker->shared_thread_data); |
457 | |
458 | g_object_unref (object: worker->stream); |
459 | |
460 | g_mutex_clear (mutex: &worker->read_lock); |
461 | g_object_unref (object: worker->cancellable); |
462 | if (worker->read_fd_list != NULL) |
463 | g_object_unref (object: worker->read_fd_list); |
464 | |
465 | g_queue_free_full (queue: worker->received_messages_while_frozen, free_func: (GDestroyNotify) g_object_unref); |
466 | g_mutex_clear (mutex: &worker->write_lock); |
467 | g_queue_free_full (queue: worker->write_queue, free_func: (GDestroyNotify) message_to_write_data_free); |
468 | g_free (mem: worker->read_buffer); |
469 | |
470 | g_free (mem: worker); |
471 | } |
472 | } |
473 | |
474 | static void |
475 | _g_dbus_worker_emit_disconnected (GDBusWorker *worker, |
476 | gboolean remote_peer_vanished, |
477 | GError *error) |
478 | { |
479 | if (!g_atomic_int_get (&worker->stopped)) |
480 | worker->disconnected_callback (worker, remote_peer_vanished, error, worker->user_data); |
481 | } |
482 | |
483 | static void |
484 | _g_dbus_worker_emit_message_received (GDBusWorker *worker, |
485 | GDBusMessage *message) |
486 | { |
487 | if (!g_atomic_int_get (&worker->stopped)) |
488 | worker->message_received_callback (worker, message, worker->user_data); |
489 | } |
490 | |
491 | static GDBusMessage * |
492 | _g_dbus_worker_emit_message_about_to_be_sent (GDBusWorker *worker, |
493 | GDBusMessage *message) |
494 | { |
495 | GDBusMessage *ret; |
496 | if (!g_atomic_int_get (&worker->stopped)) |
497 | ret = worker->message_about_to_be_sent_callback (worker, g_steal_pointer (&message), worker->user_data); |
498 | else |
499 | ret = g_steal_pointer (&message); |
500 | return ret; |
501 | } |
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 | { |
508 | if (worker->frozen || g_queue_get_length (queue: worker->received_messages_while_frozen) > 0) |
509 | { |
510 | /* queue up */ |
511 | g_queue_push_tail (queue: worker->received_messages_while_frozen, g_steal_pointer (&message)); |
512 | } |
513 | else |
514 | { |
515 | /* not frozen, nor anything in queue */ |
516 | _g_dbus_worker_emit_message_received (worker, message); |
517 | g_clear_object (&message); |
518 | } |
519 | } |
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 | { |
525 | GDBusWorker *worker = user_data; |
526 | GDBusMessage *message; |
527 | |
528 | g_mutex_lock (mutex: &worker->read_lock); |
529 | if (worker->frozen) |
530 | { |
531 | while ((message = g_queue_pop_head (queue: worker->received_messages_while_frozen)) != NULL) |
532 | { |
533 | _g_dbus_worker_emit_message_received (worker, message); |
534 | g_clear_object (&message); |
535 | } |
536 | worker->frozen = FALSE; |
537 | } |
538 | else |
539 | { |
540 | g_assert (g_queue_get_length (worker->received_messages_while_frozen) == 0); |
541 | } |
542 | g_mutex_unlock (mutex: &worker->read_lock); |
543 | return FALSE; |
544 | } |
545 | |
546 | /* can be called from any thread */ |
547 | void |
548 | _g_dbus_worker_unfreeze (GDBusWorker *worker) |
549 | { |
550 | GSource *idle_source; |
551 | idle_source = g_idle_source_new (); |
552 | g_source_set_priority (source: idle_source, G_PRIORITY_DEFAULT); |
553 | g_source_set_callback (source: idle_source, |
554 | func: unfreeze_in_idle_cb, |
555 | data: _g_dbus_worker_ref (worker), |
556 | notify: (GDestroyNotify) _g_dbus_worker_unref); |
557 | g_source_set_name (source: idle_source, name: "[gio] unfreeze_in_idle_cb" ); |
558 | g_source_attach (source: idle_source, context: worker->shared_thread_data->context); |
559 | g_source_unref (source: idle_source); |
560 | } |
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 | { |
572 | GDBusWorker *worker = user_data; |
573 | GError *error; |
574 | gssize bytes_read; |
575 | |
576 | g_mutex_lock (mutex: &worker->read_lock); |
577 | |
578 | /* If already stopped, don't even process the reply */ |
579 | if (g_atomic_int_get (&worker->stopped)) |
580 | goto out; |
581 | |
582 | error = NULL; |
583 | if (worker->socket == NULL) |
584 | bytes_read = g_input_stream_read_finish (stream: g_io_stream_get_input_stream (stream: worker->stream), |
585 | result: res, |
586 | error: &error); |
587 | else |
588 | bytes_read = _g_socket_read_with_control_messages_finish (socket: worker->socket, |
589 | result: res, |
590 | error: &error); |
591 | if (worker->read_num_ancillary_messages > 0) |
592 | { |
593 | gint n; |
594 | for (n = 0; n < worker->read_num_ancillary_messages; n++) |
595 | { |
596 | GSocketControlMessage *control_message = G_SOCKET_CONTROL_MESSAGE (worker->read_ancillary_messages[n]); |
597 | |
598 | if (FALSE) |
599 | { |
600 | } |
601 | #ifdef G_OS_UNIX |
602 | else if (G_IS_UNIX_FD_MESSAGE (control_message)) |
603 | { |
604 | GUnixFDMessage *fd_message; |
605 | gint *fds; |
606 | gint num_fds; |
607 | |
608 | fd_message = G_UNIX_FD_MESSAGE (control_message); |
609 | fds = g_unix_fd_message_steal_fds (message: fd_message, length: &num_fds); |
610 | if (worker->read_fd_list == NULL) |
611 | { |
612 | worker->read_fd_list = g_unix_fd_list_new_from_array (fds, n_fds: num_fds); |
613 | } |
614 | else |
615 | { |
616 | gint n; |
617 | for (n = 0; n < num_fds; n++) |
618 | { |
619 | /* TODO: really want a append_steal() */ |
620 | g_unix_fd_list_append (list: worker->read_fd_list, fd: fds[n], NULL); |
621 | (void) g_close (fd: fds[n], NULL); |
622 | } |
623 | } |
624 | g_free (mem: fds); |
625 | } |
626 | else if (G_IS_UNIX_CREDENTIALS_MESSAGE (control_message)) |
627 | { |
628 | /* do nothing */ |
629 | } |
630 | #endif |
631 | else |
632 | { |
633 | if (error == NULL) |
634 | { |
635 | g_set_error (err: &error, |
636 | G_IO_ERROR, |
637 | code: G_IO_ERROR_FAILED, |
638 | format: "Unexpected ancillary message of type %s received from peer" , |
639 | g_type_name (G_TYPE_FROM_INSTANCE (control_message))); |
640 | _g_dbus_worker_emit_disconnected (worker, TRUE, error); |
641 | g_error_free (error); |
642 | g_object_unref (object: control_message); |
643 | n++; |
644 | while (n < worker->read_num_ancillary_messages) |
645 | g_object_unref (object: worker->read_ancillary_messages[n++]); |
646 | g_free (mem: worker->read_ancillary_messages); |
647 | goto out; |
648 | } |
649 | } |
650 | g_object_unref (object: control_message); |
651 | } |
652 | g_free (mem: worker->read_ancillary_messages); |
653 | } |
654 | |
655 | if (bytes_read == -1) |
656 | { |
657 | if (G_UNLIKELY (_g_dbus_debug_transport ())) |
658 | { |
659 | _g_dbus_debug_print_lock (); |
660 | g_print (format: "========================================================================\n" |
661 | "GDBus-debug:Transport:\n" |
662 | " ---- READ ERROR on stream of type %s:\n" |
663 | " ---- %s %d: %s\n" , |
664 | g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_input_stream (worker->stream))), |
665 | g_quark_to_string (quark: error->domain), error->code, |
666 | error->message); |
667 | _g_dbus_debug_print_unlock (); |
668 | } |
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 | if (worker->close_expected || |
686 | g_error_matches (error, G_IO_ERROR, code: G_IO_ERROR_CANCELLED)) |
687 | _g_dbus_worker_emit_disconnected (worker, FALSE, NULL); |
688 | else |
689 | _g_dbus_worker_emit_disconnected (worker, TRUE, error); |
690 | |
691 | g_error_free (error); |
692 | goto out; |
693 | } |
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 | if (bytes_read == 0) |
708 | { |
709 | g_set_error (err: &error, |
710 | G_IO_ERROR, |
711 | code: G_IO_ERROR_FAILED, |
712 | format: "Underlying GIOStream returned 0 bytes on an async read" ); |
713 | _g_dbus_worker_emit_disconnected (worker, TRUE, error); |
714 | g_error_free (error); |
715 | goto out; |
716 | } |
717 | |
718 | read_message_print_transport_debug (bytes_read, worker); |
719 | |
720 | worker->read_buffer_cur_size += bytes_read; |
721 | if (worker->read_buffer_bytes_wanted == worker->read_buffer_cur_size) |
722 | { |
723 | /* OK, got what we asked for! */ |
724 | if (worker->read_buffer_bytes_wanted == 16) |
725 | { |
726 | gssize message_len; |
727 | /* OK, got the header - determine how many more bytes are needed */ |
728 | error = NULL; |
729 | message_len = g_dbus_message_bytes_needed (blob: (guchar *) worker->read_buffer, |
730 | blob_len: 16, |
731 | error: &error); |
732 | if (message_len == -1) |
733 | { |
734 | g_warning ("_g_dbus_worker_do_read_cb: error determining bytes needed: %s" , error->message); |
735 | _g_dbus_worker_emit_disconnected (worker, FALSE, error); |
736 | g_error_free (error); |
737 | goto out; |
738 | } |
739 | |
740 | worker->read_buffer_bytes_wanted = message_len; |
741 | _g_dbus_worker_do_read_unlocked (worker); |
742 | } |
743 | else |
744 | { |
745 | GDBusMessage *message; |
746 | error = NULL; |
747 | |
748 | /* TODO: use connection->priv->auth to decode the message */ |
749 | |
750 | message = g_dbus_message_new_from_blob (blob: (guchar *) worker->read_buffer, |
751 | blob_len: worker->read_buffer_cur_size, |
752 | capabilities: worker->capabilities, |
753 | error: &error); |
754 | if (message == NULL) |
755 | { |
756 | gchar *s; |
757 | s = _g_dbus_hexdump (data: worker->read_buffer, len: worker->read_buffer_cur_size, indent: 2); |
758 | g_warning ("Error decoding D-Bus message of %" G_GSIZE_FORMAT " bytes\n" |
759 | "The error is: %s\n" |
760 | "The payload is as follows:\n" |
761 | "%s" , |
762 | worker->read_buffer_cur_size, |
763 | error->message, |
764 | s); |
765 | g_free (mem: s); |
766 | _g_dbus_worker_emit_disconnected (worker, FALSE, error); |
767 | g_error_free (error); |
768 | goto out; |
769 | } |
770 | |
771 | #ifdef G_OS_UNIX |
772 | if (worker->read_fd_list != NULL) |
773 | { |
774 | g_dbus_message_set_unix_fd_list (message, fd_list: worker->read_fd_list); |
775 | g_object_unref (object: worker->read_fd_list); |
776 | worker->read_fd_list = NULL; |
777 | } |
778 | #endif |
779 | |
780 | if (G_UNLIKELY (_g_dbus_debug_message ())) |
781 | { |
782 | gchar *s; |
783 | _g_dbus_debug_print_lock (); |
784 | g_print (format: "========================================================================\n" |
785 | "GDBus-debug:Message:\n" |
786 | " <<<< RECEIVED D-Bus message (%" G_GSIZE_FORMAT " bytes)\n" , |
787 | worker->read_buffer_cur_size); |
788 | s = g_dbus_message_print (message, indent: 2); |
789 | g_print (format: "%s" , s); |
790 | g_free (mem: s); |
791 | if (G_UNLIKELY (_g_dbus_debug_payload ())) |
792 | { |
793 | s = _g_dbus_hexdump (data: worker->read_buffer, len: worker->read_buffer_cur_size, indent: 2); |
794 | g_print (format: "%s\n" , s); |
795 | g_free (mem: s); |
796 | } |
797 | _g_dbus_debug_print_unlock (); |
798 | } |
799 | |
800 | /* yay, got a message, go deliver it */ |
801 | _g_dbus_worker_queue_or_deliver_received_message (worker, g_steal_pointer (&message)); |
802 | |
803 | /* start reading another message! */ |
804 | worker->read_buffer_bytes_wanted = 0; |
805 | worker->read_buffer_cur_size = 0; |
806 | _g_dbus_worker_do_read_unlocked (worker); |
807 | } |
808 | } |
809 | else |
810 | { |
811 | /* didn't get all the bytes we requested - so repeat the request... */ |
812 | _g_dbus_worker_do_read_unlocked (worker); |
813 | } |
814 | |
815 | out: |
816 | g_mutex_unlock (mutex: &worker->read_lock); |
817 | |
818 | /* check if there is any pending close */ |
819 | schedule_pending_close (worker); |
820 | |
821 | /* gives up the reference acquired when calling g_input_stream_read_async() */ |
822 | _g_dbus_worker_unref (worker); |
823 | } |
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 | { |
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 | if (worker->read_buffer_bytes_wanted == 0) |
835 | { |
836 | worker->read_buffer_cur_size = 0; |
837 | worker->read_buffer_bytes_wanted = 16; |
838 | } |
839 | |
840 | /* ensure we have a (big enough) buffer */ |
841 | if (worker->read_buffer == NULL || worker->read_buffer_bytes_wanted > worker->read_buffer_allocated_size) |
842 | { |
843 | /* TODO: 4096 is randomly chosen; might want a better chosen default minimum */ |
844 | worker->read_buffer_allocated_size = MAX (worker->read_buffer_bytes_wanted, 4096); |
845 | worker->read_buffer = g_realloc (mem: worker->read_buffer, n_bytes: worker->read_buffer_allocated_size); |
846 | } |
847 | |
848 | if (worker->socket == NULL) |
849 | g_input_stream_read_async (stream: g_io_stream_get_input_stream (stream: worker->stream), |
850 | buffer: worker->read_buffer + worker->read_buffer_cur_size, |
851 | count: worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size, |
852 | G_PRIORITY_DEFAULT, |
853 | cancellable: worker->cancellable, |
854 | callback: (GAsyncReadyCallback) _g_dbus_worker_do_read_cb, |
855 | user_data: _g_dbus_worker_ref (worker)); |
856 | else |
857 | { |
858 | worker->read_ancillary_messages = NULL; |
859 | worker->read_num_ancillary_messages = 0; |
860 | _g_socket_read_with_control_messages (socket: worker->socket, |
861 | buffer: worker->read_buffer + worker->read_buffer_cur_size, |
862 | count: worker->read_buffer_bytes_wanted - worker->read_buffer_cur_size, |
863 | messages: &worker->read_ancillary_messages, |
864 | num_messages: &worker->read_num_ancillary_messages, |
865 | G_PRIORITY_DEFAULT, |
866 | cancellable: worker->cancellable, |
867 | callback: (GAsyncReadyCallback) _g_dbus_worker_do_read_cb, |
868 | user_data: _g_dbus_worker_ref (worker)); |
869 | } |
870 | } |
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 | { |
876 | GDBusWorker *worker = data; |
877 | g_mutex_lock (mutex: &worker->read_lock); |
878 | _g_dbus_worker_do_read_unlocked (worker); |
879 | g_mutex_unlock (mutex: &worker->read_lock); |
880 | return FALSE; |
881 | } |
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 | { |
899 | _g_dbus_worker_unref (worker: data->worker); |
900 | if (data->message) |
901 | g_object_unref (object: data->message); |
902 | g_free (mem: data->blob); |
903 | g_slice_free (MessageToWriteData, data); |
904 | } |
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 | { |
920 | MessageToWriteData *data = user_data; |
921 | GTask *task; |
922 | gssize bytes_written; |
923 | 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 | task = data->task; |
929 | |
930 | error = NULL; |
931 | bytes_written = g_output_stream_write_finish (G_OUTPUT_STREAM (source_object), |
932 | result: res, |
933 | error: &error); |
934 | if (bytes_written == -1) |
935 | { |
936 | g_task_return_error (task, error); |
937 | g_object_unref (object: task); |
938 | goto out; |
939 | } |
940 | g_assert (bytes_written > 0); /* zero is never returned */ |
941 | |
942 | write_message_print_transport_debug (bytes_written, data); |
943 | |
944 | data->total_written += bytes_written; |
945 | g_assert (data->total_written <= data->blob_size); |
946 | if (data->total_written == data->blob_size) |
947 | { |
948 | g_task_return_boolean (task, TRUE); |
949 | g_object_unref (object: task); |
950 | goto out; |
951 | } |
952 | |
953 | write_message_continue_writing (data); |
954 | |
955 | out: |
956 | ; |
957 | } |
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 | { |
970 | MessageToWriteData *data = user_data; |
971 | write_message_continue_writing (data); |
972 | return FALSE; /* remove source */ |
973 | } |
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 | { |
984 | GOutputStream *ostream; |
985 | #ifdef G_OS_UNIX |
986 | GTask *task; |
987 | GUnixFDList *fd_list; |
988 | #endif |
989 | |
990 | #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 | task = data->task; |
995 | #endif |
996 | |
997 | ostream = g_io_stream_get_output_stream (stream: data->worker->stream); |
998 | #ifdef G_OS_UNIX |
999 | fd_list = g_dbus_message_get_unix_fd_list (message: data->message); |
1000 | #endif |
1001 | |
1002 | g_assert (!g_output_stream_has_pending (ostream)); |
1003 | g_assert_cmpint (data->total_written, <, data->blob_size); |
1004 | |
1005 | if (FALSE) |
1006 | { |
1007 | } |
1008 | #ifdef G_OS_UNIX |
1009 | else if (G_IS_SOCKET_OUTPUT_STREAM (ostream) && data->total_written == 0) |
1010 | { |
1011 | GOutputVector vector; |
1012 | GSocketControlMessage *control_message; |
1013 | gssize bytes_written; |
1014 | GError *error; |
1015 | |
1016 | vector.buffer = data->blob; |
1017 | vector.size = data->blob_size; |
1018 | |
1019 | control_message = NULL; |
1020 | if (fd_list != NULL && g_unix_fd_list_get_length (list: fd_list) > 0) |
1021 | { |
1022 | if (!(data->worker->capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING)) |
1023 | { |
1024 | g_task_return_new_error (task, |
1025 | G_IO_ERROR, |
1026 | code: G_IO_ERROR_FAILED, |
1027 | format: "Tried sending a file descriptor but remote peer does not support this capability" ); |
1028 | g_object_unref (object: task); |
1029 | goto out; |
1030 | } |
1031 | control_message = g_unix_fd_message_new_with_fd_list (fd_list); |
1032 | } |
1033 | |
1034 | error = NULL; |
1035 | bytes_written = g_socket_send_message (socket: data->worker->socket, |
1036 | NULL, /* address */ |
1037 | vectors: &vector, |
1038 | num_vectors: 1, |
1039 | messages: control_message != NULL ? &control_message : NULL, |
1040 | num_messages: control_message != NULL ? 1 : 0, |
1041 | flags: G_SOCKET_MSG_NONE, |
1042 | cancellable: data->worker->cancellable, |
1043 | error: &error); |
1044 | if (control_message != NULL) |
1045 | g_object_unref (object: control_message); |
1046 | |
1047 | if (bytes_written == -1) |
1048 | { |
1049 | /* Handle WOULD_BLOCK by waiting until there's room in the buffer */ |
1050 | if (g_error_matches (error, G_IO_ERROR, code: G_IO_ERROR_WOULD_BLOCK)) |
1051 | { |
1052 | GSource *source; |
1053 | source = g_socket_create_source (socket: data->worker->socket, |
1054 | condition: G_IO_OUT | G_IO_HUP | G_IO_ERR, |
1055 | cancellable: data->worker->cancellable); |
1056 | g_source_set_callback (source, |
1057 | func: (GSourceFunc) on_socket_ready, |
1058 | data, |
1059 | NULL); /* GDestroyNotify */ |
1060 | g_source_attach (source, context: g_main_context_get_thread_default ()); |
1061 | g_source_unref (source); |
1062 | g_error_free (error); |
1063 | goto out; |
1064 | } |
1065 | g_task_return_error (task, error); |
1066 | g_object_unref (object: task); |
1067 | goto out; |
1068 | } |
1069 | g_assert (bytes_written > 0); /* zero is never returned */ |
1070 | |
1071 | write_message_print_transport_debug (bytes_written, data); |
1072 | |
1073 | data->total_written += bytes_written; |
1074 | g_assert (data->total_written <= data->blob_size); |
1075 | if (data->total_written == data->blob_size) |
1076 | { |
1077 | g_task_return_boolean (task, TRUE); |
1078 | g_object_unref (object: task); |
1079 | goto out; |
1080 | } |
1081 | |
1082 | write_message_continue_writing (data); |
1083 | } |
1084 | #endif |
1085 | else |
1086 | { |
1087 | #ifdef G_OS_UNIX |
1088 | if (data->total_written == 0 && fd_list != NULL) |
1089 | { |
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 | g_task_return_new_error (task, |
1094 | G_IO_ERROR, |
1095 | code: G_IO_ERROR_FAILED, |
1096 | format: "Tried sending a file descriptor on unsupported stream of type %s" , |
1097 | g_type_name (G_TYPE_FROM_INSTANCE (ostream))); |
1098 | g_object_unref (object: task); |
1099 | goto out; |
1100 | } |
1101 | #endif |
1102 | |
1103 | g_output_stream_write_async (stream: ostream, |
1104 | buffer: (const gchar *) data->blob + data->total_written, |
1105 | count: data->blob_size - data->total_written, |
1106 | G_PRIORITY_DEFAULT, |
1107 | cancellable: data->worker->cancellable, |
1108 | callback: write_message_async_cb, |
1109 | user_data: data); |
1110 | } |
1111 | #ifdef G_OS_UNIX |
1112 | out: |
1113 | #endif |
1114 | ; |
1115 | } |
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 | { |
1128 | data->task = g_task_new (NULL, NULL, callback, callback_data: user_data); |
1129 | g_task_set_source_tag (data->task, write_message_async); |
1130 | g_task_set_name (task: data->task, name: "[gio] D-Bus write message" ); |
1131 | data->total_written = 0; |
1132 | write_message_continue_writing (data); |
1133 | } |
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 | { |
1140 | g_return_val_if_fail (g_task_is_valid (res, NULL), FALSE); |
1141 | |
1142 | return g_task_propagate_boolean (G_TASK (res), error); |
1143 | } |
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 | { |
1158 | const GList *l; |
1159 | |
1160 | for (l = flushers; l != NULL; l = l->next) |
1161 | { |
1162 | FlushData *f = l->data; |
1163 | |
1164 | f->error = error != NULL ? g_error_copy (error) : NULL; |
1165 | |
1166 | g_mutex_lock (mutex: &f->mutex); |
1167 | f->finished = TRUE; |
1168 | g_cond_signal (cond: &f->cond); |
1169 | g_mutex_unlock (mutex: &f->mutex); |
1170 | } |
1171 | } |
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 | { |
1183 | FlushAsyncData *data = user_data; |
1184 | GError *error; |
1185 | |
1186 | error = NULL; |
1187 | g_output_stream_flush_finish (G_OUTPUT_STREAM (source_object), |
1188 | result: res, |
1189 | error: &error); |
1190 | |
1191 | if (error == NULL) |
1192 | { |
1193 | if (G_UNLIKELY (_g_dbus_debug_transport ())) |
1194 | { |
1195 | _g_dbus_debug_print_lock (); |
1196 | g_print (format: "========================================================================\n" |
1197 | "GDBus-debug:Transport:\n" |
1198 | " ---- FLUSHED stream of type %s\n" , |
1199 | g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream)))); |
1200 | _g_dbus_debug_print_unlock (); |
1201 | } |
1202 | } |
1203 | |
1204 | /* Make sure we tell folks that we don't have additional |
1205 | flushes pending */ |
1206 | g_mutex_lock (mutex: &data->worker->write_lock); |
1207 | data->worker->write_num_messages_flushed = data->worker->write_num_messages_written; |
1208 | g_assert (data->worker->output_pending == PENDING_FLUSH); |
1209 | data->worker->output_pending = PENDING_NONE; |
1210 | g_mutex_unlock (mutex: &data->worker->write_lock); |
1211 | |
1212 | g_assert (data->flushers != NULL); |
1213 | flush_data_list_complete (flushers: data->flushers, error); |
1214 | g_list_free (list: data->flushers); |
1215 | if (error != NULL) |
1216 | g_error_free (error); |
1217 | |
1218 | /* OK, cool, finally kick off the next write */ |
1219 | continue_writing (worker: data->worker); |
1220 | |
1221 | _g_dbus_worker_unref (worker: data->worker); |
1222 | g_free (mem: data); |
1223 | } |
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 | { |
1233 | g_output_stream_flush_async (stream: g_io_stream_get_output_stream (stream: data->worker->stream), |
1234 | G_PRIORITY_DEFAULT, |
1235 | cancellable: data->worker->cancellable, |
1236 | callback: ostream_flush_cb, |
1237 | user_data: data); |
1238 | } |
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 | { |
1249 | if (G_UNLIKELY (_g_dbus_debug_message ())) |
1250 | { |
1251 | gchar *s; |
1252 | _g_dbus_debug_print_lock (); |
1253 | g_print (format: "========================================================================\n" |
1254 | "GDBus-debug:Message:\n" |
1255 | " >>>> SENT D-Bus message (%" G_GSIZE_FORMAT " bytes)\n" , |
1256 | message_data->blob_size); |
1257 | s = g_dbus_message_print (message: message_data->message, indent: 2); |
1258 | g_print (format: "%s" , s); |
1259 | g_free (mem: s); |
1260 | if (G_UNLIKELY (_g_dbus_debug_payload ())) |
1261 | { |
1262 | s = _g_dbus_hexdump (data: message_data->blob, len: message_data->blob_size, indent: 2); |
1263 | g_print (format: "%s\n" , s); |
1264 | g_free (mem: s); |
1265 | } |
1266 | _g_dbus_debug_print_unlock (); |
1267 | } |
1268 | |
1269 | worker->write_num_messages_written += 1; |
1270 | } |
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 | { |
1282 | GList *l; |
1283 | GList *ll; |
1284 | GList *flushers; |
1285 | |
1286 | flushers = NULL; |
1287 | for (l = worker->write_pending_flushes; l != NULL; l = ll) |
1288 | { |
1289 | FlushData *f = l->data; |
1290 | ll = l->next; |
1291 | |
1292 | if (f->number_to_wait_for == worker->write_num_messages_written) |
1293 | { |
1294 | flushers = g_list_append (list: flushers, data: f); |
1295 | worker->write_pending_flushes = g_list_delete_link (list: worker->write_pending_flushes, link_: l); |
1296 | } |
1297 | } |
1298 | if (flushers != NULL) |
1299 | { |
1300 | g_assert (worker->output_pending == PENDING_NONE); |
1301 | worker->output_pending = PENDING_FLUSH; |
1302 | } |
1303 | |
1304 | if (flushers != NULL) |
1305 | { |
1306 | FlushAsyncData *data; |
1307 | |
1308 | data = g_new0 (FlushAsyncData, 1); |
1309 | data->worker = _g_dbus_worker_ref (worker); |
1310 | data->flushers = flushers; |
1311 | return data; |
1312 | } |
1313 | |
1314 | return NULL; |
1315 | } |
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 | { |
1327 | MessageToWriteData *data = user_data; |
1328 | GError *error; |
1329 | |
1330 | g_mutex_lock (mutex: &data->worker->write_lock); |
1331 | g_assert (data->worker->output_pending == PENDING_WRITE); |
1332 | data->worker->output_pending = PENDING_NONE; |
1333 | |
1334 | error = NULL; |
1335 | if (!write_message_finish (res, error: &error)) |
1336 | { |
1337 | g_mutex_unlock (mutex: &data->worker->write_lock); |
1338 | |
1339 | /* TODO: handle */ |
1340 | _g_dbus_worker_emit_disconnected (worker: data->worker, TRUE, error); |
1341 | g_error_free (error); |
1342 | |
1343 | g_mutex_lock (mutex: &data->worker->write_lock); |
1344 | } |
1345 | |
1346 | message_written_unlocked (worker: data->worker, message_data: data); |
1347 | |
1348 | g_mutex_unlock (mutex: &data->worker->write_lock); |
1349 | |
1350 | continue_writing (worker: data->worker); |
1351 | |
1352 | message_to_write_data_free (data); |
1353 | } |
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 | { |
1365 | GDBusWorker *worker = user_data; |
1366 | GError *error = NULL; |
1367 | GList *pending_close_attempts, *pending_flush_attempts; |
1368 | GQueue *send_queue; |
1369 | |
1370 | g_io_stream_close_finish (stream: worker->stream, result: res, error: &error); |
1371 | |
1372 | g_mutex_lock (mutex: &worker->write_lock); |
1373 | |
1374 | pending_close_attempts = worker->pending_close_attempts; |
1375 | worker->pending_close_attempts = NULL; |
1376 | |
1377 | pending_flush_attempts = worker->write_pending_flushes; |
1378 | worker->write_pending_flushes = NULL; |
1379 | |
1380 | send_queue = worker->write_queue; |
1381 | worker->write_queue = g_queue_new (); |
1382 | |
1383 | g_assert (worker->output_pending == PENDING_CLOSE); |
1384 | worker->output_pending = PENDING_NONE; |
1385 | |
1386 | /* Ensure threads waiting for pending flushes to finish will be unblocked. */ |
1387 | worker->write_num_messages_flushed = |
1388 | worker->write_num_messages_written + g_list_length(list: pending_flush_attempts); |
1389 | |
1390 | g_mutex_unlock (mutex: &worker->write_lock); |
1391 | |
1392 | while (pending_close_attempts != NULL) |
1393 | { |
1394 | CloseData *close_data = pending_close_attempts->data; |
1395 | |
1396 | pending_close_attempts = g_list_delete_link (list: pending_close_attempts, |
1397 | link_: pending_close_attempts); |
1398 | |
1399 | if (close_data->task != NULL) |
1400 | { |
1401 | if (error != NULL) |
1402 | g_task_return_error (task: close_data->task, error: g_error_copy (error)); |
1403 | else |
1404 | g_task_return_boolean (task: close_data->task, TRUE); |
1405 | } |
1406 | |
1407 | close_data_free (close_data); |
1408 | } |
1409 | |
1410 | g_clear_error (err: &error); |
1411 | |
1412 | /* all messages queued for sending are discarded */ |
1413 | g_queue_free_full (queue: send_queue, free_func: (GDestroyNotify) message_to_write_data_free); |
1414 | /* all queued flushes fail */ |
1415 | error = g_error_new (G_IO_ERROR, code: G_IO_ERROR_CANCELLED, |
1416 | _("Operation was cancelled" )); |
1417 | flush_data_list_complete (flushers: pending_flush_attempts, error); |
1418 | g_list_free (list: pending_flush_attempts); |
1419 | g_clear_error (err: &error); |
1420 | |
1421 | _g_dbus_worker_unref (worker); |
1422 | } |
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 | { |
1432 | MessageToWriteData *data; |
1433 | FlushAsyncData *flush_async_data; |
1434 | |
1435 | write_next: |
1436 | /* we mustn't try to write two things at once */ |
1437 | g_assert (worker->output_pending == PENDING_NONE); |
1438 | |
1439 | g_mutex_lock (mutex: &worker->write_lock); |
1440 | |
1441 | data = NULL; |
1442 | flush_async_data = NULL; |
1443 | |
1444 | /* if we want to close the connection, that takes precedence */ |
1445 | if (worker->pending_close_attempts != NULL) |
1446 | { |
1447 | GInputStream *input = g_io_stream_get_input_stream (stream: worker->stream); |
1448 | |
1449 | if (!g_input_stream_has_pending (stream: input)) |
1450 | { |
1451 | worker->close_expected = TRUE; |
1452 | worker->output_pending = PENDING_CLOSE; |
1453 | |
1454 | g_io_stream_close_async (stream: worker->stream, G_PRIORITY_DEFAULT, |
1455 | NULL, callback: iostream_close_cb, |
1456 | user_data: _g_dbus_worker_ref (worker)); |
1457 | } |
1458 | } |
1459 | else |
1460 | { |
1461 | flush_async_data = prepare_flush_unlocked (worker); |
1462 | |
1463 | if (flush_async_data == NULL) |
1464 | { |
1465 | data = g_queue_pop_head (queue: worker->write_queue); |
1466 | |
1467 | if (data != NULL) |
1468 | worker->output_pending = PENDING_WRITE; |
1469 | } |
1470 | } |
1471 | |
1472 | g_mutex_unlock (mutex: &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 | if (flush_async_data != NULL) |
1484 | { |
1485 | start_flush (data: flush_async_data); |
1486 | g_assert (data == NULL); |
1487 | } |
1488 | else if (data != NULL) |
1489 | { |
1490 | GDBusMessage *old_message; |
1491 | guchar *new_blob; |
1492 | gsize new_blob_size; |
1493 | GError *error; |
1494 | |
1495 | old_message = data->message; |
1496 | data->message = _g_dbus_worker_emit_message_about_to_be_sent (worker, message: data->message); |
1497 | if (data->message == old_message) |
1498 | { |
1499 | /* filters had no effect - do nothing */ |
1500 | } |
1501 | else if (data->message == NULL) |
1502 | { |
1503 | /* filters dropped message */ |
1504 | g_mutex_lock (mutex: &worker->write_lock); |
1505 | worker->output_pending = PENDING_NONE; |
1506 | g_mutex_unlock (mutex: &worker->write_lock); |
1507 | message_to_write_data_free (data); |
1508 | goto write_next; |
1509 | } |
1510 | else |
1511 | { |
1512 | /* filters altered the message -> re-encode */ |
1513 | error = NULL; |
1514 | new_blob = g_dbus_message_to_blob (message: data->message, |
1515 | out_size: &new_blob_size, |
1516 | capabilities: worker->capabilities, |
1517 | error: &error); |
1518 | if (new_blob == NULL) |
1519 | { |
1520 | /* if filter make the GDBusMessage unencodeable, just complain on stderr and send |
1521 | * the old message instead |
1522 | */ |
1523 | g_warning ("Error encoding GDBusMessage with serial %d altered by filter function: %s" , |
1524 | g_dbus_message_get_serial (data->message), |
1525 | error->message); |
1526 | g_error_free (error); |
1527 | } |
1528 | else |
1529 | { |
1530 | g_free (mem: data->blob); |
1531 | data->blob = (gchar *) new_blob; |
1532 | data->blob_size = new_blob_size; |
1533 | } |
1534 | } |
1535 | |
1536 | write_message_async (worker, |
1537 | data, |
1538 | callback: write_message_cb, |
1539 | user_data: data); |
1540 | } |
1541 | } |
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 | { |
1551 | 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 | if (worker->output_pending == PENDING_NONE) |
1557 | continue_writing (worker); |
1558 | |
1559 | return FALSE; |
1560 | } |
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 | { |
1578 | if (write_data != NULL) |
1579 | g_queue_push_tail (queue: worker->write_queue, data: write_data); |
1580 | |
1581 | if (flush_data != NULL) |
1582 | worker->write_pending_flushes = g_list_prepend (list: worker->write_pending_flushes, data: flush_data); |
1583 | |
1584 | if (close_data != NULL) |
1585 | worker->pending_close_attempts = g_list_prepend (list: worker->pending_close_attempts, |
1586 | data: 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 | if (worker->output_pending == PENDING_NONE) |
1596 | { |
1597 | GSource *idle_source; |
1598 | idle_source = g_idle_source_new (); |
1599 | g_source_set_priority (source: idle_source, G_PRIORITY_DEFAULT); |
1600 | g_source_set_callback (source: idle_source, |
1601 | func: continue_writing_in_idle_cb, |
1602 | data: _g_dbus_worker_ref (worker), |
1603 | notify: (GDestroyNotify) _g_dbus_worker_unref); |
1604 | g_source_set_name (source: idle_source, name: "[gio] continue_writing_in_idle_cb" ); |
1605 | g_source_attach (source: idle_source, context: worker->shared_thread_data->context); |
1606 | g_source_unref (source: idle_source); |
1607 | } |
1608 | } |
1609 | |
1610 | static void |
1611 | schedule_pending_close (GDBusWorker *worker) |
1612 | { |
1613 | g_mutex_lock (mutex: &worker->write_lock); |
1614 | if (worker->pending_close_attempts) |
1615 | schedule_writing_unlocked (worker, NULL, NULL, NULL); |
1616 | g_mutex_unlock (mutex: &worker->write_lock); |
1617 | } |
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 | { |
1632 | MessageToWriteData *data; |
1633 | |
1634 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
1635 | g_return_if_fail (blob != NULL); |
1636 | g_return_if_fail (blob_len > 16); |
1637 | |
1638 | data = g_slice_new0 (MessageToWriteData); |
1639 | data->worker = _g_dbus_worker_ref (worker); |
1640 | data->message = g_object_ref (message); |
1641 | data->blob = blob; /* steal! */ |
1642 | data->blob_size = blob_len; |
1643 | |
1644 | g_mutex_lock (mutex: &worker->write_lock); |
1645 | schedule_writing_unlocked (worker, write_data: data, NULL, NULL); |
1646 | g_mutex_unlock (mutex: &worker->write_lock); |
1647 | } |
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 | { |
1660 | GDBusWorker *worker; |
1661 | GSource *idle_source; |
1662 | |
1663 | g_return_val_if_fail (G_IS_IO_STREAM (stream), NULL); |
1664 | g_return_val_if_fail (message_received_callback != NULL, NULL); |
1665 | g_return_val_if_fail (message_about_to_be_sent_callback != NULL, NULL); |
1666 | g_return_val_if_fail (disconnected_callback != NULL, NULL); |
1667 | |
1668 | worker = g_new0 (GDBusWorker, 1); |
1669 | worker->ref_count = 1; |
1670 | |
1671 | g_mutex_init (mutex: &worker->read_lock); |
1672 | worker->message_received_callback = message_received_callback; |
1673 | worker->message_about_to_be_sent_callback = message_about_to_be_sent_callback; |
1674 | worker->disconnected_callback = disconnected_callback; |
1675 | worker->user_data = user_data; |
1676 | worker->stream = g_object_ref (stream); |
1677 | worker->capabilities = capabilities; |
1678 | worker->cancellable = g_cancellable_new (); |
1679 | worker->output_pending = PENDING_NONE; |
1680 | |
1681 | worker->frozen = initially_frozen; |
1682 | worker->received_messages_while_frozen = g_queue_new (); |
1683 | |
1684 | g_mutex_init (mutex: &worker->write_lock); |
1685 | worker->write_queue = g_queue_new (); |
1686 | |
1687 | if (G_IS_SOCKET_CONNECTION (worker->stream)) |
1688 | worker->socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (worker->stream)); |
1689 | |
1690 | worker->shared_thread_data = _g_dbus_shared_thread_ref (); |
1691 | |
1692 | /* begin reading */ |
1693 | idle_source = g_idle_source_new (); |
1694 | g_source_set_priority (source: idle_source, G_PRIORITY_DEFAULT); |
1695 | g_source_set_callback (source: idle_source, |
1696 | func: _g_dbus_worker_do_initial_read, |
1697 | data: _g_dbus_worker_ref (worker), |
1698 | notify: (GDestroyNotify) _g_dbus_worker_unref); |
1699 | g_source_set_name (source: idle_source, name: "[gio] _g_dbus_worker_do_initial_read" ); |
1700 | g_source_attach (source: idle_source, context: worker->shared_thread_data->context); |
1701 | g_source_unref (source: idle_source); |
1702 | |
1703 | return worker; |
1704 | } |
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 | { |
1717 | CloseData *close_data; |
1718 | |
1719 | close_data = g_slice_new0 (CloseData); |
1720 | close_data->worker = _g_dbus_worker_ref (worker); |
1721 | 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 | g_cancellable_cancel (cancellable: worker->cancellable); |
1727 | g_mutex_lock (mutex: &worker->write_lock); |
1728 | schedule_writing_unlocked (worker, NULL, NULL, close_data); |
1729 | g_mutex_unlock (mutex: &worker->write_lock); |
1730 | } |
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 | { |
1742 | 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 | _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 | _g_dbus_worker_unref (worker); |
1754 | } |
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 | { |
1770 | gboolean ret; |
1771 | FlushData *data; |
1772 | guint64 pending_writes; |
1773 | |
1774 | data = NULL; |
1775 | ret = TRUE; |
1776 | |
1777 | g_mutex_lock (mutex: &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 | pending_writes = g_queue_get_length (queue: 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 | if (worker->output_pending == PENDING_WRITE) |
1788 | pending_writes += 1; |
1789 | |
1790 | if (pending_writes > 0 || |
1791 | worker->write_num_messages_written != worker->write_num_messages_flushed) |
1792 | { |
1793 | data = g_new0 (FlushData, 1); |
1794 | g_mutex_init (mutex: &data->mutex); |
1795 | g_cond_init (cond: &data->cond); |
1796 | data->number_to_wait_for = worker->write_num_messages_written + pending_writes; |
1797 | data->finished = FALSE; |
1798 | g_mutex_lock (mutex: &data->mutex); |
1799 | |
1800 | schedule_writing_unlocked (worker, NULL, flush_data: data, NULL); |
1801 | } |
1802 | g_mutex_unlock (mutex: &worker->write_lock); |
1803 | |
1804 | if (data != NULL) |
1805 | { |
1806 | /* Wait for flush operations to finish. */ |
1807 | while (!data->finished) |
1808 | { |
1809 | g_cond_wait (cond: &data->cond, mutex: &data->mutex); |
1810 | } |
1811 | |
1812 | g_mutex_unlock (mutex: &data->mutex); |
1813 | g_cond_clear (cond: &data->cond); |
1814 | g_mutex_clear (mutex: &data->mutex); |
1815 | if (data->error != NULL) |
1816 | { |
1817 | ret = FALSE; |
1818 | g_propagate_error (dest: error, src: data->error); |
1819 | } |
1820 | g_free (mem: data); |
1821 | } |
1822 | |
1823 | return ret; |
1824 | } |
1825 | |
1826 | /* ---------------------------------------------------------------------------------------------------- */ |
1827 | |
1828 | #define G_DBUS_DEBUG_AUTHENTICATION (1<<0) |
1829 | #define G_DBUS_DEBUG_TRANSPORT (1<<1) |
1830 | #define G_DBUS_DEBUG_MESSAGE (1<<2) |
1831 | #define G_DBUS_DEBUG_PAYLOAD (1<<3) |
1832 | #define G_DBUS_DEBUG_CALL (1<<4) |
1833 | #define G_DBUS_DEBUG_SIGNAL (1<<5) |
1834 | #define G_DBUS_DEBUG_INCOMING (1<<6) |
1835 | #define G_DBUS_DEBUG_RETURN (1<<7) |
1836 | #define G_DBUS_DEBUG_EMISSION (1<<8) |
1837 | #define G_DBUS_DEBUG_ADDRESS (1<<9) |
1838 | #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 | { |
1845 | _g_dbus_initialize (); |
1846 | return (_gdbus_debug_flags & G_DBUS_DEBUG_AUTHENTICATION) != 0; |
1847 | } |
1848 | |
1849 | gboolean |
1850 | _g_dbus_debug_transport (void) |
1851 | { |
1852 | _g_dbus_initialize (); |
1853 | return (_gdbus_debug_flags & G_DBUS_DEBUG_TRANSPORT) != 0; |
1854 | } |
1855 | |
1856 | gboolean |
1857 | _g_dbus_debug_message (void) |
1858 | { |
1859 | _g_dbus_initialize (); |
1860 | return (_gdbus_debug_flags & G_DBUS_DEBUG_MESSAGE) != 0; |
1861 | } |
1862 | |
1863 | gboolean |
1864 | _g_dbus_debug_payload (void) |
1865 | { |
1866 | _g_dbus_initialize (); |
1867 | return (_gdbus_debug_flags & G_DBUS_DEBUG_PAYLOAD) != 0; |
1868 | } |
1869 | |
1870 | gboolean |
1871 | _g_dbus_debug_call (void) |
1872 | { |
1873 | _g_dbus_initialize (); |
1874 | return (_gdbus_debug_flags & G_DBUS_DEBUG_CALL) != 0; |
1875 | } |
1876 | |
1877 | gboolean |
1878 | _g_dbus_debug_signal (void) |
1879 | { |
1880 | _g_dbus_initialize (); |
1881 | return (_gdbus_debug_flags & G_DBUS_DEBUG_SIGNAL) != 0; |
1882 | } |
1883 | |
1884 | gboolean |
1885 | _g_dbus_debug_incoming (void) |
1886 | { |
1887 | _g_dbus_initialize (); |
1888 | return (_gdbus_debug_flags & G_DBUS_DEBUG_INCOMING) != 0; |
1889 | } |
1890 | |
1891 | gboolean |
1892 | _g_dbus_debug_return (void) |
1893 | { |
1894 | _g_dbus_initialize (); |
1895 | return (_gdbus_debug_flags & G_DBUS_DEBUG_RETURN) != 0; |
1896 | } |
1897 | |
1898 | gboolean |
1899 | _g_dbus_debug_emission (void) |
1900 | { |
1901 | _g_dbus_initialize (); |
1902 | return (_gdbus_debug_flags & G_DBUS_DEBUG_EMISSION) != 0; |
1903 | } |
1904 | |
1905 | gboolean |
1906 | _g_dbus_debug_address (void) |
1907 | { |
1908 | _g_dbus_initialize (); |
1909 | return (_gdbus_debug_flags & G_DBUS_DEBUG_ADDRESS) != 0; |
1910 | } |
1911 | |
1912 | gboolean |
1913 | _g_dbus_debug_proxy (void) |
1914 | { |
1915 | _g_dbus_initialize (); |
1916 | return (_gdbus_debug_flags & G_DBUS_DEBUG_PROXY) != 0; |
1917 | } |
1918 | |
1919 | G_LOCK_DEFINE_STATIC (print_lock); |
1920 | |
1921 | void |
1922 | _g_dbus_debug_print_lock (void) |
1923 | { |
1924 | G_LOCK (print_lock); |
1925 | } |
1926 | |
1927 | void |
1928 | _g_dbus_debug_print_unlock (void) |
1929 | { |
1930 | G_UNLOCK (print_lock); |
1931 | } |
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 | { |
1944 | static gsize initialized = 0; |
1945 | |
1946 | if (g_once_init_enter (&initialized)) |
1947 | { |
1948 | const gchar *debug; |
1949 | |
1950 | /* Ensure the domain is registered. */ |
1951 | g_dbus_error_quark (); |
1952 | |
1953 | debug = g_getenv (variable: "G_DBUS_DEBUG" ); |
1954 | if (debug != NULL) |
1955 | { |
1956 | const GDebugKey keys[] = { |
1957 | { "authentication" , G_DBUS_DEBUG_AUTHENTICATION }, |
1958 | { "transport" , G_DBUS_DEBUG_TRANSPORT }, |
1959 | { "message" , G_DBUS_DEBUG_MESSAGE }, |
1960 | { "payload" , G_DBUS_DEBUG_PAYLOAD }, |
1961 | { "call" , G_DBUS_DEBUG_CALL }, |
1962 | { "signal" , G_DBUS_DEBUG_SIGNAL }, |
1963 | { "incoming" , G_DBUS_DEBUG_INCOMING }, |
1964 | { "return" , G_DBUS_DEBUG_RETURN }, |
1965 | { "emission" , G_DBUS_DEBUG_EMISSION }, |
1966 | { "address" , G_DBUS_DEBUG_ADDRESS }, |
1967 | { "proxy" , G_DBUS_DEBUG_PROXY } |
1968 | }; |
1969 | |
1970 | _gdbus_debug_flags = g_parse_debug_string (string: debug, keys, G_N_ELEMENTS (keys)); |
1971 | if (_gdbus_debug_flags & G_DBUS_DEBUG_PAYLOAD) |
1972 | _gdbus_debug_flags |= G_DBUS_DEBUG_MESSAGE; |
1973 | } |
1974 | |
1975 | /* Work-around for https://bugzilla.gnome.org/show_bug.cgi?id=627724 */ |
1976 | ensure_required_types (); |
1977 | |
1978 | g_once_init_leave (&initialized, 1); |
1979 | } |
1980 | } |
1981 | |
1982 | /* ---------------------------------------------------------------------------------------------------- */ |
1983 | |
1984 | GVariantType * |
1985 | _g_dbus_compute_complete_signature (GDBusArgInfo **args) |
1986 | { |
1987 | const GVariantType *arg_types[256]; |
1988 | guint n; |
1989 | |
1990 | if (args) |
1991 | for (n = 0; args[n] != NULL; n++) |
1992 | { |
1993 | /* DBus places a hard limit of 255 on signature length. |
1994 | * therefore number of args must be less than 256. |
1995 | */ |
1996 | g_assert (n < 256); |
1997 | |
1998 | arg_types[n] = G_VARIANT_TYPE (args[n]->signature); |
1999 | |
2000 | if G_UNLIKELY (arg_types[n] == NULL) |
2001 | return NULL; |
2002 | } |
2003 | else |
2004 | n = 0; |
2005 | |
2006 | return g_variant_type_new_tuple (items: arg_types, length: n); |
2007 | } |
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 | { |
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 | gchar *ret = NULL; |
2474 | GError *first_error = NULL; |
2475 | gsize i; |
2476 | 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 | const gchar *var_lib_path = LOCALSTATEDIR "/lib/dbus/machine-id" ; |
2481 | const gchar *etc_path = "/etc/machine-id" ; |
2482 | |
2483 | if (!g_file_get_contents (filename: var_lib_path, |
2484 | contents: &ret, |
2485 | NULL, |
2486 | error: &first_error) && |
2487 | !g_file_get_contents (filename: etc_path, |
2488 | contents: &ret, |
2489 | NULL, |
2490 | NULL)) |
2491 | { |
2492 | g_propagate_prefixed_error (dest: error, g_steal_pointer (&first_error), |
2493 | /* Translators: Both placeholders are file paths */ |
2494 | _("Unable to load %s or %s: " ), |
2495 | var_lib_path, etc_path); |
2496 | return NULL; |
2497 | } |
2498 | |
2499 | /* ignore the error from the first try, if any */ |
2500 | g_clear_error (err: &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 | for (i = 0; ret[i] != '\0' && ret[i] != '\n'; i++) |
2508 | { |
2509 | /* Break early if it’s invalid. */ |
2510 | if (!g_ascii_isxdigit (ret[i]) || g_ascii_isupper (ret[i])) |
2511 | break; |
2512 | |
2513 | if (ret[i] != '0') |
2514 | non_zero = TRUE; |
2515 | } |
2516 | |
2517 | if (i != 32 || ret[i] != '\n' || ret[i + 1] != '\0' || !non_zero) |
2518 | { |
2519 | g_set_error (err: error, G_IO_ERROR, code: G_IO_ERROR_FAILED, |
2520 | format: "Invalid machine ID in %s or %s" , |
2521 | var_lib_path, etc_path); |
2522 | g_free (mem: ret); |
2523 | return NULL; |
2524 | } |
2525 | |
2526 | /* Strip trailing newline. */ |
2527 | ret[32] = '\0'; |
2528 | |
2529 | return g_steal_pointer (&ret); |
2530 | #endif |
2531 | } |
2532 | |
2533 | /* ---------------------------------------------------------------------------------------------------- */ |
2534 | |
2535 | gchar * |
2536 | _g_dbus_enum_to_string (GType enum_type, gint value) |
2537 | { |
2538 | gchar *ret; |
2539 | GEnumClass *klass; |
2540 | GEnumValue *enum_value; |
2541 | |
2542 | klass = g_type_class_ref (type: enum_type); |
2543 | enum_value = g_enum_get_value (enum_class: klass, value); |
2544 | if (enum_value != NULL) |
2545 | ret = g_strdup (str: enum_value->value_nick); |
2546 | else |
2547 | ret = g_strdup_printf (format: "unknown (value %d)" , value); |
2548 | g_type_class_unref (g_class: klass); |
2549 | return ret; |
2550 | } |
2551 | |
2552 | /* ---------------------------------------------------------------------------------------------------- */ |
2553 | |
2554 | static void |
2555 | write_message_print_transport_debug (gssize bytes_written, |
2556 | MessageToWriteData *data) |
2557 | { |
2558 | if (G_LIKELY (!_g_dbus_debug_transport ())) |
2559 | goto out; |
2560 | |
2561 | _g_dbus_debug_print_lock (); |
2562 | g_print (format: "========================================================================\n" |
2563 | "GDBus-debug:Transport:\n" |
2564 | " >>>> WROTE %" G_GSSIZE_FORMAT " bytes of message with serial %d and\n" |
2565 | " size %" G_GSIZE_FORMAT " from offset %" G_GSIZE_FORMAT " on a %s\n" , |
2566 | bytes_written, |
2567 | g_dbus_message_get_serial (message: data->message), |
2568 | data->blob_size, |
2569 | data->total_written, |
2570 | g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_output_stream (data->worker->stream)))); |
2571 | _g_dbus_debug_print_unlock (); |
2572 | out: |
2573 | ; |
2574 | } |
2575 | |
2576 | /* ---------------------------------------------------------------------------------------------------- */ |
2577 | |
2578 | static void |
2579 | read_message_print_transport_debug (gssize bytes_read, |
2580 | GDBusWorker *worker) |
2581 | { |
2582 | gsize size; |
2583 | gint32 serial; |
2584 | gint32 message_length; |
2585 | |
2586 | if (G_LIKELY (!_g_dbus_debug_transport ())) |
2587 | goto out; |
2588 | |
2589 | size = bytes_read + worker->read_buffer_cur_size; |
2590 | serial = 0; |
2591 | message_length = 0; |
2592 | if (size >= 16) |
2593 | message_length = g_dbus_message_bytes_needed (blob: (guchar *) worker->read_buffer, blob_len: size, NULL); |
2594 | if (size >= 1) |
2595 | { |
2596 | switch (worker->read_buffer[0]) |
2597 | { |
2598 | case 'l': |
2599 | if (size >= 12) |
2600 | serial = GUINT32_FROM_LE (((guint32 *) worker->read_buffer)[2]); |
2601 | break; |
2602 | case 'B': |
2603 | if (size >= 12) |
2604 | serial = GUINT32_FROM_BE (((guint32 *) worker->read_buffer)[2]); |
2605 | break; |
2606 | default: |
2607 | /* an error will be set elsewhere if this happens */ |
2608 | goto out; |
2609 | } |
2610 | } |
2611 | |
2612 | _g_dbus_debug_print_lock (); |
2613 | g_print (format: "========================================================================\n" |
2614 | "GDBus-debug:Transport:\n" |
2615 | " <<<< READ %" G_GSSIZE_FORMAT " bytes of message with serial %d and\n" |
2616 | " size %d to offset %" G_GSIZE_FORMAT " from a %s\n" , |
2617 | bytes_read, |
2618 | serial, |
2619 | message_length, |
2620 | worker->read_buffer_cur_size, |
2621 | g_type_name (G_TYPE_FROM_INSTANCE (g_io_stream_get_input_stream (worker->stream)))); |
2622 | _g_dbus_debug_print_unlock (); |
2623 | out: |
2624 | ; |
2625 | } |
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 | { |
2635 | gboolean continue_emission; |
2636 | gboolean signal_return; |
2637 | |
2638 | signal_return = g_value_get_boolean (value: handler_return); |
2639 | g_value_set_boolean (value: return_accu, v_boolean: signal_return); |
2640 | continue_emission = signal_return; |
2641 | |
2642 | return continue_emission; |
2643 | } |
2644 | |
2645 | /* ---------------------------------------------------------------------------------------------------- */ |
2646 | |
2647 | static void |
2648 | append_nibble (GString *s, gint val) |
2649 | { |
2650 | g_string_append_c (s, val >= 10 ? ('a' + val - 10) : ('0' + val)); |
2651 | } |
2652 | |
2653 | /* ---------------------------------------------------------------------------------------------------- */ |
2654 | |
2655 | gchar * |
2656 | _g_dbus_hexencode (const gchar *str, |
2657 | gsize str_len) |
2658 | { |
2659 | gsize n; |
2660 | GString *s; |
2661 | |
2662 | s = g_string_new (NULL); |
2663 | for (n = 0; n < str_len; n++) |
2664 | { |
2665 | gint val; |
2666 | gint upper_nibble; |
2667 | gint lower_nibble; |
2668 | |
2669 | val = ((const guchar *) str)[n]; |
2670 | upper_nibble = val >> 4; |
2671 | lower_nibble = val & 0x0f; |
2672 | |
2673 | append_nibble (s, val: upper_nibble); |
2674 | append_nibble (s, val: lower_nibble); |
2675 | } |
2676 | |
2677 | return g_string_free (string: s, FALSE); |
2678 | } |
2679 | |