[Samba] checking a locked file

Matt Eaton smb at divinehawk.com
Mon Jan 22 16:30:43 GMT 2007

I've been trying to determine if a file is locked by samba from unix. Is
there some magic incantation to find this? E.g. if someone is copying a file
in from a windows system via samba.

I'm using the default locking parameters on 3.0.10 with a 2.6 kernel. I've
tried checking posix byte ranges, posix locking, and kernel op locks.

Here is my test program:

#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

/* kernel op locks */

#define F_GETLEASE      1025

int main(int argc, char **argv)
struct flock the_lock;
int f;
int retlck;
int smblck;

if(argc != 2) {
        fprintf(stdout, "Usage: %s <file>\n", argv[0]);
        return 1;
f = open(argv[1], O_RDONLY);
the_lock.l_type = F_WRLCK;
the_lock.l_whence = SEEK_SET;
the_lock.l_start = 0;
the_lock.l_len = 0;

if(f <= 0) {
        fprintf(stderr, "Cannot open file: %s\n", argv[1]);
        return 1;
fcntl(f, F_GETLK, &the_lock);
retlck = flock(f, LOCK_EX | LOCK_NB);
smblck = fcntl(f, F_GETLEASE, 0);
fprintf(stdout, "smblck: %x\n", smblck);


if((the_lock.l_type == F_UNLCK) && (retlck == 0))
  fprintf(stdout, "File is not locked\n");
 fprintf(stdout, "File is locked\n");
 return 1;

return 0;


More information about the samba mailing list