Commit 36e9dace authored by Ralf Habacker's avatar Ralf Habacker Committed by Simon McVittie

Convert mostly DBUS_SOCKET_... and DBUS_POLLABLE_.. macros for more type safety.

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=89444Reviewed-by: 's avatarSimon McVittie <simon.mcvittie@collabora.co.uk>
parent f5e1c139
......@@ -324,7 +324,7 @@ setup_reload_pipe (DBusLoop *loop)
exit (1);
}
watch = _dbus_watch_new (DBUS_SOCKET_GET_POLLABLE (reload_pipe[RELOAD_READ_END]),
watch = _dbus_watch_new (_dbus_socket_get_pollable (reload_pipe[RELOAD_READ_END]),
DBUS_WATCH_READABLE, TRUE,
handle_reload_watch, NULL, NULL);
......@@ -355,10 +355,10 @@ close_reload_pipe (DBusWatch **watch)
*watch = NULL;
_dbus_close_socket (reload_pipe[RELOAD_READ_END], NULL);
DBUS_SOCKET_INVALIDATE (reload_pipe[RELOAD_READ_END]);
_dbus_socket_invalidate (&reload_pipe[RELOAD_READ_END]);
_dbus_close_socket (reload_pipe[RELOAD_WRITE_END], NULL);
DBUS_SOCKET_INVALIDATE (reload_pipe[RELOAD_WRITE_END]);
_dbus_socket_invalidate (&reload_pipe[RELOAD_WRITE_END]);
}
#endif /* DBUS_UNIX */
......
......@@ -5160,7 +5160,7 @@ dbus_connection_get_socket(DBusConnection *connection,
if (retval)
{
*fd = DBUS_SOCKET_GET_INT (s);
*fd = _dbus_socket_get_int (s);
}
CONNECTION_UNLOCK (connection);
......
......@@ -200,7 +200,7 @@ cull_watches_for_invalid_fd (DBusLoop *loop,
DBusList **watches;
_dbus_warn ("invalid request, socket fd %" DBUS_POLLABLE_FORMAT " not open\n",
DBUS_POLLABLE_PRINTABLE (fd));
_dbus_pollable_printable (fd));
watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
if (watches != NULL)
......@@ -240,7 +240,7 @@ refresh_watches_for_fd (DBusLoop *loop,
unsigned int flags = 0;
dbus_bool_t interested = FALSE;
_dbus_assert (DBUS_POLLABLE_IS_VALID (fd));
_dbus_assert (_dbus_pollable_is_valid (fd));
if (watches == NULL)
watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
......@@ -275,7 +275,7 @@ _dbus_loop_add_watch (DBusLoop *loop,
DBusList **watches;
fd = _dbus_watch_get_pollable (watch);
_dbus_assert (DBUS_POLLABLE_IS_VALID (fd));
_dbus_assert (_dbus_pollable_is_valid (fd));
watches = ensure_watch_table_entry (loop, fd);
......@@ -330,7 +330,7 @@ _dbus_loop_remove_watch (DBusLoop *loop,
* which has been safe since fd.o #33336 was fixed. Assert about it
* so we don't regress. */
fd = _dbus_watch_get_pollable (watch);
_dbus_assert (DBUS_POLLABLE_IS_VALID (fd));
_dbus_assert (_dbus_pollable_is_valid (fd));
watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
......
......@@ -161,7 +161,7 @@ _dbus_accept_with_noncefile (DBusSocket listen_fd, const DBusNonceFile *noncefil
if (_dbus_read_nonce (_dbus_noncefile_get_path(noncefile), &nonce, NULL) != TRUE)
return _dbus_socket_get_invalid ();
fd = _dbus_accept (listen_fd);
if (!DBUS_SOCKET_IS_VALID (fd))
if (!_dbus_socket_is_valid (fd))
return fd;
if (do_check_nonce(fd, &nonce, NULL) != TRUE) {
_dbus_verbose ("nonce check failed. Closing socket.\n");
......
......@@ -267,7 +267,7 @@ _dbus_transport_debug_pipe_new (const char *server_name,
_dbus_string_free (&address);
DBUS_SOCKET_INVALIDATE (client_fd);
_dbus_socket_invalidate (&client_fd);
server_transport = _dbus_transport_new_for_socket (server_fd,
&server->guid_hex, NULL);
......@@ -279,7 +279,7 @@ _dbus_transport_debug_pipe_new (const char *server_name,
return NULL;
}
DBUS_SOCKET_INVALIDATE (server_fd);
_dbus_socket_invalidate (&server_fd);
if (!_dbus_transport_set_auth_mechanisms (server_transport,
(const char**) server->auth_mechanisms))
......
......@@ -92,7 +92,7 @@ handle_new_client_fd_and_unlock (DBusServer *server,
void *new_connection_data;
_dbus_verbose ("Creating new client connection with fd %" DBUS_SOCKET_FORMAT "\n",
DBUS_SOCKET_PRINTABLE (client_fd));
_dbus_socket_printable (client_fd));
HAVE_LOCK_CHECK (server);
......@@ -196,7 +196,7 @@ socket_handle_watch (DBusWatch *watch,
saved_errno = _dbus_save_socket_errno ();
if (!DBUS_SOCKET_IS_VALID (client_fd))
if (!_dbus_socket_is_valid (client_fd))
{
/* EINTR handled for us */
......@@ -244,7 +244,7 @@ socket_disconnect (DBusServer *server)
}
_dbus_close_socket (socket_server->fds[i], NULL);
DBUS_SOCKET_INVALIDATE (socket_server->fds[i]);
_dbus_socket_invalidate (&socket_server->fds[i]);
}
if (socket_server->socket_name != NULL)
......@@ -308,7 +308,7 @@ _dbus_server_new_for_socket (DBusSocket *fds,
{
DBusWatch *watch;
watch = _dbus_watch_new (DBUS_SOCKET_GET_POLLABLE (fds[i]),
watch = _dbus_watch_new (_dbus_socket_get_pollable (fds[i]),
DBUS_WATCH_READABLE,
TRUE,
socket_handle_watch, socket_server,
......
......@@ -123,7 +123,7 @@ socket_set_poll_add (DBusSocketSet *set,
int i;
for (i = 0; i < self->n_fds; i++)
_dbus_assert (!DBUS_POLLABLE_EQUALS (self->fds[i].fd, fd));
_dbus_assert (!_dbus_pollable_equals (self->fds[i].fd, fd));
#endif
if (self->n_reserved >= self->n_allocated)
......@@ -143,7 +143,7 @@ socket_set_poll_add (DBusSocketSet *set,
}
_dbus_verbose ("before adding fd %" DBUS_POLLABLE_FORMAT " to %p, %d en/%d res/%d alloc\n",
DBUS_POLLABLE_PRINTABLE (fd), self, self->n_fds, self->n_reserved, self->n_allocated);
_dbus_pollable_printable (fd), self, self->n_fds, self->n_reserved, self->n_allocated);
_dbus_assert (self->n_reserved >= self->n_fds);
_dbus_assert (self->n_allocated > self->n_reserved);
......@@ -169,7 +169,7 @@ socket_set_poll_enable (DBusSocketSet *set,
for (i = 0; i < self->n_fds; i++)
{
if (DBUS_POLLABLE_EQUALS (self->fds[i].fd, fd))
if (_dbus_pollable_equals (self->fds[i].fd, fd))
{
self->fds[i].events = watch_flags_to_poll_events (flags);
return;
......@@ -194,7 +194,7 @@ socket_set_poll_disable (DBusSocketSet *set,
for (i = 0; i < self->n_fds; i++)
{
if (DBUS_POLLABLE_EQUALS (self->fds[i].fd, fd))
if (_dbus_pollable_equals (self->fds[i].fd, fd))
{
if (i != self->n_fds - 1)
{
......@@ -218,7 +218,7 @@ socket_set_poll_remove (DBusSocketSet *set,
self->n_reserved--;
_dbus_verbose ("after removing fd %" DBUS_POLLABLE_FORMAT " from %p, %d en/%d res/%d alloc\n",
DBUS_POLLABLE_PRINTABLE (fd), self, self->n_fds, self->n_reserved, self->n_allocated);
_dbus_pollable_printable (fd), self, self->n_fds, self->n_reserved, self->n_allocated);
_dbus_assert (self->n_fds <= self->n_reserved);
_dbus_assert (self->n_reserved <= self->n_allocated);
......
......@@ -1575,7 +1575,7 @@ _dbus_connect_tcp_socket_with_nonce (const char *host,
}
freeaddrinfo(ai);
if (!DBUS_SOCKET_IS_VALID (fd))
if (!_dbus_socket_is_valid (fd))
{
dbus_set_error (error,
_dbus_error_from_errno (errno),
......@@ -1854,7 +1854,7 @@ _dbus_accept (DBusSocket listen_fd)
retry:
client_fd.sock = accept (listen_fd.sock, NULL, NULL);
if (!DBUS_SOCKET_IS_VALID (client_fd))
if (!_dbus_socket_is_valid (client_fd))
{
DBUS_SOCKET_SET_ERRNO ();
if (errno == EINTR)
......
......@@ -129,21 +129,37 @@ typedef unsigned long dbus_gid_t;
typedef struct { int fd; } DBusSocket;
# define DBUS_SOCKET_FORMAT "d"
# define DBUS_SOCKET_PRINTABLE(s) ((s).fd)
# define DBUS_SOCKET_INIT { -1 }
# define DBUS_SOCKET_IS_VALID(s) ((s).fd >= 0)
# define DBUS_SOCKET_INVALIDATE(s) ((s).fd = -1)
# define DBUS_SOCKET_GET_INT(s) ((s).fd)
static inline int
_dbus_socket_printable (DBusSocket s) { return s.fd; }
static inline dbus_bool_t
_dbus_socket_is_valid (DBusSocket s) { return s.fd >= 0; }
static inline void
_dbus_socket_invalidate (DBusSocket *s) { s->fd = -1; }
static inline int
_dbus_socket_get_int (DBusSocket s) { return s.fd; }
#else /* DBUS_WIN */
typedef struct { SOCKET sock; } DBusSocket;
# define DBUS_SOCKET_FORMAT "Iu"
# define DBUS_SOCKET_PRINTABLE(s) ((s).sock)
# define DBUS_SOCKET_INIT { INVALID_SOCKET }
# define DBUS_SOCKET_IS_VALID(s) ((s).sock != INVALID_SOCKET)
# define DBUS_SOCKET_INVALIDATE(s) ((s).sock = INVALID_SOCKET)
# define DBUS_SOCKET_GET_INT(s) ((int) (s).sock)
static inline SOCKET
_dbus_socket_printable (DBusSocket s) { return s.sock; }
static inline dbus_bool_t
_dbus_socket_is_valid (DBusSocket s) { return s.sock != INVALID_SOCKET; }
static inline void
_dbus_socket_invalidate (DBusSocket *s) { s->sock = INVALID_SOCKET; }
static inline int
_dbus_socket_get_int (DBusSocket s) { return (int)s.sock; }
#endif /* DBUS_WIN */
......@@ -347,12 +363,22 @@ dbus_int32_t _dbus_atomic_get (DBusAtomic *atomic);
* use DBusSocket so that the compiler can check we are doing it right.
*/
typedef DBusSocket DBusPollable;
# define DBUS_SOCKET_GET_POLLABLE(s) (s)
# define DBUS_POLLABLE_FORMAT "Iu"
# define DBUS_POLLABLE_PRINTABLE(p) (p.sock)
# define DBUS_POLLABLE_IS_VALID(p) (DBUS_SOCKET_IS_VALID (p))
# define DBUS_POLLABLE_INVALIDATE(p) (DBUS_SOCKET_INVALIDATE (p))
# define DBUS_POLLABLE_EQUALS(a, b) ((a).sock == (b).sock)
static inline DBusPollable
_dbus_socket_get_pollable (DBusSocket s) { return s; }
static inline SOCKET
_dbus_pollable_printable (DBusPollable p) { return p.sock; }
static inline dbus_bool_t
_dbus_pollable_is_valid (DBusPollable p) { return _dbus_socket_is_valid (p); }
static inline void
_dbus_pollable_invalidate (DBusPollable *p) { _dbus_socket_invalidate (p); }
static inline dbus_bool_t
_dbus_pollable_equals (DBusPollable a, DBusPollable b) { return a.sock == b.sock; }
#else /* !DBUS_WIN */
......@@ -362,12 +388,22 @@ typedef DBusSocket DBusPollable;
* abstraction.)
*/
typedef int DBusPollable;
# define DBUS_SOCKET_GET_POLLABLE(s) (s.fd)
# define DBUS_POLLABLE_FORMAT "d"
# define DBUS_POLLABLE_PRINTABLE(p) (p)
# define DBUS_POLLABLE_IS_VALID(p) (p >= 0)
# define DBUS_POLLABLE_INVALIDATE(p) ((p) = -1)
# define DBUS_POLLABLE_EQUALS(a, b) ((a) == (b))
static inline DBusPollable
_dbus_socket_get_pollable (DBusSocket s) { return s.fd; }
static inline int
_dbus_pollable_printable (DBusPollable p) { return p; }
static inline dbus_bool_t
_dbus_pollable_is_valid (DBusPollable p) { return p >= 0; }
static inline void
_dbus_pollable_invalidate (DBusPollable *p) { *p = -1; }
static inline dbus_bool_t
_dbus_pollable_equals (DBusPollable a, DBusPollable b) { return a == b; }
#endif /* !DBUS_WIN */
......
......@@ -161,7 +161,7 @@ check_write_watch (DBusTransport *transport)
_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,
DBUS_SOCKET_PRINTABLE (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,
......@@ -178,7 +178,7 @@ check_read_watch (DBusTransport *transport)
dbus_bool_t need_read_watch;
_dbus_verbose ("fd = %" DBUS_SOCKET_FORMAT "\n",
DBUS_SOCKET_PRINTABLE (socket_transport->fd));
_dbus_socket_printable (socket_transport->fd));
if (transport->connection == NULL)
return;
......@@ -517,7 +517,7 @@ do_writing (DBusTransport *transport)
#if 1
_dbus_verbose ("do_writing(), have_messages = %d, fd = %" DBUS_SOCKET_FORMAT "\n",
_dbus_connection_has_messages_to_send_unlocked (transport->connection),
DBUS_SOCKET_PRINTABLE (socket_transport->fd));
_dbus_socket_printable (socket_transport->fd));
#endif
oom = FALSE;
......@@ -743,7 +743,7 @@ do_reading (DBusTransport *transport)
int saved_errno;
_dbus_verbose ("fd = %" DBUS_SOCKET_FORMAT "\n",
DBUS_SOCKET_PRINTABLE (socket_transport->fd));
_dbus_socket_printable (socket_transport->fd));
/* No messages without authentication! */
if (!_dbus_transport_try_to_authenticate (transport))
......@@ -1027,7 +1027,7 @@ socket_disconnect (DBusTransport *transport)
free_watches (transport);
_dbus_close_socket (socket_transport->fd, NULL);
DBUS_SOCKET_INVALIDATE (socket_transport->fd);
_dbus_socket_invalidate (&socket_transport->fd);
}
static dbus_bool_t
......@@ -1084,7 +1084,7 @@ socket_do_iteration (DBusTransport *transport,
timeout_milliseconds,
socket_transport->read_watch,
socket_transport->write_watch,
DBUS_SOCKET_PRINTABLE (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
......@@ -1092,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 = DBUS_SOCKET_GET_POLLABLE (socket_transport->fd);
poll_fd.fd = _dbus_socket_get_pollable (socket_transport->fd);
poll_fd.events = 0;
if (_dbus_transport_try_to_authenticate (transport))
......@@ -1288,14 +1288,14 @@ _dbus_transport_new_for_socket (DBusSocket fd,
if (!_dbus_string_init (&socket_transport->encoded_incoming))
goto failed_1;
socket_transport->write_watch = _dbus_watch_new (DBUS_SOCKET_GET_POLLABLE (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 (DBUS_SOCKET_GET_POLLABLE (fd),
socket_transport->read_watch = _dbus_watch_new (_dbus_socket_get_pollable (fd),
DBUS_WATCH_READABLE,
FALSE,
NULL, NULL, NULL);
......@@ -1390,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 (!DBUS_SOCKET_IS_VALID (fd))
if (!_dbus_socket_is_valid (fd))
{
_DBUS_ASSERT_ERROR_IS_SET (error);
_dbus_string_free (&address);
......@@ -1406,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);
DBUS_SOCKET_INVALIDATE (fd);
_dbus_socket_invalidate (&fd);
}
return transport;
......
......@@ -143,7 +143,7 @@ _dbus_watch_unref (DBusWatch *watch)
watch->refcount -= 1;
if (watch->refcount == 0)
{
if (DBUS_POLLABLE_IS_VALID (watch->fd))
if (_dbus_pollable_is_valid (watch->fd))
_dbus_warn ("this watch should have been invalidated");
dbus_watch_set_data (watch, NULL, NULL); /* call free_data_function */
......@@ -168,7 +168,7 @@ _dbus_watch_unref (DBusWatch *watch)
void
_dbus_watch_invalidate (DBusWatch *watch)
{
DBUS_POLLABLE_INVALIDATE (watch->fd);
_dbus_pollable_invalidate (&watch->fd);
watch->flags = 0;
}
......@@ -315,7 +315,7 @@ _dbus_watch_list_set_functions (DBusWatchList *watch_list,
_dbus_verbose ("Adding a %s watch on fd %" DBUS_POLLABLE_FORMAT " using newly-set add watch function\n",
watch_flags_to_string (dbus_watch_get_flags (link->data)),
DBUS_POLLABLE_PRINTABLE (watch->fd));
_dbus_pollable_printable (watch->fd));
#endif
if (!(* add_function) (link->data, data))
......@@ -332,7 +332,7 @@ _dbus_watch_list_set_functions (DBusWatchList *watch_list,
DBusWatch *watch2 = link2->data;
_dbus_verbose ("Removing watch on fd %" DBUS_POLLABLE_FORMAT " using newly-set remove function because initial add failed\n",
DBUS_POLLABLE_PRINTABLE (watch2->fd));
_dbus_pollable_printable (watch2->fd));
#endif
(* remove_function) (link2->data, data);
......@@ -390,7 +390,7 @@ _dbus_watch_list_add_watch (DBusWatchList *watch_list,
if (watch_list->add_watch_function != NULL)
{
_dbus_verbose ("Adding watch on fd %" DBUS_POLLABLE_FORMAT "\n",
DBUS_POLLABLE_PRINTABLE (watch->fd));
_dbus_pollable_printable (watch->fd));
if (!(* watch_list->add_watch_function) (watch,
watch_list->watch_data))
......@@ -421,7 +421,7 @@ _dbus_watch_list_remove_watch (DBusWatchList *watch_list,
if (watch_list->remove_watch_function != NULL)
{
_dbus_verbose ("Removing watch on fd %" DBUS_POLLABLE_FORMAT "\n",
DBUS_POLLABLE_PRINTABLE (watch->fd));
_dbus_pollable_printable (watch->fd));
(* watch_list->remove_watch_function) (watch,
watch_list->watch_data);
......@@ -454,7 +454,7 @@ _dbus_watch_list_toggle_watch (DBusWatchList *watch_list,
{
_dbus_verbose ("Toggling watch %p on fd %" DBUS_POLLABLE_FORMAT " to %d\n",
watch,
DBUS_POLLABLE_PRINTABLE (watch->fd),
_dbus_pollable_printable (watch->fd),
watch->enabled);
(* watch_list->watch_toggled_function) (watch,
......@@ -598,7 +598,7 @@ dbus_watch_get_socket (DBusWatch *watch)
#ifdef DBUS_UNIX
return watch->fd;
#else
return DBUS_SOCKET_GET_INT (watch->fd);
return _dbus_socket_get_int (watch->fd);
#endif
}
......@@ -682,7 +682,7 @@ dbus_watch_set_data (DBusWatch *watch,
_dbus_return_if_fail (watch != NULL);
_dbus_verbose ("Setting watch fd %" DBUS_POLLABLE_FORMAT " data to data = %p function = %p from data = %p function = %p\n",
DBUS_POLLABLE_PRINTABLE (watch->fd),
_dbus_pollable_printable (watch->fd),
data, free_data_function, watch->data, watch->free_data_function);
if (watch->free_data_function != NULL)
......@@ -737,21 +737,21 @@ dbus_watch_handle (DBusWatch *watch,
_dbus_return_val_if_fail (watch != NULL, FALSE);
#ifndef DBUS_DISABLE_CHECKS
if (!DBUS_POLLABLE_IS_VALID (watch->fd) || watch->flags == 0)
if (!_dbus_pollable_is_valid (watch->fd) || watch->flags == 0)
{
_dbus_warn_check_failed ("Watch is invalid, it should have been removed\n");
return TRUE;
}
#endif
_dbus_return_val_if_fail (DBUS_POLLABLE_IS_VALID (watch->fd) /* fails if watch was removed */, TRUE);
_dbus_return_val_if_fail (_dbus_pollable_is_valid (watch->fd) /* fails if watch was removed */, TRUE);
_dbus_watch_sanitize_condition (watch, &flags);
if (flags == 0)
{
_dbus_verbose ("After sanitization, watch flags on fd %" DBUS_POLLABLE_FORMAT " were 0\n",
DBUS_POLLABLE_PRINTABLE (watch->fd));
_dbus_pollable_printable (watch->fd));
return TRUE;
}
else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment