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"
22 #include "parse-util.h"
23 #include "path-util.h"
24 #include "process-util.h"
25 #include "socket-util.h"
29 #define SNDBUF_SIZE (8*1024*1024)
31 static void unsetenv_all(bool unset_environment
) {
33 if (!unset_environment
)
36 unsetenv("LISTEN_PID");
37 unsetenv("LISTEN_FDS");
38 unsetenv("LISTEN_FDNAMES");
41 _public_
int sd_listen_fds(int unset_environment
) {
46 e
= getenv("LISTEN_PID");
52 r
= parse_pid(e
, &pid
);
57 if (getpid_cached() != pid
) {
62 e
= getenv("LISTEN_FDS");
72 assert_cc(SD_LISTEN_FDS_START
< INT_MAX
);
73 if (n
<= 0 || n
> INT_MAX
- SD_LISTEN_FDS_START
) {
78 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
79 r
= fd_cloexec(fd
, true);
87 unsetenv_all(unset_environment
);
91 _public_
int sd_listen_fds_with_names(int unset_environment
, char ***names
) {
92 _cleanup_strv_free_
char **l
= NULL
;
94 int n_names
= 0, n_fds
;
99 return sd_listen_fds(unset_environment
);
101 e
= getenv("LISTEN_FDNAMES");
103 n_names
= strv_split_extract(&l
, e
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
105 unsetenv_all(unset_environment
);
113 n_fds
= sd_listen_fds(unset_environment
);
118 if (n_names
!= n_fds
)
121 r
= strv_extend_n(&l
, "unknown", n_fds
);
126 *names
= TAKE_PTR(l
);
131 _public_
int sd_is_fifo(int fd
, const char *path
) {
134 assert_return(fd
>= 0, -EBADF
);
136 if (fstat(fd
, &st_fd
) < 0)
139 if (!S_ISFIFO(st_fd
.st_mode
))
145 if (stat(path
, &st_path
) < 0) {
147 if (IN_SET(errno
, ENOENT
, ENOTDIR
))
154 st_path
.st_dev
== st_fd
.st_dev
&&
155 st_path
.st_ino
== st_fd
.st_ino
;
161 _public_
int sd_is_special(int fd
, const char *path
) {
164 assert_return(fd
>= 0, -EBADF
);
166 if (fstat(fd
, &st_fd
) < 0)
169 if (!S_ISREG(st_fd
.st_mode
) && !S_ISCHR(st_fd
.st_mode
))
175 if (stat(path
, &st_path
) < 0) {
177 if (IN_SET(errno
, ENOENT
, ENOTDIR
))
183 if (S_ISREG(st_fd
.st_mode
) && S_ISREG(st_path
.st_mode
))
185 st_path
.st_dev
== st_fd
.st_dev
&&
186 st_path
.st_ino
== st_fd
.st_ino
;
187 else if (S_ISCHR(st_fd
.st_mode
) && S_ISCHR(st_path
.st_mode
))
188 return st_path
.st_rdev
== st_fd
.st_rdev
;
196 static int sd_is_socket_internal(int fd
, int type
, int listening
) {
199 assert_return(fd
>= 0, -EBADF
);
200 assert_return(type
>= 0, -EINVAL
);
202 if (fstat(fd
, &st_fd
) < 0)
205 if (!S_ISSOCK(st_fd
.st_mode
))
210 socklen_t l
= sizeof(other_type
);
212 if (getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &other_type
, &l
) < 0)
215 if (l
!= sizeof(other_type
))
218 if (other_type
!= type
)
222 if (listening
>= 0) {
224 socklen_t l
= sizeof(accepting
);
226 if (getsockopt(fd
, SOL_SOCKET
, SO_ACCEPTCONN
, &accepting
, &l
) < 0)
229 if (l
!= sizeof(accepting
))
232 if (!accepting
!= !listening
)
239 _public_
int sd_is_socket(int fd
, int family
, int type
, int listening
) {
242 assert_return(fd
>= 0, -EBADF
);
243 assert_return(family
>= 0, -EINVAL
);
245 r
= sd_is_socket_internal(fd
, type
, listening
);
250 union sockaddr_union sockaddr
= {};
251 socklen_t l
= sizeof(sockaddr
);
253 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
256 if (l
< sizeof(sa_family_t
))
259 return sockaddr
.sa
.sa_family
== family
;
265 _public_
int sd_is_socket_inet(int fd
, int family
, int type
, int listening
, uint16_t port
) {
266 union sockaddr_union sockaddr
= {};
267 socklen_t l
= sizeof(sockaddr
);
270 assert_return(fd
>= 0, -EBADF
);
271 assert_return(IN_SET(family
, 0, AF_INET
, AF_INET6
), -EINVAL
);
273 r
= sd_is_socket_internal(fd
, type
, listening
);
277 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
280 if (l
< sizeof(sa_family_t
))
283 if (!IN_SET(sockaddr
.sa
.sa_family
, AF_INET
, AF_INET6
))
287 if (sockaddr
.sa
.sa_family
!= family
)
293 r
= sockaddr_port(&sockaddr
.sa
, &sa_port
);
297 return port
== sa_port
;
303 _public_
int sd_is_socket_sockaddr(int fd
, int type
, const struct sockaddr
* addr
, unsigned addr_len
, int listening
) {
304 union sockaddr_union sockaddr
= {};
305 socklen_t l
= sizeof(sockaddr
);
308 assert_return(fd
>= 0, -EBADF
);
309 assert_return(addr
, -EINVAL
);
310 assert_return(addr_len
>= sizeof(sa_family_t
), -ENOBUFS
);
311 assert_return(IN_SET(addr
->sa_family
, AF_INET
, AF_INET6
), -EPFNOSUPPORT
);
313 r
= sd_is_socket_internal(fd
, type
, listening
);
317 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
320 if (l
< sizeof(sa_family_t
))
323 if (sockaddr
.sa
.sa_family
!= addr
->sa_family
)
326 if (sockaddr
.sa
.sa_family
== AF_INET
) {
327 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) addr
;
329 if (l
< sizeof(struct sockaddr_in
) || addr_len
< sizeof(struct sockaddr_in
))
332 if (in
->sin_port
!= 0 &&
333 sockaddr
.in
.sin_port
!= in
->sin_port
)
336 return sockaddr
.in
.sin_addr
.s_addr
== in
->sin_addr
.s_addr
;
339 const struct sockaddr_in6
*in
= (const struct sockaddr_in6
*) addr
;
341 if (l
< sizeof(struct sockaddr_in6
) || addr_len
< sizeof(struct sockaddr_in6
))
344 if (in
->sin6_port
!= 0 &&
345 sockaddr
.in6
.sin6_port
!= in
->sin6_port
)
348 if (in
->sin6_flowinfo
!= 0 &&
349 sockaddr
.in6
.sin6_flowinfo
!= in
->sin6_flowinfo
)
352 if (in
->sin6_scope_id
!= 0 &&
353 sockaddr
.in6
.sin6_scope_id
!= in
->sin6_scope_id
)
356 return memcmp(sockaddr
.in6
.sin6_addr
.s6_addr
, in
->sin6_addr
.s6_addr
,
357 sizeof(in
->sin6_addr
.s6_addr
)) == 0;
361 _public_
int sd_is_socket_unix(int fd
, int type
, int listening
, const char *path
, size_t length
) {
362 union sockaddr_union sockaddr
= {};
363 socklen_t l
= sizeof(sockaddr
);
366 assert_return(fd
>= 0, -EBADF
);
368 r
= sd_is_socket_internal(fd
, type
, listening
);
372 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
375 if (l
< sizeof(sa_family_t
))
378 if (sockaddr
.sa
.sa_family
!= AF_UNIX
)
383 length
= strlen(path
);
387 return l
== offsetof(struct sockaddr_un
, sun_path
);
390 /* Normal path socket */
392 (l
>= offsetof(struct sockaddr_un
, sun_path
) + length
+ 1) &&
393 memcmp(path
, sockaddr
.un
.sun_path
, length
+1) == 0;
395 /* Abstract namespace socket */
397 (l
== offsetof(struct sockaddr_un
, sun_path
) + length
) &&
398 memcmp(path
, sockaddr
.un
.sun_path
, length
) == 0;
404 _public_
int sd_is_mq(int fd
, const char *path
) {
407 /* Check that the fd is valid */
408 assert_return(fcntl(fd
, F_GETFD
) >= 0, -errno
);
410 if (mq_getattr(fd
, &attr
) < 0) {
412 /* A non-mq fd (or an invalid one, but we ruled that out above) */
418 char fpath
[PATH_MAX
];
421 assert_return(path_is_absolute(path
), -EINVAL
);
423 if (fstat(fd
, &a
) < 0)
426 strncpy(stpcpy(fpath
, "/dev/mqueue"), path
, sizeof(fpath
) - 12);
427 fpath
[sizeof(fpath
)-1] = 0;
429 if (stat(fpath
, &b
) < 0)
432 if (a
.st_dev
!= b
.st_dev
||
433 a
.st_ino
!= b
.st_ino
)
440 _public_
int sd_pid_notify_with_fds(
442 int unset_environment
,
447 union sockaddr_union sockaddr
= {
448 .sa
.sa_family
= AF_UNIX
,
450 struct iovec iovec
= {
451 .iov_base
= (char*) state
,
453 struct msghdr msghdr
= {
456 .msg_name
= &sockaddr
,
458 _cleanup_close_
int fd
= -1;
459 struct cmsghdr
*cmsg
= NULL
;
469 if (n_fds
> 0 && !fds
) {
474 e
= getenv("NOTIFY_SOCKET");
478 /* Must be an abstract socket, or an absolute path */
479 if (!IN_SET(e
[0], '@', '/') || e
[1] == 0) {
484 if (strlen(e
) > sizeof(sockaddr
.un
.sun_path
)) {
489 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0);
495 (void) fd_inc_sndbuf(fd
, SNDBUF_SIZE
);
497 iovec
.iov_len
= strlen(state
);
499 strncpy(sockaddr
.un
.sun_path
, e
, sizeof(sockaddr
.un
.sun_path
));
500 if (sockaddr
.un
.sun_path
[0] == '@')
501 sockaddr
.un
.sun_path
[0] = 0;
503 msghdr
.msg_namelen
= SOCKADDR_UN_LEN(sockaddr
.un
);
506 (pid
!= 0 && pid
!= getpid_cached()) ||
507 getuid() != geteuid() ||
508 getgid() != getegid();
510 if (n_fds
> 0 || send_ucred
) {
511 /* CMSG_SPACE(0) may return value different than zero, which results in miscalculated controllen. */
512 msghdr
.msg_controllen
=
513 (n_fds
> 0 ? CMSG_SPACE(sizeof(int) * n_fds
) : 0) +
514 (send_ucred
? CMSG_SPACE(sizeof(struct ucred
)) : 0);
516 msghdr
.msg_control
= alloca0(msghdr
.msg_controllen
);
518 cmsg
= CMSG_FIRSTHDR(&msghdr
);
520 cmsg
->cmsg_level
= SOL_SOCKET
;
521 cmsg
->cmsg_type
= SCM_RIGHTS
;
522 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int) * n_fds
);
524 memcpy(CMSG_DATA(cmsg
), fds
, sizeof(int) * n_fds
);
527 assert_se(cmsg
= CMSG_NXTHDR(&msghdr
, cmsg
));
533 cmsg
->cmsg_level
= SOL_SOCKET
;
534 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
535 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
537 ucred
= (struct ucred
*) CMSG_DATA(cmsg
);
538 ucred
->pid
= pid
!= 0 ? pid
: getpid_cached();
539 ucred
->uid
= getuid();
540 ucred
->gid
= getgid();
544 /* First try with fake ucred data, as requested */
545 if (sendmsg(fd
, &msghdr
, MSG_NOSIGNAL
) >= 0) {
550 /* If that failed, try with our own ucred instead */
552 msghdr
.msg_controllen
-= CMSG_SPACE(sizeof(struct ucred
));
553 if (msghdr
.msg_controllen
== 0)
554 msghdr
.msg_control
= NULL
;
556 if (sendmsg(fd
, &msghdr
, MSG_NOSIGNAL
) >= 0) {
565 if (unset_environment
)
566 unsetenv("NOTIFY_SOCKET");
571 _public_
int sd_pid_notify(pid_t pid
, int unset_environment
, const char *state
) {
572 return sd_pid_notify_with_fds(pid
, unset_environment
, state
, NULL
, 0);
575 _public_
int sd_notify(int unset_environment
, const char *state
) {
576 return sd_pid_notify_with_fds(0, unset_environment
, state
, NULL
, 0);
579 _public_
int sd_pid_notifyf(pid_t pid
, int unset_environment
, const char *format
, ...) {
580 _cleanup_free_
char *p
= NULL
;
586 va_start(ap
, format
);
587 r
= vasprintf(&p
, format
, ap
);
594 return sd_pid_notify(pid
, unset_environment
, p
);
597 _public_
int sd_notifyf(int unset_environment
, const char *format
, ...) {
598 _cleanup_free_
char *p
= NULL
;
604 va_start(ap
, format
);
605 r
= vasprintf(&p
, format
, ap
);
612 return sd_pid_notify(0, unset_environment
, p
);
615 _public_
int sd_booted(void) {
616 /* We test whether the runtime unit file directory has been
617 * created. This takes place in mount-setup.c, so is
618 * guaranteed to happen very early during boot. */
620 return laccess("/run/systemd/system/", F_OK
) >= 0;
623 _public_
int sd_watchdog_enabled(int unset_environment
, uint64_t *usec
) {
624 const char *s
, *p
= ""; /* p is set to dummy value to do unsetting */
628 s
= getenv("WATCHDOG_USEC");
632 r
= safe_atou64(s
, &u
);
635 if (u
<= 0 || u
>= USEC_INFINITY
) {
640 p
= getenv("WATCHDOG_PID");
644 r
= parse_pid(p
, &pid
);
648 /* Is this for us? */
649 if (getpid_cached() != pid
) {
661 if (unset_environment
&& s
)
662 unsetenv("WATCHDOG_USEC");
663 if (unset_environment
&& p
)
664 unsetenv("WATCHDOG_PID");