|
1 |
| -From 3f789791be46cefe9ff8a5c6196a3a23bed9620f Mon Sep 17 00:00:00 2001 |
2 |
| -From: Andy-Python-Programmer < [email protected]> |
3 |
| -Date: Sun, 16 Apr 2023 14:21:09 +1000 |
4 |
| -Subject: [PATCH] <xxx> |
| 1 | +From 1e4603c9de5ce2dcb596dfb95254b0ae119d1036 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Anhad Singh < [email protected]> |
| 3 | +Date: Thu, 3 Aug 2023 16:36:12 +1000 |
| 4 | +Subject: [PATCH] socket: implement shutdown(2) |
5 | 5 |
|
6 |
| -Signed-off-by: Andy-Python-Programmer < [email protected]> |
| 6 | +Signed-off-by: Anhad Singh < [email protected]> |
7 | 7 | ---
|
8 |
| - .gitignore | 2 ++ |
9 |
| - options/ansi/generic/stdlib-stubs.cpp | 6 +++-- |
10 |
| - options/glibc/generic/execinfo.cpp | 5 ++-- |
11 |
| - options/rtdl/generic/linker.cpp | 2 +- |
12 |
| - sysdeps/aero/generic/aero.cpp | 27 +++++++++++++++------- |
13 |
| - sysdeps/aero/generic/filesystem.cpp | 33 ++++++++++++++------------- |
14 |
| - sysdeps/aero/generic/sockets.cpp | 29 +++++++++++++++++++++-- |
15 |
| - sysdeps/aero/include/aero/syscall.h | 10 ++++++++ |
16 |
| - 8 files changed, 83 insertions(+), 31 deletions(-) |
| 8 | + options/glibc/generic/execinfo.cpp | 5 ++-- |
| 9 | + options/posix/generic/sys-socket-stubs.cpp | 9 ++++++-- |
| 10 | + options/posix/include/mlibc/posix-sysdeps.hpp | 1 + |
| 11 | + sysdeps/aero/generic/signals.cpp | 19 ++++++--------- |
| 12 | + sysdeps/aero/generic/sockets.cpp | 23 +++++++++++++++++++ |
| 13 | + sysdeps/aero/include/aero/syscall.h | 3 +++ |
| 14 | + 6 files changed, 44 insertions(+), 16 deletions(-) |
17 | 15 |
|
18 |
| -diff --git a/.gitignore b/.gitignore |
19 |
| -index fdd60a0..9f811f4 100644 |
20 |
| ---- a/.gitignore |
21 |
| -+++ b/.gitignore |
22 |
| -@@ -3,3 +3,5 @@ subprojects |
23 |
| - *.xbstrap |
24 |
| - # editor configs: |
25 |
| - .vscode |
26 |
| -+# clangd cache files: |
27 |
| -+.cache |
28 | 16 | diff --git a/options/glibc/generic/execinfo.cpp b/options/glibc/generic/execinfo.cpp
|
29 |
| -index 3474615..10a2109 100644 |
| 17 | +index 3474615..aaf593a 100644 |
30 | 18 | --- a/options/glibc/generic/execinfo.cpp
|
31 | 19 | +++ b/options/glibc/generic/execinfo.cpp
|
32 | 20 | @@ -1,9 +1,10 @@
|
33 | 21 | #include <execinfo.h>
|
34 | 22 | #include <bits/ensure.h>
|
35 |
| -+#include <mlibc/debug.hpp> |
| 23 | ++#include <mlibc/charset.hpp> |
36 | 24 |
|
37 | 25 | int backtrace(void **, int) {
|
38 | 26 | - __ensure(!"Not implemented");
|
39 | 27 | - __builtin_unreachable();
|
40 |
| -+ mlibc::infoLogger() << "backtrace() is unimplemented" << frg::endlog; |
| 28 | ++ mlibc::infoLogger() << "backtrace: Not implemented" << frg::endlog; |
41 | 29 | + return 0;
|
42 | 30 | }
|
43 | 31 |
|
44 | 32 | char **backtrace_symbols(void *const *, int) {
|
45 |
| -diff --git a/options/rtdl/generic/linker.cpp b/options/rtdl/generic/linker.cpp |
46 |
| -index 003a162..ab01a7a 100644 |
47 |
| ---- a/options/rtdl/generic/linker.cpp |
48 |
| -+++ b/options/rtdl/generic/linker.cpp |
49 |
| -@@ -18,7 +18,7 @@ uintptr_t libraryBase = 0x41000000; |
50 |
| - |
51 |
| - constexpr bool verbose = false; |
52 |
| - constexpr bool stillSlightlyVerbose = false; |
53 |
| --constexpr bool logBaseAddresses = false; |
54 |
| -+constexpr bool logBaseAddresses = true; |
55 |
| - constexpr bool logRpath = false; |
56 |
| - constexpr bool eagerBinding = true; |
| 33 | +diff --git a/options/posix/generic/sys-socket-stubs.cpp b/options/posix/generic/sys-socket-stubs.cpp |
| 34 | +index 1674945..037a994 100644 |
| 35 | +--- a/options/posix/generic/sys-socket-stubs.cpp |
| 36 | ++++ b/options/posix/generic/sys-socket-stubs.cpp |
| 37 | +@@ -187,8 +187,13 @@ int setsockopt(int fd, int layer, int number, |
| 38 | + return mlibc::sys_setsockopt(fd, layer, number, buffer, size); |
| 39 | + } |
| 40 | + |
| 41 | +-int shutdown(int, int) { |
| 42 | +- mlibc::infoLogger() << "mlibc: shutdown() is a no-op!" << frg::endlog; |
| 43 | ++int shutdown(int sockfd, int how) { |
| 44 | ++ auto sysdep = MLIBC_CHECK_OR_ENOSYS(mlibc::sys_shutdown, -1); |
| 45 | ++ if(int e = sysdep(sockfd, how); e) { |
| 46 | ++ errno = e; |
| 47 | ++ return -1; |
| 48 | ++ } |
| 49 | ++ |
| 50 | + return 0; |
| 51 | + } |
| 52 | + |
| 53 | +diff --git a/options/posix/include/mlibc/posix-sysdeps.hpp b/options/posix/include/mlibc/posix-sysdeps.hpp |
| 54 | +index 9918188..c694079 100644 |
| 55 | +--- a/options/posix/include/mlibc/posix-sysdeps.hpp |
| 56 | ++++ b/options/posix/include/mlibc/posix-sysdeps.hpp |
| 57 | +@@ -159,6 +159,7 @@ int sys_vm_unmap(void *pointer, size_t size); |
| 58 | + void *__restrict buffer, socklen_t *__restrict size); |
| 59 | + [[gnu::weak]] int sys_setsockopt(int fd, int layer, int number, |
| 60 | + const void *buffer, socklen_t size); |
| 61 | ++[[gnu::weak]] int sys_shutdown(int sockfd, int how); |
| 62 | + [[gnu::weak]] int sys_sigprocmask(int how, const sigset_t *__restrict set, |
| 63 | + sigset_t *__restrict retrieve); |
| 64 | + [[gnu::weak]] int sys_sigaction(int, const struct sigaction *__restrict, |
| 65 | +diff --git a/sysdeps/aero/generic/signals.cpp b/sysdeps/aero/generic/signals.cpp |
| 66 | +index a6f69ff..611db69 100644 |
| 67 | +--- a/sysdeps/aero/generic/signals.cpp |
| 68 | ++++ b/sysdeps/aero/generic/signals.cpp |
| 69 | +@@ -29,25 +29,20 @@ int sys_sigaction(int how, const struct sigaction *__restrict action, |
| 70 | + #endif |
| 71 | + |
| 72 | + auto sigreturn = (sc_word_t)__mlibc_signal_restore; |
| 73 | +- |
| 74 | +- auto res = syscall(SYS_SIGACTION, how, (sc_word_t)action, sigreturn, |
| 75 | ++ auto ret = syscall(SYS_SIGACTION, how, (sc_word_t)action, sigreturn, |
| 76 | + (sc_word_t)old_action); |
| 77 | + |
| 78 | +- if (res < 0) { |
| 79 | +- return -res; |
| 80 | +- } |
| 81 | +- |
| 82 | ++ if(int e = sc_error(ret); e) |
| 83 | ++ return e; |
| 84 | + return 0; |
| 85 | + } |
| 86 | + |
| 87 | + int sys_sigprocmask(int how, const sigset_t *__restrict set, |
| 88 | + sigset_t *__restrict retrieve) { |
| 89 | +- auto result = syscall(SYS_SIGPROCMASK, how, set, retrieve); |
| 90 | + |
| 91 | +- if (result < 0) { |
| 92 | +- return -result; |
| 93 | +- } |
| 94 | +- |
| 95 | +- return 0; |
| 96 | ++ auto ret = syscall(SYS_SIGPROCMASK, how, set, retrieve); |
| 97 | ++ if(int e = sc_error(ret); e) |
| 98 | ++ return e; |
| 99 | ++ return 0; |
| 100 | + } |
| 101 | + } // namespace mlibc |
| 102 | +\ No newline at end of file |
| 103 | +diff --git a/sysdeps/aero/generic/sockets.cpp b/sysdeps/aero/generic/sockets.cpp |
| 104 | +index 2db218c..ed3fc06 100644 |
| 105 | +--- a/sysdeps/aero/generic/sockets.cpp |
| 106 | ++++ b/sysdeps/aero/generic/sockets.cpp |
| 107 | +@@ -221,6 +221,29 @@ int sys_setsockopt(int fd, int layer, int number, const void *buffer, |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | ++int sys_shutdown(int sockfd, int how) { |
| 112 | ++ auto ret = syscall(SYS_SOCK_SHUTDOWN, sockfd, how); |
| 113 | ++ if(int e = sc_error(ret); e) |
| 114 | ++ return e; |
| 115 | ++ return 0; |
| 116 | ++} |
| 117 | ++ |
| 118 | ++int sys_peername(int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length, socklen_t *actual_length) { |
| 119 | ++ auto ret = syscall(SYS_GETPEERNAME, fd, addr_ptr, &max_addr_length); |
| 120 | ++ if (int e = sc_error(ret); e) |
| 121 | ++ return e; |
| 122 | ++ *actual_length = max_addr_length; |
| 123 | ++ return 0; |
| 124 | ++} |
| 125 | ++ |
| 126 | ++int sys_sockname(int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length, socklen_t *actual_length) { |
| 127 | ++ auto ret = syscall(SYS_GETSOCKNAME, fd, addr_ptr, &max_addr_length); |
| 128 | ++ if (int e = sc_error(ret); e) |
| 129 | ++ return e; |
| 130 | ++ *actual_length = max_addr_length; |
| 131 | ++ return 0; |
| 132 | ++} |
| 133 | ++ |
| 134 | + int sys_if_nametoindex(const char *name, unsigned int *ret) { |
| 135 | + int fd = 0; |
| 136 | + int r = sys_socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, AF_UNSPEC, &fd); |
| 137 | +diff --git a/sysdeps/aero/include/aero/syscall.h b/sysdeps/aero/include/aero/syscall.h |
| 138 | +index d8bf99e..39c5b65 100644 |
| 139 | +--- a/sysdeps/aero/include/aero/syscall.h |
| 140 | ++++ b/sysdeps/aero/include/aero/syscall.h |
| 141 | +@@ -79,6 +79,9 @@ |
| 142 | + #define SYS_SETPGID 72 |
| 143 | + #define SYS_SETSID 73 |
| 144 | + #define SYS_GETPGID 74 |
| 145 | ++#define SYS_SOCK_SHUTDOWN 75 |
| 146 | ++#define SYS_GETPEERNAME 76 |
| 147 | ++#define SYS_GETSOCKNAME 77 |
| 148 | + |
| 149 | + // Invalid syscall used to trigger a log error in the kernel (as a hint) |
| 150 | + // so, that we can implement the syscall in the kernel. |
| 151 | +-- |
| 152 | +2.42.0 |
57 | 153 |
|
0 commit comments