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