mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-12-15 19:02:04 +00:00
47 lines
1.2 KiB
C++
47 lines
1.2 KiB
C++
#include <ctime>
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include <Common/Exception.h>
|
|
#include <Common/randomSeed.h>
|
|
#include <Common/SipHash.h>
|
|
#include <base/getThreadId.h>
|
|
#include <base/types.h>
|
|
|
|
#if defined(__linux__)
|
|
#include <sys/utsname.h>
|
|
#endif
|
|
|
|
namespace DB
|
|
{
|
|
namespace ErrorCodes
|
|
{
|
|
extern const int CANNOT_CLOCK_GETTIME;
|
|
}
|
|
}
|
|
|
|
|
|
DB::UInt64 randomSeed()
|
|
{
|
|
struct timespec times;
|
|
if (clock_gettime(CLOCK_MONOTONIC, ×))
|
|
DB::throwFromErrno("Cannot clock_gettime.", DB::ErrorCodes::CANNOT_CLOCK_GETTIME);
|
|
|
|
/// Not cryptographically secure as time, pid and stack address can be predictable.
|
|
|
|
SipHash hash;
|
|
hash.update(times.tv_nsec);
|
|
hash.update(times.tv_sec);
|
|
hash.update(getThreadId());
|
|
|
|
/// It makes sense to add something like hostname to avoid seed collision when multiple servers start simultaneously.
|
|
/// But randomSeed() must be signal-safe and gethostname and similar functions are not.
|
|
/// Let's try to get utsname.nodename using uname syscall (it's signal-safe).
|
|
#if defined(__linux__)
|
|
struct utsname sysinfo;
|
|
if (uname(&sysinfo) == 0)
|
|
hash.update(sysinfo);
|
|
#endif
|
|
|
|
return hash.get64();
|
|
}
|