1 /**
2 * collectd - src/common.h
3 * Copyright (C) 2005-2007 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 if((ptr) != NULL) { \
35 free(ptr); \
36 } \
37 (ptr) = NULL
39 #define STATIC_ARRAY_SIZE(a) (sizeof (a) / sizeof (*(a)))
41 void sstrncpy(char *d, const char *s, int len);
42 char *sstrdup(const char *s);
43 void *smalloc(size_t size);
44 char *sstrerror (int errnum, char *buf, size_t buflen);
46 /*
47 * NAME
48 * sread
49 *
50 * DESCRIPTION
51 * Reads exactly `n' bytes or failes. Syntax and other behavior is analogous
52 * to `read(2)'. If EOF is received the file descriptor is closed and an
53 * error is returned.
54 *
55 * PARAMETERS
56 * `fd' File descriptor to write to.
57 * `buf' Buffer that is to be written.
58 * `count' Numver of bytes in the buffer.
59 *
60 * RETURN VALUE
61 * Zero upon success or non-zero if an error occured. `errno' is set in this
62 * case.
63 */
64 ssize_t sread (int fd, void *buf, size_t count);
66 /*
67 * NAME
68 * swrite
69 *
70 * DESCRIPTION
71 * Writes exactly `n' bytes or failes. Syntax and other behavior is analogous
72 * to `write(2)'.
73 *
74 * PARAMETERS
75 * `fd' File descriptor to write to.
76 * `buf' Buffer that is to be written.
77 * `count' Numver of bytes in the buffer.
78 *
79 * RETURN VALUE
80 * Zero upon success or non-zero if an error occured. `errno' is set in this
81 * case.
82 */
83 ssize_t swrite (int fd, const void *buf, size_t count);
85 /*
86 * NAME
87 * strsplit
88 *
89 * DESCRIPTION
90 * Splits a string into parts and stores pointers to the parts in `fields'.
91 * The characters split at are ` ' (space) and "\t" (tab).
92 *
93 * PARAMETERS
94 * `string' String to split. This string will be modified. `fields' will
95 * contain pointers to parts of this string, so free'ing it
96 * will destroy `fields' as well.
97 * `fields' Array of strings where pointers to the parts will be stored.
98 * `size' Number of elements in the array. No more than `size'
99 * pointers will be stored in `fields'.
100 *
101 * RETURN VALUE
102 * Returns the number of parts stored in `fields'.
103 */
104 int strsplit (char *string, char **fields, size_t size);
106 /*
107 * NAME
108 * strjoin
109 *
110 * DESCRIPTION
111 * Joins together several parts of a string using `sep' as a seperator. This
112 * is equipollent to the perl buildin `join'.
113 *
114 * PARAMETERS
115 * `dst' Buffer where the result is stored.
116 * `dst_len' Length of the destination buffer. No more than this many
117 * bytes will be written to the memory pointed to by `dst',
118 * including the trailing null-byte.
119 * `fields' Array of strings to be joined.
120 * `fields_num' Number of elements in the `fields' array.
121 * `sep' String to be inserted between any two elements of `fields'.
122 * This string is neither prepended nor appended to the result.
123 * Instead of passing "" (empty string) one can pass NULL.
124 *
125 * RETURN VALUE
126 * Returns the number of characters in `dst', NOT including the trailing
127 * null-byte. If an error occured (empty array or `dst' too small) a value
128 * smaller than zero will be returned.
129 */
130 int strjoin (char *dst, size_t dst_len, char **fields, size_t fields_num, const char *sep);
132 /*
133 * NAME
134 * escape_slashes
135 *
136 * DESCRIPTION
137 * Removes slashes from the string `buf' and substitutes them with something
138 * appropriate. This function should be used whenever a path is to be used as
139 * (part of) an instance.
140 *
141 * PARAMETERS
142 * `buf' String to be escaped.
143 * `buf_len' Length of the buffer. No more then this many bytes will be
144 * written to `buf', including the trailing null-byte.
145 *
146 * RETURN VALUE
147 * Returns zero upon success and a value smaller than zero upon failure.
148 */
149 int escape_slashes (char *buf, int buf_len);
151 int strsubstitute (char *str, char c_from, char c_to);
153 /* FIXME: `timeval_sub_timespec' needs a description */
154 int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret);
156 int check_create_dir (const char *file_orig);
158 #ifdef HAVE_LIBKSTAT
159 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name);
160 long long get_kstat_value (kstat_t *ksp, char *name);
161 #endif
163 unsigned long long ntohll (unsigned long long n);
164 unsigned long long htonll (unsigned long long n);
166 int format_name (char *ret, int ret_len,
167 const char *hostname,
168 const char *plugin, const char *plugin_instance,
169 const char *type, const char *type_instance);
170 #define FORMAT_VL(ret, ret_len, vl, ds) \
171 format_name (ret, ret_len, (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
172 (ds)->type, (vl)->type_instance)
174 int parse_identifier (char *str, char **ret_host,
175 char **ret_plugin, char **ret_plugin_instance,
176 char **ret_type, char **ret_type_instance);
177 int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds);
179 #if !HAVE_GETPWNAM_R
180 int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf,
181 size_t buflen, struct passwd **pwbufp);
182 #endif
184 #endif /* COMMON_H */