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)
186 /* Queue any messages */
187 while ((link = _dbus_message_loader_pop_message_link (transport->loader)))
189 DBusMessage *message;
191 message = link->data;
193 _dbus_verbose ("queueing received message %p\n", message);
195 _dbus_message_add_size_counter (message, transport->live_messages_size);
197 /* pass ownership of link and message ref to connection */
198 _dbus_connection_queue_received_message_link (transport->connection,
202 if (_dbus_message_loader_get_is_corrupted (transport->loader))
204 _dbus_verbose ("Corrupted message stream, disconnecting\n");
205 do_io_error (transport);
208 /* check read watch in case we've now exceeded max outstanding messages */
209 check_read_watch (transport);
212 /* return value is whether we successfully read any new data. */
214 read_data_into_auth (DBusTransport *transport)
216 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
220 if (!_dbus_string_init (&buffer, _DBUS_INT_MAX))
222 /* just disconnect if we don't have memory
223 * to do an authentication
225 _dbus_verbose ("No memory for authentication\n");
226 do_io_error (transport);
230 bytes_read = _dbus_read (unix_transport->fd,
231 &buffer, unix_transport->max_bytes_read_per_iteration);
235 _dbus_verbose (" read %d bytes in auth phase\n", bytes_read);
237 if (_dbus_auth_bytes_received (transport->auth,
240 _dbus_string_free (&buffer);
241 return TRUE; /* We did read some data! woo! */
245 /* just disconnect if we don't have memory to do an
246 * authentication, don't fool with trying to save the buffer
247 * and who knows what.
249 _dbus_verbose ("No memory for authentication\n");
250 do_io_error (transport);
253 else if (bytes_read < 0)
255 /* EINTR already handled for us */
257 if (errno == EAGAIN ||
258 errno == EWOULDBLOCK)
259 ; /* do nothing, just return FALSE below */
262 _dbus_verbose ("Error reading from remote app: %s\n",
263 _dbus_strerror (errno));
264 do_io_error (transport);
267 else if (bytes_read == 0)
269 _dbus_verbose ("Disconnected from remote app\n");
270 do_io_error (transport);
273 _dbus_string_free (&buffer);
277 /* Return value is whether we successfully wrote any bytes */
279 write_data_from_auth (DBusTransport *transport)
281 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
283 const DBusString *buffer;
285 if (!_dbus_auth_get_bytes_to_send (transport->auth,
289 bytes_written = _dbus_write (unix_transport->fd,
291 0, _dbus_string_get_length (buffer));
293 if (bytes_written > 0)
295 _dbus_auth_bytes_sent (transport->auth, bytes_written);
298 else if (bytes_written < 0)
300 /* EINTR already handled for us */
302 if (errno == EAGAIN ||
303 errno == EWOULDBLOCK)
307 _dbus_verbose ("Error writing to remote app: %s\n",
308 _dbus_strerror (errno));
309 do_io_error (transport);
317 recover_unused_bytes (DBusTransport *transport)
320 if (_dbus_auth_needs_decoding (transport->auth))
322 DBusString plaintext;
327 if (!_dbus_string_init (&plaintext, _DBUS_INT_MAX))
330 if (!_dbus_string_init (&encoded, _DBUS_INT_MAX))
332 _dbus_string_free (&plaintext);
336 if (!_dbus_auth_get_unused_bytes (transport->auth,
339 _dbus_string_free (&plaintext);
340 _dbus_string_free (&encoded);
344 if (!_dbus_auth_decode_data (transport->auth,
345 &encoded, &plaintext))
347 _dbus_string_free (&plaintext);
348 _dbus_string_free (&encoded);
352 _dbus_message_loader_get_buffer (transport->loader,
355 orig_len = _dbus_string_get_length (buffer);
357 if (!_dbus_string_move (&plaintext, 0, buffer,
360 _dbus_string_free (&plaintext);
361 _dbus_string_free (&encoded);
365 _dbus_verbose (" %d unused bytes sent to message loader\n",
366 _dbus_string_get_length (buffer) -
369 _dbus_message_loader_return_buffer (transport->loader,
371 _dbus_string_get_length (buffer) -
374 _dbus_string_free (&plaintext);
375 _dbus_string_free (&encoded);
382 _dbus_message_loader_get_buffer (transport->loader,
385 orig_len = _dbus_string_get_length (buffer);
387 if (!_dbus_auth_get_unused_bytes (transport->auth,
391 _dbus_verbose (" %d unused bytes sent to message loader\n",
392 _dbus_string_get_length (buffer) -
395 _dbus_message_loader_return_buffer (transport->loader,
397 _dbus_string_get_length (buffer) -
401 queue_messages (transport);
406 _dbus_verbose ("Not enough memory to transfer unused bytes from auth conversation\n");
407 do_io_error (transport);
411 exchange_credentials (DBusTransport *transport,
412 dbus_bool_t do_reading,
413 dbus_bool_t do_writing)
415 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
417 if (do_writing && transport->send_credentials_pending)
419 if (_dbus_send_credentials_unix_socket (unix_transport->fd,
422 transport->send_credentials_pending = FALSE;
426 _dbus_verbose ("Failed to write credentials\n");
427 do_io_error (transport);
431 if (do_reading && transport->receive_credentials_pending)
433 if (_dbus_read_credentials_unix_socket (unix_transport->fd,
434 &transport->credentials,
437 transport->receive_credentials_pending = FALSE;
441 _dbus_verbose ("Failed to read credentials\n");
442 do_io_error (transport);
446 if (!(transport->send_credentials_pending ||
447 transport->receive_credentials_pending))
449 _dbus_auth_set_credentials (transport->auth,
450 &transport->credentials);
455 do_authentication (DBusTransport *transport,
456 dbus_bool_t do_reading,
457 dbus_bool_t do_writing)
459 _dbus_transport_ref (transport);
461 while (!_dbus_transport_get_is_authenticated (transport) &&
462 _dbus_transport_get_is_connected (transport))
464 exchange_credentials (transport, do_reading, do_writing);
466 if (transport->send_credentials_pending ||
467 transport->receive_credentials_pending)
469 _dbus_verbose ("send_credentials_pending = %d receive_credentials_pending = %d\n",
470 transport->send_credentials_pending,
471 transport->receive_credentials_pending);
475 switch (_dbus_auth_do_work (transport->auth))
477 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
478 _dbus_verbose (" auth state: waiting for input\n");
479 if (!do_reading || !read_data_into_auth (transport))
483 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
484 /* Screw it, just disconnect */
485 _dbus_verbose (" auth state: waiting for memory\n");
486 do_io_error (transport);
489 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
490 _dbus_verbose (" auth state: bytes to send\n");
491 if (!do_writing || !write_data_from_auth (transport))
495 case DBUS_AUTH_STATE_NEED_DISCONNECT:
496 _dbus_verbose (" auth state: need to disconnect\n");
497 do_io_error (transport);
500 case DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES:
501 _dbus_verbose (" auth state: auth with unused bytes\n");
502 recover_unused_bytes (transport);
505 case DBUS_AUTH_STATE_AUTHENTICATED:
506 _dbus_verbose (" auth state: authenticated\n");
512 check_read_watch (transport);
513 check_write_watch (transport);
514 _dbus_transport_unref (transport);
518 do_writing (DBusTransport *transport)
521 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
523 /* No messages without authentication! */
524 if (!_dbus_transport_get_is_authenticated (transport))
527 if (transport->disconnected)
532 while (!transport->disconnected &&
533 _dbus_connection_have_messages_to_send (transport->connection))
536 DBusMessage *message;
537 const DBusString *header;
538 const DBusString *body;
539 int header_len, body_len;
540 int total_bytes_to_write;
542 if (total > unix_transport->max_bytes_written_per_iteration)
544 _dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n",
545 total, unix_transport->max_bytes_written_per_iteration);
549 if (unix_transport->write_watch == NULL)
551 _dbus_verbose ("write watch removed, not writing more stuff\n");
555 message = _dbus_connection_get_message_to_send (transport->connection);
556 _dbus_assert (message != NULL);
557 _dbus_message_lock (message);
560 _dbus_verbose ("writing message %p\n", message);
563 _dbus_message_get_network_data (message,
566 header_len = _dbus_string_get_length (header);
567 body_len = _dbus_string_get_length (body);
569 if (_dbus_auth_needs_encoding (transport->auth))
571 if (_dbus_string_get_length (&unix_transport->encoded_message) == 0)
573 if (!_dbus_auth_encode_data (transport->auth,
574 header, &unix_transport->encoded_message))
577 if (!_dbus_auth_encode_data (transport->auth,
578 body, &unix_transport->encoded_message))
580 _dbus_string_set_length (&unix_transport->encoded_message, 0);
585 total_bytes_to_write = _dbus_string_get_length (&unix_transport->encoded_message);
588 _dbus_verbose ("encoded message is %d bytes\n",
589 total_bytes_to_write);
593 _dbus_write (unix_transport->fd,
594 &unix_transport->encoded_message,
595 unix_transport->message_bytes_written,
596 total_bytes_to_write - unix_transport->message_bytes_written);
600 total_bytes_to_write = header_len + body_len;
603 _dbus_verbose ("message is %d bytes\n",
604 total_bytes_to_write);
607 if (unix_transport->message_bytes_written < header_len)
610 _dbus_write_two (unix_transport->fd,
612 unix_transport->message_bytes_written,
613 header_len - unix_transport->message_bytes_written,
620 _dbus_write (unix_transport->fd,
622 (unix_transport->message_bytes_written - header_len),
624 (unix_transport->message_bytes_written - header_len));
628 if (bytes_written < 0)
630 /* EINTR already handled for us */
632 if (errno == EAGAIN ||
633 errno == EWOULDBLOCK)
637 _dbus_verbose ("Error writing to remote app: %s\n",
638 _dbus_strerror (errno));
639 do_io_error (transport);
645 _dbus_verbose (" wrote %d bytes of %d\n", bytes_written,
646 total_bytes_to_write);
648 total += bytes_written;
649 unix_transport->message_bytes_written += bytes_written;
651 _dbus_assert (unix_transport->message_bytes_written <=
652 total_bytes_to_write);
654 if (unix_transport->message_bytes_written == total_bytes_to_write)
656 unix_transport->message_bytes_written = 0;
657 _dbus_string_set_length (&unix_transport->encoded_message, 0);
659 _dbus_connection_message_sent (transport->connection,
666 return; /* I think some C compilers require a statement after a label */
670 do_reading (DBusTransport *transport)
672 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
677 /* No messages without authentication! */
678 if (!_dbus_transport_get_is_authenticated (transport))
685 /* See if we've exceeded max messages and need to disable reading */
686 check_read_watch (transport);
687 if (unix_transport->read_watch == NULL)
690 if (total > unix_transport->max_bytes_read_per_iteration)
692 _dbus_verbose ("%d bytes exceeds %d bytes read per iteration, returning\n",
693 total, unix_transport->max_bytes_read_per_iteration);
697 if (transport->disconnected)
700 if (_dbus_auth_needs_decoding (transport->auth))
704 if (!_dbus_string_init (&encoded, _DBUS_INT_MAX))
705 goto out; /* not enough memory for the moment */
707 bytes_read = _dbus_read (unix_transport->fd,
709 unix_transport->max_bytes_read_per_iteration);
715 _dbus_message_loader_get_buffer (transport->loader,
718 orig_len = _dbus_string_get_length (buffer);
720 if (!_dbus_auth_decode_data (transport->auth,
723 /* FIXME argh, we are really fucked here - nowhere to
724 * put "encoded" while we wait for more memory. Just
725 * screw it for now and disconnect. The failure may be
726 * due to badly-encoded data instead of lack of memory
729 _dbus_verbose ("Disconnected from remote app due to failure decoding data\n");
730 do_io_error (transport);
733 _dbus_message_loader_return_buffer (transport->loader,
735 _dbus_string_get_length (buffer) - orig_len);
738 _dbus_string_free (&encoded);
742 _dbus_message_loader_get_buffer (transport->loader,
745 bytes_read = _dbus_read (unix_transport->fd,
746 buffer, unix_transport->max_bytes_read_per_iteration);
748 _dbus_message_loader_return_buffer (transport->loader,
750 bytes_read < 0 ? 0 : bytes_read);
755 /* EINTR already handled for us */
757 if (errno == EAGAIN ||
758 errno == EWOULDBLOCK)
762 _dbus_verbose ("Error reading from remote app: %s\n",
763 _dbus_strerror (errno));
764 do_io_error (transport);
768 else if (bytes_read == 0)
770 _dbus_verbose ("Disconnected from remote app\n");
771 do_io_error (transport);
776 _dbus_verbose (" read %d bytes\n", bytes_read);
780 queue_messages (transport);
782 /* Try reading more data until we get EAGAIN and return, or
783 * exceed max bytes per iteration. If in blocking mode of
784 * course we'll block instead of returning.
790 return; /* I think some C compilers require a statement after a label */
794 unix_handle_watch (DBusTransport *transport,
798 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
800 _dbus_assert (watch == unix_transport->read_watch ||
801 watch == unix_transport->write_watch);
803 if (flags & (DBUS_WATCH_HANGUP | DBUS_WATCH_ERROR))
805 _dbus_transport_disconnect (transport);
809 if (watch == unix_transport->read_watch &&
810 (flags & DBUS_WATCH_READABLE))
813 _dbus_verbose ("handling read watch\n");
815 do_authentication (transport, TRUE, FALSE);
816 do_reading (transport);
818 else if (watch == unix_transport->write_watch &&
819 (flags & DBUS_WATCH_WRITABLE))
822 _dbus_verbose ("handling write watch\n");
824 do_authentication (transport, FALSE, TRUE);
825 do_writing (transport);
830 unix_disconnect (DBusTransport *transport)
832 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
834 free_watches (transport);
836 _dbus_close (unix_transport->fd, NULL);
837 unix_transport->fd = -1;
841 unix_connection_set (DBusTransport *transport)
843 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
845 if (!_dbus_connection_add_watch (transport->connection,
846 unix_transport->write_watch))
849 if (!_dbus_connection_add_watch (transport->connection,
850 unix_transport->read_watch))
852 _dbus_connection_remove_watch (transport->connection,
853 unix_transport->write_watch);
857 check_read_watch (transport);
858 check_write_watch (transport);
864 unix_messages_pending (DBusTransport *transport,
865 int messages_pending)
867 check_write_watch (transport);
870 /* FIXME use _dbus_poll(), not select() */
872 * @todo We need to have a way to wake up the select sleep if
873 * a new iteration request comes in with a flag (read/write) that
874 * we're not currently serving. Otherwise a call that just reads
875 * could block a write call forever (if there are no incoming
879 unix_do_iteration (DBusTransport *transport,
881 int timeout_milliseconds)
883 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
886 dbus_bool_t do_select;
889 _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p\n",
890 flags & DBUS_ITERATION_DO_READING ? "read" : "",
891 flags & DBUS_ITERATION_DO_WRITING ? "write" : "",
892 timeout_milliseconds,
893 unix_transport->read_watch,
894 unix_transport->write_watch);
896 /* "again" has to be up here because on EINTR the fd sets become
903 /* the passed in DO_READING/DO_WRITING flags indicate whether to
904 * read/write messages, but regardless of those we may need to block
905 * for reading/writing to do auth. But if we do reading for auth,
906 * we don't want to read any messages yet if not given DO_READING.
908 * Also, if read_watch == NULL or write_watch == NULL, we don't
909 * want to read/write so don't.
913 FD_ZERO (&write_set);
915 if (_dbus_transport_get_is_authenticated (transport))
917 if (unix_transport->read_watch &&
918 (flags & DBUS_ITERATION_DO_READING))
920 FD_SET (unix_transport->fd, &read_set);
924 if (unix_transport->write_watch &&
925 (flags & DBUS_ITERATION_DO_WRITING))
927 FD_SET (unix_transport->fd, &write_set);
933 DBusAuthState auth_state;
935 auth_state = _dbus_auth_do_work (transport->auth);
937 if (transport->receive_credentials_pending ||
938 auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
940 FD_SET (unix_transport->fd, &read_set);
944 if (transport->send_credentials_pending ||
945 auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
947 FD_SET (unix_transport->fd, &write_set);
955 struct timeval timeout;
956 dbus_bool_t use_timeout;
959 FD_SET (unix_transport->fd, &err_set);
961 if (flags & DBUS_ITERATION_BLOCK)
963 if (timeout_milliseconds >= 0)
965 timeout.tv_sec = timeout_milliseconds / 1000;
966 timeout.tv_usec = (timeout_milliseconds % 1000) * 1000;
968 /* Always use timeout if one is passed in. */
973 use_timeout = FALSE; /* NULL timeout to block forever */
978 /* 0 timeout to not block */
984 /* For blocking selects we drop the connection lock here
985 * to avoid blocking out connection access during a potentially
986 * indefinite blocking call. The io path is still protected
987 * by the io_path_cond condvar, so we won't reenter this.
989 if (flags & DBUS_ITERATION_BLOCK)
990 _dbus_connection_unlock (transport->connection);
992 select_res = select (unix_transport->fd + 1,
993 &read_set, &write_set, &err_set,
994 use_timeout ? &timeout : NULL);
996 if (flags & DBUS_ITERATION_BLOCK)
997 _dbus_connection_lock (transport->connection);
1000 if (select_res >= 0)
1002 if (FD_ISSET (unix_transport->fd, &err_set))
1003 do_io_error (transport);
1006 dbus_bool_t need_read = FD_ISSET (unix_transport->fd, &read_set);
1007 dbus_bool_t need_write = FD_ISSET (unix_transport->fd, &write_set);
1009 _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
1010 need_read, need_write);
1011 do_authentication (transport, need_read, need_write);
1013 if (need_read && (flags & DBUS_ITERATION_DO_READING))
1014 do_reading (transport);
1015 if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
1016 do_writing (transport);
1019 else if (errno == EINTR)
1023 _dbus_verbose ("Error from select(): %s\n",
1024 _dbus_strerror (errno));
1030 unix_live_messages_changed (DBusTransport *transport)
1032 /* See if we should look for incoming messages again */
1033 check_read_watch (transport);
1036 static DBusTransportVTable unix_vtable = {
1040 unix_connection_set,
1041 unix_messages_pending,
1043 unix_live_messages_changed
1047 * Creates a new transport for the given file descriptor. The file
1048 * descriptor must be nonblocking (use _dbus_set_fd_nonblocking() to
1049 * make it so). This function is shared by various transports that
1050 * boil down to a full duplex file descriptor.
1052 * @param fd the file descriptor.
1053 * @param server #TRUE if this transport is on the server side of a connection
1054 * @returns the new transport, or #NULL if no memory.
1057 _dbus_transport_new_for_fd (int fd,
1060 DBusTransportUnix *unix_transport;
1062 unix_transport = dbus_new0 (DBusTransportUnix, 1);
1063 if (unix_transport == NULL)
1066 if (!_dbus_string_init (&unix_transport->encoded_message,
1070 unix_transport->write_watch = _dbus_watch_new (fd,
1071 DBUS_WATCH_WRITABLE,
1073 if (unix_transport->write_watch == NULL)
1076 unix_transport->read_watch = _dbus_watch_new (fd,
1077 DBUS_WATCH_READABLE,
1079 if (unix_transport->read_watch == NULL)
1082 if (!_dbus_transport_init_base (&unix_transport->base,
1087 unix_transport->fd = fd;
1088 unix_transport->message_bytes_written = 0;
1090 /* These values should probably be tunable or something. */
1091 unix_transport->max_bytes_read_per_iteration = 2048;
1092 unix_transport->max_bytes_written_per_iteration = 2048;
1094 return (DBusTransport*) unix_transport;
1097 _dbus_watch_unref (unix_transport->read_watch);
1099 _dbus_watch_unref (unix_transport->write_watch);
1101 _dbus_string_free (&unix_transport->encoded_message);
1103 dbus_free (unix_transport);
1108 * Creates a new transport for the given Unix domain socket
1111 * @param path the path to the domain socket.
1112 * @param server #TRUE if this transport is on the server side of a connection
1113 * @param result location to store reason for failure.
1114 * @returns a new transport, or #NULL on failure.
1117 _dbus_transport_new_for_domain_socket (const char *path,
1119 DBusResultCode *result)
1122 DBusTransport *transport;
1124 fd = _dbus_connect_unix_socket (path, result);
1128 _dbus_fd_set_close_on_exec (fd);
1130 _dbus_verbose ("Successfully connected to unix socket %s\n",
1133 transport = _dbus_transport_new_for_fd (fd, server);
1134 if (transport == NULL)
1136 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
1137 _dbus_close (fd, NULL);
1145 * Creates a new transport for the given hostname and port.
1147 * @param host the host to connect to
1148 * @param port the port to connect to
1149 * @param server #TRUE if this transport is on the server side of a connection
1150 * @param result location to store reason for failure.
1151 * @returns a new transport, or #NULL on failure.
1154 _dbus_transport_new_for_tcp_socket (const char *host,
1157 DBusResultCode *result)
1160 DBusTransport *transport;
1162 fd = _dbus_connect_tcp_socket (host, port, result);
1166 _dbus_fd_set_close_on_exec (fd);
1168 _dbus_verbose ("Successfully connected to tcp socket %s:%d\n",
1171 transport = _dbus_transport_new_for_fd (fd, server);
1172 if (transport == NULL)
1174 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
1175 _dbus_close (fd, NULL);