ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/ircd.c
Revision: 8799
Committed: Sat Jan 19 00:22:51 2019 UTC (5 years, 2 months ago) by michael
Content type: text/x-csrc
File size: 13410 byte(s)
Log Message:
- Use bool for SetOptions.autoconn

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 8751 * Copyright (c) 1997-2019 ircd-hybrid development team
5 adx 30 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18 michael 4564 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 adx 30 * USA
20     */
21    
22 michael 2916 /*! \file ircd.c
23     * \brief Starts up and runs the ircd.
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28 michael 3347 #include "user.h"
29 michael 1011 #include "list.h"
30 adx 30 #include "ircd.h"
31     #include "channel.h"
32 michael 8088 #include "channel_mode.h"
33 adx 30 #include "client.h"
34     #include "event.h"
35     #include "fdlist.h"
36     #include "hash.h"
37 michael 6160 #include "id.h"
38 adx 30 #include "irc_string.h"
39     #include "ircd_signal.h"
40     #include "motd.h"
41 michael 1632 #include "conf.h"
42 adx 30 #include "hostmask.h"
43     #include "parse.h"
44 michael 3322 #include "res.h"
45 adx 30 #include "restart.h"
46 michael 982 #include "rng_mt.h"
47 michael 3324 #include "auth.h"
48 adx 30 #include "s_bsd.h"
49 michael 1309 #include "log.h"
50 michael 6480 #include "server.h"
51 michael 8165 #include "server_capab.h"
52 adx 30 #include "send.h"
53     #include "modules.h"
54     #include "memory.h"
55     #include "ircd_getopt.h"
56 michael 1622 #include "conf_db.h"
57 michael 1632 #include "conf_class.h"
58 michael 4326 #include "ipcache.h"
59 michael 6186 #include "isupport.h"
60 michael 8728 #include "patchlevel.h"
61     #include "serno.h"
62 adx 30
63 michael 1858
64 michael 5736 struct SetOptions GlobalSetOptions; /* /quote set variables */
65 michael 5604 struct Counter Count;
66     struct ServerState_t server_state;
67     struct ServerStatistics ServerStats;
68 michael 7329 struct ServerTime SystemTime;
69 michael 5736 struct Connection meConnection; /* That's also part of me */
70 michael 5471 struct Client me = { .connection = &meConnection }; /* That's me */
71 adx 30
72 michael 5459 char **myargv;
73 adx 30 const char *logFileName = LPATH;
74     const char *pidFileName = PPATH;
75    
76 michael 8657 bool dorehash;
77     bool doremotd;
78 adx 30
79 michael 8657 static bool printVersion;
80 michael 6736
81     static struct lgetopt myopts[] =
82     {
83     { "configfile", &ConfigGeneral.configfile,
84     STRING, "File to use for ircd.conf" },
85     { "klinefile", &ConfigGeneral.klinefile,
86     STRING, "File to use for kline database" },
87     { "dlinefile", &ConfigGeneral.dlinefile,
88     STRING, "File to use for dline database" },
89     { "xlinefile", &ConfigGeneral.xlinefile,
90     STRING, "File to use for xline database" },
91     { "resvfile", &ConfigGeneral.resvfile,
92     STRING, "File to use for resv database" },
93     { "logfile", &logFileName,
94     STRING, "File to use for ircd.log" },
95     { "pidfile", &pidFileName,
96     STRING, "File to use for process ID" },
97     { "foreground", &server_state.foreground,
98     YESNO, "Run in foreground (don't detach)" },
99     { "version", &printVersion,
100     YESNO, "Print version and exit" },
101     { "help", NULL, USAGE, "Print this text" },
102     { NULL, NULL, STRING, NULL },
103     };
104    
105 michael 4095 static struct event event_cleanup_tklines =
106     {
107     .name = "cleanup_tklines",
108     .handler = cleanup_tklines,
109     .when = CLEANUP_TKLINES_TIME
110     };
111    
112     static struct event event_try_connections =
113     {
114     .name = "try_connections",
115     .handler = try_connections,
116     .when = STARTUP_CONNECTIONS_TIME
117     };
118    
119     static struct event event_comm_checktimeouts =
120     {
121     .name = "comm_checktimeouts",
122     .handler = comm_checktimeouts,
123     .when = 1
124     };
125    
126     static struct event event_save_all_databases =
127     {
128     .name = "save_all_databases",
129     .handler = save_all_databases,
130     .when = DATABASE_UPDATE_TIMEOUT
131     };
132    
133     struct event event_write_links_file =
134     {
135     .name = "write_links_file",
136     .handler = write_links_file,
137     };
138    
139    
140 adx 30 void
141     set_time(void)
142     {
143 michael 2978 struct timeval newtime = { .tv_sec = 0, .tv_usec = 0 };
144 adx 30
145     if (gettimeofday(&newtime, NULL) == -1)
146     {
147 michael 6483 char buf[IRCD_BUFSIZE];
148    
149     snprintf(buf, sizeof(buf), "Clock failure, TS can be corrupted: %s",
150     strerror(errno));
151 michael 8707 server_die(buf, false);
152 adx 30 }
153    
154 michael 7329 if ((uintmax_t)newtime.tv_sec < CurrentTime)
155 adx 30 {
156 michael 6781 ilog(LOG_TYPE_IRCD, "System clock is running backwards - (%ju < %ju)",
157 michael 7329 (uintmax_t)newtime.tv_sec, CurrentTime);
158 michael 2980 sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE,
159 michael 6781 "System clock is running backwards - (%ju < %ju)",
160 michael 7329 (uintmax_t)newtime.tv_sec, CurrentTime);
161     event_set_back_events(CurrentTime - (uintmax_t)newtime.tv_sec);
162 adx 30 }
163    
164 michael 6480 SystemTime.tv_sec = newtime.tv_sec;
165 adx 30 SystemTime.tv_usec = newtime.tv_usec;
166     }
167    
168     static void
169     io_loop(void)
170     {
171 michael 8663 while (true)
172 adx 30 {
173     if (listing_client_list.head)
174     {
175 michael 4816 dlink_node *node = NULL, *node_next = NULL;
176     DLINK_FOREACH_SAFE(node, node_next, listing_client_list.head)
177 michael 8655 safe_list_channels(node->data, false);
178 adx 30 }
179    
180 michael 4095 /* Run pending events */
181     event_run();
182 adx 30
183     comm_select();
184     exit_aborted_clients();
185     free_exited_clients();
186    
187 michael 6736 /* Check to see whether we have to rehash the configuration. */
188 michael 8657 if (dorehash == true)
189 adx 30 {
190 michael 8657 conf_rehash(true);
191     dorehash = false;
192 adx 30 }
193 michael 3215
194 michael 8657 if (doremotd == true)
195 adx 30 {
196 michael 2150 motd_recache();
197 michael 6317 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
198 michael 3065 "Got signal SIGUSR1, reloading motd file(s)");
199 michael 8657 doremotd = false;
200 adx 30 }
201     }
202     }
203    
204     /* initalialize_global_set_options()
205     *
206     * inputs - none
207     * output - none
208 michael 2916 * side effects - This sets all global set options needed
209 adx 30 */
210     static void
211     initialize_global_set_options(void)
212     {
213 michael 5488 GlobalSetOptions.maxclients = ConfigServerInfo.default_max_clients;
214 michael 8799 GlobalSetOptions.autoconn = true;
215 adx 30 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
216 michael 5498 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
217     GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount;
218 michael 7859 GlobalSetOptions.floodtime = ConfigGeneral.default_floodtime;
219 michael 5488 GlobalSetOptions.joinfloodcount = ConfigChannel.default_join_flood_count;
220     GlobalSetOptions.joinfloodtime = ConfigChannel.default_join_flood_time;
221 adx 30 GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
222     }
223    
224     /* write_pidfile()
225     *
226     * inputs - filename+path of pid file
227     * output - NONE
228     * side effects - write the pid of the ircd to filename
229     */
230     static void
231     write_pidfile(const char *filename)
232     {
233 michael 1325 FILE *fb;
234 adx 30
235 michael 1325 if ((fb = fopen(filename, "w")))
236 adx 30 {
237 michael 6471 char buf[IRCD_BUFSIZE];
238 adx 30 unsigned int pid = (unsigned int)getpid();
239    
240 michael 6471 snprintf(buf, sizeof(buf), "%u\n", pid);
241 michael 1325
242 michael 6471 if (fputs(buf, fb) == -1)
243 michael 5736 ilog(LOG_TYPE_IRCD, "Error writing to pid file %s: %s",
244     filename, strerror(errno));
245 adx 30
246 michael 1325 fclose(fb);
247 adx 30 }
248     else
249 michael 5565 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
250 michael 4749 filename, strerror(errno));
251 adx 30 }
252    
253     /* check_pidfile()
254     *
255     * inputs - filename+path of pid file
256     * output - none
257     * side effects - reads pid from pidfile and checks if ircd is in process
258     * list. if it is, gracefully exits
259     * -kre
260     */
261     static void
262     check_pidfile(const char *filename)
263     {
264 michael 1325 FILE *fb;
265 michael 6471 char buf[IRCD_BUFSIZE];
266 adx 30
267 michael 1325 if ((fb = fopen(filename, "r")))
268 adx 30 {
269 michael 8703 if (fgets(buf, 20, fb) == NULL)
270 michael 6261 ilog(LOG_TYPE_IRCD, "Error reading from pid file %s: %s",
271     filename, strerror(errno));
272 adx 30 else
273     {
274 michael 6480 pid_t pid = atoi(buf);
275 adx 30
276 michael 8703 if (kill(pid, 0) == 0)
277 adx 30 {
278     /* log(L_ERROR, "Server is already running"); */
279     printf("ircd: daemon is already running\n");
280 michael 6647 exit(EXIT_FAILURE);
281 adx 30 }
282     }
283    
284 michael 1325 fclose(fb);
285 adx 30 }
286     else if (errno != ENOENT)
287 michael 6261 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
288     filename, strerror(errno));
289 adx 30 }
290    
291     /* setup_corefile()
292     *
293     * inputs - nothing
294     * output - nothing
295     * side effects - setups corefile to system limits.
296     * -kre
297     */
298     static void
299     setup_corefile(void)
300     {
301     struct rlimit rlim; /* resource limits */
302    
303     /* Set corefilesize to maximum */
304 michael 8703 if (getrlimit(RLIMIT_CORE, &rlim) == 0)
305 adx 30 {
306     rlim.rlim_cur = rlim.rlim_max;
307     setrlimit(RLIMIT_CORE, &rlim);
308     }
309     }
310    
311 michael 8773 static void
312     setup_fdlimit(void)
313     {
314     struct rlimit rlim; /* resource limits */
315    
316     if (getrlimit(RLIMIT_NOFILE, &rlim))
317     {
318     fprintf(stderr, "getrlimit: couldn't get maximum number of file descriptors: %s\n",
319     strerror(errno));
320     exit(EXIT_FAILURE);
321     }
322    
323     if (rlim.rlim_max > 0xFFFF)
324     rlim.rlim_max = 0xFFFF;
325     rlim.rlim_cur = rlim.rlim_max;
326    
327     if (setrlimit(RLIMIT_NOFILE, &rlim) == 0)
328     hard_fdlimit = rlim.rlim_cur;
329     else
330     {
331     fprintf(stderr, "setrlimit: couldn't set maximum number of file descriptors: %s\n",
332     strerror(errno));
333     exit(EXIT_FAILURE);
334     }
335     }
336    
337 michael 6736 /*
338     * print_startup - print startup information
339     */
340     static void
341     print_startup(int pid)
342     {
343 michael 8728 printf("ircd: version %s(%s)\n", PATCHLEVEL, SERIALNUM);
344 michael 6736 printf("ircd: pid %d\n", pid);
345     printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
346     : "foreground", ConfigGeneral.dpath);
347     }
348    
349     static void
350     make_daemon(void)
351     {
352     int pid;
353    
354     if ((pid = fork()) < 0)
355     {
356     perror("fork");
357     exit(EXIT_FAILURE);
358     }
359     else if (pid > 0)
360     {
361     print_startup(pid);
362     exit(EXIT_SUCCESS);
363     }
364    
365     setsid();
366     }
367    
368 adx 30 int
369     main(int argc, char *argv[])
370     {
371 michael 2253 /* Check to see if the user is running us as root, which is a nono */
372 michael 8703 if (geteuid() == 0)
373 adx 30 {
374 michael 3526 fprintf(stderr, "ERROR: This server won't run as root/superuser\n");
375 michael 982 return -1;
376 adx 30 }
377    
378     /* Setup corefile size immediately after boot -kre */
379     setup_corefile();
380    
381 michael 8773 setup_fdlimit();
382    
383 michael 5546 /* Save server boot time right away, so getrusage works correctly */
384 adx 30 set_time();
385    
386 michael 5546 /* It's not random, but it ought to be a little harder to guess */
387 michael 982 init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
388    
389 michael 4341 ConfigGeneral.dpath = DPATH;
390     ConfigGeneral.spath = SPATH;
391     ConfigGeneral.mpath = MPATH;
392     ConfigGeneral.configfile = CPATH; /* Server configuration file */
393     ConfigGeneral.klinefile = KPATH; /* Server kline file */
394     ConfigGeneral.xlinefile = XPATH; /* Server xline file */
395     ConfigGeneral.dlinefile = DLPATH; /* dline file */
396     ConfigGeneral.resvfile = RESVPATH; /* resv file */
397 michael 1702
398 adx 30 myargv = argv;
399 michael 5722 umask(077); /* umask 077: u=rwx,g=,o= */
400 adx 30
401     parseargs(&argc, &argv, myopts);
402    
403 michael 8659 if (printVersion == true)
404 adx 30 {
405 michael 8728 printf("ircd: version %s(%s)\n", PATCHLEVEL, SERIALNUM);
406 adx 30 exit(EXIT_SUCCESS);
407     }
408    
409 michael 4341 if (chdir(ConfigGeneral.dpath))
410 adx 30 {
411     perror("chdir");
412     exit(EXIT_FAILURE);
413     }
414    
415 michael 8659 if (server_state.foreground == false)
416 adx 30 {
417     make_daemon();
418 michael 8413 close_standard_fds(); /* this needs to be before comm_select_init()! */
419 adx 30 }
420     else
421     print_startup(getpid());
422    
423     setup_signals();
424    
425     /* We need this to initialise the fd array before anything else */
426     fdlist_init();
427 michael 1831 log_set_file(LOG_TYPE_IRCD, 0, logFileName);
428 michael 4414
429 michael 8413 comm_select_init(); /* This needs to be setup early ! -- adrian */
430 michael 7280 tls_init();
431 michael 2253
432 adx 30 /* Check if there is pidfile and daemon already running */
433     check_pidfile(pidFileName);
434    
435 michael 6186 isupport_init();
436 michael 1798 hash_init();
437 michael 4320 ipcache_init();
438 michael 1798 client_init();
439 michael 1632 class_init();
440 michael 1798 auth_init(); /* Initialise the auth code */
441 michael 7568 resolver_init(); /* Needs to be setup before the io loop */
442 michael 1404 modules_init();
443 michael 8655 read_conf_files(true); /* cold start init conf files */
444 michael 8165 capab_init(); /* Set up default_server_capabs */
445 michael 5488 initialize_global_set_options(); /* Has to be called after read_conf_files() */
446 michael 8088 channel_mode_init();
447 michael 2216 read_links_file();
448 michael 2150 motd_init();
449 michael 6190 user_modes_init();
450 adx 30
451 michael 4341 if (EmptyString(ConfigServerInfo.name))
452 michael 1115 {
453 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
454 michael 1115 exit(EXIT_FAILURE);
455     }
456    
457 michael 4341 strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name));
458 adx 30
459 michael 6480 /* serverinfo {} description must exist. If not, error out.*/
460 michael 4341 if (EmptyString(ConfigServerInfo.description))
461 adx 30 {
462 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
463 adx 30 exit(EXIT_FAILURE);
464     }
465 michael 885
466 michael 4341 strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info));
467 adx 30
468 michael 6157 if (EmptyString(ConfigServerInfo.sid))
469     {
470     ilog(LOG_TYPE_IRCD, "Generating server ID");
471     generate_sid();
472     }
473     else
474     strlcpy(me.id, ConfigServerInfo.sid, sizeof(me.id));
475    
476 michael 6465 init_uid();
477    
478 michael 5546 me.from = &me;
479     me.servptr = &me;
480     me.connection->lasttime = CurrentTime;
481     me.connection->since = CurrentTime;
482 michael 4589 me.connection->firsttime = CurrentTime;
483 adx 30
484     SetMe(&me);
485 michael 8425 server_make(&me);
486 adx 30
487 michael 1115 hash_add_id(&me);
488 adx 30 hash_add_client(&me);
489 michael 2916
490 michael 7962 dlinkAdd(&me, &me.node, &global_server_list);
491 adx 30
492 michael 6927 load_kline_database(ConfigGeneral.klinefile);
493     load_dline_database(ConfigGeneral.dlinefile);
494     load_xline_database(ConfigGeneral.xlinefile);
495     load_resv_database(ConfigGeneral.resvfile);
496 michael 1622
497 michael 8659 load_all_modules(true);
498 adx 30 load_conf_modules();
499 michael 8659 load_core_modules(true);
500 michael 1115
501 adx 30 write_pidfile(pidFileName);
502    
503 michael 4095 event_addish(&event_cleanup_tklines, NULL);
504 adx 30
505     /* We want try_connections to be called as soon as possible now! -- adrian */
506     /* No, 'cause after a restart it would cause all sorts of nick collides */
507 michael 4095 event_addish(&event_try_connections, NULL);
508 adx 30
509     /* Setup the timeout check. I'll shift it later :) -- adrian */
510 michael 4398 event_add(&event_comm_checktimeouts, NULL);
511 adx 30
512 michael 4095 event_addish(&event_save_all_databases, NULL);
513 michael 1625
514 michael 8659 if (ConfigServerHide.flatten_links_delay && event_write_links_file.active == false)
515 michael 4095 {
516 michael 6598 event_write_links_file.when = ConfigServerHide.flatten_links_delay;
517 michael 6635 event_add(&event_write_links_file, NULL);
518 michael 4095 }
519 adx 30
520 michael 8728 ilog(LOG_TYPE_IRCD, "Server ready. Running version: %s(%s)", PATCHLEVEL, SERIALNUM);
521 adx 30 io_loop();
522 michael 6465
523 michael 885 return 0;
524 adx 30 }

Properties

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