]>
Commit | Line | Data |
---|---|---|
f7a9f785 | 1 | /* Copyright (c) 1998-2016 Free Software Foundation, Inc. |
d67281a7 UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1998. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
d67281a7 UD |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
d67281a7 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
d67281a7 | 18 | |
67479a70 UD |
19 | #include <errno.h> |
20 | #include <error.h> | |
a95a08b4 | 21 | #include <inttypes.h> |
67479a70 | 22 | #include <langinfo.h> |
d67281a7 UD |
23 | #include <stdio.h> |
24 | #include <stdlib.h> | |
67479a70 | 25 | #include <string.h> |
0b20008e | 26 | #include <sys/socket.h> |
d67281a7 | 27 | #include <unistd.h> |
4360eafd | 28 | #include <libintl.h> |
67479a70 | 29 | |
d67281a7 | 30 | #include "nscd.h" |
67479a70 | 31 | #include "dbg_log.h" |
74a30a58 UD |
32 | #include "selinux.h" |
33 | #ifdef HAVE_SELINUX | |
34 | # include <selinux/selinux.h> | |
35 | # include <selinux/avc.h> | |
36 | #endif /* HAVE_SELINUX */ | |
37 | ||
67479a70 UD |
38 | |
39 | /* We use this to make sure the receiver is the same. */ | |
40 | static const char compilation[21] = __DATE__ " " __TIME__; | |
41 | ||
42 | /* Statistic data for one database. */ | |
43 | struct dbstat | |
44 | { | |
45 | int enabled; | |
46 | int check_file; | |
d13a3c57 UD |
47 | int shared; |
48 | int persistent; | |
67479a70 UD |
49 | size_t module; |
50 | ||
51 | unsigned long int postimeout; | |
52 | unsigned long int negtimeout; | |
53 | ||
a95a08b4 UD |
54 | size_t nentries; |
55 | size_t maxnentries; | |
56 | size_t maxnsearched; | |
57 | size_t datasize; | |
58 | size_t dataused; | |
c86e6aec | 59 | |
a95a08b4 UD |
60 | uintmax_t poshit; |
61 | uintmax_t neghit; | |
62 | uintmax_t posmiss; | |
63 | uintmax_t negmiss; | |
c86e6aec | 64 | |
a95a08b4 UD |
65 | uintmax_t rdlockdelayed; |
66 | uintmax_t wrlockdelayed; | |
67 | ||
68 | uintmax_t addfailed; | |
67479a70 UD |
69 | }; |
70 | ||
71 | /* Record for transmitting statistics. */ | |
72 | struct statdata | |
73 | { | |
74 | char version[sizeof (compilation)]; | |
75 | int debug_level; | |
c86e6aec | 76 | time_t runtime; |
a16e3585 | 77 | unsigned long int client_queued; |
4630012f UD |
78 | int nthreads; |
79 | int max_nthreads; | |
80 | int paranoia; | |
81 | time_t restart_interval; | |
d55d558b | 82 | unsigned int reload_count; |
67479a70 UD |
83 | int ndbs; |
84 | struct dbstat dbs[lastdb]; | |
74a30a58 UD |
85 | #ifdef HAVE_SELINUX |
86 | struct avc_cache_stats cstats; | |
87 | #endif /* HAVE_SELINUX */ | |
67479a70 UD |
88 | }; |
89 | ||
d67281a7 UD |
90 | |
91 | void | |
a95a08b4 | 92 | send_stats (int fd, struct database_dyn dbs[lastdb]) |
d67281a7 | 93 | { |
67479a70 UD |
94 | struct statdata data; |
95 | int cnt; | |
96 | ||
3cb26316 SP |
97 | memset (&data, 0, sizeof (data)); |
98 | ||
67479a70 UD |
99 | memcpy (data.version, compilation, sizeof (compilation)); |
100 | data.debug_level = debug_level; | |
c86e6aec | 101 | data.runtime = time (NULL) - start_time; |
a16e3585 | 102 | data.client_queued = client_queued; |
4630012f UD |
103 | data.nthreads = nthreads; |
104 | data.max_nthreads = max_nthreads; | |
105 | data.paranoia = paranoia; | |
106 | data.restart_interval = restart_interval; | |
d55d558b | 107 | data.reload_count = reload_count; |
67479a70 UD |
108 | data.ndbs = lastdb; |
109 | ||
110 | for (cnt = 0; cnt < lastdb; ++cnt) | |
111 | { | |
aadd7d9d | 112 | memset (&data.dbs[cnt], 0, sizeof (data.dbs[cnt])); |
67479a70 UD |
113 | data.dbs[cnt].enabled = dbs[cnt].enabled; |
114 | data.dbs[cnt].check_file = dbs[cnt].check_file; | |
d13a3c57 UD |
115 | data.dbs[cnt].shared = dbs[cnt].shared; |
116 | data.dbs[cnt].persistent = dbs[cnt].persistent; | |
67479a70 UD |
117 | data.dbs[cnt].postimeout = dbs[cnt].postimeout; |
118 | data.dbs[cnt].negtimeout = dbs[cnt].negtimeout; | |
aadd7d9d UD |
119 | if (dbs[cnt].head != NULL) |
120 | { | |
121 | data.dbs[cnt].module = dbs[cnt].head->module; | |
122 | data.dbs[cnt].poshit = dbs[cnt].head->poshit; | |
123 | data.dbs[cnt].neghit = dbs[cnt].head->neghit; | |
124 | data.dbs[cnt].posmiss = dbs[cnt].head->posmiss; | |
125 | data.dbs[cnt].negmiss = dbs[cnt].head->negmiss; | |
126 | data.dbs[cnt].nentries = dbs[cnt].head->nentries; | |
127 | data.dbs[cnt].maxnentries = dbs[cnt].head->maxnentries; | |
128 | data.dbs[cnt].datasize = dbs[cnt].head->data_size; | |
129 | data.dbs[cnt].dataused = dbs[cnt].head->first_free; | |
130 | data.dbs[cnt].maxnsearched = dbs[cnt].head->maxnsearched; | |
131 | data.dbs[cnt].rdlockdelayed = dbs[cnt].head->rdlockdelayed; | |
132 | data.dbs[cnt].wrlockdelayed = dbs[cnt].head->wrlockdelayed; | |
133 | data.dbs[cnt].addfailed = dbs[cnt].head->addfailed; | |
134 | } | |
67479a70 | 135 | } |
d67281a7 | 136 | |
74a30a58 UD |
137 | if (selinux_enabled) |
138 | nscd_avc_cache_stats (&data.cstats); | |
139 | ||
2c210d1e UD |
140 | if (TEMP_FAILURE_RETRY (send (fd, &data, sizeof (data), MSG_NOSIGNAL)) |
141 | != sizeof (data)) | |
d67281a7 | 142 | { |
67479a70 UD |
143 | char buf[256]; |
144 | dbg_log (_("cannot write statistics: %s"), | |
145 | strerror_r (errno, buf, sizeof (buf))); | |
d67281a7 | 146 | } |
67479a70 UD |
147 | } |
148 | ||
149 | ||
150 | int | |
151 | receive_print_stats (void) | |
152 | { | |
153 | struct statdata data; | |
154 | request_header req; | |
155 | ssize_t nbytes; | |
156 | int fd; | |
157 | int i; | |
a12ce44f | 158 | uid_t uid = getuid (); |
bb6e8ca3 UD |
159 | const char *yesstr = _("yes"); |
160 | const char *nostr = _("no"); | |
a12ce44f UD |
161 | |
162 | /* Find out whether there is another user but root allowed to | |
163 | request statistics. */ | |
164 | if (uid != 0) | |
165 | { | |
166 | /* User specified? */ | |
167 | if(stat_user == NULL || stat_uid != uid) | |
168 | { | |
169 | if (stat_user != NULL) | |
170 | error (EXIT_FAILURE, 0, | |
171 | _("Only root or %s is allowed to use this option!"), | |
172 | stat_user); | |
173 | else | |
174 | error (EXIT_FAILURE, 0, | |
175 | _("Only root is allowed to use this option!")); | |
176 | } | |
177 | } | |
d67281a7 | 178 | |
67479a70 UD |
179 | /* Open a socket to the running nscd. */ |
180 | fd = nscd_open_socket (); | |
181 | if (fd == -1) | |
182 | error (EXIT_FAILURE, 0, _("nscd not running!\n")); | |
183 | ||
184 | /* Send the request. */ | |
d67281a7 UD |
185 | req.version = NSCD_VERSION; |
186 | req.type = GETSTAT; | |
187 | req.key_len = 0; | |
2c210d1e UD |
188 | nbytes = TEMP_FAILURE_RETRY (send (fd, &req, sizeof (request_header), |
189 | MSG_NOSIGNAL)); | |
d67281a7 UD |
190 | if (nbytes != sizeof (request_header)) |
191 | { | |
67479a70 UD |
192 | int err = errno; |
193 | close (fd); | |
194 | error (EXIT_FAILURE, err, _("write incomplete")); | |
d67281a7 UD |
195 | } |
196 | ||
67479a70 UD |
197 | /* Read as much data as we expect. */ |
198 | if (TEMP_FAILURE_RETRY (read (fd, &data, sizeof (data))) != sizeof (data) | |
199 | || (memcmp (data.version, compilation, sizeof (compilation)) != 0 | |
200 | /* Yes, this is an assignment! */ | |
c86e6aec | 201 | && (errno = EINVAL))) |
d67281a7 | 202 | { |
67479a70 UD |
203 | /* Not the right version. */ |
204 | int err = errno; | |
205 | close (fd); | |
206 | error (EXIT_FAILURE, err, _("cannot read statistics data")); | |
d67281a7 UD |
207 | } |
208 | ||
67479a70 UD |
209 | printf (_("nscd configuration:\n\n%15d server debug level\n"), |
210 | data.debug_level); | |
211 | ||
c86e6aec UD |
212 | /* We know that we can simply subtract time_t values. */ |
213 | unsigned long int diff = data.runtime; | |
214 | unsigned int ndays = 0; | |
215 | unsigned int nhours = 0; | |
216 | unsigned int nmins = 0; | |
217 | if (diff > 24 * 60 * 60) | |
218 | { | |
219 | ndays = diff / (24 * 60 * 60); | |
220 | diff %= 24 * 60 * 60; | |
221 | } | |
222 | if (diff > 60 * 60) | |
223 | { | |
224 | nhours = diff / (60 * 60); | |
225 | diff %= 60 * 60; | |
226 | } | |
227 | if (diff > 60) | |
228 | { | |
229 | nmins = diff / 60; | |
230 | diff %= 60; | |
231 | } | |
232 | if (ndays != 0) | |
233 | printf (_("%3ud %2uh %2um %2lus server runtime\n"), | |
234 | ndays, nhours, nmins, diff); | |
235 | else if (nhours != 0) | |
236 | printf (_(" %2uh %2um %2lus server runtime\n"), nhours, nmins, diff); | |
237 | else if (nmins != 0) | |
238 | printf (_(" %2um %2lus server runtime\n"), nmins, diff); | |
239 | else | |
240 | printf (_(" %2lus server runtime\n"), diff); | |
241 | ||
27e82856 UD |
242 | printf (_("%15d current number of threads\n" |
243 | "%15d maximum number of threads\n" | |
244 | "%15lu number of times clients had to wait\n" | |
4401d759 | 245 | "%15s paranoia mode enabled\n" |
d55d558b UD |
246 | "%15lu restart internal\n" |
247 | "%15u reload count\n"), | |
4630012f UD |
248 | data.nthreads, data.max_nthreads, data.client_queued, |
249 | data.paranoia ? yesstr : nostr, | |
d55d558b | 250 | (unsigned long int) data.restart_interval, data.reload_count); |
a16e3585 | 251 | |
67479a70 UD |
252 | for (i = 0; i < lastdb; ++i) |
253 | { | |
254 | unsigned long int hit = data.dbs[i].poshit + data.dbs[i].neghit; | |
255 | unsigned long int all = hit + data.dbs[i].posmiss + data.dbs[i].negmiss; | |
27e82856 UD |
256 | const char *enabled = data.dbs[i].enabled ? yesstr : nostr; |
257 | const char *check_file = data.dbs[i].check_file ? yesstr : nostr; | |
258 | const char *shared = data.dbs[i].shared ? yesstr : nostr; | |
259 | const char *persistent = data.dbs[i].persistent ? yesstr : nostr; | |
67479a70 UD |
260 | |
261 | if (enabled[0] == '\0') | |
262 | /* The locale does not provide this information so we have to | |
263 | translate it ourself. Since we should avoid short translation | |
264 | terms we artifically increase the length. */ | |
4401d759 | 265 | enabled = data.dbs[i].enabled ? yesstr : nostr; |
67479a70 | 266 | if (check_file[0] == '\0') |
4401d759 | 267 | check_file = data.dbs[i].check_file ? yesstr : nostr; |
d13a3c57 | 268 | if (shared[0] == '\0') |
4401d759 | 269 | shared = data.dbs[i].shared ? yesstr : nostr; |
d13a3c57 | 270 | if (persistent[0] == '\0') |
4401d759 | 271 | persistent = data.dbs[i].persistent ? yesstr : nostr; |
67479a70 UD |
272 | |
273 | if (all == 0) | |
274 | /* If nothing happened so far report a 0% hit rate. */ | |
275 | all = 1; | |
276 | ||
277 | printf (_("\n%s cache:\n\n" | |
278 | "%15s cache is enabled\n" | |
d13a3c57 UD |
279 | "%15s cache is persistent\n" |
280 | "%15s cache is shared\n" | |
a95a08b4 UD |
281 | "%15zu suggested size\n" |
282 | "%15zu total data pool size\n" | |
283 | "%15zu used data pool size\n" | |
c86e6aec UD |
284 | "%15lu seconds time to live for positive entries\n" |
285 | "%15lu seconds time to live for negative entries\n" | |
a95a08b4 UD |
286 | "%15" PRIuMAX " cache hits on positive entries\n" |
287 | "%15" PRIuMAX " cache hits on negative entries\n" | |
288 | "%15" PRIuMAX " cache misses on positive entries\n" | |
289 | "%15" PRIuMAX " cache misses on negative entries\n" | |
c86e6aec | 290 | "%15lu%% cache hit rate\n" |
a95a08b4 UD |
291 | "%15zu current number of cached values\n" |
292 | "%15zu maximum number of cached values\n" | |
293 | "%15zu maximum chain length searched\n" | |
294 | "%15" PRIuMAX " number of delays on rdlock\n" | |
295 | "%15" PRIuMAX " number of delays on wrlock\n" | |
296 | "%15" PRIuMAX " memory allocations failed\n" | |
67479a70 | 297 | "%15s check /etc/%s for changes\n"), |
d13a3c57 | 298 | dbnames[i], enabled, persistent, shared, |
67479a70 | 299 | data.dbs[i].module, |
a95a08b4 | 300 | data.dbs[i].datasize, data.dbs[i].dataused, |
67479a70 UD |
301 | data.dbs[i].postimeout, data.dbs[i].negtimeout, |
302 | data.dbs[i].poshit, data.dbs[i].neghit, | |
303 | data.dbs[i].posmiss, data.dbs[i].negmiss, | |
304 | (100 * hit) / all, | |
c86e6aec UD |
305 | data.dbs[i].nentries, data.dbs[i].maxnentries, |
306 | data.dbs[i].maxnsearched, | |
307 | data.dbs[i].rdlockdelayed, | |
a95a08b4 UD |
308 | data.dbs[i].wrlockdelayed, |
309 | data.dbs[i].addfailed, check_file, dbnames[i]); | |
67479a70 UD |
310 | } |
311 | ||
74a30a58 UD |
312 | if (selinux_enabled) |
313 | nscd_avc_print_stats (&data.cstats); | |
314 | ||
67479a70 UD |
315 | close (fd); |
316 | ||
317 | exit (0); | |
d67281a7 | 318 | } |