interface, memory, ping plugins: Update copyright information.
[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 int ssnprintf (char *dest, size_t n, const char *format, ...);
45 char *sstrdup(const char *s);
46 void *smalloc(size_t size);
47 char *sstrerror (int errnum, char *buf, size_t buflen);
48
49 /*
50  * NAME
51  *   sread
52  *
53  * DESCRIPTION
54  *   Reads exactly `n' bytes or fails. Syntax and other behavior is analogous
55  *   to `read(2)'. If EOF is received the file descriptor is closed and an
56  *   error is returned.
57  *
58  * PARAMETERS
59  *   `fd'          File descriptor to write to.
60  *   `buf'         Buffer that is to be written.
61  *   `count'       Number of bytes in the buffer.
62  *
63  * RETURN VALUE
64  *   Zero upon success or non-zero if an error occurred. `errno' is set in this
65  *   case.
66  */
67 ssize_t sread (int fd, void *buf, size_t count);
68
69 /*
70  * NAME
71  *   swrite
72  *
73  * DESCRIPTION
74  *   Writes exactly `n' bytes or fails. Syntax and other behavior is analogous
75  *   to `write(2)'.
76  *
77  * PARAMETERS
78  *   `fd'          File descriptor to write to.
79  *   `buf'         Buffer that is to be written.
80  *   `count'       Number of bytes in the buffer.
81  *
82  * RETURN VALUE
83  *   Zero upon success or non-zero if an error occurred. `errno' is set in this
84  *   case.
85  */
86 ssize_t swrite (int fd, const void *buf, size_t count);
87
88 /*
89  * NAME
90  *   strsplit
91  *
92  * DESCRIPTION
93  *   Splits a string into parts and stores pointers to the parts in `fields'.
94  *   The characters split at are ` ' (space) and "\t" (tab).
95  *
96  * PARAMETERS
97  *   `string'      String to split. This string will be modified. `fields' will
98  *                 contain pointers to parts of this string, so free'ing it
99  *                 will destroy `fields' as well.
100  *   `fields'      Array of strings where pointers to the parts will be stored.
101  *   `size'        Number of elements in the array. No more than `size'
102  *                 pointers will be stored in `fields'.
103  *
104  * RETURN VALUE
105  *    Returns the number of parts stored in `fields'.
106  */
107 int strsplit (char *string, char **fields, size_t size);
108
109 /*
110  * NAME
111  *   strjoin
112  *
113  * DESCRIPTION
114  *   Joins together several parts of a string using `sep' as a separator. This
115  *   is equivalent to the Perl built-in `join'.
116  *
117  * PARAMETERS
118  *   `dst'         Buffer where the result is stored.
119  *   `dst_len'     Length of the destination buffer. No more than this many
120  *                 bytes will be written to the memory pointed to by `dst',
121  *                 including the trailing null-byte.
122  *   `fields'      Array of strings to be joined.
123  *   `fields_num'  Number of elements in the `fields' array.
124  *   `sep'         String to be inserted between any two elements of `fields'.
125  *                 This string is neither prepended nor appended to the result.
126  *                 Instead of passing "" (empty string) one can pass NULL.
127  *
128  * RETURN VALUE
129  *   Returns the number of characters in `dst', NOT including the trailing
130  *   null-byte. If an error occurred (empty array or `dst' too small) a value
131  *   smaller than zero will be returned.
132  */
133 int strjoin (char *dst, size_t dst_len, char **fields, size_t fields_num, const char *sep);
134
135 /*
136  * NAME
137  *   escape_slashes
138  *
139  * DESCRIPTION
140  *   Removes slashes from the string `buf' and substitutes them with something
141  *   appropriate. This function should be used whenever a path is to be used as
142  *   (part of) an instance.
143  *
144  * PARAMETERS
145  *   `buf'         String to be escaped.
146  *   `buf_len'     Length of the buffer. No more then this many bytes will be
147  *   written to `buf', including the trailing null-byte.
148  *
149  * RETURN VALUE
150  *   Returns zero upon success and a value smaller than zero upon failure.
151  */
152 int escape_slashes (char *buf, int buf_len);
153
154 int strsubstitute (char *str, char c_from, char c_to);
155
156 /* FIXME: `timeval_sub_timespec' needs a description */
157 int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret);
158
159 int check_create_dir (const char *file_orig);
160
161 #ifdef HAVE_LIBKSTAT
162 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name);
163 long long get_kstat_value (kstat_t *ksp, char *name);
164 #endif
165
166 unsigned long long ntohll (unsigned long long n);
167 unsigned long long htonll (unsigned long long n);
168
169 #if FP_LAYOUT_NEED_NOTHING
170 # define ntohd(d) (d)
171 # define htond(d) (d)
172 #elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP
173 double ntohd (double d);
174 double htond (double d);
175 #else
176 # error "Don't know how to convert between host and network representation of doubles."
177 #endif
178
179 int format_name (char *ret, int ret_len,
180                 const char *hostname,
181                 const char *plugin, const char *plugin_instance,
182                 const char *type, const char *type_instance);
183 #define FORMAT_VL(ret, ret_len, vl, ds) \
184         format_name (ret, ret_len, (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
185                         (ds)->type, (vl)->type_instance)
186
187 int parse_identifier (char *str, char **ret_host,
188                 char **ret_plugin, char **ret_plugin_instance,
189                 char **ret_type, char **ret_type_instance);
190 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds);
191
192 #if !HAVE_GETPWNAM_R
193 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
194                 size_t buflen, struct passwd **pwbufp);
195 #endif
196
197 int notification_init (notification_t *n, int severity, const char *message,
198                 const char *host,
199                 const char *plugin, const char *plugin_instance,
200                 const char *type, const char *type_instance);
201 #define NOTIFICATION_INIT_VL(n, vl, ds) \
202         notification_init (n, NOTIF_FAILURE, NULL, \
203                         (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
204                         (ds)->type, (vl)->type_instance)
205
206 typedef int (*dirwalk_callback_f)(const char *dirname, const char *filename,
207                 void *user_data);
208 int walk_directory (const char *dir, dirwalk_callback_f callback,
209                 void *user_data);
210 int read_file_contents (const char *filename, char *buf, int bufsize);
211
212 #endif /* COMMON_H */