Commit f5e1c139 authored by Simon McVittie's avatar Simon McVittie

Turn DBusSocket into a type-safe struct, preventing inappropriate conversion

Fix the remaining platform-specific code to look at the struct's
appropriate platform-specific member.

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=89444
parent 54395bd5
......@@ -5136,11 +5136,11 @@ bus_unix_fds_passing_test(const DBusString *test_data_dir)
if (!_dbus_close(z, &error))
_dbus_assert_not_reached("Failed to close pipe #2/other size 2nd fd ");
if (read(one[1], &r, 1) != 1 || r != 'X')
if (read(one[1].fd, &r, 1) != 1 || r != 'X')
_dbus_assert_not_reached("Failed to read value from pipe.");
if (read(two[1], &r, 1) != 1 || r != 'Y')
if (read(two[1].fd, &r, 1) != 1 || r != 'Y')
_dbus_assert_not_reached("Failed to read value from pipe.");
if (read(two[1], &r, 1) != 1 || r != 'Z')
if (read(two[1].fd, &r, 1) != 1 || r != 'Z')
_dbus_assert_not_reached("Failed to read value from pipe.");
if (!_dbus_close_socket (one[1], &error))
......
......@@ -70,7 +70,7 @@ signal_handler (int sig)
char action[2] = { ACTION_RELOAD, '\0' };
_dbus_string_init_const (&str, action);
if ((reload_pipe[RELOAD_WRITE_END] > 0) &&
if ((reload_pipe[RELOAD_WRITE_END].fd > 0) &&
!_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
{
/* If we receive SIGHUP often enough to fill the pipe buffer (4096
......@@ -103,7 +103,7 @@ signal_handler (int sig)
DBusString str;
char action[2] = { ACTION_QUIT, '\0' };
_dbus_string_init_const (&str, action);
if ((reload_pipe[RELOAD_WRITE_END] < 0) ||
if ((reload_pipe[RELOAD_WRITE_END].fd < 0) ||
!_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
{
/* If we can't write to the socket, dying seems a more
......@@ -248,7 +248,7 @@ handle_reload_watch (DBusWatch *watch,
while (!_dbus_string_init (&str))
_dbus_wait_for_memory ();
if ((reload_pipe[RELOAD_READ_END] > 0) &&
if ((reload_pipe[RELOAD_READ_END].fd > 0) &&
_dbus_read_socket (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
{
_dbus_warn ("Couldn't read from reload pipe.\n");
......
......@@ -159,7 +159,10 @@ static inline DBusPollable
_dbus_hash_iter_get_pollable_key (DBusHashIter *iter)
{
#ifdef DBUS_WIN
return _dbus_hash_iter_get_uintptr_key (iter);
DBusSocket s;
s.sock = _dbus_hash_iter_get_uintptr_key (iter);
return s;
#else
return _dbus_hash_iter_get_int_key (iter);
#endif
......@@ -170,7 +173,7 @@ _dbus_hash_table_lookup_pollable (DBusHashTable *table,
DBusPollable key)
{
#ifdef DBUS_WIN
return _dbus_hash_table_lookup_uintptr (table, key);
return _dbus_hash_table_lookup_uintptr (table, key.sock);
#else
return _dbus_hash_table_lookup_int (table, key);
#endif
......@@ -181,7 +184,7 @@ _dbus_hash_table_remove_pollable (DBusHashTable *table,
DBusPollable key)
{
#ifdef DBUS_WIN
return _dbus_hash_table_remove_uintptr (table, key);
return _dbus_hash_table_remove_uintptr (table, key.sock);
#else
return _dbus_hash_table_remove_int (table, key);
#endif
......@@ -193,7 +196,7 @@ _dbus_hash_table_insert_pollable (DBusHashTable *table,
void *value)
{
#ifdef DBUS_WIN
return _dbus_hash_table_insert_uintptr (table, key, value);
return _dbus_hash_table_insert_uintptr (table, key.sock, value);
#else
return _dbus_hash_table_insert_int (table, key, value);
#endif
......
......@@ -328,9 +328,9 @@ _dbus_server_new_for_domain_socket (const char *path,
}
}
listen_fd = _dbus_listen_unix_socket (path, abstract, error);
listen_fd.fd = _dbus_listen_unix_socket (path, abstract, error);
if (listen_fd < 0)
if (listen_fd.fd < 0)
{
_DBUS_ASSERT_ERROR_IS_SET (error);
goto failed_1;
......
......@@ -120,7 +120,7 @@ _dbus_babysitter_new (void)
sitter->child_handle = NULL;
sitter->socket_to_babysitter = sitter->socket_to_main = -1;
sitter->socket_to_babysitter = sitter->socket_to_main = _dbus_socket_get_invalid ();
sitter->argc = 0;
sitter->argv = NULL;
......@@ -171,10 +171,10 @@ close_socket_to_babysitter (DBusBabysitter *sitter)
sitter->sitter_watch = NULL;
}
if (sitter->socket_to_babysitter != DBUS_SOCKET_INVALID)
if (sitter->socket_to_babysitter.sock != INVALID_SOCKET)
{
_dbus_close_socket (sitter->socket_to_babysitter, NULL);
sitter->socket_to_babysitter = DBUS_SOCKET_INVALID;
sitter->socket_to_babysitter.sock = INVALID_SOCKET;
}
}
......@@ -198,10 +198,10 @@ _dbus_babysitter_unref (DBusBabysitter *sitter)
{
close_socket_to_babysitter (sitter);
if (sitter->socket_to_main != DBUS_SOCKET_INVALID)
if (sitter->socket_to_main.sock != INVALID_SOCKET)
{
_dbus_close_socket (sitter->socket_to_main, NULL);
sitter->socket_to_main = DBUS_SOCKET_INVALID;
sitter->socket_to_main.sock = INVALID_SOCKET;
}
PING();
......@@ -633,7 +633,7 @@ babysitter (void *parameter)
#endif
PING();
send (sitter->socket_to_main, " ", 1, 0);
send (sitter->socket_to_main.sock, " ", 1, 0);
_dbus_babysitter_unref (sitter);
......
......@@ -243,7 +243,7 @@ struct DBusBabysitter
char *log_name; /**< the name under which to log messages about this
process being spawned */
int socket_to_babysitter; /**< Connection to the babysitter process */
DBusSocket socket_to_babysitter; /**< Connection to the babysitter process */
int error_pipe_from_child; /**< Connection to the process that does the exec() */
pid_t sitter_pid; /**< PID Of the babysitter */
......@@ -275,7 +275,7 @@ _dbus_babysitter_new (void)
sitter->refcount = 1;
sitter->socket_to_babysitter = -1;
sitter->socket_to_babysitter.fd = -1;
sitter->error_pipe_from_child = -1;
sitter->sitter_pid = -1;
......@@ -538,10 +538,10 @@ close_socket_to_babysitter (DBusBabysitter *sitter)
sitter->sitter_watch = NULL;
}
if (sitter->socket_to_babysitter >= 0)
if (sitter->socket_to_babysitter.fd >= 0)
{
_dbus_close_socket (sitter->socket_to_babysitter, NULL);
sitter->socket_to_babysitter = -1;
sitter->socket_to_babysitter.fd = -1;
}
}
......@@ -561,7 +561,7 @@ close_error_pipe_from_child (DBusBabysitter *sitter)
if (sitter->error_pipe_from_child >= 0)
{
_dbus_close_socket (sitter->error_pipe_from_child, NULL);
_dbus_close (sitter->error_pipe_from_child, NULL);
sitter->error_pipe_from_child = -1;
}
}
......@@ -577,7 +577,7 @@ handle_babysitter_socket (DBusBabysitter *sitter,
if (revents & _DBUS_POLLIN)
{
_dbus_verbose ("Reading data from babysitter\n");
if (read_data (sitter, sitter->socket_to_babysitter) != READ_STATUS_OK)
if (read_data (sitter, sitter->socket_to_babysitter.fd) != READ_STATUS_OK)
close_socket_to_babysitter (sitter);
}
else if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
......@@ -623,9 +623,9 @@ babysitter_iteration (DBusBabysitter *sitter,
++i;
}
if (sitter->socket_to_babysitter >= 0)
if (sitter->socket_to_babysitter.fd >= 0)
{
fds[i].fd = sitter->socket_to_babysitter;
fds[i].fd = sitter->socket_to_babysitter.fd;
fds[i].events = _DBUS_POLLIN;
fds[i].revents = 0;
++i;
......@@ -659,7 +659,7 @@ babysitter_iteration (DBusBabysitter *sitter,
--i;
if (fds[i].fd == sitter->error_pipe_from_child)
handle_error_pipe (sitter, fds[i].revents);
else if (fds[i].fd == sitter->socket_to_babysitter)
else if (fds[i].fd == sitter->socket_to_babysitter.fd)
handle_babysitter_socket (sitter, fds[i].revents);
}
}
......@@ -672,7 +672,7 @@ babysitter_iteration (DBusBabysitter *sitter,
* Macro returns #TRUE if the babysitter still has live sockets open to the
* babysitter child or the grandchild.
*/
#define LIVE_CHILDREN(sitter) ((sitter)->socket_to_babysitter >= 0 || (sitter)->error_pipe_from_child >= 0)
#define LIVE_CHILDREN(sitter) ((sitter)->socket_to_babysitter.fd >= 0 || (sitter)->error_pipe_from_child >= 0)
/**
* Blocks until the babysitter process gives us the PID of the spawned grandchild,
......@@ -712,7 +712,7 @@ _dbus_babysitter_get_child_exited (DBusBabysitter *sitter)
;
/* We will have exited the babysitter when the child has exited */
return sitter->socket_to_babysitter < 0;
return sitter->socket_to_babysitter.fd < 0;
}
/**
......@@ -846,7 +846,7 @@ handle_watch (DBusWatch *watch,
if (fd == sitter->error_pipe_from_child)
handle_error_pipe (sitter, revents);
else if (fd == sitter->socket_to_babysitter)
else if (fd == sitter->socket_to_babysitter.fd)
handle_babysitter_socket (sitter, revents);
while (LIVE_CHILDREN (sitter) &&
......@@ -855,7 +855,7 @@ handle_watch (DBusWatch *watch,
/* fd.o #32992: if the handle_* methods closed their sockets, they previously
* didn't always remove the watches. Check that we don't regress. */
_dbus_assert (sitter->socket_to_babysitter != -1 || sitter->sitter_watch == NULL);
_dbus_assert (sitter->socket_to_babysitter.fd != -1 || sitter->sitter_watch == NULL);
_dbus_assert (sitter->error_pipe_from_child != -1 || sitter->error_watch == NULL);
if (_dbus_babysitter_get_child_exited (sitter) &&
......@@ -893,7 +893,7 @@ close_and_invalidate (int *fd)
return -1;
else
{
ret = _dbus_close_socket (*fd, NULL);
ret = _dbus_close (*fd, NULL);
*fd = -1;
}
......@@ -1217,7 +1217,7 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p,
{
DBusBabysitter *sitter;
int child_err_report_pipe[2] = { -1, -1 };
int babysitter_pipe[2] = { -1, -1 };
DBusSocket babysitter_pipe[2] = { DBUS_SOCKET_INIT, DBUS_SOCKET_INIT };
pid_t pid;
#ifdef HAVE_SYSTEMD
int fd_out = -1;
......@@ -1287,7 +1287,7 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p,
goto cleanup_and_fail;
}
sitter->sitter_watch = _dbus_watch_new (babysitter_pipe[0],
sitter->sitter_watch = _dbus_watch_new (babysitter_pipe[0].fd,
DBUS_WATCH_READABLE,
TRUE, handle_watch, sitter, NULL);
if (sitter->sitter_watch == NULL)
......@@ -1341,14 +1341,14 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p,
/* Close the parent's end of the pipes. */
close_and_invalidate (&child_err_report_pipe[READ_END]);
close_and_invalidate (&babysitter_pipe[0]);
close_and_invalidate (&babysitter_pipe[0].fd);
/* Create the child that will exec () */
grandchild_pid = fork ();
if (grandchild_pid < 0)
{
write_err_and_exit (babysitter_pipe[1],
write_err_and_exit (babysitter_pipe[1].fd,
CHILD_FORK_FAILED);
_dbus_assert_not_reached ("Got to code after write_err_and_exit()");
}
......@@ -1358,7 +1358,7 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p,
*/
signal (SIGPIPE, SIG_IGN);
close_and_invalidate (&babysitter_pipe[1]);
close_and_invalidate (&babysitter_pipe[1].fd);
#ifdef HAVE_SYSTEMD
/* log to systemd journal if possible */
if (fd_out >= 0)
......@@ -1381,7 +1381,7 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p,
close_and_invalidate (&fd_out);
close_and_invalidate (&fd_err);
#endif
babysit (grandchild_pid, babysitter_pipe[1]);
babysit (grandchild_pid, babysitter_pipe[1].fd);
_dbus_assert_not_reached ("Got to code after babysit()");
}
}
......@@ -1389,14 +1389,14 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p,
{
/* Close the uncared-about ends of the pipes */
close_and_invalidate (&child_err_report_pipe[WRITE_END]);
close_and_invalidate (&babysitter_pipe[1]);
close_and_invalidate (&babysitter_pipe[1].fd);
#ifdef HAVE_SYSTEMD
close_and_invalidate (&fd_out);
close_and_invalidate (&fd_err);
#endif
sitter->socket_to_babysitter = babysitter_pipe[0];
babysitter_pipe[0] = -1;
babysitter_pipe[0].fd = -1;
sitter->error_pipe_from_child = child_err_report_pipe[READ_END];
child_err_report_pipe[READ_END] = -1;
......@@ -1421,8 +1421,8 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p,
close_and_invalidate (&child_err_report_pipe[READ_END]);
close_and_invalidate (&child_err_report_pipe[WRITE_END]);
close_and_invalidate (&babysitter_pipe[0]);
close_and_invalidate (&babysitter_pipe[1]);
close_and_invalidate (&babysitter_pipe[0].fd);
close_and_invalidate (&babysitter_pipe[1].fd);
#ifdef HAVE_SYSTEMD
close_and_invalidate (&fd_out);
close_and_invalidate (&fd_err);
......
This diff is collapsed.
This diff is collapsed.
......@@ -122,37 +122,28 @@ typedef unsigned long dbus_gid_t;
/** an appropriate printf format for dbus_gid_t */
#define DBUS_GID_FORMAT "%lu"
/**
* Socket interface
*
* @todo Use for the file descriptors a struct
* - struct DBusSocket{ int d; }; -
* instead of int to get type-safety which
* will be checked by the compiler.
*
*/
#ifndef DBUS_WIN
typedef int DBusSocket;
# define DBUS_SOCKET_INVALID -1
typedef struct { int fd; } DBusSocket;
# define DBUS_SOCKET_FORMAT "d"
# define DBUS_SOCKET_PRINTABLE(s) (s)
# define DBUS_SOCKET_INIT -1
# define DBUS_SOCKET_IS_VALID(s) ((s) >= 0)
# define DBUS_SOCKET_INVALIDATE(s) ((s) = -1)
# define DBUS_SOCKET_GET_INT(s) (s)
# 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)
#else /* DBUS_WIN */
typedef SOCKET DBusSocket;
# define DBUS_SOCKET_INVALID INVALID_SOCKET
typedef struct { SOCKET sock; } DBusSocket;
# define DBUS_SOCKET_FORMAT "Iu"
# define DBUS_SOCKET_PRINTABLE(s) (s)
# define DBUS_SOCKET_INIT INVALID_SOCKET
# define DBUS_SOCKET_IS_VALID(s) ((s) != INVALID_SOCKET)
# define DBUS_SOCKET_INVALIDATE(s) ((s) = INVALID_SOCKET)
# define DBUS_SOCKET_GET_INT(s) ((int) (s))
# 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)
#endif /* DBUS_WIN */
......@@ -358,10 +349,10 @@ dbus_int32_t _dbus_atomic_get (DBusAtomic *atomic);
typedef DBusSocket DBusPollable;
# define DBUS_SOCKET_GET_POLLABLE(s) (s)
# define DBUS_POLLABLE_FORMAT "Iu"
# define DBUS_POLLABLE_PRINTABLE(p) (p)
# 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) == (b))
# define DBUS_POLLABLE_EQUALS(a, b) ((a).sock == (b).sock)
#else /* !DBUS_WIN */
......@@ -371,7 +362,7 @@ typedef DBusSocket DBusPollable;
* abstraction.)
*/
typedef int DBusPollable;
# define DBUS_SOCKET_GET_POLLABLE(s) (s)
# 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)
......
......@@ -59,7 +59,7 @@ _dbus_transport_new_for_domain_socket (const char *path,
dbus_bool_t abstract,
DBusError *error)
{
int fd;
DBusSocket fd = DBUS_SOCKET_INIT;
DBusTransport *transport;
DBusString address;
......@@ -71,8 +71,6 @@ _dbus_transport_new_for_domain_socket (const char *path,
return NULL;
}
fd = -1;
if ((abstract &&
!_dbus_string_append (&address, "unix:abstract=")) ||
(!abstract &&
......@@ -83,8 +81,8 @@ _dbus_transport_new_for_domain_socket (const char *path,
goto failed_0;
}
fd = _dbus_connect_unix_socket (path, abstract, error);
if (fd < 0)
fd.fd = _dbus_connect_unix_socket (path, abstract, error);
if (fd.fd < 0)
{
_DBUS_ASSERT_ERROR_IS_SET (error);
goto failed_0;
......@@ -127,7 +125,7 @@ _dbus_transport_new_for_exec (const char *path,
char *const argv[],
DBusError *error)
{
int fd;
DBusSocket fd = DBUS_SOCKET_INIT;
DBusTransport *transport;
DBusString address;
unsigned i;
......@@ -141,8 +139,6 @@ _dbus_transport_new_for_exec (const char *path,
return NULL;
}
fd = -1;
escaped = dbus_address_escape_value (path);
if (!escaped)
{
......@@ -184,8 +180,8 @@ _dbus_transport_new_for_exec (const char *path,
}
}
fd = _dbus_connect_exec (path, argv, error);
if (fd < 0)
fd.fd = _dbus_connect_exec (path, argv, error);
if (fd.fd < 0)
{
_DBUS_ASSERT_ERROR_IS_SET (error);
goto failed;
......@@ -206,7 +202,7 @@ _dbus_transport_new_for_exec (const char *path,
return transport;
failed:
if (fd >= 0)
if (fd.fd >= 0)
_dbus_close_socket (fd, NULL);
_dbus_string_free (&address);
......
......@@ -595,15 +595,27 @@ dbus_watch_get_socket (DBusWatch *watch)
{
_dbus_return_val_if_fail (watch != NULL, -1);
#ifdef DBUS_UNIX
return watch->fd;
#else
return DBUS_SOCKET_GET_INT (watch->fd);
#endif
}
DBusSocket
_dbus_watch_get_socket (DBusWatch *watch)
{
DBusSocket s;
_dbus_assert (watch != NULL);
return watch->fd;
#ifdef DBUS_UNIX
s.fd = watch->fd;
#else
s = watch->fd;
#endif
return s;
}
DBusPollable
......
......@@ -469,7 +469,7 @@ test_too_many_split (Fixture *f,
#ifdef HAVE_UNIX_FD_PASSING
DBusMessage *outgoing;
int i;
int left_client_socket;
DBusSocket left_client_socket;
char *payload;
int payload_len;
DBusString buffer;
......@@ -532,7 +532,8 @@ test_too_many_split (Fixture *f,
/* This is blatant cheating, and the API documentation specifically
* tells you not use this function in this way. Never do this
* in application code. */
if (!dbus_connection_get_socket (f->left_client_conn, &left_client_socket))
if (!dbus_connection_get_socket (f->left_client_conn,
&left_client_socket.fd))
g_error ("'unix:' DBusConnection should have had a socket");
/* Just to be sure that we're at a message boundary. */
......
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