#include "mt.h"
#include "uucp.h"
static void stlock(char *);
static int onelock(char *, char *, char *);
static int
mklock(char *name)
{
static char pid[SIZEOFPID+2] = { '\0' };
static char *tempfile;
if (pid[0] == '\0') {
tempfile = malloc(MAXNAMESIZE);
if (tempfile == NULL)
return (FAIL);
(void) sprintf(pid, "%*ld\n", SIZEOFPID, (long)getpid());
(void) snprintf(tempfile, MAXNAMESIZE, "%s/LTMP.%ld", X_LOCKDIR,
(long)getpid());
}
if (onelock(pid, tempfile, name) == -1) {
(void) unlink(tempfile);
if (cklock(name))
return (FAIL);
else {
if (onelock(pid, tempfile, name)) {
(void) unlink(tempfile);
DEBUG(4, "ulockf failed in onelock()\n%s", "");
return (FAIL);
}
}
}
stlock(name);
return (0);
}
static int
cklock(char *name)
{
int ret;
pid_t lpid = -1;
char alpid[SIZEOFPID+2];
int fd;
fd = open(name, O_RDONLY);
DEBUG(4, "ulockf name %s\n", name);
if (fd == -1) {
if (errno == ENOENT) {
return (0);
}
DEBUG(4, "Lock File--can't read (errno %d) --remove it!\n",
errno);
goto unlk;
}
ret = read(fd, (char *)alpid, SIZEOFPID + 1);
(void) close(fd);
if (ret != (SIZEOFPID+1)) {
DEBUG(4, "Lock File--bad format--remove it!\n%s", "");
goto unlk;
}
lpid = (pid_t)strtol(alpid, NULL, 10);
if ((ret = kill(lpid, 0)) == 0 || errno == EPERM) {
DEBUG(4, "Lock File--process still active--not removed\n%s",
"");
return (FAIL);
}
DEBUG(4, "kill pid (%ld), ", (long)lpid);
DEBUG(4, "returned %d", ret);
DEBUG(4, "--ok to remove lock file (%s)\n", name);
unlk:
if (unlink(name) != 0) {
DEBUG(4, "ulockf failed in unlink()\n%s", "");
return (FAIL);
}
return (0);
}
#define MAXLOCKS 10
static char *Lockfile[MAXLOCKS];
static int Nlocks = 0;
static void
stlock(char *name)
{
int i;
char *p;
for (i = 0; i < Nlocks; i++) {
if (Lockfile[i] == NULL)
break;
}
ASSERT(i < MAXLOCKS, "TOO MANY LOCKS", "", i);
if (i >= Nlocks)
i = Nlocks++;
p = calloc((unsigned)strlen(name) + 1, sizeof (char));
ASSERT(p != NULL, "CAN NOT ALLOCATE FOR", name, 0);
(void) strcpy(p, name);
Lockfile[i] = p;
}
static void
rmlock(char *name)
{
int i;
for (i = 0; i < Nlocks; i++) {
if (Lockfile[i] == NULL)
continue;
if (name == NULL || EQUALS(name, Lockfile[i])) {
(void) unlink(Lockfile[i]);
free(Lockfile[i]);
Lockfile[i] = NULL;
}
}
}
static int
onelock(char *pid, char *tempfile, char *name)
{
int fd;
char cb[100];
fd = creat(tempfile, (mode_t)0444);
if (fd < 0) {
(void) snprintf(cb, sizeof (cb),
"%s %s %d", tempfile, name, errno);
logent("ULOCKC", cb);
if ((errno == EMFILE) || (errno == ENFILE))
(void) unlink(tempfile);
return (-1);
}
if (write(fd, pid, SIZEOFPID+1) != (SIZEOFPID+1)) {
(void) snprintf(cb, sizeof (cb),
"%s %s %d", tempfile, name, errno);
logent("ULOCKW", cb);
(void) unlink(tempfile);
return (-1);
}
(void) chmod(tempfile, (mode_t)0444);
(void) chown(tempfile, UUCPUID, UUCPGID);
(void) close(fd);
if (link(tempfile, name) < 0) {
DEBUG(4, "%s: ", strerror(errno));
DEBUG(4, "link(%s, ", tempfile);
DEBUG(4, "%s)\n", name);
if (unlink(tempfile) < 0) {
(void) snprintf(cb, sizeof (cb),
"ULK err %s %d", tempfile, errno);
logent("ULOCKLNK", cb);
}
return (-1);
}
if (unlink(tempfile) < 0) {
(void) snprintf(cb, sizeof (cb), "%s %d", tempfile, errno);
logent("ULOCKF", cb);
}
return (0);
}
static int
fd_mklock(int fd)
{
int tries = 0;
struct stat64 _st_buf;
char lockname[BUFSIZ];
if (fstat64(fd, &_st_buf) != 0)
return (FAIL);
(void) snprintf(lockname, sizeof (lockname),
"%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
(unsigned long)major(_st_buf.st_dev),
(unsigned long)major(_st_buf.st_rdev),
(unsigned long)minor(_st_buf.st_rdev));
if (mklock(lockname) == FAIL)
return (FAIL);
while (lockf(fd, F_TLOCK, 0L) != 0) {
DEBUG(7, "fd_mklock: lockf returns %d\n", errno);
if ((++tries >= MAX_LOCKTRY) || (errno != EAGAIN)) {
rmlock(lockname);
logent("fd_mklock", "lockf failed");
return (FAIL);
}
(void) sleep(2);
}
DEBUG(7, "fd_mklock: ok\n%s", "");
return (SUCCESS);
}
static void
fd_rmlock(int fd)
{
struct stat64 _st_buf;
char lockname[BUFSIZ];
if (fstat64(fd, &_st_buf) == 0) {
(void) snprintf(lockname, sizeof (lockname),
"%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
(unsigned long)major(_st_buf.st_dev),
(unsigned long)major(_st_buf.st_rdev),
(unsigned long)minor(_st_buf.st_rdev));
rmlock(lockname);
}
(void) lockf(fd, F_ULOCK, 0L);
}