ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/src/ircd.c
Revision: 68
Committed: Tue Oct 4 00:44:29 2005 UTC (18 years, 5 months ago) by adx
Content type: text/x-csrc
File size: 18765 byte(s)
Log Message:
- removed last references to the ircd core, libio is independent now
- to do: split headers

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

Properties

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