summaryrefslogtreecommitdiff
path: root/usr.sbin
diff options
context:
space:
mode:
Diffstat (limited to 'usr.sbin')
-rw-r--r--usr.sbin/smtpd/control.c31
-rw-r--r--usr.sbin/smtpd/queue.c9
-rw-r--r--usr.sbin/smtpd/scheduler.c5
-rw-r--r--usr.sbin/smtpd/smtp.c8
-rw-r--r--usr.sbin/smtpd/smtpd.h3
-rw-r--r--usr.sbin/smtpd/util.c15
6 files changed, 22 insertions, 49 deletions
diff --git a/usr.sbin/smtpd/control.c b/usr.sbin/smtpd/control.c
index 11a886c94f5..c0b34978dd0 100644
--- a/usr.sbin/smtpd/control.c
+++ b/usr.sbin/smtpd/control.c
@@ -1,6 +1,7 @@
-/* $OpenBSD: control.c,v 1.71 2012/08/25 10:23:11 gilles Exp $ */
+/* $OpenBSD: control.c,v 1.72 2012/08/25 22:03:26 gilles Exp $ */
/*
+ * Copyright (c) 2012 Gilles Chehade <gilles@openbsd.org>
* Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
*
@@ -64,7 +65,7 @@ struct ctl_connlist ctl_conns;
static struct stat_backend *stat_backend = NULL;
extern const char *backend_stat;
-static size_t sessions;
+#define CONTROL_FD_RESERVE 5
void
control_imsg(struct imsgev *iev, struct imsg *imsg)
@@ -240,19 +241,12 @@ control_shutdown(void)
void
control_listen(void)
{
- int avail = availdesc();
-
if (listen(control_state.fd, CONTROL_BACKLOG) == -1)
fatal("control_listen");
- avail--;
event_set(&control_state.ev, control_state.fd, EV_READ|EV_PERSIST,
control_accept, NULL);
event_add(&control_state.ev, NULL);
-
- /* guarantee 2 fds to each accepted client */
- if ((env->sc_maxconn = avail / 2) < 1)
- fatalx("control_listen: fd starvation");
}
void
@@ -270,8 +264,13 @@ control_accept(int listenfd, short event, void *arg)
struct sockaddr_un sun;
struct ctl_conn *c;
+ if (getdtablesize() - getdtablecount() < CONTROL_FD_RESERVE)
+ goto pause;
+
len = sizeof(sun);
if ((connfd = accept(listenfd, (struct sockaddr *)&sun, &len)) == -1) {
+ if (errno == ENFILE || errno == EMFILE)
+ goto pause;
if (errno == EINTR || errno == ECONNABORTED)
return;
fatal("control_accept: accept");
@@ -290,11 +289,11 @@ control_accept(int listenfd, short event, void *arg)
TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
stat_backend->increment("control.session", 1);
+ return;
- if (++sessions >= env->sc_maxconn) {
- log_warnx("ctl client limit hit, disabling new connections");
- event_del(&control_state.ev);
- }
+pause:
+ log_warnx("ctl client limit hit, disabling new connections");
+ event_del(&control_state.ev);
}
struct ctl_conn *
@@ -326,8 +325,10 @@ control_close(int fd)
stat_backend->decrement("control.session", 1);
- if (--sessions < env->sc_maxconn &&
- !event_pending(&control_state.ev, EV_READ, NULL)) {
+ if (getdtablesize() - getdtablecount() < CONTROL_FD_RESERVE)
+ return;
+
+ if (!event_pending(&control_state.ev, EV_READ, NULL)) {
log_warnx("re-enabling ctl connections");
event_add(&control_state.ev, NULL);
}
diff --git a/usr.sbin/smtpd/queue.c b/usr.sbin/smtpd/queue.c
index ce0768e18ef..fa923d11b24 100644
--- a/usr.sbin/smtpd/queue.c
+++ b/usr.sbin/smtpd/queue.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: queue.c,v 1.132 2012/08/25 10:23:12 gilles Exp $ */
+/* $OpenBSD: queue.c,v 1.133 2012/08/25 22:03:26 gilles Exp $ */
/*
* Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org>
@@ -396,14 +396,7 @@ queue(void)
signal(SIGPIPE, SIG_IGN);
signal(SIGHUP, SIG_IGN);
- /*
- * queue opens fds for four purposes: smtp, mta, mda, and bounces.
- * Therefore, use all available fd space and set the maxconn (=max
- * session count for mta and mda) to a quarter of this value.
- */
fdlimit(1.0);
- if ((env->sc_maxconn = availdesc() / 4) < 1)
- fatalx("queue: fd starvation");
config_pipes(peers, nitems(peers));
config_peers(peers, nitems(peers));
diff --git a/usr.sbin/smtpd/scheduler.c b/usr.sbin/smtpd/scheduler.c
index ba6956c0938..541edbcf090 100644
--- a/usr.sbin/smtpd/scheduler.c
+++ b/usr.sbin/smtpd/scheduler.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: scheduler.c,v 1.18 2012/08/25 10:23:12 gilles Exp $ */
+/* $OpenBSD: scheduler.c,v 1.19 2012/08/25 22:03:26 gilles Exp $ */
/*
* Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org>
@@ -254,10 +254,7 @@ scheduler(void)
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
fatal("scheduler: cannot drop privileges");
- /* see fdlimit()-related comment in queue.c */
fdlimit(1.0);
- if ((env->sc_maxconn = availdesc() / 4) < 1)
- fatalx("scheduler: fd starvation");
env->sc_scheduler = scheduler_backend_lookup(backend_scheduler);
if (env->sc_scheduler == NULL)
diff --git a/usr.sbin/smtpd/smtp.c b/usr.sbin/smtpd/smtp.c
index e3580168c3d..14405eb767f 100644
--- a/usr.sbin/smtpd/smtp.c
+++ b/usr.sbin/smtpd/smtp.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: smtp.c,v 1.107 2012/08/25 21:33:33 gilles Exp $ */
+/* $OpenBSD: smtp.c,v 1.108 2012/08/25 22:03:26 gilles Exp $ */
/*
* Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org>
@@ -356,9 +356,7 @@ smtp(void)
signal(SIGPIPE, SIG_IGN);
signal(SIGHUP, SIG_IGN);
- /* Initial limit for use by IMSG_SMTP_ENQUEUE, will be tuned later once
- * the listening sockets arrive. */
- env->sc_maxconn = availdesc() / 2;
+ fdlimit(1.0);
config_pipes(peers, nitems(peers));
config_peers(peers, nitems(peers));
@@ -374,7 +372,6 @@ static void
smtp_setup_events(void)
{
struct listener *l;
- int avail = availdesc();
TAILQ_FOREACH(l, env->sc_listeners, entry) {
log_debug("smtp: listen on %s port %d flags 0x%01x"
@@ -390,7 +387,6 @@ smtp_setup_events(void)
event_add(&l->ev, NULL);
ssl_setup(l);
- avail--;
}
log_debug("smtp: will accept at most %d clients",
diff --git a/usr.sbin/smtpd/smtpd.h b/usr.sbin/smtpd/smtpd.h
index 2ac355dc3b6..c4495238e04 100644
--- a/usr.sbin/smtpd/smtpd.h
+++ b/usr.sbin/smtpd/smtpd.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: smtpd.h,v 1.336 2012/08/25 21:33:33 gilles Exp $ */
+/* $OpenBSD: smtpd.h,v 1.337 2012/08/25 22:03:26 gilles Exp $ */
/*
* Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org>
@@ -582,7 +582,6 @@ struct smtpd {
uint32_t sc_flags;
struct timeval sc_qintval;
int sc_qexpire;
- uint32_t sc_maxconn;
struct event sc_ev;
int *sc_pipes[PROC_COUNT]
[PROC_COUNT];
diff --git a/usr.sbin/smtpd/util.c b/usr.sbin/smtpd/util.c
index bbf8e013cc0..0435c4c6f57 100644
--- a/usr.sbin/smtpd/util.c
+++ b/usr.sbin/smtpd/util.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: util.c,v 1.71 2012/08/19 14:16:58 chl Exp $ */
+/* $OpenBSD: util.c,v 1.72 2012/08/25 22:03:26 gilles Exp $ */
/*
* Copyright (c) 2000,2001 Markus Friedl. All rights reserved.
@@ -869,19 +869,6 @@ fdlimit(double percent)
fatal("fdlimit: setrlimit");
}
-int
-availdesc(void)
-{
- int avail;
-
- avail = getdtablesize();
- avail -= 3; /* stdin, stdout, stderr */
- avail -= PROC_COUNT; /* imsg channels */
- avail -= 5; /* safety buffer */
-
- return (avail);
-}
-
void
session_socket_blockmode(int fd, enum blockmodes bm)
{