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))
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
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
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");
_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
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
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
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
}
}
- 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;
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;
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;
}
}
{
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();
#endif
PING();
- send (sitter->socket_to_main, " ", 1, 0);
+ send (sitter->socket_to_main.sock, " ", 1, 0);
_dbus_babysitter_unref (sitter);
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 */
sitter->refcount = 1;
- sitter->socket_to_babysitter = -1;
+ sitter->socket_to_babysitter.fd = -1;
sitter->error_pipe_from_child = -1;
sitter->sitter_pid = -1;
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;
}
}
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;
}
}
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))
++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;
--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);
}
}
* 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,
;
/* We will have exited the babysitter when the child has exited */
- return sitter->socket_to_babysitter < 0;
+ return sitter->socket_to_babysitter.fd < 0;
}
/**
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) &&
/* 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) &&
return -1;
else
{
- ret = _dbus_close_socket (*fd, NULL);
+ ret = _dbus_close (*fd, NULL);
*fd = -1;
}
{
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;
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)
/* 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()");
}
*/
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)
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()");
}
}
{
/* 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;
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);
_dbus_close_socket (DBusSocket fd,
DBusError *error)
{
- return _dbus_close (fd, error);
+ return _dbus_close (fd.fd, error);
}
/**
DBusString *buffer,
int count)
{
- return _dbus_read (fd, buffer, count);
+ return _dbus_read (fd.fd, buffer, count);
}
/**
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;
return bytes_written;
#else
- return _dbus_write (fd, buffer, start, len);
+ return _dbus_write (fd.fd, buffer, start, len);
#endif
}
again:
- bytes_read = recvmsg(fd, &m, 0
+ bytes_read = recvmsg (fd.fd, &m, 0
#ifdef MSG_CMSG_CLOEXEC
|MSG_CMSG_CLOEXEC
#endif
again:
- bytes_written = sendmsg (fd, &m, 0
+ bytes_written = sendmsg (fd.fd, &m, 0
#if HAVE_DECL_MSG_NOSIGNAL
|MSG_NOSIGNAL
#endif
again:
- bytes_written = sendmsg (fd, &m, MSG_NOSIGNAL);
+ bytes_written = sendmsg (fd.fd, &m, MSG_NOSIGNAL);
if (bytes_written < 0 && errno == EINTR)
goto again;
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
}
goto fail;
}
- new_fds[fd - SD_LISTEN_FDS_START] = fd;
+ new_fds[fd - SD_LISTEN_FDS_START].fd = fd;
}
*fds = new_fds;
DBusError *error)
{
int saved_errno = 0;
- int fd = -1, res;
+ DBusSocket fd = DBUS_SOCKET_INIT;
+ int res;
struct addrinfo hints;
struct addrinfo *ai, *tmp;
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;
_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;
}
}
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)
_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;
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))
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;
}
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;
}
#endif
again:
- bytes_read = recvmsg (client_fd, &msg, 0);
+ bytes_read = recvmsg (client_fd.fd, &msg, 0);
if (bytes_read < 0)
{
#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));
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));
* 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);
*/
uid_t euid;
gid_t egid;
- if (getpeereid (client_fd, &euid, &egid) == 0)
+ if (getpeereid (client_fd.fd, &euid, &egid) == 0)
{
uid_read = euid;
}
}
}
- 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;
{
_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;
* 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;
_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
DBusError *error)
{
#ifdef HAVE_SOCKETPAIR
- DBusSocket fds[2];
+ int fds[2];
int retval;
#ifdef SOCK_CLOEXEC
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
_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;
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)
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)
{
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)
{
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
again:
- if (closesocket (fd) == SOCKET_ERROR)
+ if (closesocket (fd.sock) == SOCKET_ERROR)
{
DBUS_SOCKET_SET_ERRNO ();
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;
}
_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;
}
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,
}
temp = socket (AF_INET, SOCK_STREAM, 0);
- if (temp == DBUS_SOCKET_INVALID)
+ if (temp == INVALID_SOCKET)
{
DBUS_SOCKET_SET_ERRNO ();
goto out0;
}
socket1 = socket (AF_INET, SOCK_STREAM, 0);
- if (socket1 == DBUS_SOCKET_INVALID)
+ if (socket1 == INVALID_SOCKET)
{
DBUS_SOCKET_SET_ERRNO ();
goto out0;
}
socket2 = accept (temp, (struct sockaddr *) &saddr, &len);
- if (socket2 == DBUS_SOCKET_INVALID)
+ if (socket2 == INVALID_SOCKET)
{
DBUS_SOCKET_SET_ERRNO ();
goto out1;
}
}
- *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);
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
if (fdp->events & _DBUS_POLLOUT)
lNetworkEvents |= FD_WRITE | FD_CONNECT;
- WSAEventSelect(fdp->fd, ev, lNetworkEvents);
+ WSAEventSelect(fdp->fd.sock, ev, lNetworkEvents);
pEvents[i] = ev;
}
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;
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");
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;
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
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
{
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);
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;
}
}
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;
if (!_dbus_win_startup_winsock ())
{
_DBUS_SET_OOM (error);
- return -1;
+ return _dbus_socket_get_invalid ();
}
_DBUS_ZERO (hints);
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;
_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,
"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;
}
}
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)
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);
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;
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,
}
_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
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;
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)
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;
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;
}
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))
{
goto retry;
}
- _dbus_verbose ("client fd %d accepted\n", client_fd);
+ _dbus_verbose ("client fd %Iu accepted\n", client_fd.sock);
return client_fd;
}
_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;
/** 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 */
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 */
* 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)
dbus_bool_t abstract,
DBusError *error)
{
- int fd;
+ DBusSocket fd = DBUS_SOCKET_INIT;
DBusTransport *transport;
DBusString address;
return NULL;
}
- fd = -1;
-
if ((abstract &&
!_dbus_string_append (&address, "unix:abstract=")) ||
(!abstract &&
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;
char *const argv[],
DBusError *error)
{
- int fd;
+ DBusSocket fd = DBUS_SOCKET_INIT;
DBusTransport *transport;
DBusString address;
unsigned i;
return NULL;
}
- fd = -1;
-
escaped = dbus_address_escape_value (path);
if (!escaped)
{
}
}
- 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;
return transport;
failed:
- if (fd >= 0)
+ if (fd.fd >= 0)
_dbus_close_socket (fd, NULL);
_dbus_string_free (&address);
{
_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
#ifdef HAVE_UNIX_FD_PASSING
DBusMessage *outgoing;
int i;
- int left_client_socket;
+ DBusSocket left_client_socket;
char *payload;
int payload_len;
DBusString buffer;
/* 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. */