File indexing completed on 2025-05-11 08:24:39
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 #ifdef HAVE_CONFIG_H
0030 #include "config.h"
0031 #endif
0032
0033 #include <tmacros.h>
0034 #include "test_support.h"
0035
0036 #include <unistd.h>
0037 #include <sys/types.h>
0038 #include <sys/stat.h>
0039 #include <fcntl.h>
0040 #include <errno.h>
0041 #include <rtems/libio.h>
0042 #include <rtems/malloc.h>
0043 #include <rtems/libcsupport.h>
0044
0045 #define MEMFILE_BYTES_PER_BLOCK 16
0046
0047 const char rtems_test_name[] = "PSXIMFS 2";
0048
0049
0050 rtems_task Init(rtems_task_argument argument);
0051
0052 rtems_task Init(
0053 rtems_task_argument argument
0054 )
0055 {
0056 static const char mount_point [] = "dir01";
0057 static const char fs_type [] = RTEMS_FILESYSTEM_TYPE_IMFS;
0058 static const char slink_2_name [] = "node-slink-2";
0059 static const uintptr_t mount_table_entry_size [] = {
0060 sizeof( rtems_filesystem_mount_table_entry_t )
0061 + sizeof( fs_type )
0062 + sizeof( rtems_filesystem_global_location_t )
0063 };
0064 static const uintptr_t slink_2_name_size [] = {
0065 sizeof( slink_2_name )
0066 };
0067 static const uintptr_t some_blocks [] = {
0068 MEMFILE_BYTES_PER_BLOCK * 10
0069 };
0070 static const char some_data[MEMFILE_BYTES_PER_BLOCK * 11];
0071
0072 int status = 0;
0073 void *opaque;
0074 char linkname_n[32] = {0};
0075 char linkname_p[32] = {0};
0076 int i;
0077 int fd;
0078 struct stat stat_buf;
0079
0080 TEST_BEGIN();
0081
0082 puts( "Creating directory /dir00" );
0083 status = mkdir( "/dir00", S_IRWXU );
0084 rtems_test_assert( status == 0 );
0085
0086 puts( "Creating directory /dir00/dir01" );
0087 status = mkdir( "/dir00/dir01", S_IRWXU );
0088 rtems_test_assert( status == 0 );
0089
0090 puts( "Changing directory to /dir00" );
0091 status = chdir( "/dir00" );
0092 rtems_test_assert( status == 0 );
0093
0094 puts( "Creating link dir01-link0 for dir01" );
0095 status = link( "dir01", "dir01-link0" );
0096 rtems_test_assert( status == 0 );
0097
0098 for( i = 1 ; ; ++i ) {
0099 sprintf( linkname_p, "dir01-link%04d", i-1 );
0100 sprintf( linkname_n, "dir01-link%04d", i );
0101 printf( "\nCreating link %s for %s\n", linkname_n, linkname_p );
0102 status = link( linkname_p, linkname_n );
0103 if( status != 0 ) {
0104 puts("Link creation failed" );
0105 break;
0106 }
0107 }
0108
0109 puts( "Creating a regular node /node, RDONLY" );
0110 status = mknod( "/node", S_IFREG | S_IRUSR, 0LL );
0111 rtems_test_assert( status == 0 );
0112
0113 puts( "Creating link /node-link for /node" );
0114 status = link( "/node" , "/node-link" );
0115 rtems_test_assert( status == 0 );
0116
0117 puts( "Opening /node-link in WRONLY mode -- expect EACCES" );
0118 status = open( "/node-link", O_WRONLY );
0119 rtems_test_assert( status == -1 );
0120 rtems_test_assert( errno == EACCES );
0121
0122 puts( "Creating a symlink /node-slink for /node" );
0123 status = symlink( "/node" , "/node-slink" );
0124 rtems_test_assert( status == 0 );
0125
0126 puts( "Opening /node-slink in WRONLY mode -- expect EACCES" );
0127 status = open( "/node-slink", O_WRONLY );
0128 rtems_test_assert( status == -1 );
0129 rtems_test_assert( errno == EACCES );
0130
0131 puts( "Allocate most of heap with a little bit left" );
0132 opaque = rtems_heap_greedy_allocate( some_blocks, 1 );
0133
0134 puts( "Create an empty file.");
0135 status = mknod( "/foo", S_IFREG | S_IRWXU, 0LL );
0136 rtems_test_assert( status == 0 );
0137
0138 puts( "Then increase it's size to more than remaining space" );
0139 fd = open( "/foo", O_WRONLY | O_TRUNC);
0140 rtems_test_assert( fd >= 0 );
0141 status = write(fd, some_data, sizeof(some_data));
0142 rtems_test_assert( status == -1);
0143 rtems_test_assert( errno == ENOSPC );
0144
0145 puts( "Clean up again" );
0146 status = close(fd);
0147 rtems_test_assert( status == 0);
0148 status = remove( "/foo" );
0149 rtems_test_assert( status == 0);
0150 rtems_heap_greedy_free( opaque );
0151
0152 puts( "Allocate most of heap" );
0153 opaque = rtems_heap_greedy_allocate( mount_table_entry_size, 1 );
0154
0155 printf( "Attempt to mount a fs at %s -- expect ENOMEM", mount_point );
0156 status = mount( NULL,
0157 mount_point,
0158 fs_type,
0159 RTEMS_FILESYSTEM_READ_WRITE,
0160 NULL );
0161 rtems_test_assert( status == -1 );
0162 rtems_test_assert( errno == ENOMEM );
0163
0164 puts( "Freeing allocated memory" );
0165 rtems_heap_greedy_free( opaque );
0166
0167 puts( "Changing directory to /" );
0168 status = chdir( "/" );
0169 rtems_test_assert( status == 0 );
0170
0171 puts( "Allocate most of heap" );
0172 opaque = rtems_heap_greedy_allocate( NULL, 0 );
0173
0174 puts( "Attempt to create /node-link-2 for /node -- expect ENOMEM" );
0175 status = link( "/node", "/node-link-2" );
0176 rtems_test_assert( status == -1 );
0177 rtems_test_assert( errno == ENOMEM );
0178
0179 puts( "Attempt to create /node-slink-2 for /node -- expect ENOMEM" );
0180 status = symlink( "/node", "node-slink-2" );
0181 rtems_test_assert( status == -1 );
0182 rtems_test_assert( errno == ENOMEM );
0183
0184 puts( "Freeing allocated memory" );
0185 rtems_heap_greedy_free( opaque );
0186
0187 puts( "Allocate most of heap" );
0188 opaque = rtems_heap_greedy_allocate( slink_2_name_size, 1 );
0189
0190 printf( "Attempt to create %s for /node -- expect ENOMEM", slink_2_name );
0191 status = symlink( "/node", slink_2_name );
0192 rtems_test_assert( status == -1 );
0193 rtems_test_assert( errno == ENOMEM );
0194
0195 puts( "Freeing allocated memory" );
0196 rtems_heap_greedy_free( opaque );
0197
0198 puts( "Attempt to stat a hardlink" );
0199 status = lstat( "/node-link", &stat_buf );
0200 rtems_test_assert( status == 0 );
0201
0202 puts( "Changing euid to 10" );
0203 status = seteuid( 10 );
0204 rtems_test_assert( status == 0 );
0205
0206 puts( "Attempt chmod on /node -- expect EPERM" );
0207 status = chmod( "/node", S_IRUSR );
0208 rtems_test_assert( status == -1 );
0209 rtems_test_assert( errno == EPERM );
0210
0211 puts( "Attempt chown on /node -- expect EPERM" );
0212 status = chown( "/node", 10, 10 );
0213 rtems_test_assert( status == -1 );
0214 rtems_test_assert( errno == EPERM );
0215
0216 puts( "Changing euid back to 0 [root]" );
0217 status = seteuid( 0 );
0218 rtems_test_assert( status == 0 );
0219
0220 puts( "Creating a fifo -- OK" );
0221 status = mkfifo( "/fifo", S_IRWXU );
0222 rtems_test_assert( status == 0 );
0223
0224 puts( "chown /fifo to 10 -- OK" );
0225 status = chown( "/fifo", 10, 10 );
0226 rtems_test_assert( status == 0 );
0227
0228 puts( "Changing euid to 10" );
0229 status = seteuid( 10 );
0230 rtems_test_assert( status == 0 );
0231
0232 puts( "chmod /fifo -- OK" );
0233 status = chmod( "/fifo", S_IRWXU );
0234 rtems_test_assert( status == 0 );
0235
0236 printf( "chown /fifo to %o -- OK\n", 0 );
0237 status = chown( "/fifo", 0, 0 );
0238 rtems_test_assert( status == 0 );
0239
0240 TEST_END();
0241 rtems_test_exit(0);
0242 }
0243
0244
0245
0246 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0247 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0248
0249 #define CONFIGURE_FILESYSTEM_IMFS
0250
0251 #define CONFIGURE_MAXIMUM_TASKS 1
0252 #define CONFIGURE_IMFS_MEMFILE_BYTES_PER_BLOCK MEMFILE_BYTES_PER_BLOCK
0253 #define CONFIGURE_IMFS_ENABLE_MKFIFO
0254 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 4
0255 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0256
0257 #define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT
0258 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0259
0260 #define CONFIGURE_INIT
0261
0262 #include <rtems/confdefs.h>
0263