]> jfr.im git - solanum.git/blobdiff - ircd/ircd.c
Resolve shfit/reduce conflict in timespec production (#54)
[solanum.git] / ircd / ircd.c
index 3f13d82a22633969cbb000c905dfa0afac084dde..935aa4e9671fa067ba2d6101be9db175618df058 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  charybdis: A slightly useful ircd.
+ *  Solanum: a slightly advanced ircd
  *  ircd.c: Starts up and runs the ircd.
  *
  *  Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
 #include "authproc.h"
 #include "operhash.h"
 
-#ifndef _WIN32
-static int pip[2];
-#endif
-
 static void
 ircd_die_cb(const char *str) __attribute__((noreturn));
 
@@ -191,46 +187,6 @@ ircd_shutdown(const char *reason)
        exit(0);
 }
 
-/*
- * print_startup - print startup information
- */
-static void
-print_startup(int pid)
-{
-       inotice("now running in %s mode from %s as pid %d ...",
-               !server_state_foreground ? "background" : "foreground",
-               ConfigFileEntry.dpath, pid);
-
-#ifndef _WIN32
-       int fd = open("/dev/null", O_RDWR);
-
-       if (fd == -1) {
-               perror("open /dev/null");
-               exit(EXIT_FAILURE);
-       }
-
-       (void) dup2(fd, 0);
-       (void) dup2(fd, 1);
-       (void) dup2(fd, 2);
-       (void) close(fd);
-
-       /* let the parent process know the initialization was successful
-        * -- jilles */
-       if (!server_state_foreground)
-       {
-               /* GCC complains on Linux if we don't check the value of write pedantically.
-                * Technically you're supposed to check the value, yes, but it probably can't fail.
-                * No, casting to void is of no use to shut the warning up. You HAVE to use the value.
-                * --Elizfaox
-                */
-               if(write(pip[0], ".", 1) < 1)
-                       abort();
-
-               (void) close(pip[0]);
-       }
-#endif
-}
-
 /*
  * init_sys
  *
@@ -250,7 +206,7 @@ init_sys(void)
                if(maxconnections <= MAX_BUFFER)
                {
                        fprintf(stderr, "ERROR: Shell FD limits are too low.\n");
-                       fprintf(stderr, "ERROR: charybdis reserves %d FDs, shell limits must be above this\n", MAX_BUFFER);
+                       fprintf(stderr, "ERROR: solanum reserves %d FDs, shell limits must be above this\n", MAX_BUFFER);
                        exit(EXIT_FAILURE);
                }
                return;
@@ -259,16 +215,19 @@ init_sys(void)
        maxconnections = MAXCONNECTIONS;
 }
 
+#ifndef _WIN32
 static int
 make_daemon(void)
 {
-#ifndef _WIN32
-       int pid;
-       char c;
+       int pid, nullfd, fdx;
+
+       /* The below is approximately what daemon(1, 0) does, but
+          we need control over the parent after forking to print
+          the startup message -- Aaron */
 
-       if (pipe(pip) < 0)
+       if((nullfd = open("/dev/null", O_RDWR)) < 0)
        {
-               perror("pipe");
+               perror("open /dev/null");
                exit(EXIT_FAILURE);
        }
 
@@ -279,22 +238,24 @@ make_daemon(void)
        }
        else if(pid > 0)
        {
-               (void) close(pip[1]);
-               /* Wait for initialization to finish, successfully or
-                * unsuccessfully. Until this point the child may still
-                * write to stdout/stderr.
-                * -- jilles */
-               if (read(pip[0], &c, 1) > 0)
-                       exit(EXIT_SUCCESS);
-               else
-                       exit(EXIT_FAILURE);
+               inotice("now running in background mode from %s as pid %d ...",
+                       ConfigFileEntry.dpath, pid);
+
+               exit(EXIT_SUCCESS);
        }
 
-       (void) close(pip[0]);
+       for(fdx = 0; fdx <= 2; fdx++)
+               if (fdx != nullfd)
+                       (void) dup2(nullfd, fdx);
+
+       if(nullfd > 2)
+               (void) close(nullfd);
+
        (void) setsid();
-#endif
+
        return 0;
 }
+#endif
 
 static int printVersion = 0;
 
@@ -592,7 +553,7 @@ ircd_restart_cb(const char *str)
 /*
  * Why EXIT_FAILURE here?
  * Because if ircd_die_cb() is called it's because of a fatal
- * error inside libcharybdis, and we don't know how to handle the
+ * error inside libsolanum, and we don't know how to handle the
  * exception, so it is logical to return a FAILURE exit code here.
  *    --nenolod
  */
@@ -666,7 +627,7 @@ seed_random(void *unused)
  * Side Effects - this is where the ircd gets going right now
  */
 int
-charybdis_main(int argc, char * const argv[])
+solanum_main(int argc, char * const argv[])
 {
        int fd;
 
@@ -718,6 +679,7 @@ charybdis_main(int argc, char * const argv[])
        me.localClient = &meLocalUser;
 
        /* Make sure all lists are zeroed */
+       memset(&global_client_list, 0, sizeof(global_client_list));
        memset(&unknown_list, 0, sizeof(unknown_list));
        memset(&lclient_list, 0, sizeof(lclient_list));
        memset(&serv_list, 0, sizeof(serv_list));
@@ -747,15 +709,30 @@ charybdis_main(int argc, char * const argv[])
        if (testing_conf)
                server_state_foreground = true;
 
+#ifndef _WIN32
+       /* Make sure fd 0, 1 and 2 are in use -- jilles */
+       do
+       {
+               fd = open("/dev/null", O_RDWR);
+       } while (fd < 2 && fd != -1);
+       if (fd > 2)
+               close(fd);
+       else if (fd == -1)
+               exit(1);
+#endif
+
        /* Check if there is pidfile and daemon already running */
        if(!testing_conf)
        {
                check_pidfile(pidFileName);
 
-               if(!server_state_foreground)
-                       make_daemon();
                inotice("starting %s ...", ircd_version);
                inotice("%s", rb_lib_version());
+
+#ifndef _WIN32
+               if(!server_state_foreground)
+                       make_daemon();
+#endif
        }
 
        /* Init the event subsystem */
@@ -847,13 +824,6 @@ charybdis_main(int argc, char * const argv[])
                        ircd_ssl_ok = true;
        }
 
-       if (testing_conf)
-       {
-               fprintf(stderr, "\nConfig testing complete.\n");
-               fflush(stderr);
-               return 0;       /* Why? We want the launcher to exit out. */
-       }
-
        me.from = &me;
        me.servptr = &me;
        SetMe(&me);
@@ -867,6 +837,13 @@ charybdis_main(int argc, char * const argv[])
 
        construct_umodebuf();
 
+       if (testing_conf)
+       {
+               fprintf(stderr, "\nConfig testing complete.\n");
+               fflush(stderr);
+               return 0;       /* Why? We want the launcher to exit out. */
+       }
+
        check_class();
        write_pidfile(pidFileName);
        load_help();
@@ -889,7 +866,9 @@ charybdis_main(int argc, char * const argv[])
        if(splitmode)
                check_splitmode_ev = rb_event_add("check_splitmode", check_splitmode, NULL, 5);
 
-       print_startup(getpid());
+       if(server_state_foreground)
+               inotice("now running in foreground mode from %s as pid %d ...",
+                       ConfigFileEntry.dpath, getpid());
 
        rb_lib_loop(0);