ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid-8/src/ircd.c
Revision: 1001
Committed: Sat Aug 29 22:44:44 2009 UTC (14 years, 7 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-7.2/src/ircd.c
File size: 17612 byte(s)
Log Message:
- remove half done and broken win32 support

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

Properties

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