MFC corrected printing of the slice number when adding a GPT partition.
[dragonfly.git] / contrib / sendmail-8.14 / libsmutil / lockfile.c
blob17d6237eaa6430ee568b23f0f594b7f0f7013a7d
1 /*
2 * Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.
3 * All rights reserved.
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 #include <sendmail.h>
16 SM_RCSID("@(#)$Id: lockfile.c,v 8.21 2003/11/10 22:57:38 ca Exp $")
20 ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking
22 ** Parameters:
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.
29 ** LOCK_UN -- unlock.
31 ** Returns:
32 ** true if the lock was acquired.
33 ** false otherwise.
36 bool
37 lockfile(fd, filename, ext, type)
38 int fd;
39 char *filename;
40 char *ext;
41 int type;
43 #if !HASFLOCK
44 int action;
45 struct flock lfd;
47 memset(&lfd, '\0', sizeof lfd);
48 if (bitset(LOCK_UN, type))
49 lfd.l_type = F_UNLCK;
50 else if (bitset(LOCK_EX, type))
51 lfd.l_type = F_WRLCK;
52 else
53 lfd.l_type = F_RDLCK;
54 if (bitset(LOCK_NB, type))
55 action = F_SETLK;
56 else
57 action = F_SETLKW;
59 if (fcntl(fd, action, &lfd) >= 0)
60 return true;
63 ** On SunOS, if you are testing using -oQ/tmp/mqueue or
64 ** -oA/tmp/aliases or anything like that, and /tmp is mounted
65 ** as type "tmp" (that is, served from swap space), the
66 ** previous fcntl will fail with "Invalid argument" errors.
67 ** Since this is fairly common during testing, we will assume
68 ** that this indicates that the lock is successfully grabbed.
71 if (errno == EINVAL)
72 return true;
74 #else /* !HASFLOCK */
76 if (flock(fd, type) >= 0)
77 return true;
79 #endif /* !HASFLOCK */
81 return false;