ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
(Generate patch)

Comparing ircd-hybrid/trunk/src/ircd.c (file contents):
Revision 4892 by michael, Wed Nov 19 17:14:30 2014 UTC vs.
Revision 7006 by michael, Fri Jan 1 00:07:54 2016 UTC

# Line 1 | Line 1
1   /*
2   *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3   *
4 < *  Copyright (c) 1997-2014 ircd-hybrid development team
4 > *  Copyright (c) 1997-2016 ircd-hybrid development team
5   *
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
# Line 33 | Line 33
33   #include "event.h"
34   #include "fdlist.h"
35   #include "hash.h"
36 + #include "id.h"
37   #include "irc_string.h"
38   #include "ircd_signal.h"
38 #include "gline.h"
39   #include "motd.h"
40   #include "conf.h"
41   #include "hostmask.h"
# Line 46 | Line 46
46   #include "auth.h"
47   #include "s_bsd.h"
48   #include "log.h"
49 < #include "server.h"      /* try_connections */
49 > #include "server.h"
50   #include "send.h"
51   #include "whowas.h"
52   #include "modules.h"
# Line 57 | Line 57
57   #include "conf_db.h"
58   #include "conf_class.h"
59   #include "ipcache.h"
60 + #include "isupport.h"
61 + #include "userhost.h"
62  
63  
64   #ifdef HAVE_LIBGEOIP
65   GeoIP *geoip_ctx;
66   #endif
67  
68 + struct SetOptions GlobalSetOptions;  /* /quote set variables */
69 + struct Counter Count;
70 + struct ServerState_t server_state;
71 + struct ServerStatistics ServerStats;
72   struct timeval SystemTime;
73 < struct Client me;             /* That's me */
74 < struct Connection meConnection; /* That's also part of me */
73 > struct Connection meConnection;  /* That's also part of me */
74 > struct Client me = { .connection = &meConnection };  /* That's me */
75  
76 + char **myargv;
77   const char *logFileName = LPATH;
78   const char *pidFileName = PPATH;
79  
80 < char **myargv;
80 > unsigned int dorehash;
81 > unsigned int doremotd;
82  
83 < int dorehash = 0;
76 < int doremotd = 0;
83 > static int printVersion;
84  
85 < /* Set to zero because it should be initialized later using
86 < * initialize_server_capabs
87 < */
88 < unsigned int default_server_capabs;
89 < unsigned int splitmode;
90 < unsigned int splitchecking;
91 < unsigned int split_users;
92 < unsigned int split_servers;
93 <
94 < static struct event event_cleanup_glines =
95 < {
96 <  .name = "cleanup_glines",
97 <  .handler = cleanup_glines,
98 <  .when = CLEANUP_GLINES_TIME
85 > static struct lgetopt myopts[] =
86 > {
87 >  { "configfile", &ConfigGeneral.configfile,
88 >   STRING, "File to use for ircd.conf" },
89 >  { "klinefile",  &ConfigGeneral.klinefile,
90 >   STRING, "File to use for kline database" },
91 >  { "dlinefile",  &ConfigGeneral.dlinefile,
92 >   STRING, "File to use for dline database" },
93 >  { "xlinefile",  &ConfigGeneral.xlinefile,
94 >   STRING, "File to use for xline database" },
95 >  { "resvfile",   &ConfigGeneral.resvfile,
96 >   STRING, "File to use for resv database" },
97 >  { "logfile",    &logFileName,
98 >   STRING, "File to use for ircd.log" },
99 >  { "pidfile",    &pidFileName,
100 >   STRING, "File to use for process ID" },
101 >  { "foreground", &server_state.foreground,
102 >   YESNO, "Run in foreground (don't detach)" },
103 >  { "version",    &printVersion,
104 >   YESNO, "Print version and exit" },
105 >  { "help", NULL, USAGE, "Print this text" },
106 >  { NULL, NULL, STRING, NULL },
107   };
108  
109   static struct event event_cleanup_tklines =
# Line 126 | Line 141 | struct event event_write_links_file =
141   };
142  
143  
129 /*
130 * print_startup - print startup information
131 */
132 static void
133 print_startup(int pid)
134 {
135  printf("ircd: version %s(%s)\n", ircd_version, serno);
136  printf("ircd: pid %d\n", pid);
137  printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
138         : "foreground", ConfigGeneral.dpath);
139 }
140
141 static void
142 make_daemon(void)
143 {
144  int pid;
145
146  if ((pid = fork()) < 0)
147  {
148    perror("fork");
149    exit(EXIT_FAILURE);
150  }
151  else if (pid > 0)
152  {
153    print_startup(pid);
154    exit(EXIT_SUCCESS);
155  }
156
157  setsid();
158 }
159
160 static int printVersion = 0;
161
162 static struct lgetopt myopts[] =
163 {
164  {"configfile", &ConfigGeneral.configfile,
165   STRING, "File to use for ircd.conf"},
166  {"glinefile",  &ConfigGeneral.glinefile,
167   STRING, "File to use for gline database"},
168  {"klinefile",  &ConfigGeneral.klinefile,
169   STRING, "File to use for kline database"},
170  {"dlinefile",  &ConfigGeneral.dlinefile,
171   STRING, "File to use for dline database"},
172  {"xlinefile",  &ConfigGeneral.xlinefile,
173   STRING, "File to use for xline database"},
174  {"resvfile",  &ConfigGeneral.resvfile,
175   STRING, "File to use for resv database"},
176  {"logfile",    &logFileName,
177   STRING, "File to use for ircd.log"},
178  {"pidfile",    &pidFileName,
179   STRING, "File to use for process ID"},
180  {"foreground", &server_state.foreground,
181   YESNO, "Run in foreground (don't detach)"},
182  {"version",    &printVersion,
183   YESNO, "Print version and exit"},
184  {"help", NULL, USAGE, "Print this text"},
185  {NULL, NULL, STRING, NULL},
186 };
187
144   void
145   set_time(void)
146   {
# Line 192 | Line 148 | set_time(void)
148  
149    if (gettimeofday(&newtime, NULL) == -1)
150    {
151 <    ilog(LOG_TYPE_IRCD, "Clock Failure (%s), TS can be corrupted",
152 <         strerror(errno));
153 <    sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
154 <                         "Clock Failure (%s), TS can be corrupted",
155 <                         strerror(errno));
200 <    server_die("Clock Failure", 1);
151 >    char buf[IRCD_BUFSIZE];
152 >
153 >    snprintf(buf, sizeof(buf), "Clock failure, TS can be corrupted: %s",
154 >             strerror(errno));
155 >    server_die(buf, SERVER_SHUTDOWN);
156    }
157  
158    if (newtime.tv_sec < CurrentTime)
159    {
160 <    ilog(LOG_TYPE_IRCD, "System clock is running backwards - (%lu < %lu)",
161 <         (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
160 >    ilog(LOG_TYPE_IRCD, "System clock is running backwards - (%ju < %ju)",
161 >         newtime.tv_sec, CurrentTime);
162      sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE,
163 <                         "System clock is running backwards - (%lu < %lu)",
164 <                         (unsigned long)newtime.tv_sec,
165 <                         (unsigned long)CurrentTime);
211 <    set_back_events(CurrentTime - newtime.tv_sec);
163 >                         "System clock is running backwards - (%ju < %ju)",
164 >                         newtime.tv_sec, CurrentTime);
165 >    event_set_back_events(CurrentTime - newtime.tv_sec);
166    }
167  
168 <  SystemTime.tv_sec  = newtime.tv_sec;
168 >  SystemTime.tv_sec = newtime.tv_sec;
169    SystemTime.tv_usec = newtime.tv_usec;
170   }
171  
# Line 234 | Line 188 | io_loop(void)
188      exit_aborted_clients();
189      free_exited_clients();
190  
191 <    /* Check to see whether we have to rehash the configuration .. */
191 >    /* Check to see whether we have to rehash the configuration. */
192      if (dorehash)
193      {
194 <      rehash(1);
194 >      conf_rehash(1);
195        dorehash = 0;
196      }
197  
198      if (doremotd)
199      {
200        motd_recache();
201 <      sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
201 >      sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
202                             "Got signal SIGUSR1, reloading motd file(s)");
203        doremotd = 0;
204      }
# Line 260 | Line 214 | io_loop(void)
214   static void
215   initialize_global_set_options(void)
216   {
217 <  GlobalSetOptions.autoconn  = 1;
217 >  GlobalSetOptions.maxclients = ConfigServerInfo.default_max_clients;
218 >  GlobalSetOptions.autoconn = 1;
219    GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
220 <  GlobalSetOptions.spam_num  = MAX_JOIN_LEAVE_COUNT;
221 <
222 <  if (ConfigGeneral.default_floodcount)
223 <    GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount;
269 <  else
270 <    GlobalSetOptions.floodcount = 10;
271 <
272 <  /* XXX I have no idea what to try here - Dianora */
273 <  GlobalSetOptions.joinfloodcount = 16;
274 <  GlobalSetOptions.joinfloodtime = 8;
275 <
276 <  split_servers = ConfigChannel.default_split_server_count;
277 <  split_users   = ConfigChannel.default_split_user_count;
278 <
279 <  if (split_users && split_servers && (ConfigChannel.no_create_on_split ||
280 <                                       ConfigChannel.no_join_on_split))
281 <  {
282 <    splitmode     = 1;
283 <    splitchecking = 1;
284 <  }
285 <
220 >  GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
221 >  GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount;
222 >  GlobalSetOptions.joinfloodcount = ConfigChannel.default_join_flood_count;
223 >  GlobalSetOptions.joinfloodtime = ConfigChannel.default_join_flood_time;
224    GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
225   }
226  
# Line 294 | Line 232 | initialize_global_set_options(void)
232   static void
233   initialize_server_capabs(void)
234   {
235 <  add_capability("QS", CAP_QS, 1);
236 <  add_capability("EOB", CAP_EOB, 1);
237 <  add_capability("TS6", CAP_TS6, 0);
238 <  add_capability("CLUSTER", CAP_CLUSTER, 1);
239 <  add_capability("SVS", CAP_SVS, 1);
240 <  add_capability("CHW", CAP_CHW, 1);
303 <  add_capability("HOPS", CAP_HOPS, 1);
235 >  add_capability("QS", CAPAB_QS);
236 >  add_capability("EOB", CAPAB_EOB);
237 >  add_capability("CLUSTER", CAPAB_CLUSTER);
238 >  add_capability("SVS", CAPAB_SVS);
239 >  add_capability("CHW", CAPAB_CHW);
240 >  add_capability("HOPS", CAPAB_HOPS);
241   }
242  
243   /* write_pidfile()
# Line 316 | Line 253 | write_pidfile(const char *filename)
253  
254    if ((fb = fopen(filename, "w")))
255    {
256 <    char buff[IRCD_BUFSIZE];
256 >    char buf[IRCD_BUFSIZE];
257      unsigned int pid = (unsigned int)getpid();
258  
259 <    snprintf(buff, sizeof(buff), "%u\n", pid);
259 >    snprintf(buf, sizeof(buf), "%u\n", pid);
260  
261 <    if (fputs(buff, fb) == -1)
262 <      ilog(LOG_TYPE_IRCD, "Error writing %u to pid file %s (%s)",
263 <           pid, filename, strerror(errno));
261 >    if (fputs(buf, fb) == -1)
262 >      ilog(LOG_TYPE_IRCD, "Error writing to pid file %s: %s",
263 >           filename, strerror(errno));
264  
265      fclose(fb);
266    }
267    else
268 <  {
332 <    ilog(LOG_TYPE_IRCD, "Error opening pid file %s (%s)",
268 >    ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
269           filename, strerror(errno));
334  }
270   }
271  
272   /* check_pidfile()
# Line 346 | Line 281 | static void
281   check_pidfile(const char *filename)
282   {
283    FILE *fb;
284 <  char buff[IRCD_BUFSIZE];
350 <  pid_t pidfromfile;
284 >  char buf[IRCD_BUFSIZE];
285  
352  /* Don't do logging here, since we don't have log() initialised */
286    if ((fb = fopen(filename, "r")))
287    {
288 <    if (!fgets(buff, 20, fb))
289 <    {
290 <      /* log(L_ERROR, "Error reading from pid file %s (%s)", filename,
358 <       * strerror(errno));
359 <       */
360 <    }
288 >    if (!fgets(buf, 20, fb))
289 >      ilog(LOG_TYPE_IRCD, "Error reading from pid file %s: %s",
290 >           filename, strerror(errno));
291      else
292      {
293 <      pidfromfile = atoi(buff);
293 >      pid_t pid = atoi(buf);
294  
295 <      if (!kill(pidfromfile, 0))
295 >      if (!kill(pid, 0))
296        {
297          /* log(L_ERROR, "Server is already running"); */
298          printf("ircd: daemon is already running\n");
299 <        exit(-1);
299 >        exit(EXIT_FAILURE);
300        }
301      }
302  
303      fclose(fb);
304    }
305    else if (errno != ENOENT)
306 <  {
307 <    /* log(L_ERROR, "Error opening pid file %s", filename); */
378 <  }
306 >    ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
307 >         filename, strerror(errno));
308   }
309  
310   /* setup_corefile()
# Line 408 | Line 337 | always_accept_verify_cb(int preverify_ok
337   }
338   #endif
339  
340 < /* init_ssl()
340 > /* ssl_init()
341   *
342   * inputs       - nothing
343   * output       - nothing
# Line 470 | Line 399 | ssl_init(void)
399   #endif /* HAVE_LIBCRYPTO */
400   }
401  
402 + /*
403 + * print_startup - print startup information
404 + */
405 + static void
406 + print_startup(int pid)
407 + {
408 +  printf("ircd: version %s(%s)\n", ircd_version, serno);
409 +  printf("ircd: pid %d\n", pid);
410 +  printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
411 +         : "foreground", ConfigGeneral.dpath);
412 + }
413 +
414 + static void
415 + make_daemon(void)
416 + {
417 +  int pid;
418 +
419 +  if ((pid = fork()) < 0)
420 +  {
421 +    perror("fork");
422 +    exit(EXIT_FAILURE);
423 +  }
424 +  else if (pid > 0)
425 +  {
426 +    print_startup(pid);
427 +    exit(EXIT_SUCCESS);
428 +  }
429 +
430 +  setsid();
431 + }
432 +
433   int
434   main(int argc, char *argv[])
435   {
# Line 483 | Line 443 | main(int argc, char *argv[])
443    /* Setup corefile size immediately after boot -kre */
444    setup_corefile();
445  
446 <  /* save server boot time right away, so getrusage works correctly */
446 >  /* Save server boot time right away, so getrusage works correctly */
447    set_time();
448  
449 <  /* It ain't random, but it ought to be a little harder to guess */
449 >  /* It's not random, but it ought to be a little harder to guess */
450    init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
451  
492  me.connection = &meConnection;
493  dlinkAdd(&me, &me.node, &global_client_list);  /* Pointer to beginning
494                                                   of Client list */
495  ConfigLog.use_logging = 1;
452    ConfigGeneral.dpath      = DPATH;
453    ConfigGeneral.spath      = SPATH;
454    ConfigGeneral.mpath      = MPATH;
455    ConfigGeneral.configfile = CPATH;    /* Server configuration file */
456    ConfigGeneral.klinefile  = KPATH;    /* Server kline file         */
501  ConfigGeneral.glinefile  = GPATH;    /* Server gline file         */
457    ConfigGeneral.xlinefile  = XPATH;    /* Server xline file         */
458    ConfigGeneral.dlinefile  = DLPATH;   /* dline file                */
459    ConfigGeneral.resvfile   = RESVPATH; /* resv file                 */
460  
461    myargv = argv;
462 <  umask(077);                /* better safe than sorry --SRB */
462 >  umask(077);  /* umask 077: u=rwx,g=,o= */
463  
464    parseargs(&argc, &argv, myopts);
465  
# Line 543 | Line 498 | main(int argc, char *argv[])
498  
499    mp_pool_init();
500    init_dlink_nodes();
501 <  init_isupport();
501 >  isupport_init();
502    dbuf_init();
503    hash_init();
504 +  userhost_init();
505    ipcache_init();
506    client_init();
507    class_init();
# Line 555 | Line 511 | main(int argc, char *argv[])
511    init_resolver();      /* Needs to be setup before the io loop */
512    modules_init();
513    read_conf_files(1);   /* cold start init conf files */
558  init_uid();
514    initialize_server_capabs();   /* Set up default_server_capabs */
515 <  initialize_global_set_options();
515 >  initialize_global_set_options();  /* Has to be called after read_conf_files() */
516    channel_init();
517    read_links_file();
518    motd_init();
519 +  user_modes_init();
520   #ifdef HAVE_LIBGEOIP
521    geoip_ctx = GeoIP_new(GEOIP_MEMORY_CACHE);
522   #endif
523  
568  if (EmptyString(ConfigServerInfo.sid))
569  {
570    ilog(LOG_TYPE_IRCD, "ERROR: No server id specified in serverinfo block.");
571    exit(EXIT_FAILURE);
572  }
573
574  strlcpy(me.id, ConfigServerInfo.sid, sizeof(me.id));
575
524    if (EmptyString(ConfigServerInfo.name))
525    {
526      ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
# Line 581 | Line 529 | main(int argc, char *argv[])
529  
530    strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name));
531  
532 <  /* serverinfo{} description must exist.  If not, error out.*/
532 >  /* serverinfo {} description must exist.  If not, error out.*/
533    if (EmptyString(ConfigServerInfo.description))
534    {
535      ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
# Line 590 | Line 538 | main(int argc, char *argv[])
538  
539    strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info));
540  
541 <  me.from                   = &me;
542 <  me.servptr                = &me;
543 <  me.connection->lasttime  = CurrentTime;
544 <  me.connection->since     = CurrentTime;
541 >  if (EmptyString(ConfigServerInfo.sid))
542 >  {
543 >    ilog(LOG_TYPE_IRCD, "Generating server ID");
544 >    generate_sid();
545 >  }
546 >  else
547 >    strlcpy(me.id, ConfigServerInfo.sid, sizeof(me.id));
548 >
549 >  init_uid();
550 >
551 >  me.from = &me;
552 >  me.servptr = &me;
553 >  me.connection->lasttime = CurrentTime;
554 >  me.connection->since = CurrentTime;
555    me.connection->firsttime = CurrentTime;
556  
557    SetMe(&me);
# Line 603 | Line 561 | main(int argc, char *argv[])
561    hash_add_client(&me);
562  
563    dlinkAdd(&me, make_dlink_node(), &global_server_list);
564 +  dlinkAdd(&me, &me.node, &global_client_list);
565  
566 <  load_kline_database();
567 <  load_dline_database();
568 <  load_gline_database();
569 <  load_xline_database();
611 <  load_resv_database();
612 <
613 <  if (chdir(MODPATH))
614 <  {
615 <    ilog(LOG_TYPE_IRCD, "Could not load core modules. Terminating!");
616 <    exit(EXIT_FAILURE);
617 <  }
566 >  load_kline_database(ConfigGeneral.klinefile);
567 >  load_dline_database(ConfigGeneral.dlinefile);
568 >  load_xline_database(ConfigGeneral.xlinefile);
569 >  load_resv_database(ConfigGeneral.resvfile);
570  
571    load_all_modules(1);
572    load_conf_modules();
573    load_core_modules(1);
574  
623  /* Go back to DPATH after checking to see if we can chdir to MODPATH */
624  if (chdir(ConfigGeneral.dpath))
625  {
626    perror("chdir");
627    exit(EXIT_FAILURE);
628  }
629
630  /*
631   * assemble_umode_buffer() has to be called after
632   * reading conf/loading modules.
633   */
634  assemble_umode_buffer();
635
575    write_pidfile(pidFileName);
576  
638  ilog(LOG_TYPE_IRCD, "Server Ready");
639
640  event_addish(&event_cleanup_glines, NULL);
577    event_addish(&event_cleanup_tklines, NULL);
578  
579    /* We want try_connections to be called as soon as possible now! -- adrian */
# Line 649 | Line 585 | main(int argc, char *argv[])
585  
586    event_addish(&event_save_all_databases, NULL);
587  
588 <  if (ConfigServerHide.links_delay > 0)
588 >  if (ConfigServerHide.flatten_links_delay && event_write_links_file.active == 0)
589    {
590 <    event_write_links_file.when = ConfigServerHide.links_delay;
591 <    event_addish(&event_write_links_file, NULL);
590 >    event_write_links_file.when = ConfigServerHide.flatten_links_delay;
591 >    event_add(&event_write_links_file, NULL);
592    }
657  else
658    ConfigServerHide.links_disabled = 1;
659
660  if (splitmode)
661    event_addish(&splitmode_event, NULL);
593  
594 +  ilog(LOG_TYPE_IRCD, "Server Ready");
595    io_loop();
596 +
597    return 0;
598   }

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)