Commit Graph
40 Commits
Author SHA1 Message Date
Yu Watanabe b1eb6cc28b pidref: propagate critical errors in pidref_acquire_pidfd_id()
Follow-up for 571867ffa7.

Fixes CID#1612242.
2025-07-12 19:51:01 +09:00
Lennart Poettering 571867ffa7 pidref: add pidref_set_pid_and_pidfd_id()
This new helper takes both a PID and and a pidfd ID, and initializes a
PidRef from it. It ensures they actually belong together and returns an
error if not.
2025-07-11 09:06:41 +02:00
Yu Watanabe 9a6dcc137c basic: move missing_wait.h -> include/sys/wait.h 2025-07-01 12:27:00 +09:00
Yu Watanabe 4f18ff2e29 tree-wide: include unistd.h where necessary
We use symbols provided by unistd.h without including it. E.g.
open(), close(), read(), write(), access(), symlink(), unlink(), rmdir(),
fsync(), syncfs(), lseek(), ftruncate(), fchown(), dup2(), pipe2(),
getuid(), getgid(), gettid(), getppid(), pipe2(), execv(), _exit(),
environ, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO, F_OK, and their
friends and variants, so on.

Currently, unistd.h is indirectly included mainly in the following two paths:
- through missing_syscall.h, which is planned to covert to .c file.
- through signal.h -> bits/sigstksz.h, which is new since glibc-2.34.
  Note, signal.h is included by sd-eevent.h. So, many source files
  indirectly include unistd.h if newer glibc is used.

Currently, our baseline on glibc is 2.31. We need to support glibc older
than 2.34, but unfortunately, we do not have any CI environments with
such old glibc. CIFuzz uses glibc-2.31, but it builds only fuzzers, and
many files are even not compiled.
2025-06-30 09:19:15 +02:00
Daan De Meyer 0c15577abe basic + fundamental: Clean up includes
Split out of #37344.
2025-05-25 10:06:07 +02:00
Daan De Meyer 93a1f7921a basic: Stop including log.h in macro.h
Now that the necessary functions from log.h have been moved to macro.h,
we can stop including log.h in macro.h. This requires modifying source
files all over the tree to include log.h instead.
2025-04-18 14:19:15 +02:00
Lennart Poettering 09b3390529 pidref: take more fields into account in pidref_compare_func() 2025-02-28 14:18:20 +01:00
Lennart Poettering 2ad187fb68 pidref: export hash funcs
That way we can use them for definition of additional hash_ops that map
pidrefs to arbitrary other resources.
2025-02-28 14:18:07 +01:00
Lennart Poettering 4ace93da8c pidref: now that we have the cached pidfdid of our own process, use it
Note that this drops a lot of "const" qualifiers on PidRef arguments.
That's because pidref_is_self() suddenly might end changing the PidRef
because it acquires the pidfd ID.

We had this previously already with pidfd_equal(), but this amplifies
the problem.

I guess we C's "const" doesn't really work for stuff that contains
caches, that is just conceptually constant, but not actually.
2025-01-20 21:51:40 +01:00
Lennart Poettering 8110b34b64 pidref: various shortcuts to pidref_equal()
This adds some shortcuts to pidref_equal(), so that we don't have to
query the pidfs id if there's no need.
2025-01-16 11:55:21 +01:00
Mike Yuan 6e14c46bac tree-wide: drop support for kernels without pidfd_open() and pidfd_send_signal()
Our baseline is v5.4 now.
2025-01-12 00:01:07 +01:00
Lennart Poettering 0df46f8908 pidref: drop support for kernels lacking waitid(P_PIDFD, …)
Our baseline is not 5.4, which is where P_PIDFD was introduced.
2025-01-08 14:51:19 +01:00
Lennart Poettering 06744cbb52 pidref: copy fd id in pidref_copy() too 2025-01-08 14:51:19 +01:00
Mike Yuan 2fa42318c3 pidref: move generic pidfd_get_inode_id() to pidfd-util
Prompted by https://github.com/systemd/systemd/pull/35224/commits/221d6e54c60389f26c2c79dbfa4e83204d2775d7

Also generalize pidfd_check_pidfs() and expose have_pidfs for later use.
2025-01-04 17:07:58 +01:00
Mike Yuan b234026d09 process-util: extract pidfd-related funcs into pidfd-util.[ch] 2025-01-04 16:58:13 +01:00
Lennart Poettering 9aee971185 tree-wide: use pidref_is_self() at more places 2024-12-18 13:34:35 +01:00
Lennart Poettering 7e3e540b88 pidref: add explicit concept of "remote" PidRef
This PidRef just track some data, but cannot be used for any active
operation.

Background: for https://github.com/systemd/systemd/pull/34703 it makes
sense to track explicitly if some PidRef is not a local one, so that we
never attempt to for example "kill a remote process" and thus
acccidentally hit the wrong process (i.e. a local one by the same PID).
2024-10-15 18:26:05 +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
Mike Yuan 77f709ca84 pidref: move comments that doesn't belong in pidref_copy() to pidref_dup()
Follow-up for 232e66217d
2024-07-21 22:48:53 +02:00
Mike Yuan 15930d5d9f pidref: record pidfd inode number in PidRef struct
Besides internal comparisons, the inode number of pidfds
might be interesting directly to users, too. In the future
this field should also be exposed, so that it can serve as
a unique identifier of a process (but only for display,
as there's no method to map this back to a pid or pidfd).
2024-06-14 16:59:13 +02:00
Mike YuanandLennart Poettering 1b6239632d pidref: introduce pidfd_inode_ids_supported helper
Also, correct the comment about pidfs (added in kernel 6.9
rather than 6.8).

Co-authored-by: Lennart Poettering <lennart@poettering.net>
2024-06-14 16:46:03 +02:00
Daan De Meyer 9ac8450e7f basic: Add debug logging for pidref_set_pid() 2024-05-29 21:04:13 +02:00
Lennart Poettering d6bfc3d889 pidfd: properly detect if libc offers pidfd syscalls and make use of them then
We never updated the meson checks when glibc finally learned about these
syscalls, address that.
2024-05-08 15:36:40 +02:00
Mike Yuan 74531a93d6 pidref: 0 -> NULL for pointer 2024-04-10 01:45:12 +08:00
Mike Yuan 2f41f10bcd pidref: use fd_inode_same to compare pidfds 2024-03-11 22:57:50 +08:00
Yu Watanabe 232e66217d pidref: split out pidref_copy() from pidref_dup() 2024-01-23 22:21:44 +09:00
Lennart Poettering a3f3243613 pidref: add helpers for waiting for pidref processes
A simple test case is added in a follow-up commit.
2024-01-02 17:57:34 +01:00
Yu Watanabe c01a5c0527 siphash24: introduce siphash24_compress_typesafe() macro
To prevent copy-and-paste mistake.

This also introduce in_addr_hash_func().

No functional change, just refactoring.
2023-12-25 15:38:59 +09:00
Lennart Poettering a1796e9bd8 pidref: add pidref_set_parent() for race-freely getting pidref on ppid 2023-12-13 18:00:14 +01:00
Mike Yuan 4c8d5f0217 pidref: introduce hash ops that doesn't come with destructor 2023-11-11 17:21:10 +08:00
Lennart Poettering a7a877697f pidref: add new pidref_is_self() helper
This simply checks if the specified PidRef refers to the process we are
running in.

(In case you wonder why this is not a static inline: to avoid cyclic
header inclusion problems between pidref.h + process-util.h)
2023-10-18 14:39:33 +02:00
Lennart Poettering 3d7ba61a7b pidref: we never have to verify PID 1
The process exists as long as the kernel/userns exists at all, hence we
don't have to verify a pidfd to it.
2023-10-18 14:39:33 +02:00
Lennart Poettering 44c55e5a3f pidref: make signal sending calls take const PidRef 2023-10-18 14:38:07 +02:00
Lennart Poettering bd389293f0 pidref: make pidref_verify() parameter const 2023-10-18 10:32:03 +02:00
Lennart Poettering ec8dc83530 pidref: add pidref_verify() helper
This new helper can be used after reading process info from procfs, to
verify that the data that was just read actually matches the pidfd, and
does not belong to some new process that just reused the numeric PID of
the process we originally pinned.
2023-09-28 23:22:58 +02:00
Lennart Poettering 9cb7e49f11 pidref: add pidref_hash_ops
This adds a "hash_ops" structure, which allows using PidRef structures
as keys in Hashmap and Set objects.
2023-09-28 23:22:58 +02:00
Lennart Poettering 837659825f pidref: add helpers for managing PidRef on the heap
Usually we want to embed PidRef in other structures, but sometimes it
makes sense to allocate it on the heap in case it should be used
standalone. Add helpers for that.

Primary usecase: use as key in Hashmap objects, that for example map
process to unit objects in PID 1.

This adds pidref_free()/pidref_freep() for freeing such an allocated
struct, as well as pidref_dup() (for duplicating an existing PidRef
on the heap 1:1), and pidref_new_pid() (for allocating a new PidRef from a
PID).
2023-09-28 23:22:58 +02:00
Lennart Poettering dcfcea6d02 pidref: add PIDREF_MAKE_FROM_PID()
This helper truns a pid_t into a PidRef. It's different from
pidref_set_pid() in being "passive", i.e. it does not attempt to acquire
a pidfd for the pid.

This is useful when using the PidRef as a lookup key that shall also
work after a process is already dead, and hence no conversion to a pidfd
is possible anymore.
2023-09-28 23:22:58 +02:00
Lennart Poettering a0d1659c23 pidref: add sigqueue() helper 2023-09-18 19:08:09 +02:00
Lennart Poettering 3bda3f17fa pidref: add structure that can reference a pid via both pidfd and pid_t
Let's start with the conversion of PID 1 to pidfds. Let's add a simple
structure with just two fields that can be used to maintain a reference
to arbitrary processes via both pid_t and pidfd.

This is an embeddable struct, to keep it in line with where we
previously used a pid_t directly to track a process.

Of course, since this might contain an fd on systems where we have pidfd
this structure has a proper lifecycle.

(Note that this is quite different from sd_event_add_child() event
source objects as that one is only for child processes and collects
process results, while this infra is much simpler and more generic and
can be used to reference any process, anywhere in the tree.)
2023-09-09 14:03:31 +02:00