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