2 #include "Connection.hpp"
5 #include <sys/random.h>
8 unsigned random_num(unsigned min
, unsigned max
) {
10 getrandom(&p
, sizeof(p
), 0);
11 return (p
% (max
- min
+ 1)) + min
;
14 unsigned random_port() {
16 int port
= random_num(2<<9, 2<<15);
17 Connection
conn(port
);
28 string
random_socket() {
29 return "/tmp/libmc." + to_string(random_num(1, UINT32_MAX
)) + "@" + to_string(getpid()) + ".sock";
32 string
random_socket_or_port_string(const string
&what
) {
34 return random_socket();
37 return to_string(random_port());
40 string
random_socket_or_port_flag(const string
&binary
) {
42 return random_num(0, 1) ? "-p" : "-s";
45 const char *getenv_else(const char *var
, const char *defval
) {
46 auto val
= getenv(var
);