commit 129357b0a0eb10ec2b714ad198f63d9d016b8d59
parent 83e8e3215d2f664d6bb369562fcabb427fb18b5a
Author: Rolf Eike Beer <eike@sf-mail.de>
Date: Fri, 6 Mar 2020 16:33:59 +0100
qmail-local: close race window when creating file in tmp/
The code checked if a file with the same name already exists and then tries to
create it. It properly checks if the latter fails and retries later, but this is
totally unnecessary: it could just try to create it, and handle it at this point
if the file already exists. This also saves one stat() syscall per local
delivery.
Diffstat:
1 file changed, 11 insertions(+), 9 deletions(-)
diff --git a/qmail-local.c b/qmail-local.c
@@ -84,7 +84,6 @@ char *dir;
char myhost[64];
char *s;
int loop;
- struct stat st;
int fd;
substdio ss;
substdio ssout;
@@ -102,18 +101,21 @@ char *dir;
s += fmt_ulong(s,time); *s++ = '.';
s += fmt_ulong(s,pid); *s++ = '.';
s += fmt_strn(s,myhost,sizeof(myhost)); *s++ = 0;
- if (stat(fntmptph,&st) == -1) if (errno == error_noent) break;
- /* really should never get to this point */
- if (loop == 2) _exit(1);
- sleep(2);
+ alarm(86400);
+ fd = open_excl(fntmptph);
+ if (fd >= 0)
+ break;
+ if (errno == error_exist) {
+ /* really should never get to this point */
+ if (loop == 2) _exit(1);
+ sleep(2);
+ } else {
+ _exit(1);
+ }
}
str_copy(fnnewtph,fntmptph);
byte_copy(fnnewtph,3,"new");
- alarm(86400);
- fd = open_excl(fntmptph);
- if (fd == -1) _exit(1);
-
substdio_fdbuf(&ss,read,0,buf,sizeof(buf));
substdio_fdbuf(&ssout,write,fd,outbuf,sizeof(outbuf));
if (substdio_put(&ssout,rpline.s,rpline.len) == -1) goto fail;