Debian Codegraph @

agent/mibgroup/host/hr_partition.c

raw
1 /*
2 * Host Resources MIB - partition device group implementation - hr_partition.c
3 *
4 */
5 /* Portions of this file are subject to the following copyright(s). See
6 * the Net-SNMP's COPYING file for more details and other copyrights
7 * that may apply:
8 */
9 /*
10 * Portions of this file are copyrighted by:
11 * Copyright (C) 2007 Apple, Inc. All rights reserved.
12 * Use is subject to license terms specified in the COPYING file
13 * distributed with the Net-SNMP package.
14 */
15
16 #include <net-snmp/net-snmp-config.h>
17 #include <fcntl.h>
18 #ifdef HAVE_STRING_H
19 #include <string.h>
20 #else
21 #include <strings.h>
22 #endif
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 #include <errno.h>
27
28 #include "host_res.h"
29 #include "hr_partition.h"
30 #include "hr_filesys.h"
31 #include "hr_disk.h"
32
33 #include <sys/stat.h>
34
35 #define HRP_MONOTONICALLY_INCREASING
36
37 /*********************
38 *
39 * Kernel & interface information,
40 * and internal forward declarations
41 *
42 *********************/
43
44 static int HRP_savedDiskIndex;
45 static int HRP_savedPartIndex;
46 static char HRP_savedName[1024];
47 #ifdef NETSNMP_CAN_GET_DISK_LABEL
48 static char HRP_savedLabel[1024];
49 #endif
50 #ifdef darwin
51 extern int
52 Get_HR_Disk_Label(char *string, size_t str_len, const char *devfull);
53 #endif
54
55 static int HRP_DiskIndex;
56
57 static void Save_HR_Partition(int, int);
58
59
60 /*********************
61 *
62 * Initialisation & common implementation functions
63 *
64 *********************/
65
66 static void Init_HR_Partition(void);
67 static int Get_Next_HR_Partition(void);
68
69
70 #define HRPART_INDEX 1
71 #define HRPART_LABEL 2
72 #define HRPART_ID 3
73 #define HRPART_SIZE 4
74 #define HRPART_FSIDX 5
75
76 struct variable4 hrpartition_variables[] = {
77 {HRPART_INDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
78 var_hrpartition, 2, {1, 1}},
79 {HRPART_LABEL, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
80 var_hrpartition, 2, {1, 2}},
81 {HRPART_ID, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
82 var_hrpartition, 2, {1, 3}},
83 {HRPART_SIZE, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
84 var_hrpartition, 2, {1, 4}},
85 {HRPART_FSIDX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
86 var_hrpartition, 2, {1, 5}}
87 };
88 oid hrpartition_variables_oid[] =
89 { 1, 3, 6, 1, 2, 1, 25, 3, 7 };
90
91
92 void
93 init_hr_partition(void)
94 {
95 REGISTER_MIB("host/hr_partition", hrpartition_variables, variable4,
96 hrpartition_variables_oid);
97 }
98
99
100 /*
101 * header_hrpartition(...
102 * Arguments:
103 * vp IN - pointer to variable entry that points here
104 * name IN/OUT - IN/name requested, OUT/name found
105 * length IN/OUT - length of IN/OUT oid's
106 * exact IN - TRUE if an exact match was requested
107 * var_len OUT - length of variable or 0 if function returned
108 * write_method
109 *
110 */
111
112 int
113 header_hrpartition(struct variable *vp,
114 oid * name,
115 size_t * length,
116 int exact,
117 size_t * var_len, WriteMethod ** write_method)
118 {
119 #define HRPART_DISK_NAME_LENGTH 11
120 #define HRPART_ENTRY_NAME_LENGTH 12
121 oid newname[MAX_OID_LEN];
122 int part_idx, LowDiskIndex = -1, LowPartIndex = -1;
123 int result;
124
125 DEBUGMSGTL(("host/hr_partition", "var_hrpartition: "));
126 DEBUGMSGOID(("host/hr_partition", name, *length));
127 DEBUGMSG(("host/hr_partition", " %d\n", exact));
128
129 memcpy((char *) newname, (char *) vp->name,
130 (int) vp->namelen * sizeof(oid));
131 /*
132 * Find "next" partition entry
133 */
134
135 Init_HR_Disk();
136 Init_HR_Partition();
137
138 /*
139 * Find the "next" disk and partition entries.
140 * If we're in the middle of the table, then there's
141 * no point in examining earlier disks, so set the
142 * starting disk to that of the variable being queried.
143 *
144 * If we've moved from one column of the table to another,
145 * then we need to start at the beginning again.
146 * (i.e. the 'compare' fails to match)
147 * Similarly if we're at the start of the table
148 * (i.e. *length is too short to be a full instance)
149 */
150
151 if ((snmp_oid_compare(vp->name, vp->namelen, name, vp->namelen) == 0)
152 && (*length > HRPART_DISK_NAME_LENGTH)) {
153 LowDiskIndex =
154 (name[HRPART_DISK_NAME_LENGTH] &
155 ((1 << HRDEV_TYPE_SHIFT) - 1));
156
157 DEBUGMSGTL(("host/hr_partition", "... low index %d\n", LowDiskIndex));
158 while (HRP_DiskIndex < LowDiskIndex) {
159 Init_HR_Partition(); /* moves to next disk */
160 if (HRP_DiskIndex == -1)
161 return (MATCH_FAILED);
162 }
163 }
164
165 for (;;) {
166 part_idx = Get_Next_HR_Partition();
167 DEBUGMSGTL(("host/hr_partition", "... part index %d\n", part_idx));
168 if (part_idx == 0)
169 break;
170 newname[HRPART_DISK_NAME_LENGTH] =
171 (HRDEV_DISK << HRDEV_TYPE_SHIFT) + HRP_DiskIndex;
172 newname[HRPART_ENTRY_NAME_LENGTH] = part_idx;
173 result = snmp_oid_compare(name, *length, newname, vp->namelen + 2);
174 if (exact && (result == 0)) {
175 Save_HR_Partition(HRP_DiskIndex, part_idx);
176 LowDiskIndex = HRP_DiskIndex;
177 LowPartIndex = part_idx;
178 break;
179 }
180 if (!exact && (result < 0)) {
181 if (LowPartIndex == -1) {
182 Save_HR_Partition(HRP_DiskIndex, part_idx);
183 LowDiskIndex = HRP_DiskIndex;
184 LowPartIndex = part_idx;
185 } else if (LowDiskIndex < HRP_DiskIndex)
186 break;
187 else if (part_idx < LowPartIndex) {
188 Save_HR_Partition(HRP_DiskIndex, part_idx);
189 LowDiskIndex = HRP_DiskIndex;
190 LowPartIndex = part_idx;
191 }
192 #ifdef HRP_MONOTONICALLY_INCREASING
193 break;
194 #endif
195 }
196 }
197
198 if (LowPartIndex == -1) {
199 DEBUGMSGTL(("host/hr_partition", "... index out of range\n"));
200 return (MATCH_FAILED);
201 }
202
203 newname[HRPART_DISK_NAME_LENGTH] =
204 (HRDEV_DISK << HRDEV_TYPE_SHIFT) + LowDiskIndex;
205 newname[HRPART_ENTRY_NAME_LENGTH] = LowPartIndex;
206 memcpy((char *) name, (char *) newname,
207 ((int) vp->namelen + 2) * sizeof(oid));
208 *length = vp->namelen + 2;
209 *write_method = (WriteMethod*)0;
210 *var_len = sizeof(long); /* default to 'long' results */
211
212 DEBUGMSGTL(("host/hr_partition", "... get partition stats "));
213 DEBUGMSGOID(("host/hr_partition", name, *length));
214 DEBUGMSG(("host/hr_partition", "\n"));
215 return LowPartIndex;
216 }
217
218
219 /*********************
220 *
221 * System specific implementation functions
222 *
223 *********************/
224
225
226 u_char *
227 var_hrpartition(struct variable * vp,
228 oid * name,
229 size_t * length,
230 int exact, size_t * var_len, WriteMethod ** write_method)
231 {
232 int part_idx;
233 static char string[1024];
234 struct stat stat_buf;
235
236 part_idx =
237 header_hrpartition(vp, name, length, exact, var_len, write_method);
238 if (part_idx == MATCH_FAILED)
239 return NULL;
240
241 if (stat(HRP_savedName, &stat_buf) == -1)
242 return NULL;
243
244 switch (vp->magic) {
245 case HRPART_INDEX:
246 long_return = part_idx;
247 return (u_char *) & long_return;
248 case HRPART_LABEL:
249 #ifdef NETSNMP_CAN_GET_DISK_LABEL
250 *var_len = strlen(HRP_savedLabel);
251 return (u_char *) HRP_savedLabel;
252 #else
253 *var_len = strlen(HRP_savedName);
254 return (u_char *) HRP_savedName;
255 #endif
256 case HRPART_ID: /* Use the device number */
257 sprintf(string, "0x%x", (int) stat_buf.st_rdev);
258 *var_len = strlen(string);
259 return (u_char *) string;
260 case HRPART_SIZE:
261 /*
262 * XXX - based on single partition assumption
263 */
264 long_return = Get_FSSize(HRP_savedName);
265 return (u_char *) & long_return;
266 case HRPART_FSIDX:
267 long_return = Get_FSIndex(HRP_savedName);
268 return (u_char *) & long_return;
269 default:
270 DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrpartition\n",
271 vp->magic));
272 }
273 return NULL;
274 }
275
276
277 /*********************
278 *
279 * Internal implementation functions
280 *
281 *********************/
282
283 static int HRP_index;
284
285 static void
286 Init_HR_Partition(void)
287 {
288 DEBUGMSGTL(("host/hr_partition", "Init_HR_Partition\n"));
289 HRP_DiskIndex = Get_Next_HR_Disk();
290 if (HRP_DiskIndex != -1)
291 HRP_DiskIndex &= ((1 << HRDEV_TYPE_SHIFT) - 1);
292 DEBUGMSGTL(("host/hr_partition", "... %d\n",HRP_DiskIndex));
293
294 HRP_index = -1;
295 }
296
297 static int
298 Get_Next_HR_Partition(void)
299 {
300 char string[1024];
301 int fd;
302
303 DEBUGMSGTL(("host/hr_partition", "Get_Next_HR_Partition %d\n",HRP_DiskIndex));
304 if (HRP_DiskIndex == -1) {
305 return 0;
306 }
307
308 HRP_index++;
309 while (Get_Next_HR_Disk_Partition(string, sizeof(string), HRP_index) != -1) {
310 DEBUGMSGTL(("host/hr_partition",
311 "Get_Next_HR_Partition: %s (:%d)\n",
312 string, HRP_index));
313
314 #ifdef O_NDELAY
315 fd = open(string, O_RDONLY|O_NDELAY);
316 #else
317 fd = open(string, O_RDONLY);
318 #endif
319 if (fd != -1) {
320 close(fd);
321 return HRP_index + 1;
322 } else if (errno == EBUSY) {
323 return HRP_index + 1;
324 }
325 HRP_index++;
326 }
327
328 /*
329 * Finished with this disk, try the next
330 */
331 Init_HR_Partition();
332 return (Get_Next_HR_Partition());
333 }
334
335 static void
336 Save_HR_Partition(int disk_idx, int part_idx)
337 {
338 HRP_savedDiskIndex = disk_idx;
339 HRP_savedPartIndex = part_idx;
340 (void) Get_Next_HR_Disk_Partition(HRP_savedName, sizeof(HRP_savedName), HRP_index);
341 #ifdef NETSNMP_CAN_GET_DISK_LABEL
342 (void) Get_HR_Disk_Label(HRP_savedLabel, sizeof(HRP_savedLabel), HRP_savedName);
343 #endif
344 }