]> git.saurik.com Git - apple/xnu.git/blob - tools/tests/xnu_quick_test/shared_memory_tests.c
xnu-2422.90.20.tar.gz
[apple/xnu.git] / tools / tests / xnu_quick_test / shared_memory_tests.c
1 /*
2 * shared_memory_tests.c
3 * xnu_quick_test
4 *
5 * Created by Jerry Cottingham on 6/2/2005.
6 * Copyright 2005 Apple Computer Inc. All rights reserved.
7 *
8 */
9
10 #include "tests.h"
11 #include <sys/ipc.h>
12 #include <sys/mman.h>
13 #include <sys/shm.h>
14
15 extern char g_target_path[ PATH_MAX ];
16
17
18 /* **************************************************************************************************************
19 * Test shmat, shmctl, shmdt, shmget system calls.
20 * **************************************************************************************************************
21 */
22 int shm_tests( void * the_argp )
23 {
24 #if !TARGET_OS_EMBEDDED
25 int my_err;
26 int my_shm_id;
27 void * my_shm_addr = NULL;
28 struct shmid_ds my_shmid_ds;
29
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;
34 }
35
36 my_shm_addr = shmat( my_shm_id, NULL, SHM_RND );
37 if ( my_shm_addr == (void *) -1 ) {
38 my_shm_addr = NULL;
39 printf( "shmat failed with error %d - \"%s\" \n", errno, strerror( errno) );
40 goto test_failed_exit;
41 }
42
43 /* try writing to the shared segment */
44 *((char *) my_shm_addr) = 'A';
45
46 my_err = shmctl( my_shm_id, IPC_STAT, &my_shmid_ds );
47 if ( my_err == -1 ) {
48 printf( "shmctl failed with error %d - \"%s\" \n", errno, strerror( errno) );
49 goto test_failed_exit;
50 }
51 if ( my_shmid_ds.shm_segsz != 4096 ) {
52 printf( "shmctl failed get correct shared segment size \n" );
53 goto test_failed_exit;
54 }
55 if ( getpid( ) != my_shmid_ds.shm_cpid ) {
56 printf( "shmctl failed get correct creator pid \n" );
57 goto test_failed_exit;
58 }
59
60 my_err = shmdt( my_shm_addr );
61 if ( my_err == -1 ) {
62 printf( "shmdt failed with error %d - \"%s\" \n", errno, strerror( errno) );
63 goto test_failed_exit;
64 }
65
66 my_err = shmctl( my_shm_id, IPC_RMID, NULL );
67 if ( my_err == -1 ) {
68 printf("shmctl failed to delete memory segment.\n");
69 goto test_failed_exit;
70 }
71
72 my_shm_addr = NULL;
73
74 my_err = 0;
75 goto test_passed_exit;
76
77 test_failed_exit:
78 my_err = -1;
79
80 test_passed_exit:
81 if ( my_shm_addr != NULL ) {
82 shmdt( my_shm_addr );
83 shmctl( my_shm_id, IPC_RMID, NULL);
84 }
85 return( my_err );
86 #else
87 printf( "\t--> Not supported on EMBEDDED TARGET\n" );
88 return 0;
89 #endif
90 }
91
92
93 /* **************************************************************************************************************
94 * Test BSD shared memory system calls.
95 * **************************************************************************************************************
96 */
97 int bsd_shm_tests( void * the_argp )
98 {
99 int my_err, i;
100 int my_fd = -1;
101 char * my_addr = NULL;
102 char my_name[ 64 ];
103
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 );
107 if ( my_fd != -1 )
108 break;
109 my_err = errno;
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;
113 }
114 }
115 if ( my_fd == -1 ) {
116 printf( "shm_open failed to open a shared memory object with name \"%s\" \n", &my_name[0] );
117 goto test_failed_exit;
118 }
119
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;
125 }
126
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;
131 }
132
133 /* unlinking a non existent path */
134 my_err = shm_unlink ( "/tmp/anonexistent_shm_oject" );
135 my_err = errno;
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;
139 }
140
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;
145 }
146
147 my_err = 0;
148 goto test_passed_exit;
149
150 test_failed_exit:
151 my_err = -1;
152
153 test_passed_exit:
154 if ( my_fd != -1 )
155 close( my_fd );
156 return( my_err );
157 }
158