11#include  <stdio.h> 
22#include  <stdlib.h> 
3+ #include  <string.h> 
34#include  <sys/types.h> 
45#include  <sys/stat.h> 
56#include  <unistd.h> 
67#include  <fcntl.h> 
78#include  <errno.h> 
89
10+ #include  "t/lib/tap.h" 
11+ #include  "t/lib/testutil.h" 
12+ 
913#include  "mpi.h" 
1014#include  "unifyfs.h" 
1115
16+ #define  all_ok (condition , comm , ...) \
17+     do { \
18+         int input = (int) condition; \
19+         int output; \
20+         MPI_Allreduce(&input, &output, 1, MPI_INT, MPI_LAND, comm); \
21+         int rank; \
22+         MPI_Comm_rank(comm, &rank); \
23+         if (rank == 0) { \
24+             ok_at_loc(__FILE__, __LINE__, output, __VA_ARGS__, NULL); \
25+         } \
26+     } while (0)
27+ 
1228int  mpi_sum (int  val )
1329{
1430    int  sum ;
@@ -37,7 +53,7 @@ int main(int argc, char* argv[])
3753    MPI_Comm_rank (MPI_COMM_WORLD , & rank );
3854    MPI_Comm_size (MPI_COMM_WORLD , & ranks );
3955
40-     int   ret   =   unifyfs_mount (mountpoint , rank , ranks , 0 );
56+     unifyfs_mount (mountpoint , rank , ranks , 0 );
4157
4258    char  file [256 ];
4359    sprintf (file , "%s/testfile" , mountpoint );
@@ -53,7 +69,7 @@ int main(int argc, char* argv[])
5369    errno  =  0 ;
5470    int  success  =  0 ;
5571    int  eexist   =  0 ;
56-     int  fd  =  open (file , O_WRONLY  | O_CREAT  | O_EXCL );
72+     int  fd  =  open (file , O_WRONLY  | O_CREAT  | O_EXCL ,  S_IRXWU );
5773    if  (fd  >= 0 ) {
5874        success  =  1 ;
5975        close (fd );
@@ -63,15 +79,13 @@ int main(int argc, char* argv[])
6379
6480    /* one rank should win */ 
6581    int  sum  =  mpi_sum (success );
66-     if   (sum  !=   1 ) { 
67-     } 
82+     all_ok (sum  ==   1 ,  MPI_COMM_WORLD , 
83+          "More than one process opened file in exclusive mode" ); 
6884
6985    /* all others should get EEXIST */ 
7086    sum  =  mpi_sum (eexist );
71-     if  (sum  !=  (ranks  -  1 )) {
72-     }
73- 
74-     MPI_Barrier (MPI_COMM_WORLD );
87+     all_ok (sum  ==  (ranks  -  1 ), MPI_COMM_WORLD ,
88+         "All but one process should get EEXIST when opening file in exclusive mode" );
7589
7690    /* all delete, 
7791     * one rank should win, all others should get ENOENT */ 
@@ -87,31 +101,26 @@ int main(int argc, char* argv[])
87101
88102    /* one winner */ 
89103    sum  =  mpi_sum (success );
90-     if   (sum  !=   1 ) { 
91-     } 
104+     all_ok (sum  ==   1 ,  MPI_COMM_WORLD , 
105+          "More than one process got success on unlink of the same file" ); 
92106
93107    /* everyone else should get ENOENT */ 
94108    sum  =  mpi_sum (enoent );
95-     if  (sum  !=  (ranks  -  1 )) {
96-     }
97- 
98-     MPI_Barrier (MPI_COMM_WORLD );
109+     all_ok (sum  ==  (ranks  -  1 ), MPI_COMM_WORLD ,
110+         "All but one process should get ENOENT when unlinking the same file" );
99111
100112    /* all create, this time not exclusive */ 
101113    errno  =  0 ;
102114    success  =  0 ;
103-     fd  =  open (file , O_WRONLY  | O_CREAT  | O_TRUNC );
115+     fd  =  open (file , O_WRONLY  | O_CREAT  | O_TRUNC ,  S_IRXWU );
104116    if  (fd  >= 0 ) {
105117        success  =  1 ;
106118        close (fd );
107119    }
108120
109121    /* all should succeed */ 
110-     sum  =  mpi_sum (success );
111-     if  (sum  !=  ranks ) {
112-     }
113- 
114-     MPI_Barrier (MPI_COMM_WORLD );
122+     all_ok (success , MPI_COMM_WORLD ,
123+         "All processes should open file with O_CREAT and not O_EXCL" );
115124
116125    /* open file for writing */ 
117126    errno  =  0 ;
@@ -123,9 +132,8 @@ int main(int argc, char* argv[])
123132    }
124133
125134    /* all should succeeed */ 
126-     sum  =  mpi_sum (success );
127-     if  (sum  !=  ranks ) {
128-     }
135+     all_ok (success , MPI_COMM_WORLD ,
136+         "All processes should open file with O_CREAT for writing" );
129137
130138    MPI_Barrier (MPI_COMM_WORLD );
131139
@@ -135,30 +143,37 @@ int main(int argc, char* argv[])
135143
136144    /* have all ranks write to a different section of the file, 
137145     * then open file with truncate on one rank to verify size change */ 
138-     fd  =  open (file , O_WRONLY  | O_CREAT );
146+     success  =  0 ;
147+     fd  =  open (file , O_WRONLY  | O_CREAT , S_IRXWU );
139148    if  (fd  >= 0 ) {
149+         success  =  1 ;
140150        off_t  offset  =  (off_t ) (rank  *  bufsize );
141-         pwrite (fd , buf , bufsize , offset );
151+         ssize_t  nwritten  =  pwrite (fd , buf , bufsize , offset );
152+         if  (nwritten  !=  bufsize ) {
153+             success  =  0 ;
154+         }
142155        fsync (fd );
143156        close (fd );
144157    }
145158    MPI_Barrier (MPI_COMM_WORLD );
146159    if  (rank  ==  0 ) {
147160        /* all ranks should have written some data */ 
148161        off_t  size  =  getsize (file );
149-         if  (size  !=  bufsize  *  ranks ) {
150-         }
162+         ok (size  ==  bufsize  *  ranks ,
163+             "File size %lu does not match expected size %lu" ,
164+             size , bufsize  *  ranks );
151165
152166        /* create file with truncate */ 
153-         fd  =  open (file , O_WRONLY  | O_CREAT  | O_TRUNC );
167+         fd  =  open (file , O_WRONLY  | O_CREAT  | O_TRUNC ,  S_IRXWU );
154168        if  (fd  >= 0 ) {
155169            close (fd );
156170        }
157171
158172        /* now file should be 0 length again */ 
159173        size  =  getsize (file );
160-         if  (size  !=  0 ) {
161-         }
174+         ok (size  ==  0 ,
175+             "File size %lu does not match expected size %lu" ,
176+             size , 0 );
162177    }
163178    MPI_Barrier (MPI_COMM_WORLD );
164179
0 commit comments