Replace hard-coded offsets into struct sigcontext with generated file.
[glibc.git] / nscd / nscd_stat.c
blobf04e1972b56104881e25bd547def4e105f9942af
1 /* Copyright (c) 1998, 2003 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
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <error.h>
22 #include <langinfo.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <libintl.h>
29 #include "nscd.h"
30 #include "dbg_log.h"
32 /* We use this to make sure the receiver is the same. */
33 static const char compilation[21] = __DATE__ " " __TIME__;
35 /* Statistic data for one database. */
36 struct dbstat
38 int enabled;
39 int check_file;
40 size_t module;
42 unsigned long int postimeout;
43 unsigned long int negtimeout;
45 unsigned long int poshit;
46 unsigned long int neghit;
47 unsigned long int posmiss;
48 unsigned long int negmiss;
50 unsigned long int nentries;
51 unsigned long int maxnentries;
52 unsigned long int maxnsearched;
54 unsigned long int rdlockdelayed;
55 unsigned long int wrlockdelayed;
58 /* Record for transmitting statistics. */
59 struct statdata
61 char version[sizeof (compilation)];
62 int debug_level;
63 time_t runtime;
64 unsigned long int client_queued;
65 int ndbs;
66 struct dbstat dbs[lastdb];
70 void
71 send_stats (int fd, struct database dbs[lastdb])
73 struct statdata data;
74 int cnt;
76 memcpy (data.version, compilation, sizeof (compilation));
77 data.debug_level = debug_level;
78 data.runtime = time (NULL) - start_time;
79 data.client_queued = client_queued;
80 data.ndbs = lastdb;
82 for (cnt = 0; cnt < lastdb; ++cnt)
84 data.dbs[cnt].enabled = dbs[cnt].enabled;
85 data.dbs[cnt].check_file = dbs[cnt].check_file;
86 data.dbs[cnt].module = dbs[cnt].module;
87 data.dbs[cnt].postimeout = dbs[cnt].postimeout;
88 data.dbs[cnt].negtimeout = dbs[cnt].negtimeout;
89 data.dbs[cnt].poshit = dbs[cnt].poshit;
90 data.dbs[cnt].neghit = dbs[cnt].neghit;
91 data.dbs[cnt].posmiss = dbs[cnt].posmiss;
92 data.dbs[cnt].negmiss = dbs[cnt].negmiss;
93 data.dbs[cnt].nentries = dbs[cnt].nentries;
94 data.dbs[cnt].maxnentries = dbs[cnt].maxnentries;
95 data.dbs[cnt].maxnsearched = dbs[cnt].maxnsearched;
96 data.dbs[cnt].rdlockdelayed = dbs[cnt].rdlockdelayed;
97 data.dbs[cnt].wrlockdelayed = dbs[cnt].wrlockdelayed;
100 if (TEMP_FAILURE_RETRY (write (fd, &data, sizeof (data))) != sizeof (data))
102 char buf[256];
103 dbg_log (_("cannot write statistics: %s"),
104 strerror_r (errno, buf, sizeof (buf)));
110 receive_print_stats (void)
112 struct statdata data;
113 request_header req;
114 ssize_t nbytes;
115 int fd;
116 int i;
117 uid_t uid = getuid ();
119 /* Find out whether there is another user but root allowed to
120 request statistics. */
121 if (uid != 0)
123 /* User specified? */
124 if(stat_user == NULL || stat_uid != uid)
126 if (stat_user != NULL)
127 error (EXIT_FAILURE, 0,
128 _("Only root or %s is allowed to use this option!"),
129 stat_user);
130 else
131 error (EXIT_FAILURE, 0,
132 _("Only root is allowed to use this option!"));
136 /* Open a socket to the running nscd. */
137 fd = nscd_open_socket ();
138 if (fd == -1)
139 error (EXIT_FAILURE, 0, _("nscd not running!\n"));
141 /* Send the request. */
142 req.version = NSCD_VERSION;
143 req.type = GETSTAT;
144 req.key_len = 0;
145 nbytes = TEMP_FAILURE_RETRY (write (fd, &req, sizeof (request_header)));
146 if (nbytes != sizeof (request_header))
148 int err = errno;
149 close (fd);
150 error (EXIT_FAILURE, err, _("write incomplete"));
153 /* Read as much data as we expect. */
154 if (TEMP_FAILURE_RETRY (read (fd, &data, sizeof (data))) != sizeof (data)
155 || (memcmp (data.version, compilation, sizeof (compilation)) != 0
156 /* Yes, this is an assignment! */
157 && (errno = EINVAL)))
159 /* Not the right version. */
160 int err = errno;
161 close (fd);
162 error (EXIT_FAILURE, err, _("cannot read statistics data"));
165 printf (_("nscd configuration:\n\n%15d server debug level\n"),
166 data.debug_level);
168 /* We know that we can simply subtract time_t values. */
169 unsigned long int diff = data.runtime;
170 unsigned int ndays = 0;
171 unsigned int nhours = 0;
172 unsigned int nmins = 0;
173 if (diff > 24 * 60 * 60)
175 ndays = diff / (24 * 60 * 60);
176 diff %= 24 * 60 * 60;
178 if (diff > 60 * 60)
180 nhours = diff / (60 * 60);
181 diff %= 60 * 60;
183 if (diff > 60)
185 nmins = diff / 60;
186 diff %= 60;
188 if (ndays != 0)
189 printf (_("%3ud %2uh %2um %2lus server runtime\n"),
190 ndays, nhours, nmins, diff);
191 else if (nhours != 0)
192 printf (_(" %2uh %2um %2lus server runtime\n"), nhours, nmins, diff);
193 else if (nmins != 0)
194 printf (_(" %2um %2lus server runtime\n"), nmins, diff);
195 else
196 printf (_(" %2lus server runtime\n"), diff);
198 printf (_("%15lu number of times clients had to wait\n"),
199 data.client_queued);
201 for (i = 0; i < lastdb; ++i)
203 unsigned long int hit = data.dbs[i].poshit + data.dbs[i].neghit;
204 unsigned long int all = hit + data.dbs[i].posmiss + data.dbs[i].negmiss;
205 const char *enabled = nl_langinfo (data.dbs[i].enabled ? YESSTR : NOSTR);
206 const char *check_file = nl_langinfo (data.dbs[i].check_file
207 ? YESSTR : NOSTR);
209 if (enabled[0] == '\0')
210 /* The locale does not provide this information so we have to
211 translate it ourself. Since we should avoid short translation
212 terms we artifically increase the length. */
213 enabled = data.dbs[i].enabled ? _(" yes") : _(" no");
214 if (check_file[0] == '\0')
215 check_file = data.dbs[i].check_file ? _(" yes") : _(" no");
217 if (all == 0)
218 /* If nothing happened so far report a 0% hit rate. */
219 all = 1;
221 printf (_("\n%s cache:\n\n"
222 "%15s cache is enabled\n"
223 "%15Zu suggested size\n"
224 "%15lu seconds time to live for positive entries\n"
225 "%15lu seconds time to live for negative entries\n"
226 "%15lu cache hits on positive entries\n"
227 "%15lu cache hits on negative entries\n"
228 "%15lu cache misses on positive entries\n"
229 "%15lu cache misses on negative entries\n"
230 "%15lu%% cache hit rate\n"
231 "%15lu current number of cached values\n"
232 "%15lu maximum number of cached values\n"
233 "%15lu maximum chain length searched\n"
234 "%15lu number of delays on rdlock\n"
235 "%15lu number of delays on wrlock\n"
236 "%15s check /etc/%s for changes\n"),
237 dbnames[i], enabled,
238 data.dbs[i].module,
239 data.dbs[i].postimeout, data.dbs[i].negtimeout,
240 data.dbs[i].poshit, data.dbs[i].neghit,
241 data.dbs[i].posmiss, data.dbs[i].negmiss,
242 (100 * hit) / all,
243 data.dbs[i].nentries, data.dbs[i].maxnentries,
244 data.dbs[i].maxnsearched,
245 data.dbs[i].rdlockdelayed,
246 data.dbs[i].wrlockdelayed, check_file, dbnames[i]);
249 close (fd);
251 exit (0);