10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
22132Srobinson
230Sstevel@tonic-gate /*
24*1219Sraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25132Srobinson * Use is subject to license terms.
260Sstevel@tonic-gate */
27*1219Sraf
280Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
290Sstevel@tonic-gate /* All Rights Reserved */
300Sstevel@tonic-gate
31132Srobinson #pragma ident "%Z%%M% %I% %E% SMI"
320Sstevel@tonic-gate
33*1219Sraf #include "mt.h"
340Sstevel@tonic-gate #include "uucp.h"
350Sstevel@tonic-gate
36*1219Sraf static void stlock(char *);
37*1219Sraf static int onelock(char *, char *, char *);
380Sstevel@tonic-gate
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate * make a lock file with given 'name'
410Sstevel@tonic-gate * If one already exists, send a signal 0 to the process--if
420Sstevel@tonic-gate * it fails, then unlink it and make a new one.
430Sstevel@tonic-gate *
440Sstevel@tonic-gate * input:
450Sstevel@tonic-gate * name - name of the lock file to make
460Sstevel@tonic-gate *
470Sstevel@tonic-gate * return:
480Sstevel@tonic-gate * 0 -> success
490Sstevel@tonic-gate * FAIL -> failure
500Sstevel@tonic-gate */
510Sstevel@tonic-gate
52132Srobinson static int
mklock(char * name)53132Srobinson mklock(char *name)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate static char pid[SIZEOFPID+2] = { '\0' }; /* +2 for '\n' and NULL */
560Sstevel@tonic-gate static char *tempfile;
570Sstevel@tonic-gate
580Sstevel@tonic-gate if (pid[0] == '\0') {
59132Srobinson tempfile = malloc(MAXNAMESIZE);
600Sstevel@tonic-gate if (tempfile == NULL)
610Sstevel@tonic-gate return (FAIL);
62132Srobinson (void) sprintf(pid, "%*ld\n", SIZEOFPID, (long)getpid());
63132Srobinson (void) snprintf(tempfile, MAXNAMESIZE, "%s/LTMP.%ld", X_LOCKDIR,
64132Srobinson (long)getpid());
650Sstevel@tonic-gate }
660Sstevel@tonic-gate
670Sstevel@tonic-gate if (onelock(pid, tempfile, name) == -1) {
680Sstevel@tonic-gate (void) unlink(tempfile);
69132Srobinson if (cklock(name))
700Sstevel@tonic-gate return (FAIL);
710Sstevel@tonic-gate else {
720Sstevel@tonic-gate if (onelock(pid, tempfile, name)) {
730Sstevel@tonic-gate (void) unlink(tempfile);
74132Srobinson DEBUG(4, "ulockf failed in onelock()\n%s", "");
750Sstevel@tonic-gate return (FAIL);
760Sstevel@tonic-gate }
770Sstevel@tonic-gate }
780Sstevel@tonic-gate }
790Sstevel@tonic-gate stlock(name);
800Sstevel@tonic-gate return (0);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate * check to see if the lock file exists and is still active
85132Srobinson * - use kill(pid, 0)
860Sstevel@tonic-gate *
870Sstevel@tonic-gate * return:
880Sstevel@tonic-gate * 0 -> success (lock file removed - no longer active
890Sstevel@tonic-gate * FAIL -> lock file still active
900Sstevel@tonic-gate */
91132Srobinson static int
cklock(char * name)92132Srobinson cklock(char *name)
930Sstevel@tonic-gate {
94132Srobinson int ret;
950Sstevel@tonic-gate pid_t lpid = -1;
960Sstevel@tonic-gate char alpid[SIZEOFPID+2]; /* +2 for '\n' and NULL */
970Sstevel@tonic-gate int fd;
980Sstevel@tonic-gate
990Sstevel@tonic-gate fd = open(name, O_RDONLY);
1000Sstevel@tonic-gate DEBUG(4, "ulockf name %s\n", name);
1010Sstevel@tonic-gate if (fd == -1) {
1020Sstevel@tonic-gate if (errno == ENOENT) { /* file does not exist -- OK */
103132Srobinson return (0);
1040Sstevel@tonic-gate }
105132Srobinson DEBUG(4, "Lock File--can't read (errno %d) --remove it!\n",
106132Srobinson errno);
1070Sstevel@tonic-gate goto unlk;
1080Sstevel@tonic-gate }
109132Srobinson ret = read(fd, (char *)alpid, SIZEOFPID + 1); /* +1 for '\n' */
1100Sstevel@tonic-gate (void) close(fd);
1110Sstevel@tonic-gate if (ret != (SIZEOFPID+1)) {
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate DEBUG(4, "Lock File--bad format--remove it!\n%s", "");
1140Sstevel@tonic-gate goto unlk;
1150Sstevel@tonic-gate }
116132Srobinson lpid = (pid_t)strtol(alpid, NULL, 10);
117132Srobinson if ((ret = kill(lpid, 0)) == 0 || errno == EPERM) {
118132Srobinson DEBUG(4, "Lock File--process still active--not removed\n%s",
119132Srobinson "");
1200Sstevel@tonic-gate return (FAIL);
1210Sstevel@tonic-gate }
122132Srobinson /* process no longer active */
123132Srobinson DEBUG(4, "kill pid (%ld), ", (long)lpid);
124132Srobinson DEBUG(4, "returned %d", ret);
125132Srobinson DEBUG(4, "--ok to remove lock file (%s)\n", name);
1260Sstevel@tonic-gate unlk:
127132Srobinson
1280Sstevel@tonic-gate if (unlink(name) != 0) {
129132Srobinson DEBUG(4, "ulockf failed in unlink()\n%s", "");
1300Sstevel@tonic-gate return (FAIL);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate return (0);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate
135132Srobinson #define MAXLOCKS 10 /* maximum number of lock files */
1360Sstevel@tonic-gate static char *Lockfile[MAXLOCKS];
137132Srobinson static int Nlocks = 0;
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate /*
1400Sstevel@tonic-gate * put name in list of lock files
1410Sstevel@tonic-gate * return:
1420Sstevel@tonic-gate * none
1430Sstevel@tonic-gate */
1440Sstevel@tonic-gate static void
stlock(char * name)145132Srobinson stlock(char *name)
1460Sstevel@tonic-gate {
147132Srobinson int i;
1480Sstevel@tonic-gate char *p;
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate for (i = 0; i < Nlocks; i++) {
1510Sstevel@tonic-gate if (Lockfile[i] == NULL)
1520Sstevel@tonic-gate break;
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate ASSERT(i < MAXLOCKS, "TOO MANY LOCKS", "", i);
1550Sstevel@tonic-gate if (i >= Nlocks)
1560Sstevel@tonic-gate i = Nlocks++;
157132Srobinson p = calloc((unsigned)strlen(name) + 1, sizeof (char));
1580Sstevel@tonic-gate ASSERT(p != NULL, "CAN NOT ALLOCATE FOR", name, 0);
1590Sstevel@tonic-gate (void) strcpy(p, name);
1600Sstevel@tonic-gate Lockfile[i] = p;
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate * remove the named lock. If named lock is NULL,
1650Sstevel@tonic-gate * then remove all locks currently in list.
1660Sstevel@tonic-gate * return:
1670Sstevel@tonic-gate * none
1680Sstevel@tonic-gate */
169132Srobinson static void
rmlock(char * name)170132Srobinson rmlock(char *name)
1710Sstevel@tonic-gate {
172132Srobinson int i;
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate for (i = 0; i < Nlocks; i++) {
1750Sstevel@tonic-gate if (Lockfile[i] == NULL)
1760Sstevel@tonic-gate continue;
1770Sstevel@tonic-gate if (name == NULL || EQUALS(name, Lockfile[i])) {
1780Sstevel@tonic-gate (void) unlink(Lockfile[i]);
1790Sstevel@tonic-gate free(Lockfile[i]);
1800Sstevel@tonic-gate Lockfile[i] = NULL;
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate * makes a lock on behalf of pid.
1870Sstevel@tonic-gate * input:
1880Sstevel@tonic-gate * pid - process id
1890Sstevel@tonic-gate * tempfile - name of a temporary in the same file system
1900Sstevel@tonic-gate * name - lock file name (full path name)
1910Sstevel@tonic-gate * return:
1920Sstevel@tonic-gate * -1 - failed
1930Sstevel@tonic-gate * 0 - lock made successfully
1940Sstevel@tonic-gate */
1950Sstevel@tonic-gate static int
onelock(char * pid,char * tempfile,char * name)196132Srobinson onelock(char *pid, char *tempfile, char *name)
197132Srobinson {
198132Srobinson int fd;
1990Sstevel@tonic-gate char cb[100];
2000Sstevel@tonic-gate
201132Srobinson fd = creat(tempfile, (mode_t)0444);
202132Srobinson if (fd < 0) {
203132Srobinson (void) snprintf(cb, sizeof (cb),
204132Srobinson "%s %s %d", tempfile, name, errno);
2050Sstevel@tonic-gate logent("ULOCKC", cb);
2060Sstevel@tonic-gate if ((errno == EMFILE) || (errno == ENFILE))
2070Sstevel@tonic-gate (void) unlink(tempfile);
2080Sstevel@tonic-gate return (-1);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate /* +1 for '\n' */
2110Sstevel@tonic-gate if (write(fd, pid, SIZEOFPID+1) != (SIZEOFPID+1)) {
212132Srobinson (void) snprintf(cb, sizeof (cb),
213132Srobinson "%s %s %d", tempfile, name, errno);
2140Sstevel@tonic-gate logent("ULOCKW", cb);
2150Sstevel@tonic-gate (void) unlink(tempfile);
2160Sstevel@tonic-gate return (-1);
2170Sstevel@tonic-gate }
218132Srobinson (void) chmod(tempfile, (mode_t)0444);
2190Sstevel@tonic-gate (void) chown(tempfile, UUCPUID, UUCPGID);
2200Sstevel@tonic-gate (void) close(fd);
221132Srobinson if (link(tempfile, name) < 0) {
2220Sstevel@tonic-gate DEBUG(4, "%s: ", strerror(errno));
2230Sstevel@tonic-gate DEBUG(4, "link(%s, ", tempfile);
2240Sstevel@tonic-gate DEBUG(4, "%s)\n", name);
225132Srobinson if (unlink(tempfile) < 0) {
226132Srobinson (void) snprintf(cb, sizeof (cb),
227132Srobinson "ULK err %s %d", tempfile, errno);
2280Sstevel@tonic-gate logent("ULOCKLNK", cb);
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate return (-1);
2310Sstevel@tonic-gate }
232132Srobinson if (unlink(tempfile) < 0) {
233132Srobinson (void) snprintf(cb, sizeof (cb), "%s %d", tempfile, errno);
2340Sstevel@tonic-gate logent("ULOCKF", cb);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate return (0);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate /*
2400Sstevel@tonic-gate * fd_mklock(fd) - lock the device indicated by fd is possible
2410Sstevel@tonic-gate *
2420Sstevel@tonic-gate * return -
2430Sstevel@tonic-gate * SUCCESS - this process now has the fd locked
2440Sstevel@tonic-gate * FAIL - this process was not able to lock the fd
2450Sstevel@tonic-gate */
2460Sstevel@tonic-gate
247132Srobinson static int
fd_mklock(int fd)248132Srobinson fd_mklock(int fd)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate int tries = 0;
2510Sstevel@tonic-gate struct stat64 _st_buf;
2520Sstevel@tonic-gate char lockname[BUFSIZ];
2530Sstevel@tonic-gate
254*1219Sraf if (fstat64(fd, &_st_buf) != 0)
2550Sstevel@tonic-gate return (FAIL);
2560Sstevel@tonic-gate
257132Srobinson (void) snprintf(lockname, sizeof (lockname),
258132Srobinson "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
259132Srobinson (unsigned long)major(_st_buf.st_dev),
260132Srobinson (unsigned long)major(_st_buf.st_rdev),
261132Srobinson (unsigned long)minor(_st_buf.st_rdev));
2620Sstevel@tonic-gate
263132Srobinson if (mklock(lockname) == FAIL)
2640Sstevel@tonic-gate return (FAIL);
265132Srobinson
266132Srobinson while (lockf(fd, F_TLOCK, 0L) != 0) {
2670Sstevel@tonic-gate DEBUG(7, "fd_mklock: lockf returns %d\n", errno);
2680Sstevel@tonic-gate if ((++tries >= MAX_LOCKTRY) || (errno != EAGAIN)) {
2690Sstevel@tonic-gate rmlock(lockname);
270132Srobinson logent("fd_mklock", "lockf failed");
2710Sstevel@tonic-gate return (FAIL);
2720Sstevel@tonic-gate }
273132Srobinson (void) sleep(2);
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate DEBUG(7, "fd_mklock: ok\n%s", "");
2760Sstevel@tonic-gate return (SUCCESS);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate /*
2800Sstevel@tonic-gate * remove the locks associated with the device file descriptor
2810Sstevel@tonic-gate *
2820Sstevel@tonic-gate * return -
2830Sstevel@tonic-gate * SUCCESS - both BNU lock file and advisory locks removed
284132Srobinson * FAIL -
2850Sstevel@tonic-gate */
2860Sstevel@tonic-gate
287132Srobinson static void
fd_rmlock(int fd)288132Srobinson fd_rmlock(int fd)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate struct stat64 _st_buf;
2910Sstevel@tonic-gate char lockname[BUFSIZ];
2920Sstevel@tonic-gate
293*1219Sraf if (fstat64(fd, &_st_buf) == 0) {
294132Srobinson (void) snprintf(lockname, sizeof (lockname),
295132Srobinson "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
296132Srobinson (unsigned long)major(_st_buf.st_dev),
297132Srobinson (unsigned long)major(_st_buf.st_rdev),
298132Srobinson (unsigned long)minor(_st_buf.st_rdev));
2990Sstevel@tonic-gate rmlock(lockname);
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate (void) lockf(fd, F_ULOCK, 0L);
3020Sstevel@tonic-gate }
303