]> git.saurik.com Git - apple/xnu.git/blob - tools/tests/xnu_quick_test/shared_memory_tests.c
xnu-1228.5.18.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 my_shm_addr = NULL;
66
67 my_err = 0;
68 goto test_passed_exit;
69
70 test_failed_exit:
71 my_err = -1;
72
73 test_passed_exit:
74 if ( my_shm_addr != NULL ) {
75 shmdt( my_shm_addr );
76 }
77 return( my_err );
78 #else
79 printf( "\t--> Not supported on EMBEDDED TARGET\n" );
80 return 0;
81 #endif
82 }
83
84
85 /* **************************************************************************************************************
86 * Test BSD shared memory system calls.
87 * **************************************************************************************************************
88 */
89 int bsd_shm_tests( void * the_argp )
90 {
91 int my_err, i;
92 int my_fd = -1;
93 char * my_addr = NULL;
94 char my_name[ 64 ];
95
96 for ( i = 0; i < 100; i++ ) {
97 sprintf( &my_name[0], "bsd_shm_tests_%d", i );
98 my_fd = shm_open( &my_name[0], (O_RDWR | O_CREAT | O_EXCL), S_IRWXU );
99 if ( my_fd != -1 )
100 break;
101 my_err = errno;
102 if ( my_err != EEXIST ) {
103 printf( "shm_open failed with error %d - \"%s\" \n", my_err, strerror( my_err) );
104 goto test_failed_exit;
105 }
106 }
107 if ( my_fd == -1 ) {
108 printf( "shm_open failed to open a shared memory object with name \"%s\" \n", &my_name[0] );
109 goto test_failed_exit;
110 }
111
112 /* grow shared memory object */
113 my_err = ftruncate( my_fd, 4096 );
114 if ( my_err == -1 ) {
115 printf( "ftruncate call failed with error %d - \"%s\" \n", errno, strerror( errno) );
116 goto test_failed_exit;
117 }
118
119 my_err = shm_unlink( &my_name[0] );
120 if ( my_err == -1 ) {
121 printf( "shm_unlink failed with error %d - \"%s\" \n", errno, strerror( errno) );
122 goto test_failed_exit;
123 }
124
125 my_addr = (char *) mmap( NULL, 4096, (PROT_READ | PROT_WRITE), (MAP_FILE | MAP_SHARED), my_fd, 0 );
126 if ( my_addr == (char *) -1 ) {
127 printf( "mmap call failed with error %d - \"%s\" \n", errno, strerror( errno) );
128 goto test_failed_exit;
129 }
130
131 my_err = 0;
132 goto test_passed_exit;
133
134 test_failed_exit:
135 my_err = -1;
136
137 test_passed_exit:
138 if ( my_fd != -1 )
139 close( my_fd );
140 return( my_err );
141 }
142