Debian Codegraph @

agent/mibgroup/host/data_access/swinst.c

raw
1 /*
2 * swinst.c : hrSWInstalledTable data access
3 */
4 /*
5 * Copyright (C) 2007 Apple, Inc. All rights reserved.
6 * Use is subject to license terms specified in the COPYING file
7 * distributed with the Net-SNMP package.
8 */
9 #include <net-snmp/net-snmp-config.h>
10 #include <net-snmp/net-snmp-features.h>
11 #include <net-snmp/net-snmp-includes.h>
12 #include <net-snmp/agent/net-snmp-agent-includes.h>
13 #include <net-snmp/data_access/swinst.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include "swinst.h"
17 #include "swinst_private.h"
18
19 netsnmp_feature_child_of(software_installed, libnetsnmpmibs);
20
21 netsnmp_feature_child_of(swinst_entry_remove, netsnmp_unused);
22
23 /* ---------------------------------------------------------------------
24 */
25
26 static void netsnmp_swinst_entry_free_cb(void *, void *);
27
28 void init_swinst( void )
29 {
30 static int initialized = 0;
31
32 DEBUGMSGTL(("swinst", "init called\n"));
33
34 if (initialized)
35 return; /* already initialized */
36
37
38 /*
39 * call arch init code
40 */
41 netsnmp_swinst_arch_init();
42 }
43
44 void shutdown_swinst( void )
45 {
46 DEBUGMSGTL(("swinst", "shutdown called\n"));
47
48 netsnmp_swinst_arch_shutdown();
49 }
50
51 /* ---------------------------------------------------------------------
52 */
53
54 /*
55 * load a container with installed software. If user_container is NULL,
56 * a new container will be allocated and returned, and the caller
57 * is responsible for releasing the allocated memory when done.
58 *
59 * if flags contains NETSNMP_SWINST_ALL_OR_NONE and any error occurs,
60 * the container will be completely cleared.
61 */
62 netsnmp_container *
63 netsnmp_swinst_container_load( netsnmp_container *user_container, int flags )
64 {
65 netsnmp_container *container = user_container;
66 int arch_rc;
67
68 DEBUGMSGTL(("swinst:container", "load\n"));
69
70 /*
71 * create the container, if needed
72 */
73 if (NULL == container) {
74 container = netsnmp_container_find("swinst:table_container");
75 if (NULL == container)
76 return NULL;
77 }
78 if (NULL == container->container_name)
79 container->container_name = strdup("swinst container");
80
81 /*
82 * call the arch specific code to load the container
83 */
84 arch_rc = netsnmp_swinst_arch_load( container, flags );
85 if (arch_rc && (flags & NETSNMP_SWINST_ALL_OR_NONE)) {
86 /*
87 * caller does not want a partial load, so empty the container.
88 * if we created the container, destroy it.
89 */
90 netsnmp_swinst_container_free_items(container);
91 if (container != user_container) {
92 netsnmp_swinst_container_free(container, flags);
93 }
94 }
95
96 return container;
97 }
98
99 void
100 netsnmp_swinst_container_free(netsnmp_container *container, u_int free_flags)
101 {
102 DEBUGMSGTL(("swinst:container", "free\n"));
103
104 if (NULL == container) {
105 snmp_log(LOG_ERR,
106 "invalid container for netsnmp_swinst_container_free\n");
107 return;
108 }
109
110 if(! (free_flags & NETSNMP_SWINST_DONT_FREE_ITEMS))
111 netsnmp_swinst_container_free_items(container);
112
113 CONTAINER_FREE(container);
114 }
115
116 /*
117 * free a swinst container
118 */
119 void netsnmp_swinst_container_free_items(netsnmp_container *container)
120 {
121 DEBUGMSGTL(("swinst:container", "free_items\n"));
122
123 if (NULL == container) {
124 snmp_log(LOG_ERR,
125 "invalid container for netsnmp_swinst_container_free_items\n");
126 return;
127 }
128
129 /*
130 * free all items.
131 */
132 CONTAINER_CLEAR(container, netsnmp_swinst_entry_free_cb, NULL);
133 }
134
135
136 /* ---------------------------------------------------------------------
137 */
138
139 /*
140 * create a new row in the table
141 */
142 netsnmp_swinst_entry *
143 netsnmp_swinst_entry_create(int32_t swIndex)
144 {
145 netsnmp_swinst_entry *entry;
146
147 entry = SNMP_MALLOC_TYPEDEF(netsnmp_swinst_entry);
148 if (!entry)
149 return NULL;
150
151 entry->swIndex = swIndex;
152 entry->oid_index.len = 1;
153 entry->oid_index.oids = &entry->swIndex;
154
155 entry->swType = HRSWINSTALLEDTYPE_APPLICATION;
156
157 return entry;
158 }
159
160 /*
161 * free a row
162 */
163 void
164 netsnmp_swinst_entry_free(netsnmp_swinst_entry *entry)
165 {
166 DEBUGMSGTL(("swinst:entry:free", "index %" NETSNMP_PRIo "u\n",
167 entry->swIndex));
168
169 free(entry);
170 }
171
172 /*
173 * free a row
174 */
175 static void
176 netsnmp_swinst_entry_free_cb(void *entry, void *context)
177 {
178 free(entry);
179 }
180
181 /*
182 * remove a row from the table
183 */
184 #ifndef NETSNMP_FEATURE_REMOVE_SWINST_ENTRY_REMOVE
185 void
186 netsnmp_swinst_entry_remove(netsnmp_container * container,
187 netsnmp_swinst_entry *entry)
188 {
189 DEBUGMSGTL(("swinst:container", "remove\n"));
190 if (!entry)
191 return; /* Nothing to remove */
192 CONTAINER_REMOVE(container, entry);
193 }
194 #endif /* NETSNMP_FEATURE_REMOVE_SWINST_ENTRY_REMOVE */
195
196 /* ---------------------------------------------------------------------
197 */
198
199 #ifdef TEST
200 int main(int argc, char *argv[])
201 {
202 const char *tokens = getenv("SNMP_DEBUG");
203
204 netsnmp_container_init_list();
205
206 /** swinst,verbose:swinst */
207 if (tokens)
208 debug_register_tokens(tokens);
209 else
210 debug_register_tokens("swinst");
211 snmp_set_do_debugging(1);
212
213 init_swinst();
214 netsnmp_swinst_container_load(NULL, 0);
215 shutdown_swinst();
216
217 return 0;
218 }
219 #endif