D-Bus  1.8.2
dbus-transport-socket.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-transport-socket.c Socket subclasses of DBusTransport
3  *
4  * Copyright (C) 2002, 2003, 2004, 2006 Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23 
24 #include <config.h>
25 #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 {
52  int fd;
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 = %d outgoing messages exist %d\n",
163  needed, transport->connection, socket_transport->write_watch,
164  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 = %d\n",socket_transport->fd);
181 
182  if (transport->connection == NULL)
183  return;
184 
185  if (transport->disconnected)
186  {
187  _dbus_assert (socket_transport->read_watch == NULL);
188  return;
189  }
190 
191  _dbus_transport_ref (transport);
192 
193  if (_dbus_transport_try_to_authenticate (transport))
194  need_read_watch =
197  else
198  {
199  if (transport->receive_credentials_pending)
200  need_read_watch = TRUE;
201  else
202  {
203  /* The reason to disable need_read_watch when not WAITING_FOR_INPUT
204  * is to avoid spinning on the file descriptor when we're waiting
205  * to write or for some other part of the auth process
206  */
207  DBusAuthState auth_state;
208 
209  auth_state = _dbus_auth_do_work (transport->auth);
210 
211  /* If we need memory we install the read watch just in case,
212  * if there's no need for it, it will get de-installed
213  * next time we try reading. If we're authenticated we
214  * install it since we normally have it installed while
215  * authenticated.
216  */
217  if (auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT ||
218  auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY ||
219  auth_state == DBUS_AUTH_STATE_AUTHENTICATED)
220  need_read_watch = TRUE;
221  else
222  need_read_watch = FALSE;
223  }
224  }
225 
226  _dbus_verbose (" setting read watch enabled = %d\n", need_read_watch);
228  socket_transport->read_watch,
229  need_read_watch);
230 
231  _dbus_transport_unref (transport);
232 }
233 
234 static void
235 do_io_error (DBusTransport *transport)
236 {
237  _dbus_transport_ref (transport);
238  _dbus_transport_disconnect (transport);
239  _dbus_transport_unref (transport);
240 }
241 
242 /* return value is whether we successfully read any new data. */
243 static dbus_bool_t
244 read_data_into_auth (DBusTransport *transport,
245  dbus_bool_t *oom)
246 {
247  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
248  DBusString *buffer;
249  int bytes_read;
250 
251  *oom = FALSE;
252 
253  _dbus_auth_get_buffer (transport->auth, &buffer);
254 
255  bytes_read = _dbus_read_socket (socket_transport->fd,
256  buffer, socket_transport->max_bytes_read_per_iteration);
257 
258  _dbus_auth_return_buffer (transport->auth, buffer);
259 
260  if (bytes_read > 0)
261  {
262  _dbus_verbose (" read %d bytes in auth phase\n", bytes_read);
263 
264  return TRUE;
265  }
266  else if (bytes_read < 0)
267  {
268  /* EINTR already handled for us */
269 
271  {
272  *oom = TRUE;
273  }
275  ; /* do nothing, just return FALSE below */
276  else
277  {
278  _dbus_verbose ("Error reading from remote app: %s\n",
280  do_io_error (transport);
281  }
282 
283  return FALSE;
284  }
285  else
286  {
287  _dbus_assert (bytes_read == 0);
288 
289  _dbus_verbose ("Disconnected from remote app\n");
290  do_io_error (transport);
291 
292  return FALSE;
293  }
294 }
295 
296 /* Return value is whether we successfully wrote any bytes */
297 static dbus_bool_t
298 write_data_from_auth (DBusTransport *transport)
299 {
300  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
301  int bytes_written;
302  const DBusString *buffer;
303 
304  if (!_dbus_auth_get_bytes_to_send (transport->auth,
305  &buffer))
306  return FALSE;
307 
308  bytes_written = _dbus_write_socket (socket_transport->fd,
309  buffer,
310  0, _dbus_string_get_length (buffer));
311 
312  if (bytes_written > 0)
313  {
314  _dbus_auth_bytes_sent (transport->auth, bytes_written);
315  return TRUE;
316  }
317  else if (bytes_written < 0)
318  {
319  /* EINTR already handled for us */
320 
322  ;
323  else
324  {
325  _dbus_verbose ("Error writing to remote app: %s\n",
327  do_io_error (transport);
328  }
329  }
330 
331  return FALSE;
332 }
333 
334 /* FALSE on OOM */
335 static dbus_bool_t
336 exchange_credentials (DBusTransport *transport,
337  dbus_bool_t do_reading,
338  dbus_bool_t do_writing)
339 {
340  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
341  DBusError error = DBUS_ERROR_INIT;
342 
343  _dbus_verbose ("exchange_credentials: do_reading = %d, do_writing = %d\n",
344  do_reading, do_writing);
345 
346  if (do_writing && transport->send_credentials_pending)
347  {
348  if (_dbus_send_credentials_socket (socket_transport->fd,
349  &error))
350  {
351  transport->send_credentials_pending = FALSE;
352  }
353  else
354  {
355  _dbus_verbose ("Failed to write credentials: %s\n", error.message);
356  dbus_error_free (&error);
357  do_io_error (transport);
358  }
359  }
360 
361  if (do_reading && transport->receive_credentials_pending)
362  {
363  /* FIXME this can fail due to IO error _or_ OOM, broken
364  * (somewhat tricky to fix since the OOM error can be set after
365  * we already read the credentials byte, so basically we need to
366  * separate reading the byte and storing it in the
367  * transport->credentials). Does not really matter for now
368  * because storing in credentials never actually fails on unix.
369  */
370  if (_dbus_read_credentials_socket (socket_transport->fd,
371  transport->credentials,
372  &error))
373  {
374  transport->receive_credentials_pending = FALSE;
375  }
376  else
377  {
378  _dbus_verbose ("Failed to read credentials %s\n", error.message);
379  dbus_error_free (&error);
380  do_io_error (transport);
381  }
382  }
383 
384  if (!(transport->send_credentials_pending ||
385  transport->receive_credentials_pending))
386  {
387  if (!_dbus_auth_set_credentials (transport->auth,
388  transport->credentials))
389  return FALSE;
390  }
391 
392  return TRUE;
393 }
394 
395 static dbus_bool_t
396 do_authentication (DBusTransport *transport,
397  dbus_bool_t do_reading,
398  dbus_bool_t do_writing,
399  dbus_bool_t *auth_completed)
400 {
401  dbus_bool_t oom;
402  dbus_bool_t orig_auth_state;
403 
404  oom = FALSE;
405 
406  orig_auth_state = _dbus_transport_try_to_authenticate (transport);
407 
408  /* This is essential to avoid the check_write_watch() at the end,
409  * we don't want to add a write watch in do_iteration before
410  * we try writing and get EAGAIN
411  */
412  if (orig_auth_state)
413  {
414  if (auth_completed)
415  *auth_completed = FALSE;
416  return TRUE;
417  }
418 
419  _dbus_transport_ref (transport);
420 
421  while (!_dbus_transport_try_to_authenticate (transport) &&
423  {
424  if (!exchange_credentials (transport, do_reading, do_writing))
425  {
426  /* OOM */
427  oom = TRUE;
428  goto out;
429  }
430 
431  if (transport->send_credentials_pending ||
432  transport->receive_credentials_pending)
433  {
434  _dbus_verbose ("send_credentials_pending = %d receive_credentials_pending = %d\n",
435  transport->send_credentials_pending,
436  transport->receive_credentials_pending);
437  goto out;
438  }
439 
440 #define TRANSPORT_SIDE(t) ((t)->is_server ? "server" : "client")
441  switch (_dbus_auth_do_work (transport->auth))
442  {
443  case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
444  _dbus_verbose (" %s auth state: waiting for input\n",
445  TRANSPORT_SIDE (transport));
446  if (!do_reading || !read_data_into_auth (transport, &oom))
447  goto out;
448  break;
449 
450  case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
451  _dbus_verbose (" %s auth state: waiting for memory\n",
452  TRANSPORT_SIDE (transport));
453  oom = TRUE;
454  goto out;
455  break;
456 
457  case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
458  _dbus_verbose (" %s auth state: bytes to send\n",
459  TRANSPORT_SIDE (transport));
460  if (!do_writing || !write_data_from_auth (transport))
461  goto out;
462  break;
463 
464  case DBUS_AUTH_STATE_NEED_DISCONNECT:
465  _dbus_verbose (" %s auth state: need to disconnect\n",
466  TRANSPORT_SIDE (transport));
467  do_io_error (transport);
468  break;
469 
470  case DBUS_AUTH_STATE_AUTHENTICATED:
471  _dbus_verbose (" %s auth state: authenticated\n",
472  TRANSPORT_SIDE (transport));
473  break;
474  }
475  }
476 
477  out:
478  if (auth_completed)
479  *auth_completed = (orig_auth_state != _dbus_transport_try_to_authenticate (transport));
480 
481  check_read_watch (transport);
482  check_write_watch (transport);
483  _dbus_transport_unref (transport);
484 
485  if (oom)
486  return FALSE;
487  else
488  return TRUE;
489 }
490 
491 /* returns false on oom */
492 static dbus_bool_t
493 do_writing (DBusTransport *transport)
494 {
495  int total;
496  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
497  dbus_bool_t oom;
498 
499  /* No messages without authentication! */
500  if (!_dbus_transport_try_to_authenticate (transport))
501  {
502  _dbus_verbose ("Not authenticated, not writing anything\n");
503  return TRUE;
504  }
505 
506  if (transport->disconnected)
507  {
508  _dbus_verbose ("Not connected, not writing anything\n");
509  return TRUE;
510  }
511 
512 #if 1
513  _dbus_verbose ("do_writing(), have_messages = %d, fd = %d\n",
515  socket_transport->fd);
516 #endif
517 
518  oom = FALSE;
519  total = 0;
520 
521  while (!transport->disconnected &&
523  {
524  int bytes_written;
525  DBusMessage *message;
526  const DBusString *header;
527  const DBusString *body;
528  int header_len, body_len;
529  int total_bytes_to_write;
530 
531  if (total > socket_transport->max_bytes_written_per_iteration)
532  {
533  _dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n",
534  total, socket_transport->max_bytes_written_per_iteration);
535  goto out;
536  }
537 
538  message = _dbus_connection_get_message_to_send (transport->connection);
539  _dbus_assert (message != NULL);
540  dbus_message_lock (message);
541 
542 #if 0
543  _dbus_verbose ("writing message %p\n", message);
544 #endif
545 
547  &header, &body);
548 
549  header_len = _dbus_string_get_length (header);
550  body_len = _dbus_string_get_length (body);
551 
552  if (_dbus_auth_needs_encoding (transport->auth))
553  {
554  /* Does fd passing even make sense with encoded data? */
555  _dbus_assert(!DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport));
556 
557  if (_dbus_string_get_length (&socket_transport->encoded_outgoing) == 0)
558  {
559  if (!_dbus_auth_encode_data (transport->auth,
560  header, &socket_transport->encoded_outgoing))
561  {
562  oom = TRUE;
563  goto out;
564  }
565 
566  if (!_dbus_auth_encode_data (transport->auth,
567  body, &socket_transport->encoded_outgoing))
568  {
569  _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
570  oom = TRUE;
571  goto out;
572  }
573  }
574 
575  total_bytes_to_write = _dbus_string_get_length (&socket_transport->encoded_outgoing);
576 
577 #if 0
578  _dbus_verbose ("encoded message is %d bytes\n",
579  total_bytes_to_write);
580 #endif
581 
582  bytes_written =
583  _dbus_write_socket (socket_transport->fd,
584  &socket_transport->encoded_outgoing,
585  socket_transport->message_bytes_written,
586  total_bytes_to_write - socket_transport->message_bytes_written);
587  }
588  else
589  {
590  total_bytes_to_write = header_len + body_len;
591 
592 #if 0
593  _dbus_verbose ("message is %d bytes\n",
594  total_bytes_to_write);
595 #endif
596 
597 #ifdef HAVE_UNIX_FD_PASSING
598  if (socket_transport->message_bytes_written <= 0 && DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport))
599  {
600  /* Send the fds along with the first byte of the message */
601  const int *unix_fds;
602  unsigned n;
603 
604  _dbus_message_get_unix_fds(message, &unix_fds, &n);
605 
606  bytes_written =
607  _dbus_write_socket_with_unix_fds_two (socket_transport->fd,
608  header,
609  socket_transport->message_bytes_written,
610  header_len - socket_transport->message_bytes_written,
611  body,
612  0, body_len,
613  unix_fds,
614  n);
615 
616  if (bytes_written > 0 && n > 0)
617  _dbus_verbose("Wrote %i unix fds\n", n);
618  }
619  else
620 #endif
621  {
622  if (socket_transport->message_bytes_written < header_len)
623  {
624  bytes_written =
625  _dbus_write_socket_two (socket_transport->fd,
626  header,
627  socket_transport->message_bytes_written,
628  header_len - socket_transport->message_bytes_written,
629  body,
630  0, body_len);
631  }
632  else
633  {
634  bytes_written =
635  _dbus_write_socket (socket_transport->fd,
636  body,
637  (socket_transport->message_bytes_written - header_len),
638  body_len -
639  (socket_transport->message_bytes_written - header_len));
640  }
641  }
642  }
643 
644  if (bytes_written < 0)
645  {
646  /* EINTR already handled for us */
647 
648  /* For some discussion of why we also ignore EPIPE here, see
649  * http://lists.freedesktop.org/archives/dbus/2008-March/009526.html
650  */
651 
653  goto out;
654  else
655  {
656  _dbus_verbose ("Error writing to remote app: %s\n",
658  do_io_error (transport);
659  goto out;
660  }
661  }
662  else
663  {
664  _dbus_verbose (" wrote %d bytes of %d\n", bytes_written,
665  total_bytes_to_write);
666 
667  total += bytes_written;
668  socket_transport->message_bytes_written += bytes_written;
669 
670  _dbus_assert (socket_transport->message_bytes_written <=
671  total_bytes_to_write);
672 
673  if (socket_transport->message_bytes_written == total_bytes_to_write)
674  {
675  socket_transport->message_bytes_written = 0;
676  _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
677  _dbus_string_compact (&socket_transport->encoded_outgoing, 2048);
678 
680  message);
681  }
682  }
683  }
684 
685  out:
686  if (oom)
687  return FALSE;
688  else
689  return TRUE;
690 }
691 
692 /* returns false on out-of-memory */
693 static dbus_bool_t
694 do_reading (DBusTransport *transport)
695 {
696  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
697  DBusString *buffer;
698  int bytes_read;
699  int total;
700  dbus_bool_t oom;
701 
702  _dbus_verbose ("fd = %d\n",socket_transport->fd);
703 
704  /* No messages without authentication! */
705  if (!_dbus_transport_try_to_authenticate (transport))
706  return TRUE;
707 
708  oom = FALSE;
709 
710  total = 0;
711 
712  again:
713 
714  /* See if we've exceeded max messages and need to disable reading */
715  check_read_watch (transport);
716 
717  if (total > socket_transport->max_bytes_read_per_iteration)
718  {
719  _dbus_verbose ("%d bytes exceeds %d bytes read per iteration, returning\n",
720  total, socket_transport->max_bytes_read_per_iteration);
721  goto out;
722  }
723 
724  _dbus_assert (socket_transport->read_watch != NULL ||
725  transport->disconnected);
726 
727  if (transport->disconnected)
728  goto out;
729 
730  if (!dbus_watch_get_enabled (socket_transport->read_watch))
731  return TRUE;
732 
733  if (_dbus_auth_needs_decoding (transport->auth))
734  {
735  /* Does fd passing even make sense with encoded data? */
736  _dbus_assert(!DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport));
737 
738  if (_dbus_string_get_length (&socket_transport->encoded_incoming) > 0)
739  bytes_read = _dbus_string_get_length (&socket_transport->encoded_incoming);
740  else
741  bytes_read = _dbus_read_socket (socket_transport->fd,
742  &socket_transport->encoded_incoming,
743  socket_transport->max_bytes_read_per_iteration);
744 
745  _dbus_assert (_dbus_string_get_length (&socket_transport->encoded_incoming) ==
746  bytes_read);
747 
748  if (bytes_read > 0)
749  {
751  &buffer);
752 
753  if (!_dbus_auth_decode_data (transport->auth,
754  &socket_transport->encoded_incoming,
755  buffer))
756  {
757  _dbus_verbose ("Out of memory decoding incoming data\n");
759  buffer);
760 
761  oom = TRUE;
762  goto out;
763  }
764 
766  buffer);
767 
768  _dbus_string_set_length (&socket_transport->encoded_incoming, 0);
769  _dbus_string_compact (&socket_transport->encoded_incoming, 2048);
770  }
771  }
772  else
773  {
775  &buffer);
776 
777 #ifdef HAVE_UNIX_FD_PASSING
778  if (DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport))
779  {
780  int *fds, n_fds;
781 
782  if (!_dbus_message_loader_get_unix_fds(transport->loader, &fds, &n_fds))
783  {
784  _dbus_verbose ("Out of memory reading file descriptors\n");
785  _dbus_message_loader_return_buffer (transport->loader, buffer);
786  oom = TRUE;
787  goto out;
788  }
789 
790  bytes_read = _dbus_read_socket_with_unix_fds(socket_transport->fd,
791  buffer,
792  socket_transport->max_bytes_read_per_iteration,
793  fds, &n_fds);
794 
795  if (bytes_read >= 0 && n_fds > 0)
796  _dbus_verbose("Read %i unix fds\n", n_fds);
797 
798  _dbus_message_loader_return_unix_fds(transport->loader, fds, bytes_read < 0 ? 0 : n_fds);
799  }
800  else
801 #endif
802  {
803  bytes_read = _dbus_read_socket (socket_transport->fd,
804  buffer, socket_transport->max_bytes_read_per_iteration);
805  }
806 
808  buffer);
809  }
810 
811  if (bytes_read < 0)
812  {
813  /* EINTR already handled for us */
814 
816  {
817  _dbus_verbose ("Out of memory in read()/do_reading()\n");
818  oom = TRUE;
819  goto out;
820  }
822  goto out;
823  else
824  {
825  _dbus_verbose ("Error reading from remote app: %s\n",
827  do_io_error (transport);
828  goto out;
829  }
830  }
831  else if (bytes_read == 0)
832  {
833  _dbus_verbose ("Disconnected from remote app\n");
834  do_io_error (transport);
835  goto out;
836  }
837  else
838  {
839  _dbus_verbose (" read %d bytes\n", bytes_read);
840 
841  total += bytes_read;
842 
843  if (!_dbus_transport_queue_messages (transport))
844  {
845  oom = TRUE;
846  _dbus_verbose (" out of memory when queueing messages we just read in the transport\n");
847  goto out;
848  }
849 
850  /* Try reading more data until we get EAGAIN and return, or
851  * exceed max bytes per iteration. If in blocking mode of
852  * course we'll block instead of returning.
853  */
854  goto again;
855  }
856 
857  out:
858  if (oom)
859  return FALSE;
860  else
861  return TRUE;
862 }
863 
864 static dbus_bool_t
865 unix_error_with_read_to_come (DBusTransport *itransport,
866  DBusWatch *watch,
867  unsigned int flags)
868 {
869  DBusTransportSocket *transport = (DBusTransportSocket *) itransport;
870 
871  if (!(flags & DBUS_WATCH_HANGUP || flags & DBUS_WATCH_ERROR))
872  return FALSE;
873 
874  /* If we have a read watch enabled ...
875  we -might have data incoming ... => handle the HANGUP there */
876  if (watch != transport->read_watch &&
877  _dbus_watch_get_enabled (transport->read_watch))
878  return FALSE;
879 
880  return TRUE;
881 }
882 
883 static dbus_bool_t
884 socket_handle_watch (DBusTransport *transport,
885  DBusWatch *watch,
886  unsigned int flags)
887 {
888  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
889 
890  _dbus_assert (watch == socket_transport->read_watch ||
891  watch == socket_transport->write_watch);
892  _dbus_assert (watch != NULL);
893 
894  /* If we hit an error here on a write watch, don't disconnect the transport yet because data can
895  * still be in the buffer and do_reading may need several iteration to read
896  * it all (because of its max_bytes_read_per_iteration limit).
897  */
898  if (!(flags & DBUS_WATCH_READABLE) && unix_error_with_read_to_come (transport, watch, flags))
899  {
900  _dbus_verbose ("Hang up or error on watch\n");
901  _dbus_transport_disconnect (transport);
902  return TRUE;
903  }
904 
905  if (watch == socket_transport->read_watch &&
906  (flags & DBUS_WATCH_READABLE))
907  {
908  dbus_bool_t auth_finished;
909 #if 1
910  _dbus_verbose ("handling read watch %p flags = %x\n",
911  watch, flags);
912 #endif
913  if (!do_authentication (transport, TRUE, FALSE, &auth_finished))
914  return FALSE;
915 
916  /* We don't want to do a read immediately following
917  * a successful authentication. This is so we
918  * have a chance to propagate the authentication
919  * state further up. Specifically, we need to
920  * process any pending data from the auth object.
921  */
922  if (!auth_finished)
923  {
924  if (!do_reading (transport))
925  {
926  _dbus_verbose ("no memory to read\n");
927  return FALSE;
928  }
929  }
930  else
931  {
932  _dbus_verbose ("Not reading anything since we just completed the authentication\n");
933  }
934  }
935  else if (watch == socket_transport->write_watch &&
936  (flags & DBUS_WATCH_WRITABLE))
937  {
938 #if 1
939  _dbus_verbose ("handling write watch, have_outgoing_messages = %d\n",
941 #endif
942  if (!do_authentication (transport, FALSE, TRUE, NULL))
943  return FALSE;
944 
945  if (!do_writing (transport))
946  {
947  _dbus_verbose ("no memory to write\n");
948  return FALSE;
949  }
950 
951  /* See if we still need the write watch */
952  check_write_watch (transport);
953  }
954 #ifdef DBUS_ENABLE_VERBOSE_MODE
955  else
956  {
957  if (watch == socket_transport->read_watch)
958  _dbus_verbose ("asked to handle read watch with non-read condition 0x%x\n",
959  flags);
960  else if (watch == socket_transport->write_watch)
961  _dbus_verbose ("asked to handle write watch with non-write condition 0x%x\n",
962  flags);
963  else
964  _dbus_verbose ("asked to handle watch %p on fd %d that we don't recognize\n",
965  watch, dbus_watch_get_socket (watch));
966  }
967 #endif /* DBUS_ENABLE_VERBOSE_MODE */
968 
969  return TRUE;
970 }
971 
972 static void
973 socket_disconnect (DBusTransport *transport)
974 {
975  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
976 
977  _dbus_verbose ("\n");
978 
979  free_watches (transport);
980 
981  _dbus_close_socket (socket_transport->fd, NULL);
982  socket_transport->fd = -1;
983 }
984 
985 static dbus_bool_t
986 socket_connection_set (DBusTransport *transport)
987 {
988  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
989 
990  _dbus_watch_set_handler (socket_transport->write_watch,
992  transport->connection, NULL);
993 
994  _dbus_watch_set_handler (socket_transport->read_watch,
996  transport->connection, NULL);
997 
999  socket_transport->write_watch))
1000  return FALSE;
1001 
1003  socket_transport->read_watch))
1004  {
1006  socket_transport->write_watch);
1007  return FALSE;
1008  }
1009 
1010  check_read_watch (transport);
1011  check_write_watch (transport);
1012 
1013  return TRUE;
1014 }
1015 
1023 static void
1024 socket_do_iteration (DBusTransport *transport,
1025  unsigned int flags,
1026  int timeout_milliseconds)
1027 {
1028  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1029  DBusPollFD poll_fd;
1030  int poll_res;
1031  int poll_timeout;
1032 
1033  _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p fd = %d\n",
1034  flags & DBUS_ITERATION_DO_READING ? "read" : "",
1035  flags & DBUS_ITERATION_DO_WRITING ? "write" : "",
1036  timeout_milliseconds,
1037  socket_transport->read_watch,
1038  socket_transport->write_watch,
1039  socket_transport->fd);
1040 
1041  /* the passed in DO_READING/DO_WRITING flags indicate whether to
1042  * read/write messages, but regardless of those we may need to block
1043  * for reading/writing to do auth. But if we do reading for auth,
1044  * we don't want to read any messages yet if not given DO_READING.
1045  */
1046 
1047  poll_fd.fd = socket_transport->fd;
1048  poll_fd.events = 0;
1049 
1050  if (_dbus_transport_try_to_authenticate (transport))
1051  {
1052  /* This is kind of a hack; if we have stuff to write, then try
1053  * to avoid the poll. This is probably about a 5% speedup on an
1054  * echo client/server.
1055  *
1056  * If both reading and writing were requested, we want to avoid this
1057  * since it could have funky effects:
1058  * - both ends spinning waiting for the other one to read
1059  * data so they can finish writing
1060  * - prioritizing all writing ahead of reading
1061  */
1062  if ((flags & DBUS_ITERATION_DO_WRITING) &&
1063  !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) &&
1064  !transport->disconnected &&
1066  {
1067  do_writing (transport);
1068 
1069  if (transport->disconnected ||
1071  goto out;
1072  }
1073 
1074  /* If we get here, we decided to do the poll() after all */
1075  _dbus_assert (socket_transport->read_watch);
1076  if (flags & DBUS_ITERATION_DO_READING)
1077  poll_fd.events |= _DBUS_POLLIN;
1078 
1079  _dbus_assert (socket_transport->write_watch);
1080  if (flags & DBUS_ITERATION_DO_WRITING)
1081  poll_fd.events |= _DBUS_POLLOUT;
1082  }
1083  else
1084  {
1085  DBusAuthState auth_state;
1086 
1087  auth_state = _dbus_auth_do_work (transport->auth);
1088 
1089  if (transport->receive_credentials_pending ||
1090  auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
1091  poll_fd.events |= _DBUS_POLLIN;
1092 
1093  if (transport->send_credentials_pending ||
1094  auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
1095  poll_fd.events |= _DBUS_POLLOUT;
1096  }
1097 
1098  if (poll_fd.events)
1099  {
1100  if (flags & DBUS_ITERATION_BLOCK)
1101  poll_timeout = timeout_milliseconds;
1102  else
1103  poll_timeout = 0;
1104 
1105  /* For blocking selects we drop the connection lock here
1106  * to avoid blocking out connection access during a potentially
1107  * indefinite blocking call. The io path is still protected
1108  * by the io_path_cond condvar, so we won't reenter this.
1109  */
1110  if (flags & DBUS_ITERATION_BLOCK)
1111  {
1112  _dbus_verbose ("unlock pre poll\n");
1113  _dbus_connection_unlock (transport->connection);
1114  }
1115 
1116  again:
1117  poll_res = _dbus_poll (&poll_fd, 1, poll_timeout);
1118 
1119  if (poll_res < 0 && _dbus_get_is_errno_eintr ())
1120  goto again;
1121 
1122  if (flags & DBUS_ITERATION_BLOCK)
1123  {
1124  _dbus_verbose ("lock post poll\n");
1125  _dbus_connection_lock (transport->connection);
1126  }
1127 
1128  if (poll_res >= 0)
1129  {
1130  if (poll_res == 0)
1131  poll_fd.revents = 0; /* some concern that posix does not guarantee this;
1132  * valgrind flags it as an error. though it probably
1133  * is guaranteed on linux at least.
1134  */
1135 
1136  if (poll_fd.revents & _DBUS_POLLERR)
1137  do_io_error (transport);
1138  else
1139  {
1140  dbus_bool_t need_read = (poll_fd.revents & _DBUS_POLLIN) > 0;
1141  dbus_bool_t need_write = (poll_fd.revents & _DBUS_POLLOUT) > 0;
1142  dbus_bool_t authentication_completed;
1143 
1144  _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
1145  need_read, need_write);
1146  do_authentication (transport, need_read, need_write,
1147  &authentication_completed);
1148 
1149  /* See comment in socket_handle_watch. */
1150  if (authentication_completed)
1151  goto out;
1152 
1153  if (need_read && (flags & DBUS_ITERATION_DO_READING))
1154  do_reading (transport);
1155  if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
1156  do_writing (transport);
1157  }
1158  }
1159  else
1160  {
1161  _dbus_verbose ("Error from _dbus_poll(): %s\n",
1163  }
1164  }
1165 
1166 
1167  out:
1168  /* We need to install the write watch only if we did not
1169  * successfully write everything. Note we need to be careful that we
1170  * don't call check_write_watch *before* do_writing, since it's
1171  * inefficient to add the write watch, and we can avoid it most of
1172  * the time since we can write immediately.
1173  *
1174  * However, we MUST always call check_write_watch(); DBusConnection code
1175  * relies on the fact that running an iteration will notice that
1176  * messages are pending.
1177  */
1178  check_write_watch (transport);
1179 
1180  _dbus_verbose (" ... leaving do_iteration()\n");
1181 }
1182 
1183 static void
1184 socket_live_messages_changed (DBusTransport *transport)
1185 {
1186  /* See if we should look for incoming messages again */
1187  check_read_watch (transport);
1188 }
1189 
1190 
1191 static dbus_bool_t
1192 socket_get_socket_fd (DBusTransport *transport,
1193  int *fd_p)
1194 {
1195  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1196 
1197  *fd_p = socket_transport->fd;
1198 
1199  return TRUE;
1200 }
1201 
1202 static const DBusTransportVTable socket_vtable = {
1203  socket_finalize,
1204  socket_handle_watch,
1205  socket_disconnect,
1206  socket_connection_set,
1207  socket_do_iteration,
1208  socket_live_messages_changed,
1209  socket_get_socket_fd
1210 };
1211 
1225  const DBusString *server_guid,
1226  const DBusString *address)
1227 {
1228  DBusTransportSocket *socket_transport;
1229 
1230  socket_transport = dbus_new0 (DBusTransportSocket, 1);
1231  if (socket_transport == NULL)
1232  return NULL;
1233 
1234  if (!_dbus_string_init (&socket_transport->encoded_outgoing))
1235  goto failed_0;
1236 
1237  if (!_dbus_string_init (&socket_transport->encoded_incoming))
1238  goto failed_1;
1239 
1240  socket_transport->write_watch = _dbus_watch_new (fd,
1241  DBUS_WATCH_WRITABLE,
1242  FALSE,
1243  NULL, NULL, NULL);
1244  if (socket_transport->write_watch == NULL)
1245  goto failed_2;
1246 
1247  socket_transport->read_watch = _dbus_watch_new (fd,
1248  DBUS_WATCH_READABLE,
1249  FALSE,
1250  NULL, NULL, NULL);
1251  if (socket_transport->read_watch == NULL)
1252  goto failed_3;
1253 
1254  if (!_dbus_transport_init_base (&socket_transport->base,
1255  &socket_vtable,
1256  server_guid, address))
1257  goto failed_4;
1258 
1259 #ifdef HAVE_UNIX_FD_PASSING
1261 #endif
1262 
1263  socket_transport->fd = fd;
1264  socket_transport->message_bytes_written = 0;
1265 
1266  /* These values should probably be tunable or something. */
1267  socket_transport->max_bytes_read_per_iteration = 2048;
1268  socket_transport->max_bytes_written_per_iteration = 2048;
1269 
1270  return (DBusTransport*) socket_transport;
1271 
1272  failed_4:
1273  _dbus_watch_invalidate (socket_transport->read_watch);
1274  _dbus_watch_unref (socket_transport->read_watch);
1275  failed_3:
1276  _dbus_watch_invalidate (socket_transport->write_watch);
1277  _dbus_watch_unref (socket_transport->write_watch);
1278  failed_2:
1279  _dbus_string_free (&socket_transport->encoded_incoming);
1280  failed_1:
1281  _dbus_string_free (&socket_transport->encoded_outgoing);
1282  failed_0:
1283  dbus_free (socket_transport);
1284  return NULL;
1285 }
1286 
1300  const char *port,
1301  const char *family,
1302  const char *noncefile,
1303  DBusError *error)
1304 {
1305  int fd;
1306  DBusTransport *transport;
1307  DBusString address;
1308 
1309  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1310 
1311  if (!_dbus_string_init (&address))
1312  {
1314  return NULL;
1315  }
1316 
1317  if (host == NULL)
1318  host = "localhost";
1319 
1320  if (!_dbus_string_append (&address, noncefile ? "nonce-tcp:" : "tcp:"))
1321  goto error;
1322 
1323  if (!_dbus_string_append (&address, "host=") ||
1324  !_dbus_string_append (&address, host))
1325  goto error;
1326 
1327  if (!_dbus_string_append (&address, ",port=") ||
1328  !_dbus_string_append (&address, port))
1329  goto error;
1330 
1331  if (family != NULL &&
1332  (!_dbus_string_append (&address, ",family=") ||
1333  !_dbus_string_append (&address, family)))
1334  goto error;
1335 
1336  if (noncefile != NULL &&
1337  (!_dbus_string_append (&address, ",noncefile=") ||
1338  !_dbus_string_append (&address, noncefile)))
1339  goto error;
1340 
1341  fd = _dbus_connect_tcp_socket_with_nonce (host, port, family, noncefile, error);
1342  if (fd < 0)
1343  {
1344  _DBUS_ASSERT_ERROR_IS_SET (error);
1345  _dbus_string_free (&address);
1346  return NULL;
1347  }
1348 
1349  _dbus_verbose ("Successfully connected to tcp socket %s:%s\n",
1350  host, port);
1351 
1352  transport = _dbus_transport_new_for_socket (fd, NULL, &address);
1353  _dbus_string_free (&address);
1354  if (transport == NULL)
1355  {
1357  _dbus_close_socket (fd, NULL);
1358  fd = -1;
1359  }
1360 
1361  return transport;
1362 
1363 error:
1364  _dbus_string_free (&address);
1366  return NULL;
1367 }
1368 
1377 DBusTransportOpenResult
1379  DBusTransport **transport_p,
1380  DBusError *error)
1381 {
1382  const char *method;
1383  dbus_bool_t isTcp;
1384  dbus_bool_t isNonceTcp;
1385 
1386  method = dbus_address_entry_get_method (entry);
1387  _dbus_assert (method != NULL);
1388 
1389  isTcp = strcmp (method, "tcp") == 0;
1390  isNonceTcp = strcmp (method, "nonce-tcp") == 0;
1391 
1392  if (isTcp || isNonceTcp)
1393  {
1394  const char *host = dbus_address_entry_get_value (entry, "host");
1395  const char *port = dbus_address_entry_get_value (entry, "port");
1396  const char *family = dbus_address_entry_get_value (entry, "family");
1397  const char *noncefile = dbus_address_entry_get_value (entry, "noncefile");
1398 
1399  if ((isNonceTcp == TRUE) != (noncefile != NULL)) {
1400  _dbus_set_bad_address (error, method, "noncefile", NULL);
1401  return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1402  }
1403 
1404  if (port == NULL)
1405  {
1406  _dbus_set_bad_address (error, method, "port", NULL);
1407  return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1408  }
1409 
1410  *transport_p = _dbus_transport_new_for_tcp_socket (host, port, family, noncefile, error);
1411  if (*transport_p == NULL)
1412  {
1413  _DBUS_ASSERT_ERROR_IS_SET (error);
1414  return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
1415  }
1416  else
1417  {
1418  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1419  return DBUS_TRANSPORT_OPEN_OK;
1420  }
1421  }
1422  else
1423  {
1424  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1425  return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
1426  }
1427 }
1428