D-Bus 1.15.2
dbus-transport-socket.c
1/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2/* dbus-transport-socket.c Socket subclasses of DBusTransport
3 *
4 * Copyright (C) 2002, 2003, 2004, 2006 Red Hat Inc.
5 *
6 * Licensed under the Academic Free License version 2.1
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23
24#include <config.h>
25
26#include <stdio.h>
27
28#include "dbus-internals.h"
29#include "dbus-connection-internal.h"
30#include "dbus-nonce.h"
31#include "dbus-transport-socket.h"
32#include "dbus-transport-protected.h"
33#include "dbus-watch.h"
34#include "dbus-credentials.h"
35
48
53{
72};
73
74static void
75free_watches (DBusTransport *transport)
76{
77 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
78
79 _dbus_verbose ("start\n");
80
81 if (socket_transport->read_watch)
82 {
83 if (transport->connection)
85 socket_transport->read_watch);
86 _dbus_watch_invalidate (socket_transport->read_watch);
87 _dbus_watch_unref (socket_transport->read_watch);
88 socket_transport->read_watch = NULL;
89 }
90
91 if (socket_transport->write_watch)
92 {
93 if (transport->connection)
95 socket_transport->write_watch);
96 _dbus_watch_invalidate (socket_transport->write_watch);
97 _dbus_watch_unref (socket_transport->write_watch);
98 socket_transport->write_watch = NULL;
99 }
100
101 _dbus_verbose ("end\n");
102}
103
104static void
105socket_finalize (DBusTransport *transport)
106{
107 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
108
109 _dbus_verbose ("\n");
110
111 free_watches (transport);
112
113 _dbus_string_free (&socket_transport->encoded_outgoing);
114 _dbus_string_free (&socket_transport->encoded_incoming);
115
117
118 _dbus_assert (socket_transport->read_watch == NULL);
119 _dbus_assert (socket_transport->write_watch == NULL);
120
121 dbus_free (transport);
122}
123
124static void
125check_write_watch (DBusTransport *transport)
126{
127 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
128 dbus_bool_t needed;
129
130 if (transport->connection == NULL)
131 return;
132
133 if (transport->disconnected)
134 {
135 _dbus_assert (socket_transport->write_watch == NULL);
136 return;
137 }
138
139 _dbus_transport_ref (transport);
140
143 else
144 {
145 if (transport->send_credentials_pending)
146 needed = TRUE;
147 else
148 {
149 DBusAuthState auth_state;
150
151 auth_state = _dbus_auth_do_work (transport->auth);
152
153 /* If we need memory we install the write watch just in case,
154 * if there's no need for it, it will get de-installed
155 * next time we try reading.
156 */
157 if (auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND ||
158 auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
159 needed = TRUE;
160 else
161 needed = FALSE;
162 }
163 }
164
165 _dbus_verbose ("check_write_watch(): needed = %d on connection %p watch %p fd = %" DBUS_SOCKET_FORMAT " outgoing messages exist %d\n",
166 needed, transport->connection, socket_transport->write_watch,
167 _dbus_socket_printable (socket_transport->fd),
169
171 socket_transport->write_watch,
172 needed);
173
174 _dbus_transport_unref (transport);
175}
176
177static void
178check_read_watch (DBusTransport *transport)
179{
180 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
181 dbus_bool_t need_read_watch;
182
183 _dbus_verbose ("fd = %" DBUS_SOCKET_FORMAT "\n",
184 _dbus_socket_printable (socket_transport->fd));
185
186 if (transport->connection == NULL)
187 return;
188
189 if (transport->disconnected)
190 {
191 _dbus_assert (socket_transport->read_watch == NULL);
192 return;
193 }
194
195 _dbus_transport_ref (transport);
196
198 need_read_watch =
201 else
202 {
203 if (transport->receive_credentials_pending)
204 need_read_watch = TRUE;
205 else
206 {
207 /* The reason to disable need_read_watch when not WAITING_FOR_INPUT
208 * is to avoid spinning on the file descriptor when we're waiting
209 * to write or for some other part of the auth process
210 */
211 DBusAuthState auth_state;
212
213 auth_state = _dbus_auth_do_work (transport->auth);
214
215 /* If we need memory we install the read watch just in case,
216 * if there's no need for it, it will get de-installed
217 * next time we try reading. If we're authenticated we
218 * install it since we normally have it installed while
219 * authenticated.
220 */
221 if (auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT ||
222 auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY ||
223 auth_state == DBUS_AUTH_STATE_AUTHENTICATED)
224 need_read_watch = TRUE;
225 else
226 need_read_watch = FALSE;
227 }
228 }
229
230 _dbus_verbose (" setting read watch enabled = %d\n", need_read_watch);
232 socket_transport->read_watch,
233 need_read_watch);
234
235 _dbus_transport_unref (transport);
236}
237
238static void
239do_io_error (DBusTransport *transport)
240{
241 _dbus_transport_ref (transport);
242 _dbus_transport_disconnect (transport);
243 _dbus_transport_unref (transport);
244}
245
246/* return value is whether we successfully read any new data. */
247static dbus_bool_t
248read_data_into_auth (DBusTransport *transport,
249 dbus_bool_t *oom)
250{
251 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
252 DBusString *buffer;
253 int bytes_read;
254 int saved_errno;
255
256 *oom = FALSE;
257
258 _dbus_auth_get_buffer (transport->auth, &buffer);
259
260 bytes_read = _dbus_read_socket (socket_transport->fd,
261 buffer, socket_transport->max_bytes_read_per_iteration);
262 saved_errno = _dbus_save_socket_errno ();
263
264 _dbus_auth_return_buffer (transport->auth, buffer);
265
266 if (bytes_read > 0)
267 {
268 _dbus_verbose (" read %d bytes in auth phase\n", bytes_read);
269
270 return TRUE;
271 }
272 else if (bytes_read < 0)
273 {
274 /* EINTR already handled for us */
275
276 if (_dbus_get_is_errno_enomem (saved_errno))
277 {
278 *oom = TRUE;
279 }
280 else if (_dbus_get_is_errno_eagain_or_ewouldblock (saved_errno))
281 ; /* do nothing, just return FALSE below */
282 else
283 {
284 _dbus_verbose ("Error reading from remote app: %s\n",
285 _dbus_strerror (saved_errno));
286 do_io_error (transport);
287 }
288
289 return FALSE;
290 }
291 else
292 {
293 _dbus_assert (bytes_read == 0);
294
295 _dbus_verbose ("Disconnected from remote app\n");
296 do_io_error (transport);
297
298 return FALSE;
299 }
300}
301
302/* Return value is whether we successfully wrote any bytes */
303static dbus_bool_t
304write_data_from_auth (DBusTransport *transport)
305{
306 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
307 int bytes_written;
308 int saved_errno;
309 const DBusString *buffer;
310
311 if (!_dbus_auth_get_bytes_to_send (transport->auth,
312 &buffer))
313 return FALSE;
314
315 bytes_written = _dbus_write_socket (socket_transport->fd,
316 buffer,
317 0, _dbus_string_get_length (buffer));
318 saved_errno = _dbus_save_socket_errno ();
319
320 if (bytes_written > 0)
321 {
322 _dbus_auth_bytes_sent (transport->auth, bytes_written);
323 return TRUE;
324 }
325 else if (bytes_written < 0)
326 {
327 /* EINTR already handled for us */
328
330 ;
331 else
332 {
333 _dbus_verbose ("Error writing to remote app: %s\n",
334 _dbus_strerror (saved_errno));
335 do_io_error (transport);
336 }
337 }
338
339 return FALSE;
340}
341
342/* FALSE on OOM */
343static dbus_bool_t
344exchange_credentials (DBusTransport *transport,
345 dbus_bool_t do_reading,
346 dbus_bool_t do_writing)
347{
348 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
350
351 _dbus_verbose ("exchange_credentials: do_reading = %d, do_writing = %d\n",
352 do_reading, do_writing);
353
354 if (do_writing && transport->send_credentials_pending)
355 {
356 if (_dbus_send_credentials_socket (socket_transport->fd,
357 &error))
358 {
359 transport->send_credentials_pending = FALSE;
360 }
361 else
362 {
363 _dbus_verbose ("Failed to write credentials: %s\n", error.message);
364 dbus_error_free (&error);
365 do_io_error (transport);
366 }
367 }
368
369 if (do_reading && transport->receive_credentials_pending)
370 {
371 /* FIXME this can fail due to IO error _or_ OOM, broken
372 * (somewhat tricky to fix since the OOM error can be set after
373 * we already read the credentials byte, so basically we need to
374 * separate reading the byte and storing it in the
375 * transport->credentials). Does not really matter for now
376 * because storing in credentials never actually fails on unix.
377 */
378 if (_dbus_read_credentials_socket (socket_transport->fd,
379 transport->credentials,
380 &error))
381 {
383 }
384 else
385 {
386 _dbus_verbose ("Failed to read credentials %s\n", error.message);
387 dbus_error_free (&error);
388 do_io_error (transport);
389 }
390 }
391
392 if (!(transport->send_credentials_pending ||
393 transport->receive_credentials_pending))
394 {
395 if (!_dbus_auth_set_credentials (transport->auth,
396 transport->credentials))
397 return FALSE;
398 }
399
400 return TRUE;
401}
402
403static dbus_bool_t
404do_authentication (DBusTransport *transport,
405 dbus_bool_t do_reading,
406 dbus_bool_t do_writing,
407 dbus_bool_t *auth_completed)
408{
409 dbus_bool_t oom;
410 dbus_bool_t orig_auth_state;
411
412 oom = FALSE;
413
414 orig_auth_state = _dbus_transport_try_to_authenticate (transport);
415
416 /* This is essential to avoid the check_write_watch() at the end,
417 * we don't want to add a write watch in do_iteration before
418 * we try writing and get EAGAIN
419 */
420 if (orig_auth_state)
421 {
422 if (auth_completed)
423 *auth_completed = FALSE;
424 return TRUE;
425 }
426
427 _dbus_transport_ref (transport);
428
429 while (!_dbus_transport_try_to_authenticate (transport) &&
431 {
432 if (!exchange_credentials (transport, do_reading, do_writing))
433 {
434 /* OOM */
435 oom = TRUE;
436 goto out;
437 }
438
439 if (transport->send_credentials_pending ||
441 {
442 _dbus_verbose ("send_credentials_pending = %d receive_credentials_pending = %d\n",
443 transport->send_credentials_pending,
444 transport->receive_credentials_pending);
445 goto out;
446 }
447
448#define TRANSPORT_SIDE(t) ((t)->is_server ? "server" : "client")
449 switch (_dbus_auth_do_work (transport->auth))
450 {
451 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
452 _dbus_verbose (" %s auth state: waiting for input\n",
453 TRANSPORT_SIDE (transport));
454 if (!do_reading || !read_data_into_auth (transport, &oom))
455 goto out;
456 break;
457
458 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
459 _dbus_verbose (" %s auth state: waiting for memory\n",
460 TRANSPORT_SIDE (transport));
461 oom = TRUE;
462 goto out;
463 break;
464
465 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
466 _dbus_verbose (" %s auth state: bytes to send\n",
467 TRANSPORT_SIDE (transport));
468 if (!do_writing || !write_data_from_auth (transport))
469 goto out;
470 break;
471
472 case DBUS_AUTH_STATE_NEED_DISCONNECT:
473 _dbus_verbose (" %s auth state: need to disconnect\n",
474 TRANSPORT_SIDE (transport));
475 do_io_error (transport);
476 break;
477
478 case DBUS_AUTH_STATE_AUTHENTICATED:
479 _dbus_verbose (" %s auth state: authenticated\n",
480 TRANSPORT_SIDE (transport));
481 break;
482
483 case DBUS_AUTH_STATE_INVALID:
484 /* fall through */
485 default:
486 _dbus_assert_not_reached ("invalid auth state");
487 }
488 }
489
490 out:
491 if (auth_completed)
492 *auth_completed = (orig_auth_state != _dbus_transport_try_to_authenticate (transport));
493
494 check_read_watch (transport);
495 check_write_watch (transport);
496 _dbus_transport_unref (transport);
497
498 if (oom)
499 return FALSE;
500 else
501 return TRUE;
502}
503
504/* returns false on oom */
505static dbus_bool_t
506do_writing (DBusTransport *transport)
507{
508 int total;
509 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
510 dbus_bool_t oom;
511
512 /* No messages without authentication! */
514 {
515 _dbus_verbose ("Not authenticated, not writing anything\n");
516 return TRUE;
517 }
518
519 if (transport->disconnected)
520 {
521 _dbus_verbose ("Not connected, not writing anything\n");
522 return TRUE;
523 }
524
525#if 1
526 _dbus_verbose ("do_writing(), have_messages = %d, fd = %" DBUS_SOCKET_FORMAT "\n",
528 _dbus_socket_printable (socket_transport->fd));
529#endif
530
531 oom = FALSE;
532 total = 0;
533
534 while (!transport->disconnected &&
536 {
537 int bytes_written;
538 DBusMessage *message;
539 const DBusString *header;
540 const DBusString *body;
541 int header_len, body_len;
542 int total_bytes_to_write;
543 int saved_errno;
544
545 if (total > socket_transport->max_bytes_written_per_iteration)
546 {
547 _dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n",
548 total, socket_transport->max_bytes_written_per_iteration);
549 goto out;
550 }
551
553 _dbus_assert (message != NULL);
554 dbus_message_lock (message);
555
556#if 0
557 _dbus_verbose ("writing message %p\n", message);
558#endif
559
561 &header, &body);
562
563 header_len = _dbus_string_get_length (header);
564 body_len = _dbus_string_get_length (body);
565
566 if (_dbus_auth_needs_encoding (transport->auth))
567 {
568 /* Does fd passing even make sense with encoded data? */
569 _dbus_assert(!DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport));
570
571 if (_dbus_string_get_length (&socket_transport->encoded_outgoing) == 0)
572 {
573 if (!_dbus_auth_encode_data (transport->auth,
574 header, &socket_transport->encoded_outgoing))
575 {
576 oom = TRUE;
577 goto out;
578 }
579
580 if (!_dbus_auth_encode_data (transport->auth,
581 body, &socket_transport->encoded_outgoing))
582 {
583 _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
584 oom = TRUE;
585 goto out;
586 }
587 }
588
589 total_bytes_to_write = _dbus_string_get_length (&socket_transport->encoded_outgoing);
590
591#if 0
592 _dbus_verbose ("encoded message is %d bytes\n",
593 total_bytes_to_write);
594#endif
595
596 bytes_written =
597 _dbus_write_socket (socket_transport->fd,
598 &socket_transport->encoded_outgoing,
599 socket_transport->message_bytes_written,
600 total_bytes_to_write - socket_transport->message_bytes_written);
601 saved_errno = _dbus_save_socket_errno ();
602 }
603 else
604 {
605 total_bytes_to_write = header_len + body_len;
606
607#if 0
608 _dbus_verbose ("message is %d bytes\n",
609 total_bytes_to_write);
610#endif
611
612#ifdef HAVE_UNIX_FD_PASSING
613 if (socket_transport->message_bytes_written <= 0 && DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport))
614 {
615 /* Send the fds along with the first byte of the message */
616 const int *unix_fds;
617 unsigned n;
618
619 _dbus_message_get_unix_fds(message, &unix_fds, &n);
620
621 bytes_written =
622 _dbus_write_socket_with_unix_fds_two (socket_transport->fd,
623 header,
624 socket_transport->message_bytes_written,
625 header_len - socket_transport->message_bytes_written,
626 body,
627 0, body_len,
628 unix_fds,
629 n);
630 saved_errno = _dbus_save_socket_errno ();
631
632 if (bytes_written > 0 && n > 0)
633 _dbus_verbose("Wrote %i unix fds\n", n);
634 }
635 else
636#endif
637 {
638 if (socket_transport->message_bytes_written < header_len)
639 {
640 bytes_written =
641 _dbus_write_socket_two (socket_transport->fd,
642 header,
643 socket_transport->message_bytes_written,
644 header_len - socket_transport->message_bytes_written,
645 body,
646 0, body_len);
647 }
648 else
649 {
650 bytes_written =
651 _dbus_write_socket (socket_transport->fd,
652 body,
653 (socket_transport->message_bytes_written - header_len),
654 body_len -
655 (socket_transport->message_bytes_written - header_len));
656 }
657
658 saved_errno = _dbus_save_socket_errno ();
659 }
660 }
661
662 if (bytes_written < 0)
663 {
664 /* EINTR already handled for us */
665
666 /* If the other end closed the socket with close() or shutdown(), we
667 * receive EPIPE here but we must not close the socket yet: there
668 * might still be some data to read. See:
669 * http://lists.freedesktop.org/archives/dbus/2008-March/009526.html
670 */
671
673 goto out;
674
675 /* Since Linux commit 25888e (from 2.6.37-rc4, Nov 2010), sendmsg()
676 * on Unix sockets returns -1 errno=ETOOMANYREFS when the passfd
677 * mechanism (SCM_RIGHTS) is used recursively with a recursion level
678 * of maximum 4. The kernel does not have an API to check whether
679 * the passed fds can be forwarded and it can change asynchronously.
680 * See:
681 * https://bugs.freedesktop.org/show_bug.cgi?id=80163
682 */
683
684 else if (_dbus_get_is_errno_etoomanyrefs (saved_errno))
685 {
686 /* We only send fds in the first byte of the message.
687 * ETOOMANYREFS cannot happen after.
688 */
689 _dbus_assert (socket_transport->message_bytes_written == 0);
690
691 _dbus_verbose (" discard message of %d bytes due to ETOOMANYREFS\n",
692 total_bytes_to_write);
693
694 socket_transport->message_bytes_written = 0;
695 _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
696 _dbus_string_compact (&socket_transport->encoded_outgoing, 2048);
697
698 /* The message was not actually sent but it needs to be removed
699 * from the outgoing queue
700 */
702 message);
703 }
704 else
705 {
706 _dbus_verbose ("Error writing to remote app: %s\n",
707 _dbus_strerror (saved_errno));
708 do_io_error (transport);
709 goto out;
710 }
711 }
712 else
713 {
714 _dbus_verbose (" wrote %d bytes of %d\n", bytes_written,
715 total_bytes_to_write);
716
717 total += bytes_written;
718 socket_transport->message_bytes_written += bytes_written;
719
720 _dbus_assert (socket_transport->message_bytes_written <=
721 total_bytes_to_write);
722
723 if (socket_transport->message_bytes_written == total_bytes_to_write)
724 {
725 socket_transport->message_bytes_written = 0;
726 _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
727 _dbus_string_compact (&socket_transport->encoded_outgoing, 2048);
728
730 message);
731 }
732 }
733 }
734
735 out:
736 if (oom)
737 return FALSE;
738 else
739 return TRUE;
740}
741
742/* returns false on out-of-memory */
743static dbus_bool_t
744do_reading (DBusTransport *transport)
745{
746 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
747 DBusString *buffer;
748 int bytes_read;
749 int total;
750 dbus_bool_t oom;
751 int saved_errno;
752
753 _dbus_verbose ("fd = %" DBUS_SOCKET_FORMAT "\n",
754 _dbus_socket_printable (socket_transport->fd));
755
756 /* No messages without authentication! */
758 return TRUE;
759
760 oom = FALSE;
761
762 total = 0;
763
764 again:
765
766 /* See if we've exceeded max messages and need to disable reading */
767 check_read_watch (transport);
768
769 if (total > socket_transport->max_bytes_read_per_iteration)
770 {
771 _dbus_verbose ("%d bytes exceeds %d bytes read per iteration, returning\n",
772 total, socket_transport->max_bytes_read_per_iteration);
773 goto out;
774 }
775
776 _dbus_assert (socket_transport->read_watch != NULL ||
777 transport->disconnected);
778
779 if (transport->disconnected)
780 goto out;
781
782 if (!dbus_watch_get_enabled (socket_transport->read_watch))
783 return TRUE;
784
785 if (_dbus_auth_needs_decoding (transport->auth))
786 {
787 /* Does fd passing even make sense with encoded data? */
788 _dbus_assert(!DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport));
789
790 if (_dbus_string_get_length (&socket_transport->encoded_incoming) > 0)
791 bytes_read = _dbus_string_get_length (&socket_transport->encoded_incoming);
792 else
793 bytes_read = _dbus_read_socket (socket_transport->fd,
794 &socket_transport->encoded_incoming,
795 socket_transport->max_bytes_read_per_iteration);
796
797 saved_errno = _dbus_save_socket_errno ();
798
800 bytes_read);
801
802 if (bytes_read > 0)
803 {
805 &buffer,
806 NULL,
807 NULL);
808
809 if (!_dbus_auth_decode_data (transport->auth,
810 &socket_transport->encoded_incoming,
811 buffer))
812 {
813 _dbus_verbose ("Out of memory decoding incoming data\n");
815 buffer);
816
817 oom = TRUE;
818 goto out;
819 }
820
822 buffer);
823
824 _dbus_string_set_length (&socket_transport->encoded_incoming, 0);
825 _dbus_string_compact (&socket_transport->encoded_incoming, 2048);
826 }
827 }
828 else
829 {
830 int max_to_read = DBUS_MAXIMUM_MESSAGE_LENGTH;
831 dbus_bool_t may_read_unix_fds = TRUE;
832
834 &buffer,
835 &max_to_read,
836 &may_read_unix_fds);
837
838 if (max_to_read > socket_transport->max_bytes_read_per_iteration)
839 max_to_read = socket_transport->max_bytes_read_per_iteration;
840
841#ifdef HAVE_UNIX_FD_PASSING
842 if (DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport) && may_read_unix_fds)
843 {
844 int *fds;
845 unsigned int n_fds;
846
847 if (!_dbus_message_loader_get_unix_fds(transport->loader, &fds, &n_fds))
848 {
849 _dbus_verbose ("Out of memory reading file descriptors\n");
850 _dbus_message_loader_return_buffer (transport->loader, buffer);
851 oom = TRUE;
852 goto out;
853 }
854
855 bytes_read = _dbus_read_socket_with_unix_fds(socket_transport->fd,
856 buffer,
857 max_to_read,
858 fds, &n_fds);
859 saved_errno = _dbus_save_socket_errno ();
860
861 if (bytes_read >= 0 && n_fds > 0)
862 _dbus_verbose("Read %i unix fds\n", n_fds);
863
864 _dbus_message_loader_return_unix_fds(transport->loader, fds, bytes_read < 0 ? 0 : n_fds);
865 }
866 else
867#endif
868 {
869 bytes_read = _dbus_read_socket (socket_transport->fd,
870 buffer, max_to_read);
871 saved_errno = _dbus_save_socket_errno ();
872 }
873
875 buffer);
876 }
877
878 if (bytes_read < 0)
879 {
880 /* EINTR already handled for us */
881
882 if (_dbus_get_is_errno_enomem (saved_errno))
883 {
884 _dbus_verbose ("Out of memory in read()/do_reading()\n");
885 oom = TRUE;
886 goto out;
887 }
888 else if (_dbus_get_is_errno_eagain_or_ewouldblock (saved_errno))
889 goto out;
890 else
891 {
892 _dbus_verbose ("Error reading from remote app: %s\n",
893 _dbus_strerror (saved_errno));
894 do_io_error (transport);
895 goto out;
896 }
897 }
898 else if (bytes_read == 0)
899 {
900 _dbus_verbose ("Disconnected from remote app\n");
901 do_io_error (transport);
902 goto out;
903 }
904 else
905 {
906 _dbus_verbose (" read %d bytes\n", bytes_read);
907
908 total += bytes_read;
909
910 if (!_dbus_transport_queue_messages (transport))
911 {
912 oom = TRUE;
913 _dbus_verbose (" out of memory when queueing messages we just read in the transport\n");
914 goto out;
915 }
916
917 /* Try reading more data until we get EAGAIN and return, or
918 * exceed max bytes per iteration. If in blocking mode of
919 * course we'll block instead of returning.
920 */
921 goto again;
922 }
923
924 out:
925 if (oom)
926 return FALSE;
927 else
928 return TRUE;
929}
930
931static dbus_bool_t
932unix_error_with_read_to_come (DBusTransport *itransport,
933 DBusWatch *watch,
934 unsigned int flags)
935{
936 DBusTransportSocket *transport = (DBusTransportSocket *) itransport;
937
938 if (!(flags & DBUS_WATCH_HANGUP || flags & DBUS_WATCH_ERROR))
939 return FALSE;
940
941 /* If we have a read watch enabled ...
942 we -might have data incoming ... => handle the HANGUP there */
943 if (watch != transport->read_watch &&
944 _dbus_watch_get_enabled (transport->read_watch))
945 return FALSE;
946
947 return TRUE;
948}
949
950static dbus_bool_t
951socket_handle_watch (DBusTransport *transport,
952 DBusWatch *watch,
953 unsigned int flags)
954{
955 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
956
957 _dbus_assert (watch == socket_transport->read_watch ||
958 watch == socket_transport->write_watch);
959 _dbus_assert (watch != NULL);
960
961 /* If we hit an error here on a write watch, don't disconnect the transport yet because data can
962 * still be in the buffer and do_reading may need several iteration to read
963 * it all (because of its max_bytes_read_per_iteration limit).
964 */
965 if (!(flags & DBUS_WATCH_READABLE) && unix_error_with_read_to_come (transport, watch, flags))
966 {
967 _dbus_verbose ("Hang up or error on watch\n");
968 _dbus_transport_disconnect (transport);
969 return TRUE;
970 }
971
972 if (watch == socket_transport->read_watch &&
973 (flags & DBUS_WATCH_READABLE))
974 {
975 dbus_bool_t auth_finished;
976#if 1
977 _dbus_verbose ("handling read watch %p flags = %x\n",
978 watch, flags);
979#endif
980 if (!do_authentication (transport, TRUE, FALSE, &auth_finished))
981 return FALSE;
982
983 /* We don't want to do a read immediately following
984 * a successful authentication. This is so we
985 * have a chance to propagate the authentication
986 * state further up. Specifically, we need to
987 * process any pending data from the auth object.
988 */
989 if (!auth_finished)
990 {
991 if (!do_reading (transport))
992 {
993 _dbus_verbose ("no memory to read\n");
994 return FALSE;
995 }
996 }
997 else
998 {
999 _dbus_verbose ("Not reading anything since we just completed the authentication\n");
1000 }
1001 }
1002 else if (watch == socket_transport->write_watch &&
1003 (flags & DBUS_WATCH_WRITABLE))
1004 {
1005#if 1
1006 _dbus_verbose ("handling write watch, have_outgoing_messages = %d\n",
1008#endif
1009 if (!do_authentication (transport, FALSE, TRUE, NULL))
1010 return FALSE;
1011
1012 if (!do_writing (transport))
1013 {
1014 _dbus_verbose ("no memory to write\n");
1015 return FALSE;
1016 }
1017
1018 /* See if we still need the write watch */
1019 check_write_watch (transport);
1020 }
1021#ifdef DBUS_ENABLE_VERBOSE_MODE
1022 else
1023 {
1024 if (watch == socket_transport->read_watch)
1025 _dbus_verbose ("asked to handle read watch with non-read condition 0x%x\n",
1026 flags);
1027 else if (watch == socket_transport->write_watch)
1028 _dbus_verbose ("asked to handle write watch with non-write condition 0x%x\n",
1029 flags);
1030 else
1031 _dbus_verbose ("asked to handle watch %p on fd %" DBUS_SOCKET_FORMAT " that we don't recognize\n",
1032 watch, _dbus_socket_printable (_dbus_watch_get_socket (watch)));
1033 }
1034#endif /* DBUS_ENABLE_VERBOSE_MODE */
1035
1036 return TRUE;
1037}
1038
1039static void
1040socket_disconnect (DBusTransport *transport)
1041{
1042 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1043
1044 _dbus_verbose ("\n");
1045
1046 free_watches (transport);
1047
1048 _dbus_close_socket (&socket_transport->fd, NULL);
1049}
1050
1051static dbus_bool_t
1052socket_connection_set (DBusTransport *transport)
1053{
1054 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1055
1056 _dbus_watch_set_handler (socket_transport->write_watch,
1058 transport->connection, NULL);
1059
1060 _dbus_watch_set_handler (socket_transport->read_watch,
1062 transport->connection, NULL);
1063
1065 socket_transport->write_watch))
1066 return FALSE;
1067
1069 socket_transport->read_watch))
1070 {
1072 socket_transport->write_watch);
1073 return FALSE;
1074 }
1075
1076 check_read_watch (transport);
1077 check_write_watch (transport);
1078
1079 return TRUE;
1080}
1081
1089static void
1090socket_do_iteration (DBusTransport *transport,
1091 unsigned int flags,
1092 int timeout_milliseconds)
1093{
1094 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1095 DBusPollFD poll_fd;
1096 int poll_res;
1097 int poll_timeout;
1098
1099 _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p fd = %" DBUS_SOCKET_FORMAT "\n",
1100 flags & DBUS_ITERATION_DO_READING ? "read" : "",
1101 flags & DBUS_ITERATION_DO_WRITING ? "write" : "",
1102 timeout_milliseconds,
1103 socket_transport->read_watch,
1104 socket_transport->write_watch,
1105 _dbus_socket_printable (socket_transport->fd));
1106
1107 /* the passed in DO_READING/DO_WRITING flags indicate whether to
1108 * read/write messages, but regardless of those we may need to block
1109 * for reading/writing to do auth. But if we do reading for auth,
1110 * we don't want to read any messages yet if not given DO_READING.
1111 */
1112
1113 poll_fd.fd = _dbus_socket_get_pollable (socket_transport->fd);
1114 poll_fd.events = 0;
1115
1117 {
1118 /* This is kind of a hack; if we have stuff to write, then try
1119 * to avoid the poll. This is probably about a 5% speedup on an
1120 * echo client/server.
1121 *
1122 * If both reading and writing were requested, we want to avoid this
1123 * since it could have funky effects:
1124 * - both ends spinning waiting for the other one to read
1125 * data so they can finish writing
1126 * - prioritizing all writing ahead of reading
1127 */
1128 if ((flags & DBUS_ITERATION_DO_WRITING) &&
1129 !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) &&
1130 !transport->disconnected &&
1132 {
1133 do_writing (transport);
1134
1135 if (transport->disconnected ||
1137 goto out;
1138 }
1139
1140 /* If we get here, we decided to do the poll() after all */
1141 _dbus_assert (socket_transport->read_watch);
1142 if (flags & DBUS_ITERATION_DO_READING)
1143 poll_fd.events |= _DBUS_POLLIN;
1144
1145 _dbus_assert (socket_transport->write_watch);
1146 if (flags & DBUS_ITERATION_DO_WRITING)
1147 poll_fd.events |= _DBUS_POLLOUT;
1148 }
1149 else
1150 {
1151 DBusAuthState auth_state;
1152
1153 auth_state = _dbus_auth_do_work (transport->auth);
1154
1155 if (transport->receive_credentials_pending ||
1156 auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
1157 poll_fd.events |= _DBUS_POLLIN;
1158
1159 if (transport->send_credentials_pending ||
1160 auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
1161 poll_fd.events |= _DBUS_POLLOUT;
1162 }
1163
1164 if (poll_fd.events)
1165 {
1166 int saved_errno;
1167
1168 if (flags & DBUS_ITERATION_BLOCK)
1169 poll_timeout = timeout_milliseconds;
1170 else
1171 poll_timeout = 0;
1172
1173 /* For blocking selects we drop the connection lock here
1174 * to avoid blocking out connection access during a potentially
1175 * indefinite blocking call. The io path is still protected
1176 * by the io_path_cond condvar, so we won't reenter this.
1177 */
1178 if (flags & DBUS_ITERATION_BLOCK)
1179 {
1180 _dbus_verbose ("unlock pre poll\n");
1182 }
1183
1184 again:
1185 poll_res = _dbus_poll (&poll_fd, 1, poll_timeout);
1186 saved_errno = _dbus_save_socket_errno ();
1187
1188 if (poll_res < 0 && _dbus_get_is_errno_eintr (saved_errno))
1189 goto again;
1190
1191 if (flags & DBUS_ITERATION_BLOCK)
1192 {
1193 _dbus_verbose ("lock post poll\n");
1194 _dbus_connection_lock (transport->connection);
1195 }
1196
1197 if (poll_res >= 0)
1198 {
1199 if (poll_res == 0)
1200 poll_fd.revents = 0; /* some concern that posix does not guarantee this;
1201 * valgrind flags it as an error. though it probably
1202 * is guaranteed on linux at least.
1203 */
1204
1205 if (poll_fd.revents & _DBUS_POLLERR)
1206 do_io_error (transport);
1207 else
1208 {
1209 dbus_bool_t need_read = (poll_fd.revents & _DBUS_POLLIN) > 0;
1210 dbus_bool_t need_write = (poll_fd.revents & _DBUS_POLLOUT) > 0;
1211 dbus_bool_t authentication_completed;
1212
1213 _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
1214 need_read, need_write);
1215 do_authentication (transport, need_read, need_write,
1216 &authentication_completed);
1217
1218 /* See comment in socket_handle_watch. */
1219 if (authentication_completed)
1220 goto out;
1221
1222 if (need_read && (flags & DBUS_ITERATION_DO_READING))
1223 do_reading (transport);
1224 if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
1225 do_writing (transport);
1226 }
1227 }
1228 else
1229 {
1230 _dbus_verbose ("Error from _dbus_poll(): %s\n",
1231 _dbus_strerror (saved_errno));
1232 }
1233 }
1234
1235
1236 out:
1237 /* We need to install the write watch only if we did not
1238 * successfully write everything. Note we need to be careful that we
1239 * don't call check_write_watch *before* do_writing, since it's
1240 * inefficient to add the write watch, and we can avoid it most of
1241 * the time since we can write immediately.
1242 *
1243 * However, we MUST always call check_write_watch(); DBusConnection code
1244 * relies on the fact that running an iteration will notice that
1245 * messages are pending.
1246 */
1247 check_write_watch (transport);
1248
1249 _dbus_verbose (" ... leaving do_iteration()\n");
1250}
1251
1252static void
1253socket_live_messages_changed (DBusTransport *transport)
1254{
1255 /* See if we should look for incoming messages again */
1256 check_read_watch (transport);
1257}
1258
1259
1260static dbus_bool_t
1261socket_get_socket_fd (DBusTransport *transport,
1262 DBusSocket *fd_p)
1263{
1264 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1265
1266 *fd_p = socket_transport->fd;
1267
1268 return TRUE;
1269}
1270
1271static const DBusTransportVTable socket_vtable = {
1272 socket_finalize,
1273 socket_handle_watch,
1274 socket_disconnect,
1275 socket_connection_set,
1276 socket_do_iteration,
1277 socket_live_messages_changed,
1278 socket_get_socket_fd
1279};
1280
1294 const DBusString *server_guid,
1295 const DBusString *address)
1296{
1297 DBusTransportSocket *socket_transport;
1298 DBusString invalid = _DBUS_STRING_INIT_INVALID;
1299
1300 socket_transport = dbus_new0 (DBusTransportSocket, 1);
1301 if (socket_transport == NULL)
1302 return NULL;
1303
1304 /* So they can be "freed" without error */
1305 socket_transport->encoded_outgoing = invalid;
1306 socket_transport->encoded_incoming = invalid;
1307
1308 if (!_dbus_string_init (&socket_transport->encoded_outgoing))
1309 goto failed;
1310
1311 if (!_dbus_string_init (&socket_transport->encoded_incoming))
1312 goto failed;
1313
1314 socket_transport->write_watch = _dbus_watch_new (_dbus_socket_get_pollable (fd),
1316 FALSE,
1317 NULL, NULL, NULL);
1318 if (socket_transport->write_watch == NULL)
1319 goto failed;
1320
1321 socket_transport->read_watch = _dbus_watch_new (_dbus_socket_get_pollable (fd),
1323 FALSE,
1324 NULL, NULL, NULL);
1325 if (socket_transport->read_watch == NULL)
1326 goto failed;
1327
1328 if (!_dbus_transport_init_base (&socket_transport->base,
1329 &socket_vtable,
1330 server_guid, address))
1331 goto failed;
1332
1333#ifdef HAVE_UNIX_FD_PASSING
1335#endif
1336
1337 socket_transport->fd = fd;
1338 socket_transport->message_bytes_written = 0;
1339
1340 /* These values should probably be tunable or something. */
1341 socket_transport->max_bytes_read_per_iteration = 2048;
1342 socket_transport->max_bytes_written_per_iteration = 2048;
1343
1344 return (DBusTransport*) socket_transport;
1345
1346failed:
1347 if (socket_transport->read_watch != NULL)
1348 {
1349 _dbus_watch_invalidate (socket_transport->read_watch);
1350 _dbus_watch_unref (socket_transport->read_watch);
1351 }
1352
1353 if (socket_transport->write_watch != NULL)
1354 {
1355 _dbus_watch_invalidate (socket_transport->write_watch);
1356 _dbus_watch_unref (socket_transport->write_watch);
1357 }
1358
1359 _dbus_string_free (&socket_transport->encoded_incoming);
1360 _dbus_string_free (&socket_transport->encoded_outgoing);
1361 dbus_free (socket_transport);
1362 return NULL;
1363}
1364
1378 const char *port,
1379 const char *family,
1380 const char *noncefile,
1381 DBusError *error)
1382{
1383 DBusSocket fd;
1384 DBusTransport *transport;
1385 DBusString address;
1386
1387 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1388
1389 if (!_dbus_string_init (&address))
1390 {
1392 return NULL;
1393 }
1394
1395 if (host == NULL)
1396 host = "localhost";
1397
1398 if (!_dbus_string_append (&address, noncefile ? "nonce-tcp:" : "tcp:"))
1399 goto error;
1400
1401 if (!_dbus_string_append (&address, "host=") ||
1402 !_dbus_string_append (&address, host))
1403 goto error;
1404
1405 if (!_dbus_string_append (&address, ",port=") ||
1406 !_dbus_string_append (&address, port))
1407 goto error;
1408
1409 if (family != NULL &&
1410 (!_dbus_string_append (&address, ",family=") ||
1411 !_dbus_string_append (&address, family)))
1412 goto error;
1413
1414 if (noncefile != NULL &&
1415 (!_dbus_string_append (&address, ",noncefile=") ||
1416 !_dbus_string_append (&address, noncefile)))
1417 goto error;
1418
1419 fd = _dbus_connect_tcp_socket_with_nonce (host, port, family, noncefile, error);
1420 if (!_dbus_socket_is_valid (fd))
1421 {
1422 _DBUS_ASSERT_ERROR_IS_SET (error);
1423 _dbus_string_free (&address);
1424 return NULL;
1425 }
1426
1427 _dbus_verbose ("Successfully connected to tcp socket %s:%s\n",
1428 host, port);
1429
1430 transport = _dbus_transport_new_for_socket (fd, NULL, &address);
1431 _dbus_string_free (&address);
1432 if (transport == NULL)
1433 {
1435 _dbus_close_socket (&fd, NULL);
1436 }
1437
1438 return transport;
1439
1440error:
1441 _dbus_string_free (&address);
1443 return NULL;
1444}
1445
1454DBusTransportOpenResult
1456 DBusTransport **transport_p,
1457 DBusError *error)
1458{
1459 const char *method;
1460 dbus_bool_t isTcp;
1461 dbus_bool_t isNonceTcp;
1462
1463 method = dbus_address_entry_get_method (entry);
1464 _dbus_assert (method != NULL);
1465
1466 isTcp = strcmp (method, "tcp") == 0;
1467 isNonceTcp = strcmp (method, "nonce-tcp") == 0;
1468
1469 if (isTcp || isNonceTcp)
1470 {
1471 const char *host = dbus_address_entry_get_value (entry, "host");
1472 const char *port = dbus_address_entry_get_value (entry, "port");
1473 const char *family = dbus_address_entry_get_value (entry, "family");
1474 const char *noncefile = dbus_address_entry_get_value (entry, "noncefile");
1475
1476 if ((isNonceTcp == TRUE) != (noncefile != NULL)) {
1477 _dbus_set_bad_address (error, method, "noncefile", NULL);
1478 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1479 }
1480
1481 if (port == NULL)
1482 {
1483 _dbus_set_bad_address (error, method, "port", NULL);
1484 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1485 }
1486
1487 *transport_p = _dbus_transport_new_for_tcp_socket (host, port, family, noncefile, error);
1488 if (*transport_p == NULL)
1489 {
1490 _DBUS_ASSERT_ERROR_IS_SET (error);
1491 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
1492 }
1493 else
1494 {
1495 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1496 return DBUS_TRANSPORT_OPEN_OK;
1497 }
1498 }
1499 else
1500 {
1501 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1502 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
1503 }
1504}
1505
1517 dbus_bool_t abstract,
1518 DBusError *error)
1519{
1520 DBusSocket fd = DBUS_SOCKET_INIT;
1521 DBusTransport *transport;
1522 DBusString address;
1523 DBusString unescaped_path;
1524
1525 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1526
1527 if (!_dbus_string_init (&address))
1528 {
1530 return NULL;
1531 }
1532
1533 _dbus_string_init_const (&unescaped_path, path);
1534
1535 if ((abstract &&
1536 !_dbus_string_append (&address, "unix:abstract=")) ||
1537 (!abstract &&
1538 !_dbus_string_append (&address, "unix:path=")) ||
1539 !_dbus_address_append_escaped (&address, &unescaped_path))
1540 {
1542 goto failed_0;
1543 }
1544
1545 fd = _dbus_connect_unix_socket (path, abstract, error);
1546 if (!_dbus_socket_is_valid (fd))
1547 {
1548 _DBUS_ASSERT_ERROR_IS_SET (error);
1549 goto failed_0;
1550 }
1551
1552 _dbus_verbose ("Successfully connected to unix socket %s\n",
1553 path);
1554
1555 transport = _dbus_transport_new_for_socket (fd, NULL, &address);
1556 if (transport == NULL)
1557 {
1559 goto failed_1;
1560 }
1561
1562 _dbus_string_free (&address);
1563
1564 return transport;
1565
1566 failed_1:
1567 _dbus_close_socket (&fd, NULL);
1568 failed_0:
1569 _dbus_string_free (&address);
1570 return NULL;
1571}
1572
1581DBusTransportOpenResult
1583 DBusTransport **transport_p,
1584 DBusError *error)
1585{
1586 const char *method;
1587
1588 method = dbus_address_entry_get_method (entry);
1589 _dbus_assert (method != NULL);
1590
1591 if (strcmp (method, "unix") == 0)
1592 {
1593 const char *path = dbus_address_entry_get_value (entry, "path");
1594 const char *tmpdir = dbus_address_entry_get_value (entry, "tmpdir");
1595 const char *abstract = dbus_address_entry_get_value (entry, "abstract");
1596
1597 if (tmpdir != NULL)
1598 {
1600 "cannot use the \"tmpdir\" option for an address to connect to, only in an address to listen on");
1601 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1602 }
1603
1604 if (path == NULL && abstract == NULL)
1605 {
1606 _dbus_set_bad_address (error, "unix",
1607 "path or abstract",
1608 NULL);
1609 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1610 }
1611
1612 if (path != NULL && abstract != NULL)
1613 {
1615 "can't specify both \"path\" and \"abstract\" options in an address");
1616 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1617 }
1618
1619 if (path)
1620 *transport_p = _dbus_transport_new_for_domain_socket (path, FALSE,
1621 error);
1622 else
1623 *transport_p = _dbus_transport_new_for_domain_socket (abstract, TRUE,
1624 error);
1625 if (*transport_p == NULL)
1626 {
1627 _DBUS_ASSERT_ERROR_IS_SET (error);
1628 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
1629 }
1630 else
1631 {
1632 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1633 return DBUS_TRANSPORT_OPEN_OK;
1634 }
1635 }
1636 else
1637 {
1638 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1639 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
1640 }
1641}
1642
dbus_bool_t _dbus_address_append_escaped(DBusString *escaped, const DBusString *unescaped)
Appends an escaped version of one string to another string, using the D-Bus address escaping mechanis...
Definition: dbus-address.c:107
void _dbus_set_bad_address(DBusError *error, const char *address_problem_type, const char *address_problem_field, const char *address_problem_other)
Sets DBUS_ERROR_BAD_ADDRESS.
Definition: dbus-address.c:68
const char * dbus_address_entry_get_method(DBusAddressEntry *entry)
Returns the method string of an address entry.
Definition: dbus-address.c:230
const char * dbus_address_entry_get_value(DBusAddressEntry *entry, const char *key)
Returns a value from a key of an entry.
Definition: dbus-address.c:247
DBusAuthState _dbus_auth_do_work(DBusAuth *auth)
Analyzes buffered input and moves the auth conversation forward, returning the new state of the auth ...
Definition: dbus-auth.c:2538
dbus_bool_t _dbus_auth_encode_data(DBusAuth *auth, const DBusString *plaintext, DBusString *encoded)
Called post-authentication, encodes a block of bytes for sending to the peer.
Definition: dbus-auth.c:2725
dbus_bool_t _dbus_auth_needs_encoding(DBusAuth *auth)
Called post-authentication, indicates whether we need to encode the message stream with _dbus_auth_en...
Definition: dbus-auth.c:2698
dbus_bool_t _dbus_auth_set_credentials(DBusAuth *auth, DBusCredentials *credentials)
Sets credentials received via reliable means from the operating system.
Definition: dbus-auth.c:2820
dbus_bool_t _dbus_auth_get_bytes_to_send(DBusAuth *auth, const DBusString **str)
Gets bytes that need to be sent to the peer we're conversing with.
Definition: dbus-auth.c:2582
dbus_bool_t _dbus_auth_decode_data(DBusAuth *auth, const DBusString *encoded, DBusString *plaintext)
Called post-authentication, decodes a block of bytes received from the peer.
Definition: dbus-auth.c:2788
void _dbus_auth_set_unix_fd_possible(DBusAuth *auth, dbus_bool_t b)
Sets whether unix fd passing is potentially on the transport and hence shall be negotiated.
Definition: dbus-auth.c:2896
void _dbus_auth_return_buffer(DBusAuth *auth, DBusString *buffer)
Returns a buffer with new data read into it.
Definition: dbus-auth.c:2645
void _dbus_auth_get_buffer(DBusAuth *auth, DBusString **buffer)
Get a buffer to be used for reading bytes from the peer we're conversing with.
Definition: dbus-auth.c:2627
dbus_bool_t _dbus_auth_needs_decoding(DBusAuth *auth)
Called post-authentication, indicates whether we need to decode the message stream with _dbus_auth_de...
Definition: dbus-auth.c:2757
void _dbus_auth_bytes_sent(DBusAuth *auth, int bytes_sent)
Notifies the auth conversation object that the given number of bytes of the outgoing buffer have been...
Definition: dbus-auth.c:2607
dbus_bool_t _dbus_connection_handle_watch(DBusWatch *watch, unsigned int condition, void *data)
A callback for use with dbus_watch_new() to create a DBusWatch.
dbus_bool_t _dbus_connection_has_messages_to_send_unlocked(DBusConnection *connection)
Checks whether there are messages in the outgoing message queue.
DBUS_PRIVATE_EXPORT void _dbus_connection_unlock(DBusConnection *connection)
Releases the connection lock.
DBUS_PRIVATE_EXPORT void _dbus_connection_lock(DBusConnection *connection)
Acquires the connection lock.
void _dbus_connection_remove_watch_unlocked(DBusConnection *connection, DBusWatch *watch)
Removes a watch using the connection's DBusRemoveWatchFunction if available.
void _dbus_connection_toggle_watch_unlocked(DBusConnection *connection, DBusWatch *watch, dbus_bool_t enabled)
Toggles a watch and notifies app via connection's DBusWatchToggledFunction if available.
dbus_bool_t _dbus_connection_add_watch_unlocked(DBusConnection *connection, DBusWatch *watch)
Adds a watch using the connection's DBusAddWatchFunction if available.
DBusMessage * _dbus_connection_get_message_to_send(DBusConnection *connection)
Gets the next outgoing message.
void _dbus_connection_message_sent_unlocked(DBusConnection *connection, DBusMessage *message)
Notifies the connection that a message has been sent, so the message can be removed from the outgoing...
@ DBUS_WATCH_READABLE
As in POLLIN.
@ DBUS_WATCH_WRITABLE
As in POLLOUT.
@ DBUS_WATCH_HANGUP
As in POLLHUP (can't watch for it, but can be present in current state passed to dbus_watch_handle())...
@ DBUS_WATCH_ERROR
As in POLLERR (can't watch for this, but can be present in current state passed to dbus_watch_handle(...
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:62
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:354
void dbus_error_free(DBusError *error)
Frees an error that's been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition: dbus-errors.c:211
dbus_bool_t _dbus_get_is_errno_epipe(int e)
See if errno is EPIPE.
Definition: dbus-sysdeps.c:734
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
dbus_bool_t _dbus_get_is_errno_etoomanyrefs(int e)
See if errno is ETOOMANYREFS.
Definition: dbus-sysdeps.c:744
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
dbus_bool_t _dbus_get_is_errno_eintr(int e)
See if errno is EINTR.
Definition: dbus-sysdeps.c:724
dbus_bool_t _dbus_get_is_errno_enomem(int e)
See if errno is ENOMEM.
Definition: dbus-sysdeps.c:714
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:692
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:58
void dbus_message_lock(DBusMessage *message)
Locks a message.
Definition: dbus-message.c:429
DBUS_PRIVATE_EXPORT void _dbus_message_get_unix_fds(DBusMessage *message, const int **fds, unsigned *n_fds)
Gets the unix fds to be sent over the network for this message.
Definition: dbus-message.c:260
void _dbus_message_get_network_data(DBusMessage *message, const DBusString **header, const DBusString **body)
Gets the data to be sent over the network for this message.
Definition: dbus-message.c:241
DBUS_PRIVATE_EXPORT void _dbus_message_loader_get_buffer(DBusMessageLoader *loader, DBusString **buffer, int *max_to_read, dbus_bool_t *may_read_unix_fds)
Gets the buffer to use for reading data from the network.
DBUS_PRIVATE_EXPORT void _dbus_message_loader_return_buffer(DBusMessageLoader *loader, DBusString *buffer)
Returns a buffer obtained from _dbus_message_loader_get_buffer(), indicating to the loader how many b...
#define DBUS_MAXIMUM_MESSAGE_LENGTH
The maximum total message size including header and body; similar rationale to max array size.
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
long _dbus_counter_get_unix_fd_value(DBusCounter *counter)
Gets the current value of the unix fd counter.
long _dbus_counter_get_size_value(DBusCounter *counter)
Gets the current value of the size counter.
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition: dbus-string.c:845
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition: dbus-string.c:978
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:180
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:195
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as #_DBUS_STRING_I...
Definition: dbus-string.c:276
int _dbus_string_get_length(const DBusString *str)
Gets the length of a string (not including nul termination).
Definition: dbus-string.c:782
dbus_bool_t _dbus_string_compact(DBusString *str, int max_waste)
Compacts the string to avoid wasted memory.
Definition: dbus-string.c:418
#define _DBUS_POLLOUT
Writing now will not block.
Definition: dbus-sysdeps.h:427
dbus_bool_t _dbus_get_is_errno_eagain_or_ewouldblock(int e)
See if errno is EAGAIN or EWOULDBLOCK (this has to be done differently for Winsock so is abstracted)
int _dbus_read_socket(DBusSocket fd, DBusString *buffer, int count)
Like _dbus_read(), but only works on sockets so is available on Windows.
#define _DBUS_POLLERR
Error condition.
Definition: dbus-sysdeps.h:429
dbus_bool_t _dbus_socket_can_pass_unix_fd(DBusSocket fd)
Checks whether file descriptors may be passed via the socket.
int _dbus_write_socket(DBusSocket fd, const DBusString *buffer, int start, int len)
Like _dbus_write(), but only supports sockets and is thus available on Windows.
int _dbus_read_socket_with_unix_fds(DBusSocket fd, DBusString *buffer, int count, int *fds, unsigned int *n_fds)
Like _dbus_read_socket() but also tries to read unix fds from the socket.
dbus_bool_t _dbus_read_credentials_socket(DBusSocket client_fd, DBusCredentials *credentials, DBusError *error)
Reads a single byte which must be nul (an error occurs otherwise), and reads unix credentials if avai...
#define _DBUS_POLLIN
There is data to read.
Definition: dbus-sysdeps.h:423
dbus_bool_t _dbus_send_credentials_socket(DBusSocket server_fd, DBusError *error)
Sends a single nul byte with our UNIX credentials as ancillary data.
dbus_bool_t _dbus_close_socket(DBusSocket *fd, DBusError *error)
Closes a socket and invalidates it.
DBusSocket _dbus_connect_unix_socket(const char *path, dbus_bool_t abstract, DBusError *error)
Creates a socket and connects it to the UNIX domain socket at the given path.
int _dbus_poll(DBusPollFD *fds, int n_fds, int timeout_milliseconds)
Wrapper for poll().
int _dbus_write_socket_two(DBusSocket fd, const DBusString *buffer1, int start1, int len1, const DBusString *buffer2, int start2, int len2)
Like _dbus_write_two() but only works on sockets and is thus available on Windows.
DBusTransportOpenResult _dbus_transport_open_socket(DBusAddressEntry *entry, DBusTransport **transport_p, DBusError *error)
Opens a TCP socket transport.
DBusTransportOpenResult _dbus_transport_open_unix_socket(DBusAddressEntry *entry, DBusTransport **transport_p, DBusError *error)
Opens a UNIX socket transport.
DBusTransport * _dbus_transport_new_for_tcp_socket(const char *host, const char *port, const char *family, const char *noncefile, DBusError *error)
Creates a new transport for the given hostname and port.
DBusTransport * _dbus_transport_new_for_socket(DBusSocket fd, const DBusString *server_guid, const DBusString *address)
Creates a new transport for the given socket file descriptor.
DBusTransport * _dbus_transport_new_for_domain_socket(const char *path, dbus_bool_t abstract, DBusError *error)
Creates a new transport for the given Unix domain socket path.
dbus_bool_t _dbus_transport_queue_messages(DBusTransport *transport)
Processes data we've read while handling a watch, potentially converting some of it to messages and q...
DBusTransport * _dbus_transport_ref(DBusTransport *transport)
Increments the reference count for the transport.
void _dbus_transport_disconnect(DBusTransport *transport)
Closes our end of the connection to a remote application.
dbus_bool_t _dbus_transport_init_base(DBusTransport *transport, const DBusTransportVTable *vtable, const DBusString *server_guid, const DBusString *address)
Initializes the base class members of DBusTransport.
void _dbus_transport_unref(DBusTransport *transport)
Decrements the reference count for the transport.
dbus_bool_t _dbus_transport_try_to_authenticate(DBusTransport *transport)
Returns TRUE if we have been authenticated.
dbus_bool_t _dbus_transport_get_is_connected(DBusTransport *transport)
Returns TRUE if the transport has not been disconnected.
void _dbus_transport_finalize_base(DBusTransport *transport)
Finalizes base class members of DBusTransport.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
void _dbus_watch_set_handler(DBusWatch *watch, DBusWatchHandler handler, void *data, DBusFreeFunction free_data_function)
Sets the handler for the watch.
Definition: dbus-watch.c:498
DBusWatch * _dbus_watch_new(DBusPollable fd, unsigned int flags, dbus_bool_t enabled, DBusWatchHandler handler, void *data, DBusFreeFunction free_data_function)
Creates a new DBusWatch.
Definition: dbus-watch.c:88
void _dbus_watch_unref(DBusWatch *watch)
Decrements the reference count of a DBusWatch object and finalizes the object if the count reaches ze...
Definition: dbus-watch.c:138
void _dbus_watch_invalidate(DBusWatch *watch)
Clears the file descriptor from a now-invalid watch object so that no one tries to use it.
Definition: dbus-watch.c:169
DBUS_EXPORT dbus_bool_t dbus_watch_get_enabled(DBusWatch *watch)
Returns whether a watch is enabled or not.
Definition: dbus-watch.c:702
Internals of DBusAddressEntry.
Definition: dbus-address.c:47
Object representing an exception.
Definition: dbus-errors.h:49
const char * message
public error message field
Definition: dbus-errors.h:51
Internals of DBusMessage.
short events
Events to poll for.
Definition: dbus-sysdeps.h:418
short revents
Events that occurred.
Definition: dbus-sysdeps.h:419
DBusPollable fd
File descriptor.
Definition: dbus-sysdeps.h:417
Socket interface.
Definition: dbus-sysdeps.h:179
Implementation details of DBusTransportSocket.
DBusWatch * write_watch
Watch for writability.
int message_bytes_written
Number of bytes of current outgoing message that have been written.
int max_bytes_written_per_iteration
To avoid blocking too long.
DBusString encoded_incoming
Encoded version of current incoming data.
DBusString encoded_outgoing
Encoded version of current outgoing message.
DBusSocket fd
File descriptor.
DBusTransport base
Parent instance.
DBusWatch * read_watch
Watch for readability.
int max_bytes_read_per_iteration
To avoid blocking too long.
The virtual table that must be implemented to create a new kind of transport.
Object representing a transport such as a socket.
long max_live_messages_size
Max total size of received messages.
long max_live_messages_unix_fds
Max total unix fds of received messages.
unsigned int disconnected
TRUE if we are disconnected.
unsigned int send_credentials_pending
TRUE if we need to send credentials
DBusConnection * connection
Connection owning this transport.
unsigned int receive_credentials_pending
TRUE if we need to receive credentials
DBusMessageLoader * loader
Message-loading buffer.
DBusCredentials * credentials
Credentials of other end read from the socket.
DBusAuth * auth
Authentication conversation.
DBusCounter * live_messages
Counter for size/unix fds of all live messages.
Implementation of DBusWatch.
Definition: dbus-watch.c:41