/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI"
#include "maillock.h"
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <utime.h>
/*
* Lock the specified mail file by setting the file mailfile.lock.
* We must, of course, be careful to remove the lock file by a call
* to unlock before we stop. The algorithm used here is to see if
* the lock exists, and if it does, to check its modify time. If it
* is older than 5 minutes, we assume error and set our own file.
* Otherwise, we wait for 5 seconds and try again.
*/
/*ARGSUSED*/
int
{
time_t t;
int statfailed;
if (locked)
return (0);
statfailed = 0;
for (;;) {
if (t == (time_t)0) {
locked = 1;
return (0);
}
if (statfailed++ > 5)
return (-1);
(void) sleep(5);
continue;
}
statfailed = 0;
/*
* Compare the time of the temp file with the time
* of the lock file, rather than with the current
* time of day, since the files may reside on
* another machine whose time of day differs from
* ours. If the lock file is less than 5 minutes
* old, keep trying.
*/
(void) sleep(5);
continue;
}
}
}
/*
* Remove the mail lock, and note that we no longer
* have it locked.
*/
void
mailunlock(void)
{
locked = 0;
}
/*
* Attempt to set the lock by creating the temporary file,
* else return a guess of the current time on the machine
* holding the file.
*/
static time_t
{
int fd;
if (fd < 0)
return (time(0));
/*
* Write the string "0" into the lock file to give us some
* interoperability with SVR4 mailers. SVR4 mailers expect
* a process ID to be written into the lock file and then
* use kill() to see if the process is alive or not. We write
* 0 into it so that SVR4 mailers will always think our lock file
* is valid.
*/
}
return ((time_t)0);
}
/*
* Update the change time on the lock file so
* others will know we're still using it.
*/
void
touchlock(void)
{
time_t t;
if (!locked)
return;
/* if it hasn't been at least 3 minutes, don't bother */
return;
locktime = t;
return;
/*
* Don't actually change the times, we just want the
* side effect that utime causes st_ctime to be set
* to the current time.
*/
}