commiting initial code which works
authorStefan Huber <shuber2@gmail.com>
Thu, 24 Apr 2008 13:17:39 +0000 (15:17 +0200)
committerStefan Huber <shuber2@gmail.com>
Thu, 24 Apr 2008 13:17:39 +0000 (15:17 +0200)
INSTALL.txt [new file with mode: 0644]
Makefile.am [new file with mode: 0644]
configure.ac [new file with mode: 0644]
src/Makefile.am [new file with mode: 0644]
src/main.cpp [new file with mode: 0644]

diff --git a/INSTALL.txt b/INSTALL.txt
new file mode 100644 (file)
index 0000000..6781e55
--- /dev/null
@@ -0,0 +1,16 @@
+To configure the source code you may execute
+
+       autoreconf --install
+       mkdir -p bin
+       cd bin
+       ../configure
+
+After that you can compile & install the project by
+
+       make
+       make install
+
+
+If you want to enable debug-information, you may want to call "../configure --enable-debug"
+
+
diff --git a/Makefile.am b/Makefile.am
new file mode 100644 (file)
index 0000000..af437a6
--- /dev/null
@@ -0,0 +1 @@
+SUBDIRS = src
diff --git a/configure.ac b/configure.ac
new file mode 100644 (file)
index 0000000..1473750
--- /dev/null
@@ -0,0 +1,39 @@
+AC_INIT([runfinite], [1.0], [shuber2@gmx.at])
+AC_CONFIG_AUX_DIR([build-aux])
+AM_INIT_AUTOMAKE([-Wall -Werror foreign])
+
+
+#AC_PROG_CC
+AC_PROG_CXX
+
+
+#Options which will be given by user (taken from configure.in of VRONI)
+AC_DEFUN([ADDOPT],        
+         [AC_ARG_ENABLE($1, AS_HELP_STRING([--enable-$1], [$2 (default is $4)]),
+        [$3="$enableval"],[$3="$4"])])
+
+
+#Add new options
+ADDOPT(debug, [enable code debugging], with_debug, no)
+
+
+if test "$with_debug" == "yes";
+then
+       CXXFLAGS="-O0 -g"
+else
+       CXXFLAGS="-O2 -DNDEBUG"
+fi
+
+
+#Always add -Wall
+CXXFLAGS+=" -Wall -pthread"
+
+
+
+
+
+
+AC_CONFIG_HEADERS([config.h])
+AC_CONFIG_FILES([Makefile src/Makefile])
+
+AC_OUTPUT
diff --git a/src/Makefile.am b/src/Makefile.am
new file mode 100644 (file)
index 0000000..d489a28
--- /dev/null
@@ -0,0 +1,2 @@
+bin_PROGRAMS = runfinite
+runfinite_SOURCES = main.cpp 
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644 (file)
index 0000000..5b1ca07
--- /dev/null
@@ -0,0 +1,207 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <sys/wait.h>
+#include <sys/types.h>
+
+
+
+#define ERR_WRONGARGS  -1
+#define ERR_EXECVE     -2
+#define ERR_FORK       -3
+#define ERR_WAITPID    -4
+#define ERR_KILL       -5
+
+
+
+void printUsage(FILE* out, int argc, char** argv)
+{
+       fprintf(out, "Run command <command> for at least a specific time. Kill\n");
+       fprintf(out, "process executing <command> if time is over. \n");
+       fprintf(out, "\n");
+       fprintf(out, "Usage: %s [options] <command>\n", argv[0]);
+       fprintf(out, "\n");
+       fprintf(out, "Options:\n");
+       fprintf(out, "  -h        Print this help.\n");
+       fprintf(out, "  -t <sec>  Number of seconds.\n");
+       fprintf(out, "  -v        Be verbose.\n");
+}
+
+
+int main(int argc, char** argv)
+{
+       bool argHelp=false;
+       bool argTime=false;
+       bool argVerbose=false;
+       float maxtime=0;
+
+       //Wrong arguments
+       if( argc<=1 )
+       {
+               printUsage(stderr, argc, argv);
+               return ERR_WRONGARGS;
+       }
+
+
+       //Get the argument index of the head of <command>
+       int fiarg=1;
+
+       //Aha, argument -- skip it
+       while( fiarg<argc && strncmp(argv[fiarg], "-", 1)==0 )
+       {
+               //Help option
+               if( strncmp(argv[fiarg], "-h", 2) == 0 )
+               {
+                       argHelp = true;
+               }
+
+               //Parse time
+               else if( strncmp(argv[fiarg], "-t", 2) == 0 )
+               {
+                       //Time is given -- goto next argument
+                       argTime=true;
+                       fiarg++;
+                       if( fiarg >= argc)
+                       {
+                               fprintf(stderr, "Missing time.\n");
+                               fprintf(stderr, "\n");
+                               printUsage(stderr, argc, argv);
+                               return ERR_WRONGARGS;
+                       }
+
+                       //Convert the time given in seconds to float
+                       char* endptr;
+                       maxtime = strtof( argv[fiarg], &endptr);
+                       if( endptr != argv[fiarg] + strlen(argv[fiarg]) ||
+                               maxtime<0.0)
+                       {
+                               fprintf(stderr, "Invalid time.\n");
+                               fprintf(stderr, "\n");
+                               printUsage(stderr, argc, argv);
+                               return ERR_WRONGARGS;
+                       }
+               }
+
+               //Help option
+               else if( strncmp(argv[fiarg], "-v", 2) == 0 )
+               {
+                       argVerbose = true;
+               }
+
+
+               //Unknown option
+               else
+               {
+                       fprintf(stderr, "Invalid argument: %s\n", argv[fiarg]);
+                       fprintf(stderr, "\n");
+                       printUsage(stderr, argc, argv);
+                       return ERR_WRONGARGS;
+               }
+
+               //Goto next argument
+               fiarg++;
+       }
+
+
+       //Print help
+       if( argHelp )
+       {
+               printUsage(stdout, argc, argv);
+               return 0;
+       }
+
+
+       //No time given...
+       if( !argTime )
+       {
+               fprintf(stderr, "No time given!\n");
+               fprintf(stderr, "\n");
+               printUsage(stderr, argc, argv);
+               return ERR_WRONGARGS;
+       }
+
+       //No command left
+       if( fiarg>=argc )
+       {
+               fprintf(stderr, "No command given!\n");
+               fprintf(stderr, "\n");
+               printUsage(stderr, argc, argv);
+               return ERR_WRONGARGS;
+       }
+
+
+
+       //Fork process
+       pid_t pid = fork();
+
+
+       //Childrens code...
+       if( pid == 0)
+       {
+               char** newargv = (char**) malloc( sizeof(char*)*(argc-fiarg+1) );
+               char* newenv[] = {NULL};
+
+               //Copy arguments
+               for( int i=fiarg; i<argc; i++)
+               {
+                       newargv[i-fiarg] = (char*)malloc(strlen(argv[i])+1);
+                       strcpy(newargv[i-fiarg], argv[i]);
+               }
+               newargv[argc-fiarg] = NULL;
+
+
+               //Execute child process
+               execve(newargv[0], newargv, newenv);
+               perror("execve");
+               return ERR_EXECVE;
+       }
+       //Parents code...
+       else
+       {
+               //Failure
+               if(pid==-1)
+               {
+                       perror("fork");
+                       return ERR_FORK;
+               }
+
+
+               //Nmb of loops=seconds
+               float loops=0;          
+               //Status flags of child process 
+               int status, w;
+
+               do
+               {
+                       //Kill the child
+                       if( loops >= maxtime )
+                       {
+                               if( argVerbose )
+                                       printf("Kill child...\n");
+
+                               if( kill(pid, SIGTERM ) < 0)
+                                       break;
+                       }
+
+                       //Sleep a round
+                       sleep(1);
+                       loops++;
+
+                       //Test for child
+                       if( waitpid(-1, &status, WNOHANG | WUNTRACED | WCONTINUED) < 0 )
+                               break;
+
+               }while( !WIFEXITED(status) );
+       }
+
+
+       if( argVerbose)
+               printf("Finished\n");
+
+       return 0;
+}
+
+