summaryrefslogtreecommitdiffstats
path: root/lib/libutil
diff options
context:
space:
mode:
authorache <ache@FreeBSD.org>1997-08-05 12:58:02 +0000
committerache <ache@FreeBSD.org>1997-08-05 12:58:02 +0000
commitee162dd22f64c7b9653b65be570f0a70fc2607bd (patch)
treee4bc7fd709a255696e28f5862d28aac73688d544 /lib/libutil
parentb064615bee4a2c9979fdabea3dc3556879d0706f (diff)
downloadFreeBSD-src-ee162dd22f64c7b9653b65be570f0a70fc2607bd.zip
FreeBSD-src-ee162dd22f64c7b9653b65be570f0a70fc2607bd.tar.gz
Improve weak locking by using flock()
Diffstat (limited to 'lib/libutil')
-rw-r--r--lib/libutil/uucplock.c12
1 files changed, 8 insertions, 4 deletions
diff --git a/lib/libutil/uucplock.c b/lib/libutil/uucplock.c
index 5559990..837558a 100644
--- a/lib/libutil/uucplock.c
+++ b/lib/libutil/uucplock.c
@@ -30,7 +30,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $Id: uucplock.c,v 1.5 1997/04/02 04:24:39 ache Exp $
+ * $Id: uucplock.c,v 1.6 1997/05/12 10:36:14 brian Exp $
*
*/
@@ -70,13 +70,13 @@ int uu_lock (const char *ttyname)
int err;
(void)snprintf(tbuf, sizeof(tbuf), _PATH_UUCPLOCK LOCKFMT, ttyname);
- fd = open(tbuf, O_RDWR|O_CREAT|O_EXCL, 0660);
+ fd = open(tbuf, O_RDWR|O_CREAT|O_EXCL|O_EXLOCK, 0660);
if (fd < 0) {
/*
* file is already locked
* check to see if the process holding the lock still exists
*/
- fd = open(tbuf, O_RDWR, 0);
+ fd = open(tbuf, O_RDWR|O_SHLOCK);
if (fd < 0)
return UU_LOCK_OPEN_ERR;
@@ -100,13 +100,17 @@ int uu_lock (const char *ttyname)
errno = err;
return UU_LOCK_SEEK_ERR;
}
+ if (flock(fd, LOCK_EX|LOCK_NB) < 0) {
+ (void)close(fd);
+ return UU_LOCK_INUSE;
+ }
/* fall out and finish the locking process */
}
pid = getpid();
if (!put_pid (fd, pid)) {
err = errno;
- (void)close(fd);
(void)unlink(tbuf);
+ (void)close(fd);
errno = err;
return UU_LOCK_WRITE_ERR;
}
OpenPOWER on IntegriCloud