1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Pipe I/O Backend
31  *
32  * In order to implement dcmd pipelines, we provide a pipe i/o backend that
33  * can be used to connect two mdb_iob structures (a read and write end).
34  * This backend is selected when mdb_iob_pipe is used to construct a pair of
35  * iobs.  Each iob points at the same i/o backend (the pipe i/o), and the
36  * backend manages a circular fixed-size buffer which moves data between
37  * the reader and writer.  The caller provides read and write-side service
38  * routines that are expected to perform context switching (see mdb_context.c).
39  * The pipe implementation is relatively simple: the writer calls any of the
40  * mdb_iob_* routines to fill the write-side iob, and when this iob needs to
41  * flush data to the underlying i/o, pio_write() below is called.  This
42  * routine copies data into the pipe buffer until no more free space is
43  * available, and then calls the read-side service routine (presuming that
44  * when it returns, more free space will be available).  On the read-side,
45  * pio_read() copies data up from the pipe buffer into the read-side iob.
46  * If pio_read() is called and the pipe buffer is empty, pio_read() calls
47  * the write-side service routine to force the writer to produce more data.
48  */
49 
50 #include <sys/sysmacros.h>
51 #include <stropts.h>
52 #include <limits.h>
53 
54 #include <mdb/mdb_modapi.h>
55 #include <mdb/mdb_debug.h>
56 #include <mdb/mdb_string.h>
57 #include <mdb/mdb_context.h>
58 #include <mdb/mdb_err.h>
59 #include <mdb/mdb_io_impl.h>
60 
61 typedef struct pipe_data {
62 	mdb_iobsvc_f *pipe_rdsvc;	/* Read-side service routine */
63 	mdb_iob_t *pipe_rdiob;		/* Read-side i/o buffer */
64 	mdb_iobsvc_f *pipe_wrsvc;	/* Write-side service routine */
65 	mdb_iob_t *pipe_wriob;		/* Write-side i/o buffer */
66 	char pipe_buf[BUFSIZ];		/* Ring buffer for pipe contents */
67 	mdb_iob_ctx_t pipe_ctx;		/* Context data for service routines */
68 	uint_t pipe_rdndx;		/* Next byte index for reading */
69 	uint_t pipe_wrndx;		/* Next byte index for writing */
70 	uint_t pipe_free;		/* Free space for writing in bytes */
71 	uint_t pipe_used;		/* Used space for reading in bytes */
72 } pipe_data_t;
73 
74 
75 static ssize_t
76 pio_read(mdb_io_t *io, void *buf, size_t nbytes)
77 {
78 	pipe_data_t *pd = io->io_data;
79 	size_t n, nleft;
80 
81 	if (nbytes == 0)
82 		return (0); /* return 0 for zero-length read */
83 
84 	for (nleft = nbytes; nleft == nbytes; nleft -= n) {
85 		if (pd->pipe_used == 0) {
86 			if (pd->pipe_wriob != NULL) {
87 				pd->pipe_wrsvc(pd->pipe_rdiob,
88 				    pd->pipe_wriob, &pd->pipe_ctx);
89 			}
90 			if (pd->pipe_used == 0)
91 				break;
92 		}
93 
94 		n = MIN(pd->pipe_used, nleft);
95 
96 		if (BUFSIZ - pd->pipe_rdndx < n) {
97 			/*
98 			 * Case 1: The amount to read overlaps the end of the
99 			 * circular buffer.  'n1' will be the amount to copy
100 			 * from the end of the buffer, and 'n2' will be the
101 			 * amount to copy from the beginning.  Note that since
102 			 * n <= pipe_used, it is impossible to read past
103 			 * pipe_wrndx into undefined territory.
104 			 */
105 			size_t n1 = BUFSIZ - pd->pipe_rdndx;
106 			size_t n2 = n - n1;
107 
108 			ASSERT(n2 <= pd->pipe_wrndx);
109 			bcopy(&pd->pipe_buf[pd->pipe_rdndx], buf, n1);
110 			buf = (char *)buf + n1;
111 			bcopy(&pd->pipe_buf[0], buf, n2);
112 			buf = (char *)buf + n2;
113 		} else {
114 			/*
115 			 * Case 2: The easy case.  Simply copy the data over
116 			 * to the buffer.
117 			 */
118 			bcopy(&pd->pipe_buf[pd->pipe_rdndx], buf, n);
119 			buf = (char *)buf + n;
120 		}
121 
122 		pd->pipe_rdndx = (pd->pipe_rdndx + n) % BUFSIZ;
123 		pd->pipe_free += n;
124 		pd->pipe_used -= n;
125 	}
126 
127 	/*
128 	 * If we have a writer, but pipe_wrsvc failed to produce any data,
129 	 * we return EAGAIN.  If there is no writer, then return 0 for EOF.
130 	 */
131 	if (nleft == nbytes) {
132 		if (pd->pipe_wriob != NULL)
133 			return (set_errno(EAGAIN));
134 		else
135 			return (0);
136 	}
137 
138 	return (nbytes - nleft);
139 }
140 
141 static ssize_t
142 pio_write(mdb_io_t *io, const void *buf, size_t nbytes)
143 {
144 	pipe_data_t *pd = io->io_data;
145 	size_t n, nleft;
146 
147 	if (pd->pipe_rdiob == NULL)
148 		return (set_errno(EPIPE)); /* fail with EPIPE if no reader */
149 
150 	for (nleft = nbytes; nleft != 0; nleft -= n) {
151 		if (pd->pipe_free == 0) {
152 			pd->pipe_rdsvc(pd->pipe_rdiob,
153 			    pd->pipe_wriob, &pd->pipe_ctx);
154 			if (pd->pipe_free == 0)
155 				break; /* if nothing consumed by reader, exit */
156 		}
157 
158 		n = MIN(pd->pipe_free, nleft);
159 
160 		if (BUFSIZ - pd->pipe_wrndx < n) {
161 			/*
162 			 * Case 1: The data will overlap the circular buffer
163 			 * boundary. In this case, 'n1' will be the number of
164 			 * bytes to put at the end of the buffer, and 'n2' will
165 			 * be the number of bytes to put at the beginning.
166 			 * Note that since n <= pipe_free, it is impossible to
167 			 * overlap rdndx with the initial data.
168 			 */
169 			size_t n1 = BUFSIZ - pd->pipe_wrndx;
170 			size_t n2 = n - n1;
171 
172 			ASSERT(n2 <= pd->pipe_rdndx);
173 
174 			bcopy(buf, &pd->pipe_buf[pd->pipe_wrndx], n1);
175 			buf = (const char *)buf + n1;
176 			bcopy(buf, &pd->pipe_buf[0], n2);
177 			buf = (const char *)buf + n2;
178 		} else {
179 			/*
180 			 * Case 2: The easy case.  Simply copy the data into
181 			 * the buffer.
182 			 */
183 			bcopy(buf, &pd->pipe_buf[pd->pipe_wrndx], n);
184 			buf = (const char *)buf + n;
185 		}
186 
187 		pd->pipe_wrndx = (pd->pipe_wrndx + n) % BUFSIZ;
188 		pd->pipe_free -= n;
189 		pd->pipe_used += n;
190 	}
191 
192 	if (nleft == nbytes && nbytes != 0)
193 		return (set_errno(EAGAIN));
194 
195 	return (nbytes - nleft);
196 }
197 
198 /*
199  * Provide support for STREAMS-style write-side flush ioctl.  This can be
200  * used by the caller to force a context switch to the read-side.
201  */
202 static int
203 pio_ctl(mdb_io_t *io, int req, void *arg)
204 {
205 	pipe_data_t *pd = io->io_data;
206 
207 	if (io->io_next != NULL)
208 		return (IOP_CTL(io->io_next, req, arg));
209 
210 	if (req != I_FLUSH || (intptr_t)arg != FLUSHW)
211 		return (set_errno(ENOTSUP));
212 
213 	if (pd->pipe_used != 0)
214 		pd->pipe_rdsvc(pd->pipe_rdiob, pd->pipe_wriob, &pd->pipe_ctx);
215 
216 	return (0);
217 }
218 
219 static void
220 pio_close(mdb_io_t *io)
221 {
222 	mdb_free(io->io_data, sizeof (pipe_data_t));
223 }
224 
225 /*ARGSUSED*/
226 static const char *
227 pio_name(mdb_io_t *io)
228 {
229 	return ("(pipeline)");
230 }
231 
232 static void
233 pio_link(mdb_io_t *io, mdb_iob_t *iob)
234 {
235 	pipe_data_t *pd = io->io_data;
236 
237 	/*
238 	 * Here we take advantage of the IOP_LINK calls made to associate each
239 	 * i/o backend with its iob to determine our read and write iobs.
240 	 */
241 	if (io->io_next == NULL) {
242 		if (iob->iob_flags & MDB_IOB_RDONLY)
243 			pd->pipe_rdiob = iob;
244 		else
245 			pd->pipe_wriob = iob;
246 	} else
247 		IOP_LINK(io->io_next, iob);
248 }
249 
250 static void
251 pio_unlink(mdb_io_t *io, mdb_iob_t *iob)
252 {
253 	pipe_data_t *volatile pd = io->io_data;
254 
255 	/*
256 	 * The IOP_UNLINK call will be made when one of our associated iobs is
257 	 * destroyed.  If the read-side iob is being destroyed, we simply set
258 	 * pipe_rdiob to NULL, forcing subsequent pio_write() calls to fail
259 	 * with EPIPE.  Things are more complicated when the write-side is
260 	 * being destroyed.  If this is the last close prior to destroying the
261 	 * pipe, we need to arrange for any in-transit data to be consumed by
262 	 * the reader.  We first set pipe_wriob to NULL, which forces pio_read
263 	 * to return EOF when all in-transit data is consumed.  We then call
264 	 * the read-service routine while there is still a reader and pipe_used
265 	 * is non-zero, indicating there is still data in the pipe.
266 	 */
267 	if (io->io_next == NULL) {
268 		if (pd->pipe_wriob == iob) {
269 			pd->pipe_wriob = NULL;	/* remove writer */
270 
271 			if (pd->pipe_used == 0 && pd->pipe_ctx.ctx_data == NULL)
272 				return;	/* no reader and nothing to read */
273 
274 			/*
275 			 * Note that we need to use a do-while construct here
276 			 * so that we resume the reader's context at *least*
277 			 * once.  This forces it to read EOF and exit even if
278 			 * the pipeline is already completely flushed.
279 			 */
280 			do {
281 				if (pd->pipe_rdiob == NULL ||
282 				    mdb_iob_err(pd->pipe_rdiob) != 0)
283 					break; /* don't read if error bit set */
284 
285 				if (pd->pipe_ctx.ctx_data == NULL ||
286 				    setjmp(*mdb_context_getpcb(
287 				    pd->pipe_ctx.ctx_data)) == 0) {
288 					pd->pipe_rdsvc(pd->pipe_rdiob,
289 					    pd->pipe_wriob, &pd->pipe_ctx);
290 				}
291 
292 			} while (pd->pipe_used != 0);
293 
294 			if (pd->pipe_ctx.ctx_data != NULL) {
295 				mdb_context_destroy(pd->pipe_ctx.ctx_data);
296 				pd->pipe_ctx.ctx_data = NULL;
297 			}
298 
299 		} else if (pd->pipe_rdiob == iob)
300 			pd->pipe_rdiob = NULL; /* remove reader */
301 	} else
302 		IOP_UNLINK(io->io_next, iob);
303 }
304 
305 static const mdb_io_ops_t pipeio_ops = {
306 	pio_read,
307 	pio_write,
308 	no_io_seek,
309 	pio_ctl,
310 	pio_close,
311 	pio_name,
312 	pio_link,
313 	pio_unlink,
314 	no_io_setattr,
315 	no_io_suspend,
316 	no_io_resume
317 };
318 
319 mdb_io_t *
320 mdb_pipeio_create(mdb_iobsvc_f *rdsvc, mdb_iobsvc_f *wrsvc)
321 {
322 	mdb_io_t *io = mdb_alloc(sizeof (mdb_io_t), UM_SLEEP);
323 	pipe_data_t *pd = mdb_zalloc(sizeof (pipe_data_t), UM_SLEEP);
324 
325 	ASSERT(rdsvc != NULL && wrsvc != NULL);
326 	pd->pipe_rdsvc = rdsvc;
327 	pd->pipe_wrsvc = wrsvc;
328 	pd->pipe_free = BUFSIZ;
329 
330 	io->io_ops = &pipeio_ops;
331 	io->io_data = pd;
332 	io->io_next = NULL;
333 	io->io_refcnt = 0;
334 
335 	return (io);
336 }
337 
338 int
339 mdb_iob_isapipe(mdb_iob_t *iob)
340 {
341 	mdb_io_t *io;
342 
343 	for (io = iob->iob_iop; io != NULL; io = io->io_next) {
344 		if (io->io_ops == &pipeio_ops)
345 			return (1);
346 	}
347 
348 	return (0);
349 }
350