2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000-2008 Poul-Henning Kamp
5 * Copyright (c) 2000-2008 Dag-Erling Coïdan Smørgrav
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer
13 * in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
37 #include <sys/ctype.h>
38 #include <sys/errno.h>
39 #include <sys/kernel.h>
40 #include <sys/limits.h>
41 #include <sys/malloc.h>
42 #include <sys/systm.h>
44 #include <machine/stdarg.h>
58 static MALLOC_DEFINE(M_SBUF, "sbuf", "string buffers");
59 #define SBMALLOC(size, flags) malloc(size, M_SBUF, (flags) | M_ZERO)
60 #define SBFREE(buf) free(buf, M_SBUF)
63 #define SBMALLOC(size, flags) calloc(1, size)
64 #define SBFREE(buf) free(buf)
70 #define SBUF_ISDYNAMIC(s) ((s)->s_flags & SBUF_DYNAMIC)
71 #define SBUF_ISDYNSTRUCT(s) ((s)->s_flags & SBUF_DYNSTRUCT)
72 #define SBUF_ISFINISHED(s) ((s)->s_flags & SBUF_FINISHED)
73 #define SBUF_HASROOM(s) ((s)->s_len < (s)->s_size - 1)
74 #define SBUF_FREESPACE(s) ((s)->s_size - ((s)->s_len + 1))
75 #define SBUF_CANEXTEND(s) ((s)->s_flags & SBUF_AUTOEXTEND)
76 #define SBUF_ISSECTION(s) ((s)->s_flags & SBUF_INSECTION)
77 #define SBUF_NULINCLUDED(s) ((s)->s_flags & SBUF_INCLUDENUL)
78 #define SBUF_ISDRAINTOEOR(s) ((s)->s_flags & SBUF_DRAINTOEOR)
79 #define SBUF_DODRAINTOEOR(s) (SBUF_ISSECTION(s) && SBUF_ISDRAINTOEOR(s))
80 #define SBUF_MALLOCFLAG(s) \
81 (((s)->s_flags & SBUF_NOWAIT) ? M_NOWAIT : M_WAITOK)
86 #define SBUF_SETFLAG(s, f) do { (s)->s_flags |= (f); } while (0)
87 #define SBUF_CLEARFLAG(s, f) do { (s)->s_flags &= ~(f); } while (0)
89 #define SBUF_MINSIZE 2 /* Min is 1 byte + nulterm. */
90 #define SBUF_MINEXTENDSIZE 16 /* Should be power of 2. */
93 #define SBUF_MAXEXTENDSIZE PAGE_SIZE
94 #define SBUF_MAXEXTENDINCR PAGE_SIZE
96 #define SBUF_MAXEXTENDSIZE 4096
97 #define SBUF_MAXEXTENDINCR 4096
103 #if defined(_KERNEL) && defined(INVARIANTS)
106 _assert_sbuf_integrity(const char *fun, struct sbuf *s)
110 ("%s called with a NULL sbuf pointer", fun));
111 KASSERT(s->s_buf != NULL,
112 ("%s called with uninitialized or corrupt sbuf", fun));
113 if (SBUF_ISFINISHED(s) && SBUF_NULINCLUDED(s)) {
114 KASSERT(s->s_len <= s->s_size,
115 ("wrote past end of sbuf (%jd >= %jd)",
116 (intmax_t)s->s_len, (intmax_t)s->s_size));
118 KASSERT(s->s_len < s->s_size,
119 ("wrote past end of sbuf (%jd >= %jd)",
120 (intmax_t)s->s_len, (intmax_t)s->s_size));
125 _assert_sbuf_state(const char *fun, struct sbuf *s, int state)
128 KASSERT((s->s_flags & SBUF_FINISHED) == state,
129 ("%s called with %sfinished or corrupt sbuf", fun,
130 (state ? "un" : "")));
133 #define assert_sbuf_integrity(s) _assert_sbuf_integrity(__func__, (s))
134 #define assert_sbuf_state(s, i) _assert_sbuf_state(__func__, (s), (i))
136 #else /* _KERNEL && INVARIANTS */
138 #define assert_sbuf_integrity(s) do { } while (0)
139 #define assert_sbuf_state(s, i) do { } while (0)
141 #endif /* _KERNEL && INVARIANTS */
144 CTASSERT(powerof2(SBUF_MAXEXTENDSIZE));
145 CTASSERT(powerof2(SBUF_MAXEXTENDINCR));
149 sbuf_extendsize(int size)
153 if (size < (int)SBUF_MAXEXTENDSIZE) {
154 newsize = SBUF_MINEXTENDSIZE;
155 while (newsize < size)
158 newsize = roundup2(size, SBUF_MAXEXTENDINCR);
160 KASSERT(newsize >= size, ("%s: %d < %d\n", __func__, newsize, size));
168 sbuf_extend(struct sbuf *s, int addlen)
173 if (!SBUF_CANEXTEND(s))
175 newsize = sbuf_extendsize(s->s_size + addlen);
176 newbuf = SBMALLOC(newsize, SBUF_MALLOCFLAG(s));
179 memcpy(newbuf, s->s_buf, s->s_size);
180 if (SBUF_ISDYNAMIC(s))
183 SBUF_SETFLAG(s, SBUF_DYNAMIC);
190 * Initialize the internals of an sbuf.
191 * If buf is non-NULL, it points to a static or already-allocated string
192 * big enough to hold at least length characters.
195 sbuf_newbuf(struct sbuf *s, char *buf, int length, int flags)
198 memset(s, 0, sizeof(*s));
203 if (!SBUF_CANEXTEND(s)) {
204 KASSERT(s->s_size >= SBUF_MINSIZE,
205 ("attempt to create an sbuf smaller than %d bytes",
209 if (s->s_buf != NULL)
212 if (SBUF_CANEXTEND(s))
213 s->s_size = sbuf_extendsize(s->s_size);
215 s->s_buf = SBMALLOC(s->s_size, SBUF_MALLOCFLAG(s));
216 if (s->s_buf == NULL)
218 SBUF_SETFLAG(s, SBUF_DYNAMIC);
223 * Initialize an sbuf.
224 * If buf is non-NULL, it points to a static or already-allocated string
225 * big enough to hold at least length characters.
228 sbuf_new(struct sbuf *s, char *buf, int length, int flags)
232 ("attempt to create an sbuf of negative length (%d)", length));
233 KASSERT((flags & ~SBUF_USRFLAGMSK) == 0,
234 ("%s called with invalid flags", __func__));
236 flags &= SBUF_USRFLAGMSK;
238 return (sbuf_newbuf(s, buf, length, flags));
240 s = SBMALLOC(sizeof(*s), (flags & SBUF_NOWAIT) ? M_NOWAIT : M_WAITOK);
243 if (sbuf_newbuf(s, buf, length, flags) == NULL) {
247 SBUF_SETFLAG(s, SBUF_DYNSTRUCT);
253 * Create an sbuf with uio data
256 sbuf_uionew(struct sbuf *s, struct uio *uio, int *error)
260 ("%s called with NULL uio pointer", __func__));
261 KASSERT(error != NULL,
262 ("%s called with NULL error pointer", __func__));
264 if (uio->uio_resid >= INT_MAX || uio->uio_resid < SBUF_MINSIZE - 1) {
268 s = sbuf_new(s, NULL, uio->uio_resid + 1, 0);
273 *error = uiomove(s->s_buf, uio->uio_resid, uio);
278 s->s_len = s->s_size - 1;
279 if (SBUF_ISSECTION(s))
280 s->s_sect_len = s->s_size - 1;
287 sbuf_get_flags(struct sbuf *s)
290 return (s->s_flags & SBUF_USRFLAGMSK);
294 sbuf_clear_flags(struct sbuf *s, int flags)
297 s->s_flags &= ~(flags & SBUF_USRFLAGMSK);
301 sbuf_set_flags(struct sbuf *s, int flags)
305 s->s_flags |= (flags & SBUF_USRFLAGMSK);
309 * Clear an sbuf and reset its position.
312 sbuf_clear(struct sbuf *s)
315 assert_sbuf_integrity(s);
316 /* don't care if it's finished or not */
318 SBUF_CLEARFLAG(s, SBUF_FINISHED);
326 * Set the sbuf's end position to an arbitrary value.
327 * Effectively truncates the sbuf at the new position.
330 sbuf_setpos(struct sbuf *s, ssize_t pos)
333 assert_sbuf_integrity(s);
334 assert_sbuf_state(s, 0);
337 ("attempt to seek to a negative position (%jd)", (intmax_t)pos));
338 KASSERT(pos < s->s_size,
339 ("attempt to seek past end of sbuf (%jd >= %jd)",
340 (intmax_t)pos, (intmax_t)s->s_size));
341 KASSERT(!SBUF_ISSECTION(s),
342 ("attempt to seek when in a section"));
344 if (pos < 0 || pos > s->s_len)
351 * Drain into a counter. Counts amount of data without producing output.
352 * Useful for cases like sysctl, where user may first request only size.
353 * This allows to avoid pointless allocation/freeing of large buffers.
356 sbuf_count_drain(void *arg, const char *data __unused, int len)
360 sizep = (size_t *)arg;
366 * Set up a drain function and argument on an sbuf to flush data to
367 * when the sbuf buffer overflows.
370 sbuf_set_drain(struct sbuf *s, sbuf_drain_func *func, void *ctx)
373 assert_sbuf_state(s, 0);
374 assert_sbuf_integrity(s);
375 KASSERT(func == s->s_drain_func || s->s_len == 0,
376 ("Cannot change drain to %p on non-empty sbuf %p", func, s));
377 s->s_drain_func = func;
378 s->s_drain_arg = ctx;
382 * Call the drain and process the return.
385 sbuf_drain(struct sbuf *s)
390 * Immediately return when no work to do,
391 * or an error has already been accumulated.
393 if ((s->s_len == 0) || (s->s_error != 0))
396 if (SBUF_DODRAINTOEOR(s) && s->s_rec_off == 0)
397 return (s->s_error = EDEADLK);
398 len = s->s_drain_func(s->s_drain_arg, s->s_buf,
399 SBUF_DODRAINTOEOR(s) ? s->s_rec_off : s->s_len);
401 s->s_error = len ? -len : EDEADLK;
404 KASSERT(len > 0 && len <= s->s_len,
405 ("Bad drain amount %d for sbuf %p", len, s));
409 * Fast path for the expected case where all the data was
415 * Move the remaining characters to the beginning of the
418 memmove(s->s_buf, s->s_buf + len, s->s_len);
423 * Append bytes to an sbuf. This is the core function for appending
424 * to an sbuf and is the main place that deals with extending the
425 * buffer and marking overflow.
428 sbuf_put_bytes(struct sbuf *s, const char *buf, size_t len)
432 assert_sbuf_integrity(s);
433 assert_sbuf_state(s, 0);
438 if (SBUF_FREESPACE(s) <= 0) {
440 * If there is a drain, use it, otherwise extend the
443 if (s->s_drain_func != NULL)
445 else if (sbuf_extend(s, len > INT_MAX ? INT_MAX : len)
451 n = SBUF_FREESPACE(s);
454 memcpy(&s->s_buf[s->s_len], buf, n);
456 if (SBUF_ISSECTION(s))
464 sbuf_put_byte(struct sbuf *s, char c)
467 assert_sbuf_integrity(s);
468 assert_sbuf_state(s, 0);
470 if (__predict_false(s->s_error != 0))
472 if (__predict_false(SBUF_FREESPACE(s) <= 0)) {
474 * If there is a drain, use it, otherwise extend the
477 if (s->s_drain_func != NULL)
479 else if (sbuf_extend(s, 1) < 0)
484 s->s_buf[s->s_len++] = c;
485 if (SBUF_ISSECTION(s))
490 * Append a byte string to an sbuf.
493 sbuf_bcat(struct sbuf *s, const void *buf, size_t len)
496 sbuf_put_bytes(s, buf, len);
504 * Copy a byte string from userland into an sbuf.
507 sbuf_bcopyin(struct sbuf *s, const void *uaddr, size_t len)
510 assert_sbuf_integrity(s);
511 assert_sbuf_state(s, 0);
512 KASSERT(s->s_drain_func == NULL,
513 ("Nonsensical copyin to sbuf %p with a drain", s));
519 if (len > SBUF_FREESPACE(s)) {
520 sbuf_extend(s, len - SBUF_FREESPACE(s));
521 if (SBUF_FREESPACE(s) < len)
522 len = SBUF_FREESPACE(s);
524 if (copyin(uaddr, s->s_buf + s->s_len, len) != 0)
533 * Copy a byte string into an sbuf.
536 sbuf_bcpy(struct sbuf *s, const void *buf, size_t len)
539 assert_sbuf_integrity(s);
540 assert_sbuf_state(s, 0);
543 return (sbuf_bcat(s, buf, len));
547 * Append a string to an sbuf.
550 sbuf_cat(struct sbuf *s, const char *str)
555 sbuf_put_bytes(s, str, n);
563 * Append a string from userland to an sbuf.
566 sbuf_copyin(struct sbuf *s, const void *uaddr, size_t len)
570 assert_sbuf_integrity(s);
571 assert_sbuf_state(s, 0);
572 KASSERT(s->s_drain_func == NULL,
573 ("Nonsensical copyin to sbuf %p with a drain", s));
579 len = SBUF_FREESPACE(s); /* XXX return 0? */
580 if (len > SBUF_FREESPACE(s)) {
582 if (SBUF_FREESPACE(s) < len)
583 len = SBUF_FREESPACE(s);
585 switch (copyinstr(uaddr, s->s_buf + s->s_len, len + 1, &done)) {
590 s->s_len += done - 1;
591 if (SBUF_ISSECTION(s))
592 s->s_sect_len += done - 1;
595 return (-1); /* XXX */
603 * Copy a string into an sbuf.
606 sbuf_cpy(struct sbuf *s, const char *str)
609 assert_sbuf_integrity(s);
610 assert_sbuf_state(s, 0);
613 return (sbuf_cat(s, str));
617 * Format the given argument list and append the resulting string to an sbuf.
622 * Append a non-NUL character to an sbuf. This prototype signature is
623 * suitable for use with kvprintf(9).
626 sbuf_putc_func(int c, void *arg)
629 if (__predict_true(c != '\0'))
630 sbuf_put_byte(arg, c);
634 sbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap)
637 assert_sbuf_integrity(s);
638 assert_sbuf_state(s, 0);
641 ("%s called with a NULL format string", __func__));
643 (void)kvprintf(fmt, sbuf_putc_func, s, 10, ap);
650 sbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap)
655 assert_sbuf_integrity(s);
656 assert_sbuf_state(s, 0);
659 ("%s called with a NULL format string", __func__));
665 * For the moment, there is no way to get vsnprintf(3) to hand
666 * back a character at a time, to push everything into
667 * sbuf_putc_func() as was done for the kernel.
669 * In userspace, while drains are useful, there's generally
670 * not a problem attempting to malloc(3) on out of space. So
671 * expand a userland sbuf if there is not enough room for the
672 * data produced by sbuf_[v]printf(3).
677 va_copy(ap_copy, ap);
678 len = vsnprintf(&s->s_buf[s->s_len], SBUF_FREESPACE(s) + 1,
686 if (SBUF_FREESPACE(s) >= len)
688 /* Cannot print with the current available space. */
689 if (s->s_drain_func != NULL && s->s_len > 0)
690 error = sbuf_drain(s); /* sbuf_drain() sets s_error. */
691 else if (sbuf_extend(s, len - SBUF_FREESPACE(s)) != 0)
692 s->s_error = error = ENOMEM;
693 } while (error == 0);
696 * s->s_len is the length of the string, without the terminating nul.
697 * When updating s->s_len, we must subtract 1 from the length that
698 * we passed into vsnprintf() because that length includes the
701 * vsnprintf() returns the amount that would have been copied,
702 * given sufficient space, so don't over-increment s_len.
704 if (SBUF_FREESPACE(s) < len)
705 len = SBUF_FREESPACE(s);
707 if (SBUF_ISSECTION(s))
708 s->s_sect_len += len;
710 KASSERT(s->s_len < s->s_size,
711 ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
720 * Format the given arguments and append the resulting string to an sbuf.
723 sbuf_printf(struct sbuf *s, const char *fmt, ...)
729 result = sbuf_vprintf(s, fmt, ap);
735 * Append a character to an sbuf.
738 sbuf_putc(struct sbuf *s, int c)
748 * Trim whitespace characters from end of an sbuf.
751 sbuf_trim(struct sbuf *s)
754 assert_sbuf_integrity(s);
755 assert_sbuf_state(s, 0);
756 KASSERT(s->s_drain_func == NULL,
757 ("%s makes no sense on sbuf %p with drain", __func__, s));
762 while (s->s_len > 0 && isspace(s->s_buf[s->s_len-1])) {
764 if (SBUF_ISSECTION(s))
772 * Check if an sbuf has an error.
775 sbuf_error(const struct sbuf *s)
782 * Finish off an sbuf.
785 sbuf_finish(struct sbuf *s)
788 assert_sbuf_integrity(s);
789 assert_sbuf_state(s, 0);
791 s->s_buf[s->s_len] = '\0';
792 if (SBUF_NULINCLUDED(s))
794 if (s->s_drain_func != NULL) {
795 while (s->s_len > 0 && s->s_error == 0)
796 s->s_error = sbuf_drain(s);
798 SBUF_SETFLAG(s, SBUF_FINISHED);
802 if (s->s_error != 0) {
811 * Return a pointer to the sbuf data.
814 sbuf_data(struct sbuf *s)
817 assert_sbuf_integrity(s);
818 assert_sbuf_state(s, SBUF_FINISHED);
819 KASSERT(s->s_drain_func == NULL,
820 ("%s makes no sense on sbuf %p with drain", __func__, s));
826 * Return the length of the sbuf data.
829 sbuf_len(struct sbuf *s)
832 assert_sbuf_integrity(s);
833 /* don't care if it's finished or not */
834 KASSERT(s->s_drain_func == NULL,
835 ("%s makes no sense on sbuf %p with drain", __func__, s));
840 /* If finished, nulterm is already in len, else add one. */
841 if (SBUF_NULINCLUDED(s) && !SBUF_ISFINISHED(s))
842 return (s->s_len + 1);
847 * Clear an sbuf, free its buffer if necessary.
850 sbuf_delete(struct sbuf *s)
854 assert_sbuf_integrity(s);
855 /* don't care if it's finished or not */
857 if (SBUF_ISDYNAMIC(s))
859 isdyn = SBUF_ISDYNSTRUCT(s);
860 memset(s, 0, sizeof(*s));
866 * Check if an sbuf has been finished.
869 sbuf_done(const struct sbuf *s)
872 return (SBUF_ISFINISHED(s));
879 sbuf_start_section(struct sbuf *s, ssize_t *old_lenp)
882 assert_sbuf_integrity(s);
883 assert_sbuf_state(s, 0);
885 if (!SBUF_ISSECTION(s)) {
886 KASSERT(s->s_sect_len == 0,
887 ("s_sect_len != 0 when starting a section"));
888 if (old_lenp != NULL)
890 s->s_rec_off = s->s_len;
891 SBUF_SETFLAG(s, SBUF_INSECTION);
893 KASSERT(old_lenp != NULL,
894 ("s_sect_len should be saved when starting a subsection"));
895 *old_lenp = s->s_sect_len;
901 * End the section padding to the specified length with the specified
905 sbuf_end_section(struct sbuf *s, ssize_t old_len, size_t pad, int c)
909 assert_sbuf_integrity(s);
910 assert_sbuf_state(s, 0);
911 KASSERT(SBUF_ISSECTION(s),
912 ("attempt to end a section when not in a section"));
915 len = roundup(s->s_sect_len, pad) - s->s_sect_len;
916 for (; s->s_error == 0 && len > 0; len--)
921 s->s_rec_off = s->s_sect_len = 0;
922 SBUF_CLEARFLAG(s, SBUF_INSECTION);
924 s->s_sect_len += old_len;