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);
600 if (total > unix_transport->max_bytes_read_per_iteration)
602 _dbus_verbose ("%d bytes exceeds %d bytes read per iteration, returning\n",
603 total, unix_transport->max_bytes_read_per_iteration);
607 _dbus_assert (unix_transport->read_watch != NULL ||
608 transport->disconnected);
610 if (transport->disconnected)
613 if (!dbus_watch_get_enabled (unix_transport->read_watch))
616 if (_dbus_auth_needs_decoding (transport->auth))
618 if (_dbus_string_get_length (&unix_transport->encoded_incoming) > 0)
619 bytes_read = _dbus_string_get_length (&unix_transport->encoded_incoming);
621 bytes_read = _dbus_read (unix_transport->fd,
622 &unix_transport->encoded_incoming,
623 unix_transport->max_bytes_read_per_iteration);
625 _dbus_assert (_dbus_string_get_length (&unix_transport->encoded_incoming) ==
632 _dbus_message_loader_get_buffer (transport->loader,
635 orig_len = _dbus_string_get_length (buffer);
637 if (!_dbus_auth_decode_data (transport->auth,
638 &unix_transport->encoded_incoming,
641 _dbus_verbose ("Out of memory decoding incoming data\n");
646 _dbus_message_loader_return_buffer (transport->loader,
648 _dbus_string_get_length (buffer) - orig_len);
650 _dbus_string_set_length (&unix_transport->encoded_incoming, 0);
655 _dbus_message_loader_get_buffer (transport->loader,
658 bytes_read = _dbus_read (unix_transport->fd,
659 buffer, unix_transport->max_bytes_read_per_iteration);
661 _dbus_message_loader_return_buffer (transport->loader,
663 bytes_read < 0 ? 0 : bytes_read);
668 /* EINTR already handled for us */
672 _dbus_verbose ("Out of memory in read()/do_reading()\n");
676 else if (errno == EAGAIN ||
677 errno == EWOULDBLOCK)
681 _dbus_verbose ("Error reading from remote app: %s\n",
682 _dbus_strerror (errno));
683 do_io_error (transport);
687 else if (bytes_read == 0)
689 _dbus_verbose ("Disconnected from remote app\n");
690 do_io_error (transport);
695 _dbus_verbose (" read %d bytes\n", bytes_read);
699 if (_dbus_transport_queue_messages (transport) == DBUS_DISPATCH_NEED_MEMORY)
702 _dbus_verbose (" out of memory when queueing messages we just read in the transport\n");
706 /* Try reading more data until we get EAGAIN and return, or
707 * exceed max bytes per iteration. If in blocking mode of
708 * course we'll block instead of returning.
721 unix_handle_watch (DBusTransport *transport,
725 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
727 _dbus_assert (watch == unix_transport->read_watch ||
728 watch == unix_transport->write_watch);
730 if (flags & (DBUS_WATCH_HANGUP | DBUS_WATCH_ERROR))
732 _dbus_transport_disconnect (transport);
736 if (watch == unix_transport->read_watch &&
737 (flags & DBUS_WATCH_READABLE))
740 _dbus_verbose ("handling read watch\n");
742 if (!do_authentication (transport, TRUE, FALSE))
745 if (!do_reading (transport))
747 _dbus_verbose ("no memory to read\n");
751 else if (watch == unix_transport->write_watch &&
752 (flags & DBUS_WATCH_WRITABLE))
755 _dbus_verbose ("handling write watch, messages_need_sending = %d\n",
756 transport->messages_need_sending);
758 if (!do_authentication (transport, FALSE, TRUE))
761 if (!do_writing (transport))
763 _dbus_verbose ("no memory to write\n");
767 #ifdef DBUS_ENABLE_VERBOSE_MODE
770 if (watch == unix_transport->read_watch)
771 _dbus_verbose ("asked to handle read watch with non-read condition 0x%x\n",
773 else if (watch == unix_transport->write_watch)
774 _dbus_verbose ("asked to handle write watch with non-write condition 0x%x\n",
777 _dbus_verbose ("asked to handle watch %p on fd %d that we don't recognize\n",
778 watch, dbus_watch_get_fd (watch));
780 #endif /* DBUS_ENABLE_VERBOSE_MODE */
786 unix_disconnect (DBusTransport *transport)
788 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
790 free_watches (transport);
792 _dbus_close (unix_transport->fd, NULL);
793 unix_transport->fd = -1;
797 unix_connection_set (DBusTransport *transport)
799 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
801 _dbus_watch_set_handler (unix_transport->write_watch,
802 _dbus_connection_handle_watch,
803 transport->connection, NULL);
805 _dbus_watch_set_handler (unix_transport->read_watch,
806 _dbus_connection_handle_watch,
807 transport->connection, NULL);
809 if (!_dbus_connection_add_watch (transport->connection,
810 unix_transport->write_watch))
813 if (!_dbus_connection_add_watch (transport->connection,
814 unix_transport->read_watch))
816 _dbus_connection_remove_watch (transport->connection,
817 unix_transport->write_watch);
821 check_read_watch (transport);
822 check_write_watch (transport);
828 unix_messages_pending (DBusTransport *transport,
829 int messages_pending)
831 check_write_watch (transport);
834 /* FIXME use _dbus_poll(), not select() */
836 * @todo We need to have a way to wake up the select sleep if
837 * a new iteration request comes in with a flag (read/write) that
838 * we're not currently serving. Otherwise a call that just reads
839 * could block a write call forever (if there are no incoming
843 unix_do_iteration (DBusTransport *transport,
845 int timeout_milliseconds)
847 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
850 dbus_bool_t do_select;
853 _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p\n",
854 flags & DBUS_ITERATION_DO_READING ? "read" : "",
855 flags & DBUS_ITERATION_DO_WRITING ? "write" : "",
856 timeout_milliseconds,
857 unix_transport->read_watch,
858 unix_transport->write_watch);
860 /* "again" has to be up here because on EINTR the fd sets become
867 /* the passed in DO_READING/DO_WRITING flags indicate whether to
868 * read/write messages, but regardless of those we may need to block
869 * for reading/writing to do auth. But if we do reading for auth,
870 * we don't want to read any messages yet if not given DO_READING.
872 * Also, if read_watch == NULL or write_watch == NULL, we don't
873 * want to read/write so don't.
877 FD_ZERO (&write_set);
879 if (_dbus_transport_get_is_authenticated (transport))
881 if (unix_transport->read_watch &&
882 (flags & DBUS_ITERATION_DO_READING))
884 FD_SET (unix_transport->fd, &read_set);
888 if (unix_transport->write_watch &&
889 (flags & DBUS_ITERATION_DO_WRITING))
891 FD_SET (unix_transport->fd, &write_set);
897 DBusAuthState auth_state;
899 auth_state = _dbus_auth_do_work (transport->auth);
901 if (transport->receive_credentials_pending ||
902 auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
904 FD_SET (unix_transport->fd, &read_set);
908 if (transport->send_credentials_pending ||
909 auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
911 FD_SET (unix_transport->fd, &write_set);
919 struct timeval timeout;
920 dbus_bool_t use_timeout;
923 FD_SET (unix_transport->fd, &err_set);
925 if (flags & DBUS_ITERATION_BLOCK)
927 if (timeout_milliseconds >= 0)
929 timeout.tv_sec = timeout_milliseconds / 1000;
930 timeout.tv_usec = (timeout_milliseconds % 1000) * 1000;
932 /* Always use timeout if one is passed in. */
937 use_timeout = FALSE; /* NULL timeout to block forever */
942 /* 0 timeout to not block */
948 /* For blocking selects we drop the connection lock here
949 * to avoid blocking out connection access during a potentially
950 * indefinite blocking call. The io path is still protected
951 * by the io_path_cond condvar, so we won't reenter this.
953 if (flags & DBUS_ITERATION_BLOCK)
954 _dbus_connection_unlock (transport->connection);
956 select_res = select (unix_transport->fd + 1,
957 &read_set, &write_set, &err_set,
958 use_timeout ? &timeout : NULL);
960 if (flags & DBUS_ITERATION_BLOCK)
961 _dbus_connection_lock (transport->connection);
966 if (FD_ISSET (unix_transport->fd, &err_set))
967 do_io_error (transport);
970 dbus_bool_t need_read = FD_ISSET (unix_transport->fd, &read_set);
971 dbus_bool_t need_write = FD_ISSET (unix_transport->fd, &write_set);
973 _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
974 need_read, need_write);
975 do_authentication (transport, need_read, need_write);
977 if (need_read && (flags & DBUS_ITERATION_DO_READING))
978 do_reading (transport);
979 if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
980 do_writing (transport);
983 else if (errno == EINTR)
987 _dbus_verbose ("Error from select(): %s\n",
988 _dbus_strerror (errno));
994 unix_live_messages_changed (DBusTransport *transport)
996 /* See if we should look for incoming messages again */
997 check_read_watch (transport);
1000 static DBusTransportVTable unix_vtable = {
1004 unix_connection_set,
1005 unix_messages_pending,
1007 unix_live_messages_changed
1011 * Creates a new transport for the given file descriptor. The file
1012 * descriptor must be nonblocking (use _dbus_set_fd_nonblocking() to
1013 * make it so). This function is shared by various transports that
1014 * boil down to a full duplex file descriptor.
1016 * @param fd the file descriptor.
1017 * @param server #TRUE if this transport is on the server side of a connection
1018 * @param address the transport's address
1019 * @returns the new transport, or #NULL if no memory.
1022 _dbus_transport_new_for_fd (int fd,
1024 const DBusString *address)
1026 DBusTransportUnix *unix_transport;
1028 unix_transport = dbus_new0 (DBusTransportUnix, 1);
1029 if (unix_transport == NULL)
1032 if (!_dbus_string_init (&unix_transport->encoded_outgoing))
1035 if (!_dbus_string_init (&unix_transport->encoded_incoming))
1038 unix_transport->write_watch = _dbus_watch_new (fd,
1039 DBUS_WATCH_WRITABLE,
1042 if (unix_transport->write_watch == NULL)
1045 unix_transport->read_watch = _dbus_watch_new (fd,
1046 DBUS_WATCH_READABLE,
1049 if (unix_transport->read_watch == NULL)
1052 if (!_dbus_transport_init_base (&unix_transport->base,
1057 unix_transport->fd = fd;
1058 unix_transport->message_bytes_written = 0;
1060 /* These values should probably be tunable or something. */
1061 unix_transport->max_bytes_read_per_iteration = 2048;
1062 unix_transport->max_bytes_written_per_iteration = 2048;
1064 return (DBusTransport*) unix_transport;
1067 _dbus_watch_unref (unix_transport->read_watch);
1069 _dbus_watch_unref (unix_transport->write_watch);
1071 _dbus_string_free (&unix_transport->encoded_incoming);
1073 _dbus_string_free (&unix_transport->encoded_outgoing);
1075 dbus_free (unix_transport);
1080 * Creates a new transport for the given Unix domain socket
1081 * path. This creates a client-side of a transport.
1083 * @todo once we add a way to escape paths in a dbus
1084 * address, this function needs to do escaping.
1086 * @param path the path to the domain socket.
1087 * @param abstract #TRUE to use abstract socket namespace
1088 * @param error address where an error can be returned.
1089 * @returns a new transport, or #NULL on failure.
1092 _dbus_transport_new_for_domain_socket (const char *path,
1093 dbus_bool_t abstract,
1097 DBusTransport *transport;
1100 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1102 if (!_dbus_string_init (&address))
1104 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1111 !_dbus_string_append (&address, "unix:abstract=")) ||
1113 !_dbus_string_append (&address, "unix:path=")) ||
1114 !_dbus_string_append (&address, path))
1116 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1120 fd = _dbus_connect_unix_socket (path, abstract, error);
1123 _DBUS_ASSERT_ERROR_IS_SET (error);
1127 _dbus_fd_set_close_on_exec (fd);
1129 _dbus_verbose ("Successfully connected to unix socket %s\n",
1132 transport = _dbus_transport_new_for_fd (fd, FALSE, &address);
1133 if (transport == NULL)
1135 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1139 _dbus_string_free (&address);
1144 _dbus_close (fd, NULL);
1146 _dbus_string_free (&address);
1151 * Creates a new transport for the given hostname and port.
1153 * @param host the host to connect to
1154 * @param port the port to connect to
1155 * @param error location to store reason for failure.
1156 * @returns a new transport, or #NULL on failure.
1159 _dbus_transport_new_for_tcp_socket (const char *host,
1164 DBusTransport *transport;
1167 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1169 if (!_dbus_string_init (&address))
1171 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1175 if (!_dbus_string_append (&address, "tcp:host=") ||
1176 !_dbus_string_append (&address, host) ||
1177 !_dbus_string_append (&address, ",port=") ||
1178 !_dbus_string_append_int (&address, port))
1180 _dbus_string_free (&address);
1181 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1185 fd = _dbus_connect_tcp_socket (host, port, error);
1188 _DBUS_ASSERT_ERROR_IS_SET (error);
1189 _dbus_string_free (&address);
1193 _dbus_fd_set_close_on_exec (fd);
1195 _dbus_verbose ("Successfully connected to tcp socket %s:%d\n",
1198 transport = _dbus_transport_new_for_fd (fd, FALSE, &address);
1199 if (transport == NULL)
1201 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1202 _dbus_close (fd, NULL);
1203 _dbus_string_free (&address);
1207 _dbus_string_free (&address);