Back to home page

LXR

 
 

    


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

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 <stdio.h>
0034 #include <sys/uio.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 <string.h>
0041 #include <ctype.h>
0042 #include <rtems/imfs.h>
0043 #include <reent.h>
0044 
0045 #include <rtems.h>
0046 #include <rtems/libio.h>
0047 
0048 #include <tmacros.h>
0049 #include "test_support.h"
0050 
0051 const char rtems_test_name[] = "PSXFILE 2";
0052 
0053 /* forward declarations to avoid warnings */
0054 rtems_task Init(rtems_task_argument argument);
0055 void do_with_fd(int fd, const char *description);
0056 
0057 void do_with_fd(
0058   int         fd,
0059   const char *description
0060 )
0061 {
0062   struct stat       stat_buff;
0063   struct iovec      vec[2];
0064   char              buf[2][1];
0065   off_t             res;
0066   int               status;
0067 
0068   vec[0].iov_base = buf[0];
0069   vec[0].iov_len = sizeof(buf[0]);
0070   vec[1].iov_base = buf[1];
0071   vec[1].iov_len = sizeof(buf[1]);
0072 
0073   printf("ftruncate %s\n", description);
0074   status = ftruncate(fd, 40);
0075   rtems_test_assert( status == -1 );
0076   printf( "%d: %s\n", errno, strerror( errno ) );
0077   rtems_test_assert( errno == EBADF );
0078 
0079   printf("_fcntl_r %s\n", description);
0080   status = _fcntl_r( NULL, fd, F_SETFD, 1 );
0081   rtems_test_assert( status == -1 );
0082   printf( "%d: %s\n", errno, strerror( errno ) );
0083   rtems_test_assert( errno == EBADF );
0084 
0085   printf("fdatasync %s\n", description);
0086   status = fdatasync( fd );
0087   rtems_test_assert( status == -1 );
0088   printf( "%d: %s\n", errno, strerror( errno ) );
0089   rtems_test_assert( errno == EBADF );
0090 
0091   printf("fstat %s\n", description);
0092   status = fstat( fd, &stat_buff );
0093   rtems_test_assert( status == -1 );
0094   printf( "%d: %s\n", errno, strerror( errno ) );
0095   rtems_test_assert( errno == EBADF );
0096 
0097   printf("fsync %s\n", description);
0098   status = fsync( fd );
0099   rtems_test_assert( status == -1 );
0100   printf( "%d: %s\n", errno, strerror( errno ) );
0101   rtems_test_assert( errno == EBADF );
0102 
0103   printf("ioctl %s\n", description);
0104   status = ioctl( fd, 0 );
0105   rtems_test_assert( status == -1 );
0106   printf( "%d: %s\n", errno, strerror( errno ) );
0107   rtems_test_assert( errno == EBADF );
0108 
0109   printf("_lseek_r %s\n", description);
0110   res = _lseek_r (NULL, fd, 0, SEEK_SET);
0111   rtems_test_assert( res == -1 );
0112   printf( "%d: %s\n", errno, strerror( errno ) );
0113   rtems_test_assert( errno == EBADF );
0114 
0115   printf("readv %s\n", description);
0116   status = readv(fd, vec, 2);
0117   rtems_test_assert( status == -1 );
0118   printf( "%d: %s\n", errno, strerror( errno ) );
0119   rtems_test_assert( errno == EBADF );
0120 
0121   printf("writev %s\n", description);
0122   status = writev(fd, vec, 2);
0123   rtems_test_assert( status == -1 );
0124   printf( "%d: %s\n", errno, strerror( errno ) );
0125   rtems_test_assert( errno == EBADF );
0126 
0127   printf("write %s\n", description);
0128   status = write(fd, "1234", 4);
0129   rtems_test_assert( status == -1 );
0130   printf( "%d: %s\n", errno, strerror( errno ) );
0131   rtems_test_assert( errno == EBADF );
0132 }
0133 
0134 rtems_task Init(
0135   rtems_task_argument argument
0136 )
0137 {
0138   int   status;
0139   int   fd;
0140 
0141   TEST_BEGIN();
0142 
0143   /*
0144    *  Simple open case where the file is created.
0145    */
0146   puts( "mkdir /tmp" );
0147   status = mkdir( "/tmp", S_IRWXU );
0148   rtems_test_assert( !status );
0149 
0150   puts( "open /tmp/j" );
0151   fd = open( "/tmp/j", O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO );
0152   rtems_test_assert( fd != -1 );
0153   printf( "open returned file descriptor %d\n", fd );
0154 
0155   puts( "close /tmp/j" );
0156   status = close( fd );
0157   rtems_test_assert( !status );
0158 
0159   do_with_fd( fd, "an unopened file" );
0160   puts("");
0161   do_with_fd( 1000, "a too large file descriptor" );
0162 
0163   TEST_END();
0164 
0165   rtems_test_exit(0);
0166 }
0167 
0168 /* configuration information */
0169 
0170 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0171 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0172 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 4
0173 #define CONFIGURE_MAXIMUM_TASKS 1
0174 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0175 
0176 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0177 
0178 #define CONFIGURE_INIT
0179 
0180 #include <rtems/confdefs.h>
0181 /* end of file */