From 66e827b6beb14fcd195be35e9b0d6ef5893f8fbd Mon Sep 17 00:00:00 2001 From: =?utf8?q?T=C3=BAlio=20Magno=20Quites=20Machado=20Filho?= Date: Fri, 27 Jun 2008 20:21:49 -0300 Subject: [PATCH] Commit made by Daniel, including the implementation of the daemon. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: TĂșlio Magno Quites Machado Filho --- .gitignore | 4 +- makefile | 28 +++-- src/daemon/vdi_daemon.cpp | 245 ++++++++++++++++++++++++++++++++++++ src/daemon/vdi_daemon.h | 16 +++ src/{main.cpp => tstVDI/tstVDI.cpp} | 8 +- 5 files changed, 288 insertions(+), 13 deletions(-) create mode 100644 src/daemon/vdi_daemon.cpp create mode 100644 src/daemon/vdi_daemon.h rename src/{main.cpp => tstVDI/tstVDI.cpp} (96%) diff --git a/.gitignore b/.gitignore index a32f44a..846fe56 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ *.oi *.vdi -tstVDI -obj/* +tstVDI/ +*.o *.mod *.cmd *.ko diff --git a/makefile b/makefile index 979bf1c..a74cae4 100644 --- a/makefile +++ b/makefile @@ -3,15 +3,22 @@ CPP = g++ LIBS = -OBJ = ./obj/main.o ./obj/VDICore.o ./obj/uuid.o ./obj/alloc.o ./obj/file.o ./obj/fs.o ./obj/rand.o ./obj/fileio.o ./obj/path.o ./obj/RTErrConvertFromErrno.o ./obj/filelock.o -LINKOBJ = ./obj/main.o ./obj/VDICore.o ./obj/uuid.o ./obj/alloc.o ./obj/file.o ./obj/fs.o ./obj/rand.o ./obj/fileio.o ./obj/path.o ./obj/RTErrConvertFromErrno.o ./obj/filelock.o +OBJ = ./obj/VDICore.o ./obj/uuid.o ./obj/alloc.o ./obj/file.o ./obj/fs.o ./obj/rand.o ./obj/fileio.o ./obj/path.o ./obj/RTErrConvertFromErrno.o ./obj/filelock.o +OBJ_tstVDI = ./obj/tstVDI/tstVDI.o +OBJ_daemon = ./obj/daemon/vdi_daemon.o +LINKOBJ = ./obj/VDICore.o ./obj/uuid.o ./obj/alloc.o ./obj/file.o ./obj/fs.o ./obj/rand.o ./obj/fileio.o ./obj/path.o ./obj/RTErrConvertFromErrno.o ./obj/filelock.o +LINKOBJ_tstVDI = ./obj/tstVDI/tstVDI.o +LINKOBJ_daemon = ./obj/daemon/vdi_daemon.o BIN = tstVDI +DAEMON = vdi_daemon -$(BIN): $(OBJ) - $(CPP) $(LIBS) $(LINKOBJ) -o $(BIN) +make: $(OBJ) $(OBJ_tstVDI) $(OBJ_daemon) -./obj/main.o: ./src/main.cpp - $(CPP) -c ./src/main.cpp -o ./obj/main.o +./obj/tstVDI/tstVDI.o: ./src/tstVDI/tstVDI.cpp + $(CPP) -c ./src/tstVDI/tstVDI.cpp -o ./obj/tstVDI/tstVDI.o + +./obj/daemon/vdi_daemon.o: ./src/daemon/vdi_daemon.cpp + $(CPP) -c ./src/daemon/vdi_daemon.cpp -o ./obj/daemon/vdi_daemon.o ./obj/VDICore.o: ./src/VDICore.cpp $(CPP) -c ./src/VDICore.cpp -o ./obj/VDICore.o @@ -43,4 +50,11 @@ $(BIN): $(OBJ) ./obj/filelock.o: ./src/filelock.cpp $(CPP) -c ./src/filelock.cpp -o ./obj/filelock.o -clean: rm $(BIN) $(OBJ) \ No newline at end of file +clean: + rm -f $(OBJ) $(OBJ_tstVDI) $(OBJ_daemon) ./src/*~ ./src/tstVDI/*~ ./src/daemon/*~ ./src/driver/*~ *~ + +bin: + $(CPP) $(LIBS) $(LINKOBJ) $(LINKOBJ_tstVDI) -o $(BIN) + +daemon: + $(CPP) $(LIBS) $(LINKOBJ) $(LINKOBJ_daemon) -o $(DAEMON) diff --git a/src/daemon/vdi_daemon.cpp b/src/daemon/vdi_daemon.cpp new file mode 100644 index 0000000..f2922cb --- /dev/null +++ b/src/daemon/vdi_daemon.cpp @@ -0,0 +1,245 @@ +/* + * Daemon of vdi driver + * + * To compile this file: + * g++ -o vdi_daemon vdi_daemon.cpp + * + * Substitute gcc with cc on some platforms. + * + * Daniel Borba / Tulio Magno + * 27/6/2008 + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "vdi_daemon.h" +#include "../VBoxHDD.h" +#include "../VDICore.h" +#include "../const_defines.h" + +#define DAEMON_NAME "vdi_daemon" +#define PID_FILE "/var/run/vdi_daemon.pid" + +//Global variables +int mRunDaemon = 0; + +/************************************************************************** + Function: Print Usage + + Description: + Output the command-line options for this daemon. + + Params: + @argc - Standard argument count + @argv - Standard argument array + + Returns: + returns void always +**************************************************************************/ +void PrintUsage(int argc, char *argv[]) { + if (argc >=1) { + printf("Usage: %s -h -n\n", argv[0]); + printf(" Options:\n"); + printf(" -n\tDon't fork off as a daemon.\n"); + printf(" -h\tShow this help screen\n"); + printf("\n"); + } +} + +/************************************************************************** + Function: signal_handler + + Description: + This function handles select signals that the daemon may + receive. This gives the daemon a chance to properly shut + down in emergency situations. This function is installed + as a signal handler in the 'main()' function. + + Params: + @sig - The signal received + + Returns: + returns void always +**************************************************************************/ +void signal_handler(int sig) { + + switch(sig) { + case SIGHUP: + syslog(LOG_WARNING, "Received SIGHUP signal."); + break; + case SIGTERM: + syslog(LOG_WARNING, "Received SIGTERM signal."); + mRunDaemon = 0; + break; + default: + syslog(LOG_WARNING, "Unhandled signal (%d) %s", strsignal(sig)); + break; + } +} + +/************************************************************************** + Function: main + + Description: + The cpp standard 'main' entry point function. + + Params: + @argc - count of command line arguments given on command line + @argv - array of arguments given on command line + + Returns: + returns integer which is passed back to the parent process +**************************************************************************/ +int main(int argc, char *argv[]) { + +#if defined(DEBUG) + int daemonize = 0; +#else + int daemonize = 1; +#endif + + // Setup signal handling before we start + signal(SIGHUP, signal_handler); + signal(SIGTERM, signal_handler); + signal(SIGINT, signal_handler); + signal(SIGQUIT, signal_handler); + + int c; + while( (c = getopt(argc, argv, "nh:|help")) != -1) { + switch(c){ + case 'h': + PrintUsage(argc, argv); + exit(0); + break; + case 'n': + daemonize = 0; + break; + default: + PrintUsage(argc, argv); + exit(0); + break; + } + } + + syslog(LOG_INFO, "%s daemon starting up", DAEMON_NAME); + + // Setup syslog logging - see SETLOGMASK(3) +#if defined(DEBUG) + setlogmask(LOG_UPTO(LOG_DEBUG)); + openlog(DAEMON_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); +#else + setlogmask(LOG_UPTO(LOG_INFO)); + openlog(DAEMON_NAME, LOG_CONS, LOG_USER); +#endif + + /* Our process ID and Session ID */ + pid_t pid, sid; + + if (daemonize) { + syslog(LOG_INFO, "starting the daemonizing process"); + + /* Fork off the parent process */ + pid = fork(); + if (pid < 0) { + exit(EXIT_FAILURE); + } + /* If we got a good PID, then + we can exit the parent process. */ + if (pid > 0) { + exit(EXIT_SUCCESS); + } + + /* Change the file mode mask */ + umask(0); + + /* Create a new SID for the child process */ + sid = setsid(); + if (sid < 0) { + /* Log the failure */ + exit(EXIT_FAILURE); + } + + /* Change the current working directory */ + if ((chdir("/")) < 0) { + /* Log the failure */ + exit(EXIT_FAILURE); + } + + /* Close out the standard file descriptors */ + //TODO: close(STDIN_FILENO); + //TODO: close(STDOUT_FILENO); + //TODO: close(STDERR_FILENO); + } + //**************************************************** + // Daemon task + //**************************************************** + //VDI Image File + char vVdiImageFile [255] = "tstVdiBase.vdi"; + + //VDI Request + vdi_request* req = NULL; + + mRunDaemon = 1; + + while (mRunDaemon) + { + //Espera uma chamada do sistema que voltara um vdi_request (Eh necessario que se permita ao daemon escrever e ler no buffer da requisicao) + if (req != NULL) + { + PVDIDISK vVdi = VDIDiskCreate(); + int rc = VDIDiskOpenImage(vVdi, vVdiImageFile, VDI_OPEN_FLAGS_NORMAL); + + if (VBOX_FAILURE(rc)) + { + syslog(LOG_WARNING, "%s daemon: Can't open VDI image %s", DAEMON_NAME, vVdiImageFile); + } + else + { + unsigned long vOffsetStart = VDI_GEOMETRY_SECTOR_SIZE*req->sector; + unsigned long vSize = VDI_GEOMETRY_SECTOR_SIZE*req->nr_sectors; + + switch(req->cmd) + { + case READ: + rc = VDIDiskRead(vVdi, vOffsetStart, req->buffer, vSize); + + if (VBOX_FAILURE(rc)) + { + syslog(LOG_WARNING, "%s daemon: Can't read from VDI image %s", DAEMON_NAME, vVdiImageFile); + } + + break; + case WRITE: + rc = VDIDiskWrite(vVdi, vOffsetStart, req->buffer, vSize); + + if (VBOX_FAILURE(rc)) + { + syslog(LOG_WARNING, "%s daemon: Can't write in VDI image %s", DAEMON_NAME, vVdiImageFile); + } + + break; + } + } + } + } + + + syslog(LOG_INFO, "%s daemon exiting", DAEMON_NAME); + + //**************************************************** + // TODO: Free any allocated resources before exiting + //**************************************************** + + exit(0); +} diff --git a/src/daemon/vdi_daemon.h b/src/daemon/vdi_daemon.h new file mode 100644 index 0000000..f286854 --- /dev/null +++ b/src/daemon/vdi_daemon.h @@ -0,0 +1,16 @@ +#ifndef VDI_DAEMON_H +#define VDI_DAEMON_H + +const int READ = 0; +const int WRITE = 1; + +typedef struct vdi_request_struct +{ + int cmd; + char* buffer; + unsigned long sector; + unsigned long nr_sectors; +} vdi_request; + + +#endif diff --git a/src/main.cpp b/src/tstVDI/tstVDI.cpp similarity index 96% rename from src/main.cpp rename to src/tstVDI/tstVDI.cpp index a96acb5..aecc081 100644 --- a/src/main.cpp +++ b/src/tstVDI/tstVDI.cpp @@ -15,9 +15,9 @@ * be useful, but WITHOUT ANY WARRANTY of any kind. */ -#include "VBoxHDD.h" -#include "file.h" -#include "alloc.h" +#include "../VBoxHDD.h" +#include "../file.h" +#include "../alloc.h" #include #include @@ -77,7 +77,7 @@ int dotest(const char *pszBaseFilename, const char *pszDiffFilename) for (int i = 0; i < 20; i++) buf[i] = '\0'; VDIDiskRead(pVdi, 0x0, buf, 11); - printf(buf); + printf("%s\n", buf); VDIDiskCloseAllImages(pVdi); #undef CHECK -- 2.11.4.GIT