ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 8660
Committed: Sun Nov 18 12:55:59 2018 UTC (6 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 12727 byte(s)
Log Message:
- Make use of the bool data type in some more places

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 8279 * Copyright (c) 1997-2018 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 4565 * 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 8089 #include "channel_mode.h"
33 adx 30 #include "client.h"
34     #include "event.h"
35     #include "fdlist.h"
36     #include "hash.h"
37 michael 6161 #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 6481 #include "server.h"
51 michael 8166 #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 4325 #include "ipcache.h"
59 michael 6185 #include "isupport.h"
60 adx 30
61 michael 1858
62 michael 5737 struct SetOptions GlobalSetOptions; /* /quote set variables */
63 michael 5602 struct Counter Count;
64     struct ServerState_t server_state;
65     struct ServerStatistics ServerStats;
66 michael 7330 struct ServerTime SystemTime;
67 michael 5737 struct Connection meConnection; /* That's also part of me */
68 michael 5470 struct Client me = { .connection = &meConnection }; /* That's me */
69 adx 30
70 michael 5460 char **myargv;
71 adx 30 const char *logFileName = LPATH;
72     const char *pidFileName = PPATH;
73    
74 michael 8658 bool dorehash;
75     bool doremotd;
76 adx 30
77 michael 8658 static bool printVersion;
78 michael 6735
79     static struct lgetopt myopts[] =
80     {
81     { "configfile", &ConfigGeneral.configfile,
82     STRING, "File to use for ircd.conf" },
83     { "klinefile", &ConfigGeneral.klinefile,
84     STRING, "File to use for kline database" },
85     { "dlinefile", &ConfigGeneral.dlinefile,
86     STRING, "File to use for dline database" },
87     { "xlinefile", &ConfigGeneral.xlinefile,
88     STRING, "File to use for xline database" },
89     { "resvfile", &ConfigGeneral.resvfile,
90     STRING, "File to use for resv database" },
91     { "logfile", &logFileName,
92     STRING, "File to use for ircd.log" },
93     { "pidfile", &pidFileName,
94     STRING, "File to use for process ID" },
95     { "foreground", &server_state.foreground,
96     YESNO, "Run in foreground (don't detach)" },
97     { "version", &printVersion,
98     YESNO, "Print version and exit" },
99     { "help", NULL, USAGE, "Print this text" },
100     { NULL, NULL, STRING, NULL },
101     };
102    
103 michael 4094 static struct event event_cleanup_tklines =
104     {
105     .name = "cleanup_tklines",
106     .handler = cleanup_tklines,
107     .when = CLEANUP_TKLINES_TIME
108     };
109    
110     static struct event event_try_connections =
111     {
112     .name = "try_connections",
113     .handler = try_connections,
114     .when = STARTUP_CONNECTIONS_TIME
115     };
116    
117     static struct event event_comm_checktimeouts =
118     {
119     .name = "comm_checktimeouts",
120     .handler = comm_checktimeouts,
121     .when = 1
122     };
123    
124     static struct event event_save_all_databases =
125     {
126     .name = "save_all_databases",
127     .handler = save_all_databases,
128     .when = DATABASE_UPDATE_TIMEOUT
129     };
130    
131     struct event event_write_links_file =
132     {
133     .name = "write_links_file",
134     .handler = write_links_file,
135     };
136    
137    
138 adx 30 void
139     set_time(void)
140     {
141 michael 2978 struct timeval newtime = { .tv_sec = 0, .tv_usec = 0 };
142 adx 30
143     if (gettimeofday(&newtime, NULL) == -1)
144     {
145 michael 6482 char buf[IRCD_BUFSIZE];
146    
147     snprintf(buf, sizeof(buf), "Clock failure, TS can be corrupted: %s",
148     strerror(errno));
149     server_die(buf, SERVER_SHUTDOWN);
150 adx 30 }
151    
152 michael 7330 if ((uintmax_t)newtime.tv_sec < CurrentTime)
153 adx 30 {
154 michael 6782 ilog(LOG_TYPE_IRCD, "System clock is running backwards - (%ju < %ju)",
155 michael 7330 (uintmax_t)newtime.tv_sec, CurrentTime);
156 michael 2980 sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE,
157 michael 6782 "System clock is running backwards - (%ju < %ju)",
158 michael 7330 (uintmax_t)newtime.tv_sec, CurrentTime);
159     event_set_back_events(CurrentTime - (uintmax_t)newtime.tv_sec);
160 adx 30 }
161    
162 michael 6481 SystemTime.tv_sec = newtime.tv_sec;
163 adx 30 SystemTime.tv_usec = newtime.tv_usec;
164     }
165    
166     static void
167     io_loop(void)
168     {
169 michael 3215 while (1)
170 adx 30 {
171     if (listing_client_list.head)
172     {
173 michael 4815 dlink_node *node = NULL, *node_next = NULL;
174     DLINK_FOREACH_SAFE(node, node_next, listing_client_list.head)
175 michael 8656 safe_list_channels(node->data, false);
176 adx 30 }
177    
178 michael 4094 /* Run pending events */
179     event_run();
180 adx 30
181     comm_select();
182     exit_aborted_clients();
183     free_exited_clients();
184    
185 michael 6735 /* Check to see whether we have to rehash the configuration. */
186 michael 8658 if (dorehash == true)
187 adx 30 {
188 michael 8658 conf_rehash(true);
189     dorehash = false;
190 adx 30 }
191 michael 3215
192 michael 8658 if (doremotd == true)
193 adx 30 {
194 michael 2150 motd_recache();
195 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
196 michael 3065 "Got signal SIGUSR1, reloading motd file(s)");
197 michael 8658 doremotd = false;
198 adx 30 }
199     }
200     }
201    
202     /* initalialize_global_set_options()
203     *
204     * inputs - none
205     * output - none
206 michael 2916 * side effects - This sets all global set options needed
207 adx 30 */
208     static void
209     initialize_global_set_options(void)
210     {
211 michael 5489 GlobalSetOptions.maxclients = ConfigServerInfo.default_max_clients;
212 michael 5499 GlobalSetOptions.autoconn = 1;
213 adx 30 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
214 michael 5499 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
215     GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount;
216 michael 7858 GlobalSetOptions.floodtime = ConfigGeneral.default_floodtime;
217 michael 5489 GlobalSetOptions.joinfloodcount = ConfigChannel.default_join_flood_count;
218     GlobalSetOptions.joinfloodtime = ConfigChannel.default_join_flood_time;
219 adx 30 GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
220     }
221    
222     /* write_pidfile()
223     *
224     * inputs - filename+path of pid file
225     * output - NONE
226     * side effects - write the pid of the ircd to filename
227     */
228     static void
229     write_pidfile(const char *filename)
230     {
231 michael 1325 FILE *fb;
232 adx 30
233 michael 1325 if ((fb = fopen(filename, "w")))
234 adx 30 {
235 michael 6470 char buf[IRCD_BUFSIZE];
236 adx 30 unsigned int pid = (unsigned int)getpid();
237    
238 michael 6470 snprintf(buf, sizeof(buf), "%u\n", pid);
239 michael 1325
240 michael 6470 if (fputs(buf, fb) == -1)
241 michael 5737 ilog(LOG_TYPE_IRCD, "Error writing to pid file %s: %s",
242     filename, strerror(errno));
243 adx 30
244 michael 1325 fclose(fb);
245 adx 30 }
246     else
247 michael 5566 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
248 michael 4748 filename, strerror(errno));
249 adx 30 }
250    
251     /* check_pidfile()
252     *
253     * inputs - filename+path of pid file
254     * output - none
255     * side effects - reads pid from pidfile and checks if ircd is in process
256     * list. if it is, gracefully exits
257     * -kre
258     */
259     static void
260     check_pidfile(const char *filename)
261     {
262 michael 1325 FILE *fb;
263 michael 6470 char buf[IRCD_BUFSIZE];
264 adx 30
265 michael 1325 if ((fb = fopen(filename, "r")))
266 adx 30 {
267 michael 6470 if (!fgets(buf, 20, fb))
268 michael 6260 ilog(LOG_TYPE_IRCD, "Error reading from pid file %s: %s",
269     filename, strerror(errno));
270 adx 30 else
271     {
272 michael 6481 pid_t pid = atoi(buf);
273 adx 30
274 michael 6481 if (!kill(pid, 0))
275 adx 30 {
276     /* log(L_ERROR, "Server is already running"); */
277     printf("ircd: daemon is already running\n");
278 michael 6646 exit(EXIT_FAILURE);
279 adx 30 }
280     }
281    
282 michael 1325 fclose(fb);
283 adx 30 }
284     else if (errno != ENOENT)
285 michael 6260 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
286     filename, strerror(errno));
287 adx 30 }
288    
289     /* setup_corefile()
290     *
291     * inputs - nothing
292     * output - nothing
293     * side effects - setups corefile to system limits.
294     * -kre
295     */
296     static void
297     setup_corefile(void)
298     {
299     struct rlimit rlim; /* resource limits */
300    
301     /* Set corefilesize to maximum */
302     if (!getrlimit(RLIMIT_CORE, &rlim))
303     {
304     rlim.rlim_cur = rlim.rlim_max;
305     setrlimit(RLIMIT_CORE, &rlim);
306     }
307     }
308    
309 michael 6735 /*
310     * print_startup - print startup information
311     */
312     static void
313     print_startup(int pid)
314     {
315     printf("ircd: version %s(%s)\n", ircd_version, serno);
316     printf("ircd: pid %d\n", pid);
317     printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
318     : "foreground", ConfigGeneral.dpath);
319     }
320    
321     static void
322     make_daemon(void)
323     {
324     int pid;
325    
326     if ((pid = fork()) < 0)
327     {
328     perror("fork");
329     exit(EXIT_FAILURE);
330     }
331     else if (pid > 0)
332     {
333     print_startup(pid);
334     exit(EXIT_SUCCESS);
335     }
336    
337     setsid();
338     }
339    
340 adx 30 int
341     main(int argc, char *argv[])
342     {
343 michael 2253 /* Check to see if the user is running us as root, which is a nono */
344 michael 4733 if (!geteuid())
345 adx 30 {
346 michael 3525 fprintf(stderr, "ERROR: This server won't run as root/superuser\n");
347 michael 982 return -1;
348 adx 30 }
349    
350     /* Setup corefile size immediately after boot -kre */
351     setup_corefile();
352    
353 michael 5545 /* Save server boot time right away, so getrusage works correctly */
354 adx 30 set_time();
355    
356 michael 5545 /* It's not random, but it ought to be a little harder to guess */
357 michael 982 init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
358    
359 michael 4340 ConfigGeneral.dpath = DPATH;
360     ConfigGeneral.spath = SPATH;
361     ConfigGeneral.mpath = MPATH;
362     ConfigGeneral.configfile = CPATH; /* Server configuration file */
363     ConfigGeneral.klinefile = KPATH; /* Server kline file */
364     ConfigGeneral.xlinefile = XPATH; /* Server xline file */
365     ConfigGeneral.dlinefile = DLPATH; /* dline file */
366     ConfigGeneral.resvfile = RESVPATH; /* resv file */
367 michael 1702
368 adx 30 myargv = argv;
369 michael 5723 umask(077); /* umask 077: u=rwx,g=,o= */
370 adx 30
371     parseargs(&argc, &argv, myopts);
372    
373 michael 8660 if (printVersion == true)
374 adx 30 {
375 michael 2646 printf("ircd: version %s(%s)\n", ircd_version, serno);
376 adx 30 exit(EXIT_SUCCESS);
377     }
378    
379 michael 4340 if (chdir(ConfigGeneral.dpath))
380 adx 30 {
381     perror("chdir");
382     exit(EXIT_FAILURE);
383     }
384    
385 michael 8660 if (server_state.foreground == false)
386 adx 30 {
387     make_daemon();
388 michael 8414 close_standard_fds(); /* this needs to be before comm_select_init()! */
389 adx 30 }
390     else
391     print_startup(getpid());
392    
393     setup_signals();
394    
395     /* We need this to initialise the fd array before anything else */
396     fdlist_init();
397 michael 1831 log_set_file(LOG_TYPE_IRCD, 0, logFileName);
398 michael 4415
399 michael 8414 comm_select_init(); /* This needs to be setup early ! -- adrian */
400 michael 7279 tls_init();
401 michael 2253
402 adx 30 /* Check if there is pidfile and daemon already running */
403     check_pidfile(pidFileName);
404    
405 michael 6185 isupport_init();
406 michael 1798 hash_init();
407 michael 4319 ipcache_init();
408 michael 1798 client_init();
409 michael 1632 class_init();
410 michael 1798 auth_init(); /* Initialise the auth code */
411 michael 7569 resolver_init(); /* Needs to be setup before the io loop */
412 michael 1404 modules_init();
413 michael 8656 read_conf_files(true); /* cold start init conf files */
414 michael 8166 capab_init(); /* Set up default_server_capabs */
415 michael 5489 initialize_global_set_options(); /* Has to be called after read_conf_files() */
416 michael 8089 channel_mode_init();
417 michael 2216 read_links_file();
418 michael 2150 motd_init();
419 michael 6189 user_modes_init();
420 adx 30
421 michael 4340 if (EmptyString(ConfigServerInfo.name))
422 michael 1115 {
423 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
424 michael 1115 exit(EXIT_FAILURE);
425     }
426    
427 michael 4340 strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name));
428 adx 30
429 michael 6481 /* serverinfo {} description must exist. If not, error out.*/
430 michael 4340 if (EmptyString(ConfigServerInfo.description))
431 adx 30 {
432 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
433 adx 30 exit(EXIT_FAILURE);
434     }
435 michael 885
436 michael 4340 strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info));
437 adx 30
438 michael 6156 if (EmptyString(ConfigServerInfo.sid))
439     {
440     ilog(LOG_TYPE_IRCD, "Generating server ID");
441     generate_sid();
442     }
443     else
444     strlcpy(me.id, ConfigServerInfo.sid, sizeof(me.id));
445    
446 michael 6464 init_uid();
447    
448 michael 5545 me.from = &me;
449     me.servptr = &me;
450     me.connection->lasttime = CurrentTime;
451     me.connection->since = CurrentTime;
452 michael 4588 me.connection->firsttime = CurrentTime;
453 adx 30
454     SetMe(&me);
455 michael 8426 server_make(&me);
456 adx 30
457 michael 1115 hash_add_id(&me);
458 adx 30 hash_add_client(&me);
459 michael 2916
460 michael 7963 dlinkAdd(&me, &me.node, &global_server_list);
461 adx 30
462 michael 6928 load_kline_database(ConfigGeneral.klinefile);
463     load_dline_database(ConfigGeneral.dlinefile);
464     load_xline_database(ConfigGeneral.xlinefile);
465     load_resv_database(ConfigGeneral.resvfile);
466 michael 1622
467 michael 8660 load_all_modules(true);
468 adx 30 load_conf_modules();
469 michael 8660 load_core_modules(true);
470 michael 1115
471 adx 30 write_pidfile(pidFileName);
472    
473 michael 4094 event_addish(&event_cleanup_tklines, NULL);
474 adx 30
475     /* We want try_connections to be called as soon as possible now! -- adrian */
476     /* No, 'cause after a restart it would cause all sorts of nick collides */
477 michael 4094 event_addish(&event_try_connections, NULL);
478 adx 30
479     /* Setup the timeout check. I'll shift it later :) -- adrian */
480 michael 4399 event_add(&event_comm_checktimeouts, NULL);
481 adx 30
482 michael 4094 event_addish(&event_save_all_databases, NULL);
483 michael 1625
484 michael 8660 if (ConfigServerHide.flatten_links_delay && event_write_links_file.active == false)
485 michael 4094 {
486 michael 6597 event_write_links_file.when = ConfigServerHide.flatten_links_delay;
487 michael 6636 event_add(&event_write_links_file, NULL);
488 michael 4094 }
489 adx 30
490 michael 8282 ilog(LOG_TYPE_IRCD, "Server ready. Running version: %s(%s)", ircd_version, serno);
491 adx 30 io_loop();
492 michael 6464
493 michael 885 return 0;
494 adx 30 }

Properties

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