Simple file locking wrapper command in C A simple command that wraps another command, locking a file first. It is similar to flock, just simpler.

#include &lt;stddef.h&gt;
#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/file.h&gt;
#include &lt;err.h&gt;
#include &lt;stdlib.h&gt;

void attemptLock(const char *lockFileName)
{
    int fd = open(lockFileName, O_CREAT | O_WRONLY, 0666);
    if (!fd || flock(fd, LOCK_EX))
        warn("Cannot lock %s", lockFileName);
}

int main(int argc, char *argv[])
{
    if (argc &lt; 3)
        errx(EXIT_FAILURE, "Not enough arguments.\nUsage: %s LOCKFILENAME COMMAND...\n", argv[0]);
    attemptLock(argv[1]);
    argv += 2;  // Skip our own command name and LOCKFILENAME.
    execvp(argv[0], argv);
    err(EXIT_FAILURE, "Cannot execute %s", argv[0]);
}


Anything fishy? Bogus? Wrong? Anything that can be improved (besides adding options like -h)?
Besides reinventing the wheel the things I'm concerned about are:


the warn in attemptLock, because if either creating the lock file
fails, or if you can't get the lock (or the call was interrupted), I
would expect the wrapper to exit instead,
the return value of open, which might very well be negative, so just
check for that separately and give an error message for that,
the return value of flock, where you should handle EINTR in a loop
I believe?