D-Bus 1.16.4
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 * SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
7 *
8 * Licensed under the Academic Free License version 2.1
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 *
24 */
25
26#include <config.h>
27
28#include <stdio.h>
29
30#include "dbus-internals.h"
31#include "dbus-connection-internal.h"
32#include "dbus-nonce.h"
33#include "dbus-transport-socket.h"
34#include "dbus-transport-protected.h"
35#include "dbus-watch.h"
36#include "dbus-credentials.h"
37
45
50
80
81static void
82free_watches (DBusTransport *transport)
83{
84 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
85
86 _dbus_verbose ("start\n");
87
88 if (socket_transport->read_watch)
89 {
90 if (transport->connection)
92 socket_transport->read_watch);
93 _dbus_watch_invalidate (socket_transport->read_watch);
94 _dbus_watch_unref (socket_transport->read_watch);
95 socket_transport->read_watch = NULL;
96 }
97
98 if (socket_transport->write_watch)
99 {
100 if (transport->connection)
102 socket_transport->write_watch);
103 _dbus_watch_invalidate (socket_transport->write_watch);
104 _dbus_watch_unref (socket_transport->write_watch);
105 socket_transport->write_watch = NULL;
106 }
107
108 _dbus_verbose ("end\n");
109}
110
111static void
112socket_finalize (DBusTransport *transport)
113{
114 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
115
116 _dbus_verbose ("\n");
117
118 free_watches (transport);
119
120 _dbus_string_free (&socket_transport->encoded_outgoing);
121 _dbus_string_free (&socket_transport->encoded_incoming);
122
124
125 _dbus_assert (socket_transport->read_watch == NULL);
126 _dbus_assert (socket_transport->write_watch == NULL);
127
128 dbus_free (transport);
129}
130
131static void
132check_write_watch (DBusTransport *transport)
133{
134 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
135 dbus_bool_t needed;
136
137 if (transport->connection == NULL)
138 return;
139
140 if (transport->disconnected)
141 {
142 _dbus_assert (socket_transport->write_watch == NULL);
143 return;
144 }
145
146 _dbus_transport_ref (transport);
147
150 !socket_transport->epipe_seen;
151 else
152 {
153 if (transport->send_credentials_pending)
154 needed = TRUE;
155 else
156 {
157 DBusAuthState auth_state;
158
159 auth_state = _dbus_auth_do_work (transport->auth);
160
161 /* If we need memory we install the write watch just in case,
162 * if there's no need for it, it will get de-installed
163 * next time we try reading.
164 */
165 if (auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND ||
166 auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
167 needed = TRUE;
168 else
169 needed = FALSE;
170 }
171 }
172
173 _dbus_verbose ("check_write_watch(): needed = %d on connection %p watch %p fd = %" DBUS_SOCKET_FORMAT " outgoing messages exist %d\n",
174 needed, transport->connection, socket_transport->write_watch,
175 _dbus_socket_printable (socket_transport->fd),
177
179 socket_transport->write_watch,
180 needed);
181
182 _dbus_transport_unref (transport);
183}
184
185static void
186check_read_watch (DBusTransport *transport)
187{
188 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
189 dbus_bool_t need_read_watch;
190
191 _dbus_verbose ("fd = %" DBUS_SOCKET_FORMAT "\n",
192 _dbus_socket_printable (socket_transport->fd));
193
194 if (transport->connection == NULL)
195 return;
196
197 if (transport->disconnected)
198 {
199 _dbus_assert (socket_transport->read_watch == NULL);
200 return;
201 }
202
203 _dbus_transport_ref (transport);
204
206 need_read_watch =
209 else
210 {
211 if (transport->receive_credentials_pending)
212 need_read_watch = TRUE;
213 else
214 {
215 /* The reason to disable need_read_watch when not WAITING_FOR_INPUT
216 * is to avoid spinning on the file descriptor when we're waiting
217 * to write or for some other part of the auth process
218 */
219 DBusAuthState auth_state;
220
221 auth_state = _dbus_auth_do_work (transport->auth);
222
223 /* If we need memory we install the read watch just in case,
224 * if there's no need for it, it will get de-installed
225 * next time we try reading. If we're authenticated we
226 * install it since we normally have it installed while
227 * authenticated.
228 */
229 if (auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT ||
230 auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY ||
231 auth_state == DBUS_AUTH_STATE_AUTHENTICATED)
232 need_read_watch = TRUE;
233 else
234 need_read_watch = FALSE;
235 }
236 }
237
238 _dbus_verbose (" setting read watch enabled = %d\n", need_read_watch);
240 socket_transport->read_watch,
241 need_read_watch);
242
243 _dbus_transport_unref (transport);
244}
245
246static void
247do_io_error (DBusTransport *transport)
248{
249 _dbus_transport_ref (transport);
250 _dbus_transport_disconnect (transport);
251 _dbus_transport_unref (transport);
252}
253
254/* return value is whether we successfully read any new data. */
255static dbus_bool_t
256read_data_into_auth (DBusTransport *transport,
257 dbus_bool_t *oom)
258{
259 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
260 DBusString *buffer;
261 int bytes_read;
262 int saved_errno;
263
264 *oom = FALSE;
265
266 _dbus_auth_get_buffer (transport->auth, &buffer);
267
268 bytes_read = _dbus_read_socket (socket_transport->fd,
269 buffer, socket_transport->max_bytes_read_per_iteration);
270 saved_errno = _dbus_save_socket_errno ();
271
272 _dbus_auth_return_buffer (transport->auth, buffer);
273
274 if (bytes_read > 0)
275 {
276 _dbus_verbose (" read %d bytes in auth phase\n", bytes_read);
277
278 return TRUE;
279 }
280 else if (bytes_read < 0)
281 {
282 /* EINTR already handled for us */
283
284 if (_dbus_get_is_errno_enomem (saved_errno))
285 {
286 *oom = TRUE;
287 }
288 else if (_dbus_get_is_errno_eagain_or_ewouldblock (saved_errno))
289 ; /* do nothing, just return FALSE below */
290 else
291 {
292 _dbus_verbose ("Error reading from remote app: %s\n",
293 _dbus_strerror (saved_errno));
294 do_io_error (transport);
295 }
296
297 return FALSE;
298 }
299 else
300 {
301 _dbus_assert (bytes_read == 0);
302
303 _dbus_verbose ("Disconnected from remote app\n");
304 do_io_error (transport);
305
306 return FALSE;
307 }
308}
309
310/* Return value is whether we successfully wrote any bytes */
311static dbus_bool_t
312write_data_from_auth (DBusTransport *transport)
313{
314 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
315 int bytes_written;
316 int saved_errno;
317 const DBusString *buffer;
318
319 if (!_dbus_auth_get_bytes_to_send (transport->auth,
320 &buffer))
321 return FALSE;
322
323 bytes_written = _dbus_write_socket (socket_transport->fd,
324 buffer,
325 0, _dbus_string_get_length (buffer));
326 saved_errno = _dbus_save_socket_errno ();
327
328 if (bytes_written > 0)
329 {
330 _dbus_auth_bytes_sent (transport->auth, bytes_written);
331 return TRUE;
332 }
333 else if (bytes_written < 0)
334 {
335 /* EINTR already handled for us */
336
338 ;
339 else
340 {
341 _dbus_verbose ("Error writing to remote app: %s\n",
342 _dbus_strerror (saved_errno));
343 do_io_error (transport);
344 }
345 }
346
347 return FALSE;
348}
349
350/* FALSE on OOM */
351static dbus_bool_t
352exchange_credentials (DBusTransport *transport,
353 dbus_bool_t do_reading,
354 dbus_bool_t do_writing)
355{
356 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
358
359 _dbus_verbose ("exchange_credentials: do_reading = %d, do_writing = %d\n",
360 do_reading, do_writing);
361
362 if (do_writing && transport->send_credentials_pending)
363 {
364 if (_dbus_send_credentials_socket (socket_transport->fd,
365 &error))
366 {
367 transport->send_credentials_pending = FALSE;
368 }
369 else
370 {
371 _dbus_verbose ("Failed to write credentials: %s\n", error.message);
372 dbus_error_free (&error);
373 do_io_error (transport);
374 }
375 }
376
377 if (do_reading && transport->receive_credentials_pending)
378 {
379 /* FIXME this can fail due to IO error _or_ OOM, broken
380 * (somewhat tricky to fix since the OOM error can be set after
381 * we already read the credentials byte, so basically we need to
382 * separate reading the byte and storing it in the
383 * transport->credentials). Does not really matter for now
384 * because storing in credentials never actually fails on unix.
385 */
386 if (_dbus_read_credentials_socket (socket_transport->fd,
387 transport->credentials,
388 &error))
389 {
391 }
392 else
393 {
394 _dbus_verbose ("Failed to read credentials %s\n", error.message);
395 dbus_error_free (&error);
396 do_io_error (transport);
397 }
398 }
399
400 if (!(transport->send_credentials_pending ||
401 transport->receive_credentials_pending))
402 {
403 if (!_dbus_auth_set_credentials (transport->auth,
404 transport->credentials))
405 return FALSE;
406 }
407
408 return TRUE;
409}
410
411static dbus_bool_t
412do_authentication (DBusTransport *transport,
413 dbus_bool_t do_reading,
414 dbus_bool_t do_writing,
415 dbus_bool_t *auth_completed)
416{
417 dbus_bool_t oom;
418 dbus_bool_t orig_auth_state;
419
420 oom = FALSE;
421
422 orig_auth_state = _dbus_transport_try_to_authenticate (transport);
423
424 /* This is essential to avoid the check_write_watch() at the end,
425 * we don't want to add a write watch in do_iteration before
426 * we try writing and get EAGAIN
427 */
428 if (orig_auth_state)
429 {
430 if (auth_completed)
431 *auth_completed = FALSE;
432 return TRUE;
433 }
434
435 _dbus_transport_ref (transport);
436
437 while (!_dbus_transport_try_to_authenticate (transport) &&
439 {
440 if (!exchange_credentials (transport, do_reading, do_writing))
441 {
442 /* OOM */
443 oom = TRUE;
444 goto out;
445 }
446
447 if (transport->send_credentials_pending ||
449 {
450 _dbus_verbose ("send_credentials_pending = %d receive_credentials_pending = %d\n",
451 transport->send_credentials_pending,
452 transport->receive_credentials_pending);
453 goto out;
454 }
455
456#define TRANSPORT_SIDE(t) ((t)->is_server ? "server" : "client")
457 switch (_dbus_auth_do_work (transport->auth))
458 {
459 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
460 _dbus_verbose (" %s auth state: waiting for input\n",
461 TRANSPORT_SIDE (transport));
462 if (!do_reading || !read_data_into_auth (transport, &oom))
463 goto out;
464 break;
465
466 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
467 _dbus_verbose (" %s auth state: waiting for memory\n",
468 TRANSPORT_SIDE (transport));
469 oom = TRUE;
470 goto out;
471 break;
472
473 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
474 _dbus_verbose (" %s auth state: bytes to send\n",
475 TRANSPORT_SIDE (transport));
476 if (!do_writing || !write_data_from_auth (transport))
477 goto out;
478 break;
479
480 case DBUS_AUTH_STATE_NEED_DISCONNECT:
481 _dbus_verbose (" %s auth state: need to disconnect\n",
482 TRANSPORT_SIDE (transport));
483 do_io_error (transport);
484 break;
485
486 case DBUS_AUTH_STATE_AUTHENTICATED:
487 _dbus_verbose (" %s auth state: authenticated\n",
488 TRANSPORT_SIDE (transport));
489 break;
490
491 case DBUS_AUTH_STATE_INVALID:
492 /* fall through */
493 default:
494 _dbus_assert_not_reached ("invalid auth state");
495 }
496 }
497
498 out:
499 if (auth_completed)
500 *auth_completed = (orig_auth_state != _dbus_transport_try_to_authenticate (transport));
501
502 check_read_watch (transport);
503 check_write_watch (transport);
504 _dbus_transport_unref (transport);
505
506 if (oom)
507 return FALSE;
508 else
509 return TRUE;
510}
511
512/* returns false on oom */
513static dbus_bool_t
514do_writing (DBusTransport *transport)
515{
516 int total;
517 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
518 dbus_bool_t oom;
519
520 /* No messages without authentication! */
522 {
523 _dbus_verbose ("Not authenticated, not writing anything\n");
524 return TRUE;
525 }
526
527 if (transport->disconnected)
528 {
529 _dbus_verbose ("Not connected, not writing anything\n");
530 return TRUE;
531 }
532
533#if 1
534 _dbus_verbose ("do_writing(), have_messages = %d, fd = %" DBUS_SOCKET_FORMAT "\n",
536 _dbus_socket_printable (socket_transport->fd));
537#endif
538
539 oom = FALSE;
540 total = 0;
541
542 while (!transport->disconnected &&
544 {
545 int bytes_written;
546 DBusMessage *message;
547 const DBusString *header;
548 const DBusString *body;
549 int header_len, body_len;
550 int total_bytes_to_write;
551 int saved_errno;
552
553 if (total > socket_transport->max_bytes_written_per_iteration)
554 {
555 _dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n",
556 total, socket_transport->max_bytes_written_per_iteration);
557 goto out;
558 }
559
561 _dbus_assert (message != NULL);
562 dbus_message_lock (message);
563
564#if 0
565 _dbus_verbose ("writing message %p\n", message);
566#endif
567
569 &header, &body);
570
571 header_len = _dbus_string_get_length (header);
572 body_len = _dbus_string_get_length (body);
573
574 if (_dbus_auth_needs_encoding (transport->auth))
575 {
576 /* Does fd passing even make sense with encoded data? */
577 _dbus_assert(!DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport));
578
579 if (_dbus_string_get_length (&socket_transport->encoded_outgoing) == 0)
580 {
581 if (!_dbus_auth_encode_data (transport->auth,
582 header, &socket_transport->encoded_outgoing))
583 {
584 oom = TRUE;
585 goto out;
586 }
587
588 if (!_dbus_auth_encode_data (transport->auth,
589 body, &socket_transport->encoded_outgoing))
590 {
591 _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
592 oom = TRUE;
593 goto out;
594 }
595 }
596
597 total_bytes_to_write = _dbus_string_get_length (&socket_transport->encoded_outgoing);
598
599#if 0
600 _dbus_verbose ("encoded message is %d bytes\n",
601 total_bytes_to_write);
602#endif
603
604 bytes_written =
605 _dbus_write_socket (socket_transport->fd,
606 &socket_transport->encoded_outgoing,
607 socket_transport->message_bytes_written,
608 total_bytes_to_write - socket_transport->message_bytes_written);
609 saved_errno = _dbus_save_socket_errno ();
610 }
611 else
612 {
613 total_bytes_to_write = header_len + body_len;
614
615#if 0
616 _dbus_verbose ("message is %d bytes\n",
617 total_bytes_to_write);
618#endif
619
620#ifdef HAVE_UNIX_FD_PASSING
621 if (socket_transport->message_bytes_written <= 0 && DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport))
622 {
623 /* Send the fds along with the first byte of the message */
624 const int *unix_fds;
625 unsigned n;
626
627 _dbus_message_get_unix_fds(message, &unix_fds, &n);
628
629 bytes_written =
630 _dbus_write_socket_with_unix_fds_two (socket_transport->fd,
631 header,
632 socket_transport->message_bytes_written,
633 header_len - socket_transport->message_bytes_written,
634 body,
635 0, body_len,
636 unix_fds,
637 n);
638 saved_errno = _dbus_save_socket_errno ();
639
640 if (bytes_written > 0 && n > 0)
641 _dbus_verbose("Wrote %i unix fds\n", n);
642 }
643 else
644#endif
645 {
646 if (socket_transport->message_bytes_written < header_len)
647 {
648 bytes_written =
649 _dbus_write_socket_two (socket_transport->fd,
650 header,
651 socket_transport->message_bytes_written,
652 header_len - socket_transport->message_bytes_written,
653 body,
654 0, body_len);
655 }
656 else
657 {
658 bytes_written =
659 _dbus_write_socket (socket_transport->fd,
660 body,
661 (socket_transport->message_bytes_written - header_len),
662 body_len -
663 (socket_transport->message_bytes_written - header_len));
664 }
665
666 saved_errno = _dbus_save_socket_errno ();
667 }
668 }
669
670 if (bytes_written < 0)
671 {
672 /* EINTR already handled for us */
673
674 /* If the other end closed the socket with close() or shutdown(), we
675 * receive EPIPE here but we must not close the socket yet: there
676 * might still be some data to read. See:
677 * http://lists.freedesktop.org/archives/dbus/2008-March/009526.html
678 */
679
681 goto out;
682
683 if (_dbus_get_is_errno_epipe (saved_errno))
684 {
685 /* No further writes to this peer. */
686 socket_transport->epipe_seen = TRUE;
687 goto out;
688 }
689
690 /* Since Linux commit 25888e (from 2.6.37-rc4, Nov 2010), sendmsg()
691 * on Unix sockets returns -1 errno=ETOOMANYREFS when the passfd
692 * mechanism (SCM_RIGHTS) is used recursively with a recursion level
693 * of maximum 4. The kernel does not have an API to check whether
694 * the passed fds can be forwarded and it can change asynchronously.
695 * See:
696 * https://bugs.freedesktop.org/show_bug.cgi?id=80163
697 */
698
699 else if (_dbus_get_is_errno_etoomanyrefs (saved_errno))
700 {
701 /* We only send fds in the first byte of the message.
702 * ETOOMANYREFS cannot happen after.
703 */
704 _dbus_assert (socket_transport->message_bytes_written == 0);
705
706 _dbus_verbose (" discard message of %d bytes due to ETOOMANYREFS\n",
707 total_bytes_to_write);
708
709 socket_transport->message_bytes_written = 0;
710 _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
711 _dbus_string_compact (&socket_transport->encoded_outgoing, 2048);
712
713 /* The message was not actually sent but it needs to be removed
714 * from the outgoing queue
715 */
717 message);
718 }
719 else
720 {
721 _dbus_verbose ("Error writing to remote app: %s\n",
722 _dbus_strerror (saved_errno));
723 do_io_error (transport);
724 goto out;
725 }
726 }
727 else
728 {
729 _dbus_verbose (" wrote %d bytes of %d\n", bytes_written,
730 total_bytes_to_write);
731
732 total += bytes_written;
733 socket_transport->message_bytes_written += bytes_written;
734
735 _dbus_assert (socket_transport->message_bytes_written <=
736 total_bytes_to_write);
737
738 if (socket_transport->message_bytes_written == total_bytes_to_write)
739 {
740 socket_transport->message_bytes_written = 0;
741 _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
742 _dbus_string_compact (&socket_transport->encoded_outgoing, 2048);
743
745 message);
746 }
747 }
748 }
749
750 out:
751 if (oom)
752 return FALSE;
753 else
754 return TRUE;
755}
756
757/* returns false on out-of-memory */
758static dbus_bool_t
759do_reading (DBusTransport *transport)
760{
761 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
762 DBusString *buffer;
763 int bytes_read;
764 int total;
765 dbus_bool_t oom;
766 int saved_errno;
767
768 _dbus_verbose ("fd = %" DBUS_SOCKET_FORMAT "\n",
769 _dbus_socket_printable (socket_transport->fd));
770
771 /* No messages without authentication! */
773 return TRUE;
774
775 oom = FALSE;
776
777 total = 0;
778
779 again:
780
781 /* See if we've exceeded max messages and need to disable reading */
782 check_read_watch (transport);
783
784 if (total > socket_transport->max_bytes_read_per_iteration)
785 {
786 _dbus_verbose ("%d bytes exceeds %d bytes read per iteration, returning\n",
787 total, socket_transport->max_bytes_read_per_iteration);
788 goto out;
789 }
790
791 _dbus_assert (socket_transport->read_watch != NULL ||
792 transport->disconnected);
793
794 if (transport->disconnected)
795 goto out;
796
797 if (!dbus_watch_get_enabled (socket_transport->read_watch))
798 return TRUE;
799
800 if (_dbus_auth_needs_decoding (transport->auth))
801 {
802 /* Does fd passing even make sense with encoded data? */
803 _dbus_assert(!DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport));
804
805 if (_dbus_string_get_length (&socket_transport->encoded_incoming) > 0)
806 bytes_read = _dbus_string_get_length (&socket_transport->encoded_incoming);
807 else
808 bytes_read = _dbus_read_socket (socket_transport->fd,
809 &socket_transport->encoded_incoming,
810 socket_transport->max_bytes_read_per_iteration);
811
812 saved_errno = _dbus_save_socket_errno ();
813
814 _dbus_assert (_dbus_string_get_length (&socket_transport->encoded_incoming) ==
815 bytes_read);
816
817 if (bytes_read > 0)
818 {
820 &buffer,
821 NULL,
822 NULL);
823
824 if (!_dbus_auth_decode_data (transport->auth,
825 &socket_transport->encoded_incoming,
826 buffer))
827 {
828 _dbus_verbose ("Out of memory decoding incoming data\n");
830 buffer);
831
832 oom = TRUE;
833 goto out;
834 }
835
837 buffer);
838
839 _dbus_string_set_length (&socket_transport->encoded_incoming, 0);
840 _dbus_string_compact (&socket_transport->encoded_incoming, 2048);
841 }
842 }
843 else
844 {
845 int max_to_read = DBUS_MAXIMUM_MESSAGE_LENGTH;
846 dbus_bool_t may_read_unix_fds = TRUE;
847
849 &buffer,
850 &max_to_read,
851 &may_read_unix_fds);
852
853 if (max_to_read > socket_transport->max_bytes_read_per_iteration)
854 max_to_read = socket_transport->max_bytes_read_per_iteration;
855
856#ifdef HAVE_UNIX_FD_PASSING
857 if (DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport) && may_read_unix_fds)
858 {
859 int *fds;
860 unsigned int n_fds;
861
862 if (!_dbus_message_loader_get_unix_fds(transport->loader, &fds, &n_fds))
863 {
864 _dbus_verbose ("Out of memory reading file descriptors\n");
865 _dbus_message_loader_return_buffer (transport->loader, buffer);
866 oom = TRUE;
867 goto out;
868 }
869
870 bytes_read = _dbus_read_socket_with_unix_fds(socket_transport->fd,
871 buffer,
872 max_to_read,
873 fds, &n_fds);
874 saved_errno = _dbus_save_socket_errno ();
875
876 if (bytes_read >= 0 && n_fds > 0)
877 _dbus_verbose("Read %i unix fds\n", n_fds);
878
879 _dbus_message_loader_return_unix_fds(transport->loader, fds, bytes_read < 0 ? 0 : n_fds);
880 }
881 else
882#endif
883 {
884 bytes_read = _dbus_read_socket (socket_transport->fd,
885 buffer, max_to_read);
886 saved_errno = _dbus_save_socket_errno ();
887 }
888
890 buffer);
891 }
892
893 if (bytes_read < 0)
894 {
895 /* EINTR already handled for us */
896
897 if (_dbus_get_is_errno_enomem (saved_errno))
898 {
899 _dbus_verbose ("Out of memory in read()/do_reading()\n");
900 oom = TRUE;
901 goto out;
902 }
903 else if (_dbus_get_is_errno_eagain_or_ewouldblock (saved_errno))
904 goto out;
905 else
906 {
907 _dbus_verbose ("Error reading from remote app: %s\n",
908 _dbus_strerror (saved_errno));
909 do_io_error (transport);
910 goto out;
911 }
912 }
913 else if (bytes_read == 0)
914 {
915 _dbus_verbose ("Disconnected from remote app\n");
916 do_io_error (transport);
917 goto out;
918 }
919 else
920 {
921 _dbus_verbose (" read %d bytes\n", bytes_read);
922
923 total += bytes_read;
924
925 if (!_dbus_transport_queue_messages (transport))
926 {
927 oom = TRUE;
928 _dbus_verbose (" out of memory when queueing messages we just read in the transport\n");
929 goto out;
930 }
931
932 /* Try reading more data until we get EAGAIN and return, or
933 * exceed max bytes per iteration. If in blocking mode of
934 * course we'll block instead of returning.
935 */
936 goto again;
937 }
938
939 out:
940 if (oom)
941 return FALSE;
942 else
943 return TRUE;
944}
945
946static dbus_bool_t
947unix_error_with_read_to_come (DBusTransport *itransport,
948 DBusWatch *watch,
949 unsigned int flags)
950{
951 DBusTransportSocket *transport = (DBusTransportSocket *) itransport;
952
953 if (!(flags & DBUS_WATCH_HANGUP || flags & DBUS_WATCH_ERROR))
954 return FALSE;
955
956 /* If we have a read watch enabled ...
957 we -might have data incoming ... => handle the HANGUP there */
958 if (watch != transport->read_watch &&
959 _dbus_watch_get_enabled (transport->read_watch))
960 return FALSE;
961
962 return TRUE;
963}
964
965static dbus_bool_t
966socket_handle_watch (DBusTransport *transport,
967 DBusWatch *watch,
968 unsigned int flags)
969{
970 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
971
972 _dbus_assert (watch == socket_transport->read_watch ||
973 watch == socket_transport->write_watch);
974 _dbus_assert (watch != NULL);
975
976 /* If we hit an error here on a write watch, don't disconnect the transport yet because data can
977 * still be in the buffer and do_reading may need several iteration to read
978 * it all (because of its max_bytes_read_per_iteration limit).
979 */
980 if (!(flags & DBUS_WATCH_READABLE) && unix_error_with_read_to_come (transport, watch, flags))
981 {
982 _dbus_verbose ("Hang up or error on watch\n");
983 _dbus_transport_disconnect (transport);
984 return TRUE;
985 }
986
987 if (watch == socket_transport->read_watch &&
988 (flags & DBUS_WATCH_READABLE))
989 {
990 dbus_bool_t auth_finished;
991#if 1
992 _dbus_verbose ("handling read watch %p flags = %x\n",
993 watch, flags);
994#endif
995 if (!do_authentication (transport, TRUE, FALSE, &auth_finished))
996 return FALSE;
997
998 /* We don't want to do a read immediately following
999 * a successful authentication. This is so we
1000 * have a chance to propagate the authentication
1001 * state further up. Specifically, we need to
1002 * process any pending data from the auth object.
1003 */
1004 if (!auth_finished)
1005 {
1006 if (!do_reading (transport))
1007 {
1008 _dbus_verbose ("no memory to read\n");
1009 return FALSE;
1010 }
1011 }
1012 else
1013 {
1014 _dbus_verbose ("Not reading anything since we just completed the authentication\n");
1015 }
1016 }
1017 else if (watch == socket_transport->write_watch &&
1018 (flags & DBUS_WATCH_WRITABLE))
1019 {
1020#if 1
1021 _dbus_verbose ("handling write watch, have_outgoing_messages = %d\n",
1023#endif
1024 if (!do_authentication (transport, FALSE, TRUE, NULL))
1025 return FALSE;
1026
1027 if (!do_writing (transport))
1028 {
1029 _dbus_verbose ("no memory to write\n");
1030 return FALSE;
1031 }
1032
1033 /* See if we still need the write watch */
1034 check_write_watch (transport);
1035 }
1036#ifdef DBUS_ENABLE_VERBOSE_MODE
1037 else
1038 {
1039 if (watch == socket_transport->read_watch)
1040 _dbus_verbose ("asked to handle read watch with non-read condition 0x%x\n",
1041 flags);
1042 else if (watch == socket_transport->write_watch)
1043 _dbus_verbose ("asked to handle write watch with non-write condition 0x%x\n",
1044 flags);
1045 else
1046 _dbus_verbose ("asked to handle watch %p on fd %" DBUS_SOCKET_FORMAT " that we don't recognize\n",
1047 watch, _dbus_socket_printable (_dbus_watch_get_socket (watch)));
1048 }
1049#endif /* DBUS_ENABLE_VERBOSE_MODE */
1050
1051 return TRUE;
1052}
1053
1054static void
1055socket_disconnect (DBusTransport *transport)
1056{
1057 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1058
1059 _dbus_verbose ("\n");
1060
1061 free_watches (transport);
1062
1063 _dbus_close_socket (&socket_transport->fd, NULL);
1064}
1065
1066static dbus_bool_t
1067socket_connection_set (DBusTransport *transport)
1068{
1069 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1070
1071 _dbus_watch_set_handler (socket_transport->write_watch,
1073 transport->connection, NULL);
1074
1075 _dbus_watch_set_handler (socket_transport->read_watch,
1077 transport->connection, NULL);
1078
1080 socket_transport->write_watch))
1081 return FALSE;
1082
1084 socket_transport->read_watch))
1085 {
1087 socket_transport->write_watch);
1088 return FALSE;
1089 }
1090
1091 check_read_watch (transport);
1092 check_write_watch (transport);
1093
1094 return TRUE;
1095}
1096
1104static void
1105socket_do_iteration (DBusTransport *transport,
1106 unsigned int flags,
1107 int timeout_milliseconds)
1108{
1109 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1110 DBusPollFD poll_fd;
1111 int poll_res;
1112 int poll_timeout;
1113
1114 _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p fd = %" DBUS_SOCKET_FORMAT "\n",
1115 flags & DBUS_ITERATION_DO_READING ? "read" : "",
1116 flags & DBUS_ITERATION_DO_WRITING ? "write" : "",
1117 timeout_milliseconds,
1118 socket_transport->read_watch,
1119 socket_transport->write_watch,
1120 _dbus_socket_printable (socket_transport->fd));
1121
1122 /* the passed in DO_READING/DO_WRITING flags indicate whether to
1123 * read/write messages, but regardless of those we may need to block
1124 * for reading/writing to do auth. But if we do reading for auth,
1125 * we don't want to read any messages yet if not given DO_READING.
1126 */
1127
1128 poll_fd.fd = _dbus_socket_get_pollable (socket_transport->fd);
1129 poll_fd.events = 0;
1130
1132 {
1133 /* This is kind of a hack; if we have stuff to write, then try
1134 * to avoid the poll. This is probably about a 5% speedup on an
1135 * echo client/server.
1136 *
1137 * If both reading and writing were requested, we want to avoid this
1138 * since it could have funky effects:
1139 * - both ends spinning waiting for the other one to read
1140 * data so they can finish writing
1141 * - prioritizing all writing ahead of reading
1142 */
1143 if ((flags & DBUS_ITERATION_DO_WRITING) &&
1144 !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) &&
1145 !transport->disconnected &&
1147 {
1148 do_writing (transport);
1149
1150 if (transport->disconnected ||
1152 goto out;
1153 }
1154
1155 /* If we get here, we decided to do the poll() after all */
1156 _dbus_assert (socket_transport->read_watch);
1157 if (flags & DBUS_ITERATION_DO_READING)
1158 poll_fd.events |= _DBUS_POLLIN;
1159
1160 _dbus_assert (socket_transport->write_watch);
1161 if (flags & DBUS_ITERATION_DO_WRITING)
1162 poll_fd.events |= _DBUS_POLLOUT;
1163 }
1164 else
1165 {
1166 DBusAuthState auth_state;
1167
1168 auth_state = _dbus_auth_do_work (transport->auth);
1169
1170 if (transport->receive_credentials_pending ||
1171 auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
1172 poll_fd.events |= _DBUS_POLLIN;
1173
1174 if (transport->send_credentials_pending ||
1175 auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
1176 poll_fd.events |= _DBUS_POLLOUT;
1177 }
1178
1179 if (poll_fd.events)
1180 {
1181 int saved_errno;
1182
1183 if (flags & DBUS_ITERATION_BLOCK)
1184 poll_timeout = timeout_milliseconds;
1185 else
1186 poll_timeout = 0;
1187
1188 /* For blocking selects we drop the connection lock here
1189 * to avoid blocking out connection access during a potentially
1190 * indefinite blocking call. The io path is still protected
1191 * by the io_path_cond condvar, so we won't reenter this.
1192 */
1193 if (flags & DBUS_ITERATION_BLOCK)
1194 {
1195 _dbus_verbose ("unlock pre poll\n");
1197 }
1198
1199 again:
1200 poll_res = _dbus_poll (&poll_fd, 1, poll_timeout);
1201 saved_errno = _dbus_save_socket_errno ();
1202
1203 if (poll_res < 0 && _dbus_get_is_errno_eintr (saved_errno))
1204 goto again;
1205
1206 if (flags & DBUS_ITERATION_BLOCK)
1207 {
1208 _dbus_verbose ("lock post poll\n");
1209 _dbus_connection_lock (transport->connection);
1210 }
1211
1212 if (poll_res >= 0)
1213 {
1214 if (poll_res == 0)
1215 poll_fd.revents = 0; /* some concern that posix does not guarantee this;
1216 * valgrind flags it as an error. though it probably
1217 * is guaranteed on linux at least.
1218 */
1219
1220 if (poll_fd.revents & _DBUS_POLLERR)
1221 do_io_error (transport);
1222 else
1223 {
1224 /* FreeBSD does not deliver POLLERR when the other side gets disconnected.
1225 * Instead it delivers POLLHUP that is not coupled with POLLIN nor POLLOUT.
1226 * On the input side, treat this as equivalent to the POLLHUP|POLLIN we
1227 * would see in this situation on Linux, so that we will enter
1228 * do_reading() and detect the end-of-stream condition; and similarly,
1229 * on the output side, treat it as equivalent to POLLHUP|POLLOUT.
1230 */
1231 dbus_bool_t need_read = (poll_fd.revents & (_DBUS_POLLIN | _DBUS_POLLHUP)) > 0;
1232 dbus_bool_t need_write = (poll_fd.revents & (_DBUS_POLLOUT | _DBUS_POLLHUP)) > 0;
1233 dbus_bool_t authentication_completed;
1234
1235 _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
1236 need_read, need_write);
1237 do_authentication (transport, need_read, need_write,
1238 &authentication_completed);
1239
1240 /* See comment in socket_handle_watch. */
1241 if (authentication_completed)
1242 goto out;
1243
1244 if (need_read && (flags & DBUS_ITERATION_DO_READING))
1245 do_reading (transport);
1246 if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
1247 do_writing (transport);
1248 }
1249 }
1250 else
1251 {
1252 _dbus_verbose ("Error from _dbus_poll(): %s\n",
1253 _dbus_strerror (saved_errno));
1254 }
1255 }
1256
1257
1258 out:
1259 /* We need to install the write watch only if we did not
1260 * successfully write everything. Note we need to be careful that we
1261 * don't call check_write_watch *before* do_writing, since it's
1262 * inefficient to add the write watch, and we can avoid it most of
1263 * the time since we can write immediately.
1264 *
1265 * However, we MUST always call check_write_watch(); DBusConnection code
1266 * relies on the fact that running an iteration will notice that
1267 * messages are pending.
1268 */
1269 check_write_watch (transport);
1270
1271 _dbus_verbose (" ... leaving do_iteration()\n");
1272}
1273
1274static void
1275socket_live_messages_changed (DBusTransport *transport)
1276{
1277 /* See if we should look for incoming messages again */
1278 check_read_watch (transport);
1279}
1280
1281
1282static dbus_bool_t
1283socket_get_socket_fd (DBusTransport *transport,
1284 DBusSocket *fd_p)
1285{
1286 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1287
1288 *fd_p = socket_transport->fd;
1289
1290 return TRUE;
1291}
1292
1293static const DBusTransportVTable socket_vtable = {
1294 socket_finalize,
1295 socket_handle_watch,
1296 socket_disconnect,
1297 socket_connection_set,
1298 socket_do_iteration,
1299 socket_live_messages_changed,
1300 socket_get_socket_fd
1301};
1302
1316 const DBusString *server_guid,
1317 const DBusString *address)
1318{
1319 DBusTransportSocket *socket_transport;
1320 DBusString invalid = _DBUS_STRING_INIT_INVALID;
1321
1322 socket_transport = dbus_new0 (DBusTransportSocket, 1);
1323 if (socket_transport == NULL)
1324 return NULL;
1325
1326 /* So they can be "freed" without error */
1327 socket_transport->encoded_outgoing = invalid;
1328 socket_transport->encoded_incoming = invalid;
1329
1330 if (!_dbus_string_init (&socket_transport->encoded_outgoing))
1331 goto failed;
1332
1333 if (!_dbus_string_init (&socket_transport->encoded_incoming))
1334 goto failed;
1335
1336 socket_transport->write_watch = _dbus_watch_new (_dbus_socket_get_pollable (fd),
1338 FALSE,
1339 NULL, NULL, NULL);
1340 if (socket_transport->write_watch == NULL)
1341 goto failed;
1342
1343 socket_transport->read_watch = _dbus_watch_new (_dbus_socket_get_pollable (fd),
1345 FALSE,
1346 NULL, NULL, NULL);
1347 if (socket_transport->read_watch == NULL)
1348 goto failed;
1349
1350 if (!_dbus_transport_init_base (&socket_transport->base,
1351 &socket_vtable,
1352 server_guid, address))
1353 goto failed;
1354
1355#ifdef HAVE_UNIX_FD_PASSING
1357#endif
1358
1359 socket_transport->fd = fd;
1360 socket_transport->message_bytes_written = 0;
1361 socket_transport->epipe_seen = FALSE;
1362
1363 /* These values should probably be tunable or something. */
1364 socket_transport->max_bytes_read_per_iteration = 2048;
1365 socket_transport->max_bytes_written_per_iteration = 2048;
1366
1367 return (DBusTransport*) socket_transport;
1368
1369failed:
1370 if (socket_transport->read_watch != NULL)
1371 {
1372 _dbus_watch_invalidate (socket_transport->read_watch);
1373 _dbus_watch_unref (socket_transport->read_watch);
1374 }
1375
1376 if (socket_transport->write_watch != NULL)
1377 {
1378 _dbus_watch_invalidate (socket_transport->write_watch);
1379 _dbus_watch_unref (socket_transport->write_watch);
1380 }
1381
1382 _dbus_string_free (&socket_transport->encoded_incoming);
1383 _dbus_string_free (&socket_transport->encoded_outgoing);
1384 dbus_free (socket_transport);
1385 return NULL;
1386}
1387
1401 const char *port,
1402 const char *family,
1403 const char *noncefile,
1404 DBusError *error)
1405{
1406 DBusSocket fd;
1407 DBusTransport *transport;
1408 DBusString address;
1409
1410 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1411
1412 if (!_dbus_string_init (&address))
1413 {
1415 return NULL;
1416 }
1417
1418 if (host == NULL)
1419 host = "localhost";
1420
1421 if (!_dbus_string_append (&address, noncefile ? "nonce-tcp:" : "tcp:"))
1422 goto error;
1423
1424 if (!_dbus_string_append (&address, "host=") ||
1425 !_dbus_string_append (&address, host))
1426 goto error;
1427
1428 if (!_dbus_string_append (&address, ",port=") ||
1429 !_dbus_string_append (&address, port))
1430 goto error;
1431
1432 if (family != NULL &&
1433 (!_dbus_string_append (&address, ",family=") ||
1434 !_dbus_string_append (&address, family)))
1435 goto error;
1436
1437 if (noncefile != NULL &&
1438 (!_dbus_string_append (&address, ",noncefile=") ||
1439 !_dbus_string_append (&address, noncefile)))
1440 goto error;
1441
1442 fd = _dbus_connect_tcp_socket_with_nonce (host, port, family, noncefile, error);
1443 if (!_dbus_socket_is_valid (fd))
1444 {
1445 _DBUS_ASSERT_ERROR_IS_SET (error);
1446 _dbus_string_free (&address);
1447 return NULL;
1448 }
1449
1450 _dbus_verbose ("Successfully connected to tcp socket %s:%s\n",
1451 host, port);
1452
1453 transport = _dbus_transport_new_for_socket (fd, NULL, &address);
1454 _dbus_string_free (&address);
1455 if (transport == NULL)
1456 {
1458 _dbus_close_socket (&fd, NULL);
1459 }
1460
1461 return transport;
1462
1463error:
1464 _dbus_string_free (&address);
1466 return NULL;
1467}
1468
1477DBusTransportOpenResult
1479 DBusTransport **transport_p,
1480 DBusError *error)
1481{
1482 const char *method;
1483 dbus_bool_t isTcp;
1484 dbus_bool_t isNonceTcp;
1485
1486 method = dbus_address_entry_get_method (entry);
1487 _dbus_assert (method != NULL);
1488
1489 isTcp = strcmp (method, "tcp") == 0;
1490 isNonceTcp = strcmp (method, "nonce-tcp") == 0;
1491
1492 if (isTcp || isNonceTcp)
1493 {
1494 const char *host = dbus_address_entry_get_value (entry, "host");
1495 const char *port = dbus_address_entry_get_value (entry, "port");
1496 const char *family = dbus_address_entry_get_value (entry, "family");
1497 const char *noncefile = dbus_address_entry_get_value (entry, "noncefile");
1498
1499 if ((isNonceTcp == TRUE) != (noncefile != NULL)) {
1500 _dbus_set_bad_address (error, method, "noncefile", NULL);
1501 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1502 }
1503
1504 if (port == NULL)
1505 {
1506 _dbus_set_bad_address (error, method, "port", NULL);
1507 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1508 }
1509
1510 *transport_p = _dbus_transport_new_for_tcp_socket (host, port, family, noncefile, error);
1511 if (*transport_p == NULL)
1512 {
1513 _DBUS_ASSERT_ERROR_IS_SET (error);
1514 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
1515 }
1516 else
1517 {
1518 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1519 return DBUS_TRANSPORT_OPEN_OK;
1520 }
1521 }
1522 else
1523 {
1524 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1525 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
1526 }
1527}
1528
1540 dbus_bool_t abstract,
1541 DBusError *error)
1542{
1543 DBusSocket fd = DBUS_SOCKET_INIT;
1544 DBusTransport *transport;
1545 DBusString address;
1546 DBusString unescaped_path;
1547
1548 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1549
1550 if (!_dbus_string_init (&address))
1551 {
1553 return NULL;
1554 }
1555
1556 _dbus_string_init_const (&unescaped_path, path);
1557
1558 if ((abstract &&
1559 !_dbus_string_append (&address, "unix:abstract=")) ||
1560 (!abstract &&
1561 !_dbus_string_append (&address, "unix:path=")) ||
1562 !_dbus_address_append_escaped (&address, &unescaped_path))
1563 {
1565 goto failed_0;
1566 }
1567
1568 fd = _dbus_connect_unix_socket (path, abstract, error);
1569 if (!_dbus_socket_is_valid (fd))
1570 {
1571 _DBUS_ASSERT_ERROR_IS_SET (error);
1572 goto failed_0;
1573 }
1574
1575 _dbus_verbose ("Successfully connected to unix socket %s\n",
1576 path);
1577
1578 transport = _dbus_transport_new_for_socket (fd, NULL, &address);
1579 if (transport == NULL)
1580 {
1582 goto failed_1;
1583 }
1584
1585 _dbus_string_free (&address);
1586
1587 return transport;
1588
1589 failed_1:
1590 _dbus_close_socket (&fd, NULL);
1591 failed_0:
1592 _dbus_string_free (&address);
1593 return NULL;
1594}
1595
1604DBusTransportOpenResult
1606 DBusTransport **transport_p,
1607 DBusError *error)
1608{
1609 const char *method;
1610
1611 method = dbus_address_entry_get_method (entry);
1612 _dbus_assert (method != NULL);
1613
1614 if (strcmp (method, "unix") == 0)
1615 {
1616 const char *path = dbus_address_entry_get_value (entry, "path");
1617 const char *tmpdir = dbus_address_entry_get_value (entry, "tmpdir");
1618 const char *abstract = dbus_address_entry_get_value (entry, "abstract");
1619
1620 if (tmpdir != NULL)
1621 {
1623 "cannot use the \"tmpdir\" option for an address to connect to, only in an address to listen on");
1624 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1625 }
1626
1627 if (path == NULL && abstract == NULL)
1628 {
1629 _dbus_set_bad_address (error, "unix",
1630 "path or abstract",
1631 NULL);
1632 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1633 }
1634
1635 if (path != NULL && abstract != NULL)
1636 {
1638 "can't specify both \"path\" and \"abstract\" options in an address");
1639 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1640 }
1641
1642 if (path)
1643 *transport_p = _dbus_transport_new_for_domain_socket (path, FALSE,
1644 error);
1645 else
1646 *transport_p = _dbus_transport_new_for_domain_socket (abstract, TRUE,
1647 error);
1648 if (*transport_p == NULL)
1649 {
1650 _DBUS_ASSERT_ERROR_IS_SET (error);
1651 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
1652 }
1653 else
1654 {
1655 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1656 return DBUS_TRANSPORT_OPEN_OK;
1657 }
1658 }
1659 else
1660 {
1661 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1662 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
1663 }
1664}
1665
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...
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.
const char * dbus_address_entry_get_method(DBusAddressEntry *entry)
Returns the method string of an address entry.
const char * dbus_address_entry_get_value(DBusAddressEntry *entry, const char *key)
Returns a value from a key of an entry.
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:2548
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:2735
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:2708
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:2830
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:2592
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:2798
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:2906
void _dbus_auth_return_buffer(DBusAuth *auth, DBusString *buffer)
Returns a buffer with new data read into it.
Definition dbus-auth.c:2655
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:2637
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:2767
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:2617
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:64
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
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...
dbus_bool_t _dbus_get_is_errno_epipe(int e)
See if errno is EPIPE.
#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.
#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.
dbus_bool_t _dbus_get_is_errno_enomem(int e)
See if errno is ENOMEM.
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:51
#define TRUE
Expands to "1".
Definition dbus-macros.h:41
#define FALSE
Expands to "0".
Definition dbus-macros.h:44
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition dbus-memory.h:60
void dbus_message_lock(DBusMessage *message)
Locks a message.
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.
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.
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.
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as _DBUS_STRING_IN...
dbus_bool_t _dbus_string_compact(DBusString *str, int max_waste)
Compacts the string to avoid wasted memory.
#define _DBUS_POLLOUT
Writing now will not block.
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.
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...
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).
#define _DBUS_POLLHUP
Hung up.
#define _DBUS_POLLIN
There is data to read.
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:37
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:500
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:90
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:140
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:171
DBUS_EXPORT dbus_bool_t dbus_watch_get_enabled(DBusWatch *watch)
Returns whether a watch is enabled or not.
Definition dbus-watch.c:704
Internals of DBusAddressEntry.
Object representing an exception.
Definition dbus-errors.h:51
const char * message
public error message field
Definition dbus-errors.h:53
Internals of DBusMessage.
short events
Events to poll for.
short revents
Events that occurred.
DBusPollable fd
File descriptor.
Socket interface.
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.
dbus_bool_t epipe_seen
EPIPE has occurred, so stop writing further to avoid busy-looping - but we should continue reading un...
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:43