From 28efdb08be427ae93da8928ad7bb57bc16cc92d6 Mon Sep 17 00:00:00 2001 From: eric Date: Fri, 14 Jun 2019 19:55:25 +0000 Subject: [PATCH] simplify the runq interface: - remove (unused) per-job callback - rename runq_schedule() to runq_schedule_at() and runq_delay() to runq_schedule() - remove unused runq_next() ok sunil@ gilles@ --- usr.sbin/smtpd/mta.c | 36 +++++++++++----------- usr.sbin/smtpd/mta_session.c | 16 +++++----- usr.sbin/smtpd/runq.c | 58 +++++++++++------------------------- usr.sbin/smtpd/smtpd.h | 11 ++++--- 4 files changed, 46 insertions(+), 75 deletions(-) diff --git a/usr.sbin/smtpd/mta.c b/usr.sbin/smtpd/mta.c index 6958384a115..76e0be8e86c 100644 --- a/usr.sbin/smtpd/mta.c +++ b/usr.sbin/smtpd/mta.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mta.c,v 1.227 2018/12/23 16:37:53 eric Exp $ */ +/* $OpenBSD: mta.c,v 1.228 2019/06/14 19:55:25 eric Exp $ */ /* * Copyright (c) 2008 Pierre-Yves Ritschard @@ -326,7 +326,7 @@ mta_imsg(struct mproc *p, struct imsg *imsg) if (route->flags & ROUTE_DISABLED) { log_info("smtp-out: Enabling route %s per admin request", mta_route_to_text(route)); - if (!runq_cancel(runq_route, NULL, route)) { + if (!runq_cancel(runq_route, route)) { log_warnx("warn: route not on runq"); fatalx("exiting"); } @@ -370,7 +370,7 @@ mta_imsg(struct mproc *p, struct imsg *imsg) case IMSG_CTL_MTA_SHOW_ROUTES: SPLAY_FOREACH(route, mta_route_tree, &routes) { - v = runq_pending(runq_route, NULL, route, &t); + v = runq_pending(runq_route, route, &t); (void)snprintf(buf, sizeof(buf), "%llu. %s %c%c%c%c nconn=%zu nerror=%d penalty=%d timeout=%s", (unsigned long long)route->id, @@ -1165,7 +1165,7 @@ mta_connect(struct mta_connector *c) if (c->flags & CONNECTOR_WAIT) { log_debug("debug: mta: cancelling connector timeout"); - runq_cancel(runq_connector, NULL, c); + runq_cancel(runq_connector, c); c->flags &= ~CONNECTOR_WAIT; } @@ -1257,7 +1257,7 @@ mta_connect(struct mta_connector *c) mta_connector_to_text(c), (unsigned long long) nextconn - time(NULL)); c->flags |= CONNECTOR_WAIT; - runq_schedule(runq_connector, nextconn, NULL, c); + runq_schedule_at(runq_connector, nextconn, c); return; } @@ -1336,12 +1336,12 @@ mta_route_disable(struct mta_route *route, int penalty, int reason) mta_route_to_text(route), delay); if (route->flags & ROUTE_DISABLED) - runq_cancel(runq_route, NULL, route); + runq_cancel(runq_route, route); else mta_route_ref(route); route->flags |= reason & ROUTE_DISABLED; - runq_schedule(runq_route, time(NULL) + delay, NULL, route); + runq_schedule(runq_route, delay, route); } static void @@ -1434,7 +1434,7 @@ mta_drain(struct mta_relay *r) log_debug("debug: mta: scheduling relay %s in %llus...", mta_relay_to_text(r), (unsigned long long) r->nextsource - time(NULL)); - runq_schedule(runq_relay, r->nextsource, NULL, r); + runq_schedule_at(runq_relay, r->nextsource, r); r->status |= RELAY_WAIT_CONNECTOR; mta_relay_ref(r); } @@ -1938,7 +1938,7 @@ mta_relay_show(struct mta_relay *r, struct mproc *p, uint32_t id, time_t t) SHOWSTATUS(RELAY_WAIT_CONNECTOR, "connector"); #undef SHOWSTATUS - if (runq_pending(runq_relay, NULL, r, &to)) + if (runq_pending(runq_relay, r, &to)) (void)snprintf(dur, sizeof(dur), "%s", duration_to_text(to - t)); else (void)strlcpy(dur, "-", sizeof(dur)); @@ -1957,7 +1957,7 @@ mta_relay_show(struct mta_relay *r, struct mproc *p, uint32_t id, time_t t) iter = NULL; while (tree_iter(&r->connectors, &iter, NULL, (void **)&c)) { - if (runq_pending(runq_connector, NULL, c, &to)) + if (runq_pending(runq_connector, c, &to)) (void)snprintf(dur, sizeof(dur), "%s", duration_to_text(to - t)); else (void)strlcpy(dur, "-", sizeof(dur)); @@ -2306,7 +2306,7 @@ mta_connector_free(struct mta_connector *c) if (c->flags & CONNECTOR_WAIT) { log_debug("debug: mta: cancelling timeout for %s", mta_connector_to_text(c)); - runq_cancel(runq_connector, NULL, c); + runq_cancel(runq_connector, c); } mta_source_unref(c->source); /* from constructor */ free(c); @@ -2351,7 +2351,7 @@ mta_route(struct mta_source *src, struct mta_host *dst) log_debug("debug: mta: mta_route_ref(): cancelling runq for route %s", mta_route_to_text(r)); r->flags &= ~(ROUTE_RUNQ | ROUTE_KEEPALIVE); - runq_cancel(runq_route, NULL, r); + runq_cancel(runq_route, r); r->refcount--; /* from mta_route_unref() */ } @@ -2406,7 +2406,7 @@ mta_route_unref(struct mta_route *r) if (sched > now) { r->flags |= ROUTE_RUNQ; - runq_schedule(runq_route, sched, NULL, r); + runq_schedule_at(runq_route, sched, r); r->refcount++; return; } @@ -2531,26 +2531,24 @@ mta_hoststat_update(const char *host, const char *error) { struct hoststat *hs = NULL; char buf[HOST_NAME_MAX+1]; - time_t tm; if (!lowercase(buf, host, sizeof buf)) return; - tm = time(NULL); hs = dict_get(&hoststat, buf); if (hs == NULL) { if ((hs = calloc(1, sizeof *hs)) == NULL) return; tree_init(&hs->deferred); - runq_schedule(runq_hoststat, tm+HOSTSTAT_EXPIRE_DELAY, NULL, hs); + runq_schedule(runq_hoststat, HOSTSTAT_EXPIRE_DELAY, hs); } (void)strlcpy(hs->name, buf, sizeof hs->name); (void)strlcpy(hs->error, error, sizeof hs->error); hs->tm = time(NULL); dict_set(&hoststat, buf, hs); - runq_cancel(runq_hoststat, NULL, hs); - runq_schedule(runq_hoststat, tm+HOSTSTAT_EXPIRE_DELAY, NULL, hs); + runq_cancel(runq_hoststat, hs); + runq_schedule(runq_hoststat, HOSTSTAT_EXPIRE_DELAY, hs); } void @@ -2614,5 +2612,5 @@ mta_hoststat_remove_entry(struct hoststat *hs) while (tree_poproot(&hs->deferred, NULL, NULL)) ; dict_pop(&hoststat, hs->name); - runq_cancel(runq_hoststat, NULL, hs); + runq_cancel(runq_hoststat, hs); } diff --git a/usr.sbin/smtpd/mta_session.c b/usr.sbin/smtpd/mta_session.c index 266c45e5674..1e07b1fe043 100644 --- a/usr.sbin/smtpd/mta_session.c +++ b/usr.sbin/smtpd/mta_session.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mta_session.c,v 1.116 2019/06/12 17:42:53 eric Exp $ */ +/* $OpenBSD: mta_session.c,v 1.117 2019/06/14 19:55:25 eric Exp $ */ /* * Copyright (c) 2008 Pierre-Yves Ritschard @@ -352,7 +352,7 @@ mta_free(struct mta_session *s) if (s->flags & MTA_HANGON) { log_debug("debug: mta: %p: cancelling hangon timer", s); - runq_cancel(hangon, NULL, s); + runq_cancel(hangon, s); } if (s->io) @@ -700,7 +700,7 @@ mta_enter_state(struct mta_session *s, int newstate) (long long)(s->relay->limits->sessdelay_keepalive - s->hangon)); s->flags |= MTA_HANGON; - runq_schedule(hangon, time(NULL) + 1, NULL, s); + runq_schedule(hangon, 1, s); break; } @@ -1045,9 +1045,8 @@ mta_response(struct mta_session *s, char *line) (long long int)s->relay->limits->sessdelay_transaction); s->hangon = s->relay->limits->sessdelay_transaction -1; s->flags |= MTA_HANGON; - runq_schedule(hangon, time(NULL) - + s->relay->limits->sessdelay_transaction, - NULL, s); + runq_schedule(hangon, + s->relay->limits->sessdelay_transaction, s); } else mta_enter_state(s, MTA_READY); @@ -1061,9 +1060,8 @@ mta_response(struct mta_session *s, char *line) (long long int)s->relay->limits->sessdelay_transaction); s->hangon = s->relay->limits->sessdelay_transaction -1; s->flags |= MTA_HANGON; - runq_schedule(hangon, time(NULL) - + s->relay->limits->sessdelay_transaction, - NULL, s); + runq_schedule(hangon, + s->relay->limits->sessdelay_transaction, s); } else mta_enter_state(s, MTA_READY); diff --git a/usr.sbin/smtpd/runq.c b/usr.sbin/smtpd/runq.c index ce8c67ef2a7..bbb9affa241 100644 --- a/usr.sbin/smtpd/runq.c +++ b/usr.sbin/smtpd/runq.c @@ -1,7 +1,7 @@ -/* $OpenBSD: runq.c,v 1.2 2015/01/20 17:37:54 deraadt Exp $ */ +/* $OpenBSD: runq.c,v 1.3 2019/06/14 19:55:25 eric Exp $ */ /* - * Copyright (c) 2013 Eric Faurot + * Copyright (c) 2013,2019 Eric Faurot * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -33,7 +33,6 @@ struct job { TAILQ_ENTRY(job) entry; time_t when; - void (*cb)(struct runq *, void *); void *arg; }; @@ -81,10 +80,7 @@ runq_timeout(int fd, short ev, void *arg) if (job->when > now) break; TAILQ_REMOVE(&runq->jobs, job, entry); - if (job->cb) - job->cb(runq, job->arg); - else - runq->cb(runq, job->arg); + runq->cb(runq, job->arg); free(job); } @@ -111,8 +107,16 @@ runq_init(struct runq **runqp, void (*cb)(struct runq *, void *)) } int -runq_schedule(struct runq *runq, time_t when, void (*cb)(struct runq *, void *), - void *arg) +runq_schedule(struct runq *runq, time_t delay, void *arg) +{ + time_t t; + + time(&t); + return runq_schedule_at(runq, t + delay, arg); +} + +int +runq_schedule_at(struct runq *runq, time_t when, void *arg) { struct job *job, *tmpjob; @@ -121,7 +125,6 @@ runq_schedule(struct runq *runq, time_t when, void (*cb)(struct runq *, void *), return (0); job->arg = arg; - job->cb = cb; job->when = when; TAILQ_FOREACH(tmpjob, &runq->jobs, entry) { @@ -141,20 +144,13 @@ runq_schedule(struct runq *runq, time_t when, void (*cb)(struct runq *, void *), } int -runq_delay(struct runq *runq, unsigned int delay, - void (*cb)(struct runq *, void *), void *arg) -{ - return runq_schedule(runq, time(NULL) + delay, cb, arg); -} - -int -runq_cancel(struct runq *runq, void (*cb)(struct runq *, void *), void *arg) +runq_cancel(struct runq *runq, void *arg) { struct job *job, *first; first = TAILQ_FIRST(&runq->jobs); TAILQ_FOREACH(job, &runq->jobs, entry) { - if (job->cb == cb && job->arg == arg) { + if (job->arg == arg) { TAILQ_REMOVE(&runq->jobs, job, entry); free(job); if (runq != active && job == first) { @@ -169,13 +165,12 @@ runq_cancel(struct runq *runq, void (*cb)(struct runq *, void *), void *arg) } int -runq_pending(struct runq *runq, void (*cb)(struct runq *, void *), void *arg, - time_t *when) +runq_pending(struct runq *runq, void *arg, time_t *when) { struct job *job; TAILQ_FOREACH(job, &runq->jobs, entry) { - if (job->cb == cb && job->arg == arg) { + if (job->arg == arg) { if (when) *when = job->when; return (1); @@ -184,22 +179,3 @@ runq_pending(struct runq *runq, void (*cb)(struct runq *, void *), void *arg, return (0); } - -int -runq_next(struct runq *runq, void (**cb)(struct runq *, void *), void **arg, - time_t *when) -{ - struct job *job; - - job = TAILQ_FIRST(&runq->jobs); - if (job == NULL) - return (0); - if (cb) - *cb = job->cb; - if (arg) - *arg = job->arg; - if (when) - *when = job->when; - - return (1); -} diff --git a/usr.sbin/smtpd/smtpd.h b/usr.sbin/smtpd/smtpd.h index 74e758f5665..87bc1d1dccb 100644 --- a/usr.sbin/smtpd/smtpd.h +++ b/usr.sbin/smtpd/smtpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: smtpd.h,v 1.623 2019/06/13 11:45:35 eric Exp $ */ +/* $OpenBSD: smtpd.h,v 1.624 2019/06/14 19:55:25 eric Exp $ */ /* * Copyright (c) 2008 Gilles Chehade @@ -1686,8 +1686,7 @@ void waitq_run(void *, void *); struct runq; int runq_init(struct runq **, void (*)(struct runq *, void *)); -int runq_schedule(struct runq *, time_t, void (*)(struct runq *, void *), void *); -int runq_delay(struct runq *, unsigned int, void (*)(struct runq *, void *), void *); -int runq_cancel(struct runq *, void (*)(struct runq *, void *), void *); -int runq_pending(struct runq *, void (*)(struct runq *, void *), void *, time_t *); -int runq_next(struct runq *, void (**)(struct runq *, void *), void **, time_t *); +int runq_schedule(struct runq *, time_t, void *); +int runq_schedule_at(struct runq *, time_t, void *); +int runq_cancel(struct runq *, void *); +int runq_pending(struct runq *, void *, time_t *); -- 2.20.1