From 2e024a0005abf36965f1c50b02b3f1f27e3579d2 Mon Sep 17 00:00:00 2001 From: Sascha Schumann Date: Fri, 3 Dec 1999 16:19:38 +0000 Subject: [PATCH] Improve locking when O_EXCL is available --- ext/session/mod_files.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ext/session/mod_files.c b/ext/session/mod_files.c index 1bd4524579..c2800b1604 100644 --- a/ext/session/mod_files.c +++ b/ext/session/mod_files.c @@ -131,11 +131,13 @@ static void _ps_files_open(ps_files *data, const char *key) data->lastkey = estrdup(key); #ifdef O_EXCL - /* in the common case, the file already exists */ - data->fd = open(buf, O_EXCL | O_RDWR); - if(data->fd == -1) { - /* create it, if necessary */ - data->fd = open(buf, O_EXCL | O_RDWR | O_CREAT, 0600); + data->fd = open(buf, O_RDWR); + if (data->fd == -1) { + if (errno == ENOENT) { + data->fd = open(buf, O_EXCL | O_RDWR | O_CREAT, 0600); + } + } else { + flock(data->fd, LOCK_EX); } #else data->fd = open(buf, O_CREAT | O_RDWR, 0600); -- 2.40.0