summaryrefslogtreecommitdiff
path: root/Kernel/LocalSocket.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Kernel/LocalSocket.cpp')
-rw-r--r--Kernel/LocalSocket.cpp28
1 files changed, 28 insertions, 0 deletions
diff --git a/Kernel/LocalSocket.cpp b/Kernel/LocalSocket.cpp
index 1fbf633223..fc6ea87b3b 100644
--- a/Kernel/LocalSocket.cpp
+++ b/Kernel/LocalSocket.cpp
@@ -1,6 +1,8 @@
#include <Kernel/LocalSocket.h>
#include <Kernel/UnixTypes.h>
#include <Kernel/Process.h>
+#include <Kernel/VirtualFileSystem.h>
+#include <LibC/errno_numbers.h>
RetainPtr<LocalSocket> LocalSocket::create(int type)
{
@@ -17,3 +19,29 @@ LocalSocket::~LocalSocket()
{
}
+bool LocalSocket::bind(const sockaddr* address, socklen_t address_size, int& error)
+{
+ if (address_size != sizeof(sockaddr_un)) {
+ error = -EINVAL;
+ return false;
+ }
+
+ if (address->sa_family != AF_LOCAL) {
+ error = -EINVAL;
+ return false;
+ }
+
+ const sockaddr_un& local_address = *reinterpret_cast<const sockaddr_un*>(address);
+ char safe_address[sizeof(local_address.sun_path) + 1];
+ memcpy(safe_address, local_address.sun_path, sizeof(local_address.sun_path));
+
+ kprintf("%s(%u) LocalSocket{%p} bind(%s)\n", current->name().characters(), current->pid(), safe_address);
+
+ auto descriptor = VFS::the().open(safe_address, error, O_CREAT | O_EXCL, S_IFSOCK | 0666, *current->cwd_inode());
+ if (!descriptor) {
+ if (error == -EEXIST)
+ error = -EADDRINUSE;
+ return error;
+ }
+ return true;
+}