Commit graph

916 commits

Author SHA1 Message Date
Michael Meeks
f845ac08af bgsave: have a single source for InputProcessing enable & disable.
Somehow this state can get confused in a bgsave process:

   Kit Document:
         ...
         inputProcessingEnabled: false
         ...
   SocketPoll:
     Poll [kit] with 1 socket - wakeup rfd: 39 wfd: 45
             fd        events        rbuffered        wbuffered        rtotal        wtotal
             52        0x1        process             0             0         r:    825

'process' should read 'ignore' for disabled input.

Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
Change-Id: I787eebe6fda3ae1b527d7605b8813fa764e81890
2024-04-18 17:19:19 +01:00
Michael Meeks
eceb1db8ec bgsave: remove closed sockets from Kit's SocketPoll.
Hard closing without shutdown is necessary, but we continued to poll
and read on an fd that would be re-used to open eg. a ZIP file:

[ kitbgsv_007_001 ] TRC  #19: Incoming WebSocket data of 13522 bytes: 50 4B 03 04 14 00 00 08  00 00 29 9C 90 58 33 26  AC A8 2F 00 00 00 2F 00  00 00 08 00 00 00 6D 69  | PK........)..X3&../.../.......m"
...
[ kitbgsv_007_001 ] ERR  #19: An unfragmented message or the first fragment of a fragmented message must have the opcode different than 0| net/WebSocketHandler.hpp:452

which would then close the file unhelpfully.

Not removing the socketHandler when cleaning up means that
we trigger the ForKit's ServerWSHandler::onDisconnect which
SigUtil::setTerminationFlag() causing all 2nd kit processes
to expire on start.

We also want to ensure that we update the thread-id of the last
forkit process before we start removing sockets and checking
thread-ids.

We want to get rid of the parent process' sockets we inherited
but don't need very cleanly post fork.

Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
Change-Id: I82966f4421fc96df552fd50cf81c8b0bc92b9bbb
2024-04-17 08:35:30 +01:00
Noel Grandin
8739373a88 use erase/remove idiom to clean pollSockets
which is more efficient than repeatedly erasing in a vector.

Signed-off-by: Noel Grandin <noel.grandin@collabora.co.uk>
Change-Id: Iebd41130b37a67ffd74fa2d692b83220b1cab3f4
Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
2024-04-16 14:00:42 +01:00
Caolán McNamara
e1bf395798 aarch64 doesn't have SYS_futimesat
we picked something obscure, and clearly aarch64 took advantage of
not adding something obscure unnecessary for backwards compatibility
on that arch

Signed-off-by: Caolán McNamara <caolan.mcnamara@collabora.com>
Change-Id: I097ef24c98e23931a3997c743d17d52c0afd52b7
2024-04-07 22:20:10 +02:00
Michael Meeks
29ef73868f killpoco: avoid using Poco::Random which likes /dev/random.
Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
Change-Id: I34659b3fda8a22a1b34416a37e18f483d7961f5e
2024-04-07 12:11:48 +02:00
Michael Meeks
fd635e4d69 SocketPoll: better re-entrancy protection.
Remove toErase list; instead null socket pointers earlier
to make things more deterministic.

Simplify toErase path, by just removing null sockets.

Check _socketPoll array bounds to cope with a re-entrant
mutation - imperfect; but the fd comparison will help.

Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
Change-Id: I27d81358a7d80b939b50ce4ccb1b2178a091a360
2024-04-04 07:16:53 -04:00
Michael Meeks
9e0cef08c2 Websocket: allow a hard shutdown after flushing data.
Don't wait for the other end to acknowledge closing the socket.

Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
Change-Id: If5e4bb6d9c5148c0e0c61d59c233f6cf5594c577
2024-03-29 13:04:09 +00:00
Michael Meeks
425c01f284 Socket: allow re-creation of wakeup pipes.
Before spawning a child thread we want to close wakeups and
remove them from our global list to avoid the child waking the
parent.

However - if we are re-using a SocketPoll in a forked child,
we need to be able to create new wakeup sockets, so factor
out for re-use.

Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
Change-Id: I06b088e02127482a65cd9666114ca9a93fd5ca7b
2024-03-29 13:04:09 +00:00
Michael Meeks
0a9a454c96 Socket: allow disabling of 'shutdown' on a socket.
shutdown closes a socket in both the parent and the child process,
which is not what we want when forking a bgsave process - and
certainly not for the socketpair we communicate down.

Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
Change-Id: I10418e9c7f5edca60f257a60f11ba396984d39b2
2024-03-29 13:04:09 +00:00
Michael Meeks
80eeabb1a6 Watchdog: expose global watchdog thread.
This will enable thread joining before fork for bgsave.

Change-Id: I68c7c53f892f5edef861137c48759c7ccb8532ab
Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
2024-03-29 13:04:09 +00:00
Michael Meeks
588aabb7c3 Watchdog: re-direct USR2 when enabled to snapshot a late thread.
By tracking the thread-id, we can deliver a SIGUSR2 to the right
thread at the right time; this avoids perf polling our uninteresting
watchdog thread.

In that thread use Caolan's suitably obscure futimestat system-call,
so that we can record based on that to see only slow things:

perf record -e syscalls:sys_enter_futimesat -ag --call-graph dwarf,65528

Change-Id: Iad05d8589fdc9541a7d0599f63625d2cde5fdf89
Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
2024-03-29 10:35:32 +00:00
Michael Meeks
fd77301ebb polls: switch compiled in delays to 60 seconds.
Forkit forking children is done in response to socket messages,
and parent process death should kill us too.

In general if we are relying on a poll to spin to achieve
something, we have a performance bug; this should exacerbate them
to flush them out.

Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
Change-Id: I60d1c3b3c2532bbd686a3d3cfdea10f2a541a19a
2024-03-25 08:18:35 +00:00
Michael Meeks
005ba1567e PrisonerPoll - leave unused Kit sockets in the poll.
This lets us detect when Kit processes die without waiting for
the poll() timeout and feebly spinning the PrisonerPoll loop.

Instead we get notified immediately; but to do this we then need
to be able to safely transfer the socket between SocketPolls.

SocketPoll's should own Sockets - so by switching ChildProcess to
use a weak_ptr and also the NewChildren list - we can have standard
ownership and a sensible transfer between SocketPolls. A Socket is
owned either by PrisonerPoll or a DocumentBroker in the normal way.

Clean the NewChildren list as/when children are unexpectedly killed
apparently there are still some ownership issues probably around
the strong ChildProcess _ws pointer.

Change-Id: Ie541a9d03e36aee53fd57c45953e0de21ebe1828
Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
2024-03-25 08:18:35 +00:00
Ashod Nakashian
fe3c3bc135 wsd: test: move WebSocketSession under test
WebSocketSession is used exclusively for tests,
so it's best that it is located in the test
directory.

Change-Id: I88333d619902df7991c3f26b94ed309246446ae0
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2024-03-21 09:08:51 +00:00
Jaume Pujantell
8921e19d84 reduce uses of MOBILEAPP on some files
Reduce the uses of MOBILEAPP conditionals by using the isMobileApp
function.

Signed-off-by: Jaume Pujantell <jaume.pujantell@collabora.com>
Change-Id: If541307fbc457b342674cc560b6c53454f3904cf
2024-03-20 09:13:00 +01:00
Ashod Nakashian
a48917d052 killpoco: use http::Response in admin file serving
Change-Id: I9ad49002bc85071770aba08c23e0210bb10ff55b
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2024-03-19 20:51:02 -04:00
Ashod Nakashian
882350ed70 wsd: remove sendWithDateAndAgent
The explicit header entries were needed
with Poco, but since we always set them
in our http::Response constructor, these
are redundant.

Change-Id: Ifde48fd3048018747dae3e84560b31960e511a25
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2024-03-19 20:51:02 -04:00
Ashod Nakashian
7fb6ccf4ee wsd: remove duplicate Server and Date headers
We always create http::Response instances
with these headers. These explicit entries
are vestiges from Poco sockets.

Change-Id: I65c263e95b253e22e4e6deae57fffc5616311c84
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2024-03-19 20:51:02 -04:00
Michael Meeks
c1b6b5d30f Watchdog to call probe-point when SocketPolls are slow.
Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
Change-Id: Id58efab0c0fee88a3da094765eb6b7641aec6e02
2024-03-19 11:29:07 +00:00
Michael Meeks
2e784c0a03 Socket: add API to close all sockets.
This may not look hyper-elegant, but using ~Socket to do this
calls 'shutdown' first on the sockets, which closes the parent's
duplicate of the socket very unhelpfully. So close instead.

Don't touch and thus COW memory. We will leave the Socket Poll
around un-used, and un-polling from eg. the ForKit in the Kit
process.

Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
Change-Id: I1328dee0278ce39350194c617acffa114887d523
2024-03-15 11:41:23 +00:00
Miklos Vajna
7d95971b19 clientnb: missing include
<timar> net/clientnb.cpp:117:10: error: ‘shared_ptr’ in namespace ‘std’ does not name a template type
<timar>   117 |     std::shared_ptr<WebSocket> getWebSocket()
<timar>       |          ^~~~~~~~~~
<timar> net/clientnb.cpp:38:1: note: ‘std::shared_ptr’ is defined in header ‘<memory>’; did you forget to ‘#include <memory>’?

IWYU agrees it's a missing include even if this builds for me by
accident.

Signed-off-by: Miklos Vajna <vmiklos@collabora.com>
Change-Id: I367ef3efcf2bdaf1b69c4bf21ef168e03807365c
2024-03-15 11:49:09 +10:30
Michael Meeks
c9cac383b6 net: add socketpair wrapper.
Useful to have a Unix socket-pair to communicate with
a forked process.

Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
Change-Id: Ic4ad1eee62b6d3b40a03bc8e59bce6e0e16efc28
2024-03-12 08:49:39 +00:00
Michael Meeks
a8d97dc16d Calm down warnings.
Seems browsers start to ping servers on their websockets, so don't
warn about that.

We trim memory left and right in clients, that doesn't deserve a
warning either.

Change-Id: I7bdcc99d167a8df3c847a1893dee8cd9123250f2
Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
2024-03-12 08:33:17 +00:00
Ashod Nakashian
d57baeb906 wsd: reduce directives for unused variable supression
We use [[maybe_unused]] to reduce explicit supression
especially under compile-time directives.

Change-Id: Ic5f3f3227a80efe52097cb35520d05b9cdaacb42
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2024-03-09 12:37:19 -05:00
Michael Meeks
0b91bacdc8 killpoco: reduce scope and number of various includes.
Change-Id: Ic3eb409fbb11cc665f0f3a55bb7a4e59cbd28f8b
Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
2024-03-06 20:56:55 -05:00
Ashod Nakashian
cd878f3c6b wsd: expose http::Session's socket FD
For logging and informational purposes.

Change-Id: I200a54604c40351516f7947cb2bca8cf01a89c81
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2024-03-05 07:10:34 -05:00
Noel Grandin
8a8e55c16e replace Poco in StreamSocket::send
Signed-off-by: Noel Grandin <noel.grandin@collabora.co.uk>
Change-Id: I987cc515e27fbaf4e778a5ac6f005a43ff88be71
2024-02-27 13:17:12 +00:00
Aron Budea
3b8f3d07f6 Replace Util::startsWith/endsWith w/ C++20 equivalent
Replace a few cases of Util::toString(...), too.

Signed-off-by: Aron Budea <aron.budea@collabora.com>
Change-Id: I53c5dbbdf2f60710e94add9dd137aa9becb7bd3e
2024-02-22 08:08:32 +01:00
Michael Meeks
7031c3b0ce cool#8328 - config header fixup.
config.h should be included as the 1st line in each source file.
It should not be included in headers.

config_version.h which changes on every commit should not be
included widely - so remove it from the HttpRequest heeader to
save tinderbox's ccache.

Fetch version info from helper methods rather than in-lining via
defines, to better encapsulate.

Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
Change-Id: If449a36f1ac61940f04d70d5f4180db389d9b4c4
2024-02-21 09:35:25 +00:00
Caolán McNamara
71a427c465 cid#344351 COPY_INSTEAD_OF_MOVE
and

cid#336280 COPY_INSTEAD_OF_MOVE
cid#318848 COPY_INSTEAD_OF_MOVE
cid#318964 COPY_INSTEAD_OF_MOVE
cid#344354 COPY_INSTEAD_OF_MOVE

Signed-off-by: Caolán McNamara <caolan.mcnamara@collabora.com>
Change-Id: I60a904be4b5edf61cfd5b2efab81e5c1a089b661
2024-02-12 11:12:33 +01:00
Ashod Nakashian
92e52e663c wsd: support sendMessage with string literals
Change-Id: I1caaf408c424c1cb11fec6aaf9d894dac7f99f93
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2024-02-09 15:50:50 +00:00
Ashod Nakashian
a449b8d229 wsd: simplify WebSocketHandler::sendErrorAndShutdown
Socket::shutdown() already does ignoreInput().

Change-Id: I75004ae881c481440d3e3cd31a96ba75ddbad22d
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2024-02-09 15:50:50 +00:00
Ashod Nakashian
d3905698b0 wsd: detect unencoded WOPISrc
We should always have an encoded WOPISrc.
We add detection logic to make sure
that all URIs that contain WOPISrc have
it encoded properly. We do this by
comparing the decoded WOPISrc with
the original URI.

Change-Id: Ia0c2a79b009ce105321ad35db3d4f81006e81cb3
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2024-02-09 03:57:04 -05:00
Caolán McNamara
95c98805a0 cid#336284 COPY_INSTEAD_OF_MOVE
and

cid#336290 COPY_INSTEAD_OF_MOVE
cid#336288 COPY_INSTEAD_OF_MOVE
cid#336282 COPY_INSTEAD_OF_MOVE
cid#318980 COPY_INSTEAD_OF_MOVE
cid#318853 COPY_INSTEAD_OF_MOVE

Signed-off-by: Caolán McNamara <caolan.mcnamara@collabora.com>
Change-Id: Icf6447b8298432359ec37242427ce595f8de4160
2024-02-08 08:49:26 +01:00
Michael Meeks
7387d9cc04 Reduce noise of somewhat excessive trace debug.
Given the surrounding debug we should be able infer the same
state from its absence.

Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
Change-Id: Id20de40f67ed52dc5535585aec9b5888131d6324
2024-02-07 21:17:48 +00:00
Michael Meeks
fa80ecd2d0 sockets should use CLOEXEC ticket cool#8016
Change-Id: I5f59ed9446a1fda301bb4791f98ca7c1c137e606
Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
2024-01-29 21:01:39 +00:00
Ashod Nakashian
61cf7b9601 wsd: clean up unused header includes
Clang-tidy recommendation driven header
include clean-up.

Change-Id: I30c32866b7798e70df0463ee6bc7a0bcc3de5049
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2024-01-08 08:38:48 +01:00
Michael Meeks
7183a3d3de spdx: improve machine and human readability of headers.
Change-Id: Ice934380029bf27054e830fffc07a5d037d1430f
Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
2023-11-14 19:36:31 +00:00
Michael Meeks
08d9081280 net: don't try to set TCP_NODELAY on local Unix sockets.
Change-Id: Ic0f720554080a01b7a46abbffda023834016c0ea
Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
2023-11-01 08:06:53 +00:00
Caolán McNamara
e0c161c17b These warnings are frequent but harmless
Signed-off-by: Caolán McNamara <caolan.mcnamara@collabora.com>
Change-Id: Id2517f310a1df71dc1357d103ea074bdd12e7e65
2023-10-25 20:08:20 +02:00
Ashod Nakashian
184a4a522e killpoco: replace HTTPResponse in sendFileAndShutdown
We now use our http::Response in HttpHelper::sendFileAndShutdown.

Change-Id: I77503fce7a66a11435bad896d3d5d6df611544be
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2023-10-24 21:33:02 -04:00
Ashod Nakashian
c3fb1d0dde wsd: helper to serialize pairs
Since we don't want to propagate Poco types
as well as our pair containers, it's best
to have these are named functions rather
than as operator<<.

Change-Id: I9772b8f314262147b6c445464bf3ce8cb598b56d
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2023-10-24 21:33:02 -04:00
Ashod Nakashian
6ed5dc9a64 wsd: http: no need to pass the mime-type explicitly to sendFile
Since we already have an http response instance (in all but
one case, prior to this change), there is no need to pass
the mime-type explicitly to the sendFileAndShutdown
function, only to set it on the http response.

There are already too many arguments and the mime-type
is surely redundant.

Change-Id: Iab64074dc111573b87fb8fa9b907c26a4160910c
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2023-10-24 21:33:02 -04:00
Ashod Nakashian
0c59eda780 wsd: http: use named HTTP status-code instead of naked int
Self-documenting, less error-prone, and searchable.

Change-Id: Ifc21d7cf5512d9a63c5d5bc10ebc003fcccc93c1
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2023-10-24 21:33:02 -04:00
Ashod Nakashian
7e1a23f47a wsd: test: helper to wait for any of the given messages
Change-Id: Ice48f59202306134dbafb411b8078f416e56b1c0
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2023-10-24 09:20:32 +02:00
Ashod Nakashian
7e105e1651 wsd: log TCP_NODELAY error only once
It seems that when TCP_NODELAY fails to be set,
the failure is permanent. As such, there is no
point in filling the logs with the same error.

This patch logs the error only once, per process,
and supresses further logs from Socket::setNoDelay().

Change-Id: I52c6b8cca35a8c281b4c4639d61a7e2521775d49
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2023-10-23 21:22:15 +01:00
Caolán McNamara
3d942b4d09 cid#318905 Untrusted allocation size
rearrange so coverity can see the size check

Signed-off-by: Caolán McNamara <caolan.mcnamara@collabora.com>
Change-Id: Ifad60b1a2aba2cf9c6cfbf4ea8e450b9846de576
2023-10-20 17:01:47 +02:00
Caolán McNamara
c6b0eebbf8 cid#329408 Unchecked return value from library
Signed-off-by: Caolán McNamara <caolan.mcnamara@collabora.com>
Change-Id: Id034ba2e1ca9d4d3d7f7722b929063cee2f7d4e3
2023-10-17 09:48:02 +02:00
Caolán McNamara
960938e000 cid#318957 Unchecked return value from library
Signed-off-by: Caolán McNamara <caolan.mcnamara@collabora.com>
Change-Id: I5bd471f86668d9fbd72cfb050fb67df6d70bdc9f
2023-10-16 09:37:01 +02:00
Caolán McNamara
8d42849156 ProcSMapsFile might not be a valid file descriptor
in which case don't send it, which then implies we might have
an empty shareFDs which we can treat the same as a nonexisting
shareFDs

Signed-off-by: Caolán McNamara <caolan.mcnamara@collabora.com>
Change-Id: I80a78a01c69dbee5ee28a64442a5069a6c2b4dbe
2023-10-14 12:56:15 +01:00