Annotation of researchv10no/ipc/bin/2rx.c, revision 1.1.1.1

1.1       root        1: #include <stdio.h>
                      2: #include <dk.h>
                      3: #include <string.h>
                      4: #include <sys/types.h>
                      5: #include <signal.h>
                      6: #include <libc.h>
                      7: #include <ipc.h>
                      8: 
                      9: /*
                     10:  * program to run a command
                     11:  * on another cpu on Datakit
                     12:  */
                     13: 
                     14: #define MAXCHARS 8192
                     15: char   args[MAXCHARS];
                     16: char   buf[4096];
                     17: char   *bldargs(), *malloc();
                     18: extern errno;
                     19: 
                     20: #define        MAXEOF  10
                     21: 
                     22: main(argc, argv)
                     23: char **argv;
                     24: {
                     25:        char *host, *av0;
                     26:        int ignore0=0;
                     27:        int pid, rv;
                     28:        register int rem;
                     29: 
                     30:        rem = -1;
                     31:        av0 = host = argv[0];
                     32:        if (strrchr(av0, '/'))
                     33:                av0 = host = strrchr(av0, '/')+1;
                     34:        if(strcmp(host, "rx")==0 || strcmp(host, "rsh")==0){
                     35:                if(argc<2){
                     36:                        fprintf(stderr, "usage: %s [-n] host cmd\n", av0);
                     37:                        exit(1);
                     38:                }
                     39:                if(strcmp(argv[1], "-n")==0){
                     40:                        ignore0 = 1;
                     41:                        argv++, argc--;
                     42:                }
                     43:                if(argc<2){
                     44:                        fprintf(stderr, "usage: %s [-n] host cmd\n", av0);
                     45:                        exit(1);
                     46:                }
                     47:                host = argv[1];
                     48:                argv++, argc--;
                     49:        } else if(argc<=1){
                     50:                execl("/usr/bin/con", "con", host, 0);
                     51:                fprintf(stderr, "rx: cannot exec con\n");
                     52:                exit(1);
                     53:        }
                     54:        if (strcmp(av0, "rsh")!=0)
                     55:                rem = ipcexec(host, "heavy", bldargs(argc-1, &argv[1]));
                     56:        if (rem<0)
                     57:                rem = ipcrexec(host, "heavy", bldargs(argc-1, &argv[1]));
                     58:        if (rem<0) {
                     59:                fprintf(stderr, "%s: call to %s failed: %s\n", av0, host, errstr);
                     60:                exit(1);
                     61:        }
                     62:        signal(SIGPIPE, SIG_IGN);       /* sometimes happens unreasonably in EOF handling */
                     63: 
                     64:        if(!ignore0){
                     65:                pid = getpid();
                     66:                switch(fork()){
                     67:                case -1:
                     68:                        perror("can't fork\n");
                     69:                        exit(1);
                     70:                case 0:
                     71:                        rv = fromnet(rem);
                     72:                        kill(pid, 9);
                     73:                        return rv;
                     74:                default:
                     75:                        return fromstdin(rem);
                     76:                }
                     77:        } else
                     78:                return fromnet(rem);
                     79: }
                     80: 
                     81: fromstdin(rem)
                     82: {
                     83:        register int neof, n;
                     84: 
                     85:        close(1);
                     86:        close(2);
                     87:        close(3);
                     88:        neof = 0;
                     89:        for (;;) {
                     90:                n = read(0, buf, sizeof(buf));
                     91:                if (n > 0) {
                     92:                        if (write(rem, buf, n) != n)
                     93:                                return(1);
                     94:                        neof = 0;
                     95:                } else {
                     96:                        if (++neof >= MAXEOF)
                     97:                                write(rem, buf, 0);
                     98:                }
                     99:        }
                    100:        /* not reached */
                    101: }
                    102: 
                    103: fromnet(rem)
                    104: {
                    105:        register int n;
                    106: 
                    107:        close(0);
                    108:        close(2);
                    109:        close(3);
                    110:        for (;;) {
                    111:                n = read(rem, buf, sizeof(buf));
                    112:                if (n <= 0)
                    113:                        return(0);
                    114:                if (write(1, buf, n) != n)
                    115:                        return(1);
                    116:        }
                    117: }
                    118: 
                    119: char *
                    120: bldargs(argc, argv)
                    121: register char **argv;
                    122: {
                    123:        register char *s = args, *t;
                    124: 
                    125:        while(argc--) {
                    126:                *s++ = ' ';
                    127:                t = *argv++;
                    128:                while(*s = *t++) {
                    129:                        if(s++ >= &args[MAXCHARS-2]){
                    130:                                fprintf(stderr, "rx: arg list too long\n");
                    131:                                exit(1);
                    132:                        }
                    133:                }
                    134:        }
                    135:        *s = '\0';
                    136:        return(args+1);
                    137: }

unix.superglobalmegacorp.com

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