ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/src/ircd.c
Revision: 86
Committed: Wed Oct 5 20:36:04 2005 UTC (20 years, 10 months ago) by adx
Content type: text/x-csrc
File size: 17921 byte(s)
Log Message:
* attached misc/tools.c to misc/list.c,
  this should really have been done earlier.

* moved mem_frob() to memory.c

* single libio_init() instead of all startup functions;
  btw, I don't know if ircd's still able to boot ..

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

Properties

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