Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:36

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright 2024, Alessandro Nardin <ale.daluch@gmail.com>
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #include <stdio.h>
0029 #ifdef HAVE_CONFIG_H
0030 #include "config.h"
0031 #endif
0032 
0033 #define CONFIGURE_INIT
0034 #include "system.h"
0035 #include <rtems.h>
0036 #include "tmacros.h"
0037 #include <rtems/posix/aio_misc.h>
0038 #include <aio.h>
0039 #include <stdlib.h>
0040 #include <unistd.h>
0041 #include <sched.h>
0042 #include <fcntl.h>
0043 #include <signal.h>
0044 
0045 const char rtems_test_name[] = "PSXAIO 5";
0046 
0047 #define BUFSIZE 512
0048 #define WRONG_FD 404
0049 #define BAD_MODE 38576
0050 
0051 /* forward declarations to avoid warnings */
0052 static struct aiocb *create_aiocb( int fd );
0053 static void free_aiocb( struct aiocb *aiocbp );
0054 static void test_einval( struct aiocb** aiocbp );
0055 static void test_eio( struct aiocb** aiocbp );
0056 static void test_no_wait( struct aiocb** aiocbp );
0057 static void test_wait( struct aiocb** aiocbp );
0058 
0059 static struct aiocb *create_aiocb( int fd )
0060 {
0061   struct aiocb *aiocbp;
0062 
0063   aiocbp = malloc( sizeof( struct aiocb ) );
0064   memset( aiocbp, 0, sizeof( struct aiocb ) );
0065   aiocbp->aio_buf = malloc( BUFSIZE * sizeof( char ) );
0066   aiocbp->aio_nbytes = BUFSIZE;
0067   aiocbp->aio_offset = 0;
0068   aiocbp->aio_reqprio = 0;
0069   aiocbp->aio_fildes = fd;
0070   aiocbp->aio_sigevent.sigev_notify = SIGEV_NONE;
0071   aiocbp->aio_lio_opcode = LIO_READ;
0072   return aiocbp;
0073 }
0074 
0075 static void free_aiocb( struct aiocb *aiocbp )
0076 {
0077   free( (void*) aiocbp->aio_buf );
0078   free( aiocbp );
0079 }
0080 
0081 static void test_einval( struct aiocb** aiocbp )
0082 {
0083   int status;
0084 
0085   status = lio_listio( LIO_WAIT, NULL, 1, NULL);
0086   rtems_test_assert( status == -1 );
0087   rtems_test_assert( errno == EINVAL );
0088   errno = 0;
0089 
0090   status = lio_listio( BAD_MODE, aiocbp, 1, NULL);
0091   rtems_test_assert( status == -1 );
0092   rtems_test_assert( errno == EINVAL );
0093   errno = 0;
0094 
0095   status = lio_listio( LIO_WAIT, aiocbp, -9, NULL);
0096   rtems_test_assert( status == -1 );
0097   rtems_test_assert( errno == EINVAL );
0098   errno = 0;
0099 
0100   status = lio_listio( LIO_WAIT, aiocbp, 547, NULL);
0101   rtems_test_assert( status == -1 );
0102   rtems_test_assert( errno == EINVAL );
0103   errno = 0;
0104 }
0105 
0106 static void test_eio( struct aiocb** aiocbp )
0107 {
0108   int status;
0109 
0110   status = lio_listio( LIO_NOWAIT, &aiocbp[4], 1, NULL);
0111   rtems_test_assert( status == -1 );
0112   rtems_test_assert( errno == EIO );
0113   errno = 0;
0114 
0115   status = lio_listio( LIO_WAIT, &aiocbp[4], 1, NULL);
0116   rtems_test_assert( status == -1 );
0117   rtems_test_assert( errno == EIO );
0118   errno = 0;
0119 }
0120 
0121 static void test_no_wait( struct aiocb** aiocbp )
0122 {
0123   int status, received_signal, sig;
0124   struct sigevent sigev;
0125   sigset_t sig_set;
0126   
0127   sig = SIGUSR1;
0128   sigev.sigev_notify = SIGEV_SIGNAL;
0129   sigev.sigev_signo = sig;
0130   sigev.sigev_value.sival_ptr  = NULL;
0131 
0132   status = sigemptyset( &sig_set );
0133   rtems_test_assert( status == 0 );
0134 
0135   status = sigaddset( &sig_set, sig );
0136   rtems_test_assert( status == 0 );
0137 
0138   status = sigprocmask( SIG_BLOCK, &sig_set, NULL );
0139   rtems_test_assert( status == 0 );
0140 
0141   status = lio_listio( LIO_NOWAIT, aiocbp, 4, &sigev);
0142   rtems_test_assert( status == 0 );
0143 
0144   status = sigwait( &sig_set, &received_signal );
0145   rtems_test_assert( status == 0 );
0146   rtems_test_assert( received_signal == sig );
0147 
0148   status = sigprocmask( SIG_UNBLOCK, &sig_set, NULL );
0149   rtems_test_assert( status == 0 );
0150 }
0151 
0152 static void test_wait( struct aiocb** aiocbp )
0153 {
0154   int status;
0155 
0156   status = lio_listio( LIO_WAIT, aiocbp, 4, NULL);
0157   rtems_test_assert( status == 0 );
0158 }
0159 
0160 void *POSIX_Init( void *argument )
0161 {
0162   struct aiocb *aiocbp[] = { NULL, NULL, NULL, NULL, NULL };
0163   int fd, result;
0164 
0165   rtems_aio_init();
0166 
0167   result = mkdir( "/tmp", S_IRWXU );
0168   rtems_test_assert( result == 0 );
0169   
0170   fd = open(
0171     "/tmp/aio_fildes",
0172     O_RDWR|O_CREAT,
0173     S_IRWXU|S_IRWXG|S_IRWXO
0174   );
0175   rtems_test_assert( fd != -1 );
0176 
0177   TEST_BEGIN();
0178 
0179   for (int i = 0; i<4; i++ ) {
0180     aiocbp[i] = create_aiocb( fd );
0181   }
0182   aiocbp[4] = create_aiocb( WRONG_FD );
0183 
0184   test_einval( aiocbp );
0185 
0186   test_eio( aiocbp );
0187 
0188   test_no_wait( aiocbp );
0189 
0190   test_wait( aiocbp );
0191 
0192   TEST_END();
0193 
0194   for (int i = 0; i<5; i++ ) {
0195     free_aiocb(aiocbp[i]);
0196   }
0197 
0198   close( fd );
0199   rtems_test_exit( 0 );
0200 
0201   return NULL;
0202 }