fix outgoing QOS prios
[tomato.git] / release / src / router / snmp / agent / kernel.c
bloba9b6cf49dceea76f77a816a34a780be1e388079d
2 /*
3 * 13 Jun 91 wsak (wk0x@andrew) added mips support
4 */
6 #include <net-snmp/net-snmp-config.h>
8 #ifdef CAN_USE_NLIST
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
32 #include <net-snmp/net-snmp-includes.h>
34 #include "kernel.h"
35 #include <net-snmp/agent/ds_agent.h>
37 #ifndef NULL
38 #define NULL 0
39 #endif
42 #if HAVE_KVM_H
43 kvm_t *kd;
45 void
46 init_kmem(const char *file)
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);
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);
64 #endif /* HAVE_KVM_OPENFILES */
69 * klookup:
71 * It seeks to the location off in kmem
72 * It does a read into target of siz bytes.
74 * Return 0 on failure and 1 on sucess.
79 int
80 klookup(unsigned long off, char *target, int siz)
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;
97 return 1;
100 #else /* HAVE_KVM_H */
102 static off_t klseek(off_t);
103 static int klread(char *, int);
104 int swap, mem, kmem;
106 void
107 init_kmem(const char *file)
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);
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);
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);
130 fcntl(swap, F_SETFD, 1);
131 #endif
136 * Seek into the kernel for a value.
138 static off_t
139 klseek(off_t base)
141 return (lseek(kmem, (off_t) base, SEEK_SET));
146 * Read from the kernel
148 static int
149 klread(char *buf, int buflen)
151 return (read(kmem, buf, buflen));
156 * klookup:
158 * It seeks to the location off in kmem
159 * It does a read into target of siz bytes.
161 * Return 0 on failure and 1 on sucess.
167 klookup(unsigned long off, char *target, int siz)
169 long retsiz;
171 if (kmem < 0)
172 return 0;
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);
182 if ((retsiz = klread(target, siz)) != siz) {
183 if (snmp_get_do_debugging()) {
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.
188 snmp_log(LOG_ERR, "klookup(%lx, %p, %d): ", off, target, siz);
189 snmp_log_perror("klread");
191 #ifdef EXIT_ON_BAD_KLREAD
192 exit(1);
193 #endif
194 return (0);
196 return (1);
199 #endif /* HAVE_KVM_H */
201 #endif /* CAN_USE_NLIST */