1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 00:07:35 +00:00

Kernel: Turn Thread::current and Process::current into functions

This allows us to query the current thread and process on a
per processor basis
This commit is contained in:
Tom 2020-06-28 15:34:31 -06:00 committed by Andreas Kling
parent cdc78515b6
commit 16783bd14d
39 changed files with 518 additions and 369 deletions

View file

@ -416,7 +416,7 @@ void E1000NetworkAdapter::send_raw(const u8* data, size_t length)
sti();
break;
}
Thread::current->wait_on(m_wait_queue);
Thread::current()->wait_on(m_wait_queue);
}
#ifdef E1000_DEBUG
klog() << "E1000: Sent packet, status is now " << String::format("%b", descriptor.status) << "!";

View file

@ -111,9 +111,9 @@ KResult IPv4Socket::bind(const sockaddr* user_address, socklen_t address_size)
return KResult(-EINVAL);
auto requested_local_port = ntohs(address.sin_port);
if (!Process::current->is_superuser()) {
if (!Process::current()->is_superuser()) {
if (requested_local_port < 1024) {
dbg() << "UID " << Process::current->uid() << " attempted to bind " << class_name() << " to port " << requested_local_port;
dbg() << "UID " << Process::current()->uid() << " attempted to bind " << class_name() << " to port " << requested_local_port;
return KResult(-EACCES);
}
}
@ -232,7 +232,7 @@ ssize_t IPv4Socket::sendto(FileDescription&, const void* data, size_t data_lengt
int nsent = protocol_send(data, data_length);
if (nsent > 0)
Thread::current->did_ipv4_socket_write(nsent);
Thread::current()->did_ipv4_socket_write(nsent);
return nsent;
}
@ -246,7 +246,7 @@ ssize_t IPv4Socket::receive_byte_buffered(FileDescription& description, void* bu
return -EAGAIN;
locker.unlock();
auto res = Thread::current->block<Thread::ReadBlocker>(description);
auto res = Thread::current()->block<Thread::ReadBlocker>(description);
locker.lock();
if (!m_can_read) {
@ -261,7 +261,7 @@ ssize_t IPv4Socket::receive_byte_buffered(FileDescription& description, void* bu
ASSERT(!m_receive_buffer.is_empty());
int nreceived = m_receive_buffer.read((u8*)buffer, buffer_length);
if (nreceived > 0)
Thread::current->did_ipv4_socket_read((size_t)nreceived);
Thread::current()->did_ipv4_socket_read((size_t)nreceived);
m_can_read = !m_receive_buffer.is_empty();
return nreceived;
@ -296,7 +296,7 @@ ssize_t IPv4Socket::receive_packet_buffered(FileDescription& description, void*
}
locker.unlock();
auto res = Thread::current->block<Thread::ReadBlocker>(description);
auto res = Thread::current()->block<Thread::ReadBlocker>(description);
locker.lock();
if (!m_can_read) {
@ -354,7 +354,7 @@ ssize_t IPv4Socket::recvfrom(FileDescription& description, void* buffer, size_t
nreceived = receive_packet_buffered(description, buffer, buffer_length, flags, addr, addr_length);
if (nreceived > 0)
Thread::current->did_ipv4_socket_read(nreceived);
Thread::current()->did_ipv4_socket_read(nreceived);
return nreceived;
}
@ -468,7 +468,7 @@ int IPv4Socket::ioctl(FileDescription&, unsigned request, FlatPtr arg)
auto ioctl_route = [request, arg]() {
auto* route = (rtentry*)arg;
if (!Process::current->validate_read_typed(route))
if (!Process::current()->validate_read_typed(route))
return -EFAULT;
char namebuf[IFNAMSIZ + 1];
@ -481,7 +481,7 @@ int IPv4Socket::ioctl(FileDescription&, unsigned request, FlatPtr arg)
switch (request) {
case SIOCADDRT:
if (!Process::current->is_superuser())
if (!Process::current()->is_superuser())
return -EPERM;
if (route->rt_gateway.sa_family != AF_INET)
return -EAFNOSUPPORT;
@ -500,7 +500,7 @@ int IPv4Socket::ioctl(FileDescription&, unsigned request, FlatPtr arg)
auto ioctl_interface = [request, arg]() {
auto* ifr = (ifreq*)arg;
if (!Process::current->validate_read_typed(ifr))
if (!Process::current()->validate_read_typed(ifr))
return -EFAULT;
char namebuf[IFNAMSIZ + 1];
@ -513,7 +513,7 @@ int IPv4Socket::ioctl(FileDescription&, unsigned request, FlatPtr arg)
switch (request) {
case SIOCSIFADDR:
if (!Process::current->is_superuser())
if (!Process::current()->is_superuser())
return -EPERM;
if (ifr->ifr_addr.sa_family != AF_INET)
return -EAFNOSUPPORT;
@ -521,7 +521,7 @@ int IPv4Socket::ioctl(FileDescription&, unsigned request, FlatPtr arg)
return 0;
case SIOCSIFNETMASK:
if (!Process::current->is_superuser())
if (!Process::current()->is_superuser())
return -EPERM;
if (ifr->ifr_addr.sa_family != AF_INET)
return -EAFNOSUPPORT;
@ -529,14 +529,14 @@ int IPv4Socket::ioctl(FileDescription&, unsigned request, FlatPtr arg)
return 0;
case SIOCGIFADDR:
if (!Process::current->validate_write_typed(ifr))
if (!Process::current()->validate_write_typed(ifr))
return -EFAULT;
ifr->ifr_addr.sa_family = AF_INET;
((sockaddr_in&)ifr->ifr_addr).sin_addr.s_addr = adapter->ipv4_address().to_u32();
return 0;
case SIOCGIFHWADDR:
if (!Process::current->validate_write_typed(ifr))
if (!Process::current()->validate_write_typed(ifr))
return -EFAULT;
ifr->ifr_hwaddr.sa_family = AF_INET;
{

View file

@ -63,8 +63,9 @@ LocalSocket::LocalSocket(int type)
LOCKER(all_sockets().lock());
all_sockets().resource().append(this);
m_prebind_uid = Process::current->uid();
m_prebind_gid = Process::current->gid();
auto current_process = Process::current();
m_prebind_uid = current_process->uid();
m_prebind_gid = current_process->gid();
m_prebind_mode = 0666;
#ifdef DEBUG_LOCAL_SOCKET
@ -110,7 +111,7 @@ KResult LocalSocket::bind(const sockaddr* user_address, socklen_t address_size)
mode_t mode = S_IFSOCK | (m_prebind_mode & 04777);
UidAndGid owner { m_prebind_uid, m_prebind_gid };
auto result = VFS::the().open(path, O_CREAT | O_EXCL | O_NOFOLLOW_NOERROR, mode, Process::current->current_directory(), owner);
auto result = VFS::the().open(path, O_CREAT | O_EXCL | O_NOFOLLOW_NOERROR, mode, Process::current()->current_directory(), owner);
if (result.is_error()) {
if (result.error() == -EEXIST)
return KResult(-EADDRINUSE);
@ -148,7 +149,7 @@ KResult LocalSocket::connect(FileDescription& description, const sockaddr* addre
dbg() << "LocalSocket{" << this << "} connect(" << safe_address << ")";
#endif
auto description_or_error = VFS::the().open(safe_address, O_RDWR, 0, Process::current->current_directory());
auto description_or_error = VFS::the().open(safe_address, O_RDWR, 0, Process::current()->current_directory());
if (description_or_error.is_error())
return KResult(-ECONNREFUSED);
@ -175,7 +176,7 @@ KResult LocalSocket::connect(FileDescription& description, const sockaddr* addre
return KSuccess;
}
if (Thread::current->block<Thread::ConnectBlocker>(description) != Thread::BlockResult::WokeNormally) {
if (Thread::current()->block<Thread::ConnectBlocker>(description) != Thread::BlockResult::WokeNormally) {
m_connect_side_role = Role::None;
return KResult(-EINTR);
}
@ -265,7 +266,7 @@ ssize_t LocalSocket::sendto(FileDescription& description, const void* data, size
return -EPIPE;
ssize_t nwritten = send_buffer_for(description).write((const u8*)data, data_size);
if (nwritten > 0)
Thread::current->did_unix_socket_write(nwritten);
Thread::current()->did_unix_socket_write(nwritten);
return nwritten;
}
@ -299,7 +300,7 @@ ssize_t LocalSocket::recvfrom(FileDescription& description, void* buffer, size_t
return -EAGAIN;
}
} else if (!can_read(description, 0)) {
auto result = Thread::current->block<Thread::ReadBlocker>(description);
auto result = Thread::current()->block<Thread::ReadBlocker>(description);
if (result != Thread::BlockResult::WokeNormally)
return -EINTR;
}
@ -308,7 +309,7 @@ ssize_t LocalSocket::recvfrom(FileDescription& description, void* buffer, size_t
ASSERT(!buffer_for_me.is_empty());
int nread = buffer_for_me.read((u8*)buffer, buffer_size);
if (nread > 0)
Thread::current->did_unix_socket_read(nread);
Thread::current()->did_unix_socket_read(nread);
return nread;
}
@ -389,7 +390,8 @@ KResult LocalSocket::chown(FileDescription&, uid_t uid, gid_t gid)
if (m_file)
return m_file->chown(uid, gid);
if (!Process::current->is_superuser() && (Process::current->euid() != uid || !Process::current->in_group(gid)))
auto current_process = Process::current();
if (!current_process->is_superuser() && (current_process->euid() != uid || !current_process->in_group(gid)))
return KResult(-EPERM);
m_prebind_uid = uid;

View file

@ -113,7 +113,7 @@ void NetworkTask_main()
for (;;) {
size_t packet_size = dequeue_packet(buffer, buffer_size);
if (!packet_size) {
Thread::current->wait_on(packet_wait_queue);
Thread::current()->wait_on(packet_wait_queue);
continue;
}
if (packet_size < sizeof(EthernetFrameHeader)) {

View file

@ -135,7 +135,7 @@ RoutingDecision route_to(const IPv4Address& target, const IPv4Address& source, c
request.set_sender_protocol_address(adapter->ipv4_address());
adapter->send({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, request);
(void)Thread::current->block_until("Routing (ARP)", [next_hop_ip] {
(void)Thread::current()->block_until("Routing (ARP)", [next_hop_ip] {
return arp_table().resource().get(next_hop_ip).has_value();
});

View file

@ -55,7 +55,7 @@ Socket::Socket(int domain, int type, int protocol)
, m_type(type)
, m_protocol(protocol)
{
auto& process = *Process::current;
auto& process = *Process::current();
m_origin = { process.pid(), process.uid(), process.gid() };
}
@ -82,7 +82,7 @@ RefPtr<Socket> Socket::accept()
#endif
auto client = m_pending.take_first();
ASSERT(!client->is_connected());
auto& process = *Process::current;
auto& process = *Process::current();
client->m_acceptor = { process.pid(), process.uid(), process.gid() };
client->m_connected = true;
client->m_role = Role::Accepted;

View file

@ -372,7 +372,7 @@ KResult TCPSocket::protocol_connect(FileDescription& description, ShouldBlock sh
m_direction = Direction::Outgoing;
if (should_block == ShouldBlock::Yes) {
if (Thread::current->block<Thread::ConnectBlocker>(description) != Thread::BlockResult::WokeNormally)
if (Thread::current()->block<Thread::ConnectBlocker>(description) != Thread::BlockResult::WokeNormally)
return KResult(-EINTR);
ASSERT(setup_state() == SetupState::Completed);
if (has_error()) {