This diff removes all "extern struct snmpd *" lines from source files,
replaces all 'env' occurences with 'snmpd_env' and adds the extern
declaration for snmpd_env in the snmpd.h header.
With this diff we only need to guarantee that this variable is set,
we avoid shadowing other 'env' variables and we diminish the confusion
about this env variable thing.
We need this diff (or something with the same effect) to proceed with
fork+exec, because jca@ found out that traphandler child process do not
set env. We could alternatively just set env in traphandler p_init() and
remove snmpd_env, however it looks cleaner to me to not have all this
extern in all .c files and have a propper name for 'env'.
ok?
Index: kroute.c
===================================================================
RCS file: /home/obsdcvs/src/usr.sbin/snmpd/kroute.c,v
retrieving revision 1.33
diff -u -p -r1.33 kroute.c
--- kroute.c 3 Sep 2016 15:45:02 -0000 1.33
+++ kroute.c 22 Oct 2016 22:20:00 -0000
@@ -44,8 +44,6 @@
#include "snmpd.h"
-extern struct snmpd *env;
-
struct ktable **krt;
u_int krt_size;
@@ -173,8 +171,9 @@ kr_init(void)
&opt, sizeof(opt)) == -1)
log_warn("%s: SO_USELOOPBACK", __func__); /* not fatal */
- if (env->sc_rtfilter && setsockopt(kr_state.ks_fd, PF_ROUTE,
- ROUTE_MSGFILTER, &env->sc_rtfilter, sizeof(env->sc_rtfilter)) == -1)
+ if (snmpd_env->sc_rtfilter && setsockopt(kr_state.ks_fd, PF_ROUTE,
+ ROUTE_MSGFILTER, &snmpd_env->sc_rtfilter,
+ sizeof(snmpd_env->sc_rtfilter)) == -1)
log_warn("%s: ROUTE_MSGFILTER", __func__);
/* grow receive buffer, don't wanna miss messages */
Index: mib.c
===================================================================
RCS file: /home/obsdcvs/src/usr.sbin/snmpd/mib.c,v
retrieving revision 1.80
diff -u -p -r1.80 mib.c
--- mib.c 17 Nov 2015 12:30:23 -0000 1.80
+++ mib.c 22 Oct 2016 22:26:52 -0000
@@ -58,8 +58,6 @@
#include "snmpd.h"
#include "mib.h"
-extern struct snmpd *env;
-
/*
* Defined in SNMPv2-MIB.txt (RFC 3418)
*/
@@ -255,7 +253,7 @@ mib_sysor(struct oid *oid, struct ber_oi
int
mib_getsnmp(struct oid *oid, struct ber_oid *o, struct ber_element **elm)
{
- struct snmp_stats *stats = &env->sc_stats;
+ struct snmp_stats *stats = &snmpd_env->sc_stats;
long long i;
struct statsmap {
u_int8_t m_id;
@@ -316,7 +314,7 @@ mib_getsnmp(struct oid *oid, struct ber_
int
mib_setsnmp(struct oid *oid, struct ber_oid *o, struct ber_element **elm)
{
- struct snmp_stats *stats = &env->sc_stats;
+ struct snmp_stats *stats = &snmpd_env->sc_stats;
long long i;
if (ber_get_integer(*elm, &i) == -1)
@@ -354,11 +352,11 @@ mib_engine(struct oid *oid, struct ber_o
{
switch (oid->o_oid[OIDIDX_snmpEngine]) {
case 1:
- *elm = ber_add_nstring(*elm, env->sc_engineid,
- env->sc_engineid_len);
+ *elm = ber_add_nstring(*elm, snmpd_env->sc_engineid,
+ snmpd_env->sc_engineid_len);
break;
case 2:
- *elm = ber_add_integer(*elm, env->sc_engine_boots);
+ *elm = ber_add_integer(*elm, snmpd_env->sc_engine_boots);
break;
case 3:
*elm = ber_add_integer(*elm, snmpd_engine_time());
@@ -375,7 +373,7 @@ mib_engine(struct oid *oid, struct ber_o
int
mib_usmstats(struct oid *oid, struct ber_oid *o, struct ber_element **elm)
{
- struct snmp_stats *stats = &env->sc_stats;
+ struct snmp_stats *stats = &snmpd_env->sc_stats;
long long i;
struct statsmap {
u_int8_t m_id;
@@ -697,7 +695,7 @@ mib_hrdevice(struct oid *oid, struct ber
/* Get and verify the current row index */
idx = o->bo_id[OIDIDX_hrDeviceEntry];
- if (idx > (u_int)env->sc_ncpu)
+ if (idx > (u_int)snmpd_env->sc_ncpu)
return (1);
/* Tables need to prepend the OID on their own */
@@ -748,7 +746,7 @@ mib_hrprocessor(struct oid *oid, struct
/* Get and verify the current row index */
idx = o->bo_id[OIDIDX_hrDeviceEntry];
- if (idx > (u_int)env->sc_ncpu)
+ if (idx > (u_int)snmpd_env->sc_ncpu)
return (1);
else if (idx < 1)
idx = 1;
@@ -766,9 +764,9 @@ mib_hrprocessor(struct oid *oid, struct
* The percentage of time that the system was not
* idle during the last minute.
*/
- if (env->sc_cpustates == NULL)
+ if (snmpd_env->sc_cpustates == NULL)
return (-1);
- cptime2 = env->sc_cpustates + (CPUSTATES * (idx - 1));
+ cptime2 = snmpd_env->sc_cpustates + (CPUSTATES * (idx - 1));
val = 100 -
(cptime2[CP_IDLE] > 1000 ? 1000 : (cptime2[CP_IDLE] / 10));
ber = ber_add_integer(ber, val);
Index: mps.c
===================================================================
RCS file: /home/obsdcvs/src/usr.sbin/snmpd/mps.c,v
retrieving revision 1.23
diff -u -p -r1.23 mps.c
--- mps.c 5 Dec 2015 06:42:18 -0000 1.23
+++ mps.c 22 Oct 2016 22:21:16 -0000
@@ -45,8 +45,6 @@
#include "snmpd.h"
#include "mib.h"
-extern struct snmpd *env;
-
struct ber_oid *
mps_table(struct oid *, struct ber_oid *, struct ber_oid *);
Index: smi.c
===================================================================
RCS file: /home/obsdcvs/src/usr.sbin/snmpd/smi.c,v
retrieving revision 1.19
diff -u -p -r1.19 smi.c
--- smi.c 5 Dec 2015 06:42:18 -0000 1.19
+++ smi.c 22 Oct 2016 22:19:28 -0000
@@ -49,8 +49,6 @@
#define MINIMUM(a, b) (((a) < (b)) ? (a) : (b))
-extern struct snmpd *env;
-
RB_HEAD(oidtree, oid);
RB_PROTOTYPE(oidtree, oid, o_element, smi_oid_cmp);
struct oidtree smi_oidtree;
@@ -66,9 +64,9 @@ smi_getticks(void)
u_long ticks;
gettimeofday(&now, NULL);
- if (timercmp(&now, &env->sc_starttime, <=))
+ if (timercmp(&now, &snmpd_env->sc_starttime, <=))
return (0);
- timersub(&now, &env->sc_starttime, &run);
+ timersub(&now, &snmpd_env->sc_starttime, &run);
ticks = run.tv_sec * 100;
if (run.tv_usec)
ticks += run.tv_usec / 10000;
@@ -108,7 +106,7 @@ smi_oid2string(struct ber_oid *o, char *
bcopy(o, &key.o_id, sizeof(struct ber_oid));
key.o_flags |= OID_KEY; /* do not match wildcards */
- if (env->sc_flags & SNMPD_F_NONAMES)
+ if (snmpd_env->sc_flags & SNMPD_F_NONAMES)
lookup = 0;
for (i = 0; i < o->bo_n; i++) {
Index: snmpd.h
===================================================================
RCS file: /home/obsdcvs/src/usr.sbin/snmpd/snmpd.h,v
retrieving revision 1.69
diff -u -p -r1.69 snmpd.h
--- snmpd.h 3 Oct 2016 12:16:41 -0000 1.69
+++ snmpd.h 22 Oct 2016 22:26:19 -0000
@@ -580,6 +580,8 @@ struct trapcmd {
RB_HEAD(trapcmd_tree, trapcmd);
extern struct trapcmd_tree trapcmd_tree;
+extern struct snmpd *snmpd_env;
+
/* control.c */
int control_init(struct privsep *, struct control_sock *);
int control_listen(struct control_sock *);
Index: snmpe.c
===================================================================
RCS file: /home/obsdcvs/src/usr.sbin/snmpd/snmpe.c,v
retrieving revision 1.42
diff -u -p -r1.42 snmpe.c
--- snmpe.c 16 Aug 2016 18:41:57 -0000 1.42
+++ snmpe.c 23 Oct 2016 11:37:58 -0000
@@ -52,8 +52,6 @@ void snmpe_recvmsg(int fd, short, void
int snmpe_encode(struct snmp_message *);
void snmp_msgfree(struct snmp_message *);
-struct snmpd *env = NULL;
-
struct imsgev *iev_parent;
static struct privsep_proc procs[] = {
@@ -63,13 +61,12 @@ static struct privsep_proc procs[] = {
pid_t
snmpe(struct privsep *ps, struct privsep_proc *p)
{
+ struct snmpd *env = ps->ps_env;
#ifdef DEBUG
char buf[BUFSIZ];
struct oid *oid;
#endif
- env = ps->ps_env;
-
#ifdef DEBUG
for (oid = NULL; (oid = smi_foreach(oid, 0)) != NULL;) {
smi_oid2string(&oid->o_id, buf, sizeof(buf), 0);
@@ -88,6 +85,8 @@ snmpe(struct privsep *ps, struct privsep
void
snmpe_init(struct privsep *ps, struct privsep_proc *p, void *arg)
{
+ struct snmpd *env = ps->ps_env;
+
kr_init();
trap_init();
timer_init();
@@ -149,6 +148,7 @@ snmpe_bind(struct address *addr)
int
snmpe_parse(struct snmp_message *msg)
{
+ struct snmpd *env = snmpd_env;
struct snmp_stats *stats = &env->sc_stats;
struct ber_element *a;
long long ver, req;
@@ -331,7 +331,7 @@ snmpe_parse(struct snmp_message *msg)
int
snmpe_parsevarbinds(struct snmp_message *msg)
{
- struct snmp_stats *stats = &env->sc_stats;
+ struct snmp_stats *stats = &snmpd_env->sc_stats;
char buf[BUFSIZ];
struct ber_oid o;
int ret = 0;
@@ -398,7 +398,7 @@ snmpe_parsevarbinds(struct snmp_message
goto varfail;
case SNMP_C_SETREQ:
- if (env->sc_readonly == 0) {
+ if (snmpd_env->sc_readonly == 0) {
ret = mps_setreq(msg,
msg->sm_b, &o);
if (ret == 0)
@@ -452,6 +452,7 @@ snmpe_parsevarbinds(struct snmp_message
void
snmpe_recvmsg(int fd, short sig, void *arg)
{
+ struct snmpd *env = arg;
struct snmp_stats *stats = &env->sc_stats;
ssize_t len;
struct snmp_message *msg;
@@ -508,13 +509,13 @@ snmpe_dispatchmsg(struct snmp_message *m
/* not dispatched to subagent; respond directly */
msg->sm_context = SNMP_C_GETRESP;
- snmpe_response(env->sc_sock, msg);
+ snmpe_response(snmpd_env->sc_sock, msg);
}
void
snmpe_response(int fd, struct snmp_message *msg)
{
- struct snmp_stats *stats = &env->sc_stats;
+ struct snmp_stats *stats = &snmpd_env->sc_stats;
u_int8_t *ptr = NULL;
ssize_t len;
@@ -596,8 +597,9 @@ snmpe_encode(struct snmp_message *msg)
pdu = epdu = ber_add_sequence(NULL);
if (msg->sm_version == SNMP_V3) {
- if ((epdu = ber_printf_elements(epdu, "xs{", env->sc_engineid,
- env->sc_engineid_len, msg->sm_ctxname)) == NULL) {
+ if ((epdu = ber_printf_elements(epdu, "xs{",
+ snmpd_env->sc_engineid, snmpd_env->sc_engineid_len,
+ msg->sm_ctxname)) == NULL) {
ber_free_elements(pdu);
return -1;
}
Index: timer.c
===================================================================
RCS file: /home/obsdcvs/src/usr.sbin/snmpd/timer.c,v
retrieving revision 1.6
diff -u -p -r1.6 timer.c
--- timer.c 26 Sep 2016 14:00:05 -0000 1.6
+++ timer.c 22 Oct 2016 22:22:25 -0000
@@ -42,8 +42,6 @@
#include "snmpd.h"
#include "mib.h"
-extern struct snmpd *env;
-
void timer_cpu(int, short, void *);
int percentages(int, int64_t *, int64_t *, int64_t *, int64_t *);
@@ -62,9 +60,9 @@ timer_cpu(int fd, short event, void *arg
int64_t *cptime2;
len = CPUSTATES * sizeof(int64_t);
- for (n = 0; n < env->sc_ncpu; n++) {
+ for (n = 0; n < snmpd_env->sc_ncpu; n++) {
mib[2] = n;
- cptime2 = env->sc_cpustates + (CPUSTATES * n);
+ cptime2 = snmpd_env->sc_cpustates + (CPUSTATES * n);
if (sysctl(mib, 3, cp_time[n], &len, NULL, 0) == -1)
continue;
(void)percentages(CPUSTATES, cptime2, cp_time[n],
@@ -85,18 +83,19 @@ timer_init(void)
int mib[] = { CTL_HW, HW_NCPU }, i;
size_t len;
- len = sizeof(env->sc_ncpu);
- if (sysctl(mib, 2, &env->sc_ncpu, &len, NULL, 0) == -1)
+ len = sizeof(snmpd_env->sc_ncpu);
+ if (sysctl(mib, 2, &snmpd_env->sc_ncpu, &len, NULL, 0) == -1)
fatal("sysctl");
- env->sc_cpustates = calloc(env->sc_ncpu, CPUSTATES * sizeof(int64_t));
- cp_time = calloc(env->sc_ncpu, sizeof(int64_t *));
- cp_old = calloc(env->sc_ncpu, sizeof(int64_t *));
- cp_diff = calloc(env->sc_ncpu, sizeof(int64_t *));
- if (env->sc_cpustates == NULL ||
+ snmpd_env->sc_cpustates = calloc(snmpd_env->sc_ncpu,
+ CPUSTATES * sizeof(int64_t));
+ cp_time = calloc(snmpd_env->sc_ncpu, sizeof(int64_t *));
+ cp_old = calloc(snmpd_env->sc_ncpu, sizeof(int64_t *));
+ cp_diff = calloc(snmpd_env->sc_ncpu, sizeof(int64_t *));
+ if (snmpd_env->sc_cpustates == NULL ||
cp_time == NULL || cp_old == NULL || cp_diff == NULL)
fatal("calloc");
- for (i = 0; i < env->sc_ncpu; i++) {
+ for (i = 0; i < snmpd_env->sc_ncpu; i++) {
cp_time[i] = calloc(CPUSTATES, sizeof(int64_t));
cp_old[i] = calloc(CPUSTATES, sizeof(int64_t));
cp_diff[i] = calloc(CPUSTATES, sizeof(int64_t));
Index: trap.c
===================================================================
RCS file: /home/obsdcvs/src/usr.sbin/snmpd/trap.c,v
retrieving revision 1.26
diff -u -p -r1.26 trap.c
--- trap.c 5 Dec 2015 06:42:18 -0000 1.26
+++ trap.c 22 Oct 2016 22:22:57 -0000
@@ -39,8 +39,6 @@
#include "snmpd.h"
#include "mib.h"
-extern struct snmpd *env;
-
void
trap_init(void)
{
@@ -158,7 +156,7 @@ trap_send(struct ber_oid *oid, struct be
char ostr[SNMP_MAX_OID_STRLEN];
struct oid oa, ob;
- if (TAILQ_EMPTY(&env->sc_trapreceivers))
+ if (TAILQ_EMPTY(&snmpd_env->sc_trapreceivers))
return (0);
smi_scalar_oidlen(&uptime);
@@ -187,7 +185,7 @@ trap_send(struct ber_oid *oid, struct be
bzero(&ber, sizeof(ber));
ber.fd = -1;
- TAILQ_FOREACH(tr, &env->sc_trapreceivers, entry) {
+ TAILQ_FOREACH(tr, &snmpd_env->sc_trapreceivers, entry) {
if (tr->sa_oid != NULL && tr->sa_oid->bo_n) {
/* The trap receiver may want only a specified MIB */
bcopy(&tr->sa_oid->bo_id, &ob.o_oid,
@@ -203,7 +201,7 @@ trap_send(struct ber_oid *oid, struct be
}
cmn = tr->sa_community != NULL ?
- tr->sa_community : env->sc_trcommunity;
+ tr->sa_community : snmpd_env->sc_trcommunity;
/* SNMP header */
root = ber_add_sequence(NULL);
@@ -219,7 +217,7 @@ trap_send(struct ber_oid *oid, struct be
if (ber_get_writebuf(&ber, (void *)&ptr) > 0 &&
sendto(s, ptr, len, 0, (struct sockaddr *)&tr->ss,
tr->ss.ss_len) != -1) {
- env->sc_stats.snmp_outpkts++;
+ snmpd_env->sc_stats.snmp_outpkts++;
ret++;
}
Index: usm.c
===================================================================
RCS file: /home/obsdcvs/src/usr.sbin/snmpd/usm.c,v
retrieving revision 1.10
diff -u -p -r1.10 usm.c
--- usm.c 3 Oct 2016 12:19:59 -0000 1.10
+++ usm.c 22 Oct 2016 22:25:11 -0000
@@ -42,8 +42,6 @@
#include "snmpd.h"
#include "mib.h"
-extern struct snmpd *env;
-
SLIST_HEAD(, usmuser) usmuserlist;
const EVP_MD *usm_get_md(enum usmauth);
@@ -210,7 +208,7 @@ fail:
struct ber_element *
usm_decode(struct snmp_message *msg, struct ber_element *elm, const char
**errp)
{
- struct snmp_stats *stats = &env->sc_stats;
+ struct snmp_stats *stats = &snmpd_env->sc_stats;
off_t offs, offs2;
char *usmparams;
size_t len;
@@ -263,8 +261,8 @@ usm_decode(struct snmp_message *msg, str
goto done;
}
- if (enginelen != env->sc_engineid_len ||
- memcmp(engineid, env->sc_engineid, enginelen) != 0) {
+ if (enginelen != snmpd_env->sc_engineid_len ||
+ memcmp(engineid, snmpd_env->sc_engineid, enginelen) != 0) {
*errp = "unknown engine id";
msg->sm_usmerr = OIDVAL_usmErrEngineId;
stats->snmp_usmnosuchengine++;
@@ -273,7 +271,7 @@ usm_decode(struct snmp_message *msg, str
if (engine_boots != 0LL && engine_time != 0LL) {
now = snmpd_engine_time();
- if (engine_boots != env->sc_engine_boots ||
+ if (engine_boots != snmpd_env->sc_engine_boots ||
engine_time < (long long)(now - SNMP_MAX_TIMEWINDOW) ||
engine_time > (long long)(now + SNMP_MAX_TIMEWINDOW)) {
*errp = "out of time window";
@@ -372,11 +370,12 @@ usm_encode(struct snmp_message *msg, str
} else
saltlen = 0;
- msg->sm_engine_boots = (u_int32_t)env->sc_engine_boots;
+ msg->sm_engine_boots = (u_int32_t)snmpd_env->sc_engine_boots;
msg->sm_engine_time = (u_int32_t)snmpd_engine_time();
if ((a = ber_printf_elements(usm, "xdds",
- env->sc_engineid, env->sc_engineid_len, msg->sm_engine_boots,
- msg->sm_engine_time, msg->sm_username)) == NULL)
+ snmpd_env->sc_engineid, snmpd_env->sc_engineid_len,
+ msg->sm_engine_boots, msg->sm_engine_time,
+ msg->sm_username)) == NULL)
goto done;
if ((a = ber_add_nstring(a, digest, digestlen)) == NULL)
@@ -644,14 +643,15 @@ usm_passwd2key(const EVP_MD *md, char *p
/* Localize the key */
#ifdef DEBUG
- assert(env->sc_engineid_len <= SNMPD_MAXENGINEIDLEN);
+ assert(snmpd_env->sc_engineid_len <= SNMPD_MAXENGINEIDLEN);
#endif
memcpy(pwbuf, keybuf, dlen);
- memcpy(pwbuf + dlen, env->sc_engineid, env->sc_engineid_len);
- memcpy(pwbuf + dlen + env->sc_engineid_len, keybuf, dlen);
+ memcpy(pwbuf + dlen, snmpd_env->sc_engineid,
+ snmpd_env->sc_engineid_len);
+ memcpy(pwbuf + dlen + snmpd_env->sc_engineid_len, keybuf, dlen);
EVP_DigestInit(&ctx, md);
- EVP_DigestUpdate(&ctx, pwbuf, 2 * dlen + env->sc_engineid_len);
+ EVP_DigestUpdate(&ctx, pwbuf, 2 * dlen + snmpd_env->sc_engineid_len);
EVP_DigestFinal(&ctx, keybuf, &dlen);
EVP_MD_CTX_cleanup(&ctx);