xref: /openbsd/usr.sbin/smtpd/stat_backend.c (revision d3140113)
1 /*	$OpenBSD: stat_backend.c,v 1.12 2021/06/14 17:58:16 eric Exp $	*/
2 
3 /*
4  * Copyright (c) 2012 Gilles Chehade <gilles@poolp.org>
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 #include "smtpd.h"
20 
21 extern struct stat_backend	stat_backend_ramstat;
22 
23 struct stat_backend *
stat_backend_lookup(const char * name)24 stat_backend_lookup(const char *name)
25 {
26 	return &stat_backend_ramstat;
27 }
28 
29 void
stat_increment(const char * key,size_t count)30 stat_increment(const char *key, size_t count)
31 {
32 	struct stat_value	*value;
33 
34 	if (count == 0)
35 		return;
36 
37 	value = stat_counter(count);
38 
39 	m_create(p_control, IMSG_STAT_INCREMENT, 0, 0, -1);
40 	m_add_string(p_control, key);
41 	m_add_data(p_control, value, sizeof(*value));
42 	m_close(p_control);
43 }
44 
45 void
stat_decrement(const char * key,size_t count)46 stat_decrement(const char *key, size_t count)
47 {
48 	struct stat_value	*value;
49 
50 	if (count == 0)
51 		return;
52 
53 	value = stat_counter(count);
54 
55 	m_create(p_control, IMSG_STAT_DECREMENT, 0, 0, -1);
56 	m_add_string(p_control, key);
57 	m_add_data(p_control, value, sizeof(*value));
58 	m_close(p_control);
59 }
60 
61 void
stat_set(const char * key,const struct stat_value * value)62 stat_set(const char *key, const struct stat_value *value)
63 {
64 	m_create(p_control, IMSG_STAT_SET, 0, 0, -1);
65 	m_add_string(p_control, key);
66 	m_add_data(p_control, value, sizeof(*value));
67 	m_close(p_control);
68 }
69 
70 /* helpers */
71 
72 struct stat_value *
stat_counter(size_t counter)73 stat_counter(size_t counter)
74 {
75 	static struct stat_value value;
76 
77 	value.type = STAT_COUNTER;
78 	value.u.counter = counter;
79 	return &value;
80 }
81 
82 struct stat_value *
stat_timestamp(time_t timestamp)83 stat_timestamp(time_t timestamp)
84 {
85 	static struct stat_value value;
86 
87 	value.type = STAT_TIMESTAMP;
88 	value.u.timestamp = timestamp;
89 	return &value;
90 }
91 
92 struct stat_value *
stat_timeval(struct timeval * tv)93 stat_timeval(struct timeval *tv)
94 {
95 	static struct stat_value value;
96 
97 	value.type = STAT_TIMEVAL;
98 	value.u.tv = *tv;
99 	return &value;
100 }
101 
102 struct stat_value *
stat_timespec(struct timespec * ts)103 stat_timespec(struct timespec *ts)
104 {
105 	static struct stat_value value;
106 
107 	value.type = STAT_TIMESPEC;
108 	value.u.ts = *ts;
109 	return &value;
110 }
111