imapext-2007

annotate src/osdep/unix/flockcyg.c @ 0:ada5e610ab86

imap-2007e
author yuuji@gentei.org
date Mon, 14 Sep 2009 15:17:45 +0900
parents
children
rev   line source
yuuji@0 1 /* ========================================================================
yuuji@0 2 * Copyright 1988-2006 University of Washington
yuuji@0 3 *
yuuji@0 4 * Licensed under the Apache License, Version 2.0 (the "License");
yuuji@0 5 * you may not use this file except in compliance with the License.
yuuji@0 6 * You may obtain a copy of the License at
yuuji@0 7 *
yuuji@0 8 * http://www.apache.org/licenses/LICENSE-2.0
yuuji@0 9 *
yuuji@0 10 *
yuuji@0 11 * ========================================================================
yuuji@0 12 */
yuuji@0 13
yuuji@0 14 /*
yuuji@0 15 * Program: flock emulation via fcntl() locking
yuuji@0 16 *
yuuji@0 17 * Author: Mark Crispin
yuuji@0 18 * Networks and Distributed Computing
yuuji@0 19 * Computing & Communications
yuuji@0 20 * University of Washington
yuuji@0 21 * Administration Building, AG-44
yuuji@0 22 * Seattle, WA 98195
yuuji@0 23 * Internet: MRC@CAC.Washington.EDU
yuuji@0 24 *
yuuji@0 25 * Date: 10 April 2001
yuuji@0 26 * Last Edited: 30 August 2006
yuuji@0 27 */
yuuji@0 28
yuuji@0 29
yuuji@0 30 /* Cygwin does not seem to have the design flaw in fcntl() locking that
yuuji@0 31 * most other systems do (see flocksim.c for details). If some cretin
yuuji@0 32 * decides to implement that design flaw, then Cygwin will have to use
yuuji@0 33 * flocksim. Also, we don't test NFS either.
yuuji@0 34 *
yuuji@0 35 * However, Cygwin does have the Windows misfeature (introduced in NT 4.0)
yuuji@0 36 * that you can not write to any segment which has a shared lock, and you
yuuji@0 37 * can't lock a zero-byte segment either. This screws up the shared-write
yuuji@0 38 * mailbox drivers (mbx, mtx, mx, and tenex). As a workaround, we'll only
yuuji@0 39 * lock the first byte of the file, meaning that you can't write that byte
yuuji@0 40 * shared. It's been suggested to lock the maximum off_t type, but that
yuuji@0 41 * risks having a future version of Windows (or Cygwin) deciding that this
yuuji@0 42 * also means "no lock".
yuuji@0 43 */
yuuji@0 44
yuuji@0 45 #undef flock /* name is used as a struct for fcntl */
yuuji@0 46
yuuji@0 47 /* Emulator for flock() call
yuuji@0 48 * Accepts: file descriptor
yuuji@0 49 * operation bitmask
yuuji@0 50 * Returns: 0 if successful, -1 if failure under BSD conditions
yuuji@0 51 */
yuuji@0 52
yuuji@0 53 int flocksim (int fd,int op)
yuuji@0 54 {
yuuji@0 55 char tmp[MAILTMPLEN];
yuuji@0 56 int logged = 0;
yuuji@0 57 struct flock fl;
yuuji@0 58 /* lock one bytes at byte 0 */
yuuji@0 59 fl.l_whence = SEEK_SET; fl.l_start = 0; fl.l_len = 1;
yuuji@0 60 fl.l_pid = getpid (); /* shouldn't be necessary */
yuuji@0 61 switch (op & ~LOCK_NB) { /* translate to fcntl() operation */
yuuji@0 62 case LOCK_EX: /* exclusive */
yuuji@0 63 fl.l_type = F_WRLCK;
yuuji@0 64 break;
yuuji@0 65 case LOCK_SH: /* shared */
yuuji@0 66 fl.l_type = F_RDLCK;
yuuji@0 67 break;
yuuji@0 68 case LOCK_UN: /* unlock */
yuuji@0 69 fl.l_type = F_UNLCK;
yuuji@0 70 break;
yuuji@0 71 default: /* default */
yuuji@0 72 errno = EINVAL;
yuuji@0 73 return -1;
yuuji@0 74 }
yuuji@0 75 while (fcntl (fd,(op & LOCK_NB) ? F_SETLK : F_SETLKW,&fl))
yuuji@0 76 if (errno != EINTR) {
yuuji@0 77 /* Can't use switch here because these error codes may resolve to the
yuuji@0 78 * same value on some systems.
yuuji@0 79 */
yuuji@0 80 if ((errno != EWOULDBLOCK) && (errno != EAGAIN) && (errno != EACCES)) {
yuuji@0 81 sprintf (tmp,"Unexpected file locking failure: %s",strerror (errno));
yuuji@0 82 /* give the user a warning of what happened */
yuuji@0 83 MM_NOTIFY (NIL,tmp,WARN);
yuuji@0 84 if (!logged++) syslog (LOG_ERR,"%s",tmp);
yuuji@0 85 if (op & LOCK_NB) return -1;
yuuji@0 86 sleep (5); /* slow things down for loops */
yuuji@0 87 }
yuuji@0 88 /* return failure for non-blocking lock */
yuuji@0 89 else if (op & LOCK_NB) return -1;
yuuji@0 90 }
yuuji@0 91 return 0; /* success */
yuuji@0 92 }

UW-IMAP'd extensions by yuuji