File: pipes.c

package info (click to toggle)
zmailer 2.99.55-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 19,516 kB
  • ctags: 9,694
  • sloc: ansic: 120,953; sh: 3,862; makefile: 3,166; perl: 2,695; python: 115; awk: 22
file content (109 lines) | stat: -rw-r--r-- 2,010 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 *	ZMailer 2.99.16+ Scheduler "bi-directional-pipe" routines
 *
 *	Copyright Matti Aarnio <mea@nic.funet.fi> 1995
 *
 */

#include "hostenv.h"
#include "scheduler.h"
#include "prototypes.h"

#ifdef HAVE_SOCKETPAIR

#include <sys/types.h>
#include <sys/socket.h>

/*
 *  Life is easy, we have a system with socketpair, we shall use them...
 */

int pipes_create(tochild, fromchild)
int tochild[2];
int fromchild[2];
{
	int rc = socketpair(PF_UNIX, SOCK_STREAM, 0, tochild);
	if (rc < 0) return rc;
	fromchild[0] = tochild[1];
	fromchild[1] = tochild[0];
	return 0;
}

void pipes_close_parent(tochild, fromchild)
int tochild[2];
int fromchild[2];
{
	close(tochild[0]); /*  same fd as  fromchild[1] */
}

void pipes_to_child_fds(tochild, fromchild)
int tochild[2];
int fromchild[2];
{
	if (tochild[0] != 0)
	  dup2(tochild[0],0);
	dup2(0,1);
	dup2(0,2);
	close(tochild[1]); /* Same as fromchild[0] */
}

void pipes_shutdown_child(fd)
int fd;
{
	/* We close the parent->child writer channel */
	shutdown(fd, 1 /* disable further send operations */);
}
#else /* not HAVE_SOCKETPAIR -- we have ordinary pipes then..
	 (someday we can add here SysV streams-pipes..)		*/

/*
 * Life is not sweet and simple, but rather hard as we have only
 * uni-directional FIFO-like pipes...
 */

int pipes_create(tochild, fromchild)
int tochild[2];
int fromchild[2];
{
	int rc;
	rc = epipe(tochild);
	if (rc < 0) {
	  return rc;
	}
	rc = epipe(fromchild);
	if (rc < 0) {
	  close(tochild[0]);
	  close(tochild[1]);
	}
	return rc;
}

void pipes_close_parent(tochild, fromchild)
int tochild[2];
int fromchild[2];
{
	close(tochild[0]);
	close(fromchild[1]);
}

void pipes_to_child_fds(tochild, fromchild)
int tochild[2];
int fromchild[2];
{
	if (tochild[0] != 0) {
	  dup2(tochild[0], 0);	/* STDIN channel */
	  close(tochild[0]);
	}
	if (fromchild[1] != 1) {
	  dup2(fromchild[1],1); /* STDOUT channel */
	  close(fromchild[1]);
	}
	dup2(1,2);		/* STDERR channel */
}

void pipes_shutdown_child(fd)
int fd;
{
	close(fd);
}
#endif