subr_sbuf.c revision 222004
1/*-
2 * Copyright (c) 2000-2008 Poul-Henning Kamp
3 * Copyright (c) 2000-2008 Dag-Erling Co��dan Sm��rgrav
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer
11 *    in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/kern/subr_sbuf.c 222004 2011-05-17 06:36:32Z phk $");
31
32#include <sys/param.h>
33
34#ifdef _KERNEL
35#include <sys/ctype.h>
36#include <sys/errno.h>
37#include <sys/kernel.h>
38#include <sys/malloc.h>
39#include <sys/systm.h>
40#include <sys/uio.h>
41#include <machine/stdarg.h>
42#else /* _KERNEL */
43#include <ctype.h>
44#include <errno.h>
45#include <stdarg.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#endif /* _KERNEL */
50
51#include <sys/sbuf.h>
52
53#ifdef _KERNEL
54static MALLOC_DEFINE(M_SBUF, "sbuf", "string buffers");
55#define	SBMALLOC(size)		malloc(size, M_SBUF, M_WAITOK)
56#define	SBFREE(buf)		free(buf, M_SBUF)
57#else /* _KERNEL */
58#define	KASSERT(e, m)
59#define	SBMALLOC(size)		malloc(size)
60#define	SBFREE(buf)		free(buf)
61#endif /* _KERNEL */
62
63/*
64 * Predicates
65 */
66#define	SBUF_ISDYNAMIC(s)	((s)->s_flags & SBUF_DYNAMIC)
67#define	SBUF_ISDYNSTRUCT(s)	((s)->s_flags & SBUF_DYNSTRUCT)
68#define	SBUF_ISFINISHED(s)	((s)->s_flags & SBUF_FINISHED)
69#define	SBUF_HASROOM(s)		((s)->s_len < (s)->s_size - 1)
70#define	SBUF_FREESPACE(s)	((s)->s_size - ((s)->s_len + 1))
71#define	SBUF_CANEXTEND(s)	((s)->s_flags & SBUF_AUTOEXTEND)
72
73/*
74 * Set / clear flags
75 */
76#define	SBUF_SETFLAG(s, f)	do { (s)->s_flags |= (f); } while (0)
77#define	SBUF_CLEARFLAG(s, f)	do { (s)->s_flags &= ~(f); } while (0)
78
79#define	SBUF_MINEXTENDSIZE	16		/* Should be power of 2. */
80
81#ifdef PAGE_SIZE
82#define	SBUF_MAXEXTENDSIZE	PAGE_SIZE
83#define	SBUF_MAXEXTENDINCR	PAGE_SIZE
84#else
85#define	SBUF_MAXEXTENDSIZE	4096
86#define	SBUF_MAXEXTENDINCR	4096
87#endif
88
89/*
90 * Debugging support
91 */
92#if defined(_KERNEL) && defined(INVARIANTS)
93
94static void
95_assert_sbuf_integrity(const char *fun, struct sbuf *s)
96{
97
98	KASSERT(s != NULL,
99	    ("%s called with a NULL sbuf pointer", fun));
100	KASSERT(s->s_buf != NULL,
101	    ("%s called with uninitialized or corrupt sbuf", fun));
102	KASSERT(s->s_len < s->s_size,
103	    ("wrote past end of sbuf (%jd >= %jd)",
104	    (intmax_t)s->s_len, (intmax_t)s->s_size));
105}
106
107static void
108_assert_sbuf_state(const char *fun, struct sbuf *s, int state)
109{
110
111	KASSERT((s->s_flags & SBUF_FINISHED) == state,
112	    ("%s called with %sfinished or corrupt sbuf", fun,
113	    (state ? "un" : "")));
114}
115
116#define	assert_sbuf_integrity(s) _assert_sbuf_integrity(__func__, (s))
117#define	assert_sbuf_state(s, i)	 _assert_sbuf_state(__func__, (s), (i))
118
119#else /* _KERNEL && INVARIANTS */
120
121#define	assert_sbuf_integrity(s) do { } while (0)
122#define	assert_sbuf_state(s, i)	 do { } while (0)
123
124#endif /* _KERNEL && INVARIANTS */
125
126#ifdef CTASSERT
127CTASSERT(powerof2(SBUF_MAXEXTENDSIZE));
128CTASSERT(powerof2(SBUF_MAXEXTENDINCR));
129#endif
130
131static int
132sbuf_extendsize(int size)
133{
134	int newsize;
135
136	if (size < (int)SBUF_MAXEXTENDSIZE) {
137		newsize = SBUF_MINEXTENDSIZE;
138		while (newsize < size)
139			newsize *= 2;
140	} else {
141		newsize = roundup2(size, SBUF_MAXEXTENDINCR);
142	}
143	KASSERT(newsize >= size, ("%s: %d < %d\n", __func__, newsize, size));
144	return (newsize);
145}
146
147
148/*
149 * Extend an sbuf.
150 */
151static int
152sbuf_extend(struct sbuf *s, int addlen)
153{
154	char *newbuf;
155	int newsize;
156
157	if (!SBUF_CANEXTEND(s))
158		return (-1);
159	newsize = sbuf_extendsize(s->s_size + addlen);
160	newbuf = SBMALLOC(newsize);
161	if (newbuf == NULL)
162		return (-1);
163	bcopy(s->s_buf, newbuf, s->s_size);
164	if (SBUF_ISDYNAMIC(s))
165		SBFREE(s->s_buf);
166	else
167		SBUF_SETFLAG(s, SBUF_DYNAMIC);
168	s->s_buf = newbuf;
169	s->s_size = newsize;
170	return (0);
171}
172
173/*
174 * Initialize an sbuf.
175 * If buf is non-NULL, it points to a static or already-allocated string
176 * big enough to hold at least length characters.
177 */
178struct sbuf *
179sbuf_new(struct sbuf *s, char *buf, int length, int flags)
180{
181
182	KASSERT(length >= 0,
183	    ("attempt to create an sbuf of negative length (%d)", length));
184	KASSERT((flags & ~SBUF_USRFLAGMSK) == 0,
185	    ("%s called with invalid flags", __func__));
186
187	flags &= SBUF_USRFLAGMSK;
188	if (s == NULL) {
189		s = SBMALLOC(sizeof(*s));
190		if (s == NULL)
191			return (NULL);
192		bzero(s, sizeof(*s));
193		s->s_flags = flags;
194		SBUF_SETFLAG(s, SBUF_DYNSTRUCT);
195	} else {
196		bzero(s, sizeof(*s));
197		s->s_flags = flags;
198	}
199	s->s_size = length;
200	if (buf != NULL) {
201		s->s_buf = buf;
202		return (s);
203	}
204	if ((flags & SBUF_AUTOEXTEND) != 0)
205		s->s_size = sbuf_extendsize(s->s_size);
206	s->s_buf = SBMALLOC(s->s_size);
207	if (s->s_buf == NULL) {
208		if (SBUF_ISDYNSTRUCT(s))
209			SBFREE(s);
210		return (NULL);
211	}
212	SBUF_SETFLAG(s, SBUF_DYNAMIC);
213	return (s);
214}
215
216#ifdef _KERNEL
217/*
218 * Create an sbuf with uio data
219 */
220struct sbuf *
221sbuf_uionew(struct sbuf *s, struct uio *uio, int *error)
222{
223
224	KASSERT(uio != NULL,
225	    ("%s called with NULL uio pointer", __func__));
226	KASSERT(error != NULL,
227	    ("%s called with NULL error pointer", __func__));
228
229	s = sbuf_new(s, NULL, uio->uio_resid + 1, 0);
230	if (s == NULL) {
231		*error = ENOMEM;
232		return (NULL);
233	}
234	*error = uiomove(s->s_buf, uio->uio_resid, uio);
235	if (*error != 0) {
236		sbuf_delete(s);
237		return (NULL);
238	}
239	s->s_len = s->s_size - 1;
240	*error = 0;
241	return (s);
242}
243#endif
244
245/*
246 * Clear an sbuf and reset its position.
247 */
248void
249sbuf_clear(struct sbuf *s)
250{
251
252	assert_sbuf_integrity(s);
253	/* don't care if it's finished or not */
254
255	SBUF_CLEARFLAG(s, SBUF_FINISHED);
256	s->s_error = 0;
257	s->s_len = 0;
258}
259
260/*
261 * Set the sbuf's end position to an arbitrary value.
262 * Effectively truncates the sbuf at the new position.
263 */
264int
265sbuf_setpos(struct sbuf *s, ssize_t pos)
266{
267
268	assert_sbuf_integrity(s);
269	assert_sbuf_state(s, 0);
270
271	KASSERT(pos >= 0,
272	    ("attempt to seek to a negative position (%jd)", (intmax_t)pos));
273	KASSERT(pos < s->s_size,
274	    ("attempt to seek past end of sbuf (%jd >= %jd)",
275	    (intmax_t)pos, (intmax_t)s->s_size));
276
277	if (pos < 0 || pos > s->s_len)
278		return (-1);
279	s->s_len = pos;
280	return (0);
281}
282
283/*
284 * Set up a drain function and argument on an sbuf to flush data to
285 * when the sbuf buffer overflows.
286 */
287void
288sbuf_set_drain(struct sbuf *s, sbuf_drain_func *func, void *ctx)
289{
290
291	assert_sbuf_state(s, 0);
292	assert_sbuf_integrity(s);
293	KASSERT(func == s->s_drain_func || s->s_len == 0,
294	    ("Cannot change drain to %p on non-empty sbuf %p", func, s));
295	s->s_drain_func = func;
296	s->s_drain_arg = ctx;
297}
298
299/*
300 * Call the drain and process the return.
301 */
302static int
303sbuf_drain(struct sbuf *s)
304{
305	int len;
306
307	KASSERT(s->s_len > 0, ("Shouldn't drain empty sbuf %p", s));
308	KASSERT(s->s_error == 0, ("Called %s with error on %p", __func__, s));
309	len = s->s_drain_func(s->s_drain_arg, s->s_buf, s->s_len);
310	if (len < 0) {
311		s->s_error = -len;
312		return (s->s_error);
313	}
314	KASSERT(len > 0 && len <= s->s_len,
315	    ("Bad drain amount %d for sbuf %p", len, s));
316	s->s_len -= len;
317	/*
318	 * Fast path for the expected case where all the data was
319	 * drained.
320	 */
321	if (s->s_len == 0)
322		return (0);
323	/*
324	 * Move the remaining characters to the beginning of the
325	 * string.
326	 */
327	memmove(s->s_buf, s->s_buf + len, s->s_len);
328	return (0);
329}
330
331/*
332 * Append a byte to an sbuf.  This is the core function for appending
333 * to an sbuf and is the main place that deals with extending the
334 * buffer and marking overflow.
335 */
336static void
337sbuf_put_byte(struct sbuf *s, int c)
338{
339
340	assert_sbuf_integrity(s);
341	assert_sbuf_state(s, 0);
342
343	if (s->s_error != 0)
344		return;
345	if (SBUF_FREESPACE(s) <= 0) {
346		/*
347		 * If there is a drain, use it, otherwise extend the
348		 * buffer.
349		 */
350		if (s->s_drain_func != NULL)
351			(void)sbuf_drain(s);
352		else if (sbuf_extend(s, 1) < 0)
353			s->s_error = ENOMEM;
354		if (s->s_error != 0)
355			return;
356	}
357	s->s_buf[s->s_len++] = c;
358}
359
360/*
361 * Append a byte string to an sbuf.
362 */
363int
364sbuf_bcat(struct sbuf *s, const void *buf, size_t len)
365{
366	const char *str = buf;
367	const char *end = str + len;
368
369	assert_sbuf_integrity(s);
370	assert_sbuf_state(s, 0);
371
372	if (s->s_error != 0)
373		return (-1);
374	for (; str < end; str++) {
375		sbuf_put_byte(s, *str);
376		if (s->s_error != 0)
377			return (-1);
378	}
379	return (0);
380}
381
382#ifdef _KERNEL
383/*
384 * Copy a byte string from userland into an sbuf.
385 */
386int
387sbuf_bcopyin(struct sbuf *s, const void *uaddr, size_t len)
388{
389
390	assert_sbuf_integrity(s);
391	assert_sbuf_state(s, 0);
392	KASSERT(s->s_drain_func == NULL,
393	    ("Nonsensical copyin to sbuf %p with a drain", s));
394
395	if (s->s_error != 0)
396		return (-1);
397	if (len == 0)
398		return (0);
399	if (len > SBUF_FREESPACE(s)) {
400		sbuf_extend(s, len - SBUF_FREESPACE(s));
401		if (SBUF_FREESPACE(s) < len)
402			len = SBUF_FREESPACE(s);
403	}
404	if (copyin(uaddr, s->s_buf + s->s_len, len) != 0)
405		return (-1);
406	s->s_len += len;
407
408	return (0);
409}
410#endif
411
412/*
413 * Copy a byte string into an sbuf.
414 */
415int
416sbuf_bcpy(struct sbuf *s, const void *buf, size_t len)
417{
418
419	assert_sbuf_integrity(s);
420	assert_sbuf_state(s, 0);
421
422	sbuf_clear(s);
423	return (sbuf_bcat(s, buf, len));
424}
425
426/*
427 * Append a string to an sbuf.
428 */
429int
430sbuf_cat(struct sbuf *s, const char *str)
431{
432
433	assert_sbuf_integrity(s);
434	assert_sbuf_state(s, 0);
435
436	if (s->s_error != 0)
437		return (-1);
438
439	while (*str != '\0') {
440		sbuf_put_byte(s, *str++);
441		if (s->s_error != 0)
442			return (-1);
443	}
444	return (0);
445}
446
447#ifdef _KERNEL
448/*
449 * Append a string from userland to an sbuf.
450 */
451int
452sbuf_copyin(struct sbuf *s, const void *uaddr, size_t len)
453{
454	size_t done;
455
456	assert_sbuf_integrity(s);
457	assert_sbuf_state(s, 0);
458	KASSERT(s->s_drain_func == NULL,
459	    ("Nonsensical copyin to sbuf %p with a drain", s));
460
461	if (s->s_error != 0)
462		return (-1);
463
464	if (len == 0)
465		len = SBUF_FREESPACE(s);	/* XXX return 0? */
466	if (len > SBUF_FREESPACE(s)) {
467		sbuf_extend(s, len);
468		if (SBUF_FREESPACE(s) < len)
469			len = SBUF_FREESPACE(s);
470	}
471	switch (copyinstr(uaddr, s->s_buf + s->s_len, len + 1, &done)) {
472	case ENAMETOOLONG:
473		s->s_error = ENOMEM;
474		/* fall through */
475	case 0:
476		s->s_len += done - 1;
477		break;
478	default:
479		return (-1);	/* XXX */
480	}
481
482	return (done);
483}
484#endif
485
486/*
487 * Copy a string into an sbuf.
488 */
489int
490sbuf_cpy(struct sbuf *s, const char *str)
491{
492
493	assert_sbuf_integrity(s);
494	assert_sbuf_state(s, 0);
495
496	sbuf_clear(s);
497	return (sbuf_cat(s, str));
498}
499
500/*
501 * Format the given argument list and append the resulting string to an sbuf.
502 */
503#ifdef _KERNEL
504
505/*
506 * Append a non-NUL character to an sbuf.  This prototype signature is
507 * suitable for use with kvprintf(9).
508 */
509static void
510sbuf_putc_func(int c, void *arg)
511{
512
513	if (c != '\0')
514		sbuf_put_byte(arg, c);
515}
516
517int
518sbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap)
519{
520
521	assert_sbuf_integrity(s);
522	assert_sbuf_state(s, 0);
523
524	KASSERT(fmt != NULL,
525	    ("%s called with a NULL format string", __func__));
526
527	(void)kvprintf(fmt, sbuf_putc_func, s, 10, ap);
528	if (s->s_error != 0)
529		return (-1);
530	return (0);
531}
532#else /* !_KERNEL */
533int
534sbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap)
535{
536	va_list ap_copy;
537	int error, len;
538
539	assert_sbuf_integrity(s);
540	assert_sbuf_state(s, 0);
541
542	KASSERT(fmt != NULL,
543	    ("%s called with a NULL format string", __func__));
544
545	if (s->s_error != 0)
546		return (-1);
547
548	/*
549	 * For the moment, there is no way to get vsnprintf(3) to hand
550	 * back a character at a time, to push everything into
551	 * sbuf_putc_func() as was done for the kernel.
552	 *
553	 * In userspace, while drains are useful, there's generally
554	 * not a problem attempting to malloc(3) on out of space.  So
555	 * expand a userland sbuf if there is not enough room for the
556	 * data produced by sbuf_[v]printf(3).
557	 */
558
559	error = 0;
560	do {
561		va_copy(ap_copy, ap);
562		len = vsnprintf(&s->s_buf[s->s_len], SBUF_FREESPACE(s) + 1,
563		    fmt, ap_copy);
564		va_end(ap_copy);
565
566		if (SBUF_FREESPACE(s) >= len)
567			break;
568		/* Cannot print with the current available space. */
569		if (s->s_drain_func != NULL && s->s_len > 0)
570			error = sbuf_drain(s);
571		else
572			error = sbuf_extend(s, len - SBUF_FREESPACE(s));
573	} while (error == 0);
574
575	/*
576	 * s->s_len is the length of the string, without the terminating nul.
577	 * When updating s->s_len, we must subtract 1 from the length that
578	 * we passed into vsnprintf() because that length includes the
579	 * terminating nul.
580	 *
581	 * vsnprintf() returns the amount that would have been copied,
582	 * given sufficient space, so don't over-increment s_len.
583	 */
584	if (SBUF_FREESPACE(s) < len)
585		len = SBUF_FREESPACE(s);
586	s->s_len += len;
587	if (!SBUF_HASROOM(s) && !SBUF_CANEXTEND(s))
588		s->s_error = ENOMEM;
589
590	KASSERT(s->s_len < s->s_size,
591	    ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
592
593	if (s->s_error != 0)
594		return (-1);
595	return (0);
596}
597#endif /* _KERNEL */
598
599/*
600 * Format the given arguments and append the resulting string to an sbuf.
601 */
602int
603sbuf_printf(struct sbuf *s, const char *fmt, ...)
604{
605	va_list ap;
606	int result;
607
608	va_start(ap, fmt);
609	result = sbuf_vprintf(s, fmt, ap);
610	va_end(ap);
611	return (result);
612}
613
614/*
615 * Append a character to an sbuf.
616 */
617int
618sbuf_putc(struct sbuf *s, int c)
619{
620
621	sbuf_put_byte(s, c);
622	if (s->s_error != 0)
623		return (-1);
624	return (0);
625}
626
627/*
628 * Trim whitespace characters from end of an sbuf.
629 */
630int
631sbuf_trim(struct sbuf *s)
632{
633
634	assert_sbuf_integrity(s);
635	assert_sbuf_state(s, 0);
636	KASSERT(s->s_drain_func == NULL,
637	    ("%s makes no sense on sbuf %p with drain", __func__, s));
638
639	if (s->s_error != 0)
640		return (-1);
641
642	while (s->s_len > 0 && isspace(s->s_buf[s->s_len-1]))
643		--s->s_len;
644
645	return (0);
646}
647
648/*
649 * Check if an sbuf has an error.
650 */
651int
652sbuf_error(const struct sbuf *s)
653{
654
655	return (s->s_error);
656}
657
658/*
659 * Finish off an sbuf.
660 */
661int
662sbuf_finish(struct sbuf *s)
663{
664
665	assert_sbuf_integrity(s);
666	assert_sbuf_state(s, 0);
667
668	if (s->s_drain_func != NULL) {
669		while (s->s_len > 0 && s->s_error == 0)
670			s->s_error = sbuf_drain(s);
671	}
672	s->s_buf[s->s_len] = '\0';
673	SBUF_SETFLAG(s, SBUF_FINISHED);
674#ifdef _KERNEL
675	return (s->s_error);
676#else
677	errno = s->s_error;
678	if (s->s_error)
679		return (-1);
680	return (0);
681#endif
682}
683
684/*
685 * Return a pointer to the sbuf data.
686 */
687char *
688sbuf_data(struct sbuf *s)
689{
690
691	assert_sbuf_integrity(s);
692	assert_sbuf_state(s, SBUF_FINISHED);
693	KASSERT(s->s_drain_func == NULL,
694	    ("%s makes no sense on sbuf %p with drain", __func__, s));
695
696	return (s->s_buf);
697}
698
699/*
700 * Return the length of the sbuf data.
701 */
702ssize_t
703sbuf_len(struct sbuf *s)
704{
705
706	assert_sbuf_integrity(s);
707	/* don't care if it's finished or not */
708	KASSERT(s->s_drain_func == NULL,
709	    ("%s makes no sense on sbuf %p with drain", __func__, s));
710
711	if (s->s_error != 0)
712		return (-1);
713	return (s->s_len);
714}
715
716/*
717 * Clear an sbuf, free its buffer if necessary.
718 */
719void
720sbuf_delete(struct sbuf *s)
721{
722	int isdyn;
723
724	assert_sbuf_integrity(s);
725	/* don't care if it's finished or not */
726
727	if (SBUF_ISDYNAMIC(s))
728		SBFREE(s->s_buf);
729	isdyn = SBUF_ISDYNSTRUCT(s);
730	bzero(s, sizeof(*s));
731	if (isdyn)
732		SBFREE(s);
733}
734
735/*
736 * Check if an sbuf has been finished.
737 */
738int
739sbuf_done(const struct sbuf *s)
740{
741
742	return (SBUF_ISFINISHED(s));
743}
744