1 /* run a command with a lock held
2 tridge@samba.org, April 2002
10 static void usage(void)
13 lockit <lockfile> <command>
15 Runs a command with a lockfile held. If the lock is already held then blocks
16 waiting for the lock file to be released before continuing.
18 Note that after running the lockfile is left behind in the filesystem. This is
21 The lock is inherited across exec but not fork
25 /* lock a byte range in a open file */
26 static int lock_range(int fd, int offset, int len)
30 lock.l_type = F_WRLCK;
31 lock.l_whence = SEEK_SET;
32 lock.l_start = offset;
36 return fcntl(fd,F_SETLKW,&lock);
39 int main(int argc, char *argv[])
50 fd = open(lockfile, O_CREAT|O_RDWR, 0644);
56 if (lock_range(fd, 0, 1) != 0) {
61 return execvp(argv[2], argv+2);