|
|
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: switch(pid = fork()){
66: case -1:
67: perror("can't fork\n");
68: exit(1);
69: case 0:
70: return fromstdin(rem);
71: default:
72: rv = fromnet(rem);
73: kill(pid, 9);
74: return rv;
75: }
76: } else
77: return fromnet(rem);
78: }
79:
80: fromstdin(rem)
81: {
82: register int neof, n;
83:
84: close(1);
85: close(2);
86: close(3);
87: neof = 0;
88: for (;;) {
89: n = read(0, buf, sizeof(buf));
90: if (n > 0) {
91: if (write(rem, buf, n) != n)
92: return(1);
93: neof = 0;
94: } else {
95: if (++neof >= MAXEOF)
96: write(rem, buf, 0);
97: }
98: }
99: /* not reached */
100: }
101:
102: fromnet(rem)
103: {
104: register int n;
105:
106: close(0);
107: close(2);
108: close(3);
109: for (;;) {
110: n = read(rem, buf, sizeof(buf));
111: if (n <= 0)
112: return(0);
113: if (write(1, buf, n) != n)
114: return(1);
115: }
116: }
117:
118: char *
119: bldargs(argc, argv)
120: register char **argv;
121: {
122: register char *s = args, *t;
123:
124: while(argc--) {
125: *s++ = ' ';
126: t = *argv++;
127: while(*s = *t++) {
128: if(s++ >= &args[MAXCHARS-2]){
129: fprintf(stderr, "rx: arg list too long\n");
130: exit(1);
131: }
132: }
133: }
134: *s = '\0';
135: return(args+1);
136: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.