ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 8089
Committed: Wed Mar 29 12:22:47 2017 UTC (8 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 12888 byte(s)
Log Message:
- Cleanup channel mode table handling

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

Properties

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