ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/ircd.c
Revision: 2216
Committed: Thu Jun 6 17:35:45 2013 UTC (10 years, 9 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid/trunk/src/ircd.c
File size: 17082 byte(s)
Log Message:
- Added read_links_file()

File Contents

# User Rev Content
1 adx 30 /*
2     * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3     * ircd.c: Starts up and runs the ircd.
4     *
5     * Copyright (C) 2002 by the past and present ircd coders, and others.
6     *
7     * This program is free software; you can redistribute it and/or modify
8     * it under the terms of the GNU General Public License as published by
9     * the Free Software Foundation; either version 2 of the License, or
10     * (at your option) any later version.
11     *
12     * This program is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     * GNU General Public License for more details.
16     *
17     * You should have received a copy of the GNU General Public License
18     * along with this program; if not, write to the Free Software
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20     * USA
21     *
22 knight 31 * $Id$
23 adx 30 */
24    
25     #include "stdinc.h"
26     #include "s_user.h"
27 michael 1011 #include "list.h"
28 adx 30 #include "ircd.h"
29     #include "channel.h"
30     #include "channel_mode.h"
31     #include "client.h"
32     #include "event.h"
33     #include "fdlist.h"
34     #include "hash.h"
35     #include "irc_string.h"
36     #include "ircd_signal.h"
37     #include "s_gline.h"
38     #include "motd.h"
39 michael 1632 #include "conf.h"
40 adx 30 #include "hostmask.h"
41     #include "numeric.h"
42     #include "packet.h"
43     #include "parse.h"
44     #include "irc_res.h"
45     #include "restart.h"
46 michael 982 #include "rng_mt.h"
47 adx 30 #include "s_auth.h"
48     #include "s_bsd.h"
49 michael 1309 #include "log.h"
50 adx 30 #include "s_misc.h"
51     #include "s_serv.h" /* try_connections */
52     #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 "hook.h"
58     #include "ircd_getopt.h"
59     #include "supported.h"
60 michael 876 #include "watch.h"
61 michael 1622 #include "conf_db.h"
62 michael 1632 #include "conf_class.h"
63 adx 30
64 michael 1858
65     #ifdef HAVE_LIBGEOIP
66     GeoIP *geoip_ctx;
67     #endif
68 adx 30 /* /quote set variables */
69     struct SetOptions GlobalSetOptions;
70    
71     /* configuration set from ircd.conf */
72     struct config_file_entry ConfigFileEntry;
73     /* server info set from ircd.conf */
74     struct server_info ServerInfo;
75     /* admin info set from ircd.conf */
76     struct admin_info AdminInfo = { NULL, NULL, NULL };
77 michael 1145 struct Counter Count = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
78 adx 30 struct ServerState_t server_state = { 0 };
79 michael 1324 struct logging_entry ConfigLoggingEntry = { .use_logging = 1 };
80 michael 896 struct ServerStatistics ServerStats;
81 adx 30 struct timeval SystemTime;
82     struct Client me; /* That's me */
83     struct LocalUser meLocalUser; /* That's also part of me */
84    
85     const char *logFileName = LPATH;
86     const char *pidFileName = PPATH;
87    
88     char **myargv;
89    
90     int dorehash = 0;
91     int doremotd = 0;
92    
93     /* Set to zero because it should be initialized later using
94     * initialize_server_capabs
95     */
96     int default_server_capabs = 0;
97 michael 1013 unsigned int splitmode;
98     unsigned int splitchecking;
99     unsigned int split_users;
100 adx 30 unsigned int split_servers;
101    
102     /* Do klines the same way hybrid-6 did them, i.e. at the
103     * top of the next io_loop instead of in the same loop as
104     * the klines are being applied.
105     *
106     * This should fix strange CPU starvation as very indirectly reported.
107     * (Why do you people not email bug reports? WHY? WHY?)
108     *
109     * - Dianora
110     */
111    
112     int rehashed_klines = 0;
113    
114    
115     /*
116     * print_startup - print startup information
117     */
118     static void
119     print_startup(int pid)
120     {
121     printf("ircd: version %s\n", ircd_version);
122     printf("ircd: pid %d\n", pid);
123     printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
124     : "foreground", ConfigFileEntry.dpath);
125     }
126    
127     static void
128     make_daemon(void)
129     {
130     int pid;
131    
132     if ((pid = fork()) < 0)
133     {
134     perror("fork");
135     exit(EXIT_FAILURE);
136     }
137     else if (pid > 0)
138     {
139     print_startup(pid);
140     exit(EXIT_SUCCESS);
141     }
142    
143     setsid();
144     }
145    
146     static int printVersion = 0;
147    
148 michael 1357 static struct lgetopt myopts[] = {
149 adx 30 {"configfile", &ConfigFileEntry.configfile,
150     STRING, "File to use for ircd.conf"},
151 michael 1718 {"glinefile", &ConfigFileEntry.glinefile,
152     STRING, "File to use for gline database"},
153 adx 30 {"klinefile", &ConfigFileEntry.klinefile,
154 michael 1718 STRING, "File to use for kline database"},
155     {"dlinefile", &ConfigFileEntry.dlinefile,
156     STRING, "File to use for dline database"},
157 adx 30 {"xlinefile", &ConfigFileEntry.xlinefile,
158 michael 1718 STRING, "File to use for xline database"},
159     {"resvfile", &ConfigFileEntry.resvfile,
160     STRING, "File to use for resv database"},
161 adx 30 {"logfile", &logFileName,
162     STRING, "File to use for ircd.log"},
163     {"pidfile", &pidFileName,
164     STRING, "File to use for process ID"},
165     {"foreground", &server_state.foreground,
166     YESNO, "Run in foreground (don't detach)"},
167     {"version", &printVersion,
168     YESNO, "Print version and exit"},
169     {"help", NULL, USAGE, "Print this text"},
170     {NULL, NULL, STRING, NULL},
171     };
172    
173     void
174     set_time(void)
175     {
176     static char to_send[200];
177     struct timeval newtime;
178     newtime.tv_sec = 0;
179     newtime.tv_usec = 0;
180    
181     if (gettimeofday(&newtime, NULL) == -1)
182     {
183 michael 1247 ilog(LOG_TYPE_IRCD, "Clock Failure (%s), TS can be corrupted",
184 adx 30 strerror(errno));
185 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
186 adx 30 "Clock Failure (%s), TS can be corrupted",
187     strerror(errno));
188     restart("Clock Failure");
189     }
190    
191     if (newtime.tv_sec < CurrentTime)
192     {
193 michael 1124 snprintf(to_send, sizeof(to_send),
194     "System clock is running backwards - (%lu < %lu)",
195     (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
196 adx 30 report_error(L_ALL, to_send, me.name, 0);
197     set_back_events(CurrentTime - newtime.tv_sec);
198     }
199    
200     SystemTime.tv_sec = newtime.tv_sec;
201     SystemTime.tv_usec = newtime.tv_usec;
202     }
203    
204     static void
205     io_loop(void)
206     {
207     while (1 == 1)
208     {
209     /*
210     * Maybe we want a flags word?
211     * ie. if (REHASHED_KLINES(global_flags))
212     * SET_REHASHED_KLINES(global_flags)
213     * CLEAR_REHASHED_KLINES(global_flags)
214     *
215     * - Dianora
216     */
217     if (rehashed_klines)
218     {
219     check_conf_klines();
220     rehashed_klines = 0;
221     }
222    
223     if (listing_client_list.head)
224     {
225     dlink_node *ptr = NULL, *ptr_next = NULL;
226     DLINK_FOREACH_SAFE(ptr, ptr_next, listing_client_list.head)
227     {
228     struct Client *client_p = ptr->data;
229     assert(client_p->localClient->list_task);
230 michael 896 safe_list_channels(client_p, client_p->localClient->list_task, 0);
231 adx 30 }
232     }
233    
234     /* Run pending events, then get the number of seconds to the next
235     * event
236     */
237     while (eventNextTime() <= CurrentTime)
238     eventRun();
239    
240     comm_select();
241     exit_aborted_clients();
242     free_exited_clients();
243     send_queued_all();
244    
245     /* Check to see whether we have to rehash the configuration .. */
246     if (dorehash)
247     {
248     rehash(1);
249     dorehash = 0;
250     }
251     if (doremotd)
252     {
253 michael 2150 motd_recache();
254 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
255 michael 2150 "Got signal SIGUSR1, reloading motd files");
256 adx 30 doremotd = 0;
257     }
258     }
259     }
260    
261     /* initalialize_global_set_options()
262     *
263     * inputs - none
264     * output - none
265     * side effects - This sets all global set options needed
266     */
267     static void
268     initialize_global_set_options(void)
269     {
270     memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
271    
272     GlobalSetOptions.autoconn = 1;
273     GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
274     GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
275    
276     if (ConfigFileEntry.default_floodcount)
277     GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount;
278     else
279     GlobalSetOptions.floodcount = 10;
280    
281     /* XXX I have no idea what to try here - Dianora */
282     GlobalSetOptions.joinfloodcount = 16;
283     GlobalSetOptions.joinfloodtime = 8;
284    
285     split_servers = ConfigChannel.default_split_server_count;
286     split_users = ConfigChannel.default_split_user_count;
287    
288     if (split_users && split_servers && (ConfigChannel.no_create_on_split ||
289     ConfigChannel.no_join_on_split))
290     {
291     splitmode = 1;
292     splitchecking = 1;
293     }
294    
295     GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
296     /* End of global set options */
297     }
298    
299     /* initialize_server_capabs()
300     *
301     * inputs - none
302     * output - none
303     */
304     static void
305     initialize_server_capabs(void)
306     {
307     add_capability("QS", CAP_QS, 1);
308     add_capability("EOB", CAP_EOB, 1);
309 michael 1117 add_capability("TS6", CAP_TS6, 0);
310 adx 30 add_capability("CLUSTER", CAP_CLUSTER, 1);
311 michael 1196 add_capability("SVS", CAP_SVS, 1);
312 adx 30 #ifdef HALFOPS
313     add_capability("HOPS", CAP_HOPS, 1);
314     #endif
315     }
316    
317     /* write_pidfile()
318     *
319     * inputs - filename+path of pid file
320     * output - NONE
321     * side effects - write the pid of the ircd to filename
322     */
323     static void
324     write_pidfile(const char *filename)
325     {
326 michael 1325 FILE *fb;
327 adx 30
328 michael 1325 if ((fb = fopen(filename, "w")))
329 adx 30 {
330     char buff[32];
331     unsigned int pid = (unsigned int)getpid();
332    
333 michael 1325 snprintf(buff, sizeof(buff), "%u\n", pid);
334    
335     if ((fputs(buff, fb) == -1))
336 michael 1247 ilog(LOG_TYPE_IRCD, "Error writing %u to pid file %s (%s)",
337 adx 30 pid, filename, strerror(errno));
338    
339 michael 1325 fclose(fb);
340 adx 30 }
341     else
342     {
343 michael 1247 ilog(LOG_TYPE_IRCD, "Error opening pid file %s", filename);
344 adx 30 }
345     }
346    
347     /* check_pidfile()
348     *
349     * inputs - filename+path of pid file
350     * output - none
351     * side effects - reads pid from pidfile and checks if ircd is in process
352     * list. if it is, gracefully exits
353     * -kre
354     */
355     static void
356     check_pidfile(const char *filename)
357     {
358 michael 1325 FILE *fb;
359 adx 30 char buff[32];
360     pid_t pidfromfile;
361    
362     /* Don't do logging here, since we don't have log() initialised */
363 michael 1325 if ((fb = fopen(filename, "r")))
364 adx 30 {
365 michael 1325 if (fgets(buff, 20, fb) == NULL)
366 adx 30 {
367     /* log(L_ERROR, "Error reading from pid file %s (%s)", filename,
368     * strerror(errno));
369     */
370     }
371     else
372     {
373     pidfromfile = atoi(buff);
374    
375     if (!kill(pidfromfile, 0))
376     {
377     /* log(L_ERROR, "Server is already running"); */
378     printf("ircd: daemon is already running\n");
379     exit(-1);
380     }
381     }
382    
383 michael 1325 fclose(fb);
384 adx 30 }
385     else if (errno != ENOENT)
386     {
387     /* log(L_ERROR, "Error opening pid file %s", filename); */
388     }
389     }
390    
391     /* setup_corefile()
392     *
393     * inputs - nothing
394     * output - nothing
395     * side effects - setups corefile to system limits.
396     * -kre
397     */
398     static void
399     setup_corefile(void)
400     {
401     #ifdef HAVE_SYS_RESOURCE_H
402     struct rlimit rlim; /* resource limits */
403    
404     /* Set corefilesize to maximum */
405     if (!getrlimit(RLIMIT_CORE, &rlim))
406     {
407     rlim.rlim_cur = rlim.rlim_max;
408     setrlimit(RLIMIT_CORE, &rlim);
409     }
410     #endif
411     }
412    
413     /* init_ssl()
414     *
415     * inputs - nothing
416     * output - nothing
417     * side effects - setups SSL context.
418     */
419     static void
420 michael 1798 ssl_init(void)
421 adx 30 {
422     #ifdef HAVE_LIBCRYPTO
423     SSL_load_error_strings();
424     SSLeay_add_ssl_algorithms();
425    
426 michael 967 if ((ServerInfo.server_ctx = SSL_CTX_new(SSLv23_server_method())) == NULL)
427 adx 30 {
428     const char *s;
429    
430 michael 1303 fprintf(stderr, "ERROR: Could not initialize the SSL Server context -- %s\n",
431 adx 30 s = ERR_lib_error_string(ERR_get_error()));
432 michael 1303 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Server context -- %s\n", s);
433 adx 30 }
434    
435 michael 1316 SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
436 michael 967 SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL);
437     SSL_CTX_set_verify(ServerInfo.server_ctx, SSL_VERIFY_NONE, NULL);
438 adx 30
439 michael 1303 if ((ServerInfo.client_ctx = SSL_CTX_new(SSLv23_client_method())) == NULL)
440     {
441     const char *s;
442    
443     fprintf(stderr, "ERROR: Could not initialize the SSL Client context -- %s\n",
444     s = ERR_lib_error_string(ERR_get_error()));
445     ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Client context -- %s\n", s);
446     }
447    
448 michael 1316 SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
449 michael 1303 SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL);
450     SSL_CTX_set_verify(ServerInfo.client_ctx, SSL_VERIFY_NONE, NULL);
451 adx 30 #endif /* HAVE_LIBCRYPTO */
452     }
453    
454     int
455     main(int argc, char *argv[])
456     {
457     /* Check to see if the user is running
458     * us as root, which is a nono
459     */
460     if (geteuid() == 0)
461     {
462     fprintf(stderr, "Don't run ircd as root!!!\n");
463 michael 982 return -1;
464 adx 30 }
465    
466     /* Setup corefile size immediately after boot -kre */
467     setup_corefile();
468    
469     /* save server boot time right away, so getrusage works correctly */
470     set_time();
471    
472 michael 982 /* It ain't random, but it ought to be a little harder to guess */
473     init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
474    
475 adx 30 me.localClient = &meLocalUser;
476     dlinkAdd(&me, &me.node, &global_client_list); /* Pointer to beginning
477     of Client list */
478     /* Initialise the channel capability usage counts... */
479     init_chcap_usage_counts();
480    
481     ConfigFileEntry.dpath = DPATH;
482 michael 1702 ConfigFileEntry.configfile = CPATH; /* Server configuration file */
483     ConfigFileEntry.klinefile = KPATH; /* Server kline file */
484 michael 1718 ConfigFileEntry.glinefile = GPATH; /* Server gline file */
485 michael 1702 ConfigFileEntry.xlinefile = XPATH; /* Server xline file */
486     ConfigFileEntry.dlinefile = DLPATH; /* dline file */
487     ConfigFileEntry.resvfile = RESVPATH; /* resv file */
488    
489 adx 30 myargv = argv;
490     umask(077); /* better safe than sorry --SRB */
491    
492     parseargs(&argc, &argv, myopts);
493    
494     if (printVersion)
495     {
496     printf("ircd: version %s\n", ircd_version);
497     exit(EXIT_SUCCESS);
498     }
499    
500     if (chdir(ConfigFileEntry.dpath))
501     {
502     perror("chdir");
503     exit(EXIT_FAILURE);
504     }
505    
506 michael 1798 ssl_init();
507 adx 30
508     if (!server_state.foreground)
509     {
510     make_daemon();
511     close_standard_fds(); /* this needs to be before init_netio()! */
512     }
513     else
514     print_startup(getpid());
515    
516     setup_signals();
517    
518     /* Init the event subsystem */
519     eventInit();
520     /* We need this to initialise the fd array before anything else */
521     fdlist_init();
522 michael 1831 log_set_file(LOG_TYPE_IRCD, 0, logFileName);
523 adx 30 check_can_use_v6();
524     init_comm(); /* This needs to be setup early ! -- adrian */
525     /* Check if there is pidfile and daemon already running */
526     check_pidfile(pidFileName);
527    
528 michael 1654 mp_pool_init();
529 adx 30 init_dlink_nodes();
530 michael 2159 init_isupport();
531 adx 30 dbuf_init();
532 michael 1798 hash_init();
533 adx 30 init_ip_hash_table(); /* client host ip hash table */
534     init_host_hash(); /* Host-hashtable. */
535 michael 1798 client_init();
536 michael 1632 class_init();
537 michael 1358 whowas_init();
538 michael 876 watch_init();
539 michael 1798 auth_init(); /* Initialise the auth code */
540 michael 998 init_resolver(); /* Needs to be setup before the io loop */
541 michael 1404 modules_init();
542 adx 30 read_conf_files(1); /* cold start init conf files */
543     init_uid();
544     initialize_server_capabs(); /* Set up default_server_capabs */
545     initialize_global_set_options();
546 michael 1798 channel_init();
547 michael 2216 read_links_file();
548 michael 2150 motd_init();
549 michael 1858 #ifdef HAVE_LIBGEOIP
550     geoip_ctx = GeoIP_new(GEOIP_MEMORY_CACHE);
551     #endif
552 adx 30
553 michael 1115 if (EmptyString(ServerInfo.sid))
554 adx 30 {
555 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server id specified in serverinfo block.");
556 adx 30 exit(EXIT_FAILURE);
557     }
558 michael 885
559 michael 1115 strlcpy(me.id, ServerInfo.sid, sizeof(me.id));
560    
561     if (EmptyString(ServerInfo.name))
562     {
563 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
564 michael 1115 exit(EXIT_FAILURE);
565     }
566    
567 adx 30 strlcpy(me.name, ServerInfo.name, sizeof(me.name));
568    
569     /* serverinfo{} description must exist. If not, error out.*/
570 michael 1115 if (EmptyString(ServerInfo.description))
571 adx 30 {
572 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
573 adx 30 exit(EXIT_FAILURE);
574     }
575 michael 885
576 adx 30 strlcpy(me.info, ServerInfo.description, sizeof(me.info));
577    
578 michael 1241 me.from = &me;
579     me.servptr = &me;
580     me.localClient->lasttime = CurrentTime;
581     me.localClient->since = CurrentTime;
582     me.localClient->firsttime = CurrentTime;
583 adx 30
584     SetMe(&me);
585     make_server(&me);
586    
587 michael 1115 hash_add_id(&me);
588 adx 30 hash_add_client(&me);
589    
590     /* add ourselves to global_serv_list */
591     dlinkAdd(&me, make_dlink_node(), &global_serv_list);
592    
593 michael 1622 load_kline_database();
594     load_dline_database();
595     load_gline_database();
596     load_xline_database();
597     load_resv_database();
598    
599 adx 30 if (chdir(MODPATH))
600     {
601 michael 1247 ilog(LOG_TYPE_IRCD, "Could not load core modules. Terminating!");
602 adx 30 exit(EXIT_FAILURE);
603     }
604    
605     load_all_modules(1);
606     load_conf_modules();
607     load_core_modules(1);
608 michael 1115
609 adx 30 /* Go back to DPATH after checking to see if we can chdir to MODPATH */
610 michael 1115 if (chdir(ConfigFileEntry.dpath))
611     {
612     perror("chdir");
613     exit(EXIT_FAILURE);
614     }
615 michael 1121
616 adx 30 /*
617     * assemble_umode_buffer() has to be called after
618     * reading conf/loading modules.
619     */
620     assemble_umode_buffer();
621    
622     write_pidfile(pidFileName);
623    
624 michael 1247 ilog(LOG_TYPE_IRCD, "Server Ready");
625 adx 30
626     eventAddIsh("cleanup_glines", cleanup_glines, NULL, CLEANUP_GLINES_TIME);
627     eventAddIsh("cleanup_tklines", cleanup_tklines, NULL, CLEANUP_TKLINES_TIME);
628    
629     /* We want try_connections to be called as soon as possible now! -- adrian */
630     /* No, 'cause after a restart it would cause all sorts of nick collides */
631     eventAddIsh("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
632    
633     /* Setup the timeout check. I'll shift it later :) -- adrian */
634     eventAddIsh("comm_checktimeouts", comm_checktimeouts, NULL, 1);
635    
636 michael 1625 eventAddIsh("save_all_databases", save_all_databases, NULL, DATABASE_UPDATE_TIMEOUT);
637    
638 adx 30 if (ConfigServerHide.links_delay > 0)
639     eventAddIsh("write_links_file", write_links_file, NULL, ConfigServerHide.links_delay);
640     else
641     ConfigServerHide.links_disabled = 1;
642    
643     if (splitmode)
644     eventAddIsh("check_splitmode", check_splitmode, NULL, 60);
645    
646     io_loop();
647 michael 885 return 0;
648 adx 30 }

Properties

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