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