nscd_stat.c 9.8 KB

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