File: spipe.c

package info (click to toggle)
courier 0.60.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 52,288 kB
  • ctags: 12,677
  • sloc: ansic: 165,348; cpp: 24,820; sh: 16,410; perl: 6,839; makefile: 3,621; yacc: 289; sed: 16
file content (93 lines) | stat: -rw-r--r-- 1,901 bytes parent folder | download | duplicates (6)
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
/*
** Copyright 2000 Double Precision, Inc.
** See COPYING for distribution information.
*/

static const char rcsid[]="$Id: spipe.c,v 1.3 2003/01/05 04:08:13 mrsam Exp $";

#include	"config.h"
#include	"spipe.h"

#if HAVE_SPIPE_SOCKETPAIR

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

int libmail_streampipe(int fd[2])
{
	return (socketpair(PF_UNIX, SOCK_STREAM, 0, fd));
}
#endif

#if HAVE_SPIPE_SVR3

/* This is, basically, Stevens */

#include	<sys/types.h>
#include	<sys/stream.h>		/* defines queue_t */
#include	<stropts.h>		/* defines struct strfdinsert */
#include	<fcntl.h>

#define	SPX_DEVICE	"/dev/spx"

int					/* return 0 if OK, -1 on error */
libmail_streampipe(int fd[2])
		/* two file descriptors returned through here */
{
	struct strfdinsert	ins;
	queue_t			*pointer;

	/*
	 * First open the stream clone device "/dev/spx" twice,
	 * obtaining the two file descriptors.
	 */

	if ( (fd[0] = open(SPX_DEVICE, O_RDWR)) < 0)
		return(-1);

	if ( (fd[1] = open(SPX_DEVICE, O_RDWR)) < 0) {
		close(fd[0]);
		return(-1);
	}

	/*
	 * Now link these two streams together with an I_FDINSERT ioctl.
	 */

	ins.ctlbuf.buf     = (char *) &pointer;	/* no ctl info, just the ptr */
	ins.ctlbuf.maxlen  = sizeof(queue_t *);
	ins.ctlbuf.len     = sizeof(queue_t *);

	ins.databuf.buf    = (char *) 0;	/* no data to send */
	ins.databuf.len    = -1; /* magic: must be -1, not 0, for stream pipe */
	ins.databuf.maxlen = 0;

	ins.fildes = fd[1];	/* the fd to connect with fd[0] */
	ins.flags  = 0;		/* nonpriority message */
	ins.offset = 0;		/* offset of pointer in control buffer */

	if (ioctl(fd[0], I_FDINSERT, (char * ) &ins) < 0) {
		close(fd[0]);
		close(fd[1]);
		return(-1);
	}

	return(0);		/* all OK */
}

#endif

#if HAVE_SPIPE_SVR4

#include	<stdio.h>
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif
#include	<stdlib.h>

int	libmail_streampipe(int fd[2])
{
	return (pipe(fd));
}

#endif