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