1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
9 #include <netinet/in.h>
15 #include <sys/socket.h>
20 #include "sd-daemon.h"
22 #include "alloc-util.h"
25 #include "parse-util.h"
26 #include "path-util.h"
27 #include "process-util.h"
28 #include "socket-util.h"
32 #define SNDBUF_SIZE (8*1024*1024)
34 static void unsetenv_all(bool unset_environment
) {
36 if (!unset_environment
)
39 unsetenv("LISTEN_PID");
40 unsetenv("LISTEN_FDS");
41 unsetenv("LISTEN_FDNAMES");
44 _public_
int sd_listen_fds(int unset_environment
) {
49 e
= getenv("LISTEN_PID");
55 r
= parse_pid(e
, &pid
);
60 if (getpid_cached() != pid
) {
65 e
= getenv("LISTEN_FDS");
75 assert_cc(SD_LISTEN_FDS_START
< INT_MAX
);
76 if (n
<= 0 || n
> INT_MAX
- SD_LISTEN_FDS_START
) {
81 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
82 r
= fd_cloexec(fd
, true);
90 unsetenv_all(unset_environment
);
94 _public_
int sd_listen_fds_with_names(int unset_environment
, char ***names
) {
95 _cleanup_strv_free_
char **l
= NULL
;
97 int n_names
= 0, n_fds
;
102 return sd_listen_fds(unset_environment
);
104 e
= getenv("LISTEN_FDNAMES");
106 n_names
= strv_split_extract(&l
, e
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
108 unsetenv_all(unset_environment
);
116 n_fds
= sd_listen_fds(unset_environment
);
121 if (n_names
!= n_fds
)
124 r
= strv_extend_n(&l
, "unknown", n_fds
);
129 *names
= TAKE_PTR(l
);
134 _public_
int sd_is_fifo(int fd
, const char *path
) {
137 assert_return(fd
>= 0, -EBADF
);
139 if (fstat(fd
, &st_fd
) < 0)
142 if (!S_ISFIFO(st_fd
.st_mode
))
148 if (stat(path
, &st_path
) < 0) {
150 if (IN_SET(errno
, ENOENT
, ENOTDIR
))
157 st_path
.st_dev
== st_fd
.st_dev
&&
158 st_path
.st_ino
== st_fd
.st_ino
;
164 _public_
int sd_is_special(int fd
, const char *path
) {
167 assert_return(fd
>= 0, -EBADF
);
169 if (fstat(fd
, &st_fd
) < 0)
172 if (!S_ISREG(st_fd
.st_mode
) && !S_ISCHR(st_fd
.st_mode
))
178 if (stat(path
, &st_path
) < 0) {
180 if (IN_SET(errno
, ENOENT
, ENOTDIR
))
186 if (S_ISREG(st_fd
.st_mode
) && S_ISREG(st_path
.st_mode
))
188 st_path
.st_dev
== st_fd
.st_dev
&&
189 st_path
.st_ino
== st_fd
.st_ino
;
190 else if (S_ISCHR(st_fd
.st_mode
) && S_ISCHR(st_path
.st_mode
))
191 return st_path
.st_rdev
== st_fd
.st_rdev
;
199 static int sd_is_socket_internal(int fd
, int type
, int listening
) {
202 assert_return(fd
>= 0, -EBADF
);
203 assert_return(type
>= 0, -EINVAL
);
205 if (fstat(fd
, &st_fd
) < 0)
208 if (!S_ISSOCK(st_fd
.st_mode
))
213 socklen_t l
= sizeof(other_type
);
215 if (getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &other_type
, &l
) < 0)
218 if (l
!= sizeof(other_type
))
221 if (other_type
!= type
)
225 if (listening
>= 0) {
227 socklen_t l
= sizeof(accepting
);
229 if (getsockopt(fd
, SOL_SOCKET
, SO_ACCEPTCONN
, &accepting
, &l
) < 0)
232 if (l
!= sizeof(accepting
))
235 if (!accepting
!= !listening
)
242 _public_
int sd_is_socket(int fd
, int family
, int type
, int listening
) {
245 assert_return(fd
>= 0, -EBADF
);
246 assert_return(family
>= 0, -EINVAL
);
248 r
= sd_is_socket_internal(fd
, type
, listening
);
253 union sockaddr_union sockaddr
= {};
254 socklen_t l
= sizeof(sockaddr
);
256 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
259 if (l
< sizeof(sa_family_t
))
262 return sockaddr
.sa
.sa_family
== family
;
268 _public_
int sd_is_socket_inet(int fd
, int family
, int type
, int listening
, uint16_t port
) {
269 union sockaddr_union sockaddr
= {};
270 socklen_t l
= sizeof(sockaddr
);
273 assert_return(fd
>= 0, -EBADF
);
274 assert_return(IN_SET(family
, 0, AF_INET
, AF_INET6
), -EINVAL
);
276 r
= sd_is_socket_internal(fd
, type
, listening
);
280 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
283 if (l
< sizeof(sa_family_t
))
286 if (!IN_SET(sockaddr
.sa
.sa_family
, AF_INET
, AF_INET6
))
290 if (sockaddr
.sa
.sa_family
!= family
)
296 r
= sockaddr_port(&sockaddr
.sa
, &sa_port
);
300 return port
== sa_port
;
306 _public_
int sd_is_socket_sockaddr(int fd
, int type
, const struct sockaddr
* addr
, unsigned addr_len
, int listening
) {
307 union sockaddr_union sockaddr
= {};
308 socklen_t l
= sizeof(sockaddr
);
311 assert_return(fd
>= 0, -EBADF
);
312 assert_return(addr
, -EINVAL
);
313 assert_return(addr_len
>= sizeof(sa_family_t
), -ENOBUFS
);
314 assert_return(IN_SET(addr
->sa_family
, AF_INET
, AF_INET6
), -EPFNOSUPPORT
);
316 r
= sd_is_socket_internal(fd
, type
, listening
);
320 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
323 if (l
< sizeof(sa_family_t
))
326 if (sockaddr
.sa
.sa_family
!= addr
->sa_family
)
329 if (sockaddr
.sa
.sa_family
== AF_INET
) {
330 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) addr
;
332 if (l
< sizeof(struct sockaddr_in
) || addr_len
< sizeof(struct sockaddr_in
))
335 if (in
->sin_port
!= 0 &&
336 sockaddr
.in
.sin_port
!= in
->sin_port
)
339 return sockaddr
.in
.sin_addr
.s_addr
== in
->sin_addr
.s_addr
;
342 const struct sockaddr_in6
*in
= (const struct sockaddr_in6
*) addr
;
344 if (l
< sizeof(struct sockaddr_in6
) || addr_len
< sizeof(struct sockaddr_in6
))
347 if (in
->sin6_port
!= 0 &&
348 sockaddr
.in6
.sin6_port
!= in
->sin6_port
)
351 if (in
->sin6_flowinfo
!= 0 &&
352 sockaddr
.in6
.sin6_flowinfo
!= in
->sin6_flowinfo
)
355 if (in
->sin6_scope_id
!= 0 &&
356 sockaddr
.in6
.sin6_scope_id
!= in
->sin6_scope_id
)
359 return memcmp(sockaddr
.in6
.sin6_addr
.s6_addr
, in
->sin6_addr
.s6_addr
,
360 sizeof(in
->sin6_addr
.s6_addr
)) == 0;
364 _public_
int sd_is_socket_unix(int fd
, int type
, int listening
, const char *path
, size_t length
) {
365 union sockaddr_union sockaddr
= {};
366 socklen_t l
= sizeof(sockaddr
);
369 assert_return(fd
>= 0, -EBADF
);
371 r
= sd_is_socket_internal(fd
, type
, listening
);
375 if (getsockname(fd
, &sockaddr
.sa
, &l
) < 0)
378 if (l
< sizeof(sa_family_t
))
381 if (sockaddr
.sa
.sa_family
!= AF_UNIX
)
386 length
= strlen(path
);
390 return l
== offsetof(struct sockaddr_un
, sun_path
);
393 /* Normal path socket */
395 (l
>= offsetof(struct sockaddr_un
, sun_path
) + length
+ 1) &&
396 memcmp(path
, sockaddr
.un
.sun_path
, length
+1) == 0;
398 /* Abstract namespace socket */
400 (l
== offsetof(struct sockaddr_un
, sun_path
) + length
) &&
401 memcmp(path
, sockaddr
.un
.sun_path
, length
) == 0;
407 _public_
int sd_is_mq(int fd
, const char *path
) {
410 /* Check that the fd is valid */
411 assert_return(fcntl(fd
, F_GETFD
) >= 0, -errno
);
413 if (mq_getattr(fd
, &attr
) < 0) {
415 /* A non-mq fd (or an invalid one, but we ruled that out above) */
421 char fpath
[PATH_MAX
];
424 assert_return(path_is_absolute(path
), -EINVAL
);
426 if (fstat(fd
, &a
) < 0)
429 strncpy(stpcpy(fpath
, "/dev/mqueue"), path
, sizeof(fpath
) - 12);
430 fpath
[sizeof(fpath
)-1] = 0;
432 if (stat(fpath
, &b
) < 0)
435 if (a
.st_dev
!= b
.st_dev
||
436 a
.st_ino
!= b
.st_ino
)
443 _public_
int sd_pid_notify_with_fds(
445 int unset_environment
,
450 union sockaddr_union sockaddr
= {
451 .sa
.sa_family
= AF_UNIX
,
453 struct iovec iovec
= {
454 .iov_base
= (char*) state
,
456 struct msghdr msghdr
= {
459 .msg_name
= &sockaddr
,
461 _cleanup_close_
int fd
= -1;
462 struct cmsghdr
*cmsg
= NULL
;
472 if (n_fds
> 0 && !fds
) {
477 e
= getenv("NOTIFY_SOCKET");
481 /* Must be an abstract socket, or an absolute path */
482 if (!IN_SET(e
[0], '@', '/') || e
[1] == 0) {
487 if (strlen(e
) > sizeof(sockaddr
.un
.sun_path
)) {
492 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0);
498 (void) fd_inc_sndbuf(fd
, SNDBUF_SIZE
);
500 iovec
.iov_len
= strlen(state
);
502 strncpy(sockaddr
.un
.sun_path
, e
, sizeof(sockaddr
.un
.sun_path
));
503 if (sockaddr
.un
.sun_path
[0] == '@')
504 sockaddr
.un
.sun_path
[0] = 0;
506 msghdr
.msg_namelen
= SOCKADDR_UN_LEN(sockaddr
.un
);
509 (pid
!= 0 && pid
!= getpid_cached()) ||
510 getuid() != geteuid() ||
511 getgid() != getegid();
513 if (n_fds
> 0 || send_ucred
) {
514 /* CMSG_SPACE(0) may return value different than zero, which results in miscalculated controllen. */
515 msghdr
.msg_controllen
=
516 (n_fds
> 0 ? CMSG_SPACE(sizeof(int) * n_fds
) : 0) +
517 (send_ucred
? CMSG_SPACE(sizeof(struct ucred
)) : 0);
519 msghdr
.msg_control
= alloca0(msghdr
.msg_controllen
);
521 cmsg
= CMSG_FIRSTHDR(&msghdr
);
523 cmsg
->cmsg_level
= SOL_SOCKET
;
524 cmsg
->cmsg_type
= SCM_RIGHTS
;
525 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int) * n_fds
);
527 memcpy(CMSG_DATA(cmsg
), fds
, sizeof(int) * n_fds
);
530 assert_se(cmsg
= CMSG_NXTHDR(&msghdr
, cmsg
));
536 cmsg
->cmsg_level
= SOL_SOCKET
;
537 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
538 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
540 ucred
= (struct ucred
*) CMSG_DATA(cmsg
);
541 ucred
->pid
= pid
!= 0 ? pid
: getpid_cached();
542 ucred
->uid
= getuid();
543 ucred
->gid
= getgid();
547 /* First try with fake ucred data, as requested */
548 if (sendmsg(fd
, &msghdr
, MSG_NOSIGNAL
) >= 0) {
553 /* If that failed, try with our own ucred instead */
555 msghdr
.msg_controllen
-= CMSG_SPACE(sizeof(struct ucred
));
556 if (msghdr
.msg_controllen
== 0)
557 msghdr
.msg_control
= NULL
;
559 if (sendmsg(fd
, &msghdr
, MSG_NOSIGNAL
) >= 0) {
568 if (unset_environment
)
569 unsetenv("NOTIFY_SOCKET");
574 _public_
int sd_pid_notify(pid_t pid
, int unset_environment
, const char *state
) {
575 return sd_pid_notify_with_fds(pid
, unset_environment
, state
, NULL
, 0);
578 _public_
int sd_notify(int unset_environment
, const char *state
) {
579 return sd_pid_notify_with_fds(0, unset_environment
, state
, NULL
, 0);
582 _public_
int sd_pid_notifyf(pid_t pid
, int unset_environment
, const char *format
, ...) {
583 _cleanup_free_
char *p
= NULL
;
589 va_start(ap
, format
);
590 r
= vasprintf(&p
, format
, ap
);
597 return sd_pid_notify(pid
, unset_environment
, p
);
600 _public_
int sd_notifyf(int unset_environment
, const char *format
, ...) {
601 _cleanup_free_
char *p
= NULL
;
607 va_start(ap
, format
);
608 r
= vasprintf(&p
, format
, ap
);
615 return sd_pid_notify(0, unset_environment
, p
);
618 _public_
int sd_booted(void) {
619 /* We test whether the runtime unit file directory has been
620 * created. This takes place in mount-setup.c, so is
621 * guaranteed to happen very early during boot. */
623 return laccess("/run/systemd/system/", F_OK
) >= 0;
626 _public_
int sd_watchdog_enabled(int unset_environment
, uint64_t *usec
) {
627 const char *s
, *p
= ""; /* p is set to dummy value to do unsetting */
631 s
= getenv("WATCHDOG_USEC");
635 r
= safe_atou64(s
, &u
);
638 if (u
<= 0 || u
>= USEC_INFINITY
) {
643 p
= getenv("WATCHDOG_PID");
647 r
= parse_pid(p
, &pid
);
651 /* Is this for us? */
652 if (getpid_cached() != pid
) {
664 if (unset_environment
&& s
)
665 unsetenv("WATCHDOG_USEC");
666 if (unset_environment
&& p
)
667 unsetenv("WATCHDOG_PID");