projects
/
platform
/
upstream
/
connman.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Merge "vpn: Export vpn_ipconfig_foreach as linker symbol" into tizen
[platform/upstream/connman.git]
/
src
/
util.c
diff --git
a/src/util.c
b/src/util.c
index
da32cc5
..
03b14cd
100755
(executable)
--- a/
src/util.c
+++ b/
src/util.c
@@
-36,19
+36,24
@@
#define URANDOM "/dev/urandom"
#define URANDOM "/dev/urandom"
-int f = -1;
+
static
int f = -1;
int __connman_util_get_random(uint64_t *val)
{
int __connman_util_get_random(uint64_t *val)
{
- int r
= 0
;
+ int r;
if (!val)
return -EINVAL;
if (!val)
return -EINVAL;
- if (read(f, val, sizeof(uint64_t)) < 0) {
+ r = read(f, val, sizeof(uint64_t));
+ if (r < 0) {
r = -errno;
connman_warn_once("Could not read from "URANDOM);
*val = random();
r = -errno;
connman_warn_once("Could not read from "URANDOM);
*val = random();
+ } else if (r != sizeof(uint64_t)) {
+ r = -EIO;
+ connman_warn_once("Short read from "URANDOM);
+ *val = random();
}
return r;
}
return r;
@@
-58,7
+63,7
@@
int __connman_util_init(void)
{
int r = 0;
{
int r = 0;
- if (f > 0)
+ if (f >
=
0)
return 0;
f = open(URANDOM, O_RDONLY);
return 0;
f = open(URANDOM, O_RDONLY);
@@
-81,8
+86,19
@@
int __connman_util_init(void)
void __connman_util_cleanup(void)
{
void __connman_util_cleanup(void)
{
- if (f > 0)
+ if (f >
=
0)
close(f);
f = -1;
}
close(f);
f = -1;
}
+
+/**
+ * Return a random delay in range of zero to secs*1000 milli seconds.
+ */
+unsigned int __connman_util_random_delay_ms(unsigned int secs)
+{
+ uint64_t rand;
+
+ __connman_util_get_random(&rand);
+ return rand % (secs * 1000);
+}