Imported Upstream version 1.35
[platform/upstream/connman.git] / src / util.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2014  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <inttypes.h>
30 #include <stdint.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <stdlib.h>
34
35 #include "connman.h"
36
37 #define URANDOM "/dev/urandom"
38
39 static int f = -1;
40
41 int __connman_util_get_random(uint64_t *val)
42 {
43         int r;
44
45         if (!val)
46                 return -EINVAL;
47
48         r = read(f, val, sizeof(uint64_t));
49         if (r < 0) {
50                 r = -errno;
51                 connman_warn_once("Could not read from "URANDOM);
52                 *val = random();
53         } else if (r != sizeof(uint64_t)) {
54                 r = -EIO;
55                 connman_warn_once("Short read from "URANDOM);
56                 *val = random();
57         }
58
59         return r;
60 }
61
62 int __connman_util_init(void)
63 {
64         int r = 0;
65
66         if (f >= 0)
67                 return 0;
68
69         f = open(URANDOM, O_RDONLY);
70         if (f < 0) {
71                 r = -errno;
72                 connman_warn("Could not open "URANDOM);
73                 srandom(time(NULL));
74         } else {
75                 uint64_t val;
76
77                 r = __connman_util_get_random(&val);
78                 if (r < 0)
79                         srandom(time(NULL));
80                 else
81                         srandom(val);
82         }
83
84         return r;
85 }
86
87 void __connman_util_cleanup(void)
88 {
89         if (f >= 0)
90                 close(f);
91
92         f = -1;
93 }