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