Author: Bleep <tomh@inxpress.net>
[ircu2.10.12-pk.git] / ircd / support.c
1 /*
2  * IRC - Internet Relay Chat, common/support.c
3  * Copyright (C) 1990, 1991 Armin Gruner
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 1, or (at your option)
8  * any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * $Id$
20  */
21 #include "support.h"
22 #include "fileio.h"
23 #include "ircd.h"
24 #include "ircd_chattr.h"
25 #include "s_bsd.h"
26 #include "s_debug.h"
27 #include "send.h"
28 #include "sprintf_irc.h"
29 #include "sys.h"
30
31 #include <signal.h>   /* kill */
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <time.h>
36 #include <unistd.h>
37
38 #ifdef DEBUGMODE
39
40 void dumpcore(const char *pattern, ...)
41 {
42   va_list vl;
43   static time_t lastd = 0;
44   static int dumps = 0;
45   char corename[12];
46   time_t now;
47   int p;
48
49   va_start(vl, pattern);
50
51   now = time(NULL);
52
53   if (!lastd)
54     lastd = now;
55   else if (now - lastd < 60 && dumps > 2)
56     server_die("too many core dumps");
57   if (now - lastd > 60)
58   {
59     lastd = now;
60     dumps = 1;
61   }
62   else
63     dumps++;
64   p = getpid();
65   if (fork() > 0)
66   {
67     kill(p, 3);
68     kill(p, 9);
69   }
70   sprintf_irc(corename, "core.%d", p);
71   rename("core", corename);
72   Debug((DEBUG_FATAL, "Dumped core : core.%d", p));
73   sendto_ops("Dumped core : core.%d", p);
74   vdebug(DEBUG_FATAL, pattern, vl);
75   vsendto_ops(pattern, vl);
76   va_end(vl);
77
78   server_die("debug core dump");
79
80 }
81 #endif
82
83 int check_if_ipmask(const char *mask)
84 {
85   int has_digit = 0;
86   const char *p;
87
88   for (p = mask; *p; ++p)
89     if (*p != '*' && *p != '?' && *p != '.')
90     {
91       if (!IsDigit(*p))
92         return 0;
93       has_digit = -1;
94     }
95
96   return has_digit;
97 }
98
99 /* Moved from logf() in whocmds.c to here. Modified a 
100  * bit and used for most logging now.
101  *  -Ghostwolf 12-Jul-99
102  */
103
104 extern void write_log(const char *filename, const char *pattern, ...)
105 {
106   FBFILE *logfile;
107   va_list vl;
108   static char logbuf[1024];
109
110   logfile = fbopen(filename, "a");
111
112   if (logfile)
113   {
114     va_start(vl, pattern);
115     vsprintf_irc(logbuf, pattern, vl);
116     va_end(vl);
117
118     fbputs(logbuf, logfile);
119     fbclose(logfile);
120   }
121 }