Annotation of embedtools/src/wdog.c, revision 1.1.2.3
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.3 ! misho 6: * $Id: wdog.c,v 1.1.2.2 2010/10/18 08:32:39 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.2 misho 48: char ch, szRun[MAXPATHLEN], 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.2 misho 53:
1.1.2.3 ! misho 54: while ((ch = getopt(argc, argv, "vhc:u:")) != -1)
1.1.2.2 misho 55: switch (ch) {
56: case 'v':
57: Verbose++;
58: break;
59: case 'c':
1.1.2.3 ! misho 60: if (uid) {
! 61: printf("Error:: can`t chroot, please run as root!\n");
! 62: goto end;
! 63: }
1.1.2.2 misho 64: if (access(optarg, R_OK)) {
65: printf("Error:: can`t chroot to %s #%d - %s\n", optarg,
66: errno, strerror(errno));
67: goto end;
68: } else
69: strlcpy(szChroot, optarg, MAXPATHLEN);
1.1.2.3 ! misho 70: status |= 1;
! 71: break;
! 72: case 'u':
! 73: if (uid) {
! 74: printf("Error:: can`t setuid, please run as root!\n");
! 75: goto end;
! 76: }
! 77: pass = getpwnam(optarg);
! 78: if (!pass) {
! 79: printf("Error:: can`t find user %s\n", optarg);
! 80: goto end;
! 81: } else
! 82: uid = pass->pw_uid;
! 83: endpwent();
! 84: status |= 2;
1.1.2.2 misho 85: break;
86: case 'h':
87: default:
88: Usage();
89: goto end;
90: }
91: argc -= optind;
92: argv += optind;
93: if (!argc || !argv || !*argv) {
94: Usage();
95: goto end;
96: } else
97: strlcpy(szRun, *argv, MAXPATHLEN);
98: VERB(2) printf("Info:: Chroot=%s Run=%s\n", szChroot, szRun);
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:
! 128: // while (!Kill)
1.1.2.2 misho 129: switch ((ret = fork())) {
130: case -1:
131: printf("Error:: error in fork #%d - %s\n", errno, strerror(errno));
1.1.2.3 ! misho 132: ret = 5;
1.1.2.2 misho 133: goto end;
134: case 0:
135: break;
136: default:
137: waitpid(ret, &status, 0);
138: }
139:
140: ret = 0;
141: end:
142: return ret;
1.1.2.1 misho 143: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>