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
23 /* lock a byte range in a open file */
24 static int lock_range(int fd, int offset, int len)
28 lock.l_type = F_WRLCK;
29 lock.l_whence = SEEK_SET;
30 lock.l_start = offset;
34 return fcntl(fd,F_SETLKW,&lock);
37 int main(int argc, char *argv[])
48 fd = open(lockfile, O_CREAT|O_RDWR, 0644);
54 if (lock_range(fd, 0, 1) != 0) {
59 return execvp(argv[2], argv+2);