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 (c) 1989-2012.
0005  *  On-Line Applications Research Corporation (OAR).
0006  *
0007  *  Modifications to support reference counting in the file system are
0008  *  Copyright (c) 2012 embedded brains GmbH & Co. KG
0009  *
0010  * Redistribution and use in source and binary forms, with or without
0011  * modification, are permitted provided that the following conditions
0012  * are met:
0013  * 1. Redistributions of source code must retain the above copyright
0014  *    notice, this list of conditions and the following disclaimer.
0015  * 2. Redistributions in binary form must reproduce the above copyright
0016  *    notice, this list of conditions and the following disclaimer in the
0017  *    documentation and/or other materials provided with the distribution.
0018  *
0019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0029  * POSSIBILITY OF SUCH DAMAGE.
0030  */
0031 
0032 #ifdef HAVE_CONFIG_H
0033 #include "config.h"
0034 #endif
0035 
0036 #include <stdio.h>
0037 #include <sys/types.h>
0038 #include <fcntl.h>
0039 #include <string.h>
0040 #include <unistd.h>
0041 #include <errno.h>
0042 #include <rtems/libio.h>
0043 #include <rtems/userenv.h>
0044 #include <rtems/malloc.h>
0045 #include <pmacros.h>
0046 #include <rtems/libcsupport.h>
0047 
0048 const char rtems_test_name[] = "PSXCHROOT 1";
0049 
0050 /* forward declarations to avoid warnings */
0051 int test_main(void);
0052 
0053 static void touch( char *file )
0054 {
0055   int fd;
0056 
0057   rtems_test_assert( file );
0058 
0059   fd = open( file, O_RDWR|O_CREAT, 0777 );
0060   rtems_test_assert( fd != -1 );
0061   close( fd );
0062 }
0063 
0064 static int fileexists( char *file )
0065 {
0066   int         status;
0067   struct stat statbuf;
0068 
0069   rtems_test_assert( file );
0070 
0071   status = stat( file, &statbuf );
0072 
0073   if ( status == -1 ) {
0074     /* printf( ": %s\n", strerror( errno ) ); */
0075     return 0;
0076   }
0077   return 1;
0078 }
0079 
0080 #if defined(__rtems__)
0081 int test_main(void)
0082 #else
0083 int main(
0084   int argc,
0085   char **argv
0086 )
0087 #endif
0088 {
0089   static const uintptr_t global_location_size [] = {
0090     sizeof(rtems_filesystem_global_location_t)
0091   };
0092 
0093   int status;
0094   void *opaque;
0095   struct stat st;
0096 
0097 /*
0098  *  This test is the C equivalent of this sequence.
0099 #mkdir /one
0100 #mkdir /one/one
0101 #touch /one/one.test
0102 #touch /one/two/two.test
0103 # an error case to ENOTSUP from chroot
0104 # chroot
0105 #chroot /one
0106 #if !fileexists(/one/one.test) echo "SUCCESSFUL"
0107 #if fileexists(/two/two.test) echo "SUCCESSFUL"
0108 #rtems_set_private_env() ! reset at the global environment
0109 #if fileexists(/one/one.test) echo "SUCESSFUL"
0110 #if !fileexists(/two/two.test) echo "SUCCESSFUL"
0111 */
0112 
0113   TEST_BEGIN();
0114 
0115   status = mkdir( "/one", 0777);
0116   rtems_test_assert( status == 0 );
0117 
0118   status = mkdir( "/one/one", 0777);
0119   rtems_test_assert( status == 0 );
0120 
0121   status = mkdir( "/one/two", 0777);
0122   rtems_test_assert( status == 0 );
0123 
0124   touch( "/one/one.test" );
0125   touch( "/one/two/two.test" );
0126 
0127   puts( "chroot with bad path - expect ENOENT" );
0128   status = chroot( "/three" );
0129   rtems_test_assert( status == -1 );
0130   rtems_test_assert( errno == ENOENT );
0131 
0132   puts( "chroot with file - expect ENOTDIR" );
0133   status = chroot( "/one/one.test" );
0134   rtems_test_assert( status == -1 );
0135   rtems_test_assert( errno == ENOTDIR );
0136 
0137   /* Perform deferred global location releases */
0138   status = stat( ".", &st );
0139   rtems_test_assert( status == 0 );
0140 
0141   puts( "allocate most of memory - attempt to fail chroot - expect ENOMEM" );
0142   opaque = rtems_heap_greedy_allocate( global_location_size, 1 );
0143 
0144   status = chroot( "/one" );
0145   rtems_test_assert( status == -1 );
0146   rtems_test_assert( errno == ENOMEM );
0147 
0148   puts( "freeing the allocated memory" );
0149   rtems_heap_greedy_free( opaque );
0150 
0151   status = chroot( "/one" );
0152   rtems_test_assert( status == 0 );
0153 
0154   status = fileexists( "/one/one.test" );
0155   printf( "%s on /one/one.test\n", (!status) ? "SUCCESS" : "FAILURE" );
0156 
0157   status = fileexists( "/two/two.test" );
0158   printf( "%s on /two/two.test\n", (status) ? "SUCCESS" : "FAILURE" );
0159 
0160   puts( "Go back to global environment" );
0161   rtems_libio_use_global_env();
0162 
0163   status = fileexists( "/one/one.test" );
0164   printf( "%s on /one/one.test\n", ( status) ? "SUCCESS" : "FAILURE" );
0165 
0166   status = fileexists( "/two/two.test" );
0167   printf( "%s on /two/two.test\n", (!status) ? "SUCCESS" : "FAILURE" );
0168 
0169   TEST_END();
0170   rtems_test_exit(0);
0171 }