static dbus_bool_t generate_and_write_nonce (const DBusString *filename, DBusError *error) { DBusString nonce; dbus_bool_t ret; _DBUS_ASSERT_ERROR_IS_CLEAR (error); if (!_dbus_string_init (&nonce)) { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return FALSE; } if (!_dbus_generate_random_bytes (&nonce, 16)) { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); _dbus_string_free (&nonce); return FALSE; } ret = _dbus_string_save_to_file (&nonce, filename, FALSE, error); _dbus_string_free (&nonce); return ret; }
static dbus_bool_t _dbus_create_uuid_file_exclusively (const DBusString *filename, DBusGUID *uuid, DBusError *error) { DBusString encoded; if (!_dbus_string_init (&encoded)) { _DBUS_SET_OOM (error); return FALSE; } _dbus_generate_uuid (uuid); if (!_dbus_uuid_encode (uuid, &encoded)) { _DBUS_SET_OOM (error); goto error; } /* FIXME this is racy; we need a save_file_exclusively * function. But in practice this should be fine for now. * * - first be sure we can create the file and it * doesn't exist by creating it empty with O_EXCL * - then create it by creating a temporary file and * overwriting atomically with rename() */ if (!_dbus_create_file_exclusively (filename, error)) goto error; if (!_dbus_string_append_byte (&encoded, '\n')) { _DBUS_SET_OOM (error); goto error; } if (!_dbus_string_save_to_file (&encoded, filename, error)) goto error; if (!_dbus_make_file_world_readable (filename, error)) goto error; _dbus_string_free (&encoded); _DBUS_ASSERT_ERROR_IS_CLEAR (error); return TRUE; error: _DBUS_ASSERT_ERROR_IS_SET (error); _dbus_string_free (&encoded); return FALSE; }
/** * Write the give UUID to a file. * * @param filename the file to write * @param uuid the UUID to save * @param error used to raise an error * @returns #FALSE on error */ dbus_bool_t _dbus_write_uuid_file (const DBusString *filename, const DBusGUID *uuid, DBusError *error) { DBusString encoded; if (!_dbus_string_init (&encoded)) { _DBUS_SET_OOM (error); return FALSE; } if (!_dbus_uuid_encode (uuid, &encoded)) { _DBUS_SET_OOM (error); goto error; } if (!_dbus_string_append_byte (&encoded, '\n')) { _DBUS_SET_OOM (error); goto error; } if (!_dbus_string_save_to_file (&encoded, filename, TRUE, error)) goto error; _dbus_string_free (&encoded); _DBUS_ASSERT_ERROR_IS_CLEAR (error); return TRUE; error: _DBUS_ASSERT_ERROR_IS_SET (error); _dbus_string_free (&encoded); return FALSE; }
static dbus_bool_t try_mutated_data (const DBusString *data) { int pid; total_attempts += 1; /* printf (" attempt %d\n", total_attempts); */ pid = fork (); if (pid < 0) { fprintf (stderr, "fork() failed: %s\n", strerror (errno)); exit (1); return FALSE; } if (pid == 0) { /* Child, try loading the data */ if (!dbus_internal_do_not_use_try_message_data (data, _DBUS_MESSAGE_UNKNOWN)) exit (1); else exit (0); } else { /* Parent, wait for child */ int status; DBusString filename; dbus_bool_t failed; if (waitpid (pid, &status, 0) < 0) { fprintf (stderr, "waitpid() failed: %s\n", strerror (errno)); exit (1); return FALSE; } failed = FALSE; if (!_dbus_string_init (&filename) || !_dbus_string_copy (&failure_dir, 0, &filename, 0) || !_dbus_string_append_byte (&filename, '/')) { fprintf (stderr, "out of memory\n"); exit (1); } _dbus_string_append_int (&filename, total_attempts); if (WIFEXITED (status)) { if (WEXITSTATUS (status) != 0) { _dbus_string_append (&filename, "-exited-"); _dbus_string_append_int (&filename, WEXITSTATUS (status)); failed = TRUE; } } else if (WIFSIGNALED (status)) { _dbus_string_append (&filename, "signaled-"); _dbus_string_append_int (&filename, WTERMSIG (status)); failed = TRUE; } if (failed) { DBusError error; _dbus_string_append (&filename, ".message-raw"); printf ("Child failed, writing %s\n", _dbus_string_get_const_data (&filename)); dbus_error_init (&error); if (!_dbus_string_save_to_file (data, &filename, FALSE, &error)) { fprintf (stderr, "Failed to save failed message data: %s\n", error.message); dbus_error_free (&error); exit (1); /* so we can see the seed that was printed out */ } failures_this_iteration += 1; _dbus_string_free (&filename); return FALSE; } else { _dbus_string_free (&filename); return TRUE; } } _dbus_assert_not_reached ("should not be reached"); return TRUE; }