2 * Version of same in parent directory using wait6
11 #include "tests/sys_mman.h"
13 #include <sys/types.h>
15 #include <sys/resource.h>
16 #include <sys/signal.h>
20 /** Lock an entire file exclusively.
22 * @return 1 upon success, 0 upon failure.
24 static int lock_file(const int fd
)
28 fl
.l_type
= F_WRLCK
; /* exclusive lock */
29 fl
.l_whence
= SEEK_SET
;
31 fl
.l_len
= 0; /* lock entire file */
33 return fcntl(fd
, F_SETLK
, &fl
) >= 0;
36 static int open_lock_and_map(const char* const process_name
,
37 const char* const filename
)
42 fd
= open(filename
, O_RDWR
| O_CREAT
, S_IRUSR
| S_IWUSR
);
49 flags
= fcntl(fd
, F_GETFD
);
51 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) < 0)
54 fprintf(stderr
, "%s: about to lock file for writing.\n", process_name
);
61 fprintf(stderr
, "%s: file locking attempt succeeded.\n", process_name
);
62 if (mmap(NULL
, 1, PROT_WRITE
, MAP_SHARED
, fd
, 0) == 0)
77 int main(int argc
, char *argv
[])
84 snprintf(filename
, sizeof(filename
), "/tmp/valgrind-file-locking-test.%ld",
89 if ((fd1
= open_lock_and_map("parent", filename
)) >= 0)
102 fd2
= open_lock_and_map("child", filename
);
115 struct __wrusage wrusage
;
117 wait_result
= wait6(P_PID
, fork_result
, &child_status
, WEXITED
, &wrusage
, &info
);
118 assert(wait_result
>= 0);
127 fprintf(stderr
, "Test finished.\n");