static void signal_handler (int sig) { DBusString str; switch (sig) { #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX case SIGIO: /* explicit fall-through */ #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */ case SIGHUP: _dbus_string_init_const (&str, "foo"); if (!_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1)) { _dbus_warn ("Unable to write to reload pipe.\n"); exit (1); } break; case SIGTERM: _dbus_loop_quit (bus_context_get_loop (context)); break; } }
static void signal_handler (int sig) { switch (sig) { #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX case SIGIO: /* explicit fall-through */ #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */ #ifdef SIGHUP case SIGHUP: { DBusString str; _dbus_string_init_const (&str, "foo"); if ((reload_pipe[RELOAD_WRITE_END] > 0) && !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1)) { _dbus_warn ("Unable to write to reload pipe.\n"); close_reload_pipe (); } } break; #endif } }
/** * 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; }
/* 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; int saved_errno; 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)); saved_errno = _dbus_save_socket_errno (); 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 (saved_errno)) ; else { _dbus_verbose ("Error writing to remote app: %s\n", _dbus_strerror (saved_errno)); do_io_error (transport); } } return FALSE; }
static void signal_handler (int sig) { switch (sig) { case SIGHUP: { DBusString str; char action[2] = { ACTION_RELOAD, '\0' }; _dbus_string_init_const (&str, action); if ((reload_pipe[RELOAD_WRITE_END] > 0) && !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1)) { /* If we receive SIGHUP often enough to fill the pipe buffer (4096 * times on old Linux, 65536 on modern Linux) before it can be * drained, let's just warn and ignore. The configuration will be * reloaded while draining the pipe buffer, which is what we * wanted. It's harmless that it will be reloaded fewer times than * we asked for, since the reload is delayed anyway, so new changes * will be picked up. * * We use write() because _dbus_warn uses vfprintf, which isn't * async-signal-safe. * * This is necessarily Unix-specific, but so are POSIX signals, * so... */ static const char message[] = "Unable to write to reload pipe - buffer full?\n"; if (write (STDERR_FILENO, message, strlen (message)) != strlen (message)) { /* ignore failure to write out a warning */ } } } break; case SIGTERM: { DBusString str; char action[2] = { ACTION_QUIT, '\0' }; _dbus_string_init_const (&str, action); if ((reload_pipe[RELOAD_WRITE_END] < 0) || !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1)) { /* If we can't write to the socket, dying seems a more * important response to SIGTERM than cleaning up sockets, * so we exit. We'd use exit(), but that's not async-signal-safe, * so we'll have to resort to _exit(). */ static const char message[] = "Unable to write termination signal to pipe - buffer full?\n" "Will exit instead.\n"; if (write (STDERR_FILENO, message, strlen (message)) != strlen (message)) { /* ignore failure to write out a warning */ } _exit (1); } } break; } }
/* 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 = %" DBUS_SOCKET_FORMAT "\n", _dbus_connection_has_messages_to_send_unlocked (transport->connection), _dbus_socket_printable (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; int saved_errno; 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); saved_errno = _dbus_save_socket_errno (); } 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); saved_errno = _dbus_save_socket_errno (); 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)); } saved_errno = _dbus_save_socket_errno (); } } if (bytes_written < 0) { /* EINTR already handled for us */ /* If the other end closed the socket with close() or shutdown(), we * receive EPIPE here but we must not close the socket yet: there * might still be some data to read. See: * http://lists.freedesktop.org/archives/dbus/2008-March/009526.html */ if (_dbus_get_is_errno_eagain_or_ewouldblock (saved_errno) || _dbus_get_is_errno_epipe (saved_errno)) goto out; /* Since Linux commit 25888e (from 2.6.37-rc4, Nov 2010), sendmsg() * on Unix sockets returns -1 errno=ETOOMANYREFS when the passfd * mechanism (SCM_RIGHTS) is used recursively with a recursion level * of maximum 4. The kernel does not have an API to check whether * the passed fds can be forwarded and it can change asynchronously. * See: * https://bugs.freedesktop.org/show_bug.cgi?id=80163 */ else if (_dbus_get_is_errno_etoomanyrefs (saved_errno)) { /* We only send fds in the first byte of the message. * ETOOMANYREFS cannot happen after. */ _dbus_assert (socket_transport->message_bytes_written == 0); _dbus_verbose (" discard message of %d bytes due to ETOOMANYREFS\n", 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); /* The message was not actually sent but it needs to be removed * from the outgoing queue */ _dbus_connection_message_sent_unlocked (transport->connection, message); } else { _dbus_verbose ("Error writing to remote app: %s\n", _dbus_strerror (saved_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; }
dbus_bool_t _dbus_become_daemon (const DBusString *pidfile, int print_pid_fd, DBusError *error) { const char *s; pid_t child_pid; int dev_null_fd; _dbus_verbose ("Becoming a daemon...\n"); _dbus_verbose ("chdir to /\n"); if (chdir ("/") < 0) { dbus_set_error (error, DBUS_ERROR_FAILED, "Could not chdir() to root directory"); return FALSE; } _dbus_verbose ("forking...\n"); switch ((child_pid = fork ())) { case -1: _dbus_verbose ("fork failed\n"); dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to fork daemon: %s", _dbus_strerror (errno)); return FALSE; break; case 0: _dbus_verbose ("in child, closing std file descriptors\n"); /* silently ignore failures here, if someone * doesn't have /dev/null we may as well try * to continue anyhow */ dev_null_fd = open ("/dev/null", O_RDWR); if (dev_null_fd >= 0) { dup2 (dev_null_fd, 0); dup2 (dev_null_fd, 1); s = _dbus_getenv ("DBUS_DEBUG_OUTPUT"); if (s == NULL || *s == '\0') dup2 (dev_null_fd, 2); else _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n"); } /* Get a predictable umask */ _dbus_verbose ("setting umask\n"); umask (022); break; default: if (pidfile) { _dbus_verbose ("parent writing pid file\n"); if (!_dbus_write_pid_file (pidfile, child_pid, error)) { _dbus_verbose ("pid file write failed, killing child\n"); kill (child_pid, SIGTERM); return FALSE; } } /* Write PID if requested */ if (print_pid_fd >= 0) { DBusString pid; int bytes; if (!_dbus_string_init (&pid)) { _DBUS_SET_OOM (error); kill (child_pid, SIGTERM); return FALSE; } if (!_dbus_string_append_int (&pid, child_pid) || !_dbus_string_append (&pid, "\n")) { _dbus_string_free (&pid); _DBUS_SET_OOM (error); kill (child_pid, SIGTERM); return FALSE; } bytes = _dbus_string_get_length (&pid); if (_dbus_write_socket (print_pid_fd, &pid, 0, bytes) != bytes) { dbus_set_error (error, DBUS_ERROR_FAILED, "Printing message bus PID: %s\n", _dbus_strerror (errno)); _dbus_string_free (&pid); kill (child_pid, SIGTERM); return FALSE; } _dbus_string_free (&pid); } _dbus_verbose ("parent exiting\n"); _exit (0); break; } _dbus_verbose ("calling setsid()\n"); if (setsid () == -1) _dbus_assert_not_reached ("setsid() failed"); 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; }