Deleted Added
full compact
hostres_swrun_tbl.c (154860) hostres_swrun_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_swrun_tbl.c 154860 2006-01-26 12:19:10Z harti $
29 * $FreeBSD: head/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_swrun_tbl.c 160341 2006-07-14 09:07:56Z harti $
30 *
31 * Host Resources MIB for SNMPd. Implementation for hrSWRunTable
32 */
33
34#include <sys/param.h>
35#include <sys/proc.h>
36#include <sys/sysctl.h>
37#include <sys/user.h>

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

42#include <stdlib.h>
43#include <string.h>
44#include <syslog.h>
45
46#include "hostres_snmp.h"
47#include "hostres_oid.h"
48#include "hostres_tree.h"
49
30 *
31 * Host Resources MIB for SNMPd. Implementation for hrSWRunTable
32 */
33
34#include <sys/param.h>
35#include <sys/proc.h>
36#include <sys/sysctl.h>
37#include <sys/user.h>

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

42#include <stdlib.h>
43#include <string.h>
44#include <syslog.h>
45
46#include "hostres_snmp.h"
47#include "hostres_oid.h"
48#include "hostres_tree.h"
49
50
51/*
52 * Ugly thing: PID_MAX, NO_PID defined only in kernel
53 */
54#define NO_PID 100000
55
56enum SWRunType {
57 SRT_UNKNOWN = 1,
58 SRT_OPERATING_SYSTEM = 2,

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

63
64enum SWRunStatus {
65 SRS_RUNNING = 1,
66 SRS_RUNNABLE = 2,
67 SRS_NOT_RUNNABLE = 3,
68 SRS_INVALID = 4
69};
70
50/*
51 * Ugly thing: PID_MAX, NO_PID defined only in kernel
52 */
53#define NO_PID 100000
54
55enum SWRunType {
56 SRT_UNKNOWN = 1,
57 SRT_OPERATING_SYSTEM = 2,

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

62
63enum SWRunStatus {
64 SRS_RUNNING = 1,
65 SRS_RUNNABLE = 2,
66 SRS_NOT_RUNNABLE = 3,
67 SRS_INVALID = 4
68};
69
70/* Maximum lengths for the strings according to the MIB */
71#define SWR_NAME_MLEN (64 + 1)
72#define SWR_PATH_MLEN (128 + 1)
73#define SWR_PARAM_MLEN (128 + 1)
74
71/*
72 * This structure is used to hold a SNMP table entry
73 * for both hrSWRunTable and hrSWRunPerfTable because
74 * hrSWRunPerfTable AUGMENTS hrSWRunTable
75 */
76struct swrun_entry {
77 int32_t index;
75/*
76 * This structure is used to hold a SNMP table entry
77 * for both hrSWRunTable and hrSWRunPerfTable because
78 * hrSWRunPerfTable AUGMENTS hrSWRunTable
79 */
80struct swrun_entry {
81 int32_t index;
78 u_char name[64 + 1];
79 struct asn_oid id;
80 u_char path[128 + 1];
81 u_char parameters[128 + 1];
82 u_char *name; /* it may be NULL */
83 const struct asn_oid *id;
84 u_char *path; /* it may be NULL */
85 u_char *parameters; /* it may be NULL */
82 int32_t type; /* enum SWRunType */
83 int32_t status; /* enum SWRunStatus */
84 int32_t perfCPU;
85 int32_t perfMemory;
86 int32_t type; /* enum SWRunType */
87 int32_t status; /* enum SWRunStatus */
88 int32_t perfCPU;
89 int32_t perfMemory;
86#define HR_SWRUN_FOUND 0x001
90#define HR_SWRUN_FOUND 0x001
87 uint32_t flags;
88 uint64_t r_tick; /* tick when entry refreshed */
89 TAILQ_ENTRY(swrun_entry) link;
90};
91TAILQ_HEAD(swrun_tbl, swrun_entry);
92
93/* the head of the list with hrSWRunTable's entries */
94static struct swrun_tbl swrun_tbl = TAILQ_HEAD_INITIALIZER(swrun_tbl);

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

129static void
130swrun_entry_delete(struct swrun_entry *entry)
131{
132
133 assert(entry != NULL);
134
135 TAILQ_REMOVE(&swrun_tbl, entry, link);
136
91 uint32_t flags;
92 uint64_t r_tick; /* tick when entry refreshed */
93 TAILQ_ENTRY(swrun_entry) link;
94};
95TAILQ_HEAD(swrun_tbl, swrun_entry);
96
97/* the head of the list with hrSWRunTable's entries */
98static struct swrun_tbl swrun_tbl = TAILQ_HEAD_INITIALIZER(swrun_tbl);

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

133static void
134swrun_entry_delete(struct swrun_entry *entry)
135{
136
137 assert(entry != NULL);
138
139 TAILQ_REMOVE(&swrun_tbl, entry, link);
140
141 free(entry->name);
142 free(entry->path);
143 free(entry->parameters);
137 free(entry);
138}
139
140/**
141 * Search one item by its index, return NULL if none found
142 */
143static struct swrun_entry *
144swrun_entry_find_by_index(int32_t idx)
145{
146 struct swrun_entry *entry;
147
148 TAILQ_FOREACH(entry, &swrun_tbl, link)
149 if (entry->index == idx)
150 return (entry);
151 return (NULL);
152}
153
154/**
144 free(entry);
145}
146
147/**
148 * Search one item by its index, return NULL if none found
149 */
150static struct swrun_entry *
151swrun_entry_find_by_index(int32_t idx)
152{
153 struct swrun_entry *entry;
154
155 TAILQ_FOREACH(entry, &swrun_tbl, link)
156 if (entry->index == idx)
157 return (entry);
158 return (NULL);
159}
160
161/**
155 * Translate the kernel's process status to the SNMP one.
162 * Translate the kernel's process status to SNMP.
156 */
157static enum SWRunStatus
158swrun_OS_get_proc_status(const struct kinfo_proc *kp)
159{
160
161 assert(kp != NULL);
162 if(kp == NULL) {
163 return (SRS_INVALID);

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

194 * Make an SNMP table entry from a kernel one.
195 */
196static void
197kinfo_proc_to_swrun_entry(const struct kinfo_proc *kp,
198 struct swrun_entry *entry)
199{
200 char **argv = NULL;
201 uint64_t cpu_time = 0;
163 */
164static enum SWRunStatus
165swrun_OS_get_proc_status(const struct kinfo_proc *kp)
166{
167
168 assert(kp != NULL);
169 if(kp == NULL) {
170 return (SRS_INVALID);

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

201 * Make an SNMP table entry from a kernel one.
202 */
203static void
204kinfo_proc_to_swrun_entry(const struct kinfo_proc *kp,
205 struct swrun_entry *entry)
206{
207 char **argv = NULL;
208 uint64_t cpu_time = 0;
209 size_t pname_len;
202
210
203 strlcpy((char*)entry->name, kp->ki_comm, sizeof(entry->name));
211 pname_len = strlen(kp->ki_comm) + 1;
212 entry->name = reallocf(entry->name, pname_len);
213 if (entry->name != NULL)
214 strlcpy(entry->name, kp->ki_comm, pname_len);
204
215
205 entry->id = oid_zeroDotZero; /* unknown id - FIXME */
216 entry->id = &oid_zeroDotZero; /* unknown id - FIXME */
206
217
207 entry->path[0] = '\0';
208 entry->parameters[0] = '\0';
209
210 assert(hr_kd != NULL);
211
218 assert(hr_kd != NULL);
219
212 argv = kvm_getargv(hr_kd, kp, sizeof(entry->parameters) - 1);
220 argv = kvm_getargv(hr_kd, kp, SWR_PARAM_MLEN - 1);
213 if(argv != NULL){
221 if(argv != NULL){
214 memset(entry->parameters, '\0', sizeof(entry->parameters));
222 u_char param[SWR_PARAM_MLEN];
215
223
224 memset(param, '\0', sizeof(param));
225
216 /*
217 * FIXME
218 * Path seems to not be available.
219 * Try to hack the info in argv[0];
220 * this argv is under control of the program so this info
221 * is not realiable
222 */
223 if(*argv != NULL && (*argv)[0] == '/') {
226 /*
227 * FIXME
228 * Path seems to not be available.
229 * Try to hack the info in argv[0];
230 * this argv is under control of the program so this info
231 * is not realiable
232 */
233 if(*argv != NULL && (*argv)[0] == '/') {
224 memset(entry->path, '\0', sizeof(entry->path));
225 strlcpy((char*)entry->path, *argv, sizeof(entry->path));
234 size_t path_len;
235
236 path_len = strlen(*argv) + 1;
237 if (path_len > SWR_PATH_MLEN)
238 path_len = SWR_PATH_MLEN;
239
240 entry->path = reallocf(entry->path, path_len);
241 if (entry->path != NULL) {
242 memset(entry->path, '\0', path_len);
243 strlcpy((char*)entry->path, *argv, path_len);
244 }
226 }
227
228 argv++; /* skip the first one which was used for path */
229
230 while (argv != NULL && *argv != NULL ) {
245 }
246
247 argv++; /* skip the first one which was used for path */
248
249 while (argv != NULL && *argv != NULL ) {
231 if (entry->parameters[0] != 0) {
250 if (param[0] != 0) {
232 /*
233 * add a space between parameters,
234 * except before the first one
235 */
251 /*
252 * add a space between parameters,
253 * except before the first one
254 */
236 strlcat((char *)entry->parameters,
237 " ", sizeof(entry->parameters));
255 strlcat((char *)param, " ", sizeof(param));
238 }
256 }
239 strlcat((char *)entry->parameters, *argv,
240 sizeof(entry->parameters));
257 strlcat((char *)param, *argv, sizeof(param));
241 argv++;
242 }
258 argv++;
259 }
260 /* reuse pname_len */
261 pname_len = strlen(param) + 1;
262 if (pname_len > SWR_PARAM_MLEN)
263 pname_len = SWR_PARAM_MLEN;
264
265 entry->parameters = reallocf(entry->parameters, pname_len);
266 strlcpy(entry->parameters, param, pname_len);
243 }
244
245 entry->type = (int32_t)(IS_KERNPROC(kp) ? SRT_OPERATING_SYSTEM :
246 SRT_APPLICATION);
247
248 entry->status = (int32_t)swrun_OS_get_proc_status(kp);
249 cpu_time = kp->ki_runtime / 100000; /* centi-seconds */
250

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

256
257/**
258 * Create a table entry for a KLD
259 */
260static void
261kld_file_stat_to_swrun(const struct kld_file_stat *kfs,
262 struct swrun_entry *entry)
263{
267 }
268
269 entry->type = (int32_t)(IS_KERNPROC(kp) ? SRT_OPERATING_SYSTEM :
270 SRT_APPLICATION);
271
272 entry->status = (int32_t)swrun_OS_get_proc_status(kp);
273 cpu_time = kp->ki_runtime / 100000; /* centi-seconds */
274

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

280
281/**
282 * Create a table entry for a KLD
283 */
284static void
285kld_file_stat_to_swrun(const struct kld_file_stat *kfs,
286 struct swrun_entry *entry)
287{
288 size_t name_len;
264
265 assert(kfs != NULL);
266 assert(entry != NULL);
267
289
290 assert(kfs != NULL);
291 assert(entry != NULL);
292
268 strlcpy((char *)entry->name, kfs->name, sizeof(entry->name));
293 name_len = strlen(kfs->name) + 1;
294 if (name_len > SWR_NAME_MLEN)
295 name_len = SWR_NAME_MLEN;
269
296
297 entry->name = reallocf(entry->name, name_len);
298 if (entry->name != NULL)
299 strlcpy((char *)entry->name, kfs->name, name_len);
300
270 /* FIXME: can we find the location where the module was loaded from? */
301 /* FIXME: can we find the location where the module was loaded from? */
271 entry->path[0] = '\0';
302 entry->path = NULL;
272
273 /* no parameters for kernel files (.ko) of for the kernel */
303
304 /* no parameters for kernel files (.ko) of for the kernel */
274 entry->parameters[0] = '\0';
305 entry->parameters = NULL;
275
306
276 entry->id = oid_zeroDotZero; /* unknown id - FIXME */
307 entry->id = &oid_zeroDotZero; /* unknown id - FIXME */
277
278 if (strcmp(kfs->name, "kernel") == 0) {
279 entry->type = (int32_t)SRT_OPERATING_SYSTEM;
280 SWOSIndex = entry->index;
281 } else {
282 entry->type = (int32_t)SRT_DEVICE_DRIVER; /* well, not really */
283 }
284 entry->status = (int32_t)SRS_RUNNING;

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

624 ret = SNMP_ERR_NOERROR;
625 switch (value->var.subs[sub - 1]) {
626
627 case LEAF_hrSWRunIndex:
628 value->v.integer = entry->index;
629 break;
630
631 case LEAF_hrSWRunName:
308
309 if (strcmp(kfs->name, "kernel") == 0) {
310 entry->type = (int32_t)SRT_OPERATING_SYSTEM;
311 SWOSIndex = entry->index;
312 } else {
313 entry->type = (int32_t)SRT_DEVICE_DRIVER; /* well, not really */
314 }
315 entry->status = (int32_t)SRS_RUNNING;

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

655 ret = SNMP_ERR_NOERROR;
656 switch (value->var.subs[sub - 1]) {
657
658 case LEAF_hrSWRunIndex:
659 value->v.integer = entry->index;
660 break;
661
662 case LEAF_hrSWRunName:
632 ret = string_get(value, entry->name, -1);
633 break;
663 if (entry->name != NULL)
664 ret = string_get(value, entry->name, -1);
665 else
666 ret = string_get(value, "", -1);
667 break;
634
635 case LEAF_hrSWRunID:
668
669 case LEAF_hrSWRunID:
636 value->v.oid = entry->id;
637 break;
670 assert(entry->id != NULL);
671 value->v.oid = *entry->id;
672 break;
638
673
639 case LEAF_hrSWRunPath:
640 ret = string_get(value, entry->path, -1);
641 break;
674 case LEAF_hrSWRunPath:
675 if (entry->path != NULL)
676 ret = string_get(value, entry->path, -1);
677 else
678 ret = string_get(value, "", -1);
679 break;
642
643 case LEAF_hrSWRunParameters:
680
681 case LEAF_hrSWRunParameters:
644 ret = string_get(value, entry->parameters, -1);
645 break;
682 if (entry->parameters != NULL)
683 ret = string_get(value, entry->parameters, -1);
684 else
685 ret = string_get(value, "", -1);
686 break;
646
647 case LEAF_hrSWRunType:
687
688 case LEAF_hrSWRunType:
648 value->v.integer = entry->type;
689 value->v.integer = entry->type;
649 break;
650
651 case LEAF_hrSWRunStatus:
690 break;
691
692 case LEAF_hrSWRunStatus:
652 value->v.integer = entry->status;
693 value->v.integer = entry->status;
653 break;
654
655 default:
694 break;
695
696 default:
656 abort();
697 abort();
657 }
658 return (ret);
659}
660
661/**
662 * Scalar(s) in the SWRun group
663 */
664int

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

727 case SNMP_OP_SET:
728 if ((entry = FIND_OBJECT_INT(&swrun_tbl,
729 &value->var, sub)) == NULL)
730 return (SNMP_ERR_NO_CREATION);
731 return (SNMP_ERR_NOT_WRITEABLE);
732
733 case SNMP_OP_ROLLBACK:
734 case SNMP_OP_COMMIT:
698 }
699 return (ret);
700}
701
702/**
703 * Scalar(s) in the SWRun group
704 */
705int

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

768 case SNMP_OP_SET:
769 if ((entry = FIND_OBJECT_INT(&swrun_tbl,
770 &value->var, sub)) == NULL)
771 return (SNMP_ERR_NO_CREATION);
772 return (SNMP_ERR_NOT_WRITEABLE);
773
774 case SNMP_OP_ROLLBACK:
775 case SNMP_OP_COMMIT:
735 abort();
776 abort();
736 }
737 abort();
738
739 get:
740 switch (value->var.subs[sub - 1]) {
741
742 case LEAF_hrSWRunPerfCPU:
743 value->v.integer = entry->perfCPU;
744 return (SNMP_ERR_NOERROR);
745
746 case LEAF_hrSWRunPerfMem:
747 value->v.integer = entry->perfMemory;
777 }
778 abort();
779
780 get:
781 switch (value->var.subs[sub - 1]) {
782
783 case LEAF_hrSWRunPerfCPU:
784 value->v.integer = entry->perfCPU;
785 return (SNMP_ERR_NOERROR);
786
787 case LEAF_hrSWRunPerfMem:
788 value->v.integer = entry->perfMemory;
748 return (SNMP_ERR_NOERROR);
789 return (SNMP_ERR_NOERROR);
749 }
750 abort();
751}
790 }
791 abort();
792}