2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include "mt.h"
2N/A#include "uucp.h"
2N/A
2N/Astatic void stlock(char *);
2N/Astatic int onelock(char *, char *, char *);
2N/A
2N/A/*
2N/A * make a lock file with given 'name'
2N/A * If one already exists, send a signal 0 to the process--if
2N/A * it fails, then unlink it and make a new one.
2N/A *
2N/A * input:
2N/A * name - name of the lock file to make
2N/A *
2N/A * return:
2N/A * 0 -> success
2N/A * FAIL -> failure
2N/A */
2N/A
2N/Astatic int
2N/Amklock(char *name)
2N/A{
2N/A static char pid[SIZEOFPID+2] = { '\0' }; /* +2 for '\n' and NULL */
2N/A static char *tempfile;
2N/A
2N/A if (pid[0] == '\0') {
2N/A tempfile = malloc(MAXNAMESIZE);
2N/A if (tempfile == NULL)
2N/A return (FAIL);
2N/A (void) sprintf(pid, "%*ld\n", SIZEOFPID, (long)getpid());
2N/A (void) snprintf(tempfile, MAXNAMESIZE, "%s/LTMP.%ld", X_LOCKDIR,
2N/A (long)getpid());
2N/A }
2N/A
2N/A if (onelock(pid, tempfile, name) == -1) {
2N/A (void) unlink(tempfile);
2N/A if (cklock(name))
2N/A return (FAIL);
2N/A else {
2N/A if (onelock(pid, tempfile, name)) {
2N/A (void) unlink(tempfile);
2N/A DEBUG(4, "ulockf failed in onelock()\n%s", "");
2N/A return (FAIL);
2N/A }
2N/A }
2N/A }
2N/A stlock(name);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * check to see if the lock file exists and is still active
2N/A * - use kill(pid, 0)
2N/A *
2N/A * return:
2N/A * 0 -> success (lock file removed - no longer active
2N/A * FAIL -> lock file still active
2N/A */
2N/Astatic int
2N/Acklock(char *name)
2N/A{
2N/A int ret;
2N/A pid_t lpid = -1;
2N/A char alpid[SIZEOFPID+2]; /* +2 for '\n' and NULL */
2N/A int fd;
2N/A
2N/A fd = open(name, O_RDONLY);
2N/A DEBUG(4, "ulockf name %s\n", name);
2N/A if (fd == -1) {
2N/A if (errno == ENOENT) { /* file does not exist -- OK */
2N/A return (0);
2N/A }
2N/A DEBUG(4, "Lock File--can't read (errno %d) --remove it!\n",
2N/A errno);
2N/A goto unlk;
2N/A }
2N/A ret = read(fd, (char *)alpid, SIZEOFPID + 1); /* +1 for '\n' */
2N/A (void) close(fd);
2N/A if (ret != (SIZEOFPID+1)) {
2N/A
2N/A DEBUG(4, "Lock File--bad format--remove it!\n%s", "");
2N/A goto unlk;
2N/A }
2N/A lpid = (pid_t)strtol(alpid, NULL, 10);
2N/A if ((ret = kill(lpid, 0)) == 0 || errno == EPERM) {
2N/A DEBUG(4, "Lock File--process still active--not removed\n%s",
2N/A "");
2N/A return (FAIL);
2N/A }
2N/A /* process no longer active */
2N/A DEBUG(4, "kill pid (%ld), ", (long)lpid);
2N/A DEBUG(4, "returned %d", ret);
2N/A DEBUG(4, "--ok to remove lock file (%s)\n", name);
2N/Aunlk:
2N/A
2N/A if (unlink(name) != 0) {
2N/A DEBUG(4, "ulockf failed in unlink()\n%s", "");
2N/A return (FAIL);
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/A#define MAXLOCKS 10 /* maximum number of lock files */
2N/Astatic char *Lockfile[MAXLOCKS];
2N/Astatic int Nlocks = 0;
2N/A
2N/A/*
2N/A * put name in list of lock files
2N/A * return:
2N/A * none
2N/A */
2N/Astatic void
2N/Astlock(char *name)
2N/A{
2N/A int i;
2N/A char *p;
2N/A
2N/A for (i = 0; i < Nlocks; i++) {
2N/A if (Lockfile[i] == NULL)
2N/A break;
2N/A }
2N/A ASSERT(i < MAXLOCKS, "TOO MANY LOCKS", "", i);
2N/A if (i >= Nlocks)
2N/A i = Nlocks++;
2N/A p = calloc((unsigned)strlen(name) + 1, sizeof (char));
2N/A ASSERT(p != NULL, "CAN NOT ALLOCATE FOR", name, 0);
2N/A (void) strcpy(p, name);
2N/A Lockfile[i] = p;
2N/A}
2N/A
2N/A/*
2N/A * remove the named lock. If named lock is NULL,
2N/A * then remove all locks currently in list.
2N/A * return:
2N/A * none
2N/A */
2N/Astatic void
2N/Armlock(char *name)
2N/A{
2N/A int i;
2N/A
2N/A for (i = 0; i < Nlocks; i++) {
2N/A if (Lockfile[i] == NULL)
2N/A continue;
2N/A if (name == NULL || EQUALS(name, Lockfile[i])) {
2N/A (void) unlink(Lockfile[i]);
2N/A free(Lockfile[i]);
2N/A Lockfile[i] = NULL;
2N/A }
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * makes a lock on behalf of pid.
2N/A * input:
2N/A * pid - process id
2N/A * tempfile - name of a temporary in the same file system
2N/A * name - lock file name (full path name)
2N/A * return:
2N/A * -1 - failed
2N/A * 0 - lock made successfully
2N/A */
2N/Astatic int
2N/Aonelock(char *pid, char *tempfile, char *name)
2N/A{
2N/A int fd;
2N/A char cb[100];
2N/A
2N/A fd = creat(tempfile, (mode_t)0444);
2N/A if (fd < 0) {
2N/A (void) snprintf(cb, sizeof (cb),
2N/A "%s %s %d", tempfile, name, errno);
2N/A logent("ULOCKC", cb);
2N/A if ((errno == EMFILE) || (errno == ENFILE))
2N/A (void) unlink(tempfile);
2N/A return (-1);
2N/A }
2N/A /* +1 for '\n' */
2N/A if (write(fd, pid, SIZEOFPID+1) != (SIZEOFPID+1)) {
2N/A (void) snprintf(cb, sizeof (cb),
2N/A "%s %s %d", tempfile, name, errno);
2N/A logent("ULOCKW", cb);
2N/A (void) unlink(tempfile);
2N/A return (-1);
2N/A }
2N/A (void) chmod(tempfile, (mode_t)0444);
2N/A (void) chown(tempfile, UUCPUID, UUCPGID);
2N/A (void) close(fd);
2N/A if (link(tempfile, name) < 0) {
2N/A DEBUG(4, "%s: ", strerror(errno));
2N/A DEBUG(4, "link(%s, ", tempfile);
2N/A DEBUG(4, "%s)\n", name);
2N/A if (unlink(tempfile) < 0) {
2N/A (void) snprintf(cb, sizeof (cb),
2N/A "ULK err %s %d", tempfile, errno);
2N/A logent("ULOCKLNK", cb);
2N/A }
2N/A return (-1);
2N/A }
2N/A if (unlink(tempfile) < 0) {
2N/A (void) snprintf(cb, sizeof (cb), "%s %d", tempfile, errno);
2N/A logent("ULOCKF", cb);
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * fd_mklock(fd) - lock the device indicated by fd is possible
2N/A *
2N/A * return -
2N/A * SUCCESS - this process now has the fd locked
2N/A * FAIL - this process was not able to lock the fd
2N/A */
2N/A
2N/Astatic int
2N/Afd_mklock(int fd)
2N/A{
2N/A int tries = 0;
2N/A struct stat64 _st_buf;
2N/A char lockname[BUFSIZ];
2N/A
2N/A if (fstat64(fd, &_st_buf) != 0)
2N/A return (FAIL);
2N/A
2N/A (void) snprintf(lockname, sizeof (lockname),
2N/A "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
2N/A (unsigned long)major(_st_buf.st_dev),
2N/A (unsigned long)major(_st_buf.st_rdev),
2N/A (unsigned long)minor(_st_buf.st_rdev));
2N/A
2N/A if (mklock(lockname) == FAIL)
2N/A return (FAIL);
2N/A
2N/A while (lockf(fd, F_TLOCK, 0L) != 0) {
2N/A DEBUG(7, "fd_mklock: lockf returns %d\n", errno);
2N/A if ((++tries >= MAX_LOCKTRY) || (errno != EAGAIN)) {
2N/A rmlock(lockname);
2N/A logent("fd_mklock", "lockf failed");
2N/A return (FAIL);
2N/A }
2N/A (void) sleep(2);
2N/A }
2N/A DEBUG(7, "fd_mklock: ok\n%s", "");
2N/A return (SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * remove the locks associated with the device file descriptor
2N/A *
2N/A * return -
2N/A * SUCCESS - both BNU lock file and advisory locks removed
2N/A * FAIL -
2N/A */
2N/A
2N/Astatic void
2N/Afd_rmlock(int fd)
2N/A{
2N/A struct stat64 _st_buf;
2N/A char lockname[BUFSIZ];
2N/A
2N/A if (fstat64(fd, &_st_buf) == 0) {
2N/A (void) snprintf(lockname, sizeof (lockname),
2N/A "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
2N/A (unsigned long)major(_st_buf.st_dev),
2N/A (unsigned long)major(_st_buf.st_rdev),
2N/A (unsigned long)minor(_st_buf.st_rdev));
2N/A rmlock(lockname);
2N/A }
2N/A (void) lockf(fd, F_ULOCK, 0L);
2N/A}