1
0
mirror of https://github.com/anope/anope.git synced 2026-07-02 09:06:38 +02:00

Fork earlier in startup to prevent it messing up threads, if there are any

This commit is contained in:
Adam
2011-10-14 22:07:13 -04:00
parent 2504af7d0f
commit 28ca0e1007
4 changed files with 64 additions and 25 deletions
+1
View File
@@ -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>
+51 -15
View File
@@ -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();
+10 -10
View File
@@ -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";
+2
View File
@@ -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