From 3dc10527bb9d7d8b22369260976a897222e4297c Mon Sep 17 00:00:00 2001 From: Sunil Nimmagadda Date: Wed, 27 Aug 2014 11:49:41 +0500 Subject: Rename to differentiate setup and init operations. --- maildrop.c | 4 +++- pop3d.c | 2 +- pop3d.h | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/maildrop.c b/maildrop.c index 12861f7..5562195 100644 --- a/maildrop.c +++ b/maildrop.c @@ -48,7 +48,7 @@ static size_t expand(char *, const char *, size_t, struct passwd *); static struct mdrop m; pid_t -maildrop_init(uint32_t session_id, int pair[2], struct passwd *pw, +maildrop_setup(uint32_t session_id, int pair[2], struct passwd *pw, int type, const char *path) { struct imsgev iev_session; @@ -176,6 +176,8 @@ session_imsgev(struct imsgev *iev, int code, struct imsg *imsg) switch (code) { case IMSGEV_IMSG: switch (imsg->hdr.type) { + case IMSG_MAILDROP_INIT: + break; case IMSG_MAILDROP_UPDATE: update(iev, imsg, mb); break; diff --git a/pop3d.c b/pop3d.c index cbd3ea3..26d048c 100644 --- a/pop3d.c +++ b/pop3d.c @@ -172,7 +172,7 @@ authenticate(struct imsgev *iev, struct imsg *imsg) if ((pw = getpwnam(req->user)) == NULL) fatalx("authenticate: getpwnam"); - if (maildrop_init(imsg->hdr.peerid, pair, pw, mtype, mpath) == -1) { + if (maildrop_setup(imsg->hdr.peerid, pair, pw, mtype, mpath) == -1) { logit(LOG_INFO, "%u: unable to fork maildrop process", imsg->hdr.peerid); pair[0] = -1; diff --git a/pop3d.h b/pop3d.h index 1b70163..9d74961 100644 --- a/pop3d.h +++ b/pop3d.h @@ -156,7 +156,7 @@ int session_cmp(struct session *, struct session *); SPLAY_PROTOTYPE(session_tree, session, entry, session_cmp); /* maildrop.c */ -pid_t maildrop_init(uint32_t, int [2], struct passwd *, int, const char *); +pid_t maildrop_setup(uint32_t, int [2], struct passwd *, int, const char *); /* util.c */ void set_nonblocking(int); -- cgit v1.2.3 From c2736fe4b58830f0ab987e4c1f0c1eadca6d7306 Mon Sep 17 00:00:00 2001 From: Sunil Nimmagadda Date: Wed, 27 Aug 2014 12:46:39 +0500 Subject: mtype, mpath are accessible to maildrop, simplify setup parameters. --- maildrop.c | 11 ++++++----- pop3d.c | 6 +++--- pop3d.h | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/maildrop.c b/maildrop.c index 5562195..9d7b221 100644 --- a/maildrop.c +++ b/maildrop.c @@ -48,8 +48,7 @@ static size_t expand(char *, const char *, size_t, struct passwd *); static struct mdrop m; pid_t -maildrop_setup(uint32_t session_id, int pair[2], struct passwd *pw, - int type, const char *path) +maildrop_setup(uint32_t session_id, int pair[2], struct passwd *pw) { struct imsgev iev_session; struct event ev_sigint, ev_sigterm; @@ -59,6 +58,8 @@ maildrop_setup(uint32_t session_id, int pair[2], struct passwd *pw, pid_t pid; mode_t old_mask; int fd, flags, res = -1; + extern int mtype; + extern const char *mpath; if ((pid = fork()) != 0) return (pid); @@ -70,14 +71,14 @@ maildrop_setup(uint32_t session_id, int pair[2], struct passwd *pw, close(pair[0]); setproctitle("maildrop"); - if ((mb = m_backend_lookup(type)) == NULL) + if ((mb = m_backend_lookup(mtype)) == NULL) fatalx("maildrop: invalid backend"); - if (expand(buf, path, sizeof(buf), pw) >= sizeof(buf)) + if (expand(buf, mpath, sizeof(buf), pw) >= sizeof(buf)) fatalx("maildrop: path truncation"); flags = O_CREAT; - if (type == M_MBOX) + if (mtype == M_MBOX) flags |= O_RDWR; else flags |= O_RDONLY; diff --git a/pop3d.c b/pop3d.c index 26d048c..dd71cb9 100644 --- a/pop3d.c +++ b/pop3d.c @@ -47,8 +47,8 @@ static enum m_type m_type(const char *); static void usage(void); static struct imsgev iev_pop3e; -static const char *mpath; -static int mtype = M_MBOX; +const char *mpath; +int mtype = M_MBOX; int main(int argc, char *argv[]) @@ -172,7 +172,7 @@ authenticate(struct imsgev *iev, struct imsg *imsg) if ((pw = getpwnam(req->user)) == NULL) fatalx("authenticate: getpwnam"); - if (maildrop_setup(imsg->hdr.peerid, pair, pw, mtype, mpath) == -1) { + if (maildrop_setup(imsg->hdr.peerid, pair, pw) == -1) { logit(LOG_INFO, "%u: unable to fork maildrop process", imsg->hdr.peerid); pair[0] = -1; diff --git a/pop3d.h b/pop3d.h index 9d74961..09a07f6 100644 --- a/pop3d.h +++ b/pop3d.h @@ -156,7 +156,7 @@ int session_cmp(struct session *, struct session *); SPLAY_PROTOTYPE(session_tree, session, entry, session_cmp); /* maildrop.c */ -pid_t maildrop_setup(uint32_t, int [2], struct passwd *, int, const char *); +pid_t maildrop_setup(uint32_t, int [2], struct passwd *); /* util.c */ void set_nonblocking(int); -- cgit v1.2.3 From b16316e3bc0bdb5be4e4a46ae405d5ee191bc080 Mon Sep 17 00:00:00 2001 From: Sunil Nimmagadda Date: Wed, 27 Aug 2014 14:24:56 +0500 Subject: Give session a chance to init its imsgev. Let session initiate the process of MAILDROP_INIT. Maildrop now sets up itself and waits for MAILDROP_INIT from session. This would now eliminate the doubt in the earlier setup when priv process forks and the child gets cpu slice first before parent which sends MAILDROP_INIT to an uninitialized imsgev. --- maildrop.c | 83 ++++++++++++++++++++++++++++++++++++-------------------------- session.c | 4 ++- 2 files changed, 51 insertions(+), 36 deletions(-) diff --git a/maildrop.c b/maildrop.c index 9d7b221..97c9b9b 100644 --- a/maildrop.c +++ b/maildrop.c @@ -33,6 +33,7 @@ #include "pop3d.h" static void session_imsgev(struct imsgev *, int, struct imsg *); +static void maildrop_init(struct imsgev *, struct imsg *, struct m_backend *); static void update(struct imsgev *, struct imsg *, struct m_backend *); static void retr(struct imsgev *, struct imsg *, struct m_backend *); static void dele(struct imsgev *, struct imsg *, struct m_backend *); @@ -52,14 +53,9 @@ maildrop_setup(uint32_t session_id, int pair[2], struct passwd *pw) { struct imsgev iev_session; struct event ev_sigint, ev_sigterm; - struct stats stats; struct m_backend *mb; - char buf[MAXPATHLEN]; pid_t pid; - mode_t old_mask; - int fd, flags, res = -1; extern int mtype; - extern const char *mpath; if ((pid = fork()) != 0) return (pid); @@ -74,42 +70,12 @@ maildrop_setup(uint32_t session_id, int pair[2], struct passwd *pw) if ((mb = m_backend_lookup(mtype)) == NULL) fatalx("maildrop: invalid backend"); - if (expand(buf, mpath, sizeof(buf), pw) >= sizeof(buf)) - fatalx("maildrop: path truncation"); - - flags = O_CREAT; - if (mtype == M_MBOX) - flags |= O_RDWR; - else - flags |= O_RDONLY; - - old_mask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH); - if ((fd = open(buf, flags)) == -1) - logit(LOG_CRIT, "%zu: failed to open %s", session_id , buf); - - if (fd != -1) { - m.fd = fd; - res = mb->init(&m, &stats.nmsgs, &stats.sz); - } - - umask(old_mask); event_init(); signal_set(&ev_sigint, SIGINT, sig_handler, NULL); signal_set(&ev_sigterm, SIGTERM, sig_handler, NULL); signal_add(&ev_sigint, NULL); signal_add(&ev_sigterm, NULL); imsgev_init(&iev_session, pair[1], mb, session_imsgev, needfd); - - if (res == 0) { - imsgev_xcompose(&iev_session, IMSG_MAILDROP_INIT, session_id, - 0, -1, &stats, sizeof(struct stats), "maildrop_init"); - } else { - logit(LOG_CRIT, "%zu: maildrop init failed %s", - session_id, buf); - imsgev_xcompose(&iev_session, IMSG_MAILDROP_INIT, session_id, - 0, -1, NULL, 0, "maildrop_init"); - } - if (event_dispatch() < 0) fatal("event_dispatch"); @@ -178,6 +144,7 @@ session_imsgev(struct imsgev *iev, int code, struct imsg *imsg) case IMSGEV_IMSG: switch (imsg->hdr.type) { case IMSG_MAILDROP_INIT: + maildrop_init(iev, imsg, mb); break; case IMSG_MAILDROP_UPDATE: update(iev, imsg, mb); @@ -214,6 +181,52 @@ session_imsgev(struct imsgev *iev, int code, struct imsg *imsg) } } +static void +maildrop_init(struct imsgev *iev, struct imsg *imsg, struct m_backend *mb) +{ + struct stats stats; + char buf[MAXPATHLEN]; + struct passwd *pw; + extern const char *mpath; + extern int mtype; + mode_t old_mask; + int fd, flags, res = -1; + uint32_t session_id = imsg->hdr.peerid; + const char *user = imsg->data; + + if ((pw = getpwnam(user)) == NULL) + fatalx("authenticate: getpwnam"); + + if (expand(buf, mpath, sizeof(buf), pw) >= sizeof(buf)) + fatalx("maildrop: path truncation"); + + flags = O_CREAT; + if (mtype == M_MBOX) + flags |= O_RDWR; + else + flags |= O_RDONLY; + + old_mask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH); + if ((fd = open(buf, flags)) == -1) + logit(LOG_CRIT, "%zu: failed to open %s", session_id , buf); + + if (fd != -1) { + m.fd = fd; + res = mb->init(&m, &stats.nmsgs, &stats.sz); + } + + umask(old_mask); + if (res == 0) { + imsgev_xcompose(iev, IMSG_MAILDROP_INIT, session_id, + 0, -1, &stats, sizeof(struct stats), "maildrop_init"); + } else { + logit(LOG_CRIT, "%zu: maildrop init failed %s", + session_id, buf); + imsgev_xcompose(iev, IMSG_MAILDROP_INIT, session_id, + 0, -1, NULL, 0, "maildrop_init"); + } +} + static void update(struct imsgev *iev, struct imsg *imsg, struct m_backend *mb) { diff --git a/session.c b/session.c index da90c78..102c1ef 100644 --- a/session.c +++ b/session.c @@ -436,7 +436,9 @@ get_list(struct session *s, unsigned int i, int uidl) void session_imsgev_init(struct session *s, int fd) { - imsgev_init(&s->iev_maildrop, fd, s, maildrop_imsgev, needfd); + imsgev_init(&s->iev_maildrop, fd, NULL, maildrop_imsgev, needfd); + imsgev_xcompose(&s->iev_maildrop, IMSG_MAILDROP_INIT, s->id, 0, + -1, s->user, sizeof(s->user), "session_imsgev_init"); } static void -- cgit v1.2.3 From 97a44517495ea19c5052c0cbb4c845fe1172ef9b Mon Sep 17 00:00:00 2001 From: Sunil Nimmagadda Date: Thu, 28 Aug 2014 14:16:58 +0500 Subject: space nit. --- session.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/session.c b/session.c index 102c1ef..a69b9b7 100644 --- a/session.c +++ b/session.c @@ -133,7 +133,7 @@ session_close(struct session *s, int flush) entry = SPLAY_REMOVE(session_tree, &sessions, s); if (entry == NULL) { - /* STARTTLS session was in progress and got interrupted */ + /* STARTTLS session was in progress and got interrupted */ logit(LOG_DEBUG, "%u: not in tree", s->id); entry = s; } -- cgit v1.2.3 From 2bad8d69c0e76dcbeb0fccef0592822a40b17891 Mon Sep 17 00:00:00 2001 From: Sunil Nimmagadda Date: Thu, 28 Aug 2014 15:46:14 +0500 Subject: Shorten. --- pop3d.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pop3d.h b/pop3d.h index 09a07f6..8032fdf 100644 --- a/pop3d.h +++ b/pop3d.h @@ -66,7 +66,7 @@ struct mdrop { } e; size_t nmsgs; size_t sz; - struct msg **msgs_index; /* random access to msgs */ + struct msg **msgs_index; /* random access msgs */ int fd; }; -- cgit v1.2.3