Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove daemon mode support #345

Merged
merged 1 commit into from
Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions other/antimicrox.1
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ start joysticks on a specific set. Value can be a controller index, name, or GUI
\fB\-\-next\fR
Advance profile loading set options.
.TP
\fB\-d\fR, \fB\-\-daemon\fR
launch program as a daemon.
.TP
\fB\-\-log\-level\fR \fI{debug,info,warn}\fR
Enable logging. Default: warn
.TP
Expand Down
8 changes: 0 additions & 8 deletions src/commandlineutility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ CommandLineUtility::CommandLineUtility(QObject *parent)
showRequest = false;
unloadProfile = false;
startSetNumber = 0;
daemonMode = false;
displayString = "";
listControllers = false;
mappingController = false;
Expand Down Expand Up @@ -87,7 +86,6 @@ void CommandLineUtility::parseArguments(const QApplication &parsed_app)
QCoreApplication::translate("main", "Start joysticks on a specific set. Value can be a "
"controller index, name, or GUID"),
QCoreApplication::translate("main", "number value")},
{{"daemon", "d"}, QCoreApplication::translate("main", "Launch program as a daemon. Use only on Linux.")},
{"log-level",
QCoreApplication::translate("main", "Enable logging. Levels (from the least strict): warn,info,verbose,debug"),
QCoreApplication::translate("main", "log-type")},
Expand Down Expand Up @@ -184,10 +182,6 @@ void CommandLineUtility::parseArguments(const QApplication &parsed_app)
parseArgsMap(parser);
}

if (parser.isSet("daemon"))
{
daemonMode = true;
}
#ifdef WITH_X11
if (parser.isSet("display"))
{
Expand Down Expand Up @@ -462,8 +456,6 @@ bool CommandLineUtility::shouldMapController() { return mappingController; }

QString CommandLineUtility::getEventGenerator() { return eventGenerator; }

bool CommandLineUtility::launchAsDaemon() { return daemonMode; }

QString CommandLineUtility::getDisplayString() { return displayString; }

Logger::LogLevel CommandLineUtility::getCurrentLogLevel() { return currentLogLevel; }
Expand Down
2 changes: 0 additions & 2 deletions src/commandlineutility.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ class CommandLineUtility : public QObject
QList<int> *getJoyStartSetNumberList();
QList<ControllerOptionsInfo> const &getControllerOptionsList();

bool launchAsDaemon();
QString getDisplayString();

Logger::LogLevel getCurrentLogLevel();
Expand All @@ -129,7 +128,6 @@ class CommandLineUtility : public QObject
bool hiddenRequest;
bool showRequest;
bool unloadProfile;
bool daemonMode;
bool listControllers;
bool mappingController;

Expand Down
151 changes: 16 additions & 135 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@

#include <execinfo.h>

#include <sys/stat.h>
#include <sys/types.h>

#ifdef WITH_X11
#include "x11extras.h"
#endif
Expand Down Expand Up @@ -385,152 +382,36 @@ int main(int argc, char *argv[])
LocalAntiMicroServer *localServer = nullptr;

#ifdef Q_OS_UNIX
if (cmdutility.launchAsDaemon())
{
pid_t pid, sid;
pid = fork(); // Fork the Parent Process

if (pid == 0)
{
PRINT_STDOUT() << QObject::tr("Daemon launched");

localServer = new LocalAntiMicroServer();
localServer->startLocalServer();
} else if (pid < 0)
{
qCritical() << QObject::tr("Failed to launch daemon");

deleteInputDevices(joysticks);
delete joysticks;
joysticks = nullptr;

return 1;
} else if (pid > 0) // We got a good pid, Close the Parent Process
{
PRINT_STDOUT() << QObject::tr("Launching daemon") << " PID: " << pid << "\n";

deleteInputDevices(joysticks);
delete joysticks;
joysticks = nullptr;

return 0;
}
localServer = new LocalAntiMicroServer();
localServer->startLocalServer();

#ifdef WITH_X11

if (QApplication::platformName() == QStringLiteral("xcb"))
{
if (cmdutility.getDisplayString().isEmpty())
{
X11Extras::getInstance()->syncDisplay();
} else
{
X11Extras::setCustomDisplay(cmdutility.getDisplayString());
X11Extras::getInstance()->syncDisplay();

if (X11Extras::getInstance()->display() == nullptr)
{
qCritical() << QObject::tr("Display string \"%1\" is not valid.").arg(cmdutility.getDisplayString());

deleteInputDevices(joysticks);
delete joysticks;
joysticks = nullptr;

delete localServer;
localServer = nullptr;

X11Extras::getInstance()->closeDisplay();

exit(EXIT_FAILURE);
}
}
}

#endif

umask(0); // Change File Mask
sid = setsid(); // Create a new Signature Id for our child

if (sid < 0)
if (QApplication::platformName() == QStringLiteral("xcb"))
{
if (!cmdutility.getDisplayString().isEmpty())
{
qCritical() << QObject::tr("Failed to set a signature id for the daemon");

deleteInputDevices(joysticks);
delete joysticks;
joysticks = nullptr;

delete localServer;
localServer = nullptr;
X11Extras::getInstance()->syncDisplay(cmdutility.getDisplayString());

#ifdef WITH_X11
if (QApplication::platformName() == QStringLiteral("xcb"))
if (X11Extras::getInstance()->display() == nullptr)
{
X11Extras::getInstance()->closeDisplay();
}
#endif

exit(EXIT_FAILURE);
}

if ((chdir("/")) < 0)
{
qCritical() << QObject::tr("Failed to change working directory to /");
qCritical() << QObject::tr("Display string \"%1\" is not valid.").arg(cmdutility.getDisplayString());

deleteInputDevices(joysticks);
delete joysticks;
joysticks = nullptr;
deleteInputDevices(joysticks);
delete joysticks;
joysticks = nullptr;

delete localServer;
localServer = nullptr;
delete localServer;
localServer = nullptr;

#ifdef WITH_X11

if (QApplication::platformName() == QStringLiteral("xcb"))
{
X11Extras::getInstance()->closeDisplay();
}
#endif

exit(EXIT_FAILURE);
}

// Close Standard File Descriptors
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
} else
{
localServer = new LocalAntiMicroServer();
localServer->startLocalServer();

#ifdef WITH_X11

if (QApplication::platformName() == QStringLiteral("xcb"))
{
if (!cmdutility.getDisplayString().isEmpty())
{
X11Extras::getInstance()->syncDisplay(cmdutility.getDisplayString());

if (X11Extras::getInstance()->display() == nullptr)
{
qCritical() << QObject::tr("Display string \"%1\" is not valid.").arg(cmdutility.getDisplayString());

deleteInputDevices(joysticks);
delete joysticks;
joysticks = nullptr;

delete localServer;
localServer = nullptr;

X11Extras::getInstance()->closeDisplay();

exit(EXIT_FAILURE);
}
exit(EXIT_FAILURE);
}
}

#endif
}
#endif

#elif defined(Q_OS_WIN)
localServer = new LocalAntiMicroServer();
localServer->startLocalServer();
Expand Down