aboutsummaryrefslogtreecommitdiffstats
path: root/linux_daemon/linuxdaemon.cpp
blob: 3f9e5658582ffd25c859d0aff53922581996bd84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* 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.
        if (strcmp(argv[1], "--version") == 0) {
                std::cout << "Qt License Daemon (licd) v" << DAEMON_VERSION << " "
                        << COPYRIGHT_TEXT << std::endl;
                return 0;
        }
        if (strcmp(argv[i], "--port") == 0) {
            if (i + 1 == argc) {
                all_ok = false;
                printf("No port number given");
                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", argv[i]);
            all_ok = false;
            break;
        }
    }
    if (!all_ok) {
        printf("\nUsage: qtlicenser [option] [value]\n");
        printf("Supported options are:\n");
        printf("  --port <port number>   : Specify TCP/IP server port. If omitted, default is used.\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";
}