xref: /openbsd-src/usr.sbin/smtpd/stat_backend.c (revision e5157e49389faebcb42b7237d55fbf096d9c2523)
1 /*	$OpenBSD: stat_backend.c,v 1.9 2014/07/08 10:22:15 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 <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/queue.h>
22 #include <sys/tree.h>
23 
24 #include <event.h>
25 #include <imsg.h>
26 #include <stdio.h>
27 #include <string.h>
28 
29 #include "log.h"
30 #include "smtpd.h"
31 
32 struct stat_backend	stat_backend_ramstat;
33 struct stat_backend	stat_backend_sqlite;
34 
35 struct stat_backend *
36 stat_backend_lookup(const char *name)
37 {
38 	if (!strcmp(name, "ram"))
39 		return &stat_backend_ramstat;
40 
41 	if (!strcmp(name, "sqlite"))
42 		return &stat_backend_sqlite;
43 
44 	return (NULL);
45 }
46 
47 void
48 stat_increment(const char *key, size_t count)
49 {
50 	struct stat_value	*value;
51 
52 	if (count == 0)
53 		return;
54 
55 	value = stat_counter(count);
56 
57 	m_create(p_control, IMSG_STAT_INCREMENT, 0, 0, -1);
58 	m_add_string(p_control, key);
59 	m_add_data(p_control, value, sizeof(*value));
60 	m_close(p_control);
61 }
62 
63 void
64 stat_decrement(const char *key, size_t count)
65 {
66 	struct stat_value	*value;
67 
68 	if (count == 0)
69 		return;
70 
71 	value = stat_counter(count);
72 
73 	m_create(p_control, IMSG_STAT_DECREMENT, 0, 0, -1);
74 	m_add_string(p_control, key);
75 	m_add_data(p_control, value, sizeof(*value));
76 	m_close(p_control);
77 }
78 
79 void
80 stat_set(const char *key, const struct stat_value *value)
81 {
82 	m_create(p_control, IMSG_STAT_SET, 0, 0, -1);
83 	m_add_string(p_control, key);
84 	m_add_data(p_control, value, sizeof(*value));
85 	m_close(p_control);
86 }
87 
88 /* helpers */
89 
90 struct stat_value *
91 stat_counter(size_t counter)
92 {
93 	static struct stat_value value;
94 
95 	value.type = STAT_COUNTER;
96 	value.u.counter = counter;
97 	return &value;
98 }
99 
100 struct stat_value *
101 stat_timestamp(time_t timestamp)
102 {
103 	static struct stat_value value;
104 
105 	value.type = STAT_TIMESTAMP;
106 	value.u.timestamp = timestamp;
107 	return &value;
108 }
109 
110 struct stat_value *
111 stat_timeval(struct timeval *tv)
112 {
113 	static struct stat_value value;
114 
115 	value.type = STAT_TIMEVAL;
116 	value.u.tv = *tv;
117 	return &value;
118 }
119 
120 struct stat_value *
121 stat_timespec(struct timespec *ts)
122 {
123 	static struct stat_value value;
124 
125 	value.type = STAT_TIMESPEC;
126 	value.u.ts = *ts;
127 	return &value;
128 }
129