summaryrefslogtreecommitdiffstats
path: root/src/pvsDaemon.cpp
blob: b23bc5745e41941a8ac49af12cdce33a6d61d92c (plain) (blame)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <QCoreApplication>
#include <getopt.h>
#include <stdlib.h>
#include "pvs.h"
#include "src/net/pvsLocalhostCommunicator.h"
#include "src/util/pvsSettingsManager.h"
#include "setup.h"
#include "src/net/pvsMsg.h"
#include "src/core/pvsChatClient.h"

PVS *mainClient = NULL;
QTextStream qout(stdout);

/// VERSION_STRING is defined in src/version.h
void printVersion(bool doExit)
{
    QTextStream qout(stdout);
    printf("Version:\t"VERSION_STRING"\n");
    if (doExit)
        exit(0);
}

/// outputs the full help text
void printHelp()
{
    qout << "**************************************************************\n";
    qout << "Pool Video Switch Client\n";
    qout << "**************************************************************\n";
    qout << QObject::tr("Version: ") << VERSION_STRING << "\n";
    qout << "**************************************************************\n";
    qout << QObject::tr("Usage:") << "\tpoolVSClient " << QObject::tr("<<option> <value>, ... >") << "\n\n";
    qout << QObject::tr("Options:") << "\n\n";
    qout << QObject::tr("-vncScriptFile <fullpath\\filename>") << "\n\t" << QObject::tr("Specifies a custom location for the vnc-start/stop-script.")<< "\n\t" << QObject::tr("If not specified, /usr/bin/pvs-vncsrv is expected.") << "\n\n";
    qout << QObject::tr("-freq <seconds>") << "\n\t" << QObject::tr("Specifies how long to wait until a reconnection attempt is made.") << "\n\t" << QObject::tr("Default is 5.") << "\n\n";
    qout << QObject::tr("-port <port>") << "\n\t" << QObject::tr("Specifies on which port to run.") << "\n\t" << QObject::tr("Default is ") << SERVER_PORT_INT << ".\n\n";
    qout << QObject::tr("-h or --help") << "\n\t" << QObject::tr("Shows this help text and exits.") << "\n\n";
    qout << QObject::tr("-v or --version") << "\n\t" << QObject::tr("Shows the current version and exits.") << "\n";
    qout << QObject::tr("-d or --daemon") << "\n\t" << QObject::tr("Start as daemon.") << "\n";
    qout << QObject::tr("-c <string command>:<string value>") << "\n\t" << QObject::tr("Sends the command and the optional value to a running PVS-Client.") << "\n\t" << QObject::tr("Command and value may not contain spaces or colons.") << "\n\t" << QObject::tr("The dividing colon is mandatory.") << "\n";
    qout << "-commands: \n\t" << QObject::tr("Prints out available commands to use with -c.") << "\n";
    qout << "**************************************************************\n";
    qout.flush();
    exit(0);
}

/// outputs a brief help text
void printNotice()
{
    qout << QObject::tr("Use -h or --help to get a listing of all options.\n-v or --version gives you the current version.\n\n");
    qout.flush();
}

void printCommands()
{
    printf("**************************************************************\n");
    printf("chat:<on/ off/ 1/ 2/ 3>\n ");
    printf("on = turns that chat option on\n off = turns the chat option off\n 1 = bossmode\n 2 = community\n 3 = private\n");
    printf("stop \n\t Stops the running PVS-Client.\n");
    printf("**************************************************************\n");
    exit(0);
}

///
int main(int argc, char** argv)
{
    bool daemon = false;
    int frequency = 5;
    int port = -1;

    QFileInfo script;
    #ifdef __WIN32__
        //TODO Win32
        if (!script.exists())
            script.setFile(getHomeDir() + "/.pvs/pvs-vncsrv_win32.bat");
    #else
        script.setFile("/usr/bin/pvs-vncsrv");
        if (!script.exists())
            script.setFile("/usr/local/bin/pvs-vncsrv");
        if (!script.exists())
            script.setFile(getHomeDir() + "/.pvs/pvs-vncsrv");
    #endif //__WIN32__

    QCoreApplication app(argc, argv);
    app.setOrganizationName("openslx");
    app.setOrganizationDomain("openslx.org");
    app.setApplicationName("pvs");
    // use system locale as language to translate gui
    QTranslator translator;
    translator.load(":pvs");
    app.installTranslator(&translator);


    PVSLocalhostCommunicator com(getPolicyFilePath(QString(
                                     ".comfile")));
    com.run();
    if (!(com.running()))
    {
        printf("Error. UDS Communicator is not running. Exiting.\n");
        exit(0);
    }

    int option_index = 0;

    while (1)
    {
        static struct option long_options[] =
        {
            { "help", no_argument, 0, 'h' },
            { "commands", no_argument, 0, 'o' },
            { "version", no_argument, 0, 'v' },
            { "daemon", no_argument, 0, 'd' },
            { "port", required_argument, 0, 'p' },
            { "freq", required_argument, 0, 'f' },
            { "client", required_argument, 0, 'e' },
            { "script", required_argument, 0, 's' },
            { 0, 0, 0, 0 },
        };
        /* getopt_long stores the option index here. */

        int c = getopt_long(argc, argv, "hovdc:f:e:s:p:", long_options,
                            &option_index);
        option_index++;
        if (c == -1)
            break;

        switch (c)
        {
        case 'h':
            printHelp();
            break;
        case 'o':
            printCommands();
            break;
        case 'v':
            printVersion(true);
            break;
        case 'd':
            daemon = true;
            break;
        case 'c':
        {
            if (option_index + 1 < argc)
            {
                if (com.server())
                {
                    // wont work, no daemon running
                    printf("Error. No running PVS-Client found. Exiting.\n");
                    com.stop();
                }
                else
                {
                    QStringList line = QString::fromLocal8Bit(argv[option_index
                                       + 1]).split(":");
                    QString &ident = line[0];
                    QString message;
                    if (line.size() > 1)
                        message = line[1];
                    if (ident.size() > 0) // no use sending empty idents... not even sure if this COULD happen
                    {
                        bool flag = false;
                        if (ident == "make")
                            PVSSettingsManager::getManager()->setConfigs();
                        else if (ident == "chat" && message == "boss")
                        {
                            PVSSettingsManager::getManager()->reWriteConfigs("chatmode", "bossmode");
                            flag = true;

                        }
                        else if (ident == "chat" && message == "all")
                        {
                            PVSSettingsManager::getManager()->reWriteConfigs("chatmode", "community");
                            flag = true;
                        }
                        else if (ident == "chat" && message == "private")
                        {
                            PVSSettingsManager::getManager()->reWriteConfigs("chatmode", "private");
                            flag = true;
                        }
                        else if (ident == "chat" && message == "on")
                        {
                            PVSSettingsManager::getManager()->reWriteConfigs("chatstate", "on");
                            flag = true;
                        }
                        else if (ident == "chat" && message == "off")
                        {
                            PVSSettingsManager::getManager()->reWriteConfigs("chatstate", "off");
                            flag = true;
                        }
                        else if (ident == "room")
                        {
                            //PVSSettingsManager::getManager()->reWriteConfigs(ident, message);
                            flag = true;
                        }
                        else if (ident == "stop" && message == NULL)
                            flag = true;
                        else
                            printf("option %s doesn't exist\n", ident.toUtf8().data());

                        if (flag)
                        {
                            qDebug("Will send i: %s, m: %s\n", ident.toUtf8().data(), message.toUtf8().data());
                            com.sendCommand(ident, message);
                            QCoreApplication::processEvents(QEventLoop::AllEvents);
                            printf("Sent command. Exiting.\n");
                        }
                    }

                    else
                        printf("Something went horribly wrong, since the ident is empty.\n");

                }
            }
            else
            {
                printf("Error. No command issued. Exiting.\n");
            }
            exit(0); // and gone
            break;
        }
        case 'f':
        {
            if (option_index + 1 < argc)
            {
                frequency = atoi(argv[option_index + 1]);
                if (frequency <= 0)
                {
                    frequency = 5; // return to standard
                    ConsoleLog writeError(QString(
                                              "malformed frequency, setting standard = 5"));
                    printNotice();
                }
                else
                    ConsoleLog writeLine(QString("Using frequency ").append(
                                             int2String(frequency)));
                option_index++;
                continue;
            }
            break;
        }
        case 'p':
        {
            if (option_index + 1 < argc)
            {
                port = atoi(argv[option_index + 1]);
                if (port <= 0)
                {
                    ConsoleLog writeTerminal(QString("port maybe malformed"));
                    printNotice();
                    port = -1; // return to standard
                }
                else
                    ConsoleLog writeTerminal(QString("Using port ").append(
                                                 int2String(port)));
                option_index++;
                continue;
            }
            break;
        }
        case 'e':
        {
            if (option_index + 1 < argc)
            {
                printf("WARNING: -e is deprecated\n");
                option_index++;
                continue;
            }
            break;
        }
        case 's':
        {
            if (option_index + 1 < argc)
            {
                script.setFile(QString(argv[option_index + 1]));
                option_index++;
                continue;
            }
            break;
        }
        case '?':
        {
            ConsoleLog writeError(
                QString("Unrecognized option/paramter: ").append(
                    argv[option_index]));
            printNotice();
        }
        default:
            abort();
        }
        //else
        //    printNotice();
    }

    if (!com.server())
    {
        printf("Error. PoolVSClient already running. Exiting\n");
        com.stop();
        exit(0);
    }

    ConsoleLog setLogName(QString("log.client"));
    ConsoleLog writeLine(QString("PVS-Client started."));

    if (daemon)
    {
        #ifndef __WIN32__
        pid_t pid, sid; // our process ID and session ID

        // fork off the parent process
        pid = fork();
        if (pid < 0)
            exit(-1);

        // if we got a good PID, then we can exit the parent process.
        if (pid > 0)
            exit(0);

        // change the file mode mask
        umask(0);

        // create a new SID for the child process
        sid = setsid();
        if (sid < 0)
            exit(-1);

        // change the current working directory
        if ((chdir("/")) < 0)
            exit(-1);

        // close out the standard file descriptors
        close(STDIN_FILENO);
        freopen ((QString(getHomeDir()).append(QString("/.pvs/dump"))).toUtf8()
                .data(),"w",stdout);
        //close(STDOUT_FILENO);
        close(STDERR_FILENO);
        #endif /*__WIN32__*/
    }

    mainClient = new PVS();
    PVSSettingsManager::getManager()->setConfigFile(getPolicyFilePath("clientconf"));
    com.getDispatcher()->addListener("*", mainClient, &PVS::onDaemonCommand);

    if (port <= 0)
        port = SERVER_PORT_INT;
    ConsoleLog writeLine(QString("TCP-Port: ").append(int2String(port)));
    mainClient->setScriptPath(script.filePath());
    ConsoleLog writeLine(QString("VNCScriptPath: ").append(script.filePath()));
    createPolicyDir();
    createPolicyFiles();

    //  long int gr = 0;
    mainClient->setScriptPath(script.filePath());
    app.exec();
    delete mainClient;

    return 0;
}