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

yatex.org