Files
Ammar Faizi 1ae61f6898 test: Add missing SPDX-License-Identifier
This adds missing SPDX-License-Identifier for:
  test/ce593a6c480a-test.c
  test/double-poll-crash.c
  test/exec-target.c
  test/lfs-openat-write.c
  test/lfs-openat.c
  test/pipe-eof.c
  test/pollfree.c
  test/sendmsg_fs_cve.c
  test/skip-cqe.c
  test/splice.c
  test/sq-poll-kthread.c
  test/sqpoll-cancel-hang.c
  test/sqpoll-disable-exit.c
  test/sqpoll-exit-hang.c
  test/sqpoll-sleep.c

Signed-off-by: Ammar Faizi <ammarfaizi2@gnuweeb.org>
2022-02-23 17:06:36 +07:00

84 lines
1.4 KiB
C

/* SPDX-License-Identifier: MIT */
/*
* Test that closed pipe reads returns 0, instead of waiting for more
* data.
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include "liburing.h"
#define BUFSIZE 512
struct data {
char *str;
int fds[2];
};
static void *t(void *data)
{
struct data *d = data;
int ret;
strcpy(d->str, "This is a test string");
ret = write(d->fds[1], d->str, strlen(d->str));
close(d->fds[1]);
if (ret < 0)
perror("write");
return NULL;
}
int main(int argc, char *argv[])
{
static char buf[BUFSIZE];
struct io_uring ring;
pthread_t thread;
struct data d;
int ret;
if (pipe(d.fds) < 0) {
perror("pipe");
return 1;
}
d.str = buf;
io_uring_queue_init(8, &ring, 0);
pthread_create(&thread, NULL, t, &d);
while (1) {
struct io_uring_sqe *sqe;
struct io_uring_cqe *cqe;
sqe = io_uring_get_sqe(&ring);
io_uring_prep_read(sqe, d.fds[0], buf, BUFSIZE, 0);
ret = io_uring_submit(&ring);
if (ret != 1) {
fprintf(stderr, "submit: %d\n", ret);
return 1;
}
ret = io_uring_wait_cqe(&ring, &cqe);
if (ret) {
fprintf(stderr, "wait: %d\n", ret);
return 1;
}
if (cqe->res < 0) {
fprintf(stderr, "Read error: %s\n", strerror(-cqe->res));
return 1;
}
if (cqe->res == 0)
break;
io_uring_cqe_seen(&ring, cqe);
}
pthread_join(thread, NULL);
io_uring_queue_exit(&ring);
return 0;
}