xref: /illumos-gate/usr/src/uts/common/syscall/pipe.c (revision 5eaceb49)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5da6c28aaSamw  * Common Development and Distribution License (the "License").
6da6c28aaSamw  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
225dbfd19aSTheo Schlossnagle  * Copyright 2013 OmniTI Computer Consulting, Inc. All rights reserved.
23da6c28aaSamw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
2589b43686SBayard Bell  * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include <sys/systm.h>
367c478bd9Sstevel@tonic-gate #include <sys/cred.h>
377c478bd9Sstevel@tonic-gate #include <sys/user.h>
387c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
397c478bd9Sstevel@tonic-gate #include <sys/file.h>
407c478bd9Sstevel@tonic-gate #include <sys/stream.h>
417c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
427c478bd9Sstevel@tonic-gate #include <sys/errno.h>
437c478bd9Sstevel@tonic-gate #include <sys/debug.h>
447c478bd9Sstevel@tonic-gate #include <sys/fs/fifonode.h>
455dbfd19aSTheo Schlossnagle #include <sys/fcntl.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * This is the loadable module wrapper.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
517c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
527c478bd9Sstevel@tonic-gate 
535dbfd19aSTheo Schlossnagle int pipe(intptr_t fds, int);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static struct sysent pipe_sysent = {
565dbfd19aSTheo Schlossnagle 	2,
575dbfd19aSTheo Schlossnagle 	SE_ARGC | SE_32RVAL1 | SE_NOUNLOAD,
587c478bd9Sstevel@tonic-gate 	(int (*)())pipe
597c478bd9Sstevel@tonic-gate };
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate static struct modlsys modlsys = {
657c478bd9Sstevel@tonic-gate 	&mod_syscallops, "pipe(2) syscall", &pipe_sysent
667c478bd9Sstevel@tonic-gate };
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
697c478bd9Sstevel@tonic-gate static struct modlsys modlsys32 = {
707c478bd9Sstevel@tonic-gate 	&mod_syscallops32, "32-bit pipe(2) syscall", &pipe_sysent
717c478bd9Sstevel@tonic-gate };
727c478bd9Sstevel@tonic-gate #endif
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
757c478bd9Sstevel@tonic-gate 	MODREV_1,
767c478bd9Sstevel@tonic-gate 	&modlsys,
777c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
787c478bd9Sstevel@tonic-gate 	&modlsys32,
797c478bd9Sstevel@tonic-gate #endif
807c478bd9Sstevel@tonic-gate 	NULL
817c478bd9Sstevel@tonic-gate };
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate int
_init(void)847c478bd9Sstevel@tonic-gate _init(void)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate int
_fini(void)907c478bd9Sstevel@tonic-gate _fini(void)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	return (EBUSY);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)967c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * pipe(2) system call.
1037c478bd9Sstevel@tonic-gate  * Create a pipe by connecting two streams together. Associate
1047c478bd9Sstevel@tonic-gate  * each end of the pipe with a vnode, a file descriptor and
1057c478bd9Sstevel@tonic-gate  * one of the streams.
1067c478bd9Sstevel@tonic-gate  */
1075dbfd19aSTheo Schlossnagle int
pipe(intptr_t arg,int flags)1085dbfd19aSTheo Schlossnagle pipe(intptr_t arg, int flags)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	vnode_t *vp1, *vp2;
1117c478bd9Sstevel@tonic-gate 	struct file *fp1, *fp2;
1127c478bd9Sstevel@tonic-gate 	int error = 0;
1135dbfd19aSTheo Schlossnagle 	int flag1, flag2, iflags;
1147c478bd9Sstevel@tonic-gate 	int fd1, fd2;
1157c478bd9Sstevel@tonic-gate 
1165dbfd19aSTheo Schlossnagle 	/*
1175dbfd19aSTheo Schlossnagle 	 * Validate allowed flags.
1185dbfd19aSTheo Schlossnagle 	 */
119*5eaceb49STheo Schlossnagle 	if ((flags & ~(FCLOEXEC|FNONBLOCK)) != 0) {
1205dbfd19aSTheo Schlossnagle 		return (set_errno(EINVAL));
1215dbfd19aSTheo Schlossnagle 	}
1227c478bd9Sstevel@tonic-gate 	/*
1237c478bd9Sstevel@tonic-gate 	 * Allocate and initialize two vnodes.
1247c478bd9Sstevel@tonic-gate 	 */
1257c478bd9Sstevel@tonic-gate 	makepipe(&vp1, &vp2);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	/*
1287c478bd9Sstevel@tonic-gate 	 * Allocate and initialize two file table entries and two
1297c478bd9Sstevel@tonic-gate 	 * file pointers. Each file pointer is open for read and
1307c478bd9Sstevel@tonic-gate 	 * write.
1317c478bd9Sstevel@tonic-gate 	 */
1327c478bd9Sstevel@tonic-gate 	if (error = falloc(vp1, FWRITE|FREAD, &fp1, &fd1)) {
1337c478bd9Sstevel@tonic-gate 		VN_RELE(vp1);
1347c478bd9Sstevel@tonic-gate 		VN_RELE(vp2);
1355dbfd19aSTheo Schlossnagle 		return (set_errno(error));
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	if (error = falloc(vp2, FWRITE|FREAD, &fp2, &fd2))
1397c478bd9Sstevel@tonic-gate 		goto out2;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	/*
1427c478bd9Sstevel@tonic-gate 	 * Create two stream heads and attach to each vnode.
1437c478bd9Sstevel@tonic-gate 	 */
1447c478bd9Sstevel@tonic-gate 	if (error = fifo_stropen(&vp1, FWRITE|FREAD, fp1->f_cred, 0, 0))
1457c478bd9Sstevel@tonic-gate 		goto out;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if (error = fifo_stropen(&vp2, FWRITE|FREAD, fp2->f_cred, 0, 0)) {
1487c478bd9Sstevel@tonic-gate 		(void) VOP_CLOSE(vp1, FWRITE|FREAD, 1, (offset_t)0,
149da6c28aaSamw 		    fp1->f_cred, NULL);
1507c478bd9Sstevel@tonic-gate 		goto out;
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	strmate(vp1, vp2);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	VTOF(vp1)->fn_ino = VTOF(vp2)->fn_ino = fifogetid();
1567c478bd9Sstevel@tonic-gate 
1575dbfd19aSTheo Schlossnagle 	/*
1585dbfd19aSTheo Schlossnagle 	 * Set the O_NONBLOCK flag if requested.
1595dbfd19aSTheo Schlossnagle 	 */
1605dbfd19aSTheo Schlossnagle 	if (flags & FNONBLOCK) {
1615dbfd19aSTheo Schlossnagle 		flag1 = fp1->f_flag;
1625dbfd19aSTheo Schlossnagle 		flag2 = fp2->f_flag;
1635dbfd19aSTheo Schlossnagle 		iflags = flags & FNONBLOCK;
1645dbfd19aSTheo Schlossnagle 
1655dbfd19aSTheo Schlossnagle 		if (error = VOP_SETFL(vp1, flag1, iflags, fp1->f_cred, NULL)) {
1665dbfd19aSTheo Schlossnagle 			goto out_vop_close;
1675dbfd19aSTheo Schlossnagle 		}
1685dbfd19aSTheo Schlossnagle 		fp1->f_flag |= iflags;
1695dbfd19aSTheo Schlossnagle 
1705dbfd19aSTheo Schlossnagle 		if (error = VOP_SETFL(vp2, flag2, iflags, fp2->f_cred, NULL)) {
1715dbfd19aSTheo Schlossnagle 			goto out_vop_close;
1725dbfd19aSTheo Schlossnagle 		}
1735dbfd19aSTheo Schlossnagle 		fp2->f_flag |= iflags;
1745dbfd19aSTheo Schlossnagle 	}
1755dbfd19aSTheo Schlossnagle 
1765dbfd19aSTheo Schlossnagle 	/*
1775dbfd19aSTheo Schlossnagle 	 * Return the file descriptors to the user. They now
1785dbfd19aSTheo Schlossnagle 	 * point to two different vnodes which have different
1795dbfd19aSTheo Schlossnagle 	 * stream heads.
1805dbfd19aSTheo Schlossnagle 	 */
1815dbfd19aSTheo Schlossnagle 	if (copyout(&fd1, &((int *)arg)[0], sizeof (int)) ||
1825dbfd19aSTheo Schlossnagle 	    copyout(&fd2, &((int *)arg)[1], sizeof (int))) {
1835dbfd19aSTheo Schlossnagle 		error = EFAULT;
1845dbfd19aSTheo Schlossnagle 		goto out_vop_close;
1855dbfd19aSTheo Schlossnagle 	}
1865dbfd19aSTheo Schlossnagle 
1877c478bd9Sstevel@tonic-gate 	/*
1887c478bd9Sstevel@tonic-gate 	 * Now fill in the entries that falloc reserved
1897c478bd9Sstevel@tonic-gate 	 */
1907c478bd9Sstevel@tonic-gate 	mutex_exit(&fp1->f_tlock);
1917c478bd9Sstevel@tonic-gate 	mutex_exit(&fp2->f_tlock);
1927c478bd9Sstevel@tonic-gate 	setf(fd1, fp1);
1937c478bd9Sstevel@tonic-gate 	setf(fd2, fp2);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	/*
1965dbfd19aSTheo Schlossnagle 	 * Optionally set the FCLOEXEC flag
1977c478bd9Sstevel@tonic-gate 	 */
1985dbfd19aSTheo Schlossnagle 	if ((flags & FCLOEXEC) != 0) {
1995dbfd19aSTheo Schlossnagle 		f_setfd(fd1, FD_CLOEXEC);
2005dbfd19aSTheo Schlossnagle 		f_setfd(fd2, FD_CLOEXEC);
2015dbfd19aSTheo Schlossnagle 	}
2025dbfd19aSTheo Schlossnagle 
2035dbfd19aSTheo Schlossnagle 	return (0);
2045dbfd19aSTheo Schlossnagle out_vop_close:
2055dbfd19aSTheo Schlossnagle 	(void) VOP_CLOSE(vp1, FWRITE|FREAD, 1, (offset_t)0, fp1->f_cred, NULL);
2065dbfd19aSTheo Schlossnagle 	(void) VOP_CLOSE(vp2, FWRITE|FREAD, 1, (offset_t)0, fp2->f_cred, NULL);
2077c478bd9Sstevel@tonic-gate out:
2087c478bd9Sstevel@tonic-gate 	setf(fd2, NULL);
2095dbfd19aSTheo Schlossnagle 	unfalloc(fp2);
2107c478bd9Sstevel@tonic-gate out2:
2117c478bd9Sstevel@tonic-gate 	setf(fd1, NULL);
2125dbfd19aSTheo Schlossnagle 	unfalloc(fp1);
2137c478bd9Sstevel@tonic-gate 	VN_RELE(vp1);
2147c478bd9Sstevel@tonic-gate 	VN_RELE(vp2);
2155dbfd19aSTheo Schlossnagle 	return (set_errno(error));
2167c478bd9Sstevel@tonic-gate }
217