+int
+within_one_second (a, b)
+ time_t a, b;
+{
+ return (a - b >= -1 && a - b <= 1);
+}
+\f
+/* Return 0 if nobody owns the lock file LFNAME or the lock is obsolete,
+ 1 if another process owns it (and set OWNER (if non-null) to info),
+ 2 if the current process owns it,
+ or -1 if something is wrong with the locking mechanism. */
+
+static int
+current_lock_owner (owner, lfname)
+ lock_info_type *owner;
+ char *lfname;
+{
+#ifndef index
+ extern char *rindex (), *index ();
+#endif
+ int len, ret;
+ int local_owner = 0;
+ char *at, *dot, *colon;
+ char *lfinfo = 0;
+ int bufsize = 50;
+ /* Read arbitrarily-long contents of symlink. Similar code in
+ file-symlink-p in fileio.c. */
+ do
+ {
+ bufsize *= 2;
+ lfinfo = (char *) xrealloc (lfinfo, bufsize);
+ errno = 0;
+ len = readlink (lfname, lfinfo, bufsize);
+#ifdef ERANGE
+ /* HP-UX reports ERANGE if the buffer is too small. */
+ if (len == -1 && errno == ERANGE)
+ len = bufsize;
+#endif
+ }
+ while (len >= bufsize);
+
+ /* If nonexistent lock file, all is well; otherwise, got strange error. */
+ if (len == -1)
+ {
+ xfree (lfinfo);
+ return errno == ENOENT ? 0 : -1;
+ }
+
+ /* Link info exists, so `len' is its length. Null terminate. */
+ lfinfo[len] = 0;
+
+ /* Even if the caller doesn't want the owner info, we still have to
+ read it to determine return value, so allocate it. */
+ if (!owner)
+ {
+ owner = (lock_info_type *) alloca (sizeof (lock_info_type));
+ local_owner = 1;
+ }
+
+ /* Parse USER@HOST.PID:BOOT_TIME. If can't parse, return -1. */
+ /* The USER is everything before the first @. */
+ at = index (lfinfo, '@');
+ dot = rindex (lfinfo, '.');
+ if (!at || !dot)
+ {
+ xfree (lfinfo);
+ return -1;
+ }
+ len = at - lfinfo;
+ owner->user = (char *) xmalloc (len + 1);
+ strncpy (owner->user, lfinfo, len);
+ owner->user[len] = 0;
+
+ /* The PID is everything from the last `.' to the `:'. */
+ owner->pid = atoi (dot + 1);
+ colon = dot;
+ while (*colon && *colon != ':')
+ colon++;
+ /* After the `:', if there is one, comes the boot time. */
+ if (*colon == ':')
+ owner->boot_time = atoi (colon + 1);
+ else
+ owner->boot_time = 0;
+
+ /* The host is everything in between. */
+ len = dot - at - 1;
+ owner->host = (char *) xmalloc (len + 1);
+ strncpy (owner->host, at + 1, len);
+ owner->host[len] = 0;
+
+ /* We're done looking at the link info. */
+ xfree (lfinfo);
+
+ /* On current host? */
+ if (STRINGP (Fsystem_name ())
+ && strcmp (owner->host, SDATA (Fsystem_name ())) == 0)
+ {
+ if (owner->pid == getpid ())
+ ret = 2; /* We own it. */
+ else if (owner->pid > 0
+ && (kill (owner->pid, 0) >= 0 || errno == EPERM)
+ && (owner->boot_time == 0
+ || within_one_second (owner->boot_time, get_boot_time ())))
+ ret = 1; /* An existing process on this machine owns it. */
+ /* The owner process is dead or has a strange pid (<=0), so try to
+ zap the lockfile. */
+ else if (unlink (lfname) < 0)
+ ret = -1;
+ else
+ ret = 0;
+ }
+ else
+ { /* If we wanted to support the check for stale locks on remote machines,
+ here's where we'd do it. */
+ ret = 1;
+ }
+
+ /* Avoid garbage. */
+ if (local_owner || ret <= 0)
+ {
+ FREE_LOCK_INFO (*owner);
+ }
+ return ret;
+}
+
+\f
+/* Lock the lock named LFNAME if possible.
+ Return 0 in that case.
+ Return positive if some other process owns the lock, and info about
+ that process in CLASHER.
+ Return -1 if cannot lock for any other reason. */
+
+static int
+lock_if_free (clasher, lfname)
+ lock_info_type *clasher;
+ register char *lfname;
+{
+ while (lock_file_1 (lfname, 0) == 0)
+ {
+ int locker;
+
+ if (errno != EEXIST)
+ return -1;
+
+ locker = current_lock_owner (clasher, lfname);
+ if (locker == 2)
+ {
+ FREE_LOCK_INFO (*clasher);
+ return 0; /* We ourselves locked it. */
+ }
+ else if (locker == 1)
+ return 1; /* Someone else has it. */
+ else if (locker == -1)
+ return -1; /* current_lock_owner returned strange error. */
+
+ /* We deleted a stale lock; try again to lock the file. */
+ }
+ return 0;
+}
+
+/* lock_file locks file FN,