1: /*************************************************************************
2: * (C) 2010 AITNET - Sofia/Bulgaria - <office@aitbg.com>
3: * by Michael Pounov <misho@aitbg.com>
4: *
5: * $Author: misho $
6: * $Id: wdog.c,v 1.1.2.2 2010/10/18 08:32:39 misho Exp $
7: *
8: *************************************************************************/
9: #include "global.h"
10:
11:
12: int Verbose, Kill;
13: extern char compiled[], compiledby[], compilehost[];
14:
15:
16: static void
17: Usage()
18: {
19:
20: printf( "WatchDog tool for risk process managment\n"
21: "=== %s === %s@%s ===\n\n"
22: " Syntax: wdog [options] [exec_file]\n\n"
23: "\t-v\t\tVerbose ...\n"
24: "\t-c <dir>\tBefore execute chroot to dir [default=/]\n"
25: "\n", compiled, compiledby, compilehost);
26: }
27:
28: static void
29: sigHand(int sig)
30: {
31: int stat;
32:
33: switch (sig) {
34: case SIGTERM:
35: Kill++;
36: break;
37: case SIGCHLD:
38: while (waitpid(-1, &stat, WNOHANG) > 0);
39: break;
40: }
41: }
42:
43:
44: int
45: main(int argc, char **argv)
46: {
47: char ch, szRun[MAXPATHLEN], szChroot[MAXPATHLEN] = "/";
48: int status, ret = 1;
49: struct sigaction sa;
50:
51: while ((ch = getopt(argc, argv, "vhc:")) != -1)
52: switch (ch) {
53: case 'v':
54: Verbose++;
55: break;
56: case 'c':
57: if (access(optarg, R_OK)) {
58: printf("Error:: can`t chroot to %s #%d - %s\n", optarg,
59: errno, strerror(errno));
60: goto end;
61: } else
62: strlcpy(szChroot, optarg, MAXPATHLEN);
63: break;
64: case 'h':
65: default:
66: Usage();
67: goto end;
68: }
69: argc -= optind;
70: argv += optind;
71: if (!argc || !argv || !*argv) {
72: Usage();
73: goto end;
74: } else
75: strlcpy(szRun, *argv, MAXPATHLEN);
76: VERB(2) printf("Info:: Chroot=%s Run=%s\n", szChroot, szRun);
77:
78: memset(&sa, 0, sizeof sa);
79: sa.sa_handler = sigHand;
80: sigemptyset(&sa.sa_mask);
81: sigaction(SIGTERM, &sa, NULL);
82: sigaction(SIGCHLD, &sa, NULL);
83: sa.sa_handler = SIG_IGN;
84: sigaction(SIGHUP, &sa, NULL);
85: sigaction(SIGINT, &sa, NULL);
86: sigaction(SIGQUIT, &sa, NULL);
87: sigaction(SIGPIPE, &sa, NULL);
88: sigaction(SIGTSTP, &sa, NULL);
89: sigaction(SIGSTOP, &sa, NULL);
90: VERB(5) printf("Info:: Catched signals ...\n");
91:
92: if ((ret = chroot(szChroot)) == -1) {
93: printf("Error:: error in chroot to %s #%d - %s\n", szChroot, errno, strerror(errno));
94: ret = 3;
95: goto end;
96: } else
97: VERB(1) printf("Info:: chrooted to %s\n", szChroot);
98:
99: while (!Kill)
100: switch ((ret = fork())) {
101: case -1:
102: printf("Error:: error in fork #%d - %s\n", errno, strerror(errno));
103: ret = 4;
104: goto end;
105: case 0:
106: break;
107: default:
108: waitpid(ret, &status, 0);
109: }
110:
111: ret = 0;
112: end:
113: return ret;
114: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>