1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include <netinet/in.h>
16 #include "sd-daemon.h"
18 #include "alloc-util.h"
22 #include "iovec-util.h"
23 #include "parse-util.h"
24 #include "path-util.h"
25 #include "process-util.h"
26 #include "socket-util.h"
27 #include "stat-util.h"
29 #include "time-util.h"
31 #define SNDBUF_SIZE (8*1024*1024)
33 static void unsetenv_all(bool unset_environment
) {
34 if (!unset_environment
)
37 assert_se(unsetenv("LISTEN_PID") == 0);
38 assert_se(unsetenv("LISTEN_FDS") == 0);
39 assert_se(unsetenv("LISTEN_FDNAMES") == 0);
42 _public_
int sd_listen_fds(int unset_environment
) {
47 e
= getenv("LISTEN_PID");
53 r
= parse_pid(e
, &pid
);
58 if (getpid_cached() != pid
) {
63 e
= getenv("LISTEN_FDS");
73 assert_cc(SD_LISTEN_FDS_START
< INT_MAX
);
74 if (n
<= 0 || n
> INT_MAX
- SD_LISTEN_FDS_START
) {
79 for (int fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
80 r
= fd_cloexec(fd
, true);
88 unsetenv_all(unset_environment
);
92 _public_
int sd_listen_fds_with_names(int unset_environment
, char ***names
) {
93 _cleanup_strv_free_
char **l
= NULL
;
95 int n_names
= 0, n_fds
;
100 return sd_listen_fds(unset_environment
);
102 e
= getenv("LISTEN_FDNAMES");
104 n_names
= strv_split_full(&l
, e
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
106 unsetenv_all(unset_environment
);
114 n_fds
= sd_listen_fds(unset_environment
);
119 if (n_names
!= n_fds
)
122 r
= strv_extend_n(&l
, "unknown", n_fds
);
127 *names
= TAKE_PTR(l
);
132 _public_
int sd_is_fifo(int fd
, const char *path
) {
135 assert_return(fd
>= 0, -EBADF
);
137 if (fstat(fd
, &st_fd
) < 0)
140 if (!S_ISFIFO(st_fd
.st_mode
))
146 if (stat(path
, &st_path
) < 0) {
148 if (IN_SET(errno
, ENOENT
, ENOTDIR
))
154 return stat_inode_same(&st_path
, &st_fd
);
160 _public_
int sd_is_special(int fd
, const char *path
) {
163 assert_return(fd
>= 0, -EBADF
);
165 if (fstat(fd
, &st_fd
) < 0)
168 if (!S_ISREG(st_fd
.st_mode
) && !S_ISCHR(st_fd
.st_mode
))
174 if (stat(path
, &st_path
) < 0) {
176 if (IN_SET(errno
, ENOENT
, ENOTDIR
))
182 if (S_ISREG(st_fd
.st_mode
) && S_ISREG(st_path
.st_mode
))
183 return stat_inode_same(&st_path
, &st_fd
);
184 else if (S_ISCHR(st_fd
.st_mode
) && S_ISCHR(st_path
.st_mode
))
185 return st_path
.st_rdev
== st_fd
.st_rdev
;
193 static int is_socket_internal(int fd
, int type
, int listening
) {
196 assert_return(fd
>= 0, -EBADF
);
197 assert_return(type
>= 0, -EINVAL
);
199 if (fstat(fd
, &st_fd
) < 0)
202 if (!S_ISSOCK(st_fd
.st_mode
))
207 socklen_t l
= sizeof(other_type
);
209 if (getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &other_type
, &l
) < 0)
212 if (l
!= sizeof(other_type
))
215 if (other_type
!= type
)
219 if (listening
>= 0) {
221 socklen_t l
= sizeof(accepting
);
223 if (getsockopt(fd
, SOL_SOCKET
, SO_ACCEPTCONN
, &accepting
, &l
) < 0)
226 if (l
!= sizeof(accepting
))
229 if (!accepting
!= !listening
)
236 _public_
int sd_is_socket(int fd
, int family
, int type
, int listening
) {
239 assert_return(fd
>= 0, -EBADF
);
240 assert_return(family
>= 0, -EINVAL
);
242 r
= is_socket_internal(fd
, type
, listening
);
247 union sockaddr_union sockaddr
= {};
248 socklen_t l
= sizeof(sockaddr
);
250 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
253 if (l
< sizeof(sa_family_t
))
256 return sockaddr
.sa
.sa_family
== family
;
262 _public_
int sd_is_socket_inet(int fd
, int family
, int type
, int listening
, uint16_t port
) {
263 union sockaddr_union sockaddr
= {};
264 socklen_t l
= sizeof(sockaddr
);
267 assert_return(fd
>= 0, -EBADF
);
268 assert_return(IN_SET(family
, 0, AF_INET
, AF_INET6
), -EINVAL
);
270 r
= is_socket_internal(fd
, type
, listening
);
274 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
277 if (l
< sizeof(sa_family_t
))
280 if (!IN_SET(sockaddr
.sa
.sa_family
, AF_INET
, AF_INET6
))
284 if (sockaddr
.sa
.sa_family
!= family
)
290 r
= sockaddr_port(&sockaddr
.sa
, &sa_port
);
294 return port
== sa_port
;
300 _public_
int sd_is_socket_sockaddr(int fd
, int type
, const struct sockaddr
* addr
, unsigned addr_len
, int listening
) {
301 union sockaddr_union sockaddr
= {};
302 socklen_t l
= sizeof(sockaddr
);
305 assert_return(fd
>= 0, -EBADF
);
306 assert_return(addr
, -EINVAL
);
307 assert_return(addr_len
>= sizeof(sa_family_t
), -ENOBUFS
);
308 assert_return(IN_SET(addr
->sa_family
, AF_INET
, AF_INET6
), -EPFNOSUPPORT
);
310 r
= is_socket_internal(fd
, type
, listening
);
314 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
317 if (l
< sizeof(sa_family_t
))
320 if (sockaddr
.sa
.sa_family
!= addr
->sa_family
)
323 if (sockaddr
.sa
.sa_family
== AF_INET
) {
324 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) addr
;
326 if (l
< sizeof(struct sockaddr_in
) || addr_len
< sizeof(struct sockaddr_in
))
329 if (in
->sin_port
!= 0 &&
330 sockaddr
.in
.sin_port
!= in
->sin_port
)
333 return sockaddr
.in
.sin_addr
.s_addr
== in
->sin_addr
.s_addr
;
336 const struct sockaddr_in6
*in
= (const struct sockaddr_in6
*) addr
;
338 if (l
< sizeof(struct sockaddr_in6
) || addr_len
< sizeof(struct sockaddr_in6
))
341 if (in
->sin6_port
!= 0 &&
342 sockaddr
.in6
.sin6_port
!= in
->sin6_port
)
345 if (in
->sin6_flowinfo
!= 0 &&
346 sockaddr
.in6
.sin6_flowinfo
!= in
->sin6_flowinfo
)
349 if (in
->sin6_scope_id
!= 0 &&
350 sockaddr
.in6
.sin6_scope_id
!= in
->sin6_scope_id
)
353 return memcmp(sockaddr
.in6
.sin6_addr
.s6_addr
, in
->sin6_addr
.s6_addr
,
354 sizeof(in
->sin6_addr
.s6_addr
)) == 0;
358 _public_
int sd_is_socket_unix(int fd
, int type
, int listening
, const char *path
, size_t length
) {
359 union sockaddr_union sockaddr
= {};
360 socklen_t l
= sizeof(sockaddr
);
363 assert_return(fd
>= 0, -EBADF
);
365 r
= is_socket_internal(fd
, type
, listening
);
369 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
372 if (l
< sizeof(sa_family_t
))
375 if (sockaddr
.sa
.sa_family
!= AF_UNIX
)
380 length
= strlen(path
);
384 return l
== offsetof(struct sockaddr_un
, sun_path
);
387 /* Normal path socket */
389 (l
>= offsetof(struct sockaddr_un
, sun_path
) + length
+ 1) &&
390 memcmp(path
, sockaddr
.un
.sun_path
, length
+1) == 0;
392 /* Abstract namespace socket */
394 (l
== offsetof(struct sockaddr_un
, sun_path
) + length
) &&
395 memcmp(path
, sockaddr
.un
.sun_path
, length
) == 0;
401 _public_
int sd_is_mq(int fd
, const char *path
) {
404 /* Check that the fd is valid */
405 assert_return(fcntl(fd
, F_GETFD
) >= 0, -errno
);
407 if (mq_getattr(fd
, &attr
) < 0) {
409 /* A non-mq fd (or an invalid one, but we ruled that out above) */
415 _cleanup_free_
char *fpath
= NULL
;
418 assert_return(path_is_absolute(path
), -EINVAL
);
420 if (fstat(fd
, &a
) < 0)
423 fpath
= path_join("/dev/mqueue", path
);
427 if (stat(fpath
, &b
) < 0)
430 if (!stat_inode_same(&a
, &b
))
437 static int vsock_bind_privileged_port(int fd
) {
438 union sockaddr_union sa
= {
439 .vm
.svm_family
= AF_VSOCK
,
440 .vm
.svm_cid
= VMADDR_CID_ANY
,
448 r
= RET_NERRNO(bind(fd
, &sa
.sa
, sizeof(sa
.vm
)));
449 while (r
== -EADDRINUSE
&& --sa
.vm
.svm_port
> 0);
454 static int pid_notify_with_fds_internal(
459 SocketAddress address
;
461 struct msghdr msghdr
= {
464 .msg_name
= &address
.sockaddr
,
466 _cleanup_close_
int fd
= -EBADF
;
467 struct cmsghdr
*cmsg
= NULL
;
476 if (n_fds
> 0 && !fds
)
479 e
= getenv("NOTIFY_SOCKET");
483 /* Allow AF_UNIX and AF_VSOCK, reject the rest. */
484 r
= socket_address_parse_unix(&address
, e
);
486 r
= socket_address_parse_vsock(&address
, e
);
489 msghdr
.msg_namelen
= address
.size
;
491 /* If we didn't get an address (which is a normal pattern when specifying VSOCK tuples) error out,
492 * we always require a specific CID. */
493 if (address
.sockaddr
.vm
.svm_family
== AF_VSOCK
&& address
.sockaddr
.vm
.svm_cid
== VMADDR_CID_ANY
)
496 type
= address
.type
== 0 ? SOCK_DGRAM
: address
.type
;
498 /* At the time of writing QEMU does not yet support AF_VSOCK + SOCK_DGRAM and returns
499 * ENODEV. Fallback to SOCK_SEQPACKET in that case. */
500 fd
= socket(address
.sockaddr
.sa
.sa_family
, type
|SOCK_CLOEXEC
, 0);
502 if (!(ERRNO_IS_NOT_SUPPORTED(errno
) || errno
== ENODEV
) || address
.sockaddr
.sa
.sa_family
!= AF_VSOCK
|| address
.type
> 0)
503 return log_debug_errno(errno
, "Failed to open %s notify socket to '%s': %m", socket_address_type_to_string(type
), e
);
505 type
= SOCK_SEQPACKET
;
506 fd
= socket(address
.sockaddr
.sa
.sa_family
, type
|SOCK_CLOEXEC
, 0);
507 if (fd
< 0 && ERRNO_IS_NOT_SUPPORTED(errno
)) {
509 fd
= socket(address
.sockaddr
.sa
.sa_family
, type
|SOCK_CLOEXEC
, 0);
512 return log_debug_errno(errno
, "Failed to open %s socket to '%s': %m", socket_address_type_to_string(type
), e
);
515 if (address
.sockaddr
.sa
.sa_family
== AF_VSOCK
) {
516 r
= vsock_bind_privileged_port(fd
);
517 if (r
< 0 && !ERRNO_IS_PRIVILEGE(r
))
518 return log_debug_errno(r
, "Failed to bind socket to privileged port: %m");
521 if (IN_SET(type
, SOCK_STREAM
, SOCK_SEQPACKET
)) {
522 if (connect(fd
, &address
.sockaddr
.sa
, address
.size
) < 0)
523 return log_debug_errno(errno
, "Failed to connect socket to '%s': %m", e
);
525 msghdr
.msg_name
= NULL
;
526 msghdr
.msg_namelen
= 0;
529 (void) fd_inc_sndbuf(fd
, SNDBUF_SIZE
);
531 iovec
= IOVEC_MAKE_STRING(state
);
534 (pid
!= 0 && pid
!= getpid_cached()) ||
535 getuid() != geteuid() ||
536 getgid() != getegid();
538 if (n_fds
> 0 || send_ucred
) {
539 /* CMSG_SPACE(0) may return value different than zero, which results in miscalculated controllen. */
540 msghdr
.msg_controllen
=
541 (n_fds
> 0 ? CMSG_SPACE(sizeof(int) * n_fds
) : 0) +
542 (send_ucred
? CMSG_SPACE(sizeof(struct ucred
)) : 0);
544 msghdr
.msg_control
= alloca0(msghdr
.msg_controllen
);
546 cmsg
= CMSG_FIRSTHDR(&msghdr
);
548 cmsg
->cmsg_level
= SOL_SOCKET
;
549 cmsg
->cmsg_type
= SCM_RIGHTS
;
550 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int) * n_fds
);
552 memcpy(CMSG_DATA(cmsg
), fds
, sizeof(int) * n_fds
);
555 assert_se(cmsg
= CMSG_NXTHDR(&msghdr
, cmsg
));
561 cmsg
->cmsg_level
= SOL_SOCKET
;
562 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
563 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
565 ucred
= CMSG_TYPED_DATA(cmsg
, struct ucred
);
566 ucred
->pid
= pid
!= 0 ? pid
: getpid_cached();
567 ucred
->uid
= getuid();
568 ucred
->gid
= getgid();
573 /* First try with fake ucred data, as requested */
574 n
= sendmsg(fd
, &msghdr
, MSG_NOSIGNAL
);
577 return log_debug_errno(errno
, "Failed to send notify message to '%s': %m", e
);
579 /* If that failed, try with our own ucred instead */
580 msghdr
.msg_controllen
-= CMSG_SPACE(sizeof(struct ucred
));
581 if (msghdr
.msg_controllen
== 0)
582 msghdr
.msg_control
= NULL
;
587 /* Unless we're using SOCK_STREAM, we expect to write all the contents immediately. */
588 if (type
!= SOCK_STREAM
&& (size_t) n
< iovec_total_size(msghdr
.msg_iov
, msghdr
.msg_iovlen
))
591 /* Make sure we only send fds and ucred once, even if we're using SOCK_STREAM. */
592 msghdr
.msg_control
= NULL
;
593 msghdr
.msg_controllen
= 0;
595 } while (!iovec_increment(msghdr
.msg_iov
, msghdr
.msg_iovlen
, n
));
600 _public_
int sd_pid_notify_with_fds(
602 int unset_environment
,
609 r
= pid_notify_with_fds_internal(pid
, state
, fds
, n_fds
);
611 if (unset_environment
)
612 assert_se(unsetenv("NOTIFY_SOCKET") == 0);
617 _public_
int sd_pid_notify_barrier(pid_t pid
, int unset_environment
, uint64_t timeout
) {
618 _cleanup_close_pair_
int pipe_fd
[2] = EBADF_PAIR
;
621 if (pipe2(pipe_fd
, O_CLOEXEC
) < 0)
624 r
= sd_pid_notify_with_fds(pid
, unset_environment
, "BARRIER=1", &pipe_fd
[1], 1);
628 pipe_fd
[1] = safe_close(pipe_fd
[1]);
630 r
= fd_wait_for_event(pipe_fd
[0], 0 /* POLLHUP is implicit */, timeout
);
639 _public_
int sd_notify_barrier(int unset_environment
, uint64_t timeout
) {
640 return sd_pid_notify_barrier(0, unset_environment
, timeout
);
643 _public_
int sd_pid_notify(pid_t pid
, int unset_environment
, const char *state
) {
644 return sd_pid_notify_with_fds(pid
, unset_environment
, state
, NULL
, 0);
647 _public_
int sd_notify(int unset_environment
, const char *state
) {
648 return sd_pid_notify_with_fds(0, unset_environment
, state
, NULL
, 0);
651 _public_
int sd_pid_notifyf(pid_t pid
, int unset_environment
, const char *format
, ...) {
652 _cleanup_free_
char *p
= NULL
;
658 va_start(ap
, format
);
659 r
= vasprintf(&p
, format
, ap
);
666 return sd_pid_notify(pid
, unset_environment
, p
);
669 _public_
int sd_notifyf(int unset_environment
, const char *format
, ...) {
670 _cleanup_free_
char *p
= NULL
;
676 va_start(ap
, format
);
677 r
= vasprintf(&p
, format
, ap
);
684 return sd_pid_notify(0, unset_environment
, p
);
687 _public_
int sd_pid_notifyf_with_fds(
689 int unset_environment
,
690 const int *fds
, size_t n_fds
,
691 const char *format
, ...) {
693 _cleanup_free_
char *p
= NULL
;
696 /* Paranoia check: we traditionally used 'unsigned' as array size, but we nowadays more correctly use
697 * 'size_t'. sd_pid_notifyf_with_fds() and sd_pid_notify_with_fds() are from different eras, hence
698 * differ in this. Let's catch resulting incompatibilites early, even though they are pretty much
700 if (n_fds
> UINT_MAX
)
706 va_start(ap
, format
);
707 r
= vasprintf(&p
, format
, ap
);
714 return sd_pid_notify_with_fds(pid
, unset_environment
, p
, fds
, n_fds
);
717 _public_
int sd_booted(void) {
718 /* We test whether the runtime unit file directory has been
719 * created. This takes place in mount-setup.c, so is
720 * guaranteed to happen very early during boot. */
722 if (laccess("/run/systemd/system/", F_OK
) >= 0)
731 _public_
int sd_watchdog_enabled(int unset_environment
, uint64_t *usec
) {
732 const char *s
, *p
= ""; /* p is set to dummy value to do unsetting */
736 s
= getenv("WATCHDOG_USEC");
740 r
= safe_atou64(s
, &u
);
743 if (!timestamp_is_set(u
)) {
748 p
= getenv("WATCHDOG_PID");
752 r
= parse_pid(p
, &pid
);
756 /* Is this for us? */
757 if (getpid_cached() != pid
) {
769 if (unset_environment
&& s
)
770 assert_se(unsetenv("WATCHDOG_USEC") == 0);
771 if (unset_environment
&& p
)
772 assert_se(unsetenv("WATCHDOG_PID") == 0);