Replace privilege-controlling Feature settings with
[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  * $Id$
21  *
22  */
23 #include "config.h"
24
25 #include "s_debug.h"
26 #include "channel.h"
27 #include "class.h"
28 #include "client.h"
29 #include "gline.h"
30 #include "hash.h"
31 #include "ircd_alloc.h"
32 #include "ircd_features.h"
33 #include "ircd_log.h"
34 #include "ircd_osdep.h"
35 #include "ircd_reply.h"
36 #include "ircd.h"
37 #include "jupe.h"
38 #include "list.h"
39 #include "motd.h"
40 #include "msgq.h"
41 #include "numeric.h"
42 #include "numnicks.h"
43 #include "res.h"
44 #include "s_bsd.h"
45 #include "s_conf.h"
46 #include "s_stats.h"
47 #include "send.h"
48 #include "struct.h"
49 #include "sys.h"
50 #include "whowas.h"
51
52 #include <assert.h>
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <stdarg.h>
56 #include <stddef.h>     /* offsetof */
57 #include <stdio.h>
58 #include <string.h>
59 #include <unistd.h>
60
61 /*
62  * Option string.  Must be before #ifdef DEBUGMODE.
63  */
64 static char serveropts[256]; /* should be large enough for anything */
65
66 const char* debug_serveropts(void)
67 {
68   int bp;
69   int i = 0;
70 #define AddC(c) serveropts[i++] = (c)
71
72   bp = feature_int(FEAT_BUFFERPOOL);
73   if (bp < 1000000) {
74     AddC('b');
75     if (bp > 99999)
76       AddC((char)('0' + (bp / 100000)));
77     if (bp > 9999)
78       AddC((char)('0' + (bp / 10000) % 10));
79     AddC((char)('0' + (bp / 1000) % 10));
80   } else {
81     AddC('B');
82     if (bp > 99999999)
83       AddC((char)('0' + (bp / 100000000)));
84     if (bp > 9999999)
85       AddC((char)('0' + (bp / 10000000) % 10));
86     AddC((char)('0' + (bp / 1000000) % 10));
87   }
88
89 #ifndef NDEBUG
90   AddC('A');
91 #endif
92 #ifdef  DEBUGMODE
93   AddC('D');
94 #endif
95
96   if (feature_bool(FEAT_HUB))
97     AddC('H');
98
99   if (feature_bool(FEAT_IDLE_FROM_MSG))
100     AddC('M');
101
102   if (feature_bool(FEAT_CRYPT_OPER_PASSWORD))
103     AddC('p');
104
105   if (feature_bool(FEAT_RELIABLE_CLOCK))
106     AddC('R');
107
108 #if defined(USE_POLL) && defined(HAVE_POLL_H)
109   AddC('U');
110 #endif
111
112   if (feature_bool(FEAT_VIRTUAL_HOST))
113     AddC('v');
114
115   serveropts[i] = '\0';
116
117   return serveropts;
118 }
119
120 /*
121  * debug_init
122  *
123  * If the -t option is not given on the command line when the server is
124  * started, all debugging output is sent to the file set by LPATH in config.h
125  * Here we just open that file and make sure it is opened to fd 2 so that
126  * any fprintf's to stderr also goto the logfile.  If the debuglevel is not
127  * set from the command line by -x, use /dev/null as the dummy logfile as long
128  * as DEBUGMODE has been defined, else dont waste the fd.
129  */
130 void debug_init(int use_tty)
131 {
132 #ifdef  DEBUGMODE
133   if (debuglevel >= 0) {
134     printf("isatty = %d ttyname = %s\n", isatty(2), ttyname(2));
135     log_debug_init(use_tty);
136   }
137 #endif
138 }
139
140 #ifdef DEBUGMODE
141 void vdebug(int level, const char *form, va_list vl)
142 {
143   static int loop = 0;
144   int err = errno;
145
146   if (!loop && (debuglevel >= 0) && (level <= debuglevel))
147   {
148     loop = 1;
149     log_vwrite(LS_DEBUG, L_DEBUG, 0, form, vl);
150     loop = 0;
151   }
152   errno = err;
153 }
154
155 void debug(int level, const char *form, ...)
156 {
157   va_list vl;
158   va_start(vl, form);
159   vdebug(level, form, vl);
160   va_end(vl);
161 }
162
163 static void debug_enumerator(struct Client* cptr, const char* msg)
164 {
165   assert(0 != cptr);
166   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":%s", msg);
167 }
168
169 /*
170  * This is part of the STATS replies. There is no offical numeric for this
171  * since this isnt an official command, in much the same way as HASH isnt.
172  * It is also possible that some systems wont support this call or have
173  * different field names for "struct rusage".
174  * -avalon
175  */
176 void send_usage(struct Client *cptr, struct StatDesc *sd, int stat,
177                 char *param)
178 {
179   os_get_rusage(cptr, CurrentTime - cli_since(&me), debug_enumerator);
180
181   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":DBUF alloc %d used %d",
182              DBufAllocCount, DBufUsedCount);
183 }
184 #endif /* DEBUGMODE */
185
186 void count_memory(struct Client *cptr, struct StatDesc *sd, int stat,
187                   char *param)
188 {
189   struct Client *acptr;
190   struct SLink *link;
191   struct Channel *chptr;
192   struct ConfItem *aconf;
193   const struct ConnectionClass* cltmp;
194   struct Membership* member;
195
196   int acc = 0,                  /* accounts */
197       c = 0,                    /* clients */
198       cn = 0,                   /* connections */
199       ch = 0,                   /* channels */
200       lcc = 0,                  /* local client conf links */
201       us = 0,                   /* user structs */
202       chi = 0,                  /* channel invites */
203       chb = 0,                  /* channel bans */
204       wwu = 0,                  /* whowas users */
205       cl = 0,                   /* classes */
206       co = 0,                   /* conf lines */
207       memberships = 0;          /* channel memberships */
208
209   int usi = 0,                  /* users invited */
210       aw = 0,                   /* aways set */
211       wwa = 0,                  /* whowas aways */
212       gl = 0,                   /* glines */
213       ju = 0;                   /* jupes */
214
215   size_t chm = 0,               /* memory used by channels */
216       chbm = 0,                 /* memory used by channel bans */
217       cm = 0,                   /* memory used by clients */
218       cnm = 0,                  /* memory used by connections */
219       awm = 0,                  /* memory used by aways */
220       wwam = 0,                 /* whowas away memory used */
221       wwm = 0,                  /* whowas array memory used */
222       glm = 0,                  /* memory used by glines */
223       jum = 0,                  /* memory used by jupes */
224       com = 0,                  /* memory used by conf lines */
225       dbufs_allocated = 0,      /* memory used by dbufs */
226       dbufs_used = 0,           /* memory used by dbufs */
227       msg_allocated = 0,        /* memory used by struct Msg */
228       msgbuf_allocated = 0,     /* memory used by struct MsgBuf */
229       rm = 0,                   /* res memory used */
230       totcl = 0, totch = 0, totww = 0, tot = 0;
231
232   count_whowas_memory(&wwu, &wwm, &wwa, &wwam);
233   wwm += sizeof(struct Whowas) * feature_int(FEAT_NICKNAMEHISTORYLENGTH);
234   wwm += sizeof(struct Whowas *) * WW_MAX;
235
236   for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
237   {
238     c++;
239     if (MyConnect(acptr))
240     {
241       cn++;
242       for (link = cli_confs(acptr); link; link = link->next)
243         lcc++;
244     }
245     if (cli_user(acptr))
246     {
247       us++;
248       for (link = cli_user(acptr)->invited; link; link = link->next)
249         usi++;
250       for (member = cli_user(acptr)->channel; member; member = member->next_channel)
251         ++memberships;
252       if (cli_user(acptr)->away)
253       {
254         aw++;
255         awm += (strlen(cli_user(acptr)->away) + 1);
256       }
257     }
258
259     if (IsAccount(acptr))
260       acc++;
261   }
262   cm = c * sizeof(struct Client);
263   cnm = cn * sizeof(struct Connection);
264
265   for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
266   {
267     ch++;
268     chm += (strlen(chptr->chname) + sizeof(struct Channel));
269     for (link = chptr->invites; link; link = link->next)
270       chi++;
271     for (link = chptr->banlist; link; link = link->next)
272     {
273       chb++;
274       chbm += (strlen(link->value.cp) + 1 + sizeof(struct SLink));
275     }
276   }
277
278   for (aconf = GlobalConfList; aconf; aconf = aconf->next)
279   {
280     co++;
281     com += aconf->host ? strlen(aconf->host) + 1 : 0;
282     com += aconf->passwd ? strlen(aconf->passwd) + 1 : 0;
283     com += aconf->name ? strlen(aconf->name) + 1 : 0;
284     com += sizeof(struct ConfItem);
285   }
286
287   for (cltmp = get_class_list(); cltmp; cltmp = cltmp->next)
288     cl++;
289
290   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
291              ":Clients %d(%zu) Connections %d(%zu)", c, cm, cn, cnm);
292   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
293              ":Users %d(%zu) Accounts %d(%zu) Invites %d(%zu)",
294              us, us * sizeof(struct User), acc, acc * (ACCOUNTLEN + 1),
295              usi, usi * sizeof(struct SLink));
296   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
297              ":User channels %d(%zu) Aways %d(%zu)", memberships,
298              memberships * sizeof(struct Membership), aw, awm);
299   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Attached confs %d(%zu)",
300              lcc, lcc * sizeof(struct SLink));
301
302   totcl = cm + cnm + us * sizeof(struct User) + memberships * sizeof(struct Membership) + awm;
303   totcl += lcc * sizeof(struct SLink) + usi * sizeof(struct SLink);
304
305   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Conflines %d(%zu)", co,
306              com);
307
308   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes %d(%zu)", cl,
309              cl * sizeof(struct ConnectionClass));
310
311   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
312              ":Channels %d(%zu) Bans %d(%zu)", ch, chm, chb, chbm);
313   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
314              ":Channel membrs %d(%zu) invite %d(%zu)", memberships,
315              memberships * sizeof(struct Membership), chi,
316              chi * sizeof(struct SLink));
317
318   totch = chm + chbm + chi * sizeof(struct SLink);
319
320   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
321              ":Whowas users %d(%zu) away %d(%zu)", wwu,
322              wwu * sizeof(struct User), wwa, wwam);
323   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Whowas array %d(%zu)",
324              feature_int(FEAT_NICKNAMEHISTORYLENGTH), wwm);
325
326   totww = wwu * sizeof(struct User) + wwam + wwm;
327
328   motd_memory_count(cptr);
329
330   gl = gline_memory_count(&glm);
331   ju = jupe_memory_count(&jum);
332   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
333              ":Glines %d(%zu) Jupes %d(%zu)", gl, glm, ju, jum);
334
335   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
336              ":Hash: client %d(%zu), chan is the same", HASHSIZE,
337              sizeof(void *) * HASHSIZE);
338
339   /*
340    * NOTE: this count will be accurate only for the exact instant that this
341    * message is being sent, so the count is affected by the dbufs that
342    * are being used to send this message out. If this is not desired, move
343    * the dbuf_count_memory call to a place before we start sending messages
344    * and cache DBufAllocCount and DBufUsedCount in variables until they 
345    * are sent.
346    */
347   dbuf_count_memory(&dbufs_allocated, &dbufs_used);
348   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
349              ":DBufs allocated %d(%zu) used %d(%zu)", DBufAllocCount,
350              dbufs_allocated, DBufUsedCount, dbufs_used);
351
352   /* The DBuf caveats now count for this, but this routine now sends
353    * replies all on its own.
354    */
355   msgq_count_memory(cptr, &msg_allocated, &msgbuf_allocated);
356
357   rm = cres_mem(cptr);
358
359   tot =
360       totww + totch + totcl + com + cl * sizeof(struct ConnectionClass) +
361       dbufs_allocated + msg_allocated + msgbuf_allocated + rm;
362   tot += sizeof(void *) * HASHSIZE * 3;
363
364 #if defined(MDEBUG)
365   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Allocations: %zu(%zu)",
366              fda_get_block_count(), fda_get_byte_count());
367 #endif
368
369   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
370              ":Total: ww %zu ch %zu cl %zu co %zu db %zu ms %zu mb %zu",
371              totww, totch, totcl, com, dbufs_allocated, msg_allocated,
372              msgbuf_allocated);
373 }
374