Article View: comp.sys.sun
Article #7160Sun "lockf()" fails to provide locking.
From: Steve Jukoff
Date: Fri, 22 Mar 1991 20:20
Date: Fri, 22 Mar 1991 20:20
68 lines
2561 bytes
2561 bytes
I set a lock via lockf(). An immediately test fails to identify the lock.
Code is provided. Is anyone else having this problem or did I do
something wrong?
System: Sun 4c (SparcStation 1+, O/S 4.1.1 +
"rpc.lockd jumbo patch" Patch-ID# 100075-06
Code follows...
---Cut Here------------------------------------------------------
/* lockf.c - Test lockf() function */
/* Compile as: cc -o lockf lockf.c */
/* open, "O_RDWR" */
#include <stdio.h>
#include <unistd.h>
#include <sys/file.h>
#include <errno.h>
#include <sys/time.h>
main(argc, argv)
int argc;
char *argv[];
{
int fd;
char *fname = "/tmp/yy";
extern void display_error();
if ((fd = open (fname, O_RDWR)) < 0) {
perror ("open");
(void) fprintf (stderr, "Unable to open file '%s'.\n", fname);
exit(1);
}
lseek(fd, (off_t) 0, SEEK_SET); /* rewind */
/* Lock from current loc to EOF - the whole file */
if (lockf(fd, F_TLOCK, 0L) < 0) {
display_error(errno);
} else {
fprintf(stdout, "I hope that LOCK IS ATTACHED.\n");
lseek(fd, (off_t) 0, SEEK_SET); /* rewind */
if (lockf(fd, F_TEST, 0L) < 0) { /* test for lock */
display_error(errno);
} else {
fprintf(stderr,
"lockf(F_TEST) should have returned EAGAIN but didn't.\n");
}
}
}
void
display_error(err_no)
int err_no;
{
errno = err_no;
perror("lockf()");
fprintf(stderr, "Errno = %d -- ", err_no);
switch (err_no) {
case EBADF: fprintf(stderr, "EBADF\n"); break;
case EAGAIN: fprintf(stderr, "EAGAIN\n"); break;
case EINTR: fprintf(stderr, "EINTR\n"); break;
case ENOLCK: fprintf(stderr, "ENOLCK\n"); break;
default: fprintf(stderr, "Undefined for lockf()\n");
}
}
Technical Details
Message-ID:
<2089@brchh104.bnr.ca>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!cunews!bnrgate!brtph3!brchh104!brchs1!bnr.ca!rice.edu!sun-spots-requestShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!cunews!bnrgate!brtph3!brchh104!brchs1!bnr.ca!rice.edu!sun-spots-request From: jukoff@imram.net.com (Steve Jukoff) Newsgroups: comp.sys.sun Subject: Sun "lockf()" fails to provide locking. Keywords: SunOS Message-ID: <2089@brchh104.bnr.ca> Date: 22 Mar 91 20:20:20 GMT Sender: news@brchh104.bnr.ca Organization: Sun-Spots Lines: 68 Approved: Sun-Spots@rice.edu X-Original-Date: 20 Mar 91 05:16:47 GMT X-Sun-Spots-Digest: Volume 10, Issue 61, message 5 X-Note: Submissions: sun-spots@rice.edu, Admin: sun-spots-request@rice.edu