Debian Codegraph @

src/entropy.c

raw
1 /**
2 * collectd - src/entropy.c
3 * Copyright (C) 2007 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
26
27 #include "collectd.h"
28
29 #include "plugin.h"
30 #include "utils/common/common.h"
31
32 static void entropy_submit(value_t);
33 static int entropy_read(void);
34
35 #if !KERNEL_LINUX && !KERNEL_NETBSD
36 #error "No applicable input method."
37 #endif
38
39 #if KERNEL_LINUX
40 #define ENTROPY_FILE "/proc/sys/kernel/random/entropy_avail"
41
42 static int entropy_read(void) {
43 value_t v;
44 if (parse_value_file(ENTROPY_FILE, &v, DS_TYPE_GAUGE) != 0) {
45 ERROR("entropy plugin: Reading \"" ENTROPY_FILE "\" failed.");
46 return -1;
47 }
48
49 entropy_submit(v);
50 return 0;
51 }
52 #endif /* KERNEL_LINUX */
53
54 #if KERNEL_NETBSD
55 /* Provide a NetBSD implementation, partial from rndctl.c */
56
57 /*
58 * Improved to keep the /dev/urandom open, since there's a consumption
59 * of entropy from /dev/random for every open of /dev/urandom, and this
60 * will end up opening /dev/urandom lots of times.
61 */
62
63 #include <sys/ioctl.h>
64 #include <sys/param.h>
65 #include <sys/rnd.h>
66 #include <sys/types.h>
67 #if HAVE_SYS_RNDIO_H
68 #include <sys/rndio.h>
69 #endif
70 #include <paths.h>
71
72 static int entropy_read(void) {
73 value_t v;
74 rndpoolstat_t rs;
75 static int fd;
76 char buf[30];
77
78 if (fd == 0) {
79 fd = open(_PATH_URANDOM, O_RDONLY, 0644);
80 if (fd < 0) {
81 fd = 0;
82 return -1;
83 }
84 }
85
86 if (ioctl(fd, RNDGETPOOLSTAT, &rs) < 0) {
87 (void)close(fd);
88 fd = 0; /* signal a reopening on next attempt */
89 return -1;
90 }
91 snprintf(buf, sizeof(buf), "%ju", (uintmax_t)rs.curentropy);
92 if (parse_value(buf, &v, DS_TYPE_GAUGE) != 0) {
93 ERROR("entropy plugin: Parsing \"%s\" failed.", buf);
94 return (-1);
95 }
96
97 entropy_submit(v);
98
99 return 0;
100 }
101
102 #endif /* KERNEL_NETBSD */
103
104 static void entropy_submit(value_t value) {
105 value_list_t vl = VALUE_LIST_INIT;
106
107 vl.values = &value;
108 vl.values_len = 1;
109 sstrncpy(vl.plugin, "entropy", sizeof(vl.plugin));
110 sstrncpy(vl.type, "entropy", sizeof(vl.type));
111
112 plugin_dispatch_values(&vl);
113 }
114
115 void module_register(void) {
116 plugin_register_read("entropy", entropy_read);
117 } /* void module_register */