17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
2261961e0fSrobinson
237c478bd9Sstevel@tonic-gate /*
24*e8031f0aSraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2561961e0fSrobinson * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate */
27*e8031f0aSraf
287c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
297c478bd9Sstevel@tonic-gate /* All Rights Reserved */
307c478bd9Sstevel@tonic-gate
3161961e0fSrobinson #pragma ident "%Z%%M% %I% %E% SMI"
327c478bd9Sstevel@tonic-gate
33*e8031f0aSraf #include "mt.h"
347c478bd9Sstevel@tonic-gate #include "uucp.h"
357c478bd9Sstevel@tonic-gate
36*e8031f0aSraf static void stlock(char *);
37*e8031f0aSraf static int onelock(char *, char *, char *);
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate * make a lock file with given 'name'
417c478bd9Sstevel@tonic-gate * If one already exists, send a signal 0 to the process--if
427c478bd9Sstevel@tonic-gate * it fails, then unlink it and make a new one.
437c478bd9Sstevel@tonic-gate *
447c478bd9Sstevel@tonic-gate * input:
457c478bd9Sstevel@tonic-gate * name - name of the lock file to make
467c478bd9Sstevel@tonic-gate *
477c478bd9Sstevel@tonic-gate * return:
487c478bd9Sstevel@tonic-gate * 0 -> success
497c478bd9Sstevel@tonic-gate * FAIL -> failure
507c478bd9Sstevel@tonic-gate */
517c478bd9Sstevel@tonic-gate
5261961e0fSrobinson static int
mklock(char * name)5361961e0fSrobinson mklock(char *name)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate static char pid[SIZEOFPID+2] = { '\0' }; /* +2 for '\n' and NULL */
567c478bd9Sstevel@tonic-gate static char *tempfile;
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate if (pid[0] == '\0') {
5961961e0fSrobinson tempfile = malloc(MAXNAMESIZE);
607c478bd9Sstevel@tonic-gate if (tempfile == NULL)
617c478bd9Sstevel@tonic-gate return (FAIL);
627c478bd9Sstevel@tonic-gate (void) sprintf(pid, "%*ld\n", SIZEOFPID, (long)getpid());
6361961e0fSrobinson (void) snprintf(tempfile, MAXNAMESIZE, "%s/LTMP.%ld", X_LOCKDIR,
6461961e0fSrobinson (long)getpid());
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate if (onelock(pid, tempfile, name) == -1) {
687c478bd9Sstevel@tonic-gate (void) unlink(tempfile);
6961961e0fSrobinson if (cklock(name))
707c478bd9Sstevel@tonic-gate return (FAIL);
717c478bd9Sstevel@tonic-gate else {
727c478bd9Sstevel@tonic-gate if (onelock(pid, tempfile, name)) {
737c478bd9Sstevel@tonic-gate (void) unlink(tempfile);
747c478bd9Sstevel@tonic-gate DEBUG(4, "ulockf failed in onelock()\n%s", "");
757c478bd9Sstevel@tonic-gate return (FAIL);
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate stlock(name);
807c478bd9Sstevel@tonic-gate return (0);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate * check to see if the lock file exists and is still active
857c478bd9Sstevel@tonic-gate * - use kill(pid, 0)
867c478bd9Sstevel@tonic-gate *
877c478bd9Sstevel@tonic-gate * return:
887c478bd9Sstevel@tonic-gate * 0 -> success (lock file removed - no longer active
897c478bd9Sstevel@tonic-gate * FAIL -> lock file still active
907c478bd9Sstevel@tonic-gate */
9161961e0fSrobinson static int
cklock(char * name)9261961e0fSrobinson cklock(char *name)
937c478bd9Sstevel@tonic-gate {
9461961e0fSrobinson int ret;
957c478bd9Sstevel@tonic-gate pid_t lpid = -1;
967c478bd9Sstevel@tonic-gate char alpid[SIZEOFPID+2]; /* +2 for '\n' and NULL */
977c478bd9Sstevel@tonic-gate int fd;
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate fd = open(name, O_RDONLY);
1007c478bd9Sstevel@tonic-gate DEBUG(4, "ulockf name %s\n", name);
1017c478bd9Sstevel@tonic-gate if (fd == -1) {
1027c478bd9Sstevel@tonic-gate if (errno == ENOENT) { /* file does not exist -- OK */
1037c478bd9Sstevel@tonic-gate return (0);
1047c478bd9Sstevel@tonic-gate }
10561961e0fSrobinson DEBUG(4, "Lock File--can't read (errno %d) --remove it!\n",
10661961e0fSrobinson errno);
1077c478bd9Sstevel@tonic-gate goto unlk;
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate ret = read(fd, (char *)alpid, SIZEOFPID + 1); /* +1 for '\n' */
1107c478bd9Sstevel@tonic-gate (void) close(fd);
1117c478bd9Sstevel@tonic-gate if (ret != (SIZEOFPID+1)) {
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate DEBUG(4, "Lock File--bad format--remove it!\n%s", "");
1147c478bd9Sstevel@tonic-gate goto unlk;
1157c478bd9Sstevel@tonic-gate }
11661961e0fSrobinson lpid = (pid_t)strtol(alpid, NULL, 10);
1177c478bd9Sstevel@tonic-gate if ((ret = kill(lpid, 0)) == 0 || errno == EPERM) {
11861961e0fSrobinson DEBUG(4, "Lock File--process still active--not removed\n%s",
11961961e0fSrobinson "");
1207c478bd9Sstevel@tonic-gate return (FAIL);
1217c478bd9Sstevel@tonic-gate }
12261961e0fSrobinson /* process no longer active */
1237c478bd9Sstevel@tonic-gate DEBUG(4, "kill pid (%ld), ", (long)lpid);
1247c478bd9Sstevel@tonic-gate DEBUG(4, "returned %d", ret);
1257c478bd9Sstevel@tonic-gate DEBUG(4, "--ok to remove lock file (%s)\n", name);
1267c478bd9Sstevel@tonic-gate unlk:
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate if (unlink(name) != 0) {
1297c478bd9Sstevel@tonic-gate DEBUG(4, "ulockf failed in unlink()\n%s", "");
1307c478bd9Sstevel@tonic-gate return (FAIL);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate return (0);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate #define MAXLOCKS 10 /* maximum number of lock files */
1367c478bd9Sstevel@tonic-gate static char *Lockfile[MAXLOCKS];
13761961e0fSrobinson static int Nlocks = 0;
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate * put name in list of lock files
1417c478bd9Sstevel@tonic-gate * return:
1427c478bd9Sstevel@tonic-gate * none
1437c478bd9Sstevel@tonic-gate */
1447c478bd9Sstevel@tonic-gate static void
stlock(char * name)14561961e0fSrobinson stlock(char *name)
1467c478bd9Sstevel@tonic-gate {
14761961e0fSrobinson int i;
1487c478bd9Sstevel@tonic-gate char *p;
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate for (i = 0; i < Nlocks; i++) {
1517c478bd9Sstevel@tonic-gate if (Lockfile[i] == NULL)
1527c478bd9Sstevel@tonic-gate break;
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate ASSERT(i < MAXLOCKS, "TOO MANY LOCKS", "", i);
1557c478bd9Sstevel@tonic-gate if (i >= Nlocks)
1567c478bd9Sstevel@tonic-gate i = Nlocks++;
15761961e0fSrobinson p = calloc((unsigned)strlen(name) + 1, sizeof (char));
1587c478bd9Sstevel@tonic-gate ASSERT(p != NULL, "CAN NOT ALLOCATE FOR", name, 0);
1597c478bd9Sstevel@tonic-gate (void) strcpy(p, name);
1607c478bd9Sstevel@tonic-gate Lockfile[i] = p;
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate * remove the named lock. If named lock is NULL,
1657c478bd9Sstevel@tonic-gate * then remove all locks currently in list.
1667c478bd9Sstevel@tonic-gate * return:
1677c478bd9Sstevel@tonic-gate * none
1687c478bd9Sstevel@tonic-gate */
16961961e0fSrobinson static void
rmlock(char * name)17061961e0fSrobinson rmlock(char *name)
1717c478bd9Sstevel@tonic-gate {
17261961e0fSrobinson int i;
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate for (i = 0; i < Nlocks; i++) {
1757c478bd9Sstevel@tonic-gate if (Lockfile[i] == NULL)
1767c478bd9Sstevel@tonic-gate continue;
1777c478bd9Sstevel@tonic-gate if (name == NULL || EQUALS(name, Lockfile[i])) {
1787c478bd9Sstevel@tonic-gate (void) unlink(Lockfile[i]);
1797c478bd9Sstevel@tonic-gate free(Lockfile[i]);
1807c478bd9Sstevel@tonic-gate Lockfile[i] = NULL;
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate * makes a lock on behalf of pid.
1877c478bd9Sstevel@tonic-gate * input:
1887c478bd9Sstevel@tonic-gate * pid - process id
1897c478bd9Sstevel@tonic-gate * tempfile - name of a temporary in the same file system
1907c478bd9Sstevel@tonic-gate * name - lock file name (full path name)
1917c478bd9Sstevel@tonic-gate * return:
1927c478bd9Sstevel@tonic-gate * -1 - failed
1937c478bd9Sstevel@tonic-gate * 0 - lock made successfully
1947c478bd9Sstevel@tonic-gate */
1957c478bd9Sstevel@tonic-gate static int
onelock(char * pid,char * tempfile,char * name)19661961e0fSrobinson onelock(char *pid, char *tempfile, char *name)
1977c478bd9Sstevel@tonic-gate {
19861961e0fSrobinson int fd;
1997c478bd9Sstevel@tonic-gate char cb[100];
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate fd = creat(tempfile, (mode_t)0444);
2027c478bd9Sstevel@tonic-gate if (fd < 0) {
20361961e0fSrobinson (void) snprintf(cb, sizeof (cb),
20461961e0fSrobinson "%s %s %d", tempfile, name, errno);
2057c478bd9Sstevel@tonic-gate logent("ULOCKC", cb);
2067c478bd9Sstevel@tonic-gate if ((errno == EMFILE) || (errno == ENFILE))
2077c478bd9Sstevel@tonic-gate (void) unlink(tempfile);
2087c478bd9Sstevel@tonic-gate return (-1);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate /* +1 for '\n' */
2117c478bd9Sstevel@tonic-gate if (write(fd, pid, SIZEOFPID+1) != (SIZEOFPID+1)) {
21261961e0fSrobinson (void) snprintf(cb, sizeof (cb),
21361961e0fSrobinson "%s %s %d", tempfile, name, errno);
2147c478bd9Sstevel@tonic-gate logent("ULOCKW", cb);
2157c478bd9Sstevel@tonic-gate (void) unlink(tempfile);
2167c478bd9Sstevel@tonic-gate return (-1);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate (void) chmod(tempfile, (mode_t)0444);
2197c478bd9Sstevel@tonic-gate (void) chown(tempfile, UUCPUID, UUCPGID);
2207c478bd9Sstevel@tonic-gate (void) close(fd);
2217c478bd9Sstevel@tonic-gate if (link(tempfile, name) < 0) {
2227c478bd9Sstevel@tonic-gate DEBUG(4, "%s: ", strerror(errno));
2237c478bd9Sstevel@tonic-gate DEBUG(4, "link(%s, ", tempfile);
2247c478bd9Sstevel@tonic-gate DEBUG(4, "%s)\n", name);
2257c478bd9Sstevel@tonic-gate if (unlink(tempfile) < 0) {
22661961e0fSrobinson (void) snprintf(cb, sizeof (cb),
22761961e0fSrobinson "ULK err %s %d", tempfile, errno);
2287c478bd9Sstevel@tonic-gate logent("ULOCKLNK", cb);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate return (-1);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate if (unlink(tempfile) < 0) {
23361961e0fSrobinson (void) snprintf(cb, sizeof (cb), "%s %d", tempfile, errno);
2347c478bd9Sstevel@tonic-gate logent("ULOCKF", cb);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate return (0);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate /*
2407c478bd9Sstevel@tonic-gate * fd_mklock(fd) - lock the device indicated by fd is possible
2417c478bd9Sstevel@tonic-gate *
2427c478bd9Sstevel@tonic-gate * return -
2437c478bd9Sstevel@tonic-gate * SUCCESS - this process now has the fd locked
2447c478bd9Sstevel@tonic-gate * FAIL - this process was not able to lock the fd
2457c478bd9Sstevel@tonic-gate */
2467c478bd9Sstevel@tonic-gate
24761961e0fSrobinson static int
fd_mklock(int fd)24861961e0fSrobinson fd_mklock(int fd)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate int tries = 0;
2517c478bd9Sstevel@tonic-gate struct stat64 _st_buf;
2527c478bd9Sstevel@tonic-gate char lockname[BUFSIZ];
2537c478bd9Sstevel@tonic-gate
254*e8031f0aSraf if (fstat64(fd, &_st_buf) != 0)
2557c478bd9Sstevel@tonic-gate return (FAIL);
2567c478bd9Sstevel@tonic-gate
25761961e0fSrobinson (void) snprintf(lockname, sizeof (lockname),
25861961e0fSrobinson "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
2597c478bd9Sstevel@tonic-gate (unsigned long)major(_st_buf.st_dev),
2607c478bd9Sstevel@tonic-gate (unsigned long)major(_st_buf.st_rdev),
2617c478bd9Sstevel@tonic-gate (unsigned long)minor(_st_buf.st_rdev));
2627c478bd9Sstevel@tonic-gate
26361961e0fSrobinson if (mklock(lockname) == FAIL)
2647c478bd9Sstevel@tonic-gate return (FAIL);
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate while (lockf(fd, F_TLOCK, 0L) != 0) {
2677c478bd9Sstevel@tonic-gate DEBUG(7, "fd_mklock: lockf returns %d\n", errno);
2687c478bd9Sstevel@tonic-gate if ((++tries >= MAX_LOCKTRY) || (errno != EAGAIN)) {
2697c478bd9Sstevel@tonic-gate rmlock(lockname);
2707c478bd9Sstevel@tonic-gate logent("fd_mklock", "lockf failed");
2717c478bd9Sstevel@tonic-gate return (FAIL);
2727c478bd9Sstevel@tonic-gate }
27361961e0fSrobinson (void) sleep(2);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate DEBUG(7, "fd_mklock: ok\n%s", "");
2767c478bd9Sstevel@tonic-gate return (SUCCESS);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate * remove the locks associated with the device file descriptor
2817c478bd9Sstevel@tonic-gate *
2827c478bd9Sstevel@tonic-gate * return -
2837c478bd9Sstevel@tonic-gate * SUCCESS - both BNU lock file and advisory locks removed
2847c478bd9Sstevel@tonic-gate * FAIL -
2857c478bd9Sstevel@tonic-gate */
2867c478bd9Sstevel@tonic-gate
28761961e0fSrobinson static void
fd_rmlock(int fd)28861961e0fSrobinson fd_rmlock(int fd)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate struct stat64 _st_buf;
2917c478bd9Sstevel@tonic-gate char lockname[BUFSIZ];
2927c478bd9Sstevel@tonic-gate
293*e8031f0aSraf if (fstat64(fd, &_st_buf) == 0) {
29461961e0fSrobinson (void) snprintf(lockname, sizeof (lockname),
29561961e0fSrobinson "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
2967c478bd9Sstevel@tonic-gate (unsigned long)major(_st_buf.st_dev),
2977c478bd9Sstevel@tonic-gate (unsigned long)major(_st_buf.st_rdev),
2987c478bd9Sstevel@tonic-gate (unsigned long)minor(_st_buf.st_rdev));
2997c478bd9Sstevel@tonic-gate rmlock(lockname);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate (void) lockf(fd, F_ULOCK, 0L);
3027c478bd9Sstevel@tonic-gate }
303