summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKurt Miller <kurt@cvs.openbsd.org>2009-11-09 00:18:29 +0000
committerKurt Miller <kurt@cvs.openbsd.org>2009-11-09 00:18:29 +0000
commitad0592d657a970d3b3d8200773732a354fad0461 (patch)
tree2d36a0624d8d3c7fac96e04ac86511d85bbcc56f
parent13102e76b8d893f7a1f4a778cc16d5e67e719a12 (diff)
Fix the handle locking in stdio to use flockfile/funlockfile
internally when and where required. Macros in <stdio.h> are updated to automatically call the underlying functions when the process is threaded to obtain the necessary locking. A private mutex is added to protect __sglue, the internal list of FILE handles, and another to protect the one-time initialization. Some routines in libc that use getc() change to use getc_unlocked() as they're either protected by their own lock or aren't thread-safe routines anyway. committing on behalf of and okay guenther@ now that we have install media space available.
-rw-r--r--include/stdio.h38
-rw-r--r--lib/libc/gen/getgrent.c5
-rw-r--r--lib/libc/gen/getgrouplist.c4
-rw-r--r--lib/libc/gen/getttyent.c4
-rw-r--r--lib/libc/stdio/asprintf.c4
-rw-r--r--lib/libc/stdio/clrerr.c7
-rw-r--r--lib/libc/stdio/fclose.c6
-rw-r--r--lib/libc/stdio/feof.c10
-rw-r--r--lib/libc/stdio/ferror.c10
-rw-r--r--lib/libc/stdio/fflush.c25
-rw-r--r--lib/libc/stdio/fgetc.c4
-rw-r--r--lib/libc/stdio/fgetln.c21
-rw-r--r--lib/libc/stdio/fgets.c9
-rw-r--r--lib/libc/stdio/fgetwc.c6
-rw-r--r--lib/libc/stdio/fgetws.c8
-rw-r--r--lib/libc/stdio/fileno.c10
-rw-r--r--lib/libc/stdio/findfp.c36
-rw-r--r--lib/libc/stdio/fpurge.c5
-rw-r--r--lib/libc/stdio/fputc.c7
-rw-r--r--lib/libc/stdio/fputs.c8
-rw-r--r--lib/libc/stdio/fputwc.c6
-rw-r--r--lib/libc/stdio/fputws.c8
-rw-r--r--lib/libc/stdio/fread.c5
-rw-r--r--lib/libc/stdio/freopen.c7
-rw-r--r--lib/libc/stdio/fseek.c12
-rw-r--r--lib/libc/stdio/ftell.c11
-rw-r--r--lib/libc/stdio/fvwrite.c8
-rw-r--r--lib/libc/stdio/fwalk.c7
-rw-r--r--lib/libc/stdio/fwide.c6
-rw-r--r--lib/libc/stdio/fwrite.c8
-rw-r--r--lib/libc/stdio/getc.c7
-rw-r--r--lib/libc/stdio/gets.c12
-rw-r--r--lib/libc/stdio/local.h7
-rw-r--r--lib/libc/stdio/putc.c6
-rw-r--r--lib/libc/stdio/puts.c9
-rw-r--r--lib/libc/stdio/putw.c9
-rw-r--r--lib/libc/stdio/refill.c15
-rw-r--r--lib/libc/stdio/setvbuf.c5
-rw-r--r--lib/libc/stdio/snprintf.c4
-rw-r--r--lib/libc/stdio/sprintf.c4
-rw-r--r--lib/libc/stdio/ungetc.c18
-rw-r--r--lib/libc/stdio/ungetwc.c10
-rw-r--r--lib/libc/stdio/vasprintf.c4
-rw-r--r--lib/libc/stdio/vfprintf.c17
-rw-r--r--lib/libc/stdio/vfscanf.c12
-rw-r--r--lib/libc/stdio/vsnprintf.c4
-rw-r--r--lib/libc/stdio/vsprintf.c4
-rw-r--r--lib/libc/stdio/wbuf.c8
-rw-r--r--lib/libpthread/uthread/uthread_file.c253
49 files changed, 424 insertions, 289 deletions
diff --git a/include/stdio.h b/include/stdio.h
index 14b65aae5ce..e23e7ad4667 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: stdio.h,v 1.37 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: stdio.h,v 1.38 2009/11/09 00:18:27 kurt Exp $ */
/* $NetBSD: stdio.h,v 1.18 1996/04/25 18:29:21 jtc Exp $ */
/*-
@@ -157,6 +157,7 @@ __END_DECLS
#define __SOFF 0x1000 /* set iff _offset is in fact correct */
#define __SMOD 0x2000 /* true => fgetln modified _p text */
#define __SALC 0x4000 /* allocate string space dynamically */
+#define __SIGN 0x8000 /* ignore this file in _fwalk */
/*
* The following three definitions are for ANSI C, which took them
@@ -322,12 +323,6 @@ char *tempnam(const char *, const char *);
#endif
__END_DECLS
-#ifndef _POSIX_THREADS
-# define flockfile(fp) /* nothing */
-# define ftrylockfile(fp) (0)
-# define funlockfile(fp) /* nothing */
-#endif
-
#endif /* __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE */
/*
@@ -402,32 +397,37 @@ static __inline int __sputc(int _c, FILE *_p) {
#define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF)))
#define __sfileno(p) ((p)->_file)
-#define feof(p) __sfeof(p)
-#define ferror(p) __sferror(p)
+extern int __isthreaded;
-#ifndef _POSIX_THREADS
-#define clearerr(p) __sclearerr(p)
-#endif
+#define feof(p) (!__isthreaded ? __sfeof(p) : (feof)(p))
+#define ferror(p) (!__isthreaded ? __sferror(p) : (ferror)(p))
+#define clearerr(p) (!__isthreaded ? __sclearerr(p) : (clearerr)(p))
#if __POSIX_VISIBLE
-#define fileno(p) __sfileno(p)
+#define fileno(p) (!__isthreaded ? __sfileno(p) : (fileno)(p))
#endif
+#define getc(fp) (!__isthreaded ? __sgetc(fp) : (getc)(fp))
+
+#if __BSD_VISIBLE
+/*
+ * The macro implementations of putc and putc_unlocked are not
+ * fully POSIX compliant; they do not set errno on failure
+ */
+#define putc(x, fp) (!__isthreaded ? __sputc(x, fp) : (putc)(x, fp))
+#endif /* __BSD_VISIBLE */
+
#ifndef lint
-#ifndef _POSIX_THREADS
-#define getc(fp) __sgetc(fp)
-#endif /* _POSIX_THREADS */
+#if __POSIX_VISIBLE >= 199506
#define getc_unlocked(fp) __sgetc(fp)
/*
* The macro implementations of putc and putc_unlocked are not
* fully POSIX compliant; they do not set errno on failure
*/
#if __BSD_VISIBLE
-#ifndef _POSIX_THREADS
-#define putc(x, fp) __sputc(x, fp)
-#endif /* _POSIX_THREADS */
#define putc_unlocked(x, fp) __sputc(x, fp)
#endif /* __BSD_VISIBLE */
+#endif /* __POSIX_VISIBLE >= 199506 */
#endif /* lint */
#define getchar() getc(stdin)
diff --git a/lib/libc/gen/getgrent.c b/lib/libc/gen/getgrent.c
index 341f5158901..5b52f960797 100644
--- a/lib/libc/gen/getgrent.c
+++ b/lib/libc/gen/getgrent.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: getgrent.c,v 1.34 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: getgrent.c,v 1.35 2009/11/09 00:18:27 kurt Exp $ */
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
@@ -319,7 +319,8 @@ grscan(int search, gid_t gid, const char *name, struct group *p_gr,
if (!strchr(line, '\n')) {
int ch;
- while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
+ while ((ch = getc_unlocked(_gr_fp)) != '\n' &&
+ ch != EOF)
;
continue;
}
diff --git a/lib/libc/gen/getgrouplist.c b/lib/libc/gen/getgrouplist.c
index 3f8dda203b4..3faef2d898f 100644
--- a/lib/libc/gen/getgrouplist.c
+++ b/lib/libc/gen/getgrouplist.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: getgrouplist.c,v 1.20 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: getgrouplist.c,v 1.21 2009/11/09 00:18:27 kurt Exp $ */
/*
* Copyright (c) 2008 Ingo Schwarze <schwarze@usta.de>
* Copyright (c) 1991, 1993
@@ -123,7 +123,7 @@ _read_netid(const char *key, uid_t uid, gid_t *groups, int *ngroups,
*p = '\0';
else { /* Skip lines that are too long. */
int ch;
- while ((ch = getc(fp)) != '\n' && ch != EOF)
+ while ((ch = getc_unlocked(fp)) != '\n' && ch != EOF)
;
continue;
}
diff --git a/lib/libc/gen/getttyent.c b/lib/libc/gen/getttyent.c
index f92887da485..f0ab5ab0d97 100644
--- a/lib/libc/gen/getttyent.c
+++ b/lib/libc/gen/getttyent.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: getttyent.c,v 1.11 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: getttyent.c,v 1.12 2009/11/09 00:18:27 kurt Exp $ */
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
@@ -68,7 +68,7 @@ getttyent(void)
return (NULL);
/* skip lines that are too big */
if (!strchr(p, '\n')) {
- while ((c = getc(tf)) != '\n' && c != EOF)
+ while ((c = getc_unlocked(tf)) != '\n' && c != EOF)
;
continue;
}
diff --git a/lib/libc/stdio/asprintf.c b/lib/libc/stdio/asprintf.c
index f58cc0041d3..d1d023a6b4a 100644
--- a/lib/libc/stdio/asprintf.c
+++ b/lib/libc/stdio/asprintf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: asprintf.c,v 1.17 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: asprintf.c,v 1.18 2009/11/09 00:18:27 kurt Exp $ */
/*
* Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
@@ -40,7 +40,7 @@ asprintf(char **str, const char *fmt, ...)
goto err;
f._bf._size = f._w = 127; /* Leave room for the NUL */
va_start(ap, fmt);
- ret = vfprintf(&f, fmt, ap);
+ ret = __vfprintf(&f, fmt, ap);
va_end(ap);
if (ret == -1)
goto err;
diff --git a/lib/libc/stdio/clrerr.c b/lib/libc/stdio/clrerr.c
index be993640d4f..ac08c72cd11 100644
--- a/lib/libc/stdio/clrerr.c
+++ b/lib/libc/stdio/clrerr.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: clrerr.c,v 1.8 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: clrerr.c,v 1.9 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -32,12 +32,13 @@
*/
#include <stdio.h>
+#include "local.h"
#undef clearerr
void
clearerr(FILE *fp)
{
- flockfile(fp);
+ FLOCKFILE(fp);
__sclearerr(fp);
- funlockfile(fp);
+ FUNLOCKFILE(fp);
}
diff --git a/lib/libc/stdio/fclose.c b/lib/libc/stdio/fclose.c
index 98e9826b82c..c72af540586 100644
--- a/lib/libc/stdio/fclose.c
+++ b/lib/libc/stdio/fclose.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fclose.c,v 1.8 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: fclose.c,v 1.9 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -45,6 +45,7 @@ fclose(FILE *fp)
errno = EBADF;
return (EOF);
}
+ FLOCKFILE(fp);
WCIO_FREE(fp);
r = fp->_flags & __SWR ? __sflush(fp) : 0;
if (fp->_close != NULL && (*fp->_close)(fp->_cookie) < 0)
@@ -55,7 +56,8 @@ fclose(FILE *fp)
FREEUB(fp);
if (HASLB(fp))
FREELB(fp);
- fp->_flags = 0; /* Release this FILE for reuse. */
fp->_r = fp->_w = 0; /* Mess up if reaccessed. */
+ fp->_flags = 0; /* Release this FILE for reuse. */
+ FUNLOCKFILE(fp);
return (r);
}
diff --git a/lib/libc/stdio/feof.c b/lib/libc/stdio/feof.c
index 67da8536df3..0036bab7d0e 100644
--- a/lib/libc/stdio/feof.c
+++ b/lib/libc/stdio/feof.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: feof.c,v 1.7 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: feof.c,v 1.8 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -32,6 +32,7 @@
*/
#include <stdio.h>
+#include "local.h"
/*
* A subroutine version of the macro feof.
@@ -41,5 +42,10 @@
int
feof(FILE *fp)
{
- return (__sfeof(fp));
+ int ret;
+
+ FLOCKFILE(fp);
+ ret = __sfeof(fp);
+ FUNLOCKFILE(fp);
+ return (ret);
}
diff --git a/lib/libc/stdio/ferror.c b/lib/libc/stdio/ferror.c
index 0e093410b5c..00b9c8b2c68 100644
--- a/lib/libc/stdio/ferror.c
+++ b/lib/libc/stdio/ferror.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ferror.c,v 1.7 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: ferror.c,v 1.8 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -32,6 +32,7 @@
*/
#include <stdio.h>
+#include "local.h"
/*
* A subroutine version of the macro ferror.
@@ -41,5 +42,10 @@
int
ferror(FILE *fp)
{
- return (__sferror(fp));
+ int ret;
+
+ FLOCKFILE(fp);
+ ret = __sferror(fp);
+ FUNLOCKFILE(fp);
+ return (ret);
}
diff --git a/lib/libc/stdio/fflush.c b/lib/libc/stdio/fflush.c
index 2fe62a16834..3e30f1086ff 100644
--- a/lib/libc/stdio/fflush.c
+++ b/lib/libc/stdio/fflush.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fflush.c,v 1.7 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: fflush.c,v 1.8 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -39,14 +39,18 @@
int
fflush(FILE *fp)
{
+ int r;
if (fp == NULL)
- return (_fwalk(__sflush));
+ return (_fwalk(__sflush_locked));
+ FLOCKFILE(fp);
if ((fp->_flags & (__SWR | __SRW)) == 0) {
errno = EBADF;
- return (EOF);
- }
- return (__sflush(fp));
+ r = EOF;
+ } else
+ r = __sflush(fp);
+ FUNLOCKFILE(fp);
+ return (r);
}
int
@@ -80,3 +84,14 @@ __sflush(FILE *fp)
}
return (0);
}
+
+int
+__sflush_locked(FILE *fp)
+{
+ int r;
+
+ FLOCKFILE(fp);
+ r = __sflush(fp);
+ FUNLOCKFILE(fp);
+ return (r);
+}
diff --git a/lib/libc/stdio/fgetc.c b/lib/libc/stdio/fgetc.c
index 200ed439c2c..c5d7dde2d06 100644
--- a/lib/libc/stdio/fgetc.c
+++ b/lib/libc/stdio/fgetc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fgetc.c,v 1.7 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: fgetc.c,v 1.8 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -36,5 +36,5 @@
int
fgetc(FILE *fp)
{
- return (__sgetc(fp));
+ return (getc(fp));
}
diff --git a/lib/libc/stdio/fgetln.c b/lib/libc/stdio/fgetln.c
index 061e5cb4c91..0ec02230689 100644
--- a/lib/libc/stdio/fgetln.c
+++ b/lib/libc/stdio/fgetln.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fgetln.c,v 1.9 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: fgetln.c,v 1.10 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -71,19 +71,18 @@ char *
fgetln(FILE *fp, size_t *lenp)
{
unsigned char *p;
+ char *ret;
size_t len;
size_t off;
+ FLOCKFILE(fp);
+
/* make sure there is input */
- if (fp->_r <= 0 && __srefill(fp)) {
- *lenp = 0;
- return (NULL);
- }
+ if (fp->_r <= 0 && __srefill(fp))
+ goto error;
/* look for a newline in the input */
if ((p = memchr((void *)fp->_p, '\n', fp->_r)) != NULL) {
- char *ret;
-
/*
* Found one. Flag buffer as modified to keep fseek from
* `optimising' a backward seek, in case the user stomps on
@@ -95,6 +94,7 @@ fgetln(FILE *fp, size_t *lenp)
fp->_flags |= __SMOD;
fp->_r -= len;
fp->_p = p;
+ FUNLOCKFILE(fp);
return (ret);
}
@@ -139,12 +139,15 @@ fgetln(FILE *fp, size_t *lenp)
break;
}
*lenp = len;
+ ret = (char *)fp->_lb._base;
#ifdef notdef
- fp->_lb._base[len] = '\0';
+ ret[len] = '\0';
#endif
- return ((char *)fp->_lb._base);
+ FUNLOCKFILE(fp);
+ return (ret);
error:
*lenp = 0; /* ??? */
+ FUNLOCKFILE(fp);
return (NULL); /* ??? */
}
diff --git a/lib/libc/stdio/fgets.c b/lib/libc/stdio/fgets.c
index 661ec0d3861..0ba8770e33b 100644
--- a/lib/libc/stdio/fgets.c
+++ b/lib/libc/stdio/fgets.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fgets.c,v 1.13 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: fgets.c,v 1.14 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -54,6 +54,7 @@ fgets(char *buf, int n, FILE *fp)
return (NULL);
}
+ FLOCKFILE(fp);
_SET_ORIENTATION(fp, -1);
s = buf;
n--; /* leave space for NUL */
@@ -64,8 +65,10 @@ fgets(char *buf, int n, FILE *fp)
if (fp->_r <= 0) {
if (__srefill(fp)) {
/* EOF/error: stop with partial or no line */
- if (s == buf)
+ if (s == buf) {
+ FUNLOCKFILE(fp);
return (NULL);
+ }
break;
}
}
@@ -87,6 +90,7 @@ fgets(char *buf, int n, FILE *fp)
fp->_p = t;
(void)memcpy((void *)s, (void *)p, len);
s[len] = '\0';
+ FUNLOCKFILE(fp);
return (buf);
}
fp->_r -= len;
@@ -96,5 +100,6 @@ fgets(char *buf, int n, FILE *fp)
n -= len;
}
*s = '\0';
+ FUNLOCKFILE(fp);
return (buf);
}
diff --git a/lib/libc/stdio/fgetwc.c b/lib/libc/stdio/fgetwc.c
index d42e02b44ba..c16ffaf2997 100644
--- a/lib/libc/stdio/fgetwc.c
+++ b/lib/libc/stdio/fgetwc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fgetwc.c,v 1.3 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: fgetwc.c,v 1.4 2009/11/09 00:18:27 kurt Exp $ */
/* $NetBSD: fgetwc.c,v 1.3 2003/03/07 07:11:36 tshiozak Exp $ */
/*-
@@ -82,9 +82,9 @@ fgetwc(FILE *fp)
{
wint_t r;
- flockfile(fp);
+ FLOCKFILE(fp);
r = __fgetwc_unlock(fp);
- funlockfile(fp);
+ FUNLOCKFILE(fp);
return (r);
}
diff --git a/lib/libc/stdio/fgetws.c b/lib/libc/stdio/fgetws.c
index 4563e011b6d..e8cd2490089 100644
--- a/lib/libc/stdio/fgetws.c
+++ b/lib/libc/stdio/fgetws.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fgetws.c,v 1.5 2009/11/08 16:55:17 stsp Exp $ */
+/* $OpenBSD: fgetws.c,v 1.6 2009/11/09 00:18:27 kurt Exp $ */
/* $NetBSD: fgetws.c,v 1.1 2003/03/07 07:11:37 tshiozak Exp $ */
/*-
@@ -42,7 +42,7 @@ fgetws(wchar_t * __restrict ws, int n, FILE * __restrict fp)
wchar_t *wsp;
wint_t wc;
- flockfile(fp);
+ FLOCKFILE(fp);
_SET_ORIENTATION(fp, 1);
if (n <= 0) {
@@ -69,11 +69,11 @@ fgetws(wchar_t * __restrict ws, int n, FILE * __restrict fp)
}
*wsp++ = L'\0';
- funlockfile(fp);
+ FUNLOCKFILE(fp);
return (ws);
error:
- funlockfile(fp);
+ FUNLOCKFILE(fp);
return (NULL);
}
diff --git a/lib/libc/stdio/fileno.c b/lib/libc/stdio/fileno.c
index ea5d5dd213f..58628da3c10 100644
--- a/lib/libc/stdio/fileno.c
+++ b/lib/libc/stdio/fileno.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fileno.c,v 1.7 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: fileno.c,v 1.8 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -32,6 +32,7 @@
*/
#include <stdio.h>
+#include "local.h"
/*
* A subroutine version of the macro fileno.
@@ -41,5 +42,10 @@
int
fileno(FILE *fp)
{
- return (__sfileno(fp));
+ int ret;
+
+ FLOCKFILE(fp);
+ ret = __sfileno(fp);
+ FUNLOCKFILE(fp);
+ return (ret);
}
diff --git a/lib/libc/stdio/findfp.c b/lib/libc/stdio/findfp.c
index 2be93a56c74..63458b18afa 100644
--- a/lib/libc/stdio/findfp.c
+++ b/lib/libc/stdio/findfp.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: findfp.c,v 1.11 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: findfp.c,v 1.12 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -39,6 +39,7 @@
#include <string.h>
#include "local.h"
#include "glue.h"
+#include "thread_private.h"
int __sdidinit;
@@ -54,6 +55,8 @@ int __sdidinit;
static FILE usual[FOPEN_MAX - 3];
static struct __sfileext usualext[FOPEN_MAX - 3];
static struct glue uglue = { 0, FOPEN_MAX - 3, usual };
+static struct glue *lastglue = &uglue;
+_THREAD_PRIVATE_MUTEX(__sfp_mutex);
struct __sfileext __sFext[3];
FILE __sF[3] = {
@@ -104,16 +107,25 @@ __sfp(void)
if (!__sdidinit)
__sinit();
- for (g = &__sglue;; g = g->next) {
+
+ _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex);
+ for (g = &__sglue; g != NULL; g = g->next) {
for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
if (fp->_flags == 0)
goto found;
- if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL)
- break;
}
- return (NULL);
+
+ /* release lock while mallocing */
+ _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex);
+ if ((g = moreglue(NDYNAMIC)) == NULL)
+ return (NULL);
+ _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex);
+ lastglue->next = g;
+ lastglue = g;
+ fp = g->iobs;
found:
fp->_flags = 1; /* reserve this slot; caller sets real flags */
+ _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex);
fp->_p = NULL; /* no current pointer */
fp->_w = 0; /* nothing to read or write */
fp->_r = 0;
@@ -143,8 +155,12 @@ f_prealloc(void)
n = getdtablesize() - FOPEN_MAX + 20; /* 20 for slop. */
for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next)
/* void */;
- if (n > 0)
- g->next = moreglue(n);
+ if (n > 0 && ((g = moreglue(n)) != NULL)) {
+ _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex);
+ lastglue->next = g;
+ lastglue = g;
+ _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex);
+ }
}
/*
@@ -168,12 +184,18 @@ _cleanup(void)
void
__sinit(void)
{
+ _THREAD_PRIVATE_MUTEX(__sinit_mutex);
int i;
+ _THREAD_PRIVATE_MUTEX_LOCK(__sinit_mutex);
+ if (__sdidinit)
+ goto out; /* bail out if caller lost the race */
for (i = 0; i < FOPEN_MAX - 3; i++) {
_FILEEXT_SETUP(usual+i, usualext+i);
}
/* make sure we clean up on exit */
__atexit_register_cleanup(_cleanup); /* conservative */
__sdidinit = 1;
+out:
+ _THREAD_PRIVATE_MUTEX_UNLOCK(__sinit_mutex);
}
diff --git a/lib/libc/stdio/fpurge.c b/lib/libc/stdio/fpurge.c
index 8fa541dd1c7..65bd74994f4 100644
--- a/lib/libc/stdio/fpurge.c
+++ b/lib/libc/stdio/fpurge.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fpurge.c,v 1.8 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: fpurge.c,v 1.9 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -43,7 +43,9 @@
int
fpurge(FILE *fp)
{
+ FLOCKFILE(fp);
if (!fp->_flags) {
+ FUNLOCKFILE(fp);
errno = EBADF;
return(EOF);
}
@@ -54,5 +56,6 @@ fpurge(FILE *fp)
fp->_p = fp->_bf._base;
fp->_r = 0;
fp->_w = fp->_flags & (__SLBF|__SNBF) ? 0 : fp->_bf._size;
+ FUNLOCKFILE(fp);
return (0);
}
diff --git a/lib/libc/stdio/fputc.c b/lib/libc/stdio/fputc.c
index 0c95ed9a14f..98e39603f04 100644
--- a/lib/libc/stdio/fputc.c
+++ b/lib/libc/stdio/fputc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fputc.c,v 1.9 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: fputc.c,v 1.10 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -33,14 +33,9 @@
#include <stdio.h>
#include <errno.h>
-#include "local.h"
int
fputc(int c, FILE *fp)
{
- if (cantwrite(fp)) {
- errno = EBADF;
- return (EOF);
- }
return (putc(c, fp));
}
diff --git a/lib/libc/stdio/fputs.c b/lib/libc/stdio/fputs.c
index 1ec32d85b41..ea8556a29cf 100644
--- a/lib/libc/stdio/fputs.c
+++ b/lib/libc/stdio/fputs.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fputs.c,v 1.9 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: fputs.c,v 1.10 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -44,11 +44,15 @@ fputs(const char *s, FILE *fp)
{
struct __suio uio;
struct __siov iov;
+ int ret;
iov.iov_base = (void *)s;
iov.iov_len = uio.uio_resid = strlen(s);
uio.uio_iov = &iov;
uio.uio_iovcnt = 1;
+ FLOCKFILE(fp);
_SET_ORIENTATION(fp, -1);
- return (__sfvwrite(fp, &uio));
+ ret = __sfvwrite(fp, &uio);
+ FUNLOCKFILE(fp);
+ return (ret);
}
diff --git a/lib/libc/stdio/fputwc.c b/lib/libc/stdio/fputwc.c
index d3871241660..9db70d061ae 100644
--- a/lib/libc/stdio/fputwc.c
+++ b/lib/libc/stdio/fputwc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fputwc.c,v 1.3 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: fputwc.c,v 1.4 2009/11/09 00:18:27 kurt Exp $ */
/* $NetBSD: fputwc.c,v 1.3 2003/03/07 07:11:37 tshiozak Exp $ */
/*-
@@ -80,9 +80,9 @@ fputwc(wchar_t wc, FILE *fp)
{
wint_t r;
- flockfile(fp);
+ FLOCKFILE(fp);
r = __fputwc_unlock(wc, fp);
- funlockfile(fp);
+ FUNLOCKFILE(fp);
return (r);
}
diff --git a/lib/libc/stdio/fputws.c b/lib/libc/stdio/fputws.c
index b1ab6dcd60a..ee76c5bebb8 100644
--- a/lib/libc/stdio/fputws.c
+++ b/lib/libc/stdio/fputws.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fputws.c,v 1.4 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: fputws.c,v 1.5 2009/11/09 00:18:27 kurt Exp $ */
/* $NetBSD: fputws.c,v 1.1 2003/03/07 07:11:37 tshiozak Exp $ */
/*-
@@ -40,17 +40,17 @@ fputws(ws, fp)
const wchar_t * __restrict ws;
FILE * __restrict fp;
{
- flockfile(fp);
+ FLOCKFILE(fp);
_SET_ORIENTATION(fp, 1);
while (*ws != '\0') {
if (__fputwc_unlock(*ws++, fp) == WEOF) {
- funlockfile(fp);
+ FUNLOCKFILE(fp);
return (-1);
}
}
- funlockfile(fp);
+ FUNLOCKFILE(fp);
return (0);
}
diff --git a/lib/libc/stdio/fread.c b/lib/libc/stdio/fread.c
index 99185385b0d..cb6714e1d06 100644
--- a/lib/libc/stdio/fread.c
+++ b/lib/libc/stdio/fread.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fread.c,v 1.9 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: fread.c,v 1.10 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -48,6 +48,7 @@ fread(void *buf, size_t size, size_t count, FILE *fp)
*/
if ((resid = count * size) == 0)
return (0);
+ FLOCKFILE(fp);
if (fp->_r < 0)
fp->_r = 0;
total = resid;
@@ -60,11 +61,13 @@ fread(void *buf, size_t size, size_t count, FILE *fp)
resid -= r;
if (__srefill(fp)) {
/* no more input: return partial result */
+ FUNLOCKFILE(fp);
return ((total - resid) / size);
}
}
(void)memcpy((void *)p, (void *)fp->_p, resid);
fp->_r -= resid;
fp->_p += resid;
+ FUNLOCKFILE(fp);
return (count);
}
diff --git a/lib/libc/stdio/freopen.c b/lib/libc/stdio/freopen.c
index 7b4b480619d..3158fb174dd 100644
--- a/lib/libc/stdio/freopen.c
+++ b/lib/libc/stdio/freopen.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: freopen.c,v 1.12 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: freopen.c,v 1.13 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -60,6 +60,8 @@ freopen(const char *file, const char *mode, FILE *fp)
if (!__sdidinit)
__sinit();
+ FLOCKFILE(fp);
+
/*
* There are actually programs that depend on being able to "freopen"
* descriptors that weren't originally open. Keep this from breaking.
@@ -121,6 +123,7 @@ freopen(const char *file, const char *mode, FILE *fp)
if (f < 0) { /* did not get it after all */
fp->_flags = 0; /* set it free */
+ FUNLOCKFILE(fp);
errno = sverrno; /* restore in case _close clobbered */
return (NULL);
}
@@ -140,6 +143,7 @@ freopen(const char *file, const char *mode, FILE *fp)
/* _file is only a short */
if (f > SHRT_MAX) {
fp->_flags = 0; /* set it free */
+ FUNLOCKFILE(fp);
errno = EMFILE;
return (NULL);
}
@@ -162,5 +166,6 @@ freopen(const char *file, const char *mode, FILE *fp)
*/
if (oflags & O_APPEND)
(void) __sseek((void *)fp, (fpos_t)0, SEEK_END);
+ FUNLOCKFILE(fp);
return (fp);
}
diff --git a/lib/libc/stdio/fseek.c b/lib/libc/stdio/fseek.c
index 98e95750879..0b3dce3ff22 100644
--- a/lib/libc/stdio/fseek.c
+++ b/lib/libc/stdio/fseek.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fseek.c,v 1.9 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: fseek.c,v 1.10 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -70,6 +70,7 @@ fseeko(FILE *fp, off_t offset, int whence)
* Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
* After this, whence is either SEEK_SET or SEEK_END.
*/
+ FLOCKFILE(fp);
switch (whence) {
case SEEK_CUR:
@@ -83,8 +84,10 @@ fseeko(FILE *fp, off_t offset, int whence)
curoff = fp->_offset;
else {
curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
- if (curoff == (fpos_t)-1)
+ if (curoff == (fpos_t)-1) {
+ FUNLOCKFILE(fp);
return (EOF);
+ }
}
if (fp->_flags & __SRD) {
curoff -= fp->_r;
@@ -105,6 +108,7 @@ fseeko(FILE *fp, off_t offset, int whence)
break;
default:
+ FUNLOCKFILE(fp);
errno = EINVAL;
return (EOF);
}
@@ -189,6 +193,7 @@ fseeko(FILE *fp, off_t offset, int whence)
if (HASUB(fp))
FREEUB(fp);
fp->_flags &= ~__SEOF;
+ FUNLOCKFILE(fp);
return (0);
}
@@ -215,6 +220,7 @@ fseeko(FILE *fp, off_t offset, int whence)
fp->_p += n;
fp->_r -= n;
}
+ FUNLOCKFILE(fp);
return (0);
/*
@@ -224,6 +230,7 @@ fseeko(FILE *fp, off_t offset, int whence)
dumb:
if (__sflush(fp) ||
(*seekfn)(fp->_cookie, (fpos_t)offset, whence) == POS_ERR) {
+ FUNLOCKFILE(fp);
return (EOF);
}
/* success: clear EOF indicator and discard ungetc() data */
@@ -233,6 +240,7 @@ dumb:
fp->_r = 0;
/* fp->_w = 0; */ /* unnecessary (I think...) */
fp->_flags &= ~__SEOF;
+ FUNLOCKFILE(fp);
return (0);
}
diff --git a/lib/libc/stdio/ftell.c b/lib/libc/stdio/ftell.c
index 3def3bfb6bc..0a22a14451c 100644
--- a/lib/libc/stdio/ftell.c
+++ b/lib/libc/stdio/ftell.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ftell.c,v 1.8 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: ftell.c,v 1.9 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -45,20 +45,22 @@ ftello(FILE *fp)
if (fp->_seek == NULL) {
errno = ESPIPE; /* historic practice */
- return ((off_t)-1);
+ pos = -1;
+ goto out;
}
/*
* Find offset of underlying I/O object, then
* adjust for buffered bytes.
*/
+ FLOCKFILE(fp);
__sflush(fp); /* may adjust seek offset on append stream */
if (fp->_flags & __SOFF)
pos = fp->_offset;
else {
pos = (*fp->_seek)(fp->_cookie, (fpos_t)0, SEEK_CUR);
- if (pos == -1L)
- return (pos);
+ if (pos == -1)
+ goto out;
}
if (fp->_flags & __SRD) {
/*
@@ -77,6 +79,7 @@ ftello(FILE *fp)
*/
pos += fp->_p - fp->_bf._base;
}
+out: FUNLOCKFILE(fp);
return (pos);
}
diff --git a/lib/libc/stdio/fvwrite.c b/lib/libc/stdio/fvwrite.c
index 9e916afddf5..1088991fd59 100644
--- a/lib/libc/stdio/fvwrite.c
+++ b/lib/libc/stdio/fvwrite.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fvwrite.c,v 1.16 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: fvwrite.c,v 1.17 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -134,7 +134,7 @@ __sfvwrite(FILE *fp, struct __suio *uio)
COPY(w);
/* fp->_w -= w; */ /* unneeded */
fp->_p += w;
- if (fflush(fp))
+ if (__sflush(fp))
goto err;
} else if (len >= (w = fp->_bf._size)) {
/* write directly */
@@ -174,7 +174,7 @@ __sfvwrite(FILE *fp, struct __suio *uio)
COPY(w);
/* fp->_w -= w; */
fp->_p += w;
- if (fflush(fp))
+ if (__sflush(fp))
goto err;
} else if (s >= (w = fp->_bf._size)) {
w = (*fp->_write)(fp->_cookie, p, w);
@@ -188,7 +188,7 @@ __sfvwrite(FILE *fp, struct __suio *uio)
}
if ((nldist -= w) == 0) {
/* copied the newline: flush and forget */
- if (fflush(fp))
+ if (__sflush(fp))
goto err;
nlknown = 0;
}
diff --git a/lib/libc/stdio/fwalk.c b/lib/libc/stdio/fwalk.c
index 9c6ec873282..8ac6628aa2b 100644
--- a/lib/libc/stdio/fwalk.c
+++ b/lib/libc/stdio/fwalk.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fwalk.c,v 1.9 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: fwalk.c,v 1.10 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -45,8 +45,9 @@ _fwalk(int (*function)(FILE *))
ret = 0;
for (g = &__sglue; g != NULL; g = g->next)
- for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
- if (fp->_flags != 0)
+ for (fp = g->iobs, n = g->niobs; --n >= 0; fp++) {
+ if ((fp->_flags != 0) && ((fp->_flags & __SIGN) == 0))
ret |= (*function)(fp);
+ }
return (ret);
}
diff --git a/lib/libc/stdio/fwide.c b/lib/libc/stdio/fwide.c
index 974374254b8..93cddc68e9d 100644
--- a/lib/libc/stdio/fwide.c
+++ b/lib/libc/stdio/fwide.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fwide.c,v 1.3 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: fwide.c,v 1.4 2009/11/09 00:18:27 kurt Exp $ */
/* $NetBSD: fwide.c,v 1.2 2003/01/18 11:29:54 thorpej Exp $ */
/*-
@@ -49,7 +49,7 @@ fwide(FILE *fp, int mode)
else if (mode < 0)
mode = -1;
- flockfile(fp);
+ FLOCKFILE(fp);
wcio = WCIO_GET(fp);
if (!wcio)
return 0; /* XXX */
@@ -58,7 +58,7 @@ fwide(FILE *fp, int mode)
wcio->wcio_mode = mode;
else
mode = wcio->wcio_mode;
- funlockfile(fp);
+ FUNLOCKFILE(fp);
return mode;
}
diff --git a/lib/libc/stdio/fwrite.c b/lib/libc/stdio/fwrite.c
index 9308e4d66a7..1e5f0745dfa 100644
--- a/lib/libc/stdio/fwrite.c
+++ b/lib/libc/stdio/fwrite.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fwrite.c,v 1.8 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: fwrite.c,v 1.9 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -45,6 +45,7 @@ fwrite(const void *buf, size_t size, size_t count, FILE *fp)
size_t n;
struct __suio uio;
struct __siov iov;
+ int ret;
/*
* ANSI and SUSv2 require a return value of 0 if size or count are 0.
@@ -62,7 +63,10 @@ fwrite(const void *buf, size_t size, size_t count, FILE *fp)
* skip the divide if this happens, since divides are
* generally slow and since this occurs whenever size==0.
*/
- if (__sfvwrite(fp, &uio) == 0)
+ FLOCKFILE(fp);
+ ret = __sfvwrite(fp, &uio);
+ FUNLOCKFILE(fp);
+ if (ret == 0)
return (count);
return ((n - uio.uio_resid) / size);
}
diff --git a/lib/libc/stdio/getc.c b/lib/libc/stdio/getc.c
index b77696c3d87..6879cbb7913 100644
--- a/lib/libc/stdio/getc.c
+++ b/lib/libc/stdio/getc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: getc.c,v 1.8 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: getc.c,v 1.9 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -32,6 +32,7 @@
*/
#include <stdio.h>
+#include "local.h"
/*
* A subroutine version of the macro getc_unlocked.
@@ -54,8 +55,8 @@ getc(FILE *fp)
{
int c;
- flockfile(fp);
+ FLOCKFILE(fp);
c = __sgetc(fp);
- funlockfile(fp);
+ FUNLOCKFILE(fp);
return (c);
}
diff --git a/lib/libc/stdio/gets.c b/lib/libc/stdio/gets.c
index c3666d88492..c2e1b50574e 100644
--- a/lib/libc/stdio/gets.c
+++ b/lib/libc/stdio/gets.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: gets.c,v 1.11 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: gets.c,v 1.12 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -32,6 +32,7 @@
*/
#include <stdio.h>
+#include "local.h"
__warn_references(gets,
"warning: gets() is very unsafe; consider using fgets()");
@@ -42,14 +43,17 @@ gets(char *buf)
int c;
char *s;
- for (s = buf; (c = getchar()) != '\n';)
+ FLOCKFILE(stdin);
+ for (s = buf; (c = getchar_unlocked()) != '\n';)
if (c == EOF)
- if (s == buf)
+ if (s == buf) {
+ FUNLOCKFILE(stdin);
return (NULL);
- else
+ } else
break;
else
*s++ = c;
*s = '\0';
+ FUNLOCKFILE(stdin);
return (buf);
}
diff --git a/lib/libc/stdio/local.h b/lib/libc/stdio/local.h
index 68babbd4f0c..7e227558544 100644
--- a/lib/libc/stdio/local.h
+++ b/lib/libc/stdio/local.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: local.h,v 1.14 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: local.h,v 1.15 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
@@ -42,6 +42,7 @@
#include "fileext.h"
int __sflush(FILE *);
+int __sflush_locked(FILE *);
FILE *__sfp(void);
int __srefill(FILE *);
int __sread(void *, char *, int);
@@ -56,6 +57,7 @@ int _fwalk(int (*)(FILE *));
int __swsetup(FILE *);
int __sflags(const char *, int *);
wint_t __fgetwc_unlock(FILE *);
+int __vfprintf(FILE *, const char *, __va_list);
extern void __atexit_register_cleanup(void (*)(void));
extern int __sdidinit;
@@ -86,3 +88,6 @@ extern int __sdidinit;
free((char *)(fp)->_lb._base); \
(fp)->_lb._base = NULL; \
}
+
+#define FLOCKFILE(fp) do { if (__isthreaded) flockfile(fp); } while (0)
+#define FUNLOCKFILE(fp) do { if (__isthreaded) funlockfile(fp); } while (0)
diff --git a/lib/libc/stdio/putc.c b/lib/libc/stdio/putc.c
index 9426916024e..f0a6ea91081 100644
--- a/lib/libc/stdio/putc.c
+++ b/lib/libc/stdio/putc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: putc.c,v 1.9 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: putc.c,v 1.10 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -60,8 +60,8 @@ putc(int c, FILE *fp)
{
int ret;
- flockfile(fp);
+ FLOCKFILE(fp);
ret = putc_unlocked(c, fp);
- funlockfile(fp);
+ FUNLOCKFILE(fp);
return (ret);
}
diff --git a/lib/libc/stdio/puts.c b/lib/libc/stdio/puts.c
index c932a83ce1b..d909be9fe9f 100644
--- a/lib/libc/stdio/puts.c
+++ b/lib/libc/stdio/puts.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: puts.c,v 1.9 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: puts.c,v 1.10 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -33,6 +33,7 @@
#include <stdio.h>
#include <string.h>
+#include "local.h"
#include "fvwrite.h"
/*
@@ -44,6 +45,7 @@ puts(const char *s)
size_t c = strlen(s);
struct __suio uio;
struct __siov iov[2];
+ int ret;
iov[0].iov_base = (void *)s;
iov[0].iov_len = c;
@@ -52,5 +54,8 @@ puts(const char *s)
uio.uio_resid = c + 1;
uio.uio_iov = &iov[0];
uio.uio_iovcnt = 2;
- return (__sfvwrite(stdout, &uio) ? EOF : '\n');
+ FLOCKFILE(stdout);
+ ret = __sfvwrite(stdout, &uio);
+ FUNLOCKFILE(stdout);
+ return (ret ? EOF : '\n');
}
diff --git a/lib/libc/stdio/putw.c b/lib/libc/stdio/putw.c
index af5d335ceb1..fb6d5d8ae87 100644
--- a/lib/libc/stdio/putw.c
+++ b/lib/libc/stdio/putw.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: putw.c,v 1.8 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: putw.c,v 1.9 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -32,6 +32,7 @@
*/
#include <stdio.h>
+#include "local.h"
#include "fvwrite.h"
int
@@ -39,10 +40,14 @@ putw(int w, FILE *fp)
{
struct __suio uio;
struct __siov iov;
+ int ret;
iov.iov_base = &w;
iov.iov_len = uio.uio_resid = sizeof(w);
uio.uio_iov = &iov;
uio.uio_iovcnt = 1;
- return (__sfvwrite(fp, &uio));
+ FLOCKFILE(fp);
+ ret = __sfvwrite(fp, &uio);
+ FUNLOCKFILE(fp);
+ return (ret);
}
diff --git a/lib/libc/stdio/refill.c b/lib/libc/stdio/refill.c
index 8b690b46b25..165c72a64ba 100644
--- a/lib/libc/stdio/refill.c
+++ b/lib/libc/stdio/refill.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: refill.c,v 1.10 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: refill.c,v 1.11 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -39,9 +39,8 @@
static int
lflush(FILE *fp)
{
-
if ((fp->_flags & (__SLBF|__SWR)) == (__SLBF|__SWR))
- return (__sflush(fp));
+ return (__sflush_locked(fp)); /* ignored... */
return (0);
}
@@ -103,8 +102,16 @@ __srefill(FILE *fp)
* flush all line buffered output files, per the ANSI C
* standard.
*/
- if (fp->_flags & (__SLBF|__SNBF))
+ if (fp->_flags & (__SLBF|__SNBF)) {
+ /* Ignore this file in _fwalk to avoid potential deadlock. */
+ fp->_flags |= __SIGN;
(void) _fwalk(lflush);
+ fp->_flags &= ~__SIGN;
+
+ /* Now flush this file without locking it. */
+ if ((fp->_flags & (__SLBF|__SWR)) == (__SLBF|__SWR))
+ __sflush(fp);
+ }
fp->_p = fp->_bf._base;
fp->_r = (*fp->_read)(fp->_cookie, (char *)fp->_p, fp->_bf._size);
fp->_flags &= ~__SMOD; /* buffer contents are again pristine */
diff --git a/lib/libc/stdio/setvbuf.c b/lib/libc/stdio/setvbuf.c
index e7274465265..6c49f7a5d6d 100644
--- a/lib/libc/stdio/setvbuf.c
+++ b/lib/libc/stdio/setvbuf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: setvbuf.c,v 1.10 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: setvbuf.c,v 1.11 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -61,6 +61,7 @@ setvbuf(FILE *fp, char *buf, int mode, size_t size)
* malloc()ed. We also clear any eof condition, as if this were
* a seek.
*/
+ FLOCKFILE(fp);
ret = 0;
(void)__sflush(fp);
if (HASUB(fp))
@@ -107,6 +108,7 @@ nbf:
fp->_w = 0;
fp->_bf._base = fp->_p = fp->_nbuf;
fp->_bf._size = 1;
+ FUNLOCKFILE(fp);
return (ret);
}
flags |= __SMBF;
@@ -145,6 +147,7 @@ nbf:
/* begin/continue reading, or stay in intermediate state */
fp->_w = 0;
}
+ FUNLOCKFILE(fp);
__atexit_register_cleanup(_cleanup);
return (ret);
diff --git a/lib/libc/stdio/snprintf.c b/lib/libc/stdio/snprintf.c
index 678345894a8..09659c8fb7d 100644
--- a/lib/libc/stdio/snprintf.c
+++ b/lib/libc/stdio/snprintf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: snprintf.c,v 1.16 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: snprintf.c,v 1.17 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -60,7 +60,7 @@ snprintf(char *str, size_t n, const char *fmt, ...)
f._bf._base = f._p = (unsigned char *)str;
f._bf._size = f._w = n - 1;
va_start(ap, fmt);
- ret = vfprintf(&f, fmt, ap);
+ ret = __vfprintf(&f, fmt, ap);
va_end(ap);
*f._p = '\0';
return (ret);
diff --git a/lib/libc/stdio/sprintf.c b/lib/libc/stdio/sprintf.c
index 4e9cd37a7c5..faa7c73ca20 100644
--- a/lib/libc/stdio/sprintf.c
+++ b/lib/libc/stdio/sprintf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sprintf.c,v 1.15 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: sprintf.c,v 1.16 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -56,7 +56,7 @@ sprintf(char *str, const char *fmt, ...)
f._bf._base = f._p = (unsigned char *)str;
f._bf._size = f._w = INT_MAX;
va_start(ap, fmt);
- ret = vfprintf(&f, fmt, ap);
+ ret = __vfprintf(&f, fmt, ap);
va_end(ap);
*f._p = '\0';
return (ret);
diff --git a/lib/libc/stdio/ungetc.c b/lib/libc/stdio/ungetc.c
index 85e0895f7b5..675733aa6f7 100644
--- a/lib/libc/stdio/ungetc.c
+++ b/lib/libc/stdio/ungetc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ungetc.c,v 1.11 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: ungetc.c,v 1.12 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -82,17 +82,20 @@ ungetc(int c, FILE *fp)
return (EOF);
if (!__sdidinit)
__sinit();
+ FLOCKFILE(fp);
_SET_ORIENTATION(fp, -1);
if ((fp->_flags & __SRD) == 0) {
/*
* Not already reading: no good unless reading-and-writing.
* Otherwise, flush any current write stuff.
*/
- if ((fp->_flags & __SRW) == 0)
+ if ((fp->_flags & __SRW) == 0) {
+error: FUNLOCKFILE(fp);
return (EOF);
+ }
if (fp->_flags & __SWR) {
if (__sflush(fp))
- return (EOF);
+ goto error;
fp->_flags &= ~__SWR;
fp->_w = 0;
fp->_lbfsize = 0;
@@ -107,9 +110,10 @@ ungetc(int c, FILE *fp)
*/
if (HASUB(fp)) {
if (fp->_r >= _UB(fp)._size && __submore(fp))
- return (EOF);
+ goto error;
*--fp->_p = c;
- fp->_r++;
+inc_ret: fp->_r++;
+ FUNLOCKFILE(fp);
return (c);
}
fp->_flags &= ~__SEOF;
@@ -122,8 +126,7 @@ ungetc(int c, FILE *fp)
if (fp->_bf._base != NULL && fp->_p > fp->_bf._base &&
fp->_p[-1] == c) {
fp->_p--;
- fp->_r++;
- return (c);
+ goto inc_ret;
}
/*
@@ -137,5 +140,6 @@ ungetc(int c, FILE *fp)
fp->_ubuf[sizeof(fp->_ubuf) - 1] = c;
fp->_p = &fp->_ubuf[sizeof(fp->_ubuf) - 1];
fp->_r = 1;
+ FUNLOCKFILE(fp);
return (c);
}
diff --git a/lib/libc/stdio/ungetwc.c b/lib/libc/stdio/ungetwc.c
index 06f78c07a60..60bee069f52 100644
--- a/lib/libc/stdio/ungetwc.c
+++ b/lib/libc/stdio/ungetwc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ungetwc.c,v 1.3 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: ungetwc.c,v 1.4 2009/11/09 00:18:27 kurt Exp $ */
/* $NetBSD: ungetwc.c,v 1.2 2003/01/18 11:29:59 thorpej Exp $ */
/*-
@@ -42,7 +42,7 @@ ungetwc(wint_t wc, FILE *fp)
if (wc == WEOF)
return WEOF;
- flockfile(fp);
+ FLOCKFILE(fp);
_SET_ORIENTATION(fp, 1);
/*
* XXX since we have no way to transform a wchar string to
@@ -52,19 +52,19 @@ ungetwc(wint_t wc, FILE *fp)
wcio = WCIO_GET(fp);
if (wcio == 0) {
- funlockfile(fp);
+ FUNLOCKFILE(fp);
errno = ENOMEM; /* XXX */
return WEOF;
}
if (wcio->wcio_ungetwc_inbuf >= WCIO_UNGETWC_BUFSIZE) {
- funlockfile(fp);
+ FUNLOCKFILE(fp);
return WEOF;
}
wcio->wcio_ungetwc_buf[wcio->wcio_ungetwc_inbuf++] = wc;
__sclearerr(fp);
- funlockfile(fp);
+ FUNLOCKFILE(fp);
return wc;
}
diff --git a/lib/libc/stdio/vasprintf.c b/lib/libc/stdio/vasprintf.c
index 073562cd31a..8fe7c5bb553 100644
--- a/lib/libc/stdio/vasprintf.c
+++ b/lib/libc/stdio/vasprintf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: vasprintf.c,v 1.15 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: vasprintf.c,v 1.16 2009/11/09 00:18:27 kurt Exp $ */
/*
* Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
@@ -37,7 +37,7 @@ vasprintf(char **str, const char *fmt, __va_list ap)
if (f._bf._base == NULL)
goto err;
f._bf._size = f._w = 127; /* Leave room for the NUL */
- ret = vfprintf(&f, fmt, ap);
+ ret = __vfprintf(&f, fmt, ap);
if (ret == -1)
goto err;
*f._p = '\0';
diff --git a/lib/libc/stdio/vfprintf.c b/lib/libc/stdio/vfprintf.c
index 07f4f88d1f6..5beb280a2dd 100644
--- a/lib/libc/stdio/vfprintf.c
+++ b/lib/libc/stdio/vfprintf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: vfprintf.c,v 1.57 2009/10/28 21:15:02 naddy Exp $ */
+/* $OpenBSD: vfprintf.c,v 1.58 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
@@ -130,8 +130,8 @@ __sbprintf(FILE *fp, const char *fmt, va_list ap)
fake._lbfsize = 0; /* not actually used, but Just In Case */
/* do the work, then copy any error status */
- ret = vfprintf(&fake, fmt, ap);
- if (ret >= 0 && fflush(&fake))
+ ret = __vfprintf(&fake, fmt, ap);
+ if (ret >= 0 && __sflush(&fake))
ret = EOF;
if (fake._flags & __SERR)
fp->_flags |= __SERR;
@@ -190,6 +190,17 @@ static int exponent(char *, int, int);
int
vfprintf(FILE *fp, const char *fmt0, __va_list ap)
{
+ int ret;
+
+ FLOCKFILE(fp);
+ ret = __vfprintf(fp, fmt0, ap);
+ FUNLOCKFILE(fp);
+ return (ret);
+}
+
+int
+__vfprintf(FILE *fp, const char *fmt0, __va_list ap)
+{
char *fmt; /* format string */
int ch; /* character from fmt */
int n, n2; /* handy integers (short term usage) */
diff --git a/lib/libc/stdio/vfscanf.c b/lib/libc/stdio/vfscanf.c
index 5b2fd03f4eb..48b241f79ae 100644
--- a/lib/libc/stdio/vfscanf.c
+++ b/lib/libc/stdio/vfscanf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: vfscanf.c,v 1.24 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: vfscanf.c,v 1.25 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -117,6 +117,7 @@ VFSCANF(FILE *fp, const char *fmt0, __va_list ap)
static short basefix[17] =
{ 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
+ FLOCKFILE(fp);
_SET_ORIENTATION(fp, -1);
nassigned = 0;
@@ -124,8 +125,10 @@ VFSCANF(FILE *fp, const char *fmt0, __va_list ap)
base = 0; /* XXX just to keep gcc happy */
for (;;) {
c = *fmt++;
- if (c == 0)
+ if (c == 0) {
+ FUNLOCKFILE(fp);
return (nassigned);
+ }
if (isspace(c)) {
while ((fp->_r > 0 || __srefill(fp) == 0) &&
isspace(*fp->_p))
@@ -293,6 +296,7 @@ literal:
* Disgusting backwards compatibility hacks. XXX
*/
case '\0': /* compat */
+ FUNLOCKFILE(fp);
return (EOF);
default: /* compat */
@@ -690,8 +694,10 @@ literal:
}
}
input_failure:
- return (nassigned ? nassigned : -1);
+ if (nassigned == 0)
+ nassigned = -1;
match_failure:
+ FUNLOCKFILE(fp);
return (nassigned);
}
diff --git a/lib/libc/stdio/vsnprintf.c b/lib/libc/stdio/vsnprintf.c
index a3931226bd5..8b1a088da85 100644
--- a/lib/libc/stdio/vsnprintf.c
+++ b/lib/libc/stdio/vsnprintf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: vsnprintf.c,v 1.14 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: vsnprintf.c,v 1.15 2009/11/09 00:18:28 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -58,7 +58,7 @@ vsnprintf(char *str, size_t n, const char *fmt, __va_list ap)
f._flags = __SWR | __SSTR;
f._bf._base = f._p = (unsigned char *)str;
f._bf._size = f._w = n - 1;
- ret = vfprintf(&f, fmt, ap);
+ ret = __vfprintf(&f, fmt, ap);
*f._p = '\0';
return (ret);
}
diff --git a/lib/libc/stdio/vsprintf.c b/lib/libc/stdio/vsprintf.c
index cfa1349984d..308ff375ad2 100644
--- a/lib/libc/stdio/vsprintf.c
+++ b/lib/libc/stdio/vsprintf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: vsprintf.c,v 1.15 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: vsprintf.c,v 1.16 2009/11/09 00:18:28 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -53,7 +53,7 @@ vsprintf(char *str, const char *fmt, __va_list ap)
f._flags = __SWR | __SSTR;
f._bf._base = f._p = (unsigned char *)str;
f._bf._size = f._w = INT_MAX;
- ret = vfprintf(&f, fmt, ap);
+ ret = __vfprintf(&f, fmt, ap);
*f._p = '\0';
return (ret);
}
diff --git a/lib/libc/stdio/wbuf.c b/lib/libc/stdio/wbuf.c
index 990f1ea48c4..6aa00e1d6c4 100644
--- a/lib/libc/stdio/wbuf.c
+++ b/lib/libc/stdio/wbuf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: wbuf.c,v 1.11 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: wbuf.c,v 1.12 2009/11/09 00:18:28 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -65,20 +65,20 @@ __swbuf(int c, FILE *fp)
* stuff c into the buffer. If this causes the buffer to fill
* completely, or if c is '\n' and the file is line buffered,
* flush it (perhaps a second time). The second flush will always
- * happen on unbuffered streams, where _bf._size==1; fflush()
+ * happen on unbuffered streams, where _bf._size==1; __sflush()
* guarantees that putc() will always call wbuf() by setting _w
* to 0, so we need not do anything else.
*/
n = fp->_p - fp->_bf._base;
if (n >= fp->_bf._size) {
- if (fflush(fp))
+ if (__sflush(fp))
return (EOF);
n = 0;
}
fp->_w--;
*fp->_p++ = c;
if (++n == fp->_bf._size || (fp->_flags & __SLBF && c == '\n'))
- if (fflush(fp))
+ if (__sflush(fp))
return (EOF);
return (c);
}
diff --git a/lib/libpthread/uthread/uthread_file.c b/lib/libpthread/uthread/uthread_file.c
index d8c9cdcb818..25c2d0945cc 100644
--- a/lib/libpthread/uthread/uthread_file.c
+++ b/lib/libpthread/uthread/uthread_file.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: uthread_file.c,v 1.16 2009/10/22 01:23:16 guenther Exp $ */
+/* $OpenBSD: uthread_file.c,v 1.17 2009/11/09 00:18:27 kurt Exp $ */
/*
* Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
* All rights reserved.
@@ -41,6 +41,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
#include <sys/queue.h>
#ifdef _THREAD_SAFE
#include <pthread.h>
@@ -173,68 +174,65 @@ do_lock(int idx, FILE *fp)
return(p);
}
+
void
flockfile(FILE * fp)
{
int idx = file_idx(fp);
struct file_lock *p;
- /* Check if this is a real file: */
- if (fileno(fp) >= 0) {
- /* Lock the hash table: */
- _SPINLOCK(&hash_lock);
+ /* Lock the hash table: */
+ _SPINLOCK(&hash_lock);
- /* Check if the static array has not been initialised: */
- if (!init_done) {
- /* Initialise the global array: */
- memset(flh,0,sizeof(flh));
+ /* Check if the static array has not been initialised: */
+ if (!init_done) {
+ /* Initialise the global array: */
+ memset(flh,0,sizeof(flh));
- /* Flag the initialisation as complete: */
- init_done = 1;
- }
+ /* Flag the initialisation as complete: */
+ init_done = 1;
+ }
- /* Get a pointer to any existing lock for the file: */
- if ((p = find_lock(idx, fp)) == NULL) {
- /*
- * The file is not locked, so this thread can
- * grab the lock:
- */
- p = do_lock(idx, fp);
+ /* Get a pointer to any existing lock for the file: */
+ if ((p = find_lock(idx, fp)) == NULL) {
+ /*
+ * The file is not locked, so this thread can
+ * grab the lock:
+ */
+ p = do_lock(idx, fp);
- /* Unlock the hash table: */
- _SPINUNLOCK(&hash_lock);
+ /* Unlock the hash table: */
+ _SPINUNLOCK(&hash_lock);
+ /*
+ * The file is already locked, so check if the
+ * running thread is the owner:
+ */
+ } else if (p->owner == _thread_run) {
/*
- * The file is already locked, so check if the
- * running thread is the owner:
+ * The running thread is already the
+ * owner, so increment the count of
+ * the number of times it has locked
+ * the file:
*/
- } else if (p->owner == _thread_run) {
- /*
- * The running thread is already the
- * owner, so increment the count of
- * the number of times it has locked
- * the file:
- */
- p->count++;
+ p->count++;
- /* Unlock the hash table: */
- _SPINUNLOCK(&hash_lock);
- } else {
- /*
- * The file is locked for another thread.
- * Append this thread to the queue of
- * threads waiting on the lock.
- */
- TAILQ_INSERT_TAIL(&p->l_head,_thread_run,qe);
+ /* Unlock the hash table: */
+ _SPINUNLOCK(&hash_lock);
+ } else {
+ /*
+ * The file is locked for another thread.
+ * Append this thread to the queue of
+ * threads waiting on the lock.
+ */
+ TAILQ_INSERT_TAIL(&p->l_head,_thread_run,qe);
- /* Unlock the hash table: */
- _SPINUNLOCK(&hash_lock);
+ /* Unlock the hash table: */
+ _SPINUNLOCK(&hash_lock);
- /* Wait on the FILE lock: */
- _thread_kern_sched_state(PS_FILE_WAIT, "", 0);
- }
+ /* Wait on the FILE lock: */
+ _thread_kern_sched_state(PS_FILE_WAIT, "", 0);
}
- return;
}
int
@@ -244,48 +242,45 @@ ftrylockfile(FILE * fp)
int idx = file_idx(fp);
struct file_lock *p;
- /* Check if this is a real file: */
- if (fileno(fp) >= 0) {
- /* Lock the hash table: */
- _SPINLOCK(&hash_lock);
+ /* Lock the hash table: */
+ _SPINLOCK(&hash_lock);
- /* Get a pointer to any existing lock for the file: */
- if ((p = find_lock(idx, fp)) == NULL) {
- /*
- * The file is not locked, so this thread can
- * grab the lock:
- */
- p = do_lock(idx, fp);
+ /* Get a pointer to any existing lock for the file: */
+ if ((p = find_lock(idx, fp)) == NULL) {
+ /*
+ * The file is not locked, so this thread can
+ * grab the lock:
+ */
+ p = do_lock(idx, fp);
+ /*
+ * The file is already locked, so check if the
+ * running thread is the owner:
+ */
+ } else if (p->owner == _thread_run) {
/*
- * The file is already locked, so check if the
- * running thread is the owner:
+ * The running thread is already the
+ * owner, so increment the count of
+ * the number of times it has locked
+ * the file:
*/
- } else if (p->owner == _thread_run) {
- /*
- * The running thread is already the
- * owner, so increment the count of
- * the number of times it has locked
- * the file:
- */
- p->count++;
- } else {
- /*
- * The file is locked for another thread,
- * so this try fails.
- */
- p = NULL;
- }
+ p->count++;
+ } else {
+ /*
+ * The file is locked for another thread,
+ * so this try fails.
+ */
+ p = NULL;
+ }
- /* Check if the lock was obtained: */
- if (p != NULL)
- /* Return success: */
- ret = 0;
+ /* Check if the lock was obtained: */
+ if (p != NULL)
+ /* Return success: */
+ ret = 0;
- /* Unlock the hash table: */
- _SPINUNLOCK(&hash_lock);
+ /* Unlock the hash table: */
+ _SPINUNLOCK(&hash_lock);
- }
return (ret);
}
@@ -295,68 +290,64 @@ funlockfile(FILE * fp)
int idx = file_idx(fp);
struct file_lock *p;
- /* Check if this is a real file: */
- if (fileno(fp) >= 0) {
- /*
- * Defer signals to protect the scheduling queues from
- * access by the signal handler:
- */
- _thread_kern_sig_defer();
+ /*
+ * Defer signals to protect the scheduling queues from
+ * access by the signal handler:
+ */
+ _thread_kern_sig_defer();
- /* Lock the hash table: */
- _SPINLOCK(&hash_lock);
+ /* Lock the hash table: */
+ _SPINLOCK(&hash_lock);
+ /*
+ * Get a pointer to the lock for the file and check that
+ * the running thread is the one with the lock:
+ */
+ if ((p = find_lock(idx, fp)) != NULL &&
+ p->owner == _thread_run) {
/*
- * Get a pointer to the lock for the file and check that
- * the running thread is the one with the lock:
+ * Check if this thread has locked the FILE
+ * more than once:
*/
- if ((p = find_lock(idx, fp)) != NULL &&
- p->owner == _thread_run) {
+ if (p->count > 1)
/*
- * Check if this thread has locked the FILE
- * more than once:
+ * Decrement the count of the number of
+ * times the running thread has locked this
+ * file:
*/
- if (p->count > 1)
- /*
- * Decrement the count of the number of
- * times the running thread has locked this
- * file:
- */
- p->count--;
- else {
+ p->count--;
+ else {
+ /*
+ * The running thread will release the
+ * lock now:
+ */
+ p->count = 0;
+
+ /* Get the new owner of the lock: */
+ if ((p->owner = TAILQ_FIRST(&p->l_head)) != NULL) {
+ /* Pop the thread off the queue: */
+ TAILQ_REMOVE(&p->l_head,p->owner,qe);
+
/*
- * The running thread will release the
- * lock now:
+ * This is the first lock for the new
+ * owner:
*/
- p->count = 0;
-
- /* Get the new owner of the lock: */
- if ((p->owner = TAILQ_FIRST(&p->l_head)) != NULL) {
- /* Pop the thread off the queue: */
- TAILQ_REMOVE(&p->l_head,p->owner,qe);
-
- /*
- * This is the first lock for the new
- * owner:
- */
- p->count = 1;
-
- /* Allow the new owner to run: */
- PTHREAD_NEW_STATE(p->owner,PS_RUNNING);
- }
+ p->count = 1;
+
+ /* Allow the new owner to run: */
+ PTHREAD_NEW_STATE(p->owner,PS_RUNNING);
}
}
+ }
- /* Unlock the hash table: */
- _SPINUNLOCK(&hash_lock);
+ /* Unlock the hash table: */
+ _SPINUNLOCK(&hash_lock);
- /*
- * Undefer and handle pending signals, yielding if
- * necessary:
- */
- _thread_kern_sig_undefer();
- }
- return;
+ /*
+ * Undefer and handle pending signals, yielding if
+ * necessary:
+ */
+ _thread_kern_sig_undefer();
}
#endif