diff options
-rw-r--r-- | include/services.h | 1 | ||||
-rw-r--r-- | src/init.cpp | 66 | ||||
-rw-r--r-- | src/main.cpp | 20 | ||||
-rw-r--r-- | src/win32/anope_windows.h | 2 |
4 files changed, 64 insertions, 25 deletions
diff --git a/include/services.h b/include/services.h index 59f088d61..d34d858f1 100644 --- a/include/services.h +++ b/include/services.h @@ -40,6 +40,7 @@ #include <sys/stat.h> /* for umask() on some systems */ #include <sys/types.h> +#include <sys/wait.h> #include <fcntl.h> #include <typeinfo> diff --git a/src/init.cpp b/src/init.cpp index 39dbb1c81..cd2430f8b 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -158,16 +158,10 @@ static void remove_pidfile() static void write_pidfile() { - FILE *pidfile; - - pidfile = fopen(Config->PIDFilename.c_str(), "w"); + FILE *pidfile = fopen(Config->PIDFilename.c_str(), "w"); if (pidfile) { -#ifdef _WIN32 - fprintf(pidfile, "%d\n", static_cast<int>(GetCurrentProcessId())); -#else fprintf(pidfile, "%d\n", static_cast<int>(getpid())); -#endif fclose(pidfile); atexit(remove_pidfile); } @@ -239,11 +233,7 @@ bool AtTerm() void Fork() { #ifndef _WIN32 - int i = fork(); - if (i < 0) - Log(LOG_TERMINAL) << "Unable to fork, " << Anope::LastError(); - else if (i != 0) - exit(0); + kill(getppid(), SIGUSR2); if (isatty(fileno(stdout))) fclose(stdout); @@ -258,6 +248,32 @@ void Fork() #endif } +#ifndef _WIN32 +class SignalForkExit : public Signal +{ + public: + SignalForkExit() : Signal(SIGUSR2) { } + + void OnNotify() + { + exit(0); + } +}; + +class SignalSigChld : public Signal +{ + public: + SignalSigChld() : Signal(SIGCHLD) { } + + void OnNotify() + { + int status = 0; + wait(&status); + exit(status); + } +}; +#endif + void Init(int ac, char **av) { /* Set file creation mask and group ID. */ @@ -356,6 +372,9 @@ void Init(int ac, char **av) throw FatalException("Unable to chdir to " + services_dir + ": " + Anope::LastError()); } + /* Initialize the socket engine */ + SocketEngine::Init(); + init_core_messages(); Log(LOG_TERMINAL) << "Anope " << Anope::Version() << ", " << Anope::VersionBuildString(); @@ -380,17 +399,34 @@ void Init(int ac, char **av) throw FatalException("Configuration file failed to validate"); } - /* Initialize the socket engine */ - SocketEngine::Init(); - /* Create me */ Me = new Server(NULL, Config->ServerName, 0, Config->ServerDesc, Config->Numeric); #ifdef _WIN32 if (!SupportedWindowsVersion()) throw FatalException(GetWindowsVersion() + " is not a supported version of Windows"); +#else + if (!nofork) + { + int i = fork(); + if (i > 0) + { + new SignalForkExit(); + new SignalSigChld(); + while (!quitting) + { + Log(LOG_DEBUG_3) << "Top of fork() process loop"; + SocketEngine::Process(); + } + /* Should not be reached */ + exit(-1); + } + else if (i == -1) + Log() << "Error, unable to fork: " << Anope::LastError(); + } #endif + /* Write our PID to the PID file. */ write_pidfile(); diff --git a/src/main.cpp b/src/main.cpp index 279e9792a..26d4c29c0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -45,8 +45,9 @@ bool nofork = false; /* -nofork */ bool nothird = false; /* -nothrid */ bool noexpire = false; /* -noexpire */ bool protocoldebug = false; /* -protocoldebug */ - Anope::string binary_dir; /* Used to store base path for Anope */ +static int return_code = 0; + #ifdef _WIN32 # include <process.h> # define execve _execve @@ -144,7 +145,10 @@ UplinkSocket::~UplinkSocket() Me->SetFlag(SERVER_SYNCING); if (AtTerm()) + { quitting = true; + return_code = -1; + } else if (!quitting) { int Retry = Config->RetryWait; @@ -282,29 +286,25 @@ Anope::string GetFullProgDir(const Anope::string &argv0) int main(int ac, char **av, char **envp) { - int return_code = 0; - char cwd[PATH_MAX] = ""; -#ifdef _WIN32 - GetCurrentDirectory(PATH_MAX, cwd); -#else getcwd(cwd, PATH_MAX); -#endif orig_cwd = cwd; #ifndef _WIN32 /* If we're root, issue a warning now */ if (!getuid() && !getgid()) { - fprintf(stderr, "WARNING: You are currently running Anope as the root superuser. Anope does not\n"); - fprintf(stderr, " require root privileges to run, and it is discouraged that you run Anope\n"); - fprintf(stderr, " as the root superuser.\n"); + std::cout << "WARNING: You are currently running Anope as the root superuser. Anope does not" << std::endl; + std::cout << " require root privileges to run, and it is discouraged that you run Anope" << std::endl; + std::cout << " as the root superuser." << std::endl; + sleep(3); } #endif binary_dir = GetFullProgDir(av[0]); if (binary_dir[binary_dir.length() - 1] == '.') binary_dir = binary_dir.substr(0, binary_dir.length() - 2); + #ifdef _WIN32 Anope::string::size_type n = binary_dir.rfind('\\'); services_dir = binary_dir.substr(0, n) + "\\data"; diff --git a/src/win32/anope_windows.h b/src/win32/anope_windows.h index db5f21300..a631a4a70 100644 --- a/src/win32/anope_windows.h +++ b/src/win32/anope_windows.h @@ -42,6 +42,8 @@ /* VS2008 hates having this define before its own */
#define vsnprintf _vsnprintf
#define stat _stat
+#define getcwd(x, y) GetCurrentDirectory(y, x)
+#define getpid GetCurrentProcessId
#define S_ISREG(x) ((x) & _S_IFREG)
#ifdef EINPROGRESS
# undef EINPROGRESS
|