1 /**
2 * collectd - src/common.h
3 * Copyright (C) 2005-2009 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 **/
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 char *sstrncpy (char *dest, const char *src, size_t n);
51 int ssnprintf (char *dest, size_t n, const char *format, ...);
52 char *sstrdup(const char *s);
53 void *smalloc(size_t size);
54 char *sstrerror (int errnum, char *buf, size_t buflen);
56 /*
57 * NAME
58 * sread
59 *
60 * DESCRIPTION
61 * Reads exactly `n' bytes or fails. Syntax and other behavior is analogous
62 * to `read(2)'. If EOF is received the file descriptor is closed and an
63 * error is returned.
64 *
65 * PARAMETERS
66 * `fd' File descriptor to write to.
67 * `buf' Buffer that is to be written.
68 * `count' Number of bytes in the buffer.
69 *
70 * RETURN VALUE
71 * Zero upon success or non-zero if an error occurred. `errno' is set in this
72 * case.
73 */
74 ssize_t sread (int fd, void *buf, size_t count);
76 /*
77 * NAME
78 * swrite
79 *
80 * DESCRIPTION
81 * Writes exactly `n' bytes or fails. Syntax and other behavior is analogous
82 * to `write(2)'.
83 *
84 * PARAMETERS
85 * `fd' File descriptor to write to.
86 * `buf' Buffer that is to be written.
87 * `count' Number of bytes in the buffer.
88 *
89 * RETURN VALUE
90 * Zero upon success or non-zero if an error occurred. `errno' is set in this
91 * case.
92 */
93 ssize_t swrite (int fd, const void *buf, size_t count);
95 /*
96 * NAME
97 * strsplit
98 *
99 * DESCRIPTION
100 * Splits a string into parts and stores pointers to the parts in `fields'.
101 * The characters split at are: " ", "\t", "\r", and "\n".
102 *
103 * PARAMETERS
104 * `string' String to split. This string will be modified. `fields' will
105 * contain pointers to parts of this string, so free'ing it
106 * will destroy `fields' as well.
107 * `fields' Array of strings where pointers to the parts will be stored.
108 * `size' Number of elements in the array. No more than `size'
109 * pointers will be stored in `fields'.
110 *
111 * RETURN VALUE
112 * Returns the number of parts stored in `fields'.
113 */
114 int strsplit (char *string, char **fields, size_t size);
116 /*
117 * NAME
118 * strjoin
119 *
120 * DESCRIPTION
121 * Joins together several parts of a string using `sep' as a separator. This
122 * is equivalent to the Perl built-in `join'.
123 *
124 * PARAMETERS
125 * `dst' Buffer where the result is stored.
126 * `dst_len' Length of the destination buffer. No more than this many
127 * bytes will be written to the memory pointed to by `dst',
128 * including the trailing null-byte.
129 * `fields' Array of strings to be joined.
130 * `fields_num' Number of elements in the `fields' array.
131 * `sep' String to be inserted between any two elements of `fields'.
132 * This string is neither prepended nor appended to the result.
133 * Instead of passing "" (empty string) one can pass NULL.
134 *
135 * RETURN VALUE
136 * Returns the number of characters in `dst', NOT including the trailing
137 * null-byte. If an error occurred (empty array or `dst' too small) a value
138 * smaller than zero will be returned.
139 */
140 int strjoin (char *dst, size_t dst_len, char **fields, size_t fields_num, const char *sep);
142 /*
143 * NAME
144 * escape_slashes
145 *
146 * DESCRIPTION
147 * Removes slashes from the string `buf' and substitutes them with something
148 * appropriate. This function should be used whenever a path is to be used as
149 * (part of) an instance.
150 *
151 * PARAMETERS
152 * `buf' String to be escaped.
153 * `buf_len' Length of the buffer. No more then this many bytes will be
154 * written to `buf', including the trailing null-byte.
155 *
156 * RETURN VALUE
157 * Returns zero upon success and a value smaller than zero upon failure.
158 */
159 int escape_slashes (char *buf, int buf_len);
161 /*
162 * NAME
163 * replace_special
164 *
165 * DESCRIPTION
166 * Replaces any special characters (anything that's not alpha-numeric or a
167 * dash) with an underscore.
168 *
169 * E.g. "foo$bar&" would become "foo_bar_".
170 *
171 * PARAMETERS
172 * `buffer' String to be handled.
173 * `buffer_size' Length of the string. The function returns after
174 * encountering a null-byte or reading this many bytes.
175 */
176 void replace_special (char *buffer, size_t buffer_size);
178 int strsubstitute (char *str, char c_from, char c_to);
180 /*
181 * NAME
182 * strunescape
183 *
184 * DESCRIPTION
185 * Replaces any escaped characters in a string with the appropriate special
186 * characters. The following escaped characters are recognized:
187 *
188 * \t -> <tab>
189 * \n -> <newline>
190 * \r -> <carriage return>
191 *
192 * For all other escacped characters only the backslash will be removed.
193 *
194 * PARAMETERS
195 * `buf' String to be unescaped.
196 * `buf_len' Length of the string, including the terminating null-byte.
197 *
198 * RETURN VALUE
199 * Returns zero upon success, a value less than zero else.
200 */
201 int strunescape (char *buf, size_t buf_len);
203 /*
204 * NAME
205 * timeval_cmp
206 *
207 * DESCRIPTION
208 * Compare the two time values `tv0' and `tv1' and store the absolut value
209 * of the difference in the time value pointed to by `delta' if it does not
210 * equal NULL.
211 *
212 * RETURN VALUE
213 * Returns an integer less than, equal to, or greater than zero if `tv0' is
214 * less than, equal to, or greater than `tv1' respectively.
215 */
216 int timeval_cmp (struct timeval tv0, struct timeval tv1, struct timeval *delta);
218 /* make sure tv_usec stores less than a second */
219 #define NORMALIZE_TIMEVAL(tv) \
220 do { \
221 (tv).tv_sec += (tv).tv_usec / 1000000; \
222 (tv).tv_usec = (tv).tv_usec % 1000000; \
223 } while (0)
225 /* make sure tv_sec stores less than a second */
226 #define NORMALIZE_TIMESPEC(tv) \
227 do { \
228 (tv).tv_sec += (tv).tv_nsec / 1000000000; \
229 (tv).tv_nsec = (tv).tv_nsec % 1000000000; \
230 } while (0)
232 int check_create_dir (const char *file_orig);
234 #ifdef HAVE_LIBKSTAT
235 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name);
236 long long get_kstat_value (kstat_t *ksp, char *name);
237 #endif
239 #ifndef HAVE_HTONLL
240 unsigned long long ntohll (unsigned long long n);
241 unsigned long long htonll (unsigned long long n);
242 #endif
244 #if FP_LAYOUT_NEED_NOTHING
245 # define ntohd(d) (d)
246 # define htond(d) (d)
247 #elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP
248 double ntohd (double d);
249 double htond (double d);
250 #else
251 # error "Don't know how to convert between host and network representation of doubles."
252 #endif
254 int format_name (char *ret, int ret_len,
255 const char *hostname,
256 const char *plugin, const char *plugin_instance,
257 const char *type, const char *type_instance);
258 #define FORMAT_VL(ret, ret_len, vl) \
259 format_name (ret, ret_len, (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
260 (vl)->type, (vl)->type_instance)
262 int parse_identifier (char *str, char **ret_host,
263 char **ret_plugin, char **ret_plugin_instance,
264 char **ret_type, char **ret_type_instance);
265 int parse_value (const char *value, value_t *ret_value, int ds_type);
266 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds);
268 #if !HAVE_GETPWNAM_R
269 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
270 size_t buflen, struct passwd **pwbufp);
271 #endif
273 int notification_init (notification_t *n, int severity, const char *message,
274 const char *host,
275 const char *plugin, const char *plugin_instance,
276 const char *type, const char *type_instance);
277 #define NOTIFICATION_INIT_VL(n, vl, ds) \
278 notification_init (n, NOTIF_FAILURE, NULL, \
279 (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
280 (ds)->type, (vl)->type_instance)
282 typedef int (*dirwalk_callback_f)(const char *dirname, const char *filename,
283 void *user_data);
284 int walk_directory (const char *dir, dirwalk_callback_f callback,
285 void *user_data, int hidden);
286 /* Returns the number of bytes read or negative on error. */
287 int read_file_contents (const char *filename, char *buf, int bufsize);
289 counter_t counter_diff (counter_t old_value, counter_t new_value);
291 /* Converts a service name (a string) to a port number
292 * (in the range [1-65535]). Returns less than zero on error. */
293 int service_name_to_port_number (const char *service_name);
295 /** Parse a string to a derive_t value. Returns zero on success or non-zero on
296 * failure. If failure is returned, ret_value is not touched. */
297 int strtoderive (const char *string, derive_t *ret_value);
299 #endif /* COMMON_H */