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. 7 * 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. 11 * 12 */ 13 14 #include <sendmail.h> 15 16 SM_RCSID("@(#)$Id: lockfile.c,v 8.19 2001/09/11 04:04:55 gshapiro Exp $") 17 18 19 /* 20 ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 21 ** 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. 30 ** 31 ** Returns: 32 ** true if the lock was acquired. 33 ** false otherwise. 34 */ 35 36 bool 37 lockfile(fd, filename, ext, type) 38 int fd; 39 char *filename; 40 char *ext; 41 int type; 42 { 43 #if !HASFLOCK 44 int action; 45 struct flock lfd; 46 extern int errno; 47 48 memset(&lfd, '\0', sizeof lfd); 49 if (bitset(LOCK_UN, type)) 50 lfd.l_type = F_UNLCK; 51 else if (bitset(LOCK_EX, type)) 52 lfd.l_type = F_WRLCK; 53 else 54 lfd.l_type = F_RDLCK; 55 if (bitset(LOCK_NB, type)) 56 action = F_SETLK; 57 else 58 action = F_SETLKW; 59 60 if (fcntl(fd, action, &lfd) >= 0) 61 return true; 62 63 /* 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. 70 */ 71 72 if (errno == EINVAL) 73 return true; 74 75 #else /* !HASFLOCK */ 76 77 if (flock(fd, type) >= 0) 78 return true; 79 80 #endif /* !HASFLOCK */ 81 82 return false; 83 } 84