1bc093719SEd Schouten /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni *
4bc093719SEd Schouten * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
5bc093719SEd Schouten * All rights reserved.
6bc093719SEd Schouten *
7bc093719SEd Schouten * Portions of this software were developed under sponsorship from Snow
8bc093719SEd Schouten * B.V., the Netherlands.
9bc093719SEd Schouten *
10bc093719SEd Schouten * Redistribution and use in source and binary forms, with or without
11bc093719SEd Schouten * modification, are permitted provided that the following conditions
12bc093719SEd Schouten * are met:
13bc093719SEd Schouten * 1. Redistributions of source code must retain the above copyright
14bc093719SEd Schouten * notice, this list of conditions and the following disclaimer.
15bc093719SEd Schouten * 2. Redistributions in binary form must reproduce the above copyright
16bc093719SEd Schouten * notice, this list of conditions and the following disclaimer in the
17bc093719SEd Schouten * documentation and/or other materials provided with the distribution.
18bc093719SEd Schouten *
19bc093719SEd Schouten * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20bc093719SEd Schouten * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21bc093719SEd Schouten * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22bc093719SEd Schouten * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23bc093719SEd Schouten * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24bc093719SEd Schouten * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25bc093719SEd Schouten * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26bc093719SEd Schouten * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27bc093719SEd Schouten * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28bc093719SEd Schouten * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29bc093719SEd Schouten * SUCH DAMAGE.
30bc093719SEd Schouten */
31bc093719SEd Schouten
32bc093719SEd Schouten #include <sys/param.h>
33bc093719SEd Schouten #include <sys/kernel.h>
34bc093719SEd Schouten #include <sys/lock.h>
35bc093719SEd Schouten #include <sys/queue.h>
36bc093719SEd Schouten #include <sys/sysctl.h>
37bc093719SEd Schouten #include <sys/systm.h>
38bc093719SEd Schouten #include <sys/tty.h>
39bc093719SEd Schouten #include <sys/uio.h>
40bc093719SEd Schouten
41bc093719SEd Schouten #include <vm/uma.h>
42bc093719SEd Schouten
43bc093719SEd Schouten /*
44bc093719SEd Schouten * TTY input queue buffering.
45bc093719SEd Schouten *
46bc093719SEd Schouten * Unlike the output queue, the input queue has more features that are
47bc093719SEd Schouten * needed to properly implement various features offered by the TTY
48bc093719SEd Schouten * interface:
49bc093719SEd Schouten *
50bc093719SEd Schouten * - Data can be removed from the tail of the queue, which is used to
51bc093719SEd Schouten * implement backspace.
52bc093719SEd Schouten * - Once in a while, input has to be `canonicalized'. When ICANON is
53bc093719SEd Schouten * turned on, this will be done after a CR has been inserted.
54bc093719SEd Schouten * Otherwise, it should be done after any character has been inserted.
55bc093719SEd Schouten * - The input queue can store one bit per byte, called the quoting bit.
56bc093719SEd Schouten * This bit is used by TTYDISC to make backspace work on quoted
57bc093719SEd Schouten * characters.
58bc093719SEd Schouten *
59bc093719SEd Schouten * In most cases, there is probably less input than output, so unlike
60bc093719SEd Schouten * the outq, we'll stick to 128 byte blocks here.
61bc093719SEd Schouten */
62bc093719SEd Schouten
6352f542a8SEd Schouten static int ttyinq_flush_secure = 1;
64770c15f6SEd Schouten SYSCTL_INT(_kern, OID_AUTO, tty_inq_flush_secure, CTLFLAG_RW,
65770c15f6SEd Schouten &ttyinq_flush_secure, 0, "Zero buffers while flushing");
66bc093719SEd Schouten
67bc093719SEd Schouten #define TTYINQ_QUOTESIZE (TTYINQ_DATASIZE / BMSIZE)
68bc093719SEd Schouten #define BMSIZE 32
69bc093719SEd Schouten #define GETBIT(tib,boff) \
70bc093719SEd Schouten ((tib)->tib_quotes[(boff) / BMSIZE] & (1 << ((boff) % BMSIZE)))
71bc093719SEd Schouten #define SETBIT(tib,boff) \
72bc093719SEd Schouten ((tib)->tib_quotes[(boff) / BMSIZE] |= (1 << ((boff) % BMSIZE)))
73bc093719SEd Schouten #define CLRBIT(tib,boff) \
74bc093719SEd Schouten ((tib)->tib_quotes[(boff) / BMSIZE] &= ~(1 << ((boff) % BMSIZE)))
75bc093719SEd Schouten
76bc093719SEd Schouten struct ttyinq_block {
7741ba7e9bSEd Schouten struct ttyinq_block *tib_prev;
7841ba7e9bSEd Schouten struct ttyinq_block *tib_next;
79bc093719SEd Schouten uint32_t tib_quotes[TTYINQ_QUOTESIZE];
80bc093719SEd Schouten char tib_data[TTYINQ_DATASIZE];
81bc093719SEd Schouten };
82bc093719SEd Schouten
83bc093719SEd Schouten static uma_zone_t ttyinq_zone;
84bc093719SEd Schouten
8541ba7e9bSEd Schouten #define TTYINQ_INSERT_TAIL(ti, tib) do { \
8641ba7e9bSEd Schouten if (ti->ti_end == 0) { \
8741ba7e9bSEd Schouten tib->tib_prev = NULL; \
8841ba7e9bSEd Schouten tib->tib_next = ti->ti_firstblock; \
8941ba7e9bSEd Schouten ti->ti_firstblock = tib; \
9041ba7e9bSEd Schouten } else { \
9141ba7e9bSEd Schouten tib->tib_prev = ti->ti_lastblock; \
9241ba7e9bSEd Schouten tib->tib_next = ti->ti_lastblock->tib_next; \
9341ba7e9bSEd Schouten ti->ti_lastblock->tib_next = tib; \
9441ba7e9bSEd Schouten } \
9541ba7e9bSEd Schouten if (tib->tib_next != NULL) \
9641ba7e9bSEd Schouten tib->tib_next->tib_prev = tib; \
9741ba7e9bSEd Schouten ti->ti_nblocks++; \
9841ba7e9bSEd Schouten } while (0)
9941ba7e9bSEd Schouten
10041ba7e9bSEd Schouten #define TTYINQ_REMOVE_HEAD(ti) do { \
10141ba7e9bSEd Schouten ti->ti_firstblock = ti->ti_firstblock->tib_next; \
10241ba7e9bSEd Schouten if (ti->ti_firstblock != NULL) \
10341ba7e9bSEd Schouten ti->ti_firstblock->tib_prev = NULL; \
10441ba7e9bSEd Schouten ti->ti_nblocks--; \
10541ba7e9bSEd Schouten } while (0)
10641ba7e9bSEd Schouten
10741ba7e9bSEd Schouten #define TTYINQ_RECYCLE(ti, tib) do { \
10841ba7e9bSEd Schouten if (ti->ti_quota <= ti->ti_nblocks) \
10941ba7e9bSEd Schouten uma_zfree(ttyinq_zone, tib); \
11041ba7e9bSEd Schouten else \
11141ba7e9bSEd Schouten TTYINQ_INSERT_TAIL(ti, tib); \
11241ba7e9bSEd Schouten } while (0)
11341ba7e9bSEd Schouten
114a6f63533SIan Lepore int
ttyinq_setsize(struct ttyinq * ti,struct tty * tp,size_t size)115bc093719SEd Schouten ttyinq_setsize(struct ttyinq *ti, struct tty *tp, size_t size)
116bc093719SEd Schouten {
117bc093719SEd Schouten struct ttyinq_block *tib;
118bc093719SEd Schouten
11974bb9e3aSEd Schouten ti->ti_quota = howmany(size, TTYINQ_DATASIZE);
120bc093719SEd Schouten
12174bb9e3aSEd Schouten while (ti->ti_quota > ti->ti_nblocks) {
122bc093719SEd Schouten /*
123bc093719SEd Schouten * List is getting bigger.
124bc093719SEd Schouten * Add new blocks to the tail of the list.
125bc093719SEd Schouten *
126bc093719SEd Schouten * We must unlock the TTY temporarily, because we need
127bc093719SEd Schouten * to allocate memory. This won't be a problem, because
128bc093719SEd Schouten * in the worst case, another thread ends up here, which
129bc093719SEd Schouten * may cause us to allocate too many blocks, but this
130bc093719SEd Schouten * will be caught by the loop below.
131bc093719SEd Schouten */
132bc093719SEd Schouten tty_unlock(tp);
133bc093719SEd Schouten tib = uma_zalloc(ttyinq_zone, M_WAITOK);
134bc093719SEd Schouten tty_lock(tp);
135bc093719SEd Schouten
136a6f63533SIan Lepore if (tty_gone(tp)) {
137a6f63533SIan Lepore uma_zfree(ttyinq_zone, tib);
138a6f63533SIan Lepore return (ENXIO);
139a6f63533SIan Lepore }
140a6f63533SIan Lepore
14141ba7e9bSEd Schouten TTYINQ_INSERT_TAIL(ti, tib);
142bc093719SEd Schouten }
143a6f63533SIan Lepore return (0);
14474bb9e3aSEd Schouten }
145bc093719SEd Schouten
14674bb9e3aSEd Schouten void
ttyinq_free(struct ttyinq * ti)14774bb9e3aSEd Schouten ttyinq_free(struct ttyinq *ti)
14874bb9e3aSEd Schouten {
14974bb9e3aSEd Schouten struct ttyinq_block *tib;
150bc093719SEd Schouten
15174bb9e3aSEd Schouten ttyinq_flush(ti);
15274bb9e3aSEd Schouten ti->ti_quota = 0;
15374bb9e3aSEd Schouten
15441ba7e9bSEd Schouten while ((tib = ti->ti_firstblock) != NULL) {
15541ba7e9bSEd Schouten TTYINQ_REMOVE_HEAD(ti);
156bc093719SEd Schouten uma_zfree(ttyinq_zone, tib);
157bc093719SEd Schouten }
15874bb9e3aSEd Schouten
15974bb9e3aSEd Schouten MPASS(ti->ti_nblocks == 0);
160bc093719SEd Schouten }
161bc093719SEd Schouten
162bc093719SEd Schouten int
ttyinq_read_uio(struct ttyinq * ti,struct tty * tp,struct uio * uio,size_t rlen,size_t flen)163bc093719SEd Schouten ttyinq_read_uio(struct ttyinq *ti, struct tty *tp, struct uio *uio,
164bc093719SEd Schouten size_t rlen, size_t flen)
165bc093719SEd Schouten {
166bc093719SEd Schouten
16709a43b87SKyle Evans /* rlen includes flen, flen bytes will be trimmed from the end. */
16809a43b87SKyle Evans MPASS(rlen - flen <= uio->uio_resid);
169bc093719SEd Schouten
170bc093719SEd Schouten while (rlen > 0) {
171bc093719SEd Schouten int error;
172bc093719SEd Schouten struct ttyinq_block *tib;
173bc093719SEd Schouten size_t cbegin, cend, clen;
174bc093719SEd Schouten
175bc093719SEd Schouten /* See if there still is data. */
176bc093719SEd Schouten if (ti->ti_begin == ti->ti_linestart)
177bc093719SEd Schouten return (0);
17841ba7e9bSEd Schouten tib = ti->ti_firstblock;
179bc093719SEd Schouten if (tib == NULL)
180bc093719SEd Schouten return (0);
181bc093719SEd Schouten
182bc093719SEd Schouten /*
183bc093719SEd Schouten * The end address should be the lowest of these three:
184bc093719SEd Schouten * - The write pointer
185bc093719SEd Schouten * - The blocksize - we can't read beyond the block
186bc093719SEd Schouten * - The end address if we could perform the full read
187bc093719SEd Schouten */
188bc093719SEd Schouten cbegin = ti->ti_begin;
189bc093719SEd Schouten cend = MIN(MIN(ti->ti_linestart, ti->ti_begin + rlen),
190bc093719SEd Schouten TTYINQ_DATASIZE);
191bc093719SEd Schouten clen = cend - cbegin;
192bc093719SEd Schouten MPASS(clen >= flen);
193bc093719SEd Schouten rlen -= clen;
194bc093719SEd Schouten
195bc093719SEd Schouten /*
19609a43b87SKyle Evans * Caller shouldn't request that we trim anything if we might be
19709a43b87SKyle Evans * reading across blocks. We could handle it, but today we do
19809a43b87SKyle Evans * not.
19909a43b87SKyle Evans */
20009a43b87SKyle Evans if (flen > 0)
20109a43b87SKyle Evans MPASS(rlen == 0);
20209a43b87SKyle Evans
20309a43b87SKyle Evans /*
204bc093719SEd Schouten * We can prevent buffering in some cases:
205bc093719SEd Schouten * - We need to read the block until the end.
206bc093719SEd Schouten * - We don't need to read the block until the end, but
207bc093719SEd Schouten * there is no data beyond it, which allows us to move
208bc093719SEd Schouten * the write pointer to a new block.
209bc093719SEd Schouten */
210bc093719SEd Schouten if (cend == TTYINQ_DATASIZE || cend == ti->ti_end) {
211bc093719SEd Schouten /*
212bc093719SEd Schouten * Fast path: zero copy. Remove the first block,
213bc093719SEd Schouten * so we can unlock the TTY temporarily.
214bc093719SEd Schouten */
21541ba7e9bSEd Schouten TTYINQ_REMOVE_HEAD(ti);
216bc093719SEd Schouten ti->ti_begin = 0;
217bc093719SEd Schouten
218bc093719SEd Schouten /*
219bc093719SEd Schouten * Because we remove the first block, we must
220bc093719SEd Schouten * fix up the block offsets.
221bc093719SEd Schouten */
222bc093719SEd Schouten #define CORRECT_BLOCK(t) do { \
22341ba7e9bSEd Schouten if (t <= TTYINQ_DATASIZE) \
224bc093719SEd Schouten t = 0; \
22541ba7e9bSEd Schouten else \
226bc093719SEd Schouten t -= TTYINQ_DATASIZE; \
227bc093719SEd Schouten } while (0)
228bc093719SEd Schouten CORRECT_BLOCK(ti->ti_linestart);
229bc093719SEd Schouten CORRECT_BLOCK(ti->ti_reprint);
230bc093719SEd Schouten CORRECT_BLOCK(ti->ti_end);
231bc093719SEd Schouten #undef CORRECT_BLOCK
232bc093719SEd Schouten
233bc093719SEd Schouten /*
234bc093719SEd Schouten * Temporary unlock and copy the data to
235bc093719SEd Schouten * userspace. We may need to flush trailing
236bc093719SEd Schouten * bytes, like EOF characters.
237bc093719SEd Schouten */
238bc093719SEd Schouten tty_unlock(tp);
239bc093719SEd Schouten error = uiomove(tib->tib_data + cbegin,
240bc093719SEd Schouten clen - flen, uio);
241bc093719SEd Schouten tty_lock(tp);
242bc093719SEd Schouten
243bc093719SEd Schouten /* Block can now be readded to the list. */
24441ba7e9bSEd Schouten TTYINQ_RECYCLE(ti, tib);
245bc093719SEd Schouten } else {
246bc093719SEd Schouten char ob[TTYINQ_DATASIZE - 1];
247bc093719SEd Schouten
248bc093719SEd Schouten /*
249bc093719SEd Schouten * Slow path: store data in a temporary buffer.
250bc093719SEd Schouten */
251bc093719SEd Schouten memcpy(ob, tib->tib_data + cbegin, clen - flen);
252bc093719SEd Schouten ti->ti_begin += clen;
253bc093719SEd Schouten MPASS(ti->ti_begin < TTYINQ_DATASIZE);
254bc093719SEd Schouten
255bc093719SEd Schouten /* Temporary unlock and copy the data to userspace. */
256bc093719SEd Schouten tty_unlock(tp);
257bc093719SEd Schouten error = uiomove(ob, clen - flen, uio);
258bc093719SEd Schouten tty_lock(tp);
25974bb9e3aSEd Schouten }
260bc093719SEd Schouten
261bc093719SEd Schouten if (error != 0)
262bc093719SEd Schouten return (error);
263bc093719SEd Schouten if (tty_gone(tp))
264bc093719SEd Schouten return (ENXIO);
265bc093719SEd Schouten }
266bc093719SEd Schouten
267bc093719SEd Schouten return (0);
268bc093719SEd Schouten }
269bc093719SEd Schouten
270bc093719SEd Schouten static __inline void
ttyinq_set_quotes(struct ttyinq_block * tib,size_t offset,size_t length,int value)271bc093719SEd Schouten ttyinq_set_quotes(struct ttyinq_block *tib, size_t offset,
272bc093719SEd Schouten size_t length, int value)
273bc093719SEd Schouten {
274bc093719SEd Schouten
275bc093719SEd Schouten if (value) {
276bc093719SEd Schouten /* Set the bits. */
277bc093719SEd Schouten for (; length > 0; length--, offset++)
278bc093719SEd Schouten SETBIT(tib, offset);
279bc093719SEd Schouten } else {
280bc093719SEd Schouten /* Unset the bits. */
281bc093719SEd Schouten for (; length > 0; length--, offset++)
282bc093719SEd Schouten CLRBIT(tib, offset);
283bc093719SEd Schouten }
284bc093719SEd Schouten }
285bc093719SEd Schouten
286bc093719SEd Schouten size_t
ttyinq_write(struct ttyinq * ti,const void * buf,size_t nbytes,int quote)287bc093719SEd Schouten ttyinq_write(struct ttyinq *ti, const void *buf, size_t nbytes, int quote)
288bc093719SEd Schouten {
289bc093719SEd Schouten const char *cbuf = buf;
290bc093719SEd Schouten struct ttyinq_block *tib;
291bc093719SEd Schouten unsigned int boff;
292bc093719SEd Schouten size_t l;
293bc093719SEd Schouten
294bc093719SEd Schouten while (nbytes > 0) {
295bc093719SEd Schouten boff = ti->ti_end % TTYINQ_DATASIZE;
296bc093719SEd Schouten
297bc093719SEd Schouten if (ti->ti_end == 0) {
298bc093719SEd Schouten /* First time we're being used or drained. */
299bc093719SEd Schouten MPASS(ti->ti_begin == 0);
30041ba7e9bSEd Schouten tib = ti->ti_firstblock;
301bc093719SEd Schouten if (tib == NULL) {
302bc093719SEd Schouten /* Queue has no blocks. */
303bc093719SEd Schouten break;
304bc093719SEd Schouten }
30541ba7e9bSEd Schouten ti->ti_lastblock = tib;
306bc093719SEd Schouten } else if (boff == 0) {
307bc093719SEd Schouten /* We reached the end of this block on last write. */
30841ba7e9bSEd Schouten tib = ti->ti_lastblock->tib_next;
309bc093719SEd Schouten if (tib == NULL) {
310bc093719SEd Schouten /* We've reached the watermark. */
311bc093719SEd Schouten break;
312bc093719SEd Schouten }
313bc093719SEd Schouten ti->ti_lastblock = tib;
31441ba7e9bSEd Schouten } else {
31541ba7e9bSEd Schouten tib = ti->ti_lastblock;
316bc093719SEd Schouten }
317bc093719SEd Schouten
318bc093719SEd Schouten /* Don't copy more than was requested. */
319bc093719SEd Schouten l = MIN(nbytes, TTYINQ_DATASIZE - boff);
320bc093719SEd Schouten MPASS(l > 0);
321bc093719SEd Schouten memcpy(tib->tib_data + boff, cbuf, l);
322bc093719SEd Schouten
323bc093719SEd Schouten /* Set the quoting bits for the proper region. */
324bc093719SEd Schouten ttyinq_set_quotes(tib, boff, l, quote);
325bc093719SEd Schouten
326bc093719SEd Schouten cbuf += l;
327bc093719SEd Schouten nbytes -= l;
328bc093719SEd Schouten ti->ti_end += l;
329bc093719SEd Schouten }
330bc093719SEd Schouten
331bc093719SEd Schouten return (cbuf - (const char *)buf);
332bc093719SEd Schouten }
333bc093719SEd Schouten
334bc093719SEd Schouten int
ttyinq_write_nofrag(struct ttyinq * ti,const void * buf,size_t nbytes,int quote)335bc093719SEd Schouten ttyinq_write_nofrag(struct ttyinq *ti, const void *buf, size_t nbytes, int quote)
336bc093719SEd Schouten {
337a2bb4e08SMatt Macy size_t ret __unused;
338bc093719SEd Schouten
339bc093719SEd Schouten if (ttyinq_bytesleft(ti) < nbytes)
340bc093719SEd Schouten return (-1);
341bc093719SEd Schouten
342bc093719SEd Schouten /* We should always be able to write it back. */
343a2bb4e08SMatt Macy ret = ttyinq_write(ti, buf, nbytes, quote);
344bc093719SEd Schouten MPASS(ret == nbytes);
345bc093719SEd Schouten
346bc093719SEd Schouten return (0);
347bc093719SEd Schouten }
348bc093719SEd Schouten
349bc093719SEd Schouten void
ttyinq_canonicalize(struct ttyinq * ti)350bc093719SEd Schouten ttyinq_canonicalize(struct ttyinq *ti)
351bc093719SEd Schouten {
352bc093719SEd Schouten
353bc093719SEd Schouten ti->ti_linestart = ti->ti_reprint = ti->ti_end;
354bc093719SEd Schouten ti->ti_startblock = ti->ti_reprintblock = ti->ti_lastblock;
355bc093719SEd Schouten }
356bc093719SEd Schouten
357522083ffSKyle Evans /*
358522083ffSKyle Evans * Canonicalize at one of the break characters; we'll work backwards from the
359522083ffSKyle Evans * lastblock to firstblock to try and find the latest one.
360522083ffSKyle Evans */
361522083ffSKyle Evans void
ttyinq_canonicalize_break(struct ttyinq * ti,const char * breakc)362522083ffSKyle Evans ttyinq_canonicalize_break(struct ttyinq *ti, const char *breakc)
363522083ffSKyle Evans {
364522083ffSKyle Evans struct ttyinq_block *tib = ti->ti_lastblock;
365522083ffSKyle Evans unsigned int canon, off;
366522083ffSKyle Evans unsigned int boff;
367522083ffSKyle Evans
368522083ffSKyle Evans /* No block, no change needed. */
369522083ffSKyle Evans if (tib == NULL || ti->ti_end == 0)
370522083ffSKyle Evans return;
371522083ffSKyle Evans
372522083ffSKyle Evans /* Start just past the end... */
373522083ffSKyle Evans off = ti->ti_end;
374*5738d741SKyle Evans canon = ti->ti_begin;
375522083ffSKyle Evans
376*5738d741SKyle Evans while (off > ti->ti_begin) {
377522083ffSKyle Evans off--;
378522083ffSKyle Evans boff = off % TTYINQ_DATASIZE;
379522083ffSKyle Evans
380522083ffSKyle Evans if (strchr(breakc, tib->tib_data[boff]) && !GETBIT(tib, boff)) {
381522083ffSKyle Evans canon = off + 1;
382522083ffSKyle Evans break;
383522083ffSKyle Evans }
384*5738d741SKyle Evans
385*5738d741SKyle Evans if (off != ti->ti_begin && boff == 0)
386*5738d741SKyle Evans tib = tib->tib_prev;
387522083ffSKyle Evans }
388522083ffSKyle Evans
389*5738d741SKyle Evans MPASS(canon > ti->ti_begin || off == ti->ti_begin);
390522083ffSKyle Evans
391522083ffSKyle Evans /*
392*5738d741SKyle Evans * We should only be able to hit canon == ti_begin if we walked
393*5738d741SKyle Evans * everything we have and didn't find any of the break characters, so
394*5738d741SKyle Evans * if canon == ti_begin then tib is already the correct block and we
395*5738d741SKyle Evans * should avoid touching it.
396522083ffSKyle Evans *
397522083ffSKyle Evans * For all other scenarios, if canon lies on a block boundary then tib
398522083ffSKyle Evans * has already advanced to the previous block.
399522083ffSKyle Evans */
400*5738d741SKyle Evans if (canon != ti->ti_begin && (canon % TTYINQ_DATASIZE) == 0)
401522083ffSKyle Evans tib = tib->tib_next;
402522083ffSKyle Evans ti->ti_linestart = ti->ti_reprint = canon;
403522083ffSKyle Evans ti->ti_startblock = ti->ti_reprintblock = tib;
404522083ffSKyle Evans }
405522083ffSKyle Evans
406bc093719SEd Schouten size_t
ttyinq_findchar(struct ttyinq * ti,const char * breakc,size_t maxlen,char * lastc)407bc093719SEd Schouten ttyinq_findchar(struct ttyinq *ti, const char *breakc, size_t maxlen,
408bc093719SEd Schouten char *lastc)
409bc093719SEd Schouten {
41041ba7e9bSEd Schouten struct ttyinq_block *tib = ti->ti_firstblock;
411bc093719SEd Schouten unsigned int boff = ti->ti_begin;
412bc093719SEd Schouten unsigned int bend = MIN(MIN(TTYINQ_DATASIZE, ti->ti_linestart),
413bc093719SEd Schouten ti->ti_begin + maxlen);
414bc093719SEd Schouten
415bc093719SEd Schouten MPASS(maxlen > 0);
416bc093719SEd Schouten
417bc093719SEd Schouten if (tib == NULL)
418bc093719SEd Schouten return (0);
419bc093719SEd Schouten
420bc093719SEd Schouten while (boff < bend) {
421dc15eac0SEd Schouten if (strchr(breakc, tib->tib_data[boff]) && !GETBIT(tib, boff)) {
422bc093719SEd Schouten *lastc = tib->tib_data[boff];
423bc093719SEd Schouten return (boff - ti->ti_begin + 1);
424bc093719SEd Schouten }
425bc093719SEd Schouten boff++;
426bc093719SEd Schouten }
427bc093719SEd Schouten
428bc093719SEd Schouten /* Not found - just process the entire block. */
429bc093719SEd Schouten return (bend - ti->ti_begin);
430bc093719SEd Schouten }
431bc093719SEd Schouten
432bc093719SEd Schouten void
ttyinq_flush(struct ttyinq * ti)433bc093719SEd Schouten ttyinq_flush(struct ttyinq *ti)
434bc093719SEd Schouten {
435081a0db3SEd Schouten struct ttyinq_block *tib;
436bc093719SEd Schouten
437bc093719SEd Schouten ti->ti_begin = 0;
438bc093719SEd Schouten ti->ti_linestart = 0;
439bc093719SEd Schouten ti->ti_reprint = 0;
440bc093719SEd Schouten ti->ti_end = 0;
441bc093719SEd Schouten
442770c15f6SEd Schouten /* Zero all data in the input queue to get rid of passwords. */
443770c15f6SEd Schouten if (ttyinq_flush_secure) {
444770c15f6SEd Schouten for (tib = ti->ti_firstblock; tib != NULL; tib = tib->tib_next)
445bc093719SEd Schouten bzero(&tib->tib_data, sizeof tib->tib_data);
446bc093719SEd Schouten }
447bc093719SEd Schouten }
448bc093719SEd Schouten
449bc093719SEd Schouten int
ttyinq_peekchar(struct ttyinq * ti,char * c,int * quote)450bc093719SEd Schouten ttyinq_peekchar(struct ttyinq *ti, char *c, int *quote)
451bc093719SEd Schouten {
452bc093719SEd Schouten unsigned int boff;
453bc093719SEd Schouten struct ttyinq_block *tib = ti->ti_lastblock;
454bc093719SEd Schouten
455bc093719SEd Schouten if (ti->ti_linestart == ti->ti_end)
456bc093719SEd Schouten return (-1);
457bc093719SEd Schouten
458bc093719SEd Schouten MPASS(ti->ti_end > 0);
459bc093719SEd Schouten boff = (ti->ti_end - 1) % TTYINQ_DATASIZE;
460bc093719SEd Schouten
461bc093719SEd Schouten *c = tib->tib_data[boff];
462bc093719SEd Schouten *quote = GETBIT(tib, boff);
463bc093719SEd Schouten
464bc093719SEd Schouten return (0);
465bc093719SEd Schouten }
466bc093719SEd Schouten
467bc093719SEd Schouten void
ttyinq_unputchar(struct ttyinq * ti)468bc093719SEd Schouten ttyinq_unputchar(struct ttyinq *ti)
469bc093719SEd Schouten {
470bc093719SEd Schouten
471bc093719SEd Schouten MPASS(ti->ti_linestart < ti->ti_end);
472bc093719SEd Schouten
473bc093719SEd Schouten if (--ti->ti_end % TTYINQ_DATASIZE == 0) {
474bc093719SEd Schouten /* Roll back to the previous block. */
47541ba7e9bSEd Schouten ti->ti_lastblock = ti->ti_lastblock->tib_prev;
476bc093719SEd Schouten /*
477bc093719SEd Schouten * This can only fail if we are unputchar()'ing the
478bc093719SEd Schouten * first character in the queue.
479bc093719SEd Schouten */
480bc093719SEd Schouten MPASS((ti->ti_lastblock == NULL) == (ti->ti_end == 0));
481bc093719SEd Schouten }
482bc093719SEd Schouten }
483bc093719SEd Schouten
484bc093719SEd Schouten void
ttyinq_reprintpos_set(struct ttyinq * ti)485bc093719SEd Schouten ttyinq_reprintpos_set(struct ttyinq *ti)
486bc093719SEd Schouten {
487bc093719SEd Schouten
488bc093719SEd Schouten ti->ti_reprint = ti->ti_end;
489bc093719SEd Schouten ti->ti_reprintblock = ti->ti_lastblock;
490bc093719SEd Schouten }
491bc093719SEd Schouten
492bc093719SEd Schouten void
ttyinq_reprintpos_reset(struct ttyinq * ti)493bc093719SEd Schouten ttyinq_reprintpos_reset(struct ttyinq *ti)
494bc093719SEd Schouten {
495bc093719SEd Schouten
496bc093719SEd Schouten ti->ti_reprint = ti->ti_linestart;
497bc093719SEd Schouten ti->ti_reprintblock = ti->ti_startblock;
498bc093719SEd Schouten }
499bc093719SEd Schouten
500bc093719SEd Schouten static void
ttyinq_line_iterate(struct ttyinq * ti,ttyinq_line_iterator_t * iterator,void * data,unsigned int offset,struct ttyinq_block * tib)501bc093719SEd Schouten ttyinq_line_iterate(struct ttyinq *ti,
502bc093719SEd Schouten ttyinq_line_iterator_t *iterator, void *data,
503bc093719SEd Schouten unsigned int offset, struct ttyinq_block *tib)
504bc093719SEd Schouten {
505bc093719SEd Schouten unsigned int boff;
506bc093719SEd Schouten
507bc093719SEd Schouten /* Use the proper block when we're at the queue head. */
508bc093719SEd Schouten if (offset == 0)
50941ba7e9bSEd Schouten tib = ti->ti_firstblock;
510bc093719SEd Schouten
511bc093719SEd Schouten /* Iterate all characters and call the iterator function. */
512bc093719SEd Schouten for (; offset < ti->ti_end; offset++) {
513bc093719SEd Schouten boff = offset % TTYINQ_DATASIZE;
514bc093719SEd Schouten MPASS(tib != NULL);
515bc093719SEd Schouten
516bc093719SEd Schouten /* Call back the iterator function. */
517bc093719SEd Schouten iterator(data, tib->tib_data[boff], GETBIT(tib, boff));
518bc093719SEd Schouten
519bc093719SEd Schouten /* Last byte iterated - go to the next block. */
520bc093719SEd Schouten if (boff == TTYINQ_DATASIZE - 1)
52141ba7e9bSEd Schouten tib = tib->tib_next;
522bc093719SEd Schouten }
523bc093719SEd Schouten }
524bc093719SEd Schouten
525bc093719SEd Schouten void
ttyinq_line_iterate_from_linestart(struct ttyinq * ti,ttyinq_line_iterator_t * iterator,void * data)526bc093719SEd Schouten ttyinq_line_iterate_from_linestart(struct ttyinq *ti,
527bc093719SEd Schouten ttyinq_line_iterator_t *iterator, void *data)
528bc093719SEd Schouten {
529bc093719SEd Schouten
530bc093719SEd Schouten ttyinq_line_iterate(ti, iterator, data,
531bc093719SEd Schouten ti->ti_linestart, ti->ti_startblock);
532bc093719SEd Schouten }
533bc093719SEd Schouten
534bc093719SEd Schouten void
ttyinq_line_iterate_from_reprintpos(struct ttyinq * ti,ttyinq_line_iterator_t * iterator,void * data)535bc093719SEd Schouten ttyinq_line_iterate_from_reprintpos(struct ttyinq *ti,
536bc093719SEd Schouten ttyinq_line_iterator_t *iterator, void *data)
537bc093719SEd Schouten {
538bc093719SEd Schouten
539bc093719SEd Schouten ttyinq_line_iterate(ti, iterator, data,
540bc093719SEd Schouten ti->ti_reprint, ti->ti_reprintblock);
541bc093719SEd Schouten }
542bc093719SEd Schouten
543bc093719SEd Schouten static void
ttyinq_startup(void * dummy)544bc093719SEd Schouten ttyinq_startup(void *dummy)
545bc093719SEd Schouten {
546bc093719SEd Schouten
547bc093719SEd Schouten ttyinq_zone = uma_zcreate("ttyinq", sizeof(struct ttyinq_block),
548bc093719SEd Schouten NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
549bc093719SEd Schouten }
550bc093719SEd Schouten
551bc093719SEd Schouten SYSINIT(ttyinq, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyinq_startup, NULL);
552