1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-transport-unix.c UNIX socket subclasses of DBusTransport
4 * Copyright (C) 2002, 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-internals.h"
25 #include "dbus-connection-internal.h"
26 #include "dbus-transport-unix.h"
27 #include "dbus-transport-protected.h"
28 #include "dbus-watch.h"
29 #include <sys/types.h>
35 * @defgroup DBusTransportUnix DBusTransport implementations for UNIX
36 * @ingroup DBusInternals
37 * @brief Implementation details of DBusTransport on UNIX
43 * Opaque object representing a Unix file descriptor transport.
45 typedef struct DBusTransportUnix DBusTransportUnix;
48 * Implementation details of DBusTransportUnix. All members are private.
50 struct DBusTransportUnix
52 DBusTransport base; /**< Parent instance */
53 int fd; /**< File descriptor. */
54 DBusWatch *read_watch; /**< Watch for readability. */
55 DBusWatch *write_watch; /**< Watch for writability. */
57 int max_bytes_read_per_iteration; /**< To avoid blocking too long. */
58 int max_bytes_written_per_iteration; /**< To avoid blocking too long. */
60 int message_bytes_written; /**< Number of bytes of current
61 * outgoing message that have
64 DBusString encoded_message; /**< Encoded version of current
70 free_watches (DBusTransport *transport)
72 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
74 if (unix_transport->read_watch)
76 if (transport->connection)
77 _dbus_connection_remove_watch (transport->connection,
78 unix_transport->read_watch);
79 _dbus_watch_invalidate (unix_transport->read_watch);
80 _dbus_watch_unref (unix_transport->read_watch);
81 unix_transport->read_watch = NULL;
84 if (unix_transport->write_watch)
86 if (transport->connection)
87 _dbus_connection_remove_watch (transport->connection,
88 unix_transport->write_watch);
89 _dbus_watch_invalidate (unix_transport->write_watch);
90 _dbus_watch_unref (unix_transport->write_watch);
91 unix_transport->write_watch = NULL;
96 unix_finalize (DBusTransport *transport)
98 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
100 free_watches (transport);
102 _dbus_string_free (&unix_transport->encoded_message);
104 _dbus_transport_finalize_base (transport);
106 _dbus_assert (unix_transport->read_watch == NULL);
107 _dbus_assert (unix_transport->write_watch == NULL);
109 dbus_free (transport);
113 check_write_watch (DBusTransport *transport)
115 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
116 dbus_bool_t need_write_watch;
118 if (transport->connection == NULL)
121 if (transport->disconnected)
123 _dbus_assert (unix_transport->write_watch == NULL);
127 _dbus_transport_ref (transport);
129 if (_dbus_transport_get_is_authenticated (transport))
130 need_write_watch = transport->messages_need_sending;
132 need_write_watch = transport->send_credentials_pending ||
133 _dbus_auth_do_work (transport->auth) == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
135 _dbus_connection_toggle_watch (transport->connection,
136 unix_transport->write_watch,
139 _dbus_transport_unref (transport);
143 check_read_watch (DBusTransport *transport)
145 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
146 dbus_bool_t need_read_watch;
148 if (transport->connection == NULL)
151 if (transport->disconnected)
153 _dbus_assert (unix_transport->read_watch == NULL);
157 _dbus_transport_ref (transport);
159 if (_dbus_transport_get_is_authenticated (transport))
161 _dbus_counter_get_value (transport->live_messages_size) < transport->max_live_messages_size;
163 need_read_watch = transport->receive_credentials_pending ||
164 _dbus_auth_do_work (transport->auth) == DBUS_AUTH_STATE_WAITING_FOR_INPUT;
166 _dbus_connection_toggle_watch (transport->connection,
167 unix_transport->read_watch,
170 _dbus_transport_unref (transport);
174 do_io_error (DBusTransport *transport)
176 _dbus_transport_ref (transport);
177 _dbus_transport_disconnect (transport);
178 _dbus_transport_unref (transport);
182 queue_messages (DBusTransport *transport)
184 DBusMessage *message;
186 /* Queue any messages */
187 while ((message = _dbus_message_loader_pop_message (transport->loader)))
189 _dbus_verbose ("queueing received message %p\n", message);
191 _dbus_message_add_size_counter (message, transport->live_messages_size);
192 if (!_dbus_connection_queue_received_message (transport->connection,
196 dbus_message_unref (message);
199 if (_dbus_message_loader_get_is_corrupted (transport->loader))
201 _dbus_verbose ("Corrupted message stream, disconnecting\n");
202 do_io_error (transport);
205 /* check read watch in case we've now exceeded max outstanding messages */
206 check_read_watch (transport);
209 /* return value is whether we successfully read any new data. */
211 read_data_into_auth (DBusTransport *transport)
213 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
217 if (!_dbus_string_init (&buffer, _DBUS_INT_MAX))
219 /* just disconnect if we don't have memory
220 * to do an authentication
222 _dbus_verbose ("No memory for authentication\n");
223 do_io_error (transport);
227 bytes_read = _dbus_read (unix_transport->fd,
228 &buffer, unix_transport->max_bytes_read_per_iteration);
232 _dbus_verbose (" read %d bytes in auth phase\n", bytes_read);
234 if (_dbus_auth_bytes_received (transport->auth,
237 _dbus_string_free (&buffer);
238 return TRUE; /* We did read some data! woo! */
242 /* just disconnect if we don't have memory to do an
243 * authentication, don't fool with trying to save the buffer
244 * and who knows what.
246 _dbus_verbose ("No memory for authentication\n");
247 do_io_error (transport);
250 else if (bytes_read < 0)
252 /* EINTR already handled for us */
254 if (errno == EAGAIN ||
255 errno == EWOULDBLOCK)
256 ; /* do nothing, just return FALSE below */
259 _dbus_verbose ("Error reading from remote app: %s\n",
260 _dbus_strerror (errno));
261 do_io_error (transport);
264 else if (bytes_read == 0)
266 _dbus_verbose ("Disconnected from remote app\n");
267 do_io_error (transport);
270 _dbus_string_free (&buffer);
274 /* Return value is whether we successfully wrote any bytes */
276 write_data_from_auth (DBusTransport *transport)
278 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
280 const DBusString *buffer;
282 if (!_dbus_auth_get_bytes_to_send (transport->auth,
286 bytes_written = _dbus_write (unix_transport->fd,
288 0, _dbus_string_get_length (buffer));
290 if (bytes_written > 0)
292 _dbus_auth_bytes_sent (transport->auth, bytes_written);
295 else if (bytes_written < 0)
297 /* EINTR already handled for us */
299 if (errno == EAGAIN ||
300 errno == EWOULDBLOCK)
304 _dbus_verbose ("Error writing to remote app: %s\n",
305 _dbus_strerror (errno));
306 do_io_error (transport);
314 recover_unused_bytes (DBusTransport *transport)
317 if (_dbus_auth_needs_decoding (transport->auth))
319 DBusString plaintext;
324 if (!_dbus_string_init (&plaintext, _DBUS_INT_MAX))
327 if (!_dbus_string_init (&encoded, _DBUS_INT_MAX))
329 _dbus_string_free (&plaintext);
333 if (!_dbus_auth_get_unused_bytes (transport->auth,
336 _dbus_string_free (&plaintext);
337 _dbus_string_free (&encoded);
341 if (!_dbus_auth_decode_data (transport->auth,
342 &encoded, &plaintext))
344 _dbus_string_free (&plaintext);
345 _dbus_string_free (&encoded);
349 _dbus_message_loader_get_buffer (transport->loader,
352 orig_len = _dbus_string_get_length (buffer);
354 if (!_dbus_string_move (&plaintext, 0, buffer,
357 _dbus_string_free (&plaintext);
358 _dbus_string_free (&encoded);
362 _dbus_verbose (" %d unused bytes sent to message loader\n",
363 _dbus_string_get_length (buffer) -
366 _dbus_message_loader_return_buffer (transport->loader,
368 _dbus_string_get_length (buffer) -
371 _dbus_string_free (&plaintext);
372 _dbus_string_free (&encoded);
379 _dbus_message_loader_get_buffer (transport->loader,
382 orig_len = _dbus_string_get_length (buffer);
384 if (!_dbus_auth_get_unused_bytes (transport->auth,
388 _dbus_verbose (" %d unused bytes sent to message loader\n",
389 _dbus_string_get_length (buffer) -
392 _dbus_message_loader_return_buffer (transport->loader,
394 _dbus_string_get_length (buffer) -
398 queue_messages (transport);
403 _dbus_verbose ("Not enough memory to transfer unused bytes from auth conversation\n");
404 do_io_error (transport);
408 exchange_credentials (DBusTransport *transport,
409 dbus_bool_t do_reading,
410 dbus_bool_t do_writing)
412 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
414 if (do_writing && transport->send_credentials_pending)
416 if (_dbus_send_credentials_unix_socket (unix_transport->fd,
419 transport->send_credentials_pending = FALSE;
423 _dbus_verbose ("Failed to write credentials\n");
424 do_io_error (transport);
428 if (do_reading && transport->receive_credentials_pending)
430 if (_dbus_read_credentials_unix_socket (unix_transport->fd,
431 &transport->credentials,
434 transport->receive_credentials_pending = FALSE;
438 _dbus_verbose ("Failed to read credentials\n");
439 do_io_error (transport);
443 if (!(transport->send_credentials_pending ||
444 transport->receive_credentials_pending))
446 _dbus_auth_set_credentials (transport->auth,
447 &transport->credentials);
452 do_authentication (DBusTransport *transport,
453 dbus_bool_t do_reading,
454 dbus_bool_t do_writing)
456 _dbus_transport_ref (transport);
458 while (!_dbus_transport_get_is_authenticated (transport) &&
459 _dbus_transport_get_is_connected (transport))
461 exchange_credentials (transport, do_reading, do_writing);
463 if (transport->send_credentials_pending ||
464 transport->receive_credentials_pending)
466 _dbus_verbose ("send_credentials_pending = %d receive_credentials_pending = %d\n",
467 transport->send_credentials_pending,
468 transport->receive_credentials_pending);
472 switch (_dbus_auth_do_work (transport->auth))
474 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
475 _dbus_verbose (" auth state: waiting for input\n");
476 if (!do_reading || !read_data_into_auth (transport))
480 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
481 /* Screw it, just disconnect */
482 _dbus_verbose (" auth state: waiting for memory\n");
483 do_io_error (transport);
486 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
487 _dbus_verbose (" auth state: bytes to send\n");
488 if (!do_writing || !write_data_from_auth (transport))
492 case DBUS_AUTH_STATE_NEED_DISCONNECT:
493 _dbus_verbose (" auth state: need to disconnect\n");
494 do_io_error (transport);
497 case DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES:
498 _dbus_verbose (" auth state: auth with unused bytes\n");
499 recover_unused_bytes (transport);
502 case DBUS_AUTH_STATE_AUTHENTICATED:
503 _dbus_verbose (" auth state: authenticated\n");
509 check_read_watch (transport);
510 check_write_watch (transport);
511 _dbus_transport_unref (transport);
515 do_writing (DBusTransport *transport)
518 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
520 /* No messages without authentication! */
521 if (!_dbus_transport_get_is_authenticated (transport))
524 if (transport->disconnected)
529 while (!transport->disconnected &&
530 _dbus_connection_have_messages_to_send (transport->connection))
533 DBusMessage *message;
534 const DBusString *header;
535 const DBusString *body;
536 int header_len, body_len;
537 int total_bytes_to_write;
539 if (total > unix_transport->max_bytes_written_per_iteration)
541 _dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n",
542 total, unix_transport->max_bytes_written_per_iteration);
546 if (unix_transport->write_watch == NULL)
548 _dbus_verbose ("write watch removed, not writing more stuff\n");
552 message = _dbus_connection_get_message_to_send (transport->connection);
553 _dbus_assert (message != NULL);
554 _dbus_message_lock (message);
557 _dbus_verbose ("writing message %p\n", message);
560 _dbus_message_get_network_data (message,
563 header_len = _dbus_string_get_length (header);
564 body_len = _dbus_string_get_length (body);
566 if (_dbus_auth_needs_encoding (transport->auth))
568 if (_dbus_string_get_length (&unix_transport->encoded_message) == 0)
570 if (!_dbus_auth_encode_data (transport->auth,
571 header, &unix_transport->encoded_message))
574 if (!_dbus_auth_encode_data (transport->auth,
575 body, &unix_transport->encoded_message))
577 _dbus_string_set_length (&unix_transport->encoded_message, 0);
582 total_bytes_to_write = _dbus_string_get_length (&unix_transport->encoded_message);
585 _dbus_verbose ("encoded message is %d bytes\n",
586 total_bytes_to_write);
590 _dbus_write (unix_transport->fd,
591 &unix_transport->encoded_message,
592 unix_transport->message_bytes_written,
593 total_bytes_to_write - unix_transport->message_bytes_written);
597 total_bytes_to_write = header_len + body_len;
600 _dbus_verbose ("message is %d bytes\n",
601 total_bytes_to_write);
604 if (unix_transport->message_bytes_written < header_len)
607 _dbus_write_two (unix_transport->fd,
609 unix_transport->message_bytes_written,
610 header_len - unix_transport->message_bytes_written,
617 _dbus_write (unix_transport->fd,
619 (unix_transport->message_bytes_written - header_len),
621 (unix_transport->message_bytes_written - header_len));
625 if (bytes_written < 0)
627 /* EINTR already handled for us */
629 if (errno == EAGAIN ||
630 errno == EWOULDBLOCK)
634 _dbus_verbose ("Error writing to remote app: %s\n",
635 _dbus_strerror (errno));
636 do_io_error (transport);
642 _dbus_verbose (" wrote %d bytes of %d\n", bytes_written,
643 total_bytes_to_write);
645 total += bytes_written;
646 unix_transport->message_bytes_written += bytes_written;
648 _dbus_assert (unix_transport->message_bytes_written <=
649 total_bytes_to_write);
651 if (unix_transport->message_bytes_written == total_bytes_to_write)
653 unix_transport->message_bytes_written = 0;
654 _dbus_string_set_length (&unix_transport->encoded_message, 0);
656 _dbus_connection_message_sent (transport->connection,
663 return; /* I think some C compilers require a statement after a label */
667 do_reading (DBusTransport *transport)
669 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
674 /* No messages without authentication! */
675 if (!_dbus_transport_get_is_authenticated (transport))
682 /* See if we've exceeded max messages and need to disable reading */
683 check_read_watch (transport);
684 if (unix_transport->read_watch == NULL)
687 if (total > unix_transport->max_bytes_read_per_iteration)
689 _dbus_verbose ("%d bytes exceeds %d bytes read per iteration, returning\n",
690 total, unix_transport->max_bytes_read_per_iteration);
694 if (transport->disconnected)
697 if (_dbus_auth_needs_decoding (transport->auth))
701 if (!_dbus_string_init (&encoded, _DBUS_INT_MAX))
702 goto out; /* not enough memory for the moment */
704 bytes_read = _dbus_read (unix_transport->fd,
706 unix_transport->max_bytes_read_per_iteration);
712 _dbus_message_loader_get_buffer (transport->loader,
715 orig_len = _dbus_string_get_length (buffer);
717 if (!_dbus_auth_decode_data (transport->auth,
720 /* FIXME argh, we are really fucked here - nowhere to
721 * put "encoded" while we wait for more memory. Just
722 * screw it for now and disconnect. The failure may be
723 * due to badly-encoded data instead of lack of memory
726 _dbus_verbose ("Disconnected from remote app due to failure decoding data\n");
727 do_io_error (transport);
730 _dbus_message_loader_return_buffer (transport->loader,
732 _dbus_string_get_length (buffer) - orig_len);
735 _dbus_string_free (&encoded);
739 _dbus_message_loader_get_buffer (transport->loader,
742 bytes_read = _dbus_read (unix_transport->fd,
743 buffer, unix_transport->max_bytes_read_per_iteration);
745 _dbus_message_loader_return_buffer (transport->loader,
747 bytes_read < 0 ? 0 : bytes_read);
752 /* EINTR already handled for us */
754 if (errno == EAGAIN ||
755 errno == EWOULDBLOCK)
759 _dbus_verbose ("Error reading from remote app: %s\n",
760 _dbus_strerror (errno));
761 do_io_error (transport);
765 else if (bytes_read == 0)
767 _dbus_verbose ("Disconnected from remote app\n");
768 do_io_error (transport);
773 _dbus_verbose (" read %d bytes\n", bytes_read);
777 queue_messages (transport);
779 /* Try reading more data until we get EAGAIN and return, or
780 * exceed max bytes per iteration. If in blocking mode of
781 * course we'll block instead of returning.
787 return; /* I think some C compilers require a statement after a label */
791 unix_handle_watch (DBusTransport *transport,
795 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
797 _dbus_assert (watch == unix_transport->read_watch ||
798 watch == unix_transport->write_watch);
800 if (flags & (DBUS_WATCH_HANGUP | DBUS_WATCH_ERROR))
802 _dbus_transport_disconnect (transport);
806 if (watch == unix_transport->read_watch &&
807 (flags & DBUS_WATCH_READABLE))
810 _dbus_verbose ("handling read watch\n");
812 do_authentication (transport, TRUE, FALSE);
813 do_reading (transport);
815 else if (watch == unix_transport->write_watch &&
816 (flags & DBUS_WATCH_WRITABLE))
819 _dbus_verbose ("handling write watch\n");
821 do_authentication (transport, FALSE, TRUE);
822 do_writing (transport);
827 unix_disconnect (DBusTransport *transport)
829 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
831 free_watches (transport);
833 _dbus_close (unix_transport->fd, NULL);
834 unix_transport->fd = -1;
838 unix_connection_set (DBusTransport *transport)
840 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
842 if (!_dbus_connection_add_watch (transport->connection,
843 unix_transport->write_watch))
846 if (!_dbus_connection_add_watch (transport->connection,
847 unix_transport->read_watch))
849 _dbus_connection_remove_watch (transport->connection,
850 unix_transport->write_watch);
854 check_read_watch (transport);
855 check_write_watch (transport);
861 unix_messages_pending (DBusTransport *transport,
862 int messages_pending)
864 check_write_watch (transport);
867 /* FIXME use _dbus_poll(), not select() */
869 * @todo We need to have a way to wake up the select sleep if
870 * a new iteration request comes in with a flag (read/write) that
871 * we're not currently serving. Otherwise a call that just reads
872 * could block a write call forever (if there are no incoming
876 unix_do_iteration (DBusTransport *transport,
878 int timeout_milliseconds)
880 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
883 dbus_bool_t do_select;
886 _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p\n",
887 flags & DBUS_ITERATION_DO_READING ? "read" : "",
888 flags & DBUS_ITERATION_DO_WRITING ? "write" : "",
889 timeout_milliseconds,
890 unix_transport->read_watch,
891 unix_transport->write_watch);
893 /* "again" has to be up here because on EINTR the fd sets become
900 /* the passed in DO_READING/DO_WRITING flags indicate whether to
901 * read/write messages, but regardless of those we may need to block
902 * for reading/writing to do auth. But if we do reading for auth,
903 * we don't want to read any messages yet if not given DO_READING.
905 * Also, if read_watch == NULL or write_watch == NULL, we don't
906 * want to read/write so don't.
910 FD_ZERO (&write_set);
912 if (_dbus_transport_get_is_authenticated (transport))
914 if (unix_transport->read_watch &&
915 (flags & DBUS_ITERATION_DO_READING))
917 FD_SET (unix_transport->fd, &read_set);
921 if (unix_transport->write_watch &&
922 (flags & DBUS_ITERATION_DO_WRITING))
924 FD_SET (unix_transport->fd, &write_set);
930 DBusAuthState auth_state;
932 auth_state = _dbus_auth_do_work (transport->auth);
934 if (transport->receive_credentials_pending ||
935 auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
937 FD_SET (unix_transport->fd, &read_set);
941 if (transport->send_credentials_pending ||
942 auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
944 FD_SET (unix_transport->fd, &write_set);
952 struct timeval timeout;
953 dbus_bool_t use_timeout;
956 FD_SET (unix_transport->fd, &err_set);
958 if (flags & DBUS_ITERATION_BLOCK)
960 if (timeout_milliseconds >= 0)
962 timeout.tv_sec = timeout_milliseconds / 1000;
963 timeout.tv_usec = (timeout_milliseconds % 1000) * 1000;
965 /* Always use timeout if one is passed in. */
970 use_timeout = FALSE; /* NULL timeout to block forever */
975 /* 0 timeout to not block */
981 /* For blocking selects we drop the connection lock here
982 * to avoid blocking out connection access during a potentially
983 * indefinite blocking call. The io path is still protected
984 * by the io_path_cond condvar, so we won't reenter this.
986 if (flags & DBUS_ITERATION_BLOCK)
987 _dbus_connection_unlock (transport->connection);
989 select_res = select (unix_transport->fd + 1,
990 &read_set, &write_set, &err_set,
991 use_timeout ? &timeout : NULL);
993 if (flags & DBUS_ITERATION_BLOCK)
994 _dbus_connection_lock (transport->connection);
999 if (FD_ISSET (unix_transport->fd, &err_set))
1000 do_io_error (transport);
1003 dbus_bool_t need_read = FD_ISSET (unix_transport->fd, &read_set);
1004 dbus_bool_t need_write = FD_ISSET (unix_transport->fd, &write_set);
1006 _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
1007 need_read, need_write);
1008 do_authentication (transport, need_read, need_write);
1010 if (need_read && (flags & DBUS_ITERATION_DO_READING))
1011 do_reading (transport);
1012 if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
1013 do_writing (transport);
1016 else if (errno == EINTR)
1020 _dbus_verbose ("Error from select(): %s\n",
1021 _dbus_strerror (errno));
1027 unix_live_messages_changed (DBusTransport *transport)
1029 /* See if we should look for incoming messages again */
1030 check_read_watch (transport);
1033 static DBusTransportVTable unix_vtable = {
1037 unix_connection_set,
1038 unix_messages_pending,
1040 unix_live_messages_changed
1044 * Creates a new transport for the given file descriptor. The file
1045 * descriptor must be nonblocking (use _dbus_set_fd_nonblocking() to
1046 * make it so). This function is shared by various transports that
1047 * boil down to a full duplex file descriptor.
1049 * @param fd the file descriptor.
1050 * @param server #TRUE if this transport is on the server side of a connection
1051 * @returns the new transport, or #NULL if no memory.
1054 _dbus_transport_new_for_fd (int fd,
1057 DBusTransportUnix *unix_transport;
1059 unix_transport = dbus_new0 (DBusTransportUnix, 1);
1060 if (unix_transport == NULL)
1063 if (!_dbus_string_init (&unix_transport->encoded_message,
1067 unix_transport->write_watch = _dbus_watch_new (fd,
1068 DBUS_WATCH_WRITABLE,
1070 if (unix_transport->write_watch == NULL)
1073 unix_transport->read_watch = _dbus_watch_new (fd,
1074 DBUS_WATCH_READABLE,
1076 if (unix_transport->read_watch == NULL)
1079 if (!_dbus_transport_init_base (&unix_transport->base,
1084 unix_transport->fd = fd;
1085 unix_transport->message_bytes_written = 0;
1087 /* These values should probably be tunable or something. */
1088 unix_transport->max_bytes_read_per_iteration = 2048;
1089 unix_transport->max_bytes_written_per_iteration = 2048;
1091 return (DBusTransport*) unix_transport;
1094 _dbus_watch_unref (unix_transport->read_watch);
1096 _dbus_watch_unref (unix_transport->write_watch);
1098 _dbus_string_free (&unix_transport->encoded_message);
1100 dbus_free (unix_transport);
1105 * Creates a new transport for the given Unix domain socket
1108 * @param path the path to the domain socket.
1109 * @param server #TRUE if this transport is on the server side of a connection
1110 * @param result location to store reason for failure.
1111 * @returns a new transport, or #NULL on failure.
1114 _dbus_transport_new_for_domain_socket (const char *path,
1116 DBusResultCode *result)
1119 DBusTransport *transport;
1121 fd = _dbus_connect_unix_socket (path, result);
1125 _dbus_fd_set_close_on_exec (fd);
1127 _dbus_verbose ("Successfully connected to unix socket %s\n",
1130 transport = _dbus_transport_new_for_fd (fd, server);
1131 if (transport == NULL)
1133 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
1134 _dbus_close (fd, NULL);
1142 * Creates a new transport for the given hostname and port.
1144 * @param host the host to connect to
1145 * @param port the port to connect to
1146 * @param server #TRUE if this transport is on the server side of a connection
1147 * @param result location to store reason for failure.
1148 * @returns a new transport, or #NULL on failure.
1151 _dbus_transport_new_for_tcp_socket (const char *host,
1154 DBusResultCode *result)
1157 DBusTransport *transport;
1159 fd = _dbus_connect_tcp_socket (host, port, result);
1163 _dbus_fd_set_close_on_exec (fd);
1165 _dbus_verbose ("Successfully connected to tcp socket %s:%d\n",
1168 transport = _dbus_transport_new_for_fd (fd, server);
1169 if (transport == NULL)
1171 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
1172 _dbus_close (fd, NULL);