Commit Graph
111 Commits
Author SHA1 Message Date
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
Zbigniew Jędrzejewski-Szmek 0c927c98a7 basic/socket-util: reserve more space for timeval/timespec messages
As reported in https://github.com/systemd/systemd/issues/38222, we get the
buffer size calculation wrong on 32-bit arm. The reporter suggested checking if
__TIMESIZE == 64, but I think it's better to just use the bigger size in all
cases. The code to guesstimate the buffer size was already tweaked a few times
and apparently it's not easy to get it right. Systemd and glibc might be
compiled with different combinations of __TIMESIZE, so the compile-time check
is not super reliable, and by using the bigger size, we don't depend on the
details of how glibc decided whether to duplicate the struct or not. The cost
is negligible, 16 bytes on stack, so let's do the easy and robust thing.

Solution based on the suggestion by UZver24.

While at it, drop the mock struct definitions. They are only used to calculate
the size, but since the types involved are 8-bit values, there are no alignment
issues and we can just calculate the size directly.

Fixes https://github.com/systemd/systemd/issues/38222.
2025-07-18 02:06:37 +09:00
DaanDeMeyer 47f761fda6 basic: Use hyphens in missing header names instead of underscores 2025-07-13 12:41:14 +02:00
Yu Watanabe 549f679a49 basic: move missing_socket.h -> include/sys/socket.h 2025-07-01 12:27:59 +09:00
Lennart Poettering ea71d34738 fd-util: move cmsg_close_all() to socket-util.[ch]
This call only makes sense on sockets, it's not a generic fd concept,
hence let's move this over.
2025-06-18 19:06:11 +09:00
Daan De Meyer c5d7a2d000 basic,shared: mark eligible functions with _pure_/_const_ (#37623) 2025-05-27 14:24:30 +02:00
Daan De Meyer 8cf13c8752 socket-util: Move getsockopt_int() to implementation file
And switch back to negative_errno() at the same time.
2025-05-26 21:28:10 +02:00
Mike Yuan 180341e732 basic,shared: mark eligible functions with _pure_/_const_
Following the recent witch hunt done to static inline functions
I figure we should at least give the compiler some more hint
on optimizations.
2025-05-26 20:26:58 +02:00
Daan De Meyer 0c15577abe basic + fundamental: Clean up includes
Split out of #37344.
2025-05-25 10:06:07 +02:00
Lennart Poettering 272017d32a socket-util: add trivial socket_get_cookie() helper 2025-05-15 09:45:19 +02:00
Ivan Kruglov e739395f13 socket-util: socket_address_equal_unix() 2025-05-13 01:55:15 -07:00
Todd C. Miller ffb6adb763 flush_ports: flush POSIX message queues properly
On Linux, read() on a message queue descriptor returns the message
queue statistics, not the actual message queue data.  We need to use
mq_receive() to drain the queues instead.

Fixes a problem where a POSIX message queue socket unit with messages
in the queue at shutdown time could result in a hang on reboot/shutdown.
2025-05-08 02:37:45 +09:00
Daan De Meyer a0233fcd86 socket-util: Replace sockaddr length macros with functions
There's no need for these to be macros, let's just make them regular
functions instead.
2025-05-02 09:41:41 +02:00
Yu Watanabe 0dc09ccba7 linux: include sys/socket.h in linux/vm_sockets.h
The header requires struct sockaddr declared. So, otherwise, we need to
include sys/socket.h earlier than linux/vm_sockets.h.
Let's make the header includable at any place.
2025-04-30 07:37:34 +02:00
Mike Yuan 1eeb4f9f47 socket-util: introduce socket_autobind() helper
Prompted by https://github.com/systemd/systemd/pull/36858#discussion_r2017719305
and #36833
2025-03-31 18:11:37 +02:00
Lennart Poettering b2206fe514 socket-util: introduce getpeerpidref()
This combines getpeercred() and getpeerpidfd() and returns a PidRef
2025-01-06 09:45:57 +01:00
Michiel 2053182bd1 Fix some typos in socket-util.h 2024-10-14 16:09:41 +02:00
Yu Watanabe 85fd8df03d missing_socket: drop unnecessary definitions
Now, we have copy of vm_sockets.h, hence these definitions are not
necessary anymore.
2024-09-09 14:52:18 +09:00
Mike Yuan ad501930d7 socket-util: make recvmsg_safe() handle MSG_TRUNC too
Also, unify MSG_TRUNC handling all across the codebase.
2024-09-04 18:51:44 +02:00
Yu Watanabe 029709f932 socket-util: introduce netlink_socket_get_multicast_groups()
No functional change. Preparation for later commits.
2024-08-02 11:16:33 +09:00
Lennart Poettering 06d4fe57c9 vmspawn: use our generic vsock CID parser, instead of a howngrown one
Let's also use 'unsigned' as type for the CID everywhere, and
VMADDR_CID_ANY as marker for "invalid CID", as that's what the vsock
APIs natively do.
2024-01-12 16:03:19 +01:00
Lennart Poettering fc1f05ebab socket-util: modernize socknameinfo_pretty() a bit 2024-01-09 10:00:21 +09:00
Lennart Poettering d3109d8de0 socket-util: add helper for querying the local AF_VSOCK CID 2024-01-08 10:26:34 +01:00
Lennart Poettering 8e471c6a9f socket-util: add more careful parsers for AF_VSOCK cid/port
Let's handle the magic CIDs, and filter out invalid ports.
2024-01-05 22:29:55 +01:00
Lennart Poettering da5e0c442b socket-util: add helper for getting peer pidfd 2024-01-02 17:57:34 +01:00
Luca Boccassi 598d2428e1 socket-util: add send/receive helpers for FD array 2023-07-29 11:25:05 +01:00
Zbigniew Jędrzejewski-Szmek da89046643 tree-wide: "<n>bit" → "<n>-bit"
In some places, "<n> bits" is used when more appropriate.
2023-07-02 11:10:12 +01:00
Lennart Poettering 768fcd779f socket: bump listen() backlog to INT_MAX everywhere
This is a rework of #24764 by Cristian Rodríguez
<crodriguez@owncloud.com>, which stalled.

Instead of assigning -1 we'll use a macro defined to INT_MAX however.
2023-06-13 14:17:25 +02:00
Yu Watanabe 4836f4c67d socket-util: introduce CMSG_FIND_AND_COPY_DATA()
The cmd(3) man page says about CMSG_DATA():
> The pointer returned cannot be assumed to be suitably aligned for
> accessing arbitrary payload data types. Applications should not cast
> it to a pointer type matching the payload, but should instead use
> memcpy(3) to copy data to or from a suitably declared object.

Hence, if we want to use unaligned data in cmsg, we need to copy it
before use. That's typically important for reading timestamps in
RISCV32, as the time_t is 64bit and size_t is 32bit on the system.
2023-04-16 13:26:55 +09:00
Yu Watanabe 1113e50796 tree-wide: replace __alignof__() with alignof()
Addresses https://github.com/systemd/systemd/pull/27254#discussion_r1165267046.
2023-04-14 14:39:06 +09:00
Yu Watanabe 4db752e4aa socket-util: add one missing paren
Follow-up for b6256af75e.
2023-04-14 13:49:35 +09:00
Lennart Poettering 79dec6f5cc socket-util: tighten aignment check for CMSG_TYPED_DATA()
Apparently CMSG_DATA() alignment is very much undefined. Which is quite
an ABI fuck-up, but we need to deal with this. CMSG_TYPED_DATA() already
checks alignment of the specified pointer. Let's also check matching
alignment of the underlying structures, which we already can do at
compile-time.

See: #27241

(This does not fix #27241, but should catch such errors already at
compile-time instead of runtime)
2023-04-13 10:21:31 +02:00
Lennart Poettering 5f64d2bf33 socket-util: fix socket_get_family()
Function didn't actually return anything useful. Quite a shame.
2023-03-10 09:36:18 +01:00
Luca Boccassi 747b5d963e src/shared/: split AF_UNIX/AF_VSOCK address parsing into src/basic/
We'll use it from libsystemd0 later, but AF_INET/6 requires some
netlink calls and thus the additional library dependency
2023-01-05 18:43:47 +01:00
Lennart Poettering b6256af75e socket-util: add CMSG_TYPED_DATA() as type-safe wrapper around CMSG_DATA() 2022-12-08 17:48:34 +01:00
Zbigniew Jędrzejewski-Szmek 28e7e9347d basic/socket-util: rename fd_inc_rcvbuf → fd_increase_rxbuf
See previous commit for justification.
2022-06-30 11:02:13 +02:00
Lennart Poettering 2679aee440 socket-util: add new connect_unix_path() helper
This is a short helper for connecting to AF_UNIX sockets in the file
system. It works around the 108ch limit of sockaddr_un, and supports
"at" style fds.

This doesn't come with a test of its own, but the next patch will add
that.
2022-05-14 05:01:38 +09:00
Lennart Poettering b501e42e71 socket-util: don't reference field by macro parameter name
Let's avoid ambigituies here. (Interesting that the current users
compiled at all, in fact)
2022-05-09 13:55:28 +02:00
Yu Watanabe 798931160e tree-wide: add a space after if, switch, for, and while 2022-04-01 22:48:42 +09:00
Lennart Poettering c1b91f06b9 socket-util: add helper for generically initializing sockaddr_union from in_addr_union 2021-11-22 22:18:34 +01:00
Lennart Poettering a995ce4768 util: define initializer for 'struct ucred' that properly invalidates all fields
i.e. let's make sure to invalid uid/gid to UID_INVAID + GID_INVALID
instead of zero.
2021-10-11 15:37:37 +02:00
Luca Boccassi f621b8d773 socket-util: const-ify iov parameter in send_one_fd_iov_sa 2021-09-20 12:30:02 +01:00
Lennart Poettering 6aebfec3a5 sysctl-util: make sysctl_write_ip_property() a wrapper around sysctl_write()
It does the same stuff, let's use the same codepaths as much as we can.

And while we are at it, let's generate good error codes in case we are
called with unsupported parameters/let's validate stuff more that might
originate from user input.
2021-09-15 16:32:40 +02:00
Yu Watanabe 05403363ca tree-wide: fix typo 2021-09-05 09:09:13 +02:00
Yu Watanabe 9365e296fe socket-util: introduce CMSG_SPACE_TIMEVAL/TIMESPEC macro to support additional 64bit timeval or timespec
Fixes #20482 and #20564.
2021-08-30 19:56:41 +09:00
Zbigniew Jędrzejewski-Szmek 04499a70fb Drop the text argument from assert_not_reached()
In general we almost never hit those asserts in production code, so users see
them very rarely, if ever. But either way, we just need something that users
can pass to the developers.

We have quite a few of those asserts, and some have fairly nice messages, but
many are like "WTF?" or "???" or "unexpected something". The error that is
printed includes the file location, and function name. In almost all functions
there's at most one assert, so the function name alone is enough to identify
the failure for a developer. So we don't get much extra from the message, and
we might just as well drop them.

Dropping them makes our code a tiny bit smaller, and most importantly, improves
development experience by making it easy to insert such an assert in the code
without thinking how to phrase the argument.
2021-08-03 10:05:10 +02:00
Yu Watanabe 5a3586db9a socket-util: split out checking valid character for ifname into ifname_valid_char() 2021-06-23 15:59:44 +09:00
Zbigniew Jędrzejewski-Szmek befab2c40c basic/socket-util: add hint to silence gcc's maybe-unitialized warning
[59/1551] Compiling C object src/basic/libbasic.a.p/socket-util.c.o
../src/basic/socket-util.c: In function ‘socket_get_mtu’:
../src/basic/socket-util.c:1393:16: warning: ‘mtu’ may be used uninitialized in this function [-Wmaybe-uninitialized]
 1393 |         *ret = (size_t) mtu;
      |                ^~~~~~~~~~~~
2021-03-31 18:22:53 +02:00
Lennart Poettering 83e03c4fc2 socket-util: add helper for checking if IPv6 is enabled 2021-03-05 20:51:43 +01:00
Lennart Poettering e774e6fb04 socket-util: fix indentation 2021-02-15 22:50:43 +01:00