2626#include <sys/mount.h>
2727
2828#define power_of_2 (x ) ((x) && !((x) & ((x) - 1)))
29+ #define err_errno (exit_code , ...) \
30+ do { \
31+ errno = (exit_code); \
32+ err(EXIT_FAILURE, __VA_ARGS__); \
33+ } while (0)
2934
3035static unsigned long logical_block_size ;
3136static unsigned long dma_alignment ;
@@ -42,7 +47,7 @@ static void init_args(char **argv)
4247{
4348 test_fd = open (argv [1 ], O_RDWR | O_TRUNC | O_DIRECT );
4449 if (test_fd < 0 )
45- err (errno , "%s: failed to open %s" , __func__ , argv [1 ]);
50+ err (EXIT_FAILURE , "%s: failed to open %s" , __func__ , argv [1 ]);
4651
4752 max_segments = strtoul (argv [2 ], NULL , 0 );
4853 max_bytes = strtoul (argv [3 ], NULL , 0 ) * 1024 ;
@@ -54,18 +59,18 @@ static void init_args(char **argv)
5459 !power_of_2 (dma_alignment ) ||
5560 !power_of_2 (logical_block_size )) {
5661 errno = EINVAL ;
57- err (1 , "%s: bad parameters" , __func__ );
62+ err (EXIT_FAILURE , "%s: bad parameters" , __func__ );
5863 }
5964
6065 if (virt_boundary > 1 && virt_boundary < logical_block_size ) {
6166 errno = EINVAL ;
62- err (1 , "%s: virt_boundary:%lu logical_block_size:%lu" , __func__ ,
67+ err (EXIT_FAILURE , "%s: virt_boundary:%lu logical_block_size:%lu" , __func__ ,
6368 virt_boundary , logical_block_size );
6469 }
6570
6671 if (dma_alignment > logical_block_size ) {
6772 errno = EINVAL ;
68- err (1 , "%s: dma_alignment:%lu logical_block_size:%lu" , __func__ ,
73+ err (EXIT_FAILURE , "%s: dma_alignment:%lu logical_block_size:%lu" , __func__ ,
6974 dma_alignment , logical_block_size );
7075 }
7176
@@ -87,7 +92,7 @@ static void init_buffers()
8792
8893 buf_size = max_bytes * max_segments / 2 ;
8994 if (buf_size < logical_block_size * max_segments )
90- err (EINVAL , "%s: logical block size is too big" , __func__ );
95+ err_errno (EINVAL , "%s: logical block size is too big" , __func__ );
9196
9297 if (buf_size < logical_block_size * 1024 * 4 )
9398 buf_size = logical_block_size * 1024 * 4 ;
@@ -97,26 +102,27 @@ static void init_buffers()
97102
98103 ret = ioctl (test_fd , BLKGETSIZE64 , & dev_bytes );
99104 if (ret < 0 )
100- err (ret , "%s: ioctl BLKGETSIZE64 failed" , __func__ );
105+ err (EXIT_FAILURE , "%s: ioctl BLKGETSIZE64 failed" , __func__ );
101106
102107 if (dev_bytes < buf_size )
103108 buf_size = dev_bytes ;
104109
105110 ret = posix_memalign ((void * * )& in_buf , pagesize , buf_size );
106111 if (ret )
107- err (EINVAL , "%s: failed to allocate in-buf" , __func__ );
112+ err_errno (EINVAL , "%s: failed to allocate in-buf" , __func__ );
108113
109114 ret = posix_memalign ((void * * )& out_buf , pagesize , buf_size );
110115 if (ret )
111- err (EINVAL , "%s: failed to allocate out-buf" , __func__ );
116+ err_errno (EINVAL , "%s: failed to allocate out-buf" , __func__ );
112117
113118 fd = open ("/dev/urandom" , O_RDONLY );
114119 if (fd < 0 )
115- err (EINVAL , "%s: failed to open urandom" , __func__ );
120+ err_errno (EINVAL , "%s: failed to open urandom" , __func__ );
116121
117122 ret = read (fd , out_buf , buf_size );
118123 if (ret < 0 )
119- err (EINVAL , "%s: failed to randomize output buffer" , __func__ );
124+ err_errno (EINVAL , "%s: failed to randomize output buffer" ,
125+ __func__ );
120126
121127 close (fd );
122128}
@@ -125,7 +131,7 @@ static void __compare(void *a, void *b, size_t size, const char *test)
125131{
126132 if (!memcmp (a , b , size ))
127133 return ;
128- err (EIO , "%s: data corruption" , test );
134+ err_errno (EIO , "%s: data corruption" , test );
129135}
130136#define compare (a , b , size ) __compare(a, b, size, __func__)
131137
@@ -143,11 +149,11 @@ static void test_full_size_aligned()
143149 memset (in_buf , 0 , buf_size );
144150 ret = pwrite (test_fd , out_buf , buf_size , 0 );
145151 if (ret < 0 )
146- err (errno , "%s: failed to write buf" , __func__ );
152+ err (EXIT_FAILURE , "%s: failed to write buf" , __func__ );
147153
148154 ret = pread (test_fd , in_buf , buf_size , 0 );
149155 if (ret < 0 )
150- err (errno , "%s: failed to read buf" , __func__ );
156+ err (EXIT_FAILURE , "%s: failed to read buf" , __func__ );
151157
152158 compare (out_buf , in_buf , buf_size );
153159}
@@ -164,11 +170,11 @@ static void test_dma_aligned()
164170 memset (in_buf , 0 , buf_size );
165171 ret = pwrite (test_fd , out_buf + dma_alignment , max_bytes , 0 );
166172 if (ret < 0 )
167- err (errno , "%s: failed to write buf" , __func__ );
173+ err (EXIT_FAILURE , "%s: failed to write buf" , __func__ );
168174
169175 ret = pread (test_fd , in_buf + dma_alignment , max_bytes , 0 );
170176 if (ret < 0 )
171- err (errno , "%s: failed to read buf" , __func__ );
177+ err (EXIT_FAILURE , "%s: failed to read buf" , __func__ );
172178
173179 compare (out_buf + dma_alignment , in_buf + dma_alignment , max_bytes );
174180}
@@ -194,7 +200,7 @@ static void test_page_aligned_vectors()
194200
195201 ret = pwritev (test_fd , iov , vecs , 0 );
196202 if (ret < 0 )
197- err (errno , "%s: failed to write buf" , __func__ );
203+ err (EXIT_FAILURE , "%s: failed to write buf" , __func__ );
198204
199205 for (i = 0 ; i < vecs ; i ++ ) {
200206 offset = logical_block_size * i * 4 ;
@@ -204,7 +210,7 @@ static void test_page_aligned_vectors()
204210
205211 ret = preadv (test_fd , iov , vecs , 0 );
206212 if (ret < 0 )
207- err (errno , "%s: failed to read buf" , __func__ );
213+ err (EXIT_FAILURE , "%s: failed to read buf" , __func__ );
208214
209215 for (i = 0 ; i < vecs ; i ++ ) {
210216 offset = logical_block_size * i * 4 ;
@@ -234,7 +240,7 @@ static void test_dma_aligned_vectors()
234240
235241 ret = pwritev (test_fd , iov , vecs , 0 );
236242 if (ret < 0 )
237- err (errno , "%s: failed to write buf" , __func__ );
243+ err (EXIT_FAILURE , "%s: failed to write buf" , __func__ );
238244
239245 for (i = 0 ; i < vecs ; i ++ ) {
240246 offset = logical_block_size * i * 8 + dma_alignment * (i + 1 );
@@ -244,7 +250,7 @@ static void test_dma_aligned_vectors()
244250
245251 ret = preadv (test_fd , iov , vecs , 0 );
246252 if (ret < 0 )
247- err (errno , "%s: failed to read buf" , __func__ );
253+ err (EXIT_FAILURE , "%s: failed to read buf" , __func__ );
248254
249255 for (i = 0 ; i < vecs ; i ++ ) {
250256 offset = logical_block_size * i * 8 + dma_alignment * (i + 1 );
@@ -295,7 +301,7 @@ static void test_unaligned_page_vectors()
295301 if (ret < 0 ) {
296302 if (should_fail )
297303 return ;
298- err (errno , "%s: failed to write buf" , __func__ );
304+ err (EXIT_FAILURE , "%s: failed to write buf" , __func__ );
299305 }
300306
301307 i = 0 ;
@@ -318,7 +324,7 @@ static void test_unaligned_page_vectors()
318324
319325 ret = preadv (test_fd , iov , vecs , 0 );
320326 if (ret < 0 )
321- err (errno , "%s: failed to read buf" , __func__ );
327+ err (EXIT_FAILURE , "%s: failed to read buf" , __func__ );
322328
323329 i = 0 ;
324330 offset = pagesize - (logical_block_size / 4 );
@@ -367,7 +373,7 @@ static void test_unaligned_vectors()
367373
368374 ret = preadv (test_fd , iov , vecs , 0 );
369375 if (ret < 0 )
370- err (errno , "%s: failed to read buf" , __func__ );
376+ err (EXIT_FAILURE , "%s: failed to read buf" , __func__ );
371377
372378 for (i = 0 ; i < vecs ; i ++ ) {
373379 offset = logical_block_size * i * 8 ;
@@ -400,8 +406,9 @@ static void test_invalid_starting_addr()
400406 if (ret < 0 )
401407 return ;
402408
403- err (ENOTSUP , "%s: write buf unexpectedly succeeded with NULL address ret:%d" ,
404- __func__ , ret );
409+ err_errno (ENOTSUP ,
410+ "%s: write buf unexpectedly succeeded with NULL address ret:%d" ,
411+ __func__ , ret );
405412}
406413
407414/*
@@ -436,8 +443,9 @@ static void test_invalid_middle_addr()
436443 if (ret < 0 )
437444 return ;
438445
439- err (ENOTSUP , "%s: write buf unexpectedly succeeded with NULL address ret:%d" ,
440- __func__ , ret );
446+ err_errno (ENOTSUP ,
447+ "%s: write buf unexpectedly succeeded with NULL address ret:%d" ,
448+ __func__ , ret );
441449}
442450
443451/*
@@ -458,16 +466,17 @@ static void test_invalid_dma_alignment()
458466 if (ret < 0 ) {
459467 if (should_fail )
460468 return ;
461- err (errno , "%s: failed to write buf" , __func__ );
469+ err (EXIT_FAILURE , "%s: failed to write buf" , __func__ );
462470 }
463471
464472 if (should_fail )
465- err (ENOTSUP , "%s: write buf unexpectedly succeeded with invalid DMA offset address, ret:%d" ,
466- __func__ , ret );
473+ err_errno (ENOTSUP ,
474+ "%s: write buf unexpectedly succeeded with invalid DMA offset address, ret:%d" ,
475+ __func__ , ret );
467476
468477 ret = pread (test_fd , in_buf + offset , size , 0 );
469478 if (ret < 0 )
470- err (errno , "%s: failed to read buf" , __func__ );
479+ err (EXIT_FAILURE , "%s: failed to read buf" , __func__ );
471480
472481 compare (out_buf + offset , in_buf + offset , size );
473482}
@@ -506,11 +515,12 @@ static void test_invalid_dma_vector_alignment()
506515 if (ret < 0 ) {
507516 if (should_fail )
508517 return ;
509- err (errno , "%s: failed to write buf" , __func__ );
518+ err (EXIT_FAILURE , "%s: failed to write buf" , __func__ );
510519 }
511520 if (should_fail )
512- err (ENOTSUP , "%s: write buf unexpectedly succeeded with invalid DMA offset address ret:%d" ,
513- __func__ , ret );
521+ err_errno (ENOTSUP ,
522+ "%s: write buf unexpectedly succeeded with invalid DMA offset address ret:%d" ,
523+ __func__ , ret );
514524
515525 iov [0 ].iov_base = in_buf ;
516526 iov [0 ].iov_len = max_bytes ;
@@ -529,7 +539,7 @@ static void test_invalid_dma_vector_alignment()
529539
530540 ret = preadv (test_fd , iov , vecs , 0 );
531541 if (ret < 0 )
532- err (errno , "%s: failed to read buf" , __func__ );
542+ err (EXIT_FAILURE , "%s: failed to read buf" , __func__ );
533543
534544 compare (out_buf , in_buf , max_bytes );
535545 compare (out_buf + max_bytes * 2 , in_buf + max_bytes * 2 , max_bytes );
@@ -568,12 +578,13 @@ static void test_max_vector_limits()
568578 if (ret < 0 ) {
569579 if (should_fail )
570580 return ;
571- err (errno , "%s: failed to write buf" , __func__ );
581+ err (EXIT_FAILURE , "%s: failed to write buf" , __func__ );
572582 }
573583
574584 if (should_fail )
575- err (ENOTSUP , "%s: write buf unexpectedly succeeded with excess vectors ret:%d" ,
576- __func__ , ret );
585+ err_errno (ENOTSUP ,
586+ "%s: write buf unexpectedly succeeded with excess vectors ret:%d" ,
587+ __func__ , ret );
577588
578589 for (i = 0 ; i < vecs ; i ++ ) {
579590 offset = i * iov_size * 2 ;
@@ -583,7 +594,7 @@ static void test_max_vector_limits()
583594
584595 ret = preadv (test_fd , iov , vecs , 0 );
585596 if (ret < 0 )
586- err (errno , "%s: failed to read buf" , __func__ );
597+ err (EXIT_FAILURE , "%s: failed to read buf" , __func__ );
587598
588599 for (i = 0 ; i < vecs ; i ++ ) {
589600 offset = i * iov_size * 2 ;
@@ -622,8 +633,9 @@ static void test_invalid_dma_vector_alignment_large()
622633 if (ret < 0 )
623634 return ;
624635
625- err (ENOTSUP , "%s: write buf unexpectedly succeeded with NULL address ret:%d" ,
626- __func__ , ret );
636+ err_errno (ENOTSUP ,
637+ "%s: write buf unexpectedly succeeded with NULL address ret:%d" ,
638+ __func__ , ret );
627639}
628640
629641/*
@@ -656,12 +668,13 @@ static void test_invalid_dma_vector_length()
656668 if (ret < 0 ) {
657669 if (should_fail )
658670 return ;
659- err (errno , "%s: failed to write buf" , __func__ );
671+ err (EXIT_FAILURE , "%s: failed to write buf" , __func__ );
660672 }
661673
662674 if (should_fail )
663- err (ENOTSUP , "%s: write buf unexpectedly succeeded with invalid DMA offset address ret:%d" ,
664- __func__ , ret );
675+ err_errno (ENOTSUP ,
676+ "%s: write buf unexpectedly succeeded with invalid DMA offset address ret:%d" ,
677+ __func__ , ret );
665678
666679 iov [0 ].iov_base = in_buf ;
667680 iov [0 ].iov_len = max_bytes * 2 - max_bytes / 2 ;
@@ -677,7 +690,7 @@ static void test_invalid_dma_vector_length()
677690
678691 ret = pwritev (test_fd , iov , vecs , 0 );
679692 if (ret < 0 )
680- err (errno , "%s: failed to read buf" , __func__ );
693+ err (EXIT_FAILURE , "%s: failed to read buf" , __func__ );
681694
682695 compare (out_buf , in_buf , iov [0 ].iov_len );
683696 compare (out_buf + max_bytes * 4 , in_buf + max_bytes * 4 , iov [1 ].iov_len );
0 commit comments