Commit Graph

34 Commits

Author SHA1 Message Date
Ivan Kruglov
366f57bff4 basic: pidref_is_set_or_automatic() 2025-10-20 02:10:00 -07:00
Daan De Meyer
f102bc3e5f tree-wide: Introduce sd-forward.h and shared-forward.h headers
Let's not leak details from src/shared and src/libsystemd into
src/basic, even though you can't actually do anything useful with
just forward declarations from src/shared.

The sd-forward.h header is put in src/libsystemd/sd-common as we
don't have a directory for shared internal headers for libsystemd
yet.

Let's also rename forward.h to basic-forward.h to keep things
self-explanatory.
2025-10-16 17:00:29 +02: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
Daan De Meyer
0c15577abe basic + fundamental: Clean up includes
Split out of #37344.
2025-05-25 10:06:07 +02:00
Daan De Meyer
3b7d332047 macro: Reduce transitive includes 2025-05-09 11:33:31 +02:00
Daan De Meyer
3b58245624 basic: Override glibc's sys/param.h header with an empty file
Instead of unconditionally including sys/param.h in
macro-fundamental.h which itself includes a bunch of other unnecessary
headers, let's override it with an empty file to avoid it from overriding
our MAX() macro. We can't make including it an error as it's included (
for seemingly no good reason) by <resolv.h>.
2025-05-09 11:23:14 +02:00
Daan De Meyer
793e187718 basic: Remove circular dependency between process-util.h and pidref.h 2025-04-18 14:19:16 +02: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
Yu Watanabe
d7306348b2 tree-wide: insert a space at the end of comments 2025-01-25 05:24:23 +09: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
06744cbb52 pidref: copy fd id in pidref_copy() too 2025-01-08 14:51:19 +01:00
Yu Watanabe
6a6c0dab30 pidref: fix typo
Follow-up for de34ec188c.
2024-10-17 00:46:45 +09: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 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
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
9bb64f1b8d pidref: make PIDREF_NULL const
That way compiler can put it in an immutable section
2023-10-19 18:59:51 +01: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
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
b0ae589b3e pidref: add trivial helper pidref_set_self() to set pidref to our handle to our own process 2023-10-05 17:08:35 +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
3b74b4958b pidref: add pidref_equal() helper
This compares two PidRef structures via the pid_t field. Ideally we'd do
a stricter comparison here, that is safe towards PID reuse, but so far
the pidfd API lacks suitable mechanisms for that, hence do the best we
can do.
2023-09-20 13:02:21 +08:00
Joerg Behrmann
7227dd816f treewide: fix typos
- mostly: usecase -> use case
- continously -> continuously
- single typos in docs/FILE_DESCRIPTOR_STORE.md
2023-09-19 10:05:38 +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