00001 #include <net-snmp/net-snmp-config.h> 00002 00003 #ifdef NETSNMP_CAN_USE_NLIST 00004 #if HAVE_STRING_H 00005 #include <string.h> 00006 #else 00007 #include <strings.h> 00008 #endif 00009 00010 #if HAVE_STDLIB_H 00011 #include <stdlib.h> 00012 #endif 00013 #include <stdio.h> 00014 #include <errno.h> 00015 #include <fcntl.h> 00016 #include <netinet/in.h> 00017 #ifdef HAVE_NLIST_H 00018 #include <nlist.h> 00019 #endif 00020 #if HAVE_KVM_H 00021 #include <kvm.h> 00022 #endif 00023 00024 #include <net-snmp/agent/auto_nlist.h> 00025 #include "autonlist.h" 00026 #include "kernel.h" 00027 00028 #include <net-snmp/net-snmp-includes.h> 00029 #include <net-snmp/agent/ds_agent.h> 00030 00031 struct autonlist *nlists = 0; 00032 static void init_nlist(struct nlist *); 00033 00034 long 00035 auto_nlist_value(const char *string) 00036 { 00037 struct autonlist **ptr, *it = 0; 00038 int cmp; 00039 00040 if (string == 0) 00041 return 0; 00042 00043 ptr = &nlists; 00044 while (*ptr != 0 && it == 0) { 00045 cmp = strcmp((*ptr)->symbol, string); 00046 if (cmp == 0) 00047 it = *ptr; 00048 else if (cmp < 0) { 00049 ptr = &((*ptr)->left); 00050 } else { 00051 ptr = &((*ptr)->right); 00052 } 00053 } 00054 if (*ptr == 0) { 00055 *ptr = (struct autonlist *) malloc(sizeof(struct autonlist)); 00056 it = *ptr; 00057 it->left = 0; 00058 it->right = 0; 00059 it->symbol = (char *) malloc(strlen(string) + 1); 00060 strcpy(it->symbol, string); 00061 /* 00062 * allocate an extra byte for inclusion of a preceding '_' later 00063 */ 00064 it->nl[0].n_name = (char *) malloc(strlen(string) + 2); 00065 #if defined(aix4) || defined(aix5) || defined(aix6) 00066 strcpy(it->nl[0].n_name, string); 00067 #else 00068 sprintf(it->nl[0].n_name, "_%s", string); 00069 #endif 00070 it->nl[1].n_name = 0; 00071 init_nlist(it->nl); 00072 #if !(defined(aix4) || defined(aix5) || defined(aix6)) 00073 if (it->nl[0].n_type == 0) { 00074 strcpy(it->nl[0].n_name, string); 00075 init_nlist(it->nl); 00076 } 00077 #endif 00078 if (it->nl[0].n_type == 0) { 00079 if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 00080 NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) { 00081 snmp_log(LOG_ERR, "nlist err: neither %s nor _%s found.\n", 00082 string, string); 00083 } 00084 return (-1); 00085 } else { 00086 DEBUGMSGTL(("auto_nlist:auto_nlist_value", "found symbol %s at %x.\n", 00087 it->symbol, it->nl[0].n_value)); 00088 return (it->nl[0].n_value); 00089 } 00090 } else 00091 return (it->nl[0].n_value); 00092 } 00093 00094 int 00095 auto_nlist(const char *string, char *var, int size) 00096 { 00097 long result; 00098 int ret; 00099 result = auto_nlist_value(string); 00100 if (result != -1) { 00101 if (var != NULL) { 00102 ret = klookup(result, var, size); 00103 if (!ret) 00104 snmp_log(LOG_ERR, 00105 "auto_nlist failed on %s at location %lx\n", 00106 string, result); 00107 return ret; 00108 } else 00109 return 1; 00110 } 00111 return 0; 00112 } 00113 00114 static void 00115 init_nlist(struct nlist nl[]) 00116 { 00117 int ret; 00118 #if HAVE_KVM_OPENFILES 00119 kvm_t *kernel; 00120 char kvm_errbuf[4096]; 00121 00122 if ((kernel = kvm_openfiles(KERNEL_LOC, NULL, NULL, O_RDONLY, kvm_errbuf)) 00123 == NULL) { 00124 if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 00125 NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) { 00126 return; 00127 } else { 00128 snmp_log_perror("kvm_openfiles"); 00129 snmp_log(LOG_ERR, "kvm_openfiles: %s\n", kvm_errbuf); 00130 exit(1); 00131 } 00132 } 00133 if ((ret = kvm_nlist(kernel, nl)) == -1) { 00134 if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 00135 NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) { 00136 return; 00137 } else { 00138 snmp_log_perror("kvm_nlist"); 00139 exit(1); 00140 } 00141 } 00142 kvm_close(kernel); 00143 #else /* ! HAVE_KVM_OPENFILES */ 00144 #if (defined(aix4) || defined(aix5) || defined(aix6)) && defined(HAVE_KNLIST) 00145 if (knlist(nl, 1, sizeof(struct nlist)) == -1) { 00146 DEBUGMSGTL(("auto_nlist:init_nlist", "knlist failed on symbol: %s\n", 00147 nl[0].n_name)); 00148 if (errno == EFAULT) { 00149 nl[0].n_type = 0; 00150 nl[0].n_value = 0; 00151 } else { 00152 snmp_log_perror("knlist"); 00153 if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 00154 NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) { 00155 return; 00156 } else { 00157 exit(1); 00158 } 00159 } 00160 } 00161 #else 00162 if ((ret = nlist(KERNEL_LOC, nl)) == -1) { 00163 if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 00164 NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) { 00165 return; 00166 } else { 00167 snmp_log_perror("nlist"); 00168 exit(1); 00169 } 00170 } 00171 #endif /*aix4 */ 00172 #endif /* ! HAVE_KVM_OPENFILES */ 00173 for (ret = 0; nl[ret].n_name != NULL; ret++) { 00174 #if defined(aix4) || defined(aix5) || defined(aix6) 00175 if (nl[ret].n_type == 0 && nl[ret].n_value != 0) 00176 nl[ret].n_type = 1; 00177 #endif 00178 if (nl[ret].n_type == 0) { 00179 if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 00180 NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) { 00181 DEBUGMSGTL(("auto_nlist:init_nlist", "nlist err: %s not found\n", 00182 nl[ret].n_name)); 00183 } 00184 } else { 00185 DEBUGMSGTL(("auto_nlist:init_nlist", "nlist: %s 0x%X\n", nl[ret].n_name, 00186 (unsigned int) nl[ret].n_value)); 00187 } 00188 } 00189 } 00190 00191 int 00192 KNLookup(struct nlist nl[], int nl_which, char *buf, int s) 00193 { 00194 struct nlist *nlp = &nl[nl_which]; 00195 00196 if (nlp->n_value == 0) { 00197 snmp_log(LOG_ERR, "Accessing non-nlisted variable: %s\n", 00198 nlp->n_name); 00199 nlp->n_value = -1; /* only one error message ... */ 00200 return 0; 00201 } 00202 if (nlp->n_value == -1) 00203 return 0; 00204 00205 return klookup(nlp->n_value, buf, s); 00206 } 00207 00208 #ifdef TESTING 00209 void 00210 auto_nlist_print_tree(int indent, struct autonlist *ptr) 00211 { 00212 char buf[1024]; 00213 if (indent == -2) { 00214 snmp_log(LOG_ERR, "nlist tree:\n"); 00215 auto_nlist_print_tree(12, nlists); 00216 } else { 00217 if (ptr == 0) 00218 return; 00219 sprintf(buf, "%%%ds\n", indent); 00220 /* 00221 * DEBUGMSGTL(("auto_nlist", "buf: %s\n",buf)); 00222 */ 00223 DEBUGMSGTL(("auto_nlist", buf, ptr->symbol)); 00224 auto_nlist_print_tree(indent + 2, ptr->left); 00225 auto_nlist_print_tree(indent + 2, ptr->right); 00226 } 00227 } 00228 #endif 00229 #else /* !NETSNMP_CAN_USE_NLIST */ 00230 #include <net-snmp/agent/auto_nlist.h> 00231 int 00232 auto_nlist_noop(void) 00233 { 00234 return 0; 00235 } 00236 #endif /* NETSNMP_CAN_USE_NLIST */
1.5.7.1
Last modified: Tuesday, 23-Dec-2025 17:22:04 UTC
For questions regarding web content and site functionality, please write to the net-snmp-users mail list.