]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
mlmmj-maintd: pass the mailfile to resend via file descriptor
authorBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 8 Mar 2023 10:37:49 +0000 (11:37 +0100)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 8 Mar 2023 10:37:49 +0000 (11:37 +0100)
This reduces the memory allocation and also plug some memory leak

src/mlmmj-maintd.c
src/mlmmj-send.c
tests/mlmmj-maintd.sh

index 31e89f2417f53d2c53d89f2c58e18c70ca3914fa..f0c6121d822c7542b4322b74a904482eeb5d2ec9 100644 (file)
@@ -261,7 +261,7 @@ resend_requeue(struct ml *ml, const char *mlmmjsend, int logfd)
 {
        DIR *queuedir;
        struct dirent *dp;
-       char *archivefilename, *subfilename, *subnewname;
+       char *archivefilename, *subnewname;
        struct stat st;
        time_t t;
        bool fromrequeuedir;
@@ -289,53 +289,48 @@ resend_requeue(struct ml *ml, const char *mlmmjsend, int logfd)
                    unlinkat(fd, dp->d_name, AT_REMOVEDIR) == 0)
                        continue;
 
+               int requeuefd = openat(fd, dp->d_name, O_DIRECTORY);
+
                xasprintf(&archivefilename, "archive/%s", dp->d_name);
+               int archivefd = openat(ml->fd, archivefilename, O_RDONLY);
+               free(archivefilename);
 
                /* Explicitly initialize for each mail we examine */
                fromrequeuedir = false;
 
-               if(faccessat(ml->fd, archivefilename, F_OK, 0) < 0) {
-                       /* Might be it's just not moved to the archive
-                        * yet because it's still getting sent, so just
-                        * continue
-                        */
-                       free(archivefilename);
-
+               if (archivefd == -1) {
                        /* If the list is set not to archive we want to look
                         * in /requeue/ for a mailfile
                         */
-                       xasprintf(&archivefilename, "requeue/%s/mailfile",
-                           dp->d_name);
-                       if(faccessat(ml->fd, archivefilename, F_OK, 0) < 0) {
-                               free(archivefilename);
+                       archivefd = openat(requeuefd, "mailfile", O_RDONLY);
+                       if (archivefd == -1) {
+                               close(requeuefd);
                                continue;
                        }
                        fromrequeuedir = true;
                }
-               xasprintf(&subfilename, "%s/subscribers", dp->d_name);
-               if(faccessat(fd, subfilename, F_OK, 0) < 0) {
-                       if (fromrequeuedir) {
-                               if (unlinkat(ml->fd, archivefilename, 0) == -1) {
-                                       log(" - Cound not remove %s: %s\n",
-                                           archivefilename, strerror(errno));
-                                       ret = false;
-                               }
+               int subfd = openat(requeuefd, "subscribers", O_RDONLY);
+               if (subfd == -1) {
+                       if (fromrequeuedir && unlinkat(requeuefd, "mailfile", 0) == -1) {
+                               log(" - Cound not remove requeue/%s/mailfile: %s\n",
+                                   dp->d_name, strerror(errno));
+                               ret = false;
                        }
-                       free(archivefilename);
-                       free(subfilename);
+                       close(requeuefd);
                        continue;
                }
-
-               int subfd = openat(fd, subfilename, O_RDONLY);
-               unlinkat(fd, subfilename, 0);
+               unlinkat(requeuefd, "subscribers", 0);
 
                xasprintf(&subnewname, "%d", subfd);
+               xasprintf(&archivefilename, "%d", archivefd);
                exec_and_wait(mlmmjsend, "-l", "3", "-L", ml->dir, "-m",
                    archivefilename, "-s", subnewname, "-a", "-D", NULL);
                free(subnewname);
+               free(archivefilename);
                if (fromrequeuedir)
-                       unlinkat(ml->fd, archivefilename, 0);
+                       unlinkat(requeuefd, "mailfile", 0);
                close(subfd);
+               close(requeuefd);
        }
 
        closedir(queuedir);
index b601573fbe3ecfcf909d934d4c7d57341520c7be..0536659a033a506db7a90ab0d3a59a95e303184b 100644 (file)
@@ -486,8 +486,14 @@ int main(int argc, char **argv)
 
        /* initialize file with mail to send */
 
-       if ((mailfd = open(mailfilename, O_RDWR)) == -1 ||
-           !lock(mailfd, true)) {
+       mailfd = strtoim(mailfilename, 0, INT_MAX, &errp);
+       if (errp == NULL) {
+               /* cannot delete a mail sent from file descriptor */
+               deletewhensent = false;
+       }
+
+       if (errp != NULL && ((mailfd = open(mailfilename, O_RDWR)) == -1 ||
+           !lock(mailfd, true))) {
                if (ml.dir != NULL && mailfd == -1) {
                        if ((mailfd = openat(ml.fd, mailfilename, O_RDWR)) == -1 ||
                                !lock(mailfd, true)) {
index e0deb63f7f6e046de4d7da984ff3508ab4384f4a..54fd4598351e3c718788cb8091557006f81de482 100755 (executable)
@@ -780,7 +780,7 @@ EOF
        atf_check $mlmmjmaintd -L list -F
        cat > expect1.txt <<EOF
 EHLO heloname\r
-MAIL FROM:<test+bounces-3-user1=test1@test>\r
+MAIL FROM:<test+bounces-user1=test1@test>\r
 RCPT TO:<user1@test1>\r
 DATA\r
 From: plop\r
@@ -792,7 +792,7 @@ body
 .\r
 QUIT\r
 EOF
-       atf_check -o file:expect1.txt sed -e "/^Message-ID:/d; /^Date:/d; s/confsub-.*@mlmmjtest/confsub-@mlmmjtest/g" mail-1.txt
+       atf_check -o file:expect1.txt sed -e "/^Message-ID:/d; /^Date:/d; s/bounces-.*-/bounces-/g" mail-1.txt
        atf_check -s exit:1 test -f mail-2.txt
        atf_check -s exit:1 test -f list/requeue/3/mailfile
 cat > list/archive/1 <<EOF
@@ -805,7 +805,7 @@ EOF
        # now with archive
        cat > expect2.txt <<EOF
 EHLO heloname\r
-MAIL FROM:<test+bounces-1-user1=test1@test>\r
+MAIL FROM:<test+bounces-user1=test1@test>\r
 RCPT TO:<user1@test1>\r
 DATA\r
 From: plop\r
@@ -815,7 +815,7 @@ Subject: test
 body\r
 \r
 .\r
-MAIL FROM:<test+bounces-1-user2=test2@test>\r
+MAIL FROM:<test+bounces-user2=test2@test>\r
 RCPT TO:<user2@test2>\r
 DATA\r
 From: plop\r
@@ -825,7 +825,7 @@ Subject: test
 body\r
 \r
 .\r
-MAIL FROM:<test+bounces-1-user3=test3@test>\r
+MAIL FROM:<test+bounces-user3=test3@test>\r
 RCPT TO:<user3@test3>\r
 DATA\r
 From: plop\r
@@ -838,7 +838,7 @@ body
 QUIT\r
 EOF
        atf_check $mlmmjmaintd -L list -F
-       atf_check -o file:expect2.txt sed -e "/^Message-ID:/d; /^Date:/d; s/confsub-.*@mlmmjtest/confsub-@mlmmjtest/g" mail-2.txt
+       atf_check -o file:expect2.txt sed -e "/^Message-ID:/d; /^Date:/d; s/bounces-.*-/bounces-/g" mail-2.txt
        atf_check -s exit:1 test -f mail-4.txt
        atf_check -s exit:0 test -f list/archive/1
 }