xref: /openbsd-src/usr.sbin/smtpd/stat_backend.c (revision d3140113bef2b86d3af61dd20c05a8630ff966c2)
1*d3140113Seric /*	$OpenBSD: stat_backend.c,v 1.12 2021/06/14 17:58:16 eric Exp $	*/
2b398bda7Schl 
39ed3223cSgilles /*
465c4fdfbSgilles  * Copyright (c) 2012 Gilles Chehade <gilles@poolp.org>
59ed3223cSgilles  *
69ed3223cSgilles  * Permission to use, copy, modify, and distribute this software for any
79ed3223cSgilles  * purpose with or without fee is hereby granted, provided that the above
89ed3223cSgilles  * copyright notice and this permission notice appear in all copies.
99ed3223cSgilles  *
109ed3223cSgilles  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
119ed3223cSgilles  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
129ed3223cSgilles  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
139ed3223cSgilles  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
149ed3223cSgilles  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
159ed3223cSgilles  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
169ed3223cSgilles  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
179ed3223cSgilles  */
189ed3223cSgilles 
199ed3223cSgilles #include "smtpd.h"
209ed3223cSgilles 
217769853bSgilles extern struct stat_backend	stat_backend_ramstat;
229ed3223cSgilles 
239ed3223cSgilles struct stat_backend *
stat_backend_lookup(const char * name)249ed3223cSgilles stat_backend_lookup(const char *name)
259ed3223cSgilles {
269ed3223cSgilles 	return &stat_backend_ramstat;
279ed3223cSgilles }
289ed3223cSgilles 
299ed3223cSgilles void
stat_increment(const char * key,size_t count)3065c4fdfbSgilles stat_increment(const char *key, size_t count)
319ed3223cSgilles {
32e4632cf4Sgilles 	struct stat_value	*value;
339ed3223cSgilles 
3456c5b5b4Seric 	if (count == 0)
3556c5b5b4Seric 		return;
3656c5b5b4Seric 
37e4632cf4Sgilles 	value = stat_counter(count);
389ed3223cSgilles 
39299c4efeSeric 	m_create(p_control, IMSG_STAT_INCREMENT, 0, 0, -1);
4065c4fdfbSgilles 	m_add_string(p_control, key);
4165c4fdfbSgilles 	m_add_data(p_control, value, sizeof(*value));
4265c4fdfbSgilles 	m_close(p_control);
43e4632cf4Sgilles }
44e4632cf4Sgilles 
45e4632cf4Sgilles void
stat_decrement(const char * key,size_t count)4665c4fdfbSgilles stat_decrement(const char *key, size_t count)
47e4632cf4Sgilles {
48e4632cf4Sgilles 	struct stat_value	*value;
49e4632cf4Sgilles 
5056c5b5b4Seric 	if (count == 0)
5156c5b5b4Seric 		return;
5256c5b5b4Seric 
53e4632cf4Sgilles 	value = stat_counter(count);
54e4632cf4Sgilles 
55299c4efeSeric 	m_create(p_control, IMSG_STAT_DECREMENT, 0, 0, -1);
5665c4fdfbSgilles 	m_add_string(p_control, key);
5765c4fdfbSgilles 	m_add_data(p_control, value, sizeof(*value));
5865c4fdfbSgilles 	m_close(p_control);
59e4632cf4Sgilles }
60e4632cf4Sgilles 
61e4632cf4Sgilles void
stat_set(const char * key,const struct stat_value * value)6265c4fdfbSgilles stat_set(const char *key, const struct stat_value *value)
63e4632cf4Sgilles {
64299c4efeSeric 	m_create(p_control, IMSG_STAT_SET, 0, 0, -1);
6565c4fdfbSgilles 	m_add_string(p_control, key);
6665c4fdfbSgilles 	m_add_data(p_control, value, sizeof(*value));
6765c4fdfbSgilles 	m_close(p_control);
68e4632cf4Sgilles }
69e4632cf4Sgilles 
70e4632cf4Sgilles /* helpers */
71e4632cf4Sgilles 
72e4632cf4Sgilles struct stat_value *
stat_counter(size_t counter)73e4632cf4Sgilles stat_counter(size_t counter)
74e4632cf4Sgilles {
75e4632cf4Sgilles 	static struct stat_value value;
76e4632cf4Sgilles 
77e4632cf4Sgilles 	value.type = STAT_COUNTER;
78e4632cf4Sgilles 	value.u.counter = counter;
79e4632cf4Sgilles 	return &value;
80e4632cf4Sgilles }
81e4632cf4Sgilles 
82e4632cf4Sgilles struct stat_value *
stat_timestamp(time_t timestamp)83e4632cf4Sgilles stat_timestamp(time_t timestamp)
84e4632cf4Sgilles {
85e4632cf4Sgilles 	static struct stat_value value;
86e4632cf4Sgilles 
87e4632cf4Sgilles 	value.type = STAT_TIMESTAMP;
88e4632cf4Sgilles 	value.u.timestamp = timestamp;
89e4632cf4Sgilles 	return &value;
90e4632cf4Sgilles }
91e4632cf4Sgilles 
92e4632cf4Sgilles struct stat_value *
stat_timeval(struct timeval * tv)93e4632cf4Sgilles stat_timeval(struct timeval *tv)
94e4632cf4Sgilles {
95e4632cf4Sgilles 	static struct stat_value value;
96e4632cf4Sgilles 
97e4632cf4Sgilles 	value.type = STAT_TIMEVAL;
98e4632cf4Sgilles 	value.u.tv = *tv;
99e4632cf4Sgilles 	return &value;
100e4632cf4Sgilles }
101e4632cf4Sgilles 
102e4632cf4Sgilles struct stat_value *
stat_timespec(struct timespec * ts)103e4632cf4Sgilles stat_timespec(struct timespec *ts)
104e4632cf4Sgilles {
105e4632cf4Sgilles 	static struct stat_value value;
106e4632cf4Sgilles 
107e4632cf4Sgilles 	value.type = STAT_TIMESPEC;
108e4632cf4Sgilles 	value.u.ts = *ts;
109e4632cf4Sgilles 	return &value;
1109ed3223cSgilles }
111