Logo Search packages:      
Sourcecode: leafnode version File versions

static int lock_is_stale ( const char *const   name,
const int  quiet 
) [static]

Check if the lock file given by "name" is stale and if so, erase it. A lock is stale when the process to which it belongs, is dead.

Bug:
cannot detect if lock files held by other hosts are stale.
Returns:
  • -1 for error "failure"
  • 0 if lock file is still in use or held by another host "failure"
  • 1 if lock file is stale and has been erased "success"
Parameters:
name  file name of lock file
quiet  quiet flag

Definition at line 74 of file lockfile.c.

Referenced by try_lock().

{
    char buf[512];
    int fd;
    int len;
    char *pid;
    char *host;
    char *tmp;
    unsigned long npid;

    fd = open(name, O_RDONLY, 0);
    if (fd < 0) {
      if (errno == ENOENT) {
          /* file has just disappeared, thus it's not stale */
          return 0;
      } else {
          ln_log(LNLOG_SERR, LNLOG_CTOP,
               "cannot open %s for reading: %m", name);
          return -1;
      }
    }

    if ((len = read(fd, buf, sizeof(buf) - 1)) < 0) {
      ln_log(LNLOG_SERR, LNLOG_CTOP, "read error on %s: %m", name);
      (void)close(fd);
      return -1;
    }

    if (close(fd) < 0) {
      ln_log(LNLOG_SERR, LNLOG_CTOP, "read error on %s: %m", name);
      return -1;
    }

    /* read pid and host */
    buf[len - 1] = '\0';
    pid = host = buf;
    /* we expect a single \n here */
    host += strcspn(host, "\n");
    *(host++) = '\0';

    /* kill trailing \n */
    tmp = host;
    tmp += strcspn(tmp, "\n");
    *tmp = '\0';

    npid = strtoul(pid, 0, 10);
    if (npid == ULONG_MAX && errno == ERANGE) {
      /* overflow error, should not happen, bail out */
      ln_log(LNLOG_SERR, LNLOG_CTOP, "bogus pid in %s: %m", name);
      return -1;
    }

    if (strcasecmp(host, fqdn)) {
      if (!quiet)
          ln_log(LNLOG_SERR, LNLOG_CTOP,
               "lockfile held by pid %lu on host %s, we're %s",
               npid, host, fqdn);
      return 0;         /* other host holds the lock */
    }

    /* okay, we can see if there's still a process with that pid active */
    if (kill((pid_t)npid, 0) && errno == ESRCH) {
      /* no such process, good */
      if (!unlink(name)) {
          ln_log(LNLOG_SNOTICE, LNLOG_CTOP,
               "erased stale pid %lu host %s lockfile %s",
               npid, host, name);
          return 1;
      } else {
          if (!quiet)
            ln_log(LNLOG_SERR, LNLOG_CTOP,
                   "unable to erase stale pid %lu host %s lockfile %s",
                   npid, host, name);
          return 0;
      }
    }

    /* there is a process active */
    return 0;
}


Generated by  Doxygen 1.6.0   Back to index