From 595f80b4f2420ff252f137cacb22a7d4b6f0850d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rafa=C5=82=20D=C5=82ugo=C5=82=C4=99cki?= Date: Mon, 25 Jan 2016 00:15:48 +0100 Subject: [PATCH] Removed system function invocations and placed forked execl instead --- Makefile.am | 2 +- src/kernel/kernel.cpp | 258 ++++++++++++++++++++++++++++------------- src/kernel/kernel.h | 18 ++- src/kernel/options.cpp | 24 ++-- src/kernel/options.h | 7 +- src/net/lognet.cpp | 52 ++++++--- 6 files changed, 237 insertions(+), 124 deletions(-) diff --git a/Makefile.am b/Makefile.am index 326f22e..f555025 100644 --- a/Makefile.am +++ b/Makefile.am @@ -70,7 +70,7 @@ bin_logker_SOURCES = \ src/kernel/options.cpp \ src/kernel/options.moc.cpp bin_logker_CPPFLAGS = $(bin_logker_CFLAGS) -bin_logker_LDADD = $(bin_logker_LIBS) -lconfig++ +bin_logker_LDADD = $(bin_logker_LIBS) -lconfig++ -lX11 bin_logkerdir = src/kernel bin_logker_HEADERS = \ src/kernel/kernel.h \ diff --git a/src/kernel/kernel.cpp b/src/kernel/kernel.cpp index 2f45322..8bd0311 100644 --- a/src/kernel/kernel.cpp +++ b/src/kernel/kernel.cpp @@ -29,7 +29,6 @@ ************************************************************/ - #include #include #include @@ -50,7 +49,8 @@ #include #include #include -#include +#include +#include #include #include @@ -62,6 +62,7 @@ #include #include +#include #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(); diff --git a/src/kernel/kernel.h b/src/kernel/kernel.h index eadf975..89260cc 100644 --- a/src/kernel/kernel.h +++ b/src/kernel/kernel.h @@ -36,6 +36,7 @@ #include #include #include +#include #include "comm.h" @@ -100,13 +101,12 @@ public: int NodeNumber; int ConType; - QKernel(); + QKernel(int argc, char **argv); void WriteMessage(char* msg); void InitMessage(); public slots: - void n_impl(); void NetMessage(); void IntMessage(int); @@ -138,10 +138,13 @@ private: int net_sock; int freeINTid; QSocketNotifier *Net_Notify; - char HomeDir[255]; + QDir homeDir; + char myargs[5][255]; bool info_messages; - void LoadConfig(char *); + void loadConfig(const QString &fname); + void loadConfig(const char *); + void RunGraphModule(char*); void RunNetModule(); InterpEntry *findINTbySocket(int); @@ -156,6 +159,13 @@ private: */ void setLocked(bool locked); + QString getConfigFilePath(); + const char * getHomeDir(); + const char * getRemoteDir(); + + const char * getNetModuleSocket(); + const char * getGraphModuleSocket(); + private slots: void on_actionExecute_triggered(); void on_actionKill_triggered(); diff --git a/src/kernel/options.cpp b/src/kernel/options.cpp index 2c4732f..1a0a294 100644 --- a/src/kernel/options.cpp +++ b/src/kernel/options.cpp @@ -3,7 +3,7 @@ #include "options.h" -OptionsDialog::OptionsDialog(QWidget * parent) +OptionsDialog::OptionsDialog(QString configFilePath, QWidget * parent) : QDialog(parent) { setupUi(this); @@ -11,7 +11,7 @@ OptionsDialog::OptionsDialog(QWidget * parent) connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); - loadConfig("vlp.cfg"); + loadConfig(configFilePath.toStdString().c_str()); } OptionsDialog::~OptionsDialog() @@ -66,11 +66,10 @@ void OptionsDialog::on_delConnectionButton_clicked() * * @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"); @@ -149,18 +148,16 @@ void OptionsDialog::loadConfig(char * fname) 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; @@ -175,9 +172,6 @@ void OptionsDialog::saveConfig(char * fname) 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"); diff --git a/src/kernel/options.h b/src/kernel/options.h index 771d02d..e574b08 100644 --- a/src/kernel/options.h +++ b/src/kernel/options.h @@ -11,15 +11,16 @@ class OptionsDialog : public QDialog, private Ui::OptionsDialog { Q_OBJECT public: - OptionsDialog(QWidget * parent = 0); + OptionsDialog(QString configFilePath, QWidget * parent = 0); ~OptionsDialog(); - void saveConfig(char * fname); + void saveConfig(QString fname); + void saveConfig(const char * fname); private: QString homeDir; - void loadConfig(char * fname); + void loadConfig(const char * fname); private slots: void on_addConnectionButton_clicked(); diff --git a/src/net/lognet.cpp b/src/net/lognet.cpp index 4c00e27..58e4813 100644 --- a/src/net/lognet.cpp +++ b/src/net/lognet.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -95,9 +96,9 @@ public: /* List of the Network slots */ QList 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*); @@ -129,8 +130,11 @@ public: 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; @@ -142,32 +146,31 @@ NETMOD::NETMOD(char *kernel_name) 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; @@ -187,7 +190,7 @@ NETMOD::NETMOD(char *kernel_name) /*##################### Load configuration ##########################*/ -void NETMOD::load_config(char *fname) +void NETMOD::load_config(const char *fname) { config_t cfg; config_setting_t *setting; @@ -200,8 +203,7 @@ void NETMOD::load_config(char *fname) /* 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); @@ -246,8 +248,7 @@ void NETMOD::load_config(char *fname) 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." @@ -403,7 +404,7 @@ void NETMOD::get_internal() int sj; fd_set readset; fd_set writeset; - struct timeval tout={0, 0}; + struct timeval tout= {0, 0}; INTlink *pomlink; struct sockaddr_un svr; @@ -1226,9 +1227,22 @@ void NETMOD::conn_info(int sk) 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; } -- 2.30.2