]> git.ozlabs.org Git - ccan/blob - ccan/read_write_all/test/run-write_all.c
fbfa1f99aebcbca18b37fe580fa05b3a388ff432
[ccan] / ccan / read_write_all / test / run-write_all.c
1 /* FIXME: Do something tricky to ensure we really do loop in write_all. */
2
3 #include "read_write_all/read_write_all.h"
4 #include "read_write_all/read_write_all.c"
5 #include "tap/tap.h"
6 #include <unistd.h>
7 #include <sys/types.h>
8 #include <signal.h>
9 #include <limits.h>
10 #include <sys/wait.h>
11 #include <err.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <stdio.h>
15
16 static volatile int sigcount;
17 static void got_signal(int sig)
18 {
19         sigcount++;
20 }
21
22 /* < PIPE_BUF *will* be atomic.  But > PIPE_BUF only *might* be non-atomic. */
23 #define BUFSZ (1024*1024)
24
25 int main(int argc, char *argv[])
26 {
27         char buffer[BUFSZ*2] = { 0 };
28         int p2c[2];
29         int status;
30         pid_t child;
31
32         plan_tests(4);
33
34         /* We fork and torture parent. */
35         if (pipe(p2c) != 0)
36                 err(1, "pipe");
37         child = fork();
38
39         if (!child) {
40                 /* Make sure they started writing. */
41                 if (read(p2c[0], buffer, 1) != 1)
42                         exit(1);
43                 if (kill(getppid(), SIGUSR1) != 0)
44                         exit(2);
45                 if (!read_all(p2c[0], buffer+1, sizeof(buffer)-1))
46                         exit(3);
47                 if (memchr(buffer, 0, sizeof(buffer))) {
48                         fprintf(stderr, "buffer has 0 at offset %i\n",
49                                 memchr(buffer, 0, sizeof(buffer)) - (void *)buffer);
50                         exit(4);
51                 }
52                 exit(0);
53         }
54         if (child == -1)
55                 err(1, "forking");
56
57         memset(buffer, 0xff, sizeof(buffer));
58         signal(SIGUSR1, got_signal);
59         ok1(write_all(p2c[1], buffer, sizeof(buffer)));
60         ok1(sigcount == 1);
61         ok1(wait(&status) == child);
62         ok(WIFEXITED(status) && WEXITSTATUS(status) == 0,
63            "WIFEXITED(status) = %u, WEXITSTATUS(status) = %u",
64            WIFEXITED(status), WEXITSTATUS(status));
65         return exit_status();
66 }