aboutsummaryrefslogtreecommitdiffstats
path: root/linux_daemon/linuxdaemon.cpp
blob: c055e968e2ac3b11232779ea2eaf92fa489f7311 (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
// Copyright (C) 2022 The Qt Company Ltd.
//

#include "linuxdaemon.h"

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 (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");
        exit(EXIT_FAILURE);
    }

    printf("Starting daemon\n");

    // Fork process
    pid_t pid, sid;
    pid = fork();
    if (pid > 0) {
        exit(EXIT_SUCCESS);
    }
    else if (pid < 0) {
        printf("Daemon process fork failed!\n");
        exit(EXIT_FAILURE);
    }
    umask(0);
    sid = setsid();
    // Check session ID for the child process
    if (sid < 0)
    {
        printf("Invalid session id for a child process\n");
        exit(EXIT_FAILURE);
    }

    // Fire it up
    Licenser  *license = new Licenser(tcpPort);

    license->listen();
    /* TODO change above something like:
        license->listen();
        where listen() will be kind of event loop
    */

    printf("Successfully started Qt License Daemon\n");
    delete license;
}