xref: /illumos-gate/usr/src/uts/common/syscall/pipe.c (revision da6c28aa)
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
5*da6c28aaSamw  * Common Development and Distribution License (the "License").
6*da6c28aaSamw  * 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 /*
22*da6c28aaSamw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* from SVr4.0 1.11 */
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>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * This is the loadable module wrapper.
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
507c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate char _depends_on[] = "fs/fifofs";
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate longlong_t pipe();
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static struct sysent pipe_sysent = {
577c478bd9Sstevel@tonic-gate 	0,
587c478bd9Sstevel@tonic-gate 	SE_32RVAL1 | SE_32RVAL2 | SE_NOUNLOAD | SE_ARGC,
597c478bd9Sstevel@tonic-gate 	(int (*)())pipe
607c478bd9Sstevel@tonic-gate };
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate static struct modlsys modlsys = {
667c478bd9Sstevel@tonic-gate 	&mod_syscallops, "pipe(2) syscall", &pipe_sysent
677c478bd9Sstevel@tonic-gate };
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
707c478bd9Sstevel@tonic-gate static struct modlsys modlsys32 = {
717c478bd9Sstevel@tonic-gate 	&mod_syscallops32, "32-bit pipe(2) syscall", &pipe_sysent
727c478bd9Sstevel@tonic-gate };
737c478bd9Sstevel@tonic-gate #endif
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
767c478bd9Sstevel@tonic-gate 	MODREV_1,
777c478bd9Sstevel@tonic-gate 	&modlsys,
787c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
797c478bd9Sstevel@tonic-gate 	&modlsys32,
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate 	NULL
827c478bd9Sstevel@tonic-gate };
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate int
857c478bd9Sstevel@tonic-gate _init(void)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate int
917c478bd9Sstevel@tonic-gate _fini(void)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	return (EBUSY);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate int
977c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * pipe(2) system call.
1047c478bd9Sstevel@tonic-gate  * Create a pipe by connecting two streams together. Associate
1057c478bd9Sstevel@tonic-gate  * each end of the pipe with a vnode, a file descriptor and
1067c478bd9Sstevel@tonic-gate  * one of the streams.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate longlong_t
1097c478bd9Sstevel@tonic-gate pipe()
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	vnode_t *vp1, *vp2;
1127c478bd9Sstevel@tonic-gate 	struct file *fp1, *fp2;
1137c478bd9Sstevel@tonic-gate 	int error = 0;
1147c478bd9Sstevel@tonic-gate 	int fd1, fd2;
1157c478bd9Sstevel@tonic-gate 	rval_t	r;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	/*
1187c478bd9Sstevel@tonic-gate 	 * Allocate and initialize two vnodes.
1197c478bd9Sstevel@tonic-gate 	 */
1207c478bd9Sstevel@tonic-gate 	makepipe(&vp1, &vp2);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	/*
1237c478bd9Sstevel@tonic-gate 	 * Allocate and initialize two file table entries and two
1247c478bd9Sstevel@tonic-gate 	 * file pointers. Each file pointer is open for read and
1257c478bd9Sstevel@tonic-gate 	 * write.
1267c478bd9Sstevel@tonic-gate 	 */
1277c478bd9Sstevel@tonic-gate 	if (error = falloc(vp1, FWRITE|FREAD, &fp1, &fd1)) {
1287c478bd9Sstevel@tonic-gate 		VN_RELE(vp1);
1297c478bd9Sstevel@tonic-gate 		VN_RELE(vp2);
1307c478bd9Sstevel@tonic-gate 		return ((longlong_t)set_errno(error));
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	if (error = falloc(vp2, FWRITE|FREAD, &fp2, &fd2))
1347c478bd9Sstevel@tonic-gate 		goto out2;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	/*
1377c478bd9Sstevel@tonic-gate 	 * Create two stream heads and attach to each vnode.
1387c478bd9Sstevel@tonic-gate 	 */
1397c478bd9Sstevel@tonic-gate 	if (error = fifo_stropen(&vp1, FWRITE|FREAD, fp1->f_cred, 0, 0))
1407c478bd9Sstevel@tonic-gate 		goto out;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	if (error = fifo_stropen(&vp2, FWRITE|FREAD, fp2->f_cred, 0, 0)) {
1437c478bd9Sstevel@tonic-gate 		(void) VOP_CLOSE(vp1, FWRITE|FREAD, 1, (offset_t)0,
144*da6c28aaSamw 		    fp1->f_cred, NULL);
1457c478bd9Sstevel@tonic-gate 		goto out;
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	strmate(vp1, vp2);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	VTOF(vp1)->fn_ino = VTOF(vp2)->fn_ino = fifogetid();
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/*
1537c478bd9Sstevel@tonic-gate 	 * Now fill in the entries that falloc reserved
1547c478bd9Sstevel@tonic-gate 	 */
1557c478bd9Sstevel@tonic-gate 	mutex_exit(&fp1->f_tlock);
1567c478bd9Sstevel@tonic-gate 	mutex_exit(&fp2->f_tlock);
1577c478bd9Sstevel@tonic-gate 	setf(fd1, fp1);
1587c478bd9Sstevel@tonic-gate 	setf(fd2, fp2);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	/*
1617c478bd9Sstevel@tonic-gate 	 * Return the file descriptors to the user. They now
1627c478bd9Sstevel@tonic-gate 	 * point to two different vnodes which have different
1637c478bd9Sstevel@tonic-gate 	 * stream heads.
1647c478bd9Sstevel@tonic-gate 	 */
1657c478bd9Sstevel@tonic-gate 	r.r_val1 = fd1;
1667c478bd9Sstevel@tonic-gate 	r.r_val2 = fd2;
1677c478bd9Sstevel@tonic-gate 	return (r.r_vals);
1687c478bd9Sstevel@tonic-gate out:
1697c478bd9Sstevel@tonic-gate 	unfalloc(fp2);
1707c478bd9Sstevel@tonic-gate 	setf(fd2, NULL);
1717c478bd9Sstevel@tonic-gate out2:
1727c478bd9Sstevel@tonic-gate 	unfalloc(fp1);
1737c478bd9Sstevel@tonic-gate 	setf(fd1, NULL);
1747c478bd9Sstevel@tonic-gate 	VN_RELE(vp1);
1757c478bd9Sstevel@tonic-gate 	VN_RELE(vp2);
1767c478bd9Sstevel@tonic-gate 	return ((longlong_t)set_errno(error));
1777c478bd9Sstevel@tonic-gate }
178