Set the close-on-exec flag when we lock the file we're editing.

This prevents any background sub-command executed from inheriting
the descriptor & lock (and making vi think that someone else is
editing the file when it re-edits).

Remembered from: An OpenBSD commit message from May '99
This commit is contained in:
Brian Somers 1999-08-06 08:34:42 +00:00
parent 0f854af2f8
commit 2e4e1ffe62
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=49457

View File

@ -1438,11 +1438,15 @@ file_lock(sp, name, fdp, fd, iswrite)
* they are the former. There's no portable way to do this. * they are the former. There's no portable way to do this.
*/ */
errno = 0; errno = 0;
return (flock(fd, LOCK_EX | LOCK_NB) ? errno == EAGAIN if (!flock(fd, LOCK_EX | LOCK_NB)) {
fcntl(fd, F_SETFD, 1);
return (LOCK_SUCCESS);
}
return (errno == EAGAIN
#ifdef EWOULDBLOCK #ifdef EWOULDBLOCK
|| errno == EWOULDBLOCK || errno == EWOULDBLOCK
#endif #endif
? LOCK_UNAVAIL : LOCK_FAILED : LOCK_SUCCESS); ? LOCK_UNAVAIL : LOCK_FAILED);
#endif #endif
#ifdef HAVE_LOCK_FCNTL /* Gag me. We've got fcntl(2). */ #ifdef HAVE_LOCK_FCNTL /* Gag me. We've got fcntl(2). */
{ {
@ -1470,8 +1474,11 @@ file_lock(sp, name, fdp, fd, iswrite)
} }
errno = 0; errno = 0;
if (!fcntl(fd, F_SETLK, &arg)) if (!fcntl(fd, F_SETLK, &arg)) {
fcntl(fd, F_SETFD, 1);
return (LOCK_SUCCESS); return (LOCK_SUCCESS);
}
if (didopen) { if (didopen) {
sverrno = errno; sverrno = errno;
(void)close(fd); (void)close(fd);