/* *** FILE locking demo program ***
   Pascal Meunier April 2004
   Assumes that /tmp can't be a malicious symlink
   So, even if lock file is deleted by a /tmp file sweeper
   and replaced by a symlink,
   unlink will only delete a symlink
   --- Note that it is vulnerable to a name collision attack
       i.e., if someone else creates a file of the same name
*/
/* mode_t in open call needs */
   #include <sys/types.h>
   #include <sys/stat.h>
/* open call needs */
   #include <fcntl.h>
/* errno reference needs */
   #include <errno.h>
int main () {
   char * location = "/tmp/mylock";
   int fd;
   fd = open (location, O_WRONLY | O_CREAT | O_EXCL,  S_IWUSR);
   if (fd <0) {
      perror("Could not get file lock");
      exit (-1);
   } else {
      sleep (20);
      unlink (location);
      close (fd);
      exit(0);
   }
}