121
121
#include <sys/fs/zfs.h>
122
122
#include <libnvpair.h>
123
123
124
- #define ZTEST_FD_DATA 3
125
- #define ZTEST_FD_RAND 4
124
+ static int ztest_fd_data = -1 ;
125
+ static int ztest_fd_rand = -1 ;
126
126
127
127
typedef struct ztest_shared_hdr {
128
128
uint64_t zh_hdr_size ;
@@ -783,10 +783,12 @@ ztest_random(uint64_t range)
783
783
{
784
784
uint64_t r ;
785
785
786
+ ASSERT3S (ztest_fd_rand , >=, 0 );
787
+
786
788
if (range == 0 )
787
789
return (0 );
788
790
789
- if (read (ZTEST_FD_RAND , & r , sizeof (r )) != sizeof (r ))
791
+ if (read (ztest_fd_rand , & r , sizeof (r )) != sizeof (r ))
790
792
fatal (1 , "short read from /dev/urandom" );
791
793
792
794
return (r % range );
@@ -5815,26 +5817,13 @@ ztest_init(ztest_shared_t *zs)
5815
5817
}
5816
5818
5817
5819
static void
5818
- setup_fds (void )
5820
+ setup_data_fd (void )
5819
5821
{
5820
- int fd ;
5821
-
5822
5822
char * tmp = tempnam (NULL , NULL );
5823
- fd = open (tmp , O_RDWR | O_CREAT , 0700 );
5824
- ASSERT3S (fd , >=, 0 );
5825
- if (fd != ZTEST_FD_DATA ) {
5826
- VERIFY3S (dup2 (fd , ZTEST_FD_DATA ), = = , ZTEST_FD_DATA );
5827
- close (fd );
5828
- }
5823
+ ztest_fd_data = open (tmp , O_RDWR | O_CREAT , 0700 );
5824
+ ASSERT3S (ztest_fd_data , >=, 0 );
5829
5825
(void ) unlink (tmp );
5830
5826
free (tmp );
5831
-
5832
- fd = open ("/dev/urandom" , O_RDONLY );
5833
- ASSERT3S (fd , >=, 0 );
5834
- if (fd != ZTEST_FD_RAND ) {
5835
- VERIFY3S (dup2 (fd , ZTEST_FD_RAND ), = = , ZTEST_FD_RAND );
5836
- close (fd );
5837
- }
5838
5827
}
5839
5828
5840
5829
static int
@@ -5858,10 +5847,10 @@ setup_hdr(void)
5858
5847
ztest_shared_hdr_t * hdr ;
5859
5848
5860
5849
hdr = (void * )mmap (0 , P2ROUNDUP (sizeof (* hdr ), getpagesize ()),
5861
- PROT_READ | PROT_WRITE , MAP_SHARED , ZTEST_FD_DATA , 0 );
5850
+ PROT_READ | PROT_WRITE , MAP_SHARED , ztest_fd_data , 0 );
5862
5851
ASSERT (hdr != MAP_FAILED );
5863
5852
5864
- VERIFY3U (0 , = = , ftruncate (ZTEST_FD_DATA , sizeof (ztest_shared_hdr_t )));
5853
+ VERIFY3U (0 , = = , ftruncate (ztest_fd_data , sizeof (ztest_shared_hdr_t )));
5865
5854
5866
5855
hdr -> zh_hdr_size = sizeof (ztest_shared_hdr_t );
5867
5856
hdr -> zh_opts_size = sizeof (ztest_shared_opts_t );
@@ -5872,7 +5861,7 @@ setup_hdr(void)
5872
5861
hdr -> zh_ds_count = ztest_opts .zo_datasets ;
5873
5862
5874
5863
size = shared_data_size (hdr );
5875
- VERIFY3U (0 , = = , ftruncate (ZTEST_FD_DATA , size ));
5864
+ VERIFY3U (0 , = = , ftruncate (ztest_fd_data , size ));
5876
5865
5877
5866
(void ) munmap ((caddr_t )hdr , P2ROUNDUP (sizeof (* hdr ), getpagesize ()));
5878
5867
}
@@ -5885,14 +5874,14 @@ setup_data(void)
5885
5874
uint8_t * buf ;
5886
5875
5887
5876
hdr = (void * )mmap (0 , P2ROUNDUP (sizeof (* hdr ), getpagesize ()),
5888
- PROT_READ , MAP_SHARED , ZTEST_FD_DATA , 0 );
5877
+ PROT_READ , MAP_SHARED , ztest_fd_data , 0 );
5889
5878
ASSERT (hdr != MAP_FAILED );
5890
5879
5891
5880
size = shared_data_size (hdr );
5892
5881
5893
5882
(void ) munmap ((caddr_t )hdr , P2ROUNDUP (sizeof (* hdr ), getpagesize ()));
5894
5883
hdr = ztest_shared_hdr = (void * )mmap (0 , P2ROUNDUP (size , getpagesize ()),
5895
- PROT_READ | PROT_WRITE , MAP_SHARED , ZTEST_FD_DATA , 0 );
5884
+ PROT_READ | PROT_WRITE , MAP_SHARED , ztest_fd_data , 0 );
5896
5885
ASSERT (hdr != MAP_FAILED );
5897
5886
buf = (uint8_t * )hdr ;
5898
5887
@@ -5925,9 +5914,15 @@ exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
5925
5914
5926
5915
if (pid == 0 ) { /* child */
5927
5916
char * emptyargv [2 ] = { cmd , NULL };
5917
+ char fd_data_str [12 ];
5928
5918
5929
5919
struct rlimit rl = { 1024 , 1024 };
5930
5920
(void ) setrlimit (RLIMIT_NOFILE , & rl );
5921
+
5922
+ (void ) close (ztest_fd_rand );
5923
+ VERIFY (11 >= snprintf (fd_data_str , 12 , "%d" , ztest_fd_data ));
5924
+ VERIFY (0 == setenv ("ZTEST_FD_DATA" , fd_data_str , 1 ));
5925
+
5931
5926
(void ) enable_extended_FILE_stdio (-1 , -1 );
5932
5927
if (libpath != NULL )
5933
5928
VERIFY (0 == setenv ("LD_LIBRARY_PATH" , libpath , 1 ));
@@ -6005,22 +6000,24 @@ main(int argc, char **argv)
6005
6000
char cmd [MAXNAMELEN ];
6006
6001
boolean_t hasalt ;
6007
6002
int f ;
6008
- boolean_t ischild = (0 == lseek (ZTEST_FD_DATA , 0 , SEEK_CUR ));
6009
-
6010
- ASSERT (ischild || errno == EBADF || errno == ESPIPE );
6003
+ char * fd_data_str = getenv ("ZTEST_FD_DATA" );
6011
6004
6012
6005
(void ) setvbuf (stdout , NULL , _IOLBF , 0 );
6013
6006
6014
- if (!ischild ) {
6007
+ ztest_fd_rand = open ("/dev/urandom" , O_RDONLY );
6008
+ ASSERT3S (ztest_fd_rand , >=, 0 );
6009
+
6010
+ if (!fd_data_str ) {
6015
6011
dprintf_setup (& argc , argv );
6016
6012
process_options (argc , argv );
6017
6013
6018
- setup_fds ();
6014
+ setup_data_fd ();
6019
6015
setup_hdr ();
6020
6016
setup_data ();
6021
6017
bcopy (& ztest_opts , ztest_shared_opts ,
6022
6018
sizeof (* ztest_shared_opts ));
6023
6019
} else {
6020
+ ztest_fd_data = atoi (fd_data_str );
6024
6021
setup_data ();
6025
6022
bcopy (ztest_shared_opts , & ztest_opts , sizeof (ztest_opts ));
6026
6023
}
@@ -6029,12 +6026,12 @@ main(int argc, char **argv)
6029
6026
/* Override location of zpool.cache */
6030
6027
(void ) asprintf ((char * * )& spa_config_path , "%s/zpool.cache" ,
6031
6028
ztest_opts .zo_dir );
6032
-
6029
+
6033
6030
ztest_ds = umem_alloc (ztest_opts .zo_datasets * sizeof (ztest_ds_t ),
6034
6031
UMEM_NOFAIL );
6035
6032
zs = ztest_shared ;
6036
6033
6037
- if (ischild ) {
6034
+ if (fd_data_str ) {
6038
6035
metaslab_gang_bang = ztest_opts .zo_metaslab_gang_bang ;
6039
6036
metaslab_df_alloc_threshold =
6040
6037
zs -> zs_metaslab_df_alloc_threshold ;
0 commit comments