Code

Merge remote-tracking branch 'github/pr/387'
[collectd.git] / src / common.h
1 /**
2  * collectd - src/common.h
3  * Copyright (C) 2005-2010  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 collectd.org>
20  *   Niki W. Waibel <niki.waibel@gmx.net>
21 **/
23 #ifndef COMMON_H
24 #define COMMON_H
26 #include "collectd.h"
27 #include "plugin.h"
29 #if HAVE_PWD_H
30 # include <pwd.h>
31 #endif
33 #define sfree(ptr) \
34         do { \
35                 if((ptr) != NULL) { \
36                         free(ptr); \
37                 } \
38                 (ptr) = NULL; \
39         } while (0)
41 #define STATIC_ARRAY_SIZE(a) (sizeof (a) / sizeof (*(a)))
43 #define IS_TRUE(s) ((strcasecmp ("true", (s)) == 0) \
44                 || (strcasecmp ("yes", (s)) == 0) \
45                 || (strcasecmp ("on", (s)) == 0))
46 #define IS_FALSE(s) ((strcasecmp ("false", (s)) == 0) \
47                 || (strcasecmp ("no", (s)) == 0) \
48                 || (strcasecmp ("off", (s)) == 0))
50 struct rate_to_value_state_s
51 {
52   value_t last_value;
53   cdtime_t last_time;
54   gauge_t residual;
55 };
56 typedef struct rate_to_value_state_s rate_to_value_state_t;
58 char *sstrncpy (char *dest, const char *src, size_t n);
60 __attribute__ ((format(printf,3,4)))
61 int ssnprintf (char *dest, size_t n, const char *format, ...);
63 __attribute__ ((format(printf,1,2)))
64 char *ssnprintf_alloc (char const *format, ...);
66 char *sstrdup(const char *s);
67 void *smalloc(size_t size);
68 char *sstrerror (int errnum, char *buf, size_t buflen);
70 /*
71  * NAME
72  *   sread
73  *
74  * DESCRIPTION
75  *   Reads exactly `n' bytes or fails. Syntax and other behavior is analogous
76  *   to `read(2)'. If EOF is received the file descriptor is closed and an
77  *   error is returned.
78  *
79  * PARAMETERS
80  *   `fd'          File descriptor to write to.
81  *   `buf'         Buffer that is to be written.
82  *   `count'       Number of bytes in the buffer.
83  *
84  * RETURN VALUE
85  *   Zero upon success or non-zero if an error occurred. `errno' is set in this
86  *   case.
87  */
88 ssize_t sread (int fd, void *buf, size_t count);
90 /*
91  * NAME
92  *   swrite
93  *
94  * DESCRIPTION
95  *   Writes exactly `n' bytes or fails. Syntax and other behavior is analogous
96  *   to `write(2)'.
97  *
98  * PARAMETERS
99  *   `fd'          File descriptor to write to.
100  *   `buf'         Buffer that is to be written.
101  *   `count'       Number of bytes in the buffer.
102  *
103  * RETURN VALUE
104  *   Zero upon success or non-zero if an error occurred. `errno' is set in this
105  *   case.
106  */
107 ssize_t swrite (int fd, const void *buf, size_t count);
109 /*
110  * NAME
111  *   strsplit
112  *
113  * DESCRIPTION
114  *   Splits a string into parts and stores pointers to the parts in `fields'.
115  *   The characters split at are: " ", "\t", "\r", and "\n".
116  *
117  * PARAMETERS
118  *   `string'      String to split. This string will be modified. `fields' will
119  *                 contain pointers to parts of this string, so free'ing it
120  *                 will destroy `fields' as well.
121  *   `fields'      Array of strings where pointers to the parts will be stored.
122  *   `size'        Number of elements in the array. No more than `size'
123  *                 pointers will be stored in `fields'.
124  *
125  * RETURN VALUE
126  *    Returns the number of parts stored in `fields'.
127  */
128 int strsplit (char *string, char **fields, size_t size);
130 /*
131  * NAME
132  *   strjoin
133  *
134  * DESCRIPTION
135  *   Joins together several parts of a string using `sep' as a separator. This
136  *   is equivalent to the Perl built-in `join'.
137  *
138  * PARAMETERS
139  *   `dst'         Buffer where the result is stored.
140  *   `dst_len'     Length of the destination buffer. No more than this many
141  *                 bytes will be written to the memory pointed to by `dst',
142  *                 including the trailing null-byte.
143  *   `fields'      Array of strings to be joined.
144  *   `fields_num'  Number of elements in the `fields' array.
145  *   `sep'         String to be inserted between any two elements of `fields'.
146  *                 This string is neither prepended nor appended to the result.
147  *                 Instead of passing "" (empty string) one can pass NULL.
148  *
149  * RETURN VALUE
150  *   Returns the number of characters in `dst', NOT including the trailing
151  *   null-byte. If an error occurred (empty array or `dst' too small) a value
152  *   smaller than zero will be returned.
153  */
154 int strjoin (char *dst, size_t dst_len, char **fields, size_t fields_num, const char *sep);
156 /*
157  * NAME
158  *   escape_slashes
159  *
160  * DESCRIPTION
161  *   Removes slashes from the string `buf' and substitutes them with something
162  *   appropriate. This function should be used whenever a path is to be used as
163  *   (part of) an instance.
164  *
165  * PARAMETERS
166  *   `buf'         String to be escaped.
167  *   `buf_len'     Length of the buffer. No more then this many bytes will be
168  *   written to `buf', including the trailing null-byte.
169  *
170  * RETURN VALUE
171  *   Returns zero upon success and a value smaller than zero upon failure.
172  */
173 int escape_slashes (char *buf, int buf_len);
175 /*
176  * NAME
177  *   replace_special
178  *
179  * DESCRIPTION
180  *   Replaces any special characters (anything that's not alpha-numeric or a
181  *   dash) with an underscore.
182  *
183  *   E.g. "foo$bar&" would become "foo_bar_".
184  *
185  * PARAMETERS
186  *   `buffer'      String to be handled.
187  *   `buffer_size' Length of the string. The function returns after
188  *                 encountering a null-byte or reading this many bytes.
189  */
190 void replace_special (char *buffer, size_t buffer_size);
192 int strsubstitute (char *str, char c_from, char c_to);
194 /*
195  * NAME
196  *   strunescape
197  *
198  * DESCRIPTION
199  *   Replaces any escaped characters in a string with the appropriate special
200  *   characters. The following escaped characters are recognized:
201  *
202  *     \t -> <tab>
203  *     \n -> <newline>
204  *     \r -> <carriage return>
205  *
206  *   For all other escacped characters only the backslash will be removed.
207  *
208  * PARAMETERS
209  *   `buf'         String to be unescaped.
210  *   `buf_len'     Length of the string, including the terminating null-byte.
211  *
212  * RETURN VALUE
213  *   Returns zero upon success, a value less than zero else.
214  */
215 int strunescape (char *buf, size_t buf_len);
217 /**
218  * Removed trailing newline characters (CR and LF) from buffer, which must be
219  * null terminated. Returns the length of the resulting string.
220  */
221 __attribute__((nonnull (1)))
222 size_t strstripnewline (char *buffer);
224 /*
225  * NAME
226  *   timeval_cmp
227  *
228  * DESCRIPTION
229  *   Compare the two time values `tv0' and `tv1' and store the absolut value
230  *   of the difference in the time value pointed to by `delta' if it does not
231  *   equal NULL.
232  *
233  * RETURN VALUE
234  *   Returns an integer less than, equal to, or greater than zero if `tv0' is
235  *   less than, equal to, or greater than `tv1' respectively.
236  */
237 int timeval_cmp (struct timeval tv0, struct timeval tv1, struct timeval *delta);
239 /* make sure tv_usec stores less than a second */
240 #define NORMALIZE_TIMEVAL(tv) \
241         do { \
242                 (tv).tv_sec += (tv).tv_usec / 1000000; \
243                 (tv).tv_usec = (tv).tv_usec % 1000000; \
244         } while (0)
246 /* make sure tv_sec stores less than a second */
247 #define NORMALIZE_TIMESPEC(tv) \
248         do { \
249                 (tv).tv_sec += (tv).tv_nsec / 1000000000; \
250                 (tv).tv_nsec = (tv).tv_nsec % 1000000000; \
251         } while (0)
253 int check_create_dir (const char *file_orig);
255 #ifdef HAVE_LIBKSTAT
256 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name);
257 long long get_kstat_value (kstat_t *ksp, char *name);
258 #endif
260 #ifndef HAVE_HTONLL
261 unsigned long long ntohll (unsigned long long n);
262 unsigned long long htonll (unsigned long long n);
263 #endif
265 #if FP_LAYOUT_NEED_NOTHING
266 # define ntohd(d) (d)
267 # define htond(d) (d)
268 #elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP
269 double ntohd (double d);
270 double htond (double d);
271 #else
272 # error "Don't know how to convert between host and network representation of doubles."
273 #endif
275 int format_name (char *ret, int ret_len,
276                 const char *hostname,
277                 const char *plugin, const char *plugin_instance,
278                 const char *type, const char *type_instance);
279 #define FORMAT_VL(ret, ret_len, vl) \
280         format_name (ret, ret_len, (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
281                         (vl)->type, (vl)->type_instance)
282 int format_values (char *ret, size_t ret_len,
283                 const data_set_t *ds, const value_list_t *vl,
284                 _Bool store_rates);
286 int parse_identifier (char *str, char **ret_host,
287                 char **ret_plugin, char **ret_plugin_instance,
288                 char **ret_type, char **ret_type_instance);
289 int parse_identifier_vl (const char *str, value_list_t *vl);
290 int parse_value (const char *value, value_t *ret_value, int ds_type);
291 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds);
293 #if !HAVE_GETPWNAM_R
294 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
295                 size_t buflen, struct passwd **pwbufp);
296 #endif
298 int notification_init (notification_t *n, int severity, const char *message,
299                 const char *host,
300                 const char *plugin, const char *plugin_instance,
301                 const char *type, const char *type_instance);
302 #define NOTIFICATION_INIT_VL(n, vl) \
303         notification_init (n, NOTIF_FAILURE, NULL, \
304                         (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
305                         (vl)->type, (vl)->type_instance)
307 typedef int (*dirwalk_callback_f)(const char *dirname, const char *filename,
308                 void *user_data);
309 int walk_directory (const char *dir, dirwalk_callback_f callback,
310                 void *user_data, int hidden);
311 /* Returns the number of bytes read or negative on error. */
312 ssize_t read_file_contents (char const *filename, char *buf, size_t bufsize);
314 counter_t counter_diff (counter_t old_value, counter_t new_value);
316 /* Convert a rate back to a value_t. When converting to a derive_t, counter_t
317  * or absoltue_t, take fractional residuals into account. This is important
318  * when scaling counters, for example.
319  * Returns zero on success. Returns EAGAIN when called for the first time; in
320  * this case the value_t is invalid and the next call should succeed. Other
321  * return values indicate an error. */
322 int rate_to_value (value_t *ret_value, gauge_t rate,
323                 rate_to_value_state_t *state, int ds_type, cdtime_t t);
325 /* Converts a service name (a string) to a port number
326  * (in the range [1-65535]). Returns less than zero on error. */
327 int service_name_to_port_number (const char *service_name);
329 /** Parse a string to a derive_t value. Returns zero on success or non-zero on
330  * failure. If failure is returned, ret_value is not touched. */
331 int strtoderive (const char *string, derive_t *ret_value);
333 int strarray_add (char ***ret_array, size_t *ret_array_len, char const *str);
334 void strarray_free (char **array, size_t array_len);
336 #endif /* COMMON_H */