xref: /freebsd/contrib/sendmail/libsmutil/lockfile.c (revision ee7b0571c2c18bdec848ed2044223cc88db29bd8)
106f25ae9SGregory Neil Shapiro /*
25dd76dd0SGregory Neil Shapiro  * Copyright (c) 1998-2001 Proofpoint, Inc. and its suppliers.
306f25ae9SGregory Neil Shapiro  *	All rights reserved.
406f25ae9SGregory Neil Shapiro  * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
506f25ae9SGregory Neil Shapiro  * Copyright (c) 1988, 1993
606f25ae9SGregory Neil Shapiro  *	The Regents of the University of California.  All rights reserved.
706f25ae9SGregory Neil Shapiro  *
806f25ae9SGregory Neil Shapiro  * By using this file, you agree to the terms and conditions set
906f25ae9SGregory Neil Shapiro  * forth in the LICENSE file which can be found at the top level of
1006f25ae9SGregory Neil Shapiro  * the sendmail distribution.
1106f25ae9SGregory Neil Shapiro  *
1206f25ae9SGregory Neil Shapiro  */
1306f25ae9SGregory Neil Shapiro 
1406f25ae9SGregory Neil Shapiro #include <sendmail.h>
1506f25ae9SGregory Neil Shapiro 
16*4313cc83SGregory Neil Shapiro SM_RCSID("@(#)$Id: lockfile.c,v 8.22 2013-11-22 20:51:50 ca Exp $")
1740266059SGregory Neil Shapiro 
1840266059SGregory Neil Shapiro 
1940266059SGregory Neil Shapiro /*
2006f25ae9SGregory Neil Shapiro **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
2106f25ae9SGregory Neil Shapiro **
2206f25ae9SGregory Neil Shapiro **	Parameters:
2306f25ae9SGregory Neil Shapiro **		fd -- the file descriptor of the file.
2406f25ae9SGregory Neil Shapiro **		filename -- the file name (for error messages). [unused]
2506f25ae9SGregory Neil Shapiro **		ext -- the filename extension. [unused]
2606f25ae9SGregory Neil Shapiro **		type -- type of the lock.  Bits can be:
2706f25ae9SGregory Neil Shapiro **			LOCK_EX -- exclusive lock.
2806f25ae9SGregory Neil Shapiro **			LOCK_NB -- non-blocking.
29193538b7SGregory Neil Shapiro **			LOCK_UN -- unlock.
3006f25ae9SGregory Neil Shapiro **
3106f25ae9SGregory Neil Shapiro **	Returns:
3240266059SGregory Neil Shapiro **		true if the lock was acquired.
3340266059SGregory Neil Shapiro **		false otherwise.
3406f25ae9SGregory Neil Shapiro */
3506f25ae9SGregory Neil Shapiro 
3606f25ae9SGregory Neil Shapiro bool
3706f25ae9SGregory Neil Shapiro lockfile(fd, filename, ext, type)
3806f25ae9SGregory Neil Shapiro 	int fd;
3906f25ae9SGregory Neil Shapiro 	char *filename;
4006f25ae9SGregory Neil Shapiro 	char *ext;
4106f25ae9SGregory Neil Shapiro 	int type;
4206f25ae9SGregory Neil Shapiro {
4306f25ae9SGregory Neil Shapiro #if !HASFLOCK
4406f25ae9SGregory Neil Shapiro 	int action;
4506f25ae9SGregory Neil Shapiro 	struct flock lfd;
4606f25ae9SGregory Neil Shapiro 
4706f25ae9SGregory Neil Shapiro 	memset(&lfd, '\0', sizeof lfd);
4806f25ae9SGregory Neil Shapiro 	if (bitset(LOCK_UN, type))
4906f25ae9SGregory Neil Shapiro 		lfd.l_type = F_UNLCK;
5006f25ae9SGregory Neil Shapiro 	else if (bitset(LOCK_EX, type))
5106f25ae9SGregory Neil Shapiro 		lfd.l_type = F_WRLCK;
5206f25ae9SGregory Neil Shapiro 	else
5306f25ae9SGregory Neil Shapiro 		lfd.l_type = F_RDLCK;
5406f25ae9SGregory Neil Shapiro 	if (bitset(LOCK_NB, type))
5506f25ae9SGregory Neil Shapiro 		action = F_SETLK;
5606f25ae9SGregory Neil Shapiro 	else
5706f25ae9SGregory Neil Shapiro 		action = F_SETLKW;
5806f25ae9SGregory Neil Shapiro 
5906f25ae9SGregory Neil Shapiro 	if (fcntl(fd, action, &lfd) >= 0)
6040266059SGregory Neil Shapiro 		return true;
6106f25ae9SGregory Neil Shapiro 
6206f25ae9SGregory Neil Shapiro 	/*
6306f25ae9SGregory Neil Shapiro 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
6406f25ae9SGregory Neil Shapiro 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
6506f25ae9SGregory Neil Shapiro 	**  as type "tmp" (that is, served from swap space), the
6606f25ae9SGregory Neil Shapiro 	**  previous fcntl will fail with "Invalid argument" errors.
6706f25ae9SGregory Neil Shapiro 	**  Since this is fairly common during testing, we will assume
6806f25ae9SGregory Neil Shapiro 	**  that this indicates that the lock is successfully grabbed.
6906f25ae9SGregory Neil Shapiro 	*/
7006f25ae9SGregory Neil Shapiro 
7106f25ae9SGregory Neil Shapiro 	if (errno == EINVAL)
7240266059SGregory Neil Shapiro 		return true;
7306f25ae9SGregory Neil Shapiro 
7406f25ae9SGregory Neil Shapiro #else /* !HASFLOCK */
7506f25ae9SGregory Neil Shapiro 
7606f25ae9SGregory Neil Shapiro 	if (flock(fd, type) >= 0)
7740266059SGregory Neil Shapiro 		return true;
7806f25ae9SGregory Neil Shapiro 
7906f25ae9SGregory Neil Shapiro #endif /* !HASFLOCK */
8006f25ae9SGregory Neil Shapiro 
8140266059SGregory Neil Shapiro 	return false;
8206f25ae9SGregory Neil Shapiro }
83