Commit Graph

34 Commits

Author SHA1 Message Date
Zbigniew Jędrzejewski-Szmek db3d4222e9 varlink: add debug logging
When something fails, we need some logs to figure out what happened.
This is primarily relevant for connection errors, but in general we
want to log about all errors, even if they are relatively unlikely.

We want one log on failure, and generally no logs on success.
The general idea is to not log in static functions, and to log in the
non-static functions. Non-static functions which call other functions
may thus log or not log as appropriate to have just one log entry in the
end.
2020-12-14 19:21:55 +01:00
Yu Watanabe db9ecf0501 license: LGPL-2.1+ -> LGPL-2.1-or-later 2020-11-09 13:23:58 +09:00
Anita Zhang e8e9227f5c varlink: add server write states to disconnect check
While a server is in the VARLINK_PENDING_METHOD or VARLINK_PENDING_METHOD_MORE
states and its write end is disconnected and it gets a POLLHUP, we
should disconnect since it can't write anymore.

In the case of systemd-oomd disconnecting while pid1 was pending-more, this
condition left pid1 in a state where it started throttling from
continually getting POLLHUP.
2020-10-07 16:17:23 -07:00
Frantisek Sumsal 05827831a6 shared: drop a redundant if statement 2020-09-14 16:18:18 +02:00
Lennart Poettering 8d91b2206c varlink: properly allocate connection event source
Let's make sure we keep a reference to the event source

(Note that this code is currently not used, which is why this was never
used: in all cases we do not add listener fds after the event is
attached, but before. In that case this code is not called.)
2020-09-04 23:54:14 +02:00
Zbigniew Jędrzejewski-Szmek 47b04ef632
Merge pull request #16925 from cgzones/selinux_create_label
selinux/core: create several file objects with default SELinux context
2020-09-01 22:19:52 +02:00
Christian Göttsche 63e00ccd8e selinux: create /run/systemd/userdb directory and sockets with default SELinux context 2020-09-01 16:26:12 +02:00
Zbigniew Jędrzejewski-Szmek 77472d06a4 varlink: do not parse invalid messages twice
Upon reception of a message which fails in json_parse(), we would proceed to
parse it again from a deferred callback and hang. Once we have realized that
the message is invalid, let's move the pointer in the buffer even if the
message is invalid. We don't want to look at this data again.

(before) $ build-rawhide/userdbctl --output=json user test.user
n/a: varlink: setting state idle-client
/run/systemd/userdb/io.systemd.Multiplexer: Sending message: {"method":"io.systemd.UserDatabase.GetUserRecord","parameters":{"userName":"test.user","service":"io.systemd.Multiplexer"}}
/run/systemd/userdb/io.systemd.Multiplexer: varlink: changing state idle-client → awaiting-reply
/run/systemd/userdb/io.systemd.Multiplexer: New incoming message: {...}
/run/systemd/userdb/io.systemd.Multiplexer: varlink: changing state awaiting-reply → pending-disconnect
/run/systemd/userdb/io.systemd.Multiplexer: New incoming message: {...}
/run/systemd/userdb/io.systemd.Multiplexer: varlink: changing state pending-disconnect → disconnected
^C

(after) $ n/a: varlink: setting state idle-client
/run/systemd/userdb/io.systemd.Multiplexer: Sending message: {"method":"io.systemd.UserDatabase.GetUserRecord","parameters":{"userName":"test.user","service":"io.systemd.Multiplexer"}}
/run/systemd/userdb/io.systemd.Multiplexer: varlink: changing state idle-client → awaiting-reply
/run/systemd/userdb/io.systemd.Multiplexer: New incoming message: {...}
/run/systemd/userdb/io.systemd.Multiplexer: Failed to parse JSON: Invalid argument
/run/systemd/userdb/io.systemd.Multiplexer: varlink: changing state awaiting-reply → pending-disconnect
/run/systemd/userdb/io.systemd.Multiplexer: varlink: changing state pending-disconnect → processing-disconnect
Got lookup error: io.systemd.Disconnected
/run/systemd/userdb/io.systemd.Multiplexer: varlink: changing state processing-disconnect → disconnected
Failed to find user test.user: Input/output error

This should fix #16683 and https://bugs.gentoo.org/735072.
2020-09-01 10:48:10 +02:00
Lennart Poettering 7466e94f13 varlink: add helper for generating errno errors 2020-08-26 16:46:57 +02:00
Zbigniew Jędrzejewski-Szmek 37b22b3b47 tree: wide "the the" and other trivial grammar fixes 2020-07-02 09:51:38 +02:00
Lennart Poettering 0f2d351f79 tree-wide: port to fd_wait_for_event()
Prompted by the discussion on #16110, let's migrate more code to
fd_wait_for_event().

This only leaves 7 places where we call into poll()/poll() directly in
our entire codebase. (one of which is fd_wait_for_event() itself)
2020-06-10 20:06:10 +02:00
Lennart Poettering dad28bffd6 tree-wide: check POLLNVAL everywhere
poll() sets POLLNVAL inside of the poll structures if an invalid fd is
passed. So far we generally didn't check for that, thus not taking
notice of the error. Given that this specific kind of error is generally
indication of a programming error, and given that our code is embedded
into our projects via NSS or because people link against our library,
let's explicitly check for this and convert it to EBADF.

(I ran into a busy loop because of this missing check when some of my
test code accidentally closed an fd it shouldn't close, so this is a
real thing)
2020-06-10 08:57:31 +02:00
Frantisek Sumsal 86b52a3958 tree-wide: fix spelling errors
Based on a report from Fossies.org using Codespell.

Followup to #15436
2020-04-21 23:21:08 +02:00
Zbigniew Jędrzejewski-Szmek f36a9d5909 tree-wide: use the return value from sockaddr_un_set_path()
It fully initializes the address structure, so no need for pre-initialization,
and also returns the length of the address, so no need to recalculate using
SOCKADDR_UN_LEN().

socklen_t is unsigned, so let's not use an int for it. (It doesn't matter, but
seems cleaner and more portable to not assume anything about the type.)
2020-03-02 15:55:44 +01:00
Lennart Poettering 6d4d600260 varlink: add ability to register callback for disconnections 2020-01-31 15:03:27 +01:00
Lennart Poettering c4f601f205 varlink: add API for determining number of current connections 2020-01-31 15:03:11 +01:00
Lennart Poettering 9652d74092 varlink: add varlink_close_unref() helper 2019-12-17 20:05:46 +01:00
Lennart Poettering d642f640bf json: add flags parameter to json_parse_file(), for parsing "sensitive" data
This will call json_variant_sensitive() internally while parsing for
each allocated sub-variant. This is better than calling it a posteriori
at the end, because partially parsed variants will always be properly
erased from memory this way.
2019-12-02 09:47:00 +01:00
Lennart Poettering f1194f5d59 varlink: fix enablement of varlink timeout event source 2019-11-22 10:54:52 +01:00
Lennart Poettering c4fe0cbc17 varlink: drop too much whitespace 2019-11-22 10:54:52 +01:00
Lennart Poettering 88a36d3690 varlink: port varlink code over to use getdtablesize() for sizing number of concurrent connections
Use the official glibc API for determining this parameter. In most other
cases in our tree it's better to go directly for RLIMIT_NOFILE since
it's semantically what we want, but for this case it appears more
appropriate to use the friendlier, shorter, explicit API.
2019-11-22 10:54:52 +01:00
Lennart Poettering a0c41de277 varlink: move connection fds > fd2
We want to use this code in NSS modules, and we never know the execution
environment we are run in there, hence let's move our fds up to ensure
we won't step into dangerous fd territory.

This is similar to how we already do it in sd-bus for client connection
fds.
2019-11-22 10:54:52 +01:00
Lennart Poettering 45a6c96598 varlink: fix support for more/continues method calls 2019-11-22 10:54:52 +01:00
Yu Watanabe 8cec0a5c32 tree-wide: drop duplicated blank lines
```
$ for i in */*.[ch] */*/*.[ch]; do sed -e '/^$/ {N; s/\n$//g}' -i $i; done
$ git checkout HEAD -- basic/linux shared/linux
```
2019-07-15 18:41:27 +02:00
Lennart Poettering 7e69d90c8d varlink: add varlink server to event loop only if there is one 2019-07-04 22:53:49 +02:00
Lennart Poettering ed9aa225e8 varlink: allow using varlink_wait() even with a server
This call can be useful even if a server object is declared.

(Originally this was not supported, because a server typically needs to
handle multiple connections, and thus a synchronous wait on one would
starve the others out. But in some cases it might make sense to have
varlink point-to-point connections — i.e. where the server only handles
a single connection ever — and there it makes sense to synchronously
wait on the one connection).
2019-07-04 12:35:52 +09:00
Zbigniew Jędrzejewski-Szmek e7b93f97ed shared/varlink: add missing va_end()
Coverity CID#1401347.
2019-05-30 20:36:54 +02:00
Zbigniew Jędrzejewski-Szmek a48481dc77 Revert "varlink: initialize Varlink with 0"
This reverts commit 8688c29b5a, but leaves the
reproducer. Structured assignment should be enough to fully initialize the
variable and new0 is not necessary.
2019-05-30 19:39:27 +02:00
Zbigniew Jędrzejewski-Szmek 2a04712c19 shared/varlink: add missing terminator in json strings
Should finally fix oss-fuzz-14688.
8688c29b5a wasn't enough.

The buffer retrieved from memstream has the size that the same as the written
data. When we write do write(f, s, strlen(s)), then no terminating NUL is written,
and the buffer is not (necessarilly) a proper C string.
2019-05-30 18:45:06 +02:00
Zbigniew Jędrzejewski-Szmek be44e09162 shared/varlink: add missing setting of output_buffer_allocated
Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=14708,
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=14735,
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=14725,
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=14720,
and probably others.
2019-05-17 15:09:32 +02:00
Yu Watanabe 8688c29b5a varlink: initialize Varlink with 0
Closes oss-fuzz#14688.
2019-05-16 18:51:33 +02:00
Zbigniew Jędrzejewski-Szmek 77740b5993 journalctl: improve error messages
Follow-up for #12230.
2019-05-10 16:55:37 +02:00
Zbigniew Jędrzejewski-Szmek 1d3fe304fd Use sd_event_source_disable_unref() 2019-05-10 16:55:37 +02:00
Lennart Poettering d41bd96f54 shared: add minimal varlink implementation
This adds a minimal Varlink (https://varlink.org/) implementation to our
tree. Given that we already have a JSON logic it's an easy thing to add.

Why bother?

We currently have major problems with IPC before dbus-daemon is up, and
in all components that dbus-daemon itself makes use of (such as various
NSS modules to resolve users as well as the journal which dbus-daemon
logs to). Because of that we so far ended up creating various (usually
crappy) work-arounds either coming up with secondary IPC systems or
sharing data statelessly in /run or similar. Let's clean this up, and
instead use a clean, well-defined, broker-less IPC for cases like that.

This is a minimal implementation of Varlink, i.e. the most basic logic
only. Stuff that's missing is left out on purpose: there's no
introspection/validation and there's no name service. It might make
sense to add that later, but for now let's only do the minimum buy-in we
can get away with. In particular as I'd assume that at least initially
we only use this IPC for our internal communication avoiding
introspection and the name service should be fine.

Specifically, I'd expect that we add IPC interfaces to the following
concepts with this scheme:

1. nss-resolve (so that hostname lookups with resolved work before
   resolved is up)
2. journald (so that IPC calls to journald don't have to go through
   dbus-daemon thus creating a cyclic dependency between journald and
   dbus-daemon)
3. nss-systemd (so that dynamic user lookups via PID 1 work sanely even
   inside of dbus-daemon, because otherwise we'd want to use dbus to run
   dbus which causes deadlocks)
4. networkd (to make sure one can talk to it in the initrd already,
   long before dbus is around)

And there might be other cases similar to this.
2019-05-09 14:14:20 -04:00