ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/ircd.c
Revision: 1028
Committed: Sun Nov 8 13:03:38 2009 UTC (14 years, 4 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid/src/ircd.c
File size: 17507 byte(s)
Log Message:
- move ircd-hybrid-7.2 to trunk

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

Properties

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