imapext-2007
diff src/osdep/unix/flockcyg.c @ 0:ada5e610ab86
imap-2007e
author | yuuji@gentei.org |
---|---|
date | Mon, 14 Sep 2009 15:17:45 +0900 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/osdep/unix/flockcyg.c Mon Sep 14 15:17:45 2009 +0900 1.3 @@ -0,0 +1,92 @@ 1.4 +/* ======================================================================== 1.5 + * Copyright 1988-2006 University of Washington 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * 1.14 + * ======================================================================== 1.15 + */ 1.16 + 1.17 +/* 1.18 + * Program: flock emulation via fcntl() locking 1.19 + * 1.20 + * Author: Mark Crispin 1.21 + * Networks and Distributed Computing 1.22 + * Computing & Communications 1.23 + * University of Washington 1.24 + * Administration Building, AG-44 1.25 + * Seattle, WA 98195 1.26 + * Internet: MRC@CAC.Washington.EDU 1.27 + * 1.28 + * Date: 10 April 2001 1.29 + * Last Edited: 30 August 2006 1.30 + */ 1.31 + 1.32 + 1.33 +/* Cygwin does not seem to have the design flaw in fcntl() locking that 1.34 + * most other systems do (see flocksim.c for details). If some cretin 1.35 + * decides to implement that design flaw, then Cygwin will have to use 1.36 + * flocksim. Also, we don't test NFS either. 1.37 + * 1.38 + * However, Cygwin does have the Windows misfeature (introduced in NT 4.0) 1.39 + * that you can not write to any segment which has a shared lock, and you 1.40 + * can't lock a zero-byte segment either. This screws up the shared-write 1.41 + * mailbox drivers (mbx, mtx, mx, and tenex). As a workaround, we'll only 1.42 + * lock the first byte of the file, meaning that you can't write that byte 1.43 + * shared. It's been suggested to lock the maximum off_t type, but that 1.44 + * risks having a future version of Windows (or Cygwin) deciding that this 1.45 + * also means "no lock". 1.46 + */ 1.47 + 1.48 +#undef flock /* name is used as a struct for fcntl */ 1.49 + 1.50 +/* Emulator for flock() call 1.51 + * Accepts: file descriptor 1.52 + * operation bitmask 1.53 + * Returns: 0 if successful, -1 if failure under BSD conditions 1.54 + */ 1.55 + 1.56 +int flocksim (int fd,int op) 1.57 +{ 1.58 + char tmp[MAILTMPLEN]; 1.59 + int logged = 0; 1.60 + struct flock fl; 1.61 + /* lock one bytes at byte 0 */ 1.62 + fl.l_whence = SEEK_SET; fl.l_start = 0; fl.l_len = 1; 1.63 + fl.l_pid = getpid (); /* shouldn't be necessary */ 1.64 + switch (op & ~LOCK_NB) { /* translate to fcntl() operation */ 1.65 + case LOCK_EX: /* exclusive */ 1.66 + fl.l_type = F_WRLCK; 1.67 + break; 1.68 + case LOCK_SH: /* shared */ 1.69 + fl.l_type = F_RDLCK; 1.70 + break; 1.71 + case LOCK_UN: /* unlock */ 1.72 + fl.l_type = F_UNLCK; 1.73 + break; 1.74 + default: /* default */ 1.75 + errno = EINVAL; 1.76 + return -1; 1.77 + } 1.78 + while (fcntl (fd,(op & LOCK_NB) ? F_SETLK : F_SETLKW,&fl)) 1.79 + if (errno != EINTR) { 1.80 + /* Can't use switch here because these error codes may resolve to the 1.81 + * same value on some systems. 1.82 + */ 1.83 + if ((errno != EWOULDBLOCK) && (errno != EAGAIN) && (errno != EACCES)) { 1.84 + sprintf (tmp,"Unexpected file locking failure: %s",strerror (errno)); 1.85 + /* give the user a warning of what happened */ 1.86 + MM_NOTIFY (NIL,tmp,WARN); 1.87 + if (!logged++) syslog (LOG_ERR,"%s",tmp); 1.88 + if (op & LOCK_NB) return -1; 1.89 + sleep (5); /* slow things down for loops */ 1.90 + } 1.91 + /* return failure for non-blocking lock */ 1.92 + else if (op & LOCK_NB) return -1; 1.93 + } 1.94 + return 0; /* success */ 1.95 +}