From: Waldemar Kozaczuk <[email protected]>
Committer: Waldemar Kozaczuk <[email protected]>
Branch: master

libc: replaced 6 files in /libc/stdio/ that uses syscall instruction with musl 
copies and macros

Signed-off-by: Waldemar Kozaczuk <[email protected]>
Message-Id: <[email protected]>

---
diff --git a/Makefile b/Makefile
--- a/Makefile
+++ b/Makefile
@@ -1413,10 +1413,12 @@ libc += stdio/__fopen_rb_ca.o
 libc += stdio/__fprintf_chk.o
 libc += stdio/__lockfile.o
 musl += stdio/__overflow.o
-libc += stdio/__stdio_close.o
+musl += stdio/__stdio_close.o
+$(out)/musl/src/stdio/__stdio_close.o: CFLAGS += --include 
libc/syscall_to_function.h
 musl += stdio/__stdio_exit.o
 libc += stdio/__stdio_read.o
-libc += stdio/__stdio_seek.o
+musl += stdio/__stdio_seek.o
+$(out)/musl/src/stdio/__stdio_seek.o: CFLAGS += --include 
libc/syscall_to_function.h
 libc += stdio/__stdio_write.o
 libc += stdio/__stdout_write.o
 musl += stdio/__string_read.o
@@ -1442,15 +1444,17 @@ musl += stdio/fgetws.o
 musl += stdio/fileno.o
 libc += stdio/flockfile.o
 libc += stdio/fmemopen.o
-libc += stdio/fopen.o
+musl += stdio/fopen.o
+$(out)/musl/src/stdio/fopen.o: CFLAGS += --include libc/syscall_to_function.h
 musl += stdio/fprintf.o
 libc += stdio/fputc.o
 musl += stdio/fputs.o
 musl += stdio/fputwc.o
 musl += stdio/fputws.o
 musl += stdio/fread.o
 libc += stdio/__fread_chk.o
-libc += stdio/freopen.o
+musl += stdio/freopen.o
+$(out)/musl/src/stdio/freopen.o: CFLAGS += --include libc/syscall_to_function.h
 musl += stdio/fscanf.o
 musl += stdio/fseek.o
 musl += stdio/fsetpos.o
@@ -1499,8 +1503,10 @@ libc += stdio/stdout.o
 musl += stdio/swprintf.o
 musl += stdio/swscanf.o
 musl += stdio/tempnam.o
-libc += stdio/tmpfile.o
-libc += stdio/tmpnam.o
+musl += stdio/tmpfile.o
+$(out)/musl/src/stdio/tmpfile.o: CFLAGS += --include libc/syscall_to_function.h
+musl += stdio/tmpnam.o
+$(out)/musl/src/stdio/tmpnam.o: CFLAGS += --include libc/syscall_to_function.h
 musl += stdio/ungetc.o
 musl += stdio/ungetwc.o
 musl += stdio/vasprintf.o
diff --git a/libc/aliases.ld b/libc/aliases.ld
--- a/libc/aliases.ld
+++ b/libc/aliases.ld
@@ -31,6 +31,9 @@ __setlocale = setlocale;
 /* multibyte */
 __mbrlen = mbrlen;
 
+/* stdio */
+__dup3 = dup3;
+
 /* string */
 __strtok_r = strtok_r;
 __strndup = strndup;
diff --git a/libc/stdio/__stdio_close.c b/libc/stdio/__stdio_close.c
--- a/libc/stdio/__stdio_close.c
+++ b/libc/stdio/__stdio_close.c
@@ -1,7 +0,0 @@
-#include <unistd.h>
-#include "stdio_impl.h"
-
-int __stdio_close(FILE *f)
-{
-       return close(f->fd);
-}
diff --git a/libc/stdio/__stdio_seek.c b/libc/stdio/__stdio_seek.c
--- a/libc/stdio/__stdio_seek.c
+++ b/libc/stdio/__stdio_seek.c
@@ -1,7 +0,0 @@
-#include <unistd.h>
-#include "stdio_impl.h"
-
-off_t __stdio_seek(FILE *f, off_t off, int whence)
-{
-       return lseek(f->fd, off, whence);
-}
diff --git a/libc/stdio/fopen.c b/libc/stdio/fopen.c
--- a/libc/stdio/fopen.c
+++ b/libc/stdio/fopen.c
@@ -1,32 +0,0 @@
-#include "stdio_impl.h"
-#include <fcntl.h>
-#include <string.h>
-#include <unistd.h>
-#include <errno.h>
-
-FILE *fopen(const char *restrict filename, const char *restrict mode)
-{
-       FILE *f;
-       int fd;
-       int flags;
-
-       /* Check for valid initial mode character */
-       if (!strchr("rwa", *mode)) {
-               errno = EINVAL;
-               return 0;
-       }
-
-       /* Compute the flags to pass to open() */
-       flags = __fmodeflags(mode);
-
-       fd = open(filename, flags|O_LARGEFILE, 0666);
-       if (fd < 0) return 0;
-
-       f = __fdopen(fd, mode);
-       if (f) return f;
-
-       close(fd);
-       return 0;
-}
-
-LFS64(fopen);
diff --git a/libc/stdio/freopen.c b/libc/stdio/freopen.c
--- a/libc/stdio/freopen.c
+++ b/libc/stdio/freopen.c
@@ -1,55 +0,0 @@
-#include "stdio_impl.h"
-#include <stdlib.h>
-#include <fcntl.h>
-
-/* The basic idea of this implementation is to open a new FILE,
- * hack the necessary parts of the new FILE into the old one, then
- * close the new FILE. */
-
-/* Locking IS necessary because another thread may provably hold the
- * lock, via flockfile or otherwise, when freopen is called, and in that
- * case, freopen cannot act until the lock is released. */
-
-int dup3(int, int, int);
-
-FILE *freopen(const char *restrict filename, const char *restrict mode, FILE 
*restrict f)
-{
-       int fl = __fmodeflags(mode);
-       FILE *f2;
-
-       FLOCK(f);
-
-       fflush(f);
-
-       if (!filename) {
-               if (fl&O_CLOEXEC)
-                       fcntl(f->fd, F_SETFD, FD_CLOEXEC);
-               fl &= ~(O_CREAT|O_EXCL|O_CLOEXEC);
-               if (fcntl(f->fd, F_SETFL, fl) < 0)
-                       goto fail;
-       } else {
-               f2 = fopen(filename, mode);
-               if (!f2) goto fail;
-               if (f2->fd == f->fd) f2->fd = -1; /* avoid closing in fclose */
-               else if (dup3(f2->fd, f->fd, fl&O_CLOEXEC)<0) goto fail2;
-
-               f->flags = (f->flags & F_PERM) | f2->flags;
-               f->read = f2->read;
-               f->write = f2->write;
-               f->seek = f2->seek;
-               f->close = f2->close;
-
-               fclose(f2);
-       }
-
-       FUNLOCK(f);
-       return f;
-
-fail2:
-       fclose(f2);
-fail:
-       fclose(f);
-       return NULL;
-}
-
-LFS64(freopen);
diff --git a/libc/stdio/tmpfile.c b/libc/stdio/tmpfile.c
--- a/libc/stdio/tmpfile.c
+++ b/libc/stdio/tmpfile.c
@@ -1,27 +0,0 @@
-#include <stdio.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include "stdio_impl.h"
-
-#define MAXTRIES 100
-
-FILE *tmpfile(void)
-{
-       char buf[L_tmpnam], *s;
-       int fd;
-       FILE *f;
-       int try;
-       for (try=0; try<MAXTRIES; try++) {
-               s = tmpnam(buf);
-               if (!s) return 0;
-               fd = open(s, O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600);
-               if (fd >= 0) {
-                       f = __fdopen(fd, "w+");
-                       unlink(s);
-                       return f;
-               }
-       }
-       return 0;
-}
-
-LFS64(tmpfile);
diff --git a/libc/stdio/tmpnam.c b/libc/stdio/tmpnam.c
--- a/libc/stdio/tmpnam.c
+++ b/libc/stdio/tmpnam.c
@@ -1,30 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <unistd.h>
-#include <time.h>
-#include "libc.h"
-#include "atomic.h"
-
-#define MAXTRIES 100
-
-char *tmpnam(char *s)
-{
-       static int index;
-       static char s2[L_tmpnam];
-       struct timespec ts;
-       int try = 0;
-       unsigned n;
-
-       if (!s) s = s2;
-
-       if (access(P_tmpdir, R_OK|W_OK|X_OK) != 0)
-               return NULL;
-
-       do {
-               clock_gettime(CLOCK_REALTIME, &ts);
-               n = ts.tv_nsec ^ (uintptr_t)&s ^ (uintptr_t)s;
-               snprintf(s, L_tmpnam, "/tmp/t%x-%x", a_fetch_add(&index, 1), n);
-       } while (!access(s, F_OK) && try++<MAXTRIES);
-       return try>=MAXTRIES ? 0 : s;
-}
diff --git a/libc/syscall_to_function.h b/libc/syscall_to_function.h
--- a/libc/syscall_to_function.h
+++ b/libc/syscall_to_function.h
@@ -0,0 +1,31 @@
+#include <bits/syscall.h>
+#include <unistd.h>
+
+#define __OSV_TO_FUNCTION_SYS_open(filename, flags, perm) (open(filename, 
flags, perm))
+
+#define __OSV_TO_FUNCTION_SYS_close(fd) (close(fd))
+
+#define __OSV_TO_FUNCTION_SYS_lseek(file, off, whence) (lseek(file, off, 
whence))
+
+#define __OSV_TO_FUNCTION_SYS_fcntl(fd, cmd, ...) (fcntl(fd, cmd __VA_OPT__(,) 
__VA_ARGS__))
+
+#define __OSV_TO_FUNCTION_SYS_clock_gettime(c, t, x) (clock_gettime(c, t))
+
+#define __OSV_TO_FUNCTION_SYS_access(p, i) (access(p, i))
+
+#define __OSV_TO_FUNCTION_SYS_ioctl(fd, cmd, args) (ioctl(fd, cmd, args))
+
+#define __OSV_TO_FUNCTION_SYS_unlink(path) (unlink(path))
+
+#define __OSV_TO_FUNCTION_SYS_rmdir(path) (rmdir(path))
+
+#define __OSV_TO_FUNCTION_SYS_readv(fd, cmd, args) (readv(fd, cmd, args))
+
+#define __OSV_TO_FUNCTION_SYS_writev(fd, cmd, args) (writev(fd, cmd, args))
+
+#undef __syscall
+#define __syscall(sys_number, ...) 
(__OSV_TO_FUNCTION_##sys_number(__VA_ARGS__))
+#undef syscall
+#define syscall(sys_number, ...) (__OSV_TO_FUNCTION_##sys_number(__VA_ARGS__))
+#undef syscall_cp
+#define syscall_cp(sys_number, ...) 
(__OSV_TO_FUNCTION_##sys_number(__VA_ARGS__))

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/osv-dev/000000000000afd5e705ad476b5b%40google.com.

Reply via email to