From owner-freebsd-hackers Wed Feb 24 14:46:38 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from mercury.inktomi.com (mercury.inktomi.com [209.1.32.126]) by hub.freebsd.org (Postfix) with ESMTP id AF4F21136F for ; Wed, 24 Feb 1999 14:46:32 -0800 (PST) (envelope-from jplevyak@inktomi.com) Received: from proxydev.inktomi.com (proxydev.inktomi.com [209.1.32.44]) by mercury.inktomi.com (8.9.1a/8.9.1) with ESMTP id VAA07743 for ; Tue, 23 Feb 1999 21:47:53 -0800 (PST) Received: (from jplevyak@localhost) by proxydev.inktomi.com (8.8.5/8.7.3) id VAA25006 for hackers@FreeBSD.ORG; Tue, 23 Feb 1999 21:47:44 -0800 (PST) Message-ID: <19990223214744.A24606@proxydev.inktomi.com> Date: Tue, 23 Feb 1999 21:47:44 -0800 From: John Plevyak To: hackers@FreeBSD.ORG Subject: flock and kernel threads Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 0.93.2i Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG I believe there may be a problem with lockfiles (flock) and kernel threads. The problem is demonstrated by the enclosed program, which if run twice will fail the second time. The problem seems to be that termination via signal of the non-main thread (non-leader) does not (always) result in a call to closef and hence a call to lf_clearlock. Suggestions, comments welcome. john ======================== CUT HERE ============================== #include #include #include #include #include #include #include #include #include #include #include #include #include #include pthread_t ink_thread_create(void*(*f)(void *),void * a) { pthread_t t; int ret; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); ret = pthread_create(&t, &attr, f, a); assert(!ret); pthread_attr_destroy(&attr); return t; } int lockfile_open (const char *lockfile, int *fd_ptr) { #define FAIL(x) \ { \ if (fd > 0) \ close (fd); \ return (x); \ } struct flock lock; int size; int val; int err; int fd; *fd_ptr = -1; // Try and open the lockfile. Create it if it does not already // exist. do { fd = open (lockfile, O_RDWR | O_CREAT, 0600); } while ((fd < 0) && (errno == EINTR)); if (fd < 0) FAIL (-errno); // Lock it. Note that if we can't get the lock EAGAIN will be the // error we receive. lock.l_type = F_WRLCK; lock.l_start = 0; lock.l_whence = SEEK_SET; lock.l_len = 0; do { err = fcntl (fd, F_SETLK, &lock); } while ((err < 0) && (errno == EINTR)); if (err < 0) FAIL(0); // Return the file descriptor of the opened lockfile. When this file // descriptor is closed the lock will be released. *fd_ptr = fd; return(1); // success #undef FAIL } void * dthread(void * a) { (void)kill(0, SIGKILL); // or (void)kill(getpid(), SIGKILL); } int main(int argc, char ** argv) { int fd, i; int res = lockfile_open("lockfile.lock", &fd); printf("res = %d, fd = %d\n", res, fd); if (!res) exit(0); ink_thread_create(dthread, 0); sleep(10); } ======================== CUT HERE ============================== -- John Bradley Plevyak, PhD, jplevyak@inktomi.com, PGP KeyID: 051130BD Inktomi Corporation, 1900 S. Norfolk Street, Suite 110, San Mateo, CA 94403 W:(415)653-2830 F:(415)653-2801 P:(888)491-1332/5103192436.4911332@pagenet.net To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message