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  */
22*1219Sraf 
230Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
240Sstevel@tonic-gate /*	  All Rights Reserved  	*/
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
27*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
28*1219Sraf  * Use is subject to license terms.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate /*LINTLIBRARY*/
330Sstevel@tonic-gate 
34*1219Sraf #include "c_synonyms.h"
350Sstevel@tonic-gate #include "maillock.h"
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <fcntl.h>
380Sstevel@tonic-gate #include <stdio.h>
390Sstevel@tonic-gate #include <string.h>
400Sstevel@tonic-gate #include <unistd.h>
410Sstevel@tonic-gate #include <stdlib.h>
420Sstevel@tonic-gate #include <utime.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <sys/stat.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static	char	*lockext = ".lock";	/* Lock suffix for mailname */
470Sstevel@tonic-gate static	char	curlock[PATHSIZE];	/* Last used name of lock */
480Sstevel@tonic-gate static	int	locked;			/* To note that we locked it */
490Sstevel@tonic-gate static	time_t	locktime;		/* time lock file was touched */
500Sstevel@tonic-gate static time_t	lock1(char *, char *);
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * Lock the specified mail file by setting the file mailfile.lock.
540Sstevel@tonic-gate  * We must, of course, be careful to remove the lock file by a call
550Sstevel@tonic-gate  * to unlock before we stop.  The algorithm used here is to see if
560Sstevel@tonic-gate  * the lock exists, and if it does, to check its modify time.  If it
570Sstevel@tonic-gate  * is older than 5 minutes, we assume error and set our own file.
580Sstevel@tonic-gate  * Otherwise, we wait for 5 seconds and try again.
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*ARGSUSED*/
620Sstevel@tonic-gate int
630Sstevel@tonic-gate maillock(char *user, int retrycnt)
640Sstevel@tonic-gate {
650Sstevel@tonic-gate 	time_t t;
660Sstevel@tonic-gate 	struct stat sbuf;
670Sstevel@tonic-gate 	int statfailed;
680Sstevel@tonic-gate 	char locktmp[PATHSIZE];	/* Usable lock temporary */
690Sstevel@tonic-gate 	char file[PATHSIZE];
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	if (locked)
720Sstevel@tonic-gate 		return (0);
730Sstevel@tonic-gate 	(void) strcpy(file, MAILDIR);
740Sstevel@tonic-gate 	(void) strcat(file, user);
750Sstevel@tonic-gate 	(void) strcpy(curlock, file);
760Sstevel@tonic-gate 	(void) strcat(curlock, lockext);
770Sstevel@tonic-gate 	(void) strcpy(locktmp, file);
780Sstevel@tonic-gate 	(void) strcat(locktmp, "XXXXXX");
790Sstevel@tonic-gate 	(void) mktemp(locktmp);
800Sstevel@tonic-gate 	(void) remove(locktmp);
810Sstevel@tonic-gate 	statfailed = 0;
820Sstevel@tonic-gate 	for (;;) {
830Sstevel@tonic-gate 		t = lock1(locktmp, curlock);
840Sstevel@tonic-gate 		if (t == (time_t)0) {
850Sstevel@tonic-gate 			locked = 1;
860Sstevel@tonic-gate 			locktime = time(0);
870Sstevel@tonic-gate 			return (0);
880Sstevel@tonic-gate 		}
890Sstevel@tonic-gate 		if (stat(curlock, &sbuf) < 0) {
900Sstevel@tonic-gate 			if (statfailed++ > 5)
910Sstevel@tonic-gate 				return (-1);
920Sstevel@tonic-gate 			(void) sleep(5);
930Sstevel@tonic-gate 			continue;
940Sstevel@tonic-gate 		}
950Sstevel@tonic-gate 		statfailed = 0;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 		/*
980Sstevel@tonic-gate 		 * Compare the time of the temp file with the time
990Sstevel@tonic-gate 		 * of the lock file, rather than with the current
1000Sstevel@tonic-gate 		 * time of day, since the files may reside on
1010Sstevel@tonic-gate 		 * another machine whose time of day differs from
1020Sstevel@tonic-gate 		 * ours.  If the lock file is less than 5 minutes
1030Sstevel@tonic-gate 		 * old, keep trying.
1040Sstevel@tonic-gate 		 */
1050Sstevel@tonic-gate 		if (t < sbuf.st_ctime + 300) {
1060Sstevel@tonic-gate 			(void) sleep(5);
1070Sstevel@tonic-gate 			continue;
1080Sstevel@tonic-gate 		}
1090Sstevel@tonic-gate 		(void) remove(curlock);
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate  * Remove the mail lock, and note that we no longer
1150Sstevel@tonic-gate  * have it locked.
1160Sstevel@tonic-gate  */
1170Sstevel@tonic-gate void
1180Sstevel@tonic-gate mailunlock(void)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	(void) remove(curlock);
1210Sstevel@tonic-gate 	locked = 0;
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate  * Attempt to set the lock by creating the temporary file,
1260Sstevel@tonic-gate  * then doing a link/unlink.  If it succeeds, return 0,
1270Sstevel@tonic-gate  * else return a guess of the current time on the machine
1280Sstevel@tonic-gate  * holding the file.
1290Sstevel@tonic-gate  */
1300Sstevel@tonic-gate static time_t
1310Sstevel@tonic-gate lock1(char tempfile[], char name[])
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	int fd;
1340Sstevel@tonic-gate 	struct stat sbuf;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	fd = open(tempfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1370Sstevel@tonic-gate 	if (fd < 0)
1380Sstevel@tonic-gate 		return (time(0));
1390Sstevel@tonic-gate 	(void) fstat(fd, &sbuf);
1400Sstevel@tonic-gate 	/*
1410Sstevel@tonic-gate 	 * Write the string "0" into the lock file to give us some
1420Sstevel@tonic-gate 	 * interoperability with SVR4 mailers.  SVR4 mailers expect
1430Sstevel@tonic-gate 	 * a process ID to be written into the lock file and then
1440Sstevel@tonic-gate 	 * use kill() to see if the process is alive or not.  We write
1450Sstevel@tonic-gate 	 * 0 into it so that SVR4 mailers will always think our lock file
1460Sstevel@tonic-gate 	 * is valid.
1470Sstevel@tonic-gate 	 */
1480Sstevel@tonic-gate 	(void) write(fd, "0", 2);
1490Sstevel@tonic-gate 	(void) close(fd);
1500Sstevel@tonic-gate 	if (link(tempfile, name) < 0) {
1510Sstevel@tonic-gate 		(void) remove(tempfile);
1520Sstevel@tonic-gate 		return (sbuf.st_ctime);
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 	(void) remove(tempfile);
1550Sstevel@tonic-gate 	return ((time_t)0);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /*
1590Sstevel@tonic-gate  * Update the change time on the lock file so
1600Sstevel@tonic-gate  * others will know we're still using it.
1610Sstevel@tonic-gate  */
1620Sstevel@tonic-gate void
1630Sstevel@tonic-gate touchlock(void)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate 	struct stat sbuf;
1660Sstevel@tonic-gate 	time_t t;
1670Sstevel@tonic-gate 	struct utimbuf tp;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	if (!locked)
1700Sstevel@tonic-gate 		return;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	/* if it hasn't been at least 3 minutes, don't bother */
1730Sstevel@tonic-gate 	if (time(&t) < locktime + 180)
1740Sstevel@tonic-gate 		return;
1750Sstevel@tonic-gate 	locktime = t;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if (stat(curlock, &sbuf) < 0)
1780Sstevel@tonic-gate 		return;
1790Sstevel@tonic-gate 	/*
1800Sstevel@tonic-gate 	 * Don't actually change the times, we just want the
1810Sstevel@tonic-gate 	 * side effect that utime causes st_ctime to be set
1820Sstevel@tonic-gate 	 * to the current time.
1830Sstevel@tonic-gate 	 */
1840Sstevel@tonic-gate 	tp.actime = sbuf.st_atime;
1850Sstevel@tonic-gate 	tp.modtime = sbuf.st_mtime;
1860Sstevel@tonic-gate 	(void) utime(curlock, &tp);
1870Sstevel@tonic-gate }
188