Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * =====================================================================================
- * Filename: main.cpp
- * Description: Creates a child process responsible for starting a kdb daemon server
- * The parent process launches the gui for QScanner.
- *
- * Version: 1.0
- * Created: 23/04/12 15:42:19
- * Compiler: "make" from QScanner root. Requires qt make installed
- * $ sudo apt-get install libqt4-dev
- *
- * Author: Oliver Fletcher, ttolf@lboro.ac.uk
- * University: Loughborough University
- * =====================================================================================
- */
- #include <QApplication>
- #include "qgui.h"
- #include <fcntl.h>
- using namespace std;
- string qLaunch; /* Path to q executable */
- string qRoot; /* Path to QScanner root */
- string qScanner; /* Path to Qscaner.q launch file */
- /*
- * === FUNCTION ======================================================================
- * Name: main
- * Description: Creates a QApplication instance. Gets a child process ID and uses it
- * to launch the daemon server. It then executes the GUI putting the
- * system in a permanent loop. When the GUI is closed it returns
- * and connects to the KDB server sending a kill signal.
- * =====================================================================================
- */
- int main(int argc, char *argv[])
- {
- QApplication app(argc,argv); /* Create GUI App */
- qguiApp *dialog = new qguiApp;
- qScannerPath(); /* Get current working directory */
- pid_t pID = fork(); /* Create a child process id */
- if(pID > 0){
- launchDaemon(); /* Launch the child process */
- }
- else {
- dialog->show();
- app.exec(); /* Execute the GUI (enter loop) */
- }
- int c = khp("localhost",5000); /* Kill the daemon */
- k(-c,"\\\\",(K)0);
- kclose(c);
- return 0;
- }
- /*
- * === FUNCTION ======================================================================
- * Name: open_or_die
- * Description: Opens a files. Errors if the file fails to open
- * Source: http://code.kx.com/wiki/Cookbook/Daemon
- * =====================================================================================
- */
- int open_or_die(char *path)
- {
- int file, flags = O_CREAT | O_WRONLY | O_APPEND;
- if ((NULL == path) || (-1 == (file = open (path, flags, 0666)))) {
- (void) fprintf (stderr, "Failed to open file");
- return 0;
- }
- return file;
- }
- /*
- * === FUNCTION ======================================================================
- * Name: launchDaemon
- * Description: Uses global variables qLaunch, qScanner, qRoot to execute the
- * server using the execv function. Redirects the program stdin, stdout
- * and stderr to log files.
- * Source: http://code.kx.com/wiki/Cookbook/Daemon
- * =====================================================================================
- */
- void launchDaemon()
- {
- //prepare c strings to char*
- char *qlaunch = const_cast<char*> ( qLaunch.c_str() );
- char *qscan = const_cast<char*> ( qScanner.c_str() );
- char *qroot = const_cast<char*> ( qRoot.c_str() );
- char *command[] = { qlaunch , qscan , qroot , "-p", "5000", (char *) 0};
- //Make a token attempt to see if we'll be able to exec the command.
- if (-1 == access (command[0], F_OK)) {
- (void) fprintf (stderr, "Can't access %s, exiting.\n", command[0]);
- exit(EXIT_FAILURE);
- }
- // Try to open some files for pid, stdin, stdout, stderr.
- FILE *pid_file = fopen ("logs/pid", "w+");
- int stdin_file = open_or_die("/dev/null");
- int stderr_file = open_or_die("logs/stderr");
- int stdout_file = open_or_die("logs/stdout");
- // Nuke stdin and redirect stderr, stdout.
- close (STDIN_FILENO);
- dup2 (stdin_file, STDIN_FILENO);
- close (STDOUT_FILENO);
- dup2 (stdout_file, STDOUT_FILENO);
- close (STDERR_FILENO);
- dup2 (stderr_file, STDERR_FILENO);
- // Now daemonize..
- if (0 != daemon (0, 1)) {
- printf("Error: Can't daemonize.");
- exit (EXIT_FAILURE);
- }
- // Write the pid
- fprintf (pid_file, "%d\n", getpid ());
- fclose (pid_file);
- // And away we go..
- execv (command[0], command);
- }
- /*
- * === FUNCTION ======================================================================
- * Name: qScannerPath
- * Description: Used to find the path to the Q executable, QScanner root and QScanner
- * executable
- * Inputs: None
- * Returns: Global Variables
- * qLaunch -> /path/to/l32/q
- * qRoot -> /path/to/QScanner (folder)
- * qScanner-> /path/to/QScanner/QScanner.q
- * =====================================================================================
- */
- void qScannerPath()
- {
- char buff[1024]; /* get executable path */
- ssize_t len = readlink("/proc/self/exe", buff, sizeof(buff)-1);
- string s;
- if (len != -1) { /* process into string */
- buff[len] = '\0';
- s = string(buff);
- }
- for(int i=0;i<2;i++){
- s = s.substr(0,s.find_last_of("/")); /* Remove two layers of folder depth */
- }
- qLaunch = s + "/l32/q"; /* Asign Global variables */
- qRoot = s + "/QScanner";
- qScanner = s + "/QScanner/QScanner.q";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement