File indexing completed on 2025-05-11 08:24:36
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
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
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
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
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
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
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 }