Annotation of embedtools/src/wdog.c, revision 1.1.2.5
1.1.2.1 misho 1: /*************************************************************************
2: * (C) 2010 AITNET - Sofia/Bulgaria - <office@aitbg.com>
3: * by Michael Pounov <misho@aitbg.com>
4: *
5: * $Author: misho $
1.1.2.5 ! misho 6: * $Id: wdog.c,v 1.1.2.4 2010/10/18 10:47:06 misho Exp $
1.1.2.1 misho 7: *
8: *************************************************************************/
9: #include "global.h"
10:
11:
1.1.2.2 misho 12: int Verbose, Kill;
1.1.2.1 misho 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"
1.1.2.2 misho 24: "\t-c <dir>\tBefore execute chroot to dir [default=/]\n"
1.1.2.3 misho 25: "\t-u <user>\tBefore execute change user\n"
1.1.2.1 misho 26: "\n", compiled, compiledby, compilehost);
27: }
28:
1.1.2.2 misho 29: static void
30: sigHand(int sig)
31: {
32: int stat;
33:
34: switch (sig) {
35: case SIGTERM:
36: Kill++;
37: break;
38: case SIGCHLD:
39: while (waitpid(-1, &stat, WNOHANG) > 0);
40: break;
41: }
42: }
43:
1.1.2.1 misho 44:
45: int
46: main(int argc, char **argv)
47: {
1.1.2.4 misho 48: char ch, szChroot[MAXPATHLEN] = "/";
1.1.2.3 misho 49: int status = 0, ret = 1;
1.1.2.2 misho 50: struct sigaction sa;
1.1.2.3 misho 51: struct passwd *pass = NULL;
52: uid_t uid = getuid();
1.1.2.5 ! misho 53: u_int penalty = 1;
1.1.2.2 misho 54:
1.1.2.3 misho 55: while ((ch = getopt(argc, argv, "vhc:u:")) != -1)
1.1.2.2 misho 56: switch (ch) {
57: case 'v':
58: Verbose++;
59: break;
60: case 'c':
1.1.2.3 misho 61: if (uid) {
62: printf("Error:: can`t chroot, please run as root!\n");
63: goto end;
64: }
1.1.2.2 misho 65: if (access(optarg, R_OK)) {
66: printf("Error:: can`t chroot to %s #%d - %s\n", optarg,
67: errno, strerror(errno));
68: goto end;
69: } else
70: strlcpy(szChroot, optarg, MAXPATHLEN);
1.1.2.3 misho 71: status |= 1;
72: break;
73: case 'u':
74: if (uid) {
75: printf("Error:: can`t setuid, please run as root!\n");
76: goto end;
77: }
78: pass = getpwnam(optarg);
79: if (!pass) {
80: printf("Error:: can`t find user %s\n", optarg);
81: goto end;
82: } else
83: uid = pass->pw_uid;
84: endpwent();
85: status |= 2;
1.1.2.2 misho 86: break;
87: case 'h':
88: default:
89: Usage();
90: goto end;
91: }
92: argc -= optind;
93: argv += optind;
94: if (!argc || !argv || !*argv) {
95: Usage();
96: goto end;
97: } else
1.1.2.4 misho 98: VERB(2) printf("Info:: Chroot=%s Run=%s\n", szChroot, *argv);
1.1.2.2 misho 99:
100: memset(&sa, 0, sizeof sa);
101: sa.sa_handler = sigHand;
102: sigemptyset(&sa.sa_mask);
103: sigaction(SIGTERM, &sa, NULL);
104: sigaction(SIGCHLD, &sa, NULL);
105: sa.sa_handler = SIG_IGN;
106: sigaction(SIGHUP, &sa, NULL);
107: sigaction(SIGINT, &sa, NULL);
108: sigaction(SIGQUIT, &sa, NULL);
109: sigaction(SIGPIPE, &sa, NULL);
110: sigaction(SIGTSTP, &sa, NULL);
111: sigaction(SIGSTOP, &sa, NULL);
112: VERB(5) printf("Info:: Catched signals ...\n");
113:
1.1.2.3 misho 114: if (status & 1 && (ret = chroot(szChroot)) == -1) {
1.1.2.2 misho 115: printf("Error:: error in chroot to %s #%d - %s\n", szChroot, errno, strerror(errno));
116: ret = 3;
117: goto end;
118: } else
119: VERB(1) printf("Info:: chrooted to %s\n", szChroot);
120:
1.1.2.3 misho 121: if (status & 2 && setuid(uid) == -1) {
122: printf("Error:: error in setuid to %u #%d - %s\n", uid, errno, strerror(errno));
123: ret = 4;
124: goto end;
125: } else
126: VERB(1) printf("Info:: setuid to %u\n", uid);
127:
1.1.2.4 misho 128: status ^= status;
1.1.2.5 ! misho 129: while (!Kill && penalty) {
1.1.2.2 misho 130: switch ((ret = fork())) {
131: case -1:
132: printf("Error:: error in fork #%d - %s\n", errno, strerror(errno));
1.1.2.3 misho 133: ret = 5;
1.1.2.2 misho 134: goto end;
135: case 0:
1.1.2.4 misho 136: VERB(3) printf("Info:: I`m child of shadows ...\n");
137: if (execvp(*argv, argv) == -1) {
138: printf("Error:: error in exec %s #%d - %s\n",
139: *argv, errno, strerror(errno));
140: ret = 6;
141: goto end;
142: }
1.1.2.5 ! misho 143: /* never reached !!! */
1.1.2.2 misho 144: break;
145: default:
1.1.2.4 misho 146: wait(&status);
147: kill(ret, SIGTERM);
148: ret = status;
1.1.2.2 misho 149: }
1.1.2.5 ! misho 150: /* penalty timeout retry */
! 151: usleep(penalty);
! 152: penalty <<= 1;
! 153: VERB(2) printf("Info:: penalty timeout %u microseconds\n", penalty);
! 154: }
! 155: if (!penalty)
! 156: ret = 9;
1.1.2.2 misho 157: end:
158: return ret;
1.1.2.1 misho 159: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>