#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/cred.h>
#include <sys/user.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/stream.h>
#include <sys/strsubr.h>
#include <sys/errno.h>
#include <sys/debug.h>
#include <sys/fs/fifonode.h>
#include <sys/fcntl.h>
#include <sys/modctl.h>
#include <sys/syscall.h>
int pipe(intptr_t fds, int);
static struct sysent pipe_sysent = {
2,
SE_ARGC | SE_32RVAL1 | SE_NOUNLOAD,
(int (*)())pipe
};
static struct modlsys modlsys = {
&mod_syscallops, "pipe(2) syscall", &pipe_sysent
};
#ifdef _SYSCALL32_IMPL
static struct modlsys modlsys32 = {
&mod_syscallops32, "32-bit pipe(2) syscall", &pipe_sysent
};
#endif
static struct modlinkage modlinkage = {
MODREV_1,
&modlsys,
#ifdef _SYSCALL32_IMPL
&modlsys32,
#endif
NULL
};
int
_init(void)
{
return (mod_install(&modlinkage));
}
int
_fini(void)
{
return (EBUSY);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
int
pipe(intptr_t arg, int flags)
{
vnode_t *vp1, *vp2;
struct file *fp1, *fp2;
int error = 0;
int flag1, flag2, iflags;
int fd1, fd2;
if ((flags & ~(FCLOEXEC|FNONBLOCK|FCLOFORK)) != 0) {
return (set_errno(EINVAL));
}
makepipe(&vp1, &vp2);
if (error = falloc(vp1, FWRITE|FREAD, &fp1, &fd1)) {
VN_RELE(vp1);
VN_RELE(vp2);
return (set_errno(error));
}
if (error = falloc(vp2, FWRITE|FREAD, &fp2, &fd2))
goto out2;
if (error = fifo_stropen(&vp1, FWRITE|FREAD, fp1->f_cred, 0, 0))
goto out;
if (error = fifo_stropen(&vp2, FWRITE|FREAD, fp2->f_cred, 0, 0)) {
(void) VOP_CLOSE(vp1, FWRITE|FREAD, 1, (offset_t)0,
fp1->f_cred, NULL);
goto out;
}
strmate(vp1, vp2);
VTOF(vp1)->fn_ino = VTOF(vp2)->fn_ino = fifogetid();
if (flags & FNONBLOCK) {
flag1 = fp1->f_flag;
flag2 = fp2->f_flag;
iflags = flags & FNONBLOCK;
if (error = VOP_SETFL(vp1, flag1, iflags, fp1->f_cred, NULL)) {
goto out_vop_close;
}
fp1->f_flag |= iflags;
if (error = VOP_SETFL(vp2, flag2, iflags, fp2->f_cred, NULL)) {
goto out_vop_close;
}
fp2->f_flag |= iflags;
}
if (copyout(&fd1, &((int *)arg)[0], sizeof (int)) ||
copyout(&fd2, &((int *)arg)[1], sizeof (int))) {
error = EFAULT;
goto out_vop_close;
}
mutex_exit(&fp1->f_tlock);
mutex_exit(&fp2->f_tlock);
setf(fd1, fp1);
setf(fd2, fp2);
if ((flags & FCLOEXEC) != 0) {
f_setfd_or(fd1, FD_CLOEXEC);
f_setfd_or(fd2, FD_CLOEXEC);
}
if ((flags & FCLOFORK) != 0) {
f_setfd_or(fd1, FD_CLOFORK);
f_setfd_or(fd2, FD_CLOFORK);
}
return (0);
out_vop_close:
(void) VOP_CLOSE(vp1, FWRITE|FREAD, 1, (offset_t)0, fp1->f_cred, NULL);
(void) VOP_CLOSE(vp2, FWRITE|FREAD, 1, (offset_t)0, fp2->f_cred, NULL);
out:
setf(fd2, NULL);
unfalloc(fp2);
out2:
setf(fd1, NULL);
unfalloc(fp1);
VN_RELE(vp1);
VN_RELE(vp2);
return (set_errno(error));
}