ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 7573
Committed: Tue May 24 16:34:18 2016 UTC (9 years, 3 months ago) by michael
Content type: text/x-csrc
File size: 12866 byte(s)
Log Message:
- Move initialize_server_capabs() from ircd.c to server.c and rename it to server_capab_init()

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 7006 * Copyright (c) 1997-2016 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     #include "client.h"
33     #include "event.h"
34     #include "fdlist.h"
35     #include "hash.h"
36 michael 6161 #include "id.h"
37 adx 30 #include "irc_string.h"
38     #include "ircd_signal.h"
39     #include "motd.h"
40 michael 1632 #include "conf.h"
41 adx 30 #include "hostmask.h"
42     #include "parse.h"
43 michael 3322 #include "res.h"
44 adx 30 #include "restart.h"
45 michael 982 #include "rng_mt.h"
46 michael 3324 #include "auth.h"
47 adx 30 #include "s_bsd.h"
48 michael 1309 #include "log.h"
49 michael 6481 #include "server.h"
50 adx 30 #include "send.h"
51     #include "whowas.h"
52     #include "modules.h"
53     #include "memory.h"
54 michael 1654 #include "mempool.h"
55 adx 30 #include "ircd_getopt.h"
56 michael 876 #include "watch.h"
57 michael 1622 #include "conf_db.h"
58 michael 1632 #include "conf_class.h"
59 michael 4325 #include "ipcache.h"
60 michael 6185 #include "isupport.h"
61 michael 6393 #include "userhost.h"
62 adx 30
63 michael 1858
64     #ifdef HAVE_LIBGEOIP
65 michael 7258 GeoIP *GeoIPv4_ctx;
66     GeoIP *GeoIPv6_ctx;
67 michael 1858 #endif
68 michael 2872
69 michael 5737 struct SetOptions GlobalSetOptions; /* /quote set variables */
70 michael 5602 struct Counter Count;
71     struct ServerState_t server_state;
72     struct ServerStatistics ServerStats;
73 michael 7330 struct ServerTime SystemTime;
74 michael 5737 struct Connection meConnection; /* That's also part of me */
75 michael 5470 struct Client me = { .connection = &meConnection }; /* That's me */
76 adx 30
77 michael 5460 char **myargv;
78 adx 30 const char *logFileName = LPATH;
79     const char *pidFileName = PPATH;
80    
81 michael 5460 unsigned int dorehash;
82     unsigned int doremotd;
83 adx 30
84 michael 6735 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 michael 4094 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 adx 30 void
146     set_time(void)
147     {
148 michael 2978 struct timeval newtime = { .tv_sec = 0, .tv_usec = 0 };
149 adx 30
150     if (gettimeofday(&newtime, NULL) == -1)
151     {
152 michael 6482 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 adx 30 }
158    
159 michael 7330 if ((uintmax_t)newtime.tv_sec < CurrentTime)
160 adx 30 {
161 michael 6782 ilog(LOG_TYPE_IRCD, "System clock is running backwards - (%ju < %ju)",
162 michael 7330 (uintmax_t)newtime.tv_sec, CurrentTime);
163 michael 2980 sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE,
164 michael 6782 "System clock is running backwards - (%ju < %ju)",
165 michael 7330 (uintmax_t)newtime.tv_sec, CurrentTime);
166     event_set_back_events(CurrentTime - (uintmax_t)newtime.tv_sec);
167 adx 30 }
168    
169 michael 6481 SystemTime.tv_sec = newtime.tv_sec;
170 adx 30 SystemTime.tv_usec = newtime.tv_usec;
171     }
172    
173     static void
174     io_loop(void)
175     {
176 michael 3215 while (1)
177 adx 30 {
178     if (listing_client_list.head)
179     {
180 michael 4815 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 adx 30 }
184    
185 michael 4094 /* Run pending events */
186     event_run();
187 adx 30
188     comm_select();
189     exit_aborted_clients();
190     free_exited_clients();
191    
192 michael 6735 /* Check to see whether we have to rehash the configuration. */
193 adx 30 if (dorehash)
194     {
195 michael 4982 conf_rehash(1);
196 adx 30 dorehash = 0;
197     }
198 michael 3215
199 adx 30 if (doremotd)
200     {
201 michael 2150 motd_recache();
202 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
203 michael 3065 "Got signal SIGUSR1, reloading motd file(s)");
204 adx 30 doremotd = 0;
205     }
206     }
207     }
208    
209     /* initalialize_global_set_options()
210     *
211     * inputs - none
212     * output - none
213 michael 2916 * side effects - This sets all global set options needed
214 adx 30 */
215     static void
216     initialize_global_set_options(void)
217     {
218 michael 5489 GlobalSetOptions.maxclients = ConfigServerInfo.default_max_clients;
219 michael 5499 GlobalSetOptions.autoconn = 1;
220 adx 30 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
221 michael 5499 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
222     GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount;
223 michael 5489 GlobalSetOptions.joinfloodcount = ConfigChannel.default_join_flood_count;
224     GlobalSetOptions.joinfloodtime = ConfigChannel.default_join_flood_time;
225 adx 30 GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
226     }
227    
228     /* write_pidfile()
229     *
230     * inputs - filename+path of pid file
231     * output - NONE
232     * side effects - write the pid of the ircd to filename
233     */
234     static void
235     write_pidfile(const char *filename)
236     {
237 michael 1325 FILE *fb;
238 adx 30
239 michael 1325 if ((fb = fopen(filename, "w")))
240 adx 30 {
241 michael 6470 char buf[IRCD_BUFSIZE];
242 adx 30 unsigned int pid = (unsigned int)getpid();
243    
244 michael 6470 snprintf(buf, sizeof(buf), "%u\n", pid);
245 michael 1325
246 michael 6470 if (fputs(buf, fb) == -1)
247 michael 5737 ilog(LOG_TYPE_IRCD, "Error writing to pid file %s: %s",
248     filename, strerror(errno));
249 adx 30
250 michael 1325 fclose(fb);
251 adx 30 }
252     else
253 michael 5566 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
254 michael 4748 filename, strerror(errno));
255 adx 30 }
256    
257     /* check_pidfile()
258     *
259     * inputs - filename+path of pid file
260     * output - none
261     * side effects - reads pid from pidfile and checks if ircd is in process
262     * list. if it is, gracefully exits
263     * -kre
264     */
265     static void
266     check_pidfile(const char *filename)
267     {
268 michael 1325 FILE *fb;
269 michael 6470 char buf[IRCD_BUFSIZE];
270 adx 30
271 michael 1325 if ((fb = fopen(filename, "r")))
272 adx 30 {
273 michael 6470 if (!fgets(buf, 20, fb))
274 michael 6260 ilog(LOG_TYPE_IRCD, "Error reading from pid file %s: %s",
275     filename, strerror(errno));
276 adx 30 else
277     {
278 michael 6481 pid_t pid = atoi(buf);
279 adx 30
280 michael 6481 if (!kill(pid, 0))
281 adx 30 {
282     /* log(L_ERROR, "Server is already running"); */
283     printf("ircd: daemon is already running\n");
284 michael 6646 exit(EXIT_FAILURE);
285 adx 30 }
286     }
287    
288 michael 1325 fclose(fb);
289 adx 30 }
290     else if (errno != ENOENT)
291 michael 6260 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
292     filename, strerror(errno));
293 adx 30 }
294    
295     /* setup_corefile()
296     *
297     * inputs - nothing
298     * output - nothing
299     * side effects - setups corefile to system limits.
300     * -kre
301     */
302     static void
303     setup_corefile(void)
304     {
305     #ifdef HAVE_SYS_RESOURCE_H
306     struct rlimit rlim; /* resource limits */
307    
308     /* Set corefilesize to maximum */
309     if (!getrlimit(RLIMIT_CORE, &rlim))
310     {
311     rlim.rlim_cur = rlim.rlim_max;
312     setrlimit(RLIMIT_CORE, &rlim);
313     }
314     #endif
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 2216 read_links_file();
432 michael 2150 motd_init();
433 michael 6189 user_modes_init();
434 adx 30
435 michael 4340 if (EmptyString(ConfigServerInfo.name))
436 michael 1115 {
437 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
438 michael 1115 exit(EXIT_FAILURE);
439     }
440    
441 michael 4340 strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name));
442 adx 30
443 michael 6481 /* serverinfo {} description must exist. If not, error out.*/
444 michael 4340 if (EmptyString(ConfigServerInfo.description))
445 adx 30 {
446 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
447 adx 30 exit(EXIT_FAILURE);
448     }
449 michael 885
450 michael 4340 strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info));
451 adx 30
452 michael 6156 if (EmptyString(ConfigServerInfo.sid))
453     {
454     ilog(LOG_TYPE_IRCD, "Generating server ID");
455     generate_sid();
456     }
457     else
458     strlcpy(me.id, ConfigServerInfo.sid, sizeof(me.id));
459    
460 michael 6464 init_uid();
461    
462 michael 5545 me.from = &me;
463     me.servptr = &me;
464     me.connection->lasttime = CurrentTime;
465     me.connection->since = CurrentTime;
466 michael 4588 me.connection->firsttime = CurrentTime;
467 adx 30
468     SetMe(&me);
469     make_server(&me);
470    
471 michael 1115 hash_add_id(&me);
472 adx 30 hash_add_client(&me);
473 michael 2916
474 michael 4209 dlinkAdd(&me, make_dlink_node(), &global_server_list);
475 michael 6464 dlinkAdd(&me, &me.node, &global_client_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 Revision