(pthread_cancel): Don't do anything if cancelation is disabled.
[platform/upstream/glibc.git] / nscd / nscd_stat.c
1 /* Copyright (c) 1998 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.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
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
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA. */
19
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>
28
29 #include "nscd.h"
30 #include "dbg_log.h"
31
32 /* We use this to make sure the receiver is the same.  */
33 static const char compilation[21] = __DATE__ " " __TIME__;
34
35 /* Statistic data for one database.  */
36 struct dbstat
37 {
38   int enabled;
39   int check_file;
40   size_t module;
41
42   unsigned long int postimeout;
43   unsigned long int negtimeout;
44
45   unsigned long int poshit;
46   unsigned long int neghit;
47   unsigned long int posmiss;
48   unsigned long int negmiss;
49 };
50
51 /* Record for transmitting statistics.  */
52 struct statdata
53 {
54   char version[sizeof (compilation)];
55   int debug_level;
56   int ndbs;
57   struct dbstat dbs[lastdb];
58 };
59
60
61 void
62 send_stats (int fd, struct database dbs[lastdb])
63 {
64   struct statdata data;
65   int cnt;
66
67   memcpy (data.version, compilation, sizeof (compilation));
68   data.debug_level = debug_level;
69   data.ndbs = lastdb;
70
71   for (cnt = 0; cnt < lastdb; ++cnt)
72     {
73       data.dbs[cnt].enabled = dbs[cnt].enabled;
74       data.dbs[cnt].check_file = dbs[cnt].check_file;
75       data.dbs[cnt].module = dbs[cnt].module;
76       data.dbs[cnt].postimeout = dbs[cnt].postimeout;
77       data.dbs[cnt].negtimeout = dbs[cnt].negtimeout;
78       data.dbs[cnt].poshit = dbs[cnt].poshit;
79       data.dbs[cnt].neghit = dbs[cnt].neghit;
80       data.dbs[cnt].posmiss = dbs[cnt].posmiss;
81       data.dbs[cnt].negmiss = dbs[cnt].negmiss;
82     }
83
84   if (TEMP_FAILURE_RETRY (write (fd, &data, sizeof (data))) != sizeof (data))
85     {
86       char buf[256];
87       dbg_log (_("cannot write statistics: %s"),
88                strerror_r (errno, buf, sizeof (buf)));
89     }
90 }
91
92
93 int
94 receive_print_stats (void)
95 {
96   struct statdata data;
97   request_header req;
98   ssize_t nbytes;
99   int fd;
100   int i;
101
102   /* Open a socket to the running nscd.  */
103   fd = nscd_open_socket ();
104   if (fd == -1)
105     error (EXIT_FAILURE, 0, _("nscd not running!\n"));
106
107   /* Send the request.  */
108   req.version = NSCD_VERSION;
109   req.type = GETSTAT;
110   req.key_len = 0;
111   nbytes = TEMP_FAILURE_RETRY (write (fd, &req, sizeof (request_header)));
112   if (nbytes != sizeof (request_header))
113     {
114       int err = errno;
115       close (fd);
116       error (EXIT_FAILURE, err, _("write incomplete"));
117     }
118
119   /* Read as much data as we expect.  */
120   if (TEMP_FAILURE_RETRY (read (fd, &data, sizeof (data))) != sizeof (data)
121       || (memcmp (data.version, compilation, sizeof (compilation)) != 0
122           /* Yes, this is an assignment!  */
123           && errno == EINVAL))
124     {
125       /* Not the right version.  */
126       int err = errno;
127       close (fd);
128       error (EXIT_FAILURE, err, _("cannot read statistics data"));
129     }
130
131   printf (_("nscd configuration:\n\n%15d  server debug level\n"),
132           data.debug_level);
133
134   for (i = 0; i < lastdb; ++i)
135     {
136       unsigned long int hit = data.dbs[i].poshit + data.dbs[i].neghit;
137       unsigned long int all = hit + data.dbs[i].posmiss + data.dbs[i].negmiss;
138       const char *enabled = nl_langinfo (data.dbs[i].enabled ? YESSTR : NOSTR);
139       const char *check_file = nl_langinfo (data.dbs[i].check_file
140                                             ? YESSTR : NOSTR);
141
142       if (enabled[0] == '\0')
143         /* The locale does not provide this information so we have to
144            translate it ourself.  Since we should avoid short translation
145            terms we artifically increase the length.  */
146         enabled = data.dbs[i].enabled ? _("     yes") : _("      no");
147       if (check_file[0] == '\0')
148         check_file = data.dbs[i].check_file ? _("     yes") : _("      no");
149
150       if (all == 0)
151         /* If nothing happened so far report a 0% hit rate.  */
152         all = 1;
153
154       printf (_("\n%s cache:\n\n"
155                 "%15s  cache is enabled\n"
156                 "%15Zd  suggested size\n"
157                 "%15ld  seconds time to live for positive entries\n"
158                 "%15ld  seconds time to live for negative entries\n"
159                 "%15ld  cache hits on positive entries\n"
160                 "%15ld  cache hits on negative entries\n"
161                 "%15ld  cache misses on positive entries\n"
162                 "%15ld  cache misses on negative entries\n"
163                 "%15ld%% cache hit rate\n"
164                 "%15s  check /etc/%s for changes\n"),
165               dbnames[i], enabled,
166               data.dbs[i].module,
167               data.dbs[i].postimeout, data.dbs[i].negtimeout,
168               data.dbs[i].poshit, data.dbs[i].neghit,
169               data.dbs[i].posmiss, data.dbs[i].negmiss,
170               (100 * hit) / all,
171               check_file, dbnames[i]);
172     }
173
174   close (fd);
175
176   exit (0);
177 }