Debian Codegraph @

agent/mibgroup/host/data_access/swrun.c

raw
1 /*
2 * Swrun MIB architecture support
3 *
4 * $Id: swrun.c 15768 2007-01-22 16:18:29Z rstory $
5 */
6 /*
7 * Copyright (C) 2007 Apple, Inc. All rights reserved.
8 * Use is subject to license terms specified in the COPYING file
9 * distributed with the Net-SNMP package.
10 */
11 #include <net-snmp/net-snmp-config.h>
12 #include <net-snmp/net-snmp-features.h>
13 #include <net-snmp/net-snmp-includes.h>
14
15 #include <net-snmp/agent/net-snmp-agent-includes.h>
16 #include <net-snmp/data_access/swrun.h>
17 #include "swrun.h"
18 #include "swrun_private.h"
19
20 #ifdef HAVE_PCRE_H
21 #include <pcre.h>
22 #endif
23
24 netsnmp_feature_child_of(software_running, libnetsnmpmibs);
25
26 netsnmp_feature_child_of(swrun_max_processes, software_running);
27 netsnmp_feature_child_of(swrun_count_processes_by_name, software_running);
28 netsnmp_feature_child_of(swrun_count_processes_by_regex, software_running);
29
30 /**---------------------------------------------------------------------*/
31 /*
32 * local static vars
33 */
34 static int _swrun_init = 0;
35 int _swrun_max = 0;
36 static netsnmp_cache *swrun_cache = NULL;
37 static netsnmp_container *swrun_container = NULL;
38
39 /*
40 * local static prototypes
41 */
42 static void _swrun_entry_release(void* entry, void *unused);
43
44 /**
45 * initialization
46 */
47 void
48 init_swrun(void)
49 {
50 DEBUGMSGTL(("swrun:access", "init\n"));
51
52 if (1 == _swrun_init)
53 return;
54
55 _swrun_init = 1;
56
57 (void)netsnmp_swrun_container();
58 netsnmp_arch_swrun_init();
59 (void) netsnmp_swrun_cache();
60 }
61
62 void
63 shutdown_swrun(void)
64 {
65 DEBUGMSGTL(("swrun:access", "shutdown\n"));
66
67 }
68
69 int
70 swrun_count_processes(int include_kthreads)
71 {
72 netsnmp_swrun_entry *entry;
73 netsnmp_iterator *it;
74 int i = 0;
75
76 netsnmp_cache_check_and_reload(swrun_cache);
77 if ( !swrun_container )
78 return 0; /* or -1 */
79
80 if (include_kthreads)
81 return ( swrun_container ? CONTAINER_SIZE(swrun_container) : 0 );
82
83 it = CONTAINER_ITERATOR( swrun_container );
84 while ((entry = (netsnmp_swrun_entry*)ITERATOR_NEXT( it )) != NULL) {
85 if (4 == entry->hrSWRunType)
86 i++;
87 }
88 ITERATOR_RELEASE( it );
89
90 return i;
91 }
92
93 #ifndef NETSNMP_FEATURE_REMOVE_SWRUN_MAX_PROCESSES
94 int
95 swrun_max_processes( void )
96 {
97 return _swrun_max;
98 }
99 #endif /* NETSNMP_FEATURE_REMOVE_SWRUN_MAX_PROCESSES */
100
101 #ifndef NETSNMP_FEATURE_REMOVE_SWRUN_COUNT_PROCESSES_BY_REGEX
102 #ifdef HAVE_PCRE_H
103 int
104 swrun_count_processes_by_regex( char *name, netsnmp_regex_ptr regexp )
105 {
106 netsnmp_swrun_entry *entry;
107 netsnmp_iterator *it;
108 int i = 0;
109 int found_ndx[30];
110 int found;
111 char fullCommand[64 + 128 + 128 + 3];
112
113 netsnmp_cache_check_and_reload(swrun_cache);
114 if ( !swrun_container || !name || !regexp.regex_ptr )
115 return 0; /* or -1 */
116
117 it = CONTAINER_ITERATOR( swrun_container );
118 while ((entry = (netsnmp_swrun_entry*)ITERATOR_NEXT( it )) != NULL) {
119 /* need to assemble full command back so regexps can get full picture */
120 sprintf(fullCommand, "%s %s", entry->hrSWRunPath, entry->hrSWRunParameters);
121 found = pcre_exec(regexp.regex_ptr, NULL, fullCommand, strlen(fullCommand), 0, 0, found_ndx, 30);
122 if (found > 0) {
123 i++;
124 }
125 }
126 ITERATOR_RELEASE( it );
127
128 return i;
129 }
130 #endif /* HAVE_PCRE_H */
131 #endif /* NETSNMP_FEATURE_REMOVE_SWRUN_COUNT_PROCESSES_BY_REGEX */
132
133 #ifndef NETSNMP_FEATURE_REMOVE_SWRUN_COUNT_PROCESSES_BY_NAME
134 int
135 swrun_count_processes_by_name( char *name )
136 {
137 netsnmp_swrun_entry *entry;
138 netsnmp_iterator *it;
139 int i = 0;
140
141 netsnmp_cache_check_and_reload(swrun_cache);
142 if ( !swrun_container || !name )
143 return 0; /* or -1 */
144
145 it = CONTAINER_ITERATOR( swrun_container );
146 while ((entry = (netsnmp_swrun_entry*)ITERATOR_NEXT( it )) != NULL) {
147 if (0 == strcmp( entry->hrSWRunName, name ))
148 i++;
149 }
150 ITERATOR_RELEASE( it );
151
152 return i;
153 }
154 #endif /* NETSNMP_FEATURE_REMOVE_SWRUN_COUNT_PROCESSES_BY_NAME */
155
156 /**---------------------------------------------------------------------*/
157 /*
158 * cache functions
159 */
160
161 static int
162 _cache_load( netsnmp_cache *cache, void *magic )
163 {
164 netsnmp_swrun_container_load( swrun_container, 0 );
165 return 0;
166 }
167
168 static void
169 _cache_free( netsnmp_cache *cache, void *magic )
170 {
171 netsnmp_swrun_container_free_items( swrun_container );
172 return;
173 }
174
175 /**
176 * create swrun cache
177 */
178 netsnmp_cache *
179 netsnmp_swrun_cache(void)
180 {
181 oid hrSWRunTable_oid[] = { 1, 3, 6, 1, 2, 1, 25, 4, 2 };
182 size_t hrSWRunTable_oid_len = OID_LENGTH(hrSWRunTable_oid);
183
184 if ( !swrun_cache ) {
185 swrun_cache = netsnmp_cache_create(30, /* timeout in seconds */
186 _cache_load, _cache_free,
187 hrSWRunTable_oid, hrSWRunTable_oid_len);
188 if (swrun_cache)
189 swrun_cache->flags = NETSNMP_CACHE_DONT_INVALIDATE_ON_SET;
190 }
191 return swrun_cache;
192 }
193
194
195 /**---------------------------------------------------------------------*/
196 /*
197 * container functions
198 */
199 /**
200 * create swrun container
201 */
202 netsnmp_container *
203 netsnmp_swrun_container(void)
204 {
205 DEBUGMSGTL(("swrun:container", "init\n"));
206
207 /*
208 * create the container.
209 */
210 if (!swrun_container) {
211 swrun_container = netsnmp_container_find("swrun:table_container");
212 if (NULL == swrun_container)
213 return NULL;
214
215 swrun_container->container_name = strdup("swrun container");
216 }
217
218 return swrun_container;
219 }
220
221 /**
222 * load swrun information in specified container
223 *
224 * @param container empty container to be filled.
225 * pass NULL to have the function create one.
226 * @param load_flags flags to modify behaviour. Examples:
227 * NETSNMP_SWRUN_ALL_OR_NONE
228 *
229 * @retval NULL error
230 * @retval !NULL pointer to container
231 */
232 netsnmp_container*
233 netsnmp_swrun_container_load(netsnmp_container* user_container, u_int load_flags)
234 {
235 netsnmp_container* container = user_container;
236 int rc;
237
238 DEBUGMSGTL(("swrun:container:load", "load\n"));
239 netsnmp_assert(1 == _swrun_init);
240
241 if (NULL == container)
242 container = netsnmp_swrun_container();
243 if (NULL == container) {
244 snmp_log(LOG_ERR, "no container specified/found for swrun\n");
245 return NULL;
246 }
247
248 rc = netsnmp_arch_swrun_container_load(container, load_flags);
249 if (0 != rc) {
250 if (NULL == user_container) {
251 netsnmp_swrun_container_free(container, NETSNMP_SWRUN_NOFLAGS);
252 container = NULL;
253 }
254 else if (load_flags & NETSNMP_SWRUN_ALL_OR_NONE) {
255 DEBUGMSGTL(("swrun:container:load",
256 " discarding partial results\n"));
257 netsnmp_swrun_container_free_items(container);
258 }
259 }
260
261 return container;
262 }
263
264 void
265 netsnmp_swrun_container_free(netsnmp_container *container, u_int free_flags)
266 {
267 DEBUGMSGTL(("swrun:container", "free\n"));
268
269 if (NULL == container) {
270 snmp_log(LOG_ERR, "invalid container for netsnmp_swrun_container_free\n");
271 return;
272 }
273
274 if(! (free_flags & NETSNMP_SWRUN_DONT_FREE_ITEMS))
275 netsnmp_swrun_container_free_items(container);
276
277 CONTAINER_FREE(container);
278 }
279
280 void
281 netsnmp_swrun_container_free_items(netsnmp_container *container)
282 {
283 DEBUGMSGTL(("swrun:container", "free_items\n"));
284
285 if (NULL == container) {
286 snmp_log(LOG_ERR, "invalid container for netsnmp_swrun_container_free_items\n");
287 return;
288 }
289
290 /*
291 * free all items.
292 */
293 CONTAINER_CLEAR(container, _swrun_entry_release, NULL);
294 }
295
296 /**---------------------------------------------------------------------*/
297 /*
298 * swrun_entry functions
299 */
300 /**
301 */
302 netsnmp_swrun_entry *
303 netsnmp_swrun_entry_get_by_index(netsnmp_container *container, oid index)
304 {
305 netsnmp_index tmp;
306
307 DEBUGMSGTL(("swrun:entry", "by_index\n"));
308 netsnmp_assert(1 == _swrun_init);
309
310 if (NULL == container) {
311 snmp_log(LOG_ERR,
312 "invalid container for netsnmp_swrun_entry_get_by_index\n");
313 return NULL;
314 }
315
316 tmp.len = 1;
317 tmp.oids = &index;
318
319 return (netsnmp_swrun_entry *) CONTAINER_FIND(container, &tmp);
320 }
321
322 /**
323 */
324 netsnmp_swrun_entry *
325 netsnmp_swrun_entry_create(int32_t index)
326 {
327 netsnmp_swrun_entry *entry =
328 SNMP_MALLOC_TYPEDEF(netsnmp_swrun_entry);
329
330 if(NULL == entry)
331 return NULL;
332
333 entry->hrSWRunIndex = index;
334 entry->hrSWRunType = 1; /* unknown */
335 entry->hrSWRunStatus = 2; /* runnable */
336
337 entry->oid_index.len = 1;
338 entry->oid_index.oids = (oid *) & entry->hrSWRunIndex;
339
340 return entry;
341 }
342
343 /**
344 */
345 NETSNMP_INLINE void
346 netsnmp_swrun_entry_free(netsnmp_swrun_entry * entry)
347 {
348 if (NULL == entry)
349 return;
350
351 /*
352 * SNMP_FREE not needed, for any of these,
353 * since the whole entry is about to be freed
354 */
355 free(entry);
356 }
357
358 /**---------------------------------------------------------------------*/
359 /*
360 * Utility routines
361 */
362
363 /**
364 */
365 static void
366 _swrun_entry_release(void *entry, void *context)
367 {
368 netsnmp_swrun_entry_free(entry);
369 }
370
371
372 #ifdef TEST
373 int main(int argc, char *argv[])
374 {
375 const char *tokens = getenv("SNMP_DEBUG");
376
377 netsnmp_container_init_list();
378
379 /** swrun,verbose:swrun,9:swrun,8:swrun,5:swrun */
380 if (tokens)
381 debug_register_tokens(tokens);
382 else
383 debug_register_tokens("swrun");
384 snmp_set_do_debugging(1);
385
386 init_swrun();
387 netsnmp_swrun_container_load(NULL, 0);
388 shutdown_swrun();
389
390 return 0;
391 }
392 #endif