Skip to content

Commit 56d02f2

Browse files
committed
sd-daemon: Use socket type from vsock address if set
If a socket type is explicitly provided in the vsock address, let's make sure we try only that socket type.
1 parent c31984e commit 56d02f2

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/libsystemd/sd-daemon/sd-daemon.c

+13-10
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ static int pid_notify_with_fds_internal(
466466
struct cmsghdr *cmsg = NULL;
467467
const char *e;
468468
bool send_ucred;
469-
int r;
469+
int type, r;
470470

471471
if (!state)
472472
return -EINVAL;
@@ -491,30 +491,33 @@ static int pid_notify_with_fds_internal(
491491
if (address.sockaddr.vm.svm_family == AF_VSOCK && address.sockaddr.vm.svm_cid == VMADDR_CID_ANY)
492492
return -EINVAL;
493493

494+
type = address.type == 0 ? SOCK_DGRAM : address.type;
495+
494496
/* At the time of writing QEMU does not yet support AF_VSOCK + SOCK_DGRAM and returns
495497
* ENODEV. Fallback to SOCK_SEQPACKET in that case. */
496-
fd = socket(address.sockaddr.sa.sa_family, SOCK_DGRAM|SOCK_CLOEXEC, 0);
498+
fd = socket(address.sockaddr.sa.sa_family, type|SOCK_CLOEXEC, 0);
497499
if (fd < 0) {
498-
if (!(ERRNO_IS_NOT_SUPPORTED(errno) || errno == ENODEV) || address.sockaddr.sa.sa_family != AF_VSOCK)
499-
return log_debug_errno(errno, "Failed to open datagram notify socket to '%s': %m", e);
500+
if (!(ERRNO_IS_NOT_SUPPORTED(errno) || errno == ENODEV) || address.sockaddr.sa.sa_family != AF_VSOCK || address.type > 0)
501+
return log_debug_errno(errno, "Failed to open %s notify socket to '%s': %m", socket_address_type_to_string(type), e);
500502

501-
fd = socket(address.sockaddr.sa.sa_family, SOCK_SEQPACKET|SOCK_CLOEXEC, 0);
503+
type = SOCK_SEQPACKET;
504+
fd = socket(address.sockaddr.sa.sa_family, type|SOCK_CLOEXEC, 0);
502505
if (fd < 0)
503-
return log_debug_errno(errno, "Failed to open sequential packet socket to '%s': %m", e);
506+
return log_debug_errno(errno, "Failed to open %s socket to '%s': %m", socket_address_type_to_string(type), e);
507+
}
504508

509+
if (address.sockaddr.sa.sa_family == AF_VSOCK) {
505510
r = vsock_bind_privileged_port(fd);
506511
if (r < 0 && !ERRNO_IS_PRIVILEGE(r))
507512
return log_debug_errno(r, "Failed to bind socket to privileged port: %m");
513+
}
508514

515+
if (IN_SET(type, SOCK_STREAM, SOCK_SEQPACKET)) {
509516
if (connect(fd, &address.sockaddr.sa, address.size) < 0)
510517
return log_debug_errno(errno, "Failed to connect socket to '%s': %m", e);
511518

512519
msghdr.msg_name = NULL;
513520
msghdr.msg_namelen = 0;
514-
} else if (address.sockaddr.sa.sa_family == AF_VSOCK) {
515-
r = vsock_bind_privileged_port(fd);
516-
if (r < 0 && !ERRNO_IS_PRIVILEGE(r))
517-
return log_debug_errno(r, "Failed to bind socket to privileged port: %m");
518521
}
519522

520523
(void) fd_inc_sndbuf(fd, SNDBUF_SIZE);

0 commit comments

Comments
 (0)