Skip to content

Instantly share code, notes, and snippets.

@haesbaert
Created September 26, 2022 13:59
Show Gist options
  • Save haesbaert/10d3e3bb5fa9171dfcf65e1f5b58e95c to your computer and use it in GitHub Desktop.
Save haesbaert/10d3e3bb5fa9171dfcf65e1f5b58e95c to your computer and use it in GitHub Desktop.
#include <liburing.h>
#include <err.h>
#include <stdio.h>
#include <string.h> /* bzero(3) */
#include <unistd.h>
int
pipe_bug(int blocking)
{
struct io_uring_params p;
struct io_uring ring;
struct io_uring_sqe *sqe;
struct io_uring_cqe *cqe;
char buf[1024];
int ret;
int fds[2];
/* printf("%s blocking=%d\n", __func__, blocking); */
if (pipe(fds) != 0)
err(1, "pipe");
/* explicit_bzero(&p, sizeof(p)); */
bzero(buf, sizeof(buf));
bzero(&p, sizeof(p));
if (!blocking) {
if (fcntl(fds[0], F_SETFL, O_NONBLOCK) != 0)
err(1, "fcntl");
if (fcntl(fds[1], F_SETFL, O_NONBLOCK) != 0)
err(1, "fcntl");
}
if (io_uring_queue_init_params(10, &ring, &p) != 0)
err(1, "io_uring_queue_init_params");
/* WRITE */
sqe = io_uring_get_sqe(&ring);
if (sqe == NULL)
errx(1, "no sqe");
io_uring_prep_write(sqe, fds[1], "foobar", strlen("foobar"), 0); /* or -1 */
ret = io_uring_submit(&ring);
if (ret != 1)
errx(1, "io_uring_submit %d\n", ret);
ret = io_uring_wait_cqe(&ring, &cqe);
if (ret != 0)
errx(1, "io_uring_wait_cqe %d\n", ret);
io_uring_cqe_seen(&ring, cqe);
/* CLOSE */
sqe = io_uring_get_sqe(&ring);
io_uring_prep_close(sqe, fds[1]);
ret = io_uring_submit(&ring);
if (ret != 1)
errx(1, "io_uring_submit %d\n", ret);
ret = io_uring_wait_cqe(&ring, &cqe);
if (ret != 0)
errx(1, "io_uring_wait_cqe %d\n", ret);
io_uring_cqe_seen(&ring, cqe);
/* printf("close cqe->res = %d\n", cqe->res); */
/* READ */
sqe = io_uring_get_sqe(&ring);
if (sqe == NULL)
errx(1, "no sqe");
io_uring_prep_read(sqe, fds[0], buf, sizeof(buf), 0); /* or -1 */
ret = io_uring_submit(&ring);
if (ret != 1)
errx(1, "io_uring_submit %d\n", ret);
ret = io_uring_wait_cqe(&ring, &cqe);
if (ret != 0)
errx(1, "io_uring_wait_cqe %d\n", ret);
io_uring_cqe_seen(&ring, cqe);
printf("read(A) cqe->res = %d (buf=%s)\n", cqe->res, buf);
bzero(buf, sizeof(buf));
/* READ */
sqe = io_uring_get_sqe(&ring);
if (sqe == NULL)
errx(1, "no sqe");
io_uring_prep_read(sqe, fds[0], buf, sizeof(buf), 0); /* or -1 */
ret = io_uring_submit(&ring);
if (ret != 1)
errx(1, "io_uring_submit %d\n", ret);
ret = io_uring_wait_cqe(&ring, &cqe);
if (ret != 0)
errx(1, "io_uring_wait_cqe %d\n", ret);
io_uring_cqe_seen(&ring, cqe);
printf("read(B) cqe->res = %d\n", cqe->res);
io_uring_queue_exit(&ring);
return (0);
}
int
main(void)
{
/* plain_read(); */
pipe_bug(1); /* NEVER SEES EOF */
/* pipe_bug(0); /* WORKS */
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment