/[svn]/ircd-hybrid/src/ircd.c
ViewVC logotype

Annotation of /ircd-hybrid/src/ircd.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 642 - (hide annotations)
Wed Jun 7 11:03:17 2006 UTC (16 years, 1 month ago) by michael
File MIME type: text/x-chdr
File size: 18087 byte(s)
- Moved setup_corefile() to libio

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 "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 db 91 #include "parse_aline.h"
46     #include "s_serv.h"
47 adx 30 #include "send.h"
48     #include "whowas.h"
49 db 470 #include "conf/modules.h"
50 adx 30 #include "ircd_getopt.h"
51     #include "motd.h"
52     #include "supported.h"
53 michael 217 #include "watch.h"
54 adx 30
55     /* Try and find the correct name to use with getrlimit() for setting the max.
56     * number of files allowed to be open by this process.
57     */
58    
59     /* /quote set variables */
60     struct SetOptions GlobalSetOptions;
61 michael 515 struct ServerStatistics ServerStats;
62 adx 30 /* configuration set from ircd.conf */
63     struct config_file_entry ConfigFileEntry;
64     /* server info set from ircd.conf */
65     struct server_info ServerInfo;
66     /* admin info set from ircd.conf */
67     struct admin_info AdminInfo = { NULL, NULL, NULL };
68     struct Counter Count = { 0, 0, 0, 0, 0, 0, 0, 0 };
69     struct ServerState_t server_state = { 0 };
70     struct logging_entry ConfigLoggingEntry = { 1, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0} };
71     struct Client me; /* That's me */
72     struct LocalUser meLocalUser; /* That's also part of me */
73     unsigned long connect_id = 0; /* unique connect ID */
74    
75     static unsigned long initialVMTop = 0; /* top of virtual memory at init */
76     const char *logFileName = LPATH;
77     const char *pidFileName = PPATH;
78    
79     char **myargv;
80     char ircd_platform[PLATFORMLEN];
81    
82     int dorehash = 0;
83     int doremotd = 0;
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 michael 515 return (unsigned long)vptr;
133 adx 30 }
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 michael 398 safe_list_channels(client_p, client_p->localClient->list_task, 0);
230 adx 30 }
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 michael 450
251 adx 30 if (doremotd)
252     {
253     read_message_file(&ConfigFileEntry.motd);
254     sendto_realops_flags(UMODE_ALL, L_ALL,
255     "Got signal SIGUSR1, reloading ircd motd file");
256     doremotd = 0;
257     }
258     }
259     }
260    
261     /* initalialize_global_set_options()
262     *
263     * inputs - none
264     * output - none
265     * side effects - This sets all global set options needed
266 adx 205 * XXX to be removed with old s_conf.c
267 adx 30 */
268     static void
269     initialize_global_set_options(void)
270     {
271     memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
272    
273     GlobalSetOptions.autoconn = 1;
274     GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
275     GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
276    
277     if (ConfigFileEntry.default_floodcount)
278     GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount;
279     else
280     GlobalSetOptions.floodcount = 10;
281    
282     /* XXX I have no idea what to try here - Dianora */
283     GlobalSetOptions.joinfloodcount = 16;
284     GlobalSetOptions.joinfloodtime = 8;
285    
286     split_servers = ConfigChannel.default_split_server_count;
287     split_users = ConfigChannel.default_split_user_count;
288    
289     if (split_users && split_servers && (ConfigChannel.no_create_on_split ||
290     ConfigChannel.no_join_on_split))
291     {
292     splitmode = 1;
293     splitchecking = 1;
294     }
295    
296     GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
297     GlobalSetOptions.idletime = ConfigFileEntry.idletime;
298 db 278 GlobalSetOptions.maxlisters = 10; /* XXX ya ya ya - db */
299 adx 30 }
300    
301     /* initialize_message_files()
302     *
303     * inputs - none
304     * output - none
305     * side effects - Set up all message files needed, motd etc.
306     */
307     static void
308     initialize_message_files(void)
309     {
310     init_message_file(USER_MOTD, MPATH, &ConfigFileEntry.motd);
311     init_message_file(OPER_MOTD, OPATH, &ConfigFileEntry.opermotd);
312     init_message_file(USER_LINKS, LIPATH, &ConfigFileEntry.linksfile);
313    
314     read_message_file(&ConfigFileEntry.motd);
315     read_message_file(&ConfigFileEntry.opermotd);
316     read_message_file(&ConfigFileEntry.linksfile);
317    
318     init_isupport();
319     }
320    
321     /* initialize_server_capabs()
322     *
323     * inputs - none
324     * output - none
325     */
326     static void
327     initialize_server_capabs(void)
328     {
329     add_capability("QS", CAP_QS, 1);
330     add_capability("EOB", CAP_EOB, 1);
331 adx 208 add_capability("HUB", CAP_HUB, 0);
332 adx 211 add_capability("TS6", CAP_TS6, 0);
333 adx 30 add_capability("ZIP", CAP_ZIP, 0);
334     add_capability("CLUSTER", CAP_CLUSTER, 1);
335     #ifdef HALFOPS
336     add_capability("HOPS", CAP_HOPS, 1);
337     #endif
338     }
339    
340     /* write_pidfile()
341     *
342     * inputs - filename+path of pid file
343     * output - NONE
344     * side effects - write the pid of the ircd to filename
345     */
346     static void
347     write_pidfile(const char *filename)
348     {
349     FBFILE *fb;
350    
351     if ((fb = fbopen(filename, "w")))
352     {
353     char buff[32];
354     unsigned int pid = (unsigned int)getpid();
355     size_t nbytes = ircsprintf(buff, "%u\n", pid);
356    
357     if ((fbputs(buff, fb, nbytes) == -1))
358     ilog(L_ERROR, "Error writing %u to pid file %s (%s)",
359     pid, filename, strerror(errno));
360    
361     fbclose(fb);
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     /* init_ssl()
416     *
417     * inputs - nothing
418     * output - nothing
419     * side effects - setups SSL context.
420     */
421     static void
422     init_ssl(void)
423     {
424     #ifdef HAVE_LIBCRYPTO
425 michael 452 SSL_library_init();
426 adx 30 SSL_load_error_strings();
427    
428     ServerInfo.ctx = SSL_CTX_new(SSLv23_server_method());
429 michael 450
430 adx 30 if (!ServerInfo.ctx)
431     {
432     const char *s;
433    
434     fprintf(stderr, "ERROR: Could not initialize the SSL context -- %s\n",
435     s = ERR_lib_error_string(ERR_get_error()));
436     ilog(L_CRIT, "ERROR: Could not initialize the SSL context -- %s\n", s);
437     }
438    
439     SSL_CTX_set_options(ServerInfo.ctx, SSL_OP_NO_SSLv2);
440     SSL_CTX_set_options(ServerInfo.ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL);
441     SSL_CTX_set_verify(ServerInfo.ctx, SSL_VERIFY_NONE, NULL);
442    
443     bio_spare_fd = save_spare_fd("SSL private key validation");
444     #endif /* HAVE_LIBCRYPTO */
445     }
446    
447     /* init_callbacks()
448     *
449     * inputs - nothing
450     * output - nothing
451     * side effects - setups standard hook points
452     */
453     static void
454     init_callbacks(void)
455     {
456 adx 211 entering_umode_cb = register_callback("entering_umode", NULL);
457 adx 178 iorecv_cb = register_callback("iorecv", iorecv_default);
458     iosend_cb = register_callback("iosend", iosend_default);
459 adx 30 iorecvctrl_cb = register_callback("iorecvctrl", NULL);
460     iosendctrl_cb = register_callback("iosendctrl", NULL);
461 adx 211 uid_get_cb = register_callback("uid_get", uid_get);
462     umode_cb = register_callback("changing_umode", change_simple_umode);
463 adx 30 }
464    
465 adx 68 static void *
466     changing_fdlimit(va_list args)
467     {
468     int old_fdlimit = hard_fdlimit;
469     int fdmax = va_arg(args, int);
470    
471     /* allow MAXCLIENTS_MIN clients even at the cost of MAX_BUFFER and
472     * some not really LEAKED_FDS */
473     fdmax = IRCD_MAX(fdmax, LEAKED_FDS + MAX_BUFFER + MAXCLIENTS_MIN);
474    
475     pass_callback(fdlimit_hook, fdmax);
476    
477     if (ServerInfo.max_clients > MAXCLIENTS_MAX)
478     {
479     if (old_fdlimit != 0)
480     sendto_realops_flags(UMODE_ALL, L_ALL,
481     "HARD_FDLIMIT changed to %d, adjusting MAXCLIENTS to %d",
482     hard_fdlimit, MAXCLIENTS_MAX);
483    
484     ServerInfo.max_clients = MAXCLIENTS_MAX;
485     }
486    
487     return NULL;
488     }
489    
490 adx 185 EXTERN int
491 adx 30 main(int argc, char *argv[])
492     {
493 michael 515 /*
494     * Check to see if the user is running us as root, which is a nono
495 adx 30 */
496     #ifndef _WIN32
497     if (geteuid() == 0)
498     {
499     fprintf(stderr, "Don't run ircd as root!!!\n");
500 adx 62 return 1;
501 adx 30 }
502    
503     /* Setup corefile size immediately after boot -kre */
504     setup_corefile();
505    
506     /* set initialVMTop before we allocate any memory */
507     initialVMTop = get_vm_top();
508     #endif
509    
510     memset(&ServerInfo, 0, sizeof(ServerInfo));
511 michael 515 memset(&ServerStats, 0, sizeof(ServerStats));
512 adx 30
513     ConfigFileEntry.dpath = DPATH;
514     ConfigFileEntry.configfile = CPATH; /* Server configuration file */
515     ConfigFileEntry.klinefile = KPATH; /* Server kline file */
516     ConfigFileEntry.xlinefile = XPATH; /* Server xline file */
517     ConfigFileEntry.rxlinefile = RXPATH; /* Server regex xline file */
518     ConfigFileEntry.rklinefile = RKPATH; /* Server regex kline file */
519     ConfigFileEntry.dlinefile = DLPATH; /* dline file */
520     ConfigFileEntry.glinefile = GPATH; /* gline log file */
521     ConfigFileEntry.cresvfile = CRESVPATH; /* channel resv file */
522     ConfigFileEntry.nresvfile = NRESVPATH; /* nick resv file */
523     myargv = argv;
524     umask(077); /* better safe than sorry --SRB */
525    
526     parseargs(&argc, &argv, myopts);
527    
528     if (printVersion)
529     {
530     printf("ircd: version %s\n", ircd_version);
531     exit(EXIT_SUCCESS);
532     }
533    
534     if (chdir(ConfigFileEntry.dpath))
535     {
536     perror("chdir");
537     exit(EXIT_FAILURE);
538     }
539    
540     init_ssl();
541    
542     #ifndef _WIN32
543     if (!server_state.foreground)
544     make_daemon();
545     else
546     print_startup(getpid());
547 adx 77 #endif
548 adx 30
549 adx 87 libio_init(!server_state.foreground);
550 adx 229 outofmemory = ircd_outofmemory;
551     fdlimit_hook = install_hook(fdlimit_cb, changing_fdlimit);
552    
553 adx 230 check_pidfile(pidFileName);
554 adx 229 setup_signals();
555     get_ircd_platform(ircd_platform);
556     init_log(logFileName);
557     ServerInfo.can_use_v6 = check_can_use_v6();
558    
559 db 228 /* make_dlink_node() cannot be called until after libio_init() */
560     memset(&me, 0, sizeof(me));
561     memset(&meLocalUser, 0, sizeof(meLocalUser));
562 michael 515
563 db 228 me.localClient = &meLocalUser;
564     me.from = me.servptr = &me;
565     me.lasttime = me.since = me.firsttime = CurrentTime;
566    
567     SetMe(&me);
568     make_server(&me);
569     dlinkAdd(&me, &me.node, &global_client_list);
570     dlinkAdd(&me, make_dlink_node(), &global_serv_list);
571    
572 adx 30 init_callbacks();
573     initialize_message_files();
574     init_hash();
575     init_ip_hash_table(); /* client host ip hash table */
576     init_host_hash(); /* Host-hashtable. */
577     clear_tree_parse();
578     init_client();
579     init_class();
580     init_whowas();
581     init_auth(); /* Initialise the auth code */
582     init_channels();
583 adx 48 init_channel_modes();
584 adx 211 initialize_server_capabs(); /* Set up default_server_capabs */
585 michael 504
586 adx 211 read_conf_files(1); /* cold start init conf files */
587     check_class();
588 michael 217 init_watch();
589 adx 211
590 adx 30 if (ServerInfo.name == NULL)
591     {
592 michael 515 ilog(L_CRIT, "ERROR: No server name specified in serverinfo block.");
593 adx 30 exit(EXIT_FAILURE);
594     }
595 michael 450
596 adx 30 strlcpy(me.name, ServerInfo.name, sizeof(me.name));
597    
598     /* serverinfo{} description must exist. If not, error out.*/
599     if (ServerInfo.description == NULL)
600     {
601     ilog(L_CRIT,
602     "ERROR: No server description specified in serverinfo block.");
603     exit(EXIT_FAILURE);
604     }
605 michael 450
606 adx 30 strlcpy(me.info, ServerInfo.description, sizeof(me.info));
607    
608     hash_add_client(&me);
609    
610 adx 211 init_uid(); /* XXX move this one up after inculcating new conf system */
611     initialize_global_set_options(); /* and this one is going to be deleted */
612 michael 257
613 adx 30 #ifndef STATIC_MODULES
614     if (chdir(MODPATH))
615     {
616     ilog (L_CRIT, "Could not load core modules. Terminating!");
617     exit(EXIT_FAILURE);
618     }
619 michael 257
620 adx 475 boot_modules(1);
621 michael 257
622 adx 30 /* Go back to DPATH after checking to see if we can chdir to MODPATH */
623     chdir(ConfigFileEntry.dpath);
624     #else
625     load_all_modules(1);
626     #endif
627     /*
628     * assemble_umode_buffer() has to be called after
629     * reading conf/loading modules.
630     */
631     assemble_umode_buffer();
632    
633     write_pidfile(pidFileName);
634    
635     ilog(L_NOTICE, "Server Ready");
636    
637     eventAddIsh("cleanup_tklines", cleanup_tklines, NULL, CLEANUP_TKLINES_TIME);
638    
639     /* We want try_connections to be called as soon as possible now! -- adrian */
640     /* No, 'cause after a restart it would cause all sorts of nick collides */
641     eventAddIsh("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
642    
643     eventAddIsh("collect_zipstats", collect_zipstats, NULL, ZIPSTATS_TIME);
644    
645     /* Setup the timeout check. I'll shift it later :) -- adrian */
646     eventAddIsh("comm_checktimeouts", comm_checktimeouts, NULL, 1);
647    
648 adx 205 /* XXX to be removed with old s_conf.c [superseded] */
649 adx 30 if (ConfigServerHide.links_delay > 0)
650     eventAddIsh("write_links_file", write_links_file, NULL, ConfigServerHide.links_delay);
651     else
652     ConfigServerHide.links_disabled = 1;
653    
654 adx 205 if (splitmode) /* XXX */
655 adx 30 eventAddIsh("check_splitmode", check_splitmode, NULL, 60);
656    
657     io_loop();
658 michael 450 return 0;
659 adx 30 }
660 adx 185
661     #else
662    
663     #include <windows.h>
664    
665     extern __declspec(dllimport) main(int, char **);
666    
667     /*
668     * Initial entry point for Win32 GUI applications, called by the C runtime.
669     *
670     * It should be only a wrapper for main(), since when compiled as a console
671     * application, main() is called instead.
672     */
673     int WINAPI
674     WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
675     LPSTR lpCmdLine, int nCmdShow)
676     {
677     /* Do we really need these pidfile, logfile etc arguments?
678     * And we are not on a console, so -help or -foreground is meaningless. */
679    
680     char *argv[2] = {"ircd", NULL};
681    
682     return main(1, argv);
683     }
684    
685     #endif

Properties

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

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28