D-Bus: dbus-transport-socket.c Source File

D-Bus 1.13.18

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