fix usage of locks with vfork
[musl] / src / stdio / __stdio_read.c
index 218bd88..c99ca9a 100644 (file)
@@ -1,4 +1,11 @@
 #include "stdio_impl.h"
+#include <pthread.h>
+
+static void cleanup(void *p)
+{
+       FILE *f = p;
+       if (!f->lockcount) __unlockfile(f);
+}
 
 size_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
 {
@@ -8,7 +15,13 @@ size_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
        };
        ssize_t cnt;
 
-       cnt = syscall(SYS_readv, f->fd, iov, 2);
+       if (libc.main_thread) {
+               pthread_cleanup_push(cleanup, f);
+               cnt = syscall_cp(SYS_readv, f->fd, iov, 2);
+               pthread_cleanup_pop(0);
+       } else {
+               cnt = syscall(SYS_readv, f->fd, iov, 2);
+       }
        if (cnt <= 0) {
                f->flags |= F_EOF ^ ((F_ERR^F_EOF) & cnt);
                f->rpos = f->rend = 0;