From: Stefan Huber Date: Thu, 24 Apr 2008 13:17:39 +0000 (+0200) Subject: commiting initial code which works X-Git-Url: https://git.sthu.org/?a=commitdiff_plain;h=12db387ab2607688af53d507482bee77f963e3be;p=runfinite.git commiting initial code which works --- 12db387ab2607688af53d507482bee77f963e3be diff --git a/INSTALL.txt b/INSTALL.txt new file mode 100644 index 0000000..6781e55 --- /dev/null +++ b/INSTALL.txt @@ -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 index 0000000..af437a6 --- /dev/null +++ b/Makefile.am @@ -0,0 +1 @@ +SUBDIRS = src diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..1473750 --- /dev/null +++ b/configure.ac @@ -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 index 0000000..d489a28 --- /dev/null +++ b/src/Makefile.am @@ -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 index 0000000..5b1ca07 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,207 @@ +#include +#include +#include +#include +#include + +#include +#include + + + +#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 for at least a specific time. Kill\n"); + fprintf(out, "process executing if time is over. \n"); + fprintf(out, "\n"); + fprintf(out, "Usage: %s [options] \n", argv[0]); + fprintf(out, "\n"); + fprintf(out, "Options:\n"); + fprintf(out, " -h Print this help.\n"); + fprintf(out, " -t 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 + int fiarg=1; + + //Aha, argument -- skip it + while( 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= 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; +} + +