summaryrefslogtreecommitdiffstats
path: root/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/main.cpp b/main.cpp
index 05a6e00..e00f8bb 100644
--- a/main.cpp
+++ b/main.cpp
@@ -29,6 +29,8 @@
#include <sys/un.h>
#include <unistd.h>
#include <fcntl.h>
+#include <signal.h>
+#include <sys/wait.h>
#define PID_FILE "/data/user/.appcontroller"
@@ -224,6 +226,7 @@ int main(int argc, char **argv)
bool useGDB = false;
bool useQML = false;
bool fireAndForget = false;
+ bool detach = false;
Utils::PortList range;
if (args.isEmpty()) {
@@ -280,6 +283,8 @@ int main(int argc, char **argv)
} else if (arg == "--version") {
printf("Appcontroller version: " GIT_VERSION "\nGit revision: " GIT_HASH "\n");
return 0;
+ } else if (arg == "--detach") {
+ detach = true;
} else {
args.prepend(arg);
break;
@@ -296,6 +301,11 @@ int main(int argc, char **argv)
return 1;
}
+ if (detach && (useGDB || useQML)) {
+ fprintf(stderr, "Detached debugging not possible. --detach and one of --useGDB, --useQML must not be used together.\n");
+ return 1;
+ }
+
if (useGDB) {
int port = findFirstFreePort(range);
if (port < 0) {
@@ -331,6 +341,38 @@ int main(int argc, char **argv)
return 1;
}
+ // daemonize
+ if (detach) {
+ pid_t rc = fork();
+ if (rc == -1) {
+ printf("fork failed\n");
+ return -1;
+ } else if (rc > 0) {
+ // parent
+ ::wait(NULL); // wait for the child to exit
+ return 0;
+ }
+
+ setsid();
+ chdir("/");
+ signal(SIGHUP, SIG_IGN);
+
+ // child
+ int devnull = open("/dev/null", O_RDWR);
+ if (devnull < 0)
+ return -1;
+ dup2(devnull, 0); // Replace file descriptors
+ dup2(devnull, 1);
+ dup2(devnull, 2);
+ rc = fork();
+ if (rc == -1)
+ return -1;
+ else if (rc > 0)
+ return 0;
+
+ // child
+ }
+
// Create QCoreApplication after parameter parsing to prevent printing evaluation
// message to terminal before QtCreator has parsed the output.
QCoreApplication app(argc, argv);