1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include <netinet/in.h>
14 #include <sys/socket.h>
19 #include "sd-daemon.h"
21 #include "alloc-util.h"
24 #include "parse-util.h"
25 #include "path-util.h"
26 #include "process-util.h"
27 #include "socket-util.h"
31 #define SNDBUF_SIZE (8*1024*1024)
33 static void unsetenv_all(bool unset_environment
) {
35 if (!unset_environment
)
38 unsetenv("LISTEN_PID");
39 unsetenv("LISTEN_FDS");
40 unsetenv("LISTEN_FDNAMES");
43 _public_
int sd_listen_fds(int unset_environment
) {
48 e
= getenv("LISTEN_PID");
54 r
= parse_pid(e
, &pid
);
59 if (getpid_cached() != pid
) {
64 e
= getenv("LISTEN_FDS");
74 assert_cc(SD_LISTEN_FDS_START
< INT_MAX
);
75 if (n
<= 0 || n
> INT_MAX
- SD_LISTEN_FDS_START
) {
80 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
81 r
= fd_cloexec(fd
, true);
89 unsetenv_all(unset_environment
);
93 _public_
int sd_listen_fds_with_names(int unset_environment
, char ***names
) {
94 _cleanup_strv_free_
char **l
= NULL
;
96 int n_names
= 0, n_fds
;
101 return sd_listen_fds(unset_environment
);
103 e
= getenv("LISTEN_FDNAMES");
105 n_names
= strv_split_extract(&l
, e
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
107 unsetenv_all(unset_environment
);
115 n_fds
= sd_listen_fds(unset_environment
);
120 if (n_names
!= n_fds
)
123 r
= strv_extend_n(&l
, "unknown", n_fds
);
128 *names
= TAKE_PTR(l
);
133 _public_
int sd_is_fifo(int fd
, const char *path
) {
136 assert_return(fd
>= 0, -EBADF
);
138 if (fstat(fd
, &st_fd
) < 0)
141 if (!S_ISFIFO(st_fd
.st_mode
))
147 if (stat(path
, &st_path
) < 0) {
149 if (IN_SET(errno
, ENOENT
, ENOTDIR
))
156 st_path
.st_dev
== st_fd
.st_dev
&&
157 st_path
.st_ino
== st_fd
.st_ino
;
163 _public_
int sd_is_special(int fd
, const char *path
) {
166 assert_return(fd
>= 0, -EBADF
);
168 if (fstat(fd
, &st_fd
) < 0)
171 if (!S_ISREG(st_fd
.st_mode
) && !S_ISCHR(st_fd
.st_mode
))
177 if (stat(path
, &st_path
) < 0) {
179 if (IN_SET(errno
, ENOENT
, ENOTDIR
))
185 if (S_ISREG(st_fd
.st_mode
) && S_ISREG(st_path
.st_mode
))
187 st_path
.st_dev
== st_fd
.st_dev
&&
188 st_path
.st_ino
== st_fd
.st_ino
;
189 else if (S_ISCHR(st_fd
.st_mode
) && S_ISCHR(st_path
.st_mode
))
190 return st_path
.st_rdev
== st_fd
.st_rdev
;
198 static int sd_is_socket_internal(int fd
, int type
, int listening
) {
201 assert_return(fd
>= 0, -EBADF
);
202 assert_return(type
>= 0, -EINVAL
);
204 if (fstat(fd
, &st_fd
) < 0)
207 if (!S_ISSOCK(st_fd
.st_mode
))
212 socklen_t l
= sizeof(other_type
);
214 if (getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &other_type
, &l
) < 0)
217 if (l
!= sizeof(other_type
))
220 if (other_type
!= type
)
224 if (listening
>= 0) {
226 socklen_t l
= sizeof(accepting
);
228 if (getsockopt(fd
, SOL_SOCKET
, SO_ACCEPTCONN
, &accepting
, &l
) < 0)
231 if (l
!= sizeof(accepting
))
234 if (!accepting
!= !listening
)
241 _public_
int sd_is_socket(int fd
, int family
, int type
, int listening
) {
244 assert_return(fd
>= 0, -EBADF
);
245 assert_return(family
>= 0, -EINVAL
);
247 r
= sd_is_socket_internal(fd
, type
, listening
);
252 union sockaddr_union sockaddr
= {};
253 socklen_t l
= sizeof(sockaddr
);
255 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
258 if (l
< sizeof(sa_family_t
))
261 return sockaddr
.sa
.sa_family
== family
;
267 _public_
int sd_is_socket_inet(int fd
, int family
, int type
, int listening
, uint16_t port
) {
268 union sockaddr_union sockaddr
= {};
269 socklen_t l
= sizeof(sockaddr
);
272 assert_return(fd
>= 0, -EBADF
);
273 assert_return(IN_SET(family
, 0, AF_INET
, AF_INET6
), -EINVAL
);
275 r
= sd_is_socket_internal(fd
, type
, listening
);
279 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
282 if (l
< sizeof(sa_family_t
))
285 if (!IN_SET(sockaddr
.sa
.sa_family
, AF_INET
, AF_INET6
))
289 if (sockaddr
.sa
.sa_family
!= family
)
295 r
= sockaddr_port(&sockaddr
.sa
, &sa_port
);
299 return port
== sa_port
;
305 _public_
int sd_is_socket_sockaddr(int fd
, int type
, const struct sockaddr
* addr
, unsigned addr_len
, int listening
) {
306 union sockaddr_union sockaddr
= {};
307 socklen_t l
= sizeof(sockaddr
);
310 assert_return(fd
>= 0, -EBADF
);
311 assert_return(addr
, -EINVAL
);
312 assert_return(addr_len
>= sizeof(sa_family_t
), -ENOBUFS
);
313 assert_return(IN_SET(addr
->sa_family
, AF_INET
, AF_INET6
), -EPFNOSUPPORT
);
315 r
= sd_is_socket_internal(fd
, type
, listening
);
319 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
322 if (l
< sizeof(sa_family_t
))
325 if (sockaddr
.sa
.sa_family
!= addr
->sa_family
)
328 if (sockaddr
.sa
.sa_family
== AF_INET
) {
329 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) addr
;
331 if (l
< sizeof(struct sockaddr_in
) || addr_len
< sizeof(struct sockaddr_in
))
334 if (in
->sin_port
!= 0 &&
335 sockaddr
.in
.sin_port
!= in
->sin_port
)
338 return sockaddr
.in
.sin_addr
.s_addr
== in
->sin_addr
.s_addr
;
341 const struct sockaddr_in6
*in
= (const struct sockaddr_in6
*) addr
;
343 if (l
< sizeof(struct sockaddr_in6
) || addr_len
< sizeof(struct sockaddr_in6
))
346 if (in
->sin6_port
!= 0 &&
347 sockaddr
.in6
.sin6_port
!= in
->sin6_port
)
350 if (in
->sin6_flowinfo
!= 0 &&
351 sockaddr
.in6
.sin6_flowinfo
!= in
->sin6_flowinfo
)
354 if (in
->sin6_scope_id
!= 0 &&
355 sockaddr
.in6
.sin6_scope_id
!= in
->sin6_scope_id
)
358 return memcmp(sockaddr
.in6
.sin6_addr
.s6_addr
, in
->sin6_addr
.s6_addr
,
359 sizeof(in
->sin6_addr
.s6_addr
)) == 0;
363 _public_
int sd_is_socket_unix(int fd
, int type
, int listening
, const char *path
, size_t length
) {
364 union sockaddr_union sockaddr
= {};
365 socklen_t l
= sizeof(sockaddr
);
368 assert_return(fd
>= 0, -EBADF
);
370 r
= sd_is_socket_internal(fd
, type
, listening
);
374 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
377 if (l
< sizeof(sa_family_t
))
380 if (sockaddr
.sa
.sa_family
!= AF_UNIX
)
385 length
= strlen(path
);
389 return l
== offsetof(struct sockaddr_un
, sun_path
);
392 /* Normal path socket */
394 (l
>= offsetof(struct sockaddr_un
, sun_path
) + length
+ 1) &&
395 memcmp(path
, sockaddr
.un
.sun_path
, length
+1) == 0;
397 /* Abstract namespace socket */
399 (l
== offsetof(struct sockaddr_un
, sun_path
) + length
) &&
400 memcmp(path
, sockaddr
.un
.sun_path
, length
) == 0;
406 _public_
int sd_is_mq(int fd
, const char *path
) {
409 /* Check that the fd is valid */
410 assert_return(fcntl(fd
, F_GETFD
) >= 0, -errno
);
412 if (mq_getattr(fd
, &attr
) < 0) {
414 /* A non-mq fd (or an invalid one, but we ruled that out above) */
420 char fpath
[PATH_MAX
];
423 assert_return(path_is_absolute(path
), -EINVAL
);
425 if (fstat(fd
, &a
) < 0)
428 strncpy(stpcpy(fpath
, "/dev/mqueue"), path
, sizeof(fpath
) - 12);
429 fpath
[sizeof(fpath
)-1] = 0;
431 if (stat(fpath
, &b
) < 0)
434 if (a
.st_dev
!= b
.st_dev
||
435 a
.st_ino
!= b
.st_ino
)
442 _public_
int sd_pid_notify_with_fds(
444 int unset_environment
,
449 union sockaddr_union sockaddr
= {
450 .sa
.sa_family
= AF_UNIX
,
452 struct iovec iovec
= {
453 .iov_base
= (char*) state
,
455 struct msghdr msghdr
= {
458 .msg_name
= &sockaddr
,
460 _cleanup_close_
int fd
= -1;
461 struct cmsghdr
*cmsg
= NULL
;
471 if (n_fds
> 0 && !fds
) {
476 e
= getenv("NOTIFY_SOCKET");
480 /* Must be an abstract socket, or an absolute path */
481 if (!IN_SET(e
[0], '@', '/') || e
[1] == 0) {
486 if (strlen(e
) > sizeof(sockaddr
.un
.sun_path
)) {
491 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0);
497 (void) fd_inc_sndbuf(fd
, SNDBUF_SIZE
);
499 iovec
.iov_len
= strlen(state
);
501 strncpy(sockaddr
.un
.sun_path
, e
, sizeof(sockaddr
.un
.sun_path
));
502 if (sockaddr
.un
.sun_path
[0] == '@')
503 sockaddr
.un
.sun_path
[0] = 0;
505 msghdr
.msg_namelen
= SOCKADDR_UN_LEN(sockaddr
.un
);
508 (pid
!= 0 && pid
!= getpid_cached()) ||
509 getuid() != geteuid() ||
510 getgid() != getegid();
512 if (n_fds
> 0 || send_ucred
) {
513 /* CMSG_SPACE(0) may return value different than zero, which results in miscalculated controllen. */
514 msghdr
.msg_controllen
=
515 (n_fds
> 0 ? CMSG_SPACE(sizeof(int) * n_fds
) : 0) +
516 (send_ucred
? CMSG_SPACE(sizeof(struct ucred
)) : 0);
518 msghdr
.msg_control
= alloca0(msghdr
.msg_controllen
);
520 cmsg
= CMSG_FIRSTHDR(&msghdr
);
522 cmsg
->cmsg_level
= SOL_SOCKET
;
523 cmsg
->cmsg_type
= SCM_RIGHTS
;
524 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int) * n_fds
);
526 memcpy(CMSG_DATA(cmsg
), fds
, sizeof(int) * n_fds
);
529 assert_se(cmsg
= CMSG_NXTHDR(&msghdr
, cmsg
));
535 cmsg
->cmsg_level
= SOL_SOCKET
;
536 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
537 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
539 ucred
= (struct ucred
*) CMSG_DATA(cmsg
);
540 ucred
->pid
= pid
!= 0 ? pid
: getpid_cached();
541 ucred
->uid
= getuid();
542 ucred
->gid
= getgid();
546 /* First try with fake ucred data, as requested */
547 if (sendmsg(fd
, &msghdr
, MSG_NOSIGNAL
) >= 0) {
552 /* If that failed, try with our own ucred instead */
554 msghdr
.msg_controllen
-= CMSG_SPACE(sizeof(struct ucred
));
555 if (msghdr
.msg_controllen
== 0)
556 msghdr
.msg_control
= NULL
;
558 if (sendmsg(fd
, &msghdr
, MSG_NOSIGNAL
) >= 0) {
567 if (unset_environment
)
568 unsetenv("NOTIFY_SOCKET");
573 _public_
int sd_pid_notify(pid_t pid
, int unset_environment
, const char *state
) {
574 return sd_pid_notify_with_fds(pid
, unset_environment
, state
, NULL
, 0);
577 _public_
int sd_notify(int unset_environment
, const char *state
) {
578 return sd_pid_notify_with_fds(0, unset_environment
, state
, NULL
, 0);
581 _public_
int sd_pid_notifyf(pid_t pid
, int unset_environment
, const char *format
, ...) {
582 _cleanup_free_
char *p
= NULL
;
588 va_start(ap
, format
);
589 r
= vasprintf(&p
, format
, ap
);
596 return sd_pid_notify(pid
, unset_environment
, p
);
599 _public_
int sd_notifyf(int unset_environment
, const char *format
, ...) {
600 _cleanup_free_
char *p
= NULL
;
606 va_start(ap
, format
);
607 r
= vasprintf(&p
, format
, ap
);
614 return sd_pid_notify(0, unset_environment
, p
);
617 _public_
int sd_booted(void) {
618 /* We test whether the runtime unit file directory has been
619 * created. This takes place in mount-setup.c, so is
620 * guaranteed to happen very early during boot. */
622 return laccess("/run/systemd/system/", F_OK
) >= 0;
625 _public_
int sd_watchdog_enabled(int unset_environment
, uint64_t *usec
) {
626 const char *s
, *p
= ""; /* p is set to dummy value to do unsetting */
630 s
= getenv("WATCHDOG_USEC");
634 r
= safe_atou64(s
, &u
);
637 if (u
<= 0 || u
>= USEC_INFINITY
) {
642 p
= getenv("WATCHDOG_PID");
646 r
= parse_pid(p
, &pid
);
650 /* Is this for us? */
651 if (getpid_cached() != pid
) {
663 if (unset_environment
&& s
)
664 unsetenv("WATCHDOG_USEC");
665 if (unset_environment
&& p
)
666 unsetenv("WATCHDOG_PID");