From bf1cee6221d0e31ec46466df026a3614a3a06b59 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 12 Mar 2015 20:44:42 +0000 Subject: [PATCH 11/11] 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. --- bus/dispatch.c | 6 +- bus/main.c | 6 +- dbus/dbus-hash.h | 11 ++-- dbus/dbus-server-unix.c | 4 +- dbus/dbus-spawn-win.c | 12 ++-- dbus/dbus-spawn.c | 48 +++++++------- dbus/dbus-sysdeps-unix.c | 99 ++++++++++++++-------------- dbus/dbus-sysdeps-win.c | 157 +++++++++++++++++++++++---------------------- dbus/dbus-sysdeps.h | 39 +++++------ dbus/dbus-transport-unix.c | 18 ++---- dbus/dbus-watch.c | 14 +++- test/fdpass.c | 5 +- 12 files changed, 212 insertions(+), 207 deletions(-) diff --git a/bus/dispatch.c b/bus/dispatch.c index 10b3acf..3ea944a 100644 --- a/bus/dispatch.c +++ b/bus/dispatch.c @@ -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)) diff --git a/bus/main.c b/bus/main.c index d9b9a8d..0c30f50 100644 --- a/bus/main.c +++ b/bus/main.c @@ -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"); diff --git a/dbus/dbus-hash.h b/dbus/dbus-hash.h index cc66b28..2898f51 100644 --- a/dbus/dbus-hash.h +++ b/dbus/dbus-hash.h @@ -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 diff --git a/dbus/dbus-server-unix.c b/dbus/dbus-server-unix.c index 7616033..0f67de3 100644 --- a/dbus/dbus-server-unix.c +++ b/dbus/dbus-server-unix.c @@ -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; diff --git a/dbus/dbus-spawn-win.c b/dbus/dbus-spawn-win.c index c2212a9..7f68f14 100644 --- a/dbus/dbus-spawn-win.c +++ b/dbus/dbus-spawn-win.c @@ -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); diff --git a/dbus/dbus-spawn.c b/dbus/dbus-spawn.c index 86161e9..ddd254d 100644 --- a/dbus/dbus-spawn.c +++ b/dbus/dbus-spawn.c @@ -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); diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index db973d3..f8b503f 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -208,7 +208,7 @@ dbus_bool_t _dbus_close_socket (DBusSocket fd, DBusError *error) { - return _dbus_close (fd, error); + return _dbus_close (fd.fd, error); } /** @@ -225,7 +225,7 @@ _dbus_read_socket (DBusSocket fd, DBusString *buffer, int count) { - return _dbus_read (fd, buffer, count); + return _dbus_read (fd.fd, buffer, count); } /** @@ -252,7 +252,7 @@ _dbus_write_socket (DBusSocket fd, again: - bytes_written = send (fd, data, len, MSG_NOSIGNAL); + bytes_written = send (fd.fd, data, len, MSG_NOSIGNAL); if (bytes_written < 0 && errno == EINTR) goto again; @@ -260,7 +260,7 @@ _dbus_write_socket (DBusSocket fd, return bytes_written; #else - return _dbus_write (fd, buffer, start, len); + return _dbus_write (fd.fd, buffer, start, len); #endif } @@ -335,7 +335,7 @@ _dbus_read_socket_with_unix_fds (DBusSocket fd, again: - bytes_read = recvmsg(fd, &m, 0 + bytes_read = recvmsg (fd.fd, &m, 0 #ifdef MSG_CMSG_CLOEXEC |MSG_CMSG_CLOEXEC #endif @@ -518,7 +518,7 @@ _dbus_write_socket_with_unix_fds_two(DBusSocket fd, again: - bytes_written = sendmsg (fd, &m, 0 + bytes_written = sendmsg (fd.fd, &m, 0 #if HAVE_DECL_MSG_NOSIGNAL |MSG_NOSIGNAL #endif @@ -593,7 +593,7 @@ _dbus_write_socket_two (DBusSocket fd, again: - bytes_written = sendmsg (fd, &m, MSG_NOSIGNAL); + bytes_written = sendmsg (fd.fd, &m, MSG_NOSIGNAL); if (bytes_written < 0 && errno == EINTR) goto again; @@ -601,7 +601,7 @@ _dbus_write_socket_two (DBusSocket fd, return bytes_written; #else - return _dbus_write_two (fd, buffer1, start1, len1, + return _dbus_write_two (fd.fd, buffer1, start1, len1, buffer2, start2, len2); #endif } @@ -1221,7 +1221,7 @@ _dbus_listen_systemd_sockets (DBusSocket **fds, goto fail; } - new_fds[fd - SD_LISTEN_FDS_START] = fd; + new_fds[fd - SD_LISTEN_FDS_START].fd = fd; } *fds = new_fds; @@ -1273,7 +1273,8 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, DBusError *error) { int saved_errno = 0; - int fd = -1, res; + DBusSocket fd = DBUS_SOCKET_INIT; + int res; struct addrinfo hints; struct addrinfo *ai, *tmp; @@ -1292,7 +1293,7 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS, "Unknown address family %s", family); - return -1; + return _dbus_socket_get_invalid (); } hints.ai_protocol = IPPROTO_TCP; hints.ai_socktype = SOCK_STREAM; @@ -1304,25 +1305,25 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, _dbus_error_from_errno (errno), "Failed to lookup host/port: \"%s:%s\": %s (%d)", host, port, gai_strerror(res), res); - return -1; + return _dbus_socket_get_invalid (); } tmp = ai; while (tmp) { - if (!_dbus_open_socket (&fd, tmp->ai_family, SOCK_STREAM, 0, error)) + if (!_dbus_open_socket (&fd.fd, tmp->ai_family, SOCK_STREAM, 0, error)) { freeaddrinfo(ai); _DBUS_ASSERT_ERROR_IS_SET(error); - return -1; + return _dbus_socket_get_invalid (); } _DBUS_ASSERT_ERROR_IS_CLEAR(error); - if (connect (fd, (struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen) < 0) + if (connect (fd.fd, (struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen) < 0) { saved_errno = errno; - _dbus_close(fd, NULL); - fd = -1; + _dbus_close (fd.fd, NULL); + fd.fd = -1; tmp = tmp->ai_next; continue; } @@ -1331,13 +1332,13 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, } freeaddrinfo(ai); - if (fd == -1) + if (fd.fd == -1) { dbus_set_error (error, _dbus_error_from_errno (saved_errno), "Failed to connect to socket \"%s:%s\" %s", host, port, _dbus_strerror(saved_errno)); - return -1; + return _dbus_socket_get_invalid (); } if (noncefile != NULL) @@ -1349,16 +1350,16 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, _dbus_string_free (&noncefileStr); if (!ret) - { - _dbus_close (fd, NULL); - return -1; + { + _dbus_close (fd.fd, NULL); + return _dbus_socket_get_invalid (); } } - if (!_dbus_set_fd_nonblocking (fd, error)) + if (!_dbus_set_fd_nonblocking (fd.fd, error)) { - _dbus_close (fd, NULL); - return -1; + _dbus_close (fd.fd, NULL); + return _dbus_socket_get_invalid (); } return fd; @@ -1506,7 +1507,7 @@ _dbus_listen_tcp_socket (const char *host, goto failed; } listen_fd = newlisten_fd; - listen_fd[nlisten_fd] = fd; + listen_fd[nlisten_fd].fd = fd; nlisten_fd++; if (!_dbus_string_get_length(retport)) @@ -1572,7 +1573,7 @@ _dbus_listen_tcp_socket (const char *host, for (i = 0 ; i < nlisten_fd ; i++) { - if (!_dbus_set_fd_nonblocking (listen_fd[i], error)) + if (!_dbus_set_fd_nonblocking (listen_fd[i].fd, error)) { goto failed; } @@ -1586,7 +1587,7 @@ _dbus_listen_tcp_socket (const char *host, if (ai) freeaddrinfo(ai); for (i = 0 ; i < nlisten_fd ; i++) - _dbus_close(listen_fd[i], NULL); + _dbus_close(listen_fd[i].fd, NULL); dbus_free(listen_fd); return -1; } @@ -1854,7 +1855,7 @@ _dbus_read_credentials_socket (DBusSocket client_fd, #endif again: - bytes_read = recvmsg (client_fd, &msg, 0); + bytes_read = recvmsg (client_fd.fd, &msg, 0); if (bytes_read < 0) { @@ -1906,7 +1907,7 @@ _dbus_read_credentials_socket (DBusSocket client_fd, #endif int cr_len = sizeof (cr); - if (getsockopt (client_fd, SOL_SOCKET, SO_PEERCRED, &cr, &cr_len) != 0) + if (getsockopt (client_fd.fd, SOL_SOCKET, SO_PEERCRED, &cr, &cr_len) != 0) { _dbus_verbose ("Failed to getsockopt(SO_PEERCRED): %s\n", _dbus_strerror (errno)); @@ -1927,7 +1928,7 @@ _dbus_read_credentials_socket (DBusSocket client_fd, struct unpcbid cr; socklen_t cr_len = sizeof (cr); - if (getsockopt (client_fd, 0, LOCAL_PEEREID, &cr, &cr_len) != 0) + if (getsockopt (client_fd.fd, 0, LOCAL_PEEREID, &cr, &cr_len) != 0) { _dbus_verbose ("Failed to getsockopt(LOCAL_PEEREID): %s\n", _dbus_strerror (errno)); @@ -1974,7 +1975,7 @@ _dbus_read_credentials_socket (DBusSocket client_fd, * up this list, because it carries the pid and we use this code path * for audit data. */ ucred_t * ucred = NULL; - if (getpeerucred (client_fd, &ucred) == 0) + if (getpeerucred (client_fd.fd, &ucred) == 0) { pid_read = ucred_getpid (ucred); uid_read = ucred_geteuid (ucred); @@ -2040,7 +2041,7 @@ _dbus_read_credentials_socket (DBusSocket client_fd, */ uid_t euid; gid_t egid; - if (getpeereid (client_fd, &euid, &egid) == 0) + if (getpeereid (client_fd.fd, &euid, &egid) == 0) { uid_read = euid; } @@ -2093,7 +2094,7 @@ _dbus_read_credentials_socket (DBusSocket client_fd, } } - if (!add_linux_security_label_to_credentials (client_fd, credentials)) + if (!add_linux_security_label_to_credentials (client_fd.fd, credentials)) { _DBUS_SET_OOM (error); return FALSE; @@ -2125,7 +2126,7 @@ _dbus_send_credentials_socket (DBusSocket server_fd, { _DBUS_ASSERT_ERROR_IS_CLEAR (error); - if (write_credentials_byte (server_fd, error)) + if (write_credentials_byte (server_fd.fd, error)) return TRUE; else return FALSE; @@ -2160,28 +2161,28 @@ _dbus_accept (DBusSocket listen_fd) * libc headers, SOCK_CLOEXEC is too. At runtime, it is still * not necessarily true that either is supported by the running kernel. */ - client_fd = accept4 (listen_fd, &addr, &addrlen, SOCK_CLOEXEC); - cloexec_done = client_fd >= 0; + client_fd.fd = accept4 (listen_fd.fd, &addr, &addrlen, SOCK_CLOEXEC); + cloexec_done = client_fd.fd >= 0; - if (client_fd < 0 && (errno == ENOSYS || errno == EINVAL)) + if (client_fd.fd < 0 && (errno == ENOSYS || errno == EINVAL)) #endif { - client_fd = accept (listen_fd, &addr, &addrlen); + client_fd.fd = accept (listen_fd.fd, &addr, &addrlen); } - if (client_fd < 0) + if (client_fd.fd < 0) { if (errno == EINTR) goto retry; } - _dbus_verbose ("client fd %d accepted\n", client_fd); + _dbus_verbose ("client fd %d accepted\n", client_fd.fd); #ifdef HAVE_ACCEPT4 if (!cloexec_done) #endif { - _dbus_fd_set_close_on_exec(client_fd); + _dbus_fd_set_close_on_exec(client_fd.fd); } return client_fd; @@ -3210,7 +3211,7 @@ dbus_bool_t _dbus_set_socket_nonblocking (DBusSocket fd, DBusError *error) { - return _dbus_set_fd_nonblocking (fd, error); + return _dbus_set_fd_nonblocking (fd.fd, error); } static dbus_bool_t @@ -3300,7 +3301,7 @@ _dbus_socketpair (DBusSocket *fd1, DBusError *error) { #ifdef HAVE_SOCKETPAIR - DBusSocket fds[2]; + int fds[2]; int retval; #ifdef SOCK_CLOEXEC @@ -3345,11 +3346,11 @@ _dbus_socketpair (DBusSocket *fd1, return FALSE; } - *fd1 = fds[0]; - *fd2 = fds[1]; + fd1->fd = fds[0]; + fd2->fd = fds[1]; _dbus_verbose ("full-duplex pipe %d <-> %d\n", - *fd1, *fd2); + fd1->fd, fd2->fd); return TRUE; #else @@ -4243,7 +4244,7 @@ _dbus_socket_can_pass_unix_fd (DBusSocket fd) _DBUS_ZERO(sa_buf); - if (getsockname(fd, &sa_buf.sa, &sa_len) < 0) + if (getsockname(fd.fd, &sa_buf.sa, &sa_len) < 0) return FALSE; return sa_buf.sa.sa_family == AF_UNIX; @@ -4397,7 +4398,7 @@ _dbus_append_address_from_socket (DBusSocket fd, int size = sizeof (socket); DBusString path_str; - if (getsockname (fd, &socket.sa, &size)) + if (getsockname (fd.fd, &socket.sa, &size)) goto err; switch (socket.sa.sa_family) diff --git a/dbus/dbus-sysdeps-win.c b/dbus/dbus-sysdeps-win.c index e67821a..30a09a6 100644 --- a/dbus/dbus-sysdeps-win.c +++ b/dbus/dbus-sysdeps-win.c @@ -397,8 +397,8 @@ _dbus_read_socket (DBusSocket fd, again: - _dbus_verbose ("recv: count=%d fd=%d\n", count, fd); - bytes_read = recv (fd, data, count, 0); + _dbus_verbose ("recv: count=%d fd=%Iu\n", count, fd.sock); + bytes_read = recv (fd.sock, data, count, 0); if (bytes_read == SOCKET_ERROR) { @@ -457,8 +457,8 @@ _dbus_write_socket (DBusSocket fd, again: - _dbus_verbose ("send: len=%d fd=%d\n", len, fd); - bytes_written = send (fd, data, len, 0); + _dbus_verbose ("send: len=%d fd=%Iu\n", len, fd.sock); + bytes_written = send (fd.sock, data, len, 0); if (bytes_written == SOCKET_ERROR) { @@ -495,7 +495,7 @@ _dbus_close_socket (DBusSocket fd, _DBUS_ASSERT_ERROR_IS_CLEAR (error); again: - if (closesocket (fd) == SOCKET_ERROR) + if (closesocket (fd.sock) == SOCKET_ERROR) { DBUS_SOCKET_SET_ERRNO (); @@ -503,11 +503,11 @@ _dbus_close_socket (DBusSocket fd, goto again; dbus_set_error (error, _dbus_error_from_errno (errno), - "Could not close socket: socket=%d, , %s", - fd, _dbus_strerror_from_errno ()); + "Could not close socket: socket=%Iu, , %s", + fd.sock, _dbus_strerror_from_errno ()); return FALSE; } - _dbus_verbose ("_dbus_close_socket: socket=%d, \n", fd); + _dbus_verbose ("_dbus_close_socket: socket=%Iu, \n", fd.sock); return TRUE; } @@ -545,12 +545,12 @@ _dbus_set_socket_nonblocking (DBusSocket handle, _DBUS_ASSERT_ERROR_IS_CLEAR (error); - if (ioctlsocket (handle, FIONBIO, &one) == SOCKET_ERROR) + if (ioctlsocket (handle.sock, FIONBIO, &one) == SOCKET_ERROR) { DBUS_SOCKET_SET_ERRNO (); dbus_set_error (error, _dbus_error_from_errno (errno), - "Failed to set socket %d:%d to nonblocking: %s", handle, - _dbus_strerror_from_errno ()); + "Failed to set socket %Iu to nonblocking: %s", + handle.sock, _dbus_strerror_from_errno ()); return FALSE; } @@ -618,8 +618,8 @@ _dbus_write_socket_two (DBusSocket fd, again: - _dbus_verbose ("WSASend: len1+2=%d+%d fd=%d\n", len1, len2, fd); - rc = WSASend (fd, + _dbus_verbose ("WSASend: len1+2=%d+%d fd=%Iu\n", len1, len2, fd.sock); + rc = WSASend (fd.sock, vectors, data2 ? 2 : 1, &bytes_written, @@ -1065,7 +1065,7 @@ _dbus_socketpair (DBusSocket *fd1, } temp = socket (AF_INET, SOCK_STREAM, 0); - if (temp == DBUS_SOCKET_INVALID) + if (temp == INVALID_SOCKET) { DBUS_SOCKET_SET_ERRNO (); goto out0; @@ -1096,7 +1096,7 @@ _dbus_socketpair (DBusSocket *fd1, } socket1 = socket (AF_INET, SOCK_STREAM, 0); - if (socket1 == DBUS_SOCKET_INVALID) + if (socket1 == INVALID_SOCKET) { DBUS_SOCKET_SET_ERRNO (); goto out0; @@ -1109,7 +1109,7 @@ _dbus_socketpair (DBusSocket *fd1, } socket2 = accept (temp, (struct sockaddr *) &saddr, &len); - if (socket2 == DBUS_SOCKET_INVALID) + if (socket2 == INVALID_SOCKET) { DBUS_SOCKET_SET_ERRNO (); goto out1; @@ -1132,11 +1132,11 @@ _dbus_socketpair (DBusSocket *fd1, } } - *fd1 = socket1; - *fd2 = socket2; + fd1->sock = socket1; + fd2->sock = socket2; - _dbus_verbose ("full-duplex pipe %d:%d <-> %d:%d\n", - *fd1, socket1, *fd2, socket2); + _dbus_verbose ("full-duplex pipe %Iu:%Iu <-> %Iu:%Iu\n", + fd1->sock, socket1, fd2->sock, socket2); closesocket (temp); @@ -1200,12 +1200,12 @@ _dbus_poll (DBusPollFD *fds, if (fdp->events & _DBUS_POLLIN) - msgp += sprintf (msgp, "R:%d ", fdp->fd); + msgp += sprintf (msgp, "R:%Iu ", fdp->fd.sock); if (fdp->events & _DBUS_POLLOUT) - msgp += sprintf (msgp, "W:%d ", fdp->fd); + msgp += sprintf (msgp, "W:%Iu ", fdp->fd.sock); - msgp += sprintf (msgp, "E:%d\n\t", fdp->fd); + msgp += sprintf (msgp, "E:%Iu\n\t", fdp->fd.sock); // FIXME: more robust code for long msg // create on heap when msg[] becomes too small @@ -1232,7 +1232,7 @@ _dbus_poll (DBusPollFD *fds, if (fdp->events & _DBUS_POLLOUT) lNetworkEvents |= FD_WRITE | FD_CONNECT; - WSAEventSelect(fdp->fd, ev, lNetworkEvents); + WSAEventSelect(fdp->fd.sock, ev, lNetworkEvents); pEvents[i] = ev; } @@ -1264,7 +1264,7 @@ _dbus_poll (DBusPollFD *fds, fdp->revents = 0; - WSAEnumNetworkEvents(fdp->fd, pEvents[i], &ne); + WSAEnumNetworkEvents(fdp->fd.sock, pEvents[i], &ne); if (ne.lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE)) fdp->revents |= _DBUS_POLLIN; @@ -1276,20 +1276,20 @@ _dbus_poll (DBusPollFD *fds, fdp->revents |= _DBUS_POLLERR; if (ne.lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE)) - msgp += sprintf (msgp, "R:%d ", fdp->fd); + msgp += sprintf (msgp, "R:%Iu ", fdp->fd.sock); if (ne.lNetworkEvents & (FD_WRITE | FD_CONNECT)) - msgp += sprintf (msgp, "W:%d ", fdp->fd); + msgp += sprintf (msgp, "W:%Iu ", fdp->fd.sock); if (ne.lNetworkEvents & (FD_OOB)) - msgp += sprintf (msgp, "E:%d ", fdp->fd); + msgp += sprintf (msgp, "E:%Iu ", fdp->fd.sock); msgp += sprintf (msgp, "lNetworkEvents:%d ", ne.lNetworkEvents); if(ne.lNetworkEvents) ret++; - WSAEventSelect(fdp->fd, pEvents[i], 0); + WSAEventSelect(fdp->fd.sock, pEvents[i], 0); } msgp += sprintf (msgp, "\n"); @@ -1318,7 +1318,7 @@ _dbus_poll (DBusPollFD *fds, char *msgp; fd_set read_set, write_set, err_set; - int max_fd = 0; + SOCKET max_fd = 0; int i; struct timeval tv; int ready; @@ -1337,12 +1337,12 @@ _dbus_poll (DBusPollFD *fds, if (fdp->events & _DBUS_POLLIN) - msgp += sprintf (msgp, "R:%d ", fdp->fd); + msgp += sprintf (msgp, "R:%Iu ", fdp->fd.sock); if (fdp->events & _DBUS_POLLOUT) - msgp += sprintf (msgp, "W:%d ", fdp->fd); + msgp += sprintf (msgp, "W:%Iu ", fdp->fd.sock); - msgp += sprintf (msgp, "E:%d\n\t", fdp->fd); + msgp += sprintf (msgp, "E:%Iu\n\t", fdp->fd.sock); // FIXME: more robust code for long msg // create on heap when msg[] becomes too small @@ -1360,14 +1360,14 @@ _dbus_poll (DBusPollFD *fds, DBusPollFD *fdp = &fds[i]; if (fdp->events & _DBUS_POLLIN) - FD_SET (fdp->fd, &read_set); + FD_SET (fdp->fd.sock, &read_set); if (fdp->events & _DBUS_POLLOUT) - FD_SET (fdp->fd, &write_set); + FD_SET (fdp->fd.sock, &write_set); - FD_SET (fdp->fd, &err_set); + FD_SET (fdp->fd.sock, &err_set); - max_fd = MAX (max_fd, fdp->fd); + max_fd = MAX (max_fd, fdp->fd.sock); } // Avoid random lockups with send(), for lack of a better solution so far @@ -1395,14 +1395,14 @@ _dbus_poll (DBusPollFD *fds, { DBusPollFD *fdp = &fds[i]; - if (FD_ISSET (fdp->fd, &read_set)) - msgp += sprintf (msgp, "R:%d ", fdp->fd); + if (FD_ISSET (fdp->fd.sock, &read_set)) + msgp += sprintf (msgp, "R:%Iu ", fdp->fd.sock); - if (FD_ISSET (fdp->fd, &write_set)) - msgp += sprintf (msgp, "W:%d ", fdp->fd); + if (FD_ISSET (fdp->fd.sock, &write_set)) + msgp += sprintf (msgp, "W:%Iu ", fdp->fd.sock); - if (FD_ISSET (fdp->fd, &err_set)) - msgp += sprintf (msgp, "E:%d\n\t", fdp->fd); + if (FD_ISSET (fdp->fd.sock, &err_set)) + msgp += sprintf (msgp, "E:%Iu\n\t", fdp->fd.sock); } msgp += sprintf (msgp, "\n"); _dbus_verbose ("%s",msg); @@ -1414,13 +1414,13 @@ _dbus_poll (DBusPollFD *fds, fdp->revents = 0; - if (FD_ISSET (fdp->fd, &read_set)) + if (FD_ISSET (fdp->fd.sock, &read_set)) fdp->revents |= _DBUS_POLLIN; - if (FD_ISSET (fdp->fd, &write_set)) + if (FD_ISSET (fdp->fd.sock, &write_set)) fdp->revents |= _DBUS_POLLOUT; - if (FD_ISSET (fdp->fd, &err_set)) + if (FD_ISSET (fdp->fd.sock, &err_set)) fdp->revents |= _DBUS_POLLERR; } } @@ -1500,7 +1500,8 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, const char *noncefile, DBusError *error) { - SOCKET fd = DBUS_SOCKET_INVALID, res; + DBusSocket fd = DBUS_SOCKET_INIT; + int res; struct addrinfo hints; struct addrinfo *ai, *tmp; @@ -1509,7 +1510,7 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, if (!_dbus_win_startup_winsock ()) { _DBUS_SET_OOM (error); - return -1; + return _dbus_socket_get_invalid (); } _DBUS_ZERO (hints); @@ -1525,7 +1526,7 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, "Unknown address family %s", family); - return -1; + return _dbus_socket_get_invalid (); } hints.ai_protocol = IPPROTO_TCP; hints.ai_socktype = SOCK_STREAM; @@ -1541,13 +1542,13 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, _dbus_error_from_errno (res), "Failed to lookup host/port: \"%s:%s\": %s (%d)", host, port, _dbus_strerror(res), res); - return -1; + return _dbus_socket_get_invalid (); } tmp = ai; while (tmp) { - if ((fd = socket (tmp->ai_family, SOCK_STREAM, 0)) == DBUS_SOCKET_INVALID) + if ((fd.sock = socket (tmp->ai_family, SOCK_STREAM, 0)) == INVALID_SOCKET) { DBUS_SOCKET_SET_ERRNO (); dbus_set_error (error, @@ -1555,15 +1556,15 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, "Failed to open socket: %s", _dbus_strerror_from_errno ()); freeaddrinfo(ai); - return -1; + return _dbus_socket_get_invalid (); } _DBUS_ASSERT_ERROR_IS_CLEAR(error); - if (connect (fd, (struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen) == SOCKET_ERROR) + if (connect (fd.sock, (struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen) == SOCKET_ERROR) { DBUS_SOCKET_SET_ERRNO (); - closesocket(fd); - fd = -1; + closesocket(fd.sock); + fd.sock = INVALID_SOCKET; tmp = tmp->ai_next; continue; } @@ -1572,13 +1573,13 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, } freeaddrinfo(ai); - if (fd == DBUS_SOCKET_INVALID) + if (!DBUS_SOCKET_IS_VALID (fd)) { dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to connect to socket \"%s:%s\" %s", host, port, _dbus_strerror_from_errno ()); - return -1; + return _dbus_socket_get_invalid (); } if (noncefile != NULL) @@ -1588,9 +1589,9 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, if (!_dbus_string_init (&noncefileStr) || !_dbus_string_append(&noncefileStr, noncefile)) { - closesocket (fd); + closesocket (fd.sock); dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); - return -1; + return _dbus_socket_get_invalid (); } ret = _dbus_send_nonce (fd, &noncefileStr, error); @@ -1599,18 +1600,18 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, if (!ret) { - closesocket (fd); - return -1; + closesocket (fd.sock); + return _dbus_socket_get_invalid (); } } /* Every SOCKET is also a HANDLE. */ - _dbus_win_handle_set_close_on_exec ((HANDLE) fd); + _dbus_win_handle_set_close_on_exec ((HANDLE) fd.sock); if (!_dbus_set_socket_nonblocking (fd, error)) { - closesocket (fd); - return -1; + closesocket (fd.sock); + return _dbus_socket_get_invalid (); } return fd; @@ -1700,8 +1701,8 @@ _dbus_listen_tcp_socket (const char *host, tmp = ai; while (tmp) { - DBusSocket fd = DBUS_SOCKET_INVALID, *newlisten_fd; - if ((fd = socket (tmp->ai_family, SOCK_STREAM, 0)) == DBUS_SOCKET_INVALID) + DBusSocket fd = DBUS_SOCKET_INIT, *newlisten_fd; + if ((fd.sock = socket (tmp->ai_family, SOCK_STREAM, 0)) == INVALID_SOCKET) { DBUS_SOCKET_SET_ERRNO (); dbus_set_error (error, @@ -1712,10 +1713,10 @@ _dbus_listen_tcp_socket (const char *host, } _DBUS_ASSERT_ERROR_IS_CLEAR(error); - if (bind (fd, (struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen) == SOCKET_ERROR) + if (bind (fd.sock, (struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen) == SOCKET_ERROR) { DBUS_SOCKET_SET_ERRNO (); - closesocket (fd); + closesocket (fd.sock); if (errno == WSAEADDRINUSE) { /* Calling this function with port=0 tries to @@ -1731,20 +1732,20 @@ _dbus_listen_tcp_socket (const char *host, goto failed; } - if (listen (fd, 30 /* backlog */) == SOCKET_ERROR) + if (listen (fd.sock, 30 /* backlog */) == SOCKET_ERROR) { DBUS_SOCKET_SET_ERRNO (); dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to listen on socket \"%s:%s\": %s", host ? host : "*", port, _dbus_strerror_from_errno ()); - closesocket (fd); + closesocket (fd.sock); goto failed; } - newlisten_fd = dbus_realloc(listen_fd, sizeof(int)*(nlisten_fd+1)); + newlisten_fd = dbus_realloc(listen_fd, sizeof(DBusSocket)*(nlisten_fd+1)); if (!newlisten_fd) { - closesocket (fd); + closesocket (fd.sock); dbus_set_error (error, DBUS_ERROR_NO_MEMORY, "Failed to allocate file handle array"); goto failed; @@ -1765,7 +1766,7 @@ _dbus_listen_tcp_socket (const char *host, socklen_t addrlen = sizeof(addr); char portbuf[NI_MAXSERV]; - if (getsockname(fd, &addr.Address, &addrlen) == SOCKET_ERROR || + if (getsockname(fd.sock, &addr.Address, &addrlen) == SOCKET_ERROR || (res = getnameinfo (&addr.Address, addrlen, NULL, 0, portbuf, sizeof(portbuf), NI_NUMERICSERV)) != 0) @@ -1815,7 +1816,7 @@ _dbus_listen_tcp_socket (const char *host, for (i = 0 ; i < nlisten_fd ; i++) { - _dbus_win_handle_set_close_on_exec ((HANDLE) listen_fd[i]); + _dbus_win_handle_set_close_on_exec ((HANDLE) listen_fd[i].sock); if (!_dbus_set_socket_nonblocking (listen_fd[i], error)) { goto failed; @@ -1830,7 +1831,7 @@ _dbus_listen_tcp_socket (const char *host, if (ai) freeaddrinfo(ai); for (i = 0 ; i < nlisten_fd ; i++) - closesocket (listen_fd[i]); + closesocket (listen_fd[i].sock); dbus_free(listen_fd); return -1; } @@ -1849,7 +1850,7 @@ _dbus_accept (DBusSocket listen_fd) DBusSocket client_fd; retry: - client_fd = accept (listen_fd, NULL, NULL); + client_fd.sock = accept (listen_fd.sock, NULL, NULL); if (!DBUS_SOCKET_IS_VALID (client_fd)) { @@ -1858,7 +1859,7 @@ _dbus_accept (DBusSocket listen_fd) goto retry; } - _dbus_verbose ("client fd %d accepted\n", client_fd); + _dbus_verbose ("client fd %Iu accepted\n", client_fd.sock); return client_fd; } @@ -1966,7 +1967,7 @@ _dbus_read_credentials_socket (DBusSocket handle, _dbus_string_free (&buf); } - pid = _dbus_get_peer_pid_from_tcp_handle (handle); + pid = _dbus_get_peer_pid_from_tcp_handle (handle.sock); if (pid == 0) return TRUE; diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index 4290edf..0622dcf 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -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 */ @@ -362,10 +353,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 */ @@ -375,7 +366,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) diff --git a/dbus/dbus-transport-unix.c b/dbus/dbus-transport-unix.c index 9a9fea5..f2b1f09 100644 --- a/dbus/dbus-transport-unix.c +++ b/dbus/dbus-transport-unix.c @@ -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); diff --git a/dbus/dbus-watch.c b/dbus/dbus-watch.c index 5e9dbb9..46f1d31 100644 --- a/dbus/dbus-watch.c +++ b/dbus/dbus-watch.c @@ -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 diff --git a/test/fdpass.c b/test/fdpass.c index 74235c6..335f456 100644 --- a/test/fdpass.c +++ b/test/fdpass.c @@ -477,7 +477,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; @@ -540,7 +540,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. */ -- 2.1.4