************************************************************/
-
#include <QtGui/QApplication>
#include <QtGui/QMainWindow>
#include <QtGui/QTextEdit>
#include <qcursor.h>
#include <QtCore/QSocketNotifier>
#include <QtGui/QCloseEvent>
-#include <qdir.h>
+#include <QtCore/QDir>
+#include <QtCore/QProcess>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <libconfig.h>
+#include <X11/Xlib.h>
#include "kernel.h"
#include "kernelwindow.h"
char CharLine[25] = "________________________";
-char myargs[5][255];
-
-//QApplication *app;
/**
* Event invoked on program close.
* 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;
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);
}
"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);
}
*/
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!");
+ }
+ }
}
/**
*/
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!");
+ }
+ }
}
/**
{
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);
+ }
}
/**
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...");
WriteMessage(CharLine);
break;
case VLP_REMOTE_INSTANCE:
- sprintf(ss, "%s/%s", REMOTE_PATH,
+ sprintf(ss, "%s/%s", getRemoteDir(),
msg.param.pstr);
if (info_messages) {
*/
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());
}
}
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, " &");
*/
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();
#include "options.h"
-OptionsDialog::OptionsDialog(QWidget * parent)
+OptionsDialog::OptionsDialog(QString configFilePath, QWidget * parent)
: QDialog(parent)
{
setupUi(this);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
- loadConfig("vlp.cfg");
+ loadConfig(configFilePath.toStdString().c_str());
}
OptionsDialog::~OptionsDialog()
*
* @param fname Filename of the configuration file.
*/
-void OptionsDialog::loadConfig(char * fname)
+void OptionsDialog::loadConfig(const char * 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");
fprintf(stderr, "%s! In file %s, '%s' was not found.\n", "Warning", fname, "progdir");
}
- setting = config_lookup(&cfg, "homedir");
- if (setting) {
- homeDir = config_setting_get_string(setting);
- } else {
- fprintf(stderr, "%s! In file %s, '%s' was not found.\n", "Warning", fname, "homedir");
- }
-
config_destroy(&cfg);
fclose(file);
}
-void OptionsDialog::saveConfig(char * fname)
+void OptionsDialog::saveConfig(QString fname)
+{
+ saveConfig(fname.toStdString().c_str());
+}
+
+void OptionsDialog::saveConfig(const char * fname)
{
config_t cfg;
config_setting_t *root;
setting = config_setting_add(root, "node_number", CONFIG_TYPE_INT);
config_setting_set_int(setting, nodeNumber->value());
- setting = config_setting_add(root, "homedir", CONFIG_TYPE_STRING);
- config_setting_set_string(setting, homeDir);
-
setting = config_setting_add(root, "type", CONFIG_TYPE_STRING);
if (explicitConnectionMode->isChecked()) {
config_setting_set_string(setting, "explicit");
#include <QtCore/qfile.h>
#include <QtCore/qstring.h>
#include <QtCore/qstringlist.h>
+#include <QtCore/QDir>
#include <unistd.h>
#include <libconfig.h>
/* List of the Network slots */
QList<NETlink*> Links;
- NETMOD(char*);
+ NETMOD(int argc, char ** argv);
- void load_config(char*);
+ void load_config(const char*);
void write_at_console(char*);
void send_to_kernel(MESSAGE*);
void sock_reopen(NETlink*);
void doitall();
};
-NETMOD::NETMOD(char *kernel_name)
+NETMOD::NETMOD(int argc, char ** argv)
{
+ QString kernelSocket(argv[1]);
+ QString configFile(argv[2]);
+
int i;
int len;
int on;
Links.clear();
Interpreters.clear();
- bzero(&svr, sizeof(svr));
+ bzero(&svr, sizeof(svr));
listen_sock = socket(AF_INET, SOCK_STREAM, 0);
svr.sin_family = AF_INET;
svr.sin_addr.s_addr = INADDR_ANY;
svr.sin_port = htons(LOGPORT);
bind(listen_sock, (struct sockaddr*)&svr, sizeof(svr));
listen(listen_sock,5);
- fcntl(listen_sock, F_SETFL, O_NONBLOCK | fcntl(listen_sock, F_GETFL,
- 0));
+ fcntl(listen_sock, F_SETFL, O_NONBLOCK | fcntl(listen_sock, F_GETFL, 0));
to_connect = 0;
all_connected = FALSE;
- load_config("vlp.cfg");
+
+ load_config(configFile.toStdString().c_str());
kernel_sock = socket(AF_UNIX, SOCK_STREAM, 0);
bzero(&svr1, sizeof(svr1));
svr1.sun_family = AF_UNIX;
- strcpy(svr1.sun_path, kernel_name);
- strcpy(kername, kernel_name);
+ strcpy(svr1.sun_path, kernelSocket.toStdString().c_str());
+ strcpy(kername, kernelSocket.toStdString().c_str());
len = strlen(svr1.sun_path) + sizeof(svr1.sun_family);
i = connect(kernel_sock, (struct sockaddr*)&svr1, len);
if (i == 0)
- fcntl(kernel_sock, F_SETFL, O_NONBLOCK|fcntl(kernel_sock, F_GETFL, 0));
+ fcntl(kernel_sock, F_SETFL, O_NONBLOCK|fcntl(kernel_sock, F_GETFL, 0));
on = 1;
- setsockopt(kernel_sock, IPPROTO_TCP, TCP_NODELAY, (char*)&on,
- sizeof(on));
+ setsockopt(kernel_sock, IPPROTO_TCP, TCP_NODELAY, (char*)&on, sizeof(on));
m.msg_type = MSG_NET;
m.param.pword[0] = NET_NODE;
m.param.pword[1] = MyNode;
/*##################### Load configuration ##########################*/
-void NETMOD::load_config(char *fname)
+void NETMOD::load_config(const char *fname)
{
config_t cfg;
config_setting_t *setting;
/* 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",
- fname);
+ fprintf(stderr, "[Error]/Lognet: Cannot load configuration file %s!\n", fname);
write_at_console("Cannot load configuration file!");
fclose(file);
exit(3);
break;
case CONFIG_TYPE_ARRAY:
strncpy(pomlink->addr,
- config_setting_get_string_elem(setting, 0),
- 255);
+ config_setting_get_string_elem(setting, 0), 255);
break;
default:
fprintf(stderr, "%s! In file %s, bad entry type for %s."
int sj;
fd_set readset;
fd_set writeset;
- struct timeval tout={0, 0};
+ struct timeval tout= {0, 0};
INTlink *pomlink;
struct sockaddr_un svr;
write(sk, &m, sizeof(MESSAGE));
}
+/**
+ * argv0 = program current path
+ * argv1 = socket path
+ * argv2 = config path
+ */
int main(int argc, char **argv)
{
- NETMOD netter(argv[1]);
+ if (argc < 3) {
+ return 1;
+ }
+
+ fprintf(stderr, "lognet: Net module path: %s\n", argv[0]);
+ fprintf(stderr, "lognet: Kernel socket: %s\n", argv[1]);
+ fprintf(stderr, "lognet: Configuration file: %s\n", argv[2]);
+
+ NETMOD netter(argc, argv);
netter.run();
return 0;
}