1 /* Copyright (c) 1998, 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
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.
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
13 Lesser General Public License for more details.
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
34 # include <selinux/selinux.h>
35 # include <selinux/avc.h>
36 #endif /* HAVE_SELINUX */
39 /* We use this to make sure the receiver is the same. */
40 static const char compilation
[21] = __DATE__
" " __TIME__
;
42 /* Statistic data for one database. */
51 unsigned long int postimeout
;
52 unsigned long int negtimeout
;
65 uintmax_t rdlockdelayed
;
66 uintmax_t wrlockdelayed
;
71 /* Record for transmitting statistics. */
74 char version
[sizeof (compilation
)];
77 unsigned long int client_queued
;
79 struct dbstat dbs
[lastdb
];
81 struct avc_cache_stats cstats
;
82 #endif /* HAVE_SELINUX */
87 send_stats (int fd
, struct database_dyn dbs
[lastdb
])
92 memcpy (data
.version
, compilation
, sizeof (compilation
));
93 data
.debug_level
= debug_level
;
94 data
.runtime
= time (NULL
) - start_time
;
95 data
.client_queued
= client_queued
;
98 for (cnt
= 0; cnt
< lastdb
; ++cnt
)
100 memset (&data
.dbs
[cnt
], 0, sizeof (data
.dbs
[cnt
]));
101 data
.dbs
[cnt
].enabled
= dbs
[cnt
].enabled
;
102 data
.dbs
[cnt
].check_file
= dbs
[cnt
].check_file
;
103 data
.dbs
[cnt
].shared
= dbs
[cnt
].shared
;
104 data
.dbs
[cnt
].persistent
= dbs
[cnt
].persistent
;
105 data
.dbs
[cnt
].postimeout
= dbs
[cnt
].postimeout
;
106 data
.dbs
[cnt
].negtimeout
= dbs
[cnt
].negtimeout
;
107 if (dbs
[cnt
].head
!= NULL
)
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
;
126 nscd_avc_cache_stats (&data
.cstats
);
128 if (TEMP_FAILURE_RETRY (write (fd
, &data
, sizeof (data
))) != sizeof (data
))
131 dbg_log (_("cannot write statistics: %s"),
132 strerror_r (errno
, buf
, sizeof (buf
)));
138 receive_print_stats (void)
140 struct statdata data
;
145 uid_t uid
= getuid ();
147 /* Find out whether there is another user but root allowed to
148 request statistics. */
151 /* User specified? */
152 if(stat_user
== NULL
|| stat_uid
!= uid
)
154 if (stat_user
!= NULL
)
155 error (EXIT_FAILURE
, 0,
156 _("Only root or %s is allowed to use this option!"),
159 error (EXIT_FAILURE
, 0,
160 _("Only root is allowed to use this option!"));
164 /* Open a socket to the running nscd. */
165 fd
= nscd_open_socket ();
167 error (EXIT_FAILURE
, 0, _("nscd not running!\n"));
169 /* Send the request. */
170 req
.version
= NSCD_VERSION
;
173 nbytes
= TEMP_FAILURE_RETRY (write (fd
, &req
, sizeof (request_header
)));
174 if (nbytes
!= sizeof (request_header
))
178 error (EXIT_FAILURE
, err
, _("write incomplete"));
181 /* Read as much data as we expect. */
182 if (TEMP_FAILURE_RETRY (read (fd
, &data
, sizeof (data
))) != sizeof (data
)
183 || (memcmp (data
.version
, compilation
, sizeof (compilation
)) != 0
184 /* Yes, this is an assignment! */
185 && (errno
= EINVAL
)))
187 /* Not the right version. */
190 error (EXIT_FAILURE
, err
, _("cannot read statistics data"));
193 printf (_("nscd configuration:\n\n%15d server debug level\n"),
196 /* We know that we can simply subtract time_t values. */
197 unsigned long int diff
= data
.runtime
;
198 unsigned int ndays
= 0;
199 unsigned int nhours
= 0;
200 unsigned int nmins
= 0;
201 if (diff
> 24 * 60 * 60)
203 ndays
= diff
/ (24 * 60 * 60);
204 diff
%= 24 * 60 * 60;
208 nhours
= diff
/ (60 * 60);
217 printf (_("%3ud %2uh %2um %2lus server runtime\n"),
218 ndays
, nhours
, nmins
, diff
);
219 else if (nhours
!= 0)
220 printf (_(" %2uh %2um %2lus server runtime\n"), nhours
, nmins
, diff
);
222 printf (_(" %2um %2lus server runtime\n"), nmins
, diff
);
224 printf (_(" %2lus server runtime\n"), diff
);
226 printf (_("%15lu number of times clients had to wait\n"),
229 for (i
= 0; i
< lastdb
; ++i
)
231 unsigned long int hit
= data
.dbs
[i
].poshit
+ data
.dbs
[i
].neghit
;
232 unsigned long int all
= hit
+ data
.dbs
[i
].posmiss
+ data
.dbs
[i
].negmiss
;
233 const char *enabled
= nl_langinfo (data
.dbs
[i
].enabled
? YESSTR
: NOSTR
);
234 const char *check_file
= nl_langinfo (data
.dbs
[i
].check_file
236 const char *shared
= nl_langinfo (data
.dbs
[i
].shared
? YESSTR
: NOSTR
);
237 const char *persistent
= nl_langinfo (data
.dbs
[i
].persistent
240 if (enabled
[0] == '\0')
241 /* The locale does not provide this information so we have to
242 translate it ourself. Since we should avoid short translation
243 terms we artifically increase the length. */
244 enabled
= data
.dbs
[i
].enabled
? _(" yes") : _(" no");
245 if (check_file
[0] == '\0')
246 check_file
= data
.dbs
[i
].check_file
? _(" yes") : _(" no");
247 if (shared
[0] == '\0')
248 shared
= data
.dbs
[i
].shared
? _(" yes") : _(" no");
249 if (persistent
[0] == '\0')
250 persistent
= data
.dbs
[i
].persistent
? _(" yes") : _(" no");
253 /* If nothing happened so far report a 0% hit rate. */
256 printf (_("\n%s cache:\n\n"
257 "%15s cache is enabled\n"
258 "%15s cache is persistent\n"
259 "%15s cache is shared\n"
260 "%15zu suggested size\n"
261 "%15zu total data pool size\n"
262 "%15zu used data pool size\n"
263 "%15lu seconds time to live for positive entries\n"
264 "%15lu seconds time to live for negative entries\n"
265 "%15" PRIuMAX
" cache hits on positive entries\n"
266 "%15" PRIuMAX
" cache hits on negative entries\n"
267 "%15" PRIuMAX
" cache misses on positive entries\n"
268 "%15" PRIuMAX
" cache misses on negative entries\n"
269 "%15lu%% cache hit rate\n"
270 "%15zu current number of cached values\n"
271 "%15zu maximum number of cached values\n"
272 "%15zu maximum chain length searched\n"
273 "%15" PRIuMAX
" number of delays on rdlock\n"
274 "%15" PRIuMAX
" number of delays on wrlock\n"
275 "%15" PRIuMAX
" memory allocations failed\n"
276 "%15s check /etc/%s for changes\n"),
277 dbnames
[i
], enabled
, persistent
, shared
,
279 data
.dbs
[i
].datasize
, data
.dbs
[i
].dataused
,
280 data
.dbs
[i
].postimeout
, data
.dbs
[i
].negtimeout
,
281 data
.dbs
[i
].poshit
, data
.dbs
[i
].neghit
,
282 data
.dbs
[i
].posmiss
, data
.dbs
[i
].negmiss
,
284 data
.dbs
[i
].nentries
, data
.dbs
[i
].maxnentries
,
285 data
.dbs
[i
].maxnsearched
,
286 data
.dbs
[i
].rdlockdelayed
,
287 data
.dbs
[i
].wrlockdelayed
,
288 data
.dbs
[i
].addfailed
, check_file
, dbnames
[i
]);
292 nscd_avc_print_stats (&data
.cstats
);