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.
14 #pragma ident "%Z%%M% %I% %E% SMI"
18 SM_RCSID("@(#)$Id: lockfile.c,v 8.21 2003/11/10 22:57:38 ca Exp $")
22 ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking
25 ** fd -- the file descriptor of the file.
26 ** filename -- the file name (for error messages). [unused]
27 ** ext -- the filename extension. [unused]
28 ** type -- type of the lock. Bits can be:
29 ** LOCK_EX -- exclusive lock.
30 ** LOCK_NB -- non-blocking.
34 ** true if the lock was acquired.
39 lockfile(fd
, filename
, ext
, type
)
49 memset(&lfd
, '\0', sizeof lfd
);
50 if (bitset(LOCK_UN
, type
))
52 else if (bitset(LOCK_EX
, type
))
56 if (bitset(LOCK_NB
, type
))
61 if (fcntl(fd
, action
, &lfd
) >= 0)
65 ** On SunOS, if you are testing using -oQ/tmp/mqueue or
66 ** -oA/tmp/aliases or anything like that, and /tmp is mounted
67 ** as type "tmp" (that is, served from swap space), the
68 ** previous fcntl will fail with "Invalid argument" errors.
69 ** Since this is fairly common during testing, we will assume
70 ** that this indicates that the lock is successfully grabbed.
78 if (flock(fd
, type
) >= 0)
81 #endif /* !HASFLOCK */