|
|
1.1 root 1: /* pipe.c - */
2:
3: #ifndef lint
4: static char *rcsid = "$Header: /f/osi/quipu/dish/RCS/pipe.c,v 7.1 90/07/09 14:47:21 mrose Exp $";
5: #endif
6:
7: /*
8: * $Header: /f/osi/quipu/dish/RCS/pipe.c,v 7.1 90/07/09 14:47:21 mrose Exp $
9: *
10: *
11: * $Log: pipe.c,v $
12: * Revision 7.1 90/07/09 14:47:21 mrose
13: * sync
14: *
15: * Revision 7.0 89/11/23 22:20:17 mrose
16: * Release 6.0
17: *
18: */
19:
20: /*
21: * NOTICE
22: *
23: * Acquisition, use, and distribution of this module and related
24: * materials are subject to the restrictions of a license agreement.
25: * Consult the Preface in the User's Manual for the full terms of
26: * this agreement.
27: *
28: */
29:
30:
31: #include <signal.h>
32: #include <stdio.h>
33: #include <errno.h>
34: #include "general.h"
35:
36: #ifdef SOCKETS
37: #include "internet.h"
38: #endif
39: #include <sys/ioctl.h>
40: #ifdef BSD42
41: #include <sys/file.h>
42: #endif
43: #ifdef SYS5
44: #include <fcntl.h>
45: #endif
46: #include <sys/stat.h>
47: #include "tailor.h"
48:
49: #ifdef SOCKETS
50: int sd, sd_current;
51: #else
52: char retpipe[LINESIZE];
53: int fd, wfd;
54: #endif
55:
56: #ifndef MIN
57: #define MIN(a,b) (( (b) < (a) ) ? (b) : (a) )
58: #endif
59:
60: int parent_pid;
61: extern int errno;
62:
63: init_pipe ()
64: {
65: char parent [BUFSIZ];
66: char *cp;
67:
68: #ifdef SOCKETS
69: struct sockaddr_in sin_buf;
70: struct sockaddr_in *sin = &sin_buf;
71: #else
72: #endif
73:
74: if ((cp = getenv ("DISHPARENT")) == NULLCP) {
75: (void) sprintf (parent, "%d", getppid ());
76: (void) setenv ("DISHPARENT", cp = parent);
77: }
78:
79:
80: if (sscanf (cp, "%d", &parent_pid) != 1) {
81: (void) fprintf (stderr,"DISHPARENT malformed");
82: return (NOTOK);
83: }
84:
85: #ifdef SOCKETS
86: if (get_dish_sock (sin) != 0)
87: return (NOTOK);
88:
89: if ((sd = start_tcp_server (sin, SOMAXCONN, 0, 0)) == NOTOK) {
90: perror ("start_tcp_server");
91: return NOTOK;
92: }
93: #ifdef FIOCLEX
94: (void) ioctl (sd, FIOCLEX, NULLCP);
95: #endif
96: #else
97:
98: if ((cp = getenv ("DISHPROC")) == NULL) {
99: (void) fprintf (stderr, "no DISHPROC in environment\n");
100: return (NOTOK);
101: }
102:
103: (void) strcpy (retpipe, cp);
104: (void) umask (0);
105:
106: if ((fd = open (retpipe, O_RDONLY)) < 0) {
107: (void) mknod (retpipe, S_IFIFO | 0600, 0);
108: if ((fd = open (retpipe, O_RDONLY)) < 0) {
109: (void) fprintf (stderr, "ropen failed\n");
110: (void) unlink (retpipe);
111: return (NOTOK);
112: }
113: }
114:
115: if ((wfd = open (retpipe, O_WRONLY)) < 0) {
116: (void) fprintf (stderr, "wr open failed\n");
117: (void) unlink (retpipe);
118: (void) close (fd);
119: return (NOTOK);
120: }
121: #endif
122:
123: #ifdef SETSID
124: (void) setsid ();
125: #endif
126: #ifdef TIOCNOTTY
127: {
128: int xsd;
129:
130: if ((xsd = open ("/dev/tty", O_RDWR)) != NOTOK) {
131: (void) ioctl (xsd, TIOCNOTTY, NULLCP);
132: (void) close (xsd);
133: }
134: }
135: #else
136: #ifdef SYS5
137: (void) setpgrp ();
138: (void) signal (SIGINT, SIG_IGN);
139: (void) signal (SIGQUIT, SIG_IGN);
140: #endif
141: #endif
142: return (OK);
143: }
144:
145: exit_pipe ()
146: {
147: #ifdef SOCKETS
148: (void) close_tcp_socket (sd);
149: #else
150: (void) close (fd);
151: (void) close (wfd);
152: (void) unlink (retpipe);
153: #endif
154: }
155:
156: read_pipe (buf,len)
157: char * buf;
158: int len;
159: {
160: #ifdef SOCKETS
161: struct sockaddr_in sock;
162:
163: while ((sd_current = join_tcp_client (sd, &sock)) == NOTOK) {
164: if (errno != EINTR) {
165: perror("join_tcp_client");
166: return (-1);
167: }
168: }
169: #ifdef FIOCLEX
170: (void) ioctl (sd_current, FIOCLEX, NULLCP);
171: #endif
172: #endif
173: return (read_pipe_aux(buf,len));
174: }
175:
176:
177: read_pipe_aux (buf,len)
178: char * buf;
179: int len;
180: {
181: int res;
182:
183: *buf = '\0';
184: #ifdef SOCKETS
185: if((res = recv(sd_current, buf, len, 0)) == -1) {
186: perror ("recv");
187: (void) close (sd_current);
188: return (-1);
189: }
190: #else
191: if ((res = read (fd, buf, len)) <= 0) {
192: perror ("read error");
193: reopen_ret ();
194: return (-1);
195: }
196: #endif
197:
198: *(buf + res) = 0;
199: return (res);
200: }
201:
202:
203:
204:
205: #ifndef SOCKETS
206: send_pipe (buf)
207: char * buf;
208: {
209: send_pipe_aux (buf);
210:
211: (void) close (file);
212: reopen_ret ();
213: }
214: #endif
215:
216: send_pipe_aux (buf)
217: char * buf;
218: {
219: int res,i;
220:
221: i = strlen (buf);
222:
223: #ifndef SOCKETS
224: if ((file = open (inbuf, O_WRONLY)) <= 0) {
225: (void) fprintf (stderr, "error %s on %s\n",sys_errname (errno), inbuf);
226: reopen_ret ();
227: return;
228: }
229: #endif
230:
231: while (i > 0) {
232: #ifdef SOCKETS
233: if ( (res= send(sd_current, buf, i, 0)) == -1) {
234: perror("send");
235: (void) close (sd_current);
236: return;
237: }
238: #else
239: if ((res = write (file, buf, MIN (BUFSIZ,i))) == -1 ) {
240: (void) fprintf (stderr,"result write error (2)\n");
241: reopen_ret ();
242: return;
243: }
244: #endif
245: buf += res, i -= res;
246: }
247: }
248:
249:
250: #ifdef SOCKETS
251: get_dish_sock (isock)
252: struct sockaddr_in *isock;
253: {
254: char * getenv ();
255: char * ptr;
256: char buffer [BUFSIZ];
257: int portno;
258: char *dp;
259: register struct hostent *hp;
260:
261: if ((ptr = getenv ("DISHPROC")) == NULLCP) {
262: #ifdef notanymore
263: char *cp,
264: #endif
265: portno = (getppid () & 0xffff) | 0x8000;
266: #ifdef notanymore
267: if ((hp = gethostbystring (cp = getlocalhost ())) == NULL) {
268: (void) fprintf (stderr,"%s: unknown host", cp);
269: return (-1);
270: }
271: (void) sprintf (buffer, "%s %d",
272: inet_ntoa (*(struct in_addr *) hp -> h_addr),
273: portno);
274: #else
275: (void) sprintf (buffer, "127.0.0.1 %d", portno);
276: #endif
277: (void) setenv ("DISHPROC", ptr = buffer);
278: }
279:
280: if ((dp = index (ptr, ' ')) == NULLCP || sscanf (dp + 1, "%d", &portno) != 1) {
281: (void) fprintf (stderr,"DISHPROC malformed");
282: return (-1);
283: }
284: *dp = NULL;
285:
286: if ((hp = gethostbystring (ptr)) == NULL) {
287: (void) fprintf (stderr,"%s: unknown host in DISHPROC", ptr);
288: return (-1);
289: }
290: *dp = ' ';
291:
292: bzero ((char *) isock, sizeof *isock);
293: isock -> sin_family = hp -> h_addrtype;
294: isock -> sin_port = htons ((u_short) portno);
295: inaddr_copy (hp, isock);
296:
297: return (0);
298:
299: }
300:
301: #else
302:
303: reopen_ret ()
304: {
305: (void) close (fd);
306: (void) close (wfd);
307:
308: if ((fd = open (retpipe, O_RDONLY)) < 0) {
309: if ( errno == EINTR ) {
310: reopen_ret ();
311: return;
312: }
313: (void) fprintf (stderr, "re-ropen failed\n");
314: (void) unlink (retpipe);
315: exit (-72);
316: }
317: if ((wfd = open (retpipe, O_WRONLY)) < 0) {
318: (void) fprintf (stderr, "re-wr open failed\n");
319: (void) unlink (retpipe);
320: (void) close (fd);
321: exit (-73);
322: }
323: }
324: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.