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