ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/src/ircd.c
Revision: 257
Committed: Sat Nov 12 16:00:08 2005 UTC (20 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 18705 byte(s)
Log Message:
- Forgot to remove my debugging code ..

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

Properties

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