-#if !defined(USE_POSIX_FCNTL) && !defined(USE_FLOCK) && !defined(USE_LOCKF) && defined(USE_LNLOCK)
-/* XXX - error checking in this section needs to be tightened up */
-
-/* Delete a lock file.
-*/
-int delete_lock(fn)
-char *fn;
-{
- int rc;
-
- rc = unlink(fn);
- if (rc != 0 && errno == ENOENT) rc = 0;
-
- return rc;
-}
-
-/* Create a lock file.
-*/
-int create_lock(fn, pid)
-char *fn;
-long pid;
-{
- int fd;
- FILE *f;
- int mask;
-
- (void)delete_lock(fn); /* that's MY file! */
-
- mask = umask(0027);
- fd = open(fn, O_WRONLY|O_CREAT|O_EXCL, 0640);
- umask(mask);
- if (fd == -1) return -1;
-
- if((f = fdopen(fd, "w")) == NULL) {
- aclose(fd);
- return -1;
- }
- fprintf(f, "%ld\n", pid);
- if (fclose(f) == EOF)
- return -1;
- return 0;
-}
-
-/* Read the pid out of a lock file.
-** -1=error, otherwise pid.
-*/
-long read_lock(fn)
-char *fn; /* name of lock file */
-{
- int save_errno;
- FILE *f;
- long pid;
-
- if ((f = fopen(fn, "r")) == NULL) {
- return -1;
- }
- if (fscanf(f, "%ld", &pid) != 1) {
- save_errno = errno;
- afclose(f);
- errno = save_errno;
- return -1;
- }
- if (fclose(f) != 0) {
- return -1;
- }
- return pid;
-}
-
-/* Link a lock if we can.
-** 0=done, 1=already locked, -1=error.
-*/
-int link_lock(lk, tlk)
-char *lk; /* real lock file */
-char *tlk; /* temp lock file */
-{
- int rc;
- int serrno; /* saved errno */
- struct stat lkstat, tlkstat;
-
- /* an atomic check and set operation */
- rc = link(tlk, lk);
- if (rc == 0) return 0; /* XXX do we trust it? */
-
- /* link() says it failed - don't beleive it */
- serrno = errno;
-
- if (stat(lk, &lkstat) == 0 &&
- stat(tlk, &tlkstat) == 0 &&
- lkstat.st_ino == tlkstat.st_ino)
- return 0; /* it did work! */
-
- errno = serrno;
-
- if (errno == EEXIST) rc = 1;
-
- return rc;
-}
-
-/* Steal a lock if we can.
-** 0=done; 1=still in use; -1 = error.
-*/
-int steal_lock(fn, mypid, sres)
-char *fn; /* name of lock file to steal */
-long mypid; /* my process id */
-char *sres; /* name of steal-resource to lock */
-{
- int fd;
- char buff[64];
- long pid;
- int rc;
-
- /* prevent a race with another stealer */
- rc = ln_lock(sres, 1);
- if (rc != 0) goto error;
-
- pid = read_lock(fn);
- if (pid == -1) {
- if (errno == ENOENT) goto done;
- goto error;
- }
-
- if (pid == mypid) goto steal; /* i'm the locker! */
-
- /* are they still there ? */
- rc = kill((pid_t)pid, 0);
- if (rc != 0) {
- if (errno == ESRCH) goto steal; /* locker has gone */
- goto error;
- }
-
-inuse:
- rc = ln_lock(sres, 0);
- if (rc != 0) goto error;
-
- return 1;
-
-steal:
- rc = delete_lock(fn);
- if (rc != 0) goto error;
-
-done:
- rc = ln_lock(sres, 0);
- if (rc != 0) goto error;
-
- return 0;
-
-error:
- rc = ln_lock(sres, 0);
-
- return -1;
-}
-
-/* Locking using existance of a file.
-*/
-int ln_lock(res, op)
-char *res; /* name of resource to lock */
-int op; /* true to lock; false to unlock */
-{
- long mypid;
- char *lockfile = NULL;
- char *tlockfile = NULL;
- char *mres = NULL;
- int rc;
- char pid_str[NUM_STR_SIZE];
-
- mypid = (long)getpid();
-
- lockfile = vstralloc(AMANDA_TMPDIR, "/am", res, ".lock", NULL);
-
- if (!op) {
- /* unlock the resource */
- assert(read_lock(lockfile) == mypid);
-
- (void)delete_lock(lockfile);
- amfree(lockfile);
- return 0;
- }
-
- /* lock the resource */
-
- snprintf(pid_str, sizeof(pid_str), "%ld", mypid);
- tlockfile = vstralloc(AMANDA_TMPDIR, "am", res, ".", pid_str, NULL);
-
- (void)create_lock(tlockfile, mypid);
-
- mres = stralloc2(res, ".");
-
- while(1) {
- rc = link_lock(lockfile, tlockfile);
- if (rc == -1) break;
- if (rc == 0) break;
-
- rc = steal_lock(lockfile, mypid, mres);
- if (rc == -1) break;
- if (rc == 0) continue;
- sleep(1);
- }
-
- (void) delete_lock(tlockfile);
-
- amfree(mres);
- amfree(tlockfile);
- amfree(lockfile);
-
- return rc;
-}