1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.
3*7c478bd9Sstevel@tonic-gate  *	All rights reserved.
4*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
5*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1988, 1993
6*7c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
7*7c478bd9Sstevel@tonic-gate  *
8*7c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
9*7c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
10*7c478bd9Sstevel@tonic-gate  * the sendmail distribution.
11*7c478bd9Sstevel@tonic-gate  *
12*7c478bd9Sstevel@tonic-gate  */
13*7c478bd9Sstevel@tonic-gate 
14*7c478bd9Sstevel@tonic-gate #include <sendmail.h>
15*7c478bd9Sstevel@tonic-gate 
16*7c478bd9Sstevel@tonic-gate SM_RCSID("@(#)$Id: lockfile.c,v 8.21 2003/11/10 22:57:38 ca Exp $")
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate 
19*7c478bd9Sstevel@tonic-gate /*
20*7c478bd9Sstevel@tonic-gate **  LOCKFILE -- lock a file using flock or (shudder) fcntl locking
21*7c478bd9Sstevel@tonic-gate **
22*7c478bd9Sstevel@tonic-gate **	Parameters:
23*7c478bd9Sstevel@tonic-gate **		fd -- the file descriptor of the file.
24*7c478bd9Sstevel@tonic-gate **		filename -- the file name (for error messages). [unused]
25*7c478bd9Sstevel@tonic-gate **		ext -- the filename extension. [unused]
26*7c478bd9Sstevel@tonic-gate **		type -- type of the lock.  Bits can be:
27*7c478bd9Sstevel@tonic-gate **			LOCK_EX -- exclusive lock.
28*7c478bd9Sstevel@tonic-gate **			LOCK_NB -- non-blocking.
29*7c478bd9Sstevel@tonic-gate **			LOCK_UN -- unlock.
30*7c478bd9Sstevel@tonic-gate **
31*7c478bd9Sstevel@tonic-gate **	Returns:
32*7c478bd9Sstevel@tonic-gate **		true if the lock was acquired.
33*7c478bd9Sstevel@tonic-gate **		false otherwise.
34*7c478bd9Sstevel@tonic-gate */
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate bool
37*7c478bd9Sstevel@tonic-gate lockfile(fd, filename, ext, type)
38*7c478bd9Sstevel@tonic-gate 	int fd;
39*7c478bd9Sstevel@tonic-gate 	char *filename;
40*7c478bd9Sstevel@tonic-gate 	char *ext;
41*7c478bd9Sstevel@tonic-gate 	int type;
42*7c478bd9Sstevel@tonic-gate {
43*7c478bd9Sstevel@tonic-gate #if !HASFLOCK
44*7c478bd9Sstevel@tonic-gate 	int action;
45*7c478bd9Sstevel@tonic-gate 	struct flock lfd;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate 	memset(&lfd, '\0', sizeof lfd);
48*7c478bd9Sstevel@tonic-gate 	if (bitset(LOCK_UN, type))
49*7c478bd9Sstevel@tonic-gate 		lfd.l_type = F_UNLCK;
50*7c478bd9Sstevel@tonic-gate 	else if (bitset(LOCK_EX, type))
51*7c478bd9Sstevel@tonic-gate 		lfd.l_type = F_WRLCK;
52*7c478bd9Sstevel@tonic-gate 	else
53*7c478bd9Sstevel@tonic-gate 		lfd.l_type = F_RDLCK;
54*7c478bd9Sstevel@tonic-gate 	if (bitset(LOCK_NB, type))
55*7c478bd9Sstevel@tonic-gate 		action = F_SETLK;
56*7c478bd9Sstevel@tonic-gate 	else
57*7c478bd9Sstevel@tonic-gate 		action = F_SETLKW;
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	if (fcntl(fd, action, &lfd) >= 0)
60*7c478bd9Sstevel@tonic-gate 		return true;
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 	/*
63*7c478bd9Sstevel@tonic-gate 	**  On SunOS, if you are testing using -oQ/tmp/mqueue or
64*7c478bd9Sstevel@tonic-gate 	**  -oA/tmp/aliases or anything like that, and /tmp is mounted
65*7c478bd9Sstevel@tonic-gate 	**  as type "tmp" (that is, served from swap space), the
66*7c478bd9Sstevel@tonic-gate 	**  previous fcntl will fail with "Invalid argument" errors.
67*7c478bd9Sstevel@tonic-gate 	**  Since this is fairly common during testing, we will assume
68*7c478bd9Sstevel@tonic-gate 	**  that this indicates that the lock is successfully grabbed.
69*7c478bd9Sstevel@tonic-gate 	*/
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	if (errno == EINVAL)
72*7c478bd9Sstevel@tonic-gate 		return true;
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate #else /* !HASFLOCK */
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	if (flock(fd, type) >= 0)
77*7c478bd9Sstevel@tonic-gate 		return true;
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate #endif /* !HASFLOCK */
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	return false;
82*7c478bd9Sstevel@tonic-gate }
83