static dbus_bool_t do_check_nonce (int fd, const DBusString *nonce, DBusError *error) { DBusString buffer; DBusString p; size_t nleft; dbus_bool_t result; int n; _DBUS_ASSERT_ERROR_IS_CLEAR (error); nleft = 16; if ( !_dbus_string_init (&buffer) || !_dbus_string_init (&p) ) { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); _dbus_string_free (&p); _dbus_string_free (&buffer); return FALSE; } while (nleft) { n = _dbus_read_socket (fd, &p, nleft); if (n == -1 && _dbus_get_is_errno_eintr()) ; else if (n == -1 && _dbus_get_is_errno_eagain_or_ewouldblock()) _dbus_sleep_milliseconds (100); else if (n==-1) { dbus_set_error (error, DBUS_ERROR_IO_ERROR, "Could not read nonce from socket (fd=%d)", fd ); _dbus_string_free (&p); _dbus_string_free (&buffer); return FALSE; } else if (!n) { _dbus_string_free (&p); _dbus_string_free (&buffer); dbus_set_error (error, DBUS_ERROR_IO_ERROR, "Could not read nonce from socket (fd=%d)", fd ); return FALSE; } else { _dbus_string_append_len(&buffer, _dbus_string_get_const_data (&p), n); nleft -= n; } } result = _dbus_string_equal_len (&buffer, nonce, 16); if (!result) dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, "Nonces do not match, access denied (fd=%d)", fd ); _dbus_string_free (&p); _dbus_string_free (&buffer); return result; }
static DBusHandlerResult handle_delay_echo (DBusConnection *connection, DBusMessage *message) { DBusError error; DBusMessage *reply; char *s; _dbus_verbose ("sleeping for a short time\n"); _dbus_sleep_milliseconds (50); _dbus_verbose ("sending reply to DelayEcho method\n"); dbus_error_init (&error); if (!dbus_message_get_args (message, &error, DBUS_TYPE_STRING, &s, DBUS_TYPE_INVALID)) { reply = dbus_message_new_error (message, error.name, error.message); if (reply == NULL) die ("No memory\n"); if (!dbus_connection_send (connection, reply, NULL)) die ("No memory\n"); dbus_message_unref (reply); return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } reply = dbus_message_new_method_return (message); if (reply == NULL) die ("No memory\n"); if (!dbus_message_append_args (reply, DBUS_TYPE_STRING, &s, DBUS_TYPE_INVALID)) die ("No memory"); if (!dbus_connection_send (connection, reply, NULL)) die ("No memory\n"); fprintf (stderr, "DelayEcho service echoed string: \"%s\"\n", s); dbus_message_unref (reply); return DBUS_HANDLER_RESULT_HANDLED; }
static dbus_bool_t _dbus_keyring_lock (DBusKeyring *keyring) { int n_timeouts; n_timeouts = 0; while (n_timeouts < MAX_LOCK_TIMEOUTS) { DBusError error; dbus_error_init (&error); if (_dbus_create_file_exclusively (&keyring->filename_lock, &error)) break; _dbus_verbose ("Did not get lock file, sleeping %d milliseconds (%s)\n", LOCK_TIMEOUT_MILLISECONDS, error.message); dbus_error_free (&error); _dbus_sleep_milliseconds (LOCK_TIMEOUT_MILLISECONDS); ++n_timeouts; } if (n_timeouts == MAX_LOCK_TIMEOUTS) { DBusError error; _dbus_verbose ("Lock file timed out %d times, assuming stale\n", n_timeouts); dbus_error_init (&error); if (!_dbus_delete_file (&keyring->filename_lock, &error)) { _dbus_verbose ("Couldn't delete old lock file: %s\n", error.message); dbus_error_free (&error); return FALSE; } if (!_dbus_create_file_exclusively (&keyring->filename_lock, &error)) { _dbus_verbose ("Couldn't create lock file after deleting stale one: %s\n", error.message); dbus_error_free (&error); return FALSE; } } return TRUE; }
/** * Aborts the program with SIGABRT (dumping core). */ void _dbus_abort (void) { const char *s; _dbus_print_backtrace (); s = _dbus_getenv ("DBUS_BLOCK_ON_ABORT"); if (s && *s) { /* don't use _dbus_warn here since it can _dbus_abort() */ fprintf (stderr, " Process %lu sleeping for gdb attach\n", _dbus_pid_for_log ()); _dbus_sleep_milliseconds (1000 * 180); } abort (); _dbus_exit (1); /* in case someone manages to ignore SIGABRT ? */ }
static dbus_bool_t do_check_nonce (DBusSocket fd, const DBusString *nonce, DBusError *error) { DBusString buffer; DBusString p; size_t nleft; dbus_bool_t result; int n; _DBUS_ASSERT_ERROR_IS_CLEAR (error); nleft = 16; /* This is a trick to make it safe to call _dbus_string_free on these * strings during error unwinding, even if allocating memory for them * fails. A constant DBusString is considered to be valid to "free", * even though there is nothing to free (of course the free operation * is trivial, because it does not own its own buffer); but * unlike a mutable DBusString, initializing a constant DBusString * cannot fail. * * We must successfully re-initialize the strings to be mutable before * writing to them, of course. */ _dbus_string_init_const (&buffer, ""); _dbus_string_init_const (&p, ""); if ( !_dbus_string_init (&buffer) || !_dbus_string_init (&p) ) { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); _dbus_string_free (&p); _dbus_string_free (&buffer); return FALSE; } while (nleft) { int saved_errno; n = _dbus_read_socket (fd, &p, nleft); saved_errno = _dbus_save_socket_errno (); if (n == -1 && _dbus_get_is_errno_eintr (saved_errno)) ; else if (n == -1 && _dbus_get_is_errno_eagain_or_ewouldblock (saved_errno)) _dbus_sleep_milliseconds (100); else if (n==-1) { dbus_set_error (error, DBUS_ERROR_IO_ERROR, "Could not read nonce from socket (fd=%" DBUS_SOCKET_FORMAT ")", _dbus_socket_printable (fd)); _dbus_string_free (&p); _dbus_string_free (&buffer); return FALSE; } else if (!n) { _dbus_string_free (&p); _dbus_string_free (&buffer); dbus_set_error (error, DBUS_ERROR_IO_ERROR, "Could not read nonce from socket (fd=%" DBUS_SOCKET_FORMAT ")", _dbus_socket_printable (fd)); return FALSE; } else { if (!_dbus_string_append_len (&buffer, _dbus_string_get_const_data (&p), n)) { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); _dbus_string_free (&p); _dbus_string_free (&buffer); return FALSE; } nleft -= n; } } result = _dbus_string_equal_len (&buffer, nonce, 16); if (!result) dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, "Nonces do not match, access denied (fd=%" DBUS_SOCKET_FORMAT ")", _dbus_socket_printable (fd)); _dbus_string_free (&p); _dbus_string_free (&buffer); return result; }