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 2010, Alin Rus <alin.codejunkie@gmail.com>
0005  * Copyright 2024, Alessandro Nardin <ale.daluch@gmail.com>
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, this list of conditions and the following disclaimer.
0012  * 2. Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0026  * POSSIBILITY OF SUCH DAMAGE.
0027  */
0028 
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 <rtems/chain.h>
0044 
0045 const char rtems_test_name[] = "PSXAIO 2";
0046 
0047 /* forward declarations to avoid warnings */
0048 static struct aiocb *create_aiocb( int fd );
0049 static void free_aiocb( struct aiocb *aiocbp );
0050 
0051 #define BUFSIZE 32
0052 #define FD_COUNT 10
0053 #define WRONG_FD 666
0054 
0055 static struct aiocb *create_aiocb( int fd )
0056 {
0057   struct aiocb *aiocbp;
0058 
0059   aiocbp = malloc( sizeof( struct aiocb ) );
0060   memset( aiocbp, 0, sizeof( struct aiocb ) );
0061   aiocbp->aio_buf = malloc( BUFSIZE * sizeof( char ) );
0062   aiocbp->aio_nbytes = BUFSIZE;
0063   aiocbp->aio_offset = 0;
0064   aiocbp->aio_reqprio = 0;
0065   aiocbp->aio_fildes = fd;
0066   aiocbp->aio_sigevent.sigev_notify = SIGEV_NONE;
0067   return aiocbp;
0068 }
0069 
0070 static void free_aiocb( struct aiocb *aiocbp )
0071 {
0072   free( (void*) aiocbp->aio_buf );
0073   free( aiocbp );
0074 }
0075 
0076 void *POSIX_Init( void *argument )
0077 {
0078   int fd[FD_COUNT];
0079   struct aiocb *aiocbp[FD_COUNT+1];
0080   int status, i, policy = SCHED_FIFO;
0081   char filename[BUFSIZE];
0082   struct sched_param param;
0083 
0084   status = rtems_aio_init();
0085   rtems_test_assert( status == 0 );
0086 
0087   param.sched_priority = 30;
0088   status = pthread_setschedparam( 
0089     pthread_self(),
0090     policy, &param 
0091   );
0092   rtems_test_assert( status == 0 );
0093  
0094   status = mkdir( "/tmp", S_IRWXU );
0095   rtems_test_assert( !status );
0096   
0097   TEST_BEGIN();
0098 
0099   for( i=0; i<FD_COUNT; i++ )
0100     {
0101       sprintf( filename, "/tmp/aio_fildes%d", i );
0102       fd[i] = open( 
0103         filename,
0104         O_RDWR|O_CREAT,
0105         S_IRWXU|S_IRWXG|S_IRWXO 
0106       );
0107       rtems_test_assert( fd[i] != -1 );
0108     }
0109   
0110   aiocbp[0] = create_aiocb( fd[0] );
0111   status = aio_write( aiocbp[0] );
0112   rtems_test_assert( status != -1 );
0113 
0114   aiocbp[1] = create_aiocb( fd[1] );
0115   status = aio_write( aiocbp[1] );
0116   rtems_test_assert( status != -1 );
0117   
0118   aiocbp[2] = create_aiocb( fd[1] );
0119   status = aio_read( aiocbp[2] );
0120   rtems_test_assert( status != -1 );
0121   
0122   aiocbp[3] = create_aiocb( fd[2] );
0123   status = aio_write( aiocbp[3] );
0124   rtems_test_assert( status != -1 );
0125   
0126   aiocbp[4] = create_aiocb( fd[3] );
0127   status = aio_write( aiocbp[4] );
0128   rtems_test_assert( status != -1 );
0129   
0130   aiocbp[5] = create_aiocb( fd[4] );
0131   status = aio_write( aiocbp[5] );
0132   rtems_test_assert( status != -1 );
0133   
0134   aiocbp[6] = create_aiocb( fd[5] );
0135   status = aio_write( aiocbp[6] );
0136   rtems_test_assert( status != -1 );
0137   
0138   aiocbp[7] = create_aiocb( fd[6] );
0139   status = aio_write( aiocbp[7] );
0140   rtems_test_assert( status != -1 );
0141 
0142   aiocbp[8] = create_aiocb( fd[6] ); 
0143   status = aio_read( aiocbp[8] );
0144   rtems_test_assert( status != -1 );
0145 
0146   aiocbp[9] = create_aiocb( fd[0] ); 
0147   status = aio_fsync( O_SYNC, aiocbp[9] );
0148   rtems_test_assert( status != -1 );
0149 
0150   aiocbp[9] = create_aiocb( fd[0] ); 
0151   status = aio_fsync( O_DSYNC, aiocbp[9] );
0152   rtems_test_assert( status != -1 );
0153 
0154   status = aio_cancel( WRONG_FD, NULL );
0155   rtems_test_assert( status == -1 );
0156 
0157   status = aio_cancel( fd[7], NULL );
0158   rtems_test_assert( status == AIO_ALLDONE );
0159 
0160   status = aio_cancel( fd[1], NULL );
0161   rtems_test_assert( status == AIO_CANCELED );
0162 
0163   status = aio_cancel( fd[6], NULL );
0164   rtems_test_assert( status == AIO_CANCELED );
0165 
0166   status = aio_cancel( fd[4], aiocbp[4] );
0167   rtems_test_assert( status == -1 );
0168  
0169   aiocbp[10] = create_aiocb( fd[9] );
0170   status = aio_cancel( fd[9], aiocbp[10] );
0171   rtems_test_assert( status == -1 );
0172 
0173   status = aio_cancel( fd[5], aiocbp[6] );
0174   rtems_test_assert( status == AIO_CANCELED );
0175 
0176   /* tests for aio error and aio test */
0177 
0178   int result = aio_error( aiocbp[6] );
0179   rtems_test_assert( result == ECANCELED );
0180 
0181   result = aio_return( aiocbp[6] );
0182   rtems_test_assert( result == -1 );
0183 
0184   result = aio_return( aiocbp[6] );
0185   rtems_test_assert( result == -1 );
0186 
0187   rtems_test_assert( errno == EINVAL );
0188   result = aio_error( aiocbp[6] );
0189   rtems_test_assert( result == -1 );
0190   rtems_test_assert( errno == EINVAL );
0191 
0192   status = aio_cancel( fd[0], aiocbp[9] );
0193   rtems_test_assert( status == AIO_CANCELED );
0194 
0195   status = aio_cancel( fd[5], aiocbp[6] );
0196   rtems_test_assert( status == AIO_ALLDONE );
0197 
0198   TEST_END();
0199 
0200   for ( i = 0; i < FD_COUNT; i++ )
0201     {
0202       close( fd[i] );
0203       free_aiocb( aiocbp[i] );
0204     }
0205   free_aiocb( aiocbp[i] );
0206   rtems_test_exit( 0 );
0207 
0208   return NULL;
0209 
0210 }