ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 1404
Committed: Thu May 10 20:41:45 2012 UTC (11 years, 11 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/ircd.c
File size: 17564 byte(s)
Log Message:
- minor cleanups to the module 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     #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 adx 30
63    
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     sendto_realops_flags(UMODE_ALL, L_ALL,
178     "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     sendto_realops_flags(UMODE_ALL, L_ALL,
247     "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(OPER_MOTD, OPATH, &ConfigFileEntry.opermotd);
302     init_message_file(USER_LINKS, LIPATH, &ConfigFileEntry.linksfile);
303    
304     read_message_file(&ConfigFileEntry.motd);
305     read_message_file(&ConfigFileEntry.opermotd);
306     read_message_file(&ConfigFileEntry.linksfile);
307    
308     init_isupport();
309     }
310    
311     /* initialize_server_capabs()
312     *
313     * inputs - none
314     * output - none
315     */
316     static void
317     initialize_server_capabs(void)
318     {
319     add_capability("QS", CAP_QS, 1);
320     add_capability("EOB", CAP_EOB, 1);
321 michael 1117 add_capability("TS6", CAP_TS6, 0);
322 adx 30 add_capability("CLUSTER", CAP_CLUSTER, 1);
323 michael 1196 add_capability("SVS", CAP_SVS, 1);
324 adx 30 #ifdef HALFOPS
325     add_capability("HOPS", CAP_HOPS, 1);
326     #endif
327     }
328    
329     /* write_pidfile()
330     *
331     * inputs - filename+path of pid file
332     * output - NONE
333     * side effects - write the pid of the ircd to filename
334     */
335     static void
336     write_pidfile(const char *filename)
337     {
338 michael 1325 FILE *fb;
339 adx 30
340 michael 1325 if ((fb = fopen(filename, "w")))
341 adx 30 {
342     char buff[32];
343     unsigned int pid = (unsigned int)getpid();
344    
345 michael 1325 snprintf(buff, sizeof(buff), "%u\n", pid);
346    
347     if ((fputs(buff, fb) == -1))
348 michael 1247 ilog(LOG_TYPE_IRCD, "Error writing %u to pid file %s (%s)",
349 adx 30 pid, filename, strerror(errno));
350    
351 michael 1325 fclose(fb);
352 adx 30 }
353     else
354     {
355 michael 1247 ilog(LOG_TYPE_IRCD, "Error opening pid file %s", filename);
356 adx 30 }
357     }
358    
359     /* check_pidfile()
360     *
361     * inputs - filename+path of pid file
362     * output - none
363     * side effects - reads pid from pidfile and checks if ircd is in process
364     * list. if it is, gracefully exits
365     * -kre
366     */
367     static void
368     check_pidfile(const char *filename)
369     {
370 michael 1325 FILE *fb;
371 adx 30 char buff[32];
372     pid_t pidfromfile;
373    
374     /* Don't do logging here, since we don't have log() initialised */
375 michael 1325 if ((fb = fopen(filename, "r")))
376 adx 30 {
377 michael 1325 if (fgets(buff, 20, fb) == NULL)
378 adx 30 {
379     /* log(L_ERROR, "Error reading from pid file %s (%s)", filename,
380     * strerror(errno));
381     */
382     }
383     else
384     {
385     pidfromfile = atoi(buff);
386    
387     if (!kill(pidfromfile, 0))
388     {
389     /* log(L_ERROR, "Server is already running"); */
390     printf("ircd: daemon is already running\n");
391     exit(-1);
392     }
393     }
394    
395 michael 1325 fclose(fb);
396 adx 30 }
397     else if (errno != ENOENT)
398     {
399     /* log(L_ERROR, "Error opening pid file %s", filename); */
400     }
401     }
402    
403     /* setup_corefile()
404     *
405     * inputs - nothing
406     * output - nothing
407     * side effects - setups corefile to system limits.
408     * -kre
409     */
410     static void
411     setup_corefile(void)
412     {
413     #ifdef HAVE_SYS_RESOURCE_H
414     struct rlimit rlim; /* resource limits */
415    
416     /* Set corefilesize to maximum */
417     if (!getrlimit(RLIMIT_CORE, &rlim))
418     {
419     rlim.rlim_cur = rlim.rlim_max;
420     setrlimit(RLIMIT_CORE, &rlim);
421     }
422     #endif
423     }
424    
425     /* init_ssl()
426     *
427     * inputs - nothing
428     * output - nothing
429     * side effects - setups SSL context.
430     */
431     static void
432     init_ssl(void)
433     {
434     #ifdef HAVE_LIBCRYPTO
435     SSL_load_error_strings();
436     SSLeay_add_ssl_algorithms();
437    
438 michael 967 if ((ServerInfo.server_ctx = SSL_CTX_new(SSLv23_server_method())) == NULL)
439 adx 30 {
440     const char *s;
441    
442 michael 1303 fprintf(stderr, "ERROR: Could not initialize the SSL Server context -- %s\n",
443 adx 30 s = ERR_lib_error_string(ERR_get_error()));
444 michael 1303 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Server context -- %s\n", s);
445 adx 30 }
446    
447 michael 1316 SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
448 michael 967 SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL);
449     SSL_CTX_set_verify(ServerInfo.server_ctx, SSL_VERIFY_NONE, NULL);
450 adx 30
451 michael 1303 if ((ServerInfo.client_ctx = SSL_CTX_new(SSLv23_client_method())) == NULL)
452     {
453     const char *s;
454    
455     fprintf(stderr, "ERROR: Could not initialize the SSL Client context -- %s\n",
456     s = ERR_lib_error_string(ERR_get_error()));
457     ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Client context -- %s\n", s);
458     }
459    
460 michael 1316 SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
461 michael 1303 SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL);
462     SSL_CTX_set_verify(ServerInfo.client_ctx, SSL_VERIFY_NONE, NULL);
463 adx 30 #endif /* HAVE_LIBCRYPTO */
464     }
465    
466     /* init_callbacks()
467     *
468     * inputs - nothing
469     * output - nothing
470     * side effects - setups standard hook points
471     */
472     static void
473     init_callbacks(void)
474     {
475 adx 163 iorecv_cb = register_callback("iorecv", iorecv_default);
476     iosend_cb = register_callback("iosend", iosend_default);
477 adx 30 }
478    
479     int
480     main(int argc, char *argv[])
481     {
482     /* Check to see if the user is running
483     * us as root, which is a nono
484     */
485     if (geteuid() == 0)
486     {
487     fprintf(stderr, "Don't run ircd as root!!!\n");
488 michael 982 return -1;
489 adx 30 }
490    
491     /* Setup corefile size immediately after boot -kre */
492     setup_corefile();
493    
494     /* save server boot time right away, so getrusage works correctly */
495     set_time();
496    
497 michael 982 /* It ain't random, but it ought to be a little harder to guess */
498     init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
499    
500 adx 30 me.localClient = &meLocalUser;
501     dlinkAdd(&me, &me.node, &global_client_list); /* Pointer to beginning
502     of Client list */
503     /* Initialise the channel capability usage counts... */
504     init_chcap_usage_counts();
505    
506     ConfigFileEntry.dpath = DPATH;
507     ConfigFileEntry.configfile = CPATH; /* Server configuration file */
508     ConfigFileEntry.klinefile = KPATH; /* Server kline file */
509     ConfigFileEntry.xlinefile = XPATH; /* Server xline file */
510     ConfigFileEntry.rxlinefile = RXPATH; /* Server regex xline file */
511     ConfigFileEntry.rklinefile = RKPATH; /* Server regex kline file */
512     ConfigFileEntry.dlinefile = DLPATH; /* dline file */
513     ConfigFileEntry.glinefile = GPATH; /* gline log file */
514     ConfigFileEntry.cresvfile = CRESVPATH; /* channel resv file */
515     ConfigFileEntry.nresvfile = NRESVPATH; /* nick resv file */
516     myargv = argv;
517     umask(077); /* better safe than sorry --SRB */
518    
519     parseargs(&argc, &argv, myopts);
520    
521     if (printVersion)
522     {
523     printf("ircd: version %s\n", ircd_version);
524     exit(EXIT_SUCCESS);
525     }
526    
527     if (chdir(ConfigFileEntry.dpath))
528     {
529     perror("chdir");
530     exit(EXIT_FAILURE);
531     }
532    
533     init_ssl();
534    
535     if (!server_state.foreground)
536     {
537     make_daemon();
538     close_standard_fds(); /* this needs to be before init_netio()! */
539     }
540     else
541     print_startup(getpid());
542    
543     setup_signals();
544    
545     /* Init the event subsystem */
546     eventInit();
547     /* We need this to initialise the fd array before anything else */
548     fdlist_init();
549 michael 1247 log_add_file(LOG_TYPE_IRCD, 0, logFileName);
550 adx 30 check_can_use_v6();
551     init_comm(); /* This needs to be setup early ! -- adrian */
552     /* Check if there is pidfile and daemon already running */
553     check_pidfile(pidFileName);
554    
555     initBlockHeap();
556     init_dlink_nodes();
557     init_callbacks();
558     initialize_message_files();
559     dbuf_init();
560     init_hash();
561     init_ip_hash_table(); /* client host ip hash table */
562     init_host_hash(); /* Host-hashtable. */
563     init_client();
564     init_class();
565 michael 1358 whowas_init();
566 michael 876 watch_init();
567 michael 998 init_auth(); /* Initialise the auth code */
568     init_resolver(); /* Needs to be setup before the io loop */
569 michael 1404 modules_init();
570 adx 30 read_conf_files(1); /* cold start init conf files */
571     init_uid();
572     initialize_server_capabs(); /* Set up default_server_capabs */
573     initialize_global_set_options();
574     init_channels();
575    
576 michael 1115 if (EmptyString(ServerInfo.sid))
577 adx 30 {
578 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server id specified in serverinfo block.");
579 adx 30 exit(EXIT_FAILURE);
580     }
581 michael 885
582 michael 1115 strlcpy(me.id, ServerInfo.sid, sizeof(me.id));
583    
584     if (EmptyString(ServerInfo.name))
585     {
586 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
587 michael 1115 exit(EXIT_FAILURE);
588     }
589    
590 adx 30 strlcpy(me.name, ServerInfo.name, sizeof(me.name));
591    
592     /* serverinfo{} description must exist. If not, error out.*/
593 michael 1115 if (EmptyString(ServerInfo.description))
594 adx 30 {
595 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
596 adx 30 exit(EXIT_FAILURE);
597     }
598 michael 885
599 adx 30 strlcpy(me.info, ServerInfo.description, sizeof(me.info));
600    
601 michael 1241 me.from = &me;
602     me.servptr = &me;
603     me.localClient->lasttime = CurrentTime;
604     me.localClient->since = CurrentTime;
605     me.localClient->firsttime = CurrentTime;
606 adx 30
607     SetMe(&me);
608     make_server(&me);
609    
610 michael 1115 hash_add_id(&me);
611 adx 30 hash_add_client(&me);
612    
613     /* add ourselves to global_serv_list */
614     dlinkAdd(&me, make_dlink_node(), &global_serv_list);
615    
616     if (chdir(MODPATH))
617     {
618 michael 1247 ilog(LOG_TYPE_IRCD, "Could not load core modules. Terminating!");
619 adx 30 exit(EXIT_FAILURE);
620     }
621    
622     load_all_modules(1);
623     load_conf_modules();
624     load_core_modules(1);
625 michael 1115
626 adx 30 /* Go back to DPATH after checking to see if we can chdir to MODPATH */
627 michael 1115 if (chdir(ConfigFileEntry.dpath))
628     {
629     perror("chdir");
630     exit(EXIT_FAILURE);
631     }
632 michael 1121
633 adx 30 /*
634     * assemble_umode_buffer() has to be called after
635     * reading conf/loading modules.
636     */
637     assemble_umode_buffer();
638    
639     write_pidfile(pidFileName);
640    
641 michael 1247 ilog(LOG_TYPE_IRCD, "Server Ready");
642 adx 30
643     eventAddIsh("cleanup_glines", cleanup_glines, NULL, CLEANUP_GLINES_TIME);
644     eventAddIsh("cleanup_tklines", cleanup_tklines, NULL, CLEANUP_TKLINES_TIME);
645    
646     /* We want try_connections to be called as soon as possible now! -- adrian */
647     /* No, 'cause after a restart it would cause all sorts of nick collides */
648     eventAddIsh("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
649    
650     /* Setup the timeout check. I'll shift it later :) -- adrian */
651     eventAddIsh("comm_checktimeouts", comm_checktimeouts, NULL, 1);
652    
653     if (ConfigServerHide.links_delay > 0)
654     eventAddIsh("write_links_file", write_links_file, NULL, ConfigServerHide.links_delay);
655     else
656     ConfigServerHide.links_disabled = 1;
657    
658     if (splitmode)
659     eventAddIsh("check_splitmode", check_splitmode, NULL, 60);
660    
661     io_loop();
662 michael 885 return 0;
663 adx 30 }

Properties

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