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