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 #ifdef HAVE_CONFIG_H
0029 #include "config.h"
0030 #endif
0031 
0032 #define CONFIGURE_INIT
0033 #include "system.h"
0034 #include <rtems.h>
0035 #include "tmacros.h"
0036 #include <rtems/posix/aio_misc.h>
0037 #include <aio.h>
0038 #include <stdlib.h>
0039 #include <unistd.h>
0040 #include <sched.h>
0041 #include <fcntl.h>
0042 #include <signal.h>
0043 
0044 const char rtems_test_name[] = "PSXAIO 4";
0045 
0046 #define BUFSIZE 512
0047 #define WRONG_FD 404
0048 
0049 /* forward declarations to avoid warnings */
0050 static struct aiocb *create_aiocb( int fd );
0051 static void free_aiocb( struct aiocb *aiocbp );
0052 static void notify( union sigval sig );
0053 
0054 static struct aiocb *create_aiocb( int fd )
0055 {
0056   struct aiocb *aiocbp;
0057 
0058   aiocbp = malloc( sizeof( struct aiocb ) );
0059   memset( aiocbp, 0, sizeof( struct aiocb ) );
0060   aiocbp->aio_buf = malloc( BUFSIZE * sizeof( char ) );
0061   aiocbp->aio_nbytes = BUFSIZE;
0062   aiocbp->aio_offset = 0;
0063   aiocbp->aio_reqprio = 0;
0064   aiocbp->aio_fildes = fd;
0065   aiocbp->aio_sigevent.sigev_notify = SIGEV_NONE;
0066   return aiocbp;
0067 }
0068 
0069 static void free_aiocb( struct aiocb *aiocbp )
0070 {
0071   free( (void*) aiocbp->aio_buf );
0072   free( aiocbp );
0073 }
0074 
0075 static void notify( union sigval sig )
0076 {
0077   kill( getpid(), sig.sival_int );
0078 }
0079 
0080 void *POSIX_Init( void *argument )
0081 {
0082   int fd, status, received_signal, sig;
0083   struct aiocb *aiocbp;
0084   sigset_t sig_set;
0085   sig = SIGUSR1;
0086 
0087   rtems_aio_init();
0088 
0089   status = mkdir( "/tmp", S_IRWXU );
0090   rtems_test_assert( !status );
0091   
0092   fd = open(
0093     "/tmp/aio_fildes",
0094     O_RDWR|O_CREAT,
0095     S_IRWXU|S_IRWXG|S_IRWXO
0096   );
0097   rtems_test_assert( fd != -1 );
0098 
0099   TEST_BEGIN();
0100 
0101   aiocbp = create_aiocb( fd );
0102   aiocbp->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
0103   aiocbp->aio_sigevent.sigev_signo = SIGUSR1;
0104 
0105   status =sigemptyset( &sig_set );
0106   rtems_test_assert( status == 0 );
0107 
0108   status =sigaddset( &sig_set, sig );
0109   rtems_test_assert( status == 0 );
0110 
0111   status = sigprocmask( SIG_BLOCK, &sig_set, NULL );
0112   rtems_test_assert( status == 0 );
0113 
0114   status = aio_write( aiocbp );
0115   rtems_test_assert( status == 0 );
0116 
0117   status = sigwait( &sig_set, &received_signal );
0118   rtems_test_assert( status == 0 );
0119 
0120   rtems_test_assert( received_signal == sig );
0121 
0122   aiocbp = create_aiocb( fd );
0123   aiocbp->aio_sigevent.sigev_notify = SIGEV_THREAD;
0124   aiocbp->aio_sigevent.sigev_notify_attributes = NULL;
0125   aiocbp->aio_sigevent.sigev_notify_function = &notify;
0126   aiocbp->aio_sigevent.sigev_value.sival_int = sig;
0127 
0128   status =sigemptyset( &sig_set );
0129   rtems_test_assert( status == 0 );
0130 
0131   status =sigaddset( &sig_set, sig );
0132   rtems_test_assert( status == 0 );
0133 
0134   status = sigprocmask( SIG_BLOCK, &sig_set, NULL );
0135   rtems_test_assert( status == 0 );
0136 
0137   status = aio_write( aiocbp );
0138   rtems_test_assert( status == 0 );
0139 
0140   status = sigwait( &sig_set, &received_signal );
0141   rtems_test_assert( status == 0 );
0142 
0143   rtems_test_assert( received_signal == sig );
0144 
0145   TEST_END();
0146 
0147   free_aiocb( aiocbp );
0148   close( fd );
0149   rtems_test_exit( 0 );
0150 
0151   return NULL;
0152 }