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