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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1625 - (hide annotations)
Thu Nov 1 13:49:25 2012 UTC (7 years, 10 months ago) by michael
File MIME type: text/x-chdr
File size: 17501 byte(s)
- dbs are now periodically flushed on disc every 5 minutes

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

Properties

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

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