]> git.ipfire.org Git - thirdparty/glibc.git/blame - nscd/nscd_stat.c
* argp/argp-help.c: Use _IO_vasprintf instead of vasprintf.
[thirdparty/glibc.git] / nscd / nscd_stat.c
CommitLineData
bb6e8ca3 1/* Copyright (c) 1998, 2003, 2004, 2005 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
AJ
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
d67281a7 19
67479a70
UD
20#include <errno.h>
21#include <error.h>
a95a08b4 22#include <inttypes.h>
67479a70 23#include <langinfo.h>
d67281a7
UD
24#include <stdio.h>
25#include <stdlib.h>
67479a70 26#include <string.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. */
40static const char compilation[21] = __DATE__ " " __TIME__;
41
42/* Statistic data for one database. */
43struct 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. */
72struct statdata
73{
74 char version[sizeof (compilation)];
75 int debug_level;
c86e6aec 76 time_t runtime;
a16e3585 77 unsigned long int client_queued;
67479a70
UD
78 int ndbs;
79 struct dbstat dbs[lastdb];
74a30a58
UD
80#ifdef HAVE_SELINUX
81 struct avc_cache_stats cstats;
82#endif /* HAVE_SELINUX */
67479a70
UD
83};
84
d67281a7
UD
85
86void
a95a08b4 87send_stats (int fd, struct database_dyn dbs[lastdb])
d67281a7 88{
67479a70
UD
89 struct statdata data;
90 int cnt;
91
92 memcpy (data.version, compilation, sizeof (compilation));
93 data.debug_level = debug_level;
c86e6aec 94 data.runtime = time (NULL) - start_time;
a16e3585 95 data.client_queued = client_queued;
67479a70
UD
96 data.ndbs = lastdb;
97
98 for (cnt = 0; cnt < lastdb; ++cnt)
99 {
aadd7d9d 100 memset (&data.dbs[cnt], 0, sizeof (data.dbs[cnt]));
67479a70
UD
101 data.dbs[cnt].enabled = dbs[cnt].enabled;
102 data.dbs[cnt].check_file = dbs[cnt].check_file;
d13a3c57
UD
103 data.dbs[cnt].shared = dbs[cnt].shared;
104 data.dbs[cnt].persistent = dbs[cnt].persistent;
67479a70
UD
105 data.dbs[cnt].postimeout = dbs[cnt].postimeout;
106 data.dbs[cnt].negtimeout = dbs[cnt].negtimeout;
aadd7d9d
UD
107 if (dbs[cnt].head != NULL)
108 {
109 data.dbs[cnt].module = dbs[cnt].head->module;
110 data.dbs[cnt].poshit = dbs[cnt].head->poshit;
111 data.dbs[cnt].neghit = dbs[cnt].head->neghit;
112 data.dbs[cnt].posmiss = dbs[cnt].head->posmiss;
113 data.dbs[cnt].negmiss = dbs[cnt].head->negmiss;
114 data.dbs[cnt].nentries = dbs[cnt].head->nentries;
115 data.dbs[cnt].maxnentries = dbs[cnt].head->maxnentries;
116 data.dbs[cnt].datasize = dbs[cnt].head->data_size;
117 data.dbs[cnt].dataused = dbs[cnt].head->first_free;
118 data.dbs[cnt].maxnsearched = dbs[cnt].head->maxnsearched;
119 data.dbs[cnt].rdlockdelayed = dbs[cnt].head->rdlockdelayed;
120 data.dbs[cnt].wrlockdelayed = dbs[cnt].head->wrlockdelayed;
121 data.dbs[cnt].addfailed = dbs[cnt].head->addfailed;
122 }
67479a70 123 }
d67281a7 124
74a30a58
UD
125 if (selinux_enabled)
126 nscd_avc_cache_stats (&data.cstats);
127
a334319f 128 if (TEMP_FAILURE_RETRY (write (fd, &data, sizeof (data))) != sizeof (data))
d67281a7 129 {
67479a70
UD
130 char buf[256];
131 dbg_log (_("cannot write statistics: %s"),
132 strerror_r (errno, buf, sizeof (buf)));
d67281a7 133 }
67479a70
UD
134}
135
136
137int
138receive_print_stats (void)
139{
140 struct statdata data;
141 request_header req;
142 ssize_t nbytes;
143 int fd;
144 int i;
a12ce44f 145 uid_t uid = getuid ();
bb6e8ca3
UD
146 const char *yesstr = _("yes");
147 const char *nostr = _("no");
a12ce44f
UD
148
149 /* Find out whether there is another user but root allowed to
150 request statistics. */
151 if (uid != 0)
152 {
153 /* User specified? */
154 if(stat_user == NULL || stat_uid != uid)
155 {
156 if (stat_user != NULL)
157 error (EXIT_FAILURE, 0,
158 _("Only root or %s is allowed to use this option!"),
159 stat_user);
160 else
161 error (EXIT_FAILURE, 0,
162 _("Only root is allowed to use this option!"));
163 }
164 }
d67281a7 165
67479a70
UD
166 /* Open a socket to the running nscd. */
167 fd = nscd_open_socket ();
168 if (fd == -1)
169 error (EXIT_FAILURE, 0, _("nscd not running!\n"));
170
171 /* Send the request. */
d67281a7
UD
172 req.version = NSCD_VERSION;
173 req.type = GETSTAT;
174 req.key_len = 0;
a334319f 175 nbytes = TEMP_FAILURE_RETRY (write (fd, &req, sizeof (request_header)));
d67281a7
UD
176 if (nbytes != sizeof (request_header))
177 {
67479a70
UD
178 int err = errno;
179 close (fd);
180 error (EXIT_FAILURE, err, _("write incomplete"));
d67281a7
UD
181 }
182
67479a70
UD
183 /* Read as much data as we expect. */
184 if (TEMP_FAILURE_RETRY (read (fd, &data, sizeof (data))) != sizeof (data)
185 || (memcmp (data.version, compilation, sizeof (compilation)) != 0
186 /* Yes, this is an assignment! */
c86e6aec 187 && (errno = EINVAL)))
d67281a7 188 {
67479a70
UD
189 /* Not the right version. */
190 int err = errno;
191 close (fd);
192 error (EXIT_FAILURE, err, _("cannot read statistics data"));
d67281a7
UD
193 }
194
67479a70
UD
195 printf (_("nscd configuration:\n\n%15d server debug level\n"),
196 data.debug_level);
197
c86e6aec
UD
198 /* We know that we can simply subtract time_t values. */
199 unsigned long int diff = data.runtime;
200 unsigned int ndays = 0;
201 unsigned int nhours = 0;
202 unsigned int nmins = 0;
203 if (diff > 24 * 60 * 60)
204 {
205 ndays = diff / (24 * 60 * 60);
206 diff %= 24 * 60 * 60;
207 }
208 if (diff > 60 * 60)
209 {
210 nhours = diff / (60 * 60);
211 diff %= 60 * 60;
212 }
213 if (diff > 60)
214 {
215 nmins = diff / 60;
216 diff %= 60;
217 }
218 if (ndays != 0)
219 printf (_("%3ud %2uh %2um %2lus server runtime\n"),
220 ndays, nhours, nmins, diff);
221 else if (nhours != 0)
222 printf (_(" %2uh %2um %2lus server runtime\n"), nhours, nmins, diff);
223 else if (nmins != 0)
224 printf (_(" %2um %2lus server runtime\n"), nmins, diff);
225 else
226 printf (_(" %2lus server runtime\n"), diff);
227
27e82856
UD
228 printf (_("%15d current number of threads\n"
229 "%15d maximum number of threads\n"
230 "%15lu number of times clients had to wait\n"
4401d759
UD
231 "%15s paranoia mode enabled\n"
232 "%15lu restart internal\n"),
a334319f
UD
233 nthreads, max_nthreads, data.client_queued,
234 paranoia ? yesstr : nostr, (unsigned long int) restart_interval);
a16e3585 235
67479a70
UD
236 for (i = 0; i < lastdb; ++i)
237 {
238 unsigned long int hit = data.dbs[i].poshit + data.dbs[i].neghit;
239 unsigned long int all = hit + data.dbs[i].posmiss + data.dbs[i].negmiss;
27e82856
UD
240 const char *enabled = data.dbs[i].enabled ? yesstr : nostr;
241 const char *check_file = data.dbs[i].check_file ? yesstr : nostr;
242 const char *shared = data.dbs[i].shared ? yesstr : nostr;
243 const char *persistent = data.dbs[i].persistent ? yesstr : nostr;
67479a70
UD
244
245 if (enabled[0] == '\0')
246 /* The locale does not provide this information so we have to
247 translate it ourself. Since we should avoid short translation
248 terms we artifically increase the length. */
4401d759 249 enabled = data.dbs[i].enabled ? yesstr : nostr;
67479a70 250 if (check_file[0] == '\0')
4401d759 251 check_file = data.dbs[i].check_file ? yesstr : nostr;
d13a3c57 252 if (shared[0] == '\0')
4401d759 253 shared = data.dbs[i].shared ? yesstr : nostr;
d13a3c57 254 if (persistent[0] == '\0')
4401d759 255 persistent = data.dbs[i].persistent ? yesstr : nostr;
67479a70
UD
256
257 if (all == 0)
258 /* If nothing happened so far report a 0% hit rate. */
259 all = 1;
260
261 printf (_("\n%s cache:\n\n"
262 "%15s cache is enabled\n"
d13a3c57
UD
263 "%15s cache is persistent\n"
264 "%15s cache is shared\n"
a95a08b4
UD
265 "%15zu suggested size\n"
266 "%15zu total data pool size\n"
267 "%15zu used data pool size\n"
c86e6aec
UD
268 "%15lu seconds time to live for positive entries\n"
269 "%15lu seconds time to live for negative entries\n"
a95a08b4
UD
270 "%15" PRIuMAX " cache hits on positive entries\n"
271 "%15" PRIuMAX " cache hits on negative entries\n"
272 "%15" PRIuMAX " cache misses on positive entries\n"
273 "%15" PRIuMAX " cache misses on negative entries\n"
c86e6aec 274 "%15lu%% cache hit rate\n"
a95a08b4
UD
275 "%15zu current number of cached values\n"
276 "%15zu maximum number of cached values\n"
277 "%15zu maximum chain length searched\n"
278 "%15" PRIuMAX " number of delays on rdlock\n"
279 "%15" PRIuMAX " number of delays on wrlock\n"
280 "%15" PRIuMAX " memory allocations failed\n"
67479a70 281 "%15s check /etc/%s for changes\n"),
d13a3c57 282 dbnames[i], enabled, persistent, shared,
67479a70 283 data.dbs[i].module,
a95a08b4 284 data.dbs[i].datasize, data.dbs[i].dataused,
67479a70
UD
285 data.dbs[i].postimeout, data.dbs[i].negtimeout,
286 data.dbs[i].poshit, data.dbs[i].neghit,
287 data.dbs[i].posmiss, data.dbs[i].negmiss,
288 (100 * hit) / all,
c86e6aec
UD
289 data.dbs[i].nentries, data.dbs[i].maxnentries,
290 data.dbs[i].maxnsearched,
291 data.dbs[i].rdlockdelayed,
a95a08b4
UD
292 data.dbs[i].wrlockdelayed,
293 data.dbs[i].addfailed, check_file, dbnames[i]);
67479a70
UD
294 }
295
74a30a58
UD
296 if (selinux_enabled)
297 nscd_avc_print_stats (&data.cstats);
298
67479a70
UD
299 close (fd);
300
301 exit (0);
d67281a7 302}