Create socketpair, assign one socket/FD as data_fd to Control Socket The other socket/FD will be assigned to msg_fd, which will be sent to swtpm via CmdSetDatafd control command
(&mut self)
| 129 | /// The other socket/FD will be assigned to msg_fd, which will be sent to swtpm |
| 130 | /// via CmdSetDatafd control command |
| 131 | fn prepare_data_fd(&mut self) -> Result<()> { |
| 132 | let mut res: PtmResult = 0; |
| 133 | |
| 134 | let mut fds = [-1, -1]; |
| 135 | // SAFETY: FFI calls and return value of the unsafe call is checked |
| 136 | unsafe { |
| 137 | let ret = libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()); |
| 138 | if ret == -1 { |
| 139 | return Err(Error::PrepareDataFd(anyhow!( |
| 140 | "Failed to prepare data fd for tpm emulator. Error Code {:?}", |
| 141 | std::io::Error::last_os_error() |
| 142 | ))); |
| 143 | } |
| 144 | } |
| 145 | self.control_socket.set_msgfd(fds[1]); |
| 146 | debug!("data fd to be configured in swtpm = {:?}", fds[1]); |
| 147 | self.run_control_cmd(Commands::CmdSetDatafd, &mut res, 0, mem::size_of::<u32>())?; |
| 148 | debug!("data fd in cloud-hypervisor = {:?}", fds[0]); |
| 149 | self.data_fd = fds[0]; |
| 150 | |
| 151 | // SAFETY: FFI calls and return value of the unsafe call is checked |
| 152 | unsafe { |
| 153 | let tv = libc::timeval { |
| 154 | tv_sec: 0, |
| 155 | tv_usec: 100000, // Set recv timeout to 100ms |
| 156 | }; |
| 157 | let ret = libc::setsockopt( |
| 158 | fds[0], |
| 159 | libc::SOL_SOCKET, |
| 160 | libc::SO_RCVTIMEO, |
| 161 | (&raw const tv).cast(), |
| 162 | std::mem::size_of::<libc::timeval>() as u32, |
| 163 | ); |
| 164 | if ret == -1 { |
| 165 | return Err(Error::PrepareDataFd(anyhow!( |
| 166 | "Failed to set receive timeout on data fd socket. Error Code {:?}", |
| 167 | std::io::Error::last_os_error() |
| 168 | ))); |
| 169 | } |
| 170 | } |
| 171 | self.control_socket.set_datafd(fds[0]); |
| 172 | Ok(()) |
| 173 | } |
| 174 | |
| 175 | /// Gather TPM Capabilities and cache them in Emulator |
| 176 | /// |
no test coverage detected