Can set file lock with fcntl on AIX
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[50];
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 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