xref: /openbsd-src/usr.sbin/syslogd/ringbuf.c (revision 35b969541e64fc711bac159214df286d5b03035c)
1 /* $OpenBSD: ringbuf.c,v 1.3 2004/06/25 19:10:54 djm Exp $ */
2 
3 /*
4  * Copyright (c) 2004 Damien Miller
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * Simple ringbuffer for lines of text.
21  */
22 
23 #include <sys/types.h>
24 #include <sys/param.h>
25 #include <sys/uio.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 
32 #include "syslogd.h"
33 
34 /* Initialise a ring buffer */
35 struct ringbuf *
36 ringbuf_init(size_t len)
37 {
38 	struct ringbuf *ret;
39 
40 	if (len == 0 || (ret = malloc(sizeof(*ret))) == NULL)
41 		return (NULL);
42 
43 	if ((ret->buf = malloc(len)) == NULL) {
44 		free(ret);
45 		return (NULL);
46 	}
47 
48 	ret->len = len;
49 	ret->start = ret->end = 0;
50 
51 	return (ret);
52 }
53 
54 /* Clear a ring buffer */
55 void
56 ringbuf_clear(struct ringbuf *rb)
57 {
58 	rb->start = rb->end = 0;
59 }
60 
61 /* Return the number of bytes used in a ringbuffer */
62 size_t
63 ringbuf_used(struct ringbuf *rb)
64 {
65 	return ((rb->len + rb->end - rb->start) % rb->len);
66 }
67 
68 /*
69  * Append a line to a ring buffer, will delete lines from start
70  * of buffer as necessary
71  */
72 int
73 ringbuf_append_line(struct ringbuf *rb, char *line)
74 {
75 	size_t llen, used, copy_len;
76 	int overflow = 0;
77 
78 	if (rb == NULL || line == NULL)
79 		return (-1);
80 
81 	llen = strlen(line);
82 	if (line[llen - 1] != '\n')
83 		llen++; /* one extra for appended '\n' */
84 
85 	if (rb == NULL || llen == 0 || llen >= rb->len)
86 		return (-1);
87 
88 	/*
89 	 * If necessary, advance start pointer to make room for appended
90 	 * string. Ensure that start pointer is at the beginning of a line
91 	 * once we are done (i.e move to after '\n').
92 	 */
93 	used = ringbuf_used(rb);
94 	if (used + llen >= rb->len) {
95 		rb->start = (rb->start + used + llen - rb->len) % rb->len;
96 
97 		/* Find next '\n' */
98 		while (rb->buf[rb->start] != '\n')
99 			rb->start = (rb->start + 1) % rb->len;
100 		/* Skip it */
101 		rb->start = (rb->start + 1) % rb->len;
102 
103 		overflow = 1;
104 	}
105 
106 	/*
107 	 * Now append string, starting from last pointer and wrapping if
108 	 * necessary
109 	 */
110 	if (rb->end + llen > rb->len) {
111 		copy_len = rb->len - rb->end;
112 		memcpy(rb->buf + rb->end, line, copy_len);
113 		memcpy(rb->buf, line + copy_len, llen - copy_len - 1);
114 		rb->buf[llen - copy_len - 1] = '\n';
115 	} else {
116 		memcpy(rb->buf + rb->end, line, llen - 1);
117 		rb->buf[rb->end + llen - 1] = '\n';
118 	}
119 
120 	rb->end = (rb->end + llen) % rb->len;
121 
122 	return (overflow);
123 }
124 
125 /*
126  * Copy and nul-terminate a ringbuffer to a string.
127  */
128 ssize_t
129 ringbuf_to_string(char *buf, size_t len, struct ringbuf *rb)
130 {
131 	ssize_t copy_len, n;
132 
133 	if (buf == NULL || rb == NULL)
134 		return (-1);
135 
136 	copy_len = MIN(len - 1, ringbuf_used(rb));
137 
138 	if (copy_len <= 0)
139 		return (copy_len);
140 
141 	if (rb->start < rb->end)
142 		memcpy(buf, rb->buf + rb->start, copy_len);
143 	else {
144 		/* If the buffer is wrapped, copy each hunk separately */
145 		n = rb->len - rb->start;
146 		memcpy(buf, rb->buf + rb->start, MIN(n, copy_len));
147 		if (copy_len - n > 0)
148 			memcpy(buf + n, rb->buf, MIN(rb->end, copy_len - n));
149 	}
150 	buf[copy_len] = '\0';
151 
152 	return (ringbuf_used(rb));
153 }
154