version 1.1.2.1, 2010/10/16 03:30:51
|
version 1.1.2.2, 2010/10/18 08:32:39
|
Line 9
|
Line 9
|
#include "global.h" |
#include "global.h" |
|
|
|
|
int Verbose; | int Verbose, Kill; |
extern char compiled[], compiledby[], compilehost[]; |
extern char compiled[], compiledby[], compilehost[]; |
|
|
|
|
Line 21 Usage()
|
Line 21 Usage()
|
"=== %s === %s@%s ===\n\n" |
"=== %s === %s@%s ===\n\n" |
" Syntax: wdog [options] [exec_file]\n\n" |
" Syntax: wdog [options] [exec_file]\n\n" |
"\t-v\t\tVerbose ...\n" |
"\t-v\t\tVerbose ...\n" |
"\t-u <user>\tAfter execute suid to user [default=root]\n" | "\t-c <dir>\tBefore execute chroot to dir [default=/]\n" |
"\n", compiled, compiledby, compilehost); |
"\n", compiled, compiledby, compilehost); |
} |
} |
|
|
|
static void |
|
sigHand(int sig) |
|
{ |
|
int stat; |
|
|
|
switch (sig) { |
|
case SIGTERM: |
|
Kill++; |
|
break; |
|
case SIGCHLD: |
|
while (waitpid(-1, &stat, WNOHANG) > 0); |
|
break; |
|
} |
|
} |
|
|
|
|
int |
int |
main(int argc, char **argv) |
main(int argc, char **argv) |
{ |
{ |
return 0; | char ch, szRun[MAXPATHLEN], szChroot[MAXPATHLEN] = "/"; |
| int status, ret = 1; |
| struct sigaction sa; |
| |
| while ((ch = getopt(argc, argv, "vhc:")) != -1) |
| switch (ch) { |
| case 'v': |
| Verbose++; |
| break; |
| case 'c': |
| if (access(optarg, R_OK)) { |
| printf("Error:: can`t chroot to %s #%d - %s\n", optarg, |
| errno, strerror(errno)); |
| goto end; |
| } else |
| strlcpy(szChroot, optarg, MAXPATHLEN); |
| break; |
| case 'h': |
| default: |
| Usage(); |
| goto end; |
| } |
| argc -= optind; |
| argv += optind; |
| if (!argc || !argv || !*argv) { |
| Usage(); |
| goto end; |
| } else |
| strlcpy(szRun, *argv, MAXPATHLEN); |
| VERB(2) printf("Info:: Chroot=%s Run=%s\n", szChroot, szRun); |
| |
| memset(&sa, 0, sizeof sa); |
| sa.sa_handler = sigHand; |
| sigemptyset(&sa.sa_mask); |
| sigaction(SIGTERM, &sa, NULL); |
| sigaction(SIGCHLD, &sa, NULL); |
| sa.sa_handler = SIG_IGN; |
| sigaction(SIGHUP, &sa, NULL); |
| sigaction(SIGINT, &sa, NULL); |
| sigaction(SIGQUIT, &sa, NULL); |
| sigaction(SIGPIPE, &sa, NULL); |
| sigaction(SIGTSTP, &sa, NULL); |
| sigaction(SIGSTOP, &sa, NULL); |
| VERB(5) printf("Info:: Catched signals ...\n"); |
| |
| if ((ret = chroot(szChroot)) == -1) { |
| printf("Error:: error in chroot to %s #%d - %s\n", szChroot, errno, strerror(errno)); |
| ret = 3; |
| goto end; |
| } else |
| VERB(1) printf("Info:: chrooted to %s\n", szChroot); |
| |
| while (!Kill) |
| switch ((ret = fork())) { |
| case -1: |
| printf("Error:: error in fork #%d - %s\n", errno, strerror(errno)); |
| ret = 4; |
| goto end; |
| case 0: |
| break; |
| default: |
| waitpid(ret, &status, 0); |
| } |
| |
| ret = 0; |
| end: |
| return ret; |
} |
} |