summaryrefslogtreecommitdiff
path: root/usr.sbin/smtpd/runq.c
blob: 30fd9e17f67b4d9bd7a782611481477c5f48a0c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*	$OpenBSD: runq.c,v 1.1 2013/07/19 21:14:52 eric Exp $	*/

/*
 * Copyright (c) 2013 Eric Faurot <eric@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/queue.h>
#include <sys/tree.h>
#include <sys/uio.h>

#include <imsg.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "smtpd.h"

struct job {
	TAILQ_ENTRY(job)	 entry;
	time_t			 when;
	void			(*cb)(struct runq *, void *);
	void			*arg;
};

struct runq {
	TAILQ_HEAD(, job)	 jobs;
	void			(*cb)(struct runq *, void *);
	struct event		 ev;
};

static void runq_timeout(int, short, void *);

static struct runq *active;

static void
runq_reset(struct runq *runq)
{
	struct timeval	 tv;
	struct job	*job;
	time_t		 now;

	job = TAILQ_FIRST(&runq->jobs);
	if (job == NULL)
		return;

	now = time(NULL);
	if (job->when <= now)
		tv.tv_sec = 0;
	else
		tv.tv_sec = job->when - now;
	tv.tv_usec = 0;
	evtimer_add(&runq->ev, &tv);
}

static void
runq_timeout(int fd, short ev, void *arg)
{
	struct runq	*runq = arg;
	struct job	*job;
	time_t		 now;

	active = runq;
	now = time(NULL);

	while((job = TAILQ_FIRST(&runq->jobs))) {
		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);
		free(job);
	}

	active = NULL;
	runq_reset(runq);
}

int
runq_init(struct runq **runqp, void (*cb)(struct runq *, void *))
{
	struct runq	*runq;

	runq = malloc(sizeof(*runq));
	if (runq == NULL)
		return (0);

	runq->cb = cb;
	TAILQ_INIT(&runq->jobs);
	evtimer_set(&runq->ev, runq_timeout, runq);

	*runqp = runq;

	return (1);
}

int
runq_schedule(struct runq *runq, time_t when, void (*cb)(struct runq *, void *),
    void *arg)
{
	struct job	*job, *tmpjob;

	job = malloc(sizeof(*job));
	if (job == NULL)
		return (0);

	job->arg = arg;
	job->cb = cb;
	job->when = when;

	TAILQ_FOREACH(tmpjob, &runq->jobs, entry) {
		if (tmpjob->when > job->when) {
			TAILQ_INSERT_BEFORE(tmpjob, job, entry);
			goto done;
		}
	}
	TAILQ_INSERT_TAIL(&runq->jobs, job, entry);

    done:
	if (runq != active && job == TAILQ_FIRST(&runq->jobs)) {
		evtimer_del(&runq->ev);
		runq_reset(runq);
	}
	return (1);
}

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)
{
	struct job	*job, *first;

	first = TAILQ_FIRST(&runq->jobs);
	TAILQ_FOREACH(job, &runq->jobs, entry) {
		if (job->cb == cb && job->arg == arg) {
			TAILQ_REMOVE(&runq->jobs, job, entry);
			free(job);
			if (runq != active && job == first) {
				evtimer_del(&runq->ev);
				runq_reset(runq);
			}
			return (1);
		}
	}

	return (0);
}

int
runq_pending(struct runq *runq, void (*cb)(struct runq *, void *), void *arg,
    time_t *when)
{
	struct job	*job;

	TAILQ_FOREACH(job, &runq->jobs, entry) {
		if (job->cb == cb && job->arg == arg) {
			if (when)
				*when = job->when;
			return (1);
		}
	}

	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);
}