aboutsummaryrefslogtreecommitdiffstats
path: root/src/linux_daemon/linuxdaemon.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/linux_daemon/linuxdaemon.cpp')
-rw-r--r--src/linux_daemon/linuxdaemon.cpp107
1 files changed, 107 insertions, 0 deletions
diff --git a/src/linux_daemon/linuxdaemon.cpp b/src/linux_daemon/linuxdaemon.cpp
new file mode 100644
index 0000000..0b8a10a
--- /dev/null
+++ b/src/linux_daemon/linuxdaemon.cpp
@@ -0,0 +1,107 @@
+/* Copyright (C) 2022 The Qt Company Ltd.
+ *
+ * SPDX-License-Identifier: GPL-3.0-only WITH Qt-GPL-exception-1.0
+*/
+#include <sys/stat.h>
+#include <iostream>
+#include <unistd.h>
+
+#include "licenser.h"
+#include "version.h"
+
+int startProcess(uint16_t tcpPort) {
+ // Fire it up
+ Licenser license(tcpPort);
+ std::cout << "listening..\n";
+ uint16_t count = 0;
+ while (1) {
+ count++;
+ try {
+ license.listen();
+ }
+ catch (...) {
+ std::cout << "ERROR: Qt License Daemon failed!!\n";
+ return -1;
+ }
+ }
+}
+
+int main(int argc, char *argv[])
+{
+ uint16_t tcpPort = 0;
+
+ // Get arguments
+ std::string message = "";
+ bool all_ok = true;
+ for (int i = 1; i < argc; i++) {
+ // If command-line parameter is "--nodaemon", start right away
+ // as normal CLI process (not as daemon)
+ if (strcmp(argv[1], "--nodaemon") == 0) {
+ int res = startProcess(0);
+ return res;
+ }
+ // If command-line parameter is "--version", show the version and exit.
+ else if (strcmp(argv[1], "--version") == 0) {
+ std::cout << "Qt License Daemon (qtlicd) v" << DAEMON_VERSION << " "
+ << COPYRIGHT_TEXT << std::endl;
+ return 0;
+ }
+ else if (strcmp(argv[i], "--port") == 0) {
+ if (i + 1 == argc) {
+ all_ok = false;
+ printf("No port number given\n");
+ break;
+ }
+ try {
+ tcpPort = std::stoi(argv[i+1]);
+ i++;
+ }
+ catch (...) {
+ all_ok = false;
+ printf("Invalid port - must be an integer\n");
+ break;
+ }
+ }
+ else if (strcmp(argv[i], "--help") == 0) {
+ all_ok = false;
+ break;
+ }
+ else {
+ printf("Invalid argument: %s\n", argv[i]);
+ all_ok = false;
+ break;
+ }
+ }
+ if (!all_ok) {
+ printf("\nUsage: qtlicd [option] [value]\n");
+ printf("Supported options are:\n");
+ printf(" --port <port number> : Specify TCP/IP server port. If omitted, default is used.\n");
+ printf(" --nodaemon : Run in non-daemon mode (in console like any other CLI app)\n");
+ printf(" --version : Version info\n");
+ printf(" --help : This help\n\n");
+ return -1;
+ }
+
+ printf("Starting daemon\n");
+
+ // Fork process
+ pid_t pid, sid;
+ pid = fork();
+ if (pid > 0) {
+ return 0;
+ }
+ else if (pid < 0) {
+ printf("Daemon process fork failed!\n");
+ return 1;
+ }
+ umask(0);
+ sid = setsid();
+ // Check session ID for the child process
+ if (sid < 0)
+ {
+ printf("Invalid session id for a child process\n");
+ return 1;
+ }
+ startProcess(tcpPort);
+ std::cout << "Started\n";
+}