-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrc.mtrngd
More file actions
executable file
·192 lines (175 loc) · 4.18 KB
/
Copy pathrc.mtrngd
File metadata and controls
executable file
·192 lines (175 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/bash
# start/stop/restart of the mtrngd
#
export PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin:.
# root privileges required
ID=`id -u`
if (( $? != 0 )); then ID=999; fi
if (( $ID != 0 )); then
echo "root privileges are required for starting or stopping mtrngd."
exit 1
fi
modprobe=/sbin/modprobe
rmmod=/sbin/rmmod
MODOPTS="rngsrc=0 rdelay=10 rawbit=0 strflt=1 fltlen=32"
# It is suggested that the kernel hwrandom source be used as it performs
# better under load and also while longhaul frequency scaling is in use.
USE_XSTORE=false
USE_MSR=false
USE_RAWBITS=false
USE_STRFILT=false
USE_BIAS=false
# NOTE: do not set EASY_FIPS unless you know exactly what you are doing!
EASY_FIPS=false
DEF_MODE="--background"
DEF_LOGDIR="/var/log/mtrngd"
DEF_STATFILE="${DEF_LOGDIR}/mtrngd.status"
DEF_LOGDIRARG="--logdir ${DEF_LOGDIR}"
DEF_RDEV="--random-device /dev/random"
DEF_ESRC="--entropy-source /dev/hwrandom"
DEF_XSTORE="--xstore"
DEF_DENSITY="--rng-density 80"
DEF_TIMEOUT="--timeout 60"
DEF_POOLSZ="--poolsize 512"
DEF_WRITEMK="--writemark 128"
DEF_STATS="--stats 600"
DEF_STATMAX="--statmax 128"
DEF_MSR="--msr"
DEF_MSRDEV="/dev/cpu/0/msr"
DEF_MSRDEVARG="--msr-device ${DEF_MSRDEV}"
DEF_FILTERSZ="--filtersz 32"
DEF_DCBIAS="--dcbias 0"
DEF_RAWBITS="--rawbits"
DEF_NSRC="--noisesrc 0"
DEF_DISABLE="--disable"
DEF_EASYFIPS="--forgiving"
OPTS="${OPTS} ${DEF_MODE}"
OPTS="${OPTS} ${DEF_LOGDIRARG}"
OPTS="${OPTS} ${DEF_RDEV}"
if [[ "$USE_XSTORE" == "true" ]]; then
OPTS="${OPTS} ${DEF_XSTORE}"
else
OPTS="${OPTS} ${DEF_ESRC}"
fi
OPTS="${OPTS} ${DEF_DENSITY}"
OPTS="${OPTS} ${DEF_TIMEOUT}"
OPTS="${OPTS} ${DEF_POOLSZ}"
OPTS="${OPTS} ${DEF_WRITEMK}"
OPTS="${OPTS} ${DEF_STATS}"
OPTS="${OPTS} ${DEF_STATMAX}"
if [[ "$USE_MSR" == "true" ]]; then
OPTS="${OPTS} ${DEF_MSR}"
OPTS="${OPTS} ${DEF_MSRDEVARG}"
fi
if [[ "$USE_RAWBITS" == "true" ]]; then
OPTS="${OPTS} ${DEF_RAWBITS}"
fi
if [[ "$USE_BIAS" == "true" ]]; then
OPTS="${OPTS} ${DEF_DCBIAS}"
fi
if [[ "$USE_STRFILT" == "true" ]]; then
OPTS="${OPTS} ${DEF_FILTERSZ}"
fi
OPTS="${OPTS} ${DEF_NSRC}"
OPTS="${OPTS} ${DEF_DISABLE}"
if [[ "$EASY_FIPS" == "true" ]]; then
OPTS="${OPTS} ${DEF_EASYFIPS}"
fi
check_paths () {
if [ ! -d $DEF_LOGDIR ]; then
# log directory does not exist, attempt to create it.
mkdir -p $DEF_LOGDIR 1>/dev/null 2>&1
if [ ! -d $DEF_LOGDIR ]; then
echo "Error: the log directory: $DEF_LOGDIR does not exist. Exiting."
exit 1
fi
fi
if [[ "$USE_MSR" == "true" ]]; then
if [ ! -c $DEF_MSRDEV ]; then
echo "Error: the MSR character device file: $DEF_MSRDEV does not exist. Exiting."
exit 1
fi
fi
}
check_modules () {
if [[ "$USE_XSTORE" != "true" ]]; then
grep hw_random /proc/modules 1>/dev/null 2>&1
if (( $? != 0 )); then
echo "Loading hw_random kernel module..."
$modprobe hw_random $MODOPTS 1>/dev/null 2>&1
if (( $? != 0 )); then
echo "Unable to load the hw_random kernel module. Exiting."
exit 1
fi
fi
fi
}
get_pid () {
export pid=`ps -fea | grep ' mtrngd' | sed 's/ *[^ ]* *//' | sort | head -1 | sed 's/ .*//'`
kill -0 $pid 1>/dev/null 2>&1
if (( $? != 0 )); then
export pid=0
fi
}
mtrngd_start() {
check_paths
check_modules
get_pid
if (( $pid != 0 )); then
echo "mtrngd is already running with pid: $pid"
exit 1
fi
rm -f $DEF_LOGDIR/mtrngd-stats* 1>/dev/null 2>&1
echo "starting mtrngd $OPTS"
mtrngd $OPTS
}
mtrngd_stop() {
get_pid
if (( $pid != 0 )); then
kill -TERM $pid
get_pid
if (( $pid != 0 )); then
kill -9 $pid
fi
else
echo "mtrngd is not running"
fi
}
mtrngd_status() {
get_pid
if (( $pid == 0 )); then
echo "mtrngd does not appear to be running."
exit 1;
fi
if [ -f $DEF_STATFILE ]; then
rm -f $DEF_STATFILE 1>/dev/null 2>/dev/null
fi
kill -USR1 $pid
max=10
for ((i=1; i <= $max ; i++)); do
if [ -f $DEF_STATFILE ]; then
cat $DEF_STATFILE
exit 0;
fi
usleep 20000
done
echo "Unable to get current status. Is mtrngd hung?"
exit 2;
}
case "$1" in
'start')
mtrngd_start
;;
'stop')
mtrngd_stop
;;
'restart')
mtrngd_stop
mtrngd_start
;;
'status')
mtrngd_status
;;
*)
echo "usage $0 start|stop|restart"
esac