Blame test/lfs-openat-write.c

Packit d3489f
#define _LARGEFILE_SOURCE
Packit d3489f
#define _FILE_OFFSET_BITS 64
Packit d3489f
Packit d3489f
#include <liburing.h>
Packit d3489f
#include <string.h>
Packit d3489f
#include <stdio.h>
Packit d3489f
#include <stdlib.h>
Packit d3489f
#include <sys/types.h>
Packit d3489f
#include <sys/stat.h>
Packit d3489f
#include <fcntl.h>
Packit d3489f
#include <errno.h>
Packit d3489f
#include <sys/resource.h>
Packit d3489f
#include <unistd.h>
Packit d3489f
Packit d3489f
static const int RSIZE = 2;
Packit d3489f
static const int OPEN_FLAGS = O_RDWR | O_CREAT;
Packit d3489f
static const mode_t OPEN_MODE = S_IRUSR | S_IWUSR;
Packit d3489f
Packit d3489f
#define DIE(...) do {\
Packit d3489f
		fprintf(stderr, __VA_ARGS__);\
Packit d3489f
		abort();\
Packit d3489f
	} while(0);
Packit d3489f
Packit d3489f
static int do_write(struct io_uring *ring, int fd, off_t offset)
Packit d3489f
{
Packit d3489f
	char buf[] = "some test write buf";
Packit d3489f
	struct io_uring_sqe *sqe;
Packit d3489f
	struct io_uring_cqe *cqe;
Packit d3489f
	int res, ret;
Packit d3489f
Packit d3489f
	sqe = io_uring_get_sqe(ring);
Packit d3489f
	if (!sqe) {
Packit d3489f
		fprintf(stderr, "failed to get sqe\n");
Packit d3489f
		return 1;
Packit d3489f
	}
Packit d3489f
	io_uring_prep_write(sqe, fd, buf, sizeof(buf), offset);
Packit d3489f
Packit d3489f
	ret = io_uring_submit(ring);
Packit d3489f
	if (ret < 0) {
Packit d3489f
		fprintf(stderr, "failed to submit write: %s\n", strerror(-ret));
Packit d3489f
		return 1;
Packit d3489f
	}
Packit d3489f
Packit d3489f
	ret = io_uring_wait_cqe(ring, &cqe);
Packit d3489f
	if (ret < 0) {
Packit d3489f
		fprintf(stderr, "wait_cqe failed: %s\n", strerror(-ret));
Packit d3489f
		return 1;
Packit d3489f
	}
Packit d3489f
Packit d3489f
	res = cqe->res;
Packit d3489f
	io_uring_cqe_seen(ring, cqe);
Packit d3489f
	if (res < 0) {
Packit d3489f
		fprintf(stderr, "write failed: %s\n", strerror(-res));
Packit d3489f
		return 1;
Packit d3489f
	}
Packit d3489f
Packit d3489f
	return 0;
Packit d3489f
}
Packit d3489f
Packit d3489f
static int test_open_write(struct io_uring *ring, int dfd, const char *fn)
Packit d3489f
{
Packit d3489f
	struct io_uring_sqe *sqe;
Packit d3489f
	struct io_uring_cqe *cqe;
Packit d3489f
	int ret, fd = -1;
Packit d3489f
Packit d3489f
	sqe = io_uring_get_sqe(ring);
Packit d3489f
	if (!sqe) {
Packit d3489f
		fprintf(stderr, "failed to get sqe\n");
Packit d3489f
		return 1;
Packit d3489f
	}
Packit d3489f
	io_uring_prep_openat(sqe, dfd, fn, OPEN_FLAGS, OPEN_MODE);
Packit d3489f
Packit d3489f
	ret = io_uring_submit(ring);
Packit d3489f
	if (ret < 0) {
Packit d3489f
		fprintf(stderr, "failed to submit openat: %s\n", strerror(-ret));
Packit d3489f
		return 1;
Packit d3489f
	}
Packit d3489f
Packit d3489f
	ret = io_uring_wait_cqe(ring, &cqe);
Packit d3489f
	if (ret < 0) {
Packit d3489f
		fprintf(stderr, "wait_cqe failed: %s\n", strerror(-ret));
Packit d3489f
		return 1;
Packit d3489f
	}
Packit d3489f
Packit d3489f
	fd = cqe->res;
Packit d3489f
	io_uring_cqe_seen(ring, cqe);
Packit d3489f
	if (fd < 0) {
Packit d3489f
		fprintf(stderr, "openat failed: %s\n", strerror(-fd));
Packit d3489f
		return 1;
Packit d3489f
	}
Packit d3489f
Packit d3489f
	return do_write(ring, fd, 1ULL << 32);
Packit d3489f
}
Packit d3489f
Packit d3489f
int main(int argc, char *argv[])
Packit d3489f
{
Packit d3489f
	struct io_uring ring;
Packit d3489f
	int dfd, ret;
Packit d3489f
Packit d3489f
	if (argc > 1)
Packit d3489f
		return 0;
Packit d3489f
Packit d3489f
	dfd = open("/tmp", O_RDONLY | O_DIRECTORY);
Packit d3489f
	if (dfd < 0)
Packit d3489f
		DIE("open /tmp: %s\n", strerror(errno));
Packit d3489f
Packit d3489f
	ret = io_uring_queue_init(RSIZE, &ring, 0);
Packit d3489f
	if (ret < 0)
Packit d3489f
		DIE("failed to init io_uring: %s\n", strerror(-ret));
Packit d3489f
Packit d3489f
	ret = test_open_write(&ring, dfd, "io_uring_openat_write_test1");
Packit d3489f
Packit d3489f
	io_uring_queue_exit(&ring);
Packit d3489f
	close(dfd);
Packit d3489f
	unlink("/tmp/io_uring_openat_write_test1");
Packit d3489f
	return ret;
Packit d3489f
}