Deleted Added
full compact
hostres_fs_tbl.c (154137) hostres_fs_tbl.c (160341)
1/*-
2 * Copyright (c) 2005-2006 The FreeBSD Project
3 * All rights reserved.
4 *
5 * Author: Victor Cruceru <soc-victor@freebsd.org>
6 *
7 * Redistribution of this software and documentation and use in source and
8 * binary forms, with or without modification, are permitted provided that

--- 12 unchanged lines hidden (view full) ---

21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
1/*-
2 * Copyright (c) 2005-2006 The FreeBSD Project
3 * All rights reserved.
4 *
5 * Author: Victor Cruceru <soc-victor@freebsd.org>
6 *
7 * Redistribution of this software and documentation and use in source and
8 * binary forms, with or without modification, are permitted provided that

--- 12 unchanged lines hidden (view full) ---

21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_fs_tbl.c 154133 2006-01-09 12:33:45Z harti $
29 * $FreeBSD: head/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_fs_tbl.c 160341 2006-07-14 09:07:56Z harti $
30 */
31
32/*
33 * Host Resources MIB for SNMPd. Implementation for hrFSTable
34 */
35
36#include <sys/types.h>
37#include <sys/param.h>
38#include <sys/sysctl.h>
39#include <sys/mount.h>
40
41#include <assert.h>
42#include <err.h>
43#include <stdlib.h>
44#include <string.h>
45#include <syslog.h>
30 */
31
32/*
33 * Host Resources MIB for SNMPd. Implementation for hrFSTable
34 */
35
36#include <sys/types.h>
37#include <sys/param.h>
38#include <sys/sysctl.h>
39#include <sys/mount.h>
40
41#include <assert.h>
42#include <err.h>
43#include <stdlib.h>
44#include <string.h>
45#include <syslog.h>
46#include <sysexits.h>
46
47#include "hostres_snmp.h"
48#include "hostres_oid.h"
49#include "hostres_tree.h"
50
51/*
52 * File system access enum
53 */
54enum hrFSAccess {
55 FS_READ_WRITE = 1,
56 FS_READ_ONLY = 2
57};
58
47
48#include "hostres_snmp.h"
49#include "hostres_oid.h"
50#include "hostres_tree.h"
51
52/*
53 * File system access enum
54 */
55enum hrFSAccess {
56 FS_READ_WRITE = 1,
57 FS_READ_ONLY = 2
58};
59
60/* maximum length (according to MIB) for fs_entry::mountPoint */
61#define FS_MP_MLEN (128 + 1)
62
63/* maximum length (according to MIB) for fs_entry::remoteMountPoint */
64#define FS_RMP_MLEN (128 + 1)
65
59/*
60 * This structure is used to hold a SNMP table entry
61 * for HOST-RESOURCES-MIB's hrFSTable
62 */
63struct fs_entry {
64 int32_t index;
66/*
67 * This structure is used to hold a SNMP table entry
68 * for HOST-RESOURCES-MIB's hrFSTable
69 */
70struct fs_entry {
71 int32_t index;
65 u_char mountPoint[128 + 1];
66 u_char remoteMountPoint[128 + 1];
72 u_char *mountPoint;
73 u_char *remoteMountPoint;
67 const struct asn_oid *type;
68 int32_t access; /* enum hrFSAccess, see above */
69 int32_t bootable; /* TruthValue */
70 int32_t storageIndex; /* hrStorageTblEntry::index */
71 u_char lastFullBackupDate[11];
72 u_char lastPartialBackupDate[11];
74 const struct asn_oid *type;
75 int32_t access; /* enum hrFSAccess, see above */
76 int32_t bootable; /* TruthValue */
77 int32_t storageIndex; /* hrStorageTblEntry::index */
78 u_char lastFullBackupDate[11];
79 u_char lastPartialBackupDate[11];
73#define HR_FS_FOUND 0x001
80#define HR_FS_FOUND 0x001
74 uint32_t flags; /* not in mib table, for internal use */
75 TAILQ_ENTRY(fs_entry) link;
76};
77TAILQ_HEAD(fs_tbl, fs_entry);
78
79/*
80 * Next structure is used to keep o list of mappings from a specific name
81 * (a_name) to an entry in the hrFSTblEntry. We are trying to keep the same
82 * index for a specific name at least for the duration of one SNMP agent run.
83 */
84struct fs_map_entry {
81 uint32_t flags; /* not in mib table, for internal use */
82 TAILQ_ENTRY(fs_entry) link;
83};
84TAILQ_HEAD(fs_tbl, fs_entry);
85
86/*
87 * Next structure is used to keep o list of mappings from a specific name
88 * (a_name) to an entry in the hrFSTblEntry. We are trying to keep the same
89 * index for a specific name at least for the duration of one SNMP agent run.
90 */
91struct fs_map_entry {
85 int32_t hrIndex; /* used for hrFSTblEntry::index */
86 u_char a_name[128 + 1];/* map key */
92 int32_t hrIndex; /* used for fs_entry::index */
93 u_char *a_name; /* map key same as fs_entry::mountPoint */
87
88 /* may be NULL if the respective hrFSTblEntry is (temporally) gone */
89 struct fs_entry *entry;
90 STAILQ_ENTRY(fs_map_entry) link;
91};
92STAILQ_HEAD(fs_map, fs_map_entry);
93
94/* head of the list with hrFSTable's entries */

--- 46 unchanged lines hidden (view full) ---

141 * Create an entry into the FS table and an entry in the map (if needed).
142 */
143static struct fs_entry *
144fs_entry_create(const char *name)
145{
146 struct fs_entry *entry;
147 struct fs_map_entry *map;
148
94
95 /* may be NULL if the respective hrFSTblEntry is (temporally) gone */
96 struct fs_entry *entry;
97 STAILQ_ENTRY(fs_map_entry) link;
98};
99STAILQ_HEAD(fs_map, fs_map_entry);
100
101/* head of the list with hrFSTable's entries */

--- 46 unchanged lines hidden (view full) ---

148 * Create an entry into the FS table and an entry in the map (if needed).
149 */
150static struct fs_entry *
151fs_entry_create(const char *name)
152{
153 struct fs_entry *entry;
154 struct fs_map_entry *map;
155
149 if ((entry = malloc(sizeof(*entry))) == NULL) {
150 syslog(LOG_WARNING, "%s: %m", __func__);
151 return (NULL);
152 }
156 assert(name != NULL);
157 assert(strlen(name) > 0);
153
158
154 strlcpy(entry->mountPoint, name, sizeof(entry->mountPoint));
155
156 STAILQ_FOREACH(map, &fs_map, link)
159 STAILQ_FOREACH(map, &fs_map, link)
157 if (strncmp(map->a_name, entry->mountPoint,
158 sizeof(map->a_name) - 1) == 0) {
159 entry->index = map->hrIndex;
160 map->entry = entry;
160 if (strcmp(map->a_name, name) == 0)
161 break;
161 break;
162 }
163
164 if (map == NULL) {
162
163 if (map == NULL) {
164 size_t mount_point_len;
165
165 /* new object - get a new index */
166 if (next_fs_index > INT_MAX) {
166 /* new object - get a new index */
167 if (next_fs_index > INT_MAX) {
167 /* XXX no other sensible reaction? */
168 /* Unrecoverable error - die clean and quicly*/
168 syslog(LOG_ERR, "%s: hrFSTable index wrap", __func__);
169 syslog(LOG_ERR, "%s: hrFSTable index wrap", __func__);
169 return (NULL);
170 errx(EX_SOFTWARE, "hrFSTable index wrap");
170 }
171
172 if ((map = malloc(sizeof(*map))) == NULL) {
173 syslog(LOG_ERR, "%s: %m", __func__);
171 }
172
173 if ((map = malloc(sizeof(*map))) == NULL) {
174 syslog(LOG_ERR, "%s: %m", __func__);
174 free(entry);
175 return (NULL);
176 }
177
175 return (NULL);
176 }
177
178 map->hrIndex = next_fs_index++;
179 strlcpy(map->a_name, entry->mountPoint, sizeof(map->a_name));
180 map->entry = entry;
178 mount_point_len = strlen(name) + 1;
179 if (mount_point_len > FS_MP_MLEN)
180 mount_point_len = FS_MP_MLEN;
181
181
182 if ((map->a_name = malloc(mount_point_len)) == NULL) {
183 syslog(LOG_ERR, "%s: %m", __func__);
184 free(map);
185 return (NULL);
186 }
187
188 strlcpy(map->a_name, name, mount_point_len);
189
190 map->hrIndex = next_fs_index++;
191 map->entry = NULL;
182 STAILQ_INSERT_TAIL(&fs_map, map, link);
183
184 HRDBG("%s added into hrFSMap at index=%d", name, map->hrIndex);
185 } else {
186 HRDBG("%s exists in hrFSMap index=%d", name, map->hrIndex);
187 }
188
192 STAILQ_INSERT_TAIL(&fs_map, map, link);
193
194 HRDBG("%s added into hrFSMap at index=%d", name, map->hrIndex);
195 } else {
196 HRDBG("%s exists in hrFSMap index=%d", name, map->hrIndex);
197 }
198
199 if ((entry = malloc(sizeof(*entry))) == NULL) {
200 syslog(LOG_WARNING, "%s: %m", __func__);
201 return (NULL);
202 }
203
204 if ((entry->mountPoint = strdup(name)) == NULL) {
205 syslog(LOG_ERR, "%s: %m", __func__);
206 free(entry);
207 return (NULL);
208 }
209
189 entry->index = map->hrIndex;
210 entry->index = map->hrIndex;
211 map->entry = entry;
190
191 INSERT_OBJECT_INT(entry, &fs_tbl);
212
213 INSERT_OBJECT_INT(entry, &fs_tbl);
192
193 return (entry);
194}
195
196/**
197 * Delete an entry in the FS table.
198 */
199static void
200fs_entry_delete(struct fs_entry* entry)
201{
202 struct fs_map_entry *map;
203
214 return (entry);
215}
216
217/**
218 * Delete an entry in the FS table.
219 */
220static void
221fs_entry_delete(struct fs_entry* entry)
222{
223 struct fs_map_entry *map;
224
225 assert(entry != NULL);
226
204 TAILQ_REMOVE(&fs_tbl, entry, link);
205 STAILQ_FOREACH(map, &fs_map, link)
206 if (map->entry == entry) {
207 map->entry = NULL;
208 break;
209 }
227 TAILQ_REMOVE(&fs_tbl, entry, link);
228 STAILQ_FOREACH(map, &fs_map, link)
229 if (map->entry == entry) {
230 map->entry = NULL;
231 break;
232 }
210
233 free(entry->mountPoint);
234 free(entry->remoteMountPoint);
211 free(entry);
212}
213
214/**
215 * Find a table entry by its name
216 */
217static struct fs_entry *
218fs_find_by_name(const char *name)
219{
220 struct fs_entry *entry;
221
222 TAILQ_FOREACH(entry, &fs_tbl, link)
235 free(entry);
236}
237
238/**
239 * Find a table entry by its name
240 */
241static struct fs_entry *
242fs_find_by_name(const char *name)
243{
244 struct fs_entry *entry;
245
246 TAILQ_FOREACH(entry, &fs_tbl, link)
223 if (strncmp(entry->mountPoint, name,
224 sizeof(entry->mountPoint) - 1) == 0)
247 if (strcmp(entry->mountPoint, name) == 0)
225 return (entry);
226
227 return (NULL);
228}
229
230/**
231 * Get rid of all data
232 */
233void
234fini_fs_tbl(void)
235{
236 struct fs_map_entry *n1;
237
238 while ((n1 = STAILQ_FIRST(&fs_map)) != NULL) {
239 STAILQ_REMOVE_HEAD(&fs_map, link);
240 if (n1->entry != NULL) {
241 TAILQ_REMOVE(&fs_tbl, n1->entry, link);
248 return (entry);
249
250 return (NULL);
251}
252
253/**
254 * Get rid of all data
255 */
256void
257fini_fs_tbl(void)
258{
259 struct fs_map_entry *n1;
260
261 while ((n1 = STAILQ_FIRST(&fs_map)) != NULL) {
262 STAILQ_REMOVE_HEAD(&fs_map, link);
263 if (n1->entry != NULL) {
264 TAILQ_REMOVE(&fs_tbl, n1->entry, link);
265 free(n1->entry->mountPoint);
266 free(n1->entry->remoteMountPoint);
242 free(n1->entry);
243 }
267 free(n1->entry);
268 }
269 free(n1->a_name);
244 free(n1);
245 }
246 assert(TAILQ_EMPTY(&fs_tbl));
247}
248
249/**
250 * Called before the refreshing is started from the storage table.
251 */

--- 70 unchanged lines hidden (view full) ---

322
323 if (fs_p == NULL)
324 return;
325
326 if ((entry = fs_find_by_name(fs_p->f_mntonname)) != NULL ||
327 (entry = fs_entry_create(fs_p->f_mntonname)) != NULL) {
328 entry->flags |= HR_FS_FOUND;
329
270 free(n1);
271 }
272 assert(TAILQ_EMPTY(&fs_tbl));
273}
274
275/**
276 * Called before the refreshing is started from the storage table.
277 */

--- 70 unchanged lines hidden (view full) ---

348
349 if (fs_p == NULL)
350 return;
351
352 if ((entry = fs_find_by_name(fs_p->f_mntonname)) != NULL ||
353 (entry = fs_entry_create(fs_p->f_mntonname)) != NULL) {
354 entry->flags |= HR_FS_FOUND;
355
330 strcpy(entry->mountPoint, fs_p->f_mntonname);
331
332 if (!(fs_p->f_flags & MNT_LOCAL))
356 if (!(fs_p->f_flags & MNT_LOCAL)) {
333 /* this is a remote mount */
357 /* this is a remote mount */
334 strcpy(entry->remoteMountPoint, fs_p->f_mntfromname);
335 else
336 entry->remoteMountPoint[0] = '\0';
358 entry->remoteMountPoint = strdup(fs_p->f_mntfromname);
359 /* if strdup failed, let it be NULL */
337
360
361 } else {
362 entry->remoteMountPoint = strdup("");
363 /* if strdup failed, let it be NULL */
364 }
365
338 entry->type = fs_get_type(fs_p);
339
340 if ((fs_p->f_flags & MNT_RDONLY) == MNT_RDONLY)
341 entry->access = FS_READ_ONLY;
342 else
343 entry->access = FS_READ_WRITE;
344
345 /* FIXME - bootable fs ?! */

--- 60 unchanged lines hidden (view full) ---

406 case LEAF_hrFSIndex:
407 value->v.integer = entry->index;
408 return (SNMP_ERR_NOERROR);
409
410 case LEAF_hrFSMountPoint:
411 return (string_get(value, entry->mountPoint, -1));
412
413 case LEAF_hrFSRemoteMountPoint:
366 entry->type = fs_get_type(fs_p);
367
368 if ((fs_p->f_flags & MNT_RDONLY) == MNT_RDONLY)
369 entry->access = FS_READ_ONLY;
370 else
371 entry->access = FS_READ_WRITE;
372
373 /* FIXME - bootable fs ?! */

--- 60 unchanged lines hidden (view full) ---

434 case LEAF_hrFSIndex:
435 value->v.integer = entry->index;
436 return (SNMP_ERR_NOERROR);
437
438 case LEAF_hrFSMountPoint:
439 return (string_get(value, entry->mountPoint, -1));
440
441 case LEAF_hrFSRemoteMountPoint:
414 return (string_get(value, entry->remoteMountPoint, -1));
442 if (entry->remoteMountPoint == NULL)
443 return (string_get(value, "", -1));
444 else
445 return (string_get(value, entry->remoteMountPoint, -1));
415 break;
416
417 case LEAF_hrFSType:
446 break;
447
448 case LEAF_hrFSType:
418 value->v.oid = *entry->type;
449 assert(entry->type != NULL);
450 value->v.oid = *(entry->type);
419 return (SNMP_ERR_NOERROR);
420
421 case LEAF_hrFSAccess:
422 value->v.integer = entry->access;
423 return (SNMP_ERR_NOERROR);
424
425 case LEAF_hrFSBootable:
426 value->v.integer = entry->bootable;

--- 14 unchanged lines hidden ---
451 return (SNMP_ERR_NOERROR);
452
453 case LEAF_hrFSAccess:
454 value->v.integer = entry->access;
455 return (SNMP_ERR_NOERROR);
456
457 case LEAF_hrFSBootable:
458 value->v.integer = entry->bootable;

--- 14 unchanged lines hidden ---