Home | History | Annotate | Download | only in io_submit
      1 /*
      2  * Copyright (c) Crackerjack Project., 2007
      3  * Copyright (c) 2011-2017 Cyril Hrubis <chrubis (at) suse.cz>
      4  *
      5  * This program is free software;  you can redistribute it and/or modify
      6  * it under the terms of the GNU General Public License as published by
      7  * the Free Software Foundation; either version 2 of the License, or
      8  * (at your option) any later version.
      9  *
     10  * This program is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     13  * the GNU General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU General Public License
     16  * along with this program;  if not, write to the Free Software
     17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18  */
     19 
     20 /* Porting from Crackerjack to LTP is done
     21    by Masatake YAMATO <yamato (at) redhat.com> */
     22 
     23 #include <errno.h>
     24 #include <string.h>
     25 #include <fcntl.h>
     26 
     27 #include "config.h"
     28 #include "tst_test.h"
     29 
     30 #ifdef HAVE_LIBAIO
     31 #include <libaio.h>
     32 
     33 static io_context_t ctx;
     34 static io_context_t invalid_ctx;
     35 
     36 static struct iocb iocb;
     37 static struct iocb *iocbs[] = {&iocb};
     38 
     39 static struct iocb inv_fd_iocb;
     40 static struct iocb *inv_fd_iocbs[] = {&inv_fd_iocb};
     41 
     42 static int rdonly_fd;
     43 static struct iocb rdonly_fd_iocb;
     44 static struct iocb *rdonly_fd_iocbs[] = {&rdonly_fd_iocb};
     45 
     46 static int wronly_fd;
     47 static struct iocb wronly_fd_iocb;
     48 static struct iocb *wronly_fd_iocbs[] = {&wronly_fd_iocb};
     49 
     50 static struct iocb zero_buf_iocb;
     51 static struct iocb *zero_buf_iocbs[] = {&zero_buf_iocb};
     52 
     53 static struct iocb *zero_iocbs[1];
     54 
     55 static char buf[100];
     56 
     57 static struct tcase {
     58 	io_context_t *ctx;
     59 	long nr;
     60 	struct iocb **iocbs;
     61 	int exp_errno;
     62 	const char *desc;
     63 } tcases[] = {
     64 	/* Invalid ctx */
     65 	{&invalid_ctx, 1, iocbs, -EINVAL, "invalid ctx"},
     66 	/* Invalid nr */
     67 	{&ctx, -1, iocbs, -EINVAL, "invalid nr"},
     68 	/* Invalid pointer */
     69 	{&ctx, 1, (void*)-1, -EFAULT, "invalid iocbpp pointer"},
     70 	{&ctx, 1, zero_iocbs, -EFAULT, "NULL iocb pointers"},
     71 	/* Invalid fd */
     72 	{&ctx, 1, inv_fd_iocbs, -EBADF, "invalid fd"},
     73 	{&ctx, 1, rdonly_fd_iocbs, -EBADF, "readonly fd for write"},
     74 	{&ctx, 1, wronly_fd_iocbs, -EBADF, "writeonly fd for read"},
     75 	/* No-op but should work fine */
     76 	{&ctx, 1, zero_buf_iocbs, 1, "zero buf size"},
     77 	{&ctx, 0, NULL, 0, "zero nr"},
     78 };
     79 
     80 static void setup(void)
     81 {
     82 	int rval;
     83 
     84 	rval = io_setup(1, &ctx);
     85 	if (rval)
     86 		tst_brk(TBROK | TERRNO, "io_setup() returned %d", rval);
     87 
     88 	io_prep_pread(&inv_fd_iocb, -1, buf, sizeof(buf), 0);
     89 
     90 	rdonly_fd = SAFE_OPEN("rdonly_file", O_RDONLY | O_CREAT, 0777);
     91 	io_prep_pwrite(&rdonly_fd_iocb, rdonly_fd, buf, sizeof(buf), 0);
     92 
     93 	io_prep_pread(&zero_buf_iocb, rdonly_fd, buf, 0, 0);
     94 
     95 	wronly_fd = SAFE_OPEN("wronly_file", O_WRONLY | O_CREAT, 0777);
     96 	io_prep_pread(&wronly_fd_iocb, wronly_fd, buf, sizeof(buf), 0);
     97 }
     98 
     99 static void cleanup(void)
    100 {
    101 	if (rdonly_fd > 0)
    102 		SAFE_CLOSE(rdonly_fd);
    103 
    104 	if (wronly_fd > 0)
    105 		SAFE_CLOSE(wronly_fd);
    106 }
    107 
    108 static const char *errno_name(int err)
    109 {
    110 	if (err <= 0)
    111 		return tst_strerrno(-err);
    112 
    113 	return "SUCCESS";
    114 }
    115 
    116 static void verify_io_submit(unsigned int n)
    117 {
    118 	struct tcase *t = &tcases[n];
    119 	int ret;
    120 
    121 	ret = io_submit(*t->ctx, t->nr, t->iocbs);
    122 
    123 	if (ret == t->exp_errno) {
    124 		tst_res(TPASS, "io_submit() with %s failed with %s",
    125 			t->desc, errno_name(t->exp_errno));
    126 		return;
    127 	}
    128 
    129 	tst_res(TFAIL, "io_submit() returned %i(%s), expected %s(%i)",
    130 		ret, ret < 0 ? tst_strerrno(-ret) : "SUCCESS",
    131 		errno_name(t->exp_errno), t->exp_errno);
    132 }
    133 
    134 static struct tst_test test = {
    135 	.setup = setup,
    136 	.cleanup = cleanup,
    137 	.test = verify_io_submit,
    138 	.tcnt = ARRAY_SIZE(tcases),
    139 	.needs_tmpdir = 1,
    140 };
    141 
    142 #else
    143 	TST_TEST_TCONF("test requires libaio and it's development packages");
    144 #endif
    145