diff options
author | Rainer Keller <rainer.keller@theqtcompany.com> | 2015-01-26 10:32:45 +0100 |
---|---|---|
committer | Rainer Keller <rainer.keller@theqtcompany.com> | 2015-01-30 10:03:43 +0200 |
commit | df46e63c4600afa118250967675c8f76398e90a6 (patch) | |
tree | 4ca71cb0197c4b69831eed695601d28381fd5a99 | |
parent | 86a3dc9c8eb18ef3139125f9df1a4f0bef601f48 (diff) |
Add detach option
This option allows to start applications using the regular appcontroller
startup but does not block the caller until the application finishes.
Change-Id: I954ecd58660f216f41597b04e8a4a01ca43d3d61
Reviewed-by: Christian Kandeler <christian.kandeler@theqtcompany.com>
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@theqtcompany.com>
-rw-r--r-- | main.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
@@ -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); |