1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include <netinet/in.h>
15 #include "sd-daemon.h"
17 #include "alloc-util.h"
21 #include "parse-util.h"
22 #include "path-util.h"
23 #include "process-util.h"
24 #include "socket-util.h"
28 #define SNDBUF_SIZE (8*1024*1024)
30 static void unsetenv_all(bool unset_environment
) {
32 if (!unset_environment
)
35 unsetenv("LISTEN_PID");
36 unsetenv("LISTEN_FDS");
37 unsetenv("LISTEN_FDNAMES");
40 _public_
int sd_listen_fds(int unset_environment
) {
45 e
= getenv("LISTEN_PID");
51 r
= parse_pid(e
, &pid
);
56 if (getpid_cached() != pid
) {
61 e
= getenv("LISTEN_FDS");
71 assert_cc(SD_LISTEN_FDS_START
< INT_MAX
);
72 if (n
<= 0 || n
> INT_MAX
- SD_LISTEN_FDS_START
) {
77 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
78 r
= fd_cloexec(fd
, true);
86 unsetenv_all(unset_environment
);
90 _public_
int sd_listen_fds_with_names(int unset_environment
, char ***names
) {
91 _cleanup_strv_free_
char **l
= NULL
;
93 int n_names
= 0, n_fds
;
98 return sd_listen_fds(unset_environment
);
100 e
= getenv("LISTEN_FDNAMES");
102 n_names
= strv_split_extract(&l
, e
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
104 unsetenv_all(unset_environment
);
112 n_fds
= sd_listen_fds(unset_environment
);
117 if (n_names
!= n_fds
)
120 r
= strv_extend_n(&l
, "unknown", n_fds
);
125 *names
= TAKE_PTR(l
);
130 _public_
int sd_is_fifo(int fd
, const char *path
) {
133 assert_return(fd
>= 0, -EBADF
);
135 if (fstat(fd
, &st_fd
) < 0)
138 if (!S_ISFIFO(st_fd
.st_mode
))
144 if (stat(path
, &st_path
) < 0) {
146 if (IN_SET(errno
, ENOENT
, ENOTDIR
))
153 st_path
.st_dev
== st_fd
.st_dev
&&
154 st_path
.st_ino
== st_fd
.st_ino
;
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
))
184 st_path
.st_dev
== st_fd
.st_dev
&&
185 st_path
.st_ino
== st_fd
.st_ino
;
186 else if (S_ISCHR(st_fd
.st_mode
) && S_ISCHR(st_path
.st_mode
))
187 return st_path
.st_rdev
== st_fd
.st_rdev
;
195 static int sd_is_socket_internal(int fd
, int type
, int listening
) {
198 assert_return(fd
>= 0, -EBADF
);
199 assert_return(type
>= 0, -EINVAL
);
201 if (fstat(fd
, &st_fd
) < 0)
204 if (!S_ISSOCK(st_fd
.st_mode
))
209 socklen_t l
= sizeof(other_type
);
211 if (getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &other_type
, &l
) < 0)
214 if (l
!= sizeof(other_type
))
217 if (other_type
!= type
)
221 if (listening
>= 0) {
223 socklen_t l
= sizeof(accepting
);
225 if (getsockopt(fd
, SOL_SOCKET
, SO_ACCEPTCONN
, &accepting
, &l
) < 0)
228 if (l
!= sizeof(accepting
))
231 if (!accepting
!= !listening
)
238 _public_
int sd_is_socket(int fd
, int family
, int type
, int listening
) {
241 assert_return(fd
>= 0, -EBADF
);
242 assert_return(family
>= 0, -EINVAL
);
244 r
= sd_is_socket_internal(fd
, type
, listening
);
249 union sockaddr_union sockaddr
= {};
250 socklen_t l
= sizeof(sockaddr
);
252 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
255 if (l
< sizeof(sa_family_t
))
258 return sockaddr
.sa
.sa_family
== family
;
264 _public_
int sd_is_socket_inet(int fd
, int family
, int type
, int listening
, uint16_t port
) {
265 union sockaddr_union sockaddr
= {};
266 socklen_t l
= sizeof(sockaddr
);
269 assert_return(fd
>= 0, -EBADF
);
270 assert_return(IN_SET(family
, 0, AF_INET
, AF_INET6
), -EINVAL
);
272 r
= sd_is_socket_internal(fd
, type
, listening
);
276 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
279 if (l
< sizeof(sa_family_t
))
282 if (!IN_SET(sockaddr
.sa
.sa_family
, AF_INET
, AF_INET6
))
286 if (sockaddr
.sa
.sa_family
!= family
)
292 r
= sockaddr_port(&sockaddr
.sa
, &sa_port
);
296 return port
== sa_port
;
302 _public_
int sd_is_socket_sockaddr(int fd
, int type
, const struct sockaddr
* addr
, unsigned addr_len
, int listening
) {
303 union sockaddr_union sockaddr
= {};
304 socklen_t l
= sizeof(sockaddr
);
307 assert_return(fd
>= 0, -EBADF
);
308 assert_return(addr
, -EINVAL
);
309 assert_return(addr_len
>= sizeof(sa_family_t
), -ENOBUFS
);
310 assert_return(IN_SET(addr
->sa_family
, AF_INET
, AF_INET6
), -EPFNOSUPPORT
);
312 r
= sd_is_socket_internal(fd
, type
, listening
);
316 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
319 if (l
< sizeof(sa_family_t
))
322 if (sockaddr
.sa
.sa_family
!= addr
->sa_family
)
325 if (sockaddr
.sa
.sa_family
== AF_INET
) {
326 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) addr
;
328 if (l
< sizeof(struct sockaddr_in
) || addr_len
< sizeof(struct sockaddr_in
))
331 if (in
->sin_port
!= 0 &&
332 sockaddr
.in
.sin_port
!= in
->sin_port
)
335 return sockaddr
.in
.sin_addr
.s_addr
== in
->sin_addr
.s_addr
;
338 const struct sockaddr_in6
*in
= (const struct sockaddr_in6
*) addr
;
340 if (l
< sizeof(struct sockaddr_in6
) || addr_len
< sizeof(struct sockaddr_in6
))
343 if (in
->sin6_port
!= 0 &&
344 sockaddr
.in6
.sin6_port
!= in
->sin6_port
)
347 if (in
->sin6_flowinfo
!= 0 &&
348 sockaddr
.in6
.sin6_flowinfo
!= in
->sin6_flowinfo
)
351 if (in
->sin6_scope_id
!= 0 &&
352 sockaddr
.in6
.sin6_scope_id
!= in
->sin6_scope_id
)
355 return memcmp(sockaddr
.in6
.sin6_addr
.s6_addr
, in
->sin6_addr
.s6_addr
,
356 sizeof(in
->sin6_addr
.s6_addr
)) == 0;
360 _public_
int sd_is_socket_unix(int fd
, int type
, int listening
, const char *path
, size_t length
) {
361 union sockaddr_union sockaddr
= {};
362 socklen_t l
= sizeof(sockaddr
);
365 assert_return(fd
>= 0, -EBADF
);
367 r
= sd_is_socket_internal(fd
, type
, listening
);
371 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
374 if (l
< sizeof(sa_family_t
))
377 if (sockaddr
.sa
.sa_family
!= AF_UNIX
)
382 length
= strlen(path
);
386 return l
== offsetof(struct sockaddr_un
, sun_path
);
389 /* Normal path socket */
391 (l
>= offsetof(struct sockaddr_un
, sun_path
) + length
+ 1) &&
392 memcmp(path
, sockaddr
.un
.sun_path
, length
+1) == 0;
394 /* Abstract namespace socket */
396 (l
== offsetof(struct sockaddr_un
, sun_path
) + length
) &&
397 memcmp(path
, sockaddr
.un
.sun_path
, length
) == 0;
403 _public_
int sd_is_mq(int fd
, const char *path
) {
406 /* Check that the fd is valid */
407 assert_return(fcntl(fd
, F_GETFD
) >= 0, -errno
);
409 if (mq_getattr(fd
, &attr
) < 0) {
411 /* A non-mq fd (or an invalid one, but we ruled that out above) */
417 char fpath
[PATH_MAX
];
420 assert_return(path_is_absolute(path
), -EINVAL
);
422 if (fstat(fd
, &a
) < 0)
425 strncpy(stpcpy(fpath
, "/dev/mqueue"), path
, sizeof(fpath
) - 12);
426 fpath
[sizeof(fpath
)-1] = 0;
428 if (stat(fpath
, &b
) < 0)
431 if (a
.st_dev
!= b
.st_dev
||
432 a
.st_ino
!= b
.st_ino
)
439 _public_
int sd_pid_notify_with_fds(
441 int unset_environment
,
446 union sockaddr_union sockaddr
;
448 struct msghdr msghdr
= {
451 .msg_name
= &sockaddr
,
453 _cleanup_close_
int fd
= -1;
454 struct cmsghdr
*cmsg
= NULL
;
464 if (n_fds
> 0 && !fds
) {
469 e
= getenv("NOTIFY_SOCKET");
473 r
= sockaddr_un_set_path(&sockaddr
.un
, e
);
476 msghdr
.msg_namelen
= r
;
478 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0);
484 (void) fd_inc_sndbuf(fd
, SNDBUF_SIZE
);
486 iovec
= IOVEC_MAKE_STRING(state
);
489 (pid
!= 0 && pid
!= getpid_cached()) ||
490 getuid() != geteuid() ||
491 getgid() != getegid();
493 if (n_fds
> 0 || send_ucred
) {
494 /* CMSG_SPACE(0) may return value different than zero, which results in miscalculated controllen. */
495 msghdr
.msg_controllen
=
496 (n_fds
> 0 ? CMSG_SPACE(sizeof(int) * n_fds
) : 0) +
497 (send_ucred
? CMSG_SPACE(sizeof(struct ucred
)) : 0);
499 msghdr
.msg_control
= alloca0(msghdr
.msg_controllen
);
501 cmsg
= CMSG_FIRSTHDR(&msghdr
);
503 cmsg
->cmsg_level
= SOL_SOCKET
;
504 cmsg
->cmsg_type
= SCM_RIGHTS
;
505 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int) * n_fds
);
507 memcpy(CMSG_DATA(cmsg
), fds
, sizeof(int) * n_fds
);
510 assert_se(cmsg
= CMSG_NXTHDR(&msghdr
, cmsg
));
516 cmsg
->cmsg_level
= SOL_SOCKET
;
517 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
518 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
520 ucred
= (struct ucred
*) CMSG_DATA(cmsg
);
521 ucred
->pid
= pid
!= 0 ? pid
: getpid_cached();
522 ucred
->uid
= getuid();
523 ucred
->gid
= getgid();
527 /* First try with fake ucred data, as requested */
528 if (sendmsg(fd
, &msghdr
, MSG_NOSIGNAL
) >= 0) {
533 /* If that failed, try with our own ucred instead */
535 msghdr
.msg_controllen
-= CMSG_SPACE(sizeof(struct ucred
));
536 if (msghdr
.msg_controllen
== 0)
537 msghdr
.msg_control
= NULL
;
539 if (sendmsg(fd
, &msghdr
, MSG_NOSIGNAL
) >= 0) {
548 if (unset_environment
)
549 unsetenv("NOTIFY_SOCKET");
554 _public_
int sd_pid_notify(pid_t pid
, int unset_environment
, const char *state
) {
555 return sd_pid_notify_with_fds(pid
, unset_environment
, state
, NULL
, 0);
558 _public_
int sd_notify(int unset_environment
, const char *state
) {
559 return sd_pid_notify_with_fds(0, unset_environment
, state
, NULL
, 0);
562 _public_
int sd_pid_notifyf(pid_t pid
, int unset_environment
, const char *format
, ...) {
563 _cleanup_free_
char *p
= NULL
;
569 va_start(ap
, format
);
570 r
= vasprintf(&p
, format
, ap
);
577 return sd_pid_notify(pid
, unset_environment
, p
);
580 _public_
int sd_notifyf(int unset_environment
, const char *format
, ...) {
581 _cleanup_free_
char *p
= NULL
;
587 va_start(ap
, format
);
588 r
= vasprintf(&p
, format
, ap
);
595 return sd_pid_notify(0, unset_environment
, p
);
598 _public_
int sd_booted(void) {
599 /* We test whether the runtime unit file directory has been
600 * created. This takes place in mount-setup.c, so is
601 * guaranteed to happen very early during boot. */
603 if (laccess("/run/systemd/system/", F_OK
) >= 0)
612 _public_
int sd_watchdog_enabled(int unset_environment
, uint64_t *usec
) {
613 const char *s
, *p
= ""; /* p is set to dummy value to do unsetting */
617 s
= getenv("WATCHDOG_USEC");
621 r
= safe_atou64(s
, &u
);
624 if (u
<= 0 || u
>= USEC_INFINITY
) {
629 p
= getenv("WATCHDOG_PID");
633 r
= parse_pid(p
, &pid
);
637 /* Is this for us? */
638 if (getpid_cached() != pid
) {
650 if (unset_environment
&& s
)
651 unsetenv("WATCHDOG_USEC");
652 if (unset_environment
&& p
)
653 unsetenv("WATCHDOG_PID");