/*********************************************************************** * This is a STC to show that fcntl fails. * * It tries to use fcntl() for file locking. It creates a temporary * file and tries to lock and then unlock the file. * * This produces "lock: Bad address" with Cygwin 1.7.19 x86_64. * * Compile: gcc -Wall -o stc-fcntl stc-fcntl.c ***********************************************************************/ #include #include #include #include #include #include /* A temporary file used for fcntl. */ char tmpfilename[] = "/tmp/fcntlXXXXXX"; struct flock mutex_lock_it; struct flock mutex_unlock_it; int main(int argc, const char * const * argv, const char * const *env) { int fd; int rc; /* Create the temporary file. */ fd = mkstemp(tmpfilename); if (fd < 0) { perror("open failed"); exit(1); } /* Setup mutexes */ mutex_lock_it.l_whence = SEEK_SET; /* from current point */ mutex_lock_it.l_start = 0; /* -"- */ mutex_lock_it.l_len = 0; /* until end of file */ mutex_lock_it.l_type = F_WRLCK; /* set exclusive/write lock */ mutex_lock_it.l_pid = 0; /* pid not actually interesting */ mutex_unlock_it.l_whence = SEEK_SET; /* from current point */ mutex_unlock_it.l_start = 0; /* -"- */ mutex_unlock_it.l_len = 0; /* until end of file */ mutex_unlock_it.l_type = F_UNLCK; /* set exclusive/write lock */ mutex_unlock_it.l_pid = 0; /* pid not actually interesting */ /* Get the lock. */ do { rc = fcntl(fd, F_SETLKW, &mutex_lock_it); } while (rc < 0 && errno == EINTR); if (rc < 0) { perror("lock"); exit(1); } /* Release the lock. */ do { rc = fcntl(fd, F_SETLKW, &mutex_unlock_it); } while (rc < 0 && errno == EINTR); if (rc < 0) { perror("unlock"); exit(1); } /* Clean up. */ close(fd); unlink(tmpfilename); return 0; }