Annotation of researchv10no/cmd/asd++/dkinstall.c, revision 1.1

1.1     ! root        1: #include "decl.h"
        !             2: #include <signal.h>
        !             3: #include <errno.h>
        !             4: #include <pwd.h>
        !             5: 
        !             6: extern "C" {
        !             7:        extern int dup2 (int, int);
        !             8:        extern int fork();
        !             9:        extern int setupshares (int, void(*)() = 0);
        !            10:        extern void perror (char*);
        !            11:        extern int errno;
        !            12:        extern int sys_nerr;
        !            13:        extern char *sys_errlist[];
        !            14: }
        !            15: 
        !            16: static const int bufsize = 4096;
        !            17: static const int timeout = 60*10;
        !            18: static const int wtimeout = 15;
        !            19: 
        !            20: static void
        !            21: alarmcatch(int)
        !            22: {
        !            23:        signal (SIGALRM, alarmcatch);
        !            24: }
        !            25: 
        !            26: static void
        !            27: die (String s)
        !            28: {
        !            29:        String msg = s;
        !            30:        if ((unsigned) errno < sys_nerr)
        !            31:                msg += ":" + String(sys_errlist[errno]);
        !            32:        msg += "\n";
        !            33:        cout << msg;
        !            34:        exit (1);
        !            35: }
        !            36: 
        !            37: static void
        !            38: twrite (char* buf, int len)
        !            39: {
        !            40:        alarm (timeout);
        !            41:        int n = write (1, buf, len);
        !            42:        alarm (0);
        !            43:        if (n != len)
        !            44:                die ("remote write failed");
        !            45: }
        !            46: 
        !            47: static int
        !            48: tread (char* buf, int len)
        !            49: {
        !            50:        alarm (timeout);
        !            51:        int n = read (0, buf, len);
        !            52:        alarm (0);
        !            53:        if (n < 0)
        !            54:                die ("remote read failed");
        !            55:        return n;
        !            56: }
        !            57: 
        !            58: static void
        !            59: send (char* s)
        !            60: {
        !            61:        twrite (s, strlen (s));
        !            62: }
        !            63: 
        !            64: static int
        !            65: mkfile (String file)
        !            66: {
        !            67:        unlink (file);
        !            68:        int fd = creat (file, 0600);
        !            69:        if (fd < 0)
        !            70:                die ("temp file creat");
        !            71:        if (close (fd) < 0)
        !            72:                die ("temp file close");
        !            73:        fd = open (file, 2);
        !            74:        if (fd < 0)
        !            75:                die ("temp file reopen");
        !            76:        if (unlink (file) < 0)
        !            77:                die ("temp file unlink");
        !            78:        return fd;
        !            79: }
        !            80: 
        !            81: main()
        !            82: {
        !            83:        signal (SIGALRM, alarmcatch);
        !            84: 
        !            85:        /* announce our presence */
        !            86:        send ("asd\n");
        !            87: 
        !            88:        /* use daemon's share instead of root */
        !            89:        struct passwd *pw;
        !            90:        pw = getpwnam ("daemon");
        !            91:        if (pw)
        !            92:                setupshares (pw->pw_uid);
        !            93:        char buf[bufsize];
        !            94:        int n;
        !            95:        checksum cs, rcs;
        !            96: 
        !            97:        umask (077);
        !            98: 
        !            99:        /* make a temp file to hold the data */
        !           100:        Path tfile = Path(tmpdir) & "asd" + String(dec(getpid()));
        !           101:        int infd = mkfile (tfile);
        !           102:        int outfd = mkfile (tfile);
        !           103: 
        !           104:        /* copy the data to the input temp file and checksum it */
        !           105:        while ((n = tread (buf, bufsize)) > 0) {
        !           106:                cs.combine (buf, n);
        !           107:                if (write (infd, buf, n) != n)
        !           108:                        die ("temp file write");
        !           109:        }
        !           110:        lseek (infd, 0, 0);
        !           111: 
        !           112:        /* Check for normal end of data */
        !           113:        if (n < 0)
        !           114:                die ("remote read");
        !           115: 
        !           116:        /* Try to read the checksum */
        !           117:        alarm (timeout);
        !           118:        n = read (0, rcs);
        !           119:        alarm (0);
        !           120: 
        !           121:        /* Do the checksums match? */
        !           122:        if (n != cksize || cs != rcs) {
        !           123:                cout << "checksum error\n";
        !           124:                exit (1);
        !           125:        }
        !           126: 
        !           127:        /* Assert we got the stuff successfully */
        !           128:        twrite ("", 0);
        !           129: 
        !           130:        /* Run inspkg and put the results in the output temp file */
        !           131:        int rc, status;
        !           132:        int pid = fork();
        !           133: 
        !           134:        switch (pid) {
        !           135:        case -1:
        !           136:                die ("fork");
        !           137: 
        !           138:        case 0:                 // child
        !           139:                dup2 (infd, 0);
        !           140:                dup2 (outfd, 1);
        !           141:                close (infd);
        !           142:                close (outfd);
        !           143:                dup2 (1, 2);
        !           144:                dup2 (1, 3);
        !           145:                execl (inspkg, "inspkg", (char*) 0);
        !           146:                die ("execl");
        !           147: 
        !           148:        default:                // parent
        !           149:                do {
        !           150:                        alarm (wtimeout);
        !           151:                        rc = wait (&status);
        !           152:                        alarm (0);
        !           153:                        if (rc == -1) {
        !           154:                                if (errno == EINTR)
        !           155:                                        twrite ("\n", 1);
        !           156:                                else
        !           157:                                        die ("wait");
        !           158:                        }
        !           159:                } while (rc != pid);
        !           160:        }
        !           161: 
        !           162:        /* Send the output temp file to the other end */
        !           163:        lseek (outfd, 0, 0);
        !           164:        while ((n = read (outfd, buf, bufsize)) > 0)
        !           165:                twrite (buf, n);
        !           166: 
        !           167:        /* Check for normal EOF */
        !           168:        if (n < 0)
        !           169:                die ("reading inspkg output");
        !           170: 
        !           171:        /* Report the inspkg return code */
        !           172:        cout << "return code " << status << "\n";
        !           173: 
        !           174:        return 0;
        !           175: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.