commit 17f6a5ade0d4673e039561af2ab2bf205ea662e4
parent 4fb50e25a7d48039111602e17b884de6ce75213e
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Thu, 18 May 2023 21:07:01 +0000
Catch SIGINT and SIGTERM when fetching mail
If SIGINT or SIGTERM is recieved when fetching mail, return from
getmail() without attempting to fetch any more messages. This allows
main() to call pop_quit() so that messages marked for deletion are
actually deleted.
Note that, once RETR is issued, the POP3 connection can't be closed
cleanly until the whole message is read, so the current message is
handled before returning.
Diffstat:
1 file changed, 13 insertions(+), 3 deletions(-)
diff --git a/pop3.c b/pop3.c
@@ -1,4 +1,4 @@
-/* $Id: pop3.c,v 1.15 2023/05/14 15:29:00 jacob Exp $ */
+/* $Id: pop3.c,v 1.16 2023/05/18 21:07:01 jacob Exp $ */
/*
* Copyright (c) 2022, 2023 Jacob R. Edwards <jacob@jacobedwards.org>
@@ -61,6 +61,8 @@ static char *user;
static char *host;
static char **mailer;
+static int bail;
+
struct scanlisting {
char msg[4 + 1]; /* See LISTMAX */
int bytes;
@@ -478,6 +480,12 @@ mbox(FILE *fp, char *msg, char **unused)
return 0;
}
+void
+sigbail(int unused)
+{
+ bail = 1;
+}
+
int
getmail(FILE *fp, int delete, char **mailer)
{
@@ -492,14 +500,14 @@ getmail(FILE *fp, int delete, char **mailer)
fails = 0;
sendmail = mailer ? mail : mbox;
- for (i = 0; i < len; ++i) {
+ for (i = 0; i < len && !bail; ++i) {
if (sendmail(fp, listings[i].msg, mailer) ||
(delete && pop_dele(fp, listings[i].msg)))
++fails;
}
free(listings);
- return fails;
+ return fails + (len - i);
}
int
@@ -590,6 +598,8 @@ main(int argc, char *argv[])
if (stat(fp, stdout))
die("unable to stat");
} else {
+ signal(SIGINT, sigbail);
+ signal(SIGTERM, sigbail);
if (getmail(fp, delete, mailer)) {
pop_quit(fp);
die("unable to get mail");