]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/xnu_quick_test/shared_memory_tests.c
   2  *  shared_memory_tests.c 
   5  *  Created by Jerry Cottingham on 6/2/2005. 
   6  *  Copyright 2005 Apple Computer Inc. All rights reserved. 
  15 extern char  g_target_path
[ PATH_MAX 
]; 
  18 /*  ************************************************************************************************************** 
  19  *      Test shmat, shmctl, shmdt, shmget system calls. 
  20  *  ************************************************************************************************************** 
  22 int shm_tests( void * the_argp 
) 
  24 #if !TARGET_OS_EMBEDDED 
  27         void *                          my_shm_addr 
= NULL
; 
  28         struct shmid_ds         my_shmid_ds
; 
  30         my_shm_id 
= shmget( IPC_PRIVATE
, 4096, (IPC_CREAT 
| IPC_R 
| IPC_W
) ); 
  31         if ( my_shm_id 
== -1 ) { 
  32                 printf( "shmget failed with error %d - \"%s\" \n", errno
, strerror( errno
) ); 
  33                 goto test_failed_exit
; 
  36         my_shm_addr 
= shmat( my_shm_id
, NULL
, SHM_RND 
); 
  37         if ( my_shm_addr 
== (void *) -1 ) { 
  39                 printf( "shmat failed with error %d - \"%s\" \n", errno
, strerror( errno
) ); 
  40                 goto test_failed_exit
; 
  43         /* try writing to the shared segment */ 
  44         *((char *) my_shm_addr
) = 'A'; 
  46         my_err 
= shmctl( my_shm_id
, IPC_STAT
, &my_shmid_ds 
); 
  48                 printf( "shmctl failed with error %d - \"%s\" \n", errno
, strerror( errno
) ); 
  49                 goto test_failed_exit
; 
  51         if ( my_shmid_ds
.shm_segsz 
!= 4096 ) { 
  52                 printf( "shmctl failed get correct shared segment size \n" ); 
  53                 goto test_failed_exit
; 
  55         if ( getpid( ) != my_shmid_ds
.shm_cpid 
) { 
  56                 printf( "shmctl failed get correct creator pid \n" ); 
  57                 goto test_failed_exit
; 
  60         my_err 
= shmdt( my_shm_addr 
); 
  62                 printf( "shmdt failed with error %d - \"%s\" \n", errno
, strerror( errno
) ); 
  63                 goto test_failed_exit
; 
  66         my_err 
= shmctl( my_shm_id
, IPC_RMID
, NULL 
); 
  68                 printf("shmctl failed to delete memory segment.\n"); 
  69                 goto test_failed_exit
; 
  75         goto test_passed_exit
; 
  81         if ( my_shm_addr 
!= NULL 
) { 
  83                 shmctl( my_shm_id
, IPC_RMID
, NULL
); 
  87         printf( "\t--> Not supported on EMBEDDED TARGET\n" ); 
  93 /*  ************************************************************************************************************** 
  94  *      Test BSD shared memory system calls. 
  95  *  ************************************************************************************************************** 
  97 int bsd_shm_tests( void * the_argp 
) 
 101         char *          my_addr 
= NULL
; 
 104         for ( i 
= 0; i 
< 100; i
++ ) { 
 105                 sprintf( &my_name
[0], "bsd_shm_tests_%d", i 
); 
 106                 my_fd 
= shm_open( &my_name
[0], (O_RDWR 
| O_CREAT 
| O_EXCL
), S_IRWXU 
); 
 110                 if ( my_err 
!= EEXIST 
) { 
 111                         printf( "shm_open failed with error %d - \"%s\" \n", my_err
, strerror( my_err
) ); 
 112                         goto test_failed_exit
; 
 116                 printf( "shm_open failed to open a shared memory object with name \"%s\" \n", &my_name
[0] ); 
 117                 goto test_failed_exit
; 
 120         /* grow shared memory object */ 
 121         my_err 
= ftruncate( my_fd
, 4096 );               
 122         if ( my_err 
== -1 ) { 
 123                 printf( "ftruncate call failed with error %d - \"%s\" \n", errno
, strerror( errno
) ); 
 124                 goto test_failed_exit
; 
 127         my_err 
= shm_unlink( &my_name
[0] ); 
 128         if ( my_err 
== -1 ) { 
 129                 printf( "shm_unlink failed with error %d - \"%s\" \n", errno
, strerror( errno
) ); 
 130                 goto test_failed_exit
; 
 133         /* unlinking a non existent path */ 
 134         my_err 
= shm_unlink ( "/tmp/anonexistent_shm_oject" ); 
 136         if ( my_err 
!= ENOENT 
) { 
 137                 printf( "shm_unlink of non existent path failed with error %d - \"%s\" \n", errno
, strerror( errno
) ); 
 138                 goto test_failed_exit
; 
 141         my_addr 
= (char *) mmap( NULL
, 4096, (PROT_READ 
| PROT_WRITE
), (MAP_FILE 
| MAP_SHARED
), my_fd
, 0 ); 
 142         if ( my_addr 
== (char *) -1 ) { 
 143                 printf( "mmap call failed with error %d - \"%s\" \n", errno
, strerror( errno
) ); 
 144                 goto test_failed_exit
; 
 148         goto test_passed_exit
;