fixed ssl.c bug when ssl backend returns IO_BLOCKED but IO engine doesn't get informe...
[ircu2.10.12-pk.git] / ircd / s_debug.c
1 /*
2  * IRC - Internet Relay Chat, ircd/s_debug.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 1, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 /** @file
21  * @brief Debug support for the ircd.
22  * @version $Id: s_debug.c 1429 2005-06-19 02:31:27Z entrope $
23  */
24 #include "config.h"
25
26 #include "s_debug.h"
27 #include "channel.h"
28 #include "class.h"
29 #include "client.h"
30 #include "gline.h"
31 #include "hash.h"
32 #include "ircd_alloc.h"
33 #include "ircd_features.h"
34 #include "ircd_log.h"
35 #include "ircd_osdep.h"
36 #include "ircd_reply.h"
37 #include "ircd.h"
38 #include "jupe.h"
39 #include "list.h"
40 #include "listener.h"
41 #include "motd.h"
42 #include "msgq.h"
43 #include "numeric.h"
44 #include "numnicks.h"
45 #include "res.h"
46 #include "s_bsd.h"
47 #include "s_conf.h"
48 #include "s_user.h"
49 #include "s_stats.h"
50 #include "send.h"
51 #include "ssl.h"
52 #include "struct.h"
53 #include "sys.h"
54 #include "whowas.h"
55
56 /* #include <assert.h> -- Now using assert in ircd_log.h */
57 #include <errno.h>
58 #include <fcntl.h>
59 #include <stdarg.h>
60 #include <stddef.h>     /* offsetof */
61 #include <stdio.h>
62 #include <string.h>
63 #include <unistd.h>
64
65 /*
66  * Option string.  Must be before #ifdef DEBUGMODE.
67  */
68 static char serveropts[256]; /* should be large enough for anything */
69
70 /** Return a string describing important configuration information.
71  * @return Pointer to a static buffer.
72  */
73 const char* debug_serveropts(void)
74 {
75   int bp;
76   int i = 0;
77 #define AddC(c) serveropts[i++] = (c)
78
79   bp = feature_int(FEAT_BUFFERPOOL);
80   if (bp < 1000000) {
81     AddC('b');
82     if (bp > 99999)
83       AddC((char)('0' + (bp / 100000)));
84     if (bp > 9999)
85       AddC((char)('0' + (bp / 10000) % 10));
86     AddC((char)('0' + (bp / 1000) % 10));
87   } else {
88     AddC('B');
89     if (bp > 99999999)
90       AddC((char)('0' + (bp / 100000000)));
91     if (bp > 9999999)
92       AddC((char)('0' + (bp / 10000000) % 10));
93     AddC((char)('0' + (bp / 1000000) % 10));
94   }
95
96 #ifndef NDEBUG
97   AddC('A');
98 #endif
99 #ifdef  DEBUGMODE
100   AddC('D');
101 #endif
102
103   if (feature_bool(FEAT_HUB))
104     AddC('H');
105
106   if (feature_bool(FEAT_IDLE_FROM_MSG))
107     AddC('M');
108
109   if (feature_bool(FEAT_RELIABLE_CLOCK))
110     AddC('R');
111
112 #if defined(USE_POLL) && defined(HAVE_POLL_H)
113   AddC('U');
114 #endif
115 #ifdef  IPV6
116   AddC('6');
117 #endif
118
119   serveropts[i] = '\0';
120
121   return serveropts;
122 }
123
124 /** Initialize debugging.
125  * If the -t option is not given on the command line when the server is
126  * started, all debugging output is sent to the file set by LPATH in config.h
127  * Here we just open that file and make sure it is opened to fd 2 so that
128  * any fprintf's to stderr also go to the logfile.  If the debuglevel is not
129  * set from the command line by -x, use /dev/null as the dummy logfile as long
130  * as DEBUGMODE has been defined, else don't waste the fd.
131  * @param use_tty Passed to log_debug_init().
132  */
133 void debug_init(int use_tty)
134 {
135 #ifdef  DEBUGMODE
136   if (debuglevel >= 0) {
137     printf("isatty = %d ttyname = %s\n", isatty(2), ttyname(2));
138     log_debug_init(use_tty);
139   }
140 #endif
141 }
142
143 #ifdef DEBUGMODE
144 /** Log a debug message using a va_list.
145  * If the current #debuglevel is less than \a level, do not display.
146  * @param level Debug level for message.
147  * @param form Format string, passed to log_vwrite().
148  * @param vl Varargs argument list for format string.
149  */
150 void vdebug(int level, const char *form, va_list vl)
151 {
152   static int loop = 0;
153   int err = errno;
154
155   if (!loop && (debuglevel >= 0) && (level <= debuglevel))
156   {
157     loop = 1;
158     log_vwrite(LS_DEBUG, L_DEBUG, 0, form, vl);
159     loop = 0;
160   }
161   errno = err;
162 }
163
164 /** Log a debug message using a variable number of arguments.
165  * This is a simple wrapper around debug(\a level, \a form, vl).
166  * @param level Debug level for message.
167  * @param form Format string of message.
168  */
169 void debug(int level, const char *form, ...)
170 {
171   va_list vl;
172   va_start(vl, form);
173   vdebug(level, form, vl);
174   va_end(vl);
175 }
176
177 /** Send a literal RPL_STATSDEBUG message to a user.
178  * @param cptr Client to receive the message.
179  * @param msg Text message to send to user.
180  */
181 static void debug_enumerator(struct Client* cptr, const char* msg)
182 {
183   assert(0 != cptr);
184   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":%s", msg);
185 }
186
187 /** Send resource usage statistics to a client.
188  * @param cptr Client to send data to.
189  * @param sd StatDesc that generated the stats request (ignored).
190  * @param param Extra parameter from user (ignored).
191  */
192 void send_usage(struct Client *cptr, const struct StatDesc *sd,
193                 char *param)
194 {
195   os_get_rusage(cptr, CurrentTime - cli_since(&me), debug_enumerator);
196
197   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":DBUF alloc %d used %d",
198              DBufAllocCount, DBufUsedCount);
199 }
200 #endif /* DEBUGMODE */
201
202 /** Report memory usage statistics to a client.
203  * @param cptr Client to send data to.
204  * @param sd StatDesc that generated the stats request (ignored).
205  * @param param Extra parameter from user (ignored).
206  */
207 void count_memory(struct Client *cptr, const struct StatDesc *sd,
208                   char *param)
209 {
210   struct Client *acptr;
211   struct SLink *link;
212   struct Ban *ban;
213   struct Channel *chptr;
214   struct ConfItem *aconf;
215   const struct ConnectionClass* cltmp;
216   struct Membership* member;
217
218   int acc = 0,                  /* accounts */
219       c = 0,                    /* clients */
220       cn = 0,                   /* connections */
221       ch = 0,                   /* channels */
222       lcc = 0,                  /* local client conf links */
223       chi = 0,                  /* channel invites */
224       chb = 0,                  /* channel bans */
225       wwu = 0,                  /* whowas users */
226       cl = 0,                   /* classes */
227       co = 0,                   /* conf lines */
228       listeners = 0,            /* listeners */
229       memberships = 0;          /* channel memberships */
230
231   int usi = 0,                  /* users invited */
232       aw = 0,                   /* aways set */
233       wwa = 0,                  /* whowas aways */
234       gl = 0,                   /* glines */
235       ju = 0;                   /* jupes */
236
237   size_t chm = 0,               /* memory used by channels */
238       chbm = 0,                 /* memory used by channel bans */
239       cm = 0,                   /* memory used by clients */
240       cnm = 0,                  /* memory used by connections */
241       us = 0,                   /* user structs */
242       usm = 0,                  /* memory used by user structs */
243       awm = 0,                  /* memory used by aways */
244       wwam = 0,                 /* whowas away memory used */
245       wwm = 0,                  /* whowas array memory used */
246       glm = 0,                  /* memory used by glines */
247       jum = 0,                  /* memory used by jupes */
248       com = 0,                  /* memory used by conf lines */
249       dbufs_allocated = 0,      /* memory used by dbufs */
250       dbufs_used = 0,           /* memory used by dbufs */
251       msg_allocated = 0,        /* memory used by struct Msg */
252       msgbuf_allocated = 0,     /* memory used by struct MsgBuf */
253       listenersm = 0,           /* memory used by listetners */
254       rm = 0,                   /* res memory used */
255       totcl = 0, totch = 0, totww = 0, tot = 0;
256
257   count_whowas_memory(&wwu, &wwm, &wwa, &wwam);
258   wwm += sizeof(struct Whowas) * feature_int(FEAT_NICKNAMEHISTORYLENGTH);
259   wwm += sizeof(struct Whowas *) * WW_MAX;
260
261   for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
262   {
263     c++;
264     if (MyConnect(acptr))
265     {
266       cn++;
267       for (link = cli_confs(acptr); link; link = link->next)
268         lcc++;
269     }
270     if (cli_user(acptr))
271     {
272       for (link = cli_user(acptr)->invited; link; link = link->next)
273         usi++;
274       for (member = cli_user(acptr)->channel; member; member = member->next_channel)
275         ++memberships;
276       if (cli_user(acptr)->away)
277       {
278         aw++;
279         awm += (strlen(cli_user(acptr)->away) + 1);
280       }
281     }
282
283     if (IsAccount(acptr))
284       acc++;
285   }
286   cm = c * sizeof(struct Client);
287   cnm = cn * sizeof(struct Connection);
288   user_count_memory(&us, &usm);
289
290   for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
291   {
292     ch++;
293     chm += (strlen(chptr->chname) + sizeof(struct Channel));
294     for (link = chptr->invites; link; link = link->next)
295       chi++;
296     for (ban = chptr->banlist; ban; ban = ban->next)
297     {
298       chb++;
299       chbm += strlen(ban->who) + strlen(ban->banstr) + 2 + sizeof(*ban);
300     }
301   }
302
303   for (aconf = GlobalConfList; aconf; aconf = aconf->next)
304   {
305     co++;
306     com += aconf->host ? strlen(aconf->host) + 1 : 0;
307     com += aconf->passwd ? strlen(aconf->passwd) + 1 : 0;
308     com += aconf->name ? strlen(aconf->name) + 1 : 0;
309     com += sizeof(struct ConfItem);
310   }
311
312   for (cltmp = get_class_list(); cltmp; cltmp = cltmp->next)
313     cl++;
314
315   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
316              ":Clients %d(%zu) Connections %d(%zu)", c, cm, cn, cnm);
317   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
318              ":Users %zu(%zu) Accounts %d(%zu) Invites %d(%zu)",
319              us, usm, acc, acc * (ACCOUNTLEN + 1),
320              usi, usi * sizeof(struct SLink));
321   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
322              ":User channels %d(%zu) Aways %d(%zu)", memberships,
323              memberships * sizeof(struct Membership), aw, awm);
324
325   totcl = cm + cnm + us * sizeof(struct User) + memberships * sizeof(struct Membership) + awm;
326   totcl += lcc * sizeof(struct SLink) + usi * sizeof(struct SLink);
327
328   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Conflines %d(%zu) Attached %d(%zu) Classes %d(%zu)",
329              co, com, lcc, lcc * sizeof(struct SLink),
330              cl, cl * sizeof(struct ConnectionClass));
331
332   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
333              ":Channels %d(%zu) Bans %d(%zu)", ch, chm, chb, chbm);
334   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
335              ":Channel Members %d(%zu) Invites %d(%zu)", memberships,
336              memberships * sizeof(struct Membership), chi,
337              chi * sizeof(struct SLink));
338
339   totch = chm + chbm + chi * sizeof(struct SLink);
340
341   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
342              ":Whowas Users %d(%zu) Away %d(%zu) Array %d(%zu)",
343              wwu, wwu * sizeof(struct User), wwa, wwam,
344              feature_int(FEAT_NICKNAMEHISTORYLENGTH), wwm);
345
346   totww = wwu * sizeof(struct User) + wwam + wwm;
347
348   motd_memory_count(cptr);
349
350   gl = gline_memory_count(&glm);
351   ju = jupe_memory_count(&jum);
352   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
353              ":Glines %d(%zu) Jupes %d(%zu)", gl, glm, ju, jum);
354
355   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
356              ":Hash: client %d(%zu), chan is the same", HASHSIZE,
357              sizeof(void *) * HASHSIZE);
358
359   count_listener_memory(&listeners, &listenersm);
360   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
361              ":Listeners allocated %d(%zu)", listeners, listenersm);
362   /*
363    * NOTE: this count will be accurate only for the exact instant that this
364    * message is being sent, so the count is affected by the dbufs that
365    * are being used to send this message out. If this is not desired, move
366    * the dbuf_count_memory call to a place before we start sending messages
367    * and cache DBufAllocCount and DBufUsedCount in variables until they 
368    * are sent.
369    */
370   dbuf_count_memory(&dbufs_allocated, &dbufs_used);
371   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
372              ":DBufs allocated %d(%zu) used %d(%zu)", DBufAllocCount,
373              dbufs_allocated, DBufUsedCount, dbufs_used);
374
375   /* The DBuf caveats now count for this, but this routine now sends
376    * replies all on its own.
377    */
378   msgq_count_memory(cptr, &msg_allocated, &msgbuf_allocated);
379
380   rm = cres_mem(cptr);
381
382   tot =
383       totww + totch + totcl + com + cl * sizeof(struct ConnectionClass) +
384       dbufs_allocated + msg_allocated + msgbuf_allocated + rm;
385   tot += sizeof(void *) * HASHSIZE * 3;
386
387 #if defined(MDEBUG)
388   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Allocations: %zu(%zu)",
389              fda_get_block_count(), fda_get_byte_count());
390 #endif
391
392   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
393              ":Total: ww %zu ch %zu cl %zu co %zu db %zu ms %zu mb %zu",
394              totww, totch, totcl, com, dbufs_allocated, msg_allocated,
395              msgbuf_allocated);
396 }