and added files
[bcm963xx.git] / userapps / opensource / net-snmp / agent / kernel.c
1
2 /*
3  *  13 Jun 91  wsak (wk0x@andrew) added mips support
4  */
5
6 #include <net-snmp/net-snmp-config.h>
7
8 #ifdef CAN_USE_NLIST
9
10 #include <sys/types.h>
11 #if HAVE_STDLIB_H
12 #include <stdlib.h>
13 #endif
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <stdio.h>
18 #include <errno.h>
19 #if HAVE_STRING_H
20 #include <string.h>
21 #endif
22 #if HAVE_FCNTL_H
23 #include <fcntl.h>
24 #endif
25 #if HAVE_NETINET_IN_H
26 #include <netinet/in.h>
27 #endif
28 #if HAVE_KVM_H
29 #include <kvm.h>
30 #endif
31
32 #include <net-snmp/net-snmp-includes.h>
33
34 #include "kernel.h"
35 #include <net-snmp/agent/ds_agent.h>
36
37 #ifndef NULL
38 #define NULL 0
39 #endif
40
41
42 #if HAVE_KVM_H
43 kvm_t          *kd;
44
45 void
46 init_kmem(const char *file)
47 {
48 #if HAVE_KVM_OPENFILES
49     char            err[4096];
50     kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, err);
51     if (kd == NULL && !netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 
52                                            NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {
53         snmp_log(LOG_CRIT, "init_kmem: kvm_openfiles failed: %s\n", err);
54         exit(1);
55     }
56 #else
57     kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL);
58     if (!kd && !netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 
59                                        NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {
60         snmp_log(LOG_CRIT, "init_kmem: kvm_open failed: %s\n",
61                  strerror(errno));
62         exit(1);
63     }
64 #endif                          /* HAVE_KVM_OPENFILES */
65 }
66
67
68 /*
69  *  klookup:
70  *
71  *  It seeks to the location  off  in kmem
72  *  It does a read into  target  of  siz  bytes.
73  *
74  *  Return 0 on failure and 1 on sucess.
75  *
76  */
77
78
79 int
80 klookup(unsigned long off, char *target, int siz)
81 {
82     int             result;
83     if (kd == NULL)
84         return 0;
85     result = kvm_read(kd, off, target, siz);
86     if (result != siz) {
87 #if HAVE_KVM_OPENFILES
88         snmp_log(LOG_ERR, "kvm_read(*, %lx, %p, %d) = %d: %s\n", off,
89                  target, siz, result, kvm_geterr(kd));
90 #else
91         snmp_log(LOG_ERR, "kvm_read(*, %lx, %p, %d) = %d: ", off, target,
92                  siz, result);
93         snmp_log_perror("klookup");
94 #endif
95         return 0;
96     }
97     return 1;
98 }
99
100 #else                           /* HAVE_KVM_H */
101
102 static off_t    klseek(off_t);
103 static int      klread(char *, int);
104 int             swap, mem, kmem;
105
106 void
107 init_kmem(const char *file)
108 {
109     kmem = open(file, O_RDONLY);
110     if (kmem < 0 && !netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 
111                                             NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {
112         snmp_log_perror(file);
113         exit(1);
114     }
115     fcntl(kmem, F_SETFD, 1);
116     mem = open("/dev/mem", O_RDONLY);
117     if (mem < 0 && !netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 
118                                            NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {
119         snmp_log_perror("/dev/mem");
120         exit(1);
121     }
122     fcntl(mem, F_SETFD, 1);
123 #ifdef DMEM_LOC
124     swap = open(DMEM_LOC, O_RDONLY);
125     if (swap < 0 && !netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 
126                                             NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {
127         snmp_log_perror(DMEM_LOC);
128         exit(1);
129     }
130     fcntl(swap, F_SETFD, 1);
131 #endif
132 }
133
134
135 /*
136  *  Seek into the kernel for a value.
137  */
138 static          off_t
139 klseek(off_t base)
140 {
141     return (lseek(kmem, (off_t) base, SEEK_SET));
142 }
143
144
145 /*
146  *  Read from the kernel 
147  */
148 static int
149 klread(char *buf, int buflen)
150 {
151     return (read(kmem, buf, buflen));
152 }
153
154
155 /*
156  *  klookup:
157  *
158  *  It seeks to the location  off  in kmem
159  *  It does a read into  target  of  siz  bytes.
160  *
161  *  Return 0 on failure and 1 on sucess.
162  *
163  */
164
165
166 int
167 klookup(unsigned long off, char *target, int siz)
168 {
169     long            retsiz;
170
171     if (kmem < 0)
172         return 0;
173
174     if ((retsiz = klseek((off_t) off)) != off) {
175         snmp_log(LOG_ERR, "klookup(%lx, %p, %d): ", off, target, siz);
176         snmp_log_perror("klseek");
177 #ifdef EXIT_ON_BAD_KLREAD
178         exit(1);
179 #endif
180         return (0);
181     }
182     if ((retsiz = klread(target, siz)) != siz) {
183         if (snmp_get_do_debugging()) {
184             /*
185              * these happen too often on too many architectures to print them
186              * unless we're in debugging mode. People get very full log files. 
187              */
188             snmp_log(LOG_ERR, "klookup(%lx, %p, %d): ", off, target, siz);
189             snmp_log_perror("klread");
190         }
191 #ifdef EXIT_ON_BAD_KLREAD
192         exit(1);
193 #endif
194         return (0);
195     }
196     return (1);
197 }
198
199 #endif                          /* HAVE_KVM_H */
200
201 #endif                          /* CAN_USE_NLIST */