xref: /onnv-gate/usr/src/uts/common/syscall/pipe.c (revision 5331:3047ad28a67b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*5331Samw  * Common Development and Distribution License (the "License").
6*5331Samw  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*5331Samw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* from SVr4.0 1.11 */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/sysmacros.h>
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <sys/systm.h>
360Sstevel@tonic-gate #include <sys/cred.h>
370Sstevel@tonic-gate #include <sys/user.h>
380Sstevel@tonic-gate #include <sys/vnode.h>
390Sstevel@tonic-gate #include <sys/file.h>
400Sstevel@tonic-gate #include <sys/stream.h>
410Sstevel@tonic-gate #include <sys/strsubr.h>
420Sstevel@tonic-gate #include <sys/errno.h>
430Sstevel@tonic-gate #include <sys/debug.h>
440Sstevel@tonic-gate #include <sys/fs/fifonode.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * This is the loadable module wrapper.
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate #include <sys/modctl.h>
500Sstevel@tonic-gate #include <sys/syscall.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate char _depends_on[] = "fs/fifofs";
530Sstevel@tonic-gate 
540Sstevel@tonic-gate longlong_t pipe();
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static struct sysent pipe_sysent = {
570Sstevel@tonic-gate 	0,
580Sstevel@tonic-gate 	SE_32RVAL1 | SE_32RVAL2 | SE_NOUNLOAD | SE_ARGC,
590Sstevel@tonic-gate 	(int (*)())pipe
600Sstevel@tonic-gate };
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate  * Module linkage information for the kernel.
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate static struct modlsys modlsys = {
660Sstevel@tonic-gate 	&mod_syscallops, "pipe(2) syscall", &pipe_sysent
670Sstevel@tonic-gate };
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
700Sstevel@tonic-gate static struct modlsys modlsys32 = {
710Sstevel@tonic-gate 	&mod_syscallops32, "32-bit pipe(2) syscall", &pipe_sysent
720Sstevel@tonic-gate };
730Sstevel@tonic-gate #endif
740Sstevel@tonic-gate 
750Sstevel@tonic-gate static struct modlinkage modlinkage = {
760Sstevel@tonic-gate 	MODREV_1,
770Sstevel@tonic-gate 	&modlsys,
780Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
790Sstevel@tonic-gate 	&modlsys32,
800Sstevel@tonic-gate #endif
810Sstevel@tonic-gate 	NULL
820Sstevel@tonic-gate };
830Sstevel@tonic-gate 
840Sstevel@tonic-gate int
_init(void)850Sstevel@tonic-gate _init(void)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	return (mod_install(&modlinkage));
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate int
_fini(void)910Sstevel@tonic-gate _fini(void)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	return (EBUSY);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate 
960Sstevel@tonic-gate int
_info(struct modinfo * modinfop)970Sstevel@tonic-gate _info(struct modinfo *modinfop)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate  * pipe(2) system call.
1040Sstevel@tonic-gate  * Create a pipe by connecting two streams together. Associate
1050Sstevel@tonic-gate  * each end of the pipe with a vnode, a file descriptor and
1060Sstevel@tonic-gate  * one of the streams.
1070Sstevel@tonic-gate  */
1080Sstevel@tonic-gate longlong_t
pipe()1090Sstevel@tonic-gate pipe()
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate 	vnode_t *vp1, *vp2;
1120Sstevel@tonic-gate 	struct file *fp1, *fp2;
1130Sstevel@tonic-gate 	int error = 0;
1140Sstevel@tonic-gate 	int fd1, fd2;
1150Sstevel@tonic-gate 	rval_t	r;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	/*
1180Sstevel@tonic-gate 	 * Allocate and initialize two vnodes.
1190Sstevel@tonic-gate 	 */
1200Sstevel@tonic-gate 	makepipe(&vp1, &vp2);
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	/*
1230Sstevel@tonic-gate 	 * Allocate and initialize two file table entries and two
1240Sstevel@tonic-gate 	 * file pointers. Each file pointer is open for read and
1250Sstevel@tonic-gate 	 * write.
1260Sstevel@tonic-gate 	 */
1270Sstevel@tonic-gate 	if (error = falloc(vp1, FWRITE|FREAD, &fp1, &fd1)) {
1280Sstevel@tonic-gate 		VN_RELE(vp1);
1290Sstevel@tonic-gate 		VN_RELE(vp2);
1300Sstevel@tonic-gate 		return ((longlong_t)set_errno(error));
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	if (error = falloc(vp2, FWRITE|FREAD, &fp2, &fd2))
1340Sstevel@tonic-gate 		goto out2;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	/*
1370Sstevel@tonic-gate 	 * Create two stream heads and attach to each vnode.
1380Sstevel@tonic-gate 	 */
1390Sstevel@tonic-gate 	if (error = fifo_stropen(&vp1, FWRITE|FREAD, fp1->f_cred, 0, 0))
1400Sstevel@tonic-gate 		goto out;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	if (error = fifo_stropen(&vp2, FWRITE|FREAD, fp2->f_cred, 0, 0)) {
1430Sstevel@tonic-gate 		(void) VOP_CLOSE(vp1, FWRITE|FREAD, 1, (offset_t)0,
144*5331Samw 		    fp1->f_cred, NULL);
1450Sstevel@tonic-gate 		goto out;
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	strmate(vp1, vp2);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	VTOF(vp1)->fn_ino = VTOF(vp2)->fn_ino = fifogetid();
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	/*
1530Sstevel@tonic-gate 	 * Now fill in the entries that falloc reserved
1540Sstevel@tonic-gate 	 */
1550Sstevel@tonic-gate 	mutex_exit(&fp1->f_tlock);
1560Sstevel@tonic-gate 	mutex_exit(&fp2->f_tlock);
1570Sstevel@tonic-gate 	setf(fd1, fp1);
1580Sstevel@tonic-gate 	setf(fd2, fp2);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	/*
1610Sstevel@tonic-gate 	 * Return the file descriptors to the user. They now
1620Sstevel@tonic-gate 	 * point to two different vnodes which have different
1630Sstevel@tonic-gate 	 * stream heads.
1640Sstevel@tonic-gate 	 */
1650Sstevel@tonic-gate 	r.r_val1 = fd1;
1660Sstevel@tonic-gate 	r.r_val2 = fd2;
1670Sstevel@tonic-gate 	return (r.r_vals);
1680Sstevel@tonic-gate out:
1690Sstevel@tonic-gate 	unfalloc(fp2);
1700Sstevel@tonic-gate 	setf(fd2, NULL);
1710Sstevel@tonic-gate out2:
1720Sstevel@tonic-gate 	unfalloc(fp1);
1730Sstevel@tonic-gate 	setf(fd1, NULL);
1740Sstevel@tonic-gate 	VN_RELE(vp1);
1750Sstevel@tonic-gate 	VN_RELE(vp2);
1760Sstevel@tonic-gate 	return ((longlong_t)set_errno(error));
1770Sstevel@tonic-gate }
178