Back to index

courier  0.68.2
spipe.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2000 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 
00007 #include      "config.h"
00008 #include      "spipe.h"
00009 
00010 #if HAVE_SPIPE_SOCKETPAIR
00011 
00012 #include <sys/types.h>
00013 #include <sys/socket.h>
00014 
00015 int libmail_streampipe(int fd[2])
00016 {
00017        return (socketpair(PF_UNIX, SOCK_STREAM, 0, fd));
00018 }
00019 #endif
00020 
00021 #if HAVE_SPIPE_SVR3
00022 
00023 /* This is, basically, Stevens */
00024 
00025 #include      <sys/types.h>
00026 #include      <sys/stream.h>              /* defines queue_t */
00027 #include      <stropts.h>          /* defines struct strfdinsert */
00028 #include      <fcntl.h>
00029 
00030 #define       SPX_DEVICE    "/dev/spx"
00031 
00032 int                                /* return 0 if OK, -1 on error */
00033 libmail_streampipe(int fd[2])
00034               /* two file descriptors returned through here */
00035 {
00036        struct strfdinsert   ins;
00037        queue_t                     *pointer;
00038 
00039        /*
00040         * First open the stream clone device "/dev/spx" twice,
00041         * obtaining the two file descriptors.
00042         */
00043 
00044        if ( (fd[0] = open(SPX_DEVICE, O_RDWR)) < 0)
00045               return(-1);
00046 
00047        if ( (fd[1] = open(SPX_DEVICE, O_RDWR)) < 0) {
00048               close(fd[0]);
00049               return(-1);
00050        }
00051 
00052        /*
00053         * Now link these two streams together with an I_FDINSERT ioctl.
00054         */
00055 
00056        ins.ctlbuf.buf     = (char *) &pointer;   /* no ctl info, just the ptr */
00057        ins.ctlbuf.maxlen  = sizeof(queue_t *);
00058        ins.ctlbuf.len     = sizeof(queue_t *);
00059 
00060        ins.databuf.buf    = (char *) 0;   /* no data to send */
00061        ins.databuf.len    = -1; /* magic: must be -1, not 0, for stream pipe */
00062        ins.databuf.maxlen = 0;
00063 
00064        ins.fildes = fd[1];  /* the fd to connect with fd[0] */
00065        ins.flags  = 0;             /* nonpriority message */
00066        ins.offset = 0;             /* offset of pointer in control buffer */
00067 
00068        if (ioctl(fd[0], I_FDINSERT, (char * ) &ins) < 0) {
00069               close(fd[0]);
00070               close(fd[1]);
00071               return(-1);
00072        }
00073 
00074        return(0);           /* all OK */
00075 }
00076 
00077 #endif
00078 
00079 #if HAVE_SPIPE_SVR4
00080 
00081 #include      <stdio.h>
00082 #if    HAVE_UNISTD_H
00083 #include      <unistd.h>
00084 #endif
00085 #include      <stdlib.h>
00086 
00087 int    libmail_streampipe(int fd[2])
00088 {
00089        return (pipe(fd));
00090 }
00091 
00092 #endif