/[svn]/ircd-hybrid/trunk/src/ircd.c
ViewVC logotype

Contents of /ircd-hybrid/trunk/src/ircd.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8862 - (show annotations)
Fri Feb 15 21:35:54 2019 UTC (3 years, 5 months ago) by michael
File MIME type: text/x-chdr
File size: 13414 byte(s)
- getopt: rename YESNO to BOOLEAN

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

Properties

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

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28