From 25e74062932eeb285a6be86ec3bfc3e175438bc0 Mon Sep 17 00:00:00 2001 From: Ben Collins Date: Mon, 24 Apr 2000 16:46:13 +0000 Subject: [PATCH] when opening files in /tmp use O_CREAT|O_EXCL to overcome race conditions --- servers/slurpd/rq.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/servers/slurpd/rq.c b/servers/slurpd/rq.c index 46667210b6..207b1c8ce0 100644 --- a/servers/slurpd/rq.c +++ b/servers/slurpd/rq.c @@ -35,8 +35,16 @@ #include #include +#include #include /* get ftruncate() */ +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif + #include "slurp.h" #include "globals.h" @@ -227,17 +235,28 @@ Rq_dump( { Re *re; FILE *fp; + int tmpfd; if ( rq == NULL ) { Debug( LDAP_DEBUG_ANY, "Rq_dump: rq is NULL!\n", 0, 0, 0 ); return; } - if (( fp = fopen( SLURPD_DUMPFILE, "w" )) == NULL ) { + if (unlink(SLURPD_DUMPFILE) == -1 && errno != ENOENT) { + Debug( LDAP_DEBUG_ANY, "Rq_dump: \"%s\" exists, and cannot unlink\n", + SLURPD_DUMPFILE, 0, 0 ); + return; + } + if (( tmpfd = open(SLURPD_DUMPFILE, O_CREAT|O_RDWR|O_EXCL, 0600)) == -1) { Debug( LDAP_DEBUG_ANY, "Rq_dump: cannot open \"%s\" for write\n", SLURPD_DUMPFILE, 0, 0 ); return; } + if (( fp = fdopen( tmpfd, "w" )) == NULL ) { + Debug( LDAP_DEBUG_ANY, "Rq_dump: cannot fdopen \"%s\" for write\n", + SLURPD_DUMPFILE, 0, 0 ); + return; + } rq->rq_lock( rq ); for ( re = rq->rq_gethead( rq ); re != NULL; re = rq->rq_getnext( re )) { -- 2.47.2