D-Bus  1.10.6
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 
45 
50 {
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)
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)
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))
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),
166 
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 =
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);
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 
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) &&
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  }
481 
482  out:
483  if (auth_completed)
484  *auth_completed = (orig_auth_state != _dbus_transport_try_to_authenticate (transport));
485 
486  check_read_watch (transport);
487  check_write_watch (transport);
488  _dbus_transport_unref (transport);
489 
490  if (oom)
491  return FALSE;
492  else
493  return TRUE;
494 }
495 
496 /* returns false on oom */
497 static dbus_bool_t
498 do_writing (DBusTransport *transport)
499 {
500  int total;
501  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
502  dbus_bool_t oom;
503 
504  /* No messages without authentication! */
505  if (!_dbus_transport_try_to_authenticate (transport))
506  {
507  _dbus_verbose ("Not authenticated, not writing anything\n");
508  return TRUE;
509  }
510 
511  if (transport->disconnected)
512  {
513  _dbus_verbose ("Not connected, not writing anything\n");
514  return TRUE;
515  }
516 
517 #if 1
518  _dbus_verbose ("do_writing(), have_messages = %d, fd = %" DBUS_SOCKET_FORMAT "\n",
520  _dbus_socket_printable (socket_transport->fd));
521 #endif
522 
523  oom = FALSE;
524  total = 0;
525 
526  while (!transport->disconnected &&
528  {
529  int bytes_written;
530  DBusMessage *message;
531  const DBusString *header;
532  const DBusString *body;
533  int header_len, body_len;
534  int total_bytes_to_write;
535  int saved_errno;
536 
537  if (total > socket_transport->max_bytes_written_per_iteration)
538  {
539  _dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n",
540  total, socket_transport->max_bytes_written_per_iteration);
541  goto out;
542  }
543 
544  message = _dbus_connection_get_message_to_send (transport->connection);
545  _dbus_assert (message != NULL);
546  dbus_message_lock (message);
547 
548 #if 0
549  _dbus_verbose ("writing message %p\n", message);
550 #endif
551 
553  &header, &body);
554 
555  header_len = _dbus_string_get_length (header);
556  body_len = _dbus_string_get_length (body);
557 
558  if (_dbus_auth_needs_encoding (transport->auth))
559  {
560  /* Does fd passing even make sense with encoded data? */
561  _dbus_assert(!DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport));
562 
563  if (_dbus_string_get_length (&socket_transport->encoded_outgoing) == 0)
564  {
565  if (!_dbus_auth_encode_data (transport->auth,
566  header, &socket_transport->encoded_outgoing))
567  {
568  oom = TRUE;
569  goto out;
570  }
571 
572  if (!_dbus_auth_encode_data (transport->auth,
573  body, &socket_transport->encoded_outgoing))
574  {
575  _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
576  oom = TRUE;
577  goto out;
578  }
579  }
580 
581  total_bytes_to_write = _dbus_string_get_length (&socket_transport->encoded_outgoing);
582 
583 #if 0
584  _dbus_verbose ("encoded message is %d bytes\n",
585  total_bytes_to_write);
586 #endif
587 
588  bytes_written =
589  _dbus_write_socket (socket_transport->fd,
590  &socket_transport->encoded_outgoing,
591  socket_transport->message_bytes_written,
592  total_bytes_to_write - socket_transport->message_bytes_written);
593  saved_errno = _dbus_save_socket_errno ();
594  }
595  else
596  {
597  total_bytes_to_write = header_len + body_len;
598 
599 #if 0
600  _dbus_verbose ("message is %d bytes\n",
601  total_bytes_to_write);
602 #endif
603 
604 #ifdef HAVE_UNIX_FD_PASSING
605  if (socket_transport->message_bytes_written <= 0 && DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport))
606  {
607  /* Send the fds along with the first byte of the message */
608  const int *unix_fds;
609  unsigned n;
610 
611  _dbus_message_get_unix_fds(message, &unix_fds, &n);
612 
613  bytes_written =
614  _dbus_write_socket_with_unix_fds_two (socket_transport->fd,
615  header,
616  socket_transport->message_bytes_written,
617  header_len - socket_transport->message_bytes_written,
618  body,
619  0, body_len,
620  unix_fds,
621  n);
622  saved_errno = _dbus_save_socket_errno ();
623 
624  if (bytes_written > 0 && n > 0)
625  _dbus_verbose("Wrote %i unix fds\n", n);
626  }
627  else
628 #endif
629  {
630  if (socket_transport->message_bytes_written < header_len)
631  {
632  bytes_written =
633  _dbus_write_socket_two (socket_transport->fd,
634  header,
635  socket_transport->message_bytes_written,
636  header_len - socket_transport->message_bytes_written,
637  body,
638  0, body_len);
639  }
640  else
641  {
642  bytes_written =
643  _dbus_write_socket (socket_transport->fd,
644  body,
645  (socket_transport->message_bytes_written - header_len),
646  body_len -
647  (socket_transport->message_bytes_written - header_len));
648  }
649 
650  saved_errno = _dbus_save_socket_errno ();
651  }
652  }
653 
654  if (bytes_written < 0)
655  {
656  /* EINTR already handled for us */
657 
658  /* If the other end closed the socket with close() or shutdown(), we
659  * receive EPIPE here but we must not close the socket yet: there
660  * might still be some data to read. See:
661  * http://lists.freedesktop.org/archives/dbus/2008-March/009526.html
662  */
663 
664  if (_dbus_get_is_errno_eagain_or_ewouldblock (saved_errno) || _dbus_get_is_errno_epipe (saved_errno))
665  goto out;
666 
667  /* Since Linux commit 25888e (from 2.6.37-rc4, Nov 2010), sendmsg()
668  * on Unix sockets returns -1 errno=ETOOMANYREFS when the passfd
669  * mechanism (SCM_RIGHTS) is used recursively with a recursion level
670  * of maximum 4. The kernel does not have an API to check whether
671  * the passed fds can be forwarded and it can change asynchronously.
672  * See:
673  * https://bugs.freedesktop.org/show_bug.cgi?id=80163
674  */
675 
676  else if (_dbus_get_is_errno_etoomanyrefs (saved_errno))
677  {
678  /* We only send fds in the first byte of the message.
679  * ETOOMANYREFS cannot happen after.
680  */
681  _dbus_assert (socket_transport->message_bytes_written == 0);
682 
683  _dbus_verbose (" discard message of %d bytes due to ETOOMANYREFS\n",
684  total_bytes_to_write);
685 
686  socket_transport->message_bytes_written = 0;
687  _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
688  _dbus_string_compact (&socket_transport->encoded_outgoing, 2048);
689 
690  /* The message was not actually sent but it needs to be removed
691  * from the outgoing queue
692  */
694  message);
695  }
696  else
697  {
698  _dbus_verbose ("Error writing to remote app: %s\n",
699  _dbus_strerror (saved_errno));
700  do_io_error (transport);
701  goto out;
702  }
703  }
704  else
705  {
706  _dbus_verbose (" wrote %d bytes of %d\n", bytes_written,
707  total_bytes_to_write);
708 
709  total += bytes_written;
710  socket_transport->message_bytes_written += bytes_written;
711 
712  _dbus_assert (socket_transport->message_bytes_written <=
713  total_bytes_to_write);
714 
715  if (socket_transport->message_bytes_written == total_bytes_to_write)
716  {
717  socket_transport->message_bytes_written = 0;
718  _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
719  _dbus_string_compact (&socket_transport->encoded_outgoing, 2048);
720 
722  message);
723  }
724  }
725  }
726 
727  out:
728  if (oom)
729  return FALSE;
730  else
731  return TRUE;
732 }
733 
734 /* returns false on out-of-memory */
735 static dbus_bool_t
736 do_reading (DBusTransport *transport)
737 {
738  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
739  DBusString *buffer;
740  int bytes_read;
741  int total;
742  dbus_bool_t oom;
743  int saved_errno;
744 
745  _dbus_verbose ("fd = %" DBUS_SOCKET_FORMAT "\n",
746  _dbus_socket_printable (socket_transport->fd));
747 
748  /* No messages without authentication! */
749  if (!_dbus_transport_try_to_authenticate (transport))
750  return TRUE;
751 
752  oom = FALSE;
753 
754  total = 0;
755 
756  again:
757 
758  /* See if we've exceeded max messages and need to disable reading */
759  check_read_watch (transport);
760 
761  if (total > socket_transport->max_bytes_read_per_iteration)
762  {
763  _dbus_verbose ("%d bytes exceeds %d bytes read per iteration, returning\n",
764  total, socket_transport->max_bytes_read_per_iteration);
765  goto out;
766  }
767 
768  _dbus_assert (socket_transport->read_watch != NULL ||
769  transport->disconnected);
770 
771  if (transport->disconnected)
772  goto out;
773 
774  if (!dbus_watch_get_enabled (socket_transport->read_watch))
775  return TRUE;
776 
777  if (_dbus_auth_needs_decoding (transport->auth))
778  {
779  /* Does fd passing even make sense with encoded data? */
780  _dbus_assert(!DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport));
781 
782  if (_dbus_string_get_length (&socket_transport->encoded_incoming) > 0)
783  bytes_read = _dbus_string_get_length (&socket_transport->encoded_incoming);
784  else
785  bytes_read = _dbus_read_socket (socket_transport->fd,
786  &socket_transport->encoded_incoming,
787  socket_transport->max_bytes_read_per_iteration);
788 
789  saved_errno = _dbus_save_socket_errno ();
790 
791  _dbus_assert (_dbus_string_get_length (&socket_transport->encoded_incoming) ==
792  bytes_read);
793 
794  if (bytes_read > 0)
795  {
797  &buffer,
798  NULL,
799  NULL);
800 
801  if (!_dbus_auth_decode_data (transport->auth,
802  &socket_transport->encoded_incoming,
803  buffer))
804  {
805  _dbus_verbose ("Out of memory decoding incoming data\n");
807  buffer);
808 
809  oom = TRUE;
810  goto out;
811  }
812 
814  buffer);
815 
816  _dbus_string_set_length (&socket_transport->encoded_incoming, 0);
817  _dbus_string_compact (&socket_transport->encoded_incoming, 2048);
818  }
819  }
820  else
821  {
822  int max_to_read = DBUS_MAXIMUM_MESSAGE_LENGTH;
823  dbus_bool_t may_read_unix_fds = TRUE;
824 
826  &buffer,
827  &max_to_read,
828  &may_read_unix_fds);
829 
830  if (max_to_read > socket_transport->max_bytes_read_per_iteration)
831  max_to_read = socket_transport->max_bytes_read_per_iteration;
832 
833 #ifdef HAVE_UNIX_FD_PASSING
834  if (DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport) && may_read_unix_fds)
835  {
836  int *fds, n_fds;
837 
838  if (!_dbus_message_loader_get_unix_fds(transport->loader, &fds, &n_fds))
839  {
840  _dbus_verbose ("Out of memory reading file descriptors\n");
841  _dbus_message_loader_return_buffer (transport->loader, buffer);
842  oom = TRUE;
843  goto out;
844  }
845 
846  bytes_read = _dbus_read_socket_with_unix_fds(socket_transport->fd,
847  buffer,
848  max_to_read,
849  fds, &n_fds);
850  saved_errno = _dbus_save_socket_errno ();
851 
852  if (bytes_read >= 0 && n_fds > 0)
853  _dbus_verbose("Read %i unix fds\n", n_fds);
854 
855  _dbus_message_loader_return_unix_fds(transport->loader, fds, bytes_read < 0 ? 0 : n_fds);
856  }
857  else
858 #endif
859  {
860  bytes_read = _dbus_read_socket (socket_transport->fd,
861  buffer, max_to_read);
862  saved_errno = _dbus_save_socket_errno ();
863  }
864 
866  buffer);
867  }
868 
869  if (bytes_read < 0)
870  {
871  /* EINTR already handled for us */
872 
873  if (_dbus_get_is_errno_enomem (saved_errno))
874  {
875  _dbus_verbose ("Out of memory in read()/do_reading()\n");
876  oom = TRUE;
877  goto out;
878  }
879  else if (_dbus_get_is_errno_eagain_or_ewouldblock (saved_errno))
880  goto out;
881  else
882  {
883  _dbus_verbose ("Error reading from remote app: %s\n",
884  _dbus_strerror (saved_errno));
885  do_io_error (transport);
886  goto out;
887  }
888  }
889  else if (bytes_read == 0)
890  {
891  _dbus_verbose ("Disconnected from remote app\n");
892  do_io_error (transport);
893  goto out;
894  }
895  else
896  {
897  _dbus_verbose (" read %d bytes\n", bytes_read);
898 
899  total += bytes_read;
900 
901  if (!_dbus_transport_queue_messages (transport))
902  {
903  oom = TRUE;
904  _dbus_verbose (" out of memory when queueing messages we just read in the transport\n");
905  goto out;
906  }
907 
908  /* Try reading more data until we get EAGAIN and return, or
909  * exceed max bytes per iteration. If in blocking mode of
910  * course we'll block instead of returning.
911  */
912  goto again;
913  }
914 
915  out:
916  if (oom)
917  return FALSE;
918  else
919  return TRUE;
920 }
921 
922 static dbus_bool_t
923 unix_error_with_read_to_come (DBusTransport *itransport,
924  DBusWatch *watch,
925  unsigned int flags)
926 {
927  DBusTransportSocket *transport = (DBusTransportSocket *) itransport;
928 
929  if (!(flags & DBUS_WATCH_HANGUP || flags & DBUS_WATCH_ERROR))
930  return FALSE;
931 
932  /* If we have a read watch enabled ...
933  we -might have data incoming ... => handle the HANGUP there */
934  if (watch != transport->read_watch &&
935  _dbus_watch_get_enabled (transport->read_watch))
936  return FALSE;
937 
938  return TRUE;
939 }
940 
941 static dbus_bool_t
942 socket_handle_watch (DBusTransport *transport,
943  DBusWatch *watch,
944  unsigned int flags)
945 {
946  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
947 
948  _dbus_assert (watch == socket_transport->read_watch ||
949  watch == socket_transport->write_watch);
950  _dbus_assert (watch != NULL);
951 
952  /* If we hit an error here on a write watch, don't disconnect the transport yet because data can
953  * still be in the buffer and do_reading may need several iteration to read
954  * it all (because of its max_bytes_read_per_iteration limit).
955  */
956  if (!(flags & DBUS_WATCH_READABLE) && unix_error_with_read_to_come (transport, watch, flags))
957  {
958  _dbus_verbose ("Hang up or error on watch\n");
959  _dbus_transport_disconnect (transport);
960  return TRUE;
961  }
962 
963  if (watch == socket_transport->read_watch &&
964  (flags & DBUS_WATCH_READABLE))
965  {
966  dbus_bool_t auth_finished;
967 #if 1
968  _dbus_verbose ("handling read watch %p flags = %x\n",
969  watch, flags);
970 #endif
971  if (!do_authentication (transport, TRUE, FALSE, &auth_finished))
972  return FALSE;
973 
974  /* We don't want to do a read immediately following
975  * a successful authentication. This is so we
976  * have a chance to propagate the authentication
977  * state further up. Specifically, we need to
978  * process any pending data from the auth object.
979  */
980  if (!auth_finished)
981  {
982  if (!do_reading (transport))
983  {
984  _dbus_verbose ("no memory to read\n");
985  return FALSE;
986  }
987  }
988  else
989  {
990  _dbus_verbose ("Not reading anything since we just completed the authentication\n");
991  }
992  }
993  else if (watch == socket_transport->write_watch &&
994  (flags & DBUS_WATCH_WRITABLE))
995  {
996 #if 1
997  _dbus_verbose ("handling write watch, have_outgoing_messages = %d\n",
999 #endif
1000  if (!do_authentication (transport, FALSE, TRUE, NULL))
1001  return FALSE;
1002 
1003  if (!do_writing (transport))
1004  {
1005  _dbus_verbose ("no memory to write\n");
1006  return FALSE;
1007  }
1008 
1009  /* See if we still need the write watch */
1010  check_write_watch (transport);
1011  }
1012 #ifdef DBUS_ENABLE_VERBOSE_MODE
1013  else
1014  {
1015  if (watch == socket_transport->read_watch)
1016  _dbus_verbose ("asked to handle read watch with non-read condition 0x%x\n",
1017  flags);
1018  else if (watch == socket_transport->write_watch)
1019  _dbus_verbose ("asked to handle write watch with non-write condition 0x%x\n",
1020  flags);
1021  else
1022  _dbus_verbose ("asked to handle watch %p on fd %" DBUS_SOCKET_FORMAT " that we don't recognize\n",
1023  watch, dbus_watch_get_socket (watch));
1024  }
1025 #endif /* DBUS_ENABLE_VERBOSE_MODE */
1026 
1027  return TRUE;
1028 }
1029 
1030 static void
1031 socket_disconnect (DBusTransport *transport)
1032 {
1033  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1034 
1035  _dbus_verbose ("\n");
1036 
1037  free_watches (transport);
1038 
1039  _dbus_close_socket (socket_transport->fd, NULL);
1040  _dbus_socket_invalidate (&socket_transport->fd);
1041 }
1042 
1043 static dbus_bool_t
1044 socket_connection_set (DBusTransport *transport)
1045 {
1046  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1047 
1048  _dbus_watch_set_handler (socket_transport->write_watch,
1050  transport->connection, NULL);
1051 
1052  _dbus_watch_set_handler (socket_transport->read_watch,
1054  transport->connection, NULL);
1055 
1057  socket_transport->write_watch))
1058  return FALSE;
1059 
1061  socket_transport->read_watch))
1062  {
1064  socket_transport->write_watch);
1065  return FALSE;
1066  }
1067 
1068  check_read_watch (transport);
1069  check_write_watch (transport);
1070 
1071  return TRUE;
1072 }
1073 
1081 static void
1082 socket_do_iteration (DBusTransport *transport,
1083  unsigned int flags,
1084  int timeout_milliseconds)
1085 {
1086  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1087  DBusPollFD poll_fd;
1088  int poll_res;
1089  int poll_timeout;
1090 
1091  _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p fd = %" DBUS_SOCKET_FORMAT "\n",
1092  flags & DBUS_ITERATION_DO_READING ? "read" : "",
1093  flags & DBUS_ITERATION_DO_WRITING ? "write" : "",
1094  timeout_milliseconds,
1095  socket_transport->read_watch,
1096  socket_transport->write_watch,
1097  _dbus_socket_printable (socket_transport->fd));
1098 
1099  /* the passed in DO_READING/DO_WRITING flags indicate whether to
1100  * read/write messages, but regardless of those we may need to block
1101  * for reading/writing to do auth. But if we do reading for auth,
1102  * we don't want to read any messages yet if not given DO_READING.
1103  */
1104 
1105  poll_fd.fd = _dbus_socket_get_pollable (socket_transport->fd);
1106  poll_fd.events = 0;
1107 
1108  if (_dbus_transport_try_to_authenticate (transport))
1109  {
1110  /* This is kind of a hack; if we have stuff to write, then try
1111  * to avoid the poll. This is probably about a 5% speedup on an
1112  * echo client/server.
1113  *
1114  * If both reading and writing were requested, we want to avoid this
1115  * since it could have funky effects:
1116  * - both ends spinning waiting for the other one to read
1117  * data so they can finish writing
1118  * - prioritizing all writing ahead of reading
1119  */
1120  if ((flags & DBUS_ITERATION_DO_WRITING) &&
1121  !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) &&
1122  !transport->disconnected &&
1124  {
1125  do_writing (transport);
1126 
1127  if (transport->disconnected ||
1129  goto out;
1130  }
1131 
1132  /* If we get here, we decided to do the poll() after all */
1133  _dbus_assert (socket_transport->read_watch);
1134  if (flags & DBUS_ITERATION_DO_READING)
1135  poll_fd.events |= _DBUS_POLLIN;
1136 
1137  _dbus_assert (socket_transport->write_watch);
1138  if (flags & DBUS_ITERATION_DO_WRITING)
1139  poll_fd.events |= _DBUS_POLLOUT;
1140  }
1141  else
1142  {
1143  DBusAuthState auth_state;
1144 
1145  auth_state = _dbus_auth_do_work (transport->auth);
1146 
1147  if (transport->receive_credentials_pending ||
1148  auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
1149  poll_fd.events |= _DBUS_POLLIN;
1150 
1151  if (transport->send_credentials_pending ||
1152  auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
1153  poll_fd.events |= _DBUS_POLLOUT;
1154  }
1155 
1156  if (poll_fd.events)
1157  {
1158  int saved_errno;
1159 
1160  if (flags & DBUS_ITERATION_BLOCK)
1161  poll_timeout = timeout_milliseconds;
1162  else
1163  poll_timeout = 0;
1164 
1165  /* For blocking selects we drop the connection lock here
1166  * to avoid blocking out connection access during a potentially
1167  * indefinite blocking call. The io path is still protected
1168  * by the io_path_cond condvar, so we won't reenter this.
1169  */
1170  if (flags & DBUS_ITERATION_BLOCK)
1171  {
1172  _dbus_verbose ("unlock pre poll\n");
1173  _dbus_connection_unlock (transport->connection);
1174  }
1175 
1176  again:
1177  poll_res = _dbus_poll (&poll_fd, 1, poll_timeout);
1178  saved_errno = _dbus_save_socket_errno ();
1179 
1180  if (poll_res < 0 && _dbus_get_is_errno_eintr (saved_errno))
1181  goto again;
1182 
1183  if (flags & DBUS_ITERATION_BLOCK)
1184  {
1185  _dbus_verbose ("lock post poll\n");
1186  _dbus_connection_lock (transport->connection);
1187  }
1188 
1189  if (poll_res >= 0)
1190  {
1191  if (poll_res == 0)
1192  poll_fd.revents = 0; /* some concern that posix does not guarantee this;
1193  * valgrind flags it as an error. though it probably
1194  * is guaranteed on linux at least.
1195  */
1196 
1197  if (poll_fd.revents & _DBUS_POLLERR)
1198  do_io_error (transport);
1199  else
1200  {
1201  dbus_bool_t need_read = (poll_fd.revents & _DBUS_POLLIN) > 0;
1202  dbus_bool_t need_write = (poll_fd.revents & _DBUS_POLLOUT) > 0;
1203  dbus_bool_t authentication_completed;
1204 
1205  _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
1206  need_read, need_write);
1207  do_authentication (transport, need_read, need_write,
1208  &authentication_completed);
1209 
1210  /* See comment in socket_handle_watch. */
1211  if (authentication_completed)
1212  goto out;
1213 
1214  if (need_read && (flags & DBUS_ITERATION_DO_READING))
1215  do_reading (transport);
1216  if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
1217  do_writing (transport);
1218  }
1219  }
1220  else
1221  {
1222  _dbus_verbose ("Error from _dbus_poll(): %s\n",
1223  _dbus_strerror (saved_errno));
1224  }
1225  }
1226 
1227 
1228  out:
1229  /* We need to install the write watch only if we did not
1230  * successfully write everything. Note we need to be careful that we
1231  * don't call check_write_watch *before* do_writing, since it's
1232  * inefficient to add the write watch, and we can avoid it most of
1233  * the time since we can write immediately.
1234  *
1235  * However, we MUST always call check_write_watch(); DBusConnection code
1236  * relies on the fact that running an iteration will notice that
1237  * messages are pending.
1238  */
1239  check_write_watch (transport);
1240 
1241  _dbus_verbose (" ... leaving do_iteration()\n");
1242 }
1243 
1244 static void
1245 socket_live_messages_changed (DBusTransport *transport)
1246 {
1247  /* See if we should look for incoming messages again */
1248  check_read_watch (transport);
1249 }
1250 
1251 
1252 static dbus_bool_t
1253 socket_get_socket_fd (DBusTransport *transport,
1254  DBusSocket *fd_p)
1255 {
1256  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1257 
1258  *fd_p = socket_transport->fd;
1259 
1260  return TRUE;
1261 }
1262 
1263 static const DBusTransportVTable socket_vtable = {
1264  socket_finalize,
1265  socket_handle_watch,
1266  socket_disconnect,
1267  socket_connection_set,
1268  socket_do_iteration,
1269  socket_live_messages_changed,
1270  socket_get_socket_fd
1271 };
1272 
1286  const DBusString *server_guid,
1287  const DBusString *address)
1288 {
1289  DBusTransportSocket *socket_transport;
1290 
1291  socket_transport = dbus_new0 (DBusTransportSocket, 1);
1292  if (socket_transport == NULL)
1293  return NULL;
1294 
1295  if (!_dbus_string_init (&socket_transport->encoded_outgoing))
1296  goto failed_0;
1297 
1298  if (!_dbus_string_init (&socket_transport->encoded_incoming))
1299  goto failed_1;
1300 
1301  socket_transport->write_watch = _dbus_watch_new (_dbus_socket_get_pollable (fd),
1303  FALSE,
1304  NULL, NULL, NULL);
1305  if (socket_transport->write_watch == NULL)
1306  goto failed_2;
1307 
1308  socket_transport->read_watch = _dbus_watch_new (_dbus_socket_get_pollable (fd),
1310  FALSE,
1311  NULL, NULL, NULL);
1312  if (socket_transport->read_watch == NULL)
1313  goto failed_3;
1314 
1315  if (!_dbus_transport_init_base (&socket_transport->base,
1316  &socket_vtable,
1317  server_guid, address))
1318  goto failed_4;
1319 
1320 #ifdef HAVE_UNIX_FD_PASSING
1322 #endif
1323 
1324  socket_transport->fd = fd;
1325  socket_transport->message_bytes_written = 0;
1326 
1327  /* These values should probably be tunable or something. */
1328  socket_transport->max_bytes_read_per_iteration = 2048;
1329  socket_transport->max_bytes_written_per_iteration = 2048;
1330 
1331  return (DBusTransport*) socket_transport;
1332 
1333  failed_4:
1334  _dbus_watch_invalidate (socket_transport->read_watch);
1335  _dbus_watch_unref (socket_transport->read_watch);
1336  failed_3:
1337  _dbus_watch_invalidate (socket_transport->write_watch);
1338  _dbus_watch_unref (socket_transport->write_watch);
1339  failed_2:
1340  _dbus_string_free (&socket_transport->encoded_incoming);
1341  failed_1:
1342  _dbus_string_free (&socket_transport->encoded_outgoing);
1343  failed_0:
1344  dbus_free (socket_transport);
1345  return NULL;
1346 }
1347 
1361  const char *port,
1362  const char *family,
1363  const char *noncefile,
1364  DBusError *error)
1365 {
1366  DBusSocket fd;
1367  DBusTransport *transport;
1368  DBusString address;
1369 
1370  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1371 
1372  if (!_dbus_string_init (&address))
1373  {
1375  return NULL;
1376  }
1377 
1378  if (host == NULL)
1379  host = "localhost";
1380 
1381  if (!_dbus_string_append (&address, noncefile ? "nonce-tcp:" : "tcp:"))
1382  goto error;
1383 
1384  if (!_dbus_string_append (&address, "host=") ||
1385  !_dbus_string_append (&address, host))
1386  goto error;
1387 
1388  if (!_dbus_string_append (&address, ",port=") ||
1389  !_dbus_string_append (&address, port))
1390  goto error;
1391 
1392  if (family != NULL &&
1393  (!_dbus_string_append (&address, ",family=") ||
1394  !_dbus_string_append (&address, family)))
1395  goto error;
1396 
1397  if (noncefile != NULL &&
1398  (!_dbus_string_append (&address, ",noncefile=") ||
1399  !_dbus_string_append (&address, noncefile)))
1400  goto error;
1401 
1402  fd = _dbus_connect_tcp_socket_with_nonce (host, port, family, noncefile, error);
1403  if (!_dbus_socket_is_valid (fd))
1404  {
1405  _DBUS_ASSERT_ERROR_IS_SET (error);
1406  _dbus_string_free (&address);
1407  return NULL;
1408  }
1409 
1410  _dbus_verbose ("Successfully connected to tcp socket %s:%s\n",
1411  host, port);
1412 
1413  transport = _dbus_transport_new_for_socket (fd, NULL, &address);
1414  _dbus_string_free (&address);
1415  if (transport == NULL)
1416  {
1418  _dbus_close_socket (fd, NULL);
1419  _dbus_socket_invalidate (&fd);
1420  }
1421 
1422  return transport;
1423 
1424 error:
1425  _dbus_string_free (&address);
1427  return NULL;
1428 }
1429 
1438 DBusTransportOpenResult
1440  DBusTransport **transport_p,
1441  DBusError *error)
1442 {
1443  const char *method;
1444  dbus_bool_t isTcp;
1445  dbus_bool_t isNonceTcp;
1446 
1447  method = dbus_address_entry_get_method (entry);
1448  _dbus_assert (method != NULL);
1449 
1450  isTcp = strcmp (method, "tcp") == 0;
1451  isNonceTcp = strcmp (method, "nonce-tcp") == 0;
1452 
1453  if (isTcp || isNonceTcp)
1454  {
1455  const char *host = dbus_address_entry_get_value (entry, "host");
1456  const char *port = dbus_address_entry_get_value (entry, "port");
1457  const char *family = dbus_address_entry_get_value (entry, "family");
1458  const char *noncefile = dbus_address_entry_get_value (entry, "noncefile");
1459 
1460  if ((isNonceTcp == TRUE) != (noncefile != NULL)) {
1461  _dbus_set_bad_address (error, method, "noncefile", NULL);
1462  return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1463  }
1464 
1465  if (port == NULL)
1466  {
1467  _dbus_set_bad_address (error, method, "port", NULL);
1468  return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1469  }
1470 
1471  *transport_p = _dbus_transport_new_for_tcp_socket (host, port, family, noncefile, error);
1472  if (*transport_p == NULL)
1473  {
1474  _DBUS_ASSERT_ERROR_IS_SET (error);
1475  return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
1476  }
1477  else
1478  {
1479  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1480  return DBUS_TRANSPORT_OPEN_OK;
1481  }
1482  }
1483  else
1484  {
1485  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1486  return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
1487  }
1488 }
1489 
Implementation details of DBusTransportSocket.
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:935
void dbus_message_lock(DBusMessage *message)
Locks a message.
Definition: dbus-message.c:384
DBusTransport base
Parent instance.
void _dbus_connection_toggle_watch_unlocked(DBusConnection *connection, DBusWatch *watch, dbus_bool_t enabled)
Toggles a watch and notifies app via connection&#39;s DBusWatchToggledFunction if available.
long max_live_messages_unix_fds
Max total unix fds of received messages.
const char * message
public error message field
Definition: dbus-errors.h:51
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
Implementation of DBusWatch.
Definition: dbus-watch.c:40
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.
#define NULL
A null pointer, defined appropriately for C or C++.
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...
DBusAuth * auth
Authentication conversation.
unsigned int disconnected
TRUE if we are disconnected.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:701
DBUS_PRIVATE_EXPORT void _dbus_message_loader_return_unix_fds(DBusMessageLoader *loader, int *fds, unsigned n_fds)
Returns a buffer obtained from _dbus_message_loader_get_unix_fds().
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:2713
long max_live_messages_size
Max total size of received messages.
DBUS_PRIVATE_EXPORT void _dbus_connection_lock(DBusConnection *connection)
Acquires the connection lock.
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:2681
DBUS_EXPORT dbus_bool_t dbus_watch_get_enabled(DBusWatch *watch)
Returns whether a watch is enabled or not.
Definition: dbus-watch.c:703
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:62
const char * dbus_address_entry_get_method(DBusAddressEntry *entry)
Returns the method string of an address entry.
Definition: dbus-address.c:227
DBusTransportOpenResult _dbus_transport_open_socket(DBusAddressEntry *entry, DBusTransport **transport_p, DBusError *error)
Opens a TCP socket transport.
dbus_bool_t _dbus_transport_queue_messages(DBusTransport *transport)
Processes data we&#39;ve read while handling a watch, potentially converting some of it to messages and q...
void _dbus_auth_return_buffer(DBusAuth *auth, DBusString *buffer)
Returns a buffer with new data read into it.
Definition: dbus-auth.c:2601
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:2494
void dbus_error_free(DBusError *error)
Frees an error that&#39;s been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition: dbus-errors.c:211
int max_bytes_written_per_iteration
To avoid blocking too long.
DBusConnection * connection
Connection owning this transport.
#define _DBUS_POLLIN
There is data to read.
Definition: dbus-sysdeps.h:385
unsigned int send_credentials_pending
TRUE if we need to send credentials
int _dbus_read_socket(DBusSocket fd, DBusString *buffer, int count)
Like _dbus_read(), but only works on sockets so is available on Windows.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
DBusMessage * _dbus_connection_get_message_to_send(DBusConnection *connection)
Gets the next outgoing message.
short events
Events to poll for.
Definition: dbus-sysdeps.h:380
DBusString encoded_incoming
Encoded version of current incoming data.
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.
dbus_bool_t _dbus_get_is_errno_etoomanyrefs(int e)
See if errno is ETOOMANYREFS.
Definition: dbus-sysdeps.c:735
dbus_bool_t _dbus_transport_get_is_connected(DBusTransport *transport)
Returns TRUE if the transport has not been disconnected.
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.
int _dbus_read_socket_with_unix_fds(DBusSocket fd, DBusString *buffer, int count, int *fds, int *n_fds)
Like _dbus_read_socket() but also tries to read unix fds from the socket.
Socket interface.
Definition: dbus-sysdeps.h:148
void _dbus_auth_get_buffer(DBusAuth *auth, DBusString **buffer)
Get a buffer to be used for reading bytes from the peer we&#39;re conversing with.
Definition: dbus-auth.c:2583
Internals of DBusMessage.
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:2744
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:244
DBusSocket fd
File descriptor.
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.
dbus_bool_t _dbus_get_is_errno_epipe(int e)
See if errno is EPIPE.
Definition: dbus-sysdeps.c:725
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:59
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:2563
void _dbus_connection_remove_watch_unlocked(DBusConnection *connection, DBusWatch *watch)
Removes a watch using the connection&#39;s DBusRemoveWatchFunction if available.
dbus_bool_t _dbus_string_compact(DBusString *str, int max_waste)
Compacts the string to avoid wasted memory.
Definition: dbus-string.c:389
unsigned int receive_credentials_pending
TRUE if we need to receive credentials
As in POLLOUT.
DBusCounter * live_messages
Counter for size/unix fds of all live messages.
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) ...
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
dbus_bool_t _dbus_connection_has_messages_to_send_unlocked(DBusConnection *connection)
Checks whether there are messages in the outgoing message queue.
DBUS_EXPORT int dbus_watch_get_socket(DBusWatch *watch)
Returns a socket to be watched, on UNIX this will return -1 if our transport is not socket-based so d...
Definition: dbus-watch.c:594
#define _DBUS_POLLOUT
Writing now will not block.
Definition: dbus-sysdeps.h:389
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.
Internals of DBusAddressEntry.
Definition: dbus-address.c:43
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...
DBusWatch * write_watch
Watch for writability.
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
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:65
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_get_is_errno_eintr(int e)
See if errno is EINTR.
Definition: dbus-sysdeps.c:715
Object representing an exception.
Definition: dbus-errors.h:48
dbus_bool_t _dbus_connection_add_watch_unlocked(DBusConnection *connection, DBusWatch *watch)
Adds a watch using the connection&#39;s DBusAddWatchFunction if available.
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
The virtual table that must be implemented to create a new kind of transport.
int message_bytes_written
Number of bytes of current outgoing message that have been written.
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...
As in POLLERR (can&#39;t watch for this, but can be present in current state passed to dbus_watch_handle(...
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:259
#define TRUE
Expands to "1".
DBusPollable fd
File descriptor.
Definition: dbus-sysdeps.h:379
DBusMessageLoader * loader
Message-loading buffer.
As in POLLHUP (can&#39;t watch for it, but can be present in current state passed to dbus_watch_handle())...
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:208
Object representing a transport such as a socket.
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:2776
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:2852
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_message_loader_get_unix_fds(DBusMessageLoader *loader, int **fds, unsigned *max_n_fds)
Gets the buffer to use for reading unix fds from the network.
DBusTransport * _dbus_transport_ref(DBusTransport *transport)
Increments 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_auth_get_bytes_to_send(DBusAuth *auth, const DBusString **str)
Gets bytes that need to be sent to the peer we&#39;re conversing with.
Definition: dbus-auth.c:2538
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
DBusWatch * read_watch
Watch for readability.
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
#define FALSE
Expands to "0".
DBusCredentials * credentials
Credentials of other end read from the socket.
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition: dbus-string.c:802
int max_bytes_read_per_iteration
To avoid blocking too long.
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.
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_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:2654
As in POLLIN.
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:227
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:499
void _dbus_transport_disconnect(DBusTransport *transport)
Closes our end of the connection to a remote application.
dbus_bool_t _dbus_socket_can_pass_unix_fd(DBusSocket fd)
Checks whether file descriptors may be passed via the socket.
void _dbus_transport_finalize_base(DBusTransport *transport)
Finalizes base class members of DBusTransport.
int _dbus_poll(DBusPollFD *fds, int n_fds, int timeout_milliseconds)
Wrapper for poll().
DBUS_PRIVATE_EXPORT void _dbus_connection_unlock(DBusConnection *connection)
Releases the connection lock.
short revents
Events that occurred.
Definition: dbus-sysdeps.h:381
void _dbus_transport_unref(DBusTransport *transport)
Decrements the reference count for the transport.
long _dbus_counter_get_unix_fd_value(DBusCounter *counter)
Gets the current value of the unix fd counter.
dbus_bool_t _dbus_get_is_errno_enomem(int e)
See if errno is ENOMEM.
Definition: dbus-sysdeps.c:705
#define DBUS_MAXIMUM_MESSAGE_LENGTH
The maximum total message size including header and body; similar rationale to max array size...
#define _DBUS_POLLERR
Error condition.
Definition: dbus-sysdeps.h:391
DBusString encoded_outgoing
Encoded version of current outgoing message.
long _dbus_counter_get_size_value(DBusCounter *counter)
Gets the current value of the size counter.