X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fread_write_all%2Ftest%2Frun-write_all.c;h=c789d6b7829b3b05243ce7ae9d6bd679cfc8735f;hb=a8722345053b7cd860499aa31fd6bb414c120cc8;hp=5b310688aea6283ec178515c92ed81d2e4474a18;hpb=8201ff8eec2f44e9612fea3ec280c8bd10b7300c;p=ccan diff --git a/ccan/read_write_all/test/run-write_all.c b/ccan/read_write_all/test/run-write_all.c index 5b310688..c789d6b7 100644 --- a/ccan/read_write_all/test/run-write_all.c +++ b/ccan/read_write_all/test/run-write_all.c @@ -1,7 +1,4 @@ -/* FIXME: Do something tricky to ensure we really do loop in write_all. */ - #include -#include #include #include #include @@ -13,57 +10,62 @@ #include #include -static volatile int sigcount; -static void got_signal(int sig) +static ssize_t test_write(int fd, const void *buf, size_t count); +#define write test_write +#include +#undef write + +static ssize_t write_return; + +static ssize_t test_write(int fd, const void *buf, size_t count) { - sigcount++; + (void)fd; + (void)buf; + + if (write_return == 0) { + errno = ENOSPC; + return 0; + } + + if (write_return < 0) { + errno = -write_return; + /* Don't return EINTR more than once! */ + if (errno == EINTR) + write_return = count; + return -1; + } + + if (write_return < count) + return write_return; + return count; } -/* < PIPE_BUF *will* be atomic. But > PIPE_BUF only *might* be non-atomic. */ -#define BUFSZ (1024*1024) +#define BUFSZ 1024 -int main(int argc, char *argv[]) +int main(void) { char *buffer; - int p2c[2]; - int status; - pid_t child; - buffer = calloc(BUFSZ, 1); - plan_tests(4); + buffer = malloc(BUFSZ); + plan_tests(8); - /* We fork and torture parent. */ - if (pipe(p2c) != 0) - err(1, "pipe"); - child = fork(); + write_return = -ENOSPC; + ok1(!write_all(100, buffer, BUFSZ)); + ok1(errno == ENOSPC); - if (!child) { - close(p2c[1]); - /* Make sure they started writing. */ - if (read(p2c[0], buffer, 1) != 1) - exit(1); - if (kill(getppid(), SIGUSR1) != 0) - exit(2); - if (!read_all(p2c[0], buffer+1, BUFSZ-1)) - exit(3); - if (memchr(buffer, 0, BUFSZ)) { - fprintf(stderr, "buffer has 0 at offset %ti\n", - memchr(buffer, 0, BUFSZ) - (void *)buffer); - exit(4); - } - exit(0); - } - if (child == -1) - err(1, "forking"); + write_return = -EINTR; + ok1(write_all(100, buffer, BUFSZ)); + ok1(errno == EINTR); + + write_return = 1; + errno = 0; + ok1(write_all(100, buffer, BUFSZ)); + ok1(errno == 0); + + write_return = BUFSZ; + ok1(write_all(100, buffer, BUFSZ)); + ok1(errno == 0); + free(buffer); - close(p2c[0]); - memset(buffer, 0xff, BUFSZ); - signal(SIGUSR1, got_signal); - ok1(write_all(p2c[1], buffer, BUFSZ)); - ok1(sigcount == 1); - ok1(wait(&status) == child); - ok(WIFEXITED(status) && WEXITSTATUS(status) == 0, - "WIFEXITED(status) = %u, WEXITSTATUS(status) = %u", - WIFEXITED(status), WEXITSTATUS(status)); return exit_status(); }