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_outgoing; /**< Encoded version of current
67 DBusString encoded_incoming; /**< Encoded version of current
73 free_watches (DBusTransport *transport)
75 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
77 if (unix_transport->read_watch)
79 if (transport->connection)
80 _dbus_connection_remove_watch (transport->connection,
81 unix_transport->read_watch);
82 _dbus_watch_invalidate (unix_transport->read_watch);
83 _dbus_watch_unref (unix_transport->read_watch);
84 unix_transport->read_watch = NULL;
87 if (unix_transport->write_watch)
89 if (transport->connection)
90 _dbus_connection_remove_watch (transport->connection,
91 unix_transport->write_watch);
92 _dbus_watch_invalidate (unix_transport->write_watch);
93 _dbus_watch_unref (unix_transport->write_watch);
94 unix_transport->write_watch = NULL;
99 unix_finalize (DBusTransport *transport)
101 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
103 free_watches (transport);
105 _dbus_string_free (&unix_transport->encoded_outgoing);
106 _dbus_string_free (&unix_transport->encoded_incoming);
108 _dbus_transport_finalize_base (transport);
110 _dbus_assert (unix_transport->read_watch == NULL);
111 _dbus_assert (unix_transport->write_watch == NULL);
113 dbus_free (transport);
117 check_write_watch (DBusTransport *transport)
119 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
120 dbus_bool_t need_write_watch;
122 if (transport->connection == NULL)
125 if (transport->disconnected)
127 _dbus_assert (unix_transport->write_watch == NULL);
131 _dbus_transport_ref (transport);
133 if (_dbus_transport_get_is_authenticated (transport))
134 need_write_watch = transport->messages_need_sending;
136 need_write_watch = transport->send_credentials_pending ||
137 _dbus_auth_do_work (transport->auth) == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
139 _dbus_connection_toggle_watch (transport->connection,
140 unix_transport->write_watch,
143 _dbus_transport_unref (transport);
147 check_read_watch (DBusTransport *transport)
149 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
150 dbus_bool_t need_read_watch;
152 if (transport->connection == NULL)
155 if (transport->disconnected)
157 _dbus_assert (unix_transport->read_watch == NULL);
161 _dbus_transport_ref (transport);
163 if (_dbus_transport_get_is_authenticated (transport))
165 _dbus_counter_get_value (transport->live_messages_size) < transport->max_live_messages_size;
167 need_read_watch = transport->receive_credentials_pending ||
168 _dbus_auth_do_work (transport->auth) == DBUS_AUTH_STATE_WAITING_FOR_INPUT;
170 _dbus_connection_toggle_watch (transport->connection,
171 unix_transport->read_watch,
174 _dbus_transport_unref (transport);
178 do_io_error (DBusTransport *transport)
180 _dbus_transport_ref (transport);
181 _dbus_transport_disconnect (transport);
182 _dbus_transport_unref (transport);
185 /* return value is whether we successfully read any new data. */
187 read_data_into_auth (DBusTransport *transport,
190 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
196 _dbus_auth_get_buffer (transport->auth, &buffer);
198 bytes_read = _dbus_read (unix_transport->fd,
199 buffer, unix_transport->max_bytes_read_per_iteration);
201 _dbus_auth_return_buffer (transport->auth, buffer,
202 bytes_read > 0 ? bytes_read : 0);
206 _dbus_verbose (" read %d bytes in auth phase\n", bytes_read);
210 else if (bytes_read < 0)
212 /* EINTR already handled for us */
218 else if (errno == EAGAIN ||
219 errno == EWOULDBLOCK)
220 ; /* do nothing, just return FALSE below */
223 _dbus_verbose ("Error reading from remote app: %s\n",
224 _dbus_strerror (errno));
225 do_io_error (transport);
232 _dbus_assert (bytes_read == 0);
234 _dbus_verbose ("Disconnected from remote app\n");
235 do_io_error (transport);
241 /* Return value is whether we successfully wrote any bytes */
243 write_data_from_auth (DBusTransport *transport)
245 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
247 const DBusString *buffer;
249 if (!_dbus_auth_get_bytes_to_send (transport->auth,
253 bytes_written = _dbus_write (unix_transport->fd,
255 0, _dbus_string_get_length (buffer));
257 if (bytes_written > 0)
259 _dbus_auth_bytes_sent (transport->auth, bytes_written);
262 else if (bytes_written < 0)
264 /* EINTR already handled for us */
266 if (errno == EAGAIN ||
267 errno == EWOULDBLOCK)
271 _dbus_verbose ("Error writing to remote app: %s\n",
272 _dbus_strerror (errno));
273 do_io_error (transport);
281 exchange_credentials (DBusTransport *transport,
282 dbus_bool_t do_reading,
283 dbus_bool_t do_writing)
285 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
287 if (do_writing && transport->send_credentials_pending)
289 if (_dbus_send_credentials_unix_socket (unix_transport->fd,
292 transport->send_credentials_pending = FALSE;
296 _dbus_verbose ("Failed to write credentials\n");
297 do_io_error (transport);
301 if (do_reading && transport->receive_credentials_pending)
303 if (_dbus_read_credentials_unix_socket (unix_transport->fd,
304 &transport->credentials,
307 transport->receive_credentials_pending = FALSE;
311 _dbus_verbose ("Failed to read credentials\n");
312 do_io_error (transport);
316 if (!(transport->send_credentials_pending ||
317 transport->receive_credentials_pending))
319 _dbus_auth_set_credentials (transport->auth,
320 &transport->credentials);
325 do_authentication (DBusTransport *transport,
326 dbus_bool_t do_reading,
327 dbus_bool_t do_writing)
331 _dbus_transport_ref (transport);
335 while (!_dbus_transport_get_is_authenticated (transport) &&
336 _dbus_transport_get_is_connected (transport))
338 exchange_credentials (transport, do_reading, do_writing);
340 if (transport->send_credentials_pending ||
341 transport->receive_credentials_pending)
343 _dbus_verbose ("send_credentials_pending = %d receive_credentials_pending = %d\n",
344 transport->send_credentials_pending,
345 transport->receive_credentials_pending);
349 #define TRANSPORT_SIDE(t) ((t)->is_server ? "server" : "client")
350 switch (_dbus_auth_do_work (transport->auth))
352 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
353 _dbus_verbose (" %s auth state: waiting for input\n",
354 TRANSPORT_SIDE (transport));
355 if (!do_reading || !read_data_into_auth (transport, &oom))
359 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
360 _dbus_verbose (" %s auth state: waiting for memory\n",
361 TRANSPORT_SIDE (transport));
366 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
367 _dbus_verbose (" %s auth state: bytes to send\n",
368 TRANSPORT_SIDE (transport));
369 if (!do_writing || !write_data_from_auth (transport))
373 case DBUS_AUTH_STATE_NEED_DISCONNECT:
374 _dbus_verbose (" %s auth state: need to disconnect\n",
375 TRANSPORT_SIDE (transport));
376 do_io_error (transport);
379 case DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES:
380 _dbus_verbose (" %s auth state: auth with unused bytes\n",
381 TRANSPORT_SIDE (transport));
382 /* We'll recover the unused bytes in dbus-transport.c */
386 case DBUS_AUTH_STATE_AUTHENTICATED:
387 _dbus_verbose (" %s auth state: authenticated\n",
388 TRANSPORT_SIDE (transport));
394 check_read_watch (transport);
395 check_write_watch (transport);
396 _dbus_transport_unref (transport);
404 /* returns false on oom */
406 do_writing (DBusTransport *transport)
409 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
412 /* No messages without authentication! */
413 if (!_dbus_transport_get_is_authenticated (transport))
415 _dbus_verbose ("Not authenticated, not writing anything\n");
419 if (transport->disconnected)
421 _dbus_verbose ("Not connected, not writing anything\n");
426 _dbus_verbose ("do_writing(), have_messages = %d\n",
427 _dbus_connection_have_messages_to_send (transport->connection));
433 while (!transport->disconnected &&
434 _dbus_connection_have_messages_to_send (transport->connection))
437 DBusMessage *message;
438 const DBusString *header;
439 const DBusString *body;
440 int header_len, body_len;
441 int total_bytes_to_write;
443 if (total > unix_transport->max_bytes_written_per_iteration)
445 _dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n",
446 total, unix_transport->max_bytes_written_per_iteration);
450 if (!dbus_watch_get_enabled (unix_transport->write_watch))
452 _dbus_verbose ("write watch disabled, not writing more stuff\n");
456 message = _dbus_connection_get_message_to_send (transport->connection);
457 _dbus_assert (message != NULL);
458 _dbus_message_lock (message);
461 _dbus_verbose ("writing message %p\n", message);
464 _dbus_message_get_network_data (message,
467 header_len = _dbus_string_get_length (header);
468 body_len = _dbus_string_get_length (body);
470 if (_dbus_auth_needs_encoding (transport->auth))
472 if (_dbus_string_get_length (&unix_transport->encoded_outgoing) == 0)
474 if (!_dbus_auth_encode_data (transport->auth,
475 header, &unix_transport->encoded_outgoing))
481 if (!_dbus_auth_encode_data (transport->auth,
482 body, &unix_transport->encoded_outgoing))
484 _dbus_string_set_length (&unix_transport->encoded_outgoing, 0);
490 total_bytes_to_write = _dbus_string_get_length (&unix_transport->encoded_outgoing);
493 _dbus_verbose ("encoded message is %d bytes\n",
494 total_bytes_to_write);
498 _dbus_write (unix_transport->fd,
499 &unix_transport->encoded_outgoing,
500 unix_transport->message_bytes_written,
501 total_bytes_to_write - unix_transport->message_bytes_written);
505 total_bytes_to_write = header_len + body_len;
508 _dbus_verbose ("message is %d bytes\n",
509 total_bytes_to_write);
512 if (unix_transport->message_bytes_written < header_len)
515 _dbus_write_two (unix_transport->fd,
517 unix_transport->message_bytes_written,
518 header_len - unix_transport->message_bytes_written,
525 _dbus_write (unix_transport->fd,
527 (unix_transport->message_bytes_written - header_len),
529 (unix_transport->message_bytes_written - header_len));
533 if (bytes_written < 0)
535 /* EINTR already handled for us */
537 if (errno == EAGAIN ||
538 errno == EWOULDBLOCK)
542 _dbus_verbose ("Error writing to remote app: %s\n",
543 _dbus_strerror (errno));
544 do_io_error (transport);
550 _dbus_verbose (" wrote %d bytes of %d\n", bytes_written,
551 total_bytes_to_write);
553 total += bytes_written;
554 unix_transport->message_bytes_written += bytes_written;
556 _dbus_assert (unix_transport->message_bytes_written <=
557 total_bytes_to_write);
559 if (unix_transport->message_bytes_written == total_bytes_to_write)
561 unix_transport->message_bytes_written = 0;
562 _dbus_string_set_length (&unix_transport->encoded_outgoing, 0);
564 _dbus_connection_message_sent (transport->connection,
577 /* returns false on out-of-memory */
579 do_reading (DBusTransport *transport)
581 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
587 /* No messages without authentication! */
588 if (!_dbus_transport_get_is_authenticated (transport))
597 /* See if we've exceeded max messages and need to disable reading */
598 check_read_watch (transport);
599 if (!dbus_watch_get_enabled (unix_transport->read_watch))
602 if (total > unix_transport->max_bytes_read_per_iteration)
604 _dbus_verbose ("%d bytes exceeds %d bytes read per iteration, returning\n",
605 total, unix_transport->max_bytes_read_per_iteration);
609 if (transport->disconnected)
612 if (_dbus_auth_needs_decoding (transport->auth))
614 if (_dbus_string_get_length (&unix_transport->encoded_incoming) > 0)
615 bytes_read = _dbus_string_get_length (&unix_transport->encoded_incoming);
617 bytes_read = _dbus_read (unix_transport->fd,
618 &unix_transport->encoded_incoming,
619 unix_transport->max_bytes_read_per_iteration);
621 _dbus_assert (_dbus_string_get_length (&unix_transport->encoded_incoming) ==
628 _dbus_message_loader_get_buffer (transport->loader,
631 orig_len = _dbus_string_get_length (buffer);
633 if (!_dbus_auth_decode_data (transport->auth,
634 &unix_transport->encoded_incoming,
637 _dbus_verbose ("Out of memory decoding incoming data\n");
642 _dbus_message_loader_return_buffer (transport->loader,
644 _dbus_string_get_length (buffer) - orig_len);
646 _dbus_string_set_length (&unix_transport->encoded_incoming, 0);
651 _dbus_message_loader_get_buffer (transport->loader,
654 bytes_read = _dbus_read (unix_transport->fd,
655 buffer, unix_transport->max_bytes_read_per_iteration);
657 _dbus_message_loader_return_buffer (transport->loader,
659 bytes_read < 0 ? 0 : bytes_read);
664 /* EINTR already handled for us */
668 _dbus_verbose ("Out of memory in read()/do_reading()\n");
672 else if (errno == EAGAIN ||
673 errno == EWOULDBLOCK)
677 _dbus_verbose ("Error reading from remote app: %s\n",
678 _dbus_strerror (errno));
679 do_io_error (transport);
683 else if (bytes_read == 0)
685 _dbus_verbose ("Disconnected from remote app\n");
686 do_io_error (transport);
691 _dbus_verbose (" read %d bytes\n", bytes_read);
695 if (_dbus_transport_queue_messages (transport) == DBUS_DISPATCH_NEED_MEMORY)
698 _dbus_verbose (" out of memory when queueing messages we just read in the transport\n");
702 /* Try reading more data until we get EAGAIN and return, or
703 * exceed max bytes per iteration. If in blocking mode of
704 * course we'll block instead of returning.
717 unix_handle_watch (DBusTransport *transport,
721 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
723 _dbus_assert (watch == unix_transport->read_watch ||
724 watch == unix_transport->write_watch);
726 if (flags & (DBUS_WATCH_HANGUP | DBUS_WATCH_ERROR))
728 _dbus_transport_disconnect (transport);
732 if (watch == unix_transport->read_watch &&
733 (flags & DBUS_WATCH_READABLE))
736 _dbus_verbose ("handling read watch\n");
738 if (!do_authentication (transport, TRUE, FALSE))
741 if (!do_reading (transport))
743 _dbus_verbose ("no memory to read\n");
747 else if (watch == unix_transport->write_watch &&
748 (flags & DBUS_WATCH_WRITABLE))
751 _dbus_verbose ("handling write watch, messages_need_sending = %d\n",
752 transport->messages_need_sending);
754 if (!do_authentication (transport, FALSE, TRUE))
757 if (!do_writing (transport))
759 _dbus_verbose ("no memory to write\n");
763 #ifdef DBUS_ENABLE_VERBOSE_MODE
766 if (watch == unix_transport->read_watch)
767 _dbus_verbose ("asked to handle read watch with non-read condition 0x%x\n",
769 else if (watch == unix_transport->write_watch)
770 _dbus_verbose ("asked to handle write watch with non-write condition 0x%x\n",
773 _dbus_verbose ("asked to handle watch %p on fd %d that we don't recognize\n",
774 watch, dbus_watch_get_fd (watch));
776 #endif /* DBUS_ENABLE_VERBOSE_MODE */
782 unix_disconnect (DBusTransport *transport)
784 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
786 free_watches (transport);
788 _dbus_close (unix_transport->fd, NULL);
789 unix_transport->fd = -1;
793 unix_connection_set (DBusTransport *transport)
795 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
797 _dbus_watch_set_handler (unix_transport->write_watch,
798 _dbus_connection_handle_watch,
799 transport->connection, NULL);
801 _dbus_watch_set_handler (unix_transport->read_watch,
802 _dbus_connection_handle_watch,
803 transport->connection, NULL);
805 if (!_dbus_connection_add_watch (transport->connection,
806 unix_transport->write_watch))
809 if (!_dbus_connection_add_watch (transport->connection,
810 unix_transport->read_watch))
812 _dbus_connection_remove_watch (transport->connection,
813 unix_transport->write_watch);
817 check_read_watch (transport);
818 check_write_watch (transport);
824 unix_messages_pending (DBusTransport *transport,
825 int messages_pending)
827 check_write_watch (transport);
830 /* FIXME use _dbus_poll(), not select() */
832 * @todo We need to have a way to wake up the select sleep if
833 * a new iteration request comes in with a flag (read/write) that
834 * we're not currently serving. Otherwise a call that just reads
835 * could block a write call forever (if there are no incoming
839 unix_do_iteration (DBusTransport *transport,
841 int timeout_milliseconds)
843 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
846 dbus_bool_t do_select;
849 _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p\n",
850 flags & DBUS_ITERATION_DO_READING ? "read" : "",
851 flags & DBUS_ITERATION_DO_WRITING ? "write" : "",
852 timeout_milliseconds,
853 unix_transport->read_watch,
854 unix_transport->write_watch);
856 /* "again" has to be up here because on EINTR the fd sets become
863 /* the passed in DO_READING/DO_WRITING flags indicate whether to
864 * read/write messages, but regardless of those we may need to block
865 * for reading/writing to do auth. But if we do reading for auth,
866 * we don't want to read any messages yet if not given DO_READING.
868 * Also, if read_watch == NULL or write_watch == NULL, we don't
869 * want to read/write so don't.
873 FD_ZERO (&write_set);
875 if (_dbus_transport_get_is_authenticated (transport))
877 if (unix_transport->read_watch &&
878 (flags & DBUS_ITERATION_DO_READING))
880 FD_SET (unix_transport->fd, &read_set);
884 if (unix_transport->write_watch &&
885 (flags & DBUS_ITERATION_DO_WRITING))
887 FD_SET (unix_transport->fd, &write_set);
893 DBusAuthState auth_state;
895 auth_state = _dbus_auth_do_work (transport->auth);
897 if (transport->receive_credentials_pending ||
898 auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
900 FD_SET (unix_transport->fd, &read_set);
904 if (transport->send_credentials_pending ||
905 auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
907 FD_SET (unix_transport->fd, &write_set);
915 struct timeval timeout;
916 dbus_bool_t use_timeout;
919 FD_SET (unix_transport->fd, &err_set);
921 if (flags & DBUS_ITERATION_BLOCK)
923 if (timeout_milliseconds >= 0)
925 timeout.tv_sec = timeout_milliseconds / 1000;
926 timeout.tv_usec = (timeout_milliseconds % 1000) * 1000;
928 /* Always use timeout if one is passed in. */
933 use_timeout = FALSE; /* NULL timeout to block forever */
938 /* 0 timeout to not block */
944 /* For blocking selects we drop the connection lock here
945 * to avoid blocking out connection access during a potentially
946 * indefinite blocking call. The io path is still protected
947 * by the io_path_cond condvar, so we won't reenter this.
949 if (flags & DBUS_ITERATION_BLOCK)
950 _dbus_connection_unlock (transport->connection);
952 select_res = select (unix_transport->fd + 1,
953 &read_set, &write_set, &err_set,
954 use_timeout ? &timeout : NULL);
956 if (flags & DBUS_ITERATION_BLOCK)
957 _dbus_connection_lock (transport->connection);
962 if (FD_ISSET (unix_transport->fd, &err_set))
963 do_io_error (transport);
966 dbus_bool_t need_read = FD_ISSET (unix_transport->fd, &read_set);
967 dbus_bool_t need_write = FD_ISSET (unix_transport->fd, &write_set);
969 _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
970 need_read, need_write);
971 do_authentication (transport, need_read, need_write);
973 if (need_read && (flags & DBUS_ITERATION_DO_READING))
974 do_reading (transport);
975 if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
976 do_writing (transport);
979 else if (errno == EINTR)
983 _dbus_verbose ("Error from select(): %s\n",
984 _dbus_strerror (errno));
990 unix_live_messages_changed (DBusTransport *transport)
992 /* See if we should look for incoming messages again */
993 check_read_watch (transport);
996 static DBusTransportVTable unix_vtable = {
1000 unix_connection_set,
1001 unix_messages_pending,
1003 unix_live_messages_changed
1007 * Creates a new transport for the given file descriptor. The file
1008 * descriptor must be nonblocking (use _dbus_set_fd_nonblocking() to
1009 * make it so). This function is shared by various transports that
1010 * boil down to a full duplex file descriptor.
1012 * @param fd the file descriptor.
1013 * @param server #TRUE if this transport is on the server side of a connection
1014 * @param address the transport's address
1015 * @returns the new transport, or #NULL if no memory.
1018 _dbus_transport_new_for_fd (int fd,
1020 const DBusString *address)
1022 DBusTransportUnix *unix_transport;
1024 unix_transport = dbus_new0 (DBusTransportUnix, 1);
1025 if (unix_transport == NULL)
1028 if (!_dbus_string_init (&unix_transport->encoded_outgoing))
1031 if (!_dbus_string_init (&unix_transport->encoded_incoming))
1034 unix_transport->write_watch = _dbus_watch_new (fd,
1035 DBUS_WATCH_WRITABLE,
1038 if (unix_transport->write_watch == NULL)
1041 unix_transport->read_watch = _dbus_watch_new (fd,
1042 DBUS_WATCH_READABLE,
1045 if (unix_transport->read_watch == NULL)
1048 if (!_dbus_transport_init_base (&unix_transport->base,
1053 unix_transport->fd = fd;
1054 unix_transport->message_bytes_written = 0;
1056 /* These values should probably be tunable or something. */
1057 unix_transport->max_bytes_read_per_iteration = 2048;
1058 unix_transport->max_bytes_written_per_iteration = 2048;
1060 return (DBusTransport*) unix_transport;
1063 _dbus_watch_unref (unix_transport->read_watch);
1065 _dbus_watch_unref (unix_transport->write_watch);
1067 _dbus_string_free (&unix_transport->encoded_incoming);
1069 _dbus_string_free (&unix_transport->encoded_outgoing);
1071 dbus_free (unix_transport);
1076 * Creates a new transport for the given Unix domain socket
1077 * path. This creates a client-side of a transport.
1079 * @param path the path to the domain socket.
1080 * @param error address where an error can be returned.
1081 * @returns a new transport, or #NULL on failure.
1084 _dbus_transport_new_for_domain_socket (const char *path,
1088 DBusTransport *transport;
1091 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1093 if (!_dbus_string_init (&address))
1095 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1101 if (!_dbus_string_append (&address, "unix:path=") ||
1102 !_dbus_string_append (&address, path))
1104 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1108 fd = _dbus_connect_unix_socket (path, error);
1111 _DBUS_ASSERT_ERROR_IS_SET (error);
1115 _dbus_fd_set_close_on_exec (fd);
1117 _dbus_verbose ("Successfully connected to unix socket %s\n",
1120 transport = _dbus_transport_new_for_fd (fd, FALSE, &address);
1121 if (transport == NULL)
1123 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1127 _dbus_string_free (&address);
1132 _dbus_close (fd, NULL);
1134 _dbus_string_free (&address);
1139 * Creates a new transport for the given hostname and port.
1141 * @param host the host to connect to
1142 * @param port the port to connect to
1143 * @param error location to store reason for failure.
1144 * @returns a new transport, or #NULL on failure.
1147 _dbus_transport_new_for_tcp_socket (const char *host,
1152 DBusTransport *transport;
1155 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1157 if (!_dbus_string_init (&address))
1159 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1163 if (!_dbus_string_append (&address, "tcp:host=") ||
1164 !_dbus_string_append (&address, host) ||
1165 !_dbus_string_append (&address, ",port=") ||
1166 !_dbus_string_append_int (&address, port))
1168 _dbus_string_free (&address);
1169 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1173 fd = _dbus_connect_tcp_socket (host, port, error);
1176 _DBUS_ASSERT_ERROR_IS_SET (error);
1177 _dbus_string_free (&address);
1181 _dbus_fd_set_close_on_exec (fd);
1183 _dbus_verbose ("Successfully connected to tcp socket %s:%d\n",
1186 transport = _dbus_transport_new_for_fd (fd, FALSE, &address);
1187 if (transport == NULL)
1189 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1190 _dbus_close (fd, NULL);
1191 _dbus_string_free (&address);
1195 _dbus_string_free (&address);