static unsigned int get_random_seed (void) { DBusString bytes; unsigned int seed; int fd; const char *s; seed = 0; if (!_dbus_string_init (&bytes)) exit (1); fd = open ("/dev/urandom", O_RDONLY); if (fd < 0) goto use_fallback; if (_dbus_read (fd, &bytes, 4) != 4) goto use_fallback; close (fd); s = _dbus_string_get_const_data (&bytes); seed = * (unsigned int*) s; goto out; use_fallback: { long tv_usec; fprintf (stderr, "could not open/read /dev/urandom, using current time for seed\n"); _dbus_get_current_time (NULL, &tv_usec); seed = tv_usec; } out: _dbus_string_free (&bytes); return seed; }
/** * Get a printable string describing the command used to execute * the process with pid. This string should only be used for * informative purposes such as logging; it may not be trusted. * * The command is guaranteed to be printable ASCII and no longer * than max_len. * * @param pid Process id * @param str Append command to this string * @param max_len Maximum length of returned command * @param error return location for errors * @returns #FALSE on error */ dbus_bool_t _dbus_command_for_pid (unsigned long pid, DBusString *str, int max_len, DBusError *error) { /* This is all Linux-specific for now */ DBusString path; DBusString cmdline; int fd; if (!_dbus_string_init (&path)) { _DBUS_SET_OOM (error); return FALSE; } if (!_dbus_string_init (&cmdline)) { _DBUS_SET_OOM (error); _dbus_string_free (&path); return FALSE; } if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid)) goto oom; fd = open (_dbus_string_get_const_data (&path), O_RDONLY); if (fd < 0) { dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to open \"%s\": %s", _dbus_string_get_const_data (&path), _dbus_strerror (errno)); goto fail; } if (!_dbus_read (fd, &cmdline, max_len)) { dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to read from \"%s\": %s", _dbus_string_get_const_data (&path), _dbus_strerror (errno)); goto fail; } if (!_dbus_close (fd, error)) goto fail; string_squash_nonprintable (&cmdline); if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str))) goto oom; _dbus_string_free (&cmdline); _dbus_string_free (&path); return TRUE; oom: _DBUS_SET_OOM (error); fail: _dbus_string_free (&cmdline); _dbus_string_free (&path); return FALSE; }