2 * Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * By using this file, you agree to the terms and conditions set
9 * forth in the LICENSE file which can be found at the top level of
10 * the sendmail distribution.
16 SM_RCSID("@(#)$Id: lockfile.c,v 8.19 2001/09/11 04:04:55 gshapiro Exp $")
20 ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking
23 ** fd -- the file descriptor of the file.
24 ** filename -- the file name (for error messages). [unused]
25 ** ext -- the filename extension. [unused]
26 ** type -- type of the lock. Bits can be:
27 ** LOCK_EX -- exclusive lock.
28 ** LOCK_NB -- non-blocking.
32 ** true if the lock was acquired.
37 lockfile(fd, filename, ext, type)
48 memset(&lfd, '\0', sizeof lfd);
49 if (bitset(LOCK_UN, type))
51 else if (bitset(LOCK_EX, type))
55 if (bitset(LOCK_NB, type))
60 if (fcntl(fd, action, &lfd) >= 0)
64 ** On SunOS, if you are testing using -oQ/tmp/mqueue or
65 ** -oA/tmp/aliases or anything like that, and /tmp is mounted
66 ** as type "tmp" (that is, served from swap space), the
67 ** previous fcntl will fail with "Invalid argument" errors.
68 ** Since this is fairly common during testing, we will assume
69 ** that this indicates that the lock is successfully grabbed.
77 if (flock(fd, type) >= 0)
80 #endif /* !HASFLOCK */