34
34
#include <stdlib.h>
35
35
#include <string.h>
36
36
#include <unistd.h>
37
- #include <assert.h>
38
37
#include <pthread.h>
38
+ #include <errno.h>
39
39
40
40
#include "dm.h"
41
41
#include "pci_core.h"
@@ -193,7 +193,6 @@ virtio_rnd_k_set_status(void *base, uint64_t status)
193
193
static int
194
194
virtio_rnd_kernel_init (struct virtio_rnd * rnd )
195
195
{
196
- assert (rnd -> vbs_k .fd == 0 );
197
196
198
197
rnd -> vbs_k .fd = open ("/dev/vbs_rng" , O_RDWR );
199
198
if (rnd -> vbs_k .fd < 0 ) {
@@ -305,7 +304,6 @@ virtio_rnd_get_entropy(void *param)
305
304
struct virtio_vq_info * vq = & rnd -> vq ;
306
305
struct iovec iov ;
307
306
uint16_t idx ;
308
- int ret ;
309
307
ssize_t len ;
310
308
311
309
for (;;) {
@@ -317,20 +315,24 @@ virtio_rnd_get_entropy(void *param)
317
315
* - avoid vring processing due to spurious wakeups
318
316
* - catch missing notifications before acquiring rx_mtx
319
317
*/
320
- while (!vq_has_descs (vq )) {
321
- ret = pthread_cond_wait (& rnd -> rx_cond , & rnd -> rx_mtx );
322
- assert (ret == 0 );
323
- }
318
+ while (!vq_has_descs (vq ))
319
+ pthread_cond_wait (& rnd -> rx_cond , & rnd -> rx_mtx );
324
320
325
321
rnd -> in_progress = 1 ;
326
322
pthread_mutex_unlock (& rnd -> rx_mtx );
327
323
328
324
do {
329
- ret = vq_getchain (vq , & idx , & iov , 1 , NULL );
330
- assert (ret > 0 );
331
-
325
+ vq_getchain (vq , & idx , & iov , 1 , NULL );
332
326
len = read (rnd -> fd , iov .iov_base , iov .iov_len );
333
- assert (len > 0 );
327
+ if (len <= 0 ) {
328
+ vq_retchain (vq );
329
+ vq_endchains (vq , 0 );
330
+
331
+ /* no data available */
332
+ if (len == -1 && errno == EAGAIN )
333
+ return NULL ;
334
+ break ;
335
+ }
334
336
335
337
/* release this chain and handle more */
336
338
vq_relchain (vq , idx , len );
@@ -384,7 +386,10 @@ virtio_rnd_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
384
386
* Should always be able to open /dev/random.
385
387
*/
386
388
fd = open ("/dev/random" , O_RDONLY );
387
- assert (fd >= 0 );
389
+ if (fd < 0 ) {
390
+ WPRINTF (("virtio_rnd: open failed: /dev/random \n" ));
391
+ return -1 ;
392
+ }
388
393
389
394
rnd = calloc (1 , sizeof (struct virtio_rnd ));
390
395
if (!rnd ) {
@@ -495,13 +500,16 @@ virtio_rnd_deinit(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
495
500
virtio_rnd_kernel_stop (rnd );
496
501
virtio_rnd_kernel_reset (rnd );
497
502
rnd -> vbs_k .status = VIRTIO_DEV_INITIAL ;
498
- assert (rnd -> vbs_k .fd >= 0 );
499
- close (rnd -> vbs_k .fd );
500
- rnd -> vbs_k .fd = -1 ;
503
+ if (rnd -> vbs_k .fd >= 0 ) {
504
+ close (rnd -> vbs_k .fd );
505
+ rnd -> vbs_k .fd = -1 ;
506
+ }
501
507
}
502
508
503
- assert (rnd -> fd >= 0 );
504
- close (rnd -> fd );
509
+ if (rnd -> fd >= 0 ) {
510
+ close (rnd -> fd );
511
+ rnd -> fd = -1 ;
512
+ }
505
513
DPRINTF (("%s: free struct virtio_rnd!\n" , __func__ ));
506
514
free (rnd );
507
515
}
0 commit comments