Commit Graph

3907 Commits

Author SHA1 Message Date
Lennart Poettering
0df42ebcd6 sd-varlink: allow that method handles call sd_varlink_close()
It's fine if a method handler closes the connection, deal with it
gracefully.
2024-11-07 22:30:42 +01:00
Ivan Kruglov
7779d4944c json: introduce json_dispatch_strv_environment()
I just moved json_dispatch_environment() from src/shared/user-record.c
under name 'json_dispatch_strv_environment()' to shared json code.
2024-11-06 11:37:51 +01:00
Zbigniew Jędrzejewski-Szmek
2257be13fe tree-wide: time-out → timeout
For justification, see 3f9a0a522f.
2024-11-05 19:32:19 +00:00
Luca Boccassi
1da80d2ca3 sd-daemon: some tweaks (#35011) 2024-11-05 00:56:28 +00:00
Lennart Poettering
cb42df5310 sd-daemon: add fd array size safety check to sd_notify_with_fds()
The previous commit removed the UINT_MAX check for the fd array. Let's
now re-add one, but at a better place, and with a more useful limit. As
it turns out the kernel does not allow passing more than 253 fds at the
same time, hence use that as limit. And do so immediately before
calculating the control buffer size, so that we catch multiplication
overflows.
2024-11-04 12:10:09 +01:00
Lennart Poettering
dfaff662a0 json-util: generalize json_dispatch_ifindex()
Let's move the helper from nss-resolve.c to generic code, as it's going
to be useful in #34640.

Also, let's tighten the rules, and refuse negative ifindexes, because
they are invalid.
2024-11-04 11:42:37 +01:00
Lennart Poettering
74806f7116 sd-daemon: count array elements in size_t
We fucked that up in the original sd_listen() calls, and then we fixed
that on the newer flavours. But pour internal common implementation
should of course use the full range size_t, as it should be.

This then allows us to drop a redundant range check.
2024-11-04 11:02:38 +01:00
Lennart Poettering
bea2237f67 sd-daemon: drop some redundant 'else' 2024-11-04 11:01:07 +01:00
Lennart Poettering
6606348981 sd-daemon: clean up env var unsetting
This cleans up the handling of the "unset_environment" parameter to
sd_listen() and related calls: the man pages claim we operate on it on
error too. Hence, actually do so in strictly all error paths. Previously
we'd miss out on some, because wrapper functions mishandled them.

This was addressed before in 362dcfc5db
but some codepaths were missed. Complete the work now.

This establishes a common pattern: a function to unset the relevant env
vars, that is called from a goto section at the botom on both success
and failure.
2024-11-04 11:00:13 +01:00
Yu Watanabe
4d8e5fefae sd-varlink: suppress one log message when callback already successfully enqueued an error response
Follow-up for d2ebf5cc1d.

The detailed error response is already logged, hence not necessary to
log again with the errno converted from the error response, which typically
less informative, e.g.
===
varlink-26-26: Setting state idle-server
varlink-26-26: Received message: {"method":"io.systemd.UserDatabase.GetUserRecord","parameters":{"service":""}}
varlink-26-26: Changing state idle-server → processing-method
varlink-26-26: Sending message: {"error":"io.systemd.UserDatabase.BadService","parameters":{}}
varlink-26-26: Changing state processing-method → processed-method
varlink-26-26: Callback for io.systemd.UserDatabase.GetUserRecord returned error: Invalid request descriptor
varlink-26-26: Changing state processed-method → idle-server
varlink-26-26: Got POLLHUP from socket.
===
2024-10-31 22:58:35 +01:00
Lennart Poettering
02bf14d924 logind/systemctl: one follow-up for DesignatedMaintenanceWindow (#34966)
Fixes https://github.com/systemd/systemd/issues/33429
2024-10-31 21:47:45 +01:00
Lennart Poettering
5ca96e2717 machine: several follow-ups for recent change (#34882)
Follow-ups for #34761.
2024-10-31 21:43:18 +01:00
Lennart Poettering
ccf46aa54b sd-json: don't accidentally convert between unsigned/signed when parsing signal 2024-10-31 19:54:47 +01:00
Mike Yuan
3e094f8489 bus-common-errors: use more appropriate errno for BUS_ERROR_DESIGNATED_MAINTENANCE_TIME_NOT_SCHEDULED
Fixes #33429
2024-10-31 15:46:27 +01:00
Yu Watanabe
dbceb0507f sd-json: introduce JSON_BUILD_PAIR_STRV_ENV_PAIR_NON_EMPTY() macro
It is similar to JSON_BUILD_PAIR_STRV_NON_EMPTY, but takes the
list of environment variables.
2024-10-31 11:02:35 +09:00
Yu Watanabe
32d77f5df8 sd-json: use strv_env_get_merged() 2024-10-31 11:02:35 +09:00
Lennart Poettering
d2ebf5cc1d sd-varlink: change sd_varlink_error() to always return an error
Let's make sure that sd_varlink_error() always returns an error code, so
that we can use it in a style "return sd_varlink_error(…);" everywhere,
which has two effects: return a good error reply to clients, and exit
the current stack frame with a failure code.

Interestingly sd_varlink_error_invalid_parameter() already worked like
this in some cases, but sd_varlink_error() itself didn't.

This is an alternative to the error handling tweak proposed in #34882,
but I think is a lot more generically useful, since it establishes a
pattern.

I checked our codebase, and this change should generally be OK without
breaking callsites, since the current callers (with exception of the
machined case from #34882) called sd_varlink_error() in the outermost
varlink method call dispatch stack frame, where this behaviour change
does not alter anything.

This is similar btw, how sd_bus_error_setf() and friends always return
error codes too, synthesized from its parameters.
2024-10-31 09:50:50 +09:00
Lennart Poettering
76a3af0630 sd-varlink: add helper VARLINK_STATE_WANTS_REPLY()
Let's add a helper that detects whether we still need to reply to a
state. This should make the logic easier to follow.
2024-10-31 09:50:50 +09:00
Lennart Poettering
aa5e67ae6f sd-varlink: don't show error code we already decoded as part of the log message 2024-10-31 09:50:50 +09:00
Lennart Poettering
0118074f85 sd-varlink: if we reply to errors without passing to callback, go through regular error path
If replying with an error fails, we should failt the whole connection,
and not leave the connection in a weird state.
2024-10-31 09:50:50 +09:00
Yu Watanabe
7e322c3dd0 sd-netlink,network: do not set NLM_F_CREATE and NLM_F_EXCL flags if an interface index is specified
If an ifindex is specified, we are modifying the existing interface.
Hence, these flags should not be set. Otherwise, the request will be
refused with -EEXIST.
2024-10-30 10:28:18 +01:00
Zbigniew Jędrzejewski-Szmek
23441a3d88 sd-json,tree-wide: add sd_json_format_enabled() and use it everwhere
We often used a pattern like if (!FLAGS_SET(flags, SD_JSON_FORMAT_OFF)),
which is rather verbose and also contains a double negative, which we try
to avoid. Add a little helper to avoid an explicit bit check.

This change clarifies an aditional thing: in some cases we treated
SD_JSON_FORMAT_OFF as a flag (flags & SD_JSON_FORMAT_OFF), while in other cases
we treated it as an independent enum value (flags == SD_JSON_FORMAT_OFF).
In the first form, flags like SD_JSON_FORMAT_SSE do _not_ turn the json
output on, while in the second form they do. Let's use the first form
everywhere.

No functional change intended.

Initially I wasn't sure if this helper should be made public or just internal,
but it seems such a common pattern that if we expose the flags, we might just
as well expose it too, to make life easier for any consumers.
2024-10-28 09:23:07 +01:00
Yu Watanabe
5dc0668802 sd-event: fix memleak when built without assertion
Fixes a bug introduced by baf3fdec27.

This also adds several assertions at the beginning of the function.

Fixes #34899.
2024-10-26 17:21:34 +02:00
Integral
ddb8a639d5 tree-wide: replace for loop with FOREACH_ELEMENT or FOREACH_ARRAY macros (#34893) 2024-10-26 07:10:22 +09:00
Yu Watanabe
6a6c0dab30 pidref: fix typo
Follow-up for de34ec188c.
2024-10-17 00:46:45 +09:00
Lennart Poettering
92881e7a4f json-util: initialize "remote" flag for PidRef when parsing JSON pidref serializations
Now that we have a way to recognize "remoteness" of a PidRef, let's make
sure when we decode a JSON pidref we initialize things that way.
2024-10-15 18:26:05 +02:00
Lennart Poettering
c83818edd1 Merge pull request #34778 from poettering/userdb-error-tweak
userdb: handle userbd replies indicating invalid user/group names like record not found
2024-10-15 18:17:06 +02:00
Lennart Poettering
34599ddfc6 Merge pull request #34747 from yuwata/busctl-json-fd
busctl: dump passed fd info
2024-10-15 14:38:18 +02:00
Lennart Poettering
12641ecd67 sd-varlink: add new sd_varlink_error_is_invalid_parameter() helper 2024-10-15 14:15:21 +02:00
Lennart Poettering
59577d2416 network: add AF_TO_ADDRESS_FAMILY() helper
Make some code a bit shorter.
2024-10-15 20:44:39 +09:00
Yu Watanabe
60ae3b86fb sd-json: introduce json_variant_new_fd_info()
Currently this is not used, but will be used later.
2024-10-15 19:09:38 +09:00
Yu Watanabe
32944d1c7a sd-json: introduce json_variant_new_devnum() and friends 2024-10-15 19:09:38 +09:00
Lennart Poettering
625f109604 sd-json: drop sd_json_dispatch_pid() again, as we prefer json_dispatch_pidref() now
The calls are now unused, and we generally prefer if people send a PID
triplet rather than a single PID, hence stop supporting a high-level
dispacher for pid_t.
2024-10-14 12:47:17 +02:00
Lennart Poettering
de34ec188c pidref: hookup PID_AUTOMATIC special pid_t value with PidRef
The PID_AUTOMATIC value is now properly recognized by the PidRef logic
too. This needed some massaging of header includes, to ensure pidref.h
can access process-util.h's definitions and vice versa.
2024-10-14 12:44:36 +02:00
Yu Watanabe
709fdcc75a Merge pull request #34520 from vcaputo/mmap-cache-unused-min
mmap-cache: enforce an unused windows minimum
2024-10-13 14:48:35 +09:00
Lennart Poettering
1fef1773c0 Merge pull request #34703 from poettering/pidref-varlink
Serialize "PidRef" in a reasonable way in Varlink interfaces
2024-10-11 13:33:44 +02:00
Lennart Poettering
373c810e04 Merge pull request #34681 from ikruglov/ikruglov/io-systemd-Machine-post-merge-review
machine: address post-merge review #34623
2024-10-11 12:15:56 +02:00
Lennart Poettering
1eb8a560a6 json: add builder/dispatcher for PidRef → JSON and back
So far, at the one place we sent a PID over Varlink we did so as a
simple numeric pid_t value. That's of course is racy, since classic PIDs
are recycled too eagerly.

Let's address that, by passing around JSON objects distantly resembling our
PidRef structure. Note that this JSON object does *not* contain the
pidfd, however, but just the pidfd inode number if known.

I originally planned to include the pidfd in some direct form, but I
figured that's not really the best idea, since we always need a
side-channel of some form for that (i.e. AF_UNIX/SCM_RIGHTS), but we
should be able to report about PIDs even without that.

Moreover, while sending the pid number and pidfd id around should always
be OK to do, it's a lot more problematic to always send a pidfd around,
since that implies that fd passing is on and it is OK to install fds
remotely in some IPC peers fd table. For example, when doing a wild dump
of service manager service state we really shouldn't end up with a bunch
of fds installed in our client's fd table.

Hence, all in all I think it is cleaner to define a structure carrying
pid number and pidfd inode id, wich is passed directly as JSON. And then
optionally, in a separate field also pass around a pidfd where it makes
sense.

Note that sending around pidfds is not that beneficial anymore if we
have the pidfd inode id, because we can always securely and reliably get
a pidfd back from a pair of pid + inode id: first we do pidfd_open() on
the pid, and then we check if it is really the right one by comparing
.st_ino after fstat().

This logic is implemented gracefully: if for some reason pidfd/pidfd
inode nrs are not available (too old kernel), we'll fall back to plain
PID numbers.

The dispatching logic knows two distinct levels of validation of the
provided PID data: if SD_JSON_STRICT is specified we'll acquire a pidfd
for the PID, thus verifying it currently exists and failing if it
doesn't. If the flag is not set, well just store the provided info
as-is, will try to acquire a pidfd for it, but not fail if we cannot.
Both modes are important in different contexts.

Also note that in addition to the pidfd inode nr we always store the
current boot ID of the system in the JSON object, since only the
combination of pidfd inode nr and boot ID of the system really is a
world-wide unique reference to a process.

When dispatching a JSON pid field we operate somewhat gracefully: we
either support the triplet structure of pid, pid inode nr, boot id, or
we accept a simple classic UNIX pid.
2024-10-11 10:48:07 +02:00
Yu Watanabe
ba8d48be64 sd-netlink: introduce two more _get_family()
Then, use them in sd_rtnl_message_get_family().
2024-10-11 02:53:00 +09:00
Yu Watanabe
9b8cb789dd sd-netlink,network: rename functions and RoutingPolicyRule.type
To make them consistent to the netlink message header.
No functional change, just refactoring.
2024-10-11 02:52:58 +09:00
Yu Watanabe
2bc1d78345 sd-netlink: introduce macros to define sd_rtnl_message setters and getters 2024-10-11 02:52:26 +09:00
Yu Watanabe
2fcaeaaaf6 sd-netlink: make size verifier in sd_netlink_message_read_xyz() stricter
Also, fill remaining output buffer with zero, for safety.
2024-10-11 02:38:16 +09:00
Yu Watanabe
768237e73a sd-netlink: shorten sd_netlink_message_read_string_strdup() a bit 2024-10-11 02:38:16 +09:00
Yu Watanabe
ae298c938b sd-netlink: various cleanups
- use uint8_t, uint16_t, and so on, rather than unsigned char, unsigned
  short, and so on, respectively,
- rename output parameters to ret or ret_xyz,
- add several missing assertions.
2024-10-11 02:38:16 +09:00
Ivan Kruglov
dafd44de4f sd-json: add sd_json_dispatch_pid() 2024-10-10 13:40:45 +02:00
Ivan Kruglov
729fbe78df sd-json: add sd_json_dispatch_signal() 2024-10-10 13:40:45 +02:00
Yu Watanabe
ff5ba2d6fe sd-event: rename output parameters to ret 2024-10-10 19:02:53 +09:00
Luca Boccassi
2b577d598b Merge pull request #34675 from poettering/dupfd-query
fd-util: use F_DUPFD_QUERY for same_fd()
2024-10-08 19:30:57 +01:00
Lennart Poettering
6056663a14 fd-util: introduce fd_validate() helper
It just uses F_GETFD to validate an fd. it's a bit easier to read
though, and handles the < 0 case internally.
2024-10-08 13:13:44 +02:00
Yu Watanabe
223a67e572 tree-wide: replace reallocarray() with GREEDY_REALLOC() 2024-10-08 19:56:09 +09:00