From ac2c4805e15c9bf3beec55922202a6c969956900 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 12 Mar 2015 18:33:46 +0000 Subject: [PATCH 08/11] generic socket transport code: work in terms of DBusSocket --- dbus/dbus-connection.c | 9 +++++++-- dbus/dbus-transport-protected.h | 2 +- dbus/dbus-transport-socket.c | 40 +++++++++++++++++++++------------------- dbus/dbus-transport-socket.h | 2 +- dbus/dbus-transport.c | 2 +- dbus/dbus-transport.h | 2 +- 6 files changed, 32 insertions(+), 25 deletions(-) diff --git a/dbus/dbus-connection.c b/dbus/dbus-connection.c index 8952b75..1bd1d86 100644 --- a/dbus/dbus-connection.c +++ b/dbus/dbus-connection.c @@ -5149,14 +5149,19 @@ dbus_connection_get_socket(DBusConnection *connection, int *fd) { dbus_bool_t retval; + DBusSocket s = DBUS_SOCKET_INIT; _dbus_return_val_if_fail (connection != NULL, FALSE); _dbus_return_val_if_fail (connection->transport != NULL, FALSE); CONNECTION_LOCK (connection); - retval = _dbus_transport_get_socket_fd (connection->transport, - fd); + retval = _dbus_transport_get_socket_fd (connection->transport, &s); + + if (retval) + { + *fd = DBUS_SOCKET_GET_INT (s); + } CONNECTION_UNLOCK (connection); diff --git a/dbus/dbus-transport-protected.h b/dbus/dbus-transport-protected.h index 396f0ff..ee627a3 100644 --- a/dbus/dbus-transport-protected.h +++ b/dbus/dbus-transport-protected.h @@ -67,7 +67,7 @@ struct DBusTransportVTable /**< Outstanding messages counter changed */ dbus_bool_t (* get_socket_fd) (DBusTransport *transport, - int *fd_p); + DBusSocket *fd_p); /**< Get socket file descriptor */ }; diff --git a/dbus/dbus-transport-socket.c b/dbus/dbus-transport-socket.c index c1e4701..bc4b312 100644 --- a/dbus/dbus-transport-socket.c +++ b/dbus/dbus-transport-socket.c @@ -49,7 +49,7 @@ typedef struct DBusTransportSocket DBusTransportSocket; struct DBusTransportSocket { DBusTransport base; /**< Parent instance */ - int fd; /**< File descriptor. */ + DBusSocket fd; /**< File descriptor. */ DBusWatch *read_watch; /**< Watch for readability. */ DBusWatch *write_watch; /**< Watch for writability. */ @@ -159,9 +159,9 @@ check_write_watch (DBusTransport *transport) } } - _dbus_verbose ("check_write_watch(): needed = %d on connection %p watch %p fd = %d outgoing messages exist %d\n", + _dbus_verbose ("check_write_watch(): needed = %d on connection %p watch %p fd = %" DBUS_SOCKET_FORMAT " outgoing messages exist %d\n", needed, transport->connection, socket_transport->write_watch, - socket_transport->fd, + DBUS_SOCKET_PRINTABLE (socket_transport->fd), _dbus_connection_has_messages_to_send_unlocked (transport->connection)); _dbus_connection_toggle_watch_unlocked (transport->connection, @@ -177,7 +177,8 @@ check_read_watch (DBusTransport *transport) DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport; dbus_bool_t need_read_watch; - _dbus_verbose ("fd = %d\n",socket_transport->fd); + _dbus_verbose ("fd = %" DBUS_SOCKET_FORMAT "\n", + DBUS_SOCKET_PRINTABLE (socket_transport->fd)); if (transport->connection == NULL) return; @@ -514,9 +515,9 @@ do_writing (DBusTransport *transport) } #if 1 - _dbus_verbose ("do_writing(), have_messages = %d, fd = %d\n", + _dbus_verbose ("do_writing(), have_messages = %d, fd = %" DBUS_SOCKET_FORMAT "\n", _dbus_connection_has_messages_to_send_unlocked (transport->connection), - socket_transport->fd); + DBUS_SOCKET_PRINTABLE (socket_transport->fd)); #endif oom = FALSE; @@ -741,7 +742,8 @@ do_reading (DBusTransport *transport) dbus_bool_t oom; int saved_errno; - _dbus_verbose ("fd = %d\n",socket_transport->fd); + _dbus_verbose ("fd = %" DBUS_SOCKET_FORMAT "\n", + DBUS_SOCKET_PRINTABLE (socket_transport->fd)); /* No messages without authentication! */ if (!_dbus_transport_try_to_authenticate (transport)) @@ -1007,7 +1009,7 @@ socket_handle_watch (DBusTransport *transport, _dbus_verbose ("asked to handle write watch with non-write condition 0x%x\n", flags); else - _dbus_verbose ("asked to handle watch %p on fd %d that we don't recognize\n", + _dbus_verbose ("asked to handle watch %p on fd %" DBUS_SOCKET_FORMAT " that we don't recognize\n", watch, dbus_watch_get_socket (watch)); } #endif /* DBUS_ENABLE_VERBOSE_MODE */ @@ -1025,7 +1027,7 @@ socket_disconnect (DBusTransport *transport) free_watches (transport); _dbus_close_socket (socket_transport->fd, NULL); - socket_transport->fd = -1; + DBUS_SOCKET_INVALIDATE (socket_transport->fd); } static dbus_bool_t @@ -1076,13 +1078,13 @@ socket_do_iteration (DBusTransport *transport, int poll_res; int poll_timeout; - _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p fd = %d\n", + _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p fd = %" DBUS_SOCKET_FORMAT "\n", flags & DBUS_ITERATION_DO_READING ? "read" : "", flags & DBUS_ITERATION_DO_WRITING ? "write" : "", timeout_milliseconds, socket_transport->read_watch, socket_transport->write_watch, - socket_transport->fd); + DBUS_SOCKET_PRINTABLE (socket_transport->fd)); /* the passed in DO_READING/DO_WRITING flags indicate whether to * read/write messages, but regardless of those we may need to block @@ -1090,7 +1092,7 @@ socket_do_iteration (DBusTransport *transport, * we don't want to read any messages yet if not given DO_READING. */ - poll_fd.fd = socket_transport->fd; + poll_fd.fd = DBUS_SOCKET_GET_POLLABLE (socket_transport->fd); poll_fd.events = 0; if (_dbus_transport_try_to_authenticate (transport)) @@ -1239,7 +1241,7 @@ socket_live_messages_changed (DBusTransport *transport) static dbus_bool_t socket_get_socket_fd (DBusTransport *transport, - int *fd_p) + DBusSocket *fd_p) { DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport; @@ -1270,7 +1272,7 @@ static const DBusTransportVTable socket_vtable = { * @returns the new transport, or #NULL if no memory. */ DBusTransport* -_dbus_transport_new_for_socket (int fd, +_dbus_transport_new_for_socket (DBusSocket fd, const DBusString *server_guid, const DBusString *address) { @@ -1286,14 +1288,14 @@ _dbus_transport_new_for_socket (int fd, if (!_dbus_string_init (&socket_transport->encoded_incoming)) goto failed_1; - socket_transport->write_watch = _dbus_watch_new (fd, + socket_transport->write_watch = _dbus_watch_new (DBUS_SOCKET_GET_POLLABLE (fd), DBUS_WATCH_WRITABLE, FALSE, NULL, NULL, NULL); if (socket_transport->write_watch == NULL) goto failed_2; - socket_transport->read_watch = _dbus_watch_new (fd, + socket_transport->read_watch = _dbus_watch_new (DBUS_SOCKET_GET_POLLABLE (fd), DBUS_WATCH_READABLE, FALSE, NULL, NULL, NULL); @@ -1351,7 +1353,7 @@ _dbus_transport_new_for_tcp_socket (const char *host, const char *noncefile, DBusError *error) { - int fd; + DBusSocket fd; DBusTransport *transport; DBusString address; @@ -1388,7 +1390,7 @@ _dbus_transport_new_for_tcp_socket (const char *host, goto error; fd = _dbus_connect_tcp_socket_with_nonce (host, port, family, noncefile, error); - if (fd < 0) + if (!DBUS_SOCKET_IS_VALID (fd)) { _DBUS_ASSERT_ERROR_IS_SET (error); _dbus_string_free (&address); @@ -1404,7 +1406,7 @@ _dbus_transport_new_for_tcp_socket (const char *host, { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); _dbus_close_socket (fd, NULL); - fd = -1; + DBUS_SOCKET_INVALIDATE (fd); } return transport; diff --git a/dbus/dbus-transport-socket.h b/dbus/dbus-transport-socket.h index 8aefae3..b8267ff 100644 --- a/dbus/dbus-transport-socket.h +++ b/dbus/dbus-transport-socket.h @@ -27,7 +27,7 @@ DBUS_BEGIN_DECLS -DBusTransport* _dbus_transport_new_for_socket (int fd, +DBusTransport* _dbus_transport_new_for_socket (DBusSocket fd, const DBusString *server_guid, const DBusString *address); DBusTransport* _dbus_transport_new_for_tcp_socket (const char *host, diff --git a/dbus/dbus-transport.c b/dbus/dbus-transport.c index 5ceab0a..59219c8 100644 --- a/dbus/dbus-transport.c +++ b/dbus/dbus-transport.c @@ -949,7 +949,7 @@ _dbus_transport_set_connection (DBusTransport *transport, */ dbus_bool_t _dbus_transport_get_socket_fd (DBusTransport *transport, - int *fd_p) + DBusSocket *fd_p) { dbus_bool_t retval; diff --git a/dbus/dbus-transport.h b/dbus/dbus-transport.h index 843f231..9e3787d 100644 --- a/dbus/dbus-transport.h +++ b/dbus/dbus-transport.h @@ -71,7 +71,7 @@ void _dbus_transport_set_max_received_unix_fds(DBusTransport long _dbus_transport_get_max_received_unix_fds(DBusTransport *transport); dbus_bool_t _dbus_transport_get_socket_fd (DBusTransport *transport, - int *fd_p); + DBusSocket *fd_p); dbus_bool_t _dbus_transport_get_unix_user (DBusTransport *transport, unsigned long *uid); dbus_bool_t _dbus_transport_get_unix_process_id (DBusTransport *transport, -- 2.1.4