]>
Commit | Line | Data |
---|---|---|
3bd189cb JR |
1 | /************************************************************************ |
2 | * IRC - Internet Relay Chat, iauth/iauthd.c | |
3 | * Copyright (C) 1998 Christophe Kalt | |
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 | ||
20 | #ifndef lint | |
21 | static const volatile char rcsid[] = "@(#)$Id: iauth.c,v 1.18 2005/01/03 22:16:59 q Exp $"; | |
22 | #endif | |
23 | ||
24 | #include "os.h" | |
25 | #include "a_defines.h" | |
26 | #define IAUTH_C | |
27 | #include "a_externs.h" | |
28 | #undef IAUTH_C | |
29 | ||
30 | static int do_log = 0; | |
31 | ||
32 | static RETSIGTYPE dummy(int s) | |
33 | { | |
34 | /* from common/bsd.c */ | |
35 | #ifndef HAVE_RELIABLE_SIGNALS | |
36 | (void)signal(SIGALRM, dummy); | |
37 | (void)signal(SIGPIPE, dummy); | |
38 | # ifndef HPUX /* Only 9k/800 series require this, but don't know how to.. */ | |
39 | # ifdef SIGWINCH | |
40 | (void)signal(SIGWINCH, dummy); | |
41 | # endif | |
42 | # endif | |
43 | #else | |
44 | # if POSIX_SIGNALS | |
45 | struct sigaction act; | |
46 | ||
47 | act.sa_handler = dummy; | |
48 | act.sa_flags = 0; | |
49 | (void)sigemptyset(&act.sa_mask); | |
50 | (void)sigaddset(&act.sa_mask, SIGALRM); | |
51 | (void)sigaddset(&act.sa_mask, SIGPIPE); | |
52 | # ifdef SIGWINCH | |
53 | (void)sigaddset(&act.sa_mask, SIGWINCH); | |
54 | # endif | |
55 | (void)sigaction(SIGALRM, &act, (struct sigaction *)NULL); | |
56 | (void)sigaction(SIGPIPE, &act, (struct sigaction *)NULL); | |
57 | # ifdef SIGWINCH | |
58 | (void)sigaction(SIGWINCH, &act, (struct sigaction *)NULL); | |
59 | # endif | |
60 | # endif | |
61 | #endif | |
62 | } | |
63 | ||
64 | static RETSIGTYPE s_log(int s) | |
65 | { | |
66 | # if POSIX_SIGNALS | |
67 | struct sigaction act; | |
68 | ||
69 | act.sa_handler = s_log; | |
70 | act.sa_flags = 0; | |
71 | (void)sigemptyset(&act.sa_mask); | |
72 | (void)sigaddset(&act.sa_mask, SIGUSR2); | |
73 | (void)sigaction(SIGUSR2, &act, NULL); | |
74 | # else | |
75 | (void)signal(SIGUSR2, s_log); | |
76 | # endif | |
77 | do_log = 1; | |
78 | } | |
79 | ||
80 | static void init_signals(void) | |
81 | { | |
82 | /* from ircd/ircd.c setup_signals() */ | |
83 | #if POSIX_SIGNALS | |
84 | struct sigaction act; | |
85 | ||
86 | act.sa_handler = SIG_IGN; | |
87 | act.sa_flags = 0; | |
88 | (void)sigemptyset(&act.sa_mask); | |
89 | (void)sigaddset(&act.sa_mask, SIGPIPE); | |
90 | (void)sigaddset(&act.sa_mask, SIGALRM); | |
91 | # ifdef SIGWINCH | |
92 | (void)sigaddset(&act.sa_mask, SIGWINCH); | |
93 | (void)sigaction(SIGWINCH, &act, NULL); | |
94 | # endif | |
95 | (void)sigaction(SIGPIPE, &act, NULL); | |
96 | act.sa_handler = dummy; | |
97 | (void)sigaction(SIGALRM, &act, NULL); | |
98 | /* | |
99 | act.sa_handler = s_rehash; | |
100 | (void)sigemptyset(&act.sa_mask); | |
101 | (void)sigaddset(&act.sa_mask, SIGHUP); | |
102 | (void)sigaction(SIGHUP, &act, NULL); | |
103 | act.sa_handler = s_restart; | |
104 | (void)sigaddset(&act.sa_mask, SIGINT); | |
105 | (void)sigaction(SIGINT, &act, NULL); | |
106 | act.sa_handler = s_die; | |
107 | (void)sigaddset(&act.sa_mask, SIGTERM); | |
108 | (void)sigaction(SIGTERM, &act, NULL); | |
109 | */ | |
110 | act.sa_handler = s_log; | |
111 | (void)sigaddset(&act.sa_mask, SIGUSR2); | |
112 | (void)sigaction(SIGUSR2, &act, NULL); | |
113 | #else | |
114 | # ifndef HAVE_RELIABLE_SIGNALS | |
115 | (void)signal(SIGPIPE, dummy); | |
116 | # ifdef SIGWINCH | |
117 | (void)signal(SIGWINCH, dummy); | |
118 | # endif | |
119 | # else | |
120 | # ifdef SIGWINCH | |
121 | (void)signal(SIGWINCH, SIG_IGN); | |
122 | # endif | |
123 | (void)signal(SIGPIPE, SIG_IGN); | |
124 | # endif | |
125 | (void)signal(SIGALRM, dummy); | |
126 | /* | |
127 | (void)signal(SIGHUP, s_rehash); | |
128 | (void)signal(SIGTERM, s_die); | |
129 | (void)signal(SIGINT, s_restart); | |
130 | */ | |
131 | (void)signal(SIGUSR2, s_log); | |
132 | #endif | |
133 | } | |
134 | ||
135 | void write_pidfile(void) | |
136 | { | |
137 | int fd; | |
138 | char pidbuf[32]; | |
139 | (void) truncate(IAUTHPID_PATH, 0); | |
140 | if (( fd = open(IAUTHPID_PATH, O_CREAT|O_WRONLY, 0600)) >= 0) | |
141 | { | |
142 | memset(pidbuf, '0', sizeof(pidbuf)); | |
143 | (void) sprintf(pidbuf, "%d\n", (int)getpid()); | |
144 | if (write(fd, pidbuf, strlen(pidbuf)) == -1) | |
145 | { | |
146 | (void) printf("Error writing pidfile %s\n", | |
147 | IAUTHPID_PATH); | |
148 | } | |
149 | (void) close(fd); | |
150 | } | |
151 | else | |
152 | { | |
153 | (void) printf("Error opening pidfile %s\n", | |
154 | IAUTHPID_PATH); | |
155 | } | |
156 | return; | |
157 | } | |
158 | ||
159 | int main(int argc, char *argv[]) | |
160 | { | |
161 | time_t nextst = time(NULL) + 90; | |
162 | char *xopt; | |
163 | ||
164 | if (argc == 2 && !strcmp(argv[1], "-X")) | |
165 | exit(0); | |
166 | ||
167 | if (isatty(0)) | |
168 | { | |
169 | (void)printf("iauth %s", make_version()); | |
170 | #if defined(USE_DSM) | |
171 | (void)printf(" (with DSM support)\n"); | |
172 | #else | |
173 | (void)printf("\n"); | |
174 | #endif | |
175 | if (argc == 3 && !strcmp(argv[1], "-c")) | |
176 | { | |
177 | (void)printf("\nReading \"%s\"\n\n", argv[2]); | |
178 | conf_read(argv[2]); | |
179 | } | |
180 | else | |
181 | { | |
182 | #if defined(INET6) | |
183 | (void)printf("\t+INET6\n"); | |
184 | #endif | |
185 | #if defined(IAUTH_DEBUG) | |
186 | (void)printf("\t+IAUTH_DEBUG\n"); | |
187 | #endif | |
188 | #if defined(USE_POLL) | |
189 | (void)printf("\t+USE_POLL\n"); | |
190 | #endif | |
191 | } | |
192 | exit(0); | |
193 | } | |
194 | ||
195 | init_signals(); | |
196 | init_syslog(); | |
197 | xopt = conf_read(NULL); | |
198 | init_filelogs(); | |
199 | sendto_log(ALOG_DMISC, LOG_NOTICE, "Daemon starting (%s%s).", | |
200 | make_version(), | |
201 | #if defined(IAUTH_DEBUG) | |
202 | "+debug" | |
203 | #else | |
204 | "" | |
205 | #endif | |
206 | ); | |
207 | init_io(); | |
208 | sendto_ircd("V %s", make_version()); | |
209 | sendto_ircd("O %s", xopt); | |
210 | conf_ircd(); | |
211 | ||
212 | #if defined(IAUTH_DEBUG) | |
213 | if (debuglevel & ALOG_DIRCD) | |
214 | sendto_ircd("G 1"); | |
215 | else | |
216 | #endif | |
217 | sendto_ircd("G 0"); | |
218 | ||
219 | write_pidfile(); | |
220 | while (1) | |
221 | { | |
222 | loop_io(); | |
223 | ||
224 | if (do_log) | |
225 | { | |
226 | sendto_log(ALOG_IRCD|ALOG_DMISC, LOG_INFO, | |
227 | "Got SIGUSR2, reinitializing log file(s)."); | |
228 | init_filelogs(); | |
229 | do_log = 0; | |
230 | } | |
231 | ||
232 | if (time(NULL) > nextst) | |
233 | { | |
234 | AnInstance *itmp = instances; | |
235 | ||
236 | sendto_ircd("s"); | |
237 | while (itmp) | |
238 | { | |
239 | if (itmp->mod->stats) | |
240 | itmp->mod->stats(itmp); | |
241 | itmp = itmp->nexti; | |
242 | } | |
243 | nextst = time(NULL) + 60; | |
244 | } | |
245 | } | |
246 | } | |
247 |