+ qemu-discuss@nongnu.org back.
On 9/10/24 1:15 PM, DUO Labs wrote: > Should the fd be sent through SCM_RIGHTS just before the getfd command, or can > you somehow combine the two messages into one? It is one simple cmsghdr message, with both the getfd qmp message, and also the fd/SCM_RIGHTS. I suggest read the libvirt code. 465 /** 466 * virSocketSendMsgWithFDs: 467 * @sock: socket to send payload and fds to 468 * @payload: payload to send 469 * @payload_len: length of @payload 470 * @fds: array of fds to send 471 * @fds_len: len of fds array 472 473 * Send @fds along with @payload to @sock using SCM_RIGHTS. 474 * Return number of bytes sent on success. 475 * On error, set errno and return -1. 476 */ 477 int 478 virSocketSendMsgWithFDs(int sock, 479 const char *payload, 480 size_t payload_len, 481 int *fds, 482 size_t fds_len) 483 { 484 g_autofree char *control = NULL; 485 const size_t control_size = CMSG_SPACE(sizeof(int) * fds_len); 486 struct cmsghdr *cmsg; 487 struct msghdr msg = { 0 }; 488 struct iovec iov[1]; /* Send a single payload, so set vector len to 1 */ 489 int ret; 490 491 control = g_new0(char, control_size); 492 493 iov[0].iov_base = (void *) payload; 494 iov[0].iov_len = payload_len; 495 496 msg.msg_iov = iov; 497 msg.msg_iovlen = 1; 498 499 msg.msg_control = control; 500 msg.msg_controllen = control_size; 501 502 cmsg = CMSG_FIRSTHDR(&msg); 503 /* check to eliminate "potential null pointer dereference" errors during build */ 504 if (!cmsg) { 505 errno = ENOSPC; 506 return -1; 507 } 508 509 cmsg->cmsg_len = CMSG_LEN(sizeof(int) * fds_len); 510 cmsg->cmsg_level = SOL_SOCKET; 511 cmsg->cmsg_type = SCM_RIGHTS; 512 memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * fds_len); 513 514 do { 515 ret = sendmsg(sock, &msg, 0); 516 } while (ret < 0 && errno == EINTR); 517 518 return ret; 519 } Dongli Zhang