1 #include <net-snmp/net-snmp-config.h>
16 #include <netinet/in.h>
28 #include <net-snmp/agent/auto_nlist.h>
29 #include "autonlist.h"
32 #include <net-snmp/net-snmp-includes.h>
33 #include <net-snmp/agent/ds_agent.h>
35 struct autonlist *nlists = 0;
36 static void init_nlist(struct nlist *);
39 auto_nlist_value(const char *string)
41 struct autonlist **ptr, *it = 0;
48 while (*ptr != 0 && it == 0) {
49 cmp = strcmp((*ptr)->symbol, string);
53 ptr = &((*ptr)->left);
55 ptr = &((*ptr)->right);
59 *ptr = (struct autonlist *) malloc(sizeof(struct autonlist));
63 it->symbol = (char *) malloc(strlen(string) + 1);
64 strcpy(it->symbol, string);
66 * allocate an extra byte for inclusion of a preceding '_' later
68 it->nl[0].n_name = (char *) malloc(strlen(string) + 2);
70 strcpy(it->nl[0].n_name, string);
72 sprintf(it->nl[0].n_name, "_%s", string);
77 if (it->nl[0].n_type == 0) {
78 strcpy(it->nl[0].n_name, string);
82 if (it->nl[0].n_type == 0) {
83 if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
84 NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {
85 snmp_log(LOG_ERR, "nlist err: neither %s nor _%s found.\n",
90 DEBUGMSGTL(("auto_nlist", "nlist: found symbol %s at %x.\n",
91 it->symbol, it->nl[0].n_value));
92 return (it->nl[0].n_value);
95 return (it->nl[0].n_value);
99 auto_nlist(const char *string, char *var, int size)
103 result = auto_nlist_value(string);
106 ret = klookup(result, var, size);
109 "auto_nlist failed on %s at location %lx\n",
119 init_nlist(struct nlist nl[])
123 #if HAVE_KVM_OPENFILES
125 char kvm_errbuf[4096];
127 if ((kernel = kvm_openfiles(KERNEL_LOC, NULL, NULL, O_RDONLY, kvm_errbuf))
129 if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
130 NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {
133 snmp_log_perror("kvm_openfiles");
134 snmp_log(LOG_ERR, "kvm_openfiles: %s\n", kvm_errbuf);
138 if ((ret = kvm_nlist(kernel, nl)) == -1) {
139 if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
140 NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {
143 snmp_log_perror("kvm_nlist");
148 #else /* ! HAVE_KVM_OPENFILES */
149 #if defined(aix4) && defined(HAVE_KNLIST)
150 if (knlist(nl, 1, sizeof(struct nlist)) == -1) {
151 DEBUGMSGTL(("auto_nlist", "knlist failed on symbol: %s\n",
153 if (errno == EFAULT) {
157 snmp_log_perror("knlist");
158 if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
159 NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {
167 if ((ret = nlist(KERNEL_LOC, nl)) == -1) {
168 if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
169 NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {
172 snmp_log_perror("nlist");
177 #endif /* ! HAVE_KVM_OPENFILES */
178 for (ret = 0; nl[ret].n_name != NULL; ret++) {
180 if (nl[ret].n_type == 0 && nl[ret].n_value != 0)
183 if (nl[ret].n_type == 0) {
184 if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
185 NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {
186 DEBUGMSGTL(("auto_nlist", "nlist err: %s not found\n",
190 DEBUGMSGTL(("auto_nlist", "nlist: %s 0x%X\n", nl[ret].n_name,
191 (unsigned int) nl[ret].n_value));
194 #endif /* CAN_USE_NLIST */
198 KNLookup(struct nlist nl[], int nl_which, char *buf, int s)
200 struct nlist *nlp = &nl[nl_which];
202 if (nlp->n_value == 0) {
203 snmp_log(LOG_ERR, "Accessing non-nlisted variable: %s\n",
205 nlp->n_value = -1; /* only one error message ... */
208 if (nlp->n_value == -1)
211 return klookup(nlp->n_value, buf, s);
216 auto_nlist_print_tree(int indent, struct autonlist *ptr)
220 snmp_log(LOG_ERR, "nlist tree:\n");
221 auto_nlist_print_tree(12, nlists);
225 sprintf(buf, "%%%ds\n", indent);
227 * DEBUGMSGTL(("auto_nlist", "buf: %s\n",buf));
229 DEBUGMSGTL(("auto_nlist", buf, ptr->symbol));
230 auto_nlist_print_tree(indent + 2, ptr->left);
231 auto_nlist_print_tree(indent + 2, ptr->right);
235 #else /* !CAN_USE_NLIST */
237 auto_nlist_noop(void)
241 #endif /* CAN_USE_NLIST */