blob: e4bde80a0b82e1756aded63e2b1be421f6c2351b [file] [log] [blame]
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001From c51c2c543f3c78b2a68acc61f786f903f2e0fec8 Mon Sep 17 00:00:00 2001
2From: Hongxu Jia <hongxu.jia@windriver.com>
3Date: Sun, 9 Sep 2018 07:28:09 -0400
4Subject: [PATCH] fix compile failure with libc musl
5
6The lack of random_r in musl:
7[snip]
8|main.c:1393:7: warning: implicit declaration of function 'random_r';
9did you mean 'random'? [-Wimplicit-function-declaration]
10| rv = random_r(&rand_data, &val);
11| ^~~~~~~~
12| random
13|main.c:1408:30: error: invalid application of 'sizeof' to incomplete
14type 'struct random_data'
15| memset(&rand_data, 0, sizeof(rand_data));
16[snip]
17
18s/random_r/random/, s/initstate_r/initstate/ and remove `static struct
19random_data rand_data'
20
21Here is the man of `random_r()':
22[snip]
23The random_r() function is like random(3), except that instead of using
24state information maintained in a global variable
25[snip]
26
27So use random without state information is OK.
28
29Upstream-Status: Submitted [sanlock-devel@lists.fedorahosted.org]
30Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
31---
32 src/main.c | 12 ++++--------
33 1 file changed, 4 insertions(+), 8 deletions(-)
34
35diff --git a/src/main.c b/src/main.c
36index f60b4d3..602c400 100644
37--- a/src/main.c
38+++ b/src/main.c
39@@ -84,7 +84,6 @@ static char command[COMMAND_MAX];
40 static int cmd_argc;
41 static char **cmd_argv;
42 static struct thread_pool pool;
43-static struct random_data rand_data;
44 static char rand_state[32];
45 static pthread_mutex_t rand_mutex = PTHREAD_MUTEX_INITIALIZER;
46 static const char *run_dir = NULL;
47@@ -1386,16 +1385,15 @@ int get_rand(int a, int b);
48
49 int get_rand(int a, int b)
50 {
51- int32_t val;
52- int rv;
53+ long int rv;
54
55 pthread_mutex_lock(&rand_mutex);
56- rv = random_r(&rand_data, &val);
57+ rv = random();
58 pthread_mutex_unlock(&rand_mutex);
59 if (rv < 0)
60 return rv;
61
62- return a + (int) (((float)(b - a + 1)) * val / (RAND_MAX+1.0));
63+ return a + (int) (((float)(b - a + 1)) * rv / (RAND_MAX+1.0));
64 }
65
66 static void setup_host_name(void)
67@@ -1405,9 +1403,7 @@ static void setup_host_name(void)
68 uuid_t uu;
69
70 memset(rand_state, 0, sizeof(rand_state));
71- memset(&rand_data, 0, sizeof(rand_data));
72-
73- initstate_r(time(NULL), rand_state, sizeof(rand_state), &rand_data);
74+ initstate(time(NULL), rand_state, sizeof(rand_state));
75
76 /* use host name from command line */
77
78--
792.8.1
80