• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

systemd / systemd / 14895667988

07 May 2025 08:57PM UTC coverage: 72.225% (-0.007%) from 72.232%
14895667988

push

github

yuwata
network: log_link_message_debug_errno() automatically append %m if necessary

Follow-up for d28746ef5.
Fixes CID#1609753.

0 of 1 new or added line in 1 file covered. (0.0%)

20297 existing lines in 338 files now uncovered.

297407 of 411780 relevant lines covered (72.22%)

695716.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.86
/src/basic/socket-util.h
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2
#pragma once
3

4
#include <inttypes.h>
5
#include <linux/if_ether.h>
6
#include <linux/if_infiniband.h>
7
#include <linux/if_packet.h>
8
#include <linux/netlink.h>
9
#include <linux/vm_sockets.h>
10
#include <netinet/in.h>
11
#include <stdbool.h>
12
#include <stddef.h>
13
#include <string.h>
14
#include <sys/socket.h>
15
#include <sys/types.h>
16
#include <sys/un.h>
17

18
#include "errno-util.h"
19
#include "in-addr-util.h"
20
#include "macro.h"
21
#include "missing_network.h"
22
#include "missing_socket.h"
23
#include "pidref.h"
24
#include "sparse-endian.h"
25

26
union sockaddr_union {
27
        /* The minimal, abstract version */
28
        struct sockaddr sa;
29

30
        /* The libc provided version that allocates "enough room" for every protocol */
31
        struct sockaddr_storage storage;
32

33
        /* Protocol-specific implementations */
34
        struct sockaddr_in in;
35
        struct sockaddr_in6 in6;
36
        struct sockaddr_un un;
37
        struct sockaddr_nl nl;
38
        struct sockaddr_ll ll;
39
        struct sockaddr_vm vm;
40

41
        /* Ensure there is enough space to store Infiniband addresses */
42
        uint8_t ll_buffer[offsetof(struct sockaddr_ll, sll_addr) + CONST_MAX(ETH_ALEN, INFINIBAND_ALEN)];
43

44
        /* Ensure there is enough space after the AF_UNIX sun_path for one more NUL byte, just to be sure that the path
45
         * component is always followed by at least one NUL byte. */
46
        uint8_t un_buffer[sizeof(struct sockaddr_un) + 1];
47
};
48

49
#define SUN_PATH_LEN (sizeof(((struct sockaddr_un){}).sun_path))
50

51
typedef struct SocketAddress {
52
        union sockaddr_union sockaddr;
53

54
        /* We store the size here explicitly due to the weird
55
         * sockaddr_un semantics for abstract sockets */
56
        socklen_t size;
57

58
        /* Socket type, i.e. SOCK_STREAM, SOCK_DGRAM, ... */
59
        int type;
60

61
        /* Socket protocol, IPPROTO_xxx, usually 0, except for netlink */
62
        int protocol;
63
} SocketAddress;
64

65
typedef enum SocketAddressBindIPv6Only {
66
        SOCKET_ADDRESS_DEFAULT,
67
        SOCKET_ADDRESS_BOTH,
68
        SOCKET_ADDRESS_IPV6_ONLY,
69
        _SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX,
70
        _SOCKET_ADDRESS_BIND_IPV6_ONLY_INVALID = -EINVAL,
71
} SocketAddressBindIPv6Only;
72

73
#define socket_address_family(a) ((a)->sockaddr.sa.sa_family)
74

75
const char* socket_address_type_to_string(int t) _const_;
76
int socket_address_type_from_string(const char *s) _pure_;
77

78
int sockaddr_un_unlink(const struct sockaddr_un *sa);
79

80
static inline int socket_address_unlink(const SocketAddress *a) {
41✔
81
        return socket_address_family(a) == AF_UNIX ? sockaddr_un_unlink(&a->sockaddr.un) : 0;
41✔
82
}
83

84
bool socket_address_can_accept(const SocketAddress *a) _pure_;
85

86
int socket_address_listen(
87
                const SocketAddress *a,
88
                int flags,
89
                int backlog,
90
                SocketAddressBindIPv6Only only,
91
                const char *bind_to_device,
92
                bool reuse_port,
93
                bool free_bind,
94
                bool transparent,
95
                mode_t directory_mode,
96
                mode_t socket_mode,
97
                const char *label);
98

99
int socket_address_verify(const SocketAddress *a, bool strict) _pure_;
100
int socket_address_print(const SocketAddress *a, char **p);
101
bool socket_address_matches_fd(const SocketAddress *a, int fd);
102

103
bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) _pure_;
104

105
const char* socket_address_get_path(const SocketAddress *a);
106

107
bool socket_ipv6_is_supported(void);
108
bool socket_ipv6_is_enabled(void);
109

110
int sockaddr_port(const struct sockaddr *_sa, unsigned *port);
111
const union in_addr_union *sockaddr_in_addr(const struct sockaddr *sa);
112
int sockaddr_set_in_addr(union sockaddr_union *u, int family, const union in_addr_union *a, uint16_t port);
113

114
int sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool translate_ipv6, bool include_port, char **ret);
115
int getpeername_pretty(int fd, bool include_port, char **ret);
116
int getsockname_pretty(int fd, char **ret);
117

118
int socknameinfo_pretty(const struct sockaddr *sa, socklen_t salen, char **_ret);
119

120
const char* socket_address_bind_ipv6_only_to_string(SocketAddressBindIPv6Only b) _const_;
121
SocketAddressBindIPv6Only socket_address_bind_ipv6_only_from_string(const char *s) _pure_;
122
SocketAddressBindIPv6Only socket_address_bind_ipv6_only_or_bool_from_string(const char *s);
123

124
int netlink_family_to_string_alloc(int b, char **s);
125
int netlink_family_from_string(const char *s) _pure_;
126

127
bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b);
128

129
int fd_set_sndbuf(int fd, size_t n, bool increase);
130
static inline int fd_inc_sndbuf(int fd, size_t n) {
402,296✔
131
        return fd_set_sndbuf(fd, n, true);
402,296✔
132
}
133
int fd_set_rcvbuf(int fd, size_t n, bool increase);
134
static inline int fd_increase_rxbuf(int fd, size_t n) {
30,222✔
135
        return fd_set_rcvbuf(fd, n, true);
30,222✔
136
}
137

138
int ip_tos_to_string_alloc(int i, char **s);
139
int ip_tos_from_string(const char *s);
140

141
typedef enum {
142
        IFNAME_VALID_ALTERNATIVE = 1 << 0, /* Allow "altnames" too */
143
        IFNAME_VALID_NUMERIC     = 1 << 1, /* Allow decimal formatted ifindexes too */
144
        IFNAME_VALID_SPECIAL     = 1 << 2, /* Allow the special names "all" and "default" */
145
        _IFNAME_VALID_ALL        = IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC | IFNAME_VALID_SPECIAL,
146
} IfnameValidFlags;
147
bool ifname_valid_char(char a);
148
bool ifname_valid_full(const char *p, IfnameValidFlags flags);
149
static inline bool ifname_valid(const char *p) {
5,074✔
150
        return ifname_valid_full(p, 0);
5,074✔
151
}
152
bool address_label_valid(const char *p);
153

154
int getpeercred(int fd, struct ucred *ucred);
155
int getpeersec(int fd, char **ret);
156
int getpeergroups(int fd, gid_t **ret);
157
int getpeerpidfd(int fd);
158
int getpeerpidref(int fd, PidRef *ret);
159

160
ssize_t send_many_fds_iov_sa(
161
                int transport_fd,
162
                int *fds_array, size_t n_fds_array,
163
                const struct iovec *iov, size_t iovlen,
164
                const struct sockaddr *sa, socklen_t len,
165
                int flags);
166
static inline ssize_t send_many_fds_iov(
1✔
167
                int transport_fd,
168
                int *fds_array, size_t n_fds_array,
169
                const struct iovec *iov, size_t iovlen,
170
                int flags) {
171

172
        return send_many_fds_iov_sa(transport_fd, fds_array, n_fds_array, iov, iovlen, NULL, 0, flags);
1✔
173
}
174
static inline int send_many_fds(
175
                int transport_fd,
176
                int *fds_array,
177
                size_t n_fds_array,
178
                int flags) {
179

180
        return send_many_fds_iov_sa(transport_fd, fds_array, n_fds_array, NULL, 0, NULL, 0, flags);
181
}
182
ssize_t send_one_fd_iov_sa(
183
                int transport_fd,
184
                int fd,
185
                const struct iovec *iov, size_t iovlen,
186
                const struct sockaddr *sa, socklen_t len,
187
                int flags);
188
int send_one_fd_sa(int transport_fd,
189
                   int fd,
190
                   const struct sockaddr *sa, socklen_t len,
191
                   int flags);
192
#define send_one_fd_iov(transport_fd, fd, iov, iovlen, flags) send_one_fd_iov_sa(transport_fd, fd, iov, iovlen, NULL, 0, flags)
193
#define send_one_fd(transport_fd, fd, flags) send_one_fd_iov_sa(transport_fd, fd, NULL, 0, NULL, 0, flags)
194
ssize_t receive_one_fd_iov(int transport_fd, struct iovec *iov, size_t iovlen, int flags, int *ret_fd);
195
int receive_one_fd(int transport_fd, int flags);
196
ssize_t receive_many_fds_iov(int transport_fd, struct iovec *iov, size_t iovlen, int **ret_fds_array, size_t *ret_n_fds_array, int flags);
197
int receive_many_fds(int transport_fd, int **ret_fds_array, size_t *ret_n_fds_array, int flags);
198

199
ssize_t next_datagram_size_fd(int fd);
200

201
int flush_accept(int fd);
202
ssize_t flush_mqueue(int fd);
203

204
#define CMSG_FOREACH(cmsg, mh)                                          \
205
        for ((cmsg) = CMSG_FIRSTHDR(mh); (cmsg); (cmsg) = CMSG_NXTHDR((mh), (cmsg)))
206

207
/* Returns the cmsghdr's data pointer, but safely cast to the specified type. Does two alignment checks: one
208
 * at compile time, that the requested type has a smaller or same alignment as 'struct cmsghdr', and one
209
 * during runtime, that the actual pointer matches the alignment too. This is supposed to catch cases such as
210
 * 'struct timeval' is embedded into 'struct cmsghdr' on architectures where the alignment of the former is 8
211
 * bytes (because of a 64-bit time_t), but of the latter is 4 bytes (because size_t is 32 bits), such as
212
 * riscv32. */
213
#define CMSG_TYPED_DATA(cmsg, type)                                     \
214
        ({                                                              \
215
                struct cmsghdr *_cmsg = (cmsg);                         \
216
                assert_cc(alignof(type) <= alignof(struct cmsghdr));    \
217
                _cmsg ? CAST_ALIGN_PTR(type, CMSG_DATA(_cmsg)) : (type*) NULL; \
218
        })
219

220
struct cmsghdr* cmsg_find(struct msghdr *mh, int level, int type, socklen_t length);
221
void* cmsg_find_and_copy_data(struct msghdr *mh, int level, int type, void *buf, size_t buf_len);
222

223
/* Type-safe, dereferencing version of cmsg_find() */
224
#define CMSG_FIND_DATA(mh, level, type, ctype)                          \
225
        CMSG_TYPED_DATA(cmsg_find(mh, level, type, CMSG_LEN(sizeof(ctype))), ctype)
226

227
/* Type-safe version of cmsg_find_and_copy_data() */
228
#define CMSG_FIND_AND_COPY_DATA(mh, level, type, ctype)             \
229
        (ctype*) cmsg_find_and_copy_data(mh, level, type, &(ctype){}, sizeof(ctype))
230

231
/* Resolves to a type that can carry cmsghdr structures. Make sure things are properly aligned, i.e. the type
232
 * itself is placed properly in memory and the size is also aligned to what's appropriate for "cmsghdr"
233
 * structures. */
234
#define CMSG_BUFFER_TYPE(size)                                          \
235
        union {                                                         \
236
                struct cmsghdr cmsghdr;                                 \
237
                uint8_t buf[size];                                      \
238
                uint8_t align_check[(size) >= CMSG_SPACE(0) &&          \
239
                                    (size) == CMSG_ALIGN(size) ? 1 : -1]; \
240
        }
241

242
size_t sockaddr_ll_len(const struct sockaddr_ll *sa);
243

244
size_t sockaddr_un_len(const struct sockaddr_un *sa);
245

246
size_t sockaddr_len(const union sockaddr_union *sa);
247

248
int socket_ioctl_fd(void);
249

250
int sockaddr_un_set_path(struct sockaddr_un *ret, const char *path);
251

252
static inline int setsockopt_int(int fd, int level, int optname, int value) {
977,911✔
253
        if (setsockopt(fd, level, optname, &value, sizeof(value)) < 0)
977,911✔
254
                return -errno;
40,809✔
255

256
        return 0;
257
}
258

259
static inline int getsockopt_int(int fd, int level, int optname, int *ret) {
12,744✔
260
        int v;
12,744✔
261
        socklen_t sl = sizeof(v);
12,744✔
262

263
        if (getsockopt(fd, level, optname, &v, &sl) < 0)
12,744✔
264
                return negative_errno();
60✔
265
        if (sl != sizeof(v))
12,684✔
266
                return -EIO;
267

268
        *ret = v;
12,684✔
269
        return 0;
12,684✔
270
}
271

272
int socket_bind_to_ifname(int fd, const char *ifname);
273
int socket_bind_to_ifindex(int fd, int ifindex);
274

275
int socket_autobind(int fd, char **ret_name);
276

277
/* Define a 64-bit version of timeval/timespec in any case, even on 32-bit userspace. */
278
struct timeval_large {
279
        uint64_t tvl_sec, tvl_usec;
280
};
281
struct timespec_large {
282
        uint64_t tvl_sec, tvl_nsec;
283
};
284

285
/* glibc duplicates timespec/timeval on certain 32-bit arches, once in 32-bit and once in 64-bit.
286
 * See __convert_scm_timestamps() in glibc source code. Hence, we need additional buffer space for them
287
 * to prevent truncating control msg (recvmsg() MSG_CTRUNC). */
288
#define CMSG_SPACE_TIMEVAL                                              \
289
        ((sizeof(struct timeval) == sizeof(struct timeval_large)) ?     \
290
         CMSG_SPACE(sizeof(struct timeval)) :                           \
291
         CMSG_SPACE(sizeof(struct timeval)) +                           \
292
         CMSG_SPACE(sizeof(struct timeval_large)))
293
#define CMSG_SPACE_TIMESPEC                                             \
294
        ((sizeof(struct timespec) == sizeof(struct timespec_large)) ?   \
295
         CMSG_SPACE(sizeof(struct timespec)) :                          \
296
         CMSG_SPACE(sizeof(struct timespec)) +                          \
297
         CMSG_SPACE(sizeof(struct timespec_large)))
298

299
ssize_t recvmsg_safe(int sockfd, struct msghdr *msg, int flags);
300

301
int socket_get_family(int fd);
302
int socket_set_recvpktinfo(int fd, int af, bool b);
303
int socket_set_unicast_if(int fd, int af, int ifi);
304

305
int socket_set_option(int fd, int af, int opt_ipv4, int opt_ipv6, int val);
306
static inline int socket_set_recverr(int fd, int af, bool b) {
10,319✔
307
        return socket_set_option(fd, af, IP_RECVERR, IPV6_RECVERR, b);
10,319✔
308
}
309
static inline int socket_set_recvttl(int fd, int af, bool b) {
1,324✔
310
        return socket_set_option(fd, af, IP_RECVTTL, IPV6_RECVHOPLIMIT, b);
1,324✔
311
}
312
static inline int socket_set_ttl(int fd, int af, int ttl) {
1,004✔
313
        return socket_set_option(fd, af, IP_TTL, IPV6_UNICAST_HOPS, ttl);
1,004✔
314
}
315
static inline int socket_set_freebind(int fd, int af, bool b) {
×
UNCOV
316
        return socket_set_option(fd, af, IP_FREEBIND, IPV6_FREEBIND, b);
×
317
}
318
static inline int socket_set_transparent(int fd, int af, bool b) {
×
UNCOV
319
        return socket_set_option(fd, af, IP_TRANSPARENT, IPV6_TRANSPARENT, b);
×
320
}
321
static inline int socket_set_recvfragsize(int fd, int af, bool b) {
10,484✔
322
        return socket_set_option(fd, af, IP_RECVFRAGSIZE, IPV6_RECVFRAGSIZE, b);
10,484✔
323
}
324

325
int socket_get_mtu(int fd, int af, size_t *ret);
326

327
/* an initializer for struct ucred that initialized all fields to the invalid value appropriate for each */
328
#define UCRED_INVALID { .pid = 0, .uid = UID_INVALID, .gid = GID_INVALID }
329

330
int connect_unix_path(int fd, int dir_fd, const char *path);
331

UNCOV
332
static inline bool VSOCK_CID_IS_REGULAR(unsigned cid) {
×
333
        /* 0, 1, 2, UINT32_MAX are special, refuse those */
UNCOV
334
        return cid > 2 && cid < UINT32_MAX;
×
335
}
336

337
int vsock_parse_port(const char *s, unsigned *ret);
338
int vsock_parse_cid(const char *s, unsigned *ret);
339

340
/* Parses AF_UNIX and AF_VSOCK addresses. AF_INET[6] require some netlink calls, so it cannot be in
341
 * src/basic/ and is done from 'socket_local_address from src/shared/. Return -EPROTO in case of
342
 * protocol mismatch. */
343
int socket_address_parse_unix(SocketAddress *ret_address, const char *s);
344
int socket_address_parse_vsock(SocketAddress *ret_address, const char *s);
345

346
/* libc's SOMAXCONN is defined to 128 or 4096 (at least on glibc). But actually, the value can be much
347
 * larger. In our codebase we want to set it to the max usually, since nowadays socket memory is properly
348
 * tracked by memcg, and hence we don't need to enforce extra limits here. Moreover, the kernel caps it to
349
 * /proc/sys/net/core/somaxconn anyway, thus by setting this to unbounded we just make that sysctl file
350
 * authoritative. */
351
#define SOMAXCONN_DELUXE INT_MAX
352

353
int vsock_get_local_cid(unsigned *ret);
354

355
int netlink_socket_get_multicast_groups(int fd, size_t *ret_len, uint32_t **ret_groups);
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc