Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 1989-2012.
0005  *  On-Line Applications Research Corporation (OAR).
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 #include <pmacros.h>
0034 #include <unistd.h>
0035 #include <errno.h>
0036 #include <tmacros.h>
0037 
0038 #include <fcntl.h>           /* For O_* constants */
0039 #include <sys/stat.h>        /* For mode constants */
0040 #include <mqueue.h>
0041 
0042 #include "test_support.h"
0043 
0044 const char rtems_test_name[] = "PSXMSGQ 4";
0045 
0046 /* forward declarations to avoid warnings */
0047 void *POSIX_Init(void *argument);
0048 
0049 void *POSIX_Init(
0050   void *argument
0051 )
0052 {
0053   struct mq_attr          attr;
0054   mqd_t                   Queue, second_Queue;
0055   int                     sc;
0056   Heap_Information_block  start;
0057   size_t                  to_alloc;
0058   void                   *alloced;
0059   bool                    sb;
0060   const char             *name;
0061 
0062   TEST_BEGIN();
0063 
0064   attr.mq_maxmsg = 1;
0065   attr.mq_msgsize = sizeof(int);
0066 
0067   puts( "Init - Open message queue instance 1" );
0068   Queue = mq_open( "Queue", O_CREAT | O_RDWR, 0x777, &attr );
0069   if ( Queue == (-1) )
0070     perror( "mq_open failed" );
0071   rtems_test_assert( Queue != (-1) );
0072 
0073   puts( "Init - Open message queue instance 2 - FAIL - ENFILE " );
0074   second_Queue = mq_open( "Queue2", O_CREAT | O_RDWR, 0x777, &attr );
0075   if ( second_Queue != (-1) )
0076     puts( "mq_open did not failed" );
0077   rtems_test_assert( second_Queue == (-1) );
0078   rtems_test_assert( errno == ENFILE );
0079 
0080   puts( "Init - Unlink message queue instance 1" );
0081   sc = mq_unlink( "Queue" );
0082   if ( sc != 0 )
0083     perror( "mq_unlink failed" );
0084   rtems_test_assert( sc == 0 );
0085 
0086   puts( "Init - Close message queue instance 1" );
0087   sc = mq_close( Queue );
0088   if ( sc != 0 )
0089     perror( "mq_close failed" );
0090   rtems_test_assert( sc == 0 );
0091 
0092   sb = rtems_workspace_get_information( &start );
0093   rtems_test_assert( start.Free.number == 1 );
0094   to_alloc = start.Free.largest;
0095 
0096   /* find the largest we can actually allocate */
0097   while ( 1 ) {
0098     sb = rtems_workspace_allocate( to_alloc, &alloced );
0099     if ( sb )
0100       break;
0101     to_alloc -= 4;
0102   }
0103 
0104   rtems_workspace_free( alloced );
0105 
0106   /*
0107    * Now do the test
0108    */
0109   puts( "Init - Memory allocation error test" );
0110 
0111   name = Get_Longest_Name();
0112   do {
0113     sb = rtems_workspace_allocate( to_alloc, &alloced );
0114     rtems_test_assert( sb );
0115 
0116     second_Queue = mq_open(name,O_CREAT | O_RDWR, 0x777, &attr );
0117 
0118     /* free the memory we snagged, then check the status */
0119     rtems_workspace_free( alloced );
0120 
0121     to_alloc -= 4;
0122   } while ( second_Queue == -1 );
0123 
0124   puts( "Init - Message Queue created" );
0125 
0126   puts( "Init - Unlink message queue" );
0127     sc = mq_unlink( name );
0128     if ( sc != 0 )
0129       perror( "mq_unlink failed" );
0130     rtems_test_assert( sc == 0 );
0131 
0132   puts( "Init - Close message queue" );
0133     sc = mq_close( second_Queue );
0134     if ( sc != 0 )
0135       perror( "mq_close failed" );
0136     rtems_test_assert( sc == 0 );
0137 
0138   TEST_END();
0139   rtems_test_exit( 0 );
0140 
0141   return NULL; /* just so the compiler thinks we returned something */
0142 }
0143 
0144 /* configuration information */
0145 
0146 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0147 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0148 
0149 #define CONFIGURE_POSIX_INIT_THREAD_TABLE
0150 
0151 /* account for message buffers and string names */
0152 #define CONFIGURE_MESSAGE_BUFFER_MEMORY \
0153     (2 * CONFIGURE_MESSAGE_BUFFERS_FOR_QUEUE(1, sizeof(int)))
0154 
0155 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0156 
0157 #define CONFIGURE_MAXIMUM_POSIX_THREADS                   1
0158 #define CONFIGURE_MAXIMUM_POSIX_MESSAGE_QUEUES            1
0159 
0160 #define CONFIGURE_POSIX_INIT_THREAD_TABLE
0161 
0162 #define CONFIGURE_INIT
0163 #include <rtems/confdefs.h>