/* return value is whether we successfully read any new data. */ static dbus_bool_t read_data_into_auth (DBusTransport *transport, dbus_bool_t *oom) { DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport; DBusString *buffer; int bytes_read; *oom = FALSE; _dbus_auth_get_buffer (transport->auth, &buffer); bytes_read = _dbus_read_socket (socket_transport->fd, buffer, socket_transport->max_bytes_read_per_iteration); _dbus_auth_return_buffer (transport->auth, buffer, bytes_read > 0 ? bytes_read : 0); if (bytes_read > 0) { _dbus_verbose (" read %d bytes in auth phase\n", bytes_read); return TRUE; } else if (bytes_read < 0) { /* EINTR already handled for us */ if (_dbus_get_is_errno_enomem ()) { *oom = TRUE; } else if (_dbus_get_is_errno_eagain_or_ewouldblock ()) ; /* do nothing, just return FALSE below */ else { _dbus_verbose ("Error reading from remote app: %s\n", _dbus_strerror_from_errno ()); do_io_error (transport); } return FALSE; } else { _dbus_assert (bytes_read == 0); _dbus_verbose ("Disconnected from remote app\n"); do_io_error (transport); return FALSE; } }
/** * sends the nonce over a given socket. Blocks while doing so. * * @param fd the file descriptor to write the nonce data to (usually a socket) * @param noncefile the noncefile location to read the nonce from * @param error contains error details if FALSE is returned * @return TRUE iff the nonce was successfully sent. Note that this does not * indicate whether the server accepted the nonce. */ dbus_bool_t _dbus_send_nonce (DBusSocket fd, const DBusString *noncefile, DBusError *error) { dbus_bool_t read_result; int send_result; DBusString nonce; _DBUS_ASSERT_ERROR_IS_CLEAR (error); if (_dbus_string_get_length (noncefile) == 0) return FALSE; if (!_dbus_string_init (&nonce)) { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return FALSE; } read_result = _dbus_read_nonce (noncefile, &nonce, error); if (!read_result) { _DBUS_ASSERT_ERROR_IS_SET (error); _dbus_string_free (&nonce); return FALSE; } _DBUS_ASSERT_ERROR_IS_CLEAR (error); send_result = _dbus_write_socket (fd, &nonce, 0, _dbus_string_get_length (&nonce)); _dbus_string_free (&nonce); if (send_result == -1) { dbus_set_error (error, _dbus_error_from_system_errno (), "Failed to send nonce (fd=%" DBUS_SOCKET_FORMAT "): %s", _dbus_socket_printable (fd), _dbus_strerror_from_errno ()); return FALSE; } return TRUE; }
/** * close a pipe. * * @param pipe the pipe instance * @param error return location for an error * @returns #FALSE if error is set */ int _dbus_pipe_close (DBusPipe *pipe, DBusError *error) { _DBUS_ASSERT_ERROR_IS_CLEAR (error); if (_close (pipe->fd) != 0) { dbus_set_error (error, _dbus_error_from_system_errno (), "Could not close pipe fd %d: %s", pipe->fd, _dbus_strerror_from_errno ()); return -1; } else { _dbus_pipe_invalidate (pipe); return 0; } }
/** * write data to a pipe. * * @param pipe the pipe instance * @param buffer the buffer to write data from * @param start the first byte in the buffer to write * @param len the number of bytes to try to write * @param error error return * @returns the number of bytes written or -1 on error */ int _dbus_pipe_write (DBusPipe *pipe, const DBusString *buffer, int start, int len, DBusError *error) { const char *buffer_c = _dbus_string_get_const_data (buffer); int written; written = _write (pipe->fd, buffer_c + start, len); if (written >= 0) return written; dbus_set_error (error, _dbus_error_from_system_errno (), "Writing to pipe: %s", _dbus_strerror_from_errno ()); return -1; }
/** * reads the nonce from the nonce file and stores it in a string * * @param fname the file to read the nonce from * @param nonce returns the nonce. Must be an initialized string, the nonce will be appended. * @param error error object to report possible errors * @return FALSE iff reading the nonce fails (error is set then) */ dbus_bool_t _dbus_read_nonce (const DBusString *fname, DBusString *nonce, DBusError* error) { FILE *fp; char buffer[17]; size_t nread; buffer[sizeof buffer - 1] = '\0'; _DBUS_ASSERT_ERROR_IS_CLEAR (error); _dbus_verbose ("reading nonce from file: %s\n", _dbus_string_get_const_data (fname)); fp = fopen (_dbus_string_get_const_data (fname), "rb"); if (!fp) { dbus_set_error (error, _dbus_error_from_system_errno (), "Failed to open %s for read: %s", _dbus_string_get_const_data (fname), _dbus_strerror_from_errno ()); return FALSE; } nread = fread (buffer, 1, sizeof buffer - 1, fp); fclose (fp); if (!nread) { dbus_set_error (error, DBUS_ERROR_FILE_NOT_FOUND, "Could not read nonce from file %s", _dbus_string_get_const_data (fname)); return FALSE; } if (!_dbus_string_append_len (nonce, buffer, sizeof buffer - 1 )) { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return FALSE; } return TRUE; }
/* Return value is whether we successfully wrote any bytes */ static dbus_bool_t write_data_from_auth (DBusTransport *transport) { DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport; int bytes_written; const DBusString *buffer; if (!_dbus_auth_get_bytes_to_send (transport->auth, &buffer)) return FALSE; bytes_written = _dbus_write_socket (socket_transport->fd, buffer, 0, _dbus_string_get_length (buffer)); if (bytes_written > 0) { _dbus_auth_bytes_sent (transport->auth, bytes_written); return TRUE; } else if (bytes_written < 0) { /* EINTR already handled for us */ if (_dbus_get_is_errno_eagain_or_ewouldblock ()) ; else { _dbus_verbose ("Error writing to remote app: %s\n", _dbus_strerror_from_errno ()); do_io_error (transport); } } return FALSE; }
/* returns false on out-of-memory */ static dbus_bool_t do_reading (DBusTransport *transport) { DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport; DBusString *buffer; int bytes_read; int total; dbus_bool_t oom; _dbus_verbose ("fd = %d\n",socket_transport->fd); /* No messages without authentication! */ if (!_dbus_transport_try_to_authenticate (transport)) return TRUE; oom = FALSE; total = 0; again: /* See if we've exceeded max messages and need to disable reading */ check_read_watch (transport); if (total > socket_transport->max_bytes_read_per_iteration) { _dbus_verbose ("%d bytes exceeds %d bytes read per iteration, returning\n", total, socket_transport->max_bytes_read_per_iteration); goto out; } _dbus_assert (socket_transport->read_watch != NULL || transport->disconnected); if (transport->disconnected) goto out; if (!dbus_watch_get_enabled (socket_transport->read_watch)) return TRUE; if (_dbus_auth_needs_decoding (transport->auth)) { /* Does fd passing even make sense with encoded data? */ _dbus_assert(!DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport)); if (_dbus_string_get_length (&socket_transport->encoded_incoming) > 0) bytes_read = _dbus_string_get_length (&socket_transport->encoded_incoming); else bytes_read = _dbus_read_socket (socket_transport->fd, &socket_transport->encoded_incoming, socket_transport->max_bytes_read_per_iteration); _dbus_assert (_dbus_string_get_length (&socket_transport->encoded_incoming) == bytes_read); if (bytes_read > 0) { _dbus_message_loader_get_buffer (transport->loader, &buffer); if (!_dbus_auth_decode_data (transport->auth, &socket_transport->encoded_incoming, buffer)) { _dbus_verbose ("Out of memory decoding incoming data\n"); _dbus_message_loader_return_buffer (transport->loader, buffer); oom = TRUE; goto out; } _dbus_message_loader_return_buffer (transport->loader, buffer); _dbus_string_set_length (&socket_transport->encoded_incoming, 0); _dbus_string_compact (&socket_transport->encoded_incoming, 2048); } } else { _dbus_message_loader_get_buffer (transport->loader, &buffer); #ifdef HAVE_UNIX_FD_PASSING if (DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport)) { int *fds, n_fds; if (!_dbus_message_loader_get_unix_fds(transport->loader, &fds, &n_fds)) { _dbus_verbose ("Out of memory reading file descriptors\n"); _dbus_message_loader_return_buffer (transport->loader, buffer); oom = TRUE; goto out; } bytes_read = _dbus_read_socket_with_unix_fds(socket_transport->fd, buffer, socket_transport->max_bytes_read_per_iteration, fds, &n_fds); if (bytes_read >= 0 && n_fds > 0) _dbus_verbose("Read %i unix fds\n", n_fds); _dbus_message_loader_return_unix_fds(transport->loader, fds, bytes_read < 0 ? 0 : n_fds); } else #endif { bytes_read = _dbus_read_socket (socket_transport->fd, buffer, socket_transport->max_bytes_read_per_iteration); } _dbus_message_loader_return_buffer (transport->loader, buffer); } if (bytes_read < 0) { /* EINTR already handled for us */ if (_dbus_get_is_errno_enomem ()) { _dbus_verbose ("Out of memory in read()/do_reading()\n"); oom = TRUE; goto out; } else if (_dbus_get_is_errno_eagain_or_ewouldblock ()) goto out; else { _dbus_verbose ("Error reading from remote app: %s\n", _dbus_strerror_from_errno ()); do_io_error (transport); goto out; } } else if (bytes_read == 0) { _dbus_verbose ("Disconnected from remote app\n"); do_io_error (transport); goto out; } else { _dbus_verbose (" read %d bytes\n", bytes_read); total += bytes_read; if (!_dbus_transport_queue_messages (transport)) { oom = TRUE; _dbus_verbose (" out of memory when queueing messages we just read in the transport\n"); goto out; } /* Try reading more data until we get EAGAIN and return, or * exceed max bytes per iteration. If in blocking mode of * course we'll block instead of returning. */ goto again; } out: if (oom) return FALSE; else return TRUE; }
/* returns false on oom */ static dbus_bool_t do_writing (DBusTransport *transport) { int total; DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport; dbus_bool_t oom; /* No messages without authentication! */ if (!_dbus_transport_try_to_authenticate (transport)) { _dbus_verbose ("Not authenticated, not writing anything\n"); return TRUE; } if (transport->disconnected) { _dbus_verbose ("Not connected, not writing anything\n"); return TRUE; } #if 1 _dbus_verbose ("do_writing(), have_messages = %d, fd = %d\n", _dbus_connection_has_messages_to_send_unlocked (transport->connection), socket_transport->fd); #endif oom = FALSE; total = 0; while (!transport->disconnected && _dbus_connection_has_messages_to_send_unlocked (transport->connection)) { int bytes_written; DBusMessage *message; const DBusString *header; const DBusString *body; int header_len, body_len; int total_bytes_to_write; if (total > socket_transport->max_bytes_written_per_iteration) { _dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n", total, socket_transport->max_bytes_written_per_iteration); goto out; } message = _dbus_connection_get_message_to_send (transport->connection); _dbus_assert (message != NULL); dbus_message_lock (message); #if 0 _dbus_verbose ("writing message %p\n", message); #endif _dbus_message_get_network_data (message, &header, &body); header_len = _dbus_string_get_length (header); body_len = _dbus_string_get_length (body); if (_dbus_auth_needs_encoding (transport->auth)) { /* Does fd passing even make sense with encoded data? */ _dbus_assert(!DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport)); if (_dbus_string_get_length (&socket_transport->encoded_outgoing) == 0) { if (!_dbus_auth_encode_data (transport->auth, header, &socket_transport->encoded_outgoing)) { oom = TRUE; goto out; } if (!_dbus_auth_encode_data (transport->auth, body, &socket_transport->encoded_outgoing)) { _dbus_string_set_length (&socket_transport->encoded_outgoing, 0); oom = TRUE; goto out; } } total_bytes_to_write = _dbus_string_get_length (&socket_transport->encoded_outgoing); #if 0 _dbus_verbose ("encoded message is %d bytes\n", total_bytes_to_write); #endif bytes_written = _dbus_write_socket (socket_transport->fd, &socket_transport->encoded_outgoing, socket_transport->message_bytes_written, total_bytes_to_write - socket_transport->message_bytes_written); } else { total_bytes_to_write = header_len + body_len; #if 0 _dbus_verbose ("message is %d bytes\n", total_bytes_to_write); #endif #ifdef HAVE_UNIX_FD_PASSING if (socket_transport->message_bytes_written <= 0 && DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport)) { /* Send the fds along with the first byte of the message */ const int *unix_fds; unsigned n; _dbus_message_get_unix_fds(message, &unix_fds, &n); bytes_written = _dbus_write_socket_with_unix_fds_two (socket_transport->fd, header, socket_transport->message_bytes_written, header_len - socket_transport->message_bytes_written, body, 0, body_len, unix_fds, n); if (bytes_written > 0 && n > 0) _dbus_verbose("Wrote %i unix fds\n", n); } else #endif { if (socket_transport->message_bytes_written < header_len) { bytes_written = _dbus_write_socket_two (socket_transport->fd, header, socket_transport->message_bytes_written, header_len - socket_transport->message_bytes_written, body, 0, body_len); } else { bytes_written = _dbus_write_socket (socket_transport->fd, body, (socket_transport->message_bytes_written - header_len), body_len - (socket_transport->message_bytes_written - header_len)); } } } if (bytes_written < 0) { /* EINTR already handled for us */ /* For some discussion of why we also ignore EPIPE here, see * http://lists.freedesktop.org/archives/dbus/2008-March/009526.html */ if (_dbus_get_is_errno_eagain_or_ewouldblock () || _dbus_get_is_errno_epipe ()) goto out; else { _dbus_verbose ("Error writing to remote app: %s\n", _dbus_strerror_from_errno ()); do_io_error (transport); goto out; } } else { _dbus_verbose (" wrote %d bytes of %d\n", bytes_written, total_bytes_to_write); total += bytes_written; socket_transport->message_bytes_written += bytes_written; _dbus_assert (socket_transport->message_bytes_written <= total_bytes_to_write); if (socket_transport->message_bytes_written == total_bytes_to_write) { socket_transport->message_bytes_written = 0; _dbus_string_set_length (&socket_transport->encoded_outgoing, 0); _dbus_string_compact (&socket_transport->encoded_outgoing, 2048); _dbus_connection_message_sent_unlocked (transport->connection, message); } } } out: if (oom) return FALSE; else return TRUE; }
/** * @todo We need to have a way to wake up the select sleep if * a new iteration request comes in with a flag (read/write) that * we're not currently serving. Otherwise a call that just reads * could block a write call forever (if there are no incoming * messages). */ static void socket_do_iteration (DBusTransport *transport, unsigned int flags, int timeout_milliseconds) { DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport; DBusPollFD poll_fd; int poll_res; int poll_timeout; _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p fd = %d\n", flags & DBUS_ITERATION_DO_READING ? "read" : "", flags & DBUS_ITERATION_DO_WRITING ? "write" : "", timeout_milliseconds, socket_transport->read_watch, socket_transport->write_watch, socket_transport->fd); /* the passed in DO_READING/DO_WRITING flags indicate whether to * read/write messages, but regardless of those we may need to block * for reading/writing to do auth. But if we do reading for auth, * we don't want to read any messages yet if not given DO_READING. */ poll_fd.fd = socket_transport->fd; poll_fd.events = 0; if (_dbus_transport_try_to_authenticate (transport)) { /* This is kind of a hack; if we have stuff to write, then try * to avoid the poll. This is probably about a 5% speedup on an * echo client/server. * * If both reading and writing were requested, we want to avoid this * since it could have funky effects: * - both ends spinning waiting for the other one to read * data so they can finish writing * - prioritizing all writing ahead of reading */ if ((flags & DBUS_ITERATION_DO_WRITING) && !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) && !transport->disconnected && _dbus_connection_has_messages_to_send_unlocked (transport->connection)) { do_writing (transport); if (transport->disconnected || !_dbus_connection_has_messages_to_send_unlocked (transport->connection)) goto out; } /* If we get here, we decided to do the poll() after all */ _dbus_assert (socket_transport->read_watch); if (flags & DBUS_ITERATION_DO_READING) poll_fd.events |= _DBUS_POLLIN; _dbus_assert (socket_transport->write_watch); if (flags & DBUS_ITERATION_DO_WRITING) poll_fd.events |= _DBUS_POLLOUT; } else { DBusAuthState auth_state; auth_state = _dbus_auth_do_work (transport->auth); if (transport->receive_credentials_pending || auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT) poll_fd.events |= _DBUS_POLLIN; if (transport->send_credentials_pending || auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND) poll_fd.events |= _DBUS_POLLOUT; } if (poll_fd.events) { if (flags & DBUS_ITERATION_BLOCK) poll_timeout = timeout_milliseconds; else poll_timeout = 0; /* For blocking selects we drop the connection lock here * to avoid blocking out connection access during a potentially * indefinite blocking call. The io path is still protected * by the io_path_cond condvar, so we won't reenter this. */ if (flags & DBUS_ITERATION_BLOCK) { _dbus_verbose ("unlock pre poll\n"); _dbus_connection_unlock (transport->connection); } again: poll_res = _dbus_poll (&poll_fd, 1, poll_timeout); if (poll_res < 0 && _dbus_get_is_errno_eintr ()) goto again; if (flags & DBUS_ITERATION_BLOCK) { _dbus_verbose ("lock post poll\n"); _dbus_connection_lock (transport->connection); } if (poll_res >= 0) { if (poll_res == 0) poll_fd.revents = 0; /* some concern that posix does not guarantee this; * valgrind flags it as an error. though it probably * is guaranteed on linux at least. */ if (poll_fd.revents & _DBUS_POLLERR) do_io_error (transport); else { dbus_bool_t need_read = (poll_fd.revents & _DBUS_POLLIN) > 0; dbus_bool_t need_write = (poll_fd.revents & _DBUS_POLLOUT) > 0; dbus_bool_t authentication_completed; _dbus_verbose ("in iteration, need_read=%d need_write=%d\n", need_read, need_write); do_authentication (transport, need_read, need_write, &authentication_completed); /* See comment in socket_handle_watch. */ if (authentication_completed) goto out; if (need_read && (flags & DBUS_ITERATION_DO_READING)) do_reading (transport); if (need_write && (flags & DBUS_ITERATION_DO_WRITING)) do_writing (transport); } } else { _dbus_verbose ("Error from _dbus_poll(): %s\n", _dbus_strerror_from_errno ()); } } out: /* We need to install the write watch only if we did not * successfully write everything. Note we need to be careful that we * don't call check_write_watch *before* do_writing, since it's * inefficient to add the write watch, and we can avoid it most of * the time since we can write immediately. * * However, we MUST always call check_write_watch(); DBusConnection code * relies on the fact that running an iteration will notice that * messages are pending. */ check_write_watch (transport); _dbus_verbose (" ... leaving do_iteration()\n"); }
static dbus_bool_t socket_handle_watch (DBusWatch *watch, unsigned int flags, void *data) { DBusServer *server = data; DBusServerSocket *socket_server = data; #ifndef DBUS_DISABLE_ASSERT int i; dbus_bool_t found = FALSE; #endif SERVER_LOCK (server); #ifndef DBUS_DISABLE_ASSERT for (i = 0 ; i < socket_server->n_fds ; i++) { if (socket_server->watch[i] == watch) found = TRUE; } _dbus_assert (found); #endif _dbus_verbose ("Handling client connection, flags 0x%x\n", flags); if (flags & DBUS_WATCH_READABLE) { int client_fd; int listen_fd; listen_fd = dbus_watch_get_socket (watch); if (socket_server->noncefile) client_fd = _dbus_accept_with_noncefile (listen_fd, socket_server->noncefile); else client_fd = _dbus_accept (listen_fd); if (client_fd < 0) { /* EINTR handled for us */ if (_dbus_get_is_errno_eagain_or_ewouldblock ()) _dbus_verbose ("No client available to accept after all\n"); else _dbus_verbose ("Failed to accept a client connection: %s\n", _dbus_strerror_from_errno ()); SERVER_UNLOCK (server); } else { if (!handle_new_client_fd_and_unlock (server, client_fd)) _dbus_verbose ("Rejected client connection due to lack of memory\n"); } } if (flags & DBUS_WATCH_ERROR) _dbus_verbose ("Error on server listening socket\n"); if (flags & DBUS_WATCH_HANGUP) _dbus_verbose ("Hangup on server listening socket\n"); return TRUE; }