The vi editor contains code for two different file locking methods -
one using flock(), the other using fcntl(). The fcntl method is unused
and has severe limitations (as described in a code comment). Let's
remove it for sake of readibility.

See the diff below; no binary change.

cheers,
natano


Index: build/config.h
===================================================================
RCS file: /cvs/src/usr.bin/vi/build/config.h,v
retrieving revision 1.11
diff -u -r1.11 config.h
--- build/config.h      10 Nov 2014 21:40:11 -0000      1.11
+++ build/config.h      14 Nov 2014 16:31:11 -0000
@@ -2,9 +2,3 @@
 
 /* Define if you want a debugging version. */
 /* #undef DEBUG */
-
-/* Define if you have fcntl(2) style locking. */
-/* #undef HAVE_LOCK_FCNTL */
-
-/* Define if you have flock(2) style locking. */
-#define HAVE_LOCK_FLOCK 1
Index: common/exf.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/common/exf.c,v
retrieving revision 1.31
diff -u -r1.31 exf.c
--- common/exf.c        12 Nov 2014 16:29:04 -0000      1.31
+++ common/exf.c        14 Nov 2014 16:35:48 -0000
@@ -1379,23 +1379,6 @@
  * file_lock --
  *     Get an exclusive lock on a file.
  *
- * XXX
- * The default locking is flock(2) style, not fcntl(2).  The latter is
- * known to fail badly on some systems, and its only advantage is that
- * it occasionally works over NFS.
- *
- * Furthermore, the semantics of fcntl(2) are wrong.  The problems are
- * two-fold: you can't close any file descriptor associated with the file
- * without losing all of the locks, and you can't get an exclusive lock
- * unless you have the file open for writing.  Someone ought to be shot,
- * but it's probably too late, they may already have reproduced.  To get
- * around these problems, nvi opens the files for writing when it can and
- * acquires a second file descriptor when it can't.  The recovery files
- * are examples of the former, they're always opened for writing.  The DB
- * files can't be opened for writing because the semantics of DB are that
- * files opened for writing are flushed back to disk when the DB session
- * is ended. So, in that case we have to acquire an extra file descriptor.
- *
  * PUBLIC: lockr_t file_lock(SCR *, char *, int *, int, int);
  */
 lockr_t
@@ -1408,7 +1391,6 @@
        if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
                msgq_str(sp, M_SYSERR, name, "%s");
 
-#ifdef HAVE_LOCK_FLOCK                 /* Hurrah!  We've got flock(2). */
        /*
         * !!!
         * We need to distinguish a lock not being available for the file
@@ -1420,53 +1402,4 @@
        return (flock(fd, LOCK_EX | LOCK_NB) ?
            errno == EAGAIN || errno == EWOULDBLOCK ? LOCK_UNAVAIL : 
LOCK_FAILED :
            LOCK_SUCCESS);
-#endif
-#ifdef HAVE_LOCK_FCNTL                 /* Gag me.  We've got fcntl(2). */
-{
-       struct flock arg;
-       int didopen, sverrno;
-
-       arg.l_type = F_WRLCK;
-       arg.l_whence = 0;               /* SEEK_SET */
-       arg.l_start = arg.l_len = 0;
-       arg.l_pid = 0;
-
-       /*
-        * If the file descriptor isn't opened for writing, it must fail.
-        * If we fail because we can't get a read/write file descriptor,
-        * we return LOCK_SUCCESS, believing that the file is readonly
-        * and that will be sufficient to warn the user.
-        */
-       if (!iswrite) {
-               if (name == NULL || fdp == NULL)
-                       return (LOCK_FAILED);
-               if ((fd = open(name, O_RDWR, 0)) == -1)
-                       return (LOCK_SUCCESS);
-               *fdp = fd;
-               didopen = 1;
-       }
-
-       errno = 0;
-       if (!fcntl(fd, F_SETLK, &arg))
-               return (LOCK_SUCCESS);
-       if (didopen) {
-               sverrno = errno;
-               (void)close(fd);
-               errno = sverrno;
-       }
-
-       /*
-        * !!!
-        * We need to distinguish a lock not being available for the file
-        * from the file system not supporting locking.  Fcntl is documented
-        * as returning EACCESS and EAGAIN; add EWOULDBLOCK for good measure,
-        * and assume they are the former.  There's no portable way to do this.
-        */
-       return (errno == EACCES || errno == EAGAIN || errno == EWOULDBLOCK ?
-           LOCK_UNAVAIL : LOCK_FAILED);
-}
-#endif
-#if !defined(HAVE_LOCK_FLOCK) && !defined(HAVE_LOCK_FCNTL)
-       return (LOCK_SUCCESS);
-#endif
 }

Reply via email to