--- embedtools/src/wdog.c 2010/10/18 08:47:31 1.1.2.3 +++ embedtools/src/wdog.c 2010/10/18 11:20:37 1.1.2.6 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ - * $Id: wdog.c,v 1.1.2.3 2010/10/18 08:47:31 misho Exp $ + * $Id: wdog.c,v 1.1.2.6 2010/10/18 11:20:37 misho Exp $ * *************************************************************************/ #include "global.h" @@ -23,6 +23,7 @@ Usage() "\t-v\t\tVerbose ...\n" "\t-c \tBefore execute chroot to dir [default=/]\n" "\t-u \tBefore execute change user\n" + "\t-P\t\tInfinit loop, bypass penalty timeout\n" "\n", compiled, compiledby, compilehost); } @@ -45,17 +46,21 @@ sigHand(int sig) int main(int argc, char **argv) { - char ch, szRun[MAXPATHLEN], szChroot[MAXPATHLEN] = "/"; + char ch, bypass = 0, szChroot[MAXPATHLEN] = "/"; int status = 0, ret = 1; struct sigaction sa; struct passwd *pass = NULL; uid_t uid = getuid(); + u_int penalty = 1; - while ((ch = getopt(argc, argv, "vhc:u:")) != -1) + while ((ch = getopt(argc, argv, "vhPc:u:")) != -1) switch (ch) { case 'v': Verbose++; break; + case 'P': + bypass = 1; + break; case 'c': if (uid) { printf("Error:: can`t chroot, please run as root!\n"); @@ -94,8 +99,7 @@ main(int argc, char **argv) Usage(); goto end; } else - strlcpy(szRun, *argv, MAXPATHLEN); - VERB(2) printf("Info:: Chroot=%s Run=%s\n", szChroot, szRun); + VERB(2) printf("Info:: Chroot=%s Run=%s\n", szChroot, *argv); memset(&sa, 0, sizeof sa); sa.sa_handler = sigHand; @@ -125,19 +129,37 @@ main(int argc, char **argv) } else VERB(1) printf("Info:: setuid to %u\n", uid); -// while (!Kill) + status ^= status; + while (!Kill && penalty) { switch ((ret = fork())) { case -1: printf("Error:: error in fork #%d - %s\n", errno, strerror(errno)); ret = 5; goto end; case 0: + VERB(3) printf("Info:: I`m child of shadows ...\n"); + if (execvp(*argv, argv) == -1) { + printf("Error:: error in exec %s #%d - %s\n", + *argv, errno, strerror(errno)); + ret = 6; + goto end; + } + /* never reached !!! */ break; default: - waitpid(ret, &status, 0); + wait(&status); + kill(ret, SIGTERM); + ret = status; } - - ret = 0; + /* penalty timeout retry */ + usleep(penalty); + if (!bypass) { + penalty <<= 1; + VERB(2) printf("Info:: penalty timeout %u microseconds\n", penalty); + } + } + if (!penalty) + ret = 9; end: return ret; }