math/gen: yet another little decimal formatting tool
[libc-test.git] / src / functional / fcntl.c
blob485c00cf93d7ac1c83ec1c28752a564eedfabef1
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <sys/wait.h>
7 #include "test.h"
9 #define TEST(c, ...) ((c) ? 1 : (t_error(#c" failed: " __VA_ARGS__),0))
10 #define TESTE(c) (errno=0, TEST(c, "errno = %s\n", strerror(errno)))
12 int main(void)
14 struct flock fl = {0};
15 FILE *f;
16 int fd;
17 pid_t pid;
18 int status;
20 if (!TESTE(f=tmpfile())) return t_status;
21 fd = fileno(f);
23 fl.l_type = F_WRLCK;
24 fl.l_whence = SEEK_SET;
25 fl.l_start = 0;
26 fl.l_len = 0;
27 TESTE(fcntl(fd, F_SETLK, &fl)==0);
29 pid = fork();
30 if (!pid) {
31 fl.l_type = F_RDLCK;
32 _exit(fcntl(fd, F_SETLK, &fl)==0 ||
33 (errno!=EAGAIN && errno!=EACCES));
35 while (waitpid(pid, &status, 0)<0 && errno==EINTR);
36 TEST(status==0, "lock failed to work\n");
38 pid = fork();
39 if (!pid) {
40 fl.l_type = F_WRLCK;
41 _exit(fcntl(fd, F_GETLK, &fl) || fl.l_pid != getppid());
43 while (waitpid(pid, &status, 0)<0 && errno==EINTR);
44 TEST(status==0, "child failed to detect lock held by parent\n");
46 fclose(f);
48 return t_status;