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 2.0
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"
32 * @defgroup DBusTransportUnix DBusTransport implementations for UNIX
33 * @ingroup DBusInternals
34 * @brief Implementation details of DBusTransport on UNIX
40 * Opaque object representing a Unix file descriptor transport.
42 typedef struct DBusTransportUnix DBusTransportUnix;
45 * Implementation details of DBusTransportUnix. All members are private.
47 struct DBusTransportUnix
49 DBusTransport base; /**< Parent instance */
50 int fd; /**< File descriptor. */
51 DBusWatch *read_watch; /**< Watch for readability. */
52 DBusWatch *write_watch; /**< Watch for writability. */
54 int max_bytes_read_per_iteration; /**< To avoid blocking too long. */
55 int max_bytes_written_per_iteration; /**< To avoid blocking too long. */
57 int message_bytes_written; /**< Number of bytes of current
58 * outgoing message that have
61 DBusString encoded_outgoing; /**< Encoded version of current
64 DBusString encoded_incoming; /**< 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_outgoing);
103 _dbus_string_free (&unix_transport->encoded_incoming);
105 _dbus_transport_finalize_base (transport);
107 _dbus_assert (unix_transport->read_watch == NULL);
108 _dbus_assert (unix_transport->write_watch == NULL);
110 dbus_free (transport);
114 check_write_watch (DBusTransport *transport)
116 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
117 dbus_bool_t need_write_watch;
119 if (transport->connection == NULL)
122 if (transport->disconnected)
124 _dbus_assert (unix_transport->write_watch == NULL);
128 _dbus_transport_ref (transport);
130 if (_dbus_transport_get_is_authenticated (transport))
131 need_write_watch = transport->messages_need_sending;
133 need_write_watch = transport->send_credentials_pending ||
134 _dbus_auth_do_work (transport->auth) == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
136 _dbus_connection_toggle_watch (transport->connection,
137 unix_transport->write_watch,
140 _dbus_transport_unref (transport);
144 check_read_watch (DBusTransport *transport)
146 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
147 dbus_bool_t need_read_watch;
149 if (transport->connection == NULL)
152 if (transport->disconnected)
154 _dbus_assert (unix_transport->read_watch == NULL);
158 _dbus_transport_ref (transport);
160 if (_dbus_transport_get_is_authenticated (transport))
162 _dbus_counter_get_value (transport->live_messages_size) < transport->max_live_messages_size;
164 need_read_watch = transport->receive_credentials_pending ||
165 _dbus_auth_do_work (transport->auth) == DBUS_AUTH_STATE_WAITING_FOR_INPUT;
167 _dbus_connection_toggle_watch (transport->connection,
168 unix_transport->read_watch,
171 _dbus_transport_unref (transport);
175 do_io_error (DBusTransport *transport)
177 _dbus_transport_ref (transport);
178 _dbus_transport_disconnect (transport);
179 _dbus_transport_unref (transport);
182 /* return value is whether we successfully read any new data. */
184 read_data_into_auth (DBusTransport *transport,
187 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
193 _dbus_auth_get_buffer (transport->auth, &buffer);
195 bytes_read = _dbus_read (unix_transport->fd,
196 buffer, unix_transport->max_bytes_read_per_iteration);
198 _dbus_auth_return_buffer (transport->auth, buffer,
199 bytes_read > 0 ? bytes_read : 0);
203 _dbus_verbose (" read %d bytes in auth phase\n", bytes_read);
207 else if (bytes_read < 0)
209 /* EINTR already handled for us */
215 else if (errno == EAGAIN ||
216 errno == EWOULDBLOCK)
217 ; /* do nothing, just return FALSE below */
220 _dbus_verbose ("Error reading from remote app: %s\n",
221 _dbus_strerror (errno));
222 do_io_error (transport);
229 _dbus_assert (bytes_read == 0);
231 _dbus_verbose ("Disconnected from remote app\n");
232 do_io_error (transport);
238 /* Return value is whether we successfully wrote any bytes */
240 write_data_from_auth (DBusTransport *transport)
242 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
244 const DBusString *buffer;
246 if (!_dbus_auth_get_bytes_to_send (transport->auth,
250 bytes_written = _dbus_write (unix_transport->fd,
252 0, _dbus_string_get_length (buffer));
254 if (bytes_written > 0)
256 _dbus_auth_bytes_sent (transport->auth, bytes_written);
259 else if (bytes_written < 0)
261 /* EINTR already handled for us */
263 if (errno == EAGAIN ||
264 errno == EWOULDBLOCK)
268 _dbus_verbose ("Error writing to remote app: %s\n",
269 _dbus_strerror (errno));
270 do_io_error (transport);
278 exchange_credentials (DBusTransport *transport,
279 dbus_bool_t do_reading,
280 dbus_bool_t do_writing)
282 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
284 if (do_writing && transport->send_credentials_pending)
286 if (_dbus_send_credentials_unix_socket (unix_transport->fd,
289 transport->send_credentials_pending = FALSE;
293 _dbus_verbose ("Failed to write credentials\n");
294 do_io_error (transport);
298 if (do_reading && transport->receive_credentials_pending)
300 if (_dbus_read_credentials_unix_socket (unix_transport->fd,
301 &transport->credentials,
304 transport->receive_credentials_pending = FALSE;
308 _dbus_verbose ("Failed to read credentials\n");
309 do_io_error (transport);
313 if (!(transport->send_credentials_pending ||
314 transport->receive_credentials_pending))
316 _dbus_auth_set_credentials (transport->auth,
317 &transport->credentials);
322 do_authentication (DBusTransport *transport,
323 dbus_bool_t do_reading,
324 dbus_bool_t do_writing)
328 _dbus_transport_ref (transport);
332 while (!_dbus_transport_get_is_authenticated (transport) &&
333 _dbus_transport_get_is_connected (transport))
335 exchange_credentials (transport, do_reading, do_writing);
337 if (transport->send_credentials_pending ||
338 transport->receive_credentials_pending)
340 _dbus_verbose ("send_credentials_pending = %d receive_credentials_pending = %d\n",
341 transport->send_credentials_pending,
342 transport->receive_credentials_pending);
346 #define TRANSPORT_SIDE(t) ((t)->is_server ? "server" : "client")
347 switch (_dbus_auth_do_work (transport->auth))
349 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
350 _dbus_verbose (" %s auth state: waiting for input\n",
351 TRANSPORT_SIDE (transport));
352 if (!do_reading || !read_data_into_auth (transport, &oom))
356 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
357 _dbus_verbose (" %s auth state: waiting for memory\n",
358 TRANSPORT_SIDE (transport));
363 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
364 _dbus_verbose (" %s auth state: bytes to send\n",
365 TRANSPORT_SIDE (transport));
366 if (!do_writing || !write_data_from_auth (transport))
370 case DBUS_AUTH_STATE_NEED_DISCONNECT:
371 _dbus_verbose (" %s auth state: need to disconnect\n",
372 TRANSPORT_SIDE (transport));
373 do_io_error (transport);
376 case DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES:
377 _dbus_verbose (" %s auth state: auth with unused bytes\n",
378 TRANSPORT_SIDE (transport));
379 /* We'll recover the unused bytes in dbus-transport.c */
383 case DBUS_AUTH_STATE_AUTHENTICATED:
384 _dbus_verbose (" %s auth state: authenticated\n",
385 TRANSPORT_SIDE (transport));
391 check_read_watch (transport);
392 check_write_watch (transport);
393 _dbus_transport_unref (transport);
401 /* returns false on oom */
403 do_writing (DBusTransport *transport)
406 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
409 /* No messages without authentication! */
410 if (!_dbus_transport_get_is_authenticated (transport))
412 _dbus_verbose ("Not authenticated, not writing anything\n");
416 if (transport->disconnected)
418 _dbus_verbose ("Not connected, not writing anything\n");
423 _dbus_verbose ("do_writing(), have_messages = %d\n",
424 _dbus_connection_have_messages_to_send (transport->connection));
430 while (!transport->disconnected &&
431 _dbus_connection_have_messages_to_send (transport->connection))
434 DBusMessage *message;
435 const DBusString *header;
436 const DBusString *body;
437 int header_len, body_len;
438 int total_bytes_to_write;
440 if (total > unix_transport->max_bytes_written_per_iteration)
442 _dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n",
443 total, unix_transport->max_bytes_written_per_iteration);
447 if (!dbus_watch_get_enabled (unix_transport->write_watch))
449 _dbus_verbose ("write watch disabled, not writing more stuff\n");
453 message = _dbus_connection_get_message_to_send (transport->connection);
454 _dbus_assert (message != NULL);
455 _dbus_message_lock (message);
458 _dbus_verbose ("writing message %p\n", message);
461 _dbus_message_get_network_data (message,
464 header_len = _dbus_string_get_length (header);
465 body_len = _dbus_string_get_length (body);
467 if (_dbus_auth_needs_encoding (transport->auth))
469 if (_dbus_string_get_length (&unix_transport->encoded_outgoing) == 0)
471 if (!_dbus_auth_encode_data (transport->auth,
472 header, &unix_transport->encoded_outgoing))
478 if (!_dbus_auth_encode_data (transport->auth,
479 body, &unix_transport->encoded_outgoing))
481 _dbus_string_set_length (&unix_transport->encoded_outgoing, 0);
487 total_bytes_to_write = _dbus_string_get_length (&unix_transport->encoded_outgoing);
490 _dbus_verbose ("encoded message is %d bytes\n",
491 total_bytes_to_write);
495 _dbus_write (unix_transport->fd,
496 &unix_transport->encoded_outgoing,
497 unix_transport->message_bytes_written,
498 total_bytes_to_write - unix_transport->message_bytes_written);
502 total_bytes_to_write = header_len + body_len;
505 _dbus_verbose ("message is %d bytes\n",
506 total_bytes_to_write);
509 if (unix_transport->message_bytes_written < header_len)
512 _dbus_write_two (unix_transport->fd,
514 unix_transport->message_bytes_written,
515 header_len - unix_transport->message_bytes_written,
522 _dbus_write (unix_transport->fd,
524 (unix_transport->message_bytes_written - header_len),
526 (unix_transport->message_bytes_written - header_len));
530 if (bytes_written < 0)
532 /* EINTR already handled for us */
534 if (errno == EAGAIN ||
535 errno == EWOULDBLOCK)
539 _dbus_verbose ("Error writing to remote app: %s\n",
540 _dbus_strerror (errno));
541 do_io_error (transport);
547 _dbus_verbose (" wrote %d bytes of %d\n", bytes_written,
548 total_bytes_to_write);
550 total += bytes_written;
551 unix_transport->message_bytes_written += bytes_written;
553 _dbus_assert (unix_transport->message_bytes_written <=
554 total_bytes_to_write);
556 if (unix_transport->message_bytes_written == total_bytes_to_write)
558 unix_transport->message_bytes_written = 0;
559 _dbus_string_set_length (&unix_transport->encoded_outgoing, 0);
561 _dbus_connection_message_sent (transport->connection,
574 /* returns false on out-of-memory */
576 do_reading (DBusTransport *transport)
578 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
584 /* No messages without authentication! */
585 if (!_dbus_transport_get_is_authenticated (transport))
594 /* See if we've exceeded max messages and need to disable reading */
595 check_read_watch (transport);
597 if (total > unix_transport->max_bytes_read_per_iteration)
599 _dbus_verbose ("%d bytes exceeds %d bytes read per iteration, returning\n",
600 total, unix_transport->max_bytes_read_per_iteration);
604 _dbus_assert (unix_transport->read_watch != NULL ||
605 transport->disconnected);
607 if (transport->disconnected)
610 if (!dbus_watch_get_enabled (unix_transport->read_watch))
613 if (_dbus_auth_needs_decoding (transport->auth))
615 if (_dbus_string_get_length (&unix_transport->encoded_incoming) > 0)
616 bytes_read = _dbus_string_get_length (&unix_transport->encoded_incoming);
618 bytes_read = _dbus_read (unix_transport->fd,
619 &unix_transport->encoded_incoming,
620 unix_transport->max_bytes_read_per_iteration);
622 _dbus_assert (_dbus_string_get_length (&unix_transport->encoded_incoming) ==
629 _dbus_message_loader_get_buffer (transport->loader,
632 orig_len = _dbus_string_get_length (buffer);
634 if (!_dbus_auth_decode_data (transport->auth,
635 &unix_transport->encoded_incoming,
638 _dbus_verbose ("Out of memory decoding incoming data\n");
643 _dbus_message_loader_return_buffer (transport->loader,
645 _dbus_string_get_length (buffer) - orig_len);
647 _dbus_string_set_length (&unix_transport->encoded_incoming, 0);
652 _dbus_message_loader_get_buffer (transport->loader,
655 bytes_read = _dbus_read (unix_transport->fd,
656 buffer, unix_transport->max_bytes_read_per_iteration);
658 _dbus_message_loader_return_buffer (transport->loader,
660 bytes_read < 0 ? 0 : bytes_read);
665 /* EINTR already handled for us */
669 _dbus_verbose ("Out of memory in read()/do_reading()\n");
673 else if (errno == EAGAIN ||
674 errno == EWOULDBLOCK)
678 _dbus_verbose ("Error reading from remote app: %s\n",
679 _dbus_strerror (errno));
680 do_io_error (transport);
684 else if (bytes_read == 0)
686 _dbus_verbose ("Disconnected from remote app\n");
687 do_io_error (transport);
692 _dbus_verbose (" read %d bytes\n", bytes_read);
696 if (_dbus_transport_queue_messages (transport) == DBUS_DISPATCH_NEED_MEMORY)
699 _dbus_verbose (" out of memory when queueing messages we just read in the transport\n");
703 /* Try reading more data until we get EAGAIN and return, or
704 * exceed max bytes per iteration. If in blocking mode of
705 * course we'll block instead of returning.
718 unix_handle_watch (DBusTransport *transport,
722 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
724 _dbus_assert (watch == unix_transport->read_watch ||
725 watch == unix_transport->write_watch);
727 if (watch == unix_transport->read_watch &&
728 (flags & DBUS_WATCH_READABLE))
731 _dbus_verbose ("handling read watch\n");
733 if (!do_authentication (transport, TRUE, FALSE))
736 if (!do_reading (transport))
738 _dbus_verbose ("no memory to read\n");
742 else if (watch == unix_transport->write_watch &&
743 (flags & DBUS_WATCH_WRITABLE))
746 _dbus_verbose ("handling write watch, messages_need_sending = %d\n",
747 transport->messages_need_sending);
749 if (!do_authentication (transport, FALSE, TRUE))
752 if (!do_writing (transport))
754 _dbus_verbose ("no memory to write\n");
758 #ifdef DBUS_ENABLE_VERBOSE_MODE
761 if (watch == unix_transport->read_watch)
762 _dbus_verbose ("asked to handle read watch with non-read condition 0x%x\n",
764 else if (watch == unix_transport->write_watch)
765 _dbus_verbose ("asked to handle write watch with non-write condition 0x%x\n",
768 _dbus_verbose ("asked to handle watch %p on fd %d that we don't recognize\n",
769 watch, dbus_watch_get_fd (watch));
771 #endif /* DBUS_ENABLE_VERBOSE_MODE */
773 if (flags & (DBUS_WATCH_HANGUP | DBUS_WATCH_ERROR))
775 _dbus_transport_disconnect (transport);
783 unix_disconnect (DBusTransport *transport)
785 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
787 free_watches (transport);
789 _dbus_close (unix_transport->fd, NULL);
790 unix_transport->fd = -1;
794 unix_connection_set (DBusTransport *transport)
796 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
798 _dbus_watch_set_handler (unix_transport->write_watch,
799 _dbus_connection_handle_watch,
800 transport->connection, NULL);
802 _dbus_watch_set_handler (unix_transport->read_watch,
803 _dbus_connection_handle_watch,
804 transport->connection, NULL);
806 if (!_dbus_connection_add_watch (transport->connection,
807 unix_transport->write_watch))
810 if (!_dbus_connection_add_watch (transport->connection,
811 unix_transport->read_watch))
813 _dbus_connection_remove_watch (transport->connection,
814 unix_transport->write_watch);
818 check_read_watch (transport);
819 check_write_watch (transport);
825 unix_messages_pending (DBusTransport *transport,
826 int messages_pending)
828 check_write_watch (transport);
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;
848 _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p\n",
849 flags & DBUS_ITERATION_DO_READING ? "read" : "",
850 flags & DBUS_ITERATION_DO_WRITING ? "write" : "",
851 timeout_milliseconds,
852 unix_transport->read_watch,
853 unix_transport->write_watch);
855 /* the passed in DO_READING/DO_WRITING flags indicate whether to
856 * read/write messages, but regardless of those we may need to block
857 * for reading/writing to do auth. But if we do reading for auth,
858 * we don't want to read any messages yet if not given DO_READING.
860 * Also, if read_watch == NULL or write_watch == NULL, we don't
861 * want to read/write so don't.
864 poll_fd.fd = unix_transport->fd;
867 if (_dbus_transport_get_is_authenticated (transport))
869 if (unix_transport->read_watch &&
870 (flags & DBUS_ITERATION_DO_READING))
871 poll_fd.events |= _DBUS_POLLIN;
873 if (unix_transport->write_watch &&
874 (flags & DBUS_ITERATION_DO_WRITING))
875 poll_fd.events |= _DBUS_POLLOUT;
879 DBusAuthState auth_state;
881 auth_state = _dbus_auth_do_work (transport->auth);
883 if (transport->receive_credentials_pending ||
884 auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
885 poll_fd.events |= _DBUS_POLLIN;
887 if (transport->send_credentials_pending ||
888 auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
889 poll_fd.events |= _DBUS_POLLOUT;
894 if (flags & DBUS_ITERATION_BLOCK)
895 poll_timeout = timeout_milliseconds;
899 /* For blocking selects we drop the connection lock here
900 * to avoid blocking out connection access during a potentially
901 * indefinite blocking call. The io path is still protected
902 * by the io_path_cond condvar, so we won't reenter this.
904 if (flags & DBUS_ITERATION_BLOCK)
905 _dbus_connection_unlock (transport->connection);
908 poll_res = _dbus_poll (&poll_fd, 1, poll_timeout);
910 if (poll_res < 0 && errno == EINTR)
913 if (flags & DBUS_ITERATION_BLOCK)
914 _dbus_connection_lock (transport->connection);
918 if (poll_fd.revents & _DBUS_POLLERR)
919 do_io_error (transport);
922 dbus_bool_t need_read = (poll_fd.revents & _DBUS_POLLIN) > 0;
923 dbus_bool_t need_write = (poll_fd.revents & _DBUS_POLLOUT) > 0;
925 _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
926 need_read, need_write);
927 do_authentication (transport, need_read, need_write);
929 if (need_read && (flags & DBUS_ITERATION_DO_READING))
930 do_reading (transport);
931 if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
932 do_writing (transport);
937 _dbus_verbose ("Error from _dbus_poll(): %s\n",
938 _dbus_strerror (errno));
944 unix_live_messages_changed (DBusTransport *transport)
946 /* See if we should look for incoming messages again */
947 check_read_watch (transport);
950 static DBusTransportVTable unix_vtable = {
955 unix_messages_pending,
957 unix_live_messages_changed
961 * Creates a new transport for the given file descriptor. The file
962 * descriptor must be nonblocking (use _dbus_set_fd_nonblocking() to
963 * make it so). This function is shared by various transports that
964 * boil down to a full duplex file descriptor.
966 * @param fd the file descriptor.
967 * @param server #TRUE if this transport is on the server side of a connection
968 * @param address the transport's address
969 * @returns the new transport, or #NULL if no memory.
972 _dbus_transport_new_for_fd (int fd,
974 const DBusString *address)
976 DBusTransportUnix *unix_transport;
978 unix_transport = dbus_new0 (DBusTransportUnix, 1);
979 if (unix_transport == NULL)
982 if (!_dbus_string_init (&unix_transport->encoded_outgoing))
985 if (!_dbus_string_init (&unix_transport->encoded_incoming))
988 unix_transport->write_watch = _dbus_watch_new (fd,
992 if (unix_transport->write_watch == NULL)
995 unix_transport->read_watch = _dbus_watch_new (fd,
999 if (unix_transport->read_watch == NULL)
1002 if (!_dbus_transport_init_base (&unix_transport->base,
1007 unix_transport->fd = fd;
1008 unix_transport->message_bytes_written = 0;
1010 /* These values should probably be tunable or something. */
1011 unix_transport->max_bytes_read_per_iteration = 2048;
1012 unix_transport->max_bytes_written_per_iteration = 2048;
1014 return (DBusTransport*) unix_transport;
1017 _dbus_watch_unref (unix_transport->read_watch);
1019 _dbus_watch_unref (unix_transport->write_watch);
1021 _dbus_string_free (&unix_transport->encoded_incoming);
1023 _dbus_string_free (&unix_transport->encoded_outgoing);
1025 dbus_free (unix_transport);
1030 * Creates a new transport for the given Unix domain socket
1031 * path. This creates a client-side of a transport.
1033 * @todo once we add a way to escape paths in a dbus
1034 * address, this function needs to do escaping.
1036 * @param path the path to the domain socket.
1037 * @param abstract #TRUE to use abstract socket namespace
1038 * @param error address where an error can be returned.
1039 * @returns a new transport, or #NULL on failure.
1042 _dbus_transport_new_for_domain_socket (const char *path,
1043 dbus_bool_t abstract,
1047 DBusTransport *transport;
1050 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1052 if (!_dbus_string_init (&address))
1054 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1061 !_dbus_string_append (&address, "unix:abstract=")) ||
1063 !_dbus_string_append (&address, "unix:path=")) ||
1064 !_dbus_string_append (&address, path))
1066 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1070 fd = _dbus_connect_unix_socket (path, abstract, error);
1073 _DBUS_ASSERT_ERROR_IS_SET (error);
1077 _dbus_fd_set_close_on_exec (fd);
1079 _dbus_verbose ("Successfully connected to unix socket %s\n",
1082 transport = _dbus_transport_new_for_fd (fd, FALSE, &address);
1083 if (transport == NULL)
1085 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1089 _dbus_string_free (&address);
1094 _dbus_close (fd, NULL);
1096 _dbus_string_free (&address);
1101 * Creates a new transport for the given hostname and port.
1103 * @param host the host to connect to
1104 * @param port the port to connect to
1105 * @param error location to store reason for failure.
1106 * @returns a new transport, or #NULL on failure.
1109 _dbus_transport_new_for_tcp_socket (const char *host,
1114 DBusTransport *transport;
1117 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1119 if (!_dbus_string_init (&address))
1121 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1125 if (!_dbus_string_append (&address, "tcp:host=") ||
1126 !_dbus_string_append (&address, host) ||
1127 !_dbus_string_append (&address, ",port=") ||
1128 !_dbus_string_append_int (&address, port))
1130 _dbus_string_free (&address);
1131 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1135 fd = _dbus_connect_tcp_socket (host, port, error);
1138 _DBUS_ASSERT_ERROR_IS_SET (error);
1139 _dbus_string_free (&address);
1143 _dbus_fd_set_close_on_exec (fd);
1145 _dbus_verbose ("Successfully connected to tcp socket %s:%d\n",
1148 transport = _dbus_transport_new_for_fd (fd, FALSE, &address);
1149 if (transport == NULL)
1151 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1152 _dbus_close (fd, NULL);
1153 _dbus_string_free (&address);
1157 _dbus_string_free (&address);