Merge branch 'collectd-4.3' into collectd-4.4
[collectd.git] / src / common.h
1 /**
2  * collectd - src/common.h
3  * Copyright (C) 2005-2008  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  *   Niki W. Waibel <niki.waibel@gmx.net>
21 **/
22
23 #ifndef COMMON_H
24 #define COMMON_H
25
26 #include "collectd.h"
27 #include "plugin.h"
28
29 #if HAVE_PWD_H
30 # include <pwd.h>
31 #endif
32
33 #define sfree(ptr) \
34         do { \
35                 if((ptr) != NULL) { \
36                         free(ptr); \
37                 } \
38                 (ptr) = NULL; \
39         } while (0)
40
41 #define STATIC_ARRAY_SIZE(a) (sizeof (a) / sizeof (*(a)))
42
43 char *sstrncpy (char *dest, const char *src, size_t n);
44 char *sstrdup(const char *s);
45 void *smalloc(size_t size);
46 char *sstrerror (int errnum, char *buf, size_t buflen);
47
48 /*
49  * NAME
50  *   sread
51  *
52  * DESCRIPTION
53  *   Reads exactly `n' bytes or fails. Syntax and other behavior is analogous
54  *   to `read(2)'. If EOF is received the file descriptor is closed and an
55  *   error is returned.
56  *
57  * PARAMETERS
58  *   `fd'          File descriptor to write to.
59  *   `buf'         Buffer that is to be written.
60  *   `count'       Number of bytes in the buffer.
61  *
62  * RETURN VALUE
63  *   Zero upon success or non-zero if an error occurred. `errno' is set in this
64  *   case.
65  */
66 ssize_t sread (int fd, void *buf, size_t count);
67
68 /*
69  * NAME
70  *   swrite
71  *
72  * DESCRIPTION
73  *   Writes exactly `n' bytes or fails. Syntax and other behavior is analogous
74  *   to `write(2)'.
75  *
76  * PARAMETERS
77  *   `fd'          File descriptor to write to.
78  *   `buf'         Buffer that is to be written.
79  *   `count'       Number of bytes in the buffer.
80  *
81  * RETURN VALUE
82  *   Zero upon success or non-zero if an error occurred. `errno' is set in this
83  *   case.
84  */
85 ssize_t swrite (int fd, const void *buf, size_t count);
86
87 /*
88  * NAME
89  *   strsplit
90  *
91  * DESCRIPTION
92  *   Splits a string into parts and stores pointers to the parts in `fields'.
93  *   The characters split at are ` ' (space) and "\t" (tab).
94  *
95  * PARAMETERS
96  *   `string'      String to split. This string will be modified. `fields' will
97  *                 contain pointers to parts of this string, so free'ing it
98  *                 will destroy `fields' as well.
99  *   `fields'      Array of strings where pointers to the parts will be stored.
100  *   `size'        Number of elements in the array. No more than `size'
101  *                 pointers will be stored in `fields'.
102  *
103  * RETURN VALUE
104  *    Returns the number of parts stored in `fields'.
105  */
106 int strsplit (char *string, char **fields, size_t size);
107
108 /*
109  * NAME
110  *   strjoin
111  *
112  * DESCRIPTION
113  *   Joins together several parts of a string using `sep' as a separator. This
114  *   is equivalent to the Perl built-in `join'.
115  *
116  * PARAMETERS
117  *   `dst'         Buffer where the result is stored.
118  *   `dst_len'     Length of the destination buffer. No more than this many
119  *                 bytes will be written to the memory pointed to by `dst',
120  *                 including the trailing null-byte.
121  *   `fields'      Array of strings to be joined.
122  *   `fields_num'  Number of elements in the `fields' array.
123  *   `sep'         String to be inserted between any two elements of `fields'.
124  *                 This string is neither prepended nor appended to the result.
125  *                 Instead of passing "" (empty string) one can pass NULL.
126  *
127  * RETURN VALUE
128  *   Returns the number of characters in `dst', NOT including the trailing
129  *   null-byte. If an error occurred (empty array or `dst' too small) a value
130  *   smaller than zero will be returned.
131  */
132 int strjoin (char *dst, size_t dst_len, char **fields, size_t fields_num, const char *sep);
133
134 /*
135  * NAME
136  *   escape_slashes
137  *
138  * DESCRIPTION
139  *   Removes slashes from the string `buf' and substitutes them with something
140  *   appropriate. This function should be used whenever a path is to be used as
141  *   (part of) an instance.
142  *
143  * PARAMETERS
144  *   `buf'         String to be escaped.
145  *   `buf_len'     Length of the buffer. No more then this many bytes will be
146  *   written to `buf', including the trailing null-byte.
147  *
148  * RETURN VALUE
149  *   Returns zero upon success and a value smaller than zero upon failure.
150  */
151 int escape_slashes (char *buf, int buf_len);
152
153 int strsubstitute (char *str, char c_from, char c_to);
154
155 /* FIXME: `timeval_sub_timespec' needs a description */
156 int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret);
157
158 int check_create_dir (const char *file_orig);
159
160 #ifdef HAVE_LIBKSTAT
161 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name);
162 long long get_kstat_value (kstat_t *ksp, char *name);
163 #endif
164
165 unsigned long long ntohll (unsigned long long n);
166 unsigned long long htonll (unsigned long long n);
167
168 #if FP_LAYOUT_NEED_NOTHING
169 # define ntohd(d) (d)
170 # define htond(d) (d)
171 #elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP
172 double ntohd (double d);
173 double htond (double d);
174 #else
175 # error "Don't know how to convert between host and network representation of doubles."
176 #endif
177
178 int format_name (char *ret, int ret_len,
179                 const char *hostname,
180                 const char *plugin, const char *plugin_instance,
181                 const char *type, const char *type_instance);
182 #define FORMAT_VL(ret, ret_len, vl, ds) \
183         format_name (ret, ret_len, (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
184                         (ds)->type, (vl)->type_instance)
185
186 int parse_identifier (char *str, char **ret_host,
187                 char **ret_plugin, char **ret_plugin_instance,
188                 char **ret_type, char **ret_type_instance);
189 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds);
190
191 #if !HAVE_GETPWNAM_R
192 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
193                 size_t buflen, struct passwd **pwbufp);
194 #endif
195
196 int notification_init (notification_t *n, int severity, const char *message,
197                 const char *host,
198                 const char *plugin, const char *plugin_instance,
199                 const char *type, const char *type_instance);
200 #define NOTIFICATION_INIT_VL(n, vl, ds) \
201         notification_init (n, NOTIF_FAILURE, NULL, \
202                         (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
203                         (ds)->type, (vl)->type_instance)
204 #endif /* COMMON_H */