xref: /titanic_53/usr/src/cmd/mdb/common/mdb/mdb_pipeio.c (revision 8a6a72fd51dfee674c6daa8a222f68b1532516ab)
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*8a6a72fdSaf  * Common Development and Distribution License (the "License").
6*8a6a72fdSaf  * 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*8a6a72fdSaf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Pipe I/O Backend
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * In order to implement dcmd pipelines, we provide a pipe i/o backend that
327c478bd9Sstevel@tonic-gate  * can be used to connect two mdb_iob structures (a read and write end).
337c478bd9Sstevel@tonic-gate  * This backend is selected when mdb_iob_pipe is used to construct a pair of
347c478bd9Sstevel@tonic-gate  * iobs.  Each iob points at the same i/o backend (the pipe i/o), and the
357c478bd9Sstevel@tonic-gate  * backend manages a circular fixed-size buffer which moves data between
367c478bd9Sstevel@tonic-gate  * the reader and writer.  The caller provides read and write-side service
377c478bd9Sstevel@tonic-gate  * routines that are expected to perform context switching (see mdb_context.c).
387c478bd9Sstevel@tonic-gate  * The pipe implementation is relatively simple: the writer calls any of the
397c478bd9Sstevel@tonic-gate  * mdb_iob_* routines to fill the write-side iob, and when this iob needs to
407c478bd9Sstevel@tonic-gate  * flush data to the underlying i/o, pio_write() below is called.  This
417c478bd9Sstevel@tonic-gate  * routine copies data into the pipe buffer until no more free space is
427c478bd9Sstevel@tonic-gate  * available, and then calls the read-side service routine (presuming that
437c478bd9Sstevel@tonic-gate  * when it returns, more free space will be available).  On the read-side,
447c478bd9Sstevel@tonic-gate  * pio_read() copies data up from the pipe buffer into the read-side iob.
457c478bd9Sstevel@tonic-gate  * If pio_read() is called and the pipe buffer is empty, pio_read() calls
467c478bd9Sstevel@tonic-gate  * the write-side service routine to force the writer to produce more data.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
507c478bd9Sstevel@tonic-gate #include <stropts.h>
517c478bd9Sstevel@tonic-gate #include <limits.h>
527c478bd9Sstevel@tonic-gate 
53*8a6a72fdSaf #include <mdb/mdb.h>
547c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
557c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h>
567c478bd9Sstevel@tonic-gate #include <mdb/mdb_string.h>
577c478bd9Sstevel@tonic-gate #include <mdb/mdb_context.h>
587c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
597c478bd9Sstevel@tonic-gate #include <mdb/mdb_io_impl.h>
60*8a6a72fdSaf #include <mdb/mdb_frame.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate typedef struct pipe_data {
637c478bd9Sstevel@tonic-gate 	mdb_iobsvc_f *pipe_rdsvc;	/* Read-side service routine */
647c478bd9Sstevel@tonic-gate 	mdb_iob_t *pipe_rdiob;		/* Read-side i/o buffer */
657c478bd9Sstevel@tonic-gate 	mdb_iobsvc_f *pipe_wrsvc;	/* Write-side service routine */
667c478bd9Sstevel@tonic-gate 	mdb_iob_t *pipe_wriob;		/* Write-side i/o buffer */
677c478bd9Sstevel@tonic-gate 	char pipe_buf[BUFSIZ];		/* Ring buffer for pipe contents */
687c478bd9Sstevel@tonic-gate 	mdb_iob_ctx_t pipe_ctx;		/* Context data for service routines */
697c478bd9Sstevel@tonic-gate 	uint_t pipe_rdndx;		/* Next byte index for reading */
707c478bd9Sstevel@tonic-gate 	uint_t pipe_wrndx;		/* Next byte index for writing */
717c478bd9Sstevel@tonic-gate 	uint_t pipe_free;		/* Free space for writing in bytes */
727c478bd9Sstevel@tonic-gate 	uint_t pipe_used;		/* Used space for reading in bytes */
737c478bd9Sstevel@tonic-gate } pipe_data_t;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static ssize_t
pio_read(mdb_io_t * io,void * buf,size_t nbytes)777c478bd9Sstevel@tonic-gate pio_read(mdb_io_t *io, void *buf, size_t nbytes)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	pipe_data_t *pd = io->io_data;
807c478bd9Sstevel@tonic-gate 	size_t n, nleft;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	if (nbytes == 0)
837c478bd9Sstevel@tonic-gate 		return (0); /* return 0 for zero-length read */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	for (nleft = nbytes; nleft == nbytes; nleft -= n) {
867c478bd9Sstevel@tonic-gate 		if (pd->pipe_used == 0) {
877c478bd9Sstevel@tonic-gate 			if (pd->pipe_wriob != NULL) {
887c478bd9Sstevel@tonic-gate 				pd->pipe_wrsvc(pd->pipe_rdiob,
897c478bd9Sstevel@tonic-gate 				    pd->pipe_wriob, &pd->pipe_ctx);
907c478bd9Sstevel@tonic-gate 			}
917c478bd9Sstevel@tonic-gate 			if (pd->pipe_used == 0)
927c478bd9Sstevel@tonic-gate 				break;
937c478bd9Sstevel@tonic-gate 		}
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 		n = MIN(pd->pipe_used, nleft);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 		if (BUFSIZ - pd->pipe_rdndx < n) {
987c478bd9Sstevel@tonic-gate 			/*
997c478bd9Sstevel@tonic-gate 			 * Case 1: The amount to read overlaps the end of the
1007c478bd9Sstevel@tonic-gate 			 * circular buffer.  'n1' will be the amount to copy
1017c478bd9Sstevel@tonic-gate 			 * from the end of the buffer, and 'n2' will be the
1027c478bd9Sstevel@tonic-gate 			 * amount to copy from the beginning.  Note that since
1037c478bd9Sstevel@tonic-gate 			 * n <= pipe_used, it is impossible to read past
1047c478bd9Sstevel@tonic-gate 			 * pipe_wrndx into undefined territory.
1057c478bd9Sstevel@tonic-gate 			 */
1067c478bd9Sstevel@tonic-gate 			size_t n1 = BUFSIZ - pd->pipe_rdndx;
1077c478bd9Sstevel@tonic-gate 			size_t n2 = n - n1;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 			ASSERT(n2 <= pd->pipe_wrndx);
1107c478bd9Sstevel@tonic-gate 			bcopy(&pd->pipe_buf[pd->pipe_rdndx], buf, n1);
1117c478bd9Sstevel@tonic-gate 			buf = (char *)buf + n1;
1127c478bd9Sstevel@tonic-gate 			bcopy(&pd->pipe_buf[0], buf, n2);
1137c478bd9Sstevel@tonic-gate 			buf = (char *)buf + n2;
1147c478bd9Sstevel@tonic-gate 		} else {
1157c478bd9Sstevel@tonic-gate 			/*
1167c478bd9Sstevel@tonic-gate 			 * Case 2: The easy case.  Simply copy the data over
1177c478bd9Sstevel@tonic-gate 			 * to the buffer.
1187c478bd9Sstevel@tonic-gate 			 */
1197c478bd9Sstevel@tonic-gate 			bcopy(&pd->pipe_buf[pd->pipe_rdndx], buf, n);
1207c478bd9Sstevel@tonic-gate 			buf = (char *)buf + n;
1217c478bd9Sstevel@tonic-gate 		}
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 		pd->pipe_rdndx = (pd->pipe_rdndx + n) % BUFSIZ;
1247c478bd9Sstevel@tonic-gate 		pd->pipe_free += n;
1257c478bd9Sstevel@tonic-gate 		pd->pipe_used -= n;
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	/*
1297c478bd9Sstevel@tonic-gate 	 * If we have a writer, but pipe_wrsvc failed to produce any data,
1307c478bd9Sstevel@tonic-gate 	 * we return EAGAIN.  If there is no writer, then return 0 for EOF.
1317c478bd9Sstevel@tonic-gate 	 */
1327c478bd9Sstevel@tonic-gate 	if (nleft == nbytes) {
1337c478bd9Sstevel@tonic-gate 		if (pd->pipe_wriob != NULL)
1347c478bd9Sstevel@tonic-gate 			return (set_errno(EAGAIN));
1357c478bd9Sstevel@tonic-gate 		else
1367c478bd9Sstevel@tonic-gate 			return (0);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	return (nbytes - nleft);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate static ssize_t
pio_write(mdb_io_t * io,const void * buf,size_t nbytes)1437c478bd9Sstevel@tonic-gate pio_write(mdb_io_t *io, const void *buf, size_t nbytes)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	pipe_data_t *pd = io->io_data;
1467c478bd9Sstevel@tonic-gate 	size_t n, nleft;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	if (pd->pipe_rdiob == NULL)
1497c478bd9Sstevel@tonic-gate 		return (set_errno(EPIPE)); /* fail with EPIPE if no reader */
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	for (nleft = nbytes; nleft != 0; nleft -= n) {
1527c478bd9Sstevel@tonic-gate 		if (pd->pipe_free == 0) {
1537c478bd9Sstevel@tonic-gate 			pd->pipe_rdsvc(pd->pipe_rdiob,
1547c478bd9Sstevel@tonic-gate 			    pd->pipe_wriob, &pd->pipe_ctx);
1557c478bd9Sstevel@tonic-gate 			if (pd->pipe_free == 0)
1567c478bd9Sstevel@tonic-gate 				break; /* if nothing consumed by reader, exit */
1577c478bd9Sstevel@tonic-gate 		}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 		n = MIN(pd->pipe_free, nleft);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 		if (BUFSIZ - pd->pipe_wrndx < n) {
1627c478bd9Sstevel@tonic-gate 			/*
1637c478bd9Sstevel@tonic-gate 			 * Case 1: The data will overlap the circular buffer
1647c478bd9Sstevel@tonic-gate 			 * boundary. In this case, 'n1' will be the number of
1657c478bd9Sstevel@tonic-gate 			 * bytes to put at the end of the buffer, and 'n2' will
1667c478bd9Sstevel@tonic-gate 			 * be the number of bytes to put at the beginning.
1677c478bd9Sstevel@tonic-gate 			 * Note that since n <= pipe_free, it is impossible to
1687c478bd9Sstevel@tonic-gate 			 * overlap rdndx with the initial data.
1697c478bd9Sstevel@tonic-gate 			 */
1707c478bd9Sstevel@tonic-gate 			size_t n1 = BUFSIZ - pd->pipe_wrndx;
1717c478bd9Sstevel@tonic-gate 			size_t n2 = n - n1;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 			ASSERT(n2 <= pd->pipe_rdndx);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 			bcopy(buf, &pd->pipe_buf[pd->pipe_wrndx], n1);
1767c478bd9Sstevel@tonic-gate 			buf = (const char *)buf + n1;
1777c478bd9Sstevel@tonic-gate 			bcopy(buf, &pd->pipe_buf[0], n2);
1787c478bd9Sstevel@tonic-gate 			buf = (const char *)buf + n2;
1797c478bd9Sstevel@tonic-gate 		} else {
1807c478bd9Sstevel@tonic-gate 			/*
1817c478bd9Sstevel@tonic-gate 			 * Case 2: The easy case.  Simply copy the data into
1827c478bd9Sstevel@tonic-gate 			 * the buffer.
1837c478bd9Sstevel@tonic-gate 			 */
1847c478bd9Sstevel@tonic-gate 			bcopy(buf, &pd->pipe_buf[pd->pipe_wrndx], n);
1857c478bd9Sstevel@tonic-gate 			buf = (const char *)buf + n;
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		pd->pipe_wrndx = (pd->pipe_wrndx + n) % BUFSIZ;
1897c478bd9Sstevel@tonic-gate 		pd->pipe_free -= n;
1907c478bd9Sstevel@tonic-gate 		pd->pipe_used += n;
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	if (nleft == nbytes && nbytes != 0)
1947c478bd9Sstevel@tonic-gate 		return (set_errno(EAGAIN));
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	return (nbytes - nleft);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /*
2007c478bd9Sstevel@tonic-gate  * Provide support for STREAMS-style write-side flush ioctl.  This can be
2017c478bd9Sstevel@tonic-gate  * used by the caller to force a context switch to the read-side.
2027c478bd9Sstevel@tonic-gate  */
2037c478bd9Sstevel@tonic-gate static int
pio_ctl(mdb_io_t * io,int req,void * arg)2047c478bd9Sstevel@tonic-gate pio_ctl(mdb_io_t *io, int req, void *arg)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	pipe_data_t *pd = io->io_data;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	if (io->io_next != NULL)
2097c478bd9Sstevel@tonic-gate 		return (IOP_CTL(io->io_next, req, arg));
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if (req != I_FLUSH || (intptr_t)arg != FLUSHW)
2127c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (pd->pipe_used != 0)
2157c478bd9Sstevel@tonic-gate 		pd->pipe_rdsvc(pd->pipe_rdiob, pd->pipe_wriob, &pd->pipe_ctx);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	return (0);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate static void
pio_close(mdb_io_t * io)2217c478bd9Sstevel@tonic-gate pio_close(mdb_io_t *io)
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	mdb_free(io->io_data, sizeof (pipe_data_t));
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2277c478bd9Sstevel@tonic-gate static const char *
pio_name(mdb_io_t * io)2287c478bd9Sstevel@tonic-gate pio_name(mdb_io_t *io)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	return ("(pipeline)");
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate static void
pio_link(mdb_io_t * io,mdb_iob_t * iob)2347c478bd9Sstevel@tonic-gate pio_link(mdb_io_t *io, mdb_iob_t *iob)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	pipe_data_t *pd = io->io_data;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	/*
2397c478bd9Sstevel@tonic-gate 	 * Here we take advantage of the IOP_LINK calls made to associate each
2407c478bd9Sstevel@tonic-gate 	 * i/o backend with its iob to determine our read and write iobs.
2417c478bd9Sstevel@tonic-gate 	 */
2427c478bd9Sstevel@tonic-gate 	if (io->io_next == NULL) {
2437c478bd9Sstevel@tonic-gate 		if (iob->iob_flags & MDB_IOB_RDONLY)
2447c478bd9Sstevel@tonic-gate 			pd->pipe_rdiob = iob;
2457c478bd9Sstevel@tonic-gate 		else
2467c478bd9Sstevel@tonic-gate 			pd->pipe_wriob = iob;
2477c478bd9Sstevel@tonic-gate 	} else
2487c478bd9Sstevel@tonic-gate 		IOP_LINK(io->io_next, iob);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate static void
pio_unlink(mdb_io_t * io,mdb_iob_t * iob)2527c478bd9Sstevel@tonic-gate pio_unlink(mdb_io_t *io, mdb_iob_t *iob)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	pipe_data_t *volatile pd = io->io_data;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	/*
2577c478bd9Sstevel@tonic-gate 	 * The IOP_UNLINK call will be made when one of our associated iobs is
2587c478bd9Sstevel@tonic-gate 	 * destroyed.  If the read-side iob is being destroyed, we simply set
2597c478bd9Sstevel@tonic-gate 	 * pipe_rdiob to NULL, forcing subsequent pio_write() calls to fail
2607c478bd9Sstevel@tonic-gate 	 * with EPIPE.  Things are more complicated when the write-side is
2617c478bd9Sstevel@tonic-gate 	 * being destroyed.  If this is the last close prior to destroying the
2627c478bd9Sstevel@tonic-gate 	 * pipe, we need to arrange for any in-transit data to be consumed by
2637c478bd9Sstevel@tonic-gate 	 * the reader.  We first set pipe_wriob to NULL, which forces pio_read
2647c478bd9Sstevel@tonic-gate 	 * to return EOF when all in-transit data is consumed.  We then call
2657c478bd9Sstevel@tonic-gate 	 * the read-service routine while there is still a reader and pipe_used
2667c478bd9Sstevel@tonic-gate 	 * is non-zero, indicating there is still data in the pipe.
2677c478bd9Sstevel@tonic-gate 	 */
2687c478bd9Sstevel@tonic-gate 	if (io->io_next == NULL) {
2697c478bd9Sstevel@tonic-gate 		if (pd->pipe_wriob == iob) {
2707c478bd9Sstevel@tonic-gate 			pd->pipe_wriob = NULL;	/* remove writer */
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 			if (pd->pipe_used == 0 && pd->pipe_ctx.ctx_data == NULL)
2737c478bd9Sstevel@tonic-gate 				return;	/* no reader and nothing to read */
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 			/*
2767c478bd9Sstevel@tonic-gate 			 * Note that we need to use a do-while construct here
2777c478bd9Sstevel@tonic-gate 			 * so that we resume the reader's context at *least*
2787c478bd9Sstevel@tonic-gate 			 * once.  This forces it to read EOF and exit even if
2797c478bd9Sstevel@tonic-gate 			 * the pipeline is already completely flushed.
2807c478bd9Sstevel@tonic-gate 			 */
2817c478bd9Sstevel@tonic-gate 			do {
282*8a6a72fdSaf 				if (pd->pipe_rdiob == NULL)
283*8a6a72fdSaf 					break;
284*8a6a72fdSaf 				if (mdb_iob_err(pd->pipe_rdiob) != 0) {
285*8a6a72fdSaf 					if (pd->pipe_ctx.ctx_wptr != NULL) {
286*8a6a72fdSaf 						mdb_frame_pop(
287*8a6a72fdSaf 						    pd->pipe_ctx.ctx_wptr,
288*8a6a72fdSaf 						    MDB_ERR_ABORT);
289*8a6a72fdSaf 						pd->pipe_ctx.ctx_wptr = NULL;
290*8a6a72fdSaf 					}
2917c478bd9Sstevel@tonic-gate 					break; /* don't read if error bit set */
292*8a6a72fdSaf 				}
2937c478bd9Sstevel@tonic-gate 				if (pd->pipe_ctx.ctx_data == NULL ||
2947c478bd9Sstevel@tonic-gate 				    setjmp(*mdb_context_getpcb(
2957c478bd9Sstevel@tonic-gate 				    pd->pipe_ctx.ctx_data)) == 0) {
2967c478bd9Sstevel@tonic-gate 					pd->pipe_rdsvc(pd->pipe_rdiob,
2977c478bd9Sstevel@tonic-gate 					    pd->pipe_wriob, &pd->pipe_ctx);
2987c478bd9Sstevel@tonic-gate 				}
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 			} while (pd->pipe_used != 0);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 			if (pd->pipe_ctx.ctx_data != NULL) {
3037c478bd9Sstevel@tonic-gate 				mdb_context_destroy(pd->pipe_ctx.ctx_data);
3047c478bd9Sstevel@tonic-gate 				pd->pipe_ctx.ctx_data = NULL;
3057c478bd9Sstevel@tonic-gate 			}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		} else if (pd->pipe_rdiob == iob)
3087c478bd9Sstevel@tonic-gate 			pd->pipe_rdiob = NULL; /* remove reader */
3097c478bd9Sstevel@tonic-gate 	} else
3107c478bd9Sstevel@tonic-gate 		IOP_UNLINK(io->io_next, iob);
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate static const mdb_io_ops_t pipeio_ops = {
3147c478bd9Sstevel@tonic-gate 	pio_read,
3157c478bd9Sstevel@tonic-gate 	pio_write,
3167c478bd9Sstevel@tonic-gate 	no_io_seek,
3177c478bd9Sstevel@tonic-gate 	pio_ctl,
3187c478bd9Sstevel@tonic-gate 	pio_close,
3197c478bd9Sstevel@tonic-gate 	pio_name,
3207c478bd9Sstevel@tonic-gate 	pio_link,
3217c478bd9Sstevel@tonic-gate 	pio_unlink,
3227c478bd9Sstevel@tonic-gate 	no_io_setattr,
3237c478bd9Sstevel@tonic-gate 	no_io_suspend,
3247c478bd9Sstevel@tonic-gate 	no_io_resume
3257c478bd9Sstevel@tonic-gate };
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate mdb_io_t *
mdb_pipeio_create(mdb_iobsvc_f * rdsvc,mdb_iobsvc_f * wrsvc)3287c478bd9Sstevel@tonic-gate mdb_pipeio_create(mdb_iobsvc_f *rdsvc, mdb_iobsvc_f *wrsvc)
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 	mdb_io_t *io = mdb_alloc(sizeof (mdb_io_t), UM_SLEEP);
3317c478bd9Sstevel@tonic-gate 	pipe_data_t *pd = mdb_zalloc(sizeof (pipe_data_t), UM_SLEEP);
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	ASSERT(rdsvc != NULL && wrsvc != NULL);
3347c478bd9Sstevel@tonic-gate 	pd->pipe_rdsvc = rdsvc;
3357c478bd9Sstevel@tonic-gate 	pd->pipe_wrsvc = wrsvc;
3367c478bd9Sstevel@tonic-gate 	pd->pipe_free = BUFSIZ;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	io->io_ops = &pipeio_ops;
3397c478bd9Sstevel@tonic-gate 	io->io_data = pd;
3407c478bd9Sstevel@tonic-gate 	io->io_next = NULL;
3417c478bd9Sstevel@tonic-gate 	io->io_refcnt = 0;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	return (io);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate int
mdb_iob_isapipe(mdb_iob_t * iob)3477c478bd9Sstevel@tonic-gate mdb_iob_isapipe(mdb_iob_t *iob)
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	mdb_io_t *io;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	for (io = iob->iob_iop; io != NULL; io = io->io_next) {
3527c478bd9Sstevel@tonic-gate 		if (io->io_ops == &pipeio_ops)
3537c478bd9Sstevel@tonic-gate 			return (1);
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	return (0);
3577c478bd9Sstevel@tonic-gate }
358