]>
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
)
26 void * my_shm_addr
= NULL
;
27 struct shmid_ds my_shmid_ds
;
29 my_shm_id
= shmget( IPC_PRIVATE
, 4096, (IPC_CREAT
| IPC_R
| IPC_W
) );
30 if ( my_shm_id
== -1 ) {
31 printf( "shmget failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
32 goto test_failed_exit
;
35 my_shm_addr
= shmat( my_shm_id
, NULL
, SHM_RND
);
36 if ( my_shm_addr
== (void *) -1 ) {
38 printf( "shmat failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
39 goto test_failed_exit
;
42 /* try writing to the shared segment */
43 *((char *) my_shm_addr
) = 'A';
45 my_err
= shmctl( my_shm_id
, IPC_STAT
, &my_shmid_ds
);
47 printf( "shmctl failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
48 goto test_failed_exit
;
50 if ( my_shmid_ds
.shm_segsz
!= 4096 ) {
51 printf( "shmctl failed get correct shared segment size \n" );
52 goto test_failed_exit
;
54 if ( getpid( ) != my_shmid_ds
.shm_cpid
) {
55 printf( "shmctl failed get correct creator pid \n" );
56 goto test_failed_exit
;
59 my_err
= shmdt( my_shm_addr
);
61 printf( "shmdt failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
62 goto test_failed_exit
;
67 goto test_passed_exit
;
73 if ( my_shm_addr
!= NULL
) {
80 /* **************************************************************************************************************
81 * Test BSD shared memory system calls.
82 * **************************************************************************************************************
84 int bsd_shm_tests( void * the_argp
)
88 char * my_addr
= NULL
;
91 for ( i
= 0; i
< 100; i
++ ) {
92 sprintf( &my_name
[0], "bsd_shm_tests_%d", i
);
93 my_fd
= shm_open( &my_name
[0], (O_RDWR
| O_CREAT
| O_EXCL
), S_IRWXU
);
97 if ( my_err
!= EEXIST
) {
98 printf( "shm_open failed with error %d - \"%s\" \n", my_err
, strerror( my_err
) );
99 goto test_failed_exit
;
103 printf( "shm_open failed to open a shared memory object with name \"%s\" \n", &my_name
[0] );
104 goto test_failed_exit
;
107 /* grow shared memory object */
108 my_err
= ftruncate( my_fd
, 4096 );
109 if ( my_err
== -1 ) {
110 printf( "ftruncate call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
111 goto test_failed_exit
;
114 my_err
= shm_unlink( &my_name
[0] );
115 if ( my_err
== -1 ) {
116 printf( "shm_unlink failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
117 goto test_failed_exit
;
120 my_addr
= (char *) mmap( NULL
, 4096, (PROT_READ
| PROT_WRITE
), (MAP_FILE
| MAP_SHARED
), my_fd
, 0 );
121 if ( my_addr
== (char *) -1 ) {
122 printf( "mmap call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
123 goto test_failed_exit
;
127 goto test_passed_exit
;