1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include <netinet/in.h>
11 #include <sys/socket.h>
16 #include "sd-daemon.h"
18 #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
= {};
449 struct msghdr msghdr
= {
452 .msg_name
= &sockaddr
,
454 _cleanup_close_
int fd
= -1;
455 struct cmsghdr
*cmsg
= NULL
;
465 if (n_fds
> 0 && !fds
) {
470 e
= getenv("NOTIFY_SOCKET");
474 salen
= sockaddr_un_set_path(&sockaddr
.un
, e
);
480 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0);
486 (void) fd_inc_sndbuf(fd
, SNDBUF_SIZE
);
488 iovec
= IOVEC_MAKE_STRING(state
);
489 msghdr
.msg_namelen
= salen
;
492 (pid
!= 0 && pid
!= getpid_cached()) ||
493 getuid() != geteuid() ||
494 getgid() != getegid();
496 if (n_fds
> 0 || send_ucred
) {
497 /* CMSG_SPACE(0) may return value different than zero, which results in miscalculated controllen. */
498 msghdr
.msg_controllen
=
499 (n_fds
> 0 ? CMSG_SPACE(sizeof(int) * n_fds
) : 0) +
500 (send_ucred
? CMSG_SPACE(sizeof(struct ucred
)) : 0);
502 msghdr
.msg_control
= alloca0(msghdr
.msg_controllen
);
504 cmsg
= CMSG_FIRSTHDR(&msghdr
);
506 cmsg
->cmsg_level
= SOL_SOCKET
;
507 cmsg
->cmsg_type
= SCM_RIGHTS
;
508 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int) * n_fds
);
510 memcpy(CMSG_DATA(cmsg
), fds
, sizeof(int) * n_fds
);
513 assert_se(cmsg
= CMSG_NXTHDR(&msghdr
, cmsg
));
519 cmsg
->cmsg_level
= SOL_SOCKET
;
520 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
521 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
523 ucred
= (struct ucred
*) CMSG_DATA(cmsg
);
524 ucred
->pid
= pid
!= 0 ? pid
: getpid_cached();
525 ucred
->uid
= getuid();
526 ucred
->gid
= getgid();
530 /* First try with fake ucred data, as requested */
531 if (sendmsg(fd
, &msghdr
, MSG_NOSIGNAL
) >= 0) {
536 /* If that failed, try with our own ucred instead */
538 msghdr
.msg_controllen
-= CMSG_SPACE(sizeof(struct ucred
));
539 if (msghdr
.msg_controllen
== 0)
540 msghdr
.msg_control
= NULL
;
542 if (sendmsg(fd
, &msghdr
, MSG_NOSIGNAL
) >= 0) {
551 if (unset_environment
)
552 unsetenv("NOTIFY_SOCKET");
557 _public_
int sd_pid_notify(pid_t pid
, int unset_environment
, const char *state
) {
558 return sd_pid_notify_with_fds(pid
, unset_environment
, state
, NULL
, 0);
561 _public_
int sd_notify(int unset_environment
, const char *state
) {
562 return sd_pid_notify_with_fds(0, unset_environment
, state
, NULL
, 0);
565 _public_
int sd_pid_notifyf(pid_t pid
, int unset_environment
, const char *format
, ...) {
566 _cleanup_free_
char *p
= NULL
;
572 va_start(ap
, format
);
573 r
= vasprintf(&p
, format
, ap
);
580 return sd_pid_notify(pid
, unset_environment
, p
);
583 _public_
int sd_notifyf(int unset_environment
, const char *format
, ...) {
584 _cleanup_free_
char *p
= NULL
;
590 va_start(ap
, format
);
591 r
= vasprintf(&p
, format
, ap
);
598 return sd_pid_notify(0, unset_environment
, p
);
601 _public_
int sd_booted(void) {
602 /* We test whether the runtime unit file directory has been
603 * created. This takes place in mount-setup.c, so is
604 * guaranteed to happen very early during boot. */
606 if (laccess("/run/systemd/system/", F_OK
) >= 0)
615 _public_
int sd_watchdog_enabled(int unset_environment
, uint64_t *usec
) {
616 const char *s
, *p
= ""; /* p is set to dummy value to do unsetting */
620 s
= getenv("WATCHDOG_USEC");
624 r
= safe_atou64(s
, &u
);
627 if (u
<= 0 || u
>= USEC_INFINITY
) {
632 p
= getenv("WATCHDOG_PID");
636 r
= parse_pid(p
, &pid
);
640 /* Is this for us? */
641 if (getpid_cached() != pid
) {
653 if (unset_environment
&& s
)
654 unsetenv("WATCHDOG_USEC");
655 if (unset_environment
&& p
)
656 unsetenv("WATCHDOG_PID");