1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include <netinet/in.h>
12 #include <sys/socket.h>
17 #include "sd-daemon.h"
19 #include "alloc-util.h"
23 #include "parse-util.h"
24 #include "path-util.h"
25 #include "process-util.h"
26 #include "socket-util.h"
30 #define SNDBUF_SIZE (8*1024*1024)
32 static void unsetenv_all(bool unset_environment
) {
34 if (!unset_environment
)
37 unsetenv("LISTEN_PID");
38 unsetenv("LISTEN_FDS");
39 unsetenv("LISTEN_FDNAMES");
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 (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_extract(&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
))
155 st_path
.st_dev
== st_fd
.st_dev
&&
156 st_path
.st_ino
== st_fd
.st_ino
;
162 _public_
int sd_is_special(int fd
, const char *path
) {
165 assert_return(fd
>= 0, -EBADF
);
167 if (fstat(fd
, &st_fd
) < 0)
170 if (!S_ISREG(st_fd
.st_mode
) && !S_ISCHR(st_fd
.st_mode
))
176 if (stat(path
, &st_path
) < 0) {
178 if (IN_SET(errno
, ENOENT
, ENOTDIR
))
184 if (S_ISREG(st_fd
.st_mode
) && S_ISREG(st_path
.st_mode
))
186 st_path
.st_dev
== st_fd
.st_dev
&&
187 st_path
.st_ino
== st_fd
.st_ino
;
188 else if (S_ISCHR(st_fd
.st_mode
) && S_ISCHR(st_path
.st_mode
))
189 return st_path
.st_rdev
== st_fd
.st_rdev
;
197 static int sd_is_socket_internal(int fd
, int type
, int listening
) {
200 assert_return(fd
>= 0, -EBADF
);
201 assert_return(type
>= 0, -EINVAL
);
203 if (fstat(fd
, &st_fd
) < 0)
206 if (!S_ISSOCK(st_fd
.st_mode
))
211 socklen_t l
= sizeof(other_type
);
213 if (getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &other_type
, &l
) < 0)
216 if (l
!= sizeof(other_type
))
219 if (other_type
!= type
)
223 if (listening
>= 0) {
225 socklen_t l
= sizeof(accepting
);
227 if (getsockopt(fd
, SOL_SOCKET
, SO_ACCEPTCONN
, &accepting
, &l
) < 0)
230 if (l
!= sizeof(accepting
))
233 if (!accepting
!= !listening
)
240 _public_
int sd_is_socket(int fd
, int family
, int type
, int listening
) {
243 assert_return(fd
>= 0, -EBADF
);
244 assert_return(family
>= 0, -EINVAL
);
246 r
= sd_is_socket_internal(fd
, type
, listening
);
251 union sockaddr_union sockaddr
= {};
252 socklen_t l
= sizeof(sockaddr
);
254 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
257 if (l
< sizeof(sa_family_t
))
260 return sockaddr
.sa
.sa_family
== family
;
266 _public_
int sd_is_socket_inet(int fd
, int family
, int type
, int listening
, uint16_t port
) {
267 union sockaddr_union sockaddr
= {};
268 socklen_t l
= sizeof(sockaddr
);
271 assert_return(fd
>= 0, -EBADF
);
272 assert_return(IN_SET(family
, 0, AF_INET
, AF_INET6
), -EINVAL
);
274 r
= sd_is_socket_internal(fd
, type
, listening
);
278 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
281 if (l
< sizeof(sa_family_t
))
284 if (!IN_SET(sockaddr
.sa
.sa_family
, AF_INET
, AF_INET6
))
288 if (sockaddr
.sa
.sa_family
!= family
)
294 r
= sockaddr_port(&sockaddr
.sa
, &sa_port
);
298 return port
== sa_port
;
304 _public_
int sd_is_socket_sockaddr(int fd
, int type
, const struct sockaddr
* addr
, unsigned addr_len
, int listening
) {
305 union sockaddr_union sockaddr
= {};
306 socklen_t l
= sizeof(sockaddr
);
309 assert_return(fd
>= 0, -EBADF
);
310 assert_return(addr
, -EINVAL
);
311 assert_return(addr_len
>= sizeof(sa_family_t
), -ENOBUFS
);
312 assert_return(IN_SET(addr
->sa_family
, AF_INET
, AF_INET6
), -EPFNOSUPPORT
);
314 r
= sd_is_socket_internal(fd
, type
, listening
);
318 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
321 if (l
< sizeof(sa_family_t
))
324 if (sockaddr
.sa
.sa_family
!= addr
->sa_family
)
327 if (sockaddr
.sa
.sa_family
== AF_INET
) {
328 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) addr
;
330 if (l
< sizeof(struct sockaddr_in
) || addr_len
< sizeof(struct sockaddr_in
))
333 if (in
->sin_port
!= 0 &&
334 sockaddr
.in
.sin_port
!= in
->sin_port
)
337 return sockaddr
.in
.sin_addr
.s_addr
== in
->sin_addr
.s_addr
;
340 const struct sockaddr_in6
*in
= (const struct sockaddr_in6
*) addr
;
342 if (l
< sizeof(struct sockaddr_in6
) || addr_len
< sizeof(struct sockaddr_in6
))
345 if (in
->sin6_port
!= 0 &&
346 sockaddr
.in6
.sin6_port
!= in
->sin6_port
)
349 if (in
->sin6_flowinfo
!= 0 &&
350 sockaddr
.in6
.sin6_flowinfo
!= in
->sin6_flowinfo
)
353 if (in
->sin6_scope_id
!= 0 &&
354 sockaddr
.in6
.sin6_scope_id
!= in
->sin6_scope_id
)
357 return memcmp(sockaddr
.in6
.sin6_addr
.s6_addr
, in
->sin6_addr
.s6_addr
,
358 sizeof(in
->sin6_addr
.s6_addr
)) == 0;
362 _public_
int sd_is_socket_unix(int fd
, int type
, int listening
, const char *path
, size_t length
) {
363 union sockaddr_union sockaddr
= {};
364 socklen_t l
= sizeof(sockaddr
);
367 assert_return(fd
>= 0, -EBADF
);
369 r
= sd_is_socket_internal(fd
, type
, listening
);
373 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
376 if (l
< sizeof(sa_family_t
))
379 if (sockaddr
.sa
.sa_family
!= AF_UNIX
)
384 length
= strlen(path
);
388 return l
== offsetof(struct sockaddr_un
, sun_path
);
391 /* Normal path socket */
393 (l
>= offsetof(struct sockaddr_un
, sun_path
) + length
+ 1) &&
394 memcmp(path
, sockaddr
.un
.sun_path
, length
+1) == 0;
396 /* Abstract namespace socket */
398 (l
== offsetof(struct sockaddr_un
, sun_path
) + length
) &&
399 memcmp(path
, sockaddr
.un
.sun_path
, length
) == 0;
405 _public_
int sd_is_mq(int fd
, const char *path
) {
408 /* Check that the fd is valid */
409 assert_return(fcntl(fd
, F_GETFD
) >= 0, -errno
);
411 if (mq_getattr(fd
, &attr
) < 0) {
413 /* A non-mq fd (or an invalid one, but we ruled that out above) */
419 char fpath
[PATH_MAX
];
422 assert_return(path_is_absolute(path
), -EINVAL
);
424 if (fstat(fd
, &a
) < 0)
427 strncpy(stpcpy(fpath
, "/dev/mqueue"), path
, sizeof(fpath
) - 12);
428 fpath
[sizeof(fpath
)-1] = 0;
430 if (stat(fpath
, &b
) < 0)
433 if (a
.st_dev
!= b
.st_dev
||
434 a
.st_ino
!= b
.st_ino
)
441 _public_
int sd_pid_notify_with_fds(
443 int unset_environment
,
448 union sockaddr_union sockaddr
= {};
450 struct msghdr msghdr
= {
453 .msg_name
= &sockaddr
,
455 _cleanup_close_
int fd
= -1;
456 struct cmsghdr
*cmsg
= NULL
;
466 if (n_fds
> 0 && !fds
) {
471 e
= getenv("NOTIFY_SOCKET");
475 salen
= sockaddr_un_set_path(&sockaddr
.un
, e
);
481 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0);
487 (void) fd_inc_sndbuf(fd
, SNDBUF_SIZE
);
489 iovec
= IOVEC_MAKE_STRING(state
);
490 msghdr
.msg_namelen
= salen
;
493 (pid
!= 0 && pid
!= getpid_cached()) ||
494 getuid() != geteuid() ||
495 getgid() != getegid();
497 if (n_fds
> 0 || send_ucred
) {
498 /* CMSG_SPACE(0) may return value different than zero, which results in miscalculated controllen. */
499 msghdr
.msg_controllen
=
500 (n_fds
> 0 ? CMSG_SPACE(sizeof(int) * n_fds
) : 0) +
501 (send_ucred
? CMSG_SPACE(sizeof(struct ucred
)) : 0);
503 msghdr
.msg_control
= alloca0(msghdr
.msg_controllen
);
505 cmsg
= CMSG_FIRSTHDR(&msghdr
);
507 cmsg
->cmsg_level
= SOL_SOCKET
;
508 cmsg
->cmsg_type
= SCM_RIGHTS
;
509 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int) * n_fds
);
511 memcpy(CMSG_DATA(cmsg
), fds
, sizeof(int) * n_fds
);
514 assert_se(cmsg
= CMSG_NXTHDR(&msghdr
, cmsg
));
520 cmsg
->cmsg_level
= SOL_SOCKET
;
521 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
522 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
524 ucred
= (struct ucred
*) CMSG_DATA(cmsg
);
525 ucred
->pid
= pid
!= 0 ? pid
: getpid_cached();
526 ucred
->uid
= getuid();
527 ucred
->gid
= getgid();
531 /* First try with fake ucred data, as requested */
532 if (sendmsg(fd
, &msghdr
, MSG_NOSIGNAL
) >= 0) {
537 /* If that failed, try with our own ucred instead */
539 msghdr
.msg_controllen
-= CMSG_SPACE(sizeof(struct ucred
));
540 if (msghdr
.msg_controllen
== 0)
541 msghdr
.msg_control
= NULL
;
543 if (sendmsg(fd
, &msghdr
, MSG_NOSIGNAL
) >= 0) {
552 if (unset_environment
)
553 unsetenv("NOTIFY_SOCKET");
558 _public_
int sd_pid_notify(pid_t pid
, int unset_environment
, const char *state
) {
559 return sd_pid_notify_with_fds(pid
, unset_environment
, state
, NULL
, 0);
562 _public_
int sd_notify(int unset_environment
, const char *state
) {
563 return sd_pid_notify_with_fds(0, unset_environment
, state
, NULL
, 0);
566 _public_
int sd_pid_notifyf(pid_t pid
, int unset_environment
, const char *format
, ...) {
567 _cleanup_free_
char *p
= NULL
;
573 va_start(ap
, format
);
574 r
= vasprintf(&p
, format
, ap
);
581 return sd_pid_notify(pid
, unset_environment
, p
);
584 _public_
int sd_notifyf(int unset_environment
, const char *format
, ...) {
585 _cleanup_free_
char *p
= NULL
;
591 va_start(ap
, format
);
592 r
= vasprintf(&p
, format
, ap
);
599 return sd_pid_notify(0, unset_environment
, p
);
602 _public_
int sd_booted(void) {
603 /* We test whether the runtime unit file directory has been
604 * created. This takes place in mount-setup.c, so is
605 * guaranteed to happen very early during boot. */
607 if (laccess("/run/systemd/system/", F_OK
) >= 0)
616 _public_
int sd_watchdog_enabled(int unset_environment
, uint64_t *usec
) {
617 const char *s
, *p
= ""; /* p is set to dummy value to do unsetting */
621 s
= getenv("WATCHDOG_USEC");
625 r
= safe_atou64(s
, &u
);
628 if (u
<= 0 || u
>= USEC_INFINITY
) {
633 p
= getenv("WATCHDOG_PID");
637 r
= parse_pid(p
, &pid
);
641 /* Is this for us? */
642 if (getpid_cached() != pid
) {
654 if (unset_environment
&& s
)
655 unsetenv("WATCHDOG_USEC");
656 if (unset_environment
&& p
)
657 unsetenv("WATCHDOG_PID");