2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: getLock.c,v 1.1 1998/04/10 10:54:39 simonm Exp $
6 * stdin/stout/stderr Runtime Support
12 #ifdef HAVE_SYS_TYPES_H
13 #include <sys/types.h>
16 #ifdef HAVE_SYS_STAT_H
29 #define FD_SETSIZE 256
38 static Lock readLock[FD_SETSIZE];
39 static Lock writeLock[FD_SETSIZE];
41 static int readLocks = 0;
42 static int writeLocks = 0;
45 lockFile(int fd, int exclusive)
50 while (fstat(fd, &sb) < 0) {
56 /* Only lock regular files */
57 if (!S_ISREG(sb.st_mode))
60 for (i = 0; i < writeLocks; i++)
61 if (writeLock[i].inode == sb.st_ino && writeLock[i].device == sb.st_dev) {
68 readLock[i].device = sb.st_dev;
69 readLock[i].inode = sb.st_ino;
74 for (i = 0; i < readLocks; i++)
75 if (readLock[i].inode == sb.st_ino && readLock[i].device == sb.st_dev) {
81 writeLock[i].device = sb.st_dev;
82 writeLock[i].inode = sb.st_ino;
92 for (i = 0; i < readLocks; i++)
93 if (readLock[i].fd == fd) {
94 while (++i < readLocks)
95 readLock[i - 1] = readLock[i];
100 for (i = 0; i < writeLocks; i++)
101 if (writeLock[i].fd == fd) {
102 while (++i < writeLocks)
103 writeLock[i - 1] = writeLock[i];
107 /* Signal that we did not find an entry */
112 getLock(StgAddr fp, StgInt exclusive)
114 if (lockFile(fileno((FILE *) fp), exclusive) < 0) {
125 ghc_errtype = ERR_RESOURCEBUSY;
126 ghc_errstr = "file is locked";
129 (void) fclose((FILE *) fp);