ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 8166
Committed: Sun Apr 9 16:09:54 2017 UTC (9 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 12907 byte(s)
Log Message:
- Move all CAPAB related code to server_capab.c, and server_capab.h

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2017 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 "channel_mode.h"
33 #include "client.h"
34 #include "event.h"
35 #include "fdlist.h"
36 #include "hash.h"
37 #include "id.h"
38 #include "irc_string.h"
39 #include "ircd_signal.h"
40 #include "motd.h"
41 #include "conf.h"
42 #include "hostmask.h"
43 #include "parse.h"
44 #include "res.h"
45 #include "restart.h"
46 #include "rng_mt.h"
47 #include "auth.h"
48 #include "s_bsd.h"
49 #include "log.h"
50 #include "server.h"
51 #include "server_capab.h"
52 #include "send.h"
53 #include "whowas.h"
54 #include "modules.h"
55 #include "memory.h"
56 #include "mempool.h"
57 #include "ircd_getopt.h"
58 #include "watch.h"
59 #include "conf_db.h"
60 #include "conf_class.h"
61 #include "ipcache.h"
62 #include "isupport.h"
63 #include "userhost.h"
64
65
66 #ifdef HAVE_LIBGEOIP
67 GeoIP *GeoIPv4_ctx;
68 GeoIP *GeoIPv6_ctx;
69 #endif
70
71 struct SetOptions GlobalSetOptions; /* /quote set variables */
72 struct Counter Count;
73 struct ServerState_t server_state;
74 struct ServerStatistics ServerStats;
75 struct ServerTime SystemTime;
76 struct Connection meConnection; /* That's also part of me */
77 struct Client me = { .connection = &meConnection }; /* That's me */
78
79 char **myargv;
80 const char *logFileName = LPATH;
81 const char *pidFileName = PPATH;
82
83 unsigned int dorehash;
84 unsigned int doremotd;
85
86 static int printVersion;
87
88 static struct lgetopt myopts[] =
89 {
90 { "configfile", &ConfigGeneral.configfile,
91 STRING, "File to use for ircd.conf" },
92 { "klinefile", &ConfigGeneral.klinefile,
93 STRING, "File to use for kline database" },
94 { "dlinefile", &ConfigGeneral.dlinefile,
95 STRING, "File to use for dline database" },
96 { "xlinefile", &ConfigGeneral.xlinefile,
97 STRING, "File to use for xline database" },
98 { "resvfile", &ConfigGeneral.resvfile,
99 STRING, "File to use for resv database" },
100 { "logfile", &logFileName,
101 STRING, "File to use for ircd.log" },
102 { "pidfile", &pidFileName,
103 STRING, "File to use for process ID" },
104 { "foreground", &server_state.foreground,
105 YESNO, "Run in foreground (don't detach)" },
106 { "version", &printVersion,
107 YESNO, "Print version and exit" },
108 { "help", NULL, USAGE, "Print this text" },
109 { NULL, NULL, STRING, NULL },
110 };
111
112 static struct event event_cleanup_tklines =
113 {
114 .name = "cleanup_tklines",
115 .handler = cleanup_tklines,
116 .when = CLEANUP_TKLINES_TIME
117 };
118
119 static struct event event_try_connections =
120 {
121 .name = "try_connections",
122 .handler = try_connections,
123 .when = STARTUP_CONNECTIONS_TIME
124 };
125
126 static struct event event_comm_checktimeouts =
127 {
128 .name = "comm_checktimeouts",
129 .handler = comm_checktimeouts,
130 .when = 1
131 };
132
133 static struct event event_save_all_databases =
134 {
135 .name = "save_all_databases",
136 .handler = save_all_databases,
137 .when = DATABASE_UPDATE_TIMEOUT
138 };
139
140 struct event event_write_links_file =
141 {
142 .name = "write_links_file",
143 .handler = write_links_file,
144 };
145
146
147 void
148 set_time(void)
149 {
150 struct timeval newtime = { .tv_sec = 0, .tv_usec = 0 };
151
152 if (gettimeofday(&newtime, NULL) == -1)
153 {
154 char buf[IRCD_BUFSIZE];
155
156 snprintf(buf, sizeof(buf), "Clock failure, TS can be corrupted: %s",
157 strerror(errno));
158 server_die(buf, SERVER_SHUTDOWN);
159 }
160
161 if ((uintmax_t)newtime.tv_sec < CurrentTime)
162 {
163 ilog(LOG_TYPE_IRCD, "System clock is running backwards - (%ju < %ju)",
164 (uintmax_t)newtime.tv_sec, CurrentTime);
165 sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE,
166 "System clock is running backwards - (%ju < %ju)",
167 (uintmax_t)newtime.tv_sec, CurrentTime);
168 event_set_back_events(CurrentTime - (uintmax_t)newtime.tv_sec);
169 }
170
171 SystemTime.tv_sec = newtime.tv_sec;
172 SystemTime.tv_usec = newtime.tv_usec;
173 }
174
175 static void
176 io_loop(void)
177 {
178 while (1)
179 {
180 if (listing_client_list.head)
181 {
182 dlink_node *node = NULL, *node_next = NULL;
183 DLINK_FOREACH_SAFE(node, node_next, listing_client_list.head)
184 safe_list_channels(node->data, 0);
185 }
186
187 /* Run pending events */
188 event_run();
189
190 comm_select();
191 exit_aborted_clients();
192 free_exited_clients();
193
194 /* Check to see whether we have to rehash the configuration. */
195 if (dorehash)
196 {
197 conf_rehash(1);
198 dorehash = 0;
199 }
200
201 if (doremotd)
202 {
203 motd_recache();
204 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
205 "Got signal SIGUSR1, reloading motd file(s)");
206 doremotd = 0;
207 }
208 }
209 }
210
211 /* initalialize_global_set_options()
212 *
213 * inputs - none
214 * output - none
215 * side effects - This sets all global set options needed
216 */
217 static void
218 initialize_global_set_options(void)
219 {
220 GlobalSetOptions.maxclients = ConfigServerInfo.default_max_clients;
221 GlobalSetOptions.autoconn = 1;
222 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
223 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
224 GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount;
225 GlobalSetOptions.floodtime = ConfigGeneral.default_floodtime;
226 GlobalSetOptions.joinfloodcount = ConfigChannel.default_join_flood_count;
227 GlobalSetOptions.joinfloodtime = ConfigChannel.default_join_flood_time;
228 GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
229 }
230
231 /* write_pidfile()
232 *
233 * inputs - filename+path of pid file
234 * output - NONE
235 * side effects - write the pid of the ircd to filename
236 */
237 static void
238 write_pidfile(const char *filename)
239 {
240 FILE *fb;
241
242 if ((fb = fopen(filename, "w")))
243 {
244 char buf[IRCD_BUFSIZE];
245 unsigned int pid = (unsigned int)getpid();
246
247 snprintf(buf, sizeof(buf), "%u\n", pid);
248
249 if (fputs(buf, fb) == -1)
250 ilog(LOG_TYPE_IRCD, "Error writing to pid file %s: %s",
251 filename, strerror(errno));
252
253 fclose(fb);
254 }
255 else
256 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
257 filename, strerror(errno));
258 }
259
260 /* check_pidfile()
261 *
262 * inputs - filename+path of pid file
263 * output - none
264 * side effects - reads pid from pidfile and checks if ircd is in process
265 * list. if it is, gracefully exits
266 * -kre
267 */
268 static void
269 check_pidfile(const char *filename)
270 {
271 FILE *fb;
272 char buf[IRCD_BUFSIZE];
273
274 if ((fb = fopen(filename, "r")))
275 {
276 if (!fgets(buf, 20, fb))
277 ilog(LOG_TYPE_IRCD, "Error reading from pid file %s: %s",
278 filename, strerror(errno));
279 else
280 {
281 pid_t pid = atoi(buf);
282
283 if (!kill(pid, 0))
284 {
285 /* log(L_ERROR, "Server is already running"); */
286 printf("ircd: daemon is already running\n");
287 exit(EXIT_FAILURE);
288 }
289 }
290
291 fclose(fb);
292 }
293 else if (errno != ENOENT)
294 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
295 filename, strerror(errno));
296 }
297
298 /* setup_corefile()
299 *
300 * inputs - nothing
301 * output - nothing
302 * side effects - setups corefile to system limits.
303 * -kre
304 */
305 static void
306 setup_corefile(void)
307 {
308 struct rlimit rlim; /* resource limits */
309
310 /* Set corefilesize to maximum */
311 if (!getrlimit(RLIMIT_CORE, &rlim))
312 {
313 rlim.rlim_cur = rlim.rlim_max;
314 setrlimit(RLIMIT_CORE, &rlim);
315 }
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 capab_init(); /* Set up default_server_capabs */
430 initialize_global_set_options(); /* Has to be called after read_conf_files() */
431 channel_init();
432 channel_mode_init();
433 read_links_file();
434 motd_init();
435 user_modes_init();
436
437 if (EmptyString(ConfigServerInfo.name))
438 {
439 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
440 exit(EXIT_FAILURE);
441 }
442
443 strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name));
444
445 /* serverinfo {} description must exist. If not, error out.*/
446 if (EmptyString(ConfigServerInfo.description))
447 {
448 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
449 exit(EXIT_FAILURE);
450 }
451
452 strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info));
453
454 if (EmptyString(ConfigServerInfo.sid))
455 {
456 ilog(LOG_TYPE_IRCD, "Generating server ID");
457 generate_sid();
458 }
459 else
460 strlcpy(me.id, ConfigServerInfo.sid, sizeof(me.id));
461
462 init_uid();
463
464 me.from = &me;
465 me.servptr = &me;
466 me.connection->lasttime = CurrentTime;
467 me.connection->since = CurrentTime;
468 me.connection->firsttime = CurrentTime;
469
470 SetMe(&me);
471 make_server(&me);
472
473 hash_add_id(&me);
474 hash_add_client(&me);
475
476 dlinkAdd(&me, &me.node, &global_server_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