xref: /6.6.0/sigar/include/sigar_private.h (revision 829a4c03)
1/*
2 * Copyright (c) 2004-2008 Hyperic, Inc.
3 * Copyright (c) 2009 SpringSource, Inc.
4 * Copyright (c) 2009-2010 VMware, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#ifndef SIGAR_PRIVATE_DOT_H
20#define SIGAR_PRIVATE_DOT_H
21
22#include "sigar_log.h"
23#include "sigar_ptql.h"
24
25#include <stdlib.h>
26#include <string.h>
27#include <ctype.h>
28
29#ifndef WIN32
30#include <unistd.h>
31#include <stddef.h>
32#ifndef DARWIN
33#include <strings.h>
34#endif
35#endif
36
37#ifdef DMALLOC
38#define _MEMORY_H /* exclude memory.h on solaris */
39#define DMALLOC_FUNC_CHECK
40#include <dmalloc.h>
41#endif
42
43/* common to all os sigar_t's */
44/* XXX: this is ugly; but don't want the same stuffs
45 * duplicated on 4 platforms and am too lazy to change
46 * sigar_t to the way it was originally where sigar_t was
47 * common and contained a sigar_os_t.
48 * feel free trav ;-)
49 */
50#define SIGAR_T_BASE \
51   int cpu_list_cores; \
52   int log_level; \
53   void *log_data; \
54   sigar_log_impl_t log_impl; \
55   void *ptql_re_data; \
56   sigar_ptql_re_impl_t ptql_re_impl; \
57   unsigned int ncpu; \
58   unsigned long version; \
59   unsigned long boot_time; \
60   int ticks; \
61   sigar_pid_t pid; \
62   char errbuf[256]; \
63   char *ifconf_buf; \
64   int ifconf_len; \
65   char *self_path; \
66   sigar_proc_list_t *pids; \
67   sigar_cache_t *fsdev; \
68   sigar_cache_t *proc_cpu; \
69   sigar_cache_t *net_listen; \
70   sigar_cache_t *net_services_tcp; \
71   sigar_cache_t *net_services_udp
72
73#ifdef DMALLOC
74/* linux has its own strdup macro, make sure we use dmalloc's */
75#define sigar_strdup(s) \
76    dmalloc_strndup(__FILE__, __LINE__, (s), -1, 0)
77#else
78#  ifdef WIN32
79#    define sigar_strdup(s) _strdup(s)
80#  else
81#    define sigar_strdup(s) strdup(s)
82#  endif
83#endif
84
85#define SIGAR_ZERO(s) \
86    memset(s, '\0', sizeof(*(s)))
87
88#define SIGAR_STRNCPY(dest, src, len) \
89    strncpy(dest, src, len); \
90    dest[len-1] = '\0'
91
92/* we use fixed size buffers pretty much everywhere */
93/* this is strncpy + ensured \0 terminator */
94#define SIGAR_SSTRCPY(dest, src) \
95    SIGAR_STRNCPY(dest, src, sizeof(dest))
96
97#ifndef strEQ
98#define strEQ(s1, s2) (strcmp(s1, s2) == 0)
99#endif
100
101#ifndef strnEQ
102#define strnEQ(s1, s2, n) (strncmp(s1, s2, n) == 0)
103#endif
104
105#ifdef WIN32
106#define strcasecmp stricmp
107#define strncasecmp strnicmp
108#endif
109
110#ifndef strcaseEQ
111#define strcaseEQ(s1, s2) (strcasecmp(s1, s2) == 0)
112#endif
113
114#ifndef strncaseEQ
115#define strncaseEQ(s1, s2, n) (strncasecmp(s1, s2, n) == 0)
116#endif
117
118#ifdef offsetof
119#define sigar_offsetof offsetof
120#else
121#define sigar_offsetof(type, field) ((size_t)(&((type *)0)->field))
122#endif
123
124#define SIGAR_MSEC 1000L
125#define SIGAR_USEC 1000000L
126#define SIGAR_NSEC 1000000000L
127
128#define SIGAR_SEC2NANO(s) \
129    ((sigar_uint64_t)(s) * (sigar_uint64_t)SIGAR_NSEC)
130
131/* cpu ticks to milliseconds */
132#define SIGAR_TICK2MSEC(s) \
133   ((sigar_uint64_t)(s) * ((sigar_uint64_t)SIGAR_MSEC / (double)sigar->ticks))
134
135#define SIGAR_TICK2NSEC(s) \
136   ((sigar_uint64_t)(s) * ((sigar_uint64_t)SIGAR_NSEC / (double)sigar->ticks))
137
138/* nanoseconds to milliseconds */
139#define SIGAR_NSEC2MSEC(s) \
140   ((sigar_uint64_t)(s) / ((sigar_uint64_t)1000000L))
141
142#define IFTYPE_LO  2
143#define IFTYPE_ETH 3
144
145#define SIGAR_LAST_PROC_EXPIRE 2
146
147#define SIGAR_FS_MAX 10
148
149#define SIGAR_CPU_INFO_MAX 4
150
151#define SIGAR_CPU_LIST_MAX 4
152
153#define SIGAR_PROC_LIST_MAX 256
154
155#define SIGAR_PROC_ARGS_MAX 12
156
157#define SIGAR_NET_ROUTE_LIST_MAX 6
158
159#define SIGAR_NET_IFLIST_MAX 20
160
161#define SIGAR_NET_CONNLIST_MAX 20
162
163#define SIGAR_ARP_LIST_MAX 12
164
165#define SIGAR_WHO_LIST_MAX 12
166
167int sigar_os_open(sigar_t **sigar);
168
169int sigar_os_close(sigar_t *sigar);
170
171char *sigar_os_error_string(sigar_t *sigar, int err);
172
173char *sigar_strerror_get(int err, char *errbuf, int buflen);
174
175void sigar_strerror_set(sigar_t *sigar, char *msg);
176
177void sigar_strerror_printf(sigar_t *sigar, const char *format, ...);
178
179int sigar_sys_info_get_uname(sigar_sys_info_t *sysinfo);
180
181int sigar_os_proc_list_get(sigar_t *sigar,
182                           sigar_proc_list_t *proclist);
183
184int sigar_proc_list_create(sigar_proc_list_t *proclist);
185
186int sigar_proc_list_grow(sigar_proc_list_t *proclist);
187
188#define SIGAR_PROC_LIST_GROW(proclist) \
189    if (proclist->number >= proclist->size) { \
190        sigar_proc_list_grow(proclist); \
191    }
192
193int sigar_proc_args_create(sigar_proc_args_t *proclist);
194
195int sigar_proc_args_grow(sigar_proc_args_t *procargs);
196
197#define SIGAR_PROC_ARGS_GROW(procargs) \
198    if (procargs->number >= procargs->size) { \
199        sigar_proc_args_grow(procargs); \
200    }
201
202int sigar_file_system_list_create(sigar_file_system_list_t *fslist);
203
204int sigar_file_system_list_grow(sigar_file_system_list_t *fslist);
205
206#define SIGAR_FILE_SYSTEM_LIST_GROW(fslist) \
207    if (fslist->number >= fslist->size) { \
208        sigar_file_system_list_grow(fslist); \
209    }
210
211int sigar_os_fs_type_get(sigar_file_system_t *fsp);
212
213/* os plugins that set fsp->type call fs_type_get directly */
214#define sigar_fs_type_init(fsp) \
215   fsp->type = SIGAR_FSTYPE_UNKNOWN; \
216   sigar_fs_type_get(fsp)
217
218void sigar_fs_type_get(sigar_file_system_t *fsp);
219
220int sigar_net_interface_list_create(sigar_net_interface_list_t *iflist);
221
222int sigar_net_interface_list_grow(sigar_net_interface_list_t *iflist);
223
224#define SIGAR_NET_IFLIST_GROW(iflist) \
225    if (iflist->number >= iflist->size) { \
226        sigar_net_interface_list_grow(iflist); \
227    }
228
229int sigar_net_connection_list_create(sigar_net_connection_list_t *connlist);
230
231int sigar_net_connection_list_grow(sigar_net_connection_list_t *connlist);
232
233#define SIGAR_NET_CONNLIST_GROW(connlist) \
234    if (connlist->number >= connlist->size) { \
235        sigar_net_connection_list_grow(connlist); \
236    }
237
238#define sigar_net_address_set(a, val) \
239    (a).addr.in = val; \
240    (a).family = SIGAR_AF_INET
241
242#define sigar_net_address6_set(a, val) \
243    memcpy(&((a).addr.in6), val, sizeof((a).addr.in6)); \
244    (a).family = SIGAR_AF_INET6
245
246#define SIGAR_IFHWADDRLEN 6
247
248#define sigar_net_address_mac_set(a, val, len) \
249    memcpy(&((a).addr.mac), val, len); \
250    (a).family = SIGAR_AF_LINK
251
252#define sigar_hwaddr_set_null(ifconfig) \
253    SIGAR_ZERO(&ifconfig->hwaddr.addr.mac); \
254    ifconfig->hwaddr.family = SIGAR_AF_LINK
255
256int sigar_net_interface_ipv6_config_get(sigar_t *sigar, const char *name,
257                                        sigar_net_interface_config_t *ifconfig);
258
259#define sigar_net_interface_ipv6_config_init(ifconfig) \
260    ifconfig->address6.family = SIGAR_AF_INET6; \
261    ifconfig->prefix6_length = 0; \
262    ifconfig->scope6 = 0
263
264#define SIGAR_SIN6(s) ((struct sockaddr_in6 *)(s))
265
266#define SIGAR_SIN6_ADDR(s) &SIGAR_SIN6(s)->sin6_addr
267
268#define sigar_net_interface_scope6_set(ifconfig, addr) \
269    if (IN6_IS_ADDR_LINKLOCAL(addr)) \
270        ifconfig->scope6 = SIGAR_IPV6_ADDR_LINKLOCAL; \
271    else if (IN6_IS_ADDR_SITELOCAL(addr)) \
272        ifconfig->scope6 = SIGAR_IPV6_ADDR_SITELOCAL; \
273    else if (IN6_IS_ADDR_V4COMPAT(addr)) \
274        ifconfig->scope6 = SIGAR_IPV6_ADDR_COMPATv4; \
275    else if (IN6_IS_ADDR_LOOPBACK(addr)) \
276        ifconfig->scope6 = SIGAR_IPV6_ADDR_LOOPBACK; \
277    else \
278        ifconfig->scope6 = SIGAR_IPV6_ADDR_ANY
279
280int sigar_tcp_curr_estab(sigar_t *sigar, sigar_tcp_t *tcp);
281
282int sigar_user_id_get(sigar_t *sigar, const char *name, int *uid);
283
284int sigar_user_name_get(sigar_t *sigar, int uid, char *buf, int buflen);
285
286int sigar_group_name_get(sigar_t *sigar, int gid, char *buf, int buflen);
287
288#define SIGAR_PROC_ENV_KEY_LOOKUP() \
289    if ((procenv->type == SIGAR_PROC_ENV_KEY) && \
290        (pid == sigar->pid)) \
291    { \
292        char *value = getenv(procenv->key); \
293        if (value != NULL) { \
294            procenv->env_getter(procenv->data, \
295                                procenv->key, \
296                                procenv->klen, \
297                                value, strlen(value)); \
298        } \
299        return SIGAR_OK; \
300    }
301
302#define SIGAR_DISK_STATS_INIT(disk) \
303    (disk)->reads = (disk)->writes = \
304    (disk)->read_bytes = (disk)->write_bytes = \
305    (disk)->rtime = (disk)->wtime = (disk)->qtime = (disk)->time = \
306    (disk)->queue = (disk)->service_time = SIGAR_FIELD_NOTIMPL; \
307    (disk)->snaptime = 0
308
309/* key used for filesystem (/) -> device (/dev/hda1) mapping */
310/* and disk_usage cache for service_time */
311#define SIGAR_FSDEV_ID(sb) \
312    (S_ISBLK((sb).st_mode) ? (sb).st_rdev : ((sb).st_ino + (sb).st_dev))
313
314#if defined(WIN32) || defined(NETWARE)
315int sigar_get_iftype(const char *name, int *type, int *inst);
316#endif
317
318#define SIGAR_NIC_LOOPBACK "Local Loopback"
319#define SIGAR_NIC_UNSPEC   "UNSPEC"
320#define SIGAR_NIC_SLIP     "Serial Line IP"
321#define SIGAR_NIC_CSLIP    "VJ Serial Line IP"
322#define SIGAR_NIC_SLIP6    "6-bit Serial Line IP"
323#define SIGAR_NIC_CSLIP6   "VJ 6-bit Serial Line IP"
324#define SIGAR_NIC_ADAPTIVE "Adaptive Serial Line IP"
325#define SIGAR_NIC_ETHERNET "Ethernet"
326#define SIGAR_NIC_ASH      "Ash"
327#define SIGAR_NIC_FDDI     "Fiber Distributed Data Interface"
328#define SIGAR_NIC_HIPPI    "HIPPI"
329#define SIGAR_NIC_AX25     "AMPR AX.25"
330#define SIGAR_NIC_ROSE     "AMPR ROSE"
331#define SIGAR_NIC_NETROM   "AMPR NET/ROM"
332#define SIGAR_NIC_X25      "generic X.25"
333#define SIGAR_NIC_TUNNEL   "IPIP Tunnel"
334#define SIGAR_NIC_PPP      "Point-to-Point Protocol"
335#define SIGAR_NIC_HDLC     "(Cisco)-HDLC"
336#define SIGAR_NIC_LAPB     "LAPB"
337#define SIGAR_NIC_ARCNET   "ARCnet"
338#define SIGAR_NIC_DLCI     "Frame Relay DLCI"
339#define SIGAR_NIC_FRAD     "Frame Relay Access Device"
340#define SIGAR_NIC_SIT      "IPv6-in-IPv4"
341#define SIGAR_NIC_IRDA     "IrLAP"
342#define SIGAR_NIC_EC       "Econet"
343
344#ifndef WIN32
345#include <netdb.h>
346#endif
347
348#define SIGAR_HOSTENT_LEN 512
349#if defined(_AIX)
350#define SIGAR_HAS_HOSTENT_DATA
351#endif
352
353typedef struct {
354    char buffer[SIGAR_HOSTENT_LEN];
355    int error;
356#ifndef WIN32
357    struct hostent hs;
358#endif
359#ifdef SIGAR_HAS_HOSTENT_DATA
360    struct hostent_data hd;
361#endif
362} sigar_hostent_t;
363
364#endif
365