Commit Graph

279 Commits

Author SHA1 Message Date
Sergey Bugaev
e0e1be8c33 LibCore: Fix a crash in CArgsParser 2019-08-17 12:07:55 +02:00
Andreas Kling
1febd59f83 LibCore+rpcdump: Publish CObject graph to on-demand RPC socket
All programs that have a CEventLoop now allow local socket connections
via /tmp/rpc.PID and will dump a serialized JSON array of all the live
CObjects in the program onto connecting sockets.

Also added a small /bin/rpcdump tool that connects to an RPC socket and
produces a raw dump of the JSON that comes out.
2019-08-17 11:39:26 +02:00
Andreas Kling
2fa2d72761 CObject: Put all CObjects on a global IntrusiveList for introspection
This will allow us to easily traverse the entire CObject graph.
2019-08-17 11:26:19 +02:00
Andreas Kling
7127c4fdbb LibCore: CIODevice::set_error() is meant to be called with the 'errno'
The point of this function is to stash away the innermost error code
so that we don't lose it by the time we get back to the client code.
2019-08-17 11:07:15 +02:00
Andreas Kling
ee83b1bcf4 LibCore: Use URL in CHttpRequest
Now there's just CHttpRequest::set_url(URL), no need to specify the
host, port and path manually anymore.

Updated ChanViewer and Downloader for the API change.
2019-08-10 19:32:03 +02:00
Conrad Pankoff
da615e46cd LibCore: Initialise m_port as zero in CSocketAddress 2019-08-06 15:06:20 +02:00
Sergey Bugaev
211b51dab7 CEventLoop: Create the wake pipe with O_CLOEXEC
This ensures the pipe fds don't leak into child processes.
This manifested as the Shell (and all processes started
from the shell) having two mysterious FIFOs open. This
was happening because of the Terminal, which the shell
was spawned form, leaking its CEventLoop wake pipe fds.
2019-08-05 16:04:31 +02:00
Conrad Pankoff
ed66f1d6d4 LibCore: Add CTCPServer
This is pretty much a find/replace copy of CLocalServer, and some
modifications to CTCPSocket and CSocketAddress to support it.
2019-08-05 12:53:07 +02:00
Andreas Kling
54ed6a888d LibCore: Move CHttpJob and CNetworkJob output to the right places
Errors go to stderr, debug output goes to the debug console. :^)
2019-08-04 22:10:54 +02:00
Andreas Kling
948c2657d6 CSocket: Close the underlying socket on destruction
Otherwise we leak the file descriptor.
2019-08-04 22:02:25 +02:00
Andreas Kling
b5aac9c44b CHttpJob: Drive response download via on_ready_read instead of blocking
This allows the event loop to service events while an HTTP download is
happening. Pretty cool :^)
2019-08-04 18:59:06 +02:00
Andreas Kling
a8740f82eb CHttpJob: Collect the payload as a list of buffers during download
Instead of growing the payload buffer as-we-go. This avoids wasting a
ton of time on buffer reallocation for non-trivial payloads.
2019-08-04 14:54:25 +02:00
Andreas Kling
273d9d6cf5 CHttpJob: If no "Content-Length" header was received, read until EOF
Instead of aborting after receiving the first chunk, we have to keep
reading until EOF.
2019-08-04 09:26:08 +02:00
Andreas Kling
8e684f0959 AudioServer: Port to the new generated IPC mechanism
Fork the IPC Connection classes into Server:: and Client::ConnectionNG.
The new IPC messages are serialized very snugly instead of using the
same generic data structure for all messages.

Remove ASAPI.h since we now generate all of it from AudioServer.ipc :^)
2019-08-03 19:49:19 +02:00
Andreas Kling
7f25959fa2 LibCore: Make get_current_user_home_path() return String & close passwd
This API was returning a "const char*" and it was unclear who took care
of the underlying memory. Returning a String makes that obvious.

Also make sure we close the /etc/passwd file when we're done with it.
2019-08-03 08:32:07 +02:00
Andreas Kling
3f91d2d0cc CEventLoop: Devirtualize take_pending_events_from(CEventLoop) 2019-08-02 13:58:55 +02:00
Andreas Kling
ab8891c064 LibCore: Initialize pid/id variables in CoreIPC{Client,Server}
Also rename CoreIPCServer::m_pid to m_client_pid for clarification.

Found by PVS-Studio.
2019-08-01 11:42:30 +02:00
Andreas Kling
caeb4b7a7e CEventLoop: Add a missing initializer to EventLoopTimer. 2019-08-01 10:49:31 +02:00
Andreas Kling
385e9268f4 CIODevice: printf() thought it was calling ::write() but it was write()
There's some confusion between the write syscall and CIODevice::write()
here. The internal write() returns a boolean, and has already whined
in case the syscall failed, so we don't need to do that again.
2019-08-01 10:41:04 +02:00
Andreas Kling
0f3d191a70 LibCore: Rename CFileStreamReader => CIODeviceStreamReader. 2019-07-30 15:16:39 +02:00
Andreas Kling
e6db1b81b8 AudioServer: Begin work on a new IPC API style.
The goal here is to generate most of this code from IPC protocol
descriptions, but for now I've spelled them all out to get started.

Each message gets a wrapper class in the ASAPI_Client or ASAPI_Server
namespace. They are convertible to and from the old message structs.

The real hotness happens when you want to make a synchronous request
to the other side:

    auto response = send_sync<ASAPI_Client::GetMainMixVolume>();

Each request class knows his corresponding response class, so in the
above example, "response" will be an ASAPI_Server::DidGetMainMixVolume
object, and we can get the volume like so:

    int volume = response.volume();

For posting messages that don't expect a response, you can still use
post_message() since the message classes are convertible:

    post_message(ASAPI_Server::DidGetMainMixVolume(volume));

It's not perfect yet, but I already really like it. :^)
2019-07-29 22:39:20 +02:00
Andreas Kling
5ded77df39 Kernel+ProcessManager: Let processes have an icon and show it in the table.
Processes can now have an icon assigned, which is essentially a 16x16 RGBA32
bitmap exposed as a shared buffer ID.

You set the icon ID by calling set_process_icon(int) and the icon ID will be
exposed through /proc/all.

To make this work, I added a mechanism for making shared buffers globally
accessible. For safety reasons, each app seals the icon buffer before making
it global.

Right now the first call to GWindow::set_icon() is what determines the
process icon. We'll probably change this in the future. :^)
2019-07-29 07:26:01 +02:00
Andreas Kling
a292d8cd5a LibCore: Add CFileStreamReader, a simple streaming CFile reader.
This is extremely barebones right now, but can be used to easily read binary
data from a CFile piece by piece.
2019-07-27 16:38:44 +02:00
Andreas Kling
9ed7f4576b CIODevice: Try to preallocate the exact needed buffer size in read_all().
If we can get the exact file size from fstat(), it's a very good idea to use
it since it means we avoid eleventy thousand reallocations.
2019-07-27 14:24:19 +02:00
Andreas Kling
e7957db173 LibCore: Remove CSocket's bind() and listen().
We're going to be using dedicated server socket classes instead.
This was only implemented for CLocalSocket, and clients have been switched
over to using CLocalServer.
2019-07-27 10:58:21 +02:00
Andreas Kling
fe45f5a6d2 LibCore: Port CoreIPCServer to using CLocalServer.
Use CLocalServer to listen for connections in WindowServer and AudioServer.
This allows us to accept incoming CLocalSocket objects from the CLocalServer
and construct client connections based on those.

Removed COpenedSocket since it's replaced by CLocalSocket.
2019-07-27 10:53:50 +02:00
Andreas Kling
c289e49ee5 LibCore: Use clang-format on CoreIPC{Client,Server}. 2019-07-27 10:50:26 +02:00
Andreas Kling
82446ea701 CSocket: Add an on_ready_to_read callback.
This callback uses a CNotifier internally and will fire whenever there's
something to be read from the socket.
2019-07-27 10:48:43 +02:00
Andreas Kling
8f4fba95c0 CIODevice: Add a virtual did_update_fd() no notify subclasses of fd change.
This will allow subclasses to react when the file descriptor changes.
2019-07-27 10:47:46 +02:00
Andreas Kling
b3fe9cde52 LibCore: Add CLocalServer, a server-only local socket class.
Instead of trying to support both client and server in CLocalSocket, let's
have a specialized server class.

The basic usage is:

    CLocalServer server;
    server.listen("/tmp/name-of-portal");
    server.on_ready_to_accept = [&] {
        CLocalSocket* client = server.accept();
        ...
    };

This will make things a lot simpler, since an accepting socket doesn't need
half of the stuff that a regular CIODevice provides. :^)
2019-07-27 10:32:40 +02:00
Andreas Kling
10c35d345a LibCore: Add comment about child events for partially-constructed children.
Since ChildAdded events originate from the CObject constructor, they are not
fully constructed when their parent learns that they were added.
Added a little comment about this to the child_event() declaration.
2019-07-27 09:31:46 +02:00
Andreas Kling
be2b585ca6 LibCore: Add CSocket::bind() (virtual) and CSocket::listen().
These will be useful for implementing server sockets.
2019-07-26 22:39:16 +02:00
Andreas Kling
7da5a04131 CEventLoop: Convert dbgprintf() to dbg(). 2019-07-26 16:00:56 +02:00
Andreas Kling
a599317624 LibCore: Introduce a C_OBJECT macro.
This macro goes at the top of every CObject-derived class like so:

class SomeClass : public CObject {
    C_OBJECT(SomeClass)
public:
    ...

At the moment, all it does is create an override for the class_name() getter
but in the future this will be used to automatically insert member functions
into these classes.
2019-07-25 19:49:28 +02:00
Andreas Kling
d21a4f7518 CEventLoop: Don't re-process already processed events when un-nesting.
If we had already processed a couple of queued events by the time we were
told to un-nest the event loop, we'd put the entire current batch at the
head of the outer queue. This meant that we might end up trying to process
the same events multiple times.

Let's not do that. :^)
2019-07-25 16:12:51 +02:00
Andreas Kling
f46a1377ac CSocket: Add missing <sys/un.h> include to fix host build. 2019-07-25 11:49:08 +02:00
Andreas Kling
16bcaf08a3 CSocket: Fix Clang warning about unused private member m_type. 2019-07-25 11:48:21 +02:00
Andreas Kling
e940d4b07b LibCore: Only build CThread on Serenity platforms. 2019-07-25 11:47:53 +02:00
Andreas Kling
1d0b464618 AK: Make HashMap::get(Key) return an Optional<Value>.
This allows HashMap::get() to be used for value types that cannot be default
constructed (e.g NonnullOwnPtr.)
2019-07-24 10:25:43 +02:00
Andreas Kling
93489fbc4c Convert HashMap<Key, OwnPtr<T>> to HashMap<Key, NonnullOwnPtr<T>>.
In every case I found, we never wanted to support null entry values.
With NonnullOwnPtr, we can encode that at the type level. :^)
2019-07-24 08:42:55 +02:00
Andreas Kling
3c5befde36 CEventLoop: Use NonnullOwnPtr for QueuedEvent::event.
We don't allow null events in the event queue. :^)
2019-07-24 08:38:28 +02:00
Andreas Kling
66646081b7 CEventLoop: Avoid undefined evaluation order in register_timer(). 2019-07-23 14:55:12 +02:00
Andreas Kling
0ef13e60b0 Libraries: Fix wrong paths to "Root" in the various install.sh scripts.
We were installing libraries into /Libraries/Root, rather than in /Root.
This made the ports system behave rather unpredictable, since I had old
versions of things in /Root and new versions of things in /Libraries/Root.
2019-07-21 21:38:30 +02:00
Andreas Kling
c7ea94697e Libraries: Remove unused "install" targets.
We've been using a per-directory "install.sh" for some time, so let's get
rid of the old way of doing things.
2019-07-21 21:28:48 +02:00
Robin Burchell
f2c0e55070 Userspace: Deal with select() returning EINTR on a signal interruption
Add a trivial CSafeSyscall template that calls a callback until it stops
returning EINTR, and use it everywhere we use select() now.

Thanks to Andreas for the suggestion of using a template parameter for
the syscall function to invoke.
2019-07-21 14:27:14 +02:00
Andreas Kling
98b569a702 CEventLoop: Skip over null events in the queue.
Added some FIXME's about correctness issues in nested event loop exiting.
2019-07-21 10:18:00 +02:00
Andreas Kling
046f00f77e CEventLoop: Use Vector::prepend(Vector&&) to shuffle events to outer loop.
When exiting a nested event loop, we prepend any unprocessed events to the
outer loop's event queue.
2019-07-20 16:11:45 +02:00
Andreas Kling
26c29e59ec CEventLoop: Remove some no-longer-used virtuals. 2019-07-20 15:50:03 +02:00
Andreas Kling
1c0669f010 LibDraw: Introduce (formerly known as SharedGraphics.)
Instead of LibGUI and WindowServer building their own copies of the drawing
and graphics code, let's it in a separate LibDraw library.

This avoids building the code twice, and will encourage better separation
of concerns. :^)
2019-07-18 10:18:16 +02:00
Robin Burchell
57da716be0 ps: Port to using CProcessStatisticsReader and /proc/all
Drop /proc/summary in the process.
We only needed one new field here, thankfully, so this was quite straightforward.
2019-07-18 07:23:26 +02:00
Robin Burchell
a9d1a86e6e CProcessStatisticsReader: Be consistent about terminology from the kernel down 2019-07-18 07:23:26 +02:00
Robin Burchell
9c8dd836fc Rename new IPC headers & classes
Sticking these in a namespace allows us to use a more generic
("Connection") term without clashing, which is way easier to understand
than to try to come up with unique names for both.
2019-07-17 20:16:44 +02:00
Robin Burchell
2177594c96 Port LibGUI to use CIPCClientSideConnection
As a consequence, move to use an explicit handshake() method rather than
calling virtuals from the constructor. This seemed to not bother
AClientConnection, but LibGUI crashes (rightfully) because of it.
2019-07-17 20:16:44 +02:00
Robin Burchell
41bece0682 Make AClientConnection generic 2019-07-17 20:16:44 +02:00
Robin Burchell
6eaa6826fa Port WSClientConnection to CIPCServerSideClient 2019-07-17 20:16:44 +02:00
Robin Burchell
edcbba9e98 Introduce CIPCServerSideClient
As a new base class of IPC connections server-side.
Port ASClientConnection to CIPCServerSideClient.
2019-07-17 20:16:44 +02:00
Robin Burchell
d8387f1506 CNotifier: Turn into a CObject and Use the event queue to deliver events
This way, CNotifier can mutate state to its little heart's content
without destroying the world when the global CNotifier hash changes
during delivery.
2019-07-16 20:47:32 +02:00
Robin Burchell
a714fc661d LibCore: Add a way to mark a socket as blocking (or not)
If custom I/O is being done outside CIODevice, we need a way to force blocking sometimes.
This also fixes the default of CLocalSocket to be non-blocking, the same
as CTCPSocket.
2019-07-16 20:22:54 +02:00
Robin Burchell
7bf420d83d CNotifier: Provide a way to unregister a notifier temporarily 2019-07-16 15:23:57 +02:00
Robin Burchell
e922db68d8 CSocket: Also call on_connected for local socket connections 2019-07-16 13:18:37 +02:00
Robin Burchell
14b2f90920 LibCore: Always call on_connected whether the connection was synchronous or not
It's unreasonable to expect the client to have to call it themselves if
the connection was immediate (local).
2019-07-16 13:18:37 +02:00
Robin Burchell
cd497accbe CLocalSocket: Add missing pragma once 2019-07-14 15:29:59 +02:00
Andreas Kling
e8d61bb8c0 CEventLoop: Oops, I had the pipe reader/writer fd's mixed up. 2019-07-14 14:28:24 +02:00
Andreas Kling
4c0c93ce09 LibCore: Oops, fix infinite recursion in LogStream << CSocketAddress. 2019-07-14 14:24:37 +02:00
Andreas Kling
c9ee481cdf LibCore: Port CSocket over to using dbg().
Also added a LogStream operator<< for CSocketAddress.
2019-07-14 11:02:40 +02:00
Andreas Kling
b4329a8eec CObject: Add LogStream operator<< for CObject. 2019-07-14 10:59:26 +02:00
Andreas Kling
a634fab3c4 CObject: Add custom_event() virtual.
This way you can just override custom_event() to catch CCustomEvent instead
of having to filter the entire event stream with event(). :^)
2019-07-14 10:27:27 +02:00
Andreas Kling
17ee548bcd CEventLoop: Add wake(), a mechanism for waking up when blocked in select().
This patch generalizes the concept used in Piano to wake up the event loop
so it can react to something happening on a secondary thread.
Basically, there's a pipe who is always part of the file descriptor set we
pass to select(), and calling wake() simply writes a little to that pipe.
2019-07-14 10:20:57 +02:00
Andreas Kling
f1d6a37d5d LibCore: Add CThread, a simple thread abstraction object.
Currently this is only a simple wrapper around create_thread() that
remembers the thread ID of the spawned thread.
2019-07-14 10:19:51 +02:00
Robin Burchell
ffa8cb668f AudioServer: Assorted infrastructure work
* Add a LibAudio, and move WAV file parsing there (via AWavFile and AWavLoader)
* Add CLocalSocket, and CSocket::connect() variant for local address types.
  We make some small use of this in WindowServer (as that's where we
  modelled it from), but don't get too invasive as this PR is already
  quite large, and the WS I/O is a bit carefully done
* Add an AClientConnection which will eventually be used to talk to
  AudioServer (and make use of it in Piano, though right now it really
  doesn't do anything except connect, using our new CLocalSocket...)
2019-07-13 22:57:24 +02:00
Andreas Kling
302cae5c2f CEventLoop: When asked to exit the event loop, exit right away.
Don't process any more events. We already prepend the remaining events in
this loop to the outer loop if needed.

If there were any more events queued after the exit request, the iteration
code would make an invalid access into 'queued_events'.

Fixes #300.
2019-07-13 19:54:57 +02:00
Andreas Kling
debc587ce2 LibCore: Add CCustomEvent, a custom CEvent for arbitrary use.
For convenience it includes an int, and a void*. Interpretation of the
contents is up to the client.
2019-07-13 18:35:13 +02:00
Andreas Kling
b06d2c04dc CObject: Add a "name" property.
This will be useful for things like the VisualBuilder code generator.
2019-07-10 20:33:53 +02:00
Andreas Kling
c33766f039 LibCore: Tweak slightly-off error message in CProcessStatisticsReader. 2019-07-10 15:32:30 +02:00
Andreas Kling
01216f3c3f Userland+LibCore: Use CProcessStatisticsReader to implement top.
Also tweaked CProcessStatisticsReader a bit to simplify the API.
2019-07-10 13:56:28 +02:00
Andreas Kling
0e75aba7c3 StringView: Rename characters() to characters_without_null_termination().
This should make you think twice before trying to use the const char* from
a StringView as if it's a null-terminated string.
2019-07-08 15:38:44 +02:00
Andreas Kling
56563cb305 CDirIterator: Fix another instance of StringView::characters() misuse. 2019-07-08 14:03:19 +02:00
Andreas Kling
8b0953a795 Libraries: Unbreak "make install" with new directory locations. 2019-07-04 16:41:42 +02:00
Andreas Kling
04b9dc2d30 Libraries: Create top level directory for libraries.
Things were getting a little crowded in the project root, so this patch
moves the Lib*/ directories into Libraries/.
2019-07-04 16:16:50 +02:00