Annotation of embedtools/src/wdog.c, revision 1.1.2.4

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.4 ! misho       6:  * $Id: wdog.c,v 1.1.2.3 2010/10/18 08:47:31 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.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
1.1.2.4 ! misho      97:                VERB(2) printf("Info:: Chroot=%s Run=%s\n", szChroot, *argv);
1.1.2.2   misho      98: 
                     99:        memset(&sa, 0, sizeof sa);
                    100:        sa.sa_handler = sigHand;
                    101:        sigemptyset(&sa.sa_mask);
                    102:        sigaction(SIGTERM, &sa, NULL);
                    103:        sigaction(SIGCHLD, &sa, NULL);
                    104:        sa.sa_handler = SIG_IGN;
                    105:        sigaction(SIGHUP, &sa, NULL);
                    106:        sigaction(SIGINT, &sa, NULL);
                    107:        sigaction(SIGQUIT, &sa, NULL);
                    108:        sigaction(SIGPIPE, &sa, NULL);
                    109:        sigaction(SIGTSTP, &sa, NULL);
                    110:        sigaction(SIGSTOP, &sa, NULL);
                    111:        VERB(5) printf("Info:: Catched signals ...\n");
                    112: 
1.1.2.3   misho     113:        if (status & 1 && (ret = chroot(szChroot)) == -1) {
1.1.2.2   misho     114:                printf("Error:: error in chroot to %s #%d - %s\n", szChroot, errno, strerror(errno));
                    115:                ret = 3;
                    116:                goto end;
                    117:        } else
                    118:                VERB(1) printf("Info:: chrooted to %s\n", szChroot);
                    119: 
1.1.2.3   misho     120:        if (status & 2 && setuid(uid) == -1) {
                    121:                printf("Error:: error in setuid to %u #%d - %s\n", uid, errno, strerror(errno));
                    122:                ret = 4;
                    123:                goto end;
                    124:        } else
                    125:                VERB(1) printf("Info:: setuid to %u\n", uid);
                    126: 
1.1.2.4 ! misho     127:        status ^= status;
        !           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:
1.1.2.4 ! misho     135:                                VERB(3) printf("Info:: I`m child of shadows ...\n");
        !           136:                                if (execvp(*argv, argv) == -1) {
        !           137:                                        printf("Error:: error in exec %s #%d - %s\n", 
        !           138:                                                        *argv, errno, strerror(errno));
        !           139:                                        ret = 6;
        !           140:                                        goto end;
        !           141:                                }
        !           142:                                /* never reached */
1.1.2.2   misho     143:                                break;
                    144:                        default:
1.1.2.4 ! misho     145:                                wait(&status);
        !           146:                                kill(ret, SIGTERM);
        !           147:                                ret = status;
1.1.2.2   misho     148:                }
                    149: end:
                    150:        return ret;
1.1.2.1   misho     151: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>