title: fcntl F_GETLK always return true
body: <p>i'm trying to make a single instance daemon using a lock file but <code>fcntl()</code> doesn't seem to work as expected...</p>
  
  <pre><code>int creat_lock_file (char * pid_fn)
  {
    struct flock pid_lck = {F_WRLCK, SEEK_SET,   0,      0,     0 };
  
    /* Open/Create pid file */
    int pid_fd = open (pid_fn, O_CREAT | O_WRONLY, 0640);
    if (pid_fd == -1)
    {
      syslog (LOG_ERR, "Unable to open PID file &gt; [Errno: %s]",    strerror(errno));
      return -1;
    }
  
    /* Place write lock on pid file */
    if (fcntl(pid_fd, F_SETLK, &amp;pid_lck) == -1) {
      /* Unhandled error ocured */
      syslog (LOG_ERR, "Unhandled error ocured while locking PID file &gt; [Errno: %s]", strerror(errno));
      close (pid_fd);
      return -1;
    }
  
    /* Write PID to lock file */
    char pid_lock_buf[11];
    sprintf (pid_lock_buf, "%ld\n", (long) getpid ());
    write (pid_fd, pid_lock_buf, strlen (pid_lock_buf)+1);
  
    return 0;
  }
  
  int get_lock_file_status (char * pid_fn)
  {
    struct flock pid_lck = {F_WRLCK, SEEK_SET,   0,      0,     0 };
  
    /* Open/Create pid file */
    int pid_fd = open (pid_fn, O_CREAT | O_WRONLY, 0640);
    if (pid_fd == -1)
    {
      syslog (LOG_ERR, "Unable to open PID file &gt; [Errno: %s]", strerror(errno));
      return -1;
    }
  
    /* try locking pid file*/
    if(fcntl(pid_fd, F_GETLK, &amp;pid_lck) == -1)
    {
      if(errno == EAGAIN || errno == EACCES) /* file already locked, close fd and return -1 */
      {
        close (pid_fd);
        return -1;
      }
      else /* Unhandled error ocured */
      {
        syslog (LOG_ERR, "Unhandled error ocured while locking PID file &gt; [Errno: %s]", strerror(errno));
        close (pid_fd);
        return -1;
      }
    }
  
    close (pid_fd);
    return 0;
  }
  </code></pre>
  
  <p>so i call <code>get_lock_file_status</code> and exit if it returns -1 to make sure no other instance is running than i do some stuff (fork chdir etc) and call <code>creat_lock_file</code> to crate and lock a pid file after successfully creating a daemon...</p>
  
  <p>when complied and run the program works as expected runs creates lock file and writes pid to it but when a second instance is started the second instance simply opens the same lock file and writes it's own pid to it!</p>
  
  <p>what am i doing wrong? shouldn't the second instance return -1 in <code>get_lock_file_status</code>?</p>