Removed system function invocations and placed forked execl instead
[vlp.git] / src / kernel / kernel.cpp
index 2f45322ef2cd18c9d162b4814f49ad272bcc236c..8bd0311c246cbdd23b259fb30e221964dae41570 100644 (file)
@@ -29,7 +29,6 @@
 
 ************************************************************/
 
-
 #include <QtGui/QApplication>
 #include <QtGui/QMainWindow>
 #include <QtGui/QTextEdit>
@@ -50,7 +49,8 @@
 #include <qcursor.h>
 #include <QtCore/QSocketNotifier>
 #include <QtGui/QCloseEvent>
-#include <qdir.h>
+#include <QtCore/QDir>
+#include <QtCore/QProcess>
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -62,6 +62,7 @@
 #include <netinet/in.h>
 
 #include <libconfig.h>
+#include <X11/Xlib.h>
 
 #include "kernel.h"
 #include "kernelwindow.h"
@@ -73,9 +74,6 @@
 
 
 char CharLine[25] = "________________________";
-char myargs[5][255];
-
-//QApplication *app;
 
 /**
  * Event invoked on program close.
@@ -115,17 +113,29 @@ void QKernel::setLocked(bool locked)
  * Kernel program constructor.
  * Prepares everything to work.
  */
-QKernel::QKernel()
+QKernel::QKernel(int argc, char **argv)
 {
        setupUi(this);
 
-       QDir q(REMOTE_PATH);
-       char ss[255];
+       QString arg0(argv[0]);
+       arg0 += "/";
+       homeDir = QDir(arg0);
+       homeDir.cdUp();
+
+       
+       int i;
+       for(i = 0; (i < 5) && (i < argc-1); i++) {
+               strcpy(myargs[i], "");
+               if (i < argc) {
+                       strcpy(myargs[i], argv[i+1]);
+               }
+       }
+
+       QDir q(getRemoteDir());
 
        if (!q.exists()) {
-               sprintf(ss, "mkdir %s", REMOTE_PATH);
-               system(ss);
-       } 
+               q.mkpath(getRemoteDir());
+       }
 
        info_messages = TRUE;
        wait_for_info = FALSE;
@@ -138,36 +148,57 @@ QKernel::QKernel()
        freeINTid = 1;
        ActiveConnections = 0;
        strcpy(LockPasswd, "");
-       LoadConfig("vlp.cfg");
+       loadConfig(getConfigFilePath());
        RunNetModule();
 
        Net_Notify = new QSocketNotifier(net_sock, QSocketNotifier::Read, this);
        connect(Net_Notify, SIGNAL(activated(int)), this, SLOT(NetMessage()));
 }
 
-/**
- * Displays window with information about not implemented functionality.
- */
-void QKernel::n_impl()
+QString QKernel::getConfigFilePath()
+{
+       return homeDir.absoluteFilePath("vlp.cfg");
+}
+
+const char * QKernel::getHomeDir()
 {
-       QMessageBox::information(this, "Function info", "This function is not "
-                                               "implemented yet...", "Ok");
+       return homeDir.absolutePath().toStdString().c_str();
 }
 
+const char * QKernel::getRemoteDir()
+{
+       return homeDir.absoluteFilePath(REMOTE_PATH).toStdString().c_str();
+}
+
+const char * QKernel::getNetModuleSocket()
+{
+       return homeDir.absoluteFilePath(NPATH).toStdString().c_str();
+}
+
+const char * QKernel::getGraphModuleSocket()
+{
+       return homeDir.absoluteFilePath(GPATH).toStdString().c_str();
+}
+
+void QKernel::loadConfig(const QString & fname)
+{
+       loadConfig(fname.toStdString().c_str());
+}
 /**
  * Loads configuration from the given file.
  * @param fname Filename of the configuration file.
  */
-void QKernel::LoadConfig(char * fname)
+void QKernel::loadConfig(const char * fname)
 {
+       fprintf(stderr, "2: %s\n", fname);
        config_t cfg;
        config_setting_t *setting;
-       const char *str;
 
        /* Hack for checking if file exists without using external libs.*/
+
        FILE * file = fopen(fname, "rt");
        if (!file) {
-               fprintf(stderr, "Error: Cannot load configuration file %s!\n", 
+               fprintf(stderr, "Error: Cannot load configuration file: %s!\n", 
                                                                        fname);
                exit(3);
        }
@@ -245,14 +276,6 @@ void QKernel::LoadConfig(char * fname)
                                                "Warning", fname, "progdir");
        }
 
-       setting = config_lookup(&cfg, "homedir");
-       if (setting) {
-               strncpy(HomeDir, config_setting_get_string(setting), 255);
-       } else {
-               fprintf(stderr, "%s! In file %s, '%s' was not found.\n", 
-                                               "Warning", fname, "homedir");
-       }
-
        config_destroy(&cfg);
        fclose(file);
 }
@@ -281,10 +304,23 @@ void QKernel::on_actionExecute_triggered()
  */
 void QKernel::on_actionEditor_triggered()
 {
-       char cmd[255];
-       sprintf(cmd, "%s/modules/logedit %s %s %s %s %s %s &", HomeDir, HomeDir, 
-                       myargs[0], myargs[1], myargs[2], myargs[3], myargs[4]);
-       system(cmd);
+       QString program = getHomeDir();
+       program += "/modules/logedit";
+
+       pid_t pid = fork();
+       if (pid == 0) {
+               if (execl(program.toStdString().c_str(),
+                       getHomeDir(),
+                       myargs[0],
+                       myargs[1],
+                       myargs[2],
+                       myargs[3],
+                       myargs[4],
+                       NULL
+               ) == -1) {
+                       WriteMessage("Executing logedit failed!");
+               }
+       }
 }
 
 /**
@@ -292,10 +328,27 @@ void QKernel::on_actionEditor_triggered()
  */
 void QKernel::on_actionHelp_triggered()
 {
-       char cmd[255];
-       sprintf(cmd, "%s/modules/loghelp %s/doc %s %s %s %s %s &", HomeDir,
-               HomeDir, myargs[0], myargs[1], myargs[2], myargs[3], myargs[4]);
-       system(cmd);
+       QString program = getHomeDir();
+       program += "/modules/loghelp";
+       
+       QString docDir = getHomeDir();
+       docDir += "/doc";
+
+       pid_t pid = fork();
+       if (pid == 0) {
+               if (execl(program.toStdString().c_str(),
+                       docDir.toStdString().c_str(),
+                       myargs[0],
+                       myargs[1],
+                       myargs[2],
+                       myargs[3],
+                       myargs[4],
+                       NULL
+               ) == -1) {
+
+                       WriteMessage("Executing loghelp failed!");
+               }
+       }
 }
 
 /**
@@ -305,11 +358,34 @@ void QKernel::RunGraphModule(char *sk)
 {
        char cmd[255];
 
-       sprintf(cmd, "%s/modules/loggraph %s %s %s %s %s %s &", HomeDir,
-               sk, myargs[0], myargs[1], myargs[2], myargs[3], myargs[4]);
-
-       if (system(cmd) != 0)
-               WriteMessage("Cannot connect GRAPH resources");
+       QString program = getHomeDir();
+       program += "/modules/loggraph";
+       pid_t pid = fork();
+       if (pid == 0) {
+               if (execl(program.toStdString().c_str(),
+                       program.toStdString().c_str(),
+                       sk,
+                       myargs[0],
+                       myargs[1],
+                       myargs[2],
+                       myargs[3],
+                       myargs[4],
+                       NULL
+               ) == -1) {
+
+                       WriteMessage("Executing loggraph failed!");
+                       WriteMessage("Exiting...");
+                       sleep(2);
+                       on_actionQuit_triggered();
+               }
+       }
+       else if (pid < 0) {
+               WriteMessage("fork(loggraph) failed!");
+               WriteMessage("Exiting...");
+               sleep(2);
+               on_actionQuit_triggered();
+               exit(3);
+       }
 }
 
 /**
@@ -318,39 +394,58 @@ void QKernel::RunGraphModule(char *sk)
 void QKernel::RunNetModule()
 {
        struct sockaddr_un svr;
-       int len, on;
+       int len;
+       int on;
        int sock;
-       char cmd[255];
-       sprintf(cmd, "%s/modules/lognet %s %s %s %s %s %s &", HomeDir,
-               NPATH, myargs[0], myargs[1], myargs[2], myargs[3], myargs[4]);
+
+       QString program = getHomeDir();
+       program += "/modules/lognet";
+
+       pid_t pid = fork();
+       if (pid == 0) {
+               if (execl(program.toStdString().c_str(),
+                       program.toStdString().c_str(),
+                       getNetModuleSocket(),
+                       getConfigFilePath().toStdString().c_str(),
+                       myargs[0],
+                       myargs[1],
+                       myargs[2],
+                       myargs[3],
+                       myargs[4],
+                       NULL
+               ) == -1) {
+
+                       WriteMessage("Executing lognet failed!");
+                       WriteMessage("Exiting...");
+                       sleep(2);
+                       on_actionQuit_triggered();
+               }
+       }
+       else if (pid < 0) {
+               WriteMessage("fork(lognet) failed!");
+               WriteMessage("Exiting...");
+               sleep(2);
+               on_actionQuit_triggered();
+               exit(3);
+       }
 
        /* -------- socket for NET module -------- */
-       unlink(NPATH);
+       unlink(getNetModuleSocket());
        sock = socket(AF_UNIX, SOCK_STREAM, 0);
        bzero(&svr, sizeof(svr));
        svr.sun_family = AF_UNIX;
-       strcpy(svr.sun_path, NPATH);
+       strcpy(svr.sun_path, getNetModuleSocket());
        len = strlen(svr.sun_path) + sizeof(svr.sun_family);
-       bind(sock, (struct sockaddr*)&svr, len);      
+       bind(sock, (struct sockaddr*)&svr, len);
        listen(sock, 5);
 
-       if (system(cmd) == 0) {
-               net_sock = accept(sock, (struct sockaddr*)0, (unsigned int*)0);
-               // close(sock); 
-               if (net_sock != 0) {
-                       WriteMessage("NETWORK successfully connected");
-                       fcntl(net_sock, F_SETFL, O_NONBLOCK|fcntl(net_sock,
-                                                               F_GETFL, 0));
-                       on=1;
-                       setsockopt(net_sock, IPPROTO_TCP, TCP_NODELAY,
-                                                       (char*)&on, sizeof(on)); 
-               } else {
-                       WriteMessage("Cannot connect NETWORK resources");
-                       WriteMessage("Exiting...");
-                       sleep(2);
-                       on_actionQuit_triggered();
-               }
-       /* system OK */
+       net_sock = accept(sock, (struct sockaddr*)0, (unsigned int*)0);
+       // close(sock); 
+       if (net_sock != 0) {
+               WriteMessage("NETWORK successfully connected");
+               fcntl(net_sock, F_SETFL, O_NONBLOCK|fcntl(net_sock, F_GETFL, 0));
+               on = 1;
+               setsockopt(net_sock, IPPROTO_TCP, TCP_NODELAY, (char*)&on, sizeof(on));
        } else {
                WriteMessage("Cannot connect NETWORK resources");
                WriteMessage("Exiting...");
@@ -579,7 +674,7 @@ void QKernel::NetMessage()
                                        WriteMessage(CharLine);
                                        break;
                                case VLP_REMOTE_INSTANCE:
-                                       sprintf(ss, "%s/%s", REMOTE_PATH,
+                                       sprintf(ss, "%s/%s", getRemoteDir(),
                                                                msg.param.pstr);
 
                                        if (info_messages) { 
@@ -770,10 +865,11 @@ void QKernel::on_actionInfo_messages_triggered()
  */
 void QKernel::on_actionOptions_triggered()
 {
-       OptionsDialog optionsDialog(this);
+       OptionsDialog optionsDialog(getConfigFilePath(), this);
        if (optionsDialog.exec()) {
-               optionsDialog.saveConfig("vlp.cfg");
-               LoadConfig("vlp.cfg");
+               optionsDialog.saveConfig(getConfigFilePath());
+
+               loadConfig(getConfigFilePath());
        }
 }
 
@@ -956,11 +1052,15 @@ InterpEntry *QKernel::RunIntModule(char *ss, int r)
        strcpy(newINT->fullname, ss);
 
        sprintf(a, "%s%d", IPATH, newint);
-       sprintf(cmd, "%s/modules/logint %s %s", HomeDir, a, ss);
-       if (r) 
+       sprintf(cmd, "%s/modules/logint %s %s",
+               getHomeDir(),
+               a,
+               ss);
+       if (r) {
                strcat(cmd, " r");
-       sprintf(b, " %s %s %s %s %s", myargs[0], myargs[1], myargs[2],
-                                                       myargs[3], myargs[4]);
+       }
+       sprintf(b, " %s %s %s %s %s",
+               myargs[0], myargs[1], myargs[2], myargs[3], myargs[4]);
        strcat(cmd, b);
        strcat(cmd, " &");
 
@@ -1130,16 +1230,10 @@ void QKernel::on_actionInfo_triggered()
  */
 int main(int argc, char **argv)
 {
-       int i;
-       for(i = 0; i < 5; i++) {
-               strcpy(myargs[i], "");
-       }
-       for(i = 1; i < argc; i++) {
-               strcpy(myargs[i - 1], argv[i]);
-       }
+       XInitThreads();
 
        QApplication * app = new QApplication(argc, argv);
-       QKernel kernel;
+       QKernel kernel(argc, argv);
        kernel.show();
        kernel.InitMessage();