ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 8279
Committed: Tue Feb 20 19:30:13 2018 UTC (7 years, 6 months ago) by michael
Content type: text/x-csrc
File size: 12907 byte(s)
Log Message:
- Update copyright years

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 8279 * Copyright (c) 1997-2018 ircd-hybrid development team
5 adx 30 *
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 michael 4565 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 adx 30 * USA
20     */
21    
22 michael 2916 /*! \file ircd.c
23     * \brief Starts up and runs the ircd.
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28 michael 3347 #include "user.h"
29 michael 1011 #include "list.h"
30 adx 30 #include "ircd.h"
31     #include "channel.h"
32 michael 8089 #include "channel_mode.h"
33 adx 30 #include "client.h"
34     #include "event.h"
35     #include "fdlist.h"
36     #include "hash.h"
37 michael 6161 #include "id.h"
38 adx 30 #include "irc_string.h"
39     #include "ircd_signal.h"
40     #include "motd.h"
41 michael 1632 #include "conf.h"
42 adx 30 #include "hostmask.h"
43     #include "parse.h"
44 michael 3322 #include "res.h"
45 adx 30 #include "restart.h"
46 michael 982 #include "rng_mt.h"
47 michael 3324 #include "auth.h"
48 adx 30 #include "s_bsd.h"
49 michael 1309 #include "log.h"
50 michael 6481 #include "server.h"
51 michael 8166 #include "server_capab.h"
52 adx 30 #include "send.h"
53     #include "whowas.h"
54     #include "modules.h"
55     #include "memory.h"
56 michael 1654 #include "mempool.h"
57 adx 30 #include "ircd_getopt.h"
58 michael 876 #include "watch.h"
59 michael 1622 #include "conf_db.h"
60 michael 1632 #include "conf_class.h"
61 michael 4325 #include "ipcache.h"
62 michael 6185 #include "isupport.h"
63 michael 6393 #include "userhost.h"
64 adx 30
65 michael 1858
66     #ifdef HAVE_LIBGEOIP
67 michael 7258 GeoIP *GeoIPv4_ctx;
68     GeoIP *GeoIPv6_ctx;
69 michael 1858 #endif
70 michael 2872
71 michael 5737 struct SetOptions GlobalSetOptions; /* /quote set variables */
72 michael 5602 struct Counter Count;
73     struct ServerState_t server_state;
74     struct ServerStatistics ServerStats;
75 michael 7330 struct ServerTime SystemTime;
76 michael 5737 struct Connection meConnection; /* That's also part of me */
77 michael 5470 struct Client me = { .connection = &meConnection }; /* That's me */
78 adx 30
79 michael 5460 char **myargv;
80 adx 30 const char *logFileName = LPATH;
81     const char *pidFileName = PPATH;
82    
83 michael 5460 unsigned int dorehash;
84     unsigned int doremotd;
85 adx 30
86 michael 6735 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 michael 4094 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 adx 30 void
148     set_time(void)
149     {
150 michael 2978 struct timeval newtime = { .tv_sec = 0, .tv_usec = 0 };
151 adx 30
152     if (gettimeofday(&newtime, NULL) == -1)
153     {
154 michael 6482 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 adx 30 }
160    
161 michael 7330 if ((uintmax_t)newtime.tv_sec < CurrentTime)
162 adx 30 {
163 michael 6782 ilog(LOG_TYPE_IRCD, "System clock is running backwards - (%ju < %ju)",
164 michael 7330 (uintmax_t)newtime.tv_sec, CurrentTime);
165 michael 2980 sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE,
166 michael 6782 "System clock is running backwards - (%ju < %ju)",
167 michael 7330 (uintmax_t)newtime.tv_sec, CurrentTime);
168     event_set_back_events(CurrentTime - (uintmax_t)newtime.tv_sec);
169 adx 30 }
170    
171 michael 6481 SystemTime.tv_sec = newtime.tv_sec;
172 adx 30 SystemTime.tv_usec = newtime.tv_usec;
173     }
174    
175     static void
176     io_loop(void)
177     {
178 michael 3215 while (1)
179 adx 30 {
180     if (listing_client_list.head)
181     {
182 michael 4815 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 adx 30 }
186    
187 michael 4094 /* Run pending events */
188     event_run();
189 adx 30
190     comm_select();
191     exit_aborted_clients();
192     free_exited_clients();
193    
194 michael 6735 /* Check to see whether we have to rehash the configuration. */
195 adx 30 if (dorehash)
196     {
197 michael 4982 conf_rehash(1);
198 adx 30 dorehash = 0;
199     }
200 michael 3215
201 adx 30 if (doremotd)
202     {
203 michael 2150 motd_recache();
204 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
205 michael 3065 "Got signal SIGUSR1, reloading motd file(s)");
206 adx 30 doremotd = 0;
207     }
208     }
209     }
210    
211     /* initalialize_global_set_options()
212     *
213     * inputs - none
214     * output - none
215 michael 2916 * side effects - This sets all global set options needed
216 adx 30 */
217     static void
218     initialize_global_set_options(void)
219     {
220 michael 5489 GlobalSetOptions.maxclients = ConfigServerInfo.default_max_clients;
221 michael 5499 GlobalSetOptions.autoconn = 1;
222 adx 30 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
223 michael 5499 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
224     GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount;
225 michael 7858 GlobalSetOptions.floodtime = ConfigGeneral.default_floodtime;
226 michael 5489 GlobalSetOptions.joinfloodcount = ConfigChannel.default_join_flood_count;
227     GlobalSetOptions.joinfloodtime = ConfigChannel.default_join_flood_time;
228 adx 30 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 michael 1325 FILE *fb;
241 adx 30
242 michael 1325 if ((fb = fopen(filename, "w")))
243 adx 30 {
244 michael 6470 char buf[IRCD_BUFSIZE];
245 adx 30 unsigned int pid = (unsigned int)getpid();
246    
247 michael 6470 snprintf(buf, sizeof(buf), "%u\n", pid);
248 michael 1325
249 michael 6470 if (fputs(buf, fb) == -1)
250 michael 5737 ilog(LOG_TYPE_IRCD, "Error writing to pid file %s: %s",
251     filename, strerror(errno));
252 adx 30
253 michael 1325 fclose(fb);
254 adx 30 }
255     else
256 michael 5566 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
257 michael 4748 filename, strerror(errno));
258 adx 30 }
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 michael 1325 FILE *fb;
272 michael 6470 char buf[IRCD_BUFSIZE];
273 adx 30
274 michael 1325 if ((fb = fopen(filename, "r")))
275 adx 30 {
276 michael 6470 if (!fgets(buf, 20, fb))
277 michael 6260 ilog(LOG_TYPE_IRCD, "Error reading from pid file %s: %s",
278     filename, strerror(errno));
279 adx 30 else
280     {
281 michael 6481 pid_t pid = atoi(buf);
282 adx 30
283 michael 6481 if (!kill(pid, 0))
284 adx 30 {
285     /* log(L_ERROR, "Server is already running"); */
286     printf("ircd: daemon is already running\n");
287 michael 6646 exit(EXIT_FAILURE);
288 adx 30 }
289     }
290    
291 michael 1325 fclose(fb);
292 adx 30 }
293     else if (errno != ENOENT)
294 michael 6260 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
295     filename, strerror(errno));
296 adx 30 }
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 michael 6735 /*
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 adx 30 int
350     main(int argc, char *argv[])
351     {
352 michael 2253 /* Check to see if the user is running us as root, which is a nono */
353 michael 4733 if (!geteuid())
354 adx 30 {
355 michael 3525 fprintf(stderr, "ERROR: This server won't run as root/superuser\n");
356 michael 982 return -1;
357 adx 30 }
358    
359     /* Setup corefile size immediately after boot -kre */
360     setup_corefile();
361    
362 michael 5545 /* Save server boot time right away, so getrusage works correctly */
363 adx 30 set_time();
364    
365 michael 5545 /* It's not random, but it ought to be a little harder to guess */
366 michael 982 init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
367    
368 michael 4340 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 michael 1702
377 adx 30 myargv = argv;
378 michael 5723 umask(077); /* umask 077: u=rwx,g=,o= */
379 adx 30
380     parseargs(&argc, &argv, myopts);
381    
382     if (printVersion)
383     {
384 michael 2646 printf("ircd: version %s(%s)\n", ircd_version, serno);
385 adx 30 exit(EXIT_SUCCESS);
386     }
387    
388 michael 4340 if (chdir(ConfigGeneral.dpath))
389 adx 30 {
390     perror("chdir");
391     exit(EXIT_FAILURE);
392     }
393    
394     if (!server_state.foreground)
395     {
396     make_daemon();
397 michael 7566 close_standard_fds(); /* this needs to be before netio_init()! */
398 adx 30 }
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 michael 1831 log_set_file(LOG_TYPE_IRCD, 0, logFileName);
407 michael 4415
408 michael 7566 netio_init(); /* This needs to be setup early ! -- adrian */
409 michael 7279 tls_init();
410 michael 2253
411 adx 30 /* Check if there is pidfile and daemon already running */
412     check_pidfile(pidFileName);
413    
414 michael 1654 mp_pool_init();
415 adx 30 init_dlink_nodes();
416 michael 6185 isupport_init();
417 adx 30 dbuf_init();
418 michael 1798 hash_init();
419 michael 6393 userhost_init();
420 michael 4319 ipcache_init();
421 michael 1798 client_init();
422 michael 1632 class_init();
423 michael 7438 whowas_init();
424 michael 876 watch_init();
425 michael 1798 auth_init(); /* Initialise the auth code */
426 michael 7569 resolver_init(); /* Needs to be setup before the io loop */
427 michael 1404 modules_init();
428 adx 30 read_conf_files(1); /* cold start init conf files */
429 michael 8166 capab_init(); /* Set up default_server_capabs */
430 michael 5489 initialize_global_set_options(); /* Has to be called after read_conf_files() */
431 michael 1798 channel_init();
432 michael 8089 channel_mode_init();
433 michael 2216 read_links_file();
434 michael 2150 motd_init();
435 michael 6189 user_modes_init();
436 adx 30
437 michael 4340 if (EmptyString(ConfigServerInfo.name))
438 michael 1115 {
439 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
440 michael 1115 exit(EXIT_FAILURE);
441     }
442    
443 michael 4340 strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name));
444 adx 30
445 michael 6481 /* serverinfo {} description must exist. If not, error out.*/
446 michael 4340 if (EmptyString(ConfigServerInfo.description))
447 adx 30 {
448 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
449 adx 30 exit(EXIT_FAILURE);
450     }
451 michael 885
452 michael 4340 strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info));
453 adx 30
454 michael 6156 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 michael 6464 init_uid();
463    
464 michael 5545 me.from = &me;
465     me.servptr = &me;
466     me.connection->lasttime = CurrentTime;
467     me.connection->since = CurrentTime;
468 michael 4588 me.connection->firsttime = CurrentTime;
469 adx 30
470     SetMe(&me);
471     make_server(&me);
472    
473 michael 1115 hash_add_id(&me);
474 adx 30 hash_add_client(&me);
475 michael 2916
476 michael 7963 dlinkAdd(&me, &me.node, &global_server_list);
477 adx 30
478 michael 6928 load_kline_database(ConfigGeneral.klinefile);
479     load_dline_database(ConfigGeneral.dlinefile);
480     load_xline_database(ConfigGeneral.xlinefile);
481     load_resv_database(ConfigGeneral.resvfile);
482 michael 1622
483 adx 30 load_all_modules(1);
484     load_conf_modules();
485     load_core_modules(1);
486 michael 1115
487 adx 30 write_pidfile(pidFileName);
488    
489 michael 4094 event_addish(&event_cleanup_tklines, NULL);
490 adx 30
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 michael 4094 event_addish(&event_try_connections, NULL);
494 adx 30
495     /* Setup the timeout check. I'll shift it later :) -- adrian */
496 michael 4399 event_add(&event_comm_checktimeouts, NULL);
497 adx 30
498 michael 4094 event_addish(&event_save_all_databases, NULL);
499 michael 1625
500 michael 6642 if (ConfigServerHide.flatten_links_delay && event_write_links_file.active == 0)
501 michael 4094 {
502 michael 6597 event_write_links_file.when = ConfigServerHide.flatten_links_delay;
503 michael 6636 event_add(&event_write_links_file, NULL);
504 michael 4094 }
505 adx 30
506 michael 6464 ilog(LOG_TYPE_IRCD, "Server Ready");
507 adx 30 io_loop();
508 michael 6464
509 michael 885 return 0;
510 adx 30 }

Properties

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