Skip to content

Commit dadf633

Browse files
bugaevcawesomekling
authored andcommitted
Kernel: Customize absolute_path() for more file types
1 parent 1d03391 commit dadf633

File tree

7 files changed

+91
-15
lines changed

7 files changed

+91
-15
lines changed

Kernel/Net/IPv4Socket.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <AK/StringBuilder.h>
12
#include <Kernel/FileSystem/FileDescription.h>
23
#include <Kernel/Net/ARP.h>
34
#include <Kernel/Net/ICMP.h>
@@ -268,3 +269,35 @@ void IPv4Socket::did_receive(const IPv4Address& source_address, u16 source_port,
268269
kprintf("IPv4Socket(%p): did_receive %d bytes, total_received=%u, packets in queue: %d\n", this, packet_size, m_bytes_received, m_receive_queue.size_slow());
269270
#endif
270271
}
272+
273+
String IPv4Socket::absolute_path(const FileDescription&) const
274+
{
275+
if (m_role == Role::None)
276+
return "socket";
277+
278+
StringBuilder builder;
279+
builder.append("socket:");
280+
281+
builder.appendf("%s:%d", m_local_address.to_string().characters(), m_local_port);
282+
if (m_role == Role::Accepted || m_role == Role::Connected)
283+
builder.appendf(" / %s:%d", m_peer_address.to_string().characters(), m_peer_port);
284+
285+
switch (m_role) {
286+
case Role::Listener:
287+
builder.append(" (listening)");
288+
break;
289+
case Role::Accepted:
290+
builder.append(" (accepted)");
291+
break;
292+
case Role::Connected:
293+
builder.append(" (connected)");
294+
break;
295+
case Role::Connecting:
296+
builder.append(" (connecting)");
297+
break;
298+
default:
299+
ASSERT_NOT_REACHED();
300+
}
301+
302+
return builder.to_string();
303+
}

Kernel/Net/IPv4Socket.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class IPv4Socket : public Socket {
4545

4646
IPv4SocketTuple tuple() const { return IPv4SocketTuple(m_local_address, m_local_port, m_peer_address, m_peer_port); }
4747

48+
String absolute_path(const FileDescription& description) const override;
49+
4850
protected:
4951
IPv4Socket(int type, int protocol);
5052
virtual const char* class_name() const override { return "IPv4Socket"; }

Kernel/Net/LocalSocket.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,29 @@ StringView LocalSocket::socket_path() const
254254
int len = strnlen(m_address.sun_path, sizeof(m_address.sun_path));
255255
return { m_address.sun_path, len };
256256
}
257+
258+
String LocalSocket::absolute_path(const FileDescription& description) const
259+
{
260+
StringBuilder builder;
261+
builder.append("socket:");
262+
builder.append(socket_path());
263+
264+
switch (role(description)) {
265+
case Role::Listener:
266+
builder.append(" (listening)");
267+
break;
268+
case Role::Accepted:
269+
builder.appendf(" (accepted from pid %d)", origin_pid());
270+
break;
271+
case Role::Connected:
272+
builder.appendf(" (connected to pid %d)", acceptor_pid());
273+
break;
274+
case Role::Connecting:
275+
builder.append(" (connecting)");
276+
break;
277+
default:
278+
break;
279+
}
280+
281+
return builder.to_string();
282+
}

Kernel/Net/LocalSocket.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class LocalSocket final : public Socket, public InlineLinkedListNode<LocalSocket
1515
static void for_each(Function<void(LocalSocket&)>);
1616

1717
StringView socket_path() const;
18+
String absolute_path(const FileDescription& description) const override;
19+
1820
// ^Socket
1921
virtual KResult bind(const sockaddr*, socklen_t) override;
2022
virtual KResult connect(FileDescription&, const sockaddr*, socklen_t, ShouldBlock = ShouldBlock::Yes) override;

Kernel/Net/Socket.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <AK/StringBuilder.h>
12
#include <Kernel/FileSystem/FileDescription.h>
23
#include <Kernel/Net/IPv4Socket.h>
34
#include <Kernel/Net/LocalSocket.h>
@@ -149,23 +150,28 @@ void Socket::load_send_deadline()
149150
m_send_deadline.tv_usec %= 1000000;
150151
}
151152

152-
static const char* to_string(Socket::Role role)
153-
{
154-
switch (role) {
155-
case Socket::Role::Listener:
156-
return "Listener";
157-
case Socket::Role::Accepted:
158-
return "Accepted";
159-
case Socket::Role::Connected:
160-
return "Connected";
161-
default:
162-
return "None";
163-
}
164-
}
165-
166153
String Socket::absolute_path(const FileDescription& description) const
167154
{
168-
return String::format("socket:%x (role: %s)", this, ::to_string(role(description)));
155+
StringBuilder builder;
156+
builder.appendf("socket:%x", this);
157+
158+
switch (role(description)) {
159+
case Role::None:
160+
break;
161+
case Role::Listener:
162+
builder.append(" (listening)");
163+
break;
164+
case Role::Accepted:
165+
builder.append(" (accepted)");
166+
break;
167+
case Role::Connected:
168+
builder.append(" (connected)");
169+
break;
170+
case Role::Connecting:
171+
builder.append(" (connecting)");
172+
break;
173+
}
174+
return builder.to_string();
169175
}
170176

171177
ssize_t Socket::read(FileDescription& description, u8* buffer, ssize_t size)

Kernel/TTY/MasterPTY.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,8 @@ int MasterPTY::ioctl(FileDescription& description, unsigned request, unsigned ar
102102
return m_slave->ioctl(description, request, arg);
103103
return -EINVAL;
104104
}
105+
106+
String MasterPTY::absolute_path(const FileDescription&) const
107+
{
108+
return String::format("ptm:%s", m_pts_name.characters());
109+
}

Kernel/TTY/MasterPTY.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class MasterPTY final : public CharacterDevice {
1818
void notify_slave_closed(Badge<SlavePTY>);
1919
bool is_closed() const { return m_closed; }
2020

21+
virtual String absolute_path(const FileDescription&) const override;
22+
2123
private:
2224
// ^CharacterDevice
2325
virtual ssize_t read(FileDescription&, u8*, ssize_t) override;

0 commit comments

Comments
 (0)