tweaks: don't bother using the exclusive flag when creating a lock file

One microsecond earlier, the lock file has been deleted (if it existed),
so, if between our unlink() and our open() some other process managed to
recreate the lock file... well, we want to delete it *again*.  So, just
overwrite and truncate the lock file (if it exists).

When the lock file did NOT exist (a few microseconds earlier, when
checking in do_lockfile(), before calling write_lockfile()), then
the user expects the lock file to be written, so: just write it.

That between the check and the actual writing of the lock file there
is a small window of opportunity for other processes to write this
lock file is unfortunate, but it is not a reason to bother the user
with an error message when it happens.
master
Benno Schulenberg 2020-05-19 16:58:47 +02:00
parent c45df589fa
commit b4299f4f4e
1 changed files with 2 additions and 13 deletions

View File

@ -129,7 +129,6 @@ bool write_lockfile(const char *lockfilename, const char *filename, bool modifie
struct passwd *mypwuid = getpwuid(myuid);
char myhostname[32];
struct stat fileinfo;
int fd;
FILE *filestream;
char *lockdata;
size_t wroteamt;
@ -151,22 +150,12 @@ bool write_lockfile(const char *lockfilename, const char *filename, bool modifie
if (!delete_lockfile(lockfilename))
return FALSE;
/* Try to create the lockfile. */
fd = open(lockfilename, O_CREAT|O_EXCL|O_WRONLY, RW_FOR_ALL);
if (fd < 0) {
statusline(MILD, _("Error writing lock file %s: %s"),
lockfilename, strerror(errno));
return FALSE;
}
/* Try to associate a stream with the now open lockfile. */
filestream = fdopen(fd, "wb");
/* Create lock file (or truncate existing one) and open it for writing. */
filestream = fopen(lockfilename, "wb");
if (filestream == NULL) {
statusline(MILD, _("Error writing lock file %s: %s"),
lockfilename, strerror(errno));
close(fd);
return FALSE;
}