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 <bsp.h>
0034 #include <pmacros.h>
0035 #include <sys/types.h>
0036 #include <sys/stat.h>
0037 #include <fcntl.h>
0038 #include <unistd.h>
0039 #include <errno.h>
0040 #include <rtems/libcsupport.h>
0041 #include <rtems/malloc.h>
0042 
0043 const char rtems_test_name[] = "PSXPIPE 1";
0044 
0045 /* forward declarations to avoid warnings */
0046 rtems_task Init(rtems_task_argument ignored);
0047 
0048 static int dup_fd_test(int *fd)
0049 {
0050   int r;
0051 
0052   if ((r = fcntl(*fd, F_DUPFD, 0)) < 0 ||
0053       fcntl(r, F_SETFD, FD_CLOEXEC) < 0 ||
0054       fcntl(r, F_SETFL, O_NONBLOCK))
0055     return -1;
0056   (void)close(*fd);
0057   (*fd = r);
0058   return 0;
0059 }
0060 
0061 rtems_task Init(
0062   rtems_task_argument ignored
0063 )
0064 {
0065   int fd[2] = {0,0};
0066   int dummy_fd[4] = {0,0};
0067   int status = 0;
0068   void *opaque = NULL;
0069   char c;
0070 
0071   TEST_BEGIN();
0072 
0073   puts( "Init - attempt to create pipe -- expect EFAULT" );
0074   status = pipe( NULL );
0075   rtems_test_assert( status == -1 );
0076   rtems_test_assert( errno == EFAULT );
0077 
0078   puts( "Init - create pipe -- OK" );
0079   status = pipe( fd );
0080   rtems_test_assert( status == 0 );
0081 
0082   puts( "Init - dup pipe -- OK" );
0083   status = dup_fd_test(&fd[0]);
0084   status |= dup_fd_test(&fd[1]);
0085   rtems_test_assert( status == 0 );
0086 
0087   /* close pipe writer, so reader can return immediately */
0088   puts( "Init - read pipe -- OK" );
0089   status = close( fd[1] );
0090   status |= read(fd[0], &c, 1);
0091   status |= close( fd[0] );
0092   rtems_test_assert( status == 0 );
0093 
0094   puts( "Init - create pipe -- OK" );
0095   status = pipe( fd );
0096   rtems_test_assert( status == 0 );
0097 
0098   status = close( fd[0] );
0099   status |= close( fd[1] );
0100   rtems_test_assert( status == 0 );
0101 
0102   opaque = rtems_heap_greedy_allocate( NULL, 0 );
0103 
0104   /* case where mkfifo fails */
0105   puts( "Init - attempt to create pipe -- expect ENOMEM" );
0106   status = pipe( fd );
0107   rtems_test_assert( status == -1 );
0108   rtems_test_assert( errno == ENOMEM );
0109 
0110   rtems_heap_greedy_free( opaque );
0111   
0112   dummy_fd[0] = open( "/file01", O_RDONLY | O_CREAT, S_IRWXU );
0113   rtems_test_assert( dummy_fd[0] != -1 );
0114   dummy_fd[1] = open( "/file02", O_RDONLY | O_CREAT, S_IRWXU );
0115   rtems_test_assert( dummy_fd[1] != -1 );
0116   dummy_fd[2] = open( "/file03", O_RDONLY | O_CREAT, S_IRWXU );
0117   rtems_test_assert( dummy_fd[2] != -1 );
0118   dummy_fd[3] = open( "/file04", O_RDONLY | O_CREAT, S_IRWXU );
0119   rtems_test_assert( dummy_fd[3] != -1 );
0120 
0121   /* case where fifo_open for read => open fails */
0122   puts( "Init - create pipe -- expect ENFILE" );
0123   status = pipe( fd );
0124   rtems_test_assert( status == -1 );
0125   rtems_test_assert( errno == ENFILE );
0126 
0127   status = close( dummy_fd[3] );
0128   status |= unlink( "/file04" );
0129   rtems_test_assert( status == 0 );
0130 
0131   /* case where fifo_open for write => open fails */
0132   puts( "Init - create pipe -- expect ENFILE" );
0133   status = pipe( fd );
0134   rtems_test_assert( status == -1 );
0135   rtems_test_assert( errno == ENFILE );
0136 
0137   status = close( dummy_fd[0] );
0138   status |= unlink( "/file01" );
0139   status |= close( dummy_fd[1] );
0140   status |= unlink( "/file02" );
0141   status |= close( dummy_fd[2] );
0142   status |= unlink( "/file03" );
0143   rtems_test_assert( status == 0 );
0144 
0145   TEST_END();
0146   rtems_test_exit( 0 );
0147 }
0148 
0149 /* configuration information */
0150 
0151 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0152 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0153 
0154 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 7
0155 
0156 #define CONFIGURE_MAXIMUM_TASKS 1
0157 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0158 
0159 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0160 
0161 #define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT
0162 
0163 #define CONFIGURE_IMFS_ENABLE_MKFIFO
0164 
0165 #define CONFIGURE_INIT
0166 #include <rtems/confdefs.h>
0167 /* end of file */