|
Home > Archive > Unix Programming > February 2007 > Can not set file lock with fcntl
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Can not set file lock with fcntl
|
|
|
| Hi,
In the code below, I tried to set write lock to the file: a.txt. But
failed.
#include <errno.h>
#include <fcntl.h>
#include <iostream>
#include <stdio.h>
#include <unistd.h>
using namespace std;
int main()
{
int handle;
char *BUF = "HELLO, HELLO[20]";
char b[10];
handle = open("a.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR
| S_IRGRP | S_IROTH);
int ret = write(handle, BUF, strlen(BUF));
lseek64( handle, 0, SEEK_SET);
cout << "ret:" << ret << endl;
cout << "BUF:" << BUF << endl;
int err;
int r = read(handle,b,strlen(BUF));
cout<<"r:"<<r<<" handle:" << handle << endl;
if ( r != strlen(BUF) )
{
cout<<"r:"<<r<< " b:"<<b<<endl;
perror(" Errno:");
}
struct flock l_lockSetup;
l_lockSetup.l_type = F_WRLCK;
l_lockSetup.l_whence = SEEK_SET;
l_lockSetup.l_start = 0;
l_lockSetup.l_len = 0;
// l_lockSetup.l_type = F_RDLCK;
int status;
status = fcntl(handle, F_SETFD, 0);
cout << "status: " << status << endl;
cout << "fcntl(l_fd, , F_SETLK, &l_lockSetup)" << endl;
status = fcntl(handle, F_SETLK, &l_lockSetup);
if (status == -1)
{
cout << " Failed to set write-lock "
"on file " << handle << " errno: " <<
errno << endl;
}
return 0;
}
The output is:
ret:16
BUF:HELLO, HELLO[20]
r:16 handle:3
status: 0
fcntl(l_fd, , F_SETLK, &l_lockSetup)
Failed to set write-lock on file 3 errno: 13
The definition of errno 13 is:
#define EACCES 13 /* Permission denied */
What is the problem? Does it mean that I do not have the permission to
set lock? But I can do read and write. My system is IBM AIX
Thanks a lot.
Jack
| |
| matevzb 2007-02-13, 7:23 am |
| On Feb 13, 8:49 am, "Jack" <junw2...@gmail.com> wrote:
> Hi,
>
> In the code below, I tried to set write lock to the file: a.txt. But
> failed.
>
> #include <errno.h>
> #include <fcntl.h>
> #include <iostream>
> #include <stdio.h>
> #include <unistd.h>
>
> using namespace std;
> int main()
> {
> int handle;
> char *BUF = "HELLO, HELLO[20]";
> char b[10];
> handle = open("a.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR
> | S_IRGRP | S_IROTH);
> int ret = write(handle, BUF, strlen(BUF));
> lseek64( handle, 0, SEEK_SET);
You used a normal open() - not open64() - but lseek64() instead of
lseek(). On my system this doesn't work (it could be different on
AIX). Try using lseek() first to see if it changes anything.
> cout << "ret:" << ret << endl;
> cout << "BUF:" << BUF << endl;
> int err;
> int r = read(handle,b,strlen(BUF));
"b" has space for only 10 characters, you're reading strlen(BUF) into
it and thus causing a buffer overflow.
> cout<<"r:"<<r<<" handle:" << handle << endl;
> if ( r != strlen(BUF) )
> {
> cout<<"r:"<<r<< " b:"<<b<<endl;
> perror(" Errno:");
> }
>
> struct flock l_lockSetup;
> l_lockSetup.l_type = F_WRLCK;
> l_lockSetup.l_whence = SEEK_SET;
> l_lockSetup.l_start = 0;
> l_lockSetup.l_len = 0;
>
> // l_lockSetup.l_type = F_RDLCK;
>
> int status;
>
> status = fcntl(handle, F_SETFD, 0);
> cout << "status: " << status << endl;
> cout << "fcntl(l_fd, , F_SETLK, &l_lockSetup)" << endl;
> status = fcntl(handle, F_SETLK, &l_lockSetup);
> if (status == -1)
> {
> cout << " Failed to set write-lock "
> "on file " << handle << " errno: " <<
> errno << endl;
> }
>
> return 0;
>
> }
>
> The output is:
>
> ret:16
> BUF:HELLO, HELLO[20]
> r:16 handle:3
> status: 0
> fcntl(l_fd, , F_SETLK, &l_lockSetup)
> Failed to set write-lock on file 3 errno: 13
>
> The definition of errno 13 is:
>
> #define EACCES 13 /* Permission denied */
>
> What is the problem? Does it mean that I do not have the permission to
> set lock? But I can do read and write. My system is IBM AIX
After fixing the above, it works fine for me. Just a suggestion: I'd
fix and clean up the code first, you're using way too many unnecessary
variables (err isn't used; r, ret, status are all used for one thing,
the return value from read()/fcntl() etc.)
--
WYCIWYG - what you C is what you get
| |
|
| On Feb 13, 12:43 am, "matevzb" <mate...@gmail.com> wrote:
> On Feb 13, 8:49 am, "Jack" <junw2...@gmail.com> wrote:
>
>
>
>
>
> You used a normal open() - not open64() - but lseek64() instead of
> lseek(). On my system this doesn't work (it could be different on
> AIX). Try using lseek() first to see if it changes anything.> cout << "ret:" << ret << endl;
>
> "b" has space for only 10 characters, you're reading strlen(BUF) into
> it and thus causing a buffer overflow.
>
>
>
>
>
>
>
>
>
>
>
>
>
> After fixing the above, it works fine for me. Just a suggestion: I'd
> fix and clean up the code first, you're using way too many unnecessary
> variables (err isn't used; r, ret, status are all used for one thing,
> the return value from read()/fcntl() etc.)
Thank you. I am learning Unix system programing. I fixed the problems
you suggested. But the same error still happens.
What causes the problem? System configuration?
jack
| |
| matevzb 2007-02-13, 1:17 pm |
| On Feb 13, 7:06 pm, "Jack" <junw2...@gmail.com> wrote:
<snip>
>
>
>
>
>
> Thank you. I am learning Unix system programing. I fixed the problems
> you suggested. But the same error still happens.
> What causes the problem? System configuration?
Hmm... I checked AIX 5.2 man page for fcntl() and it's a bit different
from SUSv3 specification. The "l_len" field of struct flock can be set
to 0 in SUSv3, and that means:
"A lock shall be set to extend to the largest possible value of the
file offset for that file by setting l_len to 0. If such a lock also
has l_start set to 0 and l_whence is set to SEEK_SET, the whole file
shall be locked."
AIX doesn't mention any of this, so maybe herein lies the problem. Try
specifying a value > 0 for l_len (whatever number of bytes was
returned by write()) and see if that happens. I might be missing
something here, but it did work for me (HP-UX 11.11).
You might also want to try the basic example from SUSv3 specification,
"Locking and Unlocking a File" section at http://www.opengroup.org/
onlinepubs/009695399/functions/fcntl.html.
--
WYCIWYG - what you C is what you get
| |
|
| On Feb 13, 11:14 am, "matevzb" <mate...@gmail.com> wrote:
> On Feb 13, 7:06 pm, "Jack" <junw2...@gmail.com> wrote:
> <snip>
>
>
>
>
>
>
>
> Hmm... I checked AIX 5.2 man page for fcntl() and it's a bit different
> from SUSv3 specification. The "l_len" field of struct flock can be set
> to 0 in SUSv3, and that means:
> "A lock shall be set to extend to the largest possible value of the
> file offset for that file by setting l_len to 0. If such a lock also
> has l_start set to 0 and l_whence is set to SEEK_SET, the whole file
> shall be locked."
> AIX doesn't mention any of this, so maybe herein lies the problem. Try
> specifying a value > 0 for l_len (whatever number of bytes was
> returned by write()) and see if that happens. I might be missing
> something here, but it did work for me (HP-UX 11.11).
> You might also want to try the basic example from SUSv3 specification,
> "Locking and Unlocking a File" section athttp://www.opengroup.org/
> onlinepubs/009695399/functions/fcntl.html.
> --
> WYCIWYG - what you C is what you get
I tried the code at:
http://www.opengroup.org/onlinepubs...ons/fcntl.html.
Same error happens. It is really strange.
Jack
| |
| matevzb 2007-02-17, 1:18 pm |
| On Feb 14, 6:45 pm, "Jack" <junw2...@gmail.com> wrote:
<snip>
>
>
>
<snip>[vbcol=seagreen]
> I tried the code at: http://www.opengroup.org/onlinepubs...ons/fcntl.html.
> Same error happens. It is really strange.
Check whether your AIX fcntl() man page states anything special. It's
also possible (but not very probable) they have a bug in their
library, so maybe you should check for libc updates or something
similar. You can also try opening an existing file and using fcntl()
with it, I fear it will be the same though.
--
WYCIWYG - what you C is what you get
|
|
|
|
|