ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 7858
Committed: Tue Nov 8 20:06:16 2016 UTC (8 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 12930 byte(s)
Log Message:
- Import FLOODTIME changes from p4

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2016 ircd-hybrid development team
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 2 of the License, or
9 * (at your option) 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 * USA
20 */
21
22 /*! \file ircd.c
23 * \brief Starts up and runs the ircd.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #include "user.h"
29 #include "list.h"
30 #include "ircd.h"
31 #include "channel.h"
32 #include "client.h"
33 #include "event.h"
34 #include "fdlist.h"
35 #include "hash.h"
36 #include "id.h"
37 #include "irc_string.h"
38 #include "ircd_signal.h"
39 #include "motd.h"
40 #include "conf.h"
41 #include "hostmask.h"
42 #include "parse.h"
43 #include "res.h"
44 #include "restart.h"
45 #include "rng_mt.h"
46 #include "auth.h"
47 #include "s_bsd.h"
48 #include "log.h"
49 #include "server.h"
50 #include "send.h"
51 #include "whowas.h"
52 #include "modules.h"
53 #include "memory.h"
54 #include "mempool.h"
55 #include "ircd_getopt.h"
56 #include "watch.h"
57 #include "conf_db.h"
58 #include "conf_class.h"
59 #include "ipcache.h"
60 #include "isupport.h"
61 #include "userhost.h"
62
63
64 #ifdef HAVE_LIBGEOIP
65 GeoIP *GeoIPv4_ctx;
66 GeoIP *GeoIPv6_ctx;
67 #endif
68
69 struct SetOptions GlobalSetOptions; /* /quote set variables */
70 struct Counter Count;
71 struct ServerState_t server_state;
72 struct ServerStatistics ServerStats;
73 struct ServerTime SystemTime;
74 struct Connection meConnection; /* That's also part of me */
75 struct Client me = { .connection = &meConnection }; /* That's me */
76
77 char **myargv;
78 const char *logFileName = LPATH;
79 const char *pidFileName = PPATH;
80
81 unsigned int dorehash;
82 unsigned int doremotd;
83
84 static int printVersion;
85
86 static struct lgetopt myopts[] =
87 {
88 { "configfile", &ConfigGeneral.configfile,
89 STRING, "File to use for ircd.conf" },
90 { "klinefile", &ConfigGeneral.klinefile,
91 STRING, "File to use for kline database" },
92 { "dlinefile", &ConfigGeneral.dlinefile,
93 STRING, "File to use for dline database" },
94 { "xlinefile", &ConfigGeneral.xlinefile,
95 STRING, "File to use for xline database" },
96 { "resvfile", &ConfigGeneral.resvfile,
97 STRING, "File to use for resv database" },
98 { "logfile", &logFileName,
99 STRING, "File to use for ircd.log" },
100 { "pidfile", &pidFileName,
101 STRING, "File to use for process ID" },
102 { "foreground", &server_state.foreground,
103 YESNO, "Run in foreground (don't detach)" },
104 { "version", &printVersion,
105 YESNO, "Print version and exit" },
106 { "help", NULL, USAGE, "Print this text" },
107 { NULL, NULL, STRING, NULL },
108 };
109
110 static struct event event_cleanup_tklines =
111 {
112 .name = "cleanup_tklines",
113 .handler = cleanup_tklines,
114 .when = CLEANUP_TKLINES_TIME
115 };
116
117 static struct event event_try_connections =
118 {
119 .name = "try_connections",
120 .handler = try_connections,
121 .when = STARTUP_CONNECTIONS_TIME
122 };
123
124 static struct event event_comm_checktimeouts =
125 {
126 .name = "comm_checktimeouts",
127 .handler = comm_checktimeouts,
128 .when = 1
129 };
130
131 static struct event event_save_all_databases =
132 {
133 .name = "save_all_databases",
134 .handler = save_all_databases,
135 .when = DATABASE_UPDATE_TIMEOUT
136 };
137
138 struct event event_write_links_file =
139 {
140 .name = "write_links_file",
141 .handler = write_links_file,
142 };
143
144
145 void
146 set_time(void)
147 {
148 struct timeval newtime = { .tv_sec = 0, .tv_usec = 0 };
149
150 if (gettimeofday(&newtime, NULL) == -1)
151 {
152 char buf[IRCD_BUFSIZE];
153
154 snprintf(buf, sizeof(buf), "Clock failure, TS can be corrupted: %s",
155 strerror(errno));
156 server_die(buf, SERVER_SHUTDOWN);
157 }
158
159 if ((uintmax_t)newtime.tv_sec < CurrentTime)
160 {
161 ilog(LOG_TYPE_IRCD, "System clock is running backwards - (%ju < %ju)",
162 (uintmax_t)newtime.tv_sec, CurrentTime);
163 sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE,
164 "System clock is running backwards - (%ju < %ju)",
165 (uintmax_t)newtime.tv_sec, CurrentTime);
166 event_set_back_events(CurrentTime - (uintmax_t)newtime.tv_sec);
167 }
168
169 SystemTime.tv_sec = newtime.tv_sec;
170 SystemTime.tv_usec = newtime.tv_usec;
171 }
172
173 static void
174 io_loop(void)
175 {
176 while (1)
177 {
178 if (listing_client_list.head)
179 {
180 dlink_node *node = NULL, *node_next = NULL;
181 DLINK_FOREACH_SAFE(node, node_next, listing_client_list.head)
182 safe_list_channels(node->data, 0);
183 }
184
185 /* Run pending events */
186 event_run();
187
188 comm_select();
189 exit_aborted_clients();
190 free_exited_clients();
191
192 /* Check to see whether we have to rehash the configuration. */
193 if (dorehash)
194 {
195 conf_rehash(1);
196 dorehash = 0;
197 }
198
199 if (doremotd)
200 {
201 motd_recache();
202 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
203 "Got signal SIGUSR1, reloading motd file(s)");
204 doremotd = 0;
205 }
206 }
207 }
208
209 /* initalialize_global_set_options()
210 *
211 * inputs - none
212 * output - none
213 * side effects - This sets all global set options needed
214 */
215 static void
216 initialize_global_set_options(void)
217 {
218 GlobalSetOptions.maxclients = ConfigServerInfo.default_max_clients;
219 GlobalSetOptions.autoconn = 1;
220 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
221 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
222 GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount;
223 GlobalSetOptions.floodtime = ConfigGeneral.default_floodtime;
224 GlobalSetOptions.joinfloodcount = ConfigChannel.default_join_flood_count;
225 GlobalSetOptions.joinfloodtime = ConfigChannel.default_join_flood_time;
226 GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
227 }
228
229 /* write_pidfile()
230 *
231 * inputs - filename+path of pid file
232 * output - NONE
233 * side effects - write the pid of the ircd to filename
234 */
235 static void
236 write_pidfile(const char *filename)
237 {
238 FILE *fb;
239
240 if ((fb = fopen(filename, "w")))
241 {
242 char buf[IRCD_BUFSIZE];
243 unsigned int pid = (unsigned int)getpid();
244
245 snprintf(buf, sizeof(buf), "%u\n", pid);
246
247 if (fputs(buf, fb) == -1)
248 ilog(LOG_TYPE_IRCD, "Error writing to pid file %s: %s",
249 filename, strerror(errno));
250
251 fclose(fb);
252 }
253 else
254 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
255 filename, strerror(errno));
256 }
257
258 /* check_pidfile()
259 *
260 * inputs - filename+path of pid file
261 * output - none
262 * side effects - reads pid from pidfile and checks if ircd is in process
263 * list. if it is, gracefully exits
264 * -kre
265 */
266 static void
267 check_pidfile(const char *filename)
268 {
269 FILE *fb;
270 char buf[IRCD_BUFSIZE];
271
272 if ((fb = fopen(filename, "r")))
273 {
274 if (!fgets(buf, 20, fb))
275 ilog(LOG_TYPE_IRCD, "Error reading from pid file %s: %s",
276 filename, strerror(errno));
277 else
278 {
279 pid_t pid = atoi(buf);
280
281 if (!kill(pid, 0))
282 {
283 /* log(L_ERROR, "Server is already running"); */
284 printf("ircd: daemon is already running\n");
285 exit(EXIT_FAILURE);
286 }
287 }
288
289 fclose(fb);
290 }
291 else if (errno != ENOENT)
292 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
293 filename, strerror(errno));
294 }
295
296 /* setup_corefile()
297 *
298 * inputs - nothing
299 * output - nothing
300 * side effects - setups corefile to system limits.
301 * -kre
302 */
303 static void
304 setup_corefile(void)
305 {
306 #ifdef HAVE_SYS_RESOURCE_H
307 struct rlimit rlim; /* resource limits */
308
309 /* Set corefilesize to maximum */
310 if (!getrlimit(RLIMIT_CORE, &rlim))
311 {
312 rlim.rlim_cur = rlim.rlim_max;
313 setrlimit(RLIMIT_CORE, &rlim);
314 }
315 #endif
316 }
317
318 /*
319 * print_startup - print startup information
320 */
321 static void
322 print_startup(int pid)
323 {
324 printf("ircd: version %s(%s)\n", ircd_version, serno);
325 printf("ircd: pid %d\n", pid);
326 printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
327 : "foreground", ConfigGeneral.dpath);
328 }
329
330 static void
331 make_daemon(void)
332 {
333 int pid;
334
335 if ((pid = fork()) < 0)
336 {
337 perror("fork");
338 exit(EXIT_FAILURE);
339 }
340 else if (pid > 0)
341 {
342 print_startup(pid);
343 exit(EXIT_SUCCESS);
344 }
345
346 setsid();
347 }
348
349 int
350 main(int argc, char *argv[])
351 {
352 /* Check to see if the user is running us as root, which is a nono */
353 if (!geteuid())
354 {
355 fprintf(stderr, "ERROR: This server won't run as root/superuser\n");
356 return -1;
357 }
358
359 /* Setup corefile size immediately after boot -kre */
360 setup_corefile();
361
362 /* Save server boot time right away, so getrusage works correctly */
363 set_time();
364
365 /* It's not random, but it ought to be a little harder to guess */
366 init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
367
368 ConfigGeneral.dpath = DPATH;
369 ConfigGeneral.spath = SPATH;
370 ConfigGeneral.mpath = MPATH;
371 ConfigGeneral.configfile = CPATH; /* Server configuration file */
372 ConfigGeneral.klinefile = KPATH; /* Server kline file */
373 ConfigGeneral.xlinefile = XPATH; /* Server xline file */
374 ConfigGeneral.dlinefile = DLPATH; /* dline file */
375 ConfigGeneral.resvfile = RESVPATH; /* resv file */
376
377 myargv = argv;
378 umask(077); /* umask 077: u=rwx,g=,o= */
379
380 parseargs(&argc, &argv, myopts);
381
382 if (printVersion)
383 {
384 printf("ircd: version %s(%s)\n", ircd_version, serno);
385 exit(EXIT_SUCCESS);
386 }
387
388 if (chdir(ConfigGeneral.dpath))
389 {
390 perror("chdir");
391 exit(EXIT_FAILURE);
392 }
393
394 if (!server_state.foreground)
395 {
396 make_daemon();
397 close_standard_fds(); /* this needs to be before netio_init()! */
398 }
399 else
400 print_startup(getpid());
401
402 setup_signals();
403
404 /* We need this to initialise the fd array before anything else */
405 fdlist_init();
406 log_set_file(LOG_TYPE_IRCD, 0, logFileName);
407
408 netio_init(); /* This needs to be setup early ! -- adrian */
409 tls_init();
410
411 /* Check if there is pidfile and daemon already running */
412 check_pidfile(pidFileName);
413
414 mp_pool_init();
415 init_dlink_nodes();
416 isupport_init();
417 dbuf_init();
418 hash_init();
419 userhost_init();
420 ipcache_init();
421 client_init();
422 class_init();
423 whowas_init();
424 watch_init();
425 auth_init(); /* Initialise the auth code */
426 resolver_init(); /* Needs to be setup before the io loop */
427 modules_init();
428 read_conf_files(1); /* cold start init conf files */
429 server_capab_init(); /* Set up default_server_capabs */
430 initialize_global_set_options(); /* Has to be called after read_conf_files() */
431 channel_init();
432 read_links_file();
433 motd_init();
434 user_modes_init();
435
436 if (EmptyString(ConfigServerInfo.name))
437 {
438 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
439 exit(EXIT_FAILURE);
440 }
441
442 strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name));
443
444 /* serverinfo {} description must exist. If not, error out.*/
445 if (EmptyString(ConfigServerInfo.description))
446 {
447 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
448 exit(EXIT_FAILURE);
449 }
450
451 strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info));
452
453 if (EmptyString(ConfigServerInfo.sid))
454 {
455 ilog(LOG_TYPE_IRCD, "Generating server ID");
456 generate_sid();
457 }
458 else
459 strlcpy(me.id, ConfigServerInfo.sid, sizeof(me.id));
460
461 init_uid();
462
463 me.from = &me;
464 me.servptr = &me;
465 me.connection->lasttime = CurrentTime;
466 me.connection->since = CurrentTime;
467 me.connection->firsttime = CurrentTime;
468
469 SetMe(&me);
470 make_server(&me);
471
472 hash_add_id(&me);
473 hash_add_client(&me);
474
475 dlinkAdd(&me, make_dlink_node(), &global_server_list);
476 dlinkAdd(&me, &me.node, &global_client_list);
477
478 load_kline_database(ConfigGeneral.klinefile);
479 load_dline_database(ConfigGeneral.dlinefile);
480 load_xline_database(ConfigGeneral.xlinefile);
481 load_resv_database(ConfigGeneral.resvfile);
482
483 load_all_modules(1);
484 load_conf_modules();
485 load_core_modules(1);
486
487 write_pidfile(pidFileName);
488
489 event_addish(&event_cleanup_tklines, NULL);
490
491 /* We want try_connections to be called as soon as possible now! -- adrian */
492 /* No, 'cause after a restart it would cause all sorts of nick collides */
493 event_addish(&event_try_connections, NULL);
494
495 /* Setup the timeout check. I'll shift it later :) -- adrian */
496 event_add(&event_comm_checktimeouts, NULL);
497
498 event_addish(&event_save_all_databases, NULL);
499
500 if (ConfigServerHide.flatten_links_delay && event_write_links_file.active == 0)
501 {
502 event_write_links_file.when = ConfigServerHide.flatten_links_delay;
503 event_add(&event_write_links_file, NULL);
504 }
505
506 ilog(LOG_TYPE_IRCD, "Server Ready");
507 io_loop();
508
509 return 0;
510 }

Properties

Name Value
svn:eol-style native
svn:keywords Id