2 * IRC - Internet Relay Chat, ircd/s_debug.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
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)
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.
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.
25 #if defined(HPUX) && HAVE_FCNTL_H
29 #include <sys/syscall.h>
30 #define getrusage(a,b) syscall(SYS_GETRUSAGE, a, b)
33 #include <sys/resource.h>
36 #include <sys/times.h>
64 * Option string. Must be before #ifdef DEBUGMODE.
67 #if BUFFERPOOL < 1000000
69 #if BUFFERPOOL > 99999
70 (char)('0' + (BUFFERPOOL/100000)),
73 (char)('0' + (BUFFERPOOL/10000) % 10),
75 (char)('0' + (BUFFERPOOL/1000) % 10),
78 #if BUFFERPOOL > 99999999
79 (char)('0' + (BUFFERPOOL/100000000)),
81 #if BUFFERPOOL > 9999999
82 (char)('0' + (BUFFERPOOL/10000000) % 10),
84 (char)('0' + (BUFFERPOOL/1000000) % 10),
107 #if defined(SHOW_INVISIBLE_USERS) || defined(SHOW_ALL_INVISIBLE_USERS)
108 #ifdef SHOW_ALL_INVISIBLE_USERS
115 #ifdef LOCAL_KILL_ONLY
130 #ifdef CRYPT_OPER_PASSWORD
133 #ifdef CRYPT_LINK_PASSWORD
143 #ifdef RELIABLE_CLOCK
155 #if defined(USE_POLL) && defined(HAVE_POLL_H)
173 static char debugbuf[1024];
175 void vdebug(int level, const char *form, va_list vl)
179 if ((debuglevel >= 0) && (level <= debuglevel))
181 vsprintf(debugbuf, form, vl);
184 loc_clients[2]->sendM++;
185 loc_clients[2]->sendB += strlen(debugbuf);
187 fprintf(stderr, "%s", debugbuf);
193 void debug(int level, const char *form, ...)
197 vdebug(level, form, vl);
202 * This is part of the STATS replies. There is no offical numeric for this
203 * since this isnt an official command, in much the same way as HASH isnt.
204 * It is also possible that some systems wont support this call or have
205 * different field names for "struct rusage".
208 void send_usage(aClient *cptr, char *nick)
222 hzz = (int)sysconf(_SC_CLK_TCK);
227 if (getrusage(RUSAGE_SELF, &rus) == -1)
229 if (MyUser(cptr) || Protocol(cptr->from) < 10)
230 sendto_one(cptr, ":%s NOTICE %s :Getruseage error: %s.",
231 me.name, nick, sys_errlist[errno]);
233 sendto_one(cptr, "%s NOTICE %s%s :Getruseage error: %s.",
234 NumServ(&me), NumNick(cptr), sys_errlist[errno]);
237 secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
238 rup = now - me.since;
242 #if defined(__sun__) || defined(__bsdi__) || (__GLIBC__ >= 2) || defined(__NetBSD__)
243 sendto_one(cptr, ":%s %d %s :CPU Secs %ld:%ld User %ld:%ld System %ld:%ld",
245 sendto_one(cptr, ":%s %d %s :CPU Secs %ld:%ld User %d:%d System %d:%d",
247 me.name, RPL_STATSDEBUG, nick, secs / 60, secs % 60,
248 rus.ru_utime.tv_sec / 60, rus.ru_utime.tv_sec % 60,
249 rus.ru_stime.tv_sec / 60, rus.ru_stime.tv_sec % 60);
250 sendto_one(cptr, ":%s %d %s :RSS %ld ShMem %ld Data %ld Stack %ld",
251 me.name, RPL_STATSDEBUG, nick, rus.ru_maxrss,
252 rus.ru_ixrss / (rup * hzz), rus.ru_idrss / (rup * hzz),
253 rus.ru_isrss / (rup * hzz));
254 sendto_one(cptr, ":%s %d %s :Swaps %ld Reclaims %ld Faults %ld",
255 me.name, RPL_STATSDEBUG, nick, rus.ru_nswap,
256 rus.ru_minflt, rus.ru_majflt);
257 sendto_one(cptr, ":%s %d %s :Block in %ld out %ld",
258 me.name, RPL_STATSDEBUG, nick, rus.ru_inblock, rus.ru_oublock);
259 sendto_one(cptr, ":%s %d %s :Msg Rcv %ld Send %ld",
260 me.name, RPL_STATSDEBUG, nick, rus.ru_msgrcv, rus.ru_msgsnd);
261 sendto_one(cptr, ":%s %d %s :Signals %ld Context Vol. %ld Invol %ld",
262 me.name, RPL_STATSDEBUG, nick, rus.ru_nsignals,
263 rus.ru_nvcsw, rus.ru_nivcsw);
264 #else /* HAVE_GETRUSAGE */
268 int hzz = 1, ticpermin;
269 int umin, smin, usec, ssec;
272 hzz = sysconf(_SC_CLK_TCK);
274 ticpermin = hzz * 60;
276 umin = tmsbuf.tms_utime / ticpermin;
277 usec = (tmsbuf.tms_utime % ticpermin) / (float)hzz;
278 smin = tmsbuf.tms_stime / ticpermin;
279 ssec = (tmsbuf.tms_stime % ticpermin) / (float)hzz;
281 mins = (secs / 60) + umin + smin;
284 if (times(&tmsbuf) == -1)
286 sendto_one(cptr, ":%s %d %s :times(2) error: %s.",
287 me.name, RPL_STATSDEBUG, nick, strerror(errno));
290 secs = tmsbuf.tms_utime + tmsbuf.tms_stime;
292 sendto_one(cptr, ":%s %d %s :CPU Secs %d:%d User %d:%d System %d:%d",
293 me.name, RPL_STATSDEBUG, nick, mins, secs, umin, usec, smin, ssec);
294 #endif /* HAVE_TIMES */
295 #endif /* HAVE_GETRUSAGE */
296 sendto_one(cptr, ":%s %d %s :Reads %d Writes %d",
297 me.name, RPL_STATSDEBUG, nick, readcalls, writecalls);
298 sendto_one(cptr, ":%s %d %s :DBUF alloc %d used %d",
299 me.name, RPL_STATSDEBUG, nick, DBufAllocCount, DBufUsedCount);
301 ":%s %d %s :Writes: <0 %d 0 %d <16 %d <32 %d <64 %d",
302 me.name, RPL_STATSDEBUG, nick,
303 writeb[0], writeb[1], writeb[2], writeb[3], writeb[4]);
305 ":%s %d %s :<128 %d <256 %d <512 %d <1024 %d >1024 %d",
306 me.name, RPL_STATSDEBUG, nick,
307 writeb[5], writeb[6], writeb[7], writeb[8], writeb[9]);
310 #endif /* DEBUGMODE */
312 void count_memory(aClient *cptr, char *nick)
316 Reg3 aChannel *chptr;
317 Reg4 aConfItem *aconf;
318 Reg5 aConfClass *cltmp;
320 int lc = 0, /* local clients */
321 ch = 0, /* channels */
322 lcc = 0, /* local client conf links */
323 rc = 0, /* remote clients */
324 us = 0, /* user structs */
325 chu = 0, /* channel users */
326 chi = 0, /* channel invites */
327 chb = 0, /* channel bans */
328 wwu = 0, /* whowas users */
329 cl = 0, /* classes */
330 co = 0; /* conf lines */
332 int usi = 0, /* users invited */
333 usc = 0, /* users in channels */
334 aw = 0, /* aways set */
335 wwa = 0; /* whowas aways */
337 size_t chm = 0, /* memory used by channels */
338 chbm = 0, /* memory used by channel bans */
339 lcm = 0, /* memory used by local clients */
340 rcm = 0, /* memory used by remote clients */
341 awm = 0, /* memory used by aways */
342 wwam = 0, /* whowas away memory used */
343 wwm = 0, /* whowas array memory used */
344 com = 0, /* memory used by conf lines */
345 dbufs_allocated = 0, /* memory used by dbufs */
346 dbufs_used = 0, /* memory used by dbufs */
347 rm = 0, /* res memory used */
348 totcl = 0, totch = 0, totww = 0, tot = 0;
350 count_whowas_memory(&wwu, &wwm, &wwa, &wwam);
351 wwm += sizeof(aWhowas) * NICKNAMEHISTORYLENGTH;
352 wwm += sizeof(aWhowas *) * WW_MAX;
354 for (acptr = client; acptr; acptr = acptr->next)
358 if (MyConnect(acptr))
361 for (link = acptr->confs; link; link = link->next)
369 for (link = acptr->user->invited; link; link = link->next)
371 for (link = acptr->user->channel; link; link = link->next)
373 if (acptr->user->away)
376 awm += (strlen(acptr->user->away) + 1);
380 lcm = lc * CLIENT_LOCAL_SIZE;
381 rcm = rc * CLIENT_REMOTE_SIZE;
383 for (chptr = channel; chptr; chptr = chptr->nextch)
386 chm += (strlen(chptr->chname) + sizeof(aChannel));
387 for (link = chptr->members; link; link = link->next)
389 for (link = chptr->invites; link; link = link->next)
391 for (link = chptr->banlist; link; link = link->next)
394 chbm += (strlen(link->value.cp) + 1 + sizeof(Link));
398 for (aconf = conf; aconf; aconf = aconf->next)
401 com += aconf->host ? strlen(aconf->host) + 1 : 0;
402 com += aconf->passwd ? strlen(aconf->passwd) + 1 : 0;
403 com += aconf->name ? strlen(aconf->name) + 1 : 0;
404 com += sizeof(aConfItem);
407 for (cltmp = classes; cltmp; cltmp = cltmp->next)
410 sendto_one(cptr, ":%s %d %s :Client Local %d(" SIZE_T_FMT
411 ") Remote %d(" SIZE_T_FMT ")",
412 me.name, RPL_STATSDEBUG, nick, lc, lcm, rc, rcm);
413 sendto_one(cptr, ":%s %d %s :Users %d(" SIZE_T_FMT
414 ") Invites %d(" SIZE_T_FMT ")",
415 me.name, RPL_STATSDEBUG, nick, us, us * sizeof(anUser), usi,
417 sendto_one(cptr, ":%s %d %s :User channels %d(" SIZE_T_FMT
418 ") Aways %d(" SIZE_T_FMT ")",
419 me.name, RPL_STATSDEBUG, nick, usc, usc * sizeof(Link), aw, awm);
420 sendto_one(cptr, ":%s %d %s :Attached confs %d(" SIZE_T_FMT ")",
421 me.name, RPL_STATSDEBUG, nick, lcc, lcc * sizeof(Link));
423 totcl = lcm + rcm + us * sizeof(anUser) + usc * sizeof(Link) + awm;
424 totcl += lcc * sizeof(Link) + usi * sizeof(Link);
426 sendto_one(cptr, ":%s %d %s :Conflines %d(" SIZE_T_FMT ")",
427 me.name, RPL_STATSDEBUG, nick, co, com);
429 sendto_one(cptr, ":%s %d %s :Classes %d(" SIZE_T_FMT ")",
430 me.name, RPL_STATSDEBUG, nick, cl, cl * sizeof(aConfClass));
432 sendto_one(cptr, ":%s %d %s :Channels %d(" SIZE_T_FMT
433 ") Bans %d(" SIZE_T_FMT ")",
434 me.name, RPL_STATSDEBUG, nick, ch, chm, chb, chbm);
435 sendto_one(cptr, ":%s %d %s :Channel membrs %d(" SIZE_T_FMT
436 ") invite %d(" SIZE_T_FMT ")",
437 me.name, RPL_STATSDEBUG, nick, chu, chu * sizeof(Link),
438 chi, chi * sizeof(Link));
440 totch = chm + chbm + chu * sizeof(Link) + chi * sizeof(Link);
442 sendto_one(cptr, ":%s %d %s :Whowas users %d(" SIZE_T_FMT
443 ") away %d(" SIZE_T_FMT ")",
444 me.name, RPL_STATSDEBUG, nick, wwu, wwu * sizeof(anUser), wwa, wwam);
445 sendto_one(cptr, ":%s %d %s :Whowas array %d(" SIZE_T_FMT ")",
446 me.name, RPL_STATSDEBUG, nick, NICKNAMEHISTORYLENGTH, wwm);
448 totww = wwu * sizeof(anUser) + wwam + wwm;
450 sendto_one(cptr, ":%s %d %s :Hash: client %d(" SIZE_T_FMT
451 "), chan is the same",
452 me.name, RPL_STATSDEBUG, nick, HASHSIZE, sizeof(void *) * HASHSIZE);
455 * NOTE: this count will be accurate only for the exact instant that this
456 * message is being sent, so the count is affected by the dbufs that
457 * are being used to send this message out. If this is not desired, move
458 * the dbuf_count_memory call to a place before we start sending messages
459 * and cache DBufAllocCount and DBufUsedCount in variables until they
462 dbuf_count_memory(&dbufs_allocated, &dbufs_used);
464 ":%s %d %s :DBufs allocated %d(" SIZE_T_FMT ") used %d(" SIZE_T_FMT ")",
465 me.name, RPL_STATSDEBUG, nick, DBufAllocCount, dbufs_allocated,
466 DBufUsedCount, dbufs_used);
471 totww + totch + totcl + com + cl * sizeof(aConfClass) + dbufs_allocated +
473 tot += sizeof(void *) * HASHSIZE * 3;
475 sendto_one(cptr, ":%s %d %s :Total: ww " SIZE_T_FMT " ch " SIZE_T_FMT
476 " cl " SIZE_T_FMT " co " SIZE_T_FMT " db " SIZE_T_FMT,
477 me.name, RPL_STATSDEBUG, nick, totww, totch, totcl, com, dbufs_allocated);
481 #ifdef MSGLOG_ENABLED
483 /* Define here what level of messages you want to log */
484 #define LOG_MASK_LEVEL LEVEL_MODE /* This that change some data */
486 static struct log_entry log_table[MSGLOG_SIZE];
488 static int unused_log_entries = MSGLOG_SIZE;
489 static int last_log_entry = -1; /* Nothing stored yet */
490 static int entry_stored_forlog = 0; /* Just a flag */
495 * Just a little utility function used to retract
496 * an half stored Message log entry
498 void RollBackMsgLog(void)
500 /* We won't log this, abort and free the entry */
502 unused_log_entries++;
507 * Log_Message (macroed as LogMessage)
509 * Permanently stores a log entry into the recent log memory area
510 * Store_Buffer MUST have been called before calling Log_Message
512 void Log_Message(aClient *sptr, int msgclass)
514 register int n = last_log_entry;
516 /* Clear our flag, since we are going to
517 * finish the processing of this entry */
518 entry_stored_forlog = 0;
520 /* Check if the level of this message is high enough */
521 if (msgclass < LOG_MASK_LEVEL)
527 /* Check if we wanna log the type of connection from
528 * where this message did come from */
529 if (!((0x8000 >> (8 + log_table[n].cptr_status)) & LOG_MASK_TYPE))
535 /* Complete the entry */
538 log_table[n].sptr_status = sptr->status;
539 strncpy(log_table[n].sptr_name, sptr->name, HOSTLEN);
540 log_table[n].sptr_name[HOSTLEN] = '\0';
541 strncpy(log_table[n].sptr_yxx, sptr->yxx, 4);
542 log_table[n].sptr = sptr;
546 strncpy(log_table[n].sptr_from_name, sptr->name, HOSTLEN);
547 log_table[n].sptr_from_name[HOSTLEN] = '\0';
551 memset(log_table[n].sptr_from_name, 0, HOSTLEN);
556 log_table[n].sptr_status = 0xFF; /* Dummy value */
557 memset(&log_table[n].sptr_name, 0, HOSTLEN);
558 memset(log_table[n].sptr_yxx, 0, 4);
560 log_table[n].sptr = 0;
561 memset(&log_table[n].sptr_from_name, 0, HOSTLEN);
566 * Store_Buffer (macroed as StoreBuffer)
568 * Saves the buffer and cptr info at the very first stage
569 * of parsing, if Log_Message doesn't get called between
570 * two Store_Buffer calls this function assumes that the parser
571 * has rejected the message and therefore calls Log_Message
572 * as if the message class was 0 and the sptr null
574 void Store_Buffer(char *buf, aClient *cptr)
578 /* Check if we have an entry pending, if so
579 * complete it's processing */
580 if (entry_stored_forlog)
581 Log_Message((aClient *)NULL, 0);
583 /* Update the "half used entry" flag */
584 entry_stored_forlog = 1;
586 /* First update the free entries counter */
587 if (unused_log_entries)
588 unused_log_entries--;
591 n = (last_log_entry + 1) % MSGLOG_SIZE;
593 /* Update the last_log_entry index */
596 /* Store what we have by now in it */
597 log_table[n].cptr_status = cptr->status;
598 strncpy(log_table[n].cptr_name, cptr->name, HOSTLEN);
599 log_table[n].cptr_name[HOSTLEN] = '\0';
600 strncpy(log_table[n].cptr_yxx, cptr->yxx, 4);
601 log_table[n].cptr_fd = cptr->fd;
602 log_table[n].cptr = cptr; /* No checking for this, is lossy */
603 strncpy(log_table[n].buffer, buf, 511);
606 #endif /* MSGLOG_ENABLED */