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